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

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.48! root     9055:                // the buffer is flushed but no input is attempted
1.1       root     9056:                break;
                   9057:        }
                   9058: }
                   9059: 
                   9060: inline void msdos_int_21h_0dh()
                   9061: {
                   9062: }
                   9063: 
                   9064: inline void msdos_int_21h_0eh()
                   9065: {
                   9066:        if(REG8(DL) < 26) {
                   9067:                _chdrive(REG8(DL) + 1);
                   9068:                msdos_cds_update(REG8(DL));
1.1.1.23  root     9069:                msdos_sda_update(current_psp);
1.1       root     9070:        }
                   9071:        REG8(AL) = 26; // zdrive
                   9072: }
                   9073: 
1.1.1.14  root     9074: inline void msdos_int_21h_0fh()
                   9075: {
                   9076:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9077:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     9078:        const char *path = msdos_fcb_path(fcb);
1.1.1.14  root     9079:        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     9080:        
1.1.1.14  root     9081:        if(hFile == INVALID_HANDLE_VALUE) {
                   9082:                REG8(AL) = 0xff;
                   9083:        } else {
                   9084:                REG8(AL) = 0;
                   9085:                fcb->current_block = 0;
                   9086:                fcb->record_size = 128;
                   9087:                fcb->file_size = GetFileSize(hFile, NULL);
                   9088:                fcb->handle = hFile;
                   9089:                fcb->cur_record = 0;
                   9090:        }
                   9091: }
                   9092: 
                   9093: inline void msdos_int_21h_10h()
                   9094: {
                   9095:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9096:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9097:        
                   9098:        REG8(AL) = CloseHandle(fcb->handle) ? 0 : 0xff;
                   9099: }
                   9100: 
1.1       root     9101: inline void msdos_int_21h_11h()
                   9102: {
1.1.1.3   root     9103:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9104:        fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9105:        
                   9106:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     9107:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9108:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   9109:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1.1.45  root     9110:        const char *path = msdos_fcb_path(fcb);
1.1       root     9111:        WIN32_FIND_DATA fd;
                   9112:        
1.1.1.13  root     9113:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   9114:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   9115:                FindClose(dtainfo->find_handle);
                   9116:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9117:        }
                   9118:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     9119:        dtainfo->allowable_mask = (ext_fcb->flag == 0xff) ? ext_fcb->attribute : 0x20;
                   9120:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     9121:        
1.1.1.14  root     9122:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   9123:                dtainfo->allowable_mask &= ~8;
1.1       root     9124:        }
1.1.1.14  root     9125:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   9126:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     9127:                      !msdos_find_file_has_8dot3name(&fd)) {
                   9128:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   9129:                                FindClose(dtainfo->find_handle);
                   9130:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9131:                                break;
                   9132:                        }
                   9133:                }
                   9134:        }
1.1.1.13  root     9135:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     9136:                if(ext_fcb->flag == 0xff) {
                   9137:                        ext_find->flag = 0xff;
                   9138:                        memset(ext_find->reserved, 0, 5);
                   9139:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9140:                }
                   9141:                find->drive = _getdrive();
1.1.1.13  root     9142:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     9143:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9144:                find->nt_res = 0;
                   9145:                msdos_find_file_conv_local_time(&fd);
                   9146:                find->create_time_ms = 0;
                   9147:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9148:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9149:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9150:                find->cluster_hi = find->cluster_lo = 0;
                   9151:                find->file_size = fd.nFileSizeLow;
                   9152:                REG8(AL) = 0x00;
1.1.1.14  root     9153:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     9154:                if(ext_fcb->flag == 0xff) {
                   9155:                        ext_find->flag = 0xff;
                   9156:                        memset(ext_find->reserved, 0, 5);
                   9157:                        ext_find->attribute = 8;
                   9158:                }
                   9159:                find->drive = _getdrive();
                   9160:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   9161:                find->attribute = 8;
                   9162:                find->nt_res = 0;
                   9163:                msdos_find_file_conv_local_time(&fd);
                   9164:                find->create_time_ms = 0;
                   9165:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9166:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9167:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9168:                find->cluster_hi = find->cluster_lo = 0;
                   9169:                find->file_size = 0;
1.1.1.14  root     9170:                dtainfo->allowable_mask &= ~8;
1.1       root     9171:                REG8(AL) = 0x00;
                   9172:        } else {
                   9173:                REG8(AL) = 0xff;
                   9174:        }
                   9175: }
                   9176: 
                   9177: inline void msdos_int_21h_12h()
                   9178: {
1.1.1.3   root     9179:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1.1.14  root     9180: //     fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9181:        
                   9182:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     9183:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9184:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   9185:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9186:        WIN32_FIND_DATA fd;
                   9187:        
1.1.1.13  root     9188:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   9189:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   9190:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     9191:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     9192:                              !msdos_find_file_has_8dot3name(&fd)) {
                   9193:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   9194:                                        FindClose(dtainfo->find_handle);
                   9195:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9196:                                        break;
                   9197:                                }
                   9198:                        }
                   9199:                } else {
1.1.1.13  root     9200:                        FindClose(dtainfo->find_handle);
                   9201:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9202:                }
                   9203:        }
1.1.1.13  root     9204:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     9205:                if(ext_fcb->flag == 0xff) {
                   9206:                        ext_find->flag = 0xff;
                   9207:                        memset(ext_find->reserved, 0, 5);
                   9208:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9209:                }
                   9210:                find->drive = _getdrive();
1.1.1.13  root     9211:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     9212:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9213:                find->nt_res = 0;
                   9214:                msdos_find_file_conv_local_time(&fd);
                   9215:                find->create_time_ms = 0;
                   9216:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9217:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9218:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9219:                find->cluster_hi = find->cluster_lo = 0;
                   9220:                find->file_size = fd.nFileSizeLow;
                   9221:                REG8(AL) = 0x00;
1.1.1.14  root     9222:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     9223:                if(ext_fcb->flag == 0xff) {
                   9224:                        ext_find->flag = 0xff;
                   9225:                        memset(ext_find->reserved, 0, 5);
                   9226:                        ext_find->attribute = 8;
                   9227:                }
                   9228:                find->drive = _getdrive();
                   9229:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   9230:                find->attribute = 8;
                   9231:                find->nt_res = 0;
                   9232:                msdos_find_file_conv_local_time(&fd);
                   9233:                find->create_time_ms = 0;
                   9234:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9235:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9236:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9237:                find->cluster_hi = find->cluster_lo = 0;
                   9238:                find->file_size = 0;
1.1.1.14  root     9239:                dtainfo->allowable_mask &= ~8;
1.1       root     9240:                REG8(AL) = 0x00;
                   9241:        } else {
                   9242:                REG8(AL) = 0xff;
                   9243:        }
                   9244: }
                   9245: 
                   9246: inline void msdos_int_21h_13h()
                   9247: {
1.1.1.3   root     9248:        if(remove(msdos_fcb_path((fcb_t *)(mem + SREG_BASE(DS) + REG16(DX))))) {
1.1       root     9249:                REG8(AL) = 0xff;
                   9250:        } else {
                   9251:                REG8(AL) = 0x00;
                   9252:        }
                   9253: }
                   9254: 
1.1.1.16  root     9255: inline void msdos_int_21h_14h()
                   9256: {
                   9257:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9258:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9259:        process_t *process = msdos_process_info_get(current_psp);
                   9260:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9261:        DWORD num = 0;
                   9262:        
                   9263:        memset(mem + dta_laddr, 0, fcb->record_size);
                   9264:        if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   9265:                REG8(AL) = 1;
                   9266:        } else {
                   9267:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   9268:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   9269:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   9270:                REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   9271:        }
                   9272: }
                   9273: 
                   9274: inline void msdos_int_21h_15h()
1.1.1.14  root     9275: {
                   9276:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9277:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.16  root     9278:        process_t *process = msdos_process_info_get(current_psp);
                   9279:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9280:        DWORD num = 0;
1.1.1.14  root     9281:        
1.1.1.16  root     9282:        if(!WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   9283:                REG8(AL) = 1;
                   9284:        } else {
                   9285:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   9286:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   9287:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   9288:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   9289:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   9290:        }
                   9291: }
                   9292: 
                   9293: inline void msdos_int_21h_16h()
                   9294: {
                   9295:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9296:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     9297:        const char *path = msdos_fcb_path(fcb);
1.1.1.14  root     9298:        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     9299:        
1.1.1.14  root     9300:        if(hFile == INVALID_HANDLE_VALUE) {
                   9301:                REG8(AL) = 0xff;
                   9302:        } else {
                   9303:                REG8(AL) = 0;
                   9304:                fcb->current_block = 0;
                   9305:                fcb->record_size = 128;
                   9306:                fcb->file_size = 0;
                   9307:                fcb->handle = hFile;
                   9308:                fcb->cur_record = 0;
                   9309:        }
                   9310: }
                   9311: 
1.1.1.16  root     9312: inline void msdos_int_21h_17h()
                   9313: {
                   9314:        ext_fcb_t *ext_fcb_src = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9315:        fcb_t *fcb_src = (fcb_t *)(ext_fcb_src + (ext_fcb_src->flag == 0xff ? 1 : 0));
1.1.1.45  root     9316: //     const char *path_src = msdos_fcb_path(fcb_src);
                   9317:        char path_src[MAX_PATH];
                   9318:        strcpy(path_src, msdos_fcb_path(fcb_src));
                   9319:        
1.1.1.16  root     9320:        ext_fcb_t *ext_fcb_dst = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + 16);
                   9321:        fcb_t *fcb_dst = (fcb_t *)(ext_fcb_dst + (ext_fcb_dst->flag == 0xff ? 1 : 0));
1.1.1.45  root     9322: //     const char *path_dst = msdos_fcb_path(fcb_dst);
                   9323:        char path_dst[MAX_PATH];
                   9324:        strcpy(path_dst, msdos_fcb_path(fcb_dst));
1.1.1.16  root     9325:        
                   9326:        if(rename(path_src, path_dst)) {
                   9327:                REG8(AL) = 0xff;
                   9328:        } else {
                   9329:                REG8(AL) = 0;
                   9330:        }
                   9331: }
                   9332: 
1.1       root     9333: inline void msdos_int_21h_18h()
                   9334: {
                   9335:        REG8(AL) = 0x00;
                   9336: }
                   9337: 
                   9338: inline void msdos_int_21h_19h()
                   9339: {
                   9340:        REG8(AL) = _getdrive() - 1;
                   9341: }
                   9342: 
                   9343: inline void msdos_int_21h_1ah()
                   9344: {
                   9345:        process_t *process = msdos_process_info_get(current_psp);
                   9346:        
                   9347:        process->dta.w.l = REG16(DX);
1.1.1.3   root     9348:        process->dta.w.h = SREG(DS);
1.1.1.23  root     9349:        msdos_sda_update(current_psp);
1.1       root     9350: }
                   9351: 
                   9352: inline void msdos_int_21h_1bh()
                   9353: {
                   9354:        int drive_num = _getdrive() - 1;
                   9355:        UINT16 seg, ofs;
                   9356:        
                   9357:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9358:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   9359:                REG8(AL) = dpb->highest_sector_num + 1;
                   9360:                REG16(CX) = dpb->bytes_per_sector;
                   9361:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     9362:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     9363:        } else {
                   9364:                REG8(AL) = 0xff;
1.1.1.3   root     9365:                m_CF = 1;
1.1       root     9366:        }
                   9367: 
                   9368: }
                   9369: 
                   9370: inline void msdos_int_21h_1ch()
                   9371: {
1.1.1.41  root     9372:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
1.1       root     9373:        UINT16 seg, ofs;
                   9374:        
                   9375:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9376:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   9377:                REG8(AL) = dpb->highest_sector_num + 1;
                   9378:                REG16(CX) = dpb->bytes_per_sector;
                   9379:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     9380:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     9381:        } else {
                   9382:                REG8(AL) = 0xff;
1.1.1.3   root     9383:                m_CF = 1;
1.1       root     9384:        }
                   9385: 
                   9386: }
                   9387: 
                   9388: inline void msdos_int_21h_1dh()
                   9389: {
                   9390:        REG8(AL) = 0;
                   9391: }
                   9392: 
                   9393: inline void msdos_int_21h_1eh()
                   9394: {
                   9395:        REG8(AL) = 0;
                   9396: }
                   9397: 
                   9398: inline void msdos_int_21h_1fh()
                   9399: {
                   9400:        int drive_num = _getdrive() - 1;
                   9401:        UINT16 seg, ofs;
                   9402:        
                   9403:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9404:                REG8(AL) = 0;
1.1.1.3   root     9405:                SREG(DS) = seg;
                   9406:                i386_load_segment_descriptor(DS);
1.1       root     9407:                REG16(BX) = ofs;
                   9408:        } else {
                   9409:                REG8(AL) = 0xff;
1.1.1.3   root     9410:                m_CF = 1;
1.1       root     9411:        }
                   9412: }
                   9413: 
                   9414: inline void msdos_int_21h_20h()
                   9415: {
                   9416:        REG8(AL) = 0;
                   9417: }
                   9418: 
1.1.1.14  root     9419: inline void msdos_int_21h_21h()
                   9420: {
                   9421:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9422:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9423:        
                   9424:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   9425:                REG8(AL) = 1;
                   9426:        } else {
                   9427:                process_t *process = msdos_process_info_get(current_psp);
                   9428:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9429:                memset(mem + dta_laddr, 0, fcb->record_size);
1.1.1.16  root     9430:                DWORD num = 0;
1.1.1.14  root     9431:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   9432:                        REG8(AL) = 1;
                   9433:                } else {
                   9434:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   9435:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     9436:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
1.1.1.14  root     9437:                }
                   9438:        }
                   9439: }
                   9440: 
                   9441: inline void msdos_int_21h_22h()
                   9442: {
                   9443:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9444:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9445:        
                   9446:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   9447:                REG8(AL) = 0xff;
                   9448:        } else {
                   9449:                process_t *process = msdos_process_info_get(current_psp);
                   9450:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
1.1.1.16  root     9451:                DWORD num = 0;
1.1.1.14  root     9452:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL);
                   9453:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   9454:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   9455:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     9456:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
1.1.1.14  root     9457:        }
                   9458: }
                   9459: 
1.1.1.16  root     9460: inline void msdos_int_21h_23h()
                   9461: {
                   9462:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9463:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     9464:        const char *path = msdos_fcb_path(fcb);
1.1.1.16  root     9465:        HANDLE hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   9466:        
                   9467:        if(hFile == INVALID_HANDLE_VALUE) {
                   9468:                REG8(AL) = 0xff;
                   9469:        } else {
                   9470:                UINT32 size = GetFileSize(hFile, NULL);
                   9471:                fcb->rand_record = size / fcb->record_size + ((size % fcb->record_size) != 0);
                   9472:                REG8(AL) = 0;
                   9473:        }
                   9474: }
                   9475: 
                   9476: inline void msdos_int_21h_24h()
                   9477: {
                   9478:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9479:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9480:        
                   9481:        fcb->rand_record = fcb->current_block * fcb->record_size + fcb->cur_record;
                   9482: }
                   9483: 
1.1       root     9484: inline void msdos_int_21h_25h()
                   9485: {
                   9486:        *(UINT16 *)(mem + 4 * REG8(AL) + 0) = REG16(DX);
1.1.1.3   root     9487:        *(UINT16 *)(mem + 4 * REG8(AL) + 2) = SREG(DS);
1.1       root     9488: }
                   9489: 
                   9490: inline void msdos_int_21h_26h()
                   9491: {
                   9492:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   9493:        
                   9494:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
1.1.1.48! root     9495:        psp->first_mcb = REG16(DX) + (PSP_SIZE >> 4);
1.1       root     9496:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   9497:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   9498:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   9499:        psp->parent_psp = 0;
                   9500: }
                   9501: 
1.1.1.16  root     9502: inline void msdos_int_21h_27h()
                   9503: {
                   9504:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9505:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9506:        
                   9507:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   9508:                REG8(AL) = 1;
                   9509:        } else {
                   9510:                process_t *process = msdos_process_info_get(current_psp);
                   9511:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9512:                memset(mem + dta_laddr, 0, fcb->record_size * REG16(CX));
                   9513:                DWORD num = 0;
                   9514:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL) || num == 0) {
                   9515:                        REG8(AL) = 1;
                   9516:                } else {
                   9517:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   9518:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   9519:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   9520:                        REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   9521:                }
                   9522:        }
                   9523: }
                   9524: 
                   9525: inline void msdos_int_21h_28h()
                   9526: {
                   9527:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9528:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9529:        
                   9530:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   9531:                REG8(AL) = 0xff;
                   9532:        } else {
                   9533:                process_t *process = msdos_process_info_get(current_psp);
                   9534:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9535:                DWORD num = 0;
                   9536:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL);
                   9537:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   9538:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   9539:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   9540:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   9541:                REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   9542:        }
                   9543: }
                   9544: 
1.1       root     9545: inline void msdos_int_21h_29h()
                   9546: {
1.1.1.20  root     9547:        int ofs = 0;//SREG_BASE(DS) + REG16(SI);
                   9548:        char buffer[1024], name[MAX_PATH], ext[MAX_PATH];
1.1       root     9549:        UINT8 drv = 0;
                   9550:        char sep_chars[] = ":.;,=+";
                   9551:        char end_chars[] = "\\<>|/\"[]";
                   9552:        char spc_chars[] = " \t";
                   9553:        
1.1.1.20  root     9554:        memcpy(buffer, mem + SREG_BASE(DS) + REG16(SI), 1023);
                   9555:        buffer[1023] = 0;
                   9556:        memset(name, 0x20, sizeof(name));
                   9557:        memset(ext, 0x20, sizeof(ext));
                   9558:        
1.1       root     9559:        if(REG8(AL) & 1) {
1.1.1.20  root     9560:                ofs += strspn((char *)(buffer + ofs), spc_chars);
                   9561:                if(my_strchr(sep_chars, buffer[ofs]) && buffer[ofs] != '\0') {
1.1       root     9562:                        ofs++;
                   9563:                }
                   9564:        }
1.1.1.20  root     9565:        ofs += strspn((char *)(buffer + ofs), spc_chars);
1.1       root     9566:        
1.1.1.24  root     9567:        if(buffer[ofs + 1] == ':') {
                   9568:                if(buffer[ofs] >= 'a' && buffer[ofs] <= 'z') {
                   9569:                        drv = buffer[ofs] - 'a' + 1;
1.1.1.20  root     9570:                        ofs += 2;
1.1.1.24  root     9571:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   9572:                                ofs++;
                   9573:                        }
                   9574:                } else if(buffer[ofs] >= 'A' && buffer[ofs] <= 'Z') {
                   9575:                        drv = buffer[ofs] - 'A' + 1;
1.1       root     9576:                        ofs += 2;
1.1.1.24  root     9577:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   9578:                                ofs++;
                   9579:                        }
1.1       root     9580:                }
                   9581:        }
1.1.1.20  root     9582:        for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   9583:                UINT8 c = buffer[ofs];
                   9584:                if(is_kanji) {
                   9585:                        is_kanji = 0;
                   9586:                } else if(msdos_lead_byte_check(c)) {
                   9587:                        is_kanji = 1;
                   9588:                } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     9589:                        break;
                   9590:                } else if(c >= 'a' && c <= 'z') {
                   9591:                        c -= 0x20;
                   9592:                }
                   9593:                ofs++;
                   9594:                name[i] = c;
                   9595:        }
1.1.1.20  root     9596:        if(buffer[ofs] == '.') {
1.1       root     9597:                ofs++;
1.1.1.20  root     9598:                for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   9599:                        UINT8 c = buffer[ofs];
                   9600:                        if(is_kanji) {
                   9601:                                is_kanji = 0;
                   9602:                        } else if(msdos_lead_byte_check(c)) {
                   9603:                                is_kanji = 1;
                   9604:                        } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     9605:                                break;
                   9606:                        } else if(c >= 'a' && c <= 'z') {
                   9607:                                c -= 0x20;
                   9608:                        }
                   9609:                        ofs++;
                   9610:                        ext[i] = c;
                   9611:                }
                   9612:        }
1.1.1.20  root     9613:        int si = REG16(SI) + ofs;
1.1.1.3   root     9614:        int ds = SREG(DS);
1.1       root     9615:        while(si > 0xffff) {
                   9616:                si -= 0x10;
                   9617:                ds++;
                   9618:        }
                   9619:        REG16(SI) = si;
1.1.1.3   root     9620:        SREG(DS) = ds;
                   9621:        i386_load_segment_descriptor(DS);
1.1       root     9622:        
1.1.1.3   root     9623:        UINT8 *fcb = mem + SREG_BASE(ES) + REG16(DI);
1.1.1.20  root     9624:        if(!(REG8(AL) & 2) || drv != 0) {
                   9625:                fcb[0] = drv;
                   9626:        }
                   9627:        if(!(REG8(AL) & 4) || name[0] != 0x20) {
                   9628:                memcpy(fcb + 1, name, 8);
                   9629:        }
                   9630:        if(!(REG8(AL) & 8) || ext[0] != 0x20) {
                   9631:                memcpy(fcb + 9, ext, 3);
                   9632:        }
                   9633:        for(int i = 1, found_star = 0; i < 1 + 8; i++) {
1.1       root     9634:                if(fcb[i] == '*') {
                   9635:                        found_star = 1;
                   9636:                }
                   9637:                if(found_star) {
                   9638:                        fcb[i] = '?';
                   9639:                }
                   9640:        }
1.1.1.20  root     9641:        for(int i = 9, found_star = 0; i < 9 + 3; i++) {
1.1       root     9642:                if(fcb[i] == '*') {
                   9643:                        found_star = 1;
                   9644:                }
                   9645:                if(found_star) {
                   9646:                        fcb[i] = '?';
                   9647:                }
                   9648:        }
                   9649:        
1.1.1.44  root     9650:        if(drv == 0 || msdos_is_valid_drive(drv - 1)) {
1.1       root     9651:                if(memchr(fcb + 1, '?', 8 + 3)) {
                   9652:                        REG8(AL) = 0x01;
1.1.1.20  root     9653:                } else {
                   9654:                        REG8(AL) = 0x00;
1.1       root     9655:                }
                   9656:        } else {
                   9657:                REG8(AL) = 0xff;
                   9658:        }
                   9659: }
                   9660: 
                   9661: inline void msdos_int_21h_2ah()
                   9662: {
                   9663:        SYSTEMTIME sTime;
                   9664:        
                   9665:        GetLocalTime(&sTime);
                   9666:        REG16(CX) = sTime.wYear;
                   9667:        REG8(DH) = (UINT8)sTime.wMonth;
                   9668:        REG8(DL) = (UINT8)sTime.wDay;
                   9669:        REG8(AL) = (UINT8)sTime.wDayOfWeek;
                   9670: }
                   9671: 
                   9672: inline void msdos_int_21h_2bh()
                   9673: {
1.1.1.14  root     9674:        REG8(AL) = 0xff;
1.1       root     9675: }
                   9676: 
                   9677: inline void msdos_int_21h_2ch()
                   9678: {
                   9679:        SYSTEMTIME sTime;
                   9680:        
                   9681:        GetLocalTime(&sTime);
                   9682:        REG8(CH) = (UINT8)sTime.wHour;
                   9683:        REG8(CL) = (UINT8)sTime.wMinute;
                   9684:        REG8(DH) = (UINT8)sTime.wSecond;
1.1.1.14  root     9685:        REG8(DL) = (UINT8)sTime.wMilliseconds / 10;
1.1       root     9686: }
                   9687: 
                   9688: inline void msdos_int_21h_2dh()
                   9689: {
                   9690:        REG8(AL) = 0x00;
                   9691: }
                   9692: 
                   9693: inline void msdos_int_21h_2eh()
                   9694: {
                   9695:        process_t *process = msdos_process_info_get(current_psp);
                   9696:        
                   9697:        process->verify = REG8(AL);
                   9698: }
                   9699: 
                   9700: inline void msdos_int_21h_2fh()
                   9701: {
                   9702:        process_t *process = msdos_process_info_get(current_psp);
                   9703:        
                   9704:        REG16(BX) = process->dta.w.l;
1.1.1.3   root     9705:        SREG(ES) = process->dta.w.h;
                   9706:        i386_load_segment_descriptor(ES);
1.1       root     9707: }
                   9708: 
                   9709: inline void msdos_int_21h_30h()
                   9710: {
                   9711:        // Version Flag / OEM
1.1.1.27  root     9712:        if(REG8(AL) == 0x01) {
1.1.1.29  root     9713: #ifdef SUPPORT_HMA
                   9714:                REG16(BX) = 0x0000;
                   9715: #else
                   9716:                REG16(BX) = 0x1000; // DOS is in HMA
                   9717: #endif
1.1       root     9718:        } else {
1.1.1.27  root     9719:                // NOTE: EXDEB invites BL shows the machine type (0=PC-98, 1=PC/AT, 2=FMR),
                   9720:                // but this is not correct on Windows 98 SE
                   9721: //             REG16(BX) = 0xff01;     // OEM = Microsoft, PC/AT
                   9722:                REG16(BX) = 0xff00;     // OEM = Microsoft
1.1       root     9723:        }
1.1.1.27  root     9724:        REG16(CX) = 0x0000;
1.1.1.30  root     9725:        REG8(AL) = dos_major_version;   // 7
                   9726:        REG8(AH) = dos_minor_version;   // 10
1.1       root     9727: }
                   9728: 
                   9729: inline void msdos_int_21h_31h()
                   9730: {
1.1.1.29  root     9731:        try {
                   9732:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   9733:        } catch(...) {
                   9734:                // recover the broken mcb
                   9735:                int mcb_seg = current_psp - 1;
                   9736:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     9737:                
1.1.1.29  root     9738:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     9739:                        mcb->mz = 'M';
                   9740:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   9741:                        
1.1.1.29  root     9742:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39  root     9743:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29  root     9744:                        } else {
1.1.1.39  root     9745:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29  root     9746:                        }
                   9747:                } else {
                   9748:                        mcb->mz = 'Z';
1.1.1.30  root     9749:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     9750:                }
                   9751:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   9752:        }
1.1       root     9753:        msdos_process_terminate(current_psp, REG8(AL) | 0x300, 0);
                   9754: }
                   9755: 
                   9756: inline void msdos_int_21h_32h()
                   9757: {
                   9758:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   9759:        UINT16 seg, ofs;
                   9760:        
                   9761:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9762:                REG8(AL) = 0;
1.1.1.3   root     9763:                SREG(DS) = seg;
                   9764:                i386_load_segment_descriptor(DS);
1.1       root     9765:                REG16(BX) = ofs;
                   9766:        } else {
                   9767:                REG8(AL) = 0xff;
1.1.1.3   root     9768:                m_CF = 1;
1.1       root     9769:        }
                   9770: }
                   9771: 
                   9772: inline void msdos_int_21h_33h()
                   9773: {
                   9774:        char path[MAX_PATH];
1.1.1.48! root     9775:        char drive = 3; // C:
1.1       root     9776:        
                   9777:        switch(REG8(AL)) {
                   9778:        case 0x00:
1.1.1.33  root     9779:                REG8(DL) = ctrl_break_checking;
1.1       root     9780:                break;
                   9781:        case 0x01:
1.1.1.33  root     9782:                ctrl_break_checking = REG8(DL);
                   9783:                break;
                   9784:        case 0x02:
                   9785:                {
                   9786:                        UINT8 old = ctrl_break_checking;
                   9787:                        ctrl_break_checking = REG8(DL);
                   9788:                        REG8(DL) = old;
                   9789:                }
                   9790:                break;
                   9791:        case 0x03:
                   9792:        case 0x04:
                   9793:                // DOS 4.0+ - Unused
1.1       root     9794:                break;
                   9795:        case 0x05:
1.1.1.48! root     9796:                if(GetSystemDirectory(path, MAX_PATH) != 0) {
        !          9797:                        if(path[0] >= 'a' && path[0] <= 'z') {
        !          9798:                                drive = path[0] - 'a' + 1;
        !          9799:                        } else if(path[0] >= 'A' && path[0] <= 'Z') {
        !          9800:                                drive = path[0] - 'A' + 1;
        !          9801:                        }
1.1       root     9802:                }
1.1.1.48! root     9803:                REG8(DL) = (UINT8)drive;
1.1       root     9804:                break;
                   9805:        case 0x06:
1.1.1.2   root     9806:                // MS-DOS version (7.10)
1.1       root     9807:                REG8(BL) = 7;
1.1.1.2   root     9808:                REG8(BH) = 10;
1.1       root     9809:                REG8(DL) = 0;
1.1.1.29  root     9810: #ifdef SUPPORT_HMA
                   9811:                REG8(DH) = 0x00;
                   9812: #else
                   9813:                REG8(DH) = 0x10; // DOS is in HMA
                   9814: #endif
1.1       root     9815:                break;
1.1.1.6   root     9816:        case 0x07:
                   9817:                if(REG8(DL) == 0) {
                   9818:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag &= ~0x20;
                   9819:                } else if(REG8(DL) == 1) {
                   9820:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag |= 0x20;
                   9821:                }
                   9822:                break;
1.1       root     9823:        default:
1.1.1.22  root     9824:                unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.48! root     9825: //             REG16(AX) = 0x01;
        !          9826: //             m_CF = 1;
        !          9827:                REG8(AL) = 0xff;
1.1       root     9828:                break;
                   9829:        }
                   9830: }
                   9831: 
1.1.1.23  root     9832: inline void msdos_int_21h_34h()
                   9833: {
                   9834:        SREG(ES) = SDA_TOP >> 4;
                   9835:        i386_load_segment_descriptor(ES);
                   9836:        REG16(BX) = offsetof(sda_t, indos_flag);;
                   9837: }
                   9838: 
1.1       root     9839: inline void msdos_int_21h_35h()
                   9840: {
                   9841:        REG16(BX) = *(UINT16 *)(mem + 4 * REG8(AL) + 0);
1.1.1.3   root     9842:        SREG(ES) = *(UINT16 *)(mem + 4 * REG8(AL) + 2);
                   9843:        i386_load_segment_descriptor(ES);
1.1       root     9844: }
                   9845: 
                   9846: inline void msdos_int_21h_36h()
                   9847: {
                   9848:        struct _diskfree_t df = {0};
                   9849:        
                   9850:        if(_getdiskfree(REG8(DL), &df) == 0) {
                   9851:                REG16(AX) = (UINT16)df.sectors_per_cluster;
                   9852:                REG16(CX) = (UINT16)df.bytes_per_sector;
1.1.1.13  root     9853:                REG16(BX) = df.avail_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.avail_clusters;
                   9854:                REG16(DX) = df.total_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.total_clusters;
1.1       root     9855:        } else {
                   9856:                REG16(AX) = 0xffff;
                   9857:        }
                   9858: }
                   9859: 
                   9860: inline void msdos_int_21h_37h()
                   9861: {
1.1.1.22  root     9862:        static UINT8 dev_flag = 0xff;
1.1       root     9863:        
                   9864:        switch(REG8(AL)) {
                   9865:        case 0x00:
1.1.1.22  root     9866:                {
                   9867:                        process_t *process = msdos_process_info_get(current_psp);
                   9868:                        REG8(AL) = 0x00;
                   9869:                        REG8(DL) = process->switchar;
                   9870:                }
1.1       root     9871:                break;
                   9872:        case 0x01:
1.1.1.22  root     9873:                {
                   9874:                        process_t *process = msdos_process_info_get(current_psp);
                   9875:                        REG8(AL) = 0x00;
                   9876:                        process->switchar = REG8(DL);
1.1.1.23  root     9877:                        msdos_sda_update(current_psp);
1.1.1.22  root     9878:                }
                   9879:                break;
                   9880:        case 0x02:
                   9881:                REG8(DL) = dev_flag;
                   9882:                break;
                   9883:        case 0x03:
                   9884:                dev_flag = REG8(DL);
                   9885:                break;
                   9886:        case 0xd0:
                   9887:        case 0xd1:
                   9888:        case 0xd2:
                   9889:        case 0xd3:
                   9890:        case 0xd4:
                   9891:        case 0xd5:
                   9892:        case 0xd6:
                   9893:        case 0xd7:
                   9894:        case 0xdc:
                   9895:        case 0xdd:
                   9896:        case 0xde:
                   9897:        case 0xdf:
1.1.1.48! root     9898:                // DIET v1.43e
        !          9899: //             REG16(AX) = 1;
        !          9900:                REG8(AL) = 0xff;
1.1       root     9901:                break;
                   9902:        default:
1.1.1.22  root     9903:                unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.48! root     9904: //             REG16(AX) = 1;
        !          9905:                REG8(AL) = 0xff;
1.1       root     9906:                break;
                   9907:        }
                   9908: }
                   9909: 
1.1.1.42  root     9910: int get_country_info(country_info_t *ci, LCID locale)
1.1.1.17  root     9911: {
                   9912:        char LCdata[80];
                   9913:        
1.1.1.19  root     9914:        ZeroMemory(ci, offsetof(country_info_t, reserved));
1.1.1.42  root     9915:        GetLocaleInfo(locale, LOCALE_ICURRDIGITS, LCdata, sizeof(LCdata));
1.1.1.17  root     9916:        ci->currency_dec_digits = atoi(LCdata);
1.1.1.42  root     9917:        GetLocaleInfo(locale, LOCALE_ICURRENCY, LCdata, sizeof(LCdata));
1.1.1.17  root     9918:        ci->currency_format = *LCdata - '0';
1.1.1.42  root     9919:        GetLocaleInfo(locale, LOCALE_IDATE, LCdata, sizeof(LCdata));
1.1.1.17  root     9920:        ci->date_format = *LCdata - '0';
1.1.1.42  root     9921:        GetLocaleInfo(locale, LOCALE_SCURRENCY, LCdata, sizeof(LCdata));
1.1.1.17  root     9922:        memcpy(&ci->currency_symbol, LCdata, 4);
1.1.1.42  root     9923:        GetLocaleInfo(locale, LOCALE_SDATE, LCdata, sizeof(LCdata));
1.1.1.17  root     9924:        *ci->date_sep = *LCdata;
1.1.1.42  root     9925:        GetLocaleInfo(locale, LOCALE_SDECIMAL, LCdata, sizeof(LCdata));
1.1.1.17  root     9926:        *ci->dec_sep = *LCdata;
1.1.1.42  root     9927:        GetLocaleInfo(locale, LOCALE_SLIST, LCdata, sizeof(LCdata));
1.1.1.17  root     9928:        *ci->list_sep = *LCdata;
1.1.1.42  root     9929:        GetLocaleInfo(locale, LOCALE_STHOUSAND, LCdata, sizeof(LCdata));
1.1.1.17  root     9930:        *ci->thou_sep = *LCdata;
1.1.1.42  root     9931:        GetLocaleInfo(locale, LOCALE_STIME, LCdata, sizeof(LCdata));
1.1.1.17  root     9932:        *ci->time_sep = *LCdata;
1.1.1.42  root     9933:        GetLocaleInfo(locale, LOCALE_STIMEFORMAT, LCdata, sizeof(LCdata));
1.1.1.17  root     9934:        if(strchr(LCdata, 'H') != NULL) {
                   9935:                ci->time_format = 1;
                   9936:        }
1.1.1.27  root     9937:        ci->case_map.w.l = 0x000a; // dummy case map routine is at fffd:000a
1.1.1.24  root     9938:        ci->case_map.w.h = 0xfffd;
1.1.1.42  root     9939:        GetLocaleInfo(locale, LOCALE_ICOUNTRY, LCdata, sizeof(LCdata));
1.1.1.17  root     9940:        return atoi(LCdata);
                   9941: }
                   9942: 
1.1.1.42  root     9943: int get_country_info(country_info_t *ci, USHORT usPrimaryLanguage, USHORT usSubLanguage)
                   9944: {
                   9945:        return get_country_info(ci, MAKELCID(MAKELANGID(usPrimaryLanguage, usSubLanguage), SORT_DEFAULT));
                   9946: }
                   9947: 
                   9948: int get_country_info(country_info_t *ci)
                   9949: {
                   9950:        return get_country_info(ci, LOCALE_USER_DEFAULT);
                   9951: }
                   9952: 
1.1.1.43  root     9953: void set_country_info(country_info_t *ci, int size)
                   9954: {
                   9955:        char LCdata[80];
                   9956:        
                   9957:        if(size >= 0x00 + 2) {
                   9958:                memset(LCdata, 0, sizeof(LCdata));
                   9959:                *LCdata = '0' + ci->date_format;
                   9960:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IDATE, LCdata);
                   9961:        }
                   9962:        if(size >= 0x02 + 5) {
                   9963:                memset(LCdata, 0, sizeof(LCdata));
                   9964:                memcpy(LCdata, &ci->currency_symbol, 4);
                   9965:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SCURRENCY, LCdata);
                   9966:        }
                   9967:        if(size >= 0x07 + 2) {
                   9968:                memset(LCdata, 0, sizeof(LCdata));
                   9969:                *LCdata = *ci->thou_sep;
                   9970:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, LCdata);
                   9971:        }
                   9972:        if(size >= 0x09 + 2) {
                   9973:                memset(LCdata, 0, sizeof(LCdata));
                   9974:                *LCdata = *ci->dec_sep;
                   9975:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, LCdata);
                   9976:        }
                   9977:        if(size >= 0x0b + 2) {
                   9978:                memset(LCdata, 0, sizeof(LCdata));
                   9979:                *LCdata = *ci->date_sep;
                   9980:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDATE, LCdata);
                   9981:        }
                   9982:        if(size >= 0x0d + 2) {
                   9983:                memset(LCdata, 0, sizeof(LCdata));
                   9984:                *LCdata = *ci->time_sep;
                   9985:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIME, LCdata);
                   9986:        }
                   9987:        if(size >= 0x0f + 1) {
                   9988:                memset(LCdata, 0, sizeof(LCdata));
                   9989:                *LCdata = '0' + ci->currency_format;
                   9990:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRENCY, LCdata);
                   9991:        }
                   9992:        if(size >= 0x10 + 1) {
                   9993:                sprintf(LCdata, "%d", ci->currency_dec_digits);
                   9994:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRDIGITS, LCdata);
                   9995:        }
                   9996:        if(size >= 0x11 + 1) {
                   9997:                // FIXME: is time format always H/h:mm:ss ???
                   9998:                if(ci->time_format & 1) {
                   9999:                        sprintf(LCdata, "H%cmm%css", *ci->time_sep, *ci->time_sep);
                   10000:                } else {
                   10001:                        sprintf(LCdata, "h%cmm%css", *ci->time_sep, *ci->time_sep);
                   10002:                }
                   10003:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, LCdata);
                   10004:        }
                   10005:        if(size >= 0x12 + 4) {
                   10006:                // 12h  DWORD   address of case map routine
                   10007:                //              (FAR CALL, AL = character to map to upper case [>= 80h])
                   10008:        }
                   10009:        if(size >= 0x16 + 2) {
                   10010:                memset(LCdata, 0, sizeof(LCdata));
                   10011:                *LCdata = *ci->list_sep;
                   10012:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SLIST, LCdata);
                   10013:        }
                   10014: }
                   10015: 
1.1.1.42  root     10016: #ifndef SUBLANG_SWAHILI
                   10017:        #define SUBLANG_SWAHILI 0x01
                   10018: #endif
                   10019: #ifndef SUBLANG_TSWANA_BOTSWANA
                   10020:        #define SUBLANG_TSWANA_BOTSWANA 0x02
                   10021: #endif
                   10022: #ifndef SUBLANG_LITHUANIAN_LITHUANIA
                   10023:        #define SUBLANG_LITHUANIAN_LITHUANIA 0x01
                   10024: #endif
                   10025: #ifndef LANG_BANGLA
                   10026:        #define LANG_BANGLA 0x45
                   10027: #endif
                   10028: #ifndef SUBLANG_BANGLA_BANGLADESH
                   10029:        #define SUBLANG_BANGLA_BANGLADESH 0x02
                   10030: #endif
                   10031: 
                   10032: static const struct {
                   10033:        int code;
                   10034:        USHORT usPrimaryLanguage;
                   10035:        USHORT usSubLanguage;
                   10036: } country_table[] = {
                   10037:        {0x001, LANG_ENGLISH, SUBLANG_ENGLISH_US},                              // United States
                   10038:        {0x002, LANG_FRENCH, SUBLANG_FRENCH_CANADIAN},                          // Canadian-French
                   10039:        {0x004, LANG_ENGLISH, SUBLANG_ENGLISH_CAN},                             // Canada (English)
                   10040:        {0x007, LANG_RUSSIAN, SUBLANG_RUSSIAN_RUSSIA},                          // Russia
                   10041:        {0x014, LANG_ARABIC, SUBLANG_ARABIC_EGYPT},                             // Egypt
                   10042:        {0x01B, LANG_ZULU, SUBLANG_ZULU_SOUTH_AFRICA},                          // South Africa
                   10043: //     {0x01B, LANG_XHOSA, SUBLANG_XHOSA_SOUTH_AFRICA},                        // South Africa
                   10044: //     {0x01B, LANG_AFRIKAANS, SUBLANG_AFRIKAANS_SOUTH_AFRICA},                // South Africa
                   10045: //     {0x01B, LANG_ENGLISH, SUBLANG_ENGLISH_SOUTH_AFRICA},                    // South Africa
                   10046: //     {0x01B, LANG_SOTHO, SUBLANG_SOTHO_NORTHERN_SOUTH_AFRICA},               // South Africa
                   10047: //     {0x01B, LANG_TSWANA, SUBLANG_TSWANA_SOUTH_AFRICA},                      // South Africa
                   10048:        {0x01E, LANG_GREEK, SUBLANG_GREEK_GREECE},                              // Greece
                   10049:        {0x01F, LANG_DUTCH, SUBLANG_DUTCH},                                     // Netherlands
                   10050: //     {0x01F, LANG_FRISIAN, SUBLANG_FRISIAN_NETHERLANDS},                     // Netherlands
                   10051:        {0x020, LANG_DUTCH, SUBLANG_DUTCH_BELGIAN},                             // Belgium
                   10052: //     {0x020, LANG_FRENCH, SUBLANG_FRENCH_BELGIAN},                           // Belgium
                   10053:        {0x021, LANG_FRENCH, SUBLANG_FRENCH},                                   // France
                   10054:        {0x022, LANG_SPANISH, SUBLANG_SPANISH},                                 // Spain
                   10055:        {0x023, LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA},                    // Bulgaria???
                   10056:        {0x024, LANG_HUNGARIAN, SUBLANG_HUNGARIAN_HUNGARY},                     // Hungary (not supported by DR DOS 5.0)
                   10057:        {0x027, LANG_ITALIAN, SUBLANG_ITALIAN},                                 // Italy / San Marino / Vatican City
                   10058:        {0x028, LANG_ROMANIAN, SUBLANG_ROMANIAN_ROMANIA},                       // Romania
                   10059:        {0x029, LANG_GERMAN, SUBLANG_GERMAN_SWISS},                             // Switzerland / Liechtenstein
                   10060: //     {0x029, LANG_FRENCH, SUBLANG_FRENCH_SWISS},                             // Switzerland / Liechtenstein
                   10061: //     {0x029, LANG_ITALIAN, SUBLANG_ITALIAN_SWISS},                           // Switzerland / Liechtenstein
                   10062:        {0x02A, LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA},                          // Czechoslovakia / Tjekia / Slovakia (not supported by DR DOS 5.0)
                   10063:        {0x02B, LANG_GERMAN, SUBLANG_GERMAN_AUSTRIAN},                          // Austria (DR DOS 5.0)
                   10064:        {0x02C, LANG_ENGLISH, SUBLANG_ENGLISH_UK},                              // United Kingdom
                   10065:        {0x02D, LANG_DANISH, SUBLANG_DANISH_DENMARK},                           // Denmark
                   10066:        {0x02E, LANG_SWEDISH, SUBLANG_SWEDISH},                                 // Sweden
                   10067: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_NORTHERN_SWEDEN},                       // Sweden
                   10068: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_LULE_SWEDEN},                           // Sweden
                   10069: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_SOUTHERN_SWEDEN},                       // Sweden
                   10070:        {0x02F, LANG_NORWEGIAN, SUBLANG_NORWEGIAN_BOKMAL},                      // Norway
                   10071: //     {0x02F, LANG_NORWEGIAN, SUBLANG_NORWEGIAN_NYNORSK},                     // Norway
                   10072: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_NORTHERN_NORWAY},                       // Norway
                   10073: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_LULE_NORWAY},                           // Norway
                   10074: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_SOUTHERN_NORWAY},                       // Norway
                   10075:        {0x030, LANG_POLISH, SUBLANG_POLISH_POLAND},                            // Poland (not supported by DR DOS 5.0)
                   10076:        {0x031, LANG_GERMAN, SUBLANG_GERMAN},                                   // Germany
                   10077:        {0x033, LANG_SPANISH, SUBLANG_SPANISH_PERU},                            // Peru
                   10078: //     {0x033, LANG_QUECHUA, SUBLANG_QUECHUA_PERU},                            // Peru
                   10079:        {0x034, LANG_SPANISH, SUBLANG_SPANISH_MEXICAN},                         // Mexico
                   10080:        {0x035, LANG_SPANISH, SUBLANG_NEUTRAL},                                 // Cuba
                   10081:        {0x036, LANG_SPANISH, SUBLANG_SPANISH_ARGENTINA},                       // Argentina
                   10082:        {0x037, LANG_PORTUGUESE, SUBLANG_PORTUGUESE_BRAZILIAN},                 // Brazil (not supported by DR DOS 5.0)
                   10083:        {0x038, LANG_SPANISH, SUBLANG_SPANISH_CHILE},                           // Chile
                   10084:        {0x039, LANG_SPANISH, SUBLANG_SPANISH_COLOMBIA},                        // Columbia
                   10085:        {0x03A, LANG_SPANISH, SUBLANG_SPANISH_VENEZUELA},                       // Venezuela
                   10086:        {0x03C, LANG_MALAY, SUBLANG_MALAY_MALAYSIA},                            // Malaysia
                   10087:        {0x03D, LANG_ENGLISH, SUBLANG_ENGLISH_AUS},                             // International English / Australia
                   10088:        {0x03E, LANG_INDONESIAN, SUBLANG_INDONESIAN_INDONESIA},                 // Indonesia / East Timor
                   10089:        {0x03F, LANG_ENGLISH, SUBLANG_ENGLISH_PHILIPPINES},                     // Philippines
                   10090:        {0x040, LANG_ENGLISH, SUBLANG_ENGLISH_NZ},                              // New Zealand
                   10091:        {0x041, LANG_CHINESE, SUBLANG_CHINESE_SINGAPORE},                       // Singapore
                   10092: //     {0x041, LANG_ENGLISH, SUBLANG_ENGLISH_SINGAPORE},                       // Singapore
                   10093:        {0x042, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan???
                   10094:        {0x051, LANG_JAPANESE, SUBLANG_JAPANESE_JAPAN},                         // Japan (DR DOS 5.0, MS-DOS 5.0+)
                   10095:        {0x052, LANG_KOREAN, SUBLANG_KOREAN},                                   // South Korea (DR DOS 5.0)
                   10096:        {0x054, LANG_VIETNAMESE, SUBLANG_VIETNAMESE_VIETNAM},                   // Vietnam
                   10097:        {0x056, LANG_CHINESE_SIMPLIFIED, SUBLANG_CHINESE_SIMPLIFIED},           // China (MS-DOS 5.0+)
                   10098:        {0x058, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan (MS-DOS 5.0+)
                   10099:        {0x05A, LANG_TURKISH, SUBLANG_TURKISH_TURKEY},                          // Turkey (MS-DOS 5.0+)
                   10100:        {0x05B, LANG_HINDI, SUBLANG_HINDI_INDIA},                               // India
                   10101:        {0x05C, LANG_URDU, SUBLANG_URDU_PAKISTAN},                              // Pakistan
                   10102:        {0x05D, LANG_PASHTO, SUBLANG_PASHTO_AFGHANISTAN},                       // Afghanistan
                   10103: //     {0x05D, LANG_DARI, SUBLANG_DARI_AFGHANISTAN},                           // Afghanistan
                   10104:        {0x05E, LANG_SINHALESE, SUBLANG_SINHALESE_SRI_LANKA},                   // Sri Lanka
                   10105: //     {0x05E, LANG_TAMIL, SUBLANG_TAMIL_SRI_LANKA},                           // Sri Lanka
                   10106:        {0x062, LANG_PERSIAN, SUBLANG_PERSIAN_IRAN},                            // Iran
                   10107:        {0x070, LANG_BELARUSIAN, SUBLANG_BELARUSIAN_BELARUS},                   // Belarus
                   10108:        {0x0C8, LANG_THAI, SUBLANG_THAI_THAILAND},                              // Thailand
                   10109:        {0x0D4, LANG_ARABIC, SUBLANG_ARABIC_MOROCCO},                           // Morocco
                   10110:        {0x0D5, LANG_ARABIC, SUBLANG_ARABIC_ALGERIA},                           // Algeria
                   10111:        {0x0D8, LANG_ARABIC, SUBLANG_ARABIC_TUNISIA},                           // Tunisia
                   10112:        {0x0DA, LANG_ARABIC, SUBLANG_ARABIC_LIBYA},                             // Libya
                   10113:        {0x0DD, LANG_WOLOF, SUBLANG_WOLOF_SENEGAL},                             // Senegal
                   10114: //     {0x0DD, LANG_PULAR, SUBLANG_PULAR_SENEGAL},                             // Senegal
                   10115:        {0x0EA, LANG_HAUSA, SUBLANG_HAUSA_NIGERIA_LATIN},                       // Nigeria
                   10116: //     {0x0EA, LANG_YORUBA, SUBLANG_YORUBA_NIGERIA},                           // Nigeria
                   10117: //     {0x0EA, LANG_IGBO, SUBLANG_IGBO_NIGERIA},                               // Nigeria
                   10118:        {0x0FB, LANG_AMHARIC, SUBLANG_AMHARIC_ETHIOPIA},                        // Ethiopia
                   10119: //     {0x0FB, LANG_TIGRINYA, SUBLANG_TIGRINYA_ETHIOPIA},                      // Ethiopia
                   10120:        {0x0FE, LANG_SWAHILI, SUBLANG_SWAHILI},                                 // Kenya
                   10121:        {0x107, LANG_ENGLISH, SUBLANG_ENGLISH_ZIMBABWE},                        // Zimbabwe
                   10122:        {0x10B, LANG_TSWANA, SUBLANG_TSWANA_BOTSWANA},                          // Botswana
                   10123:        {0x12A, LANG_FAEROESE, SUBLANG_FAEROESE_FAROE_ISLANDS},                 // Faroe Islands
                   10124:        {0x12B, LANG_GREENLANDIC, SUBLANG_GREENLANDIC_GREENLAND},               // Greenland
                   10125:        {0x15F, LANG_PORTUGUESE, SUBLANG_PORTUGUESE},                           // Portugal
                   10126:        {0x160, LANG_LUXEMBOURGISH, SUBLANG_LUXEMBOURGISH_LUXEMBOURG},          // Luxembourg
                   10127: //     {0x160, LANG_GERMAN, SUBLANG_GERMAN_LUXEMBOURG},                        // Luxembourg
                   10128: //     {0x160, LANG_FRENCH, SUBLANG_FRENCH_LUXEMBOURG},                        // Luxembourg
                   10129:        {0x161, LANG_IRISH, SUBLANG_IRISH_IRELAND},                             // Ireland
                   10130: //     {0x161, LANG_ENGLISH, SUBLANG_ENGLISH_IRELAND},                         // Ireland
                   10131:        {0x162, LANG_ICELANDIC, SUBLANG_ICELANDIC_ICELAND},                     // Iceland
                   10132:        {0x163, LANG_ALBANIAN, SUBLANG_ALBANIAN_ALBANIA},                       // Albania
                   10133:        {0x164, LANG_MALTESE, SUBLANG_MALTESE_MALTA},                           // Malta
                   10134:        {0x166, LANG_FINNISH, SUBLANG_FINNISH_FINLAND},                         // Finland
                   10135: //     {0x166, LANG_SAMI, SUBLANG_SAMI_NORTHERN_FINLAND},                      // Finland
                   10136: //     {0x166, LANG_SAMI, SUBLANG_SAMI_SKOLT_FINLAND},                         // Finland
                   10137: //     {0x166, LANG_SAMI, SUBLANG_SAMI_INARI_FINLAND},                         // Finland
                   10138:        {0x167, LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA},                    // Bulgaria
                   10139:        {0x172, LANG_LITHUANIAN, SUBLANG_LITHUANIAN_LITHUANIA},                 // Lithuania
                   10140:        {0x173, LANG_LATVIAN, SUBLANG_LATVIAN_LATVIA},                          // Latvia
                   10141:        {0x174, LANG_ESTONIAN, SUBLANG_ESTONIAN_ESTONIA},                       // Estonia
                   10142:        {0x17D, LANG_SERBIAN, SUBLANG_SERBIAN_LATIN},                           // Serbia / Montenegro
                   10143:        {0x180, LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA},                         // Croatia???
                   10144:        {0x181, LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA},                         // Croatia
                   10145:        {0x182, LANG_SLOVENIAN, SUBLANG_SLOVENIAN_SLOVENIA},                    // Slovenia
                   10146:        {0x183, LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_LATIN},        // Bosnia-Herzegovina (Latin)
                   10147:        {0x184, LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_CYRILLIC},     // Bosnia-Herzegovina (Cyrillic)
                   10148:        {0x185, LANG_MACEDONIAN, SUBLANG_MACEDONIAN_MACEDONIA},                 // FYR Macedonia
                   10149:        {0x1A5, LANG_CZECH, SUBLANG_CZECH_CZECH_REPUBLIC},                      // Czech Republic
                   10150:        {0x1A6, LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA},                          // Slovakia
                   10151:        {0x1F5, LANG_ENGLISH, SUBLANG_ENGLISH_BELIZE},                          // Belize
                   10152:        {0x1F6, LANG_SPANISH, SUBLANG_SPANISH_GUATEMALA},                       // Guatemala
                   10153:        {0x1F7, LANG_SPANISH, SUBLANG_SPANISH_EL_SALVADOR},                     // El Salvador
                   10154:        {0x1F8, LANG_SPANISH, SUBLANG_SPANISH_HONDURAS},                        // Honduras
                   10155:        {0x1F9, LANG_SPANISH, SUBLANG_SPANISH_NICARAGUA},                       // Nicraragua
                   10156:        {0x1FA, LANG_SPANISH, SUBLANG_SPANISH_COSTA_RICA},                      // Costa Rica
                   10157:        {0x1FB, LANG_SPANISH, SUBLANG_SPANISH_PANAMA},                          // Panama
                   10158:        {0x24F, LANG_SPANISH, SUBLANG_SPANISH_BOLIVIA},                         // Bolivia
1.1.1.43  root     10159: //     {0x24F, LANG_QUECHUA, SUBLANG_QUECHUA_BOLIVIA},                         // Bolivia
1.1.1.42  root     10160:        {0x251, LANG_SPANISH, SUBLANG_SPANISH_ECUADOR},                         // Ecuador
                   10161: //     {0x251, LANG_QUECHUA, SUBLANG_QUECHUA_ECUADOR},                         // Ecuador
                   10162:        {0x253, LANG_SPANISH, SUBLANG_SPANISH_PARAGUAY},                        // Paraguay
                   10163:        {0x256, LANG_SPANISH, SUBLANG_SPANISH_URUGUAY},                         // Uruguay
                   10164:        {0x2A1, LANG_MALAY, SUBLANG_MALAY_BRUNEI_DARUSSALAM},                   // Brunei Darussalam
                   10165:        {0x311, LANG_ARABIC, SUBLANG_NEUTRAL},                                  // Arabic (Middle East/Saudi Arabia/etc.)
                   10166:        {0x324, LANG_UKRAINIAN, SUBLANG_UKRAINIAN_UKRAINE},                     // Ukraine
                   10167:        {0x352, LANG_KOREAN, SUBLANG_KOREAN},                                   // North Korea
                   10168:        {0x354, LANG_CHINESE, SUBLANG_CHINESE_HONGKONG},                        // Hong Kong
                   10169:        {0x355, LANG_CHINESE, SUBLANG_CHINESE_MACAU},                           // Macao
                   10170:        {0x357, LANG_KHMER, SUBLANG_KHMER_CAMBODIA},                            // Cambodia
                   10171:        {0x370, LANG_BANGLA, SUBLANG_BANGLA_BANGLADESH},                        // Bangladesh
                   10172:        {0x376, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan (DOS 6.22+)
                   10173:        {0x3C0, LANG_DIVEHI, SUBLANG_DIVEHI_MALDIVES},                          // Maldives
                   10174:        {0x3C1, LANG_ARABIC, SUBLANG_ARABIC_LEBANON},                           // Lebanon
                   10175:        {0x3C2, LANG_ARABIC, SUBLANG_ARABIC_JORDAN},                            // Jordan
                   10176:        {0x3C3, LANG_ARABIC, SUBLANG_ARABIC_SYRIA},                             // Syrian Arab Republic
                   10177:        {0x3C4, LANG_ARABIC, SUBLANG_ARABIC_IRAQ},                              // Ireq
                   10178:        {0x3C5, LANG_ARABIC, SUBLANG_ARABIC_KUWAIT},                            // Kuwait
                   10179:        {0x3C6, LANG_ARABIC, SUBLANG_ARABIC_SAUDI_ARABIA},                      // Saudi Arabia
                   10180:        {0x3C7, LANG_ARABIC, SUBLANG_ARABIC_YEMEN},                             // Yemen
                   10181:        {0x3C8, LANG_ARABIC, SUBLANG_ARABIC_OMAN},                              // Oman
                   10182:        {0x3CB, LANG_ARABIC, SUBLANG_ARABIC_UAE},                               // United Arab Emirates
                   10183:        {0x3CC, LANG_HEBREW, SUBLANG_HEBREW_ISRAEL},                            // Israel (Hebrew) (DR DOS 5.0,MS-DOS 5.0+)
                   10184:        {0x3CD, LANG_ARABIC, SUBLANG_ARABIC_BAHRAIN},                           // Bahrain
                   10185:        {0x3CE, LANG_ARABIC, SUBLANG_ARABIC_QATAR},                             // Qatar
                   10186:        {0x3D0, LANG_MONGOLIAN, SUBLANG_MONGOLIAN_CYRILLIC_MONGOLIA},           // Mongolia
                   10187: //     {0x3D0, LANG_MONGOLIAN, SUBLANG_MONGOLIAN_PRC},                         // Mongolia
                   10188:        {0x3D1, LANG_NEPALI, SUBLANG_NEPALI_NEPAL},                             // Nepal
                   10189:        {-1, 0, 0},
                   10190: };
                   10191: 
1.1.1.14  root     10192: inline void msdos_int_21h_38h()
                   10193: {
                   10194:        switch(REG8(AL)) {
                   10195:        case 0x00:
1.1.1.19  root     10196:                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1.1.14  root     10197:                break;
                   10198:        default:
1.1.1.42  root     10199:                for(int i = 0;; i++) {
                   10200:                        if(country_table[i].code == (REG8(AL) != 0xff ? REG8(AL) : REG16(BX))) {
                   10201:                                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)), country_table[i].usPrimaryLanguage, country_table[i].usSubLanguage);
                   10202:                                break;
                   10203:                        } else if(country_table[i].code == -1) {
                   10204: //                             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));
                   10205: //                             REG16(AX) = 2;
                   10206: //                             m_CF = 1;
1.1.1.48! root     10207:                                // get current coutry info
1.1.1.42  root     10208:                                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
                   10209:                                break;
                   10210:                        }
                   10211:                }
1.1.1.14  root     10212:                break;
                   10213:        }
                   10214: }
                   10215: 
1.1       root     10216: inline void msdos_int_21h_39h(int lfn)
                   10217: {
1.1.1.3   root     10218:        if(_mkdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     10219:                REG16(AX) = errno;
1.1.1.3   root     10220:                m_CF = 1;
1.1       root     10221:        }
                   10222: }
                   10223: 
                   10224: inline void msdos_int_21h_3ah(int lfn)
                   10225: {
1.1.1.3   root     10226:        if(_rmdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     10227:                REG16(AX) = errno;
1.1.1.3   root     10228:                m_CF = 1;
1.1       root     10229:        }
                   10230: }
                   10231: 
                   10232: inline void msdos_int_21h_3bh(int lfn)
                   10233: {
1.1.1.45  root     10234:        const char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1.1.44  root     10235:        
                   10236:        if(_chdir(path)) {
1.1.1.17  root     10237:                REG16(AX) = 3;  // must be 3 (path not found)
1.1.1.3   root     10238:                m_CF = 1;
1.1.1.44  root     10239:        } else {
                   10240:                int drv = _getdrive() - 1;
                   10241:                if(path[1] == ':') {
                   10242:                        if(path[0] >= 'A' && path[0] <= 'Z') {
                   10243:                                drv = path[0] - 'A';
                   10244:                        } else if(path[0] >= 'a' && path[0] <= 'z') {
                   10245:                                drv = path[0] - 'a';
                   10246:                        }
                   10247:                }
                   10248:                msdos_cds_update(drv, path);
1.1       root     10249:        }
                   10250: }
                   10251: 
                   10252: inline void msdos_int_21h_3ch()
                   10253: {
1.1.1.45  root     10254:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     10255:        int attr = GetFileAttributes(path);
1.1.1.37  root     10256:        int fd = -1;
                   10257:        int sio_port = 0;
                   10258:        int lpt_port = 0;
1.1       root     10259:        
1.1.1.45  root     10260:        if(msdos_is_device_path(path)) {
                   10261:                fd = msdos_open_device(path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
1.1       root     10262:        } else {
                   10263:                fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   10264:        }
                   10265:        if(fd != -1) {
                   10266:                if(attr == -1) {
                   10267:                        attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   10268:                }
                   10269:                SetFileAttributes(path, attr);
                   10270:                REG16(AX) = fd;
1.1.1.45  root     10271:                msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     10272:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     10273:        } else {
                   10274:                REG16(AX) = errno;
1.1.1.3   root     10275:                m_CF = 1;
1.1       root     10276:        }
                   10277: }
                   10278: 
                   10279: inline void msdos_int_21h_3dh()
                   10280: {
1.1.1.45  root     10281:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     10282:        int mode = REG8(AL) & 0x03;
1.1.1.37  root     10283:        int fd = -1;
                   10284:        int sio_port = 0;
                   10285:        int lpt_port = 0;
1.1       root     10286:        
                   10287:        if(mode < 0x03) {
1.1.1.45  root     10288:                if(msdos_is_device_path(path)) {
                   10289:                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1.1.11  root     10290:                } else {
1.1.1.13  root     10291:                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     10292:                }
1.1       root     10293:                if(fd != -1) {
                   10294:                        REG16(AX) = fd;
1.1.1.45  root     10295:                        msdos_file_handler_open(fd, path, _isatty(fd), mode, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     10296:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     10297:                } else {
                   10298:                        REG16(AX) = errno;
1.1.1.3   root     10299:                        m_CF = 1;
1.1       root     10300:                }
                   10301:        } else {
                   10302:                REG16(AX) = 0x0c;
1.1.1.3   root     10303:                m_CF = 1;
1.1       root     10304:        }
                   10305: }
                   10306: 
                   10307: inline void msdos_int_21h_3eh()
                   10308: {
                   10309:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10310:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10311:        
1.1.1.20  root     10312:        if(fd < process->max_files && file_handler[fd].valid) {
                   10313:                _close(fd);
                   10314:                msdos_file_handler_close(fd);
                   10315:                msdos_psp_set_file_table(REG16(BX), 0x0ff, current_psp);
1.1       root     10316:        } else {
                   10317:                REG16(AX) = 0x06;
1.1.1.3   root     10318:                m_CF = 1;
1.1       root     10319:        }
                   10320: }
                   10321: 
1.1.1.35  root     10322: DWORD WINAPI msdos_int_21h_3fh_thread(LPVOID)
                   10323: {
                   10324:        UINT8 *buf = mem + SREG_BASE(DS) + REG16(DX);
                   10325:        int max = REG16(CX);
                   10326:        int p = 0;
                   10327:        
                   10328:        while(max > p) {
                   10329:                int chr = msdos_getch();
                   10330:                
                   10331:                if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
                   10332:                        p = 0;
                   10333:                        buf[p++] = 0x0d;
                   10334:                        if(max > p) {
                   10335:                                buf[p++] = 0x0a;
                   10336:                        }
                   10337:                        msdos_putch(0x03);
                   10338:                        msdos_putch(0x0d);
                   10339:                        msdos_putch(0x0a);
                   10340:                        break;
                   10341:                } else if(ctrl_break_pressed) {
                   10342:                        // skip this byte
                   10343:                } else if(chr == 0x00) {
                   10344:                        // skip 2nd byte
                   10345:                        msdos_getch();
                   10346:                } else if(chr == 0x0d) {
                   10347:                        // carriage return
                   10348:                        buf[p++] = 0x0d;
                   10349:                        if(max > p) {
                   10350:                                buf[p++] = 0x0a;
                   10351:                        }
                   10352:                        msdos_putch('\n');
                   10353:                        break;
                   10354:                } else if(chr == 0x08) {
                   10355:                        // back space
                   10356:                        if(p > 0) {
                   10357:                                p--;
                   10358:                                if(msdos_ctrl_code_check(buf[p])) {
                   10359:                                        msdos_putch(0x08);
                   10360:                                        msdos_putch(0x08);
                   10361:                                        msdos_putch(0x20);
                   10362:                                        msdos_putch(0x20);
                   10363:                                        msdos_putch(0x08);
                   10364:                                        msdos_putch(0x08);
1.1.1.36  root     10365:                                } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
                   10366:                                        p--;
                   10367:                                        msdos_putch(0x08);
                   10368:                                        msdos_putch(0x08);
                   10369:                                        msdos_putch(0x20);
                   10370:                                        msdos_putch(0x20);
                   10371:                                        msdos_putch(0x08);
                   10372:                                        msdos_putch(0x08);
1.1.1.35  root     10373:                                } else {
                   10374:                                        msdos_putch(0x08);
                   10375:                                        msdos_putch(0x20);
                   10376:                                        msdos_putch(0x08);
                   10377:                                }
                   10378:                        }
                   10379:                } else if(chr == 0x1b) {
                   10380:                        // escape
                   10381:                        while(p > 0) {
                   10382:                                p--;
                   10383:                                if(msdos_ctrl_code_check(buf[p])) {
                   10384:                                        msdos_putch(0x08);
                   10385:                                        msdos_putch(0x08);
                   10386:                                        msdos_putch(0x20);
                   10387:                                        msdos_putch(0x20);
                   10388:                                        msdos_putch(0x08);
                   10389:                                        msdos_putch(0x08);
                   10390:                                } else {
                   10391:                                        msdos_putch(0x08);
                   10392:                                        msdos_putch(0x20);
                   10393:                                        msdos_putch(0x08);
                   10394:                                }
                   10395:                        }
                   10396:                } else {
                   10397:                        buf[p++] = chr;
                   10398:                        msdos_putch(chr);
                   10399:                }
                   10400:        }
                   10401:        REG16(AX) = p;
                   10402:        
                   10403: #ifdef USE_SERVICE_THREAD
                   10404:        service_exit = true;
                   10405: #endif
                   10406:        return(0);
                   10407: }
                   10408: 
1.1       root     10409: inline void msdos_int_21h_3fh()
                   10410: {
                   10411:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10412:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10413:        
1.1.1.20  root     10414:        if(fd < process->max_files && file_handler[fd].valid) {
                   10415:                if(file_mode[file_handler[fd].mode].in) {
                   10416:                        if(file_handler[fd].atty) {
1.1       root     10417:                                // BX is stdin or is redirected to stdin
1.1.1.35  root     10418:                                if(REG16(CX) != 0) {
                   10419: #ifdef USE_SERVICE_THREAD
                   10420:                                        start_service_loop(msdos_int_21h_3fh_thread);
                   10421: #else
                   10422:                                        msdos_int_21h_3fh_thread(NULL);
                   10423:                                        REQUEST_HARDWRE_UPDATE();
                   10424: #endif
                   10425:                                } else {
                   10426:                                        REG16(AX) = 0;
1.1       root     10427:                                }
                   10428:                        } else {
1.1.1.37  root     10429:                                REG16(AX) = msdos_read(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     10430:                        }
                   10431:                } else {
                   10432:                        REG16(AX) = 0x05;
1.1.1.3   root     10433:                        m_CF = 1;
1.1       root     10434:                }
                   10435:        } else {
                   10436:                REG16(AX) = 0x06;
1.1.1.3   root     10437:                m_CF = 1;
1.1       root     10438:        }
                   10439: }
                   10440: 
                   10441: inline void msdos_int_21h_40h()
                   10442: {
                   10443:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10444:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10445:        
1.1.1.20  root     10446:        if(fd < process->max_files && file_handler[fd].valid) {
                   10447:                if(file_mode[file_handler[fd].mode].out) {
1.1       root     10448:                        if(REG16(CX)) {
1.1.1.20  root     10449:                                if(file_handler[fd].atty) {
1.1       root     10450:                                        // BX is stdout/stderr or is redirected to stdout
                   10451:                                        for(int i = 0; i < REG16(CX); i++) {
1.1.1.3   root     10452:                                                msdos_putch(mem[SREG_BASE(DS) + REG16(DX) + i]);
1.1       root     10453:                                        }
                   10454:                                        REG16(AX) = REG16(CX);
                   10455:                                } else {
1.1.1.20  root     10456:                                        REG16(AX) = msdos_write(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     10457:                                }
                   10458:                        } else {
1.1.1.20  root     10459:                                UINT32 pos = _tell(fd);
                   10460:                                _lseek(fd, 0, SEEK_END);
                   10461:                                UINT32 size = _tell(fd);
1.1.1.12  root     10462:                                if(pos < size) {
1.1.1.20  root     10463:                                        _lseek(fd, pos, SEEK_SET);
                   10464:                                        SetEndOfFile((HANDLE)_get_osfhandle(fd));
1.1.1.12  root     10465:                                } else {
                   10466:                                        for(UINT32 i = size; i < pos; i++) {
                   10467:                                                UINT8 tmp = 0;
1.1.1.23  root     10468:                                                msdos_write(fd, &tmp, 1);
1.1.1.12  root     10469:                                        }
1.1.1.20  root     10470:                                        _lseek(fd, pos, SEEK_SET);
1.1       root     10471:                                }
1.1.1.23  root     10472:                                REG16(AX) = 0;
1.1       root     10473:                        }
                   10474:                } else {
                   10475:                        REG16(AX) = 0x05;
1.1.1.3   root     10476:                        m_CF = 1;
1.1       root     10477:                }
                   10478:        } else {
                   10479:                REG16(AX) = 0x06;
1.1.1.3   root     10480:                m_CF = 1;
1.1       root     10481:        }
                   10482: }
                   10483: 
                   10484: inline void msdos_int_21h_41h(int lfn)
                   10485: {
1.1.1.3   root     10486:        if(remove(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     10487:                REG16(AX) = errno;
1.1.1.3   root     10488:                m_CF = 1;
1.1       root     10489:        }
                   10490: }
                   10491: 
                   10492: inline void msdos_int_21h_42h()
                   10493: {
                   10494:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10495:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10496:        
1.1.1.20  root     10497:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     10498:                if(REG8(AL) < 0x03) {
1.1.1.35  root     10499:                        static const int ptrname[] = { SEEK_SET, SEEK_CUR, SEEK_END };
1.1.1.20  root     10500:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), ptrname[REG8(AL)]);
                   10501:                        UINT32 pos = _tell(fd);
1.1       root     10502:                        REG16(AX) = pos & 0xffff;
                   10503:                        REG16(DX) = (pos >> 16);
                   10504:                } else {
                   10505:                        REG16(AX) = 0x01;
1.1.1.3   root     10506:                        m_CF = 1;
1.1       root     10507:                }
                   10508:        } else {
                   10509:                REG16(AX) = 0x06;
1.1.1.3   root     10510:                m_CF = 1;
1.1       root     10511:        }
                   10512: }
                   10513: 
                   10514: inline void msdos_int_21h_43h(int lfn)
                   10515: {
1.1.1.45  root     10516:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1       root     10517:        int attr;
                   10518:        
1.1.1.14  root     10519:        if(!lfn && REG8(AL) > 2) {
                   10520:                REG16(AX) = 0x01;
                   10521:                m_CF = 1;
                   10522:                return;
                   10523:        }
                   10524:        switch(REG8(lfn ? BL : AL)) {
1.1       root     10525:        case 0x00:
                   10526:                if((attr = GetFileAttributes(path)) != -1) {
1.1.1.14  root     10527:                        REG16(CX) = (UINT16)msdos_file_attribute_create((UINT16)attr);
                   10528:                } else {
                   10529:                        REG16(AX) = (UINT16)GetLastError();
                   10530:                        m_CF = 1;
                   10531:                }
                   10532:                break;
                   10533:        case 0x01:
                   10534:                if(!SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)))) {
                   10535:                        REG16(AX) = (UINT16)GetLastError();
                   10536:                        m_CF = 1;
                   10537:                }
                   10538:                break;
                   10539:        case 0x02:
                   10540:                {
1.1.1.45  root     10541:                        DWORD compressed_size = GetCompressedFileSize(path, NULL), file_size = 0;
                   10542:                        if(compressed_size != INVALID_FILE_SIZE) {
                   10543:                                if(compressed_size != 0) {
                   10544:                                        HANDLE hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   10545:                                        if(hFile != INVALID_HANDLE_VALUE) {
                   10546:                                                file_size = GetFileSize(hFile, NULL);
                   10547:                                                CloseHandle(hFile);
                   10548:                                        }
                   10549:                                        if(compressed_size == file_size) {
                   10550:                                                DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   10551:                                                // this isn't correct if the file is in the NTFS MFT
                   10552:                                                if(GetDiskFreeSpace(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
                   10553:                                                        compressed_size = ((compressed_size - 1) | (sectors_per_cluster * bytes_per_sector - 1)) + 1;
                   10554:                                                }
1.1.1.14  root     10555:                                        }
                   10556:                                }
1.1.1.45  root     10557:                                REG16(AX) = LOWORD(compressed_size);
                   10558:                                REG16(DX) = HIWORD(compressed_size);
1.1.1.14  root     10559:                        } else {
                   10560:                                REG16(AX) = (UINT16)GetLastError();
                   10561:                                m_CF = 1;
1.1       root     10562:                        }
1.1.1.14  root     10563:                }
                   10564:                break;
                   10565:        case 0x03:
                   10566:        case 0x05:
                   10567:        case 0x07:
1.1.1.48! root     10568:                if(lfn) {
1.1.1.14  root     10569:                        HANDLE hFile = CreateFile(path, FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   10570:                        if(hFile != INVALID_HANDLE_VALUE) {
                   10571:                                FILETIME local, time;
                   10572:                                DosDateTimeToFileTime(REG16(DI), /*REG8(BL) == 5 ? 0 : */REG16(CX), &local);
                   10573:                                if(REG8(BL) == 7) {
                   10574:                                        ULARGE_INTEGER hund;
                   10575:                                        hund.LowPart = local.dwLowDateTime;
                   10576:                                        hund.HighPart = local.dwHighDateTime;
                   10577:                                        hund.QuadPart += REG16(SI) * 100000;
                   10578:                                        local.dwLowDateTime = hund.LowPart;
                   10579:                                        local.dwHighDateTime = hund.HighPart;
                   10580:                                }
                   10581:                                LocalFileTimeToFileTime(&local, &time);
                   10582:                                if(!SetFileTime(hFile, REG8(BL) == 0x07 ? &time : NULL,
                   10583:                                                       REG8(BL) == 0x05 ? &time : NULL,
                   10584:                                                       REG8(BL) == 0x03 ? &time : NULL)) {
                   10585:                                        REG16(AX) = (UINT16)GetLastError();
                   10586:                                        m_CF = 1;
                   10587:                                }
                   10588:                                CloseHandle(hFile);
                   10589:                        } else {
                   10590:                                REG16(AX) = (UINT16)GetLastError();
                   10591:                                m_CF = 1;
1.1       root     10592:                        }
1.1.1.48! root     10593:                } else {
        !          10594:                        // 214303 DR DOS 3.41+ internal - Set Access Rights And Password
        !          10595:                        // 214305 DR DOS 5.0-6.0 internal - Set Extended File Attributes
        !          10596:                        // 214307 DR DOS 6.0 - Set File Owner
        !          10597: //                     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));
        !          10598:                        REG16(AX) = 0x01;
        !          10599:                        m_CF = 1;
1.1.1.14  root     10600:                }
                   10601:                break;
                   10602:        case 0x04:
                   10603:        case 0x06:
                   10604:        case 0x08:
1.1.1.48! root     10605:                if(lfn) {
1.1.1.14  root     10606:                        WIN32_FILE_ATTRIBUTE_DATA fad;
                   10607:                        if(GetFileAttributesEx(path, GetFileExInfoStandard, (LPVOID)&fad)) {
                   10608:                                FILETIME *time, local;
                   10609:                                time = REG8(BL) == 0x04 ? &fad.ftLastWriteTime :
                   10610:                                                   0x06 ? &fad.ftLastAccessTime :
                   10611:                                                          &fad.ftCreationTime;
                   10612:                                FileTimeToLocalFileTime(time, &local);
                   10613:                                FileTimeToDosDateTime(&local, &REG16(DI), &REG16(CX));
                   10614:                                if(REG8(BL) == 0x08) {
                   10615:                                        ULARGE_INTEGER hund;
                   10616:                                        hund.LowPart = local.dwLowDateTime;
                   10617:                                        hund.HighPart = local.dwHighDateTime;
                   10618:                                        hund.QuadPart /= 100000;
                   10619:                                        REG16(SI) = (UINT16)(hund.QuadPart % 200);
                   10620:                                }
                   10621:                        } else {
                   10622:                                REG16(AX) = (UINT16)GetLastError();
                   10623:                                m_CF = 1;
1.1       root     10624:                        }
1.1.1.48! root     10625:                } else {
        !          10626:                        // 214304 DR DOS 5.0-6.0 internal - Get Encrypted Password
        !          10627:                        // 214306 DR DOS 6.0 - Get File Owner
        !          10628: //                     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));
        !          10629:                        REG16(AX) = 0x01;
        !          10630:                        m_CF = 1;
1.1.1.14  root     10631:                }
                   10632:                break;
1.1.1.43  root     10633:        case 0xff:
1.1.1.48! root     10634:                if(!lfn && REG16(BP) == 0x5053) {
1.1.1.43  root     10635:                        if(REG8(CL) == 0x39) {
                   10636:                                msdos_int_21h_39h(1);
                   10637:                                break;
                   10638:                        } else if(REG8(CL) == 0x56) {
                   10639:                                msdos_int_21h_56h(1);
                   10640:                                break;
                   10641:                        }
                   10642:                }
1.1.1.14  root     10643:        default:
1.1.1.22  root     10644:                unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.48! root     10645:                REG16(AX) = lfn ? 0x7100 : 0x01;
1.1.1.14  root     10646:                m_CF = 1;
                   10647:                break;
                   10648:        }
                   10649: }
                   10650: 
                   10651: inline void msdos_int_21h_44h()
                   10652: {
1.1.1.22  root     10653:        static UINT16 iteration_count = 0;
                   10654:        
1.1.1.44  root     10655:        process_t *process;
                   10656:        int fd, drv;
1.1.1.14  root     10657:        
                   10658:        switch(REG8(AL)) {
                   10659:        case 0x00:
                   10660:        case 0x01:
                   10661:        case 0x02:
                   10662:        case 0x03:
                   10663:        case 0x04:
                   10664:        case 0x05:
                   10665:        case 0x06:
                   10666:        case 0x07:
1.1.1.44  root     10667:                process = msdos_process_info_get(current_psp);
                   10668:                fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1.1.20  root     10669:                if(fd >= process->max_files || !file_handler[fd].valid) {
                   10670:                        REG16(AX) = 0x06;
                   10671:                        m_CF = 1;
                   10672:                        return;
1.1.1.14  root     10673:                }
                   10674:                break;
                   10675:        case 0x08:
                   10676:        case 0x09:
1.1.1.44  root     10677:                drv = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   10678:                if(!msdos_is_valid_drive(drv)) {
                   10679:                        // invalid drive
1.1.1.14  root     10680:                        REG16(AX) = 0x0f;
                   10681:                        m_CF = 1;
                   10682:                        return;
1.1       root     10683:                }
                   10684:                break;
                   10685:        }
                   10686:        switch(REG8(AL)) {
1.1.1.48! root     10687:        case 0x00: // Get Device Information
1.1.1.20  root     10688:                REG16(DX) = file_handler[fd].info;
1.1       root     10689:                break;
1.1.1.48! root     10690:        case 0x01: // Set Device Information
1.1.1.45  root     10691:                if(REG8(DH) != 0) {
                   10692: //                     REG16(AX) = 0x0d; // data invalid
                   10693: //                     m_CF = 1;
                   10694:                        file_handler[fd].info = REG16(DX);
                   10695:                } else {
                   10696:                        file_handler[fd].info &= 0xff00;
                   10697:                        file_handler[fd].info |= REG8(DL);
                   10698:                }
1.1       root     10699:                break;
1.1.1.48! root     10700:        case 0x02: // Read From Character Device Control Channel
1.1.1.45  root     10701:                if(strstr(file_handler[fd].path, "EMMXXXX0") != NULL && support_ems) {
                   10702:                        // from DOSBox
                   10703:                        switch(*(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX))) {
                   10704:                        case 0x00:
                   10705:                                if(REG16(CX) >= 6) {
                   10706:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x0023;
                   10707:                                        *(UINT32 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = 0x0000;
                   10708:                                        REG16(AX) = 6; // number of bytes actually read
                   10709:                                } else {
                   10710:                                        REG16(AX) = 0x0d; // data invalid
                   10711:                                        m_CF = 1;
                   10712:                                }
                   10713:                                break;
                   10714:                        case 0x01:
                   10715:                                if(REG16(CX) >= 6) {
                   10716:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x000) = 0x0004;     // flags
                   10717:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x002) = 0x019d;     // size of this structure
                   10718:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x004) = 0x0001;     // version 1.0
                   10719:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x006) = 0x00000000; // reserved
                   10720:                                        for(int addr = 0, ofs = 0x00a; addr < 0x100000; addr += 0x4000, ofs += 6) {
                   10721:                                                if(addr >= EMS_TOP && addr < EMS_TOP + EMS_SIZE) {
                   10722:                                                        int page = (addr - EMS_TOP) / 0x4000;
                   10723:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 0) = 0x03;     // frame type: EMS frame in 64k page
                   10724:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 1) = 0xff;     // owner: NONE
                   10725:                                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + ofs + 2) = 0x7fff;   // no logical page number
                   10726:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 4) = page;     // physical EMS page number
                   10727:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 5) = 0x00;     // flags: EMS frame
                   10728:                                                } else {
                   10729:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 0) = 0x00;     // frame type: NONE
                   10730:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 1) = 0xff;     // owner: NONE
                   10731:                                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + ofs + 2) = 0xffff;   // non-EMS frame
                   10732:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 4) = 0xff;     // EMS page number (NONE)
                   10733:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 5) = 0xaa;     // flags: direct mapping
                   10734:                                                }
                   10735:                                        }
                   10736:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18a) = 0x74;       // ??
                   10737:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18b) = 0x00;       // no UMB descriptors following
                   10738:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18c) = 0x01;       // 1 EMS handle info record
                   10739:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x17d) = 0x0000;     // system handle
                   10740:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x18f) = 0x00000000; // handle name
                   10741:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x193) = 0x00000000; // handle name
                   10742:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x197) = 0x0001;     // system handle
                   10743:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x199) = 0x00110000; // physical address
                   10744:                                        
                   10745:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x0018;
                   10746:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = XMS_TOP >> 4;;
                   10747:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4) = 0x0001; // version 1.0
                   10748:                                        REG16(AX) = 6; // number of bytes actually read
                   10749:                                } else {
                   10750:                                        REG16(AX) = 0x0d; // data invalid
                   10751:                                        m_CF = 1;
                   10752:                                }
                   10753:                                break;
                   10754:                        case 0x02:
                   10755:                                if(REG16(CX) >= 2) {
                   10756:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x40; // EMS 4.0
                   10757:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 1) = 0x00;
                   10758:                                        REG16(AX) = 2; // number of bytes actually read
                   10759:                                } else {
                   10760:                                        REG16(AX) = 0x0d; // data invalid
                   10761:                                        m_CF = 1;
                   10762:                                }
                   10763:                                break;
                   10764:                        case 0x03:
                   10765:                                if(REG16(CX) >= 4) {
                   10766:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = MAX_EMS_PAGES * 16; // max size (kb)
                   10767:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = 0x0080; // min size (kb)
                   10768:                                        REG16(AX) = 4; // number of bytes actually read
                   10769:                                } else {
                   10770:                                        REG16(AX) = 0x0d; // data invalid
                   10771:                                        m_CF = 1;
                   10772:                                }
                   10773:                                break;
                   10774:                        default:
                   10775:                                REG16(AX) = 0x01; // function number invalid
                   10776:                                m_CF = 1;
                   10777:                        }
                   10778:                } else if(strstr(file_handler[fd].path, "CONFIG$") != NULL) {
                   10779:                        if(REG16(CX) >= 5) {
                   10780:                                memset(mem + SREG_BASE(DS) + REG16(DX), 0, 5);
                   10781:                                REG16(AX) = 5; // number of bytes actually read
                   10782:                        } else {
                   10783:                                REG16(AX) = 0x0d; // data invalid
                   10784:                                m_CF = 1;
                   10785:                        }
                   10786:                } else {
                   10787: //                     memset(mem + SREG_BASE(DS) + REG16(DX), 0, REG16(CX));
                   10788: //                     REG16(AX) = REG16(CX);
                   10789:                        REG16(AX) = 0x05; // access denied
                   10790:                        m_CF = 1;
                   10791:                }
                   10792:                break;
1.1.1.48! root     10793:        case 0x03: // Write To Character Device Control Channel
1.1.1.45  root     10794: //             REG16(AX) = 0x05;
                   10795: //             m_CF = 1;
                   10796:                REG16(AX) = 0x00; // success
                   10797:                break;
1.1.1.48! root     10798:        case 0x04: // Read From Block Device Control Channel
        !          10799:        case 0x05: // Write To Block Device Control Channel
1.1       root     10800:                REG16(AX) = 0x05;
1.1.1.3   root     10801:                m_CF = 1;
1.1       root     10802:                break;
1.1.1.48! root     10803:        case 0x06: // Get Input Status
1.1.1.20  root     10804:                if(file_mode[file_handler[fd].mode].in) {
                   10805:                        if(file_handler[fd].atty) {
1.1.1.14  root     10806:                                REG8(AL) = msdos_kbhit() ? 0xff : 0x00;
1.1       root     10807:                        } else {
1.1.1.20  root     10808:                                REG8(AL) = eof(fd) ? 0x00 : 0xff;
1.1       root     10809:                        }
1.1.1.14  root     10810:                } else {
                   10811:                        REG8(AL) = 0x00;
1.1       root     10812:                }
                   10813:                break;
1.1.1.48! root     10814:        case 0x07: // Get Output Status
1.1.1.20  root     10815:                if(file_mode[file_handler[fd].mode].out) {
1.1.1.14  root     10816:                        REG8(AL) = 0xff;
                   10817:                } else {
                   10818:                        REG8(AL) = 0x00;
1.1       root     10819:                }
                   10820:                break;
1.1.1.48! root     10821:        case 0x08: // Check If Block Device Removable
1.1.1.44  root     10822:                if(msdos_is_removable_drive(drv) || msdos_is_cdrom_drive(drv)) {
1.1.1.14  root     10823:                        // removable drive
                   10824:                        REG16(AX) = 0x00;
1.1       root     10825:                } else {
1.1.1.14  root     10826:                        // fixed drive
                   10827:                        REG16(AX) = 0x01;
1.1       root     10828:                }
                   10829:                break;
1.1.1.48! root     10830:        case 0x09: // Check If Block Device Remote
1.1.1.44  root     10831:                if(msdos_is_remote_drive(drv)) {
1.1.1.14  root     10832:                        // remote drive
                   10833:                        REG16(DX) = 0x1000;
1.1.1.44  root     10834:                } else if(msdos_is_subst_drive(drv)) {
                   10835:                        // subst drive
                   10836:                        REG16(DX) = 0x8000;
1.1       root     10837:                } else {
1.1.1.14  root     10838:                        // local drive
1.1.1.44  root     10839:                        REG16(DX) = 0x0000;
1.1       root     10840:                }
                   10841:                break;
1.1.1.48! root     10842:        case 0x0a: // Check If Handle Is Remote
1.1.1.45  root     10843:                if(!(file_handler[fd].info & 0x8000) && msdos_is_remote_drive(msdos_drive_number(file_handler[fd].path))) {
                   10844:                        REG16(DX) = 0x8000;
                   10845:                } else {
                   10846:                        REG16(DX) = 0x0000;
                   10847:                }
1.1.1.21  root     10848:                break;
1.1.1.48! root     10849:        case 0x0b: // Set Sharing Retry Count
1.1       root     10850:                break;
1.1.1.48! root     10851:        case 0x0c: // Generic Character Device Request
1.1.1.22  root     10852:                if(REG8(CL) == 0x45) {
                   10853:                        // set iteration (retry) count
                   10854:                        iteration_count = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
                   10855:                } else if(REG8(CL) == 0x4a) {
                   10856:                        // select code page
                   10857:                        active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2);
                   10858:                        msdos_nls_tables_update();
1.1.1.44  root     10859:                } else if(REG8(CL) == 0x4c) {
                   10860:                        // start code-page preparation
                   10861:                        int ids[3] = {437, 0, 0}; // 437: US English
                   10862:                        int count = 1, offset = 0;
                   10863:                        if(active_code_page != 437) {
                   10864:                                ids[count++] = active_code_page;
                   10865:                        }
                   10866:                        if(system_code_page != 437 && system_code_page != active_code_page) {
                   10867:                                ids[count++] = system_code_page;
                   10868:                        }
                   10869:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 0;
                   10870:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 2 + 2 * count;
                   10871:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   10872:                        for(int i = 0; i < count; i++) {
                   10873:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   10874:                        }
                   10875:                } else if(REG8(CL) == 0x4d) {
                   10876:                        // end code-page preparation
                   10877:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2;
                   10878:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
1.1.1.48! root     10879: //             } else if(REG8(CL) == 0x5f) {
        !          10880: //                     // set display information
1.1.1.22  root     10881:                } else if(REG8(CL) == 0x65) {
                   10882:                        // get iteration (retry) count
                   10883:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX)) = iteration_count;
                   10884:                } else if(REG8(CL) == 0x6a) {
                   10885:                        // query selected code page
                   10886:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2; // FIXME
                   10887:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
                   10888:                        
                   10889:                        CPINFO info;
                   10890:                        GetCPInfo(active_code_page, &info);
                   10891:                        
                   10892:                        if(info.MaxCharSize != 1) {
                   10893:                                for(int i = 0;; i++) {
                   10894:                                        UINT8 lo = info.LeadByte[2 * i + 0];
                   10895:                                        UINT8 hi = info.LeadByte[2 * i + 1];
                   10896:                                        
                   10897:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 0) = lo;
                   10898:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 2) = hi;
                   10899:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) + 4;
                   10900:                                        
                   10901:                                        if(lo == 0 && hi == 0) {
                   10902:                                                break;
                   10903:                                        }
                   10904:                                }
                   10905:                        }
1.1.1.44  root     10906:                } else if(REG8(CL) == 0x6b) {
                   10907:                        // query prepare list
                   10908:                        int ids[3] = {437, 0, 0}; // 437: US English
                   10909:                        int count = 1, offset = 0;
                   10910:                        if(active_code_page != 437) {
                   10911:                                ids[count++] = active_code_page;
                   10912:                        }
                   10913:                        if(system_code_page != 437 && system_code_page != active_code_page) {
                   10914:                                ids[count++] = system_code_page;
                   10915:                        }
                   10916:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 2 * (2 + 2 * count);
                   10917:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   10918:                        for(int i = 0; i < count; i++) {
                   10919:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   10920:                        }
                   10921:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   10922:                        for(int i = 0; i < count; i++) {
                   10923:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   10924:                        }
1.1.1.22  root     10925:                } else if(REG8(CL) == 0x7f) {
1.1.1.44  root     10926:                        // get display information
1.1.1.22  root     10927:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00) = 0;
                   10928:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x01) = 0;
                   10929:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) = 14;
                   10930:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04) = 1;
                   10931:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06) = 1;
                   10932:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x07) = 0;
                   10933:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08) = 4;
                   10934:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a) =  8 * (*(UINT16 *)(mem + 0x44a));
                   10935:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c) = 16 * (*(UINT8  *)(mem + 0x484) + 1);
                   10936:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e) = *(UINT16 *)(mem + 0x44a);
                   10937:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10) = *(UINT8  *)(mem + 0x484) + 1;
                   10938:                } else {
                   10939:                        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));
                   10940:                        REG16(AX) = 0x01; // invalid function
                   10941:                        m_CF = 1;
                   10942:                }
                   10943:                break;
1.1.1.48! root     10944:        case 0x0d: // Generic Block Device Request
1.1.1.22  root     10945:                if(REG8(CL) == 0x40) {
                   10946:                        // set device parameters
1.1.1.48! root     10947: //             } else if(REG8(CL) == 0x41) {
        !          10948: //                     // write logical device track
        !          10949: //             } else if(REG8(CL) == 0x42) {
        !          10950: //                     // format and verify logical device track
1.1.1.22  root     10951:                } else if(REG8(CL) == 0x46) {
                   10952:                        // set volume serial number
1.1.1.48! root     10953:                } else if(REG8(CL) == 0x47) {
        !          10954:                        // set access flag
        !          10955: //             } else if(REG8(CL) == 0x48) {
        !          10956: //                     // set media lock state
        !          10957: //             } else if(REG8(CL) == 0x49) {
        !          10958: //                     // eject media in drive
1.1.1.22  root     10959:                } else if(REG8(CL) == 0x4a) {
                   10960:                        // lock logical volume
                   10961:                } else if(REG8(CL) == 0x4b) {
                   10962:                        // lock physical volume
                   10963:                } else if(REG8(CL) == 0x60) {
                   10964:                        // get device parameters
1.1.1.42  root     10965:                        int drive_num = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
1.1.1.22  root     10966:                        
1.1.1.42  root     10967:                        if(pcbios_update_drive_param(drive_num, 1)) {
                   10968:                                drive_param_t *drive_param = &drive_params[drive_num];
                   10969:                                DISK_GEOMETRY *geo = &drive_param->geometry;
                   10970:                                
                   10971:                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x07; // ???
                   10972:                                switch(geo->MediaType) {
                   10973:                                case F5_360_512:
                   10974:                                case F5_320_512:
                   10975:                                case F5_320_1024:
                   10976:                                case F5_180_512:
                   10977:                                case F5_160_512:
                   10978:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // 320K/360K disk
                   10979:                                        break;
                   10980:                                case F5_1Pt2_512:
                   10981:                                case F3_1Pt2_512:
                   10982:                                case F3_1Pt23_1024:
                   10983:                                case F5_1Pt23_1024:
                   10984:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x01; // 1.2M disk
                   10985:                                        break;
                   10986:                                case F3_720_512:
                   10987:                                case F3_640_512:
                   10988:                                case F5_640_512:
                   10989:                                case F5_720_512:
                   10990:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02; // 720K disk
                   10991:                                        break;
                   10992:                                case F8_256_128:
                   10993:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x03; // single-density 8-inch disk
                   10994:                                        break;
                   10995:                                case FixedMedia:
                   10996:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   10997:                                        break;
                   10998:                                case F3_1Pt44_512:
                   10999:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   11000:                                        break;
                   11001:                                case F3_2Pt88_512:
                   11002:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09; // (DOS 5+) 2.88M floppy
                   11003:                                        break;
                   11004:                                default:
                   11005:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   11006: //                                     *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   11007:                                        break;
1.1.1.22  root     11008:                                }
1.1.1.42  root     11009:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = (geo->MediaType == FixedMedia) ? 0x01 : 0x00;
                   11010:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04) = (geo->Cylinders.QuadPart > 0xffff) ? 0xffff : geo->Cylinders.QuadPart;
                   11011:                                switch(geo->MediaType) {
                   11012:                                case F5_360_512:
                   11013:                                case F5_320_512:
                   11014:                                case F5_320_1024:
                   11015:                                case F5_180_512:
                   11016:                                case F5_160_512:
                   11017:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x01; // 320K/360K disk
                   11018:                                        break;
                   11019:                                default:
                   11020:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x00; // other drive types
                   11021:                                        break;
                   11022:                                }
                   11023:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x00) = geo->BytesPerSector;
                   11024:                                *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x02) = geo->SectorsPerTrack * geo->TracksPerCylinder;
                   11025:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x03) = 0;
                   11026:                                *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x05) = 0;
                   11027:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x06) = 0;
                   11028:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x08) = 0;
                   11029:                                switch(geo->MediaType) {
                   11030:                                case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   11031:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xff;
                   11032:                                        break;
                   11033:                                case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   11034:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfe;
                   11035:                                        break;
                   11036:                                case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   11037:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfd;
                   11038:                                        break;
                   11039:                                case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   11040:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfc;
                   11041:                                        break;
                   11042:                                case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   11043:                                case F3_1Pt2_512:
                   11044:                                case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   11045:                                case F5_720_512:
                   11046:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf9;
                   11047:                                        break;
                   11048:                                case FixedMedia:        // hard disk
                   11049:                                case RemovableMedia:
                   11050:                                case Unknown:
                   11051:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf8;
                   11052:                                        break;
                   11053:                                default:
                   11054:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf0;
                   11055:                                        break;
                   11056:                                }
                   11057:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0d) = geo->SectorsPerTrack;
                   11058:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0f) = 1;
                   11059:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x11) = 0;
                   11060:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x15) = geo->TracksPerCylinder * geo->Cylinders.QuadPart;
                   11061:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x1f) = geo->Cylinders.QuadPart;
                   11062:                                // 21h  BYTE    device type
                   11063:                                // 22h  WORD    device attributes (removable or not, etc)
1.1.1.22  root     11064:                        } else {
                   11065:                                REG16(AX) = 0x0f; // invalid drive
                   11066:                                m_CF = 1;
                   11067:                        }
1.1.1.48! root     11068: //             } else if(REG8(CL) == 0x61) {
        !          11069: //                     // read logical device track
        !          11070: //             } else if(REG8(CL) == 0x62) {
        !          11071: //                     // verify logical device track
1.1.1.22  root     11072:                } else if(REG8(CL) == 0x66) {
                   11073:                        // get volume serial number
                   11074:                        char path[] = "A:\\";
                   11075:                        char volume_label[MAX_PATH];
                   11076:                        DWORD serial_number = 0;
                   11077:                        char file_system[MAX_PATH];
                   11078:                        
                   11079:                        path[0] = 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   11080:                        
                   11081:                        if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
                   11082:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   11083:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = serial_number;
                   11084:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x06, 0x20, 11);
                   11085:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x06, volume_label, min(strlen(volume_label), 11));
                   11086:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x11, 0x20,  8);
                   11087:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x11, file_system, min(strlen(file_system), 8));
                   11088:                        } else {
                   11089:                                REG16(AX) = 0x0f; // invalid drive
                   11090:                                m_CF = 1;
                   11091:                        }
                   11092:                } else if(REG8(CL) == 0x67) {
                   11093:                        // get access flag
                   11094:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   11095:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 1;
                   11096:                } else if(REG8(CL) == 0x68) {
                   11097:                        // sense media type
1.1.1.42  root     11098:                        int drive_num = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
1.1.1.22  root     11099:                        
1.1.1.42  root     11100:                        if(pcbios_update_drive_param(drive_num, 1)) {
                   11101:                                drive_param_t *drive_param = &drive_params[drive_num];
                   11102:                                DISK_GEOMETRY *geo = &drive_param->geometry;
                   11103:                                
                   11104:                                switch(geo->MediaType) {
                   11105:                                case F3_720_512:
                   11106:                                case F5_720_512:
                   11107:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11108:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02;
                   11109:                                        break;
                   11110:                                case F3_1Pt44_512:
                   11111:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11112:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07;
                   11113:                                        break;
                   11114:                                case F3_2Pt88_512:
                   11115:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11116:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09;
                   11117:                                        break;
                   11118:                                default:
                   11119:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x00;
                   11120:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // ???
                   11121:                                        break;
1.1.1.22  root     11122:                                }
                   11123:                        } else {
                   11124:                                REG16(AX) = 0x0f; // invalid drive
                   11125:                                m_CF = 1;
                   11126:                        }
                   11127:                } else if(REG8(CL) == 0x6a) {
                   11128:                        // unlock logical volume
                   11129:                } else if(REG8(CL) == 0x6b) {
                   11130:                        // unlock physical volume
1.1.1.48! root     11131: //             } else if(REG8(CL) == 0x6c) {
        !          11132: //                     // get lock flag
        !          11133: //             } else if(REG8(CL) == 0x6d) {
        !          11134: //                     // enumerate open files
        !          11135: //             } else if(REG8(CL) == 0x6e) {
        !          11136: //                     // find swap file
        !          11137: //             } else if(REG8(CL) == 0x6f) {
        !          11138: //                     // get drive map information
        !          11139: //             } else if(REG8(CL) == 0x70) {
        !          11140: //                     // get current lock state
        !          11141: //             } else if(REG8(CL) == 0x71) {
        !          11142: //                     // get first cluster
1.1.1.22  root     11143:                } else {
                   11144:                        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));
                   11145:                        REG16(AX) = 0x01; // invalid function
                   11146:                        m_CF = 1;
                   11147:                }
                   11148:                break;
1.1.1.48! root     11149:        case 0x0e: // Get Lgical Drive Map
1.1.1.44  root     11150:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   11151:                        REG16(AX) = 0x0f; // invalid drive
                   11152:                        m_CF = 1;
                   11153:                } else {
                   11154:                        REG8(AL) = 0;
1.1.1.22  root     11155:                }
                   11156:                break;
1.1.1.48! root     11157:        case 0x0f: // Set Logical Drive Map
1.1.1.44  root     11158:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   11159:                        REG16(AX) = 0x0f; // invalid drive
                   11160:                        m_CF = 1;
1.1.1.22  root     11161:                }
                   11162:                break;
1.1.1.48! root     11163:        case 0x10: // Query Generic IOCTRL Capability (Handle)
1.1.1.22  root     11164:                switch(REG8(CL)) {
                   11165:                case 0x45:
                   11166:                case 0x4a:
1.1.1.48! root     11167:                case 0x4c:
        !          11168:                case 0x4d:
1.1.1.22  root     11169:                case 0x65:
                   11170:                case 0x6a:
1.1.1.48! root     11171:                case 0x6b:
1.1.1.22  root     11172:                case 0x7f:
                   11173:                        REG16(AX) = 0x0000; // supported
                   11174:                        break;
                   11175:                default:
                   11176:                        REG8(AL) = 0x01; // ioctl capability not available
                   11177:                        m_CF = 1;
                   11178:                        break;
                   11179:                }
                   11180:                break;
1.1.1.48! root     11181:        case 0x11: // Query Generic IOCTRL Capability (Drive)
1.1.1.22  root     11182:                switch(REG8(CL)) {
                   11183:                case 0x40:
                   11184:                case 0x46:
                   11185:                case 0x4a:
                   11186:                case 0x4b:
                   11187:                case 0x60:
                   11188:                case 0x66:
                   11189:                case 0x67:
                   11190:                case 0x68:
                   11191:                case 0x6a:
                   11192:                case 0x6b:
1.1.1.48! root     11193:                        if(REG8(CH) == 0x00 || REG8(CH) == 0x01 || REG8(CH) == 0x03 || REG8(CH) == 0x05) {
        !          11194:                                // CH = 00h     Unknown
        !          11195:                                // CH = 01h     COMn:
        !          11196:                                // CH = 03h     CON
        !          11197:                                // CH = 05h     LPTn:
        !          11198:                                REG16(AX) = 0x0000; // supported
        !          11199:                                break;
        !          11200:                        }
1.1.1.22  root     11201:                default:
                   11202:                        REG8(AL) = 0x01; // ioctl capability not available
                   11203:                        m_CF = 1;
                   11204:                        break;
                   11205:                }
                   11206:                break;
1.1.1.48! root     11207:        case 0x12: // DR DOS 5.0-6.0 - Determine DOS Type
        !          11208:        case 0x14: // DR DOS 5.0-6.0 - Set Global Password
        !          11209:        case 0x16: // DR DOS 5.0-6.0 - History Buffer, Share, And Hiload Control
        !          11210:        case 0x51: // Concurrent DOS v3.2+ - Installation Check
        !          11211:        case 0x52: // DR DOS 3.41+ - Determine DOS tTpe/Get DR DOS Version
        !          11212:        case 0x54: // DR DOS 3.41+ - Set Global Password
        !          11213:        case 0x56: // DR DOS 5.0+ - History Buffer Control
        !          11214:        case 0x57: // DR DOS 5.0-6.0 - Share/Hiload Control
        !          11215:        case 0x58: // DR DOS 5.0+ internal - Get Pointer To Internal Variable Table
        !          11216:        case 0x59: // DR Multiuser DOS 5.0 - API
        !          11217:                REG16(AX) = 0x01; // this is not DR-DOS
1.1.1.22  root     11218:                m_CF = 1;
                   11219:                break;
1.1       root     11220:        default:
1.1.1.22  root     11221:                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     11222:                REG16(AX) = 0x01;
1.1.1.3   root     11223:                m_CF = 1;
1.1       root     11224:                break;
                   11225:        }
                   11226: }
                   11227: 
                   11228: inline void msdos_int_21h_45h()
                   11229: {
                   11230:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11231:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11232:        
1.1.1.20  root     11233:        if(fd < process->max_files && file_handler[fd].valid) {
                   11234:                int dup_fd = _dup(fd);
                   11235:                if(dup_fd != -1) {
                   11236:                        REG16(AX) = dup_fd;
                   11237:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   11238: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   11239:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     11240:                } else {
                   11241:                        REG16(AX) = errno;
1.1.1.3   root     11242:                        m_CF = 1;
1.1       root     11243:                }
                   11244:        } else {
                   11245:                REG16(AX) = 0x06;
1.1.1.3   root     11246:                m_CF = 1;
1.1       root     11247:        }
                   11248: }
                   11249: 
                   11250: inline void msdos_int_21h_46h()
                   11251: {
                   11252:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11253:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   11254:        int dup_fd = REG16(CX);
                   11255:        int tmp_fd = msdos_psp_get_file_table(REG16(CX), current_psp);
1.1       root     11256:        
1.1.1.20  root     11257:        if(REG16(BX) == REG16(CX)) {
                   11258:                REG16(AX) = 0x06;
                   11259:                m_CF = 1;
                   11260:        } else if(fd < process->max_files && file_handler[fd].valid && dup_fd < process->max_files) {
                   11261:                if(tmp_fd < process->max_files && file_handler[tmp_fd].valid) {
                   11262:                        _close(tmp_fd);
                   11263:                        msdos_file_handler_close(tmp_fd);
                   11264:                        msdos_psp_set_file_table(dup_fd, 0x0ff, current_psp);
                   11265:                }
                   11266:                if(_dup2(fd, dup_fd) != -1) {
                   11267:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   11268: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   11269:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     11270:                } else {
                   11271:                        REG16(AX) = errno;
1.1.1.3   root     11272:                        m_CF = 1;
1.1       root     11273:                }
                   11274:        } else {
                   11275:                REG16(AX) = 0x06;
1.1.1.3   root     11276:                m_CF = 1;
1.1       root     11277:        }
                   11278: }
                   11279: 
                   11280: inline void msdos_int_21h_47h(int lfn)
                   11281: {
                   11282:        char path[MAX_PATH];
                   11283:        
                   11284:        if(_getdcwd(REG8(DL), path, MAX_PATH) != NULL) {
1.1.1.45  root     11285:                if(!lfn) {
                   11286:                        strcpy(path, msdos_short_path(path));
                   11287:                }
1.1       root     11288:                if(path[1] == ':') {
                   11289:                        // the returned path does not include a drive or the initial backslash
1.1.1.45  root     11290:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), path + 3);
1.1       root     11291:                } else {
1.1.1.45  root     11292:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), path);
1.1       root     11293:                }
                   11294:        } else {
                   11295:                REG16(AX) = errno;
1.1.1.3   root     11296:                m_CF = 1;
1.1       root     11297:        }
                   11298: }
                   11299: 
                   11300: inline void msdos_int_21h_48h()
                   11301: {
1.1.1.19  root     11302:        int seg, umb_linked;
1.1       root     11303:        
1.1.1.8   root     11304:        if((malloc_strategy & 0xf0) == 0x00) {
1.1.1.19  root     11305:                // unlink umb not to allocate memory in umb
                   11306:                if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   11307:                        msdos_mem_unlink_umb();
                   11308:                }
1.1.1.8   root     11309:                if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   11310:                        REG16(AX) = seg;
                   11311:                } else {
                   11312:                        REG16(AX) = 0x08;
                   11313:                        REG16(BX) = msdos_mem_get_free(first_mcb, 0);
                   11314:                        m_CF = 1;
                   11315:                }
1.1.1.19  root     11316:                if(umb_linked != 0) {
                   11317:                        msdos_mem_link_umb();
                   11318:                }
1.1.1.8   root     11319:        } else if((malloc_strategy & 0xf0) == 0x40) {
                   11320:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   11321:                        REG16(AX) = seg;
                   11322:                } else {
                   11323:                        REG16(AX) = 0x08;
                   11324:                        REG16(BX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   11325:                        m_CF = 1;
                   11326:                }
                   11327:        } else if((malloc_strategy & 0xf0) == 0x80) {
                   11328:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   11329:                        REG16(AX) = seg;
                   11330:                } else if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   11331:                        REG16(AX) = seg;
                   11332:                } else {
                   11333:                        REG16(AX) = 0x08;
                   11334:                        REG16(BX) = max(msdos_mem_get_free(UMB_TOP >> 4, 0), msdos_mem_get_free(first_mcb, 0));
                   11335:                        m_CF = 1;
                   11336:                }
1.1       root     11337:        }
                   11338: }
                   11339: 
                   11340: inline void msdos_int_21h_49h()
                   11341: {
1.1.1.14  root     11342:        int mcb_seg = SREG(ES) - 1;
                   11343:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   11344:        
                   11345:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   11346:                msdos_mem_free(SREG(ES));
                   11347:        } else {
1.1.1.33  root     11348:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.14  root     11349:                m_CF = 1;
                   11350:        }
1.1       root     11351: }
                   11352: 
                   11353: inline void msdos_int_21h_4ah()
                   11354: {
1.1.1.14  root     11355:        int mcb_seg = SREG(ES) - 1;
                   11356:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1       root     11357:        int max_paragraphs;
                   11358:        
1.1.1.14  root     11359:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   11360:                if(msdos_mem_realloc(SREG(ES), REG16(BX), &max_paragraphs)) {
                   11361:                        REG16(AX) = 0x08;
                   11362:                        REG16(BX) = max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs;
                   11363:                        m_CF = 1;
                   11364:                }
                   11365:        } else {
1.1.1.33  root     11366:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.3   root     11367:                m_CF = 1;
1.1       root     11368:        }
                   11369: }
                   11370: 
                   11371: inline void msdos_int_21h_4bh()
                   11372: {
1.1.1.3   root     11373:        char *command = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   11374:        param_block_t *param = (param_block_t *)(mem + SREG_BASE(ES) + REG16(BX));
1.1       root     11375:        
                   11376:        switch(REG8(AL)) {
                   11377:        case 0x00:
                   11378:        case 0x01:
                   11379:                if(msdos_process_exec(command, param, REG8(AL))) {
                   11380:                        REG16(AX) = 0x02;
1.1.1.3   root     11381:                        m_CF = 1;
1.1       root     11382:                }
                   11383:                break;
1.1.1.14  root     11384:        case 0x03:
                   11385:                {
                   11386:                        int fd;
                   11387:                        if((fd = _open(command, _O_RDONLY | _O_BINARY)) == -1) {
                   11388:                                REG16(AX) = 0x02;
                   11389:                                m_CF = 1;
                   11390:                                break;
                   11391:                        }
                   11392:                        int size = _read(fd, file_buffer, sizeof(file_buffer));
                   11393:                        _close(fd);
                   11394:                        
                   11395:                        UINT16 *overlay = (UINT16 *)param;
                   11396:                        
                   11397:                        // check exe header
                   11398:                        exe_header_t *header = (exe_header_t *)file_buffer;
                   11399:                        int header_size = 0;
                   11400:                        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   11401:                                header_size = header->header_size * 16;
                   11402:                                // relocation
                   11403:                                int start_seg = overlay[1];
                   11404:                                for(int i = 0; i < header->relocations; i++) {
                   11405:                                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   11406:                                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   11407:                                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   11408:                                }
                   11409:                        }
                   11410:                        memcpy(mem + (overlay[0] << 4), file_buffer + header_size, size - header_size);
                   11411:                }
                   11412:                break;
1.1.1.48! root     11413:        case 0x04:
        !          11414:                // Load And Execute In Background (European MS-DOS 4.0 only)
        !          11415: //     case 0x05:
        !          11416: //             // DOS 5+ - Set Execution State
        !          11417:        case 0x80:
        !          11418:                // DR DOS v3.41 - Run Already-Loaded Kernel File
        !          11419:        case 0xf0:
        !          11420:        case 0xf1:
        !          11421:                // DIET v1.10+
1.1.1.43  root     11422:        case 0xfd:
                   11423:        case 0xfe:
                   11424:                // unknown function called in FreeCOM
                   11425:                REG16(AX) = 0x01;
                   11426:                m_CF = 1;
                   11427:                break;
1.1       root     11428:        default:
1.1.1.22  root     11429:                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     11430:                REG16(AX) = 0x01;
1.1.1.3   root     11431:                m_CF = 1;
1.1       root     11432:                break;
                   11433:        }
                   11434: }
                   11435: 
                   11436: inline void msdos_int_21h_4ch()
                   11437: {
                   11438:        msdos_process_terminate(current_psp, REG8(AL), 1);
                   11439: }
                   11440: 
                   11441: inline void msdos_int_21h_4dh()
                   11442: {
                   11443:        REG16(AX) = retval;
                   11444: }
                   11445: 
                   11446: inline void msdos_int_21h_4eh()
                   11447: {
                   11448:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     11449:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   11450:        find_t *find = (find_t *)(mem + dta_laddr);
1.1.1.45  root     11451:        const char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     11452:        WIN32_FIND_DATA fd;
                   11453:        
1.1.1.14  root     11454:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   11455:        find->find_magic = FIND_MAGIC;
                   11456:        find->dta_index = dtainfo - dtalist;
1.1       root     11457:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     11458:        dtainfo->allowable_mask = REG8(CL);
                   11459:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     11460:        
1.1.1.14  root     11461:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   11462:                dtainfo->allowable_mask &= ~8;
1.1       root     11463:        }
1.1.1.14  root     11464:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   11465:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     11466:                      !msdos_find_file_has_8dot3name(&fd)) {
                   11467:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   11468:                                FindClose(dtainfo->find_handle);
                   11469:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11470:                                break;
                   11471:                        }
                   11472:                }
                   11473:        }
1.1.1.13  root     11474:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     11475:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   11476:                msdos_find_file_conv_local_time(&fd);
                   11477:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   11478:                find->size = fd.nFileSizeLow;
1.1.1.13  root     11479:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     11480:                REG16(AX) = 0;
1.1.1.14  root     11481:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     11482:                find->attrib = 8;
                   11483:                find->size = 0;
                   11484:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     11485:                dtainfo->allowable_mask &= ~8;
1.1       root     11486:                REG16(AX) = 0;
                   11487:        } else {
                   11488:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     11489:                m_CF = 1;
1.1       root     11490:        }
                   11491: }
                   11492: 
                   11493: inline void msdos_int_21h_4fh()
                   11494: {
                   11495:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     11496:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   11497:        find_t *find = (find_t *)(mem + dta_laddr);
1.1       root     11498:        WIN32_FIND_DATA fd;
                   11499:        
1.1.1.14  root     11500:        if(find->find_magic != FIND_MAGIC || find->dta_index >= MAX_DTAINFO) {
                   11501:                REG16(AX) = 0x12;
                   11502:                m_CF = 1;
                   11503:                return;
                   11504:        }
                   11505:        dtainfo_t *dtainfo = &dtalist[find->dta_index];
1.1.1.13  root     11506:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   11507:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     11508:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     11509:                              !msdos_find_file_has_8dot3name(&fd)) {
                   11510:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   11511:                                        FindClose(dtainfo->find_handle);
                   11512:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11513:                                        break;
                   11514:                                }
                   11515:                        }
                   11516:                } else {
1.1.1.13  root     11517:                        FindClose(dtainfo->find_handle);
                   11518:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11519:                }
                   11520:        }
1.1.1.13  root     11521:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     11522:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   11523:                msdos_find_file_conv_local_time(&fd);
                   11524:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   11525:                find->size = fd.nFileSizeLow;
1.1.1.13  root     11526:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     11527:                REG16(AX) = 0;
1.1.1.14  root     11528:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     11529:                find->attrib = 8;
                   11530:                find->size = 0;
                   11531:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     11532:                dtainfo->allowable_mask &= ~8;
1.1       root     11533:                REG16(AX) = 0;
                   11534:        } else {
                   11535:                REG16(AX) = 0x12;
1.1.1.3   root     11536:                m_CF = 1;
1.1       root     11537:        }
                   11538: }
                   11539: 
                   11540: inline void msdos_int_21h_50h()
                   11541: {
1.1.1.8   root     11542:        if(current_psp != REG16(BX)) {
                   11543:                process_t *process = msdos_process_info_get(current_psp);
                   11544:                if(process != NULL) {
                   11545:                        process->psp = REG16(BX);
                   11546:                }
                   11547:                current_psp = REG16(BX);
1.1.1.23  root     11548:                msdos_sda_update(current_psp);
1.1.1.8   root     11549:        }
1.1       root     11550: }
                   11551: 
                   11552: inline void msdos_int_21h_51h()
                   11553: {
                   11554:        REG16(BX) = current_psp;
                   11555: }
                   11556: 
                   11557: inline void msdos_int_21h_52h()
                   11558: {
1.1.1.25  root     11559:        SREG(ES) = DOS_INFO_TOP >> 4;
1.1.1.3   root     11560:        i386_load_segment_descriptor(ES);
1.1.1.25  root     11561:        REG16(BX) = offsetof(dos_info_t, first_dpb);
1.1       root     11562: }
                   11563: 
1.1.1.43  root     11564: inline void msdos_int_21h_53h()
                   11565: {
                   11566:        dpb_t *dpb = (dpb_t *)(mem + SREG_BASE(ES) + REG16(BP));
                   11567:        bpb_t *bpb = (bpb_t *)(mem + SREG_BASE(DS) + REG16(SI));
                   11568:        
                   11569:        dpb->bytes_per_sector = bpb->bytes_per_sector;
                   11570:        dpb->highest_sector_num = bpb->sectors_per_track - 1;
                   11571:        dpb->shift_count = 0;
                   11572:        dpb->reserved_sectors = 0;
                   11573:        dpb->fat_num = bpb->fat_num;
                   11574:        dpb->root_entries = bpb->root_entries;
                   11575:        dpb->first_data_sector = 0;
                   11576:        if(bpb->sectors_per_cluster != 0) {
                   11577:                dpb->highest_cluster_num = (UINT16)(((REG16(CX) == 0x4558 && bpb->total_sectors == 0) ? bpb->ext_total_sectors : bpb->total_sectors) / bpb->sectors_per_cluster + 1);
                   11578:        } else {
                   11579:                dpb->highest_cluster_num = 0;
                   11580:        }
                   11581:        dpb->sectors_per_fat = (REG16(CX) == 0x4558 && bpb->sectors_per_fat == 0) ? bpb->ext_sectors_per_fat : bpb->sectors_per_fat;
                   11582:        dpb->first_dir_sector = 0;
                   11583:        dpb->device_driver_header = 0;
                   11584:        dpb->media_type = bpb->media_type;
                   11585:        dpb->drive_accessed = 0;
                   11586:        dpb->next_dpb_ofs = 0xffff;
                   11587:        dpb->next_dpb_seg = 0xffff;
                   11588:        dpb->first_free_cluster = 0;
                   11589:        dpb->free_clusters = 0xffff;
                   11590: }
                   11591: 
1.1       root     11592: inline void msdos_int_21h_54h()
                   11593: {
                   11594:        process_t *process = msdos_process_info_get(current_psp);
                   11595:        
                   11596:        REG8(AL) = process->verify;
                   11597: }
                   11598: 
                   11599: inline void msdos_int_21h_55h()
                   11600: {
                   11601:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   11602:        
                   11603:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
                   11604:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   11605:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   11606:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   11607:        psp->parent_psp = current_psp;
                   11608: }
                   11609: 
                   11610: inline void msdos_int_21h_56h(int lfn)
                   11611: {
                   11612:        char src[MAX_PATH], dst[MAX_PATH];
1.1.1.3   root     11613:        strcpy(src, msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn));
                   11614:        strcpy(dst, msdos_trimmed_path((char *)(mem + SREG_BASE(ES) + REG16(DI)), lfn));
1.1       root     11615:        
                   11616:        if(rename(src, dst)) {
                   11617:                REG16(AX) = errno;
1.1.1.3   root     11618:                m_CF = 1;
1.1       root     11619:        }
                   11620: }
                   11621: 
                   11622: inline void msdos_int_21h_57h()
                   11623: {
                   11624:        FILETIME time, local;
1.1.1.14  root     11625:        FILETIME *ctime, *atime, *mtime;
1.1.1.21  root     11626:        HANDLE hHandle;
1.1       root     11627:        
1.1.1.21  root     11628:        if((hHandle = (HANDLE)_get_osfhandle(REG16(BX))) == INVALID_HANDLE_VALUE) {
1.1.1.14  root     11629:                REG16(AX) = (UINT16)GetLastError();
                   11630:                m_CF = 1;
                   11631:                return;
                   11632:        }
                   11633:        ctime = atime = mtime = NULL;
                   11634:        
1.1       root     11635:        switch(REG8(AL)) {
                   11636:        case 0x00:
1.1.1.6   root     11637:        case 0x01:
1.1.1.14  root     11638:                mtime = &time;
1.1.1.6   root     11639:                break;
                   11640:        case 0x04:
                   11641:        case 0x05:
1.1.1.14  root     11642:                atime = &time;
1.1       root     11643:                break;
1.1.1.6   root     11644:        case 0x06:
                   11645:        case 0x07:
1.1.1.14  root     11646:                ctime = &time;
                   11647:                break;
                   11648:        default:
1.1.1.22  root     11649:                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     11650:                REG16(AX) = 0x01;
                   11651:                m_CF = 1;
                   11652:                return;
                   11653:        }
                   11654:        if(REG8(AL) & 1) {
1.1       root     11655:                DosDateTimeToFileTime(REG16(DX), REG16(CX), &local);
                   11656:                LocalFileTimeToFileTime(&local, &time);
1.1.1.21  root     11657:                if(!SetFileTime(hHandle, ctime, atime, mtime)) {
1.1       root     11658:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     11659:                        m_CF = 1;
1.1       root     11660:                }
1.1.1.14  root     11661:        } else {
1.1.1.21  root     11662:                if(!GetFileTime(hHandle, ctime, atime, mtime)) {
1.1.1.14  root     11663:                        // assume a device and use the current time
                   11664:                        GetSystemTimeAsFileTime(&time);
                   11665:                }
                   11666:                FileTimeToLocalFileTime(&time, &local);
                   11667:                FileTimeToDosDateTime(&local, &REG16(DX), &REG16(CX));
1.1       root     11668:        }
                   11669: }
                   11670: 
                   11671: inline void msdos_int_21h_58h()
                   11672: {
                   11673:        switch(REG8(AL)) {
                   11674:        case 0x00:
1.1.1.7   root     11675:                REG16(AX) = malloc_strategy;
                   11676:                break;
                   11677:        case 0x01:
1.1.1.24  root     11678: //             switch(REG16(BX)) {
                   11679:                switch(REG8(BL)) {
1.1.1.7   root     11680:                case 0x0000:
                   11681:                case 0x0001:
                   11682:                case 0x0002:
                   11683:                case 0x0040:
                   11684:                case 0x0041:
                   11685:                case 0x0042:
                   11686:                case 0x0080:
                   11687:                case 0x0081:
                   11688:                case 0x0082:
                   11689:                        malloc_strategy = REG16(BX);
1.1.1.23  root     11690:                        msdos_sda_update(current_psp);
1.1.1.7   root     11691:                        break;
                   11692:                default:
1.1.1.22  root     11693:                        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     11694:                        REG16(AX) = 0x01;
                   11695:                        m_CF = 1;
                   11696:                        break;
                   11697:                }
                   11698:                break;
                   11699:        case 0x02:
1.1.1.19  root     11700:                REG8(AL) = msdos_mem_get_umb_linked() ? 1 : 0;
1.1.1.7   root     11701:                break;
                   11702:        case 0x03:
1.1.1.24  root     11703: //             switch(REG16(BX)) {
                   11704:                switch(REG8(BL)) {
1.1.1.7   root     11705:                case 0x0000:
1.1.1.19  root     11706:                        msdos_mem_unlink_umb();
                   11707:                        break;
1.1.1.7   root     11708:                case 0x0001:
1.1.1.19  root     11709:                        msdos_mem_link_umb();
1.1.1.7   root     11710:                        break;
                   11711:                default:
1.1.1.22  root     11712:                        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     11713:                        REG16(AX) = 0x01;
                   11714:                        m_CF = 1;
                   11715:                        break;
                   11716:                }
1.1       root     11717:                break;
                   11718:        default:
1.1.1.22  root     11719:                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     11720:                REG16(AX) = 0x01;
1.1.1.3   root     11721:                m_CF = 1;
1.1       root     11722:                break;
                   11723:        }
                   11724: }
                   11725: 
                   11726: inline void msdos_int_21h_59h()
                   11727: {
1.1.1.47  root     11728:        if(REG16(BX) == 0x0000) {
                   11729:                sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   11730:                
                   11731:                REG16(AX) = sda->extended_error_code;
                   11732:                REG8(BH)  = sda->error_class;
                   11733:                REG8(BL)  = sda->suggested_action;
                   11734:                REG8(CH)  = sda->locus_of_last_error;
                   11735:                // XXX: GW-BASIC 3.23 invites the registers contents of CL, DX, SI, DI, DS, and ES are not destroyed
                   11736:                if(sda->int21h_5d0ah_called != 0) {
                   11737:                        REG8(CL)  = sda->int21h_5d0ah_cl;
                   11738:                        REG16(DX) = sda->int21h_5d0ah_dx;
                   11739: //                     REG16(SI) = sda->int21h_5d0ah_si;
                   11740:                        REG16(DI) = sda->last_error_pointer.w.l;
                   11741: //                     SREG(DS)  = sda->int21h_5d0ah_ds;
                   11742: //                     i386_load_segment_descriptor(DS);
                   11743:                        SREG(ES)  = sda->last_error_pointer.w.h;
                   11744:                        i386_load_segment_descriptor(ES);
                   11745:                }
                   11746:                sda->int21h_5d0ah_called = 0;
                   11747: //     } else if(REG16(BX) == 0x0001) {
                   11748: //             // European MS-DOS 4.0 - Get Hard Error Information
                   11749:        } else {
                   11750:                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));
                   11751:                REG16(AX) = 0x01;
                   11752:                m_CF = 1;
                   11753:        }
1.1       root     11754: }
                   11755: 
                   11756: inline void msdos_int_21h_5ah()
                   11757: {
1.1.1.3   root     11758:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     11759:        int len = strlen(path);
                   11760:        char tmp[MAX_PATH];
                   11761:        
                   11762:        if(GetTempFileName(path, "TMP", 0, tmp)) {
                   11763:                int fd = _open(tmp, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   11764:                
                   11765:                SetFileAttributes(tmp, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   11766:                REG16(AX) = fd;
                   11767:                msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     11768:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     11769:                
                   11770:                strcpy(path, tmp);
                   11771:                int dx = REG16(DX) + len;
1.1.1.3   root     11772:                int ds = SREG(DS);
1.1       root     11773:                while(dx > 0xffff) {
                   11774:                        dx -= 0x10;
                   11775:                        ds++;
                   11776:                }
                   11777:                REG16(DX) = dx;
1.1.1.3   root     11778:                SREG(DS) = ds;
                   11779:                i386_load_segment_descriptor(DS);
1.1       root     11780:        } else {
                   11781:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     11782:                m_CF = 1;
1.1       root     11783:        }
                   11784: }
                   11785: 
                   11786: inline void msdos_int_21h_5bh()
                   11787: {
1.1.1.45  root     11788:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     11789:        
1.1.1.45  root     11790: //     if(msdos_is_existing_file(path)) {
                   11791:        if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1       root     11792:                // already exists
                   11793:                REG16(AX) = 0x50;
1.1.1.3   root     11794:                m_CF = 1;
1.1       root     11795:        } else {
                   11796:                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   11797:                
                   11798:                if(fd != -1) {
                   11799:                        SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   11800:                        REG16(AX) = fd;
                   11801:                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     11802:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     11803:                } else {
                   11804:                        REG16(AX) = errno;
1.1.1.3   root     11805:                        m_CF = 1;
1.1       root     11806:                }
                   11807:        }
                   11808: }
                   11809: 
                   11810: inline void msdos_int_21h_5ch()
                   11811: {
                   11812:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11813:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11814:        
1.1.1.20  root     11815:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     11816:                if(REG8(AL) == 0 || REG8(AL) == 1) {
1.1.1.35  root     11817:                        static const int modes[2] = {_LK_LOCK, _LK_UNLCK};
1.1.1.20  root     11818:                        UINT32 pos = _tell(fd);
                   11819:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), SEEK_SET);
                   11820:                        if(_locking(fd, modes[REG8(AL)], (REG16(SI) << 16) | REG16(DI))) {
1.1       root     11821:                                REG16(AX) = errno;
1.1.1.3   root     11822:                                m_CF = 1;
1.1       root     11823:                        }
1.1.1.20  root     11824:                        _lseek(fd, pos, SEEK_SET);
1.1.1.26  root     11825:                        
1.1       root     11826:                        // some seconds may be passed in _locking()
1.1.1.35  root     11827:                        REQUEST_HARDWRE_UPDATE();
1.1       root     11828:                } else {
                   11829:                        REG16(AX) = 0x01;
1.1.1.3   root     11830:                        m_CF = 1;
1.1       root     11831:                }
                   11832:        } else {
                   11833:                REG16(AX) = 0x06;
1.1.1.3   root     11834:                m_CF = 1;
1.1       root     11835:        }
                   11836: }
                   11837: 
1.1.1.22  root     11838: inline void msdos_int_21h_5dh()
                   11839: {
                   11840:        switch(REG8(AL)) {
1.1.1.45  root     11841:        case 0x00:
                   11842:                if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x12) == 0x0000) {
                   11843:                        // current system
                   11844:                        static bool reenter = false;
                   11845:                        if(!reenter) {
                   11846:                                UINT32 offset = SREG_BASE(DS) + REG16(DX);
                   11847:                                REG16(AX) = *(UINT16 *)(mem + offset + 0x00);
                   11848:                                REG16(BX) = *(UINT16 *)(mem + offset + 0x02);
                   11849:                                REG16(CX) = *(UINT16 *)(mem + offset + 0x04);
                   11850:                                REG16(DX) = *(UINT16 *)(mem + offset + 0x06);
                   11851:                                REG16(SI) = *(UINT16 *)(mem + offset + 0x08);
                   11852:                                REG16(DI) = *(UINT16 *)(mem + offset + 0x0a);
                   11853:                                SREG(DS)  = *(UINT16 *)(mem + offset + 0x0c);
                   11854:                                SREG(ES)  = *(UINT16 *)(mem + offset + 0x0e);
                   11855:                                i386_load_segment_descriptor(DS);
                   11856:                                i386_load_segment_descriptor(ES);
                   11857:                                reenter = true;
                   11858:                                try {
                   11859:                                        msdos_syscall(0x21);
                   11860:                                } catch(...) {
                   11861:                                }
                   11862:                                reenter = false;
                   11863:                        }
                   11864:                } else {
                   11865:                        REG16(AX) = 0x49; //  network software not installed
                   11866:                        m_CF = 1;
                   11867:                }
                   11868:                break;
1.1.1.22  root     11869:        case 0x06: // get address of dos swappable data area
1.1.1.23  root     11870:                SREG(DS) = (SDA_TOP >> 4);
                   11871:                i386_load_segment_descriptor(DS);
                   11872:                REG16(SI) = offsetof(sda_t, crit_error_flag);
                   11873:                REG16(CX) = 0x80;
                   11874:                REG16(DX) = 0x1a;
                   11875:                break;
1.1.1.45  root     11876:        case 0x07: // get redirected printer mode
                   11877:        case 0x08: // set redirected printer mode
                   11878:        case 0x09: // flush redirected printer output
                   11879:                REG16(AX) = 0x49; //  network software not installed
                   11880:                m_CF = 1;
                   11881:                break;
1.1.1.43  root     11882:        case 0x0a: // set extended error information
                   11883:                {
                   11884:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
1.1.1.47  root     11885:                        sda->int21h_5d0ah_called    = 1;
1.1.1.43  root     11886:                        sda->extended_error_code    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00); // AX
1.1.1.47  root     11887:                        // XXX: which one is correct ???
                   11888: #if 1
                   11889:                        // Ralf Brown's Interrupt List and DR-DOS System and Programmer's Guide
1.1.1.43  root     11890:                        sda->suggested_action       = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02); // BL
                   11891:                        sda->error_class            = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x03); // BH
1.1.1.47  root     11892:                        sda->int21h_5d0ah_cl        = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04); // CL
1.1.1.43  root     11893:                        sda->locus_of_last_error    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x05); // CH
1.1.1.47  root     11894: #else
                   11895:                        // PC DOS 7 Technical Update
                   11896:                        sda->error_class            = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02); // BH
                   11897:                        sda->suggested_action       = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x03); // BL
                   11898:                        sda->locus_of_last_error    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04); // CH
                   11899:                        sda->int21h_5d0ah_cl        = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x05); // CL
                   11900: #endif
                   11901:                        sda->int21h_5d0ah_dx        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06); // DX
                   11902: //                     sda->int21h_5d0ah_si        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08); // SI
1.1.1.43  root     11903:                        sda->last_error_pointer.w.l = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a); // DI
1.1.1.47  root     11904: //                     sda->int21h_5d0ah_ds        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c); // DS
1.1.1.43  root     11905:                        sda->last_error_pointer.w.h = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e); // ES
                   11906:                }
                   11907:                break;
1.1.1.23  root     11908:        case 0x0b: // get dos swappable data areas
1.1.1.22  root     11909:                REG16(AX) = 0x01;
                   11910:                m_CF = 1;
                   11911:                break;
                   11912:        default:
                   11913:                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));
                   11914:                REG16(AX) = 0x01;
                   11915:                m_CF = 1;
                   11916:                break;
                   11917:        }
                   11918: }
                   11919: 
1.1.1.42  root     11920: inline void msdos_int_21h_5eh()
                   11921: {
                   11922:        switch(REG8(AL)) {
                   11923:        case 0x00:
                   11924:                {
                   11925:                        char name[256] = {0};
                   11926:                        DWORD dwSize = 256;
                   11927:                        
                   11928:                        if(GetComputerName(name, &dwSize)) {
                   11929:                                char *dest = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   11930:                                for(int i = 0; i < 15; i++) {
                   11931:                                        dest[i] = (i < strlen(name)) ? name[i] : ' ';
                   11932:                                }
                   11933:                                dest[15] = '\0';
                   11934:                                REG8(CH) = 0x01; // nonzero valid
                   11935:                                REG8(CL) = 0x01; // NetBIOS number for machine name ???
                   11936:                        } else {
                   11937:                                REG16(AX) = 0x01;
                   11938:                                m_CF = 1;
                   11939:                        }
                   11940:                }
                   11941:                break;
                   11942:        default:
1.1.1.45  root     11943: //             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));
                   11944: //             REG16(AX) = 0x01;
                   11945:                REG16(AX) = 0x49; //  network software not installed
1.1.1.42  root     11946:                m_CF = 1;
                   11947:                break;
                   11948:        }
                   11949: }
                   11950: 
1.1.1.30  root     11951: inline void msdos_int_21h_5fh()
                   11952: {
                   11953:        switch(REG8(AL)) {
1.1.1.42  root     11954:        case 0x05:
1.1.1.44  root     11955:                REG16(BP) = 0;
                   11956:                for(int i = 0; i < 26; i++) {
                   11957:                        if(msdos_is_remote_drive(i)) {
                   11958:                                REG16(BP)++;
1.1.1.42  root     11959:                        }
                   11960:                }
1.1.1.30  root     11961:        case 0x02:
1.1.1.44  root     11962:                for(int i = 0, index = 0; i < 26; i++) {
                   11963:                        if(msdos_is_remote_drive(i)) {
                   11964:                                if(index == REG16(BX)) {
                   11965:                                        char volume[] = "A:";
1.1.1.30  root     11966:                                        volume[0] = 'A' + i;
1.1.1.44  root     11967:                                        DWORD dwSize = 128;
                   11968:                                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), volume);
                   11969:                                        WNetGetConnection(volume, (char *)(mem + SREG_BASE(ES) + REG16(DI)), &dwSize);
                   11970:                                        REG8(BH) = 0x00; // valid
                   11971:                                        REG8(BL) = 0x04; // disk drive
                   11972:                                        REG16(CX) = 0x00; // LANtastic
                   11973:                                        return;
1.1.1.30  root     11974:                                }
1.1.1.44  root     11975:                                index++;
1.1.1.30  root     11976:                        }
                   11977:                }
                   11978:                REG16(AX) = 0x12; // no more files
                   11979:                m_CF = 1;
                   11980:                break;
1.1.1.44  root     11981:        case 0x07:
                   11982:                if(msdos_is_valid_drive(REG8(DL))) {
                   11983:                        msdos_cds_update(REG8(DL));
                   11984:                } else {
                   11985:                        REG16(AX) = 0x0f; // invalid drive
                   11986:                        m_CF = 1;
                   11987:                }
                   11988:                break;
                   11989:        case 0x08:
                   11990:                if(msdos_is_valid_drive(REG8(DL))) {
                   11991:                        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * REG8(DL));
                   11992:                        cds->drive_attrib = 0x0000;
                   11993:                } else {
                   11994:                        REG16(AX) = 0x0f; // invalid drive
                   11995:                        m_CF = 1;
                   11996:                }
                   11997:                break;
1.1.1.30  root     11998:        default:
1.1.1.45  root     11999: //             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));
                   12000: //             REG16(AX) = 0x01;
                   12001:                REG16(AX) = 0x49; //  network software not installed
1.1.1.30  root     12002:                m_CF = 1;
                   12003:                break;
                   12004:        }
                   12005: }
                   12006: 
1.1       root     12007: inline void msdos_int_21h_60h(int lfn)
                   12008: {
1.1.1.45  root     12009:        char full[MAX_PATH];
                   12010:        const char *path = NULL;
1.1.1.14  root     12011:        
1.1       root     12012:        if(lfn) {
1.1.1.14  root     12013:                char *name;
                   12014:                *full = '\0';
1.1.1.3   root     12015:                GetFullPathName((char *)(mem + SREG_BASE(DS) + REG16(SI)), MAX_PATH, full, &name);
1.1.1.14  root     12016:                switch(REG8(CL)) {
                   12017:                case 1:
                   12018:                        GetShortPathName(full, full, MAX_PATH);
                   12019:                        my_strupr(full);
                   12020:                        break;
                   12021:                case 2:
                   12022:                        GetLongPathName(full, full, MAX_PATH);
                   12023:                        break;
                   12024:                }
                   12025:                path = full;
                   12026:        } else {
                   12027:                path = msdos_short_full_path((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   12028:        }
                   12029:        if(*path != '\0') {
                   12030:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
1.1       root     12031:        } else {
1.1.1.14  root     12032:                REG16(AX) = (UINT16)GetLastError();
                   12033:                m_CF = 1;
1.1       root     12034:        }
                   12035: }
                   12036: 
                   12037: inline void msdos_int_21h_61h()
                   12038: {
                   12039:        REG8(AL) = 0;
                   12040: }
                   12041: 
                   12042: inline void msdos_int_21h_62h()
                   12043: {
                   12044:        REG16(BX) = current_psp;
                   12045: }
                   12046: 
                   12047: inline void msdos_int_21h_63h()
                   12048: {
                   12049:        switch(REG8(AL)) {
                   12050:        case 0x00:
1.1.1.3   root     12051:                SREG(DS) = (DBCS_TABLE >> 4);
                   12052:                i386_load_segment_descriptor(DS);
1.1       root     12053:                REG16(SI) = (DBCS_TABLE & 0x0f);
                   12054:                REG8(AL) = 0x00;
                   12055:                break;
1.1.1.22  root     12056:        case 0x01: // set korean input mode
                   12057:        case 0x02: // get korean input mode
                   12058:                REG8(AL) = 0xff; // not supported
                   12059:                break;
1.1       root     12060:        default:
1.1.1.22  root     12061:                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     12062:                REG16(AX) = 0x01;
1.1.1.3   root     12063:                m_CF = 1;
1.1       root     12064:                break;
                   12065:        }
                   12066: }
                   12067: 
1.1.1.25  root     12068: UINT16 get_extended_country_info(UINT8 func)
1.1       root     12069: {
1.1.1.25  root     12070:        switch(func) {
1.1.1.17  root     12071:        case 0x01:
                   12072:                if(REG16(CX) >= 5) {
1.1.1.19  root     12073:                        UINT8 data[1 + 2 + 2 + 2 + sizeof(country_info_t)];
1.1.1.17  root     12074:                        if(REG16(CX) > sizeof(data))            // cx = actual transfer size
                   12075:                                REG16(CX) = sizeof(data);
                   12076:                        ZeroMemory(data, sizeof(data));
                   12077:                        data[0] = 0x01;
                   12078:                        *(UINT16 *)(data + 1) = REG16(CX) - 3;
1.1.1.19  root     12079:                        *(UINT16 *)(data + 3) = get_country_info((country_info_t*)(data + 7));
1.1.1.17  root     12080:                        *(UINT16 *)(data + 5) = active_code_page;
                   12081:                        memcpy(mem + SREG_BASE(ES) + REG16(DI), data, REG16(CX));
1.1.1.25  root     12082: //                     REG16(AX) = active_code_page;
1.1.1.17  root     12083:                } else {
1.1.1.25  root     12084:                        return(0x08); // insufficient memory
1.1.1.17  root     12085:                }
                   12086:                break;
                   12087:        case 0x02:
                   12088:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   12089:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (UPPERTABLE_TOP & 0x0f);
                   12090:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (UPPERTABLE_TOP >> 4);
1.1.1.25  root     12091: //             REG16(AX) = active_code_page;
1.1.1.17  root     12092:                REG16(CX) = 0x05;
                   12093:                break;
1.1.1.23  root     12094:        case 0x03:
                   12095:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   12096:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (LOWERTABLE_TOP & 0x0f);
                   12097:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (LOWERTABLE_TOP >> 4);
1.1.1.25  root     12098: //             REG16(AX) = active_code_page;
1.1.1.23  root     12099:                REG16(CX) = 0x05;
                   12100:                break;
1.1.1.17  root     12101:        case 0x04:
                   12102:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x04;
                   12103:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_UPPERTABLE_TOP & 0x0f);
                   12104:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_UPPERTABLE_TOP >> 4);
1.1.1.25  root     12105: //             REG16(AX) = active_code_page;
1.1.1.17  root     12106:                REG16(CX) = 0x05;
                   12107:                break;
                   12108:        case 0x05:
                   12109:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x05;
                   12110:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_TERMINATOR_TOP & 0x0f);
                   12111:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_TERMINATOR_TOP >> 4);
1.1.1.25  root     12112: //             REG16(AX) = active_code_page;
1.1.1.17  root     12113:                REG16(CX) = 0x05;
                   12114:                break;
                   12115:        case 0x06:
                   12116:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x06;
                   12117:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (COLLATING_TABLE_TOP & 0x0f);
                   12118:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (COLLATING_TABLE_TOP >> 4);
1.1.1.25  root     12119: //             REG16(AX) = active_code_page;
1.1.1.17  root     12120:                REG16(CX) = 0x05;
                   12121:                break;
1.1       root     12122:        case 0x07:
1.1.1.3   root     12123:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x07;
                   12124:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (DBCS_TOP & 0x0f);
                   12125:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (DBCS_TOP >> 4);
1.1.1.25  root     12126: //             REG16(AX) = active_code_page;
1.1       root     12127:                REG16(CX) = 0x05;
                   12128:                break;
1.1.1.25  root     12129:        default:
                   12130:                return(0x01); // function number invalid
                   12131:        }
                   12132:        return(0x00);
                   12133: }
                   12134: 
                   12135: inline void msdos_int_21h_65h()
                   12136: {
                   12137:        char tmp[0x10000];
                   12138:        
                   12139:        switch(REG8(AL)) {
1.1.1.43  root     12140:        case 0x00:
                   12141:                if(REG16(CX) >= 7) {
                   12142:                        set_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)), REG16(CX) - 7);
                   12143:                        REG16(AX) = system_code_page;
                   12144:                } else {
                   12145:                        REG16(AX) = 0x0c;
                   12146:                        m_CF = 1;
                   12147:                }
                   12148:                break;
1.1.1.25  root     12149:        case 0x01:
                   12150:        case 0x02:
                   12151:        case 0x03:
                   12152:        case 0x04:
                   12153:        case 0x05:
                   12154:        case 0x06:
                   12155:        case 0x07:
                   12156:                {
                   12157:                        UINT16 result = get_extended_country_info(REG8(AL));
                   12158:                        if(result) {
                   12159:                                REG16(AX) = result;
                   12160:                                m_CF = 1;
                   12161:                        } else {
                   12162:                                REG16(AX) = active_code_page; // FIXME: is this correct???
                   12163:                        }
                   12164:                }
                   12165:                break;
1.1       root     12166:        case 0x20:
1.1.1.25  root     12167:        case 0xa0:
1.1.1.19  root     12168:                memset(tmp, 0, sizeof(tmp));
                   12169:                tmp[0] = REG8(DL);
1.1       root     12170:                my_strupr(tmp);
                   12171:                REG8(DL) = tmp[0];
                   12172:                break;
                   12173:        case 0x21:
1.1.1.25  root     12174:        case 0xa1:
1.1       root     12175:                memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     12176:                memcpy(tmp, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     12177:                my_strupr(tmp);
1.1.1.3   root     12178:                memcpy(mem + SREG_BASE(DS) + REG16(DX), tmp, REG16(CX));
1.1       root     12179:                break;
                   12180:        case 0x22:
1.1.1.25  root     12181:        case 0xa2:
1.1.1.3   root     12182:                my_strupr((char *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1       root     12183:                break;
1.1.1.25  root     12184:        case 0x23:
                   12185:                // FIXME: need to check multi-byte (kanji) charactre?
1.1.1.45  root     12186:                if(REG8(DL) == 'N' || REG8(DL) == 'n' || (REG8(DL) == 0x82 && REG8(DH) == 0x6d) || (REG8(DL) == 0x82 && REG8(DH) == 0x8e)) {
1.1.1.25  root     12187:                        // 826dh/828eh: multi-byte (kanji) N and n
1.1.1.45  root     12188:                        REG16(AX) = 0x00;
                   12189:                } else if(REG8(DL) == 'Y' || REG8(DL) == 'y' || (REG8(DL) == 0x82 && REG8(DH) == 0x78) || (REG8(DL) == 0x82 && REG8(DH) == 0x99)) {
                   12190:                        // 8278h/8299h: multi-byte (kanji) Y and y
1.1.1.25  root     12191:                        REG16(AX) = 0x01;
                   12192:                } else {
                   12193:                        REG16(AX) = 0x02;
                   12194:                }
                   12195:                break;
1.1       root     12196:        default:
1.1.1.22  root     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));
1.1       root     12198:                REG16(AX) = 0x01;
1.1.1.3   root     12199:                m_CF = 1;
1.1       root     12200:                break;
                   12201:        }
                   12202: }
                   12203: 
                   12204: inline void msdos_int_21h_66h()
                   12205: {
                   12206:        switch(REG8(AL)) {
                   12207:        case 0x01:
                   12208:                REG16(BX) = active_code_page;
                   12209:                REG16(DX) = system_code_page;
                   12210:                break;
                   12211:        case 0x02:
                   12212:                if(active_code_page == REG16(BX)) {
                   12213:                        REG16(AX) = 0xeb41;
                   12214:                } else if(_setmbcp(REG16(BX)) == 0) {
                   12215:                        active_code_page = REG16(BX);
1.1.1.17  root     12216:                        msdos_nls_tables_update();
1.1       root     12217:                        REG16(AX) = 0xeb41;
1.1.1.32  root     12218:                        SetConsoleCP(active_code_page);
                   12219:                        SetConsoleOutputCP(active_code_page);
1.1       root     12220:                } else {
                   12221:                        REG16(AX) = 0x25;
1.1.1.3   root     12222:                        m_CF = 1;
1.1       root     12223:                }
                   12224:                break;
                   12225:        default:
1.1.1.22  root     12226:                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     12227:                REG16(AX) = 0x01;
1.1.1.3   root     12228:                m_CF = 1;
1.1       root     12229:                break;
                   12230:        }
                   12231: }
                   12232: 
                   12233: inline void msdos_int_21h_67h()
                   12234: {
                   12235:        process_t *process = msdos_process_info_get(current_psp);
                   12236:        
                   12237:        if(REG16(BX) <= MAX_FILES) {
                   12238:                process->max_files = max(REG16(BX), 20);
                   12239:        } else {
                   12240:                REG16(AX) = 0x08;
1.1.1.3   root     12241:                m_CF = 1;
1.1       root     12242:        }
                   12243: }
                   12244: 
                   12245: inline void msdos_int_21h_68h()
                   12246: {
                   12247:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     12248:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     12249:        
1.1.1.20  root     12250:        if(fd < process->max_files && file_handler[fd].valid) {
                   12251:                // fflush(_fdopen(fd, ""));
1.1       root     12252:        } else {
                   12253:                REG16(AX) = 0x06;
1.1.1.3   root     12254:                m_CF = 1;
1.1       root     12255:        }
                   12256: }
                   12257: 
                   12258: inline void msdos_int_21h_69h()
                   12259: {
1.1.1.3   root     12260:        drive_info_t *info = (drive_info_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     12261:        char path[] = "A:\\";
                   12262:        char volume_label[MAX_PATH];
                   12263:        DWORD serial_number = 0;
                   12264:        char file_system[MAX_PATH];
                   12265:        
                   12266:        if(REG8(BL) == 0) {
                   12267:                path[0] = 'A' + _getdrive() - 1;
                   12268:        } else {
                   12269:                path[0] = 'A' + REG8(BL) - 1;
                   12270:        }
                   12271:        
                   12272:        switch(REG8(AL)) {
                   12273:        case 0x00:
                   12274:                if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
                   12275:                        info->info_level = 0;
                   12276:                        info->serial_number = serial_number;
                   12277:                        memset(info->volume_label, 0x20, 11);
                   12278:                        memcpy(info->volume_label, volume_label, min(strlen(volume_label), 11));
                   12279:                        memset(info->file_system, 0x20, 8);
                   12280:                        memcpy(info->file_system, file_system, min(strlen(file_system), 8));
                   12281:                } else {
                   12282:                        REG16(AX) = errno;
1.1.1.3   root     12283:                        m_CF = 1;
1.1       root     12284:                }
                   12285:                break;
                   12286:        case 0x01:
                   12287:                REG16(AX) = 0x03;
1.1.1.3   root     12288:                m_CF = 1;
1.1.1.45  root     12289:                break;
                   12290:        default:
                   12291:                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));
                   12292:                REG16(AX) = 0x01;
                   12293:                m_CF = 1;
                   12294:                break;
1.1       root     12295:        }
                   12296: }
                   12297: 
                   12298: inline void msdos_int_21h_6ah()
                   12299: {
                   12300:        REG8(AH) = 0x68;
                   12301:        msdos_int_21h_68h();
                   12302: }
                   12303: 
                   12304: inline void msdos_int_21h_6bh()
                   12305: {
1.1.1.45  root     12306:        REG8(AL) = 0x00;
1.1       root     12307: }
                   12308: 
                   12309: inline void msdos_int_21h_6ch(int lfn)
                   12310: {
1.1.1.45  root     12311:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(SI)), lfn);
1.1       root     12312:        int mode = REG8(BL) & 0x03;
                   12313:        
                   12314:        if(mode < 0x03) {
1.1.1.29  root     12315:                if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1       root     12316:                        // file exists
                   12317:                        if(REG8(DL) & 1) {
1.1.1.37  root     12318:                                int fd = -1;
                   12319:                                int sio_port = 0;
                   12320:                                int lpt_port = 0;
1.1       root     12321:                                
1.1.1.45  root     12322:                                if(msdos_is_device_path(path)) {
                   12323:                                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1.1.11  root     12324:                                } else {
1.1.1.13  root     12325:                                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     12326:                                }
1.1       root     12327:                                if(fd != -1) {
                   12328:                                        REG16(AX) = fd;
                   12329:                                        REG16(CX) = 1;
1.1.1.45  root     12330:                                        msdos_file_handler_open(fd, path, _isatty(fd), mode, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     12331:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12332:                                } else {
                   12333:                                        REG16(AX) = errno;
1.1.1.3   root     12334:                                        m_CF = 1;
1.1       root     12335:                                }
                   12336:                        } else if(REG8(DL) & 2) {
                   12337:                                int attr = GetFileAttributes(path);
1.1.1.37  root     12338:                                int fd = -1;
                   12339:                                int sio_port = 0;
                   12340:                                int lpt_port = 0;
1.1       root     12341:                                
1.1.1.45  root     12342:                                if(msdos_is_device_path(path)) {
                   12343:                                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1       root     12344:                                } else {
                   12345:                                        fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12346:                                }
                   12347:                                if(fd != -1) {
                   12348:                                        if(attr == -1) {
                   12349:                                                attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   12350:                                        }
                   12351:                                        SetFileAttributes(path, attr);
                   12352:                                        REG16(AX) = fd;
                   12353:                                        REG16(CX) = 3;
1.1.1.45  root     12354:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     12355:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12356:                                } else {
                   12357:                                        REG16(AX) = errno;
1.1.1.3   root     12358:                                        m_CF = 1;
1.1       root     12359:                                }
                   12360:                        } else {
                   12361:                                REG16(AX) = 0x50;
1.1.1.3   root     12362:                                m_CF = 1;
1.1       root     12363:                        }
                   12364:                } else {
                   12365:                        // file not exists
                   12366:                        if(REG8(DL) & 0x10) {
                   12367:                                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12368:                                
                   12369:                                if(fd != -1) {
                   12370:                                        SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   12371:                                        REG16(AX) = fd;
                   12372:                                        REG16(CX) = 2;
                   12373:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     12374:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12375:                                } else {
                   12376:                                        REG16(AX) = errno;
1.1.1.3   root     12377:                                        m_CF = 1;
1.1       root     12378:                                }
                   12379:                        } else {
                   12380:                                REG16(AX) = 0x02;
1.1.1.3   root     12381:                                m_CF = 1;
1.1       root     12382:                        }
                   12383:                }
                   12384:        } else {
                   12385:                REG16(AX) = 0x0c;
1.1.1.3   root     12386:                m_CF = 1;
1.1       root     12387:        }
                   12388: }
                   12389: 
1.1.1.43  root     12390: inline void msdos_int_21h_70h()
                   12391: {
                   12392:        switch(REG8(AL)) {
1.1.1.48! root     12393:        case 0x00: // get ??? info
        !          12394:        case 0x01: // set above info
        !          12395: //             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));
        !          12396:                REG16(AX) = 0x7000;
        !          12397:                m_CF = 1;
        !          12398:                break;
        !          12399:        case 0x02: // set general internationalization info
1.1.1.43  root     12400:                if(REG16(CX) >= 7) {
                   12401:                        active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 5);
                   12402:                        msdos_nls_tables_update();
                   12403:                        set_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(SI) + 7), REG16(CX) - 7);
                   12404:                        REG16(AX) = system_code_page;
                   12405:                } else {
                   12406:                        REG16(AX) = 0x0c;
                   12407:                        m_CF = 1;
                   12408:                }
                   12409:                break;
                   12410:        default:
                   12411:                unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.48! root     12412:                REG16(AX) = 0x7000;
1.1.1.43  root     12413:                m_CF = 1;
                   12414:                break;
                   12415:        }
                   12416: }
                   12417: 
1.1       root     12418: inline void msdos_int_21h_710dh()
                   12419: {
                   12420:        // reset drive
                   12421: }
                   12422: 
1.1.1.48! root     12423: inline void msdos_int_21h_7141h()
1.1.1.17  root     12424: {
                   12425:        if(REG16(SI) == 0) {
1.1.1.48! root     12426:                msdos_int_21h_41h(1);
1.1.1.17  root     12427:                return;
                   12428:        }
                   12429:        if(REG16(SI) != 1) {
                   12430:                REG16(AX) = 5;
                   12431:                m_CF = 1;
                   12432:        }
                   12433:        /* wild card and matching attributes... */
                   12434:        char tmp[MAX_PATH * 2];
                   12435:        // copy search pathname (and quick check overrun)
                   12436:        ZeroMemory(tmp, sizeof(tmp));
                   12437:        tmp[MAX_PATH - 1] = '\0';
                   12438:        tmp[MAX_PATH] = 1;
                   12439:        strncpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(DX)), MAX_PATH);
                   12440:        
                   12441:        if(tmp[MAX_PATH - 1] != '\0' || tmp[MAX_PATH] != 1) {
                   12442:                REG16(AX) = 1;
                   12443:                m_CF = 1;
                   12444:                return;
                   12445:        }
                   12446:        for(char *s = tmp; *s; ++s) {
                   12447:                if(*s == '/') {
                   12448:                        *s = '\\';
                   12449:                }
                   12450:        }
                   12451:        char *tmp_name = (char *)_mbsrchr((unsigned char *)tmp, '\\');
                   12452:        if(tmp_name) {
                   12453:                ++tmp_name;
                   12454:        } else {
                   12455:                tmp_name = strchr(tmp, ':');
                   12456:                tmp_name = tmp_name ? tmp_name + 1 : tmp;
                   12457:        }
                   12458:        
                   12459:        WIN32_FIND_DATAA fd;
                   12460:        HANDLE fh = FindFirstFileA(tmp, &fd);
                   12461:        if(fh == INVALID_HANDLE_VALUE) {
                   12462:                REG16(AX) = 2;
                   12463:                m_CF = 1;
                   12464:                return;
                   12465:        }
                   12466:        do {
                   12467:                if(msdos_find_file_check_attribute(fd.dwFileAttributes, REG8(CL), REG8(CH)) && msdos_find_file_has_8dot3name(&fd)) {
                   12468:                        strcpy(tmp_name, fd.cFileName);
1.1.1.48! root     12469:                        if(remove(msdos_trimmed_path(tmp, 1))) {
1.1.1.17  root     12470:                                REG16(AX) = 5;
                   12471:                                m_CF = 1;
                   12472:                                break;
                   12473:                        }
                   12474:                }
                   12475:        } while(FindNextFileA(fh, &fd));
                   12476:        if(!m_CF) {
                   12477:                if(GetLastError() != ERROR_NO_MORE_FILES) {
                   12478:                        m_CF = 1;
                   12479:                        REG16(AX) = 2;
                   12480:                }
                   12481:        }
                   12482:        FindClose(fh);
                   12483: }
                   12484: 
1.1       root     12485: inline void msdos_int_21h_714eh()
                   12486: {
                   12487:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     12488:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
                   12489:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     12490:        WIN32_FIND_DATA fd;
                   12491:        
1.1.1.13  root     12492:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   12493:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   12494:                FindClose(dtainfo->find_handle);
                   12495:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12496:        }
                   12497:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     12498:        dtainfo->allowable_mask = REG8(CL);
                   12499:        dtainfo->required_mask = REG8(CH);
                   12500:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     12501:        
1.1.1.14  root     12502:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   12503:                dtainfo->allowable_mask &= ~8;
1.1       root     12504:        }
1.1.1.14  root     12505:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   12506:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13  root     12507:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   12508:                                FindClose(dtainfo->find_handle);
                   12509:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12510:                                break;
                   12511:                        }
                   12512:                }
                   12513:        }
1.1.1.13  root     12514:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     12515:                find->attrib = fd.dwFileAttributes;
                   12516:                msdos_find_file_conv_local_time(&fd);
                   12517:                if(REG16(SI) == 0) {
                   12518:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   12519:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   12520:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   12521:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   12522:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   12523:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   12524:                } else {
                   12525:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   12526:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   12527:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   12528:                }
                   12529:                find->size_hi = fd.nFileSizeHigh;
                   12530:                find->size_lo = fd.nFileSizeLow;
                   12531:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     12532:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     12533:                REG16(AX) = dtainfo - dtalist + 1;
                   12534:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     12535:                // volume label
                   12536:                find->attrib = 8;
                   12537:                find->size_hi = find->size_lo = 0;
                   12538:                strcpy(find->full_name, process->volume_label);
                   12539:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     12540:                dtainfo->allowable_mask &= ~8;
                   12541:                REG16(AX) = dtainfo - dtalist + 1;
1.1       root     12542:        } else {
                   12543:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     12544:                m_CF = 1;
1.1       root     12545:        }
                   12546: }
                   12547: 
                   12548: inline void msdos_int_21h_714fh()
                   12549: {
                   12550:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     12551:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     12552:        WIN32_FIND_DATA fd;
                   12553:        
1.1.1.14  root     12554:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   12555:                REG16(AX) = 6;
1.1.1.13  root     12556:                m_CF = 1;
                   12557:                return;
                   12558:        }
1.1.1.14  root     12559:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     12560:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   12561:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     12562:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13  root     12563:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   12564:                                        FindClose(dtainfo->find_handle);
                   12565:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12566:                                        break;
                   12567:                                }
                   12568:                        }
                   12569:                } else {
1.1.1.13  root     12570:                        FindClose(dtainfo->find_handle);
                   12571:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12572:                }
                   12573:        }
1.1.1.13  root     12574:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     12575:                find->attrib = fd.dwFileAttributes;
                   12576:                msdos_find_file_conv_local_time(&fd);
                   12577:                if(REG16(SI) == 0) {
                   12578:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   12579:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   12580:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   12581:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   12582:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   12583:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   12584:                } else {
                   12585:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   12586:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   12587:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   12588:                }
                   12589:                find->size_hi = fd.nFileSizeHigh;
                   12590:                find->size_lo = fd.nFileSizeLow;
                   12591:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     12592:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     12593:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     12594:                // volume label
                   12595:                find->attrib = 8;
                   12596:                find->size_hi = find->size_lo = 0;
                   12597:                strcpy(find->full_name, process->volume_label);
                   12598:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     12599:                dtainfo->allowable_mask &= ~8;
1.1       root     12600:        } else {
                   12601:                REG16(AX) = 0x12;
1.1.1.3   root     12602:                m_CF = 1;
1.1       root     12603:        }
                   12604: }
                   12605: 
                   12606: inline void msdos_int_21h_71a0h()
                   12607: {
                   12608:        DWORD max_component_len, file_sys_flag;
                   12609:        
1.1.1.14  root     12610:        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))) {
                   12611:                REG16(BX) = (UINT16)file_sys_flag & (FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES | FILE_UNICODE_ON_DISK | FILE_VOLUME_IS_COMPRESSED);
                   12612:                REG16(BX) |= 0x4000;                            // supports LFN functions
1.1       root     12613:                REG16(CX) = (UINT16)max_component_len;          // 255
                   12614:                REG16(DX) = (UINT16)max_component_len + 5;      // 260
                   12615:        } else {
                   12616:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     12617:                m_CF = 1;
1.1       root     12618:        }
                   12619: }
                   12620: 
                   12621: inline void msdos_int_21h_71a1h()
                   12622: {
1.1.1.14  root     12623:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   12624:                REG16(AX) = 6;
1.1.1.13  root     12625:                m_CF = 1;
                   12626:                return;
                   12627:        }
1.1.1.14  root     12628:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     12629:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   12630:                FindClose(dtainfo->find_handle);
                   12631:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12632:        }
                   12633: }
                   12634: 
                   12635: inline void msdos_int_21h_71a6h()
                   12636: {
                   12637:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     12638:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   12639:        
1.1.1.3   root     12640:        UINT8 *buffer = (UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     12641:        struct _stat64 status;
                   12642:        DWORD serial_number = 0;
                   12643:        
1.1.1.20  root     12644:        if(fd < process->max_files && file_handler[fd].valid) {
                   12645:                if(_fstat64(fd, &status) == 0) {
                   12646:                        if(file_handler[fd].path[1] == ':') {
1.1       root     12647:                                // NOTE: we need to consider the network file path "\\host\share\"
                   12648:                                char volume[] = "A:\\";
1.1.1.20  root     12649:                                volume[0] = file_handler[fd].path[1];
1.1       root     12650:                                GetVolumeInformation(volume, NULL, 0, &serial_number, NULL, NULL, NULL, 0);
                   12651:                        }
1.1.1.20  root     12652:                        *(UINT32 *)(buffer + 0x00) = GetFileAttributes(file_handler[fd].path);
1.1       root     12653:                        *(UINT32 *)(buffer + 0x04) = (UINT32)(status.st_ctime & 0xffffffff);
                   12654:                        *(UINT32 *)(buffer + 0x08) = (UINT32)((status.st_ctime >> 32) & 0xffffffff);
                   12655:                        *(UINT32 *)(buffer + 0x0c) = (UINT32)(status.st_atime & 0xffffffff);
                   12656:                        *(UINT32 *)(buffer + 0x10) = (UINT32)((status.st_atime >> 32) & 0xffffffff);
                   12657:                        *(UINT32 *)(buffer + 0x14) = (UINT32)(status.st_mtime & 0xffffffff);
                   12658:                        *(UINT32 *)(buffer + 0x18) = (UINT32)((status.st_mtime >> 32) & 0xffffffff);
                   12659:                        *(UINT32 *)(buffer + 0x1c) = serial_number;
                   12660:                        *(UINT32 *)(buffer + 0x20) = (UINT32)((status.st_size >> 32) & 0xffffffff);
                   12661:                        *(UINT32 *)(buffer + 0x24) = (UINT32)(status.st_size & 0xffffffff);
                   12662:                        *(UINT32 *)(buffer + 0x28) = status.st_nlink;
1.1.1.14  root     12663:                        // this is dummy id and it will be changed when it is reopened...
1.1       root     12664:                        *(UINT32 *)(buffer + 0x2c) = 0;
1.1.1.20  root     12665:                        *(UINT32 *)(buffer + 0x30) = file_handler[fd].id;
1.1       root     12666:                } else {
                   12667:                        REG16(AX) = errno;
1.1.1.3   root     12668:                        m_CF = 1;
1.1       root     12669:                }
                   12670:        } else {
                   12671:                REG16(AX) = 0x06;
1.1.1.3   root     12672:                m_CF = 1;
1.1       root     12673:        }
                   12674: }
                   12675: 
                   12676: inline void msdos_int_21h_71a7h()
                   12677: {
                   12678:        switch(REG8(BL)) {
                   12679:        case 0x00:
1.1.1.3   root     12680:                if(!FileTimeToDosDateTime((FILETIME *)(mem + SREG_BASE(DS) + REG16(SI)), &REG16(DX), &REG16(CX))) {
1.1       root     12681:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     12682:                        m_CF = 1;
1.1       root     12683:                }
                   12684:                break;
                   12685:        case 0x01:
                   12686:                // NOTE: we need to check BH that shows 10-millisecond untils past time in CX
1.1.1.3   root     12687:                if(!DosDateTimeToFileTime(REG16(DX), REG16(CX), (FILETIME *)(mem + SREG_BASE(ES) + REG16(DI)))) {
1.1       root     12688:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     12689:                        m_CF = 1;
1.1       root     12690:                }
                   12691:                break;
                   12692:        default:
1.1.1.22  root     12693:                unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.48! root     12694:                REG16(AX) = 0x7100;
1.1.1.3   root     12695:                m_CF = 1;
1.1       root     12696:                break;
                   12697:        }
                   12698: }
                   12699: 
                   12700: inline void msdos_int_21h_71a8h()
                   12701: {
                   12702:        if(REG8(DH) == 0) {
                   12703:                char tmp[MAX_PATH], fcb[MAX_PATH];
1.1.1.3   root     12704:                strcpy(tmp, msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     12705:                memset(fcb, 0x20, sizeof(fcb));
                   12706:                int len = strlen(tmp);
1.1.1.21  root     12707:                for(int i = 0, pos = 0; i < len; i++) {
1.1       root     12708:                        if(tmp[i] == '.') {
                   12709:                                pos = 8;
                   12710:                        } else {
                   12711:                                if(msdos_lead_byte_check(tmp[i])) {
                   12712:                                        fcb[pos++] = tmp[i++];
                   12713:                                }
                   12714:                                fcb[pos++] = tmp[i];
                   12715:                        }
                   12716:                }
1.1.1.3   root     12717:                memcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), fcb, 11);
1.1       root     12718:        } else {
1.1.1.3   root     12719:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     12720:        }
                   12721: }
                   12722: 
1.1.1.22  root     12723: inline void msdos_int_21h_71aah()
                   12724: {
                   12725:        char drv[] = "A:", path[MAX_PATH];
                   12726:        char *hoge=(char *)(mem + SREG_BASE(DS) + REG16(DX));
                   12727:        
                   12728:        if(REG8(BL) == 0) {
                   12729:                drv[0] = 'A' + _getdrive() - 1;
                   12730:        } else {
                   12731:                drv[0] = 'A' + REG8(BL) - 1;
                   12732:        }
                   12733:        switch(REG8(BH)) {
                   12734:        case 0x00:
1.1.1.44  root     12735:                if(msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   12736:                        REG16(AX) = 0x0f; // invalid drive
                   12737:                        m_CF = 1;
                   12738:                } else if(DefineDosDevice(0, drv, (char *)(mem + SREG_BASE(DS) + REG16(DX))) == 0) {
                   12739:                        REG16(AX) = 0x03; // path not found
1.1.1.22  root     12740:                        m_CF = 1;
                   12741:                }
                   12742:                break;
                   12743:        case 0x01:
1.1.1.44  root     12744:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   12745:                        REG16(AX) = 0x0f; // invalid drive
                   12746:                        m_CF = 1;
                   12747:                } else if(DefineDosDevice(DDD_REMOVE_DEFINITION, drv, NULL) == 0) {
1.1.1.22  root     12748:                        REG16(AX) = 0x0f; // invalid drive
                   12749:                        m_CF = 1;
                   12750:                }
                   12751:                break;
                   12752:        case 0x02:
1.1.1.44  root     12753:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   12754:                        REG16(AX) = 0x0f; // invalid drive
                   12755:                        m_CF = 1;
                   12756:                } else if(QueryDosDevice(drv, path, MAX_PATH) == 0) {
1.1.1.22  root     12757:                        REG16(AX) = 0x0f; // invalid drive
                   12758:                        m_CF = 1;
                   12759:                } else if(strncmp(path, "\\??\\", 4) != 0) {
                   12760:                        REG16(AX) = 0x0f; // invalid drive
                   12761:                        m_CF = 1;
                   12762:                } else {
                   12763:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(DX)), path + 4);
                   12764:                }
                   12765:                break;
                   12766:        default:
                   12767:                unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.48! root     12768:                REG16(AX) = 0x7100;
1.1.1.22  root     12769:                m_CF = 1;
                   12770:                break;
                   12771:        }
                   12772: }
                   12773: 
1.1.1.14  root     12774: inline void msdos_int_21h_7300h()
                   12775: {
1.1.1.44  root     12776:        REG8(AL) = REG8(CL);
                   12777:        REG8(AH) = 0;
1.1.1.14  root     12778: }
                   12779: 
                   12780: inline void msdos_int_21h_7302h()
                   12781: {
                   12782:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   12783:        UINT16 seg, ofs;
                   12784:        
                   12785:        if(REG16(CX) < 0x3f) {
                   12786:                REG8(AL) = 0x18;
                   12787:                m_CF = 1;
                   12788:        } else if(!msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   12789:                REG8(AL) = 0xff;
                   12790:                m_CF = 1;
                   12791:        } else {
                   12792:                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 2, mem + (seg << 4) + ofs, sizeof(dpb_t));
                   12793:        }
                   12794: }
                   12795: 
1.1       root     12796: inline void msdos_int_21h_7303h()
                   12797: {
1.1.1.3   root     12798:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   12799:        ext_space_info_t *info = (ext_space_info_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     12800:        DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   12801:        
                   12802:        if(GetDiskFreeSpace(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
                   12803:                info->size_of_structure = sizeof(ext_space_info_t);
                   12804:                info->structure_version = 0;
                   12805:                info->sectors_per_cluster = sectors_per_cluster;
                   12806:                info->bytes_per_sector = bytes_per_sector;
                   12807:                info->available_clusters_on_drive = free_clusters;
                   12808:                info->total_clusters_on_drive = total_clusters;
                   12809:                info->available_sectors_on_drive = sectors_per_cluster * free_clusters;
                   12810:                info->total_sectors_on_drive = sectors_per_cluster * total_clusters;
                   12811:                info->available_allocation_units = free_clusters;       // ???
                   12812:                info->total_allocation_units = total_clusters;          // ???
                   12813:        } else {
                   12814:                REG16(AX) = errno;
1.1.1.3   root     12815:                m_CF = 1;
1.1       root     12816:        }
                   12817: }
                   12818: 
1.1.1.30  root     12819: inline void msdos_int_21h_dbh()
                   12820: {
                   12821:        // Novell NetWare - Workstation - Get Number of Local Drives
                   12822:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   12823:        REG8(AL) = dos_info->last_drive;
                   12824: }
                   12825: 
                   12826: inline void msdos_int_21h_dch()
                   12827: {
                   12828:        // Novell NetWare - Connection Services - Get Connection Number
                   12829:        REG8(AL) = 0x00;
                   12830: }
                   12831: 
1.1.1.32  root     12832: inline void msdos_int_24h()
                   12833: {
                   12834:        const char *message = NULL;
                   12835:        int key = 0;
                   12836:        
                   12837:        for(int i = 0; i < array_length(critical_error_table); i++) {
                   12838:                if(critical_error_table[i].code == (REG16(DI) & 0xff) || critical_error_table[i].code == (UINT16)-1) {
                   12839:                        if(active_code_page == 932) {
                   12840:                                message = critical_error_table[i].message_japanese;
                   12841:                        }
                   12842:                        if(message == NULL) {
                   12843:                                message = critical_error_table[i].message_english;
                   12844:                        }
                   12845:                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   12846:                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   12847:                        
                   12848:                        SREG(ES) = WORK_TOP >> 4;
                   12849:                        i386_load_segment_descriptor(ES);
                   12850:                        REG16(DI) = 0x0000;
                   12851:                        break;
                   12852:                }
                   12853:        }
                   12854:        fprintf(stderr, "\n%s", message);
                   12855:        if(!(REG8(AH) & 0x80)) {
                   12856:                if(REG8(AH) & 0x01) {
                   12857:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�������ݒ� �h���C�u" : "writing drive", 'A' + REG8(AL));
                   12858:                } else {
                   12859:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�ǂݎ�蒆 �h���C�u" : "reading drive", 'A' + REG8(AL));
                   12860:                }
                   12861:        }
                   12862:        fprintf(stderr, "\n");
                   12863:        
1.1.1.33  root     12864:        {
1.1.1.32  root     12865:                fprintf(stderr, "%s",   (active_code_page == 932) ? "���~ (A)" : "Abort");
1.1.1.33  root     12866:        }
1.1.1.32  root     12867:        if(REG8(AH) & 0x10) {
                   12868:                fprintf(stderr, ", %s", (active_code_page == 932) ? "�Ď��s (R)" : "Retry");
                   12869:        }
                   12870:        if(REG8(AH) & 0x20) {
                   12871:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���� (I)" : "Ignore");
                   12872:        }
                   12873:        if(REG8(AH) & 0x08) {
                   12874:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���s (F)" : "Fail");
                   12875:        }
                   12876:        fprintf(stderr, "? ");
                   12877:        
                   12878:        while(1) {
                   12879:                while(!_kbhit()) {
                   12880:                        Sleep(10);
                   12881:                }
                   12882:                key = _getch();
                   12883:                
                   12884:                if(key == 'I' || key == 'i') {
                   12885:                        if(REG8(AH) & 0x20) {
                   12886:                                REG8(AL) = 0;
                   12887:                                break;
                   12888:                        }
                   12889:                } else if(key == 'R' || key == 'r') {
                   12890:                        if(REG8(AH) & 0x10) {
                   12891:                                REG8(AL) = 1;
                   12892:                                break;
                   12893:                        }
                   12894:                } else if(key == 'A' || key == 'a') {
                   12895:                        REG8(AL) = 2;
                   12896:                        break;
                   12897:                } else if(key == 'F' || key == 'f') {
                   12898:                        if(REG8(AH) & 0x08) {
                   12899:                                REG8(AL) = 3;
                   12900:                                break;
                   12901:                        }
                   12902:                }
                   12903:        }
                   12904:        fprintf(stderr, "%c\n", key);
                   12905: }
                   12906: 
1.1       root     12907: inline void msdos_int_25h()
                   12908: {
                   12909:        UINT16 seg, ofs;
                   12910:        DWORD dwSize;
                   12911:        
1.1.1.3   root     12912: #if defined(HAS_I386)
                   12913:        I386OP(pushf)();
                   12914: #else
                   12915:        PREFIX86(_pushf());
                   12916: #endif
1.1       root     12917:        
                   12918:        if(!(REG8(AL) < 26)) {
                   12919:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     12920:                m_CF = 1;
1.1       root     12921:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   12922:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     12923:                m_CF = 1;
1.1       root     12924:        } else {
                   12925:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   12926:                char dev[64];
                   12927:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   12928:                
                   12929:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   12930:                if(hFile == INVALID_HANDLE_VALUE) {
                   12931:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     12932:                        m_CF = 1;
1.1       root     12933:                } else {
1.1.1.19  root     12934:                        UINT32 top_sector  = REG16(DX);
                   12935:                        UINT16 sector_num  = REG16(CX);
                   12936:                        UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   12937:                        
                   12938:                        if(sector_num == 0xffff) {
                   12939:                                top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   12940:                                sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   12941:                                UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   12942:                                UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   12943:                                buffer_addr = (seg << 4) + ofs;
                   12944:                        }
                   12945: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   12946: //                             REG8(AL) = 0x02; // drive not ready
                   12947: //                             m_CF = 1;
                   12948: //                     } else 
                   12949:                        if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     12950:                                REG8(AL) = 0x08; // sector not found
1.1.1.3   root     12951:                                m_CF = 1;
1.1.1.19  root     12952:                        } else if(ReadFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     12953:                                REG8(AL) = 0x0b; // read error
1.1.1.3   root     12954:                                m_CF = 1;
1.1       root     12955:                        }
                   12956:                        CloseHandle(hFile);
                   12957:                }
                   12958:        }
                   12959: }
                   12960: 
                   12961: inline void msdos_int_26h()
                   12962: {
1.1.1.42  root     12963:        // this operation may cause serious damage for drives, so support only floppy disk...
1.1       root     12964:        UINT16 seg, ofs;
                   12965:        DWORD dwSize;
                   12966:        
1.1.1.3   root     12967: #if defined(HAS_I386)
                   12968:        I386OP(pushf)();
                   12969: #else
                   12970:        PREFIX86(_pushf());
                   12971: #endif
1.1       root     12972:        
                   12973:        if(!(REG8(AL) < 26)) {
                   12974:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     12975:                m_CF = 1;
1.1       root     12976:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   12977:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     12978:                m_CF = 1;
1.1       root     12979:        } else {
                   12980:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   12981:                char dev[64];
                   12982:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   12983:                
                   12984:                if(dpb->media_type == 0xf8) {
                   12985:                        // this drive is not a floppy
1.1.1.6   root     12986: //                     if(!(((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag & 0x40)) {
                   12987: //                             fatalerror("This application tried the absolute disk write to drive %c:\n", 'A' + REG8(AL));
                   12988: //                     }
1.1       root     12989:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     12990:                        m_CF = 1;
1.1       root     12991:                } else {
                   12992:                        HANDLE hFile = CreateFile(dev, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   12993:                        if(hFile == INVALID_HANDLE_VALUE) {
                   12994:                                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     12995:                                m_CF = 1;
1.1       root     12996:                        } else {
1.1.1.19  root     12997:                                UINT32 top_sector  = REG16(DX);
                   12998:                                UINT16 sector_num  = REG16(CX);
                   12999:                                UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   13000:                                
                   13001:                                if(sector_num == 0xffff) {
                   13002:                                        top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   13003:                                        sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   13004:                                        UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   13005:                                        UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   13006:                                        buffer_addr = (seg << 4) + ofs;
                   13007:                                }
1.1       root     13008:                                if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   13009:                                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13010:                                        m_CF = 1;
1.1.1.19  root     13011:                                } else if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     13012:                                        REG8(AL) = 0x08; // sector not found
1.1.1.3   root     13013:                                        m_CF = 1;
1.1.1.19  root     13014:                                } else if(WriteFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     13015:                                        REG8(AL) = 0x0a; // write error
1.1.1.3   root     13016:                                        m_CF = 1;
1.1       root     13017:                                }
                   13018:                                CloseHandle(hFile);
                   13019:                        }
                   13020:                }
                   13021:        }
                   13022: }
                   13023: 
                   13024: inline void msdos_int_27h()
                   13025: {
1.1.1.29  root     13026:        int paragraphs = (min(REG16(DX), 0xfff0) + 15) >> 4;
                   13027:        try {
                   13028:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   13029:        } catch(...) {
                   13030:                // recover the broken mcb
                   13031:                int mcb_seg = SREG(CS) - 1;
                   13032:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     13033:                
1.1.1.29  root     13034:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     13035:                        mcb->mz = 'M';
                   13036:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   13037:                        
1.1.1.29  root     13038:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39  root     13039:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29  root     13040:                        } else {
1.1.1.39  root     13041:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29  root     13042:                        }
                   13043:                } else {
                   13044:                        mcb->mz = 'Z';
1.1.1.30  root     13045:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     13046:                }
                   13047:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   13048:        }
1.1.1.3   root     13049:        msdos_process_terminate(SREG(CS), retval | 0x300, 0);
1.1       root     13050: }
                   13051: 
                   13052: inline void msdos_int_29h()
                   13053: {
1.1.1.14  root     13054: #if 1
                   13055:        // need to check escape sequences
1.1       root     13056:        msdos_putch(REG8(AL));
1.1.1.14  root     13057: #else
                   13058:        DWORD num;
                   13059:        vram_flush();
1.1.1.23  root     13060:        WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), &REG8(AL), 1, &num, NULL);
1.1.1.14  root     13061:        cursor_moved = true;
                   13062: #endif
1.1       root     13063: }
                   13064: 
                   13065: inline void msdos_int_2eh()
                   13066: {
                   13067:        char tmp[MAX_PATH], command[MAX_PATH], opt[MAX_PATH];
                   13068:        memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     13069:        strcpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
1.1       root     13070:        char *token = my_strtok(tmp, " ");
                   13071:        strcpy(command, token);
                   13072:        strcpy(opt, token + strlen(token) + 1);
                   13073:        
                   13074:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   13075:        param->env_seg = 0;
                   13076:        param->cmd_line.w.l = 44;
                   13077:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   13078:        param->fcb1.w.l = 24;
                   13079:        param->fcb1.w.h = (WORK_TOP >> 4);
                   13080:        param->fcb2.w.l = 24;
                   13081:        param->fcb2.w.h = (WORK_TOP >> 4);
                   13082:        
                   13083:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   13084:        
                   13085:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   13086:        cmd_line->len = strlen(opt);
                   13087:        strcpy(cmd_line->cmd, opt);
                   13088:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   13089:        
1.1.1.28  root     13090:        try {
                   13091:                if(msdos_process_exec(command, param, 0)) {
                   13092:                        REG16(AX) = 0xffff; // error before processing command
                   13093:                } else {
                   13094:                        // set flag to set retval to ax when the started process is terminated
                   13095:                        process_t *process = msdos_process_info_get(current_psp);
                   13096:                        process->called_by_int2eh = true;
                   13097:                }
                   13098:        } catch(...) {
                   13099:                REG16(AX) = 0xffff; // error before processing command
                   13100:        }
1.1       root     13101: }
                   13102: 
1.1.1.29  root     13103: inline void msdos_int_2fh_05h()
                   13104: {
                   13105:        switch(REG8(AL)) {
                   13106:        case 0x00:
1.1.1.32  root     13107:                REG8(AL) = 0xff;
                   13108:                break;
                   13109:        case 0x01:
                   13110:        case 0x02:
                   13111:                for(int i = 0; i < array_length(standard_error_table); i++) {
                   13112:                        if(standard_error_table[i].code == REG16(BX) || standard_error_table[i].code == (UINT16)-1) {
                   13113:                                const char *message = NULL;
                   13114:                                if(active_code_page == 932) {
                   13115:                                        message = standard_error_table[i].message_japanese;
                   13116:                                }
                   13117:                                if(message == NULL) {
                   13118:                                        message = standard_error_table[i].message_english;
                   13119:                                }
                   13120:                                strcpy((char *)(mem + WORK_TOP), message);
                   13121:                                
                   13122:                                SREG(ES) = WORK_TOP >> 4;
                   13123:                                i386_load_segment_descriptor(ES);
                   13124:                                REG16(DI) = 0x0000;
                   13125:                                REG8(AL) = 0x01;
                   13126:                                break;
                   13127:                        }
                   13128:                }
1.1.1.29  root     13129:                break;
                   13130:        default:
                   13131:                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));
                   13132:                m_CF = 1;
                   13133:        }
                   13134: }
                   13135: 
1.1.1.44  root     13136: inline void msdos_int_2fh_06h()
                   13137: {
                   13138:        switch(REG8(AL)) {
                   13139:        case 0x00:
                   13140:                // ASSIGN is not installed
                   13141:                break;
                   13142:        case 0x01:
                   13143:                // this call is available from within MIRROR.COM even if ASSIGN is not installed
                   13144:                REG16(AX) = 0x01;
                   13145:                m_CF = 1;
                   13146:                break;
                   13147:        default:
                   13148:                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));
                   13149:                REG16(AX) = 0x01;
                   13150:                m_CF = 1;
                   13151:                break;
                   13152:        }
                   13153: }
                   13154: 
1.1.1.22  root     13155: inline void msdos_int_2fh_11h()
                   13156: {
                   13157:        switch(REG8(AL)) {
                   13158:        case 0x00:
1.1.1.29  root     13159:                if(i386_read_stack() == 0xdada) {
                   13160:                        // MSCDEX is not installed
                   13161: //                     REG8(AL) = 0x00;
                   13162:                } else {
                   13163:                        // Network Redirector is not installed
                   13164: //                     REG8(AL) = 0x00;
                   13165:                }
1.1.1.22  root     13166:                break;
                   13167:        default:
1.1.1.43  root     13168: //             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     13169:                REG16(AX) = 0x49; //  network software not installed
1.1.1.22  root     13170:                m_CF = 1;
                   13171:                break;
                   13172:        }
                   13173: }
                   13174: 
1.1.1.21  root     13175: inline void msdos_int_2fh_12h()
                   13176: {
                   13177:        switch(REG8(AL)) {
1.1.1.22  root     13178:        case 0x00:
1.1.1.29  root     13179:                // DOS 3.0+ internal functions are installed
1.1.1.22  root     13180:                REG8(AL) = 0xff;
                   13181:                break;
1.1.1.29  root     13182: //     case 0x01: // DOS 3.0+ internal - Close Current File
                   13183:        case 0x02:
                   13184:                {
                   13185:                        UINT16 stack = i386_read_stack();
                   13186:                        REG16(BX) = *(UINT16 *)(mem + 4 * stack + 0);
                   13187:                        SREG(ES) = *(UINT16 *)(mem + 4 * stack + 2);
                   13188:                        i386_load_segment_descriptor(ES);
                   13189:                }
                   13190:                break;
1.1.1.30  root     13191:        case 0x03:
                   13192:                SREG(DS) = (DEVICE_TOP >> 4);
                   13193:                i386_load_segment_descriptor(DS);
                   13194:                break;
1.1.1.29  root     13195:        case 0x04:
                   13196:                {
                   13197:                        UINT16 stack = i386_read_stack();
                   13198:                        REG8(AL) = (stack == '/') ? '\\' : stack;
                   13199: #if defined(HAS_I386)
                   13200:                        m_ZF = (REG8(AL) == '\\');
                   13201: #else
                   13202:                        m_ZeroVal = (REG8(AL) != '\\');
                   13203: #endif
                   13204:                }
                   13205:                break;
                   13206:        case 0x05:
                   13207:                msdos_putch(i386_read_stack());
                   13208:                break;
                   13209: //     case 0x06: // DOS 3.0+ internal - Invole Critical Error
                   13210: //     case 0x07: // DOS 3.0+ internal - Make Disk Buffer Most Recentry Used
                   13211: //     case 0x08: // DOS 3.0+ internal - Decrement SFT Reference Count
                   13212: //     case 0x09: // DOS 3.0+ internal - Flush and FREE Disk Buffer
                   13213: //     case 0x0a: // DOS 3.0+ internal - Perform Critical Error Interrupt
                   13214: //     case 0x0b: // DOS 3.0+ internal - Signal Sharing Violation to User
                   13215: //     case 0x0c: // DOS 3.0+ internal - Open Device and Set SFT Owner/Mode
                   13216:        case 0x0d:
                   13217:                {
                   13218:                        SYSTEMTIME time;
                   13219:                        FILETIME file_time;
                   13220:                        WORD dos_date, dos_time;
                   13221:                        GetLocalTime(&time);
                   13222:                        SystemTimeToFileTime(&time, &file_time);
                   13223:                        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   13224:                        REG16(AX) = dos_date;
                   13225:                        REG16(DX) = dos_time;
                   13226:                }
                   13227:                break;
                   13228: //     case 0x0e: // DOS 3.0+ internal - Mark All Disk Buffers Unreferenced
                   13229: //     case 0x0f: // DOS 3.0+ internal - Make Buffer Most Recentry Used
                   13230: //     case 0x10: // DOS 3.0+ internal - Find Unreferenced Disk Buffer
                   13231:        case 0x11:
                   13232:                {
                   13233:                        char path[MAX_PATH], *p;
                   13234:                        strcpy(path, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   13235:                        my_strupr(path);
                   13236:                        while((p = my_strchr(path, '/')) != NULL) {
                   13237:                                *p = '\\';
                   13238:                        }
                   13239:                        strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
                   13240:                }
                   13241:                break;
                   13242:        case 0x12:
                   13243:                REG16(CX) = strlen((char *)(mem + SREG_BASE(ES) + REG16(DI)));
                   13244:                break;
                   13245:        case 0x13:
                   13246:                {
                   13247:                        char tmp[2] = {0};
                   13248:                        tmp[0] = i386_read_stack();
                   13249:                        my_strupr(tmp);
                   13250:                        REG8(AL) = tmp[0];
                   13251:                }
                   13252:                break;
                   13253:        case 0x14:
                   13254: #if defined(HAS_I386)
                   13255:                m_ZF = (SREG_BASE(DS) + REG16(SI) == SREG_BASE(ES) + REG16(DI));
                   13256: #else
                   13257:                m_ZeroVal = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   13258: #endif
                   13259:                m_CF = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   13260:                break;
                   13261: //     case 0x15: // DOS 3.0+ internal - Flush Buffer
1.1.1.21  root     13262:        case 0x16:
                   13263:                if(REG16(BX) < 20) {
                   13264:                        SREG(ES) = SFT_TOP >> 4;
                   13265:                        i386_load_segment_descriptor(ES);
                   13266:                        REG16(DI) = 6 + 0x3b * REG16(BX);
                   13267:                        
                   13268:                        // update system file table
                   13269:                        UINT8* sft = mem + SFT_TOP + 6 + 0x3b * REG16(BX);
                   13270:                        if(file_handler[REG16(BX)].valid) {
                   13271:                                int count = 0;
                   13272:                                for(int i = 0; i < 20; i++) {
                   13273:                                        if(msdos_psp_get_file_table(i, current_psp) == REG16(BX)) {
                   13274:                                                count++;
                   13275:                                        }
                   13276:                                }
                   13277:                                *(UINT16 *)(sft + 0x00) = count ? count : 0xffff;
                   13278:                                *(UINT32 *)(sft + 0x15) = _tell(REG16(BX));
                   13279:                                _lseek(REG16(BX), 0, SEEK_END);
                   13280:                                *(UINT32 *)(sft + 0x11) = _tell(REG16(BX));
                   13281:                                _lseek(REG16(BX), *(UINT32 *)(sft + 0x15), SEEK_SET);
                   13282:                        } else {
                   13283:                                memset(sft, 0, 0x3b);
                   13284:                        }
                   13285:                } else {
                   13286:                        REG16(AX) = 0x06;
                   13287:                        m_CF = 1;
                   13288:                }
                   13289:                break;
1.1.1.29  root     13290: //     case 0x17: // DOS 3.0+ internal - Get Current Directory Structure for Drive
                   13291: //     case 0x18: // DOS 3.0+ internal - Get Caller's Registers
                   13292: //     case 0x19: // DOS 3.0+ internal - Set Drive???
                   13293:        case 0x1a:
                   13294:                {
                   13295:                        char *path = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full[MAX_PATH];
                   13296:                        if(path[1] == ':') {
                   13297:                                if(path[0] >= 'a' && path[0] <= 'z') {
                   13298:                                        REG8(AL) = path[0] - 'a' + 1;
                   13299:                                } else if(path[0] >= 'A' && path[0] <= 'Z') {
                   13300:                                        REG8(AL) = path[0] - 'A' + 1;
                   13301:                                } else {
                   13302:                                        REG8(AL) = 0xff; // invalid
                   13303:                                }
                   13304:                                strcpy(full, path);
                   13305:                                strcpy(path, full + 2);
                   13306:                        } else if(GetFullPathName(path, MAX_PATH, full, NULL) != 0 && full[1] == ':') {
                   13307:                                if(full[0] >= 'a' && full[0] <= 'z') {
                   13308:                                        REG8(AL) = full[0] - 'a' + 1;
                   13309:                                } else if(full[0] >= 'A' && full[0] <= 'Z') {
                   13310:                                        REG8(AL) = full[0] - 'A' + 1;
                   13311:                                } else {
                   13312:                                        REG8(AL) = 0xff; // invalid
                   13313:                                }
                   13314:                        } else {
                   13315:                                REG8(AL) = 0x00; // default
                   13316:                        }
                   13317:                }
                   13318:                break;
                   13319:        case 0x1b:
                   13320:                {
                   13321:                        int year = REG16(CX) + 1980;
                   13322:                        REG8(AL) = ((year % 4) == 0 && (year % 100) != 0 && (year % 400) == 0) ? 29 : 28;
                   13323:                }
                   13324:                break;
                   13325: //     case 0x1c: // DOS 3.0+ internal - Check Sum Memory
                   13326: //     case 0x1d: // DOS 3.0+ internal - Sum Memory
                   13327:        case 0x1e:
                   13328:                {
                   13329:                        char *path_1st = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full_1st[MAX_PATH];
                   13330:                        char *path_2nd = (char *)(mem + SREG_BASE(ES) + REG16(DI)), full_2nd[MAX_PATH];
                   13331:                        if(GetFullPathName(path_1st, MAX_PATH, full_1st, NULL) != 0 && GetFullPathName(path_2nd, MAX_PATH, full_2nd, NULL) != 0) {
                   13332: #if defined(HAS_I386)
                   13333:                                m_ZF = (strcmp(full_1st, full_2nd) == 0);
                   13334: #else
                   13335:                                m_ZeroVal = (strcmp(full_1st, full_2nd) != 0);
                   13336: #endif
                   13337:                        } else {
                   13338: #if defined(HAS_I386)
                   13339:                                m_ZF = (strcmp(path_1st, path_2nd) == 0);
                   13340: #else
                   13341:                                m_ZeroVal = (strcmp(path_1st, path_2nd) != 0);
                   13342: #endif
                   13343:                        }
                   13344:                }
                   13345:                break;
                   13346: //     case 0x1f: // DOS 3.0+ internal - Build Current Directory Structure
1.1.1.21  root     13347:        case 0x20:
                   13348:                {
                   13349:                        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   13350:                        
                   13351:                        if(fd < 20) {
                   13352:                                SREG(ES) = current_psp;
                   13353:                                i386_load_segment_descriptor(ES);
                   13354:                                REG16(DI) = offsetof(psp_t, file_table) + fd;
                   13355:                        } else {
                   13356:                                REG16(AX) = 0x06;
                   13357:                                m_CF = 1;
                   13358:                        }
                   13359:                }
                   13360:                break;
1.1.1.29  root     13361:        case 0x21:
                   13362:                msdos_int_21h_60h(0);
                   13363:                break;
                   13364: //     case 0x22: // DOS 3.0+ internal - Set Extended Error Info
                   13365: //     case 0x23: // DOS 3.0+ internal - Check If Character Device
                   13366: //     case 0x24: // DOS 3.0+ internal - Sharing Retry Delay
                   13367:        case 0x25:
                   13368:                REG16(CX) = strlen((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   13369:                break;
                   13370:        case 0x26:
                   13371:                REG8(AL) = REG8(CL);
                   13372:                msdos_int_21h_3dh();
                   13373:                break;
                   13374:        case 0x27:
                   13375:                msdos_int_21h_3eh();
                   13376:                break;
                   13377:        case 0x28:
                   13378:                REG16(AX) = REG16(BP);
                   13379:                msdos_int_21h_42h();
                   13380:                break;
                   13381:        case 0x29:
                   13382:                msdos_int_21h_3fh();
                   13383:                break;
                   13384: //     case 0x2a: // DOS 3.0+ internal - Set Fast Open Entry Point
                   13385:        case 0x2b:
                   13386:                REG16(AX) = REG16(BP);
                   13387:                msdos_int_21h_44h();
                   13388:                break;
                   13389:        case 0x2c:
                   13390:                REG16(BX) = DEVICE_TOP >> 4;
                   13391:                REG16(AX) = 22;
                   13392:                break;
                   13393:        case 0x2d:
                   13394:                {
                   13395:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   13396:                        REG16(AX) = sda->extended_error_code;
                   13397:                }
                   13398:                break;
                   13399:        case 0x2e:
                   13400:                if(REG8(DL) == 0x00 || REG8(DL) == 0x02 || REG8(DL) == 0x04 || REG8(DL) == 0x06) {
1.1.1.32  root     13401:                        SREG(ES) = 0x0001;
                   13402:                        i386_load_segment_descriptor(ES);
                   13403:                        REG16(DI) = 0x00;
                   13404:                } else if(REG8(DL) == 0x08) {
                   13405:                        // dummy parameter error message read routine is at fffd:0010
                   13406:                        SREG(ES) = 0xfffd;
1.1.1.22  root     13407:                        i386_load_segment_descriptor(ES);
1.1.1.32  root     13408:                        REG16(DI) = 0x0010;
1.1.1.22  root     13409:                }
                   13410:                break;
1.1.1.29  root     13411:        case 0x2f:
                   13412:                if(REG16(DX) != 0) {
1.1.1.30  root     13413:                        dos_major_version = REG8(DL);
                   13414:                        dos_minor_version = REG8(DH);
1.1.1.29  root     13415:                } else {
                   13416:                        REG8(DL) = 7;
                   13417:                        REG8(DH) = 10;
                   13418:                }
                   13419:                break;
                   13420: //     case 0x30: // Windows95 - Find SFT Entry in Internal File Tables
                   13421: //     case 0x31: // Windows95 - Set/Clear "Report Windows to DOS Programs" Flag
1.1.1.22  root     13422:        default:
                   13423:                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));
                   13424:                REG16(AX) = 0x01;
                   13425:                m_CF = 1;
                   13426:                break;
                   13427:        }
                   13428: }
                   13429: 
1.1.1.30  root     13430: inline void msdos_int_2fh_13h()
                   13431: {
                   13432:        static UINT16 prevDS = 0, prevDX = 0;
                   13433:        static UINT16 prevES = 0, prevBX = 0;
                   13434:        UINT16 tmp;
                   13435:        
                   13436:        tmp = SREG(DS); SREG(DS) = prevDS; prevDS = tmp;
                   13437:        i386_load_segment_descriptor(DS);
                   13438:        tmp = REG16(DX); REG16(DX) = prevDX; prevDX = tmp;
                   13439:        
                   13440:        tmp = SREG(ES); SREG(ES) = prevES; prevES = tmp;
                   13441:        i386_load_segment_descriptor(ES);
                   13442:        tmp = REG16(BX); REG16(BX) = prevBX; prevBX = tmp;
                   13443: }
                   13444: 
1.1.1.22  root     13445: inline void msdos_int_2fh_14h()
                   13446: {
                   13447:        switch(REG8(AL)) {
                   13448:        case 0x00:
1.1.1.29  root     13449:                // NLSFUNC.COM is installed
                   13450:                REG8(AL) = 0xff;
1.1.1.25  root     13451:                break;
                   13452:        case 0x01:
                   13453:        case 0x03:
                   13454:                REG8(AL) = 0x00;
                   13455:                active_code_page = REG16(BX);
                   13456:                msdos_nls_tables_update();
                   13457:                break;
                   13458:        case 0x02:
                   13459:                REG8(AL) = get_extended_country_info(REG16(BP));
                   13460:                break;
                   13461:        case 0x04:
1.1.1.42  root     13462:                for(int i = 0;; i++) {
                   13463:                        if(country_table[i].code == REG16(DX)) {
                   13464:                                get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)), country_table[i].usPrimaryLanguage, country_table[i].usSubLanguage);
                   13465:                                break;
                   13466:                        } else if(country_table[i].code == -1) {
                   13467:                                get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)));
                   13468:                                break;
                   13469:                        }
                   13470:                }
1.1.1.25  root     13471:                REG8(AL) = 0x00;
1.1.1.22  root     13472:                break;
                   13473:        default:
                   13474:                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));
                   13475:                REG16(AX) = 0x01;
                   13476:                m_CF = 1;
                   13477:                break;
                   13478:        }
                   13479: }
                   13480: 
                   13481: inline void msdos_int_2fh_15h()
                   13482: {
                   13483:        switch(REG8(AL)) {
1.1.1.29  root     13484:        case 0x00: // CD-ROM - Installation Check
                   13485:                if(REG16(BX) == 0x0000) {
1.1.1.43  root     13486: #if 0
                   13487:                        // MSCDEX is installed
                   13488:                        REG16(BX) = 0;
                   13489:                        for(int i = 0, n = 0; i < 26; i++) {
1.1.1.44  root     13490:                                if(msdos_is_cdrom_drive(i)) {
                   13491:                                        if(REG16(BX) == 0) {
                   13492:                                                REG16(CX) = i;
1.1.1.43  root     13493:                                        }
1.1.1.44  root     13494:                                        REG16(BX)++;
1.1.1.43  root     13495:                                }
                   13496:                        }
                   13497: #else
1.1.1.29  root     13498:                        // MSCDEX is not installed
                   13499: //                     REG8(AL) = 0x00;
1.1.1.43  root     13500: #endif
1.1.1.29  root     13501:                } else {
                   13502:                        // GRAPHICS.COM is not installed
                   13503: //                     REG8(AL) = 0x00;
                   13504:                }
1.1.1.22  root     13505:                break;
1.1.1.43  root     13506:        case 0x0b:
1.1.1.44  root     13507:                // this call is available from within DOSSHELL even if MSCDEX is not installed
                   13508:                REG16(AX) = msdos_is_cdrom_drive(REG8(CL)) ? 0x5ad8 : 0x0000;
                   13509:                REG16(BX) = 0xadad;
1.1.1.43  root     13510:                break;
                   13511:        case 0x0d:
1.1.1.44  root     13512:                for(int i = 0, n = 0; i < 26; i++) {
                   13513:                        if(msdos_is_cdrom_drive(i)) {
                   13514:                                mem[SREG_BASE(ES) + REG16(BX) + (n++)] = i;
1.1.1.43  root     13515:                        }
                   13516:                }
                   13517:                break;
1.1.1.22  root     13518:        case 0xff:
1.1.1.29  root     13519:                if(REG16(BX) == 0x0000) {
                   13520:                        // CORELCDX is not installed
                   13521:                } else {
                   13522:                        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));
                   13523:                        REG16(AX) = 0x01;
                   13524:                        m_CF = 1;
                   13525:                }
1.1.1.22  root     13526:                break;
1.1.1.21  root     13527:        default:
1.1.1.22  root     13528:                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     13529:                REG16(AX) = 0x01;
                   13530:                m_CF = 1;
                   13531:                break;
                   13532:        }
                   13533: }
                   13534: 
1.1       root     13535: inline void msdos_int_2fh_16h()
                   13536: {
                   13537:        switch(REG8(AL)) {
                   13538:        case 0x00:
1.1.1.14  root     13539:                if(no_windows) {
1.1.1.29  root     13540:                        // neither Windows 3.x enhanced mode nor Windows/386 2.x running
                   13541: //                     REG8(AL) = 0x00;
1.1.1.14  root     13542:                } else {
1.1.1.30  root     13543:                        REG8(AL) = win_major_version;
                   13544:                        REG8(AH) = win_minor_version;
1.1       root     13545:                }
                   13546:                break;
1.1.1.43  root     13547:        case 0x05: // Windows Enhanced Mode & 286 DOSX Init Broadcast
1.1.1.30  root     13548:                // from DOSBox
                   13549:                i386_set_a20_line(1);
                   13550:                break;
1.1.1.43  root     13551:        case 0x06: // Windows Enhanced Mode & 286 DOSX exit Broadcast
                   13552:        case 0x08: // Windows Enhanced Mode Init Complete Broadcast
                   13553:        case 0x09: // Windows Enhanced Mode Begin Exit Broadcast
                   13554:                break;
                   13555:        case 0x07:
                   13556:                // Virtual Device Call API
                   13557:                break;
1.1.1.22  root     13558:        case 0x0a:
                   13559:                if(!no_windows) {
                   13560:                        REG16(AX) = 0x0000;
1.1.1.30  root     13561:                        REG8(BH) = win_major_version;
                   13562:                        REG8(BL) = win_minor_version;
1.1.1.22  root     13563:                        REG16(CX) = 0x0003; // enhanced
                   13564:                }
                   13565:                break;
1.1.1.30  root     13566:        case 0x0b:
                   13567:                // no TRS, keep ES:DI = 0000h:0000h
1.1.1.22  root     13568:        case 0x0e:
                   13569:        case 0x0f:
1.1.1.30  root     13570:        case 0x10:
1.1.1.22  root     13571:        case 0x11:
                   13572:        case 0x12:
                   13573:        case 0x13:
                   13574:        case 0x14:
1.1.1.30  root     13575:        case 0x15:
1.1.1.43  root     13576:        case 0x81:
                   13577:        case 0x82:
1.1.1.44  root     13578:        case 0x84:
1.1.1.33  root     13579:        case 0x86:
1.1.1.22  root     13580:        case 0x87:
1.1.1.30  root     13581:        case 0x89:
1.1.1.33  root     13582:        case 0x8a:
1.1.1.22  root     13583:                // function not supported, do not clear AX
                   13584:                break;
1.1.1.14  root     13585:        case 0x80:
                   13586:                Sleep(10);
1.1.1.35  root     13587:                REQUEST_HARDWRE_UPDATE();
1.1.1.29  root     13588:                REG8(AL) = 0x00;
1.1.1.14  root     13589:                break;
1.1.1.33  root     13590:        case 0x83:
                   13591:                REG16(BX) = 0x01; // system vm id
                   13592:                break;
1.1.1.22  root     13593:        case 0x8e:
                   13594:                REG16(AX) = 0x00; // failed
                   13595:                break;
1.1.1.20  root     13596:        case 0x8f:
                   13597:                switch(REG8(DH)) {
                   13598:                case 0x00:
                   13599:                case 0x02:
                   13600:                case 0x03:
                   13601:                        REG16(AX) = 0x00;
                   13602:                        break;
                   13603:                case 0x01:
                   13604:                        REG16(AX) = 0x168f;
                   13605:                        break;
                   13606:                }
                   13607:                break;
1.1       root     13608:        default:
1.1.1.22  root     13609:                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));
                   13610:                REG16(AX) = 0x01;
                   13611:                m_CF = 1;
                   13612:                break;
                   13613:        }
                   13614: }
                   13615: 
                   13616: inline void msdos_int_2fh_19h()
                   13617: {
                   13618:        switch(REG8(AL)) {
                   13619:        case 0x00:
1.1.1.29  root     13620:                // SHELLB.COM is not installed
                   13621: //             REG8(AL) = 0x00;
1.1.1.22  root     13622:                break;
                   13623:        case 0x01:
                   13624:        case 0x02:
                   13625:        case 0x03:
                   13626:        case 0x04:
                   13627:                REG16(AX) = 0x01;
                   13628:                m_CF = 1;
                   13629:                break;
1.1.1.29  root     13630:        case 0x80:
                   13631:                // IBM ROM-DOS v4.0 is not installed
                   13632: //             REG8(AL) = 0x00;
                   13633:                break;
1.1.1.22  root     13634:        default:
                   13635:                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     13636:                REG16(AX) = 0x01;
1.1.1.3   root     13637:                m_CF = 1;
1.1       root     13638:                break;
                   13639:        }
                   13640: }
                   13641: 
                   13642: inline void msdos_int_2fh_1ah()
                   13643: {
                   13644:        switch(REG8(AL)) {
                   13645:        case 0x00:
1.1.1.29  root     13646:                // ANSI.SYS is installed
1.1       root     13647:                REG8(AL) = 0xff;
                   13648:                break;
                   13649:        default:
1.1.1.22  root     13650:                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));
                   13651:                REG16(AX) = 0x01;
                   13652:                m_CF = 1;
                   13653:                break;
                   13654:        }
                   13655: }
                   13656: 
1.1.1.30  root     13657: inline void msdos_int_2fh_40h()
1.1.1.22  root     13658: {
                   13659:        switch(REG8(AL)) {
                   13660:        case 0x00:
1.1.1.30  root     13661:                // Windows 3+ - Get Virtual Device Driver (VDD) Capabilities
                   13662:                REG8(AL) = 0x01; // does not virtualize video access
1.1.1.22  root     13663:                break;
1.1.1.43  root     13664:        case 0x10:
                   13665:                // OS/2 v2.0+ - Installation Check
                   13666:                REG16(AX) = 0x01;
                   13667:                m_CF = 1;
                   13668:                break;
1.1.1.22  root     13669:        default:
                   13670:                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     13671:                REG16(AX) = 0x01;
1.1.1.3   root     13672:                m_CF = 1;
1.1       root     13673:                break;
                   13674:        }
                   13675: }
                   13676: 
                   13677: inline void msdos_int_2fh_43h()
                   13678: {
                   13679:        switch(REG8(AL)) {
                   13680:        case 0x00:
1.1.1.29  root     13681:                // XMS is installed ?
1.1.1.19  root     13682: #ifdef SUPPORT_XMS
                   13683:                if(support_xms) {
                   13684:                        REG8(AL) = 0x80;
1.1.1.44  root     13685:                }
                   13686: #endif
                   13687:                break;
                   13688:        case 0x08:
                   13689: #ifdef SUPPORT_XMS
                   13690:                if(support_xms) {
                   13691:                        REG8(AL) = 0x43;
                   13692:                        REG8(BL) = 0x01; // IBM PC/AT
                   13693:                        REG8(BH) = 0x01; // Fast AT A20 switch time
                   13694:                }
1.1.1.19  root     13695: #endif
                   13696:                break;
                   13697:        case 0x10:
                   13698:                SREG(ES) = XMS_TOP >> 4;
                   13699:                i386_load_segment_descriptor(ES);
1.1.1.26  root     13700:                REG16(BX) = 0x15;
1.1       root     13701:                break;
1.1.1.44  root     13702:        case 0xe0:
                   13703:                // DOS Protected Mode Services (DPMS) v1.0 is not installed
                   13704:                if(REG16(BX) == 0x0000 && REG16(CX) == 0x4450 && REG16(DX) == 0x4d53) {
                   13705:                        break;
                   13706:                }
1.1       root     13707:        default:
1.1.1.22  root     13708:                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));
                   13709:                REG16(AX) = 0x01;
                   13710:                m_CF = 1;
                   13711:                break;
                   13712:        }
                   13713: }
                   13714: 
                   13715: inline void msdos_int_2fh_46h()
                   13716: {
                   13717:        switch(REG8(AL)) {
                   13718:        case 0x80:
1.1.1.29  root     13719:                // Windows v3.0 is not installed
                   13720: //             REG8(AL) = 0x00;
1.1.1.22  root     13721:                break;
                   13722:        default:
                   13723:                unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   13724:                REG16(AX) = 0x01;
                   13725:                m_CF = 1;
                   13726:                break;
                   13727:        }
                   13728: }
                   13729: 
                   13730: inline void msdos_int_2fh_48h()
                   13731: {
                   13732:        switch(REG8(AL)) {
                   13733:        case 0x00:
1.1.1.29  root     13734:                // DOSKEY is not installed
                   13735: //             REG8(AL) = 0x00;
1.1.1.22  root     13736:                break;
                   13737:        case 0x10:
                   13738:                msdos_int_21h_0ah();
                   13739:                REG16(AX) = 0x00;
                   13740:                break;
                   13741:        default:
                   13742:                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     13743:                REG16(AX) = 0x01;
1.1.1.3   root     13744:                m_CF = 1;
1.1       root     13745:                break;
                   13746:        }
                   13747: }
                   13748: 
                   13749: inline void msdos_int_2fh_4ah()
                   13750: {
                   13751:        switch(REG8(AL)) {
1.1.1.29  root     13752: #ifdef SUPPORT_HMA
                   13753:        case 0x01: // DOS 5.0+ - Query Free HMA Space
                   13754:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   13755:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   13756:                                // restore first free mcb in high memory area
                   13757:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   13758:                        }
                   13759:                        int offset = 0xffff;
                   13760:                        if((REG16(BX) = msdos_hma_mem_get_free(&offset)) != 0) {
                   13761:                                REG16(DI) = offset + 0x10;
                   13762:                        } else {
                   13763:                                REG16(DI) = 0xffff;
                   13764:                        }
                   13765:                } else {
                   13766:                        // HMA is already used
                   13767:                        REG16(BX) = 0;
                   13768:                        REG16(DI) = 0xffff;
                   13769:                }
                   13770:                SREG(ES) = 0xffff;
                   13771:                i386_load_segment_descriptor(ES);
                   13772:                break;
                   13773:        case 0x02: // DOS 5.0+ - Allocate HMA Space
                   13774:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   13775:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   13776:                                // restore first free mcb in high memory area
                   13777:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   13778:                        }
                   13779:                        int size = REG16(BX), offset;
                   13780:                        if((size % 16) != 0) {
                   13781:                                size &= ~15;
                   13782:                                size += 16;
                   13783:                        }
                   13784:                        if((offset = msdos_hma_mem_alloc(size, current_psp)) != -1) {
                   13785:                                REG16(BX) = size;
                   13786:                                REG16(DI) = offset + 0x10;
                   13787:                                is_hma_used_by_int_2fh = true;
                   13788:                        } else {
                   13789:                                REG16(BX) = 0;
                   13790:                                REG16(DI) = 0xffff;
                   13791:                        }
                   13792:                } else {
                   13793:                        // HMA is already used
                   13794:                        REG16(BX) = 0;
                   13795:                        REG16(DI) = 0xffff;
                   13796:                }
                   13797:                SREG(ES) = 0xffff;
                   13798:                i386_load_segment_descriptor(ES);
                   13799:                break;
                   13800:        case 0x03: // Windows95 - (De)Allocate HMA Memory Block
                   13801:                if(REG8(DL) == 0x00) {
                   13802:                        if(!is_hma_used_by_xms) {
                   13803:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   13804:                                        // restore first free mcb in high memory area
                   13805:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   13806:                                        is_hma_used_by_int_2fh = false;
                   13807:                                }
                   13808:                                int size = REG16(BX), offset;
                   13809:                                if((size % 16) != 0) {
                   13810:                                        size &= ~15;
                   13811:                                        size += 16;
                   13812:                                }
                   13813:                                if((offset = msdos_hma_mem_alloc(size, REG16(CX))) != -1) {
                   13814: //                                     REG16(BX) = size;
                   13815:                                        SREG(ES) = 0xffff;
                   13816:                                        i386_load_segment_descriptor(ES);
                   13817:                                        REG16(DI) = offset + 0x10;
                   13818:                                        is_hma_used_by_int_2fh = true;
                   13819:                                } else {
                   13820:                                        REG16(DI) = 0xffff;
                   13821:                                }
                   13822:                        } else {
                   13823:                                REG16(DI) = 0xffff;
                   13824:                        }
                   13825:                } else if(REG8(DL) == 0x01) {
                   13826:                        if(!is_hma_used_by_xms) {
                   13827:                                int size = REG16(BX);
                   13828:                                if((size % 16) != 0) {
                   13829:                                        size &= ~15;
                   13830:                                        size += 16;
                   13831:                                }
                   13832:                                if(msdos_hma_mem_realloc(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10, size) != -1) {
                   13833:                                        // memory block address is not changed
                   13834:                                } else {
                   13835:                                        REG16(DI) = 0xffff;
                   13836:                                }
                   13837:                        } else {
                   13838:                                REG16(DI) = 0xffff;
                   13839:                        }
                   13840:                } else if(REG8(DL) == 0x02) {
                   13841:                        if(!is_hma_used_by_xms) {
                   13842:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   13843:                                        // restore first free mcb in high memory area
                   13844:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   13845:                                        is_hma_used_by_int_2fh = false;
                   13846:                                } else {
                   13847:                                        msdos_hma_mem_free(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10);
                   13848:                                        if(msdos_hma_mem_get_free(NULL) == 0xffe0) {
                   13849:                                                is_hma_used_by_int_2fh = false;
                   13850:                                        }
                   13851:                                }
                   13852:                        }
                   13853:                } else {
                   13854:                        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));
                   13855:                        REG16(AX) = 0x01;
                   13856:                        m_CF = 1;
                   13857:                }
                   13858:                break;
                   13859:        case 0x04: // Windows95 - Get Start of HMA Memory Chain
                   13860:                if(!is_hma_used_by_xms) {
                   13861:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   13862:                                // restore first free mcb in high memory area
                   13863:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   13864:                                is_hma_used_by_int_2fh = false;
                   13865:                        }
                   13866:                        REG16(AX) = 0x0000;
                   13867:                        SREG(ES) = 0xffff;
                   13868:                        i386_load_segment_descriptor(ES);
                   13869:                        REG16(DI) = 0x10;
                   13870:                }
                   13871:                break;
                   13872: #else
1.1       root     13873:        case 0x01:
                   13874:        case 0x02:
1.1.1.29  root     13875:                // HMA is already used
1.1.1.27  root     13876:                REG16(BX) = 0x0000;
1.1.1.3   root     13877:                SREG(ES) = 0xffff;
                   13878:                i386_load_segment_descriptor(ES);
1.1       root     13879:                REG16(DI) = 0xffff;
                   13880:                break;
1.1.1.19  root     13881:        case 0x03:
                   13882:                // unable to allocate
                   13883:                REG16(DI) = 0xffff;
                   13884:                break;
                   13885:        case 0x04:
                   13886:                // function not supported, do not clear AX
                   13887:                break;
1.1.1.29  root     13888: #endif
                   13889:        case 0x10:
1.1.1.42  root     13890:                switch(REG16(BX)) {
                   13891:                case 0x0000:
                   13892:                case 0x0001:
                   13893:                case 0x0002:
                   13894:                case 0x0003:
                   13895:                case 0x0004:
                   13896:                case 0x0005:
                   13897:                case 0x0006:
                   13898:                case 0x0007:
                   13899:                case 0x0008:
                   13900:                case 0x000a:
                   13901:                case 0x1234:
                   13902:                        // SMARTDRV v4.00+ is not installed
                   13903:                        break;
                   13904:                default:
1.1.1.29  root     13905:                        unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   13906:                        REG16(AX) = 0x01;
                   13907:                        m_CF = 1;
1.1.1.42  root     13908:                        break;
1.1.1.29  root     13909:                }
                   13910:                break;
                   13911:        case 0x11:
1.1.1.42  root     13912:                switch(REG16(BX)) {
                   13913:                case 0x0000:
                   13914:                case 0x0001:
                   13915:                case 0x0002:
                   13916:                case 0x0003:
                   13917:                case 0x0004:
                   13918:                case 0x0005:
                   13919:                case 0x0006:
                   13920:                case 0x0007:
                   13921:                case 0x0008:
                   13922:                case 0x0009:
                   13923:                case 0x000a:
                   13924:                case 0x000b:
                   13925:                case 0xfffe:
                   13926:                case 0xffff:
1.1.1.29  root     13927:                        // DBLSPACE.BIN is not installed
1.1.1.42  root     13928:                        break;
                   13929:                default:
                   13930:                        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));
                   13931:                        REG16(AX) = 0x01;
                   13932:                        m_CF = 1;
                   13933:                        break;
                   13934:                }
                   13935:                break;
                   13936:        case 0x12:
                   13937:                if(REG16(CX) == 0x4d52 && REG16(DX) == 0x4349) {
                   13938:                        // Microsoft Realtime Compression Interface (MRCI) is not installed
                   13939:                } else if(REG16(CX) == 0x5354 && REG16(DX) == 0x4143) {
                   13940:                        // Stacker 4 LZS Compression Interface (LZSAPI) is not installed
1.1.1.29  root     13941:                } else {
                   13942:                        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));
                   13943:                        REG16(AX) = 0x01;
                   13944:                        m_CF = 1;
                   13945:                }
1.1.1.22  root     13946:                break;
1.1.1.42  root     13947:        case 0x13:
                   13948:                // DBLSPACE.BIN is not installed
                   13949:                break;
1.1.1.22  root     13950:        default:
                   13951:                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));
                   13952:                REG16(AX) = 0x01;
                   13953:                m_CF = 1;
                   13954:                break;
                   13955:        }
                   13956: }
                   13957: 
                   13958: inline void msdos_int_2fh_4bh()
                   13959: {
                   13960:        switch(REG8(AL)) {
1.1.1.24  root     13961:        case 0x01:
1.1.1.22  root     13962:        case 0x02:
1.1.1.29  root     13963:                // Task Switcher is not installed
1.1.1.24  root     13964:                break;
                   13965:        case 0x03:
                   13966:                // this call is available from within DOSSHELL even if the task switcher is not installed
                   13967:                REG16(AX) = REG16(BX) = 0x0000; // no more avaiable switcher id
1.1.1.22  root     13968:                break;
1.1.1.30  root     13969:        case 0x04:
                   13970:                REG16(BX) = 0x0000; // free switcher id successfully
                   13971:                break;
1.1.1.43  root     13972:        case 0x05:
                   13973:                REG16(BX) = 0x0000; // no instance data chain
                   13974:                SREG(ES) = 0x0000;
                   13975:                i386_load_segment_descriptor(ES);
                   13976:                break;
1.1       root     13977:        default:
1.1.1.22  root     13978:                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     13979:                REG16(AX) = 0x01;
1.1.1.3   root     13980:                m_CF = 1;
1.1       root     13981:                break;
                   13982:        }
                   13983: }
                   13984: 
1.1.1.44  root     13985: inline void msdos_int_2fh_4dh()
                   13986: {
                   13987:        switch(REG8(AL)) {
                   13988:        case 0x00:
                   13989:                // KKCFUNC is not installed ???
                   13990:                break;
                   13991:        default:
                   13992: //             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));
                   13993:                REG16(AX) = 0x01; // invalid function
                   13994:                m_CF = 1;
                   13995:                break;
                   13996:        }
                   13997: }
                   13998: 
1.1       root     13999: inline void msdos_int_2fh_4fh()
                   14000: {
                   14001:        switch(REG8(AL)) {
                   14002:        case 0x00:
1.1.1.29  root     14003:                // BILING is installed
1.1.1.27  root     14004:                REG16(AX) = 0x0000;
                   14005:                REG8(DL) = 0x01;        // major version
                   14006:                REG8(DH) = 0x00;        // minor version
1.1       root     14007:                break;
                   14008:        case 0x01:
1.1.1.27  root     14009:                REG16(AX) = 0x0000;
1.1       root     14010:                REG16(BX) = active_code_page;
                   14011:                break;
                   14012:        default:
1.1.1.22  root     14013:                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));
                   14014:                REG16(AX) = 0x01;
                   14015:                m_CF = 1;
                   14016:                break;
                   14017:        }
                   14018: }
                   14019: 
                   14020: inline void msdos_int_2fh_55h()
                   14021: {
                   14022:        switch(REG8(AL)) {
                   14023:        case 0x00:
                   14024:        case 0x01:
                   14025: //             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));
                   14026:                break;
                   14027:        default:
                   14028:                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     14029:                REG16(AX) = 0x01;
1.1.1.3   root     14030:                m_CF = 1;
1.1       root     14031:                break;
                   14032:        }
                   14033: }
                   14034: 
1.1.1.44  root     14035: inline void msdos_int_2fh_56h()
                   14036: {
                   14037:        switch(REG8(AL)) {
                   14038:        case 0x00:
                   14039:                // INTERLNK is not installed
                   14040:                break;
                   14041:        case 0x01:
                   14042:                // this call is available from within SCANDISK even if INTERLNK is not installed
                   14043: //             if(msdos_is_remote_drive(REG8(BH))) {
                   14044: //                     REG8(AL) = 0x00;
                   14045: //             }
                   14046:                break;
                   14047:        default:
                   14048:                unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   14049:                REG16(AX) = 0x01;
                   14050:                m_CF = 1;
                   14051:                break;
                   14052:        }
                   14053: }
                   14054: 
1.1.1.24  root     14055: inline void msdos_int_2fh_adh()
                   14056: {
                   14057:        switch(REG8(AL)) {
                   14058:        case 0x00:
1.1.1.29  root     14059:                // DISPLAY.SYS is installed
1.1.1.24  root     14060:                REG8(AL) = 0xff;
                   14061:                REG16(BX) = 0x100; // ???
                   14062:                break;
                   14063:        case 0x01:
                   14064:                active_code_page = REG16(BX);
                   14065:                msdos_nls_tables_update();
                   14066:                REG16(AX) = 0x01;
                   14067:                break;
                   14068:        case 0x02:
                   14069:                REG16(BX) = active_code_page;
                   14070:                break;
                   14071:        case 0x03:
                   14072:                // FIXME
                   14073:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 1;
                   14074:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2) = 3;
                   14075:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4) = 1;
                   14076:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 6) = active_code_page;
                   14077:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 8) = active_code_page;
                   14078:                break;
                   14079:        case 0x80:
                   14080:                break; // keyb.com is not installed
                   14081:        default:
                   14082:                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));
                   14083:                REG16(AX) = 0x01;
                   14084:                m_CF = 1;
                   14085:                break;
                   14086:        }
                   14087: }
                   14088: 
1.1       root     14089: inline void msdos_int_2fh_aeh()
                   14090: {
                   14091:        switch(REG8(AL)) {
                   14092:        case 0x00:
1.1.1.28  root     14093:                // FIXME: we need to check the given command line
                   14094:                REG8(AL) = 0x00; // the command should be executed as usual
                   14095: //             REG8(AL) = 0xff; // this command is a TSR extension to COMMAND.COM
1.1       root     14096:                break;
                   14097:        case 0x01:
                   14098:                {
                   14099:                        char command[MAX_PATH];
                   14100:                        memset(command, 0, sizeof(command));
1.1.1.3   root     14101:                        memcpy(command, mem + SREG_BASE(DS) + REG16(SI) + 1, mem[SREG_BASE(DS) + REG16(SI)]);
1.1       root     14102:                        
                   14103:                        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   14104:                        param->env_seg = 0;
                   14105:                        param->cmd_line.w.l = 44;
                   14106:                        param->cmd_line.w.h = (WORK_TOP >> 4);
                   14107:                        param->fcb1.w.l = 24;
                   14108:                        param->fcb1.w.h = (WORK_TOP >> 4);
                   14109:                        param->fcb2.w.l = 24;
                   14110:                        param->fcb2.w.h = (WORK_TOP >> 4);
                   14111:                        
                   14112:                        memset(mem + WORK_TOP + 24, 0x20, 20);
                   14113:                        
                   14114:                        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
1.1.1.3   root     14115:                        cmd_line->len = mem[SREG_BASE(DS) + REG16(BX) + 1];
                   14116:                        memcpy(cmd_line->cmd, mem + SREG_BASE(DS) + REG16(BX) + 2, cmd_line->len);
1.1       root     14117:                        cmd_line->cmd[cmd_line->len] = 0x0d;
                   14118:                        
1.1.1.28  root     14119:                        try {
                   14120:                                msdos_process_exec(command, param, 0);
                   14121:                        } catch(...) {
                   14122:                                fatalerror("failed to start '%s' by int 2Fh, AX=AE01h\n", command);
1.1       root     14123:                        }
                   14124:                }
                   14125:                break;
                   14126:        default:
1.1.1.22  root     14127:                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     14128:                REG16(AX) = 0x01;
1.1.1.3   root     14129:                m_CF = 1;
1.1       root     14130:                break;
                   14131:        }
                   14132: }
                   14133: 
1.1.1.34  root     14134: inline void msdos_int_2fh_b7h()
                   14135: {
                   14136:        switch(REG8(AL)) {
                   14137:        case 0x00:
                   14138:                // APPEND is not installed
                   14139: //             REG8(AL) = 0x00;
                   14140:                break;
1.1.1.44  root     14141:        case 0x06:
                   14142:                REG16(BX) = 0x0000;
                   14143:                break;
1.1.1.34  root     14144:        case 0x07:
1.1.1.43  root     14145:        case 0x11:
1.1.1.34  root     14146:                // COMMAND.COM calls this service without checking APPEND is installed
                   14147:                break;
                   14148:        default:
                   14149:                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));
                   14150:                REG16(AX) = 0x01;
                   14151:                m_CF = 1;
                   14152:                break;
                   14153:        }
                   14154: }
                   14155: 
1.1.1.24  root     14156: inline void msdos_int_33h_0000h()
                   14157: {
                   14158:        REG16(AX) = 0xffff; // hardware/driver installed
                   14159:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   14160: }
                   14161: 
                   14162: inline void msdos_int_33h_0001h()
                   14163: {
1.1.1.34  root     14164:        if(mouse.hidden > 0) {
                   14165:                mouse.hidden--;
                   14166:        }
                   14167:        if(mouse.hidden == 0) {
1.1.1.24  root     14168:                if(!(dwConsoleMode & ENABLE_MOUSE_INPUT)) {
                   14169:                        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode | ENABLE_MOUSE_INPUT);
                   14170:                }
                   14171:                pic[1].imr &= ~0x10;    // enable irq12
                   14172:        }
                   14173: }
                   14174: 
                   14175: inline void msdos_int_33h_0002h()
                   14176: {
1.1.1.34  root     14177:        mouse.hidden++;
                   14178: //     SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode & ~ENABLE_MOUSE_INPUT);
                   14179:        pic[1].imr |= 0x10;     // enable irq12
1.1.1.24  root     14180: }
                   14181: 
                   14182: inline void msdos_int_33h_0003h()
                   14183: {
1.1.1.34  root     14184: //     if(mouse.hidden > 0) {
                   14185:                update_console_input();
                   14186: //     }
1.1.1.24  root     14187:        REG16(BX) = mouse.get_buttons();
1.1.1.34  root     14188:        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   14189:        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
                   14190: }
                   14191: 
                   14192: inline void msdos_int_33h_0004h()
                   14193: {
                   14194:        mouse.position.x = REG16(CX);
                   14195:        mouse.position.x = REG16(DX);
1.1.1.24  root     14196: }
                   14197: 
                   14198: inline void msdos_int_33h_0005h()
                   14199: {
1.1.1.34  root     14200: //     if(mouse.hidden > 0) {
                   14201:                update_console_input();
                   14202: //     }
1.1.1.24  root     14203:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   14204:                int idx = REG16(BX);
1.1.1.34  root     14205:                REG16(BX) = min(mouse.buttons[idx].pressed_times, 0x7fff);
                   14206:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].pressed_position.x));
                   14207:                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     14208:                mouse.buttons[idx].pressed_times = 0;
                   14209:        } else {
                   14210:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   14211:        }
                   14212:        REG16(AX) = mouse.get_buttons();
                   14213: }
                   14214: 
                   14215: inline void msdos_int_33h_0006h()
                   14216: {
1.1.1.34  root     14217: //     if(mouse.hidden > 0) {
                   14218:                update_console_input();
                   14219: //     }
1.1.1.24  root     14220:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   14221:                int idx = REG16(BX);
1.1.1.34  root     14222:                REG16(BX) = min(mouse.buttons[idx].released_times, 0x7fff);
                   14223:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].released_position.x));
                   14224:                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     14225:                mouse.buttons[idx].released_times = 0;
                   14226:        } else {
                   14227:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   14228:        }
                   14229:        REG16(AX) = mouse.get_buttons();
                   14230: }
                   14231: 
                   14232: inline void msdos_int_33h_0007h()
                   14233: {
                   14234:        mouse.min_position.x = min(REG16(CX), REG16(DX));
                   14235:        mouse.max_position.x = max(REG16(CX), REG16(DX));
                   14236: }
                   14237: 
                   14238: inline void msdos_int_33h_0008h()
                   14239: {
                   14240:        mouse.min_position.y = min(REG16(CX), REG16(DX));
                   14241:        mouse.max_position.y = max(REG16(CX), REG16(DX));
                   14242: }
                   14243: 
                   14244: inline void msdos_int_33h_0009h()
                   14245: {
                   14246:        mouse.hot_spot[0] = REG16(BX);
                   14247:        mouse.hot_spot[1] = REG16(CX);
                   14248: }
                   14249: 
                   14250: inline void msdos_int_33h_000bh()
                   14251: {
1.1.1.34  root     14252: //     if(mouse.hidden > 0) {
                   14253:                update_console_input();
                   14254: //     }
1.1.1.24  root     14255:        int dx = (mouse.position.x - mouse.prev_position.x) * mouse.mickey.x / 8;
                   14256:        int dy = (mouse.position.y - mouse.prev_position.y) * mouse.mickey.y / 8;
                   14257:        mouse.prev_position.x = mouse.position.x;
                   14258:        mouse.prev_position.y = mouse.position.y;
                   14259:        REG16(CX) = dx;
                   14260:        REG16(DX) = dy;
                   14261: }
                   14262: 
                   14263: inline void msdos_int_33h_000ch()
                   14264: {
                   14265:        mouse.call_mask = REG16(CX);
                   14266:        mouse.call_addr.w.l = REG16(DX);
                   14267:        mouse.call_addr.w.h = SREG(ES);
                   14268: }
                   14269: 
                   14270: inline void msdos_int_33h_000fh()
                   14271: {
                   14272:        mouse.mickey.x = REG16(CX);
                   14273:        mouse.mickey.y = REG16(DX);
                   14274: }
                   14275: 
                   14276: inline void msdos_int_33h_0011h()
                   14277: {
                   14278:        REG16(AX) = 0xffff;
                   14279:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   14280: }
                   14281: 
                   14282: inline void msdos_int_33h_0014h()
                   14283: {
                   14284:        UINT16 old_mask = mouse.call_mask;
                   14285:        UINT16 old_ofs = mouse.call_addr.w.l;
                   14286:        UINT16 old_seg = mouse.call_addr.w.h;
                   14287:        
                   14288:        mouse.call_mask = REG16(CX);
                   14289:        mouse.call_addr.w.l = REG16(DX);
                   14290:        mouse.call_addr.w.h = SREG(ES);
                   14291:        
                   14292:        REG16(CX) = old_mask;
                   14293:        REG16(DX) = old_ofs;
                   14294:        SREG(ES) = old_seg;
                   14295:        i386_load_segment_descriptor(ES);
                   14296: }
                   14297: 
                   14298: inline void msdos_int_33h_0015h()
                   14299: {
                   14300:        REG16(BX) = sizeof(mouse);
                   14301: }
                   14302: 
                   14303: inline void msdos_int_33h_0016h()
                   14304: {
                   14305:        memcpy(mem + SREG_BASE(ES) + REG16(DX), &mouse, sizeof(mouse));
                   14306: }
                   14307: 
                   14308: inline void msdos_int_33h_0017h()
                   14309: {
                   14310:        memcpy(&mouse, mem + SREG_BASE(ES) + REG16(DX), sizeof(mouse));
                   14311: }
                   14312: 
1.1.1.43  root     14313: inline void msdos_int_33h_0018h()
                   14314: {
                   14315:        for(int i = 0; i < 8; i++) {
                   14316:                if(REG16(CX) & (1 << i)) {
                   14317:                        if(mouse.call_addr_alt[i].dw && !(REG16(DX) == 0 && SREG(ES) == 0)) {
                   14318:                                // event handler already exists
                   14319:                                REG16(AX) = 0xffff;
                   14320:                                break;
                   14321:                        }
                   14322:                        mouse.call_addr_alt[i].w.l = REG16(DX);
                   14323:                        mouse.call_addr_alt[i].w.h = SREG(ES);
                   14324:                }
                   14325:        }
                   14326: }
                   14327: 
                   14328: inline void msdos_int_33h_0019h()
                   14329: {
                   14330:        UINT16 call_mask = REG16(CX);
                   14331:        
                   14332:        REG16(CX) = 0;
                   14333:        
                   14334:        for(int i = 0; i < 8; i++) {
                   14335:                if((call_mask & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   14336:                        for(int j = 0; j < 8; j++) {
                   14337:                                if((call_mask & (1 << j)) && mouse.call_addr_alt[i].dw == mouse.call_addr_alt[j].dw) {
                   14338:                                        REG16(CX) |= (1 << j);
                   14339:                                }
                   14340:                        }
                   14341:                        REG16(DX) = mouse.call_addr_alt[i].w.l;
                   14342:                        REG16(BX) = mouse.call_addr_alt[i].w.h;
                   14343:                        break;
                   14344:                }
                   14345:        }
                   14346: }
                   14347: 
1.1.1.24  root     14348: inline void msdos_int_33h_001ah()
                   14349: {
                   14350:        mouse.sensitivity[0] = REG16(BX);
                   14351:        mouse.sensitivity[1] = REG16(CX);
                   14352:        mouse.sensitivity[2] = REG16(DX);
                   14353: }
                   14354: 
                   14355: inline void msdos_int_33h_001bh()
                   14356: {
                   14357:        REG16(BX) = mouse.sensitivity[0];
                   14358:        REG16(CX) = mouse.sensitivity[1];
                   14359:        REG16(DX) = mouse.sensitivity[2];
                   14360: }
                   14361: 
                   14362: inline void msdos_int_33h_001dh()
                   14363: {
                   14364:        mouse.display_page = REG16(BX);
                   14365: }
                   14366: 
                   14367: inline void msdos_int_33h_001eh()
                   14368: {
                   14369:        REG16(BX) = mouse.display_page;
                   14370: }
                   14371: 
1.1.1.34  root     14372: inline void msdos_int_33h_001fh()
                   14373: {
                   14374:        // from DOSBox
                   14375:        REG16(BX) = 0x0000;
                   14376:        SREG(ES) = 0x0000;
                   14377:        i386_load_segment_descriptor(ES);
                   14378:        mouse.enabled = false;
                   14379:        mouse.old_hidden = mouse.hidden;
                   14380:        mouse.hidden = 1;
                   14381: }
                   14382: 
                   14383: inline void msdos_int_33h_0020h()
                   14384: {
                   14385:        // from DOSBox
                   14386:        mouse.enabled = true;
                   14387:        mouse.hidden = mouse.old_hidden;
                   14388: }
                   14389: 
1.1.1.24  root     14390: inline void msdos_int_33h_0021h()
                   14391: {
                   14392:        REG16(AX) = 0xffff;
                   14393:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   14394: }
                   14395: 
                   14396: inline void msdos_int_33h_0022h()
                   14397: {
                   14398:        mouse.language = REG16(BX);
                   14399: }
                   14400: 
                   14401: inline void msdos_int_33h_0023h()
                   14402: {
                   14403:        REG16(BX) = mouse.language;
                   14404: }
                   14405: 
                   14406: inline void msdos_int_33h_0024h()
                   14407: {
                   14408:        REG16(BX) = 0x0805; // V8.05
                   14409:        REG16(CX) = 0x0400; // PS/2
                   14410: }
                   14411: 
                   14412: inline void msdos_int_33h_0026h()
                   14413: {
                   14414:        REG16(BX) = 0x0000;
                   14415:        REG16(CX) = mouse.max_position.x;
                   14416:        REG16(DX) = mouse.max_position.y;
                   14417: }
                   14418: 
                   14419: inline void msdos_int_33h_002ah()
                   14420: {
1.1.1.34  root     14421:        REG16(AX) = -mouse.hidden;
1.1.1.24  root     14422:        REG16(BX) = mouse.hot_spot[0];
                   14423:        REG16(CX) = mouse.hot_spot[1];
                   14424:        REG16(DX) = 4; // PS/2
                   14425: }
                   14426: 
                   14427: inline void msdos_int_33h_0031h()
                   14428: {
                   14429:        REG16(AX) = mouse.min_position.x;
                   14430:        REG16(BX) = mouse.min_position.y;
                   14431:        REG16(CX) = mouse.max_position.x;
                   14432:        REG16(DX) = mouse.max_position.y;
                   14433: }
                   14434: 
                   14435: inline void msdos_int_33h_0032h()
                   14436: {
                   14437:        REG16(AX) = 0;
                   14438: //     REG16(AX) |= 0x8000; // 0025h
                   14439:        REG16(AX) |= 0x4000; // 0026h
                   14440: //     REG16(AX) |= 0x2000; // 0027h
                   14441: //     REG16(AX) |= 0x1000; // 0028h
                   14442: //     REG16(AX) |= 0x0800; // 0029h
                   14443:        REG16(AX) |= 0x0400; // 002ah
                   14444: //     REG16(AX) |= 0x0200; // 002bh
                   14445: //     REG16(AX) |= 0x0100; // 002ch
                   14446: //     REG16(AX) |= 0x0080; // 002dh
                   14447: //     REG16(AX) |= 0x0040; // 002eh
                   14448:        REG16(AX) |= 0x0020; // 002fh
                   14449: //     REG16(AX) |= 0x0010; // 0030h
                   14450:        REG16(AX) |= 0x0008; // 0031h
                   14451:        REG16(AX) |= 0x0004; // 0032h
                   14452: //     REG16(AX) |= 0x0002; // 0033h
                   14453: //     REG16(AX) |= 0x0001; // 0034h
                   14454: }
                   14455: 
1.1.1.19  root     14456: inline void msdos_int_67h_40h()
                   14457: {
                   14458:        if(!support_ems) {
                   14459:                REG8(AH) = 0x84;
                   14460:        } else {
                   14461:                REG8(AH) = 0x00;
                   14462:        }
                   14463: }
                   14464: 
                   14465: inline void msdos_int_67h_41h()
                   14466: {
                   14467:        if(!support_ems) {
                   14468:                REG8(AH) = 0x84;
                   14469:        } else {
                   14470:                REG8(AH) = 0x00;
                   14471:                REG16(BX) = EMS_TOP >> 4;
                   14472:        }
                   14473: }
                   14474: 
                   14475: inline void msdos_int_67h_42h()
                   14476: {
                   14477:        if(!support_ems) {
                   14478:                REG8(AH) = 0x84;
                   14479:        } else {
                   14480:                REG8(AH) = 0x00;
                   14481:                REG16(BX) = free_ems_pages;
                   14482:                REG16(DX) = MAX_EMS_PAGES;
                   14483:        }
                   14484: }
                   14485: 
                   14486: inline void msdos_int_67h_43h()
                   14487: {
                   14488:        if(!support_ems) {
                   14489:                REG8(AH) = 0x84;
                   14490:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   14491:                REG8(AH) = 0x87;
                   14492:        } else if(REG16(BX) > free_ems_pages) {
                   14493:                REG8(AH) = 0x88;
                   14494:        } else if(REG16(BX) == 0) {
                   14495:                REG8(AH) = 0x89;
                   14496:        } else {
1.1.1.31  root     14497:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     14498:                        if(!ems_handles[i].allocated) {
                   14499:                                ems_allocate_pages(i, REG16(BX));
                   14500:                                REG8(AH) = 0x00;
                   14501:                                REG16(DX) = i;
                   14502:                                return;
                   14503:                        }
                   14504:                }
                   14505:                REG8(AH) = 0x85;
                   14506:        }
                   14507: }
                   14508: 
                   14509: inline void msdos_int_67h_44h()
                   14510: {
                   14511:        if(!support_ems) {
                   14512:                REG8(AH) = 0x84;
1.1.1.31  root     14513:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     14514:                REG8(AH) = 0x83;
                   14515:        } else if(!(REG16(BX) == 0xffff || REG16(BX) < ems_handles[REG16(DX)].pages)) {
                   14516:                REG8(AH) = 0x8a;
                   14517: //     } else if(!(REG8(AL) < 4)) {
                   14518: //             REG8(AH) = 0x8b;
                   14519:        } else if(REG16(BX) == 0xffff) {
                   14520:                ems_unmap_page(REG8(AL) & 3);
                   14521:                REG8(AH) = 0x00;
                   14522:        } else {
                   14523:                ems_map_page(REG8(AL) & 3, REG16(DX), REG16(BX));
                   14524:                REG8(AH) = 0x00;
                   14525:        }
                   14526: }
                   14527: 
                   14528: inline void msdos_int_67h_45h()
                   14529: {
                   14530:        if(!support_ems) {
                   14531:                REG8(AH) = 0x84;
1.1.1.31  root     14532:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     14533:                REG8(AH) = 0x83;
                   14534:        } else {
                   14535:                ems_release_pages(REG16(DX));
                   14536:                REG8(AH) = 0x00;
                   14537:        }
                   14538: }
                   14539: 
                   14540: inline void msdos_int_67h_46h()
                   14541: {
                   14542:        if(!support_ems) {
                   14543:                REG8(AH) = 0x84;
                   14544:        } else {
1.1.1.29  root     14545: //             REG16(AX) = 0x0032; // EMS 3.2
                   14546:                REG16(AX) = 0x0040; // EMS 4.0
1.1.1.19  root     14547:        }
                   14548: }
                   14549: 
                   14550: inline void msdos_int_67h_47h()
                   14551: {
                   14552:        // NOTE: the map data should be stored in the specified ems page, not process data
                   14553:        process_t *process = msdos_process_info_get(current_psp);
                   14554:        
                   14555:        if(!support_ems) {
                   14556:                REG8(AH) = 0x84;
1.1.1.31  root     14557: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     14558: //             REG8(AH) = 0x83;
                   14559:        } else if(process->ems_pages_stored) {
                   14560:                REG8(AH) = 0x8d;
                   14561:        } else {
                   14562:                for(int i = 0; i < 4; i++) {
                   14563:                        process->ems_pages[i].handle = ems_pages[i].handle;
                   14564:                        process->ems_pages[i].page   = ems_pages[i].page;
                   14565:                        process->ems_pages[i].mapped = ems_pages[i].mapped;
                   14566:                }
                   14567:                process->ems_pages_stored = true;
                   14568:                REG8(AH) = 0x00;
                   14569:        }
                   14570: }
                   14571: 
                   14572: inline void msdos_int_67h_48h()
                   14573: {
                   14574:        // NOTE: the map data should be restored from the specified ems page, not process data
                   14575:        process_t *process = msdos_process_info_get(current_psp);
                   14576:        
                   14577:        if(!support_ems) {
                   14578:                REG8(AH) = 0x84;
1.1.1.31  root     14579: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     14580: //             REG8(AH) = 0x83;
                   14581:        } else if(!process->ems_pages_stored) {
                   14582:                REG8(AH) = 0x8e;
                   14583:        } else {
                   14584:                for(int i = 0; i < 4; i++) {
                   14585:                        if(process->ems_pages[i].mapped) {
                   14586:                                ems_map_page(i, process->ems_pages[i].handle, process->ems_pages[i].page);
                   14587:                        } else {
                   14588:                                ems_unmap_page(i);
                   14589:                        }
                   14590:                }
                   14591:                process->ems_pages_stored = false;
                   14592:                REG8(AH) = 0x00;
                   14593:        }
                   14594: }
                   14595: 
                   14596: inline void msdos_int_67h_4bh()
                   14597: {
                   14598:        if(!support_ems) {
                   14599:                REG8(AH) = 0x84;
                   14600:        } else {
                   14601:                REG8(AH) = 0x00;
                   14602:                REG16(BX) = 0;
1.1.1.31  root     14603:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     14604:                        if(ems_handles[i].allocated) {
                   14605:                                REG16(BX)++;
                   14606:                        }
                   14607:                }
                   14608:        }
                   14609: }
                   14610: 
                   14611: inline void msdos_int_67h_4ch()
                   14612: {
                   14613:        if(!support_ems) {
                   14614:                REG8(AH) = 0x84;
1.1.1.31  root     14615:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     14616:                REG8(AH) = 0x83;
                   14617:        } else {
                   14618:                REG8(AH) = 0x00;
                   14619:                REG16(BX) = ems_handles[REG16(DX)].pages;
                   14620:        }
                   14621: }
                   14622: 
                   14623: inline void msdos_int_67h_4dh()
                   14624: {
                   14625:        if(!support_ems) {
                   14626:                REG8(AH) = 0x84;
                   14627:        } else {
                   14628:                REG8(AH) = 0x00;
                   14629:                REG16(BX) = 0;
1.1.1.31  root     14630:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     14631:                        if(ems_handles[i].allocated) {
                   14632:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 0) = i;
                   14633:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 2) = ems_handles[i].pages;
                   14634:                                REG16(BX)++;
                   14635:                        }
                   14636:                }
                   14637:        }
                   14638: }
                   14639: 
1.1.1.20  root     14640: inline void msdos_int_67h_4eh()
                   14641: {
                   14642:        if(!support_ems) {
                   14643:                REG8(AH) = 0x84;
                   14644:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   14645:                if(REG8(AL) == 0x00 || REG8(AL) == 0x02) {
                   14646:                        // save page map
                   14647:                        for(int i = 0; i < 4; i++) {
                   14648:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   14649:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   14650:                        }
                   14651:                }
                   14652:                if(REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   14653:                        // restore page map
                   14654:                        for(int i = 0; i < 4; i++) {
                   14655:                                UINT16 handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   14656:                                UINT16 page   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   14657:                                
1.1.1.31  root     14658:                                if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
1.1.1.20  root     14659:                                        ems_map_page(i, handle, page);
                   14660:                                } else {
                   14661:                                        ems_unmap_page(i);
                   14662:                                }
                   14663:                        }
                   14664:                }
                   14665:                REG8(AH) = 0x00;
                   14666:        } else if(REG8(AL) == 0x03) {
                   14667:                REG8(AH) = 0x00;
1.1.1.21  root     14668:                REG8(AL) = 4 * 4;
                   14669:        } else {
1.1.1.22  root     14670:                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     14671:                REG8(AH) = 0x8f;
                   14672:        }
                   14673: }
                   14674: 
                   14675: inline void msdos_int_67h_4fh()
                   14676: {
                   14677:        if(!support_ems) {
                   14678:                REG8(AH) = 0x84;
                   14679:        } else if(REG8(AL) == 0x00) {
                   14680:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   14681:                
                   14682:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI)) = count;
                   14683:                for(int i = 0; i < count; i++) {
                   14684:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 2 * i);
                   14685:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   14686:                        
                   14687: //                     if(!(physical < 4)) {
                   14688: //                             REG8(AH) = 0x8b;
                   14689: //                             return;
                   14690: //                     }
                   14691:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 0) = segment;
1.1.1.41  root     14692:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 2) = ems_pages[physical & 3].mapped ? ems_pages[physical & 3].handle : 0xffff;
                   14693:                        *(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     14694:                }
                   14695:                REG8(AH) = 0x00;
                   14696:        } else if(REG8(AL) == 0x01) {
                   14697:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   14698:                
                   14699:                for(int i = 0; i < count; i++) {
                   14700:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 0);
                   14701:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   14702:                        UINT16 handle   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 2);
                   14703:                        UINT16 logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 4);
                   14704:                        
                   14705: //                     if(!(physical < 4)) {
                   14706: //                             REG8(AH) = 0x8b;
                   14707: //                             return;
                   14708: //                     } else
1.1.1.41  root     14709:                        if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && logical < ems_handles[handle].pages) {
1.1.1.21  root     14710:                                ems_map_page(physical & 3, handle, logical);
                   14711:                        } else {
1.1.1.41  root     14712:                                ems_unmap_page(physical & 3);
1.1.1.21  root     14713:                        }
                   14714:                }
                   14715:                REG8(AH) = 0x00;
                   14716:        } else if(REG8(AL) == 0x02) {
                   14717:                REG8(AH) = 0x00;
                   14718:                REG8(AL) = 2 + REG16(BX) * 6;
1.1.1.20  root     14719:        } else {
1.1.1.22  root     14720:                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     14721:                REG8(AH) = 0x8f;
                   14722:        }
                   14723: }
                   14724: 
                   14725: inline void msdos_int_67h_50h()
                   14726: {
                   14727:        if(!support_ems) {
                   14728:                REG8(AH) = 0x84;
1.1.1.31  root     14729:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.20  root     14730:                REG8(AH) = 0x83;
                   14731:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   14732:                for(int i = 0; i < REG16(CX); i++) {
                   14733:                        int logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   14734:                        int physical = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   14735:                        
                   14736:                        if(REG8(AL) == 0x01) {
                   14737:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   14738:                        }
                   14739: //                     if(!(physical < 4)) {
                   14740: //                             REG8(AH) = 0x8b;
                   14741: //                             return;
                   14742: //                     } else
                   14743:                        if(logical == 0xffff) {
                   14744:                                ems_unmap_page(physical & 3);
                   14745:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   14746:                                ems_map_page(physical & 3, REG16(DX), logical);
                   14747:                        } else {
                   14748:                                REG8(AH) = 0x8a;
                   14749:                                return;
                   14750:                        }
                   14751:                }
                   14752:                REG8(AH) = 0x00;
                   14753:        } else {
1.1.1.22  root     14754:                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     14755:                REG8(AH) = 0x8f;
                   14756:        }
                   14757: }
                   14758: 
1.1.1.19  root     14759: inline void msdos_int_67h_51h()
                   14760: {
                   14761:        if(!support_ems) {
                   14762:                REG8(AH) = 0x84;
1.1.1.31  root     14763:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     14764:                REG8(AH) = 0x83;
                   14765:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   14766:                REG8(AH) = 0x87;
                   14767:        } else if(REG16(BX) > free_ems_pages + ems_handles[REG16(DX)].pages) {
                   14768:                REG8(AH) = 0x88;
                   14769:        } else {
                   14770:                ems_reallocate_pages(REG16(DX), REG16(BX));
                   14771:                REG8(AH) = 0x00;
                   14772:        }
                   14773: }
                   14774: 
1.1.1.20  root     14775: inline void msdos_int_67h_52h()
                   14776: {
                   14777:        if(!support_ems) {
                   14778:                REG8(AH) = 0x84;
1.1.1.31  root     14779: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   14780: //             REG8(AH) = 0x83;
1.1.1.20  root     14781:        } else if(REG8(AL) == 0x00) {
                   14782:                REG8(AL) = 0x00; // handle is volatile
                   14783:                REG8(AH) = 0x00;
                   14784:        } else if(REG8(AL) == 0x01) {
                   14785:                if(REG8(BL) == 0x00) {
                   14786:                        REG8(AH) = 0x00;
                   14787:                } else {
                   14788:                        REG8(AH) = 0x90; // undefined attribute type
                   14789:                }
                   14790:        } else if(REG8(AL) == 0x02) {
                   14791:                REG8(AL) = 0x00; // only volatile handles supported
                   14792:                REG8(AH) = 0x00;
                   14793:        } else {
1.1.1.22  root     14794:                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     14795:                REG8(AH) = 0x8f;
                   14796:        }
                   14797: }
                   14798: 
1.1.1.19  root     14799: inline void msdos_int_67h_53h()
                   14800: {
                   14801:        if(!support_ems) {
                   14802:                REG8(AH) = 0x84;
1.1.1.31  root     14803:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     14804:                REG8(AH) = 0x83;
                   14805:        } else if(REG8(AL) == 0x00) {
                   14806:                memcpy(mem + SREG_BASE(ES) + REG16(DI), ems_handles[REG16(DX)].name, 8);
                   14807:                REG8(AH) = 0x00;
                   14808:        } else if(REG8(AL) == 0x01) {
1.1.1.31  root     14809:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     14810:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   14811:                                REG8(AH) = 0xa1;
                   14812:                                return;
                   14813:                        }
                   14814:                }
                   14815:                REG8(AH) = 0x00;
                   14816:                memcpy(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8);
                   14817:        } else {
1.1.1.22  root     14818:                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     14819:                REG8(AH) = 0x8f;
1.1.1.19  root     14820:        }
                   14821: }
                   14822: 
                   14823: inline void msdos_int_67h_54h()
                   14824: {
                   14825:        if(!support_ems) {
                   14826:                REG8(AH) = 0x84;
                   14827:        } else if(REG8(AL) == 0x00) {
1.1.1.31  root     14828:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     14829:                        if(ems_handles[i].allocated) {
                   14830:                                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, ems_handles[i].name, 10);
                   14831:                        } else {
                   14832:                                memset(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, 0, 10);
                   14833:                        }
                   14834:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 0) = i;
                   14835:                }
                   14836:                REG8(AH) = 0x00;
                   14837:                REG8(AL) = MAX_EMS_HANDLES;
                   14838:        } else if(REG8(AL) == 0x01) {
                   14839:                REG8(AH) = 0xa0; // not found
1.1.1.31  root     14840:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     14841:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   14842:                                REG8(AH) = 0x00;
                   14843:                                REG16(DX) = i;
                   14844:                                break;
                   14845:                        }
                   14846:                }
                   14847:        } else if(REG8(AL) == 0x02) {
                   14848:                REG8(AH) = 0x00;
                   14849:                REG16(BX) = MAX_EMS_HANDLES;
                   14850:        } else {
1.1.1.22  root     14851:                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     14852:                REG8(AH) = 0x8f;
                   14853:        }
                   14854: }
                   14855: 
                   14856: inline void msdos_int_67h_57h_tmp()
                   14857: {
                   14858:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   14859:        UINT8  src_type    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   14860:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x05);
                   14861:        UINT16 src_ofs     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07);
                   14862:        UINT16 src_seg     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x09);
                   14863:        UINT8  dest_type   = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0b);
                   14864:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   14865:        UINT16 dest_ofs    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0e);
                   14866:        UINT16 dest_seg    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x10);
                   14867:        
1.1.1.32  root     14868:        UINT8 *src_buffer = NULL, *dest_buffer = NULL;
1.1.1.20  root     14869:        UINT32 src_addr, dest_addr;
                   14870:        UINT32 src_addr_max, dest_addr_max;
                   14871:        
                   14872:        if(src_type == 0) {
                   14873:                src_buffer = mem;
                   14874:                src_addr = (src_seg << 4) + src_ofs;
                   14875:                src_addr_max = MAX_MEM;
                   14876:        } else {
1.1.1.31  root     14877:                if(!(src_handle >= 1 && src_handle <= MAX_EMS_HANDLES && ems_handles[src_handle].allocated)) {
1.1.1.20  root     14878:                        REG8(AH) = 0x83;
                   14879:                        return;
                   14880:                } else if(!(src_seg < ems_handles[src_handle].pages)) {
                   14881:                        REG8(AH) = 0x8a;
                   14882:                        return;
                   14883:                }
1.1.1.32  root     14884:                if(ems_handles[src_handle].buffer != NULL) {
                   14885:                        src_buffer = ems_handles[src_handle].buffer + 0x4000 * src_seg;
                   14886:                }
1.1.1.20  root     14887:                src_addr = src_ofs;
1.1.1.32  root     14888:                src_addr_max = 0x4000 * (ems_handles[src_handle].pages - src_seg);
1.1.1.20  root     14889:        }
                   14890:        if(dest_type == 0) {
                   14891:                dest_buffer = mem;
                   14892:                dest_addr = (dest_seg << 4) + dest_ofs;
                   14893:                dest_addr_max = MAX_MEM;
                   14894:        } else {
1.1.1.31  root     14895:                if(!(dest_handle >= 1 && dest_handle <= MAX_EMS_HANDLES && ems_handles[dest_handle].allocated)) {
1.1.1.20  root     14896:                        REG8(AH) = 0x83;
                   14897:                        return;
                   14898:                } else if(!(dest_seg < ems_handles[dest_handle].pages)) {
                   14899:                        REG8(AH) = 0x8a;
                   14900:                        return;
                   14901:                }
1.1.1.32  root     14902:                if(ems_handles[dest_handle].buffer != NULL) {
                   14903:                        dest_buffer = ems_handles[dest_handle].buffer + 0x4000 * dest_seg;
                   14904:                }
1.1.1.20  root     14905:                dest_addr = dest_ofs;
1.1.1.32  root     14906:                dest_addr_max = 0x4000 * (ems_handles[dest_handle].pages - dest_seg);
1.1.1.20  root     14907:        }
1.1.1.32  root     14908:        if(src_buffer != NULL && dest_buffer != NULL) {
                   14909:                for(int i = 0; i < copy_length; i++) {
                   14910:                        if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   14911:                                if(REG8(AL) == 0x00) {
                   14912:                                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   14913:                                } else if(REG8(AL) == 0x01) {
                   14914:                                        UINT8 tmp = dest_buffer[dest_addr];
                   14915:                                        dest_buffer[dest_addr++] = src_buffer[src_addr];
                   14916:                                        src_buffer[src_addr++] = tmp;
                   14917:                                }
                   14918:                        } else {
                   14919:                                REG8(AH) = 0x93;
                   14920:                                return;
1.1.1.20  root     14921:                        }
                   14922:                }
1.1.1.32  root     14923:                REG8(AH) = 0x00;
                   14924:        } else {
                   14925:                REG8(AH) = 0x80;
1.1.1.20  root     14926:        }
                   14927: }
                   14928: 
                   14929: inline void msdos_int_67h_57h()
                   14930: {
                   14931:        if(!support_ems) {
                   14932:                REG8(AH) = 0x84;
                   14933:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   14934:                struct {
                   14935:                        UINT16 handle;
                   14936:                        UINT16 page;
                   14937:                        bool mapped;
                   14938:                } tmp_pages[4];
                   14939:                
                   14940:                // unmap pages to copy memory data to ems buffer
                   14941:                for(int i = 0; i < 4; i++) {
                   14942:                        tmp_pages[i].handle = ems_pages[i].handle;
                   14943:                        tmp_pages[i].page   = ems_pages[i].page;
                   14944:                        tmp_pages[i].mapped = ems_pages[i].mapped;
                   14945:                        ems_unmap_page(i);
                   14946:                }
                   14947:                
                   14948:                // run move/exchange operation
                   14949:                msdos_int_67h_57h_tmp();
                   14950:                
                   14951:                // restore unmapped pages
                   14952:                for(int i = 0; i < 4; i++) {
                   14953:                        if(tmp_pages[i].mapped) {
                   14954:                                ems_map_page(i, tmp_pages[i].handle, tmp_pages[i].page);
                   14955:                        }
                   14956:                }
                   14957:        } else {
1.1.1.22  root     14958:                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     14959:                REG8(AH) = 0x8f;
                   14960:        }
                   14961: }
                   14962: 
                   14963: inline void msdos_int_67h_58h()
                   14964: {
                   14965:        if(!support_ems) {
                   14966:                REG8(AH) = 0x84;
                   14967:        } else if(REG8(AL) == 0x00) {
                   14968:                for(int i = 0; i < 4; i++) {
1.1.1.30  root     14969:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = (EMS_TOP + 0x4000 * i) >> 4;
                   14970:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = i;
1.1.1.20  root     14971:                }
                   14972:                REG8(AH) = 0x00;
                   14973:                REG16(CX) = 4;
                   14974:        } else if(REG8(AL) == 0x01) {
                   14975:                REG8(AH) = 0x00;
                   14976:                REG16(CX) = 4;
                   14977:        } else {
1.1.1.22  root     14978:                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     14979:                REG8(AH) = 0x8f;
                   14980:        }
                   14981: }
                   14982: 
1.1.1.42  root     14983: inline void msdos_int_67h_59h()
                   14984: {
                   14985:        if(!support_ems) {
                   14986:                REG8(AH) = 0x84;
                   14987:        } else if(REG8(AL) == 0x00) {
                   14988:                REG8(AH) = 0xa4; // access denied by operating system
                   14989:        } else if(REG8(AL) == 0x01) {
                   14990:                REG8(AH) = 0x00;
                   14991:                REG16(BX) = free_ems_pages;
                   14992:                REG16(DX) = MAX_EMS_PAGES;
                   14993:        } else {
                   14994:                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));
                   14995:                REG8(AH) = 0x8f;
                   14996:        }
                   14997: }
                   14998: 
1.1.1.20  root     14999: inline void msdos_int_67h_5ah()
                   15000: {
                   15001:        if(!support_ems) {
1.1.1.19  root     15002:                REG8(AH) = 0x84;
1.1.1.20  root     15003:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   15004:                REG8(AH) = 0x87;
                   15005:        } else if(REG16(BX) > free_ems_pages) {
                   15006:                REG8(AH) = 0x88;
                   15007: //     } else if(REG16(BX) == 0) {
                   15008: //             REG8(AH) = 0x89;
                   15009:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
1.1.1.31  root     15010:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.20  root     15011:                        if(!ems_handles[i].allocated) {
                   15012:                                ems_allocate_pages(i, REG16(BX));
                   15013:                                REG8(AH) = 0x00;
                   15014:                                REG16(DX) = i;
                   15015:                                return;
                   15016:                        }
                   15017:                }
                   15018:                REG8(AH) = 0x85;
                   15019:        } else {
1.1.1.22  root     15020:                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     15021:                REG8(AH) = 0x8f;
1.1.1.19  root     15022:        }
                   15023: }
                   15024: 
1.1.1.43  root     15025: inline void msdos_int_67h_5dh()
                   15026: {
                   15027:        if(!support_ems) {
                   15028:                REG8(AH) = 0x84;
                   15029:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   15030:                REG8(AH) = 0xa4; // operating system denied access
                   15031:        } else {
                   15032:                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));
                   15033:                REG8(AH) = 0x8f;
                   15034:        }
                   15035: }
                   15036: 
1.1.1.30  root     15037: inline void msdos_int_67h_deh()
                   15038: {
                   15039:        REG8(AH) = 0x84;
                   15040: }
                   15041: 
1.1.1.19  root     15042: #ifdef SUPPORT_XMS
                   15043: 
1.1.1.32  root     15044: void msdos_xms_init()
1.1.1.26  root     15045: {
1.1.1.30  root     15046:        emb_handle_top = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   15047:        emb_handle_top->address = EMB_TOP;
                   15048:        emb_handle_top->size_kb = (EMB_END - EMB_TOP) >> 10;
1.1.1.26  root     15049:        xms_a20_local_enb_count = 0;
                   15050: }
                   15051: 
1.1.1.32  root     15052: void msdos_xms_finish()
                   15053: {
                   15054:        msdos_xms_release();
                   15055: }
                   15056: 
                   15057: void msdos_xms_release()
1.1.1.30  root     15058: {
                   15059:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL;) {
                   15060:                emb_handle_t *next_handle = emb_handle->next;
                   15061:                free(emb_handle);
                   15062:                emb_handle = next_handle;
                   15063:        }
                   15064: }
                   15065: 
                   15066: emb_handle_t *msdos_xms_get_emb_handle(int handle)
                   15067: {
                   15068:        if(handle != 0) {
                   15069:                for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   15070:                        if(emb_handle->handle == handle) {
                   15071:                                return(emb_handle);
                   15072:                        }
                   15073:                }
                   15074:        }
                   15075:        return(NULL);
                   15076: }
                   15077: 
                   15078: int msdos_xms_get_unused_emb_handle_id()
                   15079: {
                   15080:        for(int handle = 1;; handle++) {
                   15081:                if(msdos_xms_get_emb_handle(handle) == NULL) {
                   15082:                        return(handle);
                   15083:                }
                   15084:        }
                   15085:        return(0);
                   15086: }
                   15087: 
                   15088: int msdos_xms_get_unused_emb_handle_count()
                   15089: {
                   15090:        int count = 64; //255;
                   15091:        
                   15092:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   15093:                if(emb_handle->handle != 0) {
                   15094:                        if(--count == 1) {
                   15095:                                break;
                   15096:                        }
                   15097:                }
                   15098:        }
                   15099:        return(count);
                   15100: }
                   15101: 
                   15102: void msdos_xms_split_emb_handle(emb_handle_t *emb_handle, int size_kb)
                   15103: {
                   15104:        if(emb_handle->size_kb > size_kb) {
                   15105:                emb_handle_t *new_handle = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   15106:                
                   15107:                new_handle->address = emb_handle->address + size_kb * 1024;
                   15108:                new_handle->size_kb = emb_handle->size_kb - size_kb;
                   15109:                emb_handle->size_kb = size_kb;
                   15110:                
                   15111:                new_handle->prev = emb_handle;
                   15112:                new_handle->next = emb_handle->next;
                   15113:                if(emb_handle->next != NULL) {
                   15114:                        emb_handle->next->prev = new_handle;
                   15115:                }
                   15116:                emb_handle->next = new_handle;
                   15117:        }
                   15118: }
                   15119: 
                   15120: void msdos_xms_combine_emb_handles(emb_handle_t *emb_handle)
                   15121: {
                   15122:        emb_handle_t *next_handle = emb_handle->next;
                   15123:        
                   15124:        if(next_handle != NULL) {
                   15125:                emb_handle->size_kb += next_handle->size_kb;
                   15126:                
                   15127:                if(next_handle->next != NULL) {
                   15128:                        next_handle->next->prev = emb_handle;
                   15129:                }
                   15130:                emb_handle->next = next_handle->next;
                   15131:                free(next_handle);
                   15132:        }
                   15133: }
                   15134: 
                   15135: emb_handle_t *msdos_xms_alloc_emb_handle(int size_kb)
                   15136: {
                   15137:        emb_handle_t *target_handle = NULL;
                   15138:        
                   15139:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   15140:                if(emb_handle->handle == 0 && emb_handle->size_kb >= size_kb) {
                   15141:                        if(target_handle == NULL || target_handle->size_kb > emb_handle->size_kb) {
                   15142:                                target_handle = emb_handle;
                   15143:                        }
                   15144:                }
                   15145:        }
                   15146:        if(target_handle != NULL) {
                   15147:                if(target_handle->size_kb > size_kb) {
                   15148:                        msdos_xms_split_emb_handle(target_handle, size_kb);
                   15149:                }
                   15150: //             target_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   15151:                return(target_handle);
                   15152:        }
                   15153:        return(NULL);
                   15154: }
                   15155: 
                   15156: void msdos_xms_free_emb_handle(emb_handle_t *emb_handle)
                   15157: {
                   15158:        emb_handle_t *prev_handle = emb_handle->prev;
                   15159:        emb_handle_t *next_handle = emb_handle->next;
                   15160:        
                   15161:        if(prev_handle != NULL && prev_handle->handle == 0) {
                   15162:                msdos_xms_combine_emb_handles(prev_handle);
                   15163:                emb_handle = prev_handle;
                   15164:        }
                   15165:        if(next_handle != NULL && next_handle->handle == 0) {
                   15166:                msdos_xms_combine_emb_handles(emb_handle);
                   15167:        }
                   15168:        emb_handle->handle = 0;
                   15169: }
                   15170: 
1.1.1.19  root     15171: inline void msdos_call_xms_00h()
                   15172: {
1.1.1.29  root     15173: #if defined(HAS_I386)
                   15174:        REG16(AX) = 0x0300; // V3.00 (XMS Version)
1.1.1.45  root     15175: //     REG16(BX) = 0x0395; // V3.95 (Driver Revision in BCD)
1.1.1.29  root     15176:        REG16(BX) = 0x035f; // V3.95 (Driver Revision)
                   15177: #else
                   15178:        REG16(AX) = 0x0200; // V2.00 (XMS Version)
                   15179:        REG16(BX) = 0x0270; // V2.70 (Driver Revision)
                   15180: #endif
                   15181: //     REG16(DX) = 0x0000; // HMA does not exist
                   15182:        REG16(DX) = 0x0001; // HMA does exist
1.1.1.19  root     15183: }
                   15184: 
                   15185: inline void msdos_call_xms_01h()
                   15186: {
1.1.1.29  root     15187:        if(REG8(AL) == 0x40) {
                   15188:                // HIMEM.SYS will fail function 01h with error code 91h if AL=40h and
                   15189:                // DX=KB free extended memory returned by last call of function 08h
                   15190:                REG16(AX) = 0x0000;
                   15191:                REG8(BL) = 0x91;
                   15192:                REG16(DX) = xms_dx_after_call_08h;
                   15193:        } else if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   15194:                REG16(AX) = 0x0000;
                   15195:                REG8(BL) = 0x81; // Vdisk was detected
                   15196: #ifdef SUPPORT_HMA
                   15197:        } else if(is_hma_used_by_int_2fh) {
                   15198:                REG16(AX) = 0x0000;
                   15199:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   15200:        } else if(is_hma_used_by_xms) {
                   15201:                REG16(AX) = 0x0000;
                   15202:                REG8(BL) = 0x91; // HMA is already in use
                   15203:        } else {
                   15204:                REG16(AX) = 0x0001;
                   15205:                is_hma_used_by_xms = true;
                   15206: #else
                   15207:        } else {
                   15208:                REG16(AX) = 0x0000;
                   15209:                REG8(BL) = 0x91; // HMA is already in use
                   15210: #endif
                   15211:        }
1.1.1.19  root     15212: }
                   15213: 
                   15214: inline void msdos_call_xms_02h()
                   15215: {
1.1.1.29  root     15216:        if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   15217:                REG16(AX) = 0x0000;
                   15218:                REG8(BL) = 0x81; // Vdisk was detected
                   15219: #ifdef SUPPORT_HMA
                   15220:        } else if(is_hma_used_by_int_2fh) {
                   15221:                REG16(AX) = 0x0000;
                   15222:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   15223:        } else if(!is_hma_used_by_xms) {
                   15224:                REG16(AX) = 0x0000;
                   15225:                REG8(BL) = 0x93; // HMA is not allocated
                   15226:        } else {
                   15227:                REG16(AX) = 0x0001;
                   15228:                is_hma_used_by_xms = false;
                   15229:                // restore first free mcb in high memory area
                   15230:                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   15231: #else
                   15232:        } else {
                   15233:                REG16(AX) = 0x0000;
                   15234:                REG8(BL) = 0x91; // HMA is already in use
                   15235: #endif
                   15236:        }
1.1.1.19  root     15237: }
                   15238: 
                   15239: inline void msdos_call_xms_03h()
                   15240: {
                   15241:        i386_set_a20_line(1);
                   15242:        REG16(AX) = 0x0001;
                   15243:        REG8(BL) = 0x00;
                   15244: }
                   15245: 
                   15246: inline void msdos_call_xms_04h()
                   15247: {
1.1.1.21  root     15248:        i386_set_a20_line(0);
                   15249:        REG16(AX) = 0x0001;
                   15250:        REG8(BL) = 0x00;
1.1.1.19  root     15251: }
                   15252: 
                   15253: inline void msdos_call_xms_05h()
                   15254: {
                   15255:        i386_set_a20_line(1);
                   15256:        REG16(AX) = 0x0001;
                   15257:        REG8(BL) = 0x00;
1.1.1.21  root     15258:        xms_a20_local_enb_count++;
1.1.1.19  root     15259: }
                   15260: 
                   15261: void msdos_call_xms_06h()
                   15262: {
1.1.1.21  root     15263:        if(xms_a20_local_enb_count > 0) {
1.1.1.45  root     15264:                if(--xms_a20_local_enb_count == 0) {
                   15265:                        i386_set_a20_line(0);
                   15266:                        REG16(AX) = 0x0001;
                   15267:                        REG8(BL) = 0x00;
                   15268:                } else {
                   15269:                        REG16(AX) = 0x0000;
                   15270:                        REG8(BL) = 0x94;
                   15271:                }
1.1.1.21  root     15272:        } else {
1.1.1.45  root     15273:                i386_set_a20_line(0);
1.1.1.21  root     15274:                REG16(AX) = 0x0001;
                   15275:                REG8(BL) = 0x00;
1.1.1.19  root     15276:        }
                   15277: }
                   15278: 
                   15279: inline void msdos_call_xms_07h()
                   15280: {
                   15281:        REG16(AX) = (m_a20_mask >> 20) & 1;
                   15282:        REG8(BL) = 0x00;
                   15283: }
                   15284: 
                   15285: inline void msdos_call_xms_08h()
                   15286: {
1.1.1.45  root     15287:        UINT32 eax = 0, edx = 0;
1.1.1.19  root     15288:        
1.1.1.30  root     15289:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   15290:                if(emb_handle->handle == 0) {
1.1.1.45  root     15291:                        if(eax < emb_handle->size_kb) {
                   15292:                                eax = emb_handle->size_kb;
1.1.1.19  root     15293:                        }
1.1.1.45  root     15294:                        edx += emb_handle->size_kb;
1.1.1.19  root     15295:                }
                   15296:        }
1.1.1.45  root     15297:        if(eax > 65535) {
                   15298:                eax = 65535;
                   15299:        }
                   15300:        if(edx > 65535) {
                   15301:                edx = 65535;
                   15302:        }
                   15303:        if(eax == 0 && edx == 0) {
1.1.1.19  root     15304:                REG8(BL) = 0xa0;
                   15305:        } else {
                   15306:                REG8(BL) = 0x00;
                   15307:        }
1.1.1.45  root     15308: #if defined(HAS_I386)
                   15309:        REG32(EAX) = eax;
                   15310:        REG32(EDX) = edx;
                   15311: #else
                   15312:        REG16(AX) = (UINT16)eax;
                   15313:        REG16(DX) = (UINT16)edx;
                   15314: #endif
1.1.1.29  root     15315:        xms_dx_after_call_08h = REG16(DX);
1.1.1.19  root     15316: }
                   15317: 
1.1.1.30  root     15318: void msdos_call_xms_09h(int size_kb)
1.1.1.19  root     15319: {
1.1.1.30  root     15320:        emb_handle_t *emb_handle = msdos_xms_alloc_emb_handle(size_kb);
                   15321:        
                   15322:        if(emb_handle != NULL) {
                   15323:                emb_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   15324:                
                   15325:                REG16(AX) = 0x0001;
                   15326:                REG16(DX) = emb_handle->handle;
                   15327:                REG8(BL) = 0x00;
                   15328:        } else {
                   15329:                REG16(AX) = REG16(DX) = 0x0000;
                   15330:                REG8(BL) = 0xa0;
1.1.1.19  root     15331:        }
1.1.1.30  root     15332: }
                   15333: 
                   15334: inline void msdos_call_xms_09h()
                   15335: {
                   15336:        msdos_call_xms_09h(REG16(DX));
1.1.1.19  root     15337: }
                   15338: 
                   15339: inline void msdos_call_xms_0ah()
                   15340: {
1.1.1.30  root     15341:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   15342:        
                   15343:        if(emb_handle == NULL) {
1.1.1.19  root     15344:                REG16(AX) = 0x0000;
                   15345:                REG8(BL) = 0xa2;
1.1.1.45  root     15346: //     } else if(emb_handle->lock > 0) {
                   15347: //             REG16(AX) = 0x0000;
                   15348: //             REG8(BL) = 0xab;
1.1.1.19  root     15349:        } else {
1.1.1.30  root     15350:                msdos_xms_free_emb_handle(emb_handle);
1.1.1.19  root     15351:                
                   15352:                REG16(AX) = 0x0001;
                   15353:                REG8(BL) = 0x00;
                   15354:        }
                   15355: }
                   15356: 
                   15357: inline void msdos_call_xms_0bh()
                   15358: {
                   15359:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   15360:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   15361:        UINT32 src_addr    = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06);
                   15362:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0a);
                   15363:        UINT32 dest_addr   = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   15364:        
                   15365:        UINT8 *src_buffer, *dest_buffer;
                   15366:        UINT32 src_addr_max, dest_addr_max;
1.1.1.30  root     15367:        emb_handle_t *emb_handle;
1.1.1.19  root     15368:        
                   15369:        if(src_handle == 0) {
                   15370:                src_buffer = mem;
                   15371:                src_addr = (((src_addr >> 16) & 0xffff) << 4) + (src_addr & 0xffff);
                   15372:                src_addr_max = MAX_MEM;
                   15373:        } else {
1.1.1.30  root     15374:                if((emb_handle = msdos_xms_get_emb_handle(src_handle)) == NULL) {
1.1.1.19  root     15375:                        REG16(AX) = 0x0000;
                   15376:                        REG8(BL) = 0xa3;
                   15377:                        return;
                   15378:                }
1.1.1.30  root     15379:                src_buffer = mem + emb_handle->address;
                   15380:                src_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     15381:        }
                   15382:        if(dest_handle == 0) {
                   15383:                dest_buffer = mem;
                   15384:                dest_addr = (((dest_addr >> 16) & 0xffff) << 4) + (dest_addr & 0xffff);
                   15385:                dest_addr_max = MAX_MEM;
                   15386:        } else {
1.1.1.30  root     15387:                if((emb_handle = msdos_xms_get_emb_handle(dest_handle)) == NULL) {
1.1.1.19  root     15388:                        REG16(AX) = 0x0000;
                   15389:                        REG8(BL) = 0xa5;
                   15390:                        return;
                   15391:                }
1.1.1.30  root     15392:                dest_buffer = mem + emb_handle->address;
                   15393:                dest_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     15394:        }
                   15395:        for(int i = 0; i < copy_length; i++) {
                   15396:                if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   15397:                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   15398:                } else {
                   15399:                        break;
                   15400:                }
                   15401:        }
                   15402:        REG16(AX) = 0x0001;
                   15403:        REG8(BL) = 0x00;
                   15404: }
                   15405: 
                   15406: inline void msdos_call_xms_0ch()
                   15407: {
1.1.1.30  root     15408:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   15409:        
                   15410:        if(emb_handle == NULL) {
1.1.1.19  root     15411:                REG16(AX) = 0x0000;
                   15412:                REG8(BL) = 0xa2;
                   15413:        } else {
1.1.1.45  root     15414:                if(emb_handle->lock < 255) {
                   15415:                        emb_handle->lock++;
                   15416:                }
1.1.1.19  root     15417:                REG16(AX) = 0x0001;
1.1.1.30  root     15418:                REG16(DX) = (emb_handle->address >> 16) & 0xffff;
                   15419:                REG16(BX) = (emb_handle->address      ) & 0xffff;
1.1.1.19  root     15420:        }
                   15421: }
                   15422: 
                   15423: inline void msdos_call_xms_0dh()
                   15424: {
1.1.1.30  root     15425:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   15426:        
                   15427:        if(emb_handle == NULL) {
1.1.1.19  root     15428:                REG16(AX) = 0x0000;
                   15429:                REG8(BL) = 0xa2;
1.1.1.30  root     15430:        } else if(!(emb_handle->lock > 0)) {
1.1.1.19  root     15431:                REG16(AX) = 0x0000;
                   15432:                REG8(BL) = 0xaa;
                   15433:        } else {
1.1.1.30  root     15434:                emb_handle->lock--;
1.1.1.19  root     15435:                REG16(AX) = 0x0001;
                   15436:                REG8(BL) = 0x00;
                   15437:        }
                   15438: }
                   15439: 
                   15440: inline void msdos_call_xms_0eh()
                   15441: {
1.1.1.30  root     15442:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   15443:        
                   15444:        if(emb_handle == NULL) {
1.1.1.19  root     15445:                REG16(AX) = 0x0000;
                   15446:                REG8(BL) = 0xa2;
                   15447:        } else {
                   15448:                REG16(AX) = 0x0001;
1.1.1.30  root     15449:                REG8(BH) = emb_handle->lock;
                   15450:                REG8(BL) = msdos_xms_get_unused_emb_handle_count();
                   15451:                REG16(DX) = emb_handle->size_kb;
1.1.1.19  root     15452:        }
                   15453: }
                   15454: 
1.1.1.30  root     15455: void msdos_call_xms_0fh(int size_kb)
1.1.1.19  root     15456: {
1.1.1.30  root     15457:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   15458:        
                   15459:        if(emb_handle == NULL) {
1.1.1.19  root     15460:                REG16(AX) = 0x0000;
                   15461:                REG8(BL) = 0xa2;
1.1.1.30  root     15462:        } else if(emb_handle->lock > 0) {
1.1.1.19  root     15463:                REG16(AX) = 0x0000;
                   15464:                REG8(BL) = 0xab;
                   15465:        } else {
1.1.1.30  root     15466:                if(emb_handle->size_kb < size_kb) {
                   15467:                        if(emb_handle->next != NULL && emb_handle->next->handle == 0 && (emb_handle->size_kb + emb_handle->next->size_kb) >= size_kb) {
                   15468:                                msdos_xms_combine_emb_handles(emb_handle);
                   15469:                                if(emb_handle->size_kb > size_kb) {
                   15470:                                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   15471:                                }
                   15472:                        } else {
                   15473:                                int old_handle = emb_handle->handle;
                   15474:                                int old_size_kb = emb_handle->size_kb;
                   15475:                                UINT8 *buffer = (UINT8 *)malloc(old_size_kb * 1024);
                   15476:                                
                   15477:                                memcpy(buffer, mem + emb_handle->address, old_size_kb * 1024);
                   15478:                                msdos_xms_free_emb_handle(emb_handle);
                   15479:                                
                   15480:                                if((emb_handle = msdos_xms_alloc_emb_handle(size_kb)) == NULL) {
                   15481:                                        emb_handle = msdos_xms_alloc_emb_handle(old_size_kb); // should be always successed
                   15482:                                }
                   15483:                                emb_handle->handle = old_handle;
                   15484:                                memcpy(mem + emb_handle->address, buffer, old_size_kb * 1024);
                   15485:                                free(buffer);
                   15486:                        }
                   15487:                } else if(emb_handle->size_kb > size_kb) {
                   15488:                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   15489:                }
                   15490:                if(emb_handle->size_kb != size_kb) {
                   15491:                        REG16(AX) = 0x0000;
                   15492:                        REG8(BL) = 0xa0;
                   15493:                } else {
                   15494:                        REG16(AX) = 0x0001;
                   15495:                        REG8(BL) = 0x00;
                   15496:                }
1.1.1.19  root     15497:        }
                   15498: }
                   15499: 
1.1.1.30  root     15500: inline void msdos_call_xms_0fh()
                   15501: {
                   15502:        msdos_call_xms_0fh(REG16(BX));
                   15503: }
                   15504: 
1.1.1.19  root     15505: inline void msdos_call_xms_10h()
                   15506: {
                   15507:        int seg;
                   15508:        
                   15509:        if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(DX), 0)) != -1) {
                   15510:                REG16(AX) = 0x0001;
                   15511:                REG16(BX) = seg;
                   15512:        } else {
                   15513:                REG16(AX) = 0x0000;
                   15514:                REG8(BL) = 0xb0;
                   15515:                REG16(DX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   15516:        }
                   15517: }
                   15518: 
                   15519: inline void msdos_call_xms_11h()
                   15520: {
                   15521:        int mcb_seg = REG16(DX) - 1;
                   15522:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   15523:        
                   15524:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   15525:                msdos_mem_free(REG16(DX));
                   15526:                REG16(AX) = 0x0001;
                   15527:                REG8(BL) = 0x00;
                   15528:        } else {
                   15529:                REG16(AX) = 0x0000;
                   15530:                REG8(BL) = 0xb2;
                   15531:        }
                   15532: }
                   15533: 
                   15534: inline void msdos_call_xms_12h()
                   15535: {
                   15536:        int mcb_seg = REG16(DX) - 1;
                   15537:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   15538:        int max_paragraphs;
                   15539:        
                   15540:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   15541:                if(!msdos_mem_realloc(REG16(DX), REG16(BX), &max_paragraphs)) {
                   15542:                        REG16(AX) = 0x0001;
                   15543:                        REG8(BL) = 0x00;
                   15544:                } else {
                   15545:                        REG16(AX) = 0x0000;
                   15546:                        REG8(BL) = 0xb0;
                   15547:                        REG16(DX) = max_paragraphs;
                   15548:                }
                   15549:        } else {
                   15550:                REG16(AX) = 0x0000;
                   15551:                REG8(BL) = 0xb2;
                   15552:        }
                   15553: }
                   15554: 
1.1.1.29  root     15555: #if defined(HAS_I386)
                   15556: 
                   15557: inline void msdos_call_xms_88h()
                   15558: {
                   15559:        REG32(EAX) = REG32(EDX) = 0x0000;
                   15560:        
1.1.1.30  root     15561:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   15562:                if(emb_handle->handle == 0) {
                   15563:                        if(REG32(EAX) < emb_handle->size_kb) {
                   15564:                                REG32(EAX) = emb_handle->size_kb;
1.1.1.29  root     15565:                        }
1.1.1.30  root     15566:                        REG32(EDX) += emb_handle->size_kb;
1.1.1.29  root     15567:                }
                   15568:        }
                   15569:        if(REG32(EAX) == 0 && REG32(EDX) == 0) {
                   15570:                REG8(BL) = 0xa0;
                   15571:        } else {
                   15572:                REG8(BL) = 0x00;
                   15573:        }
                   15574:        REG32(ECX) = EMB_END - 1;
                   15575: }
                   15576: 
                   15577: inline void msdos_call_xms_89h()
                   15578: {
1.1.1.30  root     15579:        msdos_call_xms_09h(REG32(EDX));
1.1.1.29  root     15580: }
                   15581: 
                   15582: inline void msdos_call_xms_8eh()
                   15583: {
1.1.1.30  root     15584:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   15585:        
                   15586:        if(emb_handle == NULL) {
1.1.1.29  root     15587:                REG16(AX) = 0x0000;
                   15588:                REG8(BL) = 0xa2;
                   15589:        } else {
                   15590:                REG16(AX) = 0x0001;
1.1.1.30  root     15591:                REG8(BH) = emb_handle->lock;
                   15592:                REG16(CX) = msdos_xms_get_unused_emb_handle_count();
                   15593:                REG32(EDX) = emb_handle->size_kb;
1.1.1.29  root     15594:        }
                   15595: }
                   15596: 
                   15597: inline void msdos_call_xms_8fh()
                   15598: {
1.1.1.30  root     15599:        msdos_call_xms_0fh(REG32(EBX));
1.1.1.29  root     15600: }
                   15601: 
                   15602: #endif
1.1.1.19  root     15603: #endif
                   15604: 
1.1.1.26  root     15605: UINT16 msdos_get_equipment()
                   15606: {
                   15607:        static UINT16 equip = 0;
                   15608:        
                   15609:        if(equip == 0) {
                   15610: #ifdef SUPPORT_FPU
                   15611:                equip |= (1 << 1);      // 80x87 coprocessor installed
                   15612: #endif
                   15613:                equip |= (1 << 2);      // pointing device installed (PS/2)
                   15614:                equip |= (2 << 4);      // initial video mode (80x25 color)
                   15615: //             equip |= (1 << 8);      // 0 if DMA installed
                   15616:                equip |= (2 << 9);      // number of serial ports
                   15617:                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     15618:                
                   15619:                // check only A: and B: if it is floppy drive
                   15620:                int n = 0;
                   15621:                for(int i = 0; i < 2; i++) {
1.1.1.44  root     15622:                        if(msdos_is_valid_drive(i) && msdos_is_removable_drive(i)) {
                   15623:                                n++;
1.1.1.28  root     15624:                        }
                   15625:                }
                   15626:                if(n != 0) {
                   15627:                        equip |= (1 << 0);      // floppy disk(s) installed
                   15628:                        n--;
                   15629:                        equip |= (n << 6);      // number of floppies installed less 1
                   15630:                }
                   15631: //             if(joyGetNumDevs() != 0) {
                   15632: //                     equip |= (1 << 12);     // game port installed
                   15633: //             }
1.1.1.26  root     15634:        }
                   15635:        return(equip);
                   15636: }
                   15637: 
1.1       root     15638: void msdos_syscall(unsigned num)
                   15639: {
1.1.1.22  root     15640: #ifdef ENABLE_DEBUG_SYSCALL
1.1.1.43  root     15641:        if(num == 0x08 || num == 0x1c) {
                   15642:                // don't log the timer interrupts
1.1.1.45  root     15643: //             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     15644:        } else if(num == 0x68) {
1.1.1.22  root     15645:                // dummy interrupt for EMS (int 67h)
1.1.1.33  root     15646:                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     15647:        } else if(num == 0x69) {
                   15648:                // dummy interrupt for XMS (call far)
1.1.1.33  root     15649:                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     15650:        } else if(num >= 0x6a && num < 0x6f) {
1.1.1.45  root     15651:                // dummy interrupt
1.1.1.46  root     15652:        } else if(num == 0x6f) {
                   15653:                // dummy interrupt for call 0005h (call near)
                   15654:                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     15655:        } else {
1.1.1.33  root     15656:                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     15657:        }
                   15658: #endif
1.1.1.36  root     15659:        // update cursor position
                   15660:        if(cursor_moved) {
                   15661:                pcbios_update_cursor_position();
                   15662:                cursor_moved = false;
                   15663:        }
1.1.1.33  root     15664:        ctrl_break_detected = ctrl_break_pressed = ctrl_c_pressed = false;
1.1.1.22  root     15665:        
1.1       root     15666:        switch(num) {
                   15667:        case 0x00:
1.1.1.28  root     15668:                try {
                   15669:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   15670:                        error("division by zero\n");
                   15671:                } catch(...) {
                   15672:                        fatalerror("division by zero detected, and failed to terminate current process\n");
                   15673:                }
1.1       root     15674:                break;
                   15675:        case 0x04:
1.1.1.28  root     15676:                try {
                   15677:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   15678:                        error("overflow\n");
                   15679:                } catch(...) {
                   15680:                        fatalerror("overflow detected, and failed to terminate current process\n");
                   15681:                }
1.1       root     15682:                break;
                   15683:        case 0x06:
                   15684:                // NOTE: ish.com has illegal instruction...
1.1.1.14  root     15685:                if(!ignore_illegal_insn) {
1.1.1.28  root     15686:                        try {
                   15687:                                msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   15688:                                error("illegal instruction\n");
                   15689:                        } catch(...) {
                   15690:                                fatalerror("illegal instruction detected, and failed to terminate current process\n");
                   15691:                        }
1.1.1.14  root     15692:                } else {
                   15693: #if defined(HAS_I386)
1.1.1.39  root     15694:                        m_eip = m_int6h_skip_eip;
                   15695: #elif defined(HAS_I286)
                   15696:                        m_pc = m_int6h_skip_pc;
1.1.1.14  root     15697: #else
1.1.1.39  root     15698:                        // 8086/80186 ignore an invalid opcode
1.1.1.14  root     15699: #endif
                   15700:                }
1.1       root     15701:                break;
1.1.1.33  root     15702:        case 0x09:
                   15703:                // ctrl-break is pressed
                   15704:                if(raise_int_1bh) {
                   15705: #if defined(HAS_I386)
                   15706:                        m_ext = 0; // not an external interrupt
                   15707:                        i386_trap(0x1b, 1, 0);
                   15708:                        m_ext = 1;
                   15709: #else
                   15710:                        PREFIX86(_interrupt)(0x1b);
                   15711: #endif
                   15712:                        raise_int_1bh = false;
                   15713:                }
1.1.1.8   root     15714:        case 0x08:
1.1.1.14  root     15715: //             pcbios_irq0(); // this causes too slow emulation...
1.1.1.8   root     15716:        case 0x0b:
                   15717:        case 0x0c:
                   15718:        case 0x0d:
                   15719:        case 0x0e:
                   15720:        case 0x0f:
                   15721:                // EOI
                   15722:                pic[0].isr &= ~(1 << (num - 0x08));
                   15723:                pic_update();
                   15724:                break;
1.1       root     15725:        case 0x10:
                   15726:                // PC BIOS - Video
1.1.1.14  root     15727:                if(!restore_console_on_exit) {
1.1.1.15  root     15728:                        change_console_size(scr_width, scr_height);
1.1       root     15729:                }
1.1.1.3   root     15730:                m_CF = 0;
1.1       root     15731:                switch(REG8(AH)) {
1.1.1.16  root     15732:                case 0x00: pcbios_int_10h_00h(); break;
1.1       root     15733:                case 0x01: pcbios_int_10h_01h(); break;
                   15734:                case 0x02: pcbios_int_10h_02h(); break;
                   15735:                case 0x03: pcbios_int_10h_03h(); break;
                   15736:                case 0x05: pcbios_int_10h_05h(); break;
                   15737:                case 0x06: pcbios_int_10h_06h(); break;
                   15738:                case 0x07: pcbios_int_10h_07h(); break;
                   15739:                case 0x08: pcbios_int_10h_08h(); break;
                   15740:                case 0x09: pcbios_int_10h_09h(); break;
                   15741:                case 0x0a: pcbios_int_10h_0ah(); break;
                   15742:                case 0x0b: break;
1.1.1.40  root     15743:                case 0x0c: pcbios_int_10h_0ch(); break;
                   15744:                case 0x0d: pcbios_int_10h_0dh(); break;
1.1       root     15745:                case 0x0e: pcbios_int_10h_0eh(); break;
                   15746:                case 0x0f: pcbios_int_10h_0fh(); break;
                   15747:                case 0x10: break;
1.1.1.14  root     15748:                case 0x11: pcbios_int_10h_11h(); break;
                   15749:                case 0x12: pcbios_int_10h_12h(); break;
1.1       root     15750:                case 0x13: pcbios_int_10h_13h(); break;
1.1.1.30  root     15751:                case 0x18: pcbios_int_10h_18h(); break;
1.1.1.14  root     15752:                case 0x1a: pcbios_int_10h_1ah(); break;
1.1.1.24  root     15753:                case 0x1b: REG8(AL) = 0x00; break; // functionality/state information is not supported
                   15754:                case 0x1c: REG8(AL) = 0x00; break; // save/restore video state is not supported
1.1       root     15755:                case 0x1d: pcbios_int_10h_1dh(); break;
1.1.1.24  root     15756:                case 0x1e: REG8(AL) = 0x00; break; // flat-panel functions are not supported
                   15757:                case 0x1f: REG8(AL) = 0x00; break; // xga functions are not supported
1.1.1.22  root     15758:                case 0x4f: pcbios_int_10h_4fh(); break;
1.1.1.30  root     15759:                case 0x6f: break;
1.1.1.22  root     15760:                case 0x80: m_CF = 1; break; // unknown
                   15761:                case 0x81: m_CF = 1; break; // unknown
1.1       root     15762:                case 0x82: pcbios_int_10h_82h(); break;
1.1.1.22  root     15763:                case 0x83: pcbios_int_10h_83h(); break;
                   15764:                case 0x8b: break;
                   15765:                case 0x8c: m_CF = 1; break; // unknown
                   15766:                case 0x8d: m_CF = 1; break; // unknown
                   15767:                case 0x8e: m_CF = 1; break; // unknown
                   15768:                case 0x90: pcbios_int_10h_90h(); break;
                   15769:                case 0x91: pcbios_int_10h_91h(); break;
                   15770:                case 0x92: break;
                   15771:                case 0x93: break;
                   15772:                case 0xef: pcbios_int_10h_efh(); break;
1.1.1.24  root     15773:                case 0xfa: break; // ega register interface library is not installed
1.1       root     15774:                case 0xfe: pcbios_int_10h_feh(); break;
                   15775:                case 0xff: pcbios_int_10h_ffh(); break;
                   15776:                default:
1.1.1.22  root     15777:                        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));
                   15778:                        m_CF = 1;
1.1       root     15779:                        break;
                   15780:                }
                   15781:                break;
                   15782:        case 0x11:
                   15783:                // PC BIOS - Get Equipment List
1.1.1.26  root     15784:                REG16(AX) = msdos_get_equipment();
1.1       root     15785:                break;
                   15786:        case 0x12:
                   15787:                // PC BIOS - Get Memory Size
1.1.1.33  root     15788:                REG16(AX) = *(UINT16 *)(mem + 0x413);
1.1       root     15789:                break;
                   15790:        case 0x13:
1.1.1.42  root     15791:                // PC BIOS - Disk I/O
                   15792:                {
                   15793:                        static UINT8 last = 0x00;
                   15794:                        switch(REG8(AH)) {
                   15795:                        case 0x00: pcbios_int_13h_00h(); break;
                   15796:                        case 0x01: // get last status
                   15797:                                REG8(AH) = last;
                   15798:                                break;
                   15799:                        case 0x02: pcbios_int_13h_02h(); break;
                   15800:                        case 0x03: pcbios_int_13h_03h(); break;
                   15801:                        case 0x04: pcbios_int_13h_04h(); break;
                   15802:                        case 0x08: pcbios_int_13h_08h(); break;
                   15803:                        case 0x0a: pcbios_int_13h_02h(); break;
                   15804:                        case 0x0b: pcbios_int_13h_03h(); break;
                   15805:                        case 0x0d: pcbios_int_13h_00h(); break;
                   15806:                        case 0x10: pcbios_int_13h_10h(); break;
                   15807:                        case 0x15: pcbios_int_13h_15h(); break;
1.1.1.43  root     15808:                        case 0x41: pcbios_int_13h_41h(); break;
1.1.1.42  root     15809:                        case 0x05: // format
                   15810:                        case 0x06:
                   15811:                        case 0x07:
                   15812:                                REG8(AH) = 0x0c; // unsupported track or invalid media
                   15813:                                m_CF = 1;
                   15814:                                break;
                   15815:                        case 0x09:
                   15816:                        case 0x0c: // seek
                   15817:                        case 0x11: // recalib
                   15818:                        case 0x14:
                   15819:                        case 0x17:
                   15820:                                REG8(AH) = 0x00; // successful completion
                   15821:                                break;
1.1.1.43  root     15822:                        case 0x21: // QUICKCACHE II v4.20 - Flush Cache
                   15823:                        case 0xa1: // Super PC-Kwik v3.20+ - Flush Cache
                   15824:                                REG8(AH) = 0x01; // invalid function
                   15825:                                m_CF = 1;
                   15826:                                break;
1.1.1.42  root     15827:                        default:
                   15828:                                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));
                   15829:                                REG8(AH) = 0x01; // invalid function
                   15830:                                m_CF = 1;
                   15831:                                break;
                   15832:                        }
                   15833:                        last = REG8(AH);
                   15834:                }
1.1       root     15835:                break;
                   15836:        case 0x14:
                   15837:                // PC BIOS - Serial I/O
1.1.1.25  root     15838:                switch(REG8(AH)) {
                   15839:                case 0x00: pcbios_int_14h_00h(); break;
                   15840:                case 0x01: pcbios_int_14h_01h(); break;
                   15841:                case 0x02: pcbios_int_14h_02h(); break;
                   15842:                case 0x03: pcbios_int_14h_03h(); break;
                   15843:                case 0x04: pcbios_int_14h_04h(); break;
                   15844:                case 0x05: pcbios_int_14h_05h(); break;
                   15845:                default:
                   15846:                        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));
                   15847:                        break;
                   15848:                }
1.1       root     15849:                break;
                   15850:        case 0x15:
                   15851:                // PC BIOS
1.1.1.3   root     15852:                m_CF = 0;
1.1       root     15853:                switch(REG8(AH)) {
1.1.1.14  root     15854:                case 0x10: pcbios_int_15h_10h(); break;
1.1       root     15855:                case 0x23: pcbios_int_15h_23h(); break;
                   15856:                case 0x24: pcbios_int_15h_24h(); break;
1.1.1.24  root     15857:                case 0x41: break;
1.1       root     15858:                case 0x49: pcbios_int_15h_49h(); break;
1.1.1.22  root     15859:                case 0x50: pcbios_int_15h_50h(); break;
1.1.1.30  root     15860:                case 0x53: pcbios_int_15h_53h(); break;
1.1.1.43  root     15861:                case 0x84: pcbios_int_15h_84h(); break;
1.1       root     15862:                case 0x86: pcbios_int_15h_86h(); break;
                   15863:                case 0x87: pcbios_int_15h_87h(); break;
                   15864:                case 0x88: pcbios_int_15h_88h(); break;
                   15865:                case 0x89: pcbios_int_15h_89h(); break;
1.1.1.21  root     15866:                case 0x8a: pcbios_int_15h_8ah(); break;
1.1.1.22  root     15867:                case 0xc0: // PS/2 ???
                   15868:                case 0xc1:
                   15869:                case 0xc2:
1.1.1.30  root     15870:                case 0xc3: // PS50+ ???
                   15871:                case 0xc4:
1.1.1.22  root     15872:                        REG8(AH) = 0x86;
                   15873:                        m_CF = 1;
                   15874:                        break;
1.1.1.3   root     15875: #if defined(HAS_I386)
1.1       root     15876:                case 0xc9: pcbios_int_15h_c9h(); break;
1.1.1.3   root     15877: #endif
1.1       root     15878:                case 0xca: pcbios_int_15h_cah(); break;
1.1.1.22  root     15879:                case 0xe8: pcbios_int_15h_e8h(); break;
1.1       root     15880:                default:
1.1.1.22  root     15881:                        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));
                   15882:                        REG8(AH) = 0x86;
1.1.1.3   root     15883:                        m_CF = 1;
1.1       root     15884:                        break;
                   15885:                }
                   15886:                break;
                   15887:        case 0x16:
                   15888:                // PC BIOS - Keyboard
1.1.1.3   root     15889:                m_CF = 0;
1.1       root     15890:                switch(REG8(AH)) {
                   15891:                case 0x00: pcbios_int_16h_00h(); break;
                   15892:                case 0x01: pcbios_int_16h_01h(); break;
                   15893:                case 0x02: pcbios_int_16h_02h(); break;
                   15894:                case 0x03: pcbios_int_16h_03h(); break;
                   15895:                case 0x05: pcbios_int_16h_05h(); break;
                   15896:                case 0x10: pcbios_int_16h_00h(); break;
                   15897:                case 0x11: pcbios_int_16h_01h(); break;
                   15898:                case 0x12: pcbios_int_16h_12h(); break;
                   15899:                case 0x13: pcbios_int_16h_13h(); break;
                   15900:                case 0x14: pcbios_int_16h_14h(); break;
1.1.1.24  root     15901:                case 0x55: pcbios_int_16h_55h(); break;
1.1.1.30  root     15902:                case 0x6f: pcbios_int_16h_6fh(); break;
1.1.1.22  root     15903:                case 0xda: break; // unknown
1.1.1.43  root     15904:                case 0xdb: break; // unknown
1.1.1.22  root     15905:                case 0xff: break; // unknown
1.1       root     15906:                default:
1.1.1.22  root     15907:                        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     15908:                        break;
                   15909:                }
                   15910:                break;
                   15911:        case 0x17:
                   15912:                // PC BIOS - Printer
1.1.1.37  root     15913:                m_CF = 0;
                   15914:                switch(REG8(AH)) {
                   15915:                case 0x00: pcbios_int_17h_00h(); break;
                   15916:                case 0x01: pcbios_int_17h_01h(); break;
                   15917:                case 0x02: pcbios_int_17h_02h(); break;
                   15918:                case 0x03: pcbios_int_17h_03h(); break;
                   15919:                case 0x50: pcbios_int_17h_50h(); break;
                   15920:                case 0x51: pcbios_int_17h_51h(); break;
                   15921:                case 0x52: pcbios_int_17h_52h(); break;
                   15922:                case 0x84: pcbios_int_17h_84h(); break;
                   15923:                case 0x85: pcbios_int_17h_85h(); break;
                   15924:                default:
                   15925:                        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));
                   15926:                        break;
                   15927:                }
1.1       root     15928:                break;
                   15929:        case 0x1a:
                   15930:                // PC BIOS - Timer
1.1.1.3   root     15931:                m_CF = 0;
1.1       root     15932:                switch(REG8(AH)) {
                   15933:                case 0x00: pcbios_int_1ah_00h(); break;
                   15934:                case 0x01: break;
                   15935:                case 0x02: pcbios_int_1ah_02h(); break;
                   15936:                case 0x03: break;
                   15937:                case 0x04: pcbios_int_1ah_04h(); break;
                   15938:                case 0x05: break;
                   15939:                case 0x0a: pcbios_int_1ah_0ah(); break;
                   15940:                case 0x0b: break;
1.1.1.14  root     15941:                case 0x35: break; // Word Perfect Third Party Interface?
                   15942:                case 0x36: break; // Word Perfect Third Party Interface
                   15943:                case 0x70: break; // SNAP? (Simple Network Application Protocol)
1.1.1.44  root     15944:                case 0xb0: break; // Microsoft Real-Time Compression Interface (MRCI)
1.1.1.43  root     15945:                case 0xb1: break; // PCI BIOS v2.0c+
                   15946:                case 0xb4: break; // Intel Plug-and-Play Auto-Configuration
1.1       root     15947:                default:
1.1.1.22  root     15948:                        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     15949:                        break;
                   15950:                }
                   15951:                break;
1.1.1.33  root     15952:        case 0x1b:
                   15953:                mem[0x471] = 0x00;
                   15954:                break;
1.1       root     15955:        case 0x20:
1.1.1.28  root     15956:                try {
                   15957:                        msdos_process_terminate(SREG(CS), retval, 1);
                   15958:                } catch(...) {
                   15959:                        fatalerror("failed to terminate the process (PSP=%04X) by int 20h\n", SREG(CS));
                   15960:                }
1.1       root     15961:                break;
1.1.1.46  root     15962:        case 0x6f:
                   15963:                // dummy interrupt for case map routine pointed in the country info
                   15964: //             if(!(REG8(CL) >= 0x00 && REG8(CL) <= 0x24)) {
                   15965: //                     REG8(AL) = 0x00;
                   15966: //                     break;
                   15967: //             }
1.1       root     15968:        case 0x21:
                   15969:                // MS-DOS System Call
1.1.1.3   root     15970:                m_CF = 0;
1.1.1.28  root     15971:                try {
1.1.1.46  root     15972:                        switch(num == 0x21 ? REG8(AH) : REG8(CL)) {
1.1.1.28  root     15973:                        case 0x00: msdos_int_21h_00h(); break;
                   15974:                        case 0x01: msdos_int_21h_01h(); break;
                   15975:                        case 0x02: msdos_int_21h_02h(); break;
                   15976:                        case 0x03: msdos_int_21h_03h(); break;
                   15977:                        case 0x04: msdos_int_21h_04h(); break;
                   15978:                        case 0x05: msdos_int_21h_05h(); break;
                   15979:                        case 0x06: msdos_int_21h_06h(); break;
                   15980:                        case 0x07: msdos_int_21h_07h(); break;
                   15981:                        case 0x08: msdos_int_21h_08h(); break;
                   15982:                        case 0x09: msdos_int_21h_09h(); break;
                   15983:                        case 0x0a: msdos_int_21h_0ah(); break;
                   15984:                        case 0x0b: msdos_int_21h_0bh(); break;
                   15985:                        case 0x0c: msdos_int_21h_0ch(); break;
                   15986:                        case 0x0d: msdos_int_21h_0dh(); break;
                   15987:                        case 0x0e: msdos_int_21h_0eh(); break;
                   15988:                        case 0x0f: msdos_int_21h_0fh(); break;
                   15989:                        case 0x10: msdos_int_21h_10h(); break;
                   15990:                        case 0x11: msdos_int_21h_11h(); break;
                   15991:                        case 0x12: msdos_int_21h_12h(); break;
                   15992:                        case 0x13: msdos_int_21h_13h(); break;
                   15993:                        case 0x14: msdos_int_21h_14h(); break;
                   15994:                        case 0x15: msdos_int_21h_15h(); break;
                   15995:                        case 0x16: msdos_int_21h_16h(); break;
                   15996:                        case 0x17: msdos_int_21h_17h(); break;
                   15997:                        case 0x18: msdos_int_21h_18h(); break;
                   15998:                        case 0x19: msdos_int_21h_19h(); break;
                   15999:                        case 0x1a: msdos_int_21h_1ah(); break;
                   16000:                        case 0x1b: msdos_int_21h_1bh(); break;
                   16001:                        case 0x1c: msdos_int_21h_1ch(); break;
                   16002:                        case 0x1d: msdos_int_21h_1dh(); break;
                   16003:                        case 0x1e: msdos_int_21h_1eh(); break;
                   16004:                        case 0x1f: msdos_int_21h_1fh(); break;
                   16005:                        case 0x20: msdos_int_21h_20h(); break;
                   16006:                        case 0x21: msdos_int_21h_21h(); break;
                   16007:                        case 0x22: msdos_int_21h_22h(); break;
                   16008:                        case 0x23: msdos_int_21h_23h(); break;
                   16009:                        case 0x24: msdos_int_21h_24h(); break;
                   16010:                        case 0x25: msdos_int_21h_25h(); break;
                   16011:                        case 0x26: msdos_int_21h_26h(); break;
                   16012:                        case 0x27: msdos_int_21h_27h(); break;
                   16013:                        case 0x28: msdos_int_21h_28h(); break;
                   16014:                        case 0x29: msdos_int_21h_29h(); break;
                   16015:                        case 0x2a: msdos_int_21h_2ah(); break;
                   16016:                        case 0x2b: msdos_int_21h_2bh(); break;
                   16017:                        case 0x2c: msdos_int_21h_2ch(); break;
                   16018:                        case 0x2d: msdos_int_21h_2dh(); break;
                   16019:                        case 0x2e: msdos_int_21h_2eh(); break;
                   16020:                        case 0x2f: msdos_int_21h_2fh(); break;
                   16021:                        case 0x30: msdos_int_21h_30h(); break;
                   16022:                        case 0x31: msdos_int_21h_31h(); break;
                   16023:                        case 0x32: msdos_int_21h_32h(); break;
                   16024:                        case 0x33: msdos_int_21h_33h(); break;
                   16025:                        case 0x34: msdos_int_21h_34h(); break;
                   16026:                        case 0x35: msdos_int_21h_35h(); break;
                   16027:                        case 0x36: msdos_int_21h_36h(); break;
                   16028:                        case 0x37: msdos_int_21h_37h(); break;
                   16029:                        case 0x38: msdos_int_21h_38h(); break;
                   16030:                        case 0x39: msdos_int_21h_39h(0); break;
                   16031:                        case 0x3a: msdos_int_21h_3ah(0); break;
                   16032:                        case 0x3b: msdos_int_21h_3bh(0); break;
                   16033:                        case 0x3c: msdos_int_21h_3ch(); break;
                   16034:                        case 0x3d: msdos_int_21h_3dh(); break;
                   16035:                        case 0x3e: msdos_int_21h_3eh(); break;
                   16036:                        case 0x3f: msdos_int_21h_3fh(); break;
                   16037:                        case 0x40: msdos_int_21h_40h(); break;
                   16038:                        case 0x41: msdos_int_21h_41h(0); break;
                   16039:                        case 0x42: msdos_int_21h_42h(); break;
                   16040:                        case 0x43: msdos_int_21h_43h(0); break;
                   16041:                        case 0x44: msdos_int_21h_44h(); break;
                   16042:                        case 0x45: msdos_int_21h_45h(); break;
                   16043:                        case 0x46: msdos_int_21h_46h(); break;
                   16044:                        case 0x47: msdos_int_21h_47h(0); break;
                   16045:                        case 0x48: msdos_int_21h_48h(); break;
                   16046:                        case 0x49: msdos_int_21h_49h(); break;
                   16047:                        case 0x4a: msdos_int_21h_4ah(); break;
                   16048:                        case 0x4b: msdos_int_21h_4bh(); break;
                   16049:                        case 0x4c: msdos_int_21h_4ch(); break;
                   16050:                        case 0x4d: msdos_int_21h_4dh(); break;
                   16051:                        case 0x4e: msdos_int_21h_4eh(); break;
                   16052:                        case 0x4f: msdos_int_21h_4fh(); break;
                   16053:                        case 0x50: msdos_int_21h_50h(); break;
                   16054:                        case 0x51: msdos_int_21h_51h(); break;
                   16055:                        case 0x52: msdos_int_21h_52h(); break;
1.1.1.43  root     16056:                        case 0x53: msdos_int_21h_53h(); break;
1.1.1.28  root     16057:                        case 0x54: msdos_int_21h_54h(); break;
                   16058:                        case 0x55: msdos_int_21h_55h(); break;
                   16059:                        case 0x56: msdos_int_21h_56h(0); break;
                   16060:                        case 0x57: msdos_int_21h_57h(); break;
                   16061:                        case 0x58: msdos_int_21h_58h(); break;
                   16062:                        case 0x59: msdos_int_21h_59h(); break;
                   16063:                        case 0x5a: msdos_int_21h_5ah(); break;
                   16064:                        case 0x5b: msdos_int_21h_5bh(); break;
                   16065:                        case 0x5c: msdos_int_21h_5ch(); break;
                   16066:                        case 0x5d: msdos_int_21h_5dh(); break;
1.1.1.42  root     16067:                        case 0x5e: msdos_int_21h_5eh(); break;
1.1.1.30  root     16068:                        case 0x5f: msdos_int_21h_5fh(); break;
1.1.1.28  root     16069:                        case 0x60: msdos_int_21h_60h(0); break;
                   16070:                        case 0x61: msdos_int_21h_61h(); break;
                   16071:                        case 0x62: msdos_int_21h_62h(); break;
                   16072:                        case 0x63: msdos_int_21h_63h(); break;
1.1.1.33  root     16073:                        // 0x64: Set Device Driver Lockahead Flag
1.1.1.28  root     16074:                        case 0x65: msdos_int_21h_65h(); break;
                   16075:                        case 0x66: msdos_int_21h_66h(); break;
                   16076:                        case 0x67: msdos_int_21h_67h(); break;
                   16077:                        case 0x68: msdos_int_21h_68h(); break;
                   16078:                        case 0x69: msdos_int_21h_69h(); break;
                   16079:                        case 0x6a: msdos_int_21h_6ah(); break;
                   16080:                        case 0x6b: msdos_int_21h_6bh(); break;
                   16081:                        case 0x6c: msdos_int_21h_6ch(0); break;
1.1.1.48! root     16082:                        case 0x6d: // Find First ROM Program
        !          16083:                        case 0x6e: // Find Next ROM Program
        !          16084:                        case 0x6f: // Get/Set ROM Scan Start Address
        !          16085:                                REG8(AL) = 0x00; // if not supported (DOS <5, MS-DOS 5+ non-ROM versions)
        !          16086:                                break;
1.1.1.43  root     16087:                        case 0x70: msdos_int_21h_70h(); break;
1.1.1.48! root     16088:                        case 0x71: // Windows95 - Long Filename Functions
1.1.1.28  root     16089:                                switch(REG8(AL)) {
                   16090:                                case 0x0d: msdos_int_21h_710dh(); break;
                   16091:                                case 0x39: msdos_int_21h_39h(1); break;
                   16092:                                case 0x3a: msdos_int_21h_3ah(1); break;
                   16093:                                case 0x3b: msdos_int_21h_3bh(1); break;
1.1.1.48! root     16094:                                case 0x41: msdos_int_21h_7141h(); break;
1.1.1.28  root     16095:                                case 0x43: msdos_int_21h_43h(1); break;
                   16096:                                case 0x47: msdos_int_21h_47h(1); break;
                   16097:                                case 0x4e: msdos_int_21h_714eh(); break;
                   16098:                                case 0x4f: msdos_int_21h_714fh(); break;
                   16099:                                case 0x56: msdos_int_21h_56h(1); break;
                   16100:                                case 0x60: msdos_int_21h_60h(1); break;
                   16101:                                case 0x6c: msdos_int_21h_6ch(1); break;
                   16102:                                case 0xa0: msdos_int_21h_71a0h(); break;
                   16103:                                case 0xa1: msdos_int_21h_71a1h(); break;
                   16104:                                case 0xa6: msdos_int_21h_71a6h(); break;
                   16105:                                case 0xa7: msdos_int_21h_71a7h(); break;
                   16106:                                case 0xa8: msdos_int_21h_71a8h(); break;
1.1.1.45  root     16107:                                case 0xa9: msdos_int_21h_6ch(1); break;
1.1.1.28  root     16108:                                case 0xaa: msdos_int_21h_71aah(); break;
                   16109:                                default:
                   16110:                                        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));
                   16111:                                        REG16(AX) = 0x7100;
                   16112:                                        m_CF = 1;
                   16113:                                        break;
                   16114:                                }
                   16115:                                break;
1.1.1.48! root     16116:                        case 0x72: // Windows95 beta - LFN FindClose
        !          16117: //                             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));
        !          16118:                                REG16(AX) = 0x7200;
        !          16119:                                m_CF = 1;
        !          16120:                                break;
        !          16121:                        case 0x73: // Windows95 - FAT32 Functions
1.1.1.28  root     16122:                                switch(REG8(AL)) {
                   16123:                                case 0x00: msdos_int_21h_7300h(); break;
1.1.1.33  root     16124:                                // 0x01: Set Drive Locking ???
1.1.1.28  root     16125:                                case 0x02: msdos_int_21h_7302h(); break;
                   16126:                                case 0x03: msdos_int_21h_7303h(); break;
1.1.1.33  root     16127:                                // 0x04: Set DPB to Use for Formatting
                   16128:                                // 0x05: Extended Absolute Disk Read/Write
1.1.1.28  root     16129:                                default:
                   16130:                                        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));
                   16131:                                        REG16(AX) = 0x7300;
                   16132:                                        m_CF = 1;
                   16133:                                        break;
                   16134:                                }
1.1       root     16135:                                break;
1.1.1.30  root     16136:                        case 0xdb: msdos_int_21h_dbh(); break;
                   16137:                        case 0xdc: msdos_int_21h_dch(); break;
1.1       root     16138:                        default:
1.1.1.22  root     16139:                                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     16140:                                REG16(AX) = 0x01;
1.1.1.3   root     16141:                                m_CF = 1;
1.1       root     16142:                                break;
                   16143:                        }
1.1.1.28  root     16144:                } catch(int error) {
                   16145:                        REG16(AX) = error;
                   16146:                        m_CF = 1;
                   16147:                } catch(...) {
                   16148:                        REG16(AX) = 0x1f; // general failure
1.1.1.3   root     16149:                        m_CF = 1;
1.1       root     16150:                }
1.1.1.3   root     16151:                if(m_CF) {
1.1.1.23  root     16152:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
1.1.1.47  root     16153:                        sda->int21h_5d0ah_called = 0;
1.1.1.23  root     16154:                        sda->extended_error_code = REG16(AX);
                   16155:                        switch(sda->extended_error_code) {
                   16156:                        case  4: // Too many open files
                   16157:                        case  8: // Insufficient memory
                   16158:                                sda->error_class = 1; // Out of resource
                   16159:                                break;
                   16160:                        case  5: // Access denied
                   16161:                                sda->error_class = 3; // Authorization
                   16162:                                break;
                   16163:                        case  7: // Memory control block destroyed
                   16164:                                sda->error_class = 4; // Internal
                   16165:                                break;
                   16166:                        case  2: // File not found
                   16167:                        case  3: // Path not found
                   16168:                        case 15: // Invaid drive specified
                   16169:                        case 18: // No more files
                   16170:                                sda->error_class = 8; // Not found
                   16171:                                break;
                   16172:                        case 32: // Sharing violation
                   16173:                        case 33: // Lock violation
                   16174:                                sda->error_class = 10; // Locked
                   16175:                                break;
                   16176: //                     case 16: // Removal of current directory attempted
                   16177:                        case 19: // Attempted write on protected disk
                   16178:                        case 21: // Drive not ready
                   16179: //                     case 29: // Write failure
                   16180: //                     case 30: // Read failure
                   16181: //                     case 82: // Cannot create subdirectory
                   16182:                                sda->error_class = 11; // Media
                   16183:                                break;
                   16184:                        case 80: // File already exists
                   16185:                                sda->error_class = 12; // Already exist
                   16186:                                break;
                   16187:                        default:
                   16188:                                sda->error_class = 13; // Unknown
                   16189:                                break;
                   16190:                        }
                   16191:                        sda->suggested_action = 1; // Retry
                   16192:                        sda->locus_of_last_error = 1; // Unknown
1.1       root     16193:                }
1.1.1.33  root     16194:                if(ctrl_break_checking && ctrl_break_detected) {
1.1.1.26  root     16195:                        // raise int 23h
                   16196: #if defined(HAS_I386)
                   16197:                        m_ext = 0; // not an external interrupt
                   16198:                        i386_trap(0x23, 1, 0);
                   16199:                        m_ext = 1;
                   16200: #else
                   16201:                        PREFIX86(_interrupt)(0x23);
                   16202: #endif
                   16203:                }
1.1       root     16204:                break;
                   16205:        case 0x22:
                   16206:                fatalerror("int 22h (terminate address)\n");
                   16207:        case 0x23:
1.1.1.28  root     16208:                try {
                   16209:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x100, 1);
                   16210:                } catch(...) {
                   16211:                        fatalerror("failed to terminate the current process by int 23h\n");
                   16212:                }
1.1       root     16213:                break;
                   16214:        case 0x24:
1.1.1.32  root     16215: /*
1.1.1.28  root     16216:                try {
                   16217:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   16218:                } catch(...) {
                   16219:                        fatalerror("failed to terminate the current process by int 24h\n");
                   16220:                }
1.1.1.32  root     16221: */
                   16222:                msdos_int_24h();
1.1       root     16223:                break;
                   16224:        case 0x25:
                   16225:                msdos_int_25h();
                   16226:                break;
                   16227:        case 0x26:
                   16228:                msdos_int_26h();
                   16229:                break;
                   16230:        case 0x27:
1.1.1.28  root     16231:                try {
                   16232:                        msdos_int_27h();
                   16233:                } catch(...) {
                   16234:                        fatalerror("failed to terminate the process (PSP=%04X) by int 27h\n", SREG(CS));
                   16235:                }
1.1       root     16236:                break;
                   16237:        case 0x28:
                   16238:                Sleep(10);
1.1.1.35  root     16239:                REQUEST_HARDWRE_UPDATE();
1.1       root     16240:                break;
                   16241:        case 0x29:
                   16242:                msdos_int_29h();
                   16243:                break;
                   16244:        case 0x2e:
                   16245:                msdos_int_2eh();
                   16246:                break;
                   16247:        case 0x2f:
                   16248:                // multiplex interrupt
                   16249:                switch(REG8(AH)) {
1.1.1.22  root     16250:                case 0x05: msdos_int_2fh_05h(); break;
1.1.1.44  root     16251:                case 0x06: msdos_int_2fh_06h(); break;
1.1.1.22  root     16252:                case 0x11: msdos_int_2fh_11h(); break;
1.1.1.21  root     16253:                case 0x12: msdos_int_2fh_12h(); break;
1.1.1.30  root     16254:                case 0x13: msdos_int_2fh_13h(); break;
1.1.1.22  root     16255:                case 0x14: msdos_int_2fh_14h(); break;
                   16256:                case 0x15: msdos_int_2fh_15h(); break;
1.1       root     16257:                case 0x16: msdos_int_2fh_16h(); break;
1.1.1.22  root     16258:                case 0x19: msdos_int_2fh_19h(); break;
1.1       root     16259:                case 0x1a: msdos_int_2fh_1ah(); break;
1.1.1.30  root     16260:                case 0x40: msdos_int_2fh_40h(); break;
1.1       root     16261:                case 0x43: msdos_int_2fh_43h(); break;
1.1.1.22  root     16262:                case 0x46: msdos_int_2fh_46h(); break;
                   16263:                case 0x48: msdos_int_2fh_48h(); break;
1.1       root     16264:                case 0x4a: msdos_int_2fh_4ah(); break;
1.1.1.22  root     16265:                case 0x4b: msdos_int_2fh_4bh(); break;
1.1.1.44  root     16266:                case 0x4d: msdos_int_2fh_4dh(); break;
1.1       root     16267:                case 0x4f: msdos_int_2fh_4fh(); break;
1.1.1.22  root     16268:                case 0x55: msdos_int_2fh_55h(); break;
1.1.1.44  root     16269:                case 0x56: msdos_int_2fh_56h(); break;
1.1.1.24  root     16270:                case 0xad: msdos_int_2fh_adh(); break;
1.1       root     16271:                case 0xae: msdos_int_2fh_aeh(); break;
1.1.1.34  root     16272:                case 0xb7: msdos_int_2fh_b7h(); break;
1.1.1.43  root     16273:                default:
1.1.1.30  root     16274:                        switch(REG8(AL)) {
                   16275:                        case 0x00:
                   16276:                                // This is not installed
                   16277: //                             REG8(AL) = 0x00;
                   16278:                                break;
1.1.1.33  root     16279:                        case 0x01:
1.1.1.42  root     16280:                                // Quarterdeck RPCI - QEMM/QRAM - PCL-838.EXE is not installed
                   16281:                                if(REG8(AH) == 0xd2 && REG16(BX) == 0x5145 && REG16(CX) == 0x4d4d && REG16(DX) == 0x3432) {
                   16282:                                        break;
                   16283:                                }
1.1.1.33  root     16284:                                // Banyan VINES v4+ is not installed
                   16285:                                if(REG8(AH) == 0xd7 && REG16(BX) == 0x0000) {
                   16286:                                        break;
                   16287:                                }
1.1.1.42  root     16288:                                // Quarterdeck QDPMI.SYS v1.0 is not installed
                   16289:                                if(REG8(AH) == 0xde && REG16(BX) == 0x4450 && REG16(CX) == 0x4d49 && REG16(DX) == 0x8f4f) {
                   16290:                                        break;
                   16291:                                }
1.1.1.30  root     16292:                        default:
1.1.1.42  root     16293:                                // NORTON UTILITIES 5.0+
                   16294:                                if(REG8(AH) == 0xfe && REG16(DI) == 0x4e55) {
                   16295:                                        break;
                   16296:                                }
1.1.1.30  root     16297:                                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     16298:                                REG16(AX) = 0x01; // invalid function
1.1.1.30  root     16299:                                m_CF = 1;
                   16300:                                break;
                   16301:                        }
                   16302:                        break;
1.1       root     16303:                }
                   16304:                break;
1.1.1.24  root     16305:        case 0x33:
                   16306:                switch(REG8(AH)) {
                   16307:                case 0x00:
                   16308:                        // Mouse
                   16309:                        switch(REG8(AL)) {
                   16310:                        case 0x00: msdos_int_33h_0000h(); break;
                   16311:                        case 0x01: msdos_int_33h_0001h(); break;
                   16312:                        case 0x02: msdos_int_33h_0002h(); break;
                   16313:                        case 0x03: msdos_int_33h_0003h(); break;
1.1.1.34  root     16314:                        case 0x04: msdos_int_33h_0004h(); break;
1.1.1.24  root     16315:                        case 0x05: msdos_int_33h_0005h(); break;
                   16316:                        case 0x06: msdos_int_33h_0006h(); break;
                   16317:                        case 0x07: msdos_int_33h_0007h(); break;
                   16318:                        case 0x08: msdos_int_33h_0008h(); break;
                   16319:                        case 0x09: msdos_int_33h_0009h(); break;
1.1.1.34  root     16320:                        case 0x0a: break; // Define Text Cursor
1.1.1.24  root     16321:                        case 0x0b: msdos_int_33h_000bh(); break;
                   16322:                        case 0x0c: msdos_int_33h_000ch(); break;
1.1.1.34  root     16323:                        case 0x0d: break; // Light Pen Emulation On
                   16324:                        case 0x0e: break; // Light Pen Emulation Off
1.1.1.24  root     16325:                        case 0x0f: msdos_int_33h_000fh(); break;
1.1.1.34  root     16326:                        case 0x10: break; // Define Screen Region for Updating
1.1.1.24  root     16327:                        case 0x11: msdos_int_33h_0011h(); break;
1.1.1.34  root     16328:                        case 0x12: REG16(AX) = 0xffff; break; // Set Large Graphics Cursor Block
                   16329:                        case 0x13: break; // Define Double-Speed Threshold
1.1.1.24  root     16330:                        case 0x14: msdos_int_33h_0014h(); break;
                   16331:                        case 0x15: msdos_int_33h_0015h(); break;
                   16332:                        case 0x16: msdos_int_33h_0016h(); break;
                   16333:                        case 0x17: msdos_int_33h_0017h(); break;
1.1.1.43  root     16334:                        case 0x18: msdos_int_33h_0018h(); break;
                   16335:                        case 0x19: msdos_int_33h_0019h(); break;
1.1.1.24  root     16336:                        case 0x1a: msdos_int_33h_001ah(); break;
                   16337:                        case 0x1b: msdos_int_33h_001bh(); break;
                   16338:                        case 0x1d: msdos_int_33h_001dh(); break;
                   16339:                        case 0x1e: msdos_int_33h_001eh(); break;
1.1.1.34  root     16340:                        case 0x1f: msdos_int_33h_001fh(); break;
                   16341:                        case 0x20: msdos_int_33h_0020h(); break;
1.1.1.24  root     16342:                        case 0x21: msdos_int_33h_0021h(); break;
                   16343:                        case 0x22: msdos_int_33h_0022h(); break;
                   16344:                        case 0x23: msdos_int_33h_0023h(); break;
                   16345:                        case 0x24: msdos_int_33h_0024h(); break;
                   16346:                        case 0x26: msdos_int_33h_0026h(); break;
                   16347:                        case 0x2a: msdos_int_33h_002ah(); break;
1.1.1.34  root     16348:                        case 0x2f: break; // Mouse Hardware Reset
1.1.1.24  root     16349:                        case 0x31: msdos_int_33h_0031h(); break;
                   16350:                        case 0x32: msdos_int_33h_0032h(); break;
                   16351:                        default:
                   16352:                                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));
                   16353:                                break;
                   16354:                        }
                   16355:                        break;
                   16356:                default:
                   16357:                        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));
                   16358:                        break;
                   16359:                }
                   16360:                break;
1.1.1.19  root     16361:        case 0x68:
                   16362:                // dummy interrupt for EMS (int 67h)
                   16363:                switch(REG8(AH)) {
                   16364:                case 0x40: msdos_int_67h_40h(); break;
                   16365:                case 0x41: msdos_int_67h_41h(); break;
                   16366:                case 0x42: msdos_int_67h_42h(); break;
                   16367:                case 0x43: msdos_int_67h_43h(); break;
                   16368:                case 0x44: msdos_int_67h_44h(); break;
                   16369:                case 0x45: msdos_int_67h_45h(); break;
                   16370:                case 0x46: msdos_int_67h_46h(); break;
                   16371:                case 0x47: msdos_int_67h_47h(); break;
                   16372:                case 0x48: msdos_int_67h_48h(); break;
1.1.1.20  root     16373:                // 0x49: LIM EMS - Reserved - Get I/O Port Address (Undocumented in EMS 3.2)
                   16374:                // 0x4a: LIM EMS - Reserved - Get Translation Array (Undocumented in EMS 3.2)
1.1.1.19  root     16375:                case 0x4b: msdos_int_67h_4bh(); break;
                   16376:                case 0x4c: msdos_int_67h_4ch(); break;
                   16377:                case 0x4d: msdos_int_67h_4dh(); break;
1.1.1.20  root     16378:                case 0x4e: msdos_int_67h_4eh(); break;
1.1.1.21  root     16379:                case 0x4f: msdos_int_67h_4fh(); break;
1.1.1.20  root     16380:                case 0x50: msdos_int_67h_50h(); break;
1.1.1.19  root     16381:                case 0x51: msdos_int_67h_51h(); break;
1.1.1.20  root     16382:                case 0x52: msdos_int_67h_52h(); break;
1.1.1.19  root     16383:                case 0x53: msdos_int_67h_53h(); break;
                   16384:                case 0x54: msdos_int_67h_54h(); break;
1.1.1.20  root     16385:                // 0x55: LIM EMS 4.0 - Alter Page Map And JUMP
                   16386:                // 0x56: LIM EMS 4.0 - Alter Page Map And CALL
                   16387:                case 0x57: msdos_int_67h_57h(); break;
                   16388:                case 0x58: msdos_int_67h_58h(); break;
1.1.1.42  root     16389:                case 0x59: msdos_int_67h_59h(); break;
1.1.1.20  root     16390:                case 0x5a: msdos_int_67h_5ah(); break;
                   16391:                // 0x5b: LIM EMS 4.0 - Alternate Map Register Set (for DOS Kernel)
1.1.1.43  root     16392:                // 0x5c: LIM EMS 4.0 - Prepare Expanded Memory Hardware For Warm Boot
                   16393:                case 0x5d: msdos_int_67h_5dh(); break;
1.1.1.31  root     16394:                // 0x60: EEMS - Get Physical Window Array
                   16395:                // 0x61: EEMS - Generic Accelerator Card Support
                   16396:                // 0x68: EEMS - Get Address of All Pge Frames om System
                   16397:                // 0x69: EEMS - Map Page into Frame
                   16398:                // 0x6a: EEMS - Page Mapping
                   16399:                // 0xde: VCPI
1.1.1.30  root     16400:                case 0xde: msdos_int_67h_deh(); break;
1.1.1.19  root     16401:                default:
1.1.1.22  root     16402:                        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     16403:                        REG8(AH) = 0x84;
                   16404:                        break;
                   16405:                }
                   16406:                break;
                   16407: #ifdef SUPPORT_XMS
                   16408:        case 0x69:
                   16409:                // dummy interrupt for XMS (call far)
1.1.1.28  root     16410:                try {
                   16411:                        switch(REG8(AH)) {
                   16412:                        case 0x00: msdos_call_xms_00h(); break;
                   16413:                        case 0x01: msdos_call_xms_01h(); break;
                   16414:                        case 0x02: msdos_call_xms_02h(); break;
                   16415:                        case 0x03: msdos_call_xms_03h(); break;
                   16416:                        case 0x04: msdos_call_xms_04h(); break;
                   16417:                        case 0x05: msdos_call_xms_05h(); break;
                   16418:                        case 0x06: msdos_call_xms_06h(); break;
                   16419:                        case 0x07: msdos_call_xms_07h(); break;
                   16420:                        case 0x08: msdos_call_xms_08h(); break;
                   16421:                        case 0x09: msdos_call_xms_09h(); break;
                   16422:                        case 0x0a: msdos_call_xms_0ah(); break;
                   16423:                        case 0x0b: msdos_call_xms_0bh(); break;
                   16424:                        case 0x0c: msdos_call_xms_0ch(); break;
                   16425:                        case 0x0d: msdos_call_xms_0dh(); break;
                   16426:                        case 0x0e: msdos_call_xms_0eh(); break;
                   16427:                        case 0x0f: msdos_call_xms_0fh(); break;
                   16428:                        case 0x10: msdos_call_xms_10h(); break;
                   16429:                        case 0x11: msdos_call_xms_11h(); break;
                   16430:                        case 0x12: msdos_call_xms_12h(); break;
1.1.1.29  root     16431: #if defined(HAS_I386)
                   16432:                        case 0x88: msdos_call_xms_88h(); break;
                   16433:                        case 0x89: msdos_call_xms_89h(); break;
                   16434:                        case 0x8e: msdos_call_xms_8eh(); break;
                   16435:                        case 0x8f: msdos_call_xms_8fh(); break;
                   16436: #endif
1.1.1.28  root     16437:                        default:
                   16438:                                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));
                   16439:                                REG16(AX) = 0x0000;
                   16440:                                REG8(BL) = 0x80; // function not implemented
                   16441:                                break;
                   16442:                        }
                   16443:                } catch(...) {
1.1.1.19  root     16444:                        REG16(AX) = 0x0000;
1.1.1.28  root     16445:                        REG8(BL) = 0x8f; // unrecoverable driver error
1.1.1.19  root     16446:                }
                   16447:                break;
                   16448: #endif
                   16449:        case 0x6a:
1.1.1.24  root     16450:                // irq12 (mouse)
                   16451:                mouse_push_ax = REG16(AX);
                   16452:                mouse_push_bx = REG16(BX);
                   16453:                mouse_push_cx = REG16(CX);
                   16454:                mouse_push_dx = REG16(DX);
                   16455:                mouse_push_si = REG16(SI);
                   16456:                mouse_push_di = REG16(DI);
                   16457:                
1.1.1.43  root     16458:                if(mouse.status_irq && mouse.call_addr.dw) {
1.1.1.24  root     16459:                        REG16(AX) = mouse.status_irq;
                   16460:                        REG16(BX) = mouse.get_buttons();
1.1.1.34  root     16461:                        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   16462:                        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
1.1.1.24  root     16463:                        REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
                   16464:                        REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
                   16465:                        
                   16466:                        mem[0xfffd0 + 0x02] = 0x9a;     // call far
                   16467:                        mem[0xfffd0 + 0x03] = mouse.call_addr.w.l & 0xff;
                   16468:                        mem[0xfffd0 + 0x04] = mouse.call_addr.w.l >> 8;
                   16469:                        mem[0xfffd0 + 0x05] = mouse.call_addr.w.h & 0xff;
                   16470:                        mem[0xfffd0 + 0x06] = mouse.call_addr.w.h >> 8;
1.1.1.43  root     16471:                        break;
1.1.1.24  root     16472:                }
1.1.1.43  root     16473:                for(int i = 0; i < 8; i++) {
                   16474:                        if((mouse.status_irq_alt & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   16475:                                REG16(AX) = mouse.status_irq_alt;
                   16476:                                REG16(BX) = mouse.get_buttons();
                   16477:                                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   16478:                                REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
                   16479:                                REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
                   16480:                                REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
                   16481:                                
                   16482:                                mem[0xfffd0 + 0x02] = 0x9a;     // call far
                   16483:                                mem[0xfffd0 + 0x03] = mouse.call_addr_alt[i].w.l & 0xff;
                   16484:                                mem[0xfffd0 + 0x04] = mouse.call_addr_alt[i].w.l >> 8;
                   16485:                                mem[0xfffd0 + 0x05] = mouse.call_addr_alt[i].w.h & 0xff;
                   16486:                                mem[0xfffd0 + 0x06] = mouse.call_addr_alt[i].w.h >> 8;
                   16487:                                break;
                   16488:                        }
                   16489:                }
                   16490:                // invalid call addr :-(
                   16491:                mem[0xfffd0 + 0x02] = 0x90;     // nop
                   16492:                mem[0xfffd0 + 0x03] = 0x90;     // nop
                   16493:                mem[0xfffd0 + 0x04] = 0x90;     // nop
                   16494:                mem[0xfffd0 + 0x05] = 0x90;     // nop
                   16495:                mem[0xfffd0 + 0x06] = 0x90;     // nop
1.1.1.24  root     16496:                break;
                   16497:        case 0x6b:
                   16498:                // end of irq12 (mouse)
                   16499:                REG16(AX) = mouse_push_ax;
                   16500:                REG16(BX) = mouse_push_bx;
                   16501:                REG16(CX) = mouse_push_cx;
                   16502:                REG16(DX) = mouse_push_dx;
                   16503:                REG16(SI) = mouse_push_si;
                   16504:                REG16(DI) = mouse_push_di;
                   16505:                
                   16506:                // EOI
                   16507:                if((pic[1].isr &= ~(1 << 4)) == 0) {
                   16508:                        pic[0].isr &= ~(1 << 2); // master
                   16509:                }
                   16510:                pic_update();
                   16511:                break;
                   16512:        case 0x6c:
1.1.1.19  root     16513:                // dummy interrupt for case map routine pointed in the country info
                   16514:                if(REG8(AL) >= 0x80) {
                   16515:                        char tmp[2] = {0};
                   16516:                        tmp[0] = REG8(AL);
                   16517:                        my_strupr(tmp);
                   16518:                        REG8(AL) = tmp[0];
                   16519:                }
                   16520:                break;
1.1.1.27  root     16521:        case 0x6d:
                   16522:                // dummy interrupt for font read routine pointed by int 15h, ax=5000h
                   16523:                REG8(AL) = 0x86; // not supported
                   16524:                m_CF = 1;
                   16525:                break;
1.1.1.32  root     16526:        case 0x6e:
                   16527:                // dummy interrupt for parameter error message read routine pointed by int 2fh, ax=122eh, dl=08h
                   16528:                {
                   16529:                        UINT16 code = REG16(AX);
                   16530:                        if(code & 0xf0) {
                   16531:                                code = (code & 7) | ((code & 0x10) >> 1);
                   16532:                        }
                   16533:                        for(int i = 0; i < array_length(param_error_table); i++) {
                   16534:                                if(param_error_table[i].code == code || param_error_table[i].code == (UINT16)-1) {
                   16535:                                        const char *message = NULL;
                   16536:                                        if(active_code_page == 932) {
                   16537:                                                message = param_error_table[i].message_japanese;
                   16538:                                        }
                   16539:                                        if(message == NULL) {
                   16540:                                                message = param_error_table[i].message_english;
                   16541:                                        }
                   16542:                                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   16543:                                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   16544:                                        
                   16545:                                        SREG(ES) = WORK_TOP >> 4;
                   16546:                                        i386_load_segment_descriptor(ES);
                   16547:                                        REG16(DI) = 0x0000;
                   16548:                                        break;
                   16549:                                }
                   16550:                        }
                   16551:                }
                   16552:                break;
1.1.1.8   root     16553:        case 0x70:
                   16554:        case 0x71:
                   16555:        case 0x72:
                   16556:        case 0x73:
                   16557:        case 0x74:
                   16558:        case 0x75:
                   16559:        case 0x76:
                   16560:        case 0x77:
                   16561:                // EOI
                   16562:                if((pic[1].isr &= ~(1 << (num - 0x70))) == 0) {
                   16563:                        pic[0].isr &= ~(1 << 2); // master
                   16564:                }
                   16565:                pic_update();
                   16566:                break;
1.1       root     16567:        default:
1.1.1.22  root     16568: //             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     16569:                break;
                   16570:        }
                   16571:        
                   16572:        // update cursor position
                   16573:        if(cursor_moved) {
1.1.1.36  root     16574:                pcbios_update_cursor_position();
1.1       root     16575:                cursor_moved = false;
                   16576:        }
                   16577: }
                   16578: 
                   16579: // init
                   16580: 
                   16581: int msdos_init(int argc, char *argv[], char *envp[], int standard_env)
                   16582: {
                   16583:        // init file handler
                   16584:        memset(file_handler, 0, sizeof(file_handler));
                   16585:        msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   16586:        msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   16587:        msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
1.1.1.21  root     16588: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     16589:        if(_open("stdaux.txt", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
1.1.1.21  root     16590: #else
                   16591:        if(_open("NUL", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
                   16592: #endif
                   16593:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
1.1       root     16594:        }
1.1.1.21  root     16595:        if(_open("NUL", _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 4) {
1.1.1.45  root     16596: //             msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0, 0, 1); // LPT1
                   16597:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0x80a0, 0, 0, 1); // LPT1
1.1.1.21  root     16598:        }
1.1       root     16599:        _dup2(0, DUP_STDIN);
                   16600:        _dup2(1, DUP_STDOUT);
                   16601:        _dup2(2, DUP_STDERR);
1.1.1.21  root     16602:        _dup2(3, DUP_STDAUX);
                   16603:        _dup2(4, DUP_STDPRN);
1.1       root     16604:        
1.1.1.24  root     16605:        // init mouse
                   16606:        memset(&mouse, 0, sizeof(mouse));
1.1.1.34  root     16607:        mouse.enabled = true;   // from DOSBox
                   16608:        mouse.hidden = 1;       // hidden in default ???
                   16609:        mouse.old_hidden = 1;   // from DOSBox
                   16610:        mouse.max_position.x = 8 * (scr_width  - 1);
                   16611:        mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24  root     16612:        mouse.mickey.x = 8;
                   16613:        mouse.mickey.y = 16;
                   16614:        
1.1.1.26  root     16615: #ifdef SUPPORT_XMS
                   16616:        // init xms
                   16617:        msdos_xms_init();
                   16618: #endif
                   16619:        
1.1       root     16620:        // init process
                   16621:        memset(process, 0, sizeof(process));
                   16622:        
1.1.1.13  root     16623:        // init dtainfo
                   16624:        msdos_dta_info_init();
                   16625:        
1.1       root     16626:        // init memory
                   16627:        memset(mem, 0, sizeof(mem));
                   16628:        
                   16629:        // bios data area
1.1.1.23  root     16630:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     16631:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   16632:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.14  root     16633:        CONSOLE_FONT_INFO cfi;
                   16634:        GetCurrentConsoleFont(hStdout, FALSE, &cfi);
                   16635:        
                   16636:        int regen = min(scr_width * scr_height * 2, 0x8000);
                   16637:        text_vram_top_address = TEXT_VRAM_TOP;
                   16638:        text_vram_end_address = text_vram_top_address + regen;
                   16639:        shadow_buffer_top_address = SHADOW_BUF_TOP;
                   16640:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
                   16641:        
                   16642:        if(regen > 0x4000) {
                   16643:                regen = 0x8000;
                   16644:                vram_pages = 1;
                   16645:        } else if(regen > 0x2000) {
                   16646:                regen = 0x4000;
                   16647:                vram_pages = 2;
                   16648:        } else if(regen > 0x1000) {
                   16649:                regen = 0x2000;
                   16650:                vram_pages = 4;
                   16651:        } else {
                   16652:                regen = 0x1000;
                   16653:                vram_pages = 8;
                   16654:        }
1.1       root     16655:        
1.1.1.25  root     16656:        *(UINT16 *)(mem + 0x400) = 0x3f8; // com1 port address
                   16657:        *(UINT16 *)(mem + 0x402) = 0x2f8; // com2 port address
1.1.1.29  root     16658:        *(UINT16 *)(mem + 0x404) = 0x3e8; // com3 port address
                   16659:        *(UINT16 *)(mem + 0x406) = 0x2e8; // com4 port address
1.1.1.25  root     16660:        *(UINT16 *)(mem + 0x408) = 0x378; // lpt1 port address
1.1.1.37  root     16661:        *(UINT16 *)(mem + 0x40a) = 0x278; // lpt2 port address
                   16662:        *(UINT16 *)(mem + 0x40c) = 0x3bc; // lpt3 port address
1.1.1.32  root     16663: #ifdef EXT_BIOS_TOP
1.1.1.25  root     16664:        *(UINT16 *)(mem + 0x40e) = EXT_BIOS_TOP >> 4;
1.1.1.32  root     16665: #endif
1.1.1.26  root     16666:        *(UINT16 *)(mem + 0x410) = msdos_get_equipment();
1.1.1.33  root     16667:        *(UINT16 *)(mem + 0x413) = MEMORY_END >> 10;
1.1.1.41  root     16668:        *(UINT16 *)(mem + 0x41a) = 0x1e;
                   16669:        *(UINT16 *)(mem + 0x41c) = 0x1e;
1.1       root     16670:        *(UINT8  *)(mem + 0x449) = 0x03;//0x73;
1.1.1.14  root     16671:        *(UINT16 *)(mem + 0x44a) = csbi.dwSize.X;
                   16672:        *(UINT16 *)(mem + 0x44c) = regen;
1.1       root     16673:        *(UINT16 *)(mem + 0x44e) = 0;
                   16674:        *(UINT8  *)(mem + 0x450) = csbi.dwCursorPosition.X;
1.1.1.14  root     16675:        *(UINT8  *)(mem + 0x451) = csbi.dwCursorPosition.Y - scr_top;
1.1       root     16676:        *(UINT8  *)(mem + 0x460) = 7;
                   16677:        *(UINT8  *)(mem + 0x461) = 7;
                   16678:        *(UINT8  *)(mem + 0x462) = 0;
                   16679:        *(UINT16 *)(mem + 0x463) = 0x3d4;
1.1.1.19  root     16680:        *(UINT8  *)(mem + 0x465) = 0x09;
                   16681:        *(UINT32 *)(mem + 0x46c) = get_ticks_since_midnight(timeGetTime());
1.1.1.40  root     16682:        *(UINT16 *)(mem + 0x472) = 0x4321; // preserve memory in cpu reset
1.1.1.41  root     16683:        *(UINT16 *)(mem + 0x480) = 0x1e;
                   16684:        *(UINT16 *)(mem + 0x482) = 0x3e;
1.1.1.14  root     16685:        *(UINT8  *)(mem + 0x484) = csbi.srWindow.Bottom - csbi.srWindow.Top;
                   16686:        *(UINT8  *)(mem + 0x485) = cfi.dwFontSize.Y;
                   16687:        *(UINT8  *)(mem + 0x487) = 0x60;
                   16688:        *(UINT8  *)(mem + 0x496) = 0x10; // enhanced keyboard installed
1.1.1.32  root     16689: #ifdef EXT_BIOS_TOP
1.1.1.25  root     16690:        *(UINT16 *)(mem + EXT_BIOS_TOP) = 1;
1.1.1.32  root     16691: #endif
1.1.1.14  root     16692:        
                   16693:        // initial screen
                   16694:        SMALL_RECT rect;
                   16695:        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   16696:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   16697:        for(int y = 0, ofs1 = TEXT_VRAM_TOP, ofs2 = SHADOW_BUF_TOP; y < scr_height; y++) {
                   16698:                for(int x = 0; x < scr_width; x++) {
                   16699:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Char.AsciiChar;
                   16700:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Attributes;
                   16701:                }
                   16702:        }
1.1       root     16703:        
1.1.1.19  root     16704:        // init mcb
1.1       root     16705:        int seg = MEMORY_TOP >> 4;
1.1.1.19  root     16706:        
                   16707:        // iret table
                   16708:        // note: int 2eh vector should address the routine in command.com,
                   16709:        // and some softwares invite (int 2eh vector segment) - 1 must address the mcb of command.com.
                   16710:        // so move iret table into allocated memory block
                   16711:        // http://www5c.biglobe.ne.jp/~ecb/assembler2/2_6.html
1.1.1.33  root     16712:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, IRET_SIZE >> 4);
1.1.1.19  root     16713:        IRET_TOP = seg << 4;
                   16714:        seg += IRET_SIZE >> 4;
1.1.1.25  root     16715:        memset(mem + IRET_TOP, 0xcf, IRET_SIZE); // iret
1.1.1.19  root     16716:        
                   16717:        // dummy xms/ems device
1.1.1.33  root     16718:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, XMS_SIZE >> 4);
1.1.1.19  root     16719:        XMS_TOP = seg << 4;
                   16720:        seg += XMS_SIZE >> 4;
                   16721:        
                   16722:        // environment
1.1.1.33  root     16723:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, ENV_SIZE >> 4);
1.1       root     16724:        int env_seg = seg;
                   16725:        int ofs = 0;
1.1.1.32  root     16726:        char env_append[ENV_SIZE] = {0}, append_added = 0;
                   16727:        char comspec_added = 0;
1.1.1.33  root     16728:        char lastdrive_added = 0;
1.1.1.32  root     16729:        char env_msdos_path[ENV_SIZE] = {0};
                   16730:        char env_path[ENV_SIZE] = {0}, path_added = 0;
1.1.1.33  root     16731:        char prompt_added = 0;
1.1.1.32  root     16732:        char env_temp[ENV_SIZE] = {0}, temp_added = 0, tmp_added = 0;
1.1.1.33  root     16733:        char tz_added = 0;
1.1.1.45  root     16734:        const char *path, *short_path;
1.1.1.32  root     16735:        
                   16736:        if((path = getenv("MSDOS_APPEND")) != NULL) {
                   16737:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   16738:                        strcpy(env_append, short_path);
                   16739:                }
                   16740:        }
                   16741:        if((path = getenv("APPEND")) != NULL) {
                   16742:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   16743:                        if(env_append[0] != '\0') {
                   16744:                                strcat(env_append, ";");
                   16745:                        }
                   16746:                        strcat(env_append, short_path);
                   16747:                }
                   16748:        }
                   16749:        
                   16750:        if((path = msdos_search_command_com(argv[0], env_path)) != NULL) {
                   16751:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   16752:                        strcpy(comspec_path, short_path);
                   16753:                }
                   16754:        }
                   16755:        if((path = getenv("MSDOS_COMSPEC")) != NULL && _access(path, 0) == 0) {
                   16756:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   16757:                        strcpy(comspec_path, short_path);
                   16758:                }
                   16759:        }
1.1       root     16760:        
1.1.1.28  root     16761:        if((path = getenv("MSDOS_PATH")) != NULL) {
1.1.1.32  root     16762:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   16763:                        strcpy(env_msdos_path, short_path);
                   16764:                        strcpy(env_path, short_path);
1.1.1.14  root     16765:                }
                   16766:        }
1.1.1.28  root     16767:        if((path = getenv("PATH")) != NULL) {
1.1.1.32  root     16768:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   16769:                        if(env_path[0] != '\0') {
                   16770:                                strcat(env_path, ";");
                   16771:                        }
                   16772:                        strcat(env_path, short_path);
1.1.1.9   root     16773:                }
                   16774:        }
1.1.1.32  root     16775:        
                   16776:        if(GetTempPath(ENV_SIZE, env_temp) != 0) {
                   16777:                strcpy(env_temp, msdos_get_multiple_short_path(env_temp));
1.1.1.15  root     16778:        }
1.1.1.32  root     16779:        for(int i = 0; i < 4; i++) {
                   16780:                static const char *name[4] = {"MSDOS_TEMP", "MSDOS_TMP", "TEMP", "TMP"};
                   16781:                if((path = getenv(name[i])) != NULL && _access(path, 0) == 0) {
                   16782:                        if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   16783:                                strcpy(env_temp, short_path);
                   16784:                                break;
                   16785:                        }
                   16786:                }
1.1.1.24  root     16787:        }
1.1.1.32  root     16788:        
1.1.1.9   root     16789:        for(char **p = envp; p != NULL && *p != NULL; p++) {
1.1       root     16790:                // lower to upper
1.1.1.28  root     16791:                char tmp[ENV_SIZE], name[ENV_SIZE];
1.1       root     16792:                strcpy(tmp, *p);
                   16793:                for(int i = 0;; i++) {
                   16794:                        if(tmp[i] == '=') {
                   16795:                                tmp[i] = '\0';
                   16796:                                sprintf(name, ";%s;", tmp);
1.1.1.25  root     16797:                                my_strupr(name);
1.1       root     16798:                                tmp[i] = '=';
                   16799:                                break;
                   16800:                        } else if(tmp[i] >= 'a' && tmp[i] <= 'z') {
1.1.1.28  root     16801:                                tmp[i] = (tmp[i] - 'a') + 'A';
1.1       root     16802:                        }
                   16803:                }
1.1.1.33  root     16804:                if(strstr(";MSDOS_APPEND;MSDOS_COMSPEC;MSDOS_LASTDRIVE;MSDOS_TEMP;MSDOS_TMP;MSDOS_TZ;", name) != NULL) {
                   16805:                        // ignore MSDOS_(APPEND/COMSPEC/LASTDRIVE/TEMP/TMP/TZ)
                   16806:                } else if(standard_env && strstr(";APPEND;COMSPEC;LASTDRIVE;MSDOS_PATH;PATH;PROMPT;TEMP;TMP;TZ;", name) == NULL) {
1.1.1.18  root     16807:                        // ignore non standard environments
                   16808:                } else {
1.1.1.33  root     16809:                        if(strncmp(tmp, "APPEND=", 7) == 0) {
1.1.1.32  root     16810:                                if(env_append[0] != '\0') {
                   16811:                                        sprintf(tmp, "APPEND=%s", env_append);
                   16812:                                } else {
                   16813:                                        sprintf(tmp, "APPEND=%s", msdos_get_multiple_short_path(tmp + 7));
                   16814:                                }
                   16815:                                append_added = 1;
                   16816:                        } else if(strncmp(tmp, "COMSPEC=", 8) == 0) {
1.1.1.14  root     16817:                                strcpy(tmp, "COMSPEC=C:\\COMMAND.COM");
1.1.1.32  root     16818:                                comspec_added = 1;
1.1.1.33  root     16819:                        } else if(strncmp(tmp, "LASTDRIVE=", 10) == 0) {
                   16820:                                char *env = getenv("MSDOS_LASTDRIVE");
                   16821:                                if(env != NULL) {
                   16822:                                        sprintf(tmp, "LASTDRIVE=%s", env);
                   16823:                                }
                   16824:                                lastdrive_added = 1;
                   16825:                        } else if(strncmp(tmp, "MSDOS_PATH=", 11) == 0) {
1.1.1.28  root     16826:                                if(env_msdos_path[0] != '\0') {
                   16827:                                        sprintf(tmp, "MSDOS_PATH=%s", env_msdos_path);
                   16828:                                } else {
                   16829:                                        sprintf(tmp, "MSDOS_PATH=%s", msdos_get_multiple_short_path(tmp + 11));
                   16830:                                }
1.1.1.33  root     16831:                        } else if(strncmp(tmp, "PATH=", 5) == 0) {
1.1.1.28  root     16832:                                if(env_path[0] != '\0') {
                   16833:                                        sprintf(tmp, "PATH=%s", env_path);
                   16834:                                } else {
                   16835:                                        sprintf(tmp, "PATH=%s", msdos_get_multiple_short_path(tmp + 5));
                   16836:                                }
1.1.1.32  root     16837:                                path_added = 1;
1.1.1.33  root     16838:                        } else if(strncmp(tmp, "PROMPT=", 7) == 0) {
                   16839:                                prompt_added = 1;
1.1.1.28  root     16840:                        } else if(strncmp(tmp, "TEMP=", 5) == 0) {
                   16841:                                if(env_temp[0] != '\0') {
                   16842:                                        sprintf(tmp, "TEMP=%s", env_temp);
                   16843:                                } else {
                   16844:                                        sprintf(tmp, "TEMP=%s", msdos_get_multiple_short_path(tmp + 5));
                   16845:                                }
1.1.1.32  root     16846:                                temp_added = 1;
1.1.1.33  root     16847:                        } else if(strncmp(tmp, "TMP=", 4) == 0) {
1.1.1.28  root     16848:                                if(env_temp[0] != '\0') {
                   16849:                                        sprintf(tmp, "TMP=%s", env_temp);
                   16850:                                } else {
                   16851:                                        sprintf(tmp, "TMP=%s", msdos_get_multiple_short_path(tmp + 4));
1.1       root     16852:                                }
1.1.1.32  root     16853:                                tmp_added = 1;
1.1.1.33  root     16854:                        } else if(strncmp(tmp, "TZ=", 3) == 0) {
                   16855:                                char *env = getenv("MSDOS_TZ");
                   16856:                                if(env != NULL) {
                   16857:                                        sprintf(tmp, "TZ=%s", env);
                   16858:                                }
                   16859:                                tz_added = 1;
1.1       root     16860:                        }
                   16861:                        int len = strlen(tmp);
1.1.1.14  root     16862:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) {
1.1       root     16863:                                fatalerror("too many environments\n");
                   16864:                        }
                   16865:                        memcpy(mem + (seg << 4) + ofs, tmp, len);
                   16866:                        ofs += len + 1;
                   16867:                }
                   16868:        }
1.1.1.32  root     16869:        if(!append_added && env_append[0] != '\0') {
                   16870:                #define SET_ENV(name, value) { \
                   16871:                        char tmp[ENV_SIZE]; \
                   16872:                        sprintf(tmp, "%s=%s", name, value); \
                   16873:                        int len = strlen(tmp); \
                   16874:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) { \
                   16875:                                fatalerror("too many environments\n"); \
                   16876:                        } \
                   16877:                        memcpy(mem + (seg << 4) + ofs, tmp, len); \
                   16878:                        ofs += len + 1; \
                   16879:                }
                   16880:                SET_ENV("APPEND", env_append);
                   16881:        }
                   16882:        if(!comspec_added) {
                   16883:                SET_ENV("COMSPEC", "C:\\COMMAND.COM");
                   16884:        }
1.1.1.33  root     16885:        if(!lastdrive_added) {
                   16886:                SET_ENV("LASTDRIVE", "Z");
                   16887:        }
1.1.1.32  root     16888:        if(!path_added) {
                   16889:                SET_ENV("PATH", env_path);
                   16890:        }
1.1.1.33  root     16891:        if(!prompt_added) {
                   16892:                SET_ENV("PROMPT", "$P$G");
                   16893:        }
1.1.1.32  root     16894:        if(!temp_added) {
                   16895:                SET_ENV("TEMP", env_temp);
                   16896:        }
                   16897:        if(!tmp_added) {
                   16898:                SET_ENV("TMP", env_temp);
                   16899:        }
1.1.1.33  root     16900:        if(!tz_added) {
                   16901:                TIME_ZONE_INFORMATION tzi;
                   16902:                HKEY hKey, hSubKey;
                   16903:                char tzi_std_name[64];
                   16904:                char tz_std[8] = "GMT";
                   16905:                char tz_dlt[8] = "GST";
                   16906:                char tz_value[32];
                   16907:                
                   16908:                // timezone name from GetTimeZoneInformation may not be english
                   16909:                bool daylight = (GetTimeZoneInformation(&tzi) != TIME_ZONE_ID_UNKNOWN);
                   16910:                setlocale(LC_CTYPE, "");
                   16911:                wcstombs(tzi_std_name, tzi.StandardName, sizeof(tzi_std_name));
                   16912:                
                   16913:                // get english timezone name from registry
                   16914:                if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones", 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
                   16915:                        for(DWORD i = 0; !tz_added; i++) {
                   16916:                                char reg_name[256], sub_key[1024], std_name[256];
                   16917:                                DWORD size;
                   16918:                                FILETIME ftTime;
                   16919:                                LONG result = RegEnumKeyEx(hKey, i, reg_name, &(size = array_length(reg_name)), NULL, NULL, NULL, &ftTime);
                   16920:                                
                   16921:                                if(result == ERROR_SUCCESS) {
                   16922:                                        sprintf(sub_key, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\%s", reg_name);
                   16923:                                        if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, sub_key, 0, KEY_QUERY_VALUE, &hSubKey) == ERROR_SUCCESS) {
                   16924:                                                if(RegQueryValueEx(hSubKey, "Std", NULL, NULL, (LPBYTE)std_name, &(size = array_length(std_name))) == ERROR_SUCCESS) {
                   16925:                                                        // search english timezone name from table
1.1.1.37  root     16926:                                                        if(strcmp(std_name, tzi_std_name) == 0) {
1.1.1.33  root     16927:                                                                for(int j = 0; j < array_length(tz_table); j++) {
                   16928:                                                                        if(strcmp(reg_name, tz_table[j].name) == 0 && (tz_table[j].lcid == 0 || tz_table[j].lcid == GetUserDefaultLCID())) {
                   16929:                                                                                if(tz_table[j].std != NULL) {
                   16930:                                                                                        strcpy(tz_std, tz_table[j].std);
                   16931:                                                                                }
                   16932:                                                                                if(tz_table[j].dlt != NULL) {
                   16933:                                                                                        strcpy(tz_dlt, tz_table[j].dlt);
                   16934:                                                                                }
                   16935:                                                                                tz_added = 1;
                   16936:                                                                                break;
                   16937:                                                                        }
                   16938:                                                                }
                   16939:                                                        }
                   16940:                                                }
                   16941:                                                RegCloseKey(hSubKey);
                   16942:                                        }
                   16943:                                } else if(result == ERROR_NO_MORE_ITEMS) {
                   16944:                                        break;
                   16945:                                }
                   16946:                        }
                   16947:                        RegCloseKey(hKey);
                   16948:                }
                   16949:                if((tzi.Bias % 60) != 0) {
                   16950:                        sprintf(tz_value, "%s%d:%2d", tz_std, tzi.Bias / 60, abs(tzi.Bias % 60));
                   16951:                } else {
                   16952:                        sprintf(tz_value, "%s%d", tz_std, tzi.Bias / 60);
                   16953:                }
                   16954:                if(daylight) {
                   16955:                        strcat(tz_value, tz_dlt);
                   16956:                }
                   16957:                SET_ENV("TZ", tz_value);
                   16958:        }
1.1       root     16959:        seg += (ENV_SIZE >> 4);
                   16960:        
                   16961:        // psp
1.1.1.33  root     16962:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, PSP_SIZE >> 4);
1.1       root     16963:        current_psp = seg;
1.1.1.35  root     16964:        psp_t *psp = msdos_psp_create(seg, seg + (PSP_SIZE >> 4), -1, env_seg);
                   16965:        psp->parent_psp = current_psp;
1.1       root     16966:        seg += (PSP_SIZE >> 4);
                   16967:        
1.1.1.19  root     16968:        // first free mcb in conventional memory
1.1.1.33  root     16969:        msdos_mcb_create(seg, 'M', 0, (MEMORY_END >> 4) - seg - 2);
1.1.1.8   root     16970:        first_mcb = seg;
                   16971:        
1.1.1.19  root     16972:        // dummy mcb to link to umb
1.1.1.33  root     16973: #if 0
1.1.1.39  root     16974:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC"); // link umb
1.1.1.33  root     16975: #else
1.1.1.39  root     16976:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC"); // unlink umb
1.1.1.33  root     16977: #endif
1.1.1.19  root     16978:        
                   16979:        // first free mcb in upper memory block
1.1.1.8   root     16980:        msdos_mcb_create(UMB_TOP >> 4, 'Z', 0, (UMB_END >> 4) - (UMB_TOP >> 4) - 1);
1.1       root     16981:        
1.1.1.29  root     16982: #ifdef SUPPORT_HMA
                   16983:        // first free mcb in high memory area
                   16984:        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   16985: #endif
                   16986:        
1.1.1.26  root     16987:        // interrupt vector
                   16988:        for(int i = 0; i < 0x80; i++) {
                   16989:                *(UINT16 *)(mem + 4 * i + 0) = i;
                   16990:                *(UINT16 *)(mem + 4 * i + 2) = (IRET_TOP >> 4);
                   16991:        }
1.1.1.35  root     16992:        *(UINT16 *)(mem + 4 * 0x08 + 0) = 0x0018;       // fffd:0018 irq0 (system timer)
1.1.1.26  root     16993:        *(UINT16 *)(mem + 4 * 0x08 + 2) = 0xfffd;
                   16994:        *(UINT16 *)(mem + 4 * 0x22 + 0) = 0x0000;       // ffff:0000 boot
                   16995:        *(UINT16 *)(mem + 4 * 0x22 + 2) = 0xffff;
                   16996:        *(UINT16 *)(mem + 4 * 0x67 + 0) = 0x0012;       // xxxx:0012 ems
                   16997:        *(UINT16 *)(mem + 4 * 0x67 + 2) = XMS_TOP >> 4;
                   16998:        *(UINT16 *)(mem + 4 * 0x74 + 0) = 0x0000;       // fffd:0000 irq12 (mouse)
                   16999:        *(UINT16 *)(mem + 4 * 0x74 + 2) = 0xfffd;
                   17000:        
1.1.1.29  root     17001:        // dummy devices (NUL -> CON -> ... -> CONFIG$ -> EMMXXXX0)
1.1.1.26  root     17002:        static const struct {
                   17003:                UINT16 attributes;
                   17004:                char *dev_name;
                   17005:        } dummy_devices[] = {
                   17006:                {0x8013, "CON     "},
                   17007:                {0x8000, "AUX     "},
                   17008:                {0xa0c0, "PRN     "},
                   17009:                {0x8008, "CLOCK$  "},
                   17010:                {0x8000, "COM1    "},
                   17011:                {0xa0c0, "LPT1    "},
                   17012:                {0xa0c0, "LPT2    "},
                   17013:                {0xa0c0, "LPT3    "},
                   17014:                {0x8000, "COM2    "},
                   17015:                {0x8000, "COM3    "},
                   17016:                {0x8000, "COM4    "},
1.1.1.30  root     17017: //             {0xc000, "CONFIG$ "},
                   17018:                {0xc000, "$IBMADSP"}, // for windows3.1 setup.exe
1.1.1.26  root     17019:        };
                   17020:        static const UINT8 dummy_device_routine[] = {
                   17021:                // from NUL device of Windows 98 SE
                   17022:                // or word ptr ES:[BX+03],0100
                   17023:                0x26, 0x81, 0x4f, 0x03, 0x00, 0x01,
                   17024:                // retf
                   17025:                0xcb,
                   17026:        };
1.1.1.29  root     17027:        device_t *last = NULL;
1.1.1.32  root     17028:        for(int i = 0; i < array_length(dummy_devices); i++) {
1.1.1.26  root     17029:                device_t *device = (device_t *)(mem + DEVICE_TOP + 22 + 18 * i);
1.1.1.29  root     17030:                device->next_driver.w.l = 22 + 18 * (i + 1);
                   17031:                device->next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     17032:                device->attributes = dummy_devices[i].attributes;
1.1.1.29  root     17033:                device->strategy = DEVICE_SIZE - sizeof(dummy_device_routine);
                   17034:                device->interrupt = DEVICE_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.26  root     17035:                memcpy(device->dev_name, dummy_devices[i].dev_name, 8);
1.1.1.29  root     17036:                last = device;
                   17037:        }
                   17038:        if(last != NULL) {
                   17039:                last->next_driver.w.l = 0;
                   17040:                last->next_driver.w.h = XMS_TOP >> 4;
1.1.1.26  root     17041:        }
1.1.1.29  root     17042:        memcpy(mem + DEVICE_TOP + DEVICE_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.26  root     17043:        
1.1.1.25  root     17044:        // dos info
                   17045:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   17046:        dos_info->magic_word = 1;
                   17047:        dos_info->first_mcb = MEMORY_TOP >> 4;
                   17048:        dos_info->first_dpb.w.l = 0;
                   17049:        dos_info->first_dpb.w.h = DPB_TOP >> 4;
                   17050:        dos_info->first_sft.w.l = 0;
                   17051:        dos_info->first_sft.w.h = SFT_TOP >> 4;
1.1.1.41  root     17052:        dos_info->clock_device.w.l = 22 + 18 * 3;       // CLOCK$ is the 4th device in IO.SYS
1.1.1.25  root     17053:        dos_info->clock_device.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     17054:        dos_info->con_device.w.l = 22 + 18 * 0;         // CON is the 1st device in IO.SYS
1.1.1.25  root     17055:        dos_info->con_device.w.h = DEVICE_TOP >> 4;
                   17056:        dos_info->max_sector_len = 512;
                   17057:        dos_info->disk_buf_info.w.l = offsetof(dos_info_t, disk_buf_heads);
                   17058:        dos_info->disk_buf_info.w.h = DOS_INFO_TOP >> 4;
                   17059:        dos_info->cds.w.l = 0;
                   17060:        dos_info->cds.w.h = CDS_TOP >> 4;
                   17061:        dos_info->fcb_table.w.l = 0;
                   17062:        dos_info->fcb_table.w.h = FCB_TABLE_TOP >> 4;
                   17063:        dos_info->last_drive = 'Z' - 'A' + 1;
                   17064:        dos_info->buffers_x = 20;
                   17065:        dos_info->buffers_y = 0;
                   17066:        dos_info->boot_drive = 'C' - 'A' + 1;
1.1.1.29  root     17067:        dos_info->nul_device.next_driver.w.l = 22;
                   17068:        dos_info->nul_device.next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.25  root     17069:        dos_info->nul_device.attributes = 0x8004;
1.1.1.29  root     17070:        dos_info->nul_device.strategy = DOS_INFO_SIZE - sizeof(dummy_device_routine);
                   17071:        dos_info->nul_device.interrupt = DOS_INFO_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     17072:        memcpy(dos_info->nul_device.dev_name, "NUL     ", 8);
                   17073:        dos_info->disk_buf_heads.w.l = 0;
                   17074:        dos_info->disk_buf_heads.w.h = DISK_BUF_TOP >> 4;
1.1.1.39  root     17075:        dos_info->umb_linked = (((mcb_t *)(mem + MEMORY_END - 16))->mz == 'M' ? 0x01 : 0x00);
1.1.1.25  root     17076:        dos_info->first_umb_fcb = UMB_TOP >> 4;
                   17077:        dos_info->first_mcb_2 = MEMORY_TOP >> 4;
1.1.1.29  root     17078:        memcpy(mem + DOS_INFO_TOP + DOS_INFO_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.25  root     17079:        
                   17080:        char *env;
                   17081:        if((env = getenv("LASTDRIVE")) != NULL) {
                   17082:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   17083:                        dos_info->last_drive = env[0] - 'A' + 1;
                   17084:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   17085:                        dos_info->last_drive = env[0] - 'a' + 1;
                   17086:                }
                   17087:        }
                   17088:        if((env = getenv("windir")) != NULL) {
                   17089:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   17090:                        dos_info->boot_drive = env[0] - 'A' + 1;
                   17091:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   17092:                        dos_info->boot_drive = env[0] - 'a' + 1;
                   17093:                }
                   17094:        }
                   17095: #if defined(HAS_I386)
                   17096:        dos_info->i386_or_later = 1;
                   17097: #else
                   17098:        dos_info->i386_or_later = 0;
                   17099: #endif
                   17100:        dos_info->ext_mem_size = (min(MAX_MEM, 0x4000000) - 0x100000) >> 10;
                   17101:        
1.1.1.27  root     17102:        // ems (int 67h) and xms
1.1.1.25  root     17103:        device_t *xms_device = (device_t *)(mem + XMS_TOP);
                   17104:        xms_device->next_driver.w.l = 0xffff;
                   17105:        xms_device->next_driver.w.h = 0xffff;
                   17106:        xms_device->attributes = 0xc000;
1.1.1.29  root     17107:        xms_device->strategy = XMS_SIZE - sizeof(dummy_device_routine);
                   17108:        xms_device->interrupt = XMS_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     17109:        memcpy(xms_device->dev_name, "EMMXXXX0", 8);
                   17110:        
1.1.1.26  root     17111:        mem[XMS_TOP + 0x12] = 0xcd;     // int 68h (dummy)
                   17112:        mem[XMS_TOP + 0x13] = 0x68;
                   17113:        mem[XMS_TOP + 0x14] = 0xcf;     // iret
1.1.1.19  root     17114: #ifdef SUPPORT_XMS
                   17115:        if(support_xms) {
1.1.1.26  root     17116:                mem[XMS_TOP + 0x15] = 0xcd;     // int 69h (dummy)
                   17117:                mem[XMS_TOP + 0x16] = 0x69;
                   17118:                mem[XMS_TOP + 0x17] = 0xcb;     // retf
1.1.1.19  root     17119:        } else
                   17120: #endif
1.1.1.26  root     17121:        mem[XMS_TOP + 0x15] = 0xcb;     // retf
1.1.1.29  root     17122:        memcpy(mem + XMS_TOP + XMS_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.19  root     17123:        
1.1.1.26  root     17124:        // irq12 routine (mouse)
1.1.1.24  root     17125:        mem[0xfffd0 + 0x00] = 0xcd;     // int 6ah (dummy)
                   17126:        mem[0xfffd0 + 0x01] = 0x6a;
                   17127:        mem[0xfffd0 + 0x02] = 0x9a;     // call far mouse
                   17128:        mem[0xfffd0 + 0x03] = 0xff;
                   17129:        mem[0xfffd0 + 0x04] = 0xff;
                   17130:        mem[0xfffd0 + 0x05] = 0xff;
                   17131:        mem[0xfffd0 + 0x06] = 0xff;
                   17132:        mem[0xfffd0 + 0x07] = 0xcd;     // int 6bh (dummy)
                   17133:        mem[0xfffd0 + 0x08] = 0x6b;
                   17134:        mem[0xfffd0 + 0x09] = 0xcf;     // iret
                   17135:        
1.1.1.27  root     17136:        // case map routine
                   17137:        mem[0xfffd0 + 0x0a] = 0xcd;     // int 6ch (dummy)
                   17138:        mem[0xfffd0 + 0x0b] = 0x6c;
                   17139:        mem[0xfffd0 + 0x0c] = 0xcb;     // retf
                   17140:        
                   17141:        // font read routine
                   17142:        mem[0xfffd0 + 0x0d] = 0xcd;     // int 6dh (dummy)
                   17143:        mem[0xfffd0 + 0x0e] = 0x6d;
                   17144:        mem[0xfffd0 + 0x0f] = 0xcb;     // retf
1.1.1.19  root     17145:        
1.1.1.32  root     17146:        // error message read routine
                   17147:        mem[0xfffd0 + 0x10] = 0xcd;     // int 6eh (dummy)
                   17148:        mem[0xfffd0 + 0x11] = 0x6e;
                   17149:        mem[0xfffd0 + 0x12] = 0xcb;     // retf
                   17150:        
1.1.1.35  root     17151:        // dummy loop to wait BIOS/DOS service is done
                   17152:        mem[0xfffd0 + 0x13] = 0xe6;     // out f7h, al
                   17153:        mem[0xfffd0 + 0x14] = 0xf7;
                   17154:        mem[0xfffd0 + 0x15] = 0x78;     // js/jns -4
                   17155:        mem[0xfffd0 + 0x16] = 0xfc;
                   17156:        mem[0xfffd0 + 0x17] = 0xcb;     // retf
                   17157:        
1.1.1.26  root     17158:        // irq0 routine (system time)
1.1.1.35  root     17159:        mem[0xfffd0 + 0x18] = 0xcd;     // int 1ch
                   17160:        mem[0xfffd0 + 0x19] = 0x1c;
                   17161:        mem[0xfffd0 + 0x1a] = 0xea;     // jmp far (IRET_TOP >> 4):0008
                   17162:        mem[0xfffd0 + 0x1b] = 0x08;
                   17163:        mem[0xfffd0 + 0x1c] = 0x00;
                   17164:        mem[0xfffd0 + 0x1d] = ((IRET_TOP >> 4)     ) & 0xff;
                   17165:        mem[0xfffd0 + 0x1e] = ((IRET_TOP >> 4) >> 8) & 0xff;
1.1.1.14  root     17166:        
1.1.1.26  root     17167:        // boot routine
1.1       root     17168:        mem[0xffff0] = 0xf4;    // halt
                   17169:        
1.1.1.24  root     17170:        mem[0xffff5] = '0';     // rom date
                   17171:        mem[0xffff6] = '2';
                   17172:        mem[0xffff7] = '/';
                   17173:        mem[0xffff8] = '2';
                   17174:        mem[0xffff9] = '2';
                   17175:        mem[0xffffa] = '/';
                   17176:        mem[0xffffb] = '0';
                   17177:        mem[0xffffc] = '6';
                   17178:        mem[0xffffe] = 0xfc;    // machine id
                   17179:        mem[0xfffff] = 0x00;
                   17180:        
1.1       root     17181:        // param block
                   17182:        // + 0: param block (22bytes)
                   17183:        // +24: fcb1/2 (20bytes)
                   17184:        // +44: command tail (128bytes)
                   17185:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   17186:        param->env_seg = 0;
                   17187:        param->cmd_line.w.l = 44;
                   17188:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   17189:        param->fcb1.w.l = 24;
                   17190:        param->fcb1.w.h = (WORK_TOP >> 4);
                   17191:        param->fcb2.w.l = 24;
                   17192:        param->fcb2.w.h = (WORK_TOP >> 4);
                   17193:        
                   17194:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   17195:        
                   17196:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   17197:        if(argc > 1) {
                   17198:                sprintf(cmd_line->cmd, " %s", argv[1]);
                   17199:                for(int i = 2; i < argc; i++) {
                   17200:                        char tmp[128];
                   17201:                        sprintf(tmp, "%s %s", cmd_line->cmd, argv[i]);
                   17202:                        strcpy(cmd_line->cmd, tmp);
                   17203:                }
                   17204:                cmd_line->len = (UINT8)strlen(cmd_line->cmd);
                   17205:        } else {
                   17206:                cmd_line->len = 0;
                   17207:        }
                   17208:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   17209:        
                   17210:        // system file table
1.1.1.21  root     17211:        *(UINT32 *)(mem + SFT_TOP + 0) = 0xffffffff;
                   17212:        *(UINT16 *)(mem + SFT_TOP + 4) = 20;
1.1       root     17213:        
1.1.1.19  root     17214:        // disk buffer header (from DOSBox)
                   17215:        *(UINT16 *)(mem + DISK_BUF_TOP +  0) = 0xffff;          // forward ptr
                   17216:        *(UINT16 *)(mem + DISK_BUF_TOP +  2) = 0xffff;          // backward ptr
                   17217:        *(UINT8  *)(mem + DISK_BUF_TOP +  4) = 0xff;            // not in use
                   17218:        *(UINT8  *)(mem + DISK_BUF_TOP + 10) = 0x01;            // number of FATs
                   17219:        *(UINT32 *)(mem + DISK_BUF_TOP + 13) = 0xffffffff;      // pointer to DPB
                   17220:        
1.1       root     17221:        // fcb table
                   17222:        *(UINT32 *)(mem + FCB_TABLE_TOP + 0) = 0xffffffff;
1.1.1.14  root     17223:        *(UINT16 *)(mem + FCB_TABLE_TOP + 4) = 0;
1.1       root     17224:        
1.1.1.41  root     17225:        // drive parameter block
1.1.1.42  root     17226:        for(int i = 0; i < 2; i++) {
1.1.1.43  root     17227:                // may be a floppy drive
1.1.1.44  root     17228:                cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * i);
                   17229:                sprintf(cds->path_name, "%c:\\", 'A' + i);
                   17230:                cds->drive_attrib = 0x4000;     // physical drive
                   17231:                cds->dpb_ptr.w.l = sizeof(dpb_t) * i;
                   17232:                cds->dpb_ptr.w.h = DPB_TOP >> 4;
                   17233:                cds->word_1 = cds->word_2 = cds->word_3 = 0xffff;
                   17234:                cds->bs_offset = 2;
                   17235:                
1.1.1.41  root     17236:                dpb_t *dpb = (dpb_t *)(mem + DPB_TOP + sizeof(dpb_t) * i);
                   17237:                dpb->drive_num = i;
                   17238:                dpb->unit_num = i;
1.1.1.43  root     17239:                dpb->next_dpb_ofs = /*(i == 25) ? 0xffff : */sizeof(dpb_t) * (i + 1);
                   17240:                dpb->next_dpb_seg = /*(i == 25) ? 0xffff : */DPB_TOP >> 4;
1.1.1.42  root     17241:        }
                   17242:        for(int i = 2; i < 26; i++) {
1.1.1.44  root     17243:                msdos_cds_update(i);
1.1.1.42  root     17244:                UINT16 seg, ofs;
                   17245:                msdos_drive_param_block_update(i, &seg, &ofs, 1);
1.1.1.41  root     17246:        }
                   17247:        
1.1.1.17  root     17248:        // nls stuff
                   17249:        msdos_nls_tables_init();
1.1       root     17250:        
                   17251:        // execute command
1.1.1.28  root     17252:        try {
                   17253:                if(msdos_process_exec(argv[0], param, 0)) {
                   17254:                        fatalerror("'%s' not found\n", argv[0]);
                   17255:                }
                   17256:        } catch(...) {
                   17257:                // we should not reach here :-(
                   17258:                fatalerror("failed to start '%s' because of unexpected exeption\n", argv[0]);
1.1       root     17259:        }
                   17260:        retval = 0;
                   17261:        return(0);
                   17262: }
                   17263: 
                   17264: #define remove_std_file(path) { \
                   17265:        int fd = _open(path, _O_RDONLY | _O_BINARY); \
                   17266:        if(fd != -1) { \
                   17267:                _lseek(fd, 0, SEEK_END); \
                   17268:                int size = _tell(fd); \
                   17269:                _close(fd); \
                   17270:                if(size == 0) { \
                   17271:                        remove(path); \
                   17272:                } \
                   17273:        } \
                   17274: }
                   17275: 
                   17276: void msdos_finish()
                   17277: {
                   17278:        for(int i = 0; i < MAX_FILES; i++) {
                   17279:                if(file_handler[i].valid) {
                   17280:                        _close(i);
                   17281:                }
                   17282:        }
1.1.1.21  root     17283: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     17284:        remove_std_file("stdaux.txt");
1.1.1.21  root     17285: #endif
1.1.1.30  root     17286: #ifdef SUPPORT_XMS
                   17287:        msdos_xms_finish();
                   17288: #endif
1.1       root     17289:        msdos_dbcs_table_finish();
                   17290: }
                   17291: 
                   17292: /* ----------------------------------------------------------------------------
                   17293:        PC/AT hardware emulation
                   17294: ---------------------------------------------------------------------------- */
                   17295: 
                   17296: void hardware_init()
                   17297: {
1.1.1.3   root     17298:        CPU_INIT_CALL(CPU_MODEL);
1.1       root     17299:        CPU_RESET_CALL(CPU_MODEL);
1.1.1.14  root     17300:        m_IF = 1;
1.1.1.3   root     17301: #if defined(HAS_I386)
1.1       root     17302:        cpu_type = (REG32(EDX) >> 8) & 0x0f;
                   17303:        cpu_step = (REG32(EDX) >> 0) & 0x0f;
1.1.1.3   root     17304: #endif
                   17305:        i386_set_a20_line(0);
1.1.1.14  root     17306:        
1.1.1.19  root     17307:        ems_init();
1.1.1.25  root     17308:        dma_init();
1.1       root     17309:        pic_init();
1.1.1.25  root     17310:        pio_init();
1.1.1.8   root     17311: #ifdef PIT_ALWAYS_RUNNING
                   17312:        pit_init();
                   17313: #else
1.1       root     17314:        pit_active = 0;
                   17315: #endif
1.1.1.25  root     17316:        sio_init();
1.1.1.8   root     17317:        cmos_init();
                   17318:        kbd_init();
1.1       root     17319: }
                   17320: 
1.1.1.10  root     17321: void hardware_finish()
                   17322: {
                   17323: #if defined(HAS_I386)
                   17324:        vtlb_free(m_vtlb);
                   17325: #endif
1.1.1.19  root     17326:        ems_finish();
1.1.1.37  root     17327:        pio_finish();
1.1.1.25  root     17328:        sio_finish();
1.1.1.10  root     17329: }
                   17330: 
1.1.1.28  root     17331: void hardware_release()
                   17332: {
                   17333:        // release hardware resources when this program will be terminated abnormally
                   17334: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     17335:        if(fp_debug_log != NULL) {
                   17336:                fclose(fp_debug_log);
                   17337:                fp_debug_log = NULL;
1.1.1.28  root     17338:        }
                   17339: #endif
                   17340: #if defined(HAS_I386)
                   17341:        vtlb_free(m_vtlb);
                   17342: #endif
                   17343:        ems_release();
1.1.1.37  root     17344:        pio_release();
1.1.1.28  root     17345:        sio_release();
                   17346: }
                   17347: 
1.1       root     17348: void hardware_run()
                   17349: {
1.1.1.22  root     17350: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.28  root     17351:        // open debug log file after msdos_init() is done not to use the standard file handlers
1.1.1.33  root     17352:        fp_debug_log = fopen("debug.log", "w");
1.1.1.22  root     17353: #endif
1.1.1.3   root     17354:        while(!m_halted) {
                   17355: #if defined(HAS_I386)
1.1       root     17356:                CPU_EXECUTE_CALL(i386);
1.1.1.14  root     17357:                if(m_eip != m_prev_eip) {
1.1.1.35  root     17358:                        idle_ops++;
                   17359:                }
1.1.1.14  root     17360: #else
1.1.1.35  root     17361:                CPU_EXECUTE_CALL(CPU_MODEL);
1.1.1.14  root     17362:                if(m_pc != m_prevpc) {
1.1.1.35  root     17363:                        idle_ops++;
1.1.1.14  root     17364:                }
1.1.1.35  root     17365: #endif
                   17366:                if(++update_ops == UPDATE_OPS) {
1.1       root     17367:                        hardware_update();
1.1.1.35  root     17368:                        update_ops = 0;
1.1       root     17369:                }
                   17370:        }
1.1.1.22  root     17371: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     17372:        if(fp_debug_log != NULL) {
                   17373:                fclose(fp_debug_log);
                   17374:                fp_debug_log = NULL;
1.1.1.28  root     17375:        }
1.1.1.22  root     17376: #endif
1.1       root     17377: }
                   17378: 
                   17379: void hardware_update()
                   17380: {
1.1.1.8   root     17381:        static UINT32 prev_time = 0;
                   17382:        UINT32 cur_time = timeGetTime();
                   17383:        
                   17384:        if(prev_time != cur_time) {
                   17385:                // update pit and raise irq0
                   17386: #ifndef PIT_ALWAYS_RUNNING
                   17387:                if(pit_active)
                   17388: #endif
                   17389:                {
                   17390:                        if(pit_run(0, cur_time)) {
                   17391:                                pic_req(0, 0, 1);
                   17392:                        }
                   17393:                        pit_run(1, cur_time);
                   17394:                        pit_run(2, cur_time);
                   17395:                }
1.1.1.24  root     17396:                
1.1.1.25  root     17397:                // update sio and raise irq4/3
1.1.1.29  root     17398:                for(int c = 0; c < 4; c++) {
1.1.1.25  root     17399:                        sio_update(c);
                   17400:                }
                   17401:                
1.1.1.24  root     17402:                // update keyboard and mouse
1.1.1.14  root     17403:                static UINT32 prev_tick = 0;
                   17404:                UINT32 cur_tick = cur_time / 32;
1.1.1.25  root     17405:                
1.1.1.14  root     17406:                if(prev_tick != cur_tick) {
                   17407:                        // update keyboard flags
                   17408:                        UINT8 state;
1.1.1.24  root     17409:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x0001) ? 0x80 : 0;
                   17410:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x0001) ? 0x40 : 0;
                   17411:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x0001) ? 0x20 : 0;
                   17412:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x0001) ? 0x10 : 0;
                   17413:                        state |= (GetAsyncKeyState(VK_MENU    ) & 0x8000) ? 0x08 : 0;
                   17414:                        state |= (GetAsyncKeyState(VK_CONTROL ) & 0x8000) ? 0x04 : 0;
                   17415:                        state |= (GetAsyncKeyState(VK_LSHIFT  ) & 0x8000) ? 0x02 : 0;
                   17416:                        state |= (GetAsyncKeyState(VK_RSHIFT  ) & 0x8000) ? 0x01 : 0;
1.1.1.14  root     17417:                        mem[0x417] = state;
                   17418:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x8000) ? 0x80 : 0;
                   17419:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   17420:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   17421:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
1.1.1.24  root     17422: //                     state |= (GetAsyncKeyState(VK_PAUSE   ) & 0x0001) ? 0x08 : 0;
                   17423: //                     state |= (GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x04 : 0;
1.1.1.14  root     17424:                        state |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   17425:                        state |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   17426:                        mem[0x418] = state;
                   17427:                        
1.1.1.24  root     17428:                        // update console input if needed
1.1.1.34  root     17429:                        if(!key_changed || mouse.hidden == 0) {
1.1.1.24  root     17430:                                update_console_input();
                   17431:                        }
                   17432:                        
                   17433:                        // raise irq1 if key is pressed/released
                   17434:                        if(key_changed) {
1.1.1.8   root     17435:                                pic_req(0, 1, 1);
1.1.1.24  root     17436:                                key_changed = false;
                   17437:                        }
                   17438:                        
                   17439:                        // raise irq12 if mouse status is changed
1.1.1.43  root     17440:                        if((mouse.status & mouse.call_mask) && mouse.call_addr.dw) {
                   17441:                                mouse.status_irq = mouse.status & mouse.call_mask;
                   17442:                                mouse.status_irq_alt = 0; // ???
1.1.1.24  root     17443:                                mouse.status &= ~mouse.call_mask;
1.1.1.43  root     17444:                                pic_req(1, 4, 1);
                   17445:                        } else {
                   17446:                                for(int i = 0; i < 8; i++) {
                   17447:                                        if((mouse.status_alt & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   17448:                                                mouse.status_irq = 0; // ???
                   17449:                                                mouse.status_irq_alt = 0;
                   17450:                                                for(int j = 0; j < 8; j++) {
                   17451:                                                        if((mouse.status_alt & (1 << j)) && mouse.call_addr_alt[i].dw == mouse.call_addr_alt[j].dw) {
                   17452:                                                                mouse.status_irq_alt |=  (1 << j);
                   17453:                                                                mouse.status_alt     &= ~(1 << j);
                   17454:                                                        }
                   17455:                                                }
                   17456:                                                pic_req(1, 4, 1);
                   17457:                                                break;
                   17458:                                        }
                   17459:                                }
1.1.1.8   root     17460:                        }
1.1.1.24  root     17461:                        
1.1.1.14  root     17462:                        prev_tick = cur_tick;
1.1.1.8   root     17463:                }
1.1.1.24  root     17464:                
1.1.1.19  root     17465:                // update daily timer counter
                   17466:                pcbios_update_daily_timer_counter(cur_time);
1.1.1.25  root     17467:                
1.1.1.8   root     17468:                prev_time = cur_time;
1.1       root     17469:        }
                   17470: }
                   17471: 
1.1.1.19  root     17472: // ems
                   17473: 
                   17474: void ems_init()
                   17475: {
                   17476:        memset(ems_handles, 0, sizeof(ems_handles));
                   17477:        memset(ems_pages, 0, sizeof(ems_pages));
                   17478:        free_ems_pages = MAX_EMS_PAGES;
                   17479: }
                   17480: 
                   17481: void ems_finish()
                   17482: {
1.1.1.28  root     17483:        ems_release();
                   17484: }
                   17485: 
                   17486: void ems_release()
                   17487: {
1.1.1.31  root     17488:        for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.28  root     17489:                if(ems_handles[i].buffer != NULL) {
1.1.1.19  root     17490:                        free(ems_handles[i].buffer);
                   17491:                        ems_handles[i].buffer = NULL;
                   17492:                }
                   17493:        }
                   17494: }
                   17495: 
                   17496: void ems_allocate_pages(int handle, int pages)
                   17497: {
                   17498:        if(pages > 0) {
                   17499:                ems_handles[handle].buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   17500:        } else {
                   17501:                ems_handles[handle].buffer = NULL;
                   17502:        }
                   17503:        ems_handles[handle].pages = pages;
                   17504:        ems_handles[handle].allocated = true;
                   17505:        free_ems_pages -= pages;
                   17506: }
                   17507: 
                   17508: void ems_reallocate_pages(int handle, int pages)
                   17509: {
                   17510:        if(ems_handles[handle].allocated) {
                   17511:                if(ems_handles[handle].pages != pages) {
                   17512:                        UINT8 *new_buffer = NULL;
                   17513:                        
                   17514:                        if(pages > 0) {
                   17515:                                new_buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   17516:                        }
1.1.1.32  root     17517:                        if(ems_handles[handle].buffer != NULL) {
                   17518:                                if(new_buffer != NULL) {
1.1.1.19  root     17519:                                        if(pages > ems_handles[handle].pages) {
                   17520:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * ems_handles[handle].pages);
                   17521:                                        } else {
                   17522:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * pages);
                   17523:                                        }
                   17524:                                }
                   17525:                                free(ems_handles[handle].buffer);
                   17526:                                ems_handles[handle].buffer = NULL;
                   17527:                        }
                   17528:                        free_ems_pages += ems_handles[handle].pages;
                   17529:                        
                   17530:                        ems_handles[handle].buffer = new_buffer;
                   17531:                        ems_handles[handle].pages = pages;
                   17532:                        free_ems_pages -= pages;
                   17533:                }
                   17534:        } else {
                   17535:                ems_allocate_pages(handle, pages);
                   17536:        }
                   17537: }
                   17538: 
                   17539: void ems_release_pages(int handle)
                   17540: {
                   17541:        if(ems_handles[handle].allocated) {
1.1.1.32  root     17542:                if(ems_handles[handle].buffer != NULL) {
1.1.1.19  root     17543:                        free(ems_handles[handle].buffer);
                   17544:                        ems_handles[handle].buffer = NULL;
                   17545:                }
                   17546:                free_ems_pages += ems_handles[handle].pages;
                   17547:                ems_handles[handle].allocated = false;
                   17548:        }
                   17549: }
                   17550: 
                   17551: void ems_map_page(int physical, int handle, int logical)
                   17552: {
                   17553:        if(ems_pages[physical].mapped) {
                   17554:                if(ems_pages[physical].handle == handle && ems_pages[physical].page == logical) {
                   17555:                        return;
                   17556:                }
                   17557:                ems_unmap_page(physical);
                   17558:        }
1.1.1.32  root     17559:        if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     17560:                memcpy(mem + EMS_TOP + 0x4000 * physical, ems_handles[handle].buffer + 0x4000 * logical, 0x4000);
                   17561:        }
                   17562:        ems_pages[physical].handle = handle;
                   17563:        ems_pages[physical].page = logical;
                   17564:        ems_pages[physical].mapped = true;
                   17565: }
                   17566: 
                   17567: void ems_unmap_page(int physical)
                   17568: {
                   17569:        if(ems_pages[physical].mapped) {
                   17570:                int handle = ems_pages[physical].handle;
                   17571:                int logical = ems_pages[physical].page;
                   17572:                
1.1.1.32  root     17573:                if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     17574:                        memcpy(ems_handles[handle].buffer + 0x4000 * logical, mem + EMS_TOP + 0x4000 * physical, 0x4000);
                   17575:                }
                   17576:                ems_pages[physical].mapped = false;
                   17577:        }
                   17578: }
                   17579: 
1.1.1.25  root     17580: // dma
1.1       root     17581: 
1.1.1.25  root     17582: void dma_init()
1.1       root     17583: {
1.1.1.26  root     17584:        memset(dma, 0, sizeof(dma));
1.1.1.25  root     17585:        for(int c = 0; c < 2; c++) {
1.1.1.26  root     17586: //             for(int ch = 0; ch < 4; ch++) {
                   17587: //                     dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w = 0xffff;
                   17588: //             }
1.1.1.25  root     17589:                dma_reset(c);
                   17590:        }
1.1       root     17591: }
                   17592: 
1.1.1.25  root     17593: void dma_reset(int c)
1.1       root     17594: {
1.1.1.25  root     17595:        dma[c].low_high = false;
                   17596:        dma[c].cmd = dma[c].req = dma[c].tc = 0;
                   17597:        dma[c].mask = 0xff;
                   17598: }
                   17599: 
                   17600: void dma_write(int c, UINT32 addr, UINT8 data)
                   17601: {
                   17602:        int ch = (addr >> 1) & 3;
                   17603:        UINT8 bit = 1 << (data & 3);
                   17604:        
                   17605:        switch(addr & 0x0f) {
                   17606:        case 0x00: case 0x02: case 0x04: case 0x06:
                   17607:                if(dma[c].low_high) {
                   17608:                        dma[c].ch[ch].bareg.b.h = data;
1.1       root     17609:                } else {
1.1.1.25  root     17610:                        dma[c].ch[ch].bareg.b.l = data;
                   17611:                }
                   17612:                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   17613:                dma[c].low_high = !dma[c].low_high;
                   17614:                break;
                   17615:        case 0x01: case 0x03: case 0x05: case 0x07:
                   17616:                if(dma[c].low_high) {
                   17617:                        dma[c].ch[ch].bcreg.b.h = data;
                   17618:                } else {
                   17619:                        dma[c].ch[ch].bcreg.b.l = data;
                   17620:                }
                   17621:                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   17622:                dma[c].low_high = !dma[c].low_high;
                   17623:                break;
                   17624:        case 0x08:
                   17625:                // command register
                   17626:                dma[c].cmd = data;
                   17627:                break;
                   17628:        case 0x09:
                   17629:                // dma[c].request register
                   17630:                if(data & 4) {
                   17631:                        if(!(dma[c].req & bit)) {
                   17632:                                dma[c].req |= bit;
                   17633: //                             dma_run(c, ch);
                   17634:                        }
                   17635:                } else {
                   17636:                        dma[c].req &= ~bit;
                   17637:                }
                   17638:                break;
                   17639:        case 0x0a:
                   17640:                // single mask register
                   17641:                if(data & 4) {
                   17642:                        dma[c].mask |= bit;
                   17643:                } else {
                   17644:                        dma[c].mask &= ~bit;
                   17645:                }
                   17646:                break;
                   17647:        case 0x0b:
                   17648:                // mode register
                   17649:                dma[c].ch[data & 3].mode = data;
                   17650:                break;
                   17651:        case 0x0c:
                   17652:                dma[c].low_high = false;
                   17653:                break;
                   17654:        case 0x0d:
                   17655:                // clear master
                   17656:                dma_reset(c);
                   17657:                break;
                   17658:        case 0x0e:
                   17659:                // clear mask register
                   17660:                dma[c].mask = 0;
                   17661:                break;
                   17662:        case 0x0f:
                   17663:                // all mask register
                   17664:                dma[c].mask = data & 0x0f;
                   17665:                break;
                   17666:        }
                   17667: }
                   17668: 
                   17669: UINT8 dma_read(int c, UINT32 addr)
                   17670: {
                   17671:        int ch = (addr >> 1) & 3;
                   17672:        UINT8 val = 0xff;
                   17673:        
                   17674:        switch(addr & 0x0f) {
                   17675:        case 0x00: case 0x02: case 0x04: case 0x06:
                   17676:                if(dma[c].low_high) {
                   17677:                        val = dma[c].ch[ch].areg.b.h;
                   17678:                } else {
                   17679:                        val = dma[c].ch[ch].areg.b.l;
                   17680:                }
                   17681:                dma[c].low_high = !dma[c].low_high;
                   17682:                return(val);
                   17683:        case 0x01: case 0x03: case 0x05: case 0x07:
                   17684:                if(dma[c].low_high) {
                   17685:                        val = dma[c].ch[ch].creg.b.h;
                   17686:                } else {
                   17687:                        val = dma[c].ch[ch].creg.b.l;
                   17688:                }
                   17689:                dma[c].low_high = !dma[c].low_high;
                   17690:                return(val);
                   17691:        case 0x08:
                   17692:                // status register
                   17693:                val = (dma[c].req << 4) | dma[c].tc;
                   17694:                dma[c].tc = 0;
                   17695:                return(val);
                   17696:        case 0x0d:
1.1.1.26  root     17697:                // temporary register (intel 82374 does not support)
1.1.1.25  root     17698:                return(dma[c].tmp & 0xff);
1.1.1.26  root     17699:        case 0x0f:
                   17700:                // mask register (intel 82374 does support)
                   17701:                return(dma[c].mask);
1.1.1.25  root     17702:        }
                   17703:        return(0xff);
                   17704: }
                   17705: 
                   17706: void dma_page_write(int c, int ch, UINT8 data)
                   17707: {
                   17708:        dma[c].ch[ch].pagereg = data;
                   17709: }
                   17710: 
                   17711: UINT8 dma_page_read(int c, int ch)
                   17712: {
                   17713:        return(dma[c].ch[ch].pagereg);
                   17714: }
                   17715: 
                   17716: void dma_run(int c, int ch)
                   17717: {
                   17718:        UINT8 bit = 1 << ch;
                   17719:        
                   17720:        if((dma[c].req & bit) && !(dma[c].mask & bit)) {
                   17721:                // execute dma
                   17722:                while(dma[c].req & bit) {
                   17723:                        if(ch == 0 && (dma[c].cmd & 0x01)) {
                   17724:                                // memory -> memory
                   17725:                                UINT32 saddr = dma[c].ch[0].areg.w | (dma[c].ch[0].pagereg << 16);
                   17726:                                UINT32 daddr = dma[c].ch[1].areg.w | (dma[c].ch[1].pagereg << 16);
                   17727:                                
                   17728:                                if(c == 0) {
                   17729:                                        dma[c].tmp = read_byte(saddr);
                   17730:                                        write_byte(daddr, dma[c].tmp);
                   17731:                                } else {
                   17732:                                        dma[c].tmp = read_word(saddr << 1);
                   17733:                                        write_word(daddr << 1, dma[c].tmp);
                   17734:                                }
                   17735:                                if(!(dma[c].cmd & 0x02)) {
                   17736:                                        if(dma[c].ch[0].mode & 0x20) {
                   17737:                                                dma[c].ch[0].areg.w--;
                   17738:                                                if(dma[c].ch[0].areg.w == 0xffff) {
                   17739:                                                        dma[c].ch[0].pagereg--;
                   17740:                                                }
                   17741:                                        } else {
                   17742:                                                dma[c].ch[0].areg.w++;
                   17743:                                                if(dma[c].ch[0].areg.w == 0) {
                   17744:                                                        dma[c].ch[0].pagereg++;
                   17745:                                                }
                   17746:                                        }
                   17747:                                }
                   17748:                                if(dma[c].ch[1].mode & 0x20) {
                   17749:                                        dma[c].ch[1].areg.w--;
                   17750:                                        if(dma[c].ch[1].areg.w == 0xffff) {
                   17751:                                                dma[c].ch[1].pagereg--;
                   17752:                                        }
                   17753:                                } else {
                   17754:                                        dma[c].ch[1].areg.w++;
                   17755:                                        if(dma[c].ch[1].areg.w == 0) {
                   17756:                                                dma[c].ch[1].pagereg++;
                   17757:                                        }
                   17758:                                }
                   17759:                                
                   17760:                                // check dma condition
                   17761:                                if(dma[c].ch[0].creg.w-- == 0) {
                   17762:                                        if(dma[c].ch[0].mode & 0x10) {
                   17763:                                                // self initialize
                   17764:                                                dma[c].ch[0].areg.w = dma[c].ch[0].bareg.w;
                   17765:                                                dma[c].ch[0].creg.w = dma[c].ch[0].bcreg.w;
                   17766:                                        } else {
                   17767: //                                             dma[c].mask |= bit;
                   17768:                                        }
                   17769:                                }
                   17770:                                if(dma[c].ch[1].creg.w-- == 0) {
                   17771:                                        // terminal count
                   17772:                                        if(dma[c].ch[1].mode & 0x10) {
                   17773:                                                // self initialize
                   17774:                                                dma[c].ch[1].areg.w = dma[c].ch[1].bareg.w;
                   17775:                                                dma[c].ch[1].creg.w = dma[c].ch[1].bcreg.w;
                   17776:                                        } else {
                   17777:                                                dma[c].mask |= bit;
                   17778:                                        }
                   17779:                                        dma[c].req &= ~bit;
                   17780:                                        dma[c].tc |= bit;
                   17781:                                }
                   17782:                        } else {
                   17783:                                UINT32 addr = dma[c].ch[ch].areg.w | (dma[c].ch[ch].pagereg << 16);
                   17784:                                
                   17785:                                if((dma[c].ch[ch].mode & 0x0c) == 0x00) {
                   17786:                                        // verify
                   17787:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x04) {
                   17788:                                        // io -> memory
                   17789:                                        if(c == 0) {
                   17790:                                                dma[c].tmp = read_io_byte(dma[c].ch[ch].port);
                   17791:                                                write_byte(addr, dma[c].tmp);
                   17792:                                        } else {
                   17793:                                                dma[c].tmp = read_io_word(dma[c].ch[ch].port);
                   17794:                                                write_word(addr << 1, dma[c].tmp);
                   17795:                                        }
                   17796:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x08) {
                   17797:                                        // memory -> io
                   17798:                                        if(c == 0) {
                   17799:                                                dma[c].tmp = read_byte(addr);
                   17800:                                                write_io_byte(dma[c].ch[ch].port, dma[c].tmp);
                   17801:                                        } else {
                   17802:                                                dma[c].tmp = read_word(addr << 1);
                   17803:                                                write_io_word(dma[c].ch[ch].port, dma[c].tmp);
                   17804:                                        }
                   17805:                                }
                   17806:                                if(dma[c].ch[ch].mode & 0x20) {
                   17807:                                        dma[c].ch[ch].areg.w--;
                   17808:                                        if(dma[c].ch[ch].areg.w == 0xffff) {
                   17809:                                                dma[c].ch[ch].pagereg--;
                   17810:                                        }
                   17811:                                } else {
                   17812:                                        dma[c].ch[ch].areg.w++;
                   17813:                                        if(dma[c].ch[ch].areg.w == 0) {
                   17814:                                                dma[c].ch[ch].pagereg++;
                   17815:                                        }
                   17816:                                }
                   17817:                                
                   17818:                                // check dma condition
                   17819:                                if(dma[c].ch[ch].creg.w-- == 0) {
                   17820:                                        // terminal count
                   17821:                                        if(dma[c].ch[ch].mode & 0x10) {
                   17822:                                                // self initialize
                   17823:                                                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   17824:                                                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   17825:                                        } else {
                   17826:                                                dma[c].mask |= bit;
                   17827:                                        }
                   17828:                                        dma[c].req &= ~bit;
                   17829:                                        dma[c].tc |= bit;
                   17830:                                } else if((dma[c].ch[ch].mode & 0xc0) == 0x40) {
                   17831:                                        // single mode
                   17832:                                        break;
                   17833:                                }
                   17834:                        }
                   17835:                }
                   17836:        }
                   17837: }
                   17838: 
                   17839: // pic
                   17840: 
                   17841: void pic_init()
                   17842: {
                   17843:        memset(pic, 0, sizeof(pic));
                   17844:        pic[0].imr = pic[1].imr = 0xff;
                   17845:        
                   17846:        // from bochs bios
                   17847:        pic_write(0, 0, 0x11);  // icw1 = 11h
                   17848:        pic_write(0, 1, 0x08);  // icw2 = 08h
                   17849:        pic_write(0, 1, 0x04);  // icw3 = 04h
                   17850:        pic_write(0, 1, 0x01);  // icw4 = 01h
                   17851:        pic_write(0, 1, 0xb8);  // ocw1 = b8h
                   17852:        pic_write(1, 0, 0x11);  // icw1 = 11h
                   17853:        pic_write(1, 1, 0x70);  // icw2 = 70h
                   17854:        pic_write(1, 1, 0x02);  // icw3 = 02h
                   17855:        pic_write(1, 1, 0x01);  // icw4 = 01h
                   17856: }
                   17857: 
                   17858: void pic_write(int c, UINT32 addr, UINT8 data)
                   17859: {
                   17860:        if(addr & 1) {
                   17861:                if(pic[c].icw2_r) {
                   17862:                        // icw2
                   17863:                        pic[c].icw2 = data;
                   17864:                        pic[c].icw2_r = 0;
                   17865:                } else if(pic[c].icw3_r) {
                   17866:                        // icw3
                   17867:                        pic[c].icw3 = data;
                   17868:                        pic[c].icw3_r = 0;
                   17869:                } else if(pic[c].icw4_r) {
                   17870:                        // icw4
                   17871:                        pic[c].icw4 = data;
                   17872:                        pic[c].icw4_r = 0;
                   17873:                } else {
                   17874:                        // ocw1
1.1       root     17875:                        pic[c].imr = data;
                   17876:                }
                   17877:        } else {
                   17878:                if(data & 0x10) {
                   17879:                        // icw1
                   17880:                        pic[c].icw1 = data;
                   17881:                        pic[c].icw2_r = 1;
                   17882:                        pic[c].icw3_r = (data & 2) ? 0 : 1;
                   17883:                        pic[c].icw4_r = data & 1;
                   17884:                        pic[c].irr = 0;
                   17885:                        pic[c].isr = 0;
                   17886:                        pic[c].imr = 0;
                   17887:                        pic[c].prio = 0;
                   17888:                        if(!(pic[c].icw1 & 1)) {
                   17889:                                pic[c].icw4 = 0;
                   17890:                        }
                   17891:                        pic[c].ocw3 = 0;
                   17892:                } else if(data & 8) {
                   17893:                        // ocw3
                   17894:                        if(!(data & 2)) {
                   17895:                                data = (data & ~1) | (pic[c].ocw3 & 1);
                   17896:                        }
                   17897:                        if(!(data & 0x40)) {
                   17898:                                data = (data & ~0x20) | (pic[c].ocw3 & 0x20);
                   17899:                        }
                   17900:                        pic[c].ocw3 = data;
                   17901:                } else {
                   17902:                        // ocw2
                   17903:                        int level = 0;
                   17904:                        if(data & 0x40) {
                   17905:                                level = data & 7;
                   17906:                        } else {
                   17907:                                if(!pic[c].isr) {
                   17908:                                        return;
                   17909:                                }
                   17910:                                level = pic[c].prio;
                   17911:                                while(!(pic[c].isr & (1 << level))) {
                   17912:                                        level = (level + 1) & 7;
                   17913:                                }
                   17914:                        }
                   17915:                        if(data & 0x80) {
                   17916:                                pic[c].prio = (level + 1) & 7;
                   17917:                        }
                   17918:                        if(data & 0x20) {
                   17919:                                pic[c].isr &= ~(1 << level);
                   17920:                        }
                   17921:                }
                   17922:        }
                   17923:        pic_update();
                   17924: }
                   17925: 
                   17926: UINT8 pic_read(int c, UINT32 addr)
                   17927: {
                   17928:        if(addr & 1) {
                   17929:                return(pic[c].imr);
                   17930:        } else {
                   17931:                // polling mode is not supported...
                   17932:                //if(pic[c].ocw3 & 4) {
                   17933:                //      return ???;
                   17934:                //}
                   17935:                if(pic[c].ocw3 & 1) {
                   17936:                        return(pic[c].isr);
                   17937:                } else {
                   17938:                        return(pic[c].irr);
                   17939:                }
                   17940:        }
                   17941: }
                   17942: 
                   17943: void pic_req(int c, int level, int signal)
                   17944: {
                   17945:        if(signal) {
                   17946:                pic[c].irr |= (1 << level);
                   17947:        } else {
                   17948:                pic[c].irr &= ~(1 << level);
                   17949:        }
                   17950:        pic_update();
                   17951: }
                   17952: 
                   17953: int pic_ack()
                   17954: {
                   17955:        // ack (INTA=L)
                   17956:        pic[pic_req_chip].isr |= pic_req_bit;
                   17957:        pic[pic_req_chip].irr &= ~pic_req_bit;
                   17958:        if(pic_req_chip > 0) {
                   17959:                // update isr and irr of master
                   17960:                UINT8 slave = 1 << (pic[pic_req_chip].icw3 & 7);
                   17961:                pic[pic_req_chip - 1].isr |= slave;
                   17962:                pic[pic_req_chip - 1].irr &= ~slave;
                   17963:        }
                   17964:        //if(pic[pic_req_chip].icw4 & 1) {
                   17965:                // 8086 mode
                   17966:                int vector = (pic[pic_req_chip].icw2 & 0xf8) | pic_req_level;
                   17967:        //} else {
                   17968:        //      // 8080 mode
                   17969:        //      UINT16 addr = (UINT16)pic[pic_req_chip].icw2 << 8;
                   17970:        //      if(pic[pic_req_chip].icw1 & 4) {
                   17971:        //              addr |= (pic[pic_req_chip].icw1 & 0xe0) | (pic_req_level << 2);
                   17972:        //      } else {
                   17973:        //              addr |= (pic[pic_req_chip].icw1 & 0xc0) | (pic_req_level << 3);
                   17974:        //      }
                   17975:        //      vector = 0xcd | (addr << 8);
                   17976:        //}
                   17977:        if(pic[pic_req_chip].icw4 & 2) {
                   17978:                // auto eoi
                   17979:                pic[pic_req_chip].isr &= ~pic_req_bit;
                   17980:        }
                   17981:        return(vector);
                   17982: }
                   17983: 
                   17984: void pic_update()
                   17985: {
                   17986:        for(int c = 0; c < 2; c++) {
                   17987:                UINT8 irr = pic[c].irr;
                   17988:                if(c + 1 < 2) {
                   17989:                        // this is master
                   17990:                        if(pic[c + 1].irr & (~pic[c + 1].imr)) {
                   17991:                                // request from slave
                   17992:                                irr |= 1 << (pic[c + 1].icw3 & 7);
                   17993:                        }
                   17994:                }
                   17995:                irr &= (~pic[c].imr);
                   17996:                if(!irr) {
                   17997:                        break;
                   17998:                }
                   17999:                if(!(pic[c].ocw3 & 0x20)) {
                   18000:                        irr |= pic[c].isr;
                   18001:                }
                   18002:                int level = pic[c].prio;
                   18003:                UINT8 bit = 1 << level;
                   18004:                while(!(irr & bit)) {
                   18005:                        level = (level + 1) & 7;
                   18006:                        bit = 1 << level;
                   18007:                }
                   18008:                if((c + 1 < 2) && (pic[c].icw3 & bit)) {
                   18009:                        // check slave
                   18010:                        continue;
                   18011:                }
                   18012:                if(pic[c].isr & bit) {
                   18013:                        break;
                   18014:                }
                   18015:                // interrupt request
                   18016:                pic_req_chip = c;
                   18017:                pic_req_level = level;
                   18018:                pic_req_bit = bit;
1.1.1.3   root     18019:                i386_set_irq_line(INPUT_LINE_IRQ, HOLD_LINE);
1.1       root     18020:                return;
                   18021:        }
1.1.1.3   root     18022:        i386_set_irq_line(INPUT_LINE_IRQ, CLEAR_LINE);
1.1.1.2   root     18023: }
1.1       root     18024: 
1.1.1.25  root     18025: // pio
                   18026: 
                   18027: void pio_init()
                   18028: {
1.1.1.38  root     18029: //     bool conv_mode = (GetConsoleCP() == 932);
                   18030:        
1.1.1.26  root     18031:        memset(pio, 0, sizeof(pio));
1.1.1.37  root     18032:        
1.1.1.25  root     18033:        for(int c = 0; c < 2; c++) {
1.1.1.37  root     18034:                pio[c].stat = 0xdf;
1.1.1.25  root     18035:                pio[c].ctrl = 0x0c;
1.1.1.38  root     18036: //             pio[c].conv_mode = conv_mode;
1.1.1.25  root     18037:        }
                   18038: }
                   18039: 
1.1.1.37  root     18040: void pio_finish()
                   18041: {
                   18042:        pio_release();
                   18043: }
                   18044: 
                   18045: void pio_release()
                   18046: {
                   18047:        for(int c = 0; c < 2; c++) {
                   18048:                if(pio[c].fp != NULL) {
1.1.1.38  root     18049:                        if(pio[c].jis_mode) {
                   18050:                                fputc(0x1c, pio[c].fp);
                   18051:                                fputc(0x2e, pio[c].fp);
                   18052:                        }
1.1.1.37  root     18053:                        fclose(pio[c].fp);
                   18054:                        pio[c].fp = NULL;
                   18055:                }
                   18056:        }
                   18057: }
                   18058: 
1.1.1.25  root     18059: void pio_write(int c, UINT32 addr, UINT8 data)
                   18060: {
                   18061:        switch(addr & 3) {
                   18062:        case 0:
                   18063:                pio[c].data = data;
                   18064:                break;
                   18065:        case 2:
1.1.1.37  root     18066:                if((pio[c].ctrl & 0x01) && !(data & 0x01)) {
                   18067:                        // strobe H -> L
                   18068:                        if(pio[c].data == 0x0d && (data & 0x02)) {
                   18069:                                // auto feed
                   18070:                                printer_out(c, 0x0d);
                   18071:                                printer_out(c, 0x0a);
                   18072:                        } else {
                   18073:                                printer_out(c, pio[c].data);
                   18074:                        }
                   18075:                        pio[c].stat &= ~0x40; // set ack
                   18076:                }
1.1.1.25  root     18077:                pio[c].ctrl = data;
                   18078:                break;
                   18079:        }
                   18080: }
                   18081: 
                   18082: UINT8 pio_read(int c, UINT32 addr)
                   18083: {
                   18084:        switch(addr & 3) {
                   18085:        case 0:
1.1.1.37  root     18086:                if(pio[c].ctrl & 0x20) {
                   18087:                        // input mode
                   18088:                        return(0xff);
                   18089:                }
1.1.1.25  root     18090:                return(pio[c].data);
                   18091:        case 1:
1.1.1.37  root     18092:                {
                   18093:                        UINT8 stat = pio[c].stat;
                   18094:                        pio[c].stat |= 0x40; // clear ack
                   18095:                        return(stat);
                   18096:                }
1.1.1.25  root     18097:        case 2:
                   18098:                return(pio[c].ctrl);
                   18099:        }
                   18100:        return(0xff);
                   18101: }
                   18102: 
1.1.1.37  root     18103: void printer_out(int c, UINT8 data)
                   18104: {
                   18105:        SYSTEMTIME time;
1.1.1.38  root     18106:        bool jis_mode = false;
1.1.1.37  root     18107:        
                   18108:        GetLocalTime(&time);
                   18109:        
                   18110:        if(pio[c].fp != NULL) {
                   18111:                // if at least 1000ms passed from last written, close the current file
                   18112:                FILETIME ftime1;
                   18113:                FILETIME ftime2;
                   18114:                SystemTimeToFileTime(&pio[c].time, &ftime1);
                   18115:                SystemTimeToFileTime(&time, &ftime2);
                   18116:                INT64 *time1 = (INT64 *)&ftime1;
                   18117:                INT64 *time2 = (INT64 *)&ftime2;
                   18118:                INT64 msec = (*time2 - *time1) / 10000;
                   18119:                
                   18120:                if(msec >= 1000) {
1.1.1.38  root     18121:                        if(pio[c].jis_mode) {
                   18122:                                fputc(0x1c, pio[c].fp);
                   18123:                                fputc(0x2e, pio[c].fp);
                   18124:                                jis_mode = true;
                   18125:                        }
1.1.1.37  root     18126:                        fclose(pio[c].fp);
                   18127:                        pio[c].fp = NULL;
                   18128:                }
                   18129:        }
                   18130:        if(pio[c].fp == NULL) {
                   18131:                // create a new file in the temp folder
                   18132:                char file_name[MAX_PATH];
                   18133:                
                   18134:                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);
                   18135:                if(GetTempPath(MAX_PATH, pio[c].path)) {
                   18136:                        strcat(pio[c].path, file_name);
                   18137:                } else {
                   18138:                        strcpy(pio[c].path, file_name);
                   18139:                }
1.1.1.38  root     18140:                pio[c].fp = fopen(pio[c].path, "w+b");
1.1.1.37  root     18141:        }
                   18142:        if(pio[c].fp != NULL) {
1.1.1.38  root     18143:                if(jis_mode) {
                   18144:                        fputc(0x1c, pio[c].fp);
                   18145:                        fputc(0x26, pio[c].fp);
                   18146:                }
1.1.1.37  root     18147:                fputc(data, pio[c].fp);
1.1.1.38  root     18148:                
                   18149:                // reopen file if 1ch 26h 1ch 2eh (kanji-on  kanji-off) are written at the top
                   18150:                if(data == 0x2e && ftell(pio[c].fp) == 4) {
                   18151:                        UINT8 buffer[4];
                   18152:                        fseek(pio[c].fp, 0, SEEK_SET);
                   18153:                        fread(buffer, 4, 1, pio[c].fp);
                   18154:                        if(buffer[0] == 0x1c && buffer[1] == 0x26 && buffer[2] == 0x1c/* && buffer[3] == 0x2e*/) {
                   18155:                                fclose(pio[c].fp);
                   18156:                                pio[c].fp = fopen(pio[c].path, "w+b");
                   18157:                        }
                   18158:                }
1.1.1.37  root     18159:                pio[c].time = time;
                   18160:        }
                   18161: }
                   18162: 
1.1       root     18163: // pit
                   18164: 
1.1.1.22  root     18165: #define PIT_FREQ 1193182ULL
1.1       root     18166: #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)
                   18167: 
                   18168: void pit_init()
                   18169: {
1.1.1.8   root     18170:        memset(pit, 0, sizeof(pit));
1.1       root     18171:        for(int ch = 0; ch < 3; ch++) {
                   18172:                pit[ch].count = 0x10000;
                   18173:                pit[ch].ctrl_reg = 0x34;
                   18174:                pit[ch].mode = 3;
                   18175:        }
                   18176:        
                   18177:        // from bochs bios
                   18178:        pit_write(3, 0x34);
                   18179:        pit_write(0, 0x00);
                   18180:        pit_write(0, 0x00);
                   18181: }
                   18182: 
                   18183: void pit_write(int ch, UINT8 val)
                   18184: {
1.1.1.8   root     18185: #ifndef PIT_ALWAYS_RUNNING
1.1       root     18186:        if(!pit_active) {
                   18187:                pit_active = 1;
                   18188:                pit_init();
                   18189:        }
1.1.1.8   root     18190: #endif
1.1       root     18191:        switch(ch) {
                   18192:        case 0:
                   18193:        case 1:
                   18194:        case 2:
                   18195:                // write count register
                   18196:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   18197:                        if(pit[ch].ctrl_reg & 0x10) {
                   18198:                                pit[ch].low_write = 1;
                   18199:                        }
                   18200:                        if(pit[ch].ctrl_reg & 0x20) {
                   18201:                                pit[ch].high_write = 1;
                   18202:                        }
                   18203:                }
                   18204:                if(pit[ch].low_write) {
                   18205:                        pit[ch].count_reg = val;
                   18206:                        pit[ch].low_write = 0;
                   18207:                } else if(pit[ch].high_write) {
                   18208:                        if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   18209:                                pit[ch].count_reg = val << 8;
                   18210:                        } else {
                   18211:                                pit[ch].count_reg |= val << 8;
                   18212:                        }
                   18213:                        pit[ch].high_write = 0;
                   18214:                }
                   18215:                // start count
1.1.1.8   root     18216:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   18217:                        if(pit[ch].mode == 0 || pit[ch].mode == 4 || pit[ch].prev_time == 0) {
                   18218:                                pit[ch].count = PIT_COUNT_VALUE(ch);
                   18219:                                pit[ch].prev_time = timeGetTime();
                   18220:                                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     18221:                        }
                   18222:                }
                   18223:                break;
                   18224:        case 3: // ctrl reg
                   18225:                if((val & 0xc0) == 0xc0) {
                   18226:                        // i8254 read-back command
                   18227:                        for(ch = 0; ch < 3; ch++) {
                   18228:                                if(!(val & 0x10) && !pit[ch].status_latched) {
                   18229:                                        pit[ch].status = pit[ch].ctrl_reg & 0x3f;
                   18230:                                        pit[ch].status_latched = 1;
                   18231:                                }
                   18232:                                if(!(val & 0x20) && !pit[ch].count_latched) {
                   18233:                                        pit_latch_count(ch);
                   18234:                                }
                   18235:                        }
                   18236:                        break;
                   18237:                }
                   18238:                ch = (val >> 6) & 3;
                   18239:                if(val & 0x30) {
1.1.1.35  root     18240:                        static const int modes[8] = {0, 1, 2, 3, 4, 5, 2, 3};
1.1       root     18241:                        pit[ch].mode = modes[(val >> 1) & 7];
                   18242:                        pit[ch].count_latched = 0;
                   18243:                        pit[ch].low_read = pit[ch].high_read = 0;
                   18244:                        pit[ch].low_write = pit[ch].high_write = 0;
                   18245:                        pit[ch].ctrl_reg = val;
                   18246:                        // stop count
1.1.1.8   root     18247:                        pit[ch].prev_time = pit[ch].expired_time = 0;
1.1       root     18248:                        pit[ch].count_reg = 0;
                   18249:                } else if(!pit[ch].count_latched) {
                   18250:                        pit_latch_count(ch);
                   18251:                }
                   18252:                break;
                   18253:        }
                   18254: }
                   18255: 
                   18256: UINT8 pit_read(int ch)
                   18257: {
1.1.1.8   root     18258: #ifndef PIT_ALWAYS_RUNNING
1.1       root     18259:        if(!pit_active) {
                   18260:                pit_active = 1;
                   18261:                pit_init();
                   18262:        }
1.1.1.8   root     18263: #endif
1.1       root     18264:        switch(ch) {
                   18265:        case 0:
                   18266:        case 1:
                   18267:        case 2:
                   18268:                if(pit[ch].status_latched) {
                   18269:                        pit[ch].status_latched = 0;
                   18270:                        return(pit[ch].status);
                   18271:                }
                   18272:                // if not latched, through current count
                   18273:                if(!pit[ch].count_latched) {
                   18274:                        if(!pit[ch].low_read && !pit[ch].high_read) {
                   18275:                                pit_latch_count(ch);
                   18276:                        }
                   18277:                }
                   18278:                // return latched count
                   18279:                if(pit[ch].low_read) {
                   18280:                        pit[ch].low_read = 0;
                   18281:                        if(!pit[ch].high_read) {
                   18282:                                pit[ch].count_latched = 0;
                   18283:                        }
                   18284:                        return(pit[ch].latch & 0xff);
                   18285:                } else if(pit[ch].high_read) {
                   18286:                        pit[ch].high_read = 0;
                   18287:                        pit[ch].count_latched = 0;
                   18288:                        return((pit[ch].latch >> 8) & 0xff);
                   18289:                }
                   18290:        }
                   18291:        return(0xff);
                   18292: }
                   18293: 
1.1.1.8   root     18294: int pit_run(int ch, UINT32 cur_time)
1.1       root     18295: {
1.1.1.8   root     18296:        if(pit[ch].expired_time != 0 && cur_time >= pit[ch].expired_time) {
1.1       root     18297:                pit[ch].count = PIT_COUNT_VALUE(ch);
1.1.1.8   root     18298:                pit[ch].prev_time = pit[ch].expired_time;
                   18299:                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
                   18300:                if(cur_time >= pit[ch].expired_time) {
                   18301:                        pit[ch].prev_time = cur_time;
                   18302:                        pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     18303:                }
1.1.1.8   root     18304:                return(1);
1.1       root     18305:        }
1.1.1.8   root     18306:        return(0);
1.1       root     18307: }
                   18308: 
                   18309: void pit_latch_count(int ch)
                   18310: {
1.1.1.8   root     18311:        if(pit[ch].expired_time != 0) {
1.1.1.26  root     18312:                UINT32 cur_time = timeGetTime();
1.1.1.8   root     18313:                pit_run(ch, cur_time);
                   18314:                UINT32 tmp = (pit[ch].count * (pit[ch].expired_time - cur_time)) / (pit[ch].expired_time - pit[ch].prev_time);
1.1.1.26  root     18315:                UINT16 latch = (tmp != 0) ? (UINT16)tmp : 1;
                   18316:                
                   18317:                if(pit[ch].prev_latch == latch && pit[ch].expired_time > cur_time) {
                   18318:                        // decrement counter in 1msec period
                   18319:                        if(pit[ch].next_latch == 0) {
                   18320:                                tmp = (pit[ch].count * (pit[ch].expired_time - cur_time - 1)) / (pit[ch].expired_time - pit[ch].prev_time);
                   18321:                                pit[ch].next_latch = (tmp != 0) ? (UINT16)tmp : 1;
                   18322:                        }
                   18323:                        if(pit[ch].latch > pit[ch].next_latch) {
                   18324:                                pit[ch].latch--;
                   18325:                        }
                   18326:                } else {
                   18327:                        pit[ch].prev_latch = pit[ch].latch = latch;
                   18328:                        pit[ch].next_latch = 0;
                   18329:                }
1.1.1.8   root     18330:        } else {
                   18331:                pit[ch].latch = (UINT16)pit[ch].count;
1.1.1.26  root     18332:                pit[ch].prev_latch = pit[ch].next_latch = 0;
1.1       root     18333:        }
                   18334:        pit[ch].count_latched = 1;
                   18335:        if((pit[ch].ctrl_reg & 0x30) == 0x10) {
                   18336:                // lower byte
                   18337:                pit[ch].low_read = 1;
                   18338:                pit[ch].high_read = 0;
                   18339:        } else if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   18340:                // upper byte
                   18341:                pit[ch].low_read = 0;
                   18342:                pit[ch].high_read = 1;
                   18343:        } else {
                   18344:                // lower -> upper
1.1.1.14  root     18345:                pit[ch].low_read = pit[ch].high_read = 1;
1.1       root     18346:        }
                   18347: }
                   18348: 
1.1.1.8   root     18349: int pit_get_expired_time(int ch)
1.1       root     18350: {
1.1.1.22  root     18351:        pit[ch].accum += 1024ULL * 1000ULL * (UINT64)pit[ch].count / PIT_FREQ;
                   18352:        UINT64 val = pit[ch].accum >> 10;
                   18353:        pit[ch].accum -= val << 10;
                   18354:        return((val != 0) ? val : 1);
1.1.1.8   root     18355: }
                   18356: 
1.1.1.25  root     18357: // sio
                   18358: 
                   18359: void sio_init()
                   18360: {
1.1.1.26  root     18361:        memset(sio, 0, sizeof(sio));
                   18362:        memset(sio_mt, 0, sizeof(sio_mt));
                   18363:        
1.1.1.29  root     18364:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     18365:                sio[c].send_buffer = new FIFO(SIO_BUFFER_SIZE);
                   18366:                sio[c].recv_buffer = new FIFO(SIO_BUFFER_SIZE);
                   18367:                
                   18368:                sio[c].divisor.w = 12;          // 115200Hz / 9600Baud
                   18369:                sio[c].line_ctrl = 0x03;        // 8bit, stop 1bit, non parity
1.1.1.26  root     18370:                sio[c].modem_ctrl = 0x03;       // rts=on, dtr=on
                   18371:                sio[c].set_rts = sio[c].set_dtr = true;
1.1.1.25  root     18372:                sio[c].line_stat_buf = 0x60;    // send/recv buffers are empty
                   18373:                sio[c].irq_identify = 0x01;     // no pending irq
                   18374:                
                   18375:                InitializeCriticalSection(&sio_mt[c].csSendData);
                   18376:                InitializeCriticalSection(&sio_mt[c].csRecvData);
                   18377:                InitializeCriticalSection(&sio_mt[c].csLineCtrl);
                   18378:                InitializeCriticalSection(&sio_mt[c].csLineStat);
                   18379:                InitializeCriticalSection(&sio_mt[c].csModemCtrl);
                   18380:                InitializeCriticalSection(&sio_mt[c].csModemStat);
                   18381:                
1.1.1.26  root     18382:                if(sio_port_number[c] != 0) {
1.1.1.25  root     18383:                        sio[c].channel = c;
                   18384:                        sio_mt[c].hThread = CreateThread(NULL, 0, sio_thread, &sio[c], 0, NULL);
                   18385:                }
                   18386:        }
                   18387: }
                   18388: 
                   18389: void sio_finish()
                   18390: {
1.1.1.29  root     18391:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     18392:                if(sio_mt[c].hThread != NULL) {
                   18393:                        WaitForSingleObject(sio_mt[c].hThread, INFINITE);
                   18394:                        CloseHandle(sio_mt[c].hThread);
1.1.1.28  root     18395:                        sio_mt[c].hThread = NULL;
1.1.1.25  root     18396:                }
                   18397:                DeleteCriticalSection(&sio_mt[c].csSendData);
                   18398:                DeleteCriticalSection(&sio_mt[c].csRecvData);
                   18399:                DeleteCriticalSection(&sio_mt[c].csLineCtrl);
                   18400:                DeleteCriticalSection(&sio_mt[c].csLineStat);
                   18401:                DeleteCriticalSection(&sio_mt[c].csModemCtrl);
                   18402:                DeleteCriticalSection(&sio_mt[c].csModemStat);
1.1.1.28  root     18403:        }
                   18404:        sio_release();
                   18405: }
                   18406: 
                   18407: void sio_release()
                   18408: {
1.1.1.29  root     18409:        for(int c = 0; c < 4; c++) {
1.1.1.28  root     18410:                // sio_thread() may access the resources :-(
1.1.1.32  root     18411:                bool running = (sio_mt[c].hThread != NULL);
                   18412:                
                   18413:                if(running) {
                   18414:                        EnterCriticalSection(&sio_mt[c].csSendData);
                   18415:                }
                   18416:                if(sio[c].send_buffer != NULL) {
                   18417:                        sio[c].send_buffer->release();
                   18418:                        delete sio[c].send_buffer;
                   18419:                        sio[c].send_buffer = NULL;
                   18420:                }
                   18421:                if(running) {
                   18422:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   18423:                        EnterCriticalSection(&sio_mt[c].csRecvData);
                   18424:                }
                   18425:                if(sio[c].recv_buffer != NULL) {
                   18426:                        sio[c].recv_buffer->release();
                   18427:                        delete sio[c].recv_buffer;
                   18428:                        sio[c].recv_buffer = NULL;
                   18429:                }
                   18430:                if(running) {
                   18431:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
1.1.1.28  root     18432:                }
1.1.1.25  root     18433:        }
                   18434: }
                   18435: 
                   18436: void sio_write(int c, UINT32 addr, UINT8 data)
                   18437: {
                   18438:        switch(addr & 7) {
                   18439:        case 0:
                   18440:                if(sio[c].selector & 0x80) {
                   18441:                        if(sio[c].divisor.b.l != data) {
                   18442:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   18443:                                sio[c].divisor.b.l = data;
                   18444:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   18445:                        }
                   18446:                } else {
                   18447:                        EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     18448:                        if(sio[c].send_buffer != NULL) {
                   18449:                                sio[c].send_buffer->write(data);
                   18450:                        }
1.1.1.25  root     18451:                        // transmitter holding/shift registers are not empty
                   18452:                        sio[c].line_stat_buf &= ~0x60;
                   18453:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   18454:                        
                   18455:                        if(sio[c].irq_enable & 0x02) {
                   18456:                                sio_update_irq(c);
                   18457:                        }
                   18458:                }
                   18459:                break;
                   18460:        case 1:
                   18461:                if(sio[c].selector & 0x80) {
                   18462:                        if(sio[c].divisor.b.h != data) {
                   18463:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   18464:                                sio[c].divisor.b.h = data;
                   18465:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   18466:                        }
                   18467:                } else {
                   18468:                        if(sio[c].irq_enable != data) {
                   18469:                                sio[c].irq_enable = data;
                   18470:                                sio_update_irq(c);
                   18471:                        }
                   18472:                }
                   18473:                break;
                   18474:        case 3:
                   18475:                {
                   18476:                        UINT8 line_ctrl = data & 0x3f;
                   18477:                        bool set_brk = ((data & 0x40) != 0);
                   18478:                        
                   18479:                        if(sio[c].line_ctrl != line_ctrl) {
                   18480:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   18481:                                sio[c].line_ctrl = line_ctrl;
                   18482:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   18483:                        }
                   18484:                        if(sio[c].set_brk != set_brk) {
                   18485:                                EnterCriticalSection(&sio_mt[c].csModemCtrl);
                   18486:                                sio[c].set_brk = set_brk;
                   18487:                                LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   18488:                        }
                   18489:                }
                   18490:                sio[c].selector = data;
                   18491:                break;
                   18492:        case 4:
                   18493:                {
                   18494:                        bool set_dtr = ((data & 0x01) != 0);
                   18495:                        bool set_rts = ((data & 0x02) != 0);
                   18496:                        
                   18497:                        if(sio[c].set_dtr != set_dtr || sio[c].set_rts != set_rts) {
1.1.1.26  root     18498: //                             EnterCriticalSection(&sio_mt[c].csModemCtrl);
1.1.1.25  root     18499:                                sio[c].set_dtr = set_dtr;
                   18500:                                sio[c].set_rts = set_rts;
1.1.1.26  root     18501: //                             LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   18502:                                
                   18503:                                bool state_changed = false;
                   18504:                                
                   18505:                                EnterCriticalSection(&sio_mt[c].csModemStat);
                   18506:                                if(set_dtr) {
                   18507:                                        sio[c].modem_stat |= 0x20;      // dsr on
                   18508:                                } else {
                   18509:                                        sio[c].modem_stat &= ~0x20;     // dsr off
                   18510:                                }
                   18511:                                if(set_rts) {
                   18512:                                        sio[c].modem_stat |= 0x10;      // cts on
                   18513:                                } else {
                   18514:                                        sio[c].modem_stat &= ~0x10;     // cts off
                   18515:                                }
                   18516:                                if((sio[c].prev_modem_stat & 0x20) != (sio[c].modem_stat & 0x20)) {
                   18517:                                        if(!(sio[c].modem_stat & 0x02)) {
                   18518:                                                if(sio[c].irq_enable & 0x08) {
                   18519:                                                        state_changed = true;
                   18520:                                                }
                   18521:                                                sio[c].modem_stat |= 0x02;
                   18522:                                        }
                   18523:                                }
                   18524:                                if((sio[c].prev_modem_stat & 0x10) != (sio[c].modem_stat & 0x10)) {
                   18525:                                        if(!(sio[c].modem_stat & 0x01)) {
                   18526:                                                if(sio[c].irq_enable & 0x08) {
                   18527:                                                        state_changed = true;
                   18528:                                                }
                   18529:                                                sio[c].modem_stat |= 0x01;
                   18530:                                        }
                   18531:                                }
                   18532:                                LeaveCriticalSection(&sio_mt[c].csModemStat);
                   18533:                                
                   18534:                                if(state_changed) {
                   18535:                                        sio_update_irq(c);
                   18536:                                }
1.1.1.25  root     18537:                        }
                   18538:                }
                   18539:                sio[c].modem_ctrl = data;
                   18540:                break;
                   18541:        case 7:
                   18542:                sio[c].scratch = data;
                   18543:                break;
                   18544:        }
                   18545: }
                   18546: 
                   18547: UINT8 sio_read(int c, UINT32 addr)
                   18548: {
                   18549:        switch(addr & 7) {
                   18550:        case 0:
                   18551:                if(sio[c].selector & 0x80) {
                   18552:                        return(sio[c].divisor.b.l);
                   18553:                } else {
                   18554:                        EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     18555:                        UINT8 data = 0;
                   18556:                        if(sio[c].recv_buffer != NULL) {
                   18557:                                data = sio[c].recv_buffer->read();
                   18558:                        }
1.1.1.25  root     18559:                        // data is not ready
                   18560:                        sio[c].line_stat_buf &= ~0x01;
                   18561:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
                   18562:                        
                   18563:                        if(sio[c].irq_enable & 0x01) {
                   18564:                                sio_update_irq(c);
                   18565:                        }
                   18566:                        return(data);
                   18567:                }
                   18568:        case 1:
                   18569:                if(sio[c].selector & 0x80) {
                   18570:                        return(sio[c].divisor.b.h);
                   18571:                } else {
                   18572:                        return(sio[c].irq_enable);
                   18573:                }
                   18574:        case 2:
                   18575:                return(sio[c].irq_identify);
                   18576:        case 3:
                   18577:                return(sio[c].selector);
                   18578:        case 4:
                   18579:                return(sio[c].modem_ctrl);
                   18580:        case 5:
                   18581:                {
                   18582:                        EnterCriticalSection(&sio_mt[c].csLineStat);
                   18583:                        UINT8 val = sio[c].line_stat_err | sio[c].line_stat_buf;
                   18584:                        sio[c].line_stat_err = 0x00;
                   18585:                        LeaveCriticalSection(&sio_mt[c].csLineStat);
                   18586:                        
                   18587:                        bool state_changed = false;
                   18588:                        
                   18589:                        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   18590:                                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     18591:                                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     18592:                                        // transmitter holding register will be empty first
                   18593:                                        if(sio[c].irq_enable & 0x02) {
                   18594:                                                state_changed = true;
                   18595:                                        }
                   18596:                                        sio[c].line_stat_buf |= 0x20;
                   18597:                                }
                   18598:                                LeaveCriticalSection(&sio_mt[c].csSendData);
                   18599:                        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   18600:                                // transmitter shift register will be empty later
                   18601:                                sio[c].line_stat_buf |= 0x40;
                   18602:                        }
                   18603:                        if(!(sio[c].line_stat_buf & 0x01)) {
                   18604:                                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     18605:                                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     18606:                                        // data is ready
                   18607:                                        if(sio[c].irq_enable & 0x01) {
                   18608:                                                state_changed = true;
                   18609:                                        }
                   18610:                                        sio[c].line_stat_buf |= 0x01;
                   18611:                                }
                   18612:                                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   18613:                        }
                   18614:                        if(state_changed) {
                   18615:                                sio_update_irq(c);
                   18616:                        }
                   18617:                        return(val);
                   18618:                }
                   18619:        case 6:
                   18620:                {
                   18621:                        EnterCriticalSection(&sio_mt[c].csModemStat);
                   18622:                        UINT8 val = sio[c].modem_stat;
                   18623:                        sio[c].modem_stat &= 0xf0;
                   18624:                        sio[c].prev_modem_stat = sio[c].modem_stat;
                   18625:                        LeaveCriticalSection(&sio_mt[c].csModemStat);
                   18626:                        
                   18627:                        if(sio[c].modem_ctrl & 0x10) {
                   18628:                                // loop-back
                   18629:                                val &= 0x0f;
                   18630:                                val |= (sio[c].modem_ctrl & 0x0c) << 4;
                   18631:                                val |= (sio[c].modem_ctrl & 0x01) << 5;
                   18632:                                val |= (sio[c].modem_ctrl & 0x02) << 3;
                   18633:                        }
                   18634:                        return(val);
                   18635:                }
                   18636:        case 7:
                   18637:                return(sio[c].scratch);
                   18638:        }
                   18639:        return(0xff);
                   18640: }
                   18641: 
                   18642: void sio_update(int c)
                   18643: {
                   18644:        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   18645:                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     18646:                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     18647:                        // transmitter holding/shift registers will be empty
                   18648:                        sio[c].line_stat_buf |= 0x60;
                   18649:                }
                   18650:                LeaveCriticalSection(&sio_mt[c].csSendData);
                   18651:        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   18652:                // transmitter shift register will be empty
                   18653:                sio[c].line_stat_buf |= 0x40;
                   18654:        }
                   18655:        if(!(sio[c].line_stat_buf & 0x01)) {
                   18656:                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     18657:                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     18658:                        // data is ready
                   18659:                        sio[c].line_stat_buf |= 0x01;
                   18660:                }
                   18661:                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   18662:        }
                   18663:        sio_update_irq(c);
                   18664: }
                   18665: 
                   18666: void sio_update_irq(int c)
                   18667: {
                   18668:        int level = -1;
                   18669:        
                   18670:        if(sio[c].irq_enable & 0x08) {
                   18671:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   18672:                if((sio[c].modem_stat & 0x0f) != 0) {
                   18673:                        level = 0;
                   18674:                }
                   18675:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   18676:        }
                   18677:        if(sio[c].irq_enable & 0x02) {
                   18678:                if(sio[c].line_stat_buf & 0x20) {
                   18679:                        level = 1;
                   18680:                }
                   18681:        }
                   18682:        if(sio[c].irq_enable & 0x01) {
                   18683:                if(sio[c].line_stat_buf & 0x01) {
                   18684:                        level = 2;
                   18685:                }
                   18686:        }
                   18687:        if(sio[c].irq_enable & 0x04) {
                   18688:                EnterCriticalSection(&sio_mt[c].csLineStat);
                   18689:                if(sio[c].line_stat_err != 0) {
                   18690:                        level = 3;
                   18691:                }
                   18692:                LeaveCriticalSection(&sio_mt[c].csLineStat);
                   18693:        }
1.1.1.29  root     18694:        
                   18695:        // COM1 and COM3 shares IRQ4, COM2 and COM4 shares IRQ3
1.1.1.25  root     18696:        if(level != -1) {
                   18697:                sio[c].irq_identify = level << 1;
1.1.1.29  root     18698:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 1);
1.1.1.25  root     18699:        } else {
                   18700:                sio[c].irq_identify = 1;
1.1.1.29  root     18701:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 0);
1.1.1.25  root     18702:        }
                   18703: }
                   18704: 
                   18705: DWORD WINAPI sio_thread(void *lpx)
                   18706: {
                   18707:        volatile sio_t *p = (sio_t *)lpx;
                   18708:        sio_mt_t *q = &sio_mt[p->channel];
                   18709:        
                   18710:        char name[] = "COM1";
1.1.1.26  root     18711:        name[3] = '0' + sio_port_number[p->channel];
                   18712:        HANDLE hComm = NULL;
                   18713:        COMMPROP commProp;
                   18714:        DCB dcb;
                   18715:        DWORD dwSettableBaud = 0xffb; // 75, 110, 150, 300, 600, 1200, 1800, 2400, 4800, 7200, and 9600bps
                   18716:        BYTE bytBuffer[SIO_BUFFER_SIZE];
                   18717:        
                   18718:        if((hComm = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL)) != INVALID_HANDLE_VALUE) {
                   18719:                if(GetCommProperties(hComm, &commProp)) {
                   18720:                        dwSettableBaud = commProp.dwSettableBaud;
                   18721:                }
1.1.1.25  root     18722:                EscapeCommFunction(hComm, CLRBREAK);
1.1.1.26  root     18723: //             EscapeCommFunction(hComm, SETRTS);
                   18724: //             EscapeCommFunction(hComm, SETDTR);
1.1.1.25  root     18725:                
                   18726:                while(!m_halted) {
                   18727:                        // setup comm port
                   18728:                        bool comm_state_changed = false;
                   18729:                        
                   18730:                        EnterCriticalSection(&q->csLineCtrl);
                   18731:                        if((p->prev_divisor != p->divisor.w || p->prev_line_ctrl != p->line_ctrl) && p->divisor.w != 0) {
                   18732:                                p->prev_divisor = p->divisor.w;
                   18733:                                p->prev_line_ctrl = p->line_ctrl;
                   18734:                                comm_state_changed = true;
                   18735:                        }
                   18736:                        LeaveCriticalSection(&q->csLineCtrl);
                   18737:                        
                   18738:                        if(comm_state_changed) {
1.1.1.26  root     18739:                                if(GetCommState(hComm, &dcb)) {
                   18740: //                                     dcb.BaudRate = min(9600, 115200 / p->prev_divisor);
                   18741:                                        DWORD baud = 115200 / p->prev_divisor;
                   18742:                                        dcb.BaudRate = 9600; // default
                   18743:                                        
                   18744:                                        if((dwSettableBaud & BAUD_075  ) && baud >= 75   ) dcb.BaudRate = 75;
                   18745:                                        if((dwSettableBaud & BAUD_110  ) && baud >= 110  ) dcb.BaudRate = 110;
                   18746:                                        // 134.5bps is not supported ???
                   18747: //                                     if((dwSettableBaud & BAUD_134_5) && baud >= 134.5) dcb.BaudRate = 134.5;
                   18748:                                        if((dwSettableBaud & BAUD_150  ) && baud >= 150  ) dcb.BaudRate = 150;
                   18749:                                        if((dwSettableBaud & BAUD_300  ) && baud >= 300  ) dcb.BaudRate = 300;
                   18750:                                        if((dwSettableBaud & BAUD_600  ) && baud >= 600  ) dcb.BaudRate = 600;
                   18751:                                        if((dwSettableBaud & BAUD_1200 ) && baud >= 1200 ) dcb.BaudRate = 1200;
                   18752:                                        if((dwSettableBaud & BAUD_1800 ) && baud >= 1800 ) dcb.BaudRate = 1800;
                   18753:                                        if((dwSettableBaud & BAUD_2400 ) && baud >= 2400 ) dcb.BaudRate = 2400;
                   18754:                                        if((dwSettableBaud & BAUD_4800 ) && baud >= 4800 ) dcb.BaudRate = 4800;
                   18755:                                        if((dwSettableBaud & BAUD_7200 ) && baud >= 7200 ) dcb.BaudRate = 7200;
                   18756:                                        if((dwSettableBaud & BAUD_9600 ) && baud >= 9600 ) dcb.BaudRate = 9600;
                   18757: //                                     if((dwSettableBaud & BAUD_14400) && baud >= 14400) dcb.BaudRate = 14400;
                   18758: //                                     if((dwSettableBaud & BAUD_19200) && baud >= 19200) dcb.BaudRate = 19200;
                   18759: //                                     if((dwSettableBaud & BAUD_38400) && baud >= 38400) dcb.BaudRate = 38400;
                   18760:                                        
                   18761:                                        switch(p->prev_line_ctrl & 0x03) {
                   18762:                                        case 0x00: dcb.ByteSize = 5; break;
                   18763:                                        case 0x01: dcb.ByteSize = 6; break;
                   18764:                                        case 0x02: dcb.ByteSize = 7; break;
                   18765:                                        case 0x03: dcb.ByteSize = 8; break;
                   18766:                                        }
                   18767:                                        switch(p->prev_line_ctrl & 0x04) {
                   18768:                                        case 0x00: dcb.StopBits = ONESTOPBIT; break;
                   18769:                                        case 0x04: dcb.StopBits = (dcb.ByteSize == 5) ? ONE5STOPBITS : TWOSTOPBITS; break;
                   18770:                                        }
                   18771:                                        switch(p->prev_line_ctrl & 0x38) {
                   18772:                                        case 0x08: dcb.Parity = ODDPARITY;   break;
                   18773:                                        case 0x18: dcb.Parity = EVENPARITY;  break;
                   18774:                                        case 0x28: dcb.Parity = MARKPARITY;  break;
                   18775:                                        case 0x38: dcb.Parity = SPACEPARITY; break;
                   18776:                                        default:   dcb.Parity = NOPARITY;    break;
                   18777:                                        }
                   18778:                                        dcb.fBinary = TRUE;
                   18779:                                        dcb.fParity = (dcb.Parity != NOPARITY);
                   18780:                                        dcb.fOutxCtsFlow = dcb.fOutxDsrFlow = TRUE;
                   18781:                                        dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
                   18782:                                        dcb.fDsrSensitivity = FALSE;//TRUE;
                   18783:                                        dcb.fTXContinueOnXoff = TRUE;
                   18784:                                        dcb.fOutX = dcb.fInX = FALSE;
                   18785:                                        dcb.fErrorChar = FALSE;
                   18786:                                        dcb.fNull = FALSE;
                   18787:                                        dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
                   18788:                                        dcb.fAbortOnError = FALSE;
                   18789:                                        
                   18790:                                        SetCommState(hComm, &dcb);
1.1.1.25  root     18791:                                }
                   18792:                                
                   18793:                                // check again to apply all comm state changes
                   18794:                                Sleep(10);
                   18795:                                continue;
                   18796:                        }
                   18797:                        
                   18798:                        // set comm pins
                   18799:                        bool change_brk = false;
1.1.1.26  root     18800: //                     bool change_rts = false;
                   18801: //                     bool change_dtr = false;
1.1.1.25  root     18802:                        
                   18803:                        EnterCriticalSection(&q->csModemCtrl);
                   18804:                        if(p->prev_set_brk != p->set_brk) {
                   18805:                                p->prev_set_brk = p->set_brk;
                   18806:                                change_brk = true;
                   18807:                        }
1.1.1.26  root     18808: //                     if(p->prev_set_rts != p->set_rts) {
                   18809: //                             p->prev_set_rts = p->set_rts;
                   18810: //                             change_rts = true;
                   18811: //                     }
                   18812: //                     if(p->prev_set_dtr != p->set_dtr) {
                   18813: //                             p->prev_set_dtr = p->set_dtr;
                   18814: //                             change_dtr = true;
                   18815: //                     }
1.1.1.25  root     18816:                        LeaveCriticalSection(&q->csModemCtrl);
                   18817:                        
                   18818:                        if(change_brk) {
1.1.1.26  root     18819:                                static UINT32 clear_time = 0;
                   18820:                                if(p->prev_set_brk) {
                   18821:                                        EscapeCommFunction(hComm, SETBREAK);
                   18822:                                        clear_time = timeGetTime() + 200;
                   18823:                                } else {
                   18824:                                        // keep break for at least 200msec
                   18825:                                        UINT32 cur_time = timeGetTime();
                   18826:                                        if(clear_time > cur_time) {
                   18827:                                                Sleep(clear_time - cur_time);
                   18828:                                        }
                   18829:                                        EscapeCommFunction(hComm, CLRBREAK);
                   18830:                                }
1.1.1.25  root     18831:                        }
1.1.1.26  root     18832: //                     if(change_rts) {
                   18833: //                             if(p->prev_set_rts) {
                   18834: //                                     EscapeCommFunction(hComm, SETRTS);
                   18835: //                             } else {
                   18836: //                                     EscapeCommFunction(hComm, CLRRTS);
                   18837: //                             }
                   18838: //                     }
                   18839: //                     if(change_dtr) {
                   18840: //                             if(p->prev_set_dtr) {
                   18841: //                                     EscapeCommFunction(hComm, SETDTR);
                   18842: //                             } else {
                   18843: //                                     EscapeCommFunction(hComm, CLRDTR);
                   18844: //                             }
                   18845: //                     }
1.1.1.25  root     18846:                        
                   18847:                        // get comm pins
                   18848:                        DWORD dwModemStat = 0;
                   18849:                        
                   18850:                        if(GetCommModemStatus(hComm, &dwModemStat)) {
                   18851:                                EnterCriticalSection(&q->csModemStat);
                   18852:                                if(dwModemStat & MS_RLSD_ON) {
                   18853:                                        p->modem_stat |= 0x80;
                   18854:                                } else {
                   18855:                                        p->modem_stat &= ~0x80;
                   18856:                                }
                   18857:                                if(dwModemStat & MS_RING_ON) {
                   18858:                                        p->modem_stat |= 0x40;
                   18859:                                } else {
                   18860:                                        p->modem_stat &= ~0x40;
                   18861:                                }
1.1.1.26  root     18862: //                             if(dwModemStat & MS_DSR_ON) {
                   18863: //                                     p->modem_stat |= 0x20;
                   18864: //                             } else {
                   18865: //                                     p->modem_stat &= ~0x20;
                   18866: //                             }
                   18867: //                             if(dwModemStat & MS_CTS_ON) {
                   18868: //                                     p->modem_stat |= 0x10;
                   18869: //                             } else {
                   18870: //                                     p->modem_stat &= ~0x10;
                   18871: //                             }
1.1.1.25  root     18872:                                if((p->prev_modem_stat & 0x80) != (p->modem_stat & 0x80)) {
                   18873:                                        p->modem_stat |= 0x08;
                   18874:                                }
                   18875:                                if((p->prev_modem_stat & 0x40) && !(p->modem_stat & 0x40)) {
                   18876:                                        p->modem_stat |= 0x04;
                   18877:                                }
1.1.1.26  root     18878: //                             if((p->prev_modem_stat & 0x20) != (p->modem_stat & 0x20)) {
                   18879: //                                     p->modem_stat |= 0x02;
                   18880: //                             }
                   18881: //                             if((p->prev_modem_stat & 0x10) != (p->modem_stat & 0x10)) {
                   18882: //                                     p->modem_stat |= 0x01;
                   18883: //                             }
1.1.1.25  root     18884:                                LeaveCriticalSection(&q->csModemStat);
                   18885:                        }
                   18886:                        
                   18887:                        // send data
                   18888:                        DWORD dwSend = 0;
                   18889:                        
                   18890:                        EnterCriticalSection(&q->csSendData);
1.1.1.32  root     18891:                        while(p->send_buffer != NULL && !p->send_buffer->empty()) {
1.1.1.25  root     18892:                                bytBuffer[dwSend++] = p->send_buffer->read();
                   18893:                        }
                   18894:                        LeaveCriticalSection(&q->csSendData);
                   18895:                        
                   18896:                        if(dwSend != 0) {
                   18897:                                DWORD dwWritten = 0;
                   18898:                                WriteFile(hComm, bytBuffer, dwSend, &dwWritten, NULL);
                   18899:                        }
                   18900:                        
                   18901:                        // get line status and recv data
                   18902:                        DWORD dwLineStat = 0;
                   18903:                        COMSTAT comStat;
                   18904:                        
                   18905:                        if(ClearCommError(hComm, &dwLineStat, &comStat)) {
                   18906:                                EnterCriticalSection(&q->csLineStat);
                   18907:                                if(dwLineStat & CE_BREAK) {
                   18908:                                        p->line_stat_err |= 0x10;
                   18909:                                }
                   18910:                                if(dwLineStat & CE_FRAME) {
                   18911:                                        p->line_stat_err |= 0x08;
                   18912:                                }
                   18913:                                if(dwLineStat & CE_RXPARITY) {
                   18914:                                        p->line_stat_err |= 0x04;
                   18915:                                }
                   18916:                                if(dwLineStat & CE_OVERRUN) {
                   18917:                                        p->line_stat_err |= 0x02;
                   18918:                                }
                   18919:                                LeaveCriticalSection(&q->csLineStat);
                   18920:                                
                   18921:                                if(comStat.cbInQue != 0) {
                   18922:                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     18923:                                        DWORD dwRecv = 0;
                   18924:                                        if(p->recv_buffer != NULL) {
                   18925:                                                dwRecv = min(comStat.cbInQue, p->recv_buffer->remain());
                   18926:                                        }
1.1.1.25  root     18927:                                        LeaveCriticalSection(&q->csRecvData);
                   18928:                                        
                   18929:                                        if(dwRecv != 0) {
                   18930:                                                DWORD dwRead = 0;
                   18931:                                                if(ReadFile(hComm, bytBuffer, dwRecv, &dwRead, NULL) && dwRead != 0) {
                   18932:                                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     18933:                                                        if(p->recv_buffer != NULL) {
                   18934:                                                                for(int i = 0; i < dwRead; i++) {
                   18935:                                                                        p->recv_buffer->write(bytBuffer[i]);
                   18936:                                                                }
1.1.1.25  root     18937:                                                        }
                   18938:                                                        LeaveCriticalSection(&q->csRecvData);
                   18939:                                                }
                   18940:                                        }
                   18941:                                }
                   18942:                        }
                   18943:                        Sleep(10);
                   18944:                }
                   18945:                CloseHandle(hComm);
                   18946:        }
                   18947:        return 0;
                   18948: }
                   18949: 
1.1.1.8   root     18950: // cmos
                   18951: 
                   18952: void cmos_init()
                   18953: {
                   18954:        memset(cmos, 0, sizeof(cmos));
                   18955:        cmos_addr = 0;
1.1       root     18956:        
1.1.1.8   root     18957:        // from DOSBox
                   18958:        cmos_write(0x0a, 0x26);
                   18959:        cmos_write(0x0b, 0x02);
                   18960:        cmos_write(0x0d, 0x80);
1.1       root     18961: }
                   18962: 
1.1.1.8   root     18963: void cmos_write(int addr, UINT8 val)
1.1       root     18964: {
1.1.1.8   root     18965:        cmos[addr & 0x7f] = val;
                   18966: }
                   18967: 
                   18968: #define CMOS_GET_TIME() { \
                   18969:        UINT32 cur_sec = timeGetTime() / 1000 ; \
                   18970:        if(prev_sec != cur_sec) { \
                   18971:                GetLocalTime(&time); \
                   18972:                prev_sec = cur_sec; \
                   18973:        } \
1.1       root     18974: }
1.1.1.8   root     18975: #define CMOS_BCD(v) ((cmos[0x0b] & 4) ? (v) : to_bcd(v))
1.1       root     18976: 
1.1.1.8   root     18977: UINT8 cmos_read(int addr)
1.1       root     18978: {
1.1.1.8   root     18979:        static SYSTEMTIME time;
                   18980:        static UINT32 prev_sec = 0;
1.1       root     18981:        
1.1.1.8   root     18982:        switch(addr & 0x7f) {
                   18983:        case 0x00: CMOS_GET_TIME(); return(CMOS_BCD(time.wSecond));
                   18984:        case 0x02: CMOS_GET_TIME(); return(CMOS_BCD(time.wMinute));
                   18985:        case 0x04: CMOS_GET_TIME(); return(CMOS_BCD(time.wHour));
                   18986:        case 0x06: CMOS_GET_TIME(); return(time.wDayOfWeek + 1);
                   18987:        case 0x07: CMOS_GET_TIME(); return(CMOS_BCD(time.wDay));
                   18988:        case 0x08: CMOS_GET_TIME(); return(CMOS_BCD(time.wMonth));
                   18989:        case 0x09: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear));
                   18990: //     case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 2 ? 0x80 : 0));       // 2msec
                   18991:        case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 20 ? 0x80 : 0));      // precision of timeGetTime() may not be 1msec
                   18992:        case 0x15: return((MEMORY_END >> 10) & 0xff);
                   18993:        case 0x16: return((MEMORY_END >> 18) & 0xff);
                   18994:        case 0x17: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   18995:        case 0x18: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   18996:        case 0x30: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   18997:        case 0x31: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   18998:        case 0x32: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear / 100));
1.1       root     18999:        }
1.1.1.8   root     19000:        return(cmos[addr & 0x7f]);
1.1       root     19001: }
                   19002: 
1.1.1.7   root     19003: // kbd (a20)
                   19004: 
                   19005: void kbd_init()
                   19006: {
1.1.1.8   root     19007:        kbd_data = kbd_command = 0;
1.1.1.7   root     19008:        kbd_status = 0x18;
                   19009: }
                   19010: 
                   19011: UINT8 kbd_read_data()
                   19012: {
1.1.1.8   root     19013:        kbd_status &= ~1;
1.1.1.7   root     19014:        return(kbd_data);
                   19015: }
                   19016: 
                   19017: void kbd_write_data(UINT8 val)
                   19018: {
                   19019:        switch(kbd_command) {
                   19020:        case 0xd1:
                   19021:                i386_set_a20_line((val >> 1) & 1);
                   19022:                break;
                   19023:        }
                   19024:        kbd_command = 0;
1.1.1.8   root     19025:        kbd_status &= ~8;
1.1.1.7   root     19026: }
                   19027: 
                   19028: UINT8 kbd_read_status()
                   19029: {
                   19030:        return(kbd_status);
                   19031: }
                   19032: 
                   19033: void kbd_write_command(UINT8 val)
                   19034: {
                   19035:        switch(val) {
                   19036:        case 0xd0:
                   19037:                kbd_data = ((m_a20_mask >> 19) & 2) | 1;
1.1.1.8   root     19038:                kbd_status |= 1;
1.1.1.7   root     19039:                break;
                   19040:        case 0xdd:
                   19041:                i386_set_a20_line(0);
                   19042:                break;
                   19043:        case 0xdf:
                   19044:                i386_set_a20_line(1);
                   19045:                break;
1.1.1.26  root     19046:        case 0xf0: case 0xf1: case 0xf2: case 0xf3: case 0xf4: case 0xf5: case 0xf6: case 0xf7:
                   19047:        case 0xf8: case 0xf9: case 0xfa: case 0xfb: case 0xfc: case 0xfd: case 0xfe: case 0xff:
1.1.1.7   root     19048:                if(!(val & 1)) {
1.1.1.8   root     19049:                        if((cmos[0x0f] & 0x7f) == 5) {
1.1.1.7   root     19050:                                // reset pic
                   19051:                                pic_init();
                   19052:                                pic[0].irr = pic[1].irr = 0x00;
                   19053:                                pic[0].imr = pic[1].imr = 0xff;
                   19054:                        }
                   19055:                        CPU_RESET_CALL(CPU_MODEL);
1.1.1.40  root     19056:                        UINT16 address = *(UINT16 *)(mem + 0x467);
                   19057:                        UINT16 selector = *(UINT16 *)(mem + 0x469);
                   19058:                        i386_jmp_far(selector, address);
1.1.1.7   root     19059:                }
                   19060:                i386_set_a20_line((val >> 1) & 1);
                   19061:                break;
                   19062:        }
                   19063:        kbd_command = val;
1.1.1.8   root     19064:        kbd_status |= 8;
1.1.1.7   root     19065: }
                   19066: 
1.1.1.9   root     19067: // vga
                   19068: 
                   19069: UINT8 vga_read_status()
                   19070: {
                   19071:        // 60hz
                   19072:        static const int period[3] = {16, 17, 17};
                   19073:        static int index = 0;
                   19074:        UINT32 time = timeGetTime() % period[index];
                   19075:        
                   19076:        index = (index + 1) % 3;
1.1.1.14  root     19077:        return((time < 4 ? 0x08 : 0) | (time == 0 ? 0 : 0x01));
1.1.1.9   root     19078: }
                   19079: 
1.1       root     19080: // i/o bus
                   19081: 
1.1.1.29  root     19082: // this is ugly patch for SW1US.EXE, it sometimes mistakely read/write 01h-10h for serial I/O
                   19083: //#define SW1US_PATCH
                   19084: 
1.1.1.25  root     19085: UINT8 read_io_byte(offs_t addr)
1.1.1.33  root     19086: #ifdef USE_DEBUGGER
1.1.1.25  root     19087: {
1.1.1.33  root     19088:        if(now_debugging) {
                   19089:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   19090:                        if(in_break_point.table[i].status == 1) {
                   19091:                                if(addr == in_break_point.table[i].addr) {
                   19092:                                        in_break_point.hit = i + 1;
                   19093:                                        now_suspended = true;
                   19094:                                        break;
                   19095:                                }
                   19096:                        }
                   19097:                }
1.1.1.25  root     19098:        }
1.1.1.33  root     19099:        return(debugger_read_io_byte(addr));
1.1.1.25  root     19100: }
1.1.1.33  root     19101: UINT8 debugger_read_io_byte(offs_t addr)
1.1.1.25  root     19102: #endif
1.1       root     19103: {
1.1.1.33  root     19104:        UINT8 val = 0xff;
                   19105:        
1.1       root     19106:        switch(addr) {
1.1.1.29  root     19107: #ifdef SW1US_PATCH
                   19108:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   19109:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
1.1.1.33  root     19110:                val = sio_read(0, addr - 1);
                   19111:                break;
1.1.1.29  root     19112: #else
1.1.1.25  root     19113:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   19114:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
1.1.1.33  root     19115:                val = dma_read(0, addr);
                   19116:                break;
1.1.1.29  root     19117: #endif
1.1.1.25  root     19118:        case 0x20: case 0x21:
1.1.1.33  root     19119:                val = pic_read(0, addr);
                   19120:                break;
1.1.1.25  root     19121:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1.1.33  root     19122:                val = pit_read(addr & 0x03);
                   19123:                break;
1.1.1.7   root     19124:        case 0x60:
1.1.1.33  root     19125:                val = kbd_read_data();
                   19126:                break;
1.1.1.9   root     19127:        case 0x61:
1.1.1.33  root     19128:                val = system_port;
                   19129:                break;
1.1.1.7   root     19130:        case 0x64:
1.1.1.33  root     19131:                val = kbd_read_status();
                   19132:                break;
1.1       root     19133:        case 0x71:
1.1.1.33  root     19134:                val = cmos_read(cmos_addr);
                   19135:                break;
1.1.1.25  root     19136:        case 0x81:
1.1.1.33  root     19137:                val = dma_page_read(0, 2);
                   19138:                break;
1.1.1.25  root     19139:        case 0x82:
1.1.1.33  root     19140:                val = dma_page_read(0, 3);
                   19141:                break;
1.1.1.25  root     19142:        case 0x83:
1.1.1.33  root     19143:                val = dma_page_read(0, 1);
                   19144:                break;
1.1.1.25  root     19145:        case 0x87:
1.1.1.33  root     19146:                val = dma_page_read(0, 0);
                   19147:                break;
1.1.1.25  root     19148:        case 0x89:
1.1.1.33  root     19149:                val = dma_page_read(1, 2);
                   19150:                break;
1.1.1.25  root     19151:        case 0x8a:
1.1.1.33  root     19152:                val = dma_page_read(1, 3);
                   19153:                break;
1.1.1.25  root     19154:        case 0x8b:
1.1.1.33  root     19155:                val = dma_page_read(1, 1);
                   19156:                break;
1.1.1.25  root     19157:        case 0x8f:
1.1.1.33  root     19158:                val = dma_page_read(1, 0);
                   19159:                break;
1.1       root     19160:        case 0x92:
1.1.1.33  root     19161:                val = (m_a20_mask >> 19) & 2;
                   19162:                break;
1.1.1.25  root     19163:        case 0xa0: case 0xa1:
1.1.1.33  root     19164:                val = pic_read(1, addr);
                   19165:                break;
1.1.1.25  root     19166:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   19167:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.33  root     19168:                val = dma_read(1, (addr - 0xc0) >> 1);
                   19169:                break;
1.1.1.37  root     19170:        case 0x278: case 0x279: case 0x27a:
                   19171:                val = pio_read(1, addr);
                   19172:                break;
1.1.1.29  root     19173:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
1.1.1.33  root     19174:                val = sio_read(3, addr);
                   19175:                break;
1.1.1.25  root     19176:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
1.1.1.33  root     19177:                val = sio_read(1, addr);
                   19178:                break;
1.1.1.25  root     19179:        case 0x378: case 0x379: case 0x37a:
1.1.1.33  root     19180:                val = pio_read(0, addr);
                   19181:                break;
1.1.1.25  root     19182:        case 0x3ba: case 0x3da:
1.1.1.33  root     19183:                val = vga_read_status();
                   19184:                break;
1.1.1.37  root     19185:        case 0x3bc: case 0x3bd: case 0x3be:
                   19186:                val = pio_read(2, addr);
                   19187:                break;
1.1.1.29  root     19188:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
1.1.1.33  root     19189:                val = sio_read(2, addr);
                   19190:                break;
1.1.1.25  root     19191:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
1.1.1.33  root     19192:                val = sio_read(0, addr);
                   19193:                break;
1.1       root     19194:        default:
1.1.1.33  root     19195: //             fatalerror("unknown inb %4x\n", addr);
1.1       root     19196:                break;
                   19197:        }
1.1.1.33  root     19198: #ifdef ENABLE_DEBUG_IOPORT
                   19199:        if(fp_debug_log != NULL) {
                   19200:                fprintf(fp_debug_log, "inb %04X, %02X\n", addr, val);
                   19201:        }
                   19202: #endif
                   19203:        return(val);
1.1       root     19204: }
                   19205: 
                   19206: UINT16 read_io_word(offs_t addr)
                   19207: {
                   19208:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8));
                   19209: }
                   19210: 
1.1.1.33  root     19211: #ifdef USE_DEBUGGER
                   19212: UINT16 debugger_read_io_word(offs_t addr)
                   19213: {
                   19214:        return(debugger_read_io_byte(addr) | (debugger_read_io_byte(addr + 1) << 8));
                   19215: }
                   19216: #endif
                   19217: 
1.1       root     19218: UINT32 read_io_dword(offs_t addr)
                   19219: {
                   19220:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8) | (read_io_byte(addr + 2) << 16) | (read_io_byte(addr + 3) << 24));
                   19221: }
                   19222: 
1.1.1.33  root     19223: #ifdef USE_DEBUGGER
                   19224: UINT32 debugger_read_io_dword(offs_t addr)
                   19225: {
                   19226:        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));
                   19227: }
                   19228: #endif
                   19229: 
1.1       root     19230: void write_io_byte(offs_t addr, UINT8 val)
1.1.1.33  root     19231: #ifdef USE_DEBUGGER
                   19232: {
                   19233:        if(now_debugging) {
                   19234:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   19235:                        if(out_break_point.table[i].status == 1) {
                   19236:                                if(addr == out_break_point.table[i].addr) {
                   19237:                                        out_break_point.hit = i + 1;
                   19238:                                        now_suspended = true;
                   19239:                                        break;
                   19240:                                }
                   19241:                        }
                   19242:                }
                   19243:        }
                   19244:        debugger_write_io_byte(addr, val);
                   19245: }
                   19246: void debugger_write_io_byte(offs_t addr, UINT8 val)
                   19247: #endif
1.1       root     19248: {
1.1.1.25  root     19249: #ifdef ENABLE_DEBUG_IOPORT
1.1.1.33  root     19250:        if(fp_debug_log != NULL) {
1.1.1.43  root     19251: #ifdef USE_SERVICE_THREAD
                   19252:                if(addr != 0xf7)
                   19253: #endif
1.1.1.33  root     19254:                fprintf(fp_debug_log, "outb %04X, %02X\n", addr, val);
1.1.1.25  root     19255:        }
                   19256: #endif
1.1       root     19257:        switch(addr) {
1.1.1.29  root     19258: #ifdef SW1US_PATCH
                   19259:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   19260:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
                   19261:                sio_write(0, addr - 1, val);
                   19262:                break;
                   19263: #else
1.1.1.25  root     19264:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   19265:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
                   19266:                dma_write(0, addr, val);
                   19267:                break;
1.1.1.29  root     19268: #endif
1.1.1.25  root     19269:        case 0x20: case 0x21:
1.1       root     19270:                pic_write(0, addr, val);
                   19271:                break;
1.1.1.25  root     19272:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1       root     19273:                pit_write(addr & 0x03, val);
                   19274:                break;
1.1.1.7   root     19275:        case 0x60:
                   19276:                kbd_write_data(val);
                   19277:                break;
1.1.1.9   root     19278:        case 0x61:
                   19279:                if((system_port & 3) != 3 && (val & 3) == 3) {
                   19280:                        // beep on
                   19281: //                     MessageBeep(-1);
                   19282:                } else if((system_port & 3) == 3 && (val & 3) != 3) {
                   19283:                        // beep off
                   19284:                }
                   19285:                system_port = val;
                   19286:                break;
1.1       root     19287:        case 0x64:
1.1.1.7   root     19288:                kbd_write_command(val);
1.1       root     19289:                break;
                   19290:        case 0x70:
                   19291:                cmos_addr = val;
                   19292:                break;
                   19293:        case 0x71:
1.1.1.8   root     19294:                cmos_write(cmos_addr, val);
1.1       root     19295:                break;
1.1.1.25  root     19296:        case 0x81:
                   19297:                dma_page_write(0, 2, val);
                   19298:        case 0x82:
                   19299:                dma_page_write(0, 3, val);
                   19300:        case 0x83:
                   19301:                dma_page_write(0, 1, val);
                   19302:        case 0x87:
                   19303:                dma_page_write(0, 0, val);
                   19304:        case 0x89:
                   19305:                dma_page_write(1, 2, val);
                   19306:        case 0x8a:
                   19307:                dma_page_write(1, 3, val);
                   19308:        case 0x8b:
                   19309:                dma_page_write(1, 1, val);
                   19310:        case 0x8f:
                   19311:                dma_page_write(1, 0, val);
1.1       root     19312:        case 0x92:
1.1.1.7   root     19313:                i386_set_a20_line((val >> 1) & 1);
1.1       root     19314:                break;
1.1.1.25  root     19315:        case 0xa0: case 0xa1:
1.1       root     19316:                pic_write(1, addr, val);
                   19317:                break;
1.1.1.25  root     19318:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   19319:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.26  root     19320:                dma_write(1, (addr - 0xc0) >> 1, val);
1.1.1.25  root     19321:                break;
1.1.1.35  root     19322: #ifdef USE_SERVICE_THREAD
                   19323:        case 0xf7:
                   19324:                // dummy i/o for BIOS/DOS service
1.1.1.36  root     19325:                if(in_service && cursor_moved) {
                   19326:                        // update cursor position before service is done
                   19327:                        pcbios_update_cursor_position();
                   19328:                        cursor_moved = false;
                   19329:                }
1.1.1.35  root     19330:                finish_service_loop();
                   19331:                break;
                   19332: #endif
1.1.1.37  root     19333:        case 0x278: case 0x279: case 0x27a:
                   19334:                pio_write(1, addr, val);
                   19335:                break;
1.1.1.29  root     19336:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
                   19337:                sio_write(3, addr, val);
                   19338:                break;
1.1.1.25  root     19339:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
                   19340:                sio_write(1, addr, val);
                   19341:                break;
                   19342:        case 0x378: case 0x379: case 0x37a:
                   19343:                pio_write(0, addr, val);
                   19344:                break;
1.1.1.37  root     19345:        case 0x3bc: case 0x3bd: case 0x3be:
                   19346:                pio_write(2, addr, val);
                   19347:                break;
1.1.1.29  root     19348:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
                   19349:                sio_write(2, addr, val);
                   19350:                break;
1.1.1.25  root     19351:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
                   19352:                sio_write(0, addr, val);
                   19353:                break;
1.1       root     19354:        default:
1.1.1.33  root     19355: //             fatalerror("unknown outb %4x,%2x\n", addr, val);
1.1       root     19356:                break;
                   19357:        }
                   19358: }
                   19359: 
                   19360: void write_io_word(offs_t addr, UINT16 val)
                   19361: {
                   19362:        write_io_byte(addr + 0, (val >> 0) & 0xff);
                   19363:        write_io_byte(addr + 1, (val >> 8) & 0xff);
                   19364: }
                   19365: 
1.1.1.33  root     19366: #ifdef USE_DEBUGGER
                   19367: void debugger_write_io_word(offs_t addr, UINT16 val)
                   19368: {
                   19369:        debugger_write_io_byte(addr + 0, (val >> 0) & 0xff);
                   19370:        debugger_write_io_byte(addr + 1, (val >> 8) & 0xff);
                   19371: }
                   19372: #endif
                   19373: 
1.1       root     19374: void write_io_dword(offs_t addr, UINT32 val)
                   19375: {
                   19376:        write_io_byte(addr + 0, (val >>  0) & 0xff);
                   19377:        write_io_byte(addr + 1, (val >>  8) & 0xff);
                   19378:        write_io_byte(addr + 2, (val >> 16) & 0xff);
                   19379:        write_io_byte(addr + 3, (val >> 24) & 0xff);
                   19380: }
1.1.1.33  root     19381: 
                   19382: #ifdef USE_DEBUGGER
                   19383: void debugger_write_io_dword(offs_t addr, UINT32 val)
                   19384: {
                   19385:        debugger_write_io_byte(addr + 0, (val >>  0) & 0xff);
                   19386:        debugger_write_io_byte(addr + 1, (val >>  8) & 0xff);
                   19387:        debugger_write_io_byte(addr + 2, (val >> 16) & 0xff);
                   19388:        debugger_write_io_byte(addr + 3, (val >> 24) & 0xff);
                   19389: }
                   19390: #endif

unix.superglobalmegacorp.com

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