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

1.1       root        1: /*
                      2:        MS-DOS Player for Win32 console
                      3: 
                      4:        Author : Takeda.Toshiya
                      5:        Date   : 2009.11.09-
                      6: */
                      7: 
                      8: #include "msdos.h"
                      9: 
1.1.1.28  root       10: void exit_handler();
                     11: 
1.1       root       12: #define fatalerror(...) { \
                     13:        fprintf(stderr, __VA_ARGS__); \
1.1.1.28  root       14:        exit_handler(); \
1.1       root       15:        exit(1); \
                     16: }
                     17: #define error(...) fprintf(stderr, "error: " __VA_ARGS__)
1.1.1.22  root       18: #define nolog(...)
                     19: 
1.1.1.33  root       20: //#define ENABLE_DEBUG_LOG
                     21: #ifdef ENABLE_DEBUG_LOG
1.1.1.22  root       22:        #define EXPORT_DEBUG_TO_FILE
                     23:        #define ENABLE_DEBUG_SYSCALL
                     24:        #define ENABLE_DEBUG_UNIMPLEMENTED
1.1.1.25  root       25:        #define ENABLE_DEBUG_IOPORT
1.1.1.22  root       26:        
                     27:        #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root       28:                FILE* fp_debug_log = NULL;
1.1.1.22  root       29:        #else
1.1.1.33  root       30:                #define fp_debug_log stderr
1.1.1.22  root       31:        #endif
                     32:        #ifdef ENABLE_DEBUG_UNIMPLEMENTED
                     33:                #define unimplemented_10h fatalerror
1.1.1.42  root       34:                #define unimplemented_13h fatalerror
1.1.1.25  root       35:                #define unimplemented_14h fatalerror
1.1.1.22  root       36:                #define unimplemented_15h fatalerror
                     37:                #define unimplemented_16h fatalerror
1.1.1.37  root       38:                #define unimplemented_17h fatalerror
1.1.1.22  root       39:                #define unimplemented_1ah fatalerror
                     40:                #define unimplemented_21h fatalerror
                     41:                #define unimplemented_2fh fatalerror
1.1.1.24  root       42:                #define unimplemented_33h fatalerror
1.1.1.22  root       43:                #define unimplemented_67h fatalerror
                     44:                #define unimplemented_xms fatalerror
                     45:        #endif
                     46: #endif
                     47: #ifndef unimplemented_10h
                     48:        #define unimplemented_10h nolog
                     49: #endif
1.1.1.42  root       50: #ifndef unimplemented_13h
                     51:        #define unimplemented_13h nolog
                     52: #endif
1.1.1.25  root       53: #ifndef unimplemented_14h
                     54:        #define unimplemented_14h nolog
                     55: #endif
1.1.1.22  root       56: #ifndef unimplemented_15h
                     57:        #define unimplemented_15h nolog
                     58: #endif
                     59: #ifndef unimplemented_16h
                     60:        #define unimplemented_16h nolog
                     61: #endif
1.1.1.37  root       62: #ifndef unimplemented_17h
                     63:        #define unimplemented_17h nolog
                     64: #endif
1.1.1.22  root       65: #ifndef unimplemented_1ah
                     66:        #define unimplemented_1ah nolog
                     67: #endif
                     68: #ifndef unimplemented_21h
                     69:        #define unimplemented_21h nolog
                     70: #endif
                     71: #ifndef unimplemented_2fh
                     72:        #define unimplemented_2fh nolog
                     73: #endif
1.1.1.24  root       74: #ifndef unimplemented_33h
                     75:        #define unimplemented_33h nolog
                     76: #endif
1.1.1.22  root       77: #ifndef unimplemented_67h
                     78:        #define unimplemented_67h nolog
                     79: #endif
                     80: #ifndef unimplemented_xms
                     81:        #define unimplemented_xms nolog
                     82: #endif
                     83: 
1.1.1.32  root       84: #define array_length(array) (sizeof(array) / sizeof(array[0]))
1.1.1.22  root       85: #define my_strchr(str, chr) (char *)_mbschr((unsigned char *)(str), (unsigned int)(chr))
                     86: #define my_strtok(tok, del) (char *)_mbstok((unsigned char *)(tok), (const unsigned char *)(del))
                     87: #define my_strupr(str) (char *)_mbsupr((unsigned char *)(str))
1.1       root       88: 
1.1.1.12  root       89: #if defined(__MINGW32__)
                     90: extern "C" int _CRT_glob = 0;
                     91: #endif
                     92: 
                     93: /*
                     94:        kludge for "more-standardized" C++
                     95: */
                     96: #if !defined(_MSC_VER)
                     97: inline int kludge_min(int a, int b) { return (a<b ? a:b); }
                     98: inline int kludge_max(int a, int b) { return (a>b ? a:b); }
                     99: #define min(a,b) kludge_min(a,b)
                    100: #define max(a,b) kludge_max(a,b)
1.1.1.14  root      101: #elif _MSC_VER >= 1400
                    102: void ignore_invalid_parameters(const wchar_t *, const wchar_t *, const wchar_t *, unsigned int, uintptr_t)
                    103: {
                    104: }
                    105: #endif
                    106: 
1.1.1.35  root      107: #define USE_VRAM_THREAD
1.1.1.14  root      108: 
1.1.1.35  root      109: #ifdef USE_VRAM_THREAD
1.1.1.14  root      110: static CRITICAL_SECTION vram_crit_sect;
                    111: #else
                    112: #define vram_flush()
1.1.1.12  root      113: #endif
                    114: 
1.1.1.14  root      115: #define VIDEO_REGEN *(UINT16 *)(mem + 0x44c)
                    116: #define SCR_BUF(y,x) scr_buf[(y) * scr_buf_size.X + (x)]
                    117: 
                    118: void change_console_size(int width, int height);
                    119: void clear_scr_buffer(WORD attr);
                    120: 
                    121: static UINT32 vram_length_char = 0, vram_length_attr = 0;
                    122: static UINT32 vram_last_length_char = 0, vram_last_length_attr = 0;
                    123: static COORD vram_coord_char, vram_coord_attr;
                    124: 
1.1.1.28  root      125: char temp_file_path[MAX_PATH];
                    126: bool temp_file_created = false;
                    127: 
1.1.1.14  root      128: bool ignore_illegal_insn = false;
                    129: bool limit_max_memory = false;
                    130: bool no_windows = false;
                    131: bool stay_busy = false;
1.1.1.19  root      132: bool support_ems = false;
                    133: #ifdef SUPPORT_XMS
                    134: bool support_xms = false;
                    135: #endif
1.1.1.29  root      136: int sio_port_number[4] = {0, 0, 0, 0};
1.1.1.14  root      137: 
                    138: BOOL is_vista_or_later;
                    139: 
1.1.1.35  root      140: #define UPDATE_OPS 16384
                    141: #define REQUEST_HARDWRE_UPDATE() { \
                    142:        update_ops = UPDATE_OPS - 1; \
                    143: }
                    144: UINT32 update_ops = 0;
                    145: UINT32 idle_ops = 0;
                    146: 
1.1.1.14  root      147: inline void maybe_idle()
                    148: {
                    149:        // if it appears to be in a tight loop, assume waiting for input
                    150:        // allow for one updated video character, for a spinning cursor
1.1.1.35  root      151:        if(!stay_busy && idle_ops < 1024 && vram_length_char <= 1 && vram_length_attr <= 1) {
1.1.1.14  root      152:                Sleep(10);
1.1.1.35  root      153:                REQUEST_HARDWRE_UPDATE();
1.1.1.14  root      154:        }
1.1.1.35  root      155:        idle_ops = 0;
1.1.1.14  root      156: }
1.1.1.12  root      157: 
1.1       root      158: /* ----------------------------------------------------------------------------
1.1.1.3   root      159:        MAME i86/i386
1.1       root      160: ---------------------------------------------------------------------------- */
                    161: 
1.1.1.10  root      162: #ifndef __BIG_ENDIAN__
1.1       root      163: #define LSB_FIRST
1.1.1.10  root      164: #endif
1.1       root      165: 
                    166: #ifndef INLINE
                    167: #define INLINE inline
                    168: #endif
                    169: #define U64(v) UINT64(v)
                    170: 
                    171: //#define logerror(...) fprintf(stderr, __VA_ARGS__)
                    172: #define logerror(...)
                    173: //#define popmessage(...) fprintf(stderr, __VA_ARGS__)
                    174: #define popmessage(...)
                    175: 
                    176: /*****************************************************************************/
1.1.1.10  root      177: /* src/emu/devcpu.h */
                    178: 
                    179: // CPU interface functions
                    180: #define CPU_INIT_NAME(name)                    cpu_init_##name
                    181: #define CPU_INIT(name)                         void CPU_INIT_NAME(name)()
                    182: #define CPU_INIT_CALL(name)                    CPU_INIT_NAME(name)()
                    183: 
                    184: #define CPU_RESET_NAME(name)                   cpu_reset_##name
                    185: #define CPU_RESET(name)                                void CPU_RESET_NAME(name)()
                    186: #define CPU_RESET_CALL(name)                   CPU_RESET_NAME(name)()
                    187: 
                    188: #define CPU_EXECUTE_NAME(name)                 cpu_execute_##name
                    189: #define CPU_EXECUTE(name)                      void CPU_EXECUTE_NAME(name)()
                    190: #define CPU_EXECUTE_CALL(name)                 CPU_EXECUTE_NAME(name)()
                    191: 
                    192: #define CPU_TRANSLATE_NAME(name)               cpu_translate_##name
                    193: #define CPU_TRANSLATE(name)                    int CPU_TRANSLATE_NAME(name)(address_spacenum space, int intention, offs_t *address)
                    194: #define CPU_TRANSLATE_CALL(name)               CPU_TRANSLATE_NAME(name)(space, intention, address)
                    195: 
                    196: #define CPU_DISASSEMBLE_NAME(name)             cpu_disassemble_##name
                    197: #define CPU_DISASSEMBLE(name)                  int CPU_DISASSEMBLE_NAME(name)(char *buffer, offs_t eip, const UINT8 *oprom)
                    198: #define CPU_DISASSEMBLE_CALL(name)             CPU_DISASSEMBLE_NAME(name)(buffer, eip, oprom)
                    199: 
1.1.1.14  root      200: #define CPU_MODEL_STR(name)                    #name
                    201: #define CPU_MODEL_NAME(name)                   CPU_MODEL_STR(name)
                    202: 
1.1.1.10  root      203: /*****************************************************************************/
                    204: /* src/emu/didisasm.h */
                    205: 
                    206: // Disassembler constants
                    207: const UINT32 DASMFLAG_SUPPORTED     = 0x80000000;   // are disassembly flags supported?
                    208: const UINT32 DASMFLAG_STEP_OUT      = 0x40000000;   // this instruction should be the end of a step out sequence
                    209: const UINT32 DASMFLAG_STEP_OVER     = 0x20000000;   // this instruction should be stepped over by setting a breakpoint afterwards
                    210: const UINT32 DASMFLAG_OVERINSTMASK  = 0x18000000;   // number of extra instructions to skip when stepping over
                    211: const UINT32 DASMFLAG_OVERINSTSHIFT = 27;           // bits to shift after masking to get the value
                    212: const UINT32 DASMFLAG_LENGTHMASK    = 0x0000ffff;   // the low 16-bits contain the actual length
                    213: 
                    214: /*****************************************************************************/
1.1       root      215: /* src/emu/diexec.h */
                    216: 
                    217: // I/O line states
                    218: enum line_state
                    219: {
                    220:        CLEAR_LINE = 0,                         // clear (a fired or held) line
                    221:        ASSERT_LINE,                            // assert an interrupt immediately
                    222:        HOLD_LINE,                              // hold interrupt line until acknowledged
                    223:        PULSE_LINE                              // pulse interrupt line instantaneously (only for NMI, RESET)
                    224: };
                    225: 
                    226: // I/O line definitions
                    227: enum
                    228: {
                    229:        INPUT_LINE_IRQ = 0,
                    230:        INPUT_LINE_NMI
                    231: };
                    232: 
                    233: /*****************************************************************************/
1.1.1.10  root      234: /* src/emu/dimemory.h */
1.1       root      235: 
1.1.1.10  root      236: // Translation intentions
                    237: const int TRANSLATE_TYPE_MASK       = 0x03;     // read write or fetch
                    238: const int TRANSLATE_USER_MASK       = 0x04;     // user mode or fully privileged
                    239: const int TRANSLATE_DEBUG_MASK      = 0x08;     // debug mode (no side effects)
                    240: 
                    241: const int TRANSLATE_READ            = 0;        // translate for read
                    242: const int TRANSLATE_WRITE           = 1;        // translate for write
                    243: const int TRANSLATE_FETCH           = 2;        // translate for instruction fetch
                    244: const int TRANSLATE_READ_USER       = (TRANSLATE_READ | TRANSLATE_USER_MASK);
                    245: const int TRANSLATE_WRITE_USER      = (TRANSLATE_WRITE | TRANSLATE_USER_MASK);
                    246: const int TRANSLATE_FETCH_USER      = (TRANSLATE_FETCH | TRANSLATE_USER_MASK);
                    247: const int TRANSLATE_READ_DEBUG      = (TRANSLATE_READ | TRANSLATE_DEBUG_MASK);
                    248: const int TRANSLATE_WRITE_DEBUG     = (TRANSLATE_WRITE | TRANSLATE_DEBUG_MASK);
                    249: const int TRANSLATE_FETCH_DEBUG     = (TRANSLATE_FETCH | TRANSLATE_DEBUG_MASK);
1.1       root      250: 
1.1.1.10  root      251: /*****************************************************************************/
                    252: /* src/emu/emucore.h */
1.1       root      253: 
1.1.1.10  root      254: // constants for expression endianness
                    255: enum endianness_t
                    256: {
                    257:        ENDIANNESS_LITTLE,
                    258:        ENDIANNESS_BIG
                    259: };
1.1       root      260: 
1.1.1.10  root      261: // declare native endianness to be one or the other
                    262: #ifdef LSB_FIRST
                    263: const endianness_t ENDIANNESS_NATIVE = ENDIANNESS_LITTLE;
                    264: #else
                    265: const endianness_t ENDIANNESS_NATIVE = ENDIANNESS_BIG;
                    266: #endif
                    267: 
                    268: // endian-based value: first value is if 'endian' is little-endian, second is if 'endian' is big-endian
                    269: #define ENDIAN_VALUE_LE_BE(endian,leval,beval) (((endian) == ENDIANNESS_LITTLE) ? (leval) : (beval))
                    270: 
                    271: // endian-based value: first value is if native endianness is little-endian, second is if native is big-endian
                    272: #define NATIVE_ENDIAN_VALUE_LE_BE(leval,beval) ENDIAN_VALUE_LE_BE(ENDIANNESS_NATIVE, leval, beval)
                    273: 
                    274: // endian-based value: first value is if 'endian' matches native, second is if 'endian' doesn't match native
                    275: #define ENDIAN_VALUE_NE_NNE(endian,leval,beval)        (((endian) == ENDIANNESS_NATIVE) ? (neval) : (nneval))
1.1       root      276: 
                    277: /*****************************************************************************/
                    278: /* src/emu/memory.h */
                    279: 
1.1.1.10  root      280: // address spaces
                    281: enum address_spacenum
                    282: {
                    283:        AS_0,                           // first address space
                    284:        AS_1,                           // second address space
                    285:        AS_2,                           // third address space
                    286:        AS_3,                           // fourth address space
                    287:        ADDRESS_SPACES,                 // maximum number of address spaces
                    288: 
                    289:        // alternate address space names for common use
                    290:        AS_PROGRAM = AS_0,              // program address space
                    291:        AS_DATA = AS_1,                 // data address space
                    292:        AS_IO = AS_2                    // I/O address space
                    293: };
                    294: 
1.1       root      295: // offsets and addresses are 32-bit (for now...)
1.1.1.30  root      296: //typedef UINT32       offs_t;
1.1       root      297: 
                    298: // read accessors
                    299: UINT8 read_byte(offs_t byteaddress)
1.1.1.33  root      300: #ifdef USE_DEBUGGER
                    301: {
                    302:        if(now_debugging) {
                    303:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    304:                        if(rd_break_point.table[i].status == 1) {
                    305:                                if(byteaddress == rd_break_point.table[i].addr) {
                    306:                                        rd_break_point.hit = i + 1;
                    307:                                        now_suspended = true;
                    308:                                        break;
                    309:                                }
                    310:                        }
                    311:                }
                    312:        }
                    313:        return(debugger_read_byte(byteaddress));
                    314: }
                    315: UINT8 debugger_read_byte(offs_t byteaddress)
                    316: #endif
1.1       root      317: {
1.1.1.4   root      318: #if defined(HAS_I386)
1.1       root      319:        if(byteaddress < MAX_MEM) {
                    320:                return mem[byteaddress];
1.1.1.3   root      321: //     } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
                    322: //             return read_byte(byteaddress & 0xfffff);
1.1       root      323:        }
                    324:        return 0;
1.1.1.4   root      325: #else
                    326:        return mem[byteaddress];
                    327: #endif
1.1       root      328: }
                    329: 
                    330: UINT16 read_word(offs_t byteaddress)
1.1.1.33  root      331: #ifdef USE_DEBUGGER
                    332: {
                    333:        if(now_debugging) {
                    334:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    335:                        if(rd_break_point.table[i].status == 1) {
                    336:                                if(byteaddress >= rd_break_point.table[i].addr && byteaddress < rd_break_point.table[i].addr + 2) {
                    337:                                        rd_break_point.hit = i + 1;
                    338:                                        now_suspended = true;
                    339:                                        break;
                    340:                                }
                    341:                        }
                    342:                }
                    343:        }
                    344:        return(debugger_read_word(byteaddress));
                    345: }
                    346: UINT16 debugger_read_word(offs_t byteaddress)
                    347: #endif
1.1       root      348: {
1.1.1.14  root      349:        if(byteaddress == 0x41c) {
                    350:                // pointer to first free slot in keyboard buffer
                    351:                // XXX: the buffer itself doesn't actually exist in DOS memory
1.1.1.35  root      352:                if(key_buf_char != NULL && key_buf_scan != NULL) {
                    353: #ifdef USE_SERVICE_THREAD
                    354:                        EnterCriticalSection(&key_buf_crit_sect);
                    355: #endif
1.1.1.41  root      356:                        int count = min(key_buf_char->count(), 15);
                    357:                        // write to top of key buffer
                    358:                        for(int i = 0; i < count; i++) {
                    359:                                mem[0x41e + 2 * i + 0] = key_buf_char->read_not_remove(i);
                    360:                                mem[0x41e + 2 * i + 1] = key_buf_scan->read_not_remove(i);
                    361:                        }
1.1.1.35  root      362: #ifdef USE_SERVICE_THREAD
                    363:                        LeaveCriticalSection(&key_buf_crit_sect);
                    364: #endif
                    365:                        if(count == 0) {
1.1.1.32  root      366:                                maybe_idle();
                    367:                        }
1.1.1.41  root      368:                        return (UINT16)(0x1e + 2 * count);
1.1.1.14  root      369:                }
1.1.1.41  root      370:                return 0x1e;
1.1.1.14  root      371:        }
1.1.1.4   root      372: #if defined(HAS_I386)
1.1       root      373:        if(byteaddress < MAX_MEM - 1) {
                    374:                return *(UINT16 *)(mem + byteaddress);
1.1.1.3   root      375: //     } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
                    376: //             return read_word(byteaddress & 0xfffff);
1.1       root      377:        }
                    378:        return 0;
1.1.1.4   root      379: #else
                    380:        return *(UINT16 *)(mem + byteaddress);
                    381: #endif
1.1       root      382: }
                    383: 
                    384: UINT32 read_dword(offs_t byteaddress)
1.1.1.33  root      385: #ifdef USE_DEBUGGER
                    386: {
                    387:        if(now_debugging) {
                    388:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    389:                        if(rd_break_point.table[i].status == 1) {
                    390:                                if(byteaddress >= rd_break_point.table[i].addr && byteaddress < rd_break_point.table[i].addr + 4) {
                    391:                                        rd_break_point.hit = i + 1;
                    392:                                        now_suspended = true;
                    393:                                        break;
                    394:                                }
                    395:                        }
                    396:                }
                    397:        }
                    398:        return(debugger_read_dword(byteaddress));
                    399: }
                    400: UINT32 debugger_read_dword(offs_t byteaddress)
                    401: #endif
1.1       root      402: {
1.1.1.4   root      403: #if defined(HAS_I386)
1.1       root      404:        if(byteaddress < MAX_MEM - 3) {
                    405:                return *(UINT32 *)(mem + byteaddress);
1.1.1.3   root      406: //     } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
                    407: //             return read_dword(byteaddress & 0xfffff);
1.1       root      408:        }
                    409:        return 0;
1.1.1.4   root      410: #else
                    411:        return *(UINT32 *)(mem + byteaddress);
                    412: #endif
1.1       root      413: }
                    414: 
                    415: // write accessors
1.1.1.35  root      416: #ifdef USE_VRAM_THREAD
1.1.1.14  root      417: void vram_flush_char()
                    418: {
                    419:        if(vram_length_char != 0) {
                    420:                DWORD num;
1.1.1.23  root      421:                WriteConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), scr_char, vram_length_char, vram_coord_char, &num);
1.1.1.14  root      422:                vram_length_char = vram_last_length_char = 0;
                    423:        }
                    424: }
                    425: 
                    426: void vram_flush_attr()
                    427: {
                    428:        if(vram_length_attr != 0) {
                    429:                DWORD num;
1.1.1.23  root      430:                WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), scr_attr, vram_length_attr, vram_coord_attr, &num);
1.1.1.14  root      431:                vram_length_attr = vram_last_length_attr = 0;
                    432:        }
                    433: }
                    434: 
                    435: void vram_flush()
                    436: {
                    437:        if(vram_length_char != 0 || vram_length_attr != 0) {
                    438:                EnterCriticalSection(&vram_crit_sect);
                    439:                vram_flush_char();
                    440:                vram_flush_attr();
                    441:                LeaveCriticalSection(&vram_crit_sect);
                    442:        }
                    443: }
                    444: #endif
                    445: 
                    446: void write_text_vram_char(offs_t offset, UINT8 data)
1.1.1.8   root      447: {
1.1.1.35  root      448: #ifdef USE_VRAM_THREAD
1.1.1.14  root      449:        static offs_t first_offset_char, last_offset_char;
                    450:        
                    451:        if(vram_length_char != 0) {
                    452:                if(offset <= last_offset_char && offset >= first_offset_char) {
                    453:                        scr_char[(offset - first_offset_char) >> 1] = data;
                    454:                        return;
                    455:                }
                    456:                if(offset != last_offset_char + 2) {
                    457:                        vram_flush_char();
                    458:                }
                    459:        }
                    460:        if(vram_length_char == 0) {
                    461:                first_offset_char = offset;
                    462:                vram_coord_char.X = (offset >> 1) % scr_width;
                    463:                vram_coord_char.Y = (offset >> 1) / scr_width + scr_top;
                    464:        }
                    465:        scr_char[vram_length_char++] = data;
                    466:        last_offset_char = offset;
                    467: #else
1.1.1.8   root      468:        COORD co;
                    469:        DWORD num;
                    470:        
1.1.1.14  root      471:        co.X = (offset >> 1) % scr_width;
                    472:        co.Y = (offset >> 1) / scr_width;
                    473:        scr_char[0] = data;
1.1.1.23  root      474:        WriteConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), scr_char, 1, co, &num);
1.1.1.14  root      475: #endif
                    476: }
                    477: 
                    478: void write_text_vram_attr(offs_t offset, UINT8 data)
                    479: {
1.1.1.35  root      480: #ifdef USE_VRAM_THREAD
1.1.1.14  root      481:        static offs_t first_offset_attr, last_offset_attr;
                    482:        
                    483:        if(vram_length_attr != 0) {
                    484:                if(offset <= last_offset_attr && offset >= first_offset_attr) {
                    485:                        scr_attr[(offset - first_offset_attr) >> 1] = data;
                    486:                        return;
                    487:                }
                    488:                if(offset != last_offset_attr + 2) {
                    489:                        vram_flush_attr();
                    490:                }
                    491:        }
                    492:        if(vram_length_attr == 0) {
                    493:                first_offset_attr = offset;
                    494:                vram_coord_attr.X = (offset >> 1) % scr_width;
                    495:                vram_coord_attr.Y = (offset >> 1) / scr_width + scr_top;
                    496:        }
                    497:        scr_attr[vram_length_attr++] = data;
                    498:        last_offset_attr = offset;
                    499: #else
                    500:        COORD co;
                    501:        DWORD num;
1.1.1.8   root      502:        
1.1.1.14  root      503:        co.X = (offset >> 1) % scr_width;
                    504:        co.Y = (offset >> 1) / scr_width;
                    505:        scr_attr[0] = data;
1.1.1.23  root      506:        WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), scr_attr, 1, co, &num);
1.1.1.14  root      507: #endif
                    508: }
                    509: 
                    510: void write_text_vram_byte(offs_t offset, UINT8 data)
                    511: {
1.1.1.35  root      512: #ifdef USE_VRAM_THREAD
1.1.1.14  root      513:        EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root      514: #endif
1.1.1.8   root      515:        if(offset & 1) {
1.1.1.14  root      516:                write_text_vram_attr(offset, data);
1.1.1.8   root      517:        } else {
1.1.1.14  root      518:                write_text_vram_char(offset, data);
1.1.1.8   root      519:        }
1.1.1.35  root      520: #ifdef USE_VRAM_THREAD
1.1.1.14  root      521:        LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root      522: #endif
1.1.1.8   root      523: }
                    524: 
                    525: void write_text_vram_word(offs_t offset, UINT16 data)
                    526: {
1.1.1.35  root      527: #ifdef USE_VRAM_THREAD
1.1.1.14  root      528:        EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root      529: #endif
1.1.1.8   root      530:        if(offset & 1) {
1.1.1.14  root      531:                write_text_vram_attr(offset    , (data     ) & 0xff);
                    532:                write_text_vram_char(offset + 1, (data >> 8) & 0xff);
1.1.1.8   root      533:        } else {
1.1.1.14  root      534:                write_text_vram_char(offset    , (data     ) & 0xff);
                    535:                write_text_vram_attr(offset + 1, (data >> 8) & 0xff);
1.1.1.8   root      536:        }
1.1.1.35  root      537: #ifdef USE_VRAM_THREAD
1.1.1.14  root      538:        LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root      539: #endif
1.1.1.8   root      540: }
                    541: 
                    542: void write_text_vram_dword(offs_t offset, UINT32 data)
                    543: {
1.1.1.35  root      544: #ifdef USE_VRAM_THREAD
1.1.1.14  root      545:        EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root      546: #endif
1.1.1.8   root      547:        if(offset & 1) {
1.1.1.14  root      548:                write_text_vram_attr(offset    , (data      ) & 0xff);
                    549:                write_text_vram_char(offset + 1, (data >>  8) & 0xff);
                    550:                write_text_vram_attr(offset + 2, (data >> 16) & 0xff);
                    551:                write_text_vram_char(offset + 3, (data >> 24) & 0xff);
                    552:        } else {
                    553:                write_text_vram_char(offset    , (data      ) & 0xff);
                    554:                write_text_vram_attr(offset + 1, (data >>  8) & 0xff);
                    555:                write_text_vram_char(offset + 2, (data >> 16) & 0xff);
                    556:                write_text_vram_attr(offset + 3, (data >> 24) & 0xff);
1.1.1.8   root      557:        }
1.1.1.35  root      558: #ifdef USE_VRAM_THREAD
1.1.1.14  root      559:        LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root      560: #endif
1.1.1.8   root      561: }
                    562: 
1.1       root      563: void write_byte(offs_t byteaddress, UINT8 data)
1.1.1.33  root      564: #ifdef USE_DEBUGGER
                    565: {
                    566:        if(now_debugging) {
                    567:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    568:                        if(wr_break_point.table[i].status == 1) {
                    569:                                if(byteaddress == wr_break_point.table[i].addr) {
                    570:                                        wr_break_point.hit = i + 1;
                    571:                                        now_suspended = true;
                    572:                                        break;
                    573:                                }
                    574:                        }
                    575:                }
                    576:        }
                    577:        debugger_write_byte(byteaddress, data);
                    578: }
                    579: void debugger_write_byte(offs_t byteaddress, UINT8 data)
                    580: #endif
1.1       root      581: {
1.1.1.8   root      582:        if(byteaddress < MEMORY_END) {
1.1.1.3   root      583:                mem[byteaddress] = data;
1.1.1.8   root      584:        } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14  root      585:                if(!restore_console_on_exit) {
                    586:                        change_console_size(scr_width, scr_height);
1.1.1.12  root      587:                }
1.1.1.8   root      588:                write_text_vram_byte(byteaddress - text_vram_top_address, data);
                    589:                mem[byteaddress] = data;
                    590:        } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
                    591:                if(int_10h_feh_called && !int_10h_ffh_called) {
                    592:                        write_text_vram_byte(byteaddress - shadow_buffer_top_address, data);
1.1       root      593:                }
                    594:                mem[byteaddress] = data;
1.1.1.4   root      595: #if defined(HAS_I386)
1.1.1.3   root      596:        } else if(byteaddress < MAX_MEM) {
1.1.1.4   root      597: #else
                    598:        } else {
                    599: #endif
1.1.1.3   root      600:                mem[byteaddress] = data;
1.1       root      601:        }
                    602: }
                    603: 
                    604: void write_word(offs_t byteaddress, UINT16 data)
1.1.1.33  root      605: #ifdef USE_DEBUGGER
                    606: {
                    607:        if(now_debugging) {
                    608:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    609:                        if(wr_break_point.table[i].status == 1) {
                    610:                                if(byteaddress >= wr_break_point.table[i].addr && byteaddress < wr_break_point.table[i].addr + 2) {
                    611:                                        wr_break_point.hit = i + 1;
                    612:                                        now_suspended = true;
                    613:                                        break;
                    614:                                }
                    615:                        }
                    616:                }
                    617:        }
                    618:        debugger_write_word(byteaddress, data);
                    619: }
                    620: void debugger_write_word(offs_t byteaddress, UINT16 data)
                    621: #endif
1.1       root      622: {
1.1.1.8   root      623:        if(byteaddress < MEMORY_END) {
1.1.1.14  root      624:                if(byteaddress == 0x450 + mem[0x462] * 2) {
                    625:                        COORD co;
                    626:                        co.X = data & 0xff;
                    627:                        co.Y = (data >> 8) + scr_top;
1.1.1.23  root      628:                        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), co);
1.1.1.14  root      629:                }
1.1.1.3   root      630:                *(UINT16 *)(mem + byteaddress) = data;
1.1.1.8   root      631:        } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14  root      632:                if(!restore_console_on_exit) {
                    633:                        change_console_size(scr_width, scr_height);
1.1.1.12  root      634:                }
1.1.1.8   root      635:                write_text_vram_word(byteaddress - text_vram_top_address, data);
                    636:                *(UINT16 *)(mem + byteaddress) = data;
                    637:        } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
                    638:                if(int_10h_feh_called && !int_10h_ffh_called) {
                    639:                        write_text_vram_word(byteaddress - shadow_buffer_top_address, data);
1.1       root      640:                }
                    641:                *(UINT16 *)(mem + byteaddress) = data;
1.1.1.4   root      642: #if defined(HAS_I386)
1.1.1.3   root      643:        } else if(byteaddress < MAX_MEM - 1) {
1.1.1.4   root      644: #else
                    645:        } else {
                    646: #endif
1.1.1.3   root      647:                *(UINT16 *)(mem + byteaddress) = data;
1.1       root      648:        }
                    649: }
                    650: 
                    651: void write_dword(offs_t byteaddress, UINT32 data)
1.1.1.33  root      652: #ifdef USE_DEBUGGER
                    653: {
                    654:        if(now_debugging) {
                    655:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    656:                        if(wr_break_point.table[i].status == 1) {
                    657:                                if(byteaddress >= wr_break_point.table[i].addr && byteaddress < wr_break_point.table[i].addr + 4) {
                    658:                                        wr_break_point.hit = i + 1;
                    659:                                        now_suspended = true;
                    660:                                        break;
                    661:                                }
                    662:                        }
                    663:                }
                    664:        }
                    665:        debugger_write_dword(byteaddress, data);
                    666: }
                    667: void debugger_write_dword(offs_t byteaddress, UINT32 data)
                    668: #endif
1.1       root      669: {
1.1.1.8   root      670:        if(byteaddress < MEMORY_END) {
1.1.1.3   root      671:                *(UINT32 *)(mem + byteaddress) = data;
1.1.1.8   root      672:        } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14  root      673:                if(!restore_console_on_exit) {
                    674:                        change_console_size(scr_width, scr_height);
1.1.1.12  root      675:                }
1.1.1.8   root      676:                write_text_vram_dword(byteaddress - text_vram_top_address, data);
                    677:                *(UINT32 *)(mem + byteaddress) = data;
                    678:        } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
                    679:                if(int_10h_feh_called && !int_10h_ffh_called) {
                    680:                        write_text_vram_dword(byteaddress - shadow_buffer_top_address, data);
1.1       root      681:                }
                    682:                *(UINT32 *)(mem + byteaddress) = data;
1.1.1.4   root      683: #if defined(HAS_I386)
1.1.1.3   root      684:        } else if(byteaddress < MAX_MEM - 3) {
1.1.1.4   root      685: #else
                    686:        } else {
                    687: #endif
1.1.1.3   root      688:                *(UINT32 *)(mem + byteaddress) = data;
1.1       root      689:        }
                    690: }
                    691: 
                    692: #define read_decrypted_byte read_byte
                    693: #define read_decrypted_word read_word
                    694: #define read_decrypted_dword read_dword
                    695: 
1.1.1.3   root      696: #define read_raw_byte read_byte
                    697: #define write_raw_byte write_byte
                    698: 
                    699: #define read_word_unaligned read_word
                    700: #define write_word_unaligned write_word
                    701: 
                    702: #define read_io_word_unaligned read_io_word
                    703: #define write_io_word_unaligned write_io_word
                    704: 
1.1       root      705: UINT8 read_io_byte(offs_t byteaddress);
                    706: UINT16 read_io_word(offs_t byteaddress);
                    707: UINT32 read_io_dword(offs_t byteaddress);
                    708: 
                    709: void write_io_byte(offs_t byteaddress, UINT8 data);
                    710: void write_io_word(offs_t byteaddress, UINT16 data);
                    711: void write_io_dword(offs_t byteaddress, UINT32 data);
                    712: 
                    713: /*****************************************************************************/
                    714: /* src/osd/osdcomm.h */
                    715: 
                    716: /* Highly useful macro for compile-time knowledge of an array size */
                    717: #define ARRAY_LENGTH(x)     (sizeof(x) / sizeof(x[0]))
                    718: 
1.1.1.3   root      719: #if defined(HAS_I386)
1.1.1.10  root      720:        static CPU_TRANSLATE(i386);
                    721:        #include "mame/lib/softfloat/softfloat.c"
                    722:        #include "mame/emu/cpu/i386/i386.c"
1.1.1.12  root      723:        #include "mame/emu/cpu/vtlb.c"
1.1.1.3   root      724: #elif defined(HAS_I286)
1.1.1.10  root      725:        #include "mame/emu/cpu/i86/i286.c"
1.1.1.3   root      726: #else
1.1.1.10  root      727:        #include "mame/emu/cpu/i86/i86.c"
1.1.1.3   root      728: #endif
1.1.1.33  root      729: #ifdef USE_DEBUGGER
1.1.1.10  root      730:        #include "mame/emu/cpu/i386/i386dasm.c"
1.1       root      731: #endif
                    732: 
1.1.1.3   root      733: #if defined(HAS_I386)
                    734:        #define SREG(x)                         m_sreg[x].selector
                    735:        #define SREG_BASE(x)                    m_sreg[x].base
                    736:        int cpu_type, cpu_step;
                    737: #else
                    738:        #define REG8(x)                         m_regs.b[x]
                    739:        #define REG16(x)                        m_regs.w[x]
                    740:        #define SREG(x)                         m_sregs[x]
                    741:        #define SREG_BASE(x)                    m_base[x]
1.1.1.33  root      742:        #define m_eip                           (m_pc - m_base[CS])
1.1.1.3   root      743:        #define m_CF                            m_CarryVal
                    744:        #define m_a20_mask                      AMASK
                    745:        #define i386_load_segment_descriptor(x) m_base[x] = SegBase(x)
                    746:        #if defined(HAS_I286)
                    747:                #define i386_set_a20_line(x)    i80286_set_a20_line(x)
                    748:        #else
                    749:                #define i386_set_a20_line(x)
                    750:        #endif
                    751:        #define i386_set_irq_line(x, y)         set_irq_line(x, y)
                    752: #endif
1.1       root      753: 
                    754: void i386_jmp_far(UINT16 selector, UINT32 address)
                    755: {
1.1.1.3   root      756: #if defined(HAS_I386)
1.1       root      757:        if(PROTECTED_MODE && !V8086_MODE) {
1.1.1.3   root      758:                i386_protected_mode_jump(selector, address, 1, m_operand_size);
1.1       root      759:        } else {
1.1.1.3   root      760:                SREG(CS) = selector;
                    761:                m_performed_intersegment_jump = 1;
                    762:                i386_load_segment_descriptor(CS);
                    763:                m_eip = address;
                    764:                CHANGE_PC(m_eip);
1.1       root      765:        }
1.1.1.3   root      766: #elif defined(HAS_I286)
                    767:        i80286_code_descriptor(selector, address, 1);
                    768: #else
                    769:        SREG(CS) = selector;
                    770:        i386_load_segment_descriptor(CS);
                    771:        m_pc = (SREG_BASE(CS) + address) & m_a20_mask;
                    772: #endif
1.1       root      773: }
                    774: 
1.1.1.35  root      775: #ifdef USE_SERVICE_THREAD
1.1.1.24  root      776: void i386_call_far(UINT16 selector, UINT32 address)
                    777: {
                    778: #if defined(HAS_I386)
                    779:        if(PROTECTED_MODE && !V8086_MODE) {
                    780:                i386_protected_mode_call(selector, address, 1, m_operand_size);
                    781:        } else {
                    782:                PUSH16(SREG(CS));
                    783:                PUSH16(m_eip);
                    784:                SREG(CS) = selector;
                    785:                m_performed_intersegment_jump = 1;
                    786:                i386_load_segment_descriptor(CS);
                    787:                m_eip = address;
                    788:                CHANGE_PC(m_eip);
                    789:        }
                    790: #else
                    791:        UINT16 ip = m_pc - SREG_BASE(CS);
                    792:        UINT16 cs = SREG(CS);
                    793: #if defined(HAS_I286)
                    794:        i80286_code_descriptor(selector, address, 2);
                    795: #else
                    796:        SREG(CS) = selector;
                    797:        i386_load_segment_descriptor(CS);
                    798:        m_pc = (SREG_BASE(CS) + address) & m_a20_mask;
                    799: #endif
                    800:        PUSH(cs);
                    801:        PUSH(ip);
                    802:        CHANGE_PC(m_pc);
                    803: #endif
                    804: }
1.1.1.35  root      805: #endif
1.1.1.24  root      806: 
1.1.1.29  root      807: UINT16 i386_read_stack()
                    808: {
                    809: #if defined(HAS_I386)
                    810:        UINT32 ea, new_esp;
                    811:        if( STACK_32BIT ) {
                    812:                new_esp = REG32(ESP) + 2;
                    813:                ea = i386_translate(SS, new_esp - 2, 0);
                    814:        } else {
                    815:                new_esp = REG16(SP) + 2;
                    816:                ea = i386_translate(SS, (new_esp - 2) & 0xffff, 0);
                    817:        }
                    818:        return READ16(ea);
                    819: #else
                    820:        UINT16 sp = m_regs.w[SP] + 2;
                    821:        return ReadWord(((m_base[SS] + ((sp - 2) & 0xffff)) & AMASK));
                    822: #endif
                    823: }
                    824: 
1.1       root      825: /* ----------------------------------------------------------------------------
1.1.1.33  root      826:        debugger
                    827: ---------------------------------------------------------------------------- */
                    828: 
                    829: #ifdef USE_DEBUGGER
                    830: #define TELNET_BLUE      0x0004 // text color contains blue.
                    831: #define TELNET_GREEN     0x0002 // text color contains green.
                    832: #define TELNET_RED       0x0001 // text color contains red.
                    833: #define TELNET_INTENSITY 0x0008 // text color is intensified.
                    834: 
                    835: int svr_socket = 0;
                    836: int cli_socket = 0;
                    837: 
                    838: process_t *msdos_process_info_get(UINT16 psp_seg, bool show_error);
                    839: 
                    840: void debugger_init()
                    841: {
                    842:        now_debugging = false;
                    843:        now_going = false;
                    844:        now_suspended = false;
                    845:        force_suspend = false;
                    846:        
                    847:        memset(&break_point, 0, sizeof(break_point_t));
                    848:        memset(&rd_break_point, 0, sizeof(break_point_t));
                    849:        memset(&wr_break_point, 0, sizeof(break_point_t));
                    850:        memset(&in_break_point, 0, sizeof(break_point_t));
                    851:        memset(&out_break_point, 0, sizeof(break_point_t));
                    852:        memset(&int_break_point, 0, sizeof(int_break_point_t));
                    853: }
                    854: 
1.1.1.45  root      855: void telnet_send(const char *string)
1.1.1.33  root      856: {
                    857:        char buffer[8192], *ptr;
                    858:        strcpy(buffer, string);
                    859:        while((ptr = strstr(buffer, "\n")) != NULL) {
                    860:                char tmp[8192];
                    861:                *ptr = '\0';
                    862:                sprintf(tmp, "%s\033E%s", buffer, ptr + 1);
                    863:                strcpy(buffer, tmp);
                    864:        }
                    865:        
                    866:        int len = strlen(buffer), res;
                    867:        ptr = buffer;
                    868:        while(len > 0) {
                    869:                if((res = send(cli_socket, ptr, len, 0)) > 0) {
                    870:                        len -= res;
                    871:                        ptr += res;
                    872:                }
                    873:        }
                    874: }
                    875: 
                    876: void telnet_command(const char *format, ...)
                    877: {
                    878:        char buffer[1024];
                    879:        va_list ap;
                    880:        va_start(ap, format);
                    881:        vsprintf(buffer, format, ap);
                    882:        va_end(ap);
                    883:        
                    884:        telnet_send(buffer);
                    885: }
                    886: 
                    887: void telnet_printf(const char *format, ...)
                    888: {
                    889:        char buffer[1024];
                    890:        va_list ap;
                    891:        va_start(ap, format);
                    892:        vsprintf(buffer, format, ap);
                    893:        va_end(ap);
                    894:        
                    895:        if(fp_debugger != NULL) {
                    896:                fprintf(fp_debugger, "%s", buffer);
                    897:        }
                    898:        telnet_send(buffer);
                    899: }
                    900: 
                    901: bool telnet_gets(char *str, int n)
                    902: {
                    903:        char buffer[1024];
                    904:        int ptr = 0;
                    905:        
                    906:        telnet_command("\033[12l"); // local echo on
                    907:        telnet_command("\033[2l");  // key unlock
                    908:        
                    909:        while(!m_halted) {
                    910:                int len = recv(cli_socket, buffer, sizeof(buffer), 0);
                    911:                
                    912:                if(len > 0 && buffer[0] != 0xff) {
                    913:                        for(int i = 0; i < len; i++) {
                    914:                                if(buffer[i] == 0x0d || buffer[i] == 0x0a) {
                    915:                                        str[ptr] = 0;
                    916:                                        telnet_command("\033[2h");  // key lock
                    917:                                        telnet_command("\033[12h"); // local echo off
                    918:                                        return(!m_halted);
                    919:                                } else if(buffer[i] == 0x08) {
                    920:                                        if(ptr > 0) {
                    921:                                                telnet_command("\033[0K"); // erase from cursor position
                    922:                                                ptr--;
                    923:                                        } else {
                    924:                                                telnet_command("\033[1C"); // move cursor forward
                    925:                                        }
                    926:                                } else if(ptr < n - 1) {
1.1.1.37  root      927:                                        if(buffer[i] >= 0x20 && buffer[i] <= 0x7e) {
1.1.1.33  root      928:                                                str[ptr++] = buffer[i];
                    929:                                        }
                    930:                                } else {
                    931:                                        telnet_command("\033[1D\033[0K"); // move cursor backward and erase from cursor position
                    932:                                }
                    933:                        }
                    934:                } else if(len == -1) {
                    935:                        if(WSAGetLastError() != WSAEWOULDBLOCK) {
                    936:                                return(false);
                    937:                        }
                    938:                } else if(len == 0) {
                    939:                        return(false);
                    940:                }
                    941:                Sleep(10);
                    942:        }
                    943:        return(!m_halted);
                    944: }
                    945: 
                    946: bool telnet_kbhit()
                    947: {
                    948:        char buffer[1024];
                    949:        
                    950:        if(!m_halted) {
                    951:                int len = recv(cli_socket, buffer, sizeof(buffer), 0);
                    952:                
                    953:                if(len > 0) {
                    954:                        for(int i = 0; i < len; i++) {
                    955:                                if(buffer[i] == 0x0d || buffer[i] == 0x0a) {
                    956:                                        return(true);
                    957:                                }
                    958:                        }
                    959:                } else if(len == 0) {
                    960:                        return(true); // disconnected
                    961:                }
                    962:        }
                    963:        return(false);
                    964: }
                    965: 
                    966: bool telnet_disconnected()
                    967: {
                    968:        char buffer[1024];
                    969:        int len = recv(cli_socket, buffer, sizeof(buffer), 0);
                    970:        
                    971:        if(len == 0) {
                    972:                return(true);
                    973:        } else if(len == -1) {
                    974:                if(WSAGetLastError() != WSAEWOULDBLOCK) {
                    975:                        return(true);
                    976:                }
                    977:        }
                    978:        return(false);
                    979: }
                    980: 
                    981: void telnet_set_color(int color)
                    982: {
                    983:        telnet_command("\033[%dm\033[3%dm", (color >> 3) & 1, (color & 7));
                    984: }
                    985: 
                    986: int debugger_dasm(char *buffer, UINT32 cs, UINT32 eip)
                    987: {
                    988: //     UINT8 *oprom = mem + (((cs << 4) + eip) & (MAX_MEM - 1));
                    989:        UINT8 ops[16];
                    990:        for(int i = 0; i < 16; i++) {
                    991:                ops[i] = debugger_read_byte(((cs << 4) + (eip + i)) & ADDR_MASK);
                    992:        }
                    993:        UINT8 *oprom = ops;
                    994:        
                    995: #if defined(HAS_I386)
                    996:        if(m_operand_size) {
                    997:                return(CPU_DISASSEMBLE_CALL(x86_32) & DASMFLAG_LENGTHMASK);
                    998:        } else
                    999: #endif
                   1000:        return(CPU_DISASSEMBLE_CALL(x86_16) & DASMFLAG_LENGTHMASK);
                   1001: }
                   1002: 
                   1003: void debugger_regs_info(char *buffer)
                   1004: {
                   1005: #if defined(HAS_I386)
                   1006:        UINT32 flags = get_flags();
                   1007: #else
                   1008:        UINT32 flags = CompressFlags();
                   1009: #endif
                   1010: #if defined(HAS_I386)
                   1011:        if(m_operand_size) {
                   1012:                sprintf(buffer, "EAX=%08X  EBX=%08X  ECX=%08X  EDX=%08X\nESP=%08X  EBP=%08X  ESI=%08X  EDI=%08X\nEIP=%08X  DS=%04X  ES=%04X  SS=%04X  CS=%04X  FLAG=[%c %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c]\n",
                   1013:                REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SP), REG16(BP), REG16(SI), REG16(DI), m_eip, SREG(DS), SREG(ES), SREG(SS), SREG(CS),
                   1014:                PROTECTED_MODE ? "PE" : "--",
                   1015:                (flags & 0x40000) ? 'A' : '-',
                   1016:                (flags & 0x20000) ? 'V' : '-',
                   1017:                (flags & 0x10000) ? 'R' : '-',
                   1018:                (flags & 0x04000) ? 'N' : '-',
                   1019:                (flags & 0x02000) ? '1' : '0',
                   1020:                (flags & 0x01000) ? '1' : '0',
                   1021:                (flags & 0x00800) ? 'O' : '-',
                   1022:                (flags & 0x00400) ? 'D' : '-',
                   1023:                (flags & 0x00200) ? 'I' : '-',
                   1024:                (flags & 0x00100) ? 'T' : '-',
                   1025:                (flags & 0x00080) ? 'S' : '-',
                   1026:                (flags & 0x00040) ? 'Z' : '-',
                   1027:                (flags & 0x00010) ? 'A' : '-',
                   1028:                (flags & 0x00004) ? 'P' : '-',
                   1029:                (flags & 0x00001) ? 'C' : '-');
                   1030:        } else {
                   1031: #endif
                   1032:                sprintf(buffer, "AX=%04X  BX=%04X  CX=%04X  DX=%04X  SP=%04X  BP=%04X  SI=%04X  DI=%04X\nIP=%04X  DS=%04X  ES=%04X  SS=%04X  CS=%04X  FLAG=[%c %c%c%c%c%c%c%c%c%c%c%c%c%c%c%c]\n",
                   1033:                REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SP), REG16(BP), REG16(SI), REG16(DI), m_eip, SREG(DS), SREG(ES), SREG(SS), SREG(CS),
                   1034: #if defined(HAS_I386)
                   1035:                PROTECTED_MODE ? "PE" : "--",
                   1036: #else
                   1037:                "--",
                   1038: #endif
                   1039:                (flags & 0x40000) ? 'A' : '-',
                   1040:                (flags & 0x20000) ? 'V' : '-',
                   1041:                (flags & 0x10000) ? 'R' : '-',
                   1042:                (flags & 0x04000) ? 'N' : '-',
                   1043:                (flags & 0x02000) ? '1' : '0',
                   1044:                (flags & 0x01000) ? '1' : '0',
                   1045:                (flags & 0x00800) ? 'O' : '-',
                   1046:                (flags & 0x00400) ? 'D' : '-',
                   1047:                (flags & 0x00200) ? 'I' : '-',
                   1048:                (flags & 0x00100) ? 'T' : '-',
                   1049:                (flags & 0x00080) ? 'S' : '-',
                   1050:                (flags & 0x00040) ? 'Z' : '-',
                   1051:                (flags & 0x00010) ? 'A' : '-',
                   1052:                (flags & 0x00004) ? 'P' : '-',
                   1053:                (flags & 0x00001) ? 'C' : '-');
                   1054: #if defined(HAS_I386)
                   1055:        }
                   1056: #endif
                   1057: }
                   1058: 
                   1059: void debugger_process_info(char *buffer)
                   1060: {
                   1061:        UINT16 psp_seg = current_psp;
                   1062:        process_t *process;
                   1063:        bool check[0x10000] = {0};
                   1064:        
                   1065:        buffer[0] = '\0';
                   1066:        
                   1067:        while(!check[psp_seg] && (process  = msdos_process_info_get(psp_seg, false)) != NULL) {
                   1068:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   1069:                char *file = process->module_path, *s;
                   1070:                char tmp[8192];
                   1071:                
                   1072:                while((s = strstr(file, "\\")) != NULL) {
                   1073:                        file = s + 1;
                   1074:                }
                   1075:                sprintf(tmp, "PSP=%04X  ENV=%04X  RETURN=%04X:%04X  PROGRAM=%s\n", psp_seg, psp->env_seg, psp->int_22h.w.h, psp->int_22h.w.l, my_strupr(file));
                   1076:                strcat(tmp, buffer);
                   1077:                strcpy(buffer, tmp);
                   1078:                
                   1079:                check[psp_seg] = true;
                   1080:                psp_seg = psp->parent_psp;
                   1081:        }
                   1082: }
                   1083: 
                   1084: UINT32 debugger_get_val(const char *str)
                   1085: {
                   1086:        char tmp[1024];
                   1087:        
                   1088:        if(str == NULL || strlen(str) == 0) {
                   1089:                return(0);
                   1090:        }
                   1091:        strcpy(tmp, str);
                   1092:        
                   1093:        if(strlen(tmp) == 3 && tmp[0] == '\'' && tmp[2] == '\'') {
                   1094:                // ank
                   1095:                return(tmp[1] & 0xff);
                   1096:        } else if(tmp[0] == '%') {
                   1097:                // decimal
                   1098:                return(strtoul(tmp + 1, NULL, 10));
                   1099:        }
                   1100:        return(strtoul(tmp, NULL, 16));
                   1101: }
                   1102: 
                   1103: UINT32 debugger_get_seg(const char *str, UINT32 val)
                   1104: {
                   1105:        char tmp[1024], *s;
                   1106:        
                   1107:        if(str == NULL || strlen(str) == 0) {
                   1108:                return(val);
                   1109:        }
                   1110:        strcpy(tmp, str);
                   1111:        
                   1112:        if((s = strstr(tmp, ":")) != NULL) {
                   1113:                // 0000:0000
                   1114:                *s = '\0';
                   1115:                return(debugger_get_val(tmp));
                   1116:        }
                   1117:        return(val);
                   1118: }
                   1119: 
                   1120: UINT32 debugger_get_ofs(const char *str)
                   1121: {
                   1122:        char tmp[1024], *s;
                   1123:        
                   1124:        if(str == NULL || strlen(str) == 0) {
                   1125:                return(0);
                   1126:        }
                   1127:        strcpy(tmp, str);
                   1128:        
                   1129:        if((s = strstr(tmp, ":")) != NULL) {
                   1130:                // 0000:0000
                   1131:                return(debugger_get_val(s + 1));
                   1132:        }
                   1133:        return(debugger_get_val(tmp));
                   1134: }
                   1135: 
                   1136: void debugger_main()
                   1137: {
                   1138:        telnet_command("\033[20h"); // cr-lf
                   1139:        
                   1140:        force_suspend = true;
                   1141:        now_going = false;
                   1142:        now_debugging = true;
                   1143:        Sleep(100);
                   1144:        
                   1145:        if(!m_halted && !now_suspended) {
                   1146:                telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1147:                telnet_printf("waiting until cpu is suspended...\n");
                   1148:        }
                   1149:        while(!m_halted && !now_suspended) {
                   1150:                if(telnet_disconnected()) {
                   1151:                        break;
                   1152:                }
                   1153:                Sleep(10);
                   1154:        }
                   1155:        
                   1156:        char buffer[8192];
                   1157:        
                   1158:        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1159:        debugger_process_info(buffer);
                   1160:        telnet_printf("%s", buffer);
                   1161:        debugger_regs_info(buffer);
                   1162:        telnet_printf("%s", buffer);
                   1163:        telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1164:        telnet_printf("breaked at %04X:%04X\n", SREG(CS), m_eip);
                   1165:        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1166:        debugger_dasm(buffer, SREG(CS), m_eip);
                   1167:        telnet_printf("next\t%04X:%04X  %s\n", SREG(CS), m_eip, buffer);
                   1168:        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1169:        
                   1170:        #define MAX_COMMAND_LEN 64
                   1171:        
                   1172:        char command[MAX_COMMAND_LEN + 1];
                   1173:        char prev_command[MAX_COMMAND_LEN + 1] = {0};
                   1174:        
                   1175:        UINT32 data_seg = SREG(DS);
                   1176:        UINT32 data_ofs = 0;
                   1177:        UINT32 dasm_seg = SREG(CS);
                   1178:        UINT32 dasm_ofs = m_eip;
                   1179:        
                   1180:        while(!m_halted) {
                   1181:                telnet_printf("- ");
                   1182:                command[0] = '\0';
                   1183:                
                   1184:                if(fi_debugger != NULL) {
                   1185:                        while(command[0] == '\0') {
                   1186:                                if(fgets(command, sizeof(command), fi_debugger) == NULL) {
                   1187:                                        break;
                   1188:                                }
                   1189:                                while(strlen(command) > 0 && (command[strlen(command) - 1] == 0x0d || command[strlen(command) - 1] == 0x0a)) {
                   1190:                                        command[strlen(command) - 1] = '\0';
                   1191:                                }
                   1192:                        }
                   1193:                        if(command[0] != '\0') {
                   1194:                                telnet_command("%s\n", command);
                   1195:                        }
                   1196:                }
                   1197:                if(command[0] == '\0') {
                   1198:                        if(!telnet_gets(command, sizeof(command))) {
                   1199:                                break;
                   1200:                        }
                   1201:                }
                   1202:                if(command[0] == '\0') {
                   1203:                        strcpy(command, prev_command);
                   1204:                } else {
                   1205:                        strcpy(prev_command, command);
                   1206:                }
                   1207:                if(fp_debugger != NULL) {
                   1208:                        fprintf(fp_debugger, "%s\n", command);
                   1209:                }
                   1210:                
                   1211:                if(!m_halted && command[0] != 0) {
                   1212:                        char *params[32], *token = NULL;
                   1213:                        int num = 0;
                   1214:                        
                   1215:                        if((token = strtok(command, " ")) != NULL) {
                   1216:                                params[num++] = token;
                   1217:                                while(num < 32 && (token = strtok(NULL, " ")) != NULL) {
                   1218:                                        params[num++] = token;
                   1219:                                }
                   1220:                        }
                   1221:                        if(stricmp(params[0], "D") == 0) {
                   1222:                                if(num <= 3) {
                   1223:                                        if(num >= 2) {
                   1224:                                                data_seg = debugger_get_seg(params[1], data_seg);
                   1225:                                                data_ofs = debugger_get_ofs(params[1]);
                   1226:                                        }
                   1227:                                        UINT32 end_seg = data_seg;
                   1228:                                        UINT32 end_ofs = data_ofs + 8 * 16 - 1;
                   1229:                                        if(num == 3) {
                   1230:                                                end_seg = debugger_get_seg(params[2], data_seg);
                   1231:                                                end_ofs = debugger_get_ofs(params[2]);
                   1232:                                        }
                   1233:                                        UINT64 start_addr = (data_seg << 4) + data_ofs;
                   1234:                                        UINT64 end_addr = (end_seg << 4) + end_ofs;
1.1.1.37  root     1235: //                                     bool is_sjis = false;
1.1.1.33  root     1236:                                        
                   1237:                                        for(UINT64 addr = (start_addr & ~0x0f); addr <= (end_addr | 0x0f); addr++) {
                   1238:                                                if((addr & 0x0f) == 0) {
                   1239:                                                        if((data_ofs = addr - (data_seg << 4)) > 0xffff) {
                   1240:                                                                data_seg += 0x1000;
                   1241:                                                                data_ofs -= 0x10000;
                   1242:                                                        }
                   1243:                                                        telnet_printf("%06X:%04X ", data_seg, data_ofs);
                   1244:                                                        memset(buffer, 0, sizeof(buffer));
                   1245:                                                }
                   1246:                                                if(addr < start_addr || addr > end_addr) {
                   1247:                                                        telnet_printf("   ");
                   1248:                                                        buffer[addr & 0x0f] = ' ';
                   1249:                                                } else {
                   1250:                                                        UINT8 data = debugger_read_byte(addr & ADDR_MASK);
                   1251:                                                        telnet_printf(" %02X", data);
1.1.1.37  root     1252: //                                                     if(is_sjis) {
1.1.1.33  root     1253: //                                                             buffer[addr & 0x0f] = data;
1.1.1.37  root     1254: //                                                             is_sjis = false;
1.1.1.33  root     1255: //                                                     } else if(((data >= 0x81 && data <= 0x9f) || (data >= 0xe0 && data <= 0xef)) && (addr & 0x0f) < 0x0f) {
                   1256: //                                                             buffer[addr & 0x0f] = data;
1.1.1.37  root     1257: //                                                             is_sjis = true;
1.1.1.33  root     1258: //                                                     } else
                   1259:                                                        if((data >= 0x20 && data <= 0x7e)/* || (data >= 0xa1 && data <= 0xdf)*/) {
                   1260:                                                                buffer[addr & 0x0f] = data;
                   1261:                                                        } else {
                   1262:                                                                buffer[addr & 0x0f] = '.';
                   1263:                                                        }
                   1264:                                                }
                   1265:                                                if((addr & 0x0f) == 0x0f) {
                   1266:                                                        telnet_printf("  %s\n", buffer);
                   1267:                                                }
                   1268:                                        }
                   1269:                                        if((data_ofs = (end_addr + 1) - (data_seg << 4)) > 0xffff) {
                   1270:                                                data_seg += 0x1000;
                   1271:                                                data_ofs -= 0x10000;
                   1272:                                        }
                   1273:                                        prev_command[1] = '\0'; // remove parameters to dump continuously
                   1274:                                } else {
                   1275:                                        telnet_printf("invalid parameter number\n");
                   1276:                                }
                   1277:                        } else if(stricmp(params[0], "E") == 0 || stricmp(params[0], "EB") == 0) {
                   1278:                                if(num >= 3) {
                   1279:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1280:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1281:                                        for(int i = 2, j = 0; i < num; i++, j++) {
                   1282:                                                debugger_write_byte(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]) & 0xff);
                   1283:                                        }
                   1284:                                } else {
                   1285:                                        telnet_printf("invalid parameter number\n");
                   1286:                                }
                   1287:                        } else if(stricmp(params[0], "EW") == 0) {
                   1288:                                if(num >= 3) {
                   1289:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1290:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1291:                                        for(int i = 2, j = 0; i < num; i++, j += 2) {
                   1292:                                                debugger_write_word(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]) & 0xffff);
                   1293:                                        }
                   1294:                                } else {
                   1295:                                        telnet_printf("invalid parameter number\n");
                   1296:                                }
                   1297:                        } else if(stricmp(params[0], "ED") == 0) {
                   1298:                                if(num >= 3) {
                   1299:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1300:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1301:                                        for(int i = 2, j = 0; i < num; i++, j += 4) {
                   1302:                                                debugger_write_dword(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]));
                   1303:                                        }
                   1304:                                } else {
                   1305:                                        telnet_printf("invalid parameter number\n");
                   1306:                                }
                   1307:                        } else if(stricmp(params[0], "EA") == 0) {
                   1308:                                if(num >= 3) {
                   1309:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1310:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1311:                                        strcpy(buffer, prev_command);
                   1312:                                        if((token = strtok(buffer, "\"")) != NULL && (token = strtok(NULL, "\"")) != NULL) {
                   1313:                                                int len = strlen(token);
                   1314:                                                for(int i = 0; i < len; i++) {
                   1315:                                                        debugger_write_byte(((seg << 4) + (ofs + i)) & ADDR_MASK, token[i] & 0xff);
                   1316:                                                }
                   1317:                                        } else {
                   1318:                                                telnet_printf("invalid parameter\n");
                   1319:                                        }
                   1320:                                } else {
                   1321:                                        telnet_printf("invalid parameter number\n");
                   1322:                                }
                   1323:                        } else if(stricmp(params[0], "I") == 0 || stricmp(params[0], "IB") == 0) {
                   1324:                                if(num == 2) {
                   1325:                                        telnet_printf("%02X\n", debugger_read_io_byte(debugger_get_val(params[1])) & 0xff);
                   1326:                                } else {
                   1327:                                        telnet_printf("invalid parameter number\n");
                   1328:                                }
                   1329:                        } else if(stricmp(params[0], "IW") == 0) {
                   1330:                                if(num == 2) {
                   1331:                                        telnet_printf("%04X\n", debugger_read_io_word(debugger_get_val(params[1])) & 0xffff);
                   1332:                                } else {
                   1333:                                        telnet_printf("invalid parameter number\n");
                   1334:                                }
                   1335:                        } else if(stricmp(params[0], "ID") == 0) {
                   1336:                                if(num == 2) {
                   1337:                                        telnet_printf("%08X\n", debugger_read_io_dword(debugger_get_val(params[1])));
                   1338:                                } else {
                   1339:                                        telnet_printf("invalid parameter number\n");
                   1340:                                }
                   1341:                        } else if(stricmp(params[0], "O") == 0 || stricmp(params[0], "OB") == 0) {
                   1342:                                if(num == 3) {
                   1343:                                        debugger_write_io_byte(debugger_get_val(params[1]), debugger_get_val(params[2]) & 0xff);
                   1344:                                } else {
                   1345:                                        telnet_printf("invalid parameter number\n");
                   1346:                                }
                   1347:                        } else if(stricmp(params[0], "OW") == 0) {
                   1348:                                if(num == 3) {
                   1349:                                        debugger_write_io_word(debugger_get_val(params[1]), debugger_get_val(params[2]) & 0xffff);
                   1350:                                } else {
                   1351:                                        telnet_printf("invalid parameter number\n");
                   1352:                                }
                   1353:                        } else if(stricmp(params[0], "OD") == 0) {
                   1354:                                if(num == 3) {
                   1355:                                        debugger_write_io_dword(debugger_get_val(params[1]), debugger_get_val(params[2]));
                   1356:                                } else {
                   1357:                                        telnet_printf("invalid parameter number\n");
                   1358:                                }
                   1359:                        } else if(stricmp(params[0], "R") == 0) {
                   1360:                                if(num == 1) {
                   1361:                                        debugger_regs_info(buffer);
                   1362:                                        telnet_printf("%s", buffer);
                   1363:                                } else if(num == 3) {
                   1364: #if defined(HAS_I386)
                   1365:                                        if(stricmp(params[1], "EAX") == 0) {
                   1366:                                                REG32(EAX) = debugger_get_val(params[2]);
                   1367:                                        } else if(stricmp(params[1], "EBX") == 0) {
                   1368:                                                REG32(EBX) = debugger_get_val(params[2]);
                   1369:                                        } else if(stricmp(params[1], "ECX") == 0) {
                   1370:                                                REG32(ECX) = debugger_get_val(params[2]);
                   1371:                                        } else if(stricmp(params[1], "EDX") == 0) {
                   1372:                                                REG32(EDX) = debugger_get_val(params[2]);
                   1373:                                        } else if(stricmp(params[1], "ESP") == 0) {
                   1374:                                                REG32(ESP) = debugger_get_val(params[2]);
                   1375:                                        } else if(stricmp(params[1], "EBP") == 0) {
                   1376:                                                REG32(EBP) = debugger_get_val(params[2]);
                   1377:                                        } else if(stricmp(params[1], "ESI") == 0) {
                   1378:                                                REG32(ESI) = debugger_get_val(params[2]);
                   1379:                                        } else if(stricmp(params[1], "EDI") == 0) {
                   1380:                                                REG32(EDI) = debugger_get_val(params[2]);
                   1381:                                        } else
                   1382: #endif
                   1383:                                        if(stricmp(params[1], "AX") == 0) {
                   1384:                                                REG16(AX) = debugger_get_val(params[2]);
                   1385:                                        } else if(stricmp(params[1], "BX") == 0) {
                   1386:                                                REG16(BX) = debugger_get_val(params[2]);
                   1387:                                        } else if(stricmp(params[1], "CX") == 0) {
                   1388:                                                REG16(CX) = debugger_get_val(params[2]);
                   1389:                                        } else if(stricmp(params[1], "DX") == 0) {
                   1390:                                                REG16(DX) = debugger_get_val(params[2]);
                   1391:                                        } else if(stricmp(params[1], "SP") == 0) {
                   1392:                                                REG16(SP) = debugger_get_val(params[2]);
                   1393:                                        } else if(stricmp(params[1], "BP") == 0) {
                   1394:                                                REG16(BP) = debugger_get_val(params[2]);
                   1395:                                        } else if(stricmp(params[1], "SI") == 0) {
                   1396:                                                REG16(SI) = debugger_get_val(params[2]);
                   1397:                                        } else if(stricmp(params[1], "DI") == 0) {
                   1398:                                                REG16(DI) = debugger_get_val(params[2]);
                   1399:                                        } else if(stricmp(params[1], "IP") == 0 || stricmp(params[1], "EIP") == 0) {
                   1400: #if defined(HAS_I386)
                   1401:                                                if(m_operand_size) {
                   1402:                                                        m_eip = debugger_get_val(params[2]);
                   1403:                                                } else {
                   1404:                                                        m_eip = debugger_get_val(params[2]) & 0xffff;
                   1405:                                                }
                   1406:                                                CHANGE_PC(m_eip);
                   1407: #else
                   1408:                                                m_pc = (SREG_BASE(CS) + (debugger_get_val(params[2]) & 0xffff)) & ADDR_MASK;
                   1409:                                                CHANGE_PC(m_pc);
                   1410: #endif
                   1411:                                        } else if(stricmp(params[1], "AL") == 0) {
                   1412:                                                REG8(AL) = debugger_get_val(params[2]);
                   1413:                                        } else if(stricmp(params[1], "AH") == 0) {
                   1414:                                                REG8(AH) = debugger_get_val(params[2]);
                   1415:                                        } else if(stricmp(params[1], "BL") == 0) {
                   1416:                                                REG8(BL) = debugger_get_val(params[2]);
                   1417:                                        } else if(stricmp(params[1], "BH") == 0) {
                   1418:                                                REG8(BH) = debugger_get_val(params[2]);
                   1419:                                        } else if(stricmp(params[1], "CL") == 0) {
                   1420:                                                REG8(CL) = debugger_get_val(params[2]);
                   1421:                                        } else if(stricmp(params[1], "CH") == 0) {
                   1422:                                                REG8(CH) = debugger_get_val(params[2]);
                   1423:                                        } else if(stricmp(params[1], "DL") == 0) {
                   1424:                                                REG8(DL) = debugger_get_val(params[2]);
                   1425:                                        } else if(stricmp(params[1], "DH") == 0) {
                   1426:                                                REG8(DH) = debugger_get_val(params[2]);
                   1427:                                        } else {
                   1428:                                                telnet_printf("unknown register %s\n", params[1]);
                   1429:                                        }
                   1430:                                } else {
                   1431:                                        telnet_printf("invalid parameter number\n");
                   1432:                                }
                   1433:                        } else if(_tcsicmp(params[0], "S") == 0) {
                   1434:                                if(num >= 4) {
                   1435:                                        UINT32 cur_seg = debugger_get_seg(params[1], data_seg);
                   1436:                                        UINT32 cur_ofs = debugger_get_ofs(params[1]);
                   1437:                                        UINT32 end_seg = debugger_get_seg(params[2], cur_seg);
                   1438:                                        UINT32 end_ofs = debugger_get_ofs(params[2]);
                   1439:                                        UINT8 list[32];
                   1440:                                        
                   1441:                                        for(int i = 3, j = 0; i < num && j < 32; i++, j++) {
                   1442:                                                list[j] = debugger_get_val(params[i]);
                   1443:                                        }
                   1444:                                        while((cur_seg << 4) + cur_ofs <= (end_seg << 4) + end_ofs) {
                   1445:                                                bool found = true;
                   1446:                                                for(int i = 3, j = 0; i < num && j < 32; i++, j++) {
                   1447:                                                        if(debugger_read_byte(((cur_seg << 4) + (cur_ofs + j)) & ADDR_MASK) != list[j]) {
                   1448:                                                                found = false;
                   1449:                                                                break;
                   1450:                                                        }
                   1451:                                                }
                   1452:                                                if(found) {
                   1453:                                                        telnet_printf("%04X:%04X\n", cur_seg, cur_ofs);
                   1454:                                                }
                   1455:                                                if((cur_ofs += 1) > 0xffff) {
                   1456:                                                        cur_seg += 0x1000;
                   1457:                                                        cur_ofs -= 0x10000;
                   1458:                                                }
                   1459:                                        }
                   1460:                                } else {
                   1461:                                        telnet_printf("invalid parameter number\n");
                   1462:                                }
                   1463:                        } else if(stricmp(params[0], "U") == 0) {
                   1464:                                if(num <= 3) {
                   1465:                                        if(num >= 2) {
                   1466:                                                dasm_seg = debugger_get_seg(params[1], dasm_seg);
                   1467:                                                dasm_ofs = debugger_get_ofs(params[1]);
                   1468:                                        }
                   1469:                                        if(num == 3) {
                   1470:                                                UINT32 end_seg = debugger_get_seg(params[2], dasm_seg);
                   1471:                                                UINT32 end_ofs = debugger_get_ofs(params[2]);
                   1472:                                                
                   1473:                                                while((dasm_seg << 4) + dasm_ofs <= (end_seg << 4) + end_ofs) {
                   1474:                                                        int len = debugger_dasm(buffer, dasm_seg, dasm_ofs);
                   1475:                                                        telnet_printf("%04X:%04X  ", dasm_seg, dasm_ofs);
                   1476:                                                        for(int i = 0; i < len; i++) {
                   1477:                                                                telnet_printf("%02X", debugger_read_byte(((dasm_seg << 4) + (dasm_ofs + i)) & ADDR_MASK));
                   1478:                                                        }
                   1479:                                                        for(int i = len; i < 8; i++) {
                   1480:                                                                telnet_printf("  ");
                   1481:                                                        }
                   1482:                                                        telnet_printf("  %s\n", buffer);
                   1483:                                                        if((dasm_ofs += len) > 0xffff) {
                   1484:                                                                dasm_seg += 0x1000;
                   1485:                                                                dasm_ofs -= 0x10000;
                   1486:                                                        }
                   1487:                                                }
                   1488:                                        } else {
                   1489:                                                for(int i = 0; i < 16; i++) {
                   1490:                                                        int len = debugger_dasm(buffer, dasm_seg, dasm_ofs);
                   1491:                                                        telnet_printf("%04X:%04X  ", dasm_seg, dasm_ofs);
                   1492:                                                        for(int i = 0; i < len; i++) {
                   1493:                                                                telnet_printf("%02X", debugger_read_byte(((dasm_seg << 4) + (dasm_ofs + i)) & ADDR_MASK));
                   1494:                                                        }
                   1495:                                                        for(int i = len; i < 8; i++) {
                   1496:                                                                telnet_printf("  ");
                   1497:                                                        }
                   1498:                                                        telnet_printf("  %s\n", buffer);
                   1499:                                                        if((dasm_ofs += len) > 0xffff) {
                   1500:                                                                dasm_seg += 0x1000;
                   1501:                                                                dasm_ofs -= 0x10000;
                   1502:                                                        }
                   1503:                                                }
                   1504:                                        }
                   1505:                                        prev_command[1] = '\0'; // remove parameters to disassemble continuously
                   1506:                                } else {
                   1507:                                        telnet_printf("invalid parameter number\n");
                   1508:                                }
                   1509:                        } else if(stricmp(params[0], "H") == 0) {
                   1510:                                if(num == 3) {
                   1511:                                        UINT32 l = debugger_get_val(params[1]);
                   1512:                                        UINT32 r = debugger_get_val(params[2]);
                   1513:                                        telnet_printf("%08X  %08X\n", l + r, l - r);
                   1514:                                } else {
                   1515:                                        telnet_printf("invalid parameter number\n");
                   1516:                                }
                   1517:                        } else if(stricmp(params[0], "BP") == 0 || stricmp(params[0], "RBP") == 0 || stricmp(params[0], "WBP") == 0) {
                   1518:                                break_point_t *break_point_ptr;
                   1519:                                #define GET_BREAK_POINT_PTR() { \
                   1520:                                        if(params[0][0] == 'R') { \
                   1521:                                                break_point_ptr = &rd_break_point; \
                   1522:                                        } else if(params[0][0] == 'W') { \
                   1523:                                                break_point_ptr = &wr_break_point; \
                   1524:                                        } else if(params[0][0] == 'I') { \
                   1525:                                                break_point_ptr = &in_break_point; \
                   1526:                                        } else if(params[0][0] == 'O') { \
                   1527:                                                break_point_ptr = &out_break_point; \
                   1528:                                        } else { \
                   1529:                                                break_point_ptr = &break_point; \
                   1530:                                        } \
                   1531:                                }
                   1532:                                GET_BREAK_POINT_PTR();
                   1533:                                if(num == 2) {
                   1534:                                        UINT32 seg = debugger_get_seg(params[1], SREG(CS));
                   1535:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1536:                                        bool found = false;
                   1537:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1538:                                                if(break_point_ptr->table[i].status == 0 || break_point_ptr->table[i].addr == ((seg << 4) + ofs)) {
                   1539:                                                        break_point_ptr->table[i].addr = (seg << 4) + ofs;
                   1540:                                                        break_point_ptr->table[i].seg = seg;
                   1541:                                                        break_point_ptr->table[i].ofs = ofs;
                   1542:                                                        break_point_ptr->table[i].status = 1;
                   1543:                                                        found = true;
                   1544:                                                }
                   1545:                                        }
                   1546:                                        if(!found) {
                   1547:                                                telnet_printf("too many break points\n");
                   1548:                                        }
                   1549:                                } else {
                   1550:                                        telnet_printf("invalid parameter number\n");
                   1551:                                }
                   1552:                        } else if(stricmp(params[0], "IBP") == 0 || stricmp(params[0], "OBP") == 0) {
                   1553:                                break_point_t *break_point_ptr;
                   1554:                                GET_BREAK_POINT_PTR();
                   1555:                                if(num == 2) {
                   1556:                                        UINT32 addr = debugger_get_val(params[1]);
                   1557:                                        bool found = false;
                   1558:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1559:                                                if(break_point_ptr->table[i].status == 0 || break_point_ptr->table[i].addr == addr) {
                   1560:                                                        break_point_ptr->table[i].addr = addr;
                   1561:                                                        break_point_ptr->table[i].status = 1;
                   1562:                                                        found = true;
                   1563:                                                }
                   1564:                                        }
                   1565:                                        if(!found) {
                   1566:                                                telnet_printf("too many break points\n");
                   1567:                                        }
                   1568:                                } else {
                   1569:                                        telnet_printf("invalid parameter number\n");
                   1570:                                }
                   1571:                        } else if(stricmp(params[0], "BC") == 0 || stricmp(params[0], "RBC") == 0 || stricmp(params[0], "WBC") == 0 || stricmp(params[0], "IBC") == 0 || stricmp(params[0], "OBC") == 0) {
                   1572:                                break_point_t *break_point_ptr;
                   1573:                                GET_BREAK_POINT_PTR();
                   1574:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1575:                                        memset(break_point_ptr, 0, sizeof(break_point_t));
                   1576:                                } else if(num >= 2) {
                   1577:                                        for(int i = 1; i < num; i++) {
                   1578:                                                int index = debugger_get_val(params[i]);
                   1579:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1580:                                                        telnet_printf("invalid index %x\n", index);
                   1581:                                                } else {
                   1582:                                                        break_point_ptr->table[index - 1].addr = 0;
                   1583:                                                        break_point_ptr->table[index - 1].seg = 0;
                   1584:                                                        break_point_ptr->table[index - 1].ofs = 0;
                   1585:                                                        break_point_ptr->table[index - 1].status = 0;
                   1586:                                                }
                   1587:                                        }
                   1588:                                } else {
                   1589:                                        telnet_printf("invalid parameter number\n");
                   1590:                                }
                   1591:                        } else if(stricmp(params[0], "BD") == 0 || stricmp(params[0], "RBD") == 0 || stricmp(params[0], "WBD") == 0 || stricmp(params[0], "IBD") == 0 || stricmp(params[0], "OBD") == 0 ||
                   1592:                                  stricmp(params[0], "BE") == 0 || stricmp(params[0], "RBE") == 0 || stricmp(params[0], "WBE") == 0 || stricmp(params[0], "IBE") == 0 || stricmp(params[0], "OBE") == 0) {
                   1593:                                break_point_t *break_point_ptr;
                   1594:                                GET_BREAK_POINT_PTR();
                   1595:                                bool enabled = (params[0][strlen(params[0]) - 1] == 'E' || params[0][strlen(params[0]) - 1] == 'e');
                   1596:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1597:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1598:                                                if(break_point_ptr->table[i].status != 0) {
                   1599:                                                        break_point_ptr->table[i].status = enabled ? 1 : -1;
                   1600:                                                }
                   1601:                                        }
                   1602:                                } else if(num >= 2) {
                   1603:                                        for(int i = 1; i < num; i++) {
                   1604:                                                int index = debugger_get_val(params[i]);
                   1605:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1606:                                                        telnet_printf("invalid index %x\n", index);
                   1607:                                                } else if(break_point_ptr->table[index - 1].status == 0) {
                   1608:                                                        telnet_printf("break point %x is null\n", index);
                   1609:                                                } else {
                   1610:                                                        break_point_ptr->table[index - 1].status = enabled ? 1 : -1;
                   1611:                                                }
                   1612:                                        }
                   1613:                                } else {
                   1614:                                        telnet_printf("invalid parameter number\n");
                   1615:                                }
                   1616:                        } else if(stricmp(params[0], "BL") == 0 || stricmp(params[0], "RBL") == 0 || stricmp(params[0], "WBL") == 0) {
                   1617:                                break_point_t *break_point_ptr;
                   1618:                                GET_BREAK_POINT_PTR();
                   1619:                                if(num == 1) {
                   1620:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1621:                                                if(break_point_ptr->table[i].status) {
                   1622:                                                        telnet_printf("%d %c %04X:%04X\n", i + 1, break_point_ptr->table[i].status == 1 ? 'e' : 'd', break_point_ptr->table[i].seg, break_point_ptr->table[i].ofs);
                   1623:                                                }
                   1624:                                        }
                   1625:                                } else {
                   1626:                                        telnet_printf("invalid parameter number\n");
                   1627:                                }
                   1628:                        } else if(stricmp(params[0], "IBL") == 0 || stricmp(params[0], "OBL") == 0) {
                   1629:                                break_point_t *break_point_ptr;
                   1630:                                GET_BREAK_POINT_PTR();
                   1631:                                if(num == 1) {
                   1632:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1633:                                                if(break_point_ptr->table[i].status) {
                   1634:                                                        telnet_printf("%d %c %04X\n", i + 1, break_point_ptr->table[i].status == 1 ? 'e' : 'd', break_point_ptr->table[i].addr);
                   1635:                                                }
                   1636:                                        }
                   1637:                                } else {
                   1638:                                        telnet_printf("invalid parameter number\n");
                   1639:                                }
                   1640:                        } else if(stricmp(params[0], "INTBP") == 0) {
                   1641:                                if(num >= 2 && num <= 4) {
                   1642:                                        int int_num = debugger_get_val(params[1]);
                   1643:                                        UINT8 ah = 0, ah_registered = 0;
                   1644:                                        UINT8 al = 0, al_registered = 0;
                   1645:                                        if(num >= 3) {
                   1646:                                                ah = debugger_get_val(params[2]);
                   1647:                                                ah_registered = 1;
                   1648:                                        }
                   1649:                                        if(num == 4) {
                   1650:                                                al = debugger_get_val(params[3]);
                   1651:                                                al_registered = 1;
                   1652:                                        }
                   1653:                                        bool found = false;
                   1654:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1655:                                                if(int_break_point.table[i].status == 0 || (
                   1656:                                                   int_break_point.table[i].int_num == int_num &&
                   1657:                                                   int_break_point.table[i].ah == ah && int_break_point.table[i].ah_registered == ah_registered &&
                   1658:                                                   int_break_point.table[i].al == al && int_break_point.table[i].al_registered == al_registered)) {
                   1659:                                                        int_break_point.table[i].int_num = int_num;
                   1660:                                                        int_break_point.table[i].ah = ah;
                   1661:                                                        int_break_point.table[i].ah_registered = ah_registered;
                   1662:                                                        int_break_point.table[i].al = al;
                   1663:                                                        int_break_point.table[i].al_registered = al_registered;
                   1664:                                                        int_break_point.table[i].status = 1;
                   1665:                                                        found = true;
                   1666:                                                }
                   1667:                                        }
                   1668:                                        if(!found) {
                   1669:                                                telnet_printf("too many break points\n");
                   1670:                                        }
                   1671:                                } else {
                   1672:                                        telnet_printf("invalid parameter number\n");
                   1673:                                }
                   1674:                        } else if(stricmp(params[0], "INTBC") == 0) {
                   1675:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1676:                                        memset(&int_break_point, 0, sizeof(int_break_point_t));
                   1677:                                } else if(num >= 2) {
                   1678:                                        for(int i = 1; i < num; i++) {
                   1679:                                                int index = debugger_get_val(params[i]);
                   1680:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1681:                                                        telnet_printf("invalid index %x\n", index);
                   1682:                                                } else {
                   1683:                                                        int_break_point.table[index - 1].int_num = 0;
                   1684:                                                        int_break_point.table[index - 1].ah = 0;
                   1685:                                                        int_break_point.table[index - 1].ah_registered = 0;
                   1686:                                                        int_break_point.table[index - 1].al = 0;
                   1687:                                                        int_break_point.table[index - 1].al_registered = 0;
                   1688:                                                        int_break_point.table[index - 1].status = 0;
                   1689:                                                }
                   1690:                                        }
                   1691:                                } else {
                   1692:                                        telnet_printf("invalid parameter number\n");
                   1693:                                }
                   1694:                        } else if(stricmp(params[0], "INTBD") == 0 || stricmp(params[0], "INTBE") == 0) {
                   1695:                                bool enabled = (params[0][strlen(params[0]) - 1] == 'E' || params[0][strlen(params[0]) - 1] == 'e');
                   1696:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1697:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1698:                                                if(int_break_point.table[i].status != 0) {
                   1699:                                                        int_break_point.table[i].status = enabled ? 1 : -1;
                   1700:                                                }
                   1701:                                        }
                   1702:                                } else if(num >= 2) {
                   1703:                                        for(int i = 1; i < num; i++) {
                   1704:                                                int index = debugger_get_val(params[i]);
                   1705:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1706:                                                        telnet_printf("invalid index %x\n", index);
                   1707:                                                } else if(int_break_point.table[index - 1].status == 0) {
                   1708:                                                        telnet_printf("break point %x is null\n", index);
                   1709:                                                } else {
                   1710:                                                        int_break_point.table[index - 1].status = enabled ? 1 : -1;
                   1711:                                                }
                   1712:                                        }
                   1713:                                } else {
                   1714:                                        telnet_printf("invalid parameter number\n");
                   1715:                                }
                   1716:                        } else if(stricmp(params[0], "INTBL") == 0) {
                   1717:                                if(num == 1) {
                   1718:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1719:                                                if(int_break_point.table[i].status) {
                   1720:                                                        telnet_printf("%d %c %02X", i + 1, int_break_point.table[i].status == 1 ? 'e' : 'd', int_break_point.table[i].int_num);
                   1721:                                                        if(int_break_point.table[i].ah_registered) {
                   1722:                                                                telnet_printf(" %02X", int_break_point.table[i].ah);
                   1723:                                                        }
                   1724:                                                        if(int_break_point.table[i].al_registered) {
                   1725:                                                                telnet_printf(" %02X", int_break_point.table[i].al);
                   1726:                                                        }
                   1727:                                                        telnet_printf("\n");
                   1728:                                                }
                   1729:                                        }
                   1730:                                } else {
                   1731:                                        telnet_printf("invalid parameter number\n");
                   1732:                                }
                   1733:                        } else if(stricmp(params[0], "G") == 0 || stricmp(params[0], "P") == 0) {
                   1734:                                if(num == 1 || num == 2) {
                   1735:                                        break_point_t break_point_stored;
                   1736:                                        bool break_points_stored = false;
                   1737:                                        
                   1738:                                        if(stricmp(params[0], "P") == 0) {
                   1739:                                                memcpy(&break_point_stored, &break_point, sizeof(break_point_t));
                   1740:                                                memset(&break_point, 0, sizeof(break_point_t));
                   1741:                                                break_points_stored = true;
                   1742:                                                
                   1743:                                                break_point.table[0].addr = (SREG(CS) << 4) + m_eip + debugger_dasm(buffer, SREG(CS), m_eip);
                   1744:                                                break_point.table[0].status = 1;
                   1745:                                        } else if(num >= 2) {
                   1746:                                                memcpy(&break_point_stored, &break_point, sizeof(break_point_t));
                   1747:                                                memset(&break_point, 0, sizeof(break_point_t));
                   1748:                                                break_points_stored = true;
                   1749:                                                
                   1750:                                                UINT32 seg = debugger_get_seg(params[1], SREG(CS));
                   1751:                                                UINT32 ofs = debugger_get_ofs(params[1]);
                   1752:                                                break_point.table[0].addr = (seg << 4) + ofs;
                   1753:                                                break_point.table[0].seg = seg;
                   1754:                                                break_point.table[0].ofs = ofs;
                   1755:                                                break_point.table[0].status = 1;
                   1756:                                        }
                   1757:                                        break_point.hit = rd_break_point.hit = wr_break_point.hit = in_break_point.hit = out_break_point.hit = int_break_point.hit = 0;
                   1758:                                        now_going = true;
                   1759:                                        now_suspended = false;
                   1760:                                        
                   1761:                                        telnet_command("\033[2l"); // key unlock
                   1762:                                        while(!m_halted && !now_suspended) {
                   1763:                                                if(telnet_kbhit()) {
                   1764:                                                        break;
                   1765:                                                }
                   1766:                                                Sleep(10);
                   1767:                                        }
                   1768:                                        now_going = false;
                   1769:                                        telnet_command("\033[2h"); // key lock
                   1770:                                        
                   1771:                                        if(!(break_point.hit || rd_break_point.hit || wr_break_point.hit || in_break_point.hit || out_break_point.hit || int_break_point.hit)) {
                   1772:                                                Sleep(100);
                   1773:                                                if(!m_halted && !now_suspended) {
                   1774:                                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1775:                                                        telnet_printf("waiting until cpu is suspended...\n");
                   1776:                                                }
                   1777:                                        }
                   1778:                                        while(!m_halted && !now_suspended) {
                   1779:                                                if(telnet_disconnected()) {
                   1780:                                                        break;
                   1781:                                                }
                   1782:                                                Sleep(10);
                   1783:                                        }
                   1784:                                        dasm_seg = SREG(CS);
                   1785:                                        dasm_ofs = m_eip;
                   1786:                                        
                   1787:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1788:                                        debugger_dasm(buffer, m_prev_cs, m_prev_eip);
                   1789:                                        telnet_printf("done\t%04X:%04X  %s\n", m_prev_cs, m_prev_eip, buffer);
                   1790:                                        
                   1791:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1792:                                        debugger_regs_info(buffer);
                   1793:                                        telnet_printf("%s", buffer);
                   1794:                                        
                   1795:                                        if(break_point.hit) {
                   1796:                                                if(stricmp(params[0], "G") == 0 && num == 1) {
                   1797:                                                        telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1798:                                                        telnet_printf("breaked at %04X:%04X: break point is hit\n", SREG(CS), m_eip);
                   1799:                                                }
                   1800:                                        } else if(rd_break_point.hit) {
                   1801:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1802:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1803:                                                rd_break_point.table[rd_break_point.hit - 1].seg, rd_break_point.table[rd_break_point.hit - 1].ofs,
                   1804:                                                m_prev_cs, m_prev_eip);
                   1805:                                        } else if(wr_break_point.hit) {
                   1806:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1807:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1808:                                                wr_break_point.table[wr_break_point.hit - 1].seg, wr_break_point.table[wr_break_point.hit - 1].ofs,
                   1809:                                                m_prev_cs, m_prev_eip);
                   1810:                                        } else if(in_break_point.hit) {
                   1811:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1812:                                                telnet_printf("breaked at %04X:%04X: port %04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1813:                                                in_break_point.table[in_break_point.hit - 1].addr,
                   1814:                                                m_prev_cs, m_prev_eip);
                   1815:                                        } else if(out_break_point.hit) {
                   1816:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1817:                                                telnet_printf("breaked at %04X:%04X: port %04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1818:                                                out_break_point.table[out_break_point.hit - 1].addr,
                   1819:                                                m_prev_cs, m_prev_eip);
                   1820:                                        } else if(int_break_point.hit) {
                   1821:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1822:                                                telnet_printf("breaked at %04X:%04X: INT %02x", SREG(CS), m_eip, int_break_point.table[int_break_point.hit - 1].int_num);
                   1823:                                                if(int_break_point.table[int_break_point.hit - 1].ah_registered) {
                   1824:                                                        telnet_printf(" AH=%02x", int_break_point.table[int_break_point.hit - 1].ah);
                   1825:                                                }
                   1826:                                                if(int_break_point.table[int_break_point.hit - 1].al_registered) {
                   1827:                                                        telnet_printf(" AL=%02x", int_break_point.table[int_break_point.hit - 1].al);
                   1828:                                                }
                   1829:                                                telnet_printf(" is raised at %04X:%04X\n", m_prev_cs, m_prev_eip);
                   1830:                                        } else {
                   1831:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1832:                                                telnet_printf("breaked at %04X:%04X: enter key was pressed\n", SREG(CS), m_eip);
                   1833:                                        }
                   1834:                                        if(break_points_stored) {
                   1835:                                                memcpy(&break_point, &break_point_stored, sizeof(break_point_t));
                   1836:                                        }
                   1837:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1838:                                        debugger_dasm(buffer, SREG(CS), m_eip);
                   1839:                                        telnet_printf("next\t%04X:%04X  %s\n", SREG(CS), m_eip, buffer);
                   1840:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1841:                                } else {
                   1842:                                        telnet_printf("invalid parameter number\n");
                   1843:                                }
                   1844:                        } else if(stricmp(params[0], "T") == 0) {
                   1845:                                if(num == 1 || num == 2) {
                   1846:                                        int steps = 1;
                   1847:                                        if(num >= 2) {
                   1848:                                                steps = debugger_get_val(params[1]);
                   1849:                                        }
                   1850:                                        
                   1851:                                        telnet_command("\033[2l"); // key unlock
                   1852:                                        while(steps-- > 0) {
                   1853:                                                break_point.hit = rd_break_point.hit = wr_break_point.hit = in_break_point.hit = out_break_point.hit = int_break_point.hit = 0;
                   1854:                                                now_going = false;
                   1855:                                                now_suspended = false;
                   1856:                                                
                   1857:                                                while(!m_halted && !now_suspended) {
                   1858:                                                        if(telnet_disconnected()) {
                   1859:                                                                break;
                   1860:                                                        }
                   1861:                                                        Sleep(10);
                   1862:                                                }
                   1863:                                                dasm_seg = SREG(CS);
                   1864:                                                dasm_ofs = m_eip;
                   1865:                                                
                   1866:                                                telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1867:                                                debugger_dasm(buffer, m_prev_cs, m_prev_eip);
                   1868:                                                telnet_printf("done\t%04X:%04X  %s\n", m_prev_cs, m_prev_eip, buffer);
                   1869:                                                
                   1870:                                                telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1871:                                                debugger_regs_info(buffer);
                   1872:                                                telnet_printf("%s", buffer);
                   1873:                                                
                   1874:                                                if(break_point.hit || rd_break_point.hit || wr_break_point.hit || in_break_point.hit || out_break_point.hit || int_break_point.hit || telnet_kbhit()) {
                   1875:                                                        break;
                   1876:                                                }
                   1877:                                        }
                   1878:                                        telnet_command("\033[2h"); // key lock
                   1879:                                        
                   1880:                                        if(!(break_point.hit || rd_break_point.hit || wr_break_point.hit || in_break_point.hit || out_break_point.hit || int_break_point.hit)) {
                   1881:                                                Sleep(100);
                   1882:                                                if(!m_halted && !now_suspended) {
                   1883:                                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1884:                                                        telnet_printf("waiting until cpu is suspended...\n");
                   1885:                                                }
                   1886:                                        }
                   1887:                                        while(!m_halted && !now_suspended) {
                   1888:                                                if(telnet_disconnected()) {
                   1889:                                                        break;
                   1890:                                                }
                   1891:                                                Sleep(10);
                   1892:                                        }
                   1893:                                        if(break_point.hit) {
                   1894:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1895:                                                telnet_printf("breaked at %04X:%04X: break point is hit\n", SREG(CS), m_eip);
                   1896:                                        } else if(rd_break_point.hit) {
                   1897:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1898:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1899:                                                rd_break_point.table[rd_break_point.hit - 1].seg, rd_break_point.table[rd_break_point.hit - 1].ofs,
                   1900:                                                m_prev_cs, m_prev_eip);
                   1901:                                        } else if(wr_break_point.hit) {
                   1902:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1903:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1904:                                                wr_break_point.table[wr_break_point.hit - 1].seg, wr_break_point.table[wr_break_point.hit - 1].ofs,
                   1905:                                                m_prev_cs, m_prev_eip);
                   1906:                                        } else if(in_break_point.hit) {
                   1907:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1908:                                                telnet_printf("breaked at %04X:%04X: port %04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1909:                                                in_break_point.table[in_break_point.hit - 1].addr,
                   1910:                                                m_prev_cs, m_prev_eip);
                   1911:                                        } else if(out_break_point.hit) {
                   1912:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1913:                                                telnet_printf("breaked at %04X:%04X: port %04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1914:                                                out_break_point.table[out_break_point.hit - 1].addr,
                   1915:                                                m_prev_cs, m_prev_eip);
                   1916:                                        } else if(int_break_point.hit) {
                   1917:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1918:                                                telnet_printf("breaked at %04X:%04X: INT %02x", SREG(CS), m_eip, int_break_point.table[int_break_point.hit - 1].int_num);
                   1919:                                                if(int_break_point.table[int_break_point.hit - 1].ah_registered) {
                   1920:                                                        telnet_printf(" AH=%02x", int_break_point.table[int_break_point.hit - 1].ah);
                   1921:                                                }
                   1922:                                                if(int_break_point.table[int_break_point.hit - 1].al_registered) {
                   1923:                                                        telnet_printf(" AL=%02x", int_break_point.table[int_break_point.hit - 1].al);
                   1924:                                                }
                   1925:                                                telnet_printf(" is raised at %04X:%04X\n", m_prev_cs, m_prev_eip);
                   1926:                                        } else if(steps > 0) {
                   1927:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1928:                                                telnet_printf("breaked at %04X:%04X: enter key was pressed\n", SREG(CS), m_eip);
                   1929:                                        }
                   1930:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1931:                                        debugger_dasm(buffer, SREG(CS), m_eip);
                   1932:                                        telnet_printf("next\t%04X:%04X  %s\n", SREG(CS), m_eip, buffer);
                   1933:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1934:                                } else {
                   1935:                                        telnet_printf("invalid parameter number\n");
                   1936:                                }
                   1937:                        } else if(stricmp(params[0], "Q") == 0) {
                   1938:                                break;
                   1939:                        } else if(stricmp(params[0], "X") == 0) {
                   1940:                                debugger_process_info(buffer);
                   1941:                                telnet_printf("%s", buffer);
                   1942:                        } else if(stricmp(params[0], ">") == 0) {
                   1943:                                if(num == 2) {
                   1944:                                        if(fp_debugger != NULL) {
                   1945:                                                fclose(fp_debugger);
                   1946:                                                fp_debugger = NULL;
                   1947:                                        }
                   1948:                                        fp_debugger = fopen(params[1], "w");
                   1949:                                } else {
                   1950:                                        telnet_printf("invalid parameter number\n");
                   1951:                                }
                   1952:                        } else if(stricmp(params[0], "<") == 0) {
                   1953:                                if(num == 2) {
                   1954:                                        if(fi_debugger != NULL) {
                   1955:                                                fclose(fi_debugger);
                   1956:                                                fi_debugger = NULL;
                   1957:                                        }
                   1958:                                        fi_debugger = fopen(params[1], "r");
                   1959:                                } else {
                   1960:                                        telnet_printf("invalid parameter number\n");
                   1961:                                }
                   1962:                        } else if(stricmp(params[0], "?") == 0) {
                   1963:                                telnet_printf("D [<start> [<end>]] - dump memory\n");
                   1964:                                telnet_printf("E[{B,W,D}] <address> <list> - edit memory (byte,word,dword)\n");
                   1965:                                telnet_printf("EA <address> \"<value>\" - edit memory (ascii)\n");
                   1966:                                telnet_printf("I[{B,W,D}] <port> - input port (byte,word,dword)\n");
                   1967:                                telnet_printf("O[{B,W,D}] <port> <value> - output port (byte,word,dword)\n");
                   1968:                                
                   1969:                                telnet_printf("R - show registers\n");
                   1970:                                telnet_printf("R <reg> <value> - edit register\n");
                   1971:                                telnet_printf("S <start> <end> <list> - search\n");
                   1972:                                telnet_printf("U [<start> [<end>]] - unassemble\n");
                   1973:                                
                   1974:                                telnet_printf("H <value> <value> - hexadd\n");
                   1975:                                
                   1976:                                telnet_printf("BP <address> - set breakpoint\n");
                   1977:                                telnet_printf("{R,W}BP <address> - set breakpoint (break at memory access)\n");
                   1978:                                telnet_printf("{I,O}BP <port> - set breakpoint (break at i/o access)\n");
                   1979:                                telnet_printf("INTBP <num> [<ah> [<al>]]- set breakpoint (break at interrupt)\n");
                   1980:                                telnet_printf("[{R,W,I,O,INT}]B{C,D,E} {*,<list>} - clear/disable/enable breakpoint(s)\n");
                   1981:                                telnet_printf("[{R,W,I,O,INT}]BL - list breakpoint(s)\n");
                   1982:                                
                   1983:                                telnet_printf("G - go (press enter key to break)\n");
                   1984:                                telnet_printf("G <address> - go and break at address\n");
                   1985:                                telnet_printf("P - trace one opcode (step over)\n");
                   1986:                                telnet_printf("T [<count>] - trace (step in)\n");
                   1987:                                telnet_printf("Q - quit\n");
                   1988:                                telnet_printf("X - show dos process info\n");
                   1989:                                
                   1990:                                telnet_printf("> <filename> - output logfile\n");
                   1991:                                telnet_printf("< <filename> - input commands from file\n");
                   1992:                                
                   1993:                                telnet_printf("<value> - hexa, decimal(%%d), ascii('a')\n");
                   1994:                                telnet_printf("<list> - <value> [<value> [<value> [...]]]\n");
                   1995:                        } else {
                   1996:                                telnet_printf("unknown command %s\n", params[0]);
                   1997:                        }
                   1998:                }
                   1999:        }
                   2000:        if(fp_debugger != NULL) {
                   2001:                fclose(fp_debugger);
                   2002:                fp_debugger = NULL;
                   2003:        }
                   2004:        if(fi_debugger != NULL) {
                   2005:                fclose(fi_debugger);
                   2006:                fi_debugger = NULL;
                   2007:        }
                   2008:        now_debugging = now_going = now_suspended = force_suspend = false;
                   2009:        closesocket(cli_socket);
                   2010: }
                   2011: 
                   2012: const char *debugger_get_ttermpro_path()
                   2013: {
                   2014:        static char path[MAX_PATH] = {0};
                   2015:        
                   2016:        if(getenv("ProgramFiles")) {
                   2017:                sprintf(path, "%s\\teraterm\\ttermpro.exe", getenv("ProgramFiles"));
                   2018:        }
                   2019:        return(path);
                   2020: }
                   2021: 
                   2022: const char *debugger_get_ttermpro_x86_path()
                   2023: {
                   2024:        static char path[MAX_PATH] = {0};
                   2025:        
                   2026:        if(getenv("ProgramFiles(x86)")) {
                   2027:                sprintf(path, "%s\\teraterm\\ttermpro.exe", getenv("ProgramFiles(x86)"));
                   2028:        }
                   2029:        return(path);
                   2030: }
                   2031: 
                   2032: const char *debugger_get_putty_path()
                   2033: {
                   2034:        static char path[MAX_PATH] = {0};
                   2035:        
                   2036:        if(getenv("ProgramFiles")) {
                   2037:                sprintf(path, "%s\\PuTTY\\putty.exe", getenv("ProgramFiles"));
                   2038:        }
                   2039:        return(path);
                   2040: }
                   2041: 
                   2042: const char *debugger_get_putty_x86_path()
                   2043: {
                   2044:        static char path[MAX_PATH] = {0};
                   2045:        
                   2046:        if(getenv("ProgramFiles(x86)")) {
                   2047:                sprintf(path, "%s\\PuTTY\\putty.exe", getenv("ProgramFiles(x86)"));
                   2048:        }
                   2049:        return(path);
                   2050: }
                   2051: 
                   2052: const char *debugger_get_telnet_path()
                   2053: {
                   2054:        // NOTE: When you run 32bit version of msdos.exe on Windows x64,
                   2055:        // C:\Windows\System32\telnet.exe is redirected to telnet.exe in SysWOW64.
                   2056:        // But 32bit version of telnet.exe will not be installed in SysWOW64
                   2057:        // and 64bit version of telnet.exe will be installed in System32.
                   2058:        static char path[MAX_PATH] = {0};
                   2059:        
                   2060:        if(getenv("windir") != NULL) {
                   2061:                sprintf(path, "%s\\System32\\telnet.exe", getenv("windir"));
                   2062:        }
                   2063:        return(path);
                   2064: }
                   2065: 
                   2066: DWORD WINAPI debugger_thread(LPVOID)
                   2067: {
                   2068:        WSADATA was_data;
                   2069:        struct sockaddr_in svr_addr;
                   2070:        struct sockaddr_in cli_addr;
                   2071:        int cli_addr_len = sizeof(cli_addr);
                   2072:        int port = 23;
                   2073:        int bind_stat = SOCKET_ERROR;
                   2074:        struct timeval timeout;
                   2075:        
                   2076:        WSAStartup(MAKEWORD(2,0), &was_data);
                   2077:        
                   2078:        if((svr_socket = socket(AF_INET, SOCK_STREAM, 0)) != INVALID_SOCKET) {
                   2079:                memset(&svr_addr, 0, sizeof(svr_addr));
                   2080:                svr_addr.sin_family = AF_INET;
                   2081:                svr_addr.sin_addr.s_addr = htonl(INADDR_ANY);
                   2082:                
                   2083:                while(!m_halted && port < 10000) {
                   2084:                        svr_addr.sin_port = htons(port);
                   2085:                        if((bind_stat = bind(svr_socket, (struct sockaddr *)&svr_addr, sizeof(svr_addr))) == 0) {
                   2086:                                break;
                   2087:                        } else {
                   2088:                                port = (port == 23) ? 9000 : (port + 1);
                   2089:                        }
                   2090:                }
                   2091:                if(bind_stat == 0) {
                   2092:                        timeout.tv_sec = 1;
                   2093:                        timeout.tv_usec = 0;
1.1.1.45  root     2094:                        setsockopt(svr_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
1.1.1.33  root     2095:                        
                   2096:                        listen(svr_socket, 1);
                   2097:                        
                   2098:                        char command[MAX_PATH] = {0};
                   2099:                        STARTUPINFO si;
                   2100:                        PROCESS_INFORMATION pi;
                   2101:                        
                   2102:                        if(_access(debugger_get_ttermpro_path(), 0) == 0) {
                   2103:                                sprintf(command, "%s localhost:%d /T=1", debugger_get_ttermpro_path(), port);
                   2104:                        } else if(_access(debugger_get_ttermpro_x86_path(), 0) == 0) {
                   2105:                                sprintf(command, "%s localhost:%d /T=1", debugger_get_ttermpro_x86_path(), port);
                   2106:                        } else if(_access(debugger_get_putty_path(), 0) == 0) {
                   2107:                                sprintf(command, "%s -telnet localhost %d", debugger_get_putty_path(), port);
                   2108:                        } else if(_access(debugger_get_putty_x86_path(), 0) == 0) {
                   2109:                                sprintf(command, "%s -telnet localhost %d", debugger_get_putty_x86_path(), port);
                   2110:                        } else if(_access(debugger_get_telnet_path(), 0) == 0) {
                   2111:                                sprintf(command, "%s -t vt100 localhost %d", debugger_get_telnet_path(), port);
                   2112:                        }
                   2113:                        if(command[0] != '\0') {
                   2114:                                memset(&si, 0, sizeof(STARTUPINFO));
                   2115:                                memset(&pi, 0, sizeof(PROCESS_INFORMATION));
                   2116:                                CreateProcess(NULL, command, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
                   2117:                        }
                   2118:                        
                   2119:                        while(!m_halted) {
                   2120:                                if((cli_socket = accept(svr_socket, (struct sockaddr *) &cli_addr, &cli_addr_len)) != INVALID_SOCKET) {
                   2121:                                        u_long val = 1;
                   2122:                                        ioctlsocket(cli_socket, FIONBIO, &val);
                   2123:                                        debugger_main();
                   2124:                                }
                   2125:                        }
                   2126:                }
                   2127:        }
                   2128:        WSACleanup();
                   2129:        return(0);
                   2130: }
                   2131: #endif
                   2132: 
                   2133: /* ----------------------------------------------------------------------------
1.1       root     2134:        main
                   2135: ---------------------------------------------------------------------------- */
                   2136: 
1.1.1.28  root     2137: BOOL WINAPI ctrl_handler(DWORD dwCtrlType)
                   2138: {
                   2139:        if(dwCtrlType == CTRL_BREAK_EVENT) {
1.1.1.33  root     2140:                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     2141: #ifdef USE_SERVICE_THREAD
                   2142:                        EnterCriticalSection(&key_buf_crit_sect);
                   2143: #endif
1.1.1.33  root     2144:                        key_buf_char->clear();
                   2145:                        key_buf_scan->clear();
1.1.1.35  root     2146: #ifdef USE_SERVICE_THREAD
                   2147:                        LeaveCriticalSection(&key_buf_crit_sect);
                   2148: #endif
1.1.1.33  root     2149:                }
                   2150: //             key_code = key_recv = 0;
1.1.1.28  root     2151:                return TRUE;
                   2152:        } else if(dwCtrlType == CTRL_C_EVENT) {
                   2153:                return TRUE;
                   2154:        } else if(dwCtrlType == CTRL_CLOSE_EVENT) {
                   2155:                // this program will be terminated abnormally, do minimum end process
                   2156:                exit_handler();
                   2157:                exit(1);
                   2158:        }
                   2159:        return FALSE;
                   2160: }
                   2161: 
                   2162: void exit_handler()
                   2163: {
                   2164:        if(temp_file_created) {
                   2165:                DeleteFile(temp_file_path);
                   2166:                temp_file_created = false;
                   2167:        }
                   2168:        if(key_buf_char != NULL) {
                   2169:                key_buf_char->release();
                   2170:                delete key_buf_char;
                   2171:                key_buf_char = NULL;
                   2172:        }
                   2173:        if(key_buf_scan != NULL) {
                   2174:                key_buf_scan->release();
                   2175:                delete key_buf_scan;
                   2176:                key_buf_scan = NULL;
                   2177:        }
1.1.1.32  root     2178: #ifdef SUPPORT_XMS
                   2179:        msdos_xms_release();
                   2180: #endif
1.1.1.28  root     2181:        hardware_release();
                   2182: }
                   2183: 
1.1.1.35  root     2184: #ifdef USE_VRAM_THREAD
1.1.1.28  root     2185: DWORD WINAPI vram_thread(LPVOID)
                   2186: {
                   2187:        while(!m_halted) {
                   2188:                EnterCriticalSection(&vram_crit_sect);
                   2189:                if(vram_length_char != 0 && vram_length_char == vram_last_length_char) {
                   2190:                        vram_flush_char();
                   2191:                }
                   2192:                if(vram_length_attr != 0 && vram_length_attr == vram_last_length_attr) {
                   2193:                        vram_flush_attr();
                   2194:                }
                   2195:                vram_last_length_char = vram_length_char;
                   2196:                vram_last_length_attr = vram_length_attr;
                   2197:                LeaveCriticalSection(&vram_crit_sect);
                   2198:                // this is about half the maximum keyboard repeat rate - any
                   2199:                // lower tends to be jerky, any higher misses updates
                   2200:                Sleep(15);
                   2201:        }
                   2202:        return 0;
                   2203: }
                   2204: #endif
                   2205: 
1.1.1.45  root     2206: long get_section_in_exec_file(FILE *fp, const char *name)
1.1.1.28  root     2207: {
                   2208:        UINT8 header[0x400];
                   2209:        
                   2210:        long position = ftell(fp);
                   2211:        fseek(fp, 0, SEEK_SET);
                   2212:        fread(header, sizeof(header), 1, fp);
                   2213:        fseek(fp, position, SEEK_SET);
                   2214:        
                   2215:        try {
                   2216:                _IMAGE_DOS_HEADER *dosHeader = (_IMAGE_DOS_HEADER *)(header + 0);
                   2217:                DWORD dwTopOfSignature = dosHeader->e_lfanew;
                   2218:                DWORD dwTopOfCoffHeader = dwTopOfSignature + 4;
                   2219:                _IMAGE_FILE_HEADER *coffHeader = (_IMAGE_FILE_HEADER *)(header + dwTopOfCoffHeader);
                   2220:                DWORD dwTopOfOptionalHeader = dwTopOfCoffHeader + sizeof(_IMAGE_FILE_HEADER);
                   2221:                DWORD dwTopOfFirstSectionHeader = dwTopOfOptionalHeader + coffHeader->SizeOfOptionalHeader;
                   2222:                
                   2223:                for(int i = 0; i < coffHeader->NumberOfSections; i++) {
                   2224:                        _IMAGE_SECTION_HEADER *sectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * i);
                   2225:                        if(memcmp(sectionHeader->Name, name, strlen(name)) == 0) {
                   2226:                                return(sectionHeader->PointerToRawData);
                   2227:                        }
                   2228:                }
                   2229:        } catch(...) {
                   2230:        }
                   2231:        return(0);
                   2232: }
                   2233: 
1.1.1.10  root     2234: bool is_started_from_command_prompt()
                   2235: {
1.1.1.18  root     2236:        bool ret = false;
                   2237:        
                   2238:        HMODULE hLibrary = LoadLibrary(_T("Kernel32.dll"));
                   2239:        if(hLibrary) {
                   2240:                typedef DWORD (WINAPI *GetConsoleProcessListFunction)(__out LPDWORD lpdwProcessList, __in DWORD dwProcessCount);
                   2241:                GetConsoleProcessListFunction lpfnGetConsoleProcessList;
                   2242:                lpfnGetConsoleProcessList = reinterpret_cast<GetConsoleProcessListFunction>(::GetProcAddress(hLibrary, "GetConsoleProcessList"));
                   2243:                if(lpfnGetConsoleProcessList) {
                   2244:                        DWORD pl;
                   2245:                        ret = (lpfnGetConsoleProcessList(&pl, 1) > 1);
                   2246:                        FreeLibrary(hLibrary);
                   2247:                        return(ret);
                   2248:                }
                   2249:                FreeLibrary(hLibrary);
                   2250:        }
                   2251:        
                   2252:        HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
                   2253:        if(hSnapshot != INVALID_HANDLE_VALUE) {
                   2254:                DWORD dwParentProcessID = 0;
                   2255:                PROCESSENTRY32 pe32;
                   2256:                pe32.dwSize = sizeof(PROCESSENTRY32);
                   2257:                if(Process32First(hSnapshot, &pe32)) {
                   2258:                        do {
                   2259:                                if(pe32.th32ProcessID == GetCurrentProcessId()) {
                   2260:                                        dwParentProcessID = pe32.th32ParentProcessID;
                   2261:                                        break;
                   2262:                                }
                   2263:                        } while(Process32Next(hSnapshot, &pe32));
                   2264:                }
                   2265:                CloseHandle(hSnapshot);
                   2266:                if(dwParentProcessID != 0) {
                   2267:                        HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwParentProcessID);
                   2268:                        if(hProcess != NULL) {
                   2269:                                HMODULE hMod;
                   2270:                                DWORD cbNeeded;
                   2271:                                if(EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded)) {
                   2272:                                        char module_name[MAX_PATH];
                   2273:                                        if(GetModuleBaseName(hProcess, hMod, module_name, sizeof(module_name))) {
                   2274:                                                ret = (_strnicmp(module_name, "cmd.exe", 7) == 0);
                   2275:                                        }
                   2276:                                }
                   2277:                                CloseHandle(hProcess);
                   2278:                        }
                   2279:                }
                   2280:        }
                   2281:        return(ret);
1.1.1.14  root     2282: }
                   2283: 
                   2284: BOOL is_greater_windows_version(DWORD dwMajorVersion, DWORD dwMinorVersion, WORD wServicePackMajor, WORD wServicePackMinor)
                   2285: {
1.1.1.24  root     2286:        // https://msdn.microsoft.com/en-us/library/windows/desktop/ms725491(v=vs.85).aspx
1.1.1.14  root     2287:        OSVERSIONINFOEX osvi;
                   2288:        DWORDLONG dwlConditionMask = 0;
                   2289:        int op = VER_GREATER_EQUAL;
                   2290:        
                   2291:        // Initialize the OSVERSIONINFOEX structure.
                   2292:        ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
                   2293:        osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
                   2294:        osvi.dwMajorVersion = dwMajorVersion;
                   2295:        osvi.dwMinorVersion = dwMinorVersion;
                   2296:        osvi.wServicePackMajor = wServicePackMajor;
                   2297:        osvi.wServicePackMinor = wServicePackMinor;
                   2298:        
                   2299:         // Initialize the condition mask.
                   2300:        VER_SET_CONDITION( dwlConditionMask, VER_MAJORVERSION, op );
                   2301:        VER_SET_CONDITION( dwlConditionMask, VER_MINORVERSION, op );
                   2302:        VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMAJOR, op );
                   2303:        VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMINOR, op );
                   2304:        
                   2305:        // Perform the test.
                   2306:        return VerifyVersionInfo(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR, dwlConditionMask);
                   2307: }
                   2308: 
1.1.1.27  root     2309: void get_sio_port_numbers()
                   2310: {
                   2311:        SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)};
                   2312:        HDEVINFO hDevInfo = 0;
                   2313:        HKEY hKey = 0;
                   2314:        if((hDevInfo = SetupDiGetClassDevs(&GUID_DEVINTERFACE_COMPORT, NULL, NULL, (DIGCF_PRESENT | DIGCF_DEVICEINTERFACE))) != 0) {
                   2315:                for(int i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++) {
                   2316:                        if((hKey = SetupDiOpenDevRegKey(hDevInfo, &DeviceInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_QUERY_VALUE)) != INVALID_HANDLE_VALUE) {
                   2317:                                char chData[256];
                   2318:                                DWORD dwType = 0;
                   2319:                                DWORD dwSize = sizeof(chData);
                   2320:                                int port_number = 0;
                   2321:                                
                   2322:                                if(RegQueryValueEx(hKey, _T("PortName"), NULL, &dwType, (BYTE *)chData, &dwSize) == ERROR_SUCCESS) {
                   2323:                                        if(_strnicmp(chData, "COM", 3) == 0) {
                   2324:                                                port_number = atoi(chData + 3);
                   2325:                                        }
                   2326:                                }
                   2327:                                RegCloseKey(hKey);
                   2328:                                
1.1.1.29  root     2329:                                if(sio_port_number[0] == port_number || sio_port_number[1] == port_number || sio_port_number[2] == port_number || sio_port_number[3] == port_number) {
1.1.1.27  root     2330:                                        continue;
                   2331:                                }
                   2332:                                if(sio_port_number[0] == 0) {
                   2333:                                        sio_port_number[0] = port_number;
                   2334:                                } else if(sio_port_number[1] == 0) {
                   2335:                                        sio_port_number[1] = port_number;
1.1.1.29  root     2336:                                } else if(sio_port_number[2] == 0) {
                   2337:                                        sio_port_number[2] = port_number;
                   2338:                                } else if(sio_port_number[3] == 0) {
                   2339:                                        sio_port_number[3] = port_number;
1.1.1.27  root     2340:                                }
1.1.1.29  root     2341:                                if(sio_port_number[0] != 0 && sio_port_number[1] != 0 && sio_port_number[2] != 0 && sio_port_number[3] != 0) {
1.1.1.27  root     2342:                                        break;
                   2343:                                }
                   2344:                        }
                   2345:                }
                   2346:        }
                   2347: }
                   2348: 
1.1.1.28  root     2349: #define IS_NUMERIC(c) ((c) >= '0' && (c) <= '9')
                   2350: 
1.1       root     2351: int main(int argc, char *argv[], char *envp[])
                   2352: {
1.1.1.9   root     2353:        int arg_offset = 0;
                   2354:        int standard_env = 0;
1.1.1.14  root     2355:        int buf_width = 0, buf_height = 0;
1.1.1.28  root     2356:        bool get_console_info_success = false;
                   2357:        bool screen_size_changed = false;
                   2358:        
                   2359:        _TCHAR path[MAX_PATH], full[MAX_PATH], *name = NULL;
                   2360:        GetModuleFileName(NULL, path, MAX_PATH);
                   2361:        GetFullPathName(path, MAX_PATH, full, &name);
1.1       root     2362:        
1.1.1.27  root     2363:        char dummy_argv_0[] = "msdos.exe";
                   2364:        char dummy_argv_1[MAX_PATH];
                   2365:        char *dummy_argv[256] = {dummy_argv_0, dummy_argv_1, 0};
                   2366:        char new_exec_file[MAX_PATH];
                   2367:        bool convert_cmd_file = false;
1.1.1.28  root     2368:        unsigned int code_page = 0;
1.1.1.27  root     2369:        
                   2370:        if(name != NULL && stricmp(name, "msdos.exe") != 0) {
1.1.1.28  root     2371:                // check if command file is embedded to this execution file
                   2372:                // if this execution file name is msdos.exe, don't check
1.1.1.27  root     2373:                FILE* fp = fopen(full, "rb");
1.1.1.28  root     2374:                long offset = get_section_in_exec_file(fp, ".msdos");
                   2375:                if(offset != 0) {
1.1.1.30  root     2376:                        UINT8 buffer[16];
1.1.1.28  root     2377:                        fseek(fp, offset, SEEK_SET);
                   2378:                        fread(buffer, sizeof(buffer), 1, fp);
                   2379:                        
                   2380:                        // restore flags
                   2381:                        stay_busy           = ((buffer[0] & 0x01) != 0);
                   2382:                        no_windows          = ((buffer[0] & 0x02) != 0);
                   2383:                        standard_env        = ((buffer[0] & 0x04) != 0);
                   2384:                        ignore_illegal_insn = ((buffer[0] & 0x08) != 0);
                   2385:                        limit_max_memory    = ((buffer[0] & 0x10) != 0);
                   2386:                        if((buffer[0] & 0x20) != 0) {
                   2387:                                get_sio_port_numbers();
                   2388:                        }
                   2389:                        if((buffer[0] & 0x40) != 0) {
                   2390:                                UMB_TOP = EMS_TOP + EMS_SIZE;
                   2391:                                support_ems = true;
1.1.1.30  root     2392:                        }
1.1.1.27  root     2393: #ifdef SUPPORT_XMS
1.1.1.30  root     2394:                        if((buffer[0] & 0x80) != 0) {
1.1.1.28  root     2395:                                support_xms = true;
                   2396:                        }
1.1.1.30  root     2397: #endif
1.1.1.28  root     2398:                        if((buffer[1] != 0 || buffer[2] != 0) && (buffer[3] != 0 || buffer[4] != 0)) {
                   2399:                                buf_width  = buffer[1] | (buffer[2] << 8);
                   2400:                                buf_height = buffer[3] | (buffer[4] << 8);
                   2401:                        }
                   2402:                        if(buffer[5] != 0) {
1.1.1.30  root     2403:                                dos_major_version = buffer[5];
                   2404:                                dos_minor_version = buffer[6];
                   2405:                        }
                   2406:                        if(buffer[7] != 0) {
                   2407:                                win_major_version = buffer[7];
                   2408:                                win_minor_version = buffer[8];
1.1.1.28  root     2409:                        }
1.1.1.30  root     2410:                        if((code_page = buffer[9] | (buffer[10] << 8)) != 0) {
1.1.1.28  root     2411:                                SetConsoleCP(code_page);
                   2412:                                SetConsoleOutputCP(code_page);
                   2413:                        }
1.1.1.30  root     2414:                        int name_len = buffer[11];
                   2415:                        int file_len = buffer[12] | (buffer[13] << 8) | (buffer[14] << 16) | (buffer[15] << 24);
1.1.1.28  root     2416:                        
                   2417:                        // restore command file name
                   2418:                        memset(dummy_argv_1, 0, sizeof(dummy_argv_1));
                   2419:                        fread(dummy_argv_1, name_len, 1, fp);
                   2420:                        
                   2421:                        if(name_len != 0 && _access(dummy_argv_1, 0) == 0) {
                   2422:                                // if original command file exists, create a temporary file name
                   2423:                                if(GetTempFileName(_T("."), _T("DOS"), 0, dummy_argv_1) != 0) {
                   2424:                                        // create a temporary command file in the current director
                   2425:                                        DeleteFile(dummy_argv_1);
1.1.1.27  root     2426:                                } else {
1.1.1.28  root     2427:                                        // create a temporary command file in the temporary folder
                   2428:                                        GetTempPath(MAX_PATH, path);
                   2429:                                        if(GetTempFileName(path, _T("DOS"), 0, dummy_argv_1) != 0) {
                   2430:                                                DeleteFile(dummy_argv_1);
                   2431:                                        } else {
                   2432:                                                sprintf(dummy_argv_1, "%s$DOSPRG$.TMP", path);
                   2433:                                        }
1.1.1.27  root     2434:                                }
1.1.1.28  root     2435:                                // check the command file type
                   2436:                                fread(buffer, 2, 1, fp);
                   2437:                                fseek(fp, -2, SEEK_CUR);
                   2438:                                if(memcmp(buffer, "MZ", 2) != 0) {
                   2439:                                        memcpy(dummy_argv_1 + strlen(dummy_argv_1) - 4, ".COM", 4);
                   2440:                                } else {
                   2441:                                        memcpy(dummy_argv_1 + strlen(dummy_argv_1) - 4, ".EXE", 4);
1.1.1.27  root     2442:                                }
                   2443:                        }
1.1.1.28  root     2444:                        
                   2445:                        // restore command file
                   2446:                        FILE* fo = fopen(dummy_argv_1, "wb");
                   2447:                        for(int i = 0; i < file_len; i++) {
                   2448:                                fputc(fgetc(fp), fo);
                   2449:                        }
                   2450:                        fclose(fo);
                   2451:                        
                   2452:                        GetFullPathName(dummy_argv_1, MAX_PATH, temp_file_path, NULL);
                   2453:                        temp_file_created = true;
                   2454:                        SetFileAttributes(temp_file_path, FILE_ATTRIBUTE_HIDDEN);
                   2455:                        
                   2456:                        // adjust argc/argv
                   2457:                        for(int i = 1; i < argc && (i + 1) < 256; i++) {
                   2458:                                dummy_argv[i + 1] = argv[i];
                   2459:                        }
                   2460:                        argc++;
                   2461:                        argv = dummy_argv;
1.1.1.27  root     2462:                }
                   2463:                fclose(fp);
                   2464:        }
1.1.1.9   root     2465:        for(int i = 1; i < argc; i++) {
1.1.1.25  root     2466:                if(_strnicmp(argv[i], "-b", 2) == 0) {
                   2467:                        stay_busy = true;
                   2468:                        arg_offset++;
1.1.1.27  root     2469:                } else if(_strnicmp(argv[i], "-c", 2) == 0) {
                   2470:                        if(argv[i][2] != '\0') {
                   2471:                                strcpy(new_exec_file, &argv[i][2]);
                   2472:                        } else {
                   2473:                                strcpy(new_exec_file, "new_exec_file.exe");
                   2474:                        }
                   2475:                        convert_cmd_file = true;
                   2476:                        arg_offset++;
1.1.1.28  root     2477:                } else if(_strnicmp(argv[i], "-p", 2) == 0) {
                   2478:                        if(IS_NUMERIC(argv[i][2])) {
                   2479:                                code_page = atoi(&argv[i][2]);
                   2480:                        } else {
                   2481:                                code_page = GetConsoleCP();
                   2482:                        }
                   2483:                        arg_offset++;
1.1.1.25  root     2484:                } else if(_strnicmp(argv[i], "-d", 2) == 0) {
                   2485:                        no_windows = true;
                   2486:                        arg_offset++;
                   2487:                } else if(_strnicmp(argv[i], "-e", 2) == 0) {
1.1.1.9   root     2488:                        standard_env = 1;
                   2489:                        arg_offset++;
1.1.1.14  root     2490:                } else if(_strnicmp(argv[i], "-i", 2) == 0) {
                   2491:                        ignore_illegal_insn = true;
                   2492:                        arg_offset++;
                   2493:                } else if(_strnicmp(argv[i], "-m", 2) == 0) {
                   2494:                        limit_max_memory = true;
                   2495:                        arg_offset++;
                   2496:                } else if(_strnicmp(argv[i], "-n", 2) == 0) {
1.1.1.17  root     2497:                        if(sscanf(argv[1] + 2, "%d,%d", &buf_height, &buf_width) != 2) {
                   2498:                                buf_width = buf_height = 0;
                   2499:                        }
                   2500:                        if(buf_width <= 0 || buf_width > 0x7fff) {
                   2501:                                buf_width = 80;
                   2502:                        }
                   2503:                        if(buf_height <= 0 || buf_height > 0x7fff) {
                   2504:                                buf_height = 25;
                   2505:                        }
1.1.1.14  root     2506:                        arg_offset++;
1.1.1.25  root     2507:                } else if(_strnicmp(argv[i], "-s", 2) == 0) {
1.1.1.28  root     2508:                        if(IS_NUMERIC(argv[i][2])) {
1.1.1.29  root     2509:                                char *p0 = &argv[i][2], *p1, *p2, *p3;
                   2510:                                if((p1 = strchr(p0, ',')) != NULL && IS_NUMERIC(p1[1])) {
                   2511:                                        sio_port_number[1] = atoi(p1 + 1);
                   2512:                                        if((p2 = strchr(p1, ',')) != NULL && IS_NUMERIC(p2[1])) {
                   2513:                                                sio_port_number[2] = atoi(p2 + 1);
                   2514:                                                if((p3 = strchr(p2, ',')) != NULL && IS_NUMERIC(p3[1])) {
                   2515:                                                        sio_port_number[3] = atoi(p3 + 1);
                   2516:                                                }
                   2517:                                        }
1.1.1.25  root     2518:                                }
1.1.1.29  root     2519:                                sio_port_number[0] = atoi(p0);
1.1.1.25  root     2520:                        }
1.1.1.29  root     2521:                        if(sio_port_number[0] == 0 || sio_port_number[1] == 0 || sio_port_number[2] == 0 || sio_port_number[3] == 0) {
1.1.1.27  root     2522:                                get_sio_port_numbers();
1.1.1.25  root     2523:                        }
                   2524:                        arg_offset++;
1.1.1.9   root     2525:                } else if(_strnicmp(argv[i], "-v", 2) == 0) {
1.1.1.17  root     2526:                        if(strlen(argv[i]) >= 5 && IS_NUMERIC(argv[i][2]) && argv[i][3] == '.' && IS_NUMERIC(argv[i][4]) && (argv[i][5] == '\0' || IS_NUMERIC(argv[i][5]))) {
1.1.1.30  root     2527:                                dos_major_version = argv[i][2] - '0';
                   2528:                                dos_minor_version = (argv[i][4] - '0') * 10 + (argv[i][5] ? (argv[i][5] - '0') : 0);
                   2529:                        }
                   2530:                        arg_offset++;
                   2531:                } else if(_strnicmp(argv[i], "-w", 2) == 0) {
                   2532:                        if(strlen(argv[i]) >= 5 && IS_NUMERIC(argv[i][2]) && argv[i][3] == '.' && IS_NUMERIC(argv[i][4]) && (argv[i][5] == '\0' || IS_NUMERIC(argv[i][5]))) {
                   2533:                                win_major_version = argv[i][2] - '0';
                   2534:                                win_minor_version = (argv[i][4] - '0') * 10 + (argv[i][5] ? (argv[i][5] - '0') : 0);
1.1.1.9   root     2535:                        }
                   2536:                        arg_offset++;
1.1.1.25  root     2537:                } else if(_strnicmp(argv[i], "-x", 2) == 0) {
                   2538:                        UMB_TOP = EMS_TOP + EMS_SIZE;
                   2539:                        support_ems = true;
                   2540: #ifdef SUPPORT_XMS
                   2541:                        support_xms = true;
                   2542: #endif
                   2543:                        arg_offset++;
1.1.1.9   root     2544:                } else {
                   2545:                        break;
                   2546:                }
                   2547:        }
                   2548:        if(argc < 2 + arg_offset) {
1.1       root     2549: #ifdef _WIN64
1.1.1.14  root     2550:                fprintf(stderr, "MS-DOS Player (" CPU_MODEL_NAME(CPU_MODEL) ") for Win32-x64 console\n\n");
1.1       root     2551: #else
1.1.1.14  root     2552:                fprintf(stderr, "MS-DOS Player (" CPU_MODEL_NAME(CPU_MODEL) ") for Win32 console\n\n");
1.1       root     2553: #endif
1.1.1.25  root     2554:                fprintf(stderr,
1.1.1.28  root     2555:                        "Usage: MSDOS [-b] [-c[(new exec file)] [-p[P]]] [-d] [-e] [-i] [-m] [-n[L[,C]]]\n"
1.1.1.30  root     2556:                        "             [-s[P1[,P2[,P3[,P4]]]]] [-vX.XX] [-wX.XX] [-x] (command) [options]\n"
1.1.1.25  root     2557:                        "\n"
                   2558:                        "\t-b\tstay busy during keyboard polling\n"
1.1.1.28  root     2559: #ifdef _WIN64
1.1.1.27  root     2560:                        "\t-c\tconvert command file to 64bit execution file\n"
1.1.1.28  root     2561: #else
1.1.1.27  root     2562:                        "\t-c\tconvert command file to 32bit execution file\n"
                   2563: #endif
1.1.1.28  root     2564:                        "\t-p\trecord current code page when convert command file\n"
1.1.1.25  root     2565:                        "\t-d\tpretend running under straight DOS, not Windows\n"
                   2566:                        "\t-e\tuse a reduced environment block\n"
                   2567:                        "\t-i\tignore invalid instructions\n"
                   2568:                        "\t-m\trestrict free memory to 0x7FFF paragraphs\n"
                   2569:                        "\t-n\tcreate a new buffer (25 lines, 80 columns by default)\n"
                   2570:                        "\t-s\tenable serial I/O and set host's COM port numbers\n"
                   2571:                        "\t-v\tset the DOS version\n"
1.1.1.30  root     2572:                        "\t-w\tset the Windows version\n"
1.1.1.19  root     2573: #ifdef SUPPORT_XMS
1.1.1.28  root     2574:                        "\t-x\tenable XMS and LIM EMS\n"
1.1.1.19  root     2575: #else
1.1.1.28  root     2576:                        "\t-x\tenable LIM EMS\n"
1.1.1.19  root     2577: #endif
                   2578:                );
1.1.1.10  root     2579:                
                   2580:                if(!is_started_from_command_prompt()) {
                   2581:                        fprintf(stderr, "\nStart this program from a command prompt!\n\nHit any key to quit...");
                   2582:                        while(!_kbhit()) {
                   2583:                                Sleep(10);
                   2584:                        }
                   2585:                }
1.1.1.20  root     2586: #ifdef _DEBUG
                   2587:                _CrtDumpMemoryLeaks();
                   2588: #endif
1.1       root     2589:                return(EXIT_FAILURE);
                   2590:        }
1.1.1.27  root     2591:        if(convert_cmd_file) {
                   2592:                retval = EXIT_FAILURE;
1.1.1.28  root     2593:                if(name != NULL/* && stricmp(name, "msdos.exe") == 0*/) {
1.1.1.27  root     2594:                        FILE *fp = NULL, *fs = NULL, *fo = NULL;
1.1.1.28  root     2595:                        int len = strlen(argv[arg_offset + 1]), data;
1.1.1.27  root     2596:                        
1.1.1.28  root     2597:                        if(!(len > 4 && (stricmp(argv[arg_offset + 1] + len - 4, ".COM") == 0 || stricmp(argv[arg_offset + 1] + len - 4, ".EXE") == 0))) {
                   2598:                                fprintf(stderr, "Specify command file with extenstion (.COM or .EXE)\n");
                   2599:                        } else if((fp = fopen(full, "rb")) == NULL) {
                   2600:                                fprintf(stderr, "Can't open '%s'\n", name);
1.1.1.27  root     2601:                        } else {
1.1.1.28  root     2602:                                long offset = get_section_in_exec_file(fp, ".msdos");
                   2603:                                if(offset != 0) {
                   2604:                                        UINT8 buffer[14];
                   2605:                                        fseek(fp, offset, SEEK_SET);
                   2606:                                        fread(buffer, sizeof(buffer), 1, fp);
                   2607:                                        memset(path, 0, sizeof(path));
                   2608:                                        fread(path, buffer[9], 1, fp);
                   2609:                                        fprintf(stderr, "Command file '%s' was already embedded to '%s'\n", path, name);
                   2610:                                } else if((fs = fopen(argv[arg_offset + 1], "rb")) == NULL) {
                   2611:                                        fprintf(stderr, "Can't open '%s'\n", argv[arg_offset + 1]);
                   2612:                                } else if((fo = fopen(new_exec_file, "wb")) == NULL) {
                   2613:                                        fprintf(stderr, "Can't open '%s'\n", new_exec_file);
                   2614:                                } else {
                   2615:                                        // read pe header of msdos.exe
                   2616:                                        UINT8 header[0x400];
                   2617:                                        fseek(fp, 0, SEEK_SET);
                   2618:                                        fread(header, sizeof(header), 1, fp);
                   2619:                                        
                   2620:                                        _IMAGE_DOS_HEADER *dosHeader = (_IMAGE_DOS_HEADER *)(header + 0);
                   2621:                                        DWORD dwTopOfSignature = dosHeader->e_lfanew;
                   2622:                                        DWORD dwTopOfCoffHeader = dwTopOfSignature + 4;
                   2623:                                        _IMAGE_FILE_HEADER *coffHeader = (_IMAGE_FILE_HEADER *)(header + dwTopOfCoffHeader);
                   2624:                                        DWORD dwTopOfOptionalHeader = dwTopOfCoffHeader + sizeof(_IMAGE_FILE_HEADER);
                   2625:                                        _IMAGE_OPTIONAL_HEADER *optionalHeader = (_IMAGE_OPTIONAL_HEADER *)(header + dwTopOfOptionalHeader);
                   2626:                                        DWORD dwTopOfFirstSectionHeader = dwTopOfOptionalHeader + coffHeader->SizeOfOptionalHeader;
                   2627:                                        
                   2628:                                        _IMAGE_SECTION_HEADER *lastSectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * (coffHeader->NumberOfSections - 1));
                   2629:                                        DWORD dwEndOfFile = lastSectionHeader->PointerToRawData + lastSectionHeader->SizeOfRawData;
                   2630:                                        DWORD dwLastSectionSize = lastSectionHeader->SizeOfRawData;
                   2631:                                        DWORD dwExtraLastSectionBytes = dwLastSectionSize % optionalHeader->SectionAlignment;
                   2632:                                        if(dwExtraLastSectionBytes != 0) {
                   2633:                                                DWORD dwRemain = optionalHeader->SectionAlignment - dwExtraLastSectionBytes;
                   2634:                                                dwLastSectionSize += dwRemain;
                   2635:                                        }
                   2636:                                        DWORD dwVirtualAddress = lastSectionHeader->VirtualAddress + dwLastSectionSize;
                   2637:                                        
                   2638:                                        // store msdos.exe
                   2639:                                        fseek(fp, 0, SEEK_SET);
                   2640:                                        for(int i = 0; i < dwEndOfFile; i++) {
                   2641:                                                if((data = fgetc(fp)) != EOF) {
                   2642:                                                        fputc(data, fo);
                   2643:                                                } else {
                   2644:                                                        // we should not reach here :-(
                   2645:                                                        fputc(0, fo);
                   2646:                                                }
                   2647:                                        }
                   2648:                                        
                   2649:                                        // store options
                   2650:                                        UINT8 flags = 0;
                   2651:                                        if(stay_busy) {
                   2652:                                                flags |= 0x01;
                   2653:                                        }
                   2654:                                        if(no_windows) {
                   2655:                                                flags |= 0x02;
                   2656:                                        }
                   2657:                                        if(standard_env) {
                   2658:                                                flags |= 0x04;
                   2659:                                        }
                   2660:                                        if(ignore_illegal_insn) {
                   2661:                                                flags |= 0x08;
                   2662:                                        }
                   2663:                                        if(limit_max_memory) {
                   2664:                                                flags |= 0x10;
                   2665:                                        }
1.1.1.29  root     2666:                                        if(sio_port_number[0] != 0 || sio_port_number[1] != 0 || sio_port_number[2] != 0 || sio_port_number[3] != 0) {
1.1.1.28  root     2667:                                                flags |= 0x20;
                   2668:                                        }
                   2669:                                        if(support_ems) {
                   2670:                                                flags |= 0x40;
                   2671:                                        }
1.1.1.30  root     2672: #ifdef SUPPORT_XMS
                   2673:                                        if(support_xms) {
                   2674:                                                flags |= 0x80;
                   2675:                                        }
                   2676: #endif
1.1.1.28  root     2677:                                        
                   2678:                                        fputc(flags, fo);
                   2679:                                        fputc((buf_width  >> 0) & 0xff, fo);
                   2680:                                        fputc((buf_width  >> 8) & 0xff, fo);
                   2681:                                        fputc((buf_height >> 0) & 0xff, fo);
                   2682:                                        fputc((buf_height >> 8) & 0xff, fo);
1.1.1.30  root     2683:                                        fputc(dos_major_version, fo);
                   2684:                                        fputc(dos_minor_version, fo);
                   2685:                                        fputc(win_major_version, fo);
                   2686:                                        fputc(win_minor_version, fo);
1.1.1.28  root     2687:                                        fputc((code_page >> 0) & 0xff, fo);
                   2688:                                        fputc((code_page >> 8) & 0xff, fo);
                   2689:                                        
                   2690:                                        // store command file info
                   2691:                                        GetFullPathName(argv[arg_offset + 1], MAX_PATH, full, &name);
                   2692:                                        int name_len = strlen(name);
                   2693:                                        fseek(fs, 0, SEEK_END);
                   2694:                                        long file_size = ftell(fs);
                   2695:                                        
                   2696:                                        fputc(name_len, fo);
                   2697:                                        fputc((file_size >>  0) & 0xff, fo);
                   2698:                                        fputc((file_size >>  8) & 0xff, fo);
                   2699:                                        fputc((file_size >> 16) & 0xff, fo);
                   2700:                                        fputc((file_size >> 24) & 0xff, fo);
                   2701:                                        fwrite(name, name_len, 1, fo);
                   2702:                                        
                   2703:                                        // store command file
                   2704:                                        fseek(fs, 0, SEEK_SET);
                   2705:                                        for(int i = 0; i < file_size; i++) {
                   2706:                                                if((data = fgetc(fs)) != EOF) {
                   2707:                                                        fputc(data, fo);
                   2708:                                                } else {
                   2709:                                                        // we should not reach here :-(
                   2710:                                                        fputc(0, fo);
                   2711:                                                }
                   2712:                                        }
                   2713:                                        
                   2714:                                        // store padding data and update pe header
1.1.1.29  root     2715:                                        _IMAGE_SECTION_HEADER *newSectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * coffHeader->NumberOfSections);
                   2716:                                        coffHeader->NumberOfSections++;
                   2717:                                        memset(newSectionHeader, 0, IMAGE_SIZEOF_SECTION_HEADER);
                   2718:                                        memcpy(newSectionHeader->Name, ".msdos", 6);
                   2719:                                        newSectionHeader->VirtualAddress = dwVirtualAddress;
                   2720:                                        newSectionHeader->PointerToRawData = dwEndOfFile;
                   2721:                                        newSectionHeader->Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_DISCARDABLE;
1.1.1.28  root     2722:                                        newSectionHeader->SizeOfRawData = 14 + name_len + file_size;
                   2723:                                        DWORD dwExtraRawBytes = newSectionHeader->SizeOfRawData % optionalHeader->FileAlignment;
                   2724:                                        if(dwExtraRawBytes != 0) {
1.1.1.29  root     2725:                                                static const char padding[] = "PADDINGXXPADDING";
1.1.1.28  root     2726:                                                DWORD dwRemain = optionalHeader->FileAlignment - dwExtraRawBytes;
                   2727:                                                for(int i = 0; i < dwRemain; i++) {
1.1.1.29  root     2728:                                                        if(i < 2) {
                   2729:                                                                fputc(padding[i & 15], fo);
                   2730:                                                        } else {
                   2731:                                                                fputc(padding[(i - 2) & 15], fo);
                   2732:                                                        }
1.1.1.28  root     2733:                                                }
                   2734:                                                newSectionHeader->SizeOfRawData += dwRemain;
                   2735:                                        }
                   2736:                                        newSectionHeader->Misc.VirtualSize = newSectionHeader->SizeOfRawData;
                   2737:                                        
                   2738:                                        DWORD dwNewSectionSize = newSectionHeader->SizeOfRawData;
                   2739:                                        DWORD dwExtraNewSectionBytes = dwNewSectionSize % optionalHeader->SectionAlignment;
                   2740:                                        if(dwExtraNewSectionBytes != 0) {
                   2741:                                                DWORD dwRemain = optionalHeader->SectionAlignment - dwExtraNewSectionBytes;
                   2742:                                                dwNewSectionSize += dwRemain;
                   2743:                                        }
                   2744:                                        optionalHeader->SizeOfImage += dwNewSectionSize;
                   2745:                                        
                   2746:                                        fseek(fo, 0, SEEK_SET);
                   2747:                                        fwrite(header, sizeof(header), 1, fo);
                   2748:                                        
                   2749:                                        fprintf(stderr, "'%s' is successfully created\n", new_exec_file);
                   2750:                                        retval = EXIT_SUCCESS;
1.1.1.27  root     2751:                                }
                   2752:                        }
                   2753:                        if(fp != NULL) {
                   2754:                                fclose(fp);
                   2755:                        }
                   2756:                        if(fs != NULL) {
                   2757:                                fclose(fs);
                   2758:                        }
                   2759:                        if(fo != NULL) {
                   2760:                                fclose(fo);
                   2761:                        }
                   2762:                }
                   2763: #ifdef _DEBUG
                   2764:                _CrtDumpMemoryLeaks();
                   2765: #endif
                   2766:                return(retval);
                   2767:        }
1.1       root     2768:        
1.1.1.14  root     2769:        is_vista_or_later = is_greater_windows_version(6, 0, 0, 0);
                   2770:        
1.1.1.23  root     2771:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     2772:        CONSOLE_SCREEN_BUFFER_INFO csbi;
1.1.1.14  root     2773:        CONSOLE_CURSOR_INFO ci;
1.1.1.23  root     2774:        
1.1.1.28  root     2775:        get_console_info_success = (GetConsoleScreenBufferInfo(hStdout, &csbi) != 0);
1.1.1.14  root     2776:        GetConsoleCursorInfo(hStdout, &ci);
1.1.1.24  root     2777:        GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &dwConsoleMode);
1.1       root     2778:        
1.1.1.14  root     2779:        for(int y = 0; y < SCR_BUF_WIDTH; y++) {
                   2780:                for(int x = 0; x < SCR_BUF_HEIGHT; x++) {
                   2781:                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   2782:                        SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1       root     2783:                }
                   2784:        }
1.1.1.28  root     2785:        if(get_console_info_success) {
1.1.1.12  root     2786:                scr_width = csbi.dwSize.X;
1.1.1.14  root     2787:                scr_height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
                   2788:                
1.1.1.28  root     2789:                // v-text shadow buffer size must be lesser than 0x7fd0
                   2790:                if((scr_width > SCR_BUF_WIDTH) || (scr_height > SCR_BUF_HEIGHT) || (scr_width * scr_height * 2 > 0x7fd0) ||
1.1.1.14  root     2791:                   (buf_width != 0 && buf_width != scr_width) || (buf_height != 0 && buf_height != scr_height)) {
                   2792:                        scr_width = min(buf_width != 0 ? buf_width : scr_width, SCR_BUF_WIDTH);
                   2793:                        scr_height = min(buf_height != 0 ? buf_height : scr_height, SCR_BUF_HEIGHT);
1.1.1.28  root     2794:                        if(scr_width * scr_height * 2 > 0x7fd0) {
1.1.1.14  root     2795:                                scr_width = 80;
                   2796:                                scr_height = 25;
                   2797:                        }
1.1.1.28  root     2798:                        screen_size_changed = true;
1.1.1.14  root     2799:                }
1.1.1.12  root     2800:        } else {
                   2801:                // for a proof (not a console)
                   2802:                scr_width = 80;
                   2803:                scr_height = 25;
                   2804:        }
1.1.1.14  root     2805:        scr_buf_size.X = scr_width;
                   2806:        scr_buf_size.Y = scr_height;
                   2807:        scr_buf_pos.X = scr_buf_pos.Y = 0;
                   2808:        scr_top = csbi.srWindow.Top;
1.1       root     2809:        cursor_moved = false;
                   2810:        
1.1.1.35  root     2811: #ifdef USE_SERVICE_THREAD
                   2812:        InitializeCriticalSection(&input_crit_sect);
                   2813:        InitializeCriticalSection(&key_buf_crit_sect);
                   2814:        InitializeCriticalSection(&putch_crit_sect);
                   2815: #endif
1.1.1.25  root     2816:        key_buf_char = new FIFO(256);
                   2817:        key_buf_scan = new FIFO(256);
1.1       root     2818:        
                   2819:        hardware_init();
                   2820:        
1.1.1.33  root     2821: #ifdef USE_DEBUGGER
                   2822:        debugger_init();
                   2823: #endif
                   2824:        
1.1.1.9   root     2825:        if(msdos_init(argc - (arg_offset + 1), argv + (arg_offset + 1), envp, standard_env)) {
1.1       root     2826:                retval = EXIT_FAILURE;
                   2827:        } else {
1.1.1.27  root     2828: #if defined(_MSC_VER) && (_MSC_VER >= 1400)
1.1.1.14  root     2829:                _set_invalid_parameter_handler((_invalid_parameter_handler)ignore_invalid_parameters);
                   2830: #endif
                   2831:                SetConsoleCtrlHandler(ctrl_handler, TRUE);
                   2832:                
1.1.1.28  root     2833:                if(screen_size_changed) {
1.1.1.24  root     2834:                        change_console_size(scr_width, scr_height);
                   2835:                }
1.1.1.8   root     2836:                TIMECAPS caps;
                   2837:                timeGetDevCaps(&caps, sizeof(TIMECAPS));
                   2838:                timeBeginPeriod(caps.wPeriodMin);
1.1.1.35  root     2839: #ifdef USE_VRAM_THREAD
1.1.1.14  root     2840:                InitializeCriticalSection(&vram_crit_sect);
                   2841:                CloseHandle(CreateThread(NULL, 4096, vram_thread, NULL, 0, NULL));
                   2842: #endif
1.1.1.33  root     2843: #ifdef USE_DEBUGGER
                   2844:                CloseHandle(CreateThread(NULL, 0, debugger_thread, NULL, 0, NULL));
                   2845:                // wait until telnet client starts and connects to me
                   2846:                if(_access(debugger_get_ttermpro_path(), 0) == 0 ||
                   2847:                   _access(debugger_get_ttermpro_x86_path(), 0) == 0 ||
                   2848:                   _access(debugger_get_putty_path(), 0) == 0 ||
                   2849:                   _access(debugger_get_putty_x86_path(), 0) == 0 ||
                   2850:                   _access(debugger_get_telnet_path(), 0) == 0) {
                   2851:                        for(int i = 0; i < 100 && cli_socket == 0; i++) {
                   2852:                                Sleep(100);
                   2853:                        }
                   2854:                }
                   2855: #endif
1.1       root     2856:                hardware_run();
1.1.1.35  root     2857: #ifdef USE_VRAM_THREAD
1.1.1.14  root     2858:                vram_flush();
                   2859:                DeleteCriticalSection(&vram_crit_sect);
                   2860: #endif
1.1.1.24  root     2861:                timeEndPeriod(caps.wPeriodMin);
1.1.1.14  root     2862:                
1.1.1.24  root     2863:                // hStdin/hStdout (and all handles) will be closed in msdos_finish()...
1.1.1.28  root     2864:                if(get_console_info_success) {
1.1.1.23  root     2865:                        hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.12  root     2866:                        if(restore_console_on_exit) {
1.1.1.14  root     2867:                                // window can't be bigger than buffer,
                   2868:                                // buffer can't be smaller than window,
                   2869:                                // so make a tiny window,
                   2870:                                // set the required buffer,
                   2871:                                // then set the required window
                   2872:                                SMALL_RECT rect;
                   2873:                                SET_RECT(rect, 0, csbi.srWindow.Top, 0, csbi.srWindow.Top);
                   2874:                                SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.12  root     2875:                                SetConsoleScreenBufferSize(hStdout, csbi.dwSize);
1.1.1.14  root     2876:                                SET_RECT(rect, 0, 0, csbi.srWindow.Right - csbi.srWindow.Left, csbi.srWindow.Bottom - csbi.srWindow.Top);
1.1.1.12  root     2877:                                SetConsoleWindowInfo(hStdout, TRUE, &rect);
                   2878:                        }
1.1.1.14  root     2879:                        SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   2880:                        SetConsoleCursorInfo(hStdout, &ci);
1.1.1.12  root     2881:                }
1.1.1.24  root     2882:                SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode);
                   2883:                
1.1       root     2884:                msdos_finish();
1.1.1.14  root     2885:                
                   2886:                SetConsoleCtrlHandler(ctrl_handler, FALSE);
1.1       root     2887:        }
1.1.1.35  root     2888:        if(temp_file_created) {
                   2889:                DeleteFile(temp_file_path);
                   2890:                temp_file_created = false;
                   2891:        }
1.1.1.10  root     2892:        hardware_finish();
                   2893:        
1.1.1.28  root     2894:        if(key_buf_char != NULL) {
                   2895:                key_buf_char->release();
                   2896:                delete key_buf_char;
                   2897:                key_buf_char = NULL;
                   2898:        }
                   2899:        if(key_buf_scan != NULL) {
                   2900:                key_buf_scan->release();
                   2901:                delete key_buf_scan;
                   2902:                key_buf_scan = NULL;
                   2903:        }
1.1.1.35  root     2904: #ifdef USE_SERVICE_THREAD
                   2905:        DeleteCriticalSection(&input_crit_sect);
                   2906:        DeleteCriticalSection(&key_buf_crit_sect);
                   2907:        DeleteCriticalSection(&putch_crit_sect);
                   2908: #endif
1.1.1.20  root     2909: #ifdef _DEBUG
                   2910:        _CrtDumpMemoryLeaks();
                   2911: #endif
1.1       root     2912:        return(retval);
                   2913: }
                   2914: 
1.1.1.20  root     2915: /* ----------------------------------------------------------------------------
                   2916:        console
                   2917: ---------------------------------------------------------------------------- */
                   2918: 
1.1.1.14  root     2919: void change_console_size(int width, int height)
1.1.1.12  root     2920: {
1.1.1.23  root     2921:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.12  root     2922:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   2923:        SMALL_RECT rect;
                   2924:        COORD co;
                   2925:        
                   2926:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.14  root     2927:        if(csbi.srWindow.Top != 0 || csbi.dwCursorPosition.Y > height - 1) {
                   2928:                if(csbi.srWindow.Right - csbi.srWindow.Left + 1 == width && csbi.srWindow.Bottom - csbi.srWindow.Top + 1 == height) {
                   2929:                        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &csbi.srWindow);
                   2930:                        SET_RECT(rect, 0, 0, width - 1, height - 1);
                   2931:                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   2932:                } else if(csbi.dwCursorPosition.Y > height - 1) {
                   2933:                        SET_RECT(rect, 0, csbi.dwCursorPosition.Y - (height - 1), width - 1, csbi.dwCursorPosition.Y);
                   2934:                        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   2935:                        SET_RECT(rect, 0, 0, width - 1, height - 1);
                   2936:                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.12  root     2937:                }
                   2938:        }
1.1.1.14  root     2939:        if(csbi.dwCursorPosition.Y > height - 1) {
1.1.1.12  root     2940:                co.X = csbi.dwCursorPosition.X;
1.1.1.14  root     2941:                co.Y = min(height - 1, csbi.dwCursorPosition.Y - csbi.srWindow.Top);
1.1.1.12  root     2942:                SetConsoleCursorPosition(hStdout, co);
                   2943:                cursor_moved = true;
                   2944:        }
1.1.1.14  root     2945:        
                   2946:        // window can't be bigger than buffer,
                   2947:        // buffer can't be smaller than window,
                   2948:        // so make a tiny window,
                   2949:        // set the required buffer,
                   2950:        // then set the required window
                   2951:        SET_RECT(rect, 0, csbi.srWindow.Top, 0, csbi.srWindow.Top);
1.1.1.12  root     2952:        SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.14  root     2953:        co.X = width;
                   2954:        co.Y = height;
1.1.1.12  root     2955:        SetConsoleScreenBufferSize(hStdout, co);
1.1.1.14  root     2956:        SET_RECT(rect, 0, 0, width - 1, height - 1);
                   2957:        SetConsoleWindowInfo(hStdout, TRUE, &rect);
                   2958:        
                   2959:        scr_width = scr_buf_size.X = width;
                   2960:        scr_height = scr_buf_size.Y = height;
                   2961:        scr_top = 0;
                   2962:        
                   2963:        clear_scr_buffer(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
                   2964:        
                   2965:        int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.15  root     2966:        text_vram_end_address = text_vram_top_address + regen;
                   2967:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
                   2968:        
1.1.1.14  root     2969:        if(regen > 0x4000) {
                   2970:                regen = 0x8000;
                   2971:                vram_pages = 1;
                   2972:        } else if(regen > 0x2000) {
                   2973:                regen = 0x4000;
                   2974:                vram_pages = 2;
                   2975:        } else if(regen > 0x1000) {
                   2976:                regen = 0x2000;
                   2977:                vram_pages = 4;
                   2978:        } else {
                   2979:                regen = 0x1000;
                   2980:                vram_pages = 8;
                   2981:        }
1.1.1.15  root     2982:        *(UINT16 *)(mem + 0x44a) = scr_width;
                   2983:        *(UINT16 *)(mem + 0x44c) = regen;
                   2984:        *(UINT8  *)(mem + 0x484) = scr_height - 1;
                   2985:        
1.1.1.24  root     2986:        mouse.min_position.x = 0;
                   2987:        mouse.min_position.y = 0;
1.1.1.34  root     2988:        mouse.max_position.x = 8 * (scr_width  - 1);
                   2989:        mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24  root     2990:        
1.1.1.15  root     2991:        restore_console_on_exit = true;
1.1.1.14  root     2992: }
                   2993: 
                   2994: void clear_scr_buffer(WORD attr)
                   2995: {
                   2996:        for(int y = 0; y < scr_height; y++) {
                   2997:                for(int x = 0; x < scr_width; x++) {
                   2998:                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   2999:                        SCR_BUF(y,x).Attributes = attr;
                   3000:                }
                   3001:        }
1.1.1.12  root     3002: }
                   3003: 
1.1.1.24  root     3004: bool update_console_input()
1.1       root     3005: {
1.1.1.35  root     3006: #ifdef USE_SERVICE_THREAD
                   3007:        EnterCriticalSection(&input_crit_sect);
                   3008: #endif
1.1.1.23  root     3009:        HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
1.1.1.8   root     3010:        DWORD dwNumberOfEvents = 0;
1.1       root     3011:        DWORD dwRead;
                   3012:        INPUT_RECORD ir[16];
1.1.1.24  root     3013:        CONSOLE_SCREEN_BUFFER_INFO csbi = {0};
                   3014:        bool result = false;
1.1       root     3015:        
1.1.1.8   root     3016:        if(GetNumberOfConsoleInputEvents(hStdin, &dwNumberOfEvents) && dwNumberOfEvents != 0) {
                   3017:                if(ReadConsoleInputA(hStdin, ir, 16, &dwRead)) {
                   3018:                        for(int i = 0; i < dwRead; i++) {
1.1.1.24  root     3019:                                if(ir[i].EventType & MOUSE_EVENT) {
1.1.1.34  root     3020:                                        if(mouse.hidden == 0) {
                   3021:                                                // NOTE: if restore_console_on_exit, console is not scrolled
                   3022:                                                if(!restore_console_on_exit && csbi.srWindow.Bottom == 0) {
                   3023:                                                        GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
                   3024:                                                }
                   3025:                                                // FIXME: character size is always 8x8 ???
                   3026:                                                int x = 8 * (ir[i].Event.MouseEvent.dwMousePosition.X);
                   3027:                                                int y = 8 * (ir[i].Event.MouseEvent.dwMousePosition.Y - csbi.srWindow.Top);
                   3028:                                                
                   3029:                                                if(mouse.position.x != x || mouse.position.y != y) {
                   3030:                                                        mouse.position.x = x;
                   3031:                                                        mouse.position.y = y;
                   3032:                                                        mouse.status |= 1;
1.1.1.43  root     3033:                                                        mouse.status_alt |= 1;
1.1.1.34  root     3034:                                                }
                   3035:                                        }
                   3036:                                        if(ir[i].Event.MouseEvent.dwEventFlags == 0) {
                   3037:                                                for(int i = 0; i < MAX_MOUSE_BUTTONS; i++) {
                   3038:                                                        static const DWORD bits[] = {
                   3039:                                                                FROM_LEFT_1ST_BUTTON_PRESSED,   // left
                   3040:                                                                RIGHTMOST_BUTTON_PRESSED,       // right
                   3041:                                                                FROM_LEFT_2ND_BUTTON_PRESSED,   // middle
                   3042:                                                        };
                   3043:                                                        bool prev_status = mouse.buttons[i].status;
                   3044:                                                        mouse.buttons[i].status = ((ir[i].Event.MouseEvent.dwButtonState & bits[i]) != 0);
                   3045:                                                        
                   3046:                                                        if(!prev_status && mouse.buttons[i].status) {
                   3047:                                                                mouse.buttons[i].pressed_times++;
                   3048:                                                                mouse.buttons[i].pressed_position.x = mouse.position.x;
                   3049:                                                                mouse.buttons[i].pressed_position.y = mouse.position.x;
1.1.1.43  root     3050:                                                                if(i < 2) {
                   3051:                                                                        mouse.status_alt |= 2 << (i * 2);
                   3052:                                                                }
1.1.1.34  root     3053:                                                                mouse.status |= 2 << (i * 2);
                   3054:                                                        } else if(prev_status && !mouse.buttons[i].status) {
                   3055:                                                                mouse.buttons[i].released_times++;
                   3056:                                                                mouse.buttons[i].released_position.x = mouse.position.x;
                   3057:                                                                mouse.buttons[i].released_position.y = mouse.position.x;
1.1.1.43  root     3058:                                                                if(i < 2) {
                   3059:                                                                        mouse.status_alt |= 4 << (i * 2);
                   3060:                                                                }
1.1.1.34  root     3061:                                                                mouse.status |= 4 << (i * 2);
1.1.1.14  root     3062:                                                        }
                   3063:                                                }
                   3064:                                        }
1.1.1.24  root     3065:                                } else if(ir[i].EventType & KEY_EVENT) {
1.1.1.33  root     3066:                                        // update keyboard flags in bios data area
1.1.1.35  root     3067:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & CAPSLOCK_ON) {
                   3068:                                                mem[0x417] |= 0x40;
1.1.1.33  root     3069:                                        } else {
1.1.1.35  root     3070:                                                mem[0x417] &= ~0x40;
1.1.1.33  root     3071:                                        }
1.1.1.35  root     3072:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & NUMLOCK_ON) {
                   3073:                                                mem[0x417] |= 0x20;
1.1.1.33  root     3074:                                        } else {
1.1.1.35  root     3075:                                                mem[0x417] &= ~0x20;
                   3076:                                        }
                   3077:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & SCROLLLOCK_ON) {
                   3078:                                                mem[0x417] |= 0x10;
                   3079:                                        } else {
                   3080:                                                mem[0x417] &= ~0x10;
1.1.1.33  root     3081:                                        }
                   3082:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
1.1.1.43  root     3083:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3084:                                                        mouse.status_alt |= 0x80;
                   3085:                                                }
1.1.1.33  root     3086:                                                mem[0x417] |= 0x08;
                   3087:                                        } else {
                   3088:                                                mem[0x417] &= ~0x08;
                   3089:                                        }
1.1.1.35  root     3090:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
1.1.1.43  root     3091:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3092:                                                        mouse.status_alt |= 0x40;
                   3093:                                                }
1.1.1.35  root     3094:                                                mem[0x417] |= 0x04;
1.1.1.33  root     3095:                                        } else {
1.1.1.35  root     3096:                                                mem[0x417] &= ~0x04;
1.1.1.33  root     3097:                                        }
                   3098:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) {
1.1.1.43  root     3099:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3100:                                                        mouse.status_alt |= 0x20;
                   3101:                                                }
1.1.1.33  root     3102:                                                if(!(mem[0x417] & 0x03)) {
                   3103:                                                        mem[0x417] |= 0x02; // left shift
                   3104:                                                }
                   3105:                                        } else {
                   3106:                                                mem[0x417] &= ~0x03;
                   3107:                                        }
1.1.1.35  root     3108:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & LEFT_ALT_PRESSED) {
                   3109:                                                mem[0x418] |= 0x02;
                   3110:                                        } else {
                   3111:                                                mem[0x418] &= ~0x02;
                   3112:                                        }
                   3113:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & LEFT_CTRL_PRESSED) {
                   3114:                                                mem[0x418] |= 0x01;
                   3115:                                        } else {
                   3116:                                                mem[0x418] &= ~0x01;
                   3117:                                        }
1.1.1.33  root     3118:                                        
1.1.1.28  root     3119:                                        // set scan code of last pressed/release key to kbd_data (in-port 60h)
1.1.1.24  root     3120:                                        kbd_data = ir[i].Event.KeyEvent.wVirtualScanCode;
1.1.1.33  root     3121:                                        kbd_status |= 1;
                   3122:                                        
                   3123:                                        // update dos key buffer
                   3124:                                        UINT8 chr = ir[i].Event.KeyEvent.uChar.AsciiChar;
                   3125:                                        UINT8 scn = ir[i].Event.KeyEvent.wVirtualScanCode & 0xff;
                   3126:                                        
                   3127:                                        if(ir[i].Event.KeyEvent.bKeyDown) {
1.1.1.28  root     3128:                                                // make
1.1.1.24  root     3129:                                                kbd_data &= 0x7f;
                   3130:                                                
1.1.1.33  root     3131:                                                if(chr == 0x00) {
1.1.1.24  root     3132:                                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
                   3133:                                                                if(scn >= 0x3b && scn <= 0x44) {
                   3134:                                                                        scn += 0x68 - 0x3b;     // F1 to F10
                   3135:                                                                } else if(scn == 0x57 || scn == 0x58) {
                   3136:                                                                        scn += 0x8b - 0x57;     // F11 & F12
                   3137:                                                                } else if(scn >= 0x47 && scn <= 0x53) {
                   3138:                                                                        scn += 0x97 - 0x47;     // edit/arrow clusters
                   3139:                                                                } else if(scn == 0x35) {
                   3140:                                                                        scn = 0xa4;             // keypad /
                   3141:                                                                }
                   3142:                                                        } else if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
                   3143:                                                                if(scn == 0x07) {
                   3144:                                                                        chr = 0x1e;     // Ctrl+^
                   3145:                                                                } else if(scn == 0x0c) {
                   3146:                                                                        chr = 0x1f;     // Ctrl+_
                   3147:                                                                } else if(scn >= 0x35 && scn <= 0x58) {
                   3148:                                                                        static const UINT8 ctrl_map[] = {
                   3149:                                                                                0x95,   // keypad /
                   3150:                                                                                0,
                   3151:                                                                                0x96,   // keypad *
                   3152:                                                                                0, 0, 0,
                   3153:                                                                                0x5e,   // F1
                   3154:                                                                                0x5f,   // F2
                   3155:                                                                                0x60,   // F3
                   3156:                                                                                0x61,   // F4
                   3157:                                                                                0x62,   // F5
                   3158:                                                                                0x63,   // F6
                   3159:                                                                                0x64,   // F7
                   3160:                                                                                0x65,   // F8
                   3161:                                                                                0x66,   // F9
                   3162:                                                                                0x67,   // F10
                   3163:                                                                                0,
                   3164:                                                                                0,
                   3165:                                                                                0x77,   // Home
                   3166:                                                                                0x8d,   // Up
                   3167:                                                                                0x84,   // PgUp
                   3168:                                                                                0x8e,   // keypad -
                   3169:                                                                                0x73,   // Left
                   3170:                                                                                0x8f,   // keypad center
                   3171:                                                                                0x74,   // Right
                   3172:                                                                                0x90,   // keyapd +
                   3173:                                                                                0x75,   // End
                   3174:                                                                                0x91,   // Down
                   3175:                                                                                0x76,   // PgDn
                   3176:                                                                                0x92,   // Insert
                   3177:                                                                                0x93,   // Delete
                   3178:                                                                                0, 0, 0,
                   3179:                                                                                0x89,   // F11
                   3180:                                                                                0x8a,   // F12
                   3181:                                                                        };
                   3182:                                                                        scn = ctrl_map[scn - 0x35];
                   3183:                                                                }
                   3184:                                                        } else if(ir[i].Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) {
                   3185:                                                                if(scn >= 0x3b && scn <= 0x44) {
                   3186:                                                                        scn += 0x54 - 0x3b;     // F1 to F10
                   3187:                                                                } else if(scn == 0x57 || scn == 0x58) {
                   3188:                                                                        scn += 0x87 - 0x57;     // F11 & F12
                   3189:                                                                }
                   3190:                                                        } else if(scn == 0x57 || scn == 0x58) {
                   3191:                                                                scn += 0x85 - 0x57;
                   3192:                                                        }
                   3193:                                                        // ignore shift, ctrl, alt, win and menu keys
                   3194:                                                        if(scn != 0x1d && scn != 0x2a && scn != 0x36 && scn != 0x38 && (scn < 0x5b || scn > 0x5e)) {
1.1.1.32  root     3195:                                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3196: #ifdef USE_SERVICE_THREAD
                   3197:                                                                        EnterCriticalSection(&key_buf_crit_sect);
                   3198: #endif
1.1.1.32  root     3199:                                                                        if(chr == 0) {
                   3200:                                                                                key_buf_char->write(0x00);
                   3201:                                                                                key_buf_scan->write(ir[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY ? 0xe0 : 0x00);
                   3202:                                                                        }
                   3203:                                                                        key_buf_char->write(chr);
                   3204:                                                                        key_buf_scan->write(scn);
1.1.1.35  root     3205: #ifdef USE_SERVICE_THREAD
                   3206:                                                                        LeaveCriticalSection(&key_buf_crit_sect);
                   3207: #endif
1.1.1.24  root     3208:                                                                }
                   3209:                                                        }
                   3210:                                                } else {
                   3211:                                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
                   3212:                                                                chr = 0;
                   3213:                                                                if(scn >= 0x02 && scn <= 0x0e) {
                   3214:                                                                        scn += 0x78 - 0x02;     // 1 to 0 - =
                   3215:                                                                }
                   3216:                                                        }
1.1.1.32  root     3217:                                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3218: #ifdef USE_SERVICE_THREAD
                   3219:                                                                EnterCriticalSection(&key_buf_crit_sect);
                   3220: #endif
1.1.1.32  root     3221:                                                                key_buf_char->write(chr);
                   3222:                                                                key_buf_scan->write(scn);
1.1.1.35  root     3223: #ifdef USE_SERVICE_THREAD
                   3224:                                                                LeaveCriticalSection(&key_buf_crit_sect);
                   3225: #endif
1.1.1.32  root     3226:                                                        }
1.1.1.24  root     3227:                                                }
1.1.1.33  root     3228:                                        } else if(chr == 0x03 && (ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))) {
                   3229:                                                // ctrl-break, ctrl-c
                   3230:                                                if(scn == 0x46) {
                   3231:                                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3232: #ifdef USE_SERVICE_THREAD
                   3233:                                                                EnterCriticalSection(&key_buf_crit_sect);
                   3234: #endif
1.1.1.33  root     3235:                                                                key_buf_char->write(0x00);
                   3236:                                                                key_buf_scan->write(0x00);
1.1.1.35  root     3237: #ifdef USE_SERVICE_THREAD
                   3238:                                                                LeaveCriticalSection(&key_buf_crit_sect);
                   3239: #endif
1.1.1.33  root     3240:                                                        }
                   3241:                                                        ctrl_break_pressed = true;
                   3242:                                                        mem[0x471] = 0x80;
                   3243:                                                        raise_int_1bh = true;
                   3244:                                                } else {
                   3245:                                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3246: #ifdef USE_SERVICE_THREAD
                   3247:                                                                EnterCriticalSection(&key_buf_crit_sect);
                   3248: #endif
1.1.1.33  root     3249:                                                                key_buf_char->write(chr);
                   3250:                                                                key_buf_scan->write(scn);
1.1.1.35  root     3251: #ifdef USE_SERVICE_THREAD
                   3252:                                                                LeaveCriticalSection(&key_buf_crit_sect);
                   3253: #endif
1.1.1.33  root     3254:                                                        }
                   3255:                                                        ctrl_c_pressed = (scn == 0x2e);
                   3256:                                                }
                   3257:                                        } else {
                   3258:                                                // break
                   3259:                                                kbd_data |= 0x80;
1.1       root     3260:                                        }
1.1.1.24  root     3261:                                        result = key_changed = true;
1.1.1.36  root     3262:                                        // IME may be on and it may causes screen scroll up and cursor position change
                   3263:                                        cursor_moved = true;
1.1       root     3264:                                }
                   3265:                        }
                   3266:                }
                   3267:        }
1.1.1.35  root     3268: #ifdef USE_SERVICE_THREAD
                   3269:        LeaveCriticalSection(&input_crit_sect);
                   3270: #endif
1.1.1.24  root     3271:        return(result);
1.1.1.8   root     3272: }
                   3273: 
1.1.1.14  root     3274: bool update_key_buffer()
1.1.1.8   root     3275: {
1.1.1.35  root     3276:        if(update_console_input()) {
                   3277:                return(true);
                   3278:        }
                   3279:        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   3280: #ifdef USE_SERVICE_THREAD
                   3281:                EnterCriticalSection(&key_buf_crit_sect);
                   3282: #endif
                   3283:                bool empty = key_buf_char->empty();
                   3284: #ifdef USE_SERVICE_THREAD
                   3285:                LeaveCriticalSection(&key_buf_crit_sect);
                   3286: #endif
                   3287:                if(!empty) return(true);
                   3288:        }
                   3289:        return(false);
1.1.1.8   root     3290: }
                   3291: 
1.1.1.20  root     3292: /* ----------------------------------------------------------------------------
                   3293:        MS-DOS virtual machine
                   3294: ---------------------------------------------------------------------------- */
                   3295: 
1.1.1.32  root     3296: static const struct {
1.1.1.33  root     3297:        char *name;
                   3298:        DWORD lcid;
                   3299:        char *std;
                   3300:        char *dlt;
                   3301: } tz_table[] = {
                   3302:        // https://science.ksc.nasa.gov/software/winvn/userguide/3_1_4.htm
                   3303: //     0       GMT             Greenwich Mean Time             GMT0
                   3304:        {"GMT Standard Time",                   0x0809, "GMT", "BST"},          // (UTC+00:00) GB London (en-gb)
                   3305:        {"GMT Standard Time",                   0x1809, "GMT", "IST"},          // (UTC+00:00) IE Dublin (en-ie)
                   3306:        {"GMT Standard Time",                   0x0000, "WET", "WES"},          // (UTC+00:00) PT Lisbon
                   3307:        {"Greenwich Standard Time",             0x0000, "GMT", "GST"},          // (UTC+00:00) IS Reykjavik
                   3308: //     2       FST     FDT     Fernando De Noronha Std         FST2FDT
                   3309:        {"Mid-Atlantic Standard Time",          0x0416, "FST", "FDT"},          // (UTC-02:00) BR Noronha (pt-br)
                   3310:        {"UTC-02",                              0x0416, "FST", "FDT"},          // (UTC-02:00) BR Noronha (pt-br)
                   3311: //     3       BST             Brazil Standard Time            BST3
                   3312:        {"Bahia Standard Time",                 0x0000, "BST", "BDT"},          // (UTC-03:00) BR Bahia
                   3313:        {"SA Eastern Standard Time",            0x0000, "BST", "BDT"},          // (UTC-03:00) BR Fortaleza
                   3314:        {"Tocantins Standard Time",             0x0000, "BST", "BDT"},          // (UTC-03:00) BR Palmas
                   3315: //     3       EST     EDT     Eastern Standard (Brazil)       EST3EDT
                   3316:        {"E. South America Standard Time",      0x0000, "EST", "EDT"},          // (UTC-03:00) BR Sao Paulo
                   3317: //     3       GST             Greenland Standard Time         GST3
                   3318:        {"Greenland Standard Time",             0x0000, "GST", "GDT"},          // (UTC-03:00) GL Godthab
                   3319: //     3:30    NST     NDT     Newfoundland Standard Time      NST3:30NDT
                   3320:        {"Newfoundland Standard Time",          0x0000, "NST", "NDT"},          // (UTC-03:30) CA St.Johns
                   3321: //     4       AST     ADT     Atlantic Standard Time          AST4ADT
                   3322:        {"Atlantic Standard Time",              0x0000, "AST", "ADT"},          // (UTC-04:00) CA Halifax
                   3323: //     4       WST     WDT     Western Standard (Brazil)       WST4WDT
                   3324:        {"Central Brazilian Standard Time",     0x0000, "WST", "WDT"},          // (UTC-04:00) BR Cuiaba
                   3325:        {"SA Western Standard Time",            0x0000, "WST", "WDT"},          // (UTC-04:00) BR Manaus
                   3326: //     5       EST     EDT     Eastern Standard Time           EST5EDT
                   3327:        {"Eastern Standard Time",               0x0000, "EST", "EDT"},          // (UTC-05:00) US New York
                   3328:        {"Eastern Standard Time (Mexico)",      0x0000, "EST", "EDT"},          // (UTC-05:00) MX Cancun
                   3329:        {"US Eastern Standard Time",            0x0000, "EST", "EDT"},          // (UTC-05:00) US Indianapolis
                   3330: //     5       CST     CDT     Chile Standard Time             CST5CDT
                   3331:        {"Pacific SA Standard Time",            0x0000, "CST", "CDT"},          // (UTC-04:00) CL Santiago
                   3332: //     5       AST     ADT     Acre Standard Time              AST5ADT
                   3333:        {"SA Pacific Standard Time",            0x0000, "AST", "ADT"},          // (UTC-05:00) BR Rio Branco
                   3334: //     5       CST     CDT     Cuba Standard Time              CST5CDT
                   3335:        {"Cuba Standard Time",                  0x0000, "CST", "CDT"},          // (UTC-05:00) CU Havana
                   3336: //     6       CST     CDT     Central Standard Time           CST6CDT
                   3337:        {"Canada Central Standard Time",        0x0000, "CST", "CDT"},          // (UTC-06:00) CA Regina
                   3338:        {"Central Standard Time",               0x0000, "CST", "CDT"},          // (UTC-06:00) US Chicago
                   3339:        {"Central Standard Time (Mexico)",      0x0000, "CST", "CDT"},          // (UTC-06:00) MX Mexico City
                   3340: //     6       EST     EDT     Easter Island Standard          EST6EDT
                   3341:        {"Easter Island Standard Time",         0x0000, "EST", "EDT"},          // (UTC-06:00) CL Easter
                   3342: //     7       MST     MDT     Mountain Standard Time          MST7MDT
                   3343:        {"Mountain Standard Time",              0x0000, "MST", "MDT"},          // (UTC-07:00) US Denver
                   3344:        {"Mountain Standard Time (Mexico)",     0x0000, "MST", "MDT"},          // (UTC-07:00) MX Chihuahua
                   3345:        {"US Mountain Standard Time",           0x0000, "MST", "MDT"},          // (UTC-07:00) US Phoenix
                   3346: //     8       PST     PDT     Pacific Standard Time           PST8PDT
                   3347:        {"Pacific Standard Time",               0x0000, "PST", "PDT"},          // (UTC-08:00) US Los Angeles
                   3348:        {"Pacific Standard Time (Mexico)",      0x0000, "PST", "PDT"},          // (UTC-08:00) MX Tijuana
                   3349: //     9       AKS     AKD     Alaska Standard Time            AKS9AKD
                   3350: //     9       YST     YDT     Yukon Standard Time             YST9YST
                   3351:        {"Alaskan Standard Time",               0x0000, "AKS", "AKD"},          // (UTC-09:00) US Anchorage
                   3352: //     10      HST     HDT     Hawaii Standard Time            HST10HDT
                   3353:        {"Aleutian Standard Time",              0x0000, "HST", "HDT"},          // (UTC-10:00) US Aleutian
                   3354:        {"Hawaiian Standard Time",              0x0000, "HST", "HDT"},          // (UTC-10:00) US Honolulu
                   3355: //     11      SST             Samoa Standard Time             SST11
                   3356:        {"Samoa Standard Time",                 0x0000, "SST", "SDT"},          // (UTC-11:00) US Samoa
                   3357: //     -12     NZS     NZD     New Zealand Standard Time       NZS-12NZD
                   3358:        {"New Zealand Standard Time",           0x0000, "NZS", "NZD"},          // (UTC+12:00) NZ Auckland
                   3359: //     -10     GST             Guam Standard Time              GST-10
                   3360:        {"West Pacific Standard Time",          0x0000, "GST", "GDT"},          // (UTC+10:00) GU Guam
                   3361: //     -10     EAS     EAD     Eastern Australian Standard     EAS-10EAD
                   3362:        {"AUS Eastern Standard Time",           0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Sydney
                   3363:        {"E. Australia Standard Time",          0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Brisbane
                   3364:        {"Tasmania Standard Time",              0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Hobart
                   3365: //     -9:30   CAS     CAD     Central Australian Standard     CAS-9:30CAD
                   3366:        {"AUS Central Standard Time",           0x0000, "CAS", "CAD"},          // (UTC+09:30) AU Darwin
                   3367:        {"Cen. Australia Standard Time",        0x0000, "CAS", "CAD"},          // (UTC+09:30) AU Adelaide
                   3368: //     -9      JST             Japan Standard Time             JST-9
                   3369:        {"Tokyo Standard Time",                 0x0000, "JST", "JDT"},          // (UTC+09:00) JP Tokyo
                   3370: //     -9      KST     KDT     Korean Standard Time            KST-9KDT
                   3371:        {"Korea Standard Time",                 0x0000, "KST", "KDT"},          // (UTC+09:00) KR Seoul
                   3372:        {"North Korea Standard Time",           0x0000, "KST", "KDT"},          // (UTC+08:30) KP Pyongyang
                   3373: //     -8      HKT             Hong Kong Time                  HKT-8
                   3374:        {"China Standard Time",                 0x0C04, "HKT", "HKS"},          // (UTC+08:00) HK Hong Kong (zh-hk)
                   3375: //     -8      CCT             China Coast Time                CCT-8
                   3376:        {"China Standard Time",                 0x0000, "CCT", "CDT"},          // (UTC+08:00) CN Shanghai
                   3377:        {"Taipei Standard Time",                0x0000, "CCT", "CDT"},          // (UTC+08:00) TW Taipei
                   3378: //     -8      SST             Singapore Standard Time         SST-8
                   3379:        {"Singapore Standard Time",             0x0000, "SST", "SDT"},          // (UTC+08:00) SG Singapore
                   3380: //     -8      WAS     WAD     Western Australian Standard     WAS-8WAD
                   3381:        {"Aus Central W. Standard Time",        0x0000, "WAS", "WAD"},          // (UTC+08:45) AU Eucla
                   3382:        {"W. Australia Standard Time",          0x0000, "WAS", "WAD"},          // (UTC+08:00) AU Perth
                   3383: //     -7:30   JT              Java Standard Time              JST-7:30
                   3384: //     -7      NST             North Sumatra Time              NST-7
                   3385:        {"SE Asia Standard Time",               0x0000, "NST", "NDT"},          // (UTC+07:00) ID Jakarta
                   3386: //     -5:30   IST             Indian Standard Time            IST-5:30
                   3387:        {"India Standard Time",                 0x0000, "IST", "IDT"},          // (UTC+05:30) IN Calcutta
                   3388: //     -3:30   IST     IDT     Iran Standard Time              IST-3:30IDT
                   3389:        {"Iran Standard Time",                  0x0000, "IST", "IDT"},          // (UTC+03:30) IR Tehran
                   3390: //     -3      MSK     MSD     Moscow Winter Time              MSK-3MSD
                   3391:        {"Belarus Standard Time",               0x0000, "MSK", "MSD"},          // (UTC+03:00) BY Minsk
                   3392:        {"Russian Standard Time",               0x0000, "MSK", "MSD"},          // (UTC+03:00) RU Moscow
                   3393: //     -2      EET             Eastern Europe Time             EET-2
                   3394:        {"E. Europe Standard Time",             0x0000, "EET", "EES"},          // (UTC+02:00) MD Chisinau
                   3395:        {"FLE Standard Time",                   0x0000, "EET", "EES"},          // (UTC+02:00) UA Kiev
                   3396:        {"GTB Standard Time",                   0x0000, "EET", "EES"},          // (UTC+02:00) RO Bucharest
                   3397:        {"Kaliningrad Standard Time",           0x0000, "EET", "EES"},          // (UTC+02:00) RU Kaliningrad
                   3398: //     -2      IST     IDT     Israel Standard Time            IST-2IDT
                   3399:        {"Israel Standard Time",                0x0000, "IST", "IDT"},          // (UTC+02:00) IL Jerusalem
                   3400: //     -1      MEZ     MES     Middle European Time            MEZ-1MES
                   3401: //     -1      SWT     SST     Swedish Winter Time             SWT-1SST
                   3402: //     -1      FWT     FST     French Winter Time              FWT-1FST
                   3403: //     -1      CET     CES     Central European Time           CET-1CES
                   3404:        {"Central Europe Standard Time",        0x0000, "CET", "CES"},          // (UTC+01:00) HU Budapest
                   3405:        {"Central European Standard Time",      0x0000, "CET", "CES"},          // (UTC+01:00) PL Warsaw
                   3406:        {"Romance Standard Time",               0x0000, "CET", "CES"},          // (UTC+01:00) FR Paris
                   3407:        {"W. Europe Standard Time",             0x0000, "CET", "CES"},          // (UTC+01:00) DE Berlin
                   3408: //     -1      WAT             West African Time               WAT-1
                   3409:        {"Namibia Standard Time",               0x0000, "WAT", "WAS"},          // (UTC+01:00) NA Windhoek
                   3410:        {"W. Central Africa Standard Time",     0x0000, "WAT", "WAS"},          // (UTC+01:00) NG Lagos
                   3411: //     0       UTC             Universal Coordinated Time      UTC0
                   3412:        {"UTC",                                 0x0000, "UTC", ""   },          // (UTC+00:00) GMT+0
                   3413:        {"UTC-02",                              0x0000, "UTC", ""   },          // (UTC-02:00) GMT+2
                   3414:        {"UTC-08",                              0x0000, "UTC", ""   },          // (UTC-08:00) GMT+8
                   3415:        {"UTC-09",                              0x0000, "UTC", ""   },          // (UTC-09:00) GMT+9
                   3416:        {"UTC-11",                              0x0000, "UTC", ""   },          // (UTC-11:00) GMT+11
                   3417:        {"UTC+12",                              0x0000, "UTC", ""   },          // (UTC+12:00) GMT-12
                   3418: };
                   3419: 
                   3420: static const struct {
1.1.1.32  root     3421:        UINT16 code;
                   3422:        char *message_english;
                   3423:        char *message_japanese;
                   3424: } standard_error_table[] = {
                   3425:        {0x01,  "Invalid function", "�����ȃt�@���N�V�����ł�."},
                   3426:        {0x02,  "File not found", "�t�@�C�������‚���܂���."},
                   3427:        {0x03,  "Path not found", "�p�X�����‚���܂���."},
                   3428:        {0x04,  "Too many open files", "�J����Ă���t�@�C�����������܂�."},
                   3429:        {0x05,  "Access denied", "�A�N�Z�X�͋��ۂ���܂���."},
                   3430:        {0x06,  "Invalid handle", "�����ȃn���h���ł�."},
                   3431:        {0x07,  "Memory control blocks destroyed", "����������u���b�N���j������܂���."},
                   3432:        {0x08,  "Insufficient memory", "������������܂���."},
                   3433:        {0x09,  "Invalid memory block address", "�����ȃ������u���b�N�̃A�h���X�ł�."},
                   3434:        {0x0A,  "Invalid Environment", "�����Ȋ‹��ł�."},
                   3435:        {0x0B,  "Invalid format", "�����ȃt�H�[�}�b�g�ł�."},
                   3436:        {0x0C,  "Invalid function parameter", "�����ȃt�@���N�V�����p�����[�^�ł�."},
                   3437:        {0x0D,  "Invalid data", "�����ȃf�[�^�ł�."},
                   3438:        {0x0F,  "Invalid drive specification", "�����ȃh���C�u�̎w��ł�."},
                   3439:        {0x10,  "Attempt to remove current directory", "���݂̃f�B���N�g�����폜���悤�Ƃ��܂���."},
                   3440:        {0x11,  "Not same device", "�����f�o�C�X�ł͂���܂���."},
                   3441:        {0x12,  "No more files", "�t�@�C���͂���ȏ゠��܂���."},
                   3442:        {0x13,  "Write protect error", "�������ݕی�G���[�ł�."},
                   3443:        {0x14,  "Invalid unit", "�����ȃ��j�b�g�ł�."},
                   3444:        {0x15,  "Not ready", "�������ł��Ă��܂���."},
                   3445:        {0x16,  "Invalid device request", "�����ȃf�o�C�X�v���ł�."},
                   3446:        {0x17,  "Data error", "�f�[�^�G���[�ł�."},
                   3447:        {0x18,  "Invalid device request parameters", "�����ȃf�o�C�X�v���̃p�����[�^�ł�."},
                   3448:        {0x19,  "Seek error", "�V�[�N�G���[�ł�."},
                   3449:        {0x1A,  "Invalid media type", "�����ȃ��f�B�A�̎�ނł�."},
                   3450:        {0x1B,  "Sector not found", "�Z�N�^�����‚���܂���."},
                   3451:        {0x1C,  "Printer out of paper error", "�v�����^�̗p��������܂���."},
                   3452:        {0x1D,  "Write fault error", "�������݃G���[�ł�."},
                   3453:        {0x1E,  "Read fault error", "�ǂݎ��G���[�ł�."},
                   3454:        {0x1F,  "General failure", "�G���[�ł�."},
                   3455:        {0x20,  "Sharing violation", "���L�ᔽ�ł�."},
                   3456:        {0x21,  "Lock violation", "���b�N�ᔽ�ł�."},
                   3457:        {0x22,  "Invalid disk change", "�����ȃf�B�X�N�̌����ł�."},
                   3458:        {0x23,  "FCB unavailable", "FCB �͎g���܂���."},
                   3459:        {0x24,  "System resource exhausted", "�V�X�e�����\�[�X�͂����g���܂���."},
                   3460:        {0x25,  "Code page mismatch", "�R�[�h �y�[�W����v���܂���."},
                   3461:        {0x26,  "Out of input", "���͂��I���܂���."},
                   3462:        {0x27,  "Insufficient disk space", "�f�B�X�N�̋󂫗̈悪����܂���."},
                   3463: /*
                   3464:        {0x32,  "Network request not supported", NULL},
                   3465:        {0x33,  "Remote computer not listening", NULL},
                   3466:        {0x34,  "Duplicate name on network", NULL},
                   3467:        {0x35,  "Network name not found", NULL},
                   3468:        {0x36,  "Network busy", NULL},
                   3469:        {0x37,  "Network device no longer exists", NULL},
                   3470:        {0x38,  "Network BIOS command limit exceeded", NULL},
                   3471:        {0x39,  "Network adapter hardware error", NULL},
                   3472:        {0x3A,  "Incorrect response from network", NULL},
                   3473:        {0x3B,  "Unexpected network error", NULL},
                   3474:        {0x3C,  "Incompatible remote adapter", NULL},
                   3475:        {0x3D,  "Print queue full", NULL},
                   3476:        {0x3E,  "Queue not full", NULL},
                   3477:        {0x3F,  "Not enough space to print file", NULL},
                   3478:        {0x40,  "Network name was deleted", NULL},
                   3479:        {0x41,  "Network: Access denied", NULL},
                   3480:        {0x42,  "Network device type incorrect", NULL},
                   3481:        {0x43,  "Network name not found", NULL},
                   3482:        {0x44,  "Network name limit exceeded", NULL},
                   3483:        {0x45,  "Network BIOS session limit exceeded", NULL},
                   3484:        {0x46,  "Temporarily paused", NULL},
                   3485:        {0x47,  "Network request not accepted", NULL},
                   3486:        {0x48,  "Network print/disk redirection paused", NULL},
                   3487:        {0x49,  "Network software not installed", NULL},
                   3488:        {0x4A,  "Unexpected adapter close", NULL},
                   3489: */
                   3490:        {0x50,  "File exists", "�t�@�C���͑��݂��܂�."},
1.1.1.33  root     3491:        {0x52,  "Cannot make directory entry", "�f�B���N�g���G���g�����쐬�ł��܂���."},
1.1.1.32  root     3492:        {0x53,  "Fail on INT 24", "INT 24 �Ŏ��s���܂���."},
                   3493:        {0x54,  "Too many redirections", "���_�C���N�g���������܂�."},
                   3494:        {0x55,  "Duplicate redirection", "���_�C���N�g���d�����Ă��܂�."},
                   3495:        {0x56,  "Invalid password", "�p�X���[�h���Ⴂ�܂�."},
                   3496:        {0x57,  "Invalid parameter", "�p�����[�^�̎w�肪�Ⴂ�܂�."},
                   3497:        {0x58,  "Network data fault", "�l�b�g���[�N�f�[�^�̃G���[�ł�."},
                   3498:        {0x59,  "Function not supported by network", "�t�@���N�V�����̓l�b�g���[�N�ŃT�|�[�g����Ă��܂���."},
                   3499:        {0x5A,  "Required system component not installe", "�K�v�ȃV�X�e�� �R���|�[�l���g���g�ݍ��܂�Ă��܂���."},
                   3500: /*
                   3501:        {0x64,  "Unknown error", "�s���ȃG���[�ł�."},
                   3502:        {0x65,  "Not ready", "�������ł��Ă��܂���."},
                   3503:        {0x66,  "EMS memory no longer valid", "EMS �������͂����L���ł͂���܂���."},
                   3504:        {0x67,  "CDROM not High Sierra or ISO-9660 format", "CDROM �� High Sierra �܂��� ISO-9660 �t�H�[�}�b�g�ł͂���܂���."},
                   3505:        {0x68,  "Door open", "���o�[���‚܂��Ă��܂���."
                   3506: */
                   3507:        {0xB0,  "Volume is not locked", "�{�����[�������b�N����Ă��܂���."},
                   3508:        {0xB1,  "Volume is locked in drive", "�{�����[�������b�N����Ă��܂�."},
                   3509:        {0xB2,  "Volume is not removable", "�{�����[���͎��O���ł��܂���."},
                   3510:        {0xB4,  "Lock count has been exceeded", "�{�����[��������ȏネ�b�N�ł��܂���."},
                   3511:        {0xB5,  "A valid eject request failed", "���o���Ɏ��s���܂���."},
                   3512:        {-1  ,  "Unknown error", "�s���ȃG���[�ł�."},
                   3513: };
                   3514: 
                   3515: static const struct {
                   3516:        UINT16 code;
                   3517:        char *message_english;
                   3518:        char *message_japanese;
                   3519: } param_error_table[] = {
                   3520:        {0x01,  "Too many parameters", "�p�����[�^���������܂�."},
                   3521:        {0x02,  "Required parameter missing", "�p�����[�^������܂���."},
                   3522:        {0x03,  "Invalid switch", "�����ȃX�C�b�`�ł�."},
                   3523:        {0x04,  "Invalid keyword", "�����ȃL�[���[�h�ł�."},
                   3524:        {0x06,  "Parameter value not in allowed range", "�p�����[�^�̒l���͈͂𒴂��Ă��܂�."},
                   3525:        {0x07,  "Parameter value not allowed", "���̃p�����[�^�̒l�͎g���܂���."},
                   3526:        {0x08,  "Parameter value not allowed", "���̃p�����[�^�̒l�͎g���܂���."},
                   3527:        {0x09,  "Parameter format not correct", "�p�����[�^�̏������Ⴂ�܂�."},
                   3528:        {0x0A,  "Invalid parameter", "�����ȃp�����[�^�ł�."},
                   3529:        {0x0B,  "Invalid parameter combination", "�����ȃp�����[�^�̑g�ݍ��킹�ł�."},
                   3530:        {-1  ,  "Unknown error", "�s���ȃG���[�ł�."},
                   3531: };
                   3532: 
                   3533: static const struct {
                   3534:        UINT16 code;
                   3535:        char *message_english;
                   3536:        char *message_japanese;
                   3537: } critical_error_table[] = {
                   3538:        {0x00,  "Write protect error", "�������ݕی�G���[�ł�."},
                   3539:        {0x01,  "Invalid unit", "�����ȃ��j�b�g�ł�."},
                   3540:        {0x02,  "Not ready", "�������ł��Ă��܂���."},
                   3541:        {0x03,  "Invalid device request", "�����ȃf�o�C�X�v���ł�."},
                   3542:        {0x04,  "Data error", "�f�[�^�G���[�ł�."},
                   3543:        {0x05,  "Invalid device request parameters", "�����ȃf�o�C�X�v���̃p�����[�^�ł�."},
                   3544:        {0x06,  "Seek error", "�V�[�N�G���[�ł�."},
                   3545:        {0x07,  "Invalid media type", "�����ȃ��f�B�A�̎�ނł�."},
                   3546:        {0x08,  "Sector not found", "�Z�N�^�����‚���܂���."},
                   3547:        {0x09,  "Printer out of paper error", "�v�����^�̗p��������܂���."},
                   3548:        {0x0A,  "Write fault error", "�������݃G���[�ł�."},
                   3549:        {0x0B,  "Read fault error", "�ǂݎ��G���[�ł�."},
                   3550:        {0x0C,  "General failure", "�G���[�ł�."},
                   3551:        {0x0D,  "Sharing violation", "���L�ᔽ�ł�."},
                   3552:        {0x0E,  "Lock violation", "���b�N�ᔽ�ł�."},
                   3553:        {0x0F,  "Invalid disk change", "�����ȃf�B�X�N�̌����ł�."},
                   3554:        {0x10,  "FCB unavailable", "FCB �͎g���܂���."},
                   3555:        {0x11,  "System resource exhausted", "�V�X�e�����\�[�X�͂����g���܂���."},
                   3556:        {0x12,  "Code page mismatch", "�R�[�h �y�[�W����v���܂���."},
                   3557:        {0x13,  "Out of input", "���͂��I���܂���."},
                   3558:        {0x14,  "Insufficient disk space", "�f�B�X�N�̋󂫗̈悪����܂���."},
                   3559:        {-1  ,  "Critical error", "�v���I�ȃG���[�ł�."},
                   3560: };
                   3561: 
1.1.1.20  root     3562: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg);
                   3563: int msdos_psp_get_file_table(int fd, int psp_seg);
                   3564: void msdos_putch(UINT8 data);
1.1.1.35  root     3565: #ifdef USE_SERVICE_THREAD
                   3566: void msdos_putch_tmp(UINT8 data);
                   3567: #endif
1.1.1.45  root     3568: const char *msdos_short_path(const char *path);
1.1.1.44  root     3569: bool msdos_is_valid_drive(int drv);
                   3570: bool msdos_is_removable_drive(int drv);
                   3571: bool msdos_is_cdrom_drive(int drv);
                   3572: bool msdos_is_remote_drive(int drv);
                   3573: bool msdos_is_subst_drive(int drv);
1.1.1.20  root     3574: 
1.1       root     3575: // process info
                   3576: 
                   3577: process_t *msdos_process_info_create(UINT16 psp_seg)
                   3578: {
                   3579:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3580:                if(process[i].psp == 0 || process[i].psp == psp_seg) {
                   3581:                        memset(&process[i], 0, sizeof(process_t));
                   3582:                        process[i].psp = psp_seg;
                   3583:                        return(&process[i]);
                   3584:                }
                   3585:        }
                   3586:        fatalerror("too many processes\n");
                   3587:        return(NULL);
                   3588: }
                   3589: 
1.1.1.33  root     3590: process_t *msdos_process_info_get(UINT16 psp_seg, bool show_error)
1.1       root     3591: {
                   3592:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3593:                if(process[i].psp == psp_seg) {
                   3594:                        return(&process[i]);
                   3595:                }
                   3596:        }
1.1.1.33  root     3597:        if(show_error) {
                   3598:                fatalerror("invalid psp address\n");
                   3599:        }
1.1       root     3600:        return(NULL);
                   3601: }
                   3602: 
1.1.1.33  root     3603: process_t *msdos_process_info_get(UINT16 psp_seg)
                   3604: {
                   3605:        return(msdos_process_info_get(psp_seg, true));
                   3606: }
                   3607: 
1.1.1.23  root     3608: void msdos_sda_update(int psp_seg)
                   3609: {
                   3610:        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   3611:        
                   3612:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3613:                if(process[i].psp == psp_seg) {
                   3614:                        sda->switchar = process[i].switchar;
                   3615:                        sda->current_dta.w.l = process[i].dta.w.l;
                   3616:                        sda->current_dta.w.h = process[i].dta.w.h;
                   3617:                        sda->current_psp = process[i].psp;
                   3618:                        break;
                   3619:                }
                   3620:        }
                   3621:        sda->malloc_strategy = malloc_strategy;
                   3622:        sda->return_code = retval;
                   3623:        sda->current_drive = _getdrive();
                   3624: }
                   3625: 
1.1.1.13  root     3626: // dta info
                   3627: 
                   3628: void msdos_dta_info_init()
                   3629: {
1.1.1.14  root     3630:        for(int i = 0; i < MAX_DTAINFO; i++) {
1.1.1.13  root     3631:                dtalist[i].find_handle = INVALID_HANDLE_VALUE;
                   3632:        }
                   3633: }
                   3634: 
                   3635: dtainfo_t *msdos_dta_info_get(UINT16 psp_seg, UINT32 dta_laddr)
                   3636: {
                   3637:        dtainfo_t *free_dta = NULL;
1.1.1.14  root     3638:        for(int i = 0; i < MAX_DTAINFO; i++) {
                   3639:                if(dtalist[i].find_handle == INVALID_HANDLE_VALUE) {
                   3640:                        if(free_dta == NULL) {
1.1.1.13  root     3641:                                free_dta = &dtalist[i];
                   3642:                        }
1.1.1.14  root     3643:                } else if(dta_laddr < LFN_DTA_LADDR && dtalist[i].dta == dta_laddr) {
1.1.1.13  root     3644:                        return(&dtalist[i]);
                   3645:                }
                   3646:        }
1.1.1.14  root     3647:        if(free_dta) {
1.1.1.13  root     3648:                free_dta->psp = psp_seg;
                   3649:                free_dta->dta = dta_laddr;
                   3650:                return(free_dta);
                   3651:        }
                   3652:        fatalerror("too many dta\n");
                   3653:        return(NULL);
                   3654: }
                   3655: 
                   3656: void msdos_dta_info_free(UINT16 psp_seg)
                   3657: {
1.1.1.14  root     3658:        for(int i = 0; i < MAX_DTAINFO; i++) {
                   3659:                if(dtalist[i].psp == psp_seg && dtalist[i].find_handle != INVALID_HANDLE_VALUE) {
1.1.1.13  root     3660:                        FindClose(dtalist[i].find_handle);
                   3661:                        dtalist[i].find_handle = INVALID_HANDLE_VALUE;
                   3662:                }
                   3663:        }
                   3664: }
                   3665: 
1.1       root     3666: void msdos_cds_update(int drv)
                   3667: {
1.1.1.44  root     3668:        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * drv);
1.1       root     3669:        
1.1.1.44  root     3670:        memset(cds, 0, 88);
                   3671:        
                   3672:        if(msdos_is_valid_drive(drv)) {
                   3673:                char path[MAX_PATH];
                   3674:                if(msdos_is_remote_drive(drv)) {
                   3675:                        cds->drive_attrib = 0xc000;     // network drive
                   3676:                } else if(msdos_is_subst_drive(drv)) {
                   3677:                        cds->drive_attrib = 0x5000;     // subst drive
                   3678:                } else {
                   3679:                        cds->drive_attrib = 0x4000;     // physical drive
                   3680:                }
                   3681:                if(_getdcwd(drv + 1, path, MAX_PATH) != NULL) {
                   3682:                        strcpy_s(cds->path_name, sizeof(cds->path_name), msdos_short_path(path));
                   3683:                }
                   3684:        }
                   3685:        if(cds->path_name[0] == '\0') {
                   3686:                sprintf(cds->path_name, "%c:\\", 'A' + drv);
                   3687:        }
                   3688:        cds->dpb_ptr.w.h = DPB_TOP >> 4;
                   3689:        cds->dpb_ptr.w.l = sizeof(dpb_t) * drv;
                   3690:        cds->word_1 = cds->word_2 = 0xffff;
                   3691:        cds->word_3 = 0xffff; // stored user data from INT 21/AX=5F03h if this is network drive
                   3692:        cds->bs_offset = 2;
                   3693: }
                   3694: 
1.1.1.45  root     3695: void msdos_cds_update(int drv, const char *path)
1.1.1.44  root     3696: {
                   3697:        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * drv);
                   3698:        
                   3699:        strcpy_s(cds->path_name, sizeof(cds->path_name), msdos_short_path(path));
1.1       root     3700: }
                   3701: 
1.1.1.17  root     3702: // nls information tables
                   3703: 
                   3704: // uppercase table (func 6502h)
                   3705: void msdos_upper_table_update()
                   3706: {
                   3707:        *(UINT16 *)(mem + UPPERTABLE_TOP) = 0x80;
1.1.1.22  root     3708:        for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17  root     3709:                UINT8 c[4];
1.1.1.33  root     3710:                *(UINT32 *)c = 0;               // reset internal conversion state
                   3711:                CharUpperBuffA((LPSTR)c, 4);    // (workaround for MBCS codepage)
1.1.1.17  root     3712:                c[0] = 0x80 + i;
                   3713:                DWORD rc = CharUpperBuffA((LPSTR)c, 1);
                   3714:                mem[UPPERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
                   3715:        }
                   3716: }
                   3717: 
1.1.1.23  root     3718: // lowercase table (func 6503h)
                   3719: void msdos_lower_table_update()
                   3720: {
                   3721:        *(UINT16 *)(mem + LOWERTABLE_TOP) = 0x80;
                   3722:        for(unsigned i = 0; i < 0x80; ++i) {
                   3723:                UINT8 c[4];
1.1.1.33  root     3724:                *(UINT32 *)c = 0;               // reset internal conversion state
                   3725:                CharLowerBuffA((LPSTR)c, 4);    // (workaround for MBCS codepage)
1.1.1.23  root     3726:                c[0] = 0x80 + i;
                   3727:                DWORD rc = CharLowerBuffA((LPSTR)c, 1);
                   3728:                mem[LOWERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
                   3729:        }
                   3730: }
                   3731: 
1.1.1.17  root     3732: // filename uppercase table (func 6504h)
                   3733: void msdos_filename_upper_table_init()
                   3734: {
                   3735:        // depended on (file)system, not on active codepage
                   3736:        // temporary solution: just filling data
                   3737:        *(UINT16 *)(mem + FILENAME_UPPERTABLE_TOP) = 0x80;
1.1.1.22  root     3738:        for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17  root     3739:                mem[FILENAME_UPPERTABLE_TOP + 2 + i] = 0x80 + i;
                   3740:        }
                   3741: }
                   3742: 
                   3743: // filaname terminator table (func 6505h)
                   3744: void msdos_filename_terminator_table_init()
                   3745: {
                   3746:        const char illegal_chars[] = ".\"/\\[]:|<>+=;,";        // for standard MS-DOS fs.
                   3747:        UINT8 *data = mem + FILENAME_TERMINATOR_TOP;
                   3748:        
                   3749:        data[2] = 1;            // marker? (permissible character value)
                   3750:        data[3] = 0x00;         // 00h...FFh
                   3751:        data[4] = 0xff;
                   3752:        data[5] = 0;            // marker? (excluded character)
                   3753:        data[6] = 0x00;         // 00h...20h
                   3754:        data[7] = 0x20;
                   3755:        data[8] = 2;            // marker? (illegal characters for filename)
                   3756:        data[9] = (UINT8)strlen(illegal_chars);
                   3757:        memcpy(data + 10, illegal_chars, data[9]);
                   3758:        
                   3759:        // total length
                   3760:        *(UINT16 *)data = (10 - 2) + data[9];
                   3761: }
                   3762: 
                   3763: // collating table (func 6506h)
                   3764: void msdos_collating_table_update()
                   3765: {
                   3766:        // temporary solution: just filling data
                   3767:        *(UINT16 *)(mem + COLLATING_TABLE_TOP) = 0x100;
1.1.1.22  root     3768:        for(unsigned i = 0; i < 256; ++i) {
1.1.1.17  root     3769:                mem[COLLATING_TABLE_TOP + 2 + i] = i;
                   3770:        }
                   3771: }
                   3772: 
1.1       root     3773: // dbcs
                   3774: 
                   3775: void msdos_dbcs_table_update()
                   3776: {
                   3777:        UINT8 dbcs_data[DBCS_SIZE];
                   3778:        memset(dbcs_data, 0, sizeof(dbcs_data));
                   3779:        
                   3780:        CPINFO info;
                   3781:        GetCPInfo(active_code_page, &info);
                   3782:        
                   3783:        if(info.MaxCharSize != 1) {
                   3784:                for(int i = 0;; i += 2) {
                   3785:                        UINT8 lo = info.LeadByte[i + 0];
                   3786:                        UINT8 hi = info.LeadByte[i + 1];
                   3787:                        dbcs_data[2 + i + 0] = lo;
                   3788:                        dbcs_data[2 + i + 1] = hi;
                   3789:                        if(lo == 0 && hi == 0) {
                   3790:                                dbcs_data[0] = i + 2;
                   3791:                                break;
                   3792:                        }
                   3793:                }
                   3794:        } else {
                   3795:                dbcs_data[0] = 2;       // ???
                   3796:        }
                   3797:        memcpy(mem + DBCS_TOP, dbcs_data, sizeof(dbcs_data));
                   3798: }
                   3799: 
1.1.1.17  root     3800: void msdos_dbcs_table_finish()
                   3801: {
1.1.1.32  root     3802:        if(system_code_page != _getmbcp()) {
1.1.1.17  root     3803:                _setmbcp(system_code_page);
                   3804:        }
1.1.1.32  root     3805:        if(console_code_page != GetConsoleCP()) {
                   3806:                SetConsoleCP(console_code_page);
                   3807:                SetConsoleOutputCP(console_code_page);
                   3808:        }
1.1.1.17  root     3809: }
                   3810: 
                   3811: void msdos_nls_tables_init()
1.1       root     3812: {
1.1.1.32  root     3813:        active_code_page = console_code_page = GetConsoleCP();
                   3814:        system_code_page = _getmbcp();
                   3815:        
                   3816:        if(active_code_page != system_code_page) {
                   3817:                if(_setmbcp(active_code_page) != 0) {
                   3818:                        active_code_page = system_code_page;
                   3819:                }
                   3820:        }
                   3821:        
1.1.1.17  root     3822:        msdos_upper_table_update();
1.1.1.23  root     3823:        msdos_lower_table_update();
1.1.1.17  root     3824:        msdos_filename_terminator_table_init();
                   3825:        msdos_filename_upper_table_init();
                   3826:        msdos_collating_table_update();
1.1       root     3827:        msdos_dbcs_table_update();
                   3828: }
                   3829: 
1.1.1.17  root     3830: void msdos_nls_tables_update()
1.1       root     3831: {
1.1.1.17  root     3832:        msdos_dbcs_table_update();
                   3833:        msdos_upper_table_update();
1.1.1.23  root     3834:        msdos_lower_table_update();
                   3835: //     msdos_collating_table_update();
1.1       root     3836: }
                   3837: 
                   3838: int msdos_lead_byte_check(UINT8 code)
                   3839: {
                   3840:        UINT8 *dbcs_table = mem + DBCS_TABLE;
                   3841:        
                   3842:        for(int i = 0;; i += 2) {
                   3843:                UINT8 lo = dbcs_table[i + 0];
                   3844:                UINT8 hi = dbcs_table[i + 1];
                   3845:                if(lo == 0 && hi == 0) {
                   3846:                        break;
                   3847:                }
                   3848:                if(lo <= code && code <= hi) {
                   3849:                        return(1);
                   3850:                }
                   3851:        }
                   3852:        return(0);
                   3853: }
                   3854: 
1.1.1.20  root     3855: int msdos_ctrl_code_check(UINT8 code)
                   3856: {
1.1.1.22  root     3857:        return (code >= 0x01 && code <= 0x1a && code != 0x07 && code != 0x08 && code != 0x09 && code != 0x0a && code != 0x0d);
1.1.1.20  root     3858: }
                   3859: 
1.1.1.36  root     3860: int msdos_kanji_2nd_byte_check(UINT8 *buf, int n)
                   3861: {
                   3862:        int is_kanji_1st = 0;
                   3863:        int is_kanji_2nd = 0;
                   3864:        
                   3865:        for(int p = 0;; p++) {
                   3866:                if(is_kanji_1st) {
                   3867:                        is_kanji_1st = 0;
                   3868:                        is_kanji_2nd = 1;
                   3869:                } else if(msdos_lead_byte_check(buf[p])) {
                   3870:                        is_kanji_1st = 1;
                   3871:                }
                   3872:                if(p == n) {
                   3873:                        return(is_kanji_2nd);
                   3874:                }
                   3875:                is_kanji_2nd = 0;
                   3876:        }
                   3877: }
                   3878: 
1.1       root     3879: // file control
                   3880: 
1.1.1.45  root     3881: const char *msdos_remove_double_quote(const char *path)
1.1.1.14  root     3882: {
                   3883:        static char tmp[MAX_PATH];
                   3884:        
                   3885:        if(strlen(path) >= 2 && path[0] == '"' && path[strlen(path) - 1] == '"') {
1.1.1.45  root     3886:                memset(tmp, 0, sizeof(tmp));
1.1.1.14  root     3887:                memcpy(tmp, path + 1, strlen(path) - 2);
                   3888:        } else {
                   3889:                strcpy(tmp, path);
                   3890:        }
                   3891:        return(tmp);
                   3892: }
                   3893: 
1.1.1.45  root     3894: const char *msdos_remove_end_separator(const char *path)
1.1.1.32  root     3895: {
                   3896:        static char tmp[MAX_PATH];
                   3897:        
                   3898:        strcpy(tmp, path);
1.1.1.45  root     3899:        
                   3900:        // for example "C:\" case, the end separator should not be removed
                   3901:        if(strlen(tmp) > 3 && tmp[strlen(tmp) - 1] == '\\') {
                   3902:                tmp[strlen(tmp) - 1] = '\0';
1.1.1.32  root     3903:        }
                   3904:        return(tmp);
                   3905: }
                   3906: 
1.1.1.45  root     3907: const char *msdos_combine_path(const char *dir, const char *file)
1.1.1.14  root     3908: {
                   3909:        static char tmp[MAX_PATH];
1.1.1.45  root     3910:        const char *tmp_dir = msdos_remove_double_quote(dir);
1.1.1.14  root     3911:        
                   3912:        if(strlen(tmp_dir) == 0) {
                   3913:                strcpy(tmp, file);
                   3914:        } else if(tmp_dir[strlen(tmp_dir) - 1] == '\\') {
                   3915:                sprintf(tmp, "%s%s", tmp_dir, file);
                   3916:        } else {
                   3917:                sprintf(tmp, "%s\\%s", tmp_dir, file);
                   3918:        }
                   3919:        return(tmp);
                   3920: }
                   3921: 
1.1.1.45  root     3922: const char *msdos_trimmed_path(const char *path, int lfn)
1.1       root     3923: {
                   3924:        static char tmp[MAX_PATH];
                   3925:        
                   3926:        if(lfn) {
                   3927:                strcpy(tmp, path);
                   3928:        } else {
                   3929:                // remove space in the path
1.1.1.45  root     3930:                const char *src = path;
                   3931:                char *dst = tmp;
1.1       root     3932:                
                   3933:                while(*src != '\0') {
                   3934:                        if(msdos_lead_byte_check(*src)) {
                   3935:                                *dst++ = *src++;
                   3936:                                *dst++ = *src++;
                   3937:                        } else if(*src != ' ') {
                   3938:                                *dst++ = *src++;
                   3939:                        } else {
                   3940:                                src++;  // skip space
                   3941:                        }
                   3942:                }
                   3943:                *dst = '\0';
                   3944:        }
1.1.1.14  root     3945:        if(_stricmp(tmp, "C:\\COMMAND.COM") == 0) {
                   3946:                // redirect C:\COMMAND.COM to comspec_path
                   3947:                strcpy(tmp, comspec_path);
                   3948:        } else if(is_vista_or_later && _access(tmp, 0) != 0) {
                   3949:                // redirect new files (without wildcards) in C:\ to %TEMP%, since C:\ is not usually writable
                   3950:                static int root_drive_protected = -1;
                   3951:                char temp[MAX_PATH], name[MAX_PATH], *name_temp = NULL;
                   3952:                dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   3953:                
                   3954:                if(GetFullPathName(tmp, MAX_PATH, temp, &name_temp) != 0 &&
                   3955:                   name_temp != NULL && strstr(name_temp, "?") == NULL && strstr(name_temp, "*") == NULL) {
                   3956:                        strcpy(name, name_temp);
                   3957:                        name_temp[0] = '\0';
                   3958:                        
                   3959:                        if((temp[0] == 'A' + dos_info->boot_drive - 1 || temp[0] == 'a' + dos_info->boot_drive - 1) &&
                   3960:                           (temp[1] == ':') && (temp[2] == '\\' || temp[2] == '/') && (temp[3] == '\0')) {
                   3961:                                if(root_drive_protected == -1) {
                   3962:                                        FILE *fp = NULL;
                   3963:                                        
                   3964:                                        sprintf(temp, "%c:\\MS-DOS_Player.$$$", 'A' + dos_info->boot_drive - 1);
                   3965:                                        root_drive_protected = 1;
                   3966:                                        try {
                   3967:                                                if((fp = fopen(temp, "w")) != NULL) {
                   3968:                                                        if(fprintf(fp, "TEST") == 4) {
                   3969:                                                                root_drive_protected = 0;
                   3970:                                                        }
                   3971:                                                }
                   3972:                                        } catch(...) {
                   3973:                                        }
                   3974:                                        if(fp != NULL) {
                   3975:                                                fclose(fp);
                   3976:                                        }
                   3977:                                        if(_access(temp, 0) == 0) {
                   3978:                                                remove(temp);
                   3979:                                        }
                   3980:                                }
                   3981:                                if(root_drive_protected == 1) {
                   3982:                                        if(GetEnvironmentVariable("TEMP", temp, MAX_PATH) != 0 ||
                   3983:                                           GetEnvironmentVariable("TMP",  temp, MAX_PATH) != 0) {
                   3984:                                                strcpy(tmp, msdos_combine_path(temp, name));
                   3985:                                        }
                   3986:                                }
                   3987:                        }
                   3988:                }
                   3989:        }
1.1       root     3990:        return(tmp);
                   3991: }
                   3992: 
1.1.1.45  root     3993: const char *msdos_get_multiple_short_path(const char *src)
1.1.1.28  root     3994: {
1.1.1.32  root     3995:        // "LONGPATH\";"LONGPATH\";"LONGPATH\" to SHORTPATH;SHORTPATH;SHORTPATH
1.1.1.28  root     3996:        static char env_path[ENV_SIZE];
                   3997:        char tmp[ENV_SIZE], *token;
                   3998:        
                   3999:        memset(env_path, 0, sizeof(env_path));
                   4000:        strcpy(tmp, src);
                   4001:        token = my_strtok(tmp, ";");
                   4002:        
                   4003:        while(token != NULL) {
                   4004:                if(token[0] != '\0') {
1.1.1.45  root     4005:                        const char *path = msdos_remove_double_quote(token);
                   4006:                        char short_path[MAX_PATH];
1.1.1.32  root     4007:                        if(path != NULL && strlen(path) != 0) {
                   4008:                                if(env_path[0] != '\0') {
                   4009:                                        strcat(env_path, ";");
                   4010:                                }
1.1.1.28  root     4011:                                if(GetShortPathName(path, short_path, MAX_PATH) == 0) {
1.1.1.32  root     4012:                                        strcat(env_path, msdos_remove_end_separator(path));
1.1.1.28  root     4013:                                } else {
                   4014:                                        my_strupr(short_path);
1.1.1.32  root     4015:                                        strcat(env_path, msdos_remove_end_separator(short_path));
1.1.1.28  root     4016:                                }
                   4017:                        }
                   4018:                }
                   4019:                token = my_strtok(NULL, ";");
                   4020:        }
                   4021:        return(env_path);
                   4022: }
                   4023: 
1.1.1.45  root     4024: bool match(const char *text, const char *pattern)
1.1       root     4025: {
1.1.1.24  root     4026:        // http://www.prefield.com/algorithm/string/wildcard.html
1.1.1.14  root     4027:        switch(*pattern) {
1.1       root     4028:        case '\0':
                   4029:                return !*text;
                   4030:        case '*':
1.1.1.14  root     4031:                return match(text, pattern + 1) || (*text && match(text + 1, pattern));
1.1       root     4032:        case '?':
                   4033:                return *text && match(text + 1, pattern + 1);
                   4034:        default:
                   4035:                return (*text == *pattern) && match(text + 1, pattern + 1);
                   4036:        }
                   4037: }
                   4038: 
1.1.1.45  root     4039: bool msdos_match_volume_label(const char *path, const char *volume)
1.1       root     4040: {
1.1.1.45  root     4041:        const char *p = NULL;
1.1       root     4042:        
1.1.1.14  root     4043:        if(!*volume) {
                   4044:                return false;
                   4045:        } else if((p = my_strchr(path, ':')) != NULL) {
1.1       root     4046:                return msdos_match_volume_label(p + 1, volume);
                   4047:        } else if((p = my_strchr(path, '\\')) != NULL) {
                   4048:                return msdos_match_volume_label(p + 1, volume);
                   4049:        } else if((p = my_strchr(path, '.')) != NULL) {
1.1.1.14  root     4050:                char tmp[MAX_PATH];
                   4051:                sprintf(tmp, "%.*s%s", (int)(p - path), path, p + 1);
                   4052:                return match(volume, tmp);
1.1       root     4053:        } else {
                   4054:                return match(volume, path);
                   4055:        }
                   4056: }
                   4057: 
1.1.1.45  root     4058: const char *msdos_fcb_path(fcb_t *fcb)
1.1       root     4059: {
                   4060:        static char tmp[MAX_PATH];
                   4061:        char name[9], ext[4];
                   4062:        
                   4063:        memset(name, 0, sizeof(name));
                   4064:        memcpy(name, fcb->file_name, 8);
                   4065:        strcpy(name, msdos_trimmed_path(name, 0));
                   4066:        
                   4067:        memset(ext, 0, sizeof(ext));
                   4068:        memcpy(ext, fcb->file_name + 8, 3);
                   4069:        strcpy(ext, msdos_trimmed_path(ext, 0));
                   4070:        
                   4071:        if(name[0] == '\0' || strcmp(name, "????????") == 0) {
                   4072:                strcpy(name, "*");
                   4073:        }
                   4074:        if(ext[0] == '\0') {
                   4075:                strcpy(tmp, name);
                   4076:        } else {
                   4077:                if(strcmp(ext, "???") == 0) {
                   4078:                        strcpy(ext, "*");
                   4079:                }
                   4080:                sprintf(tmp, "%s.%s", name, ext);
                   4081:        }
                   4082:        return(tmp);
                   4083: }
                   4084: 
1.1.1.45  root     4085: void msdos_set_fcb_path(fcb_t *fcb, const char *path)
1.1       root     4086: {
                   4087:        char *ext = my_strchr(path, '.');
                   4088:        
                   4089:        memset(fcb->file_name, 0x20, 8 + 3);
                   4090:        if(ext != NULL && path[0] != '.') {
                   4091:                *ext = '\0';
                   4092:                memcpy(fcb->file_name + 8, ext + 1, strlen(ext + 1));
                   4093:        }
                   4094:        memcpy(fcb->file_name, path, strlen(path));
                   4095: }
                   4096: 
1.1.1.45  root     4097: const char *msdos_short_path(const char *path)
1.1       root     4098: {
                   4099:        static char tmp[MAX_PATH];
                   4100:        
1.1.1.24  root     4101:        if(GetShortPathName(path, tmp, MAX_PATH) == 0) {
                   4102:                strcpy(tmp, path);
                   4103:        }
1.1       root     4104:        my_strupr(tmp);
                   4105:        return(tmp);
                   4106: }
                   4107: 
1.1.1.45  root     4108: const char *msdos_short_name(WIN32_FIND_DATA *fd)
1.1.1.13  root     4109: {
                   4110:        static char tmp[MAX_PATH];
1.1.1.45  root     4111:        
1.1.1.14  root     4112:        if(fd->cAlternateFileName[0]) {
1.1.1.13  root     4113:                strcpy(tmp, fd->cAlternateFileName);
                   4114:        } else {
                   4115:                strcpy(tmp, fd->cFileName);
                   4116:        }
                   4117:        my_strupr(tmp);
                   4118:        return(tmp);
                   4119: }
                   4120: 
1.1.1.45  root     4121: const char *msdos_short_full_path(const char *path)
1.1       root     4122: {
                   4123:        static char tmp[MAX_PATH];
                   4124:        char full[MAX_PATH], *name;
                   4125:        
1.1.1.14  root     4126:        // Full works with non-existent files, but Short does not
1.1       root     4127:        GetFullPathName(path, MAX_PATH, full, &name);
1.1.1.14  root     4128:        *tmp = '\0';
                   4129:        if(GetShortPathName(full, tmp, MAX_PATH) == 0 && name > path) {
                   4130:                name[-1] = '\0';
                   4131:                DWORD len = GetShortPathName(full, tmp, MAX_PATH);
                   4132:                if(len == 0) {
                   4133:                        strcpy(tmp, full);
                   4134:                } else {
                   4135:                        tmp[len++] = '\\';
                   4136:                        strcpy(tmp + len, name);
                   4137:                }
                   4138:        }
1.1       root     4139:        my_strupr(tmp);
                   4140:        return(tmp);
                   4141: }
                   4142: 
1.1.1.45  root     4143: const char *msdos_short_full_dir(const char *path)
1.1       root     4144: {
                   4145:        static char tmp[MAX_PATH];
                   4146:        char full[MAX_PATH], *name;
                   4147:        
                   4148:        GetFullPathName(path, MAX_PATH, full, &name);
                   4149:        name[-1] = '\0';
1.1.1.24  root     4150:        if(GetShortPathName(full, tmp, MAX_PATH) == 0) {
                   4151:                strcpy(tmp, full);
                   4152:        }
1.1       root     4153:        my_strupr(tmp);
                   4154:        return(tmp);
                   4155: }
                   4156: 
1.1.1.45  root     4157: const char *msdos_local_file_path(const char *path, int lfn)
1.1       root     4158: {
1.1.1.45  root     4159:        static char trimmed[MAX_PATH];
                   4160:        
                   4161:        strcpy(trimmed, msdos_trimmed_path(path, lfn));
1.1.1.14  root     4162: #if 0
                   4163:        // I have forgotten the reason of this routine... :-(
1.1       root     4164:        if(_access(trimmed, 0) != 0) {
                   4165:                process_t *process = msdos_process_info_get(current_psp);
                   4166:                static char tmp[MAX_PATH];
                   4167:                
                   4168:                sprintf(tmp, "%s\\%s", process->module_dir, trimmed);
                   4169:                if(_access(tmp, 0) == 0) {
                   4170:                        return(tmp);
                   4171:                }
                   4172:        }
1.1.1.14  root     4173: #endif
1.1       root     4174:        return(trimmed);
                   4175: }
                   4176: 
1.1.1.45  root     4177: bool msdos_is_device_path(const char *path)
1.1.1.11  root     4178: {
                   4179:        char full[MAX_PATH], *name;
                   4180:        
1.1.1.24  root     4181:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4182:                if(_stricmp(full, "\\\\.\\AUX" ) == 0 ||
                   4183:                   _stricmp(full, "\\\\.\\CON" ) == 0 ||
                   4184:                   _stricmp(full, "\\\\.\\NUL" ) == 0 ||
                   4185:                   _stricmp(full, "\\\\.\\PRN" ) == 0 ||
                   4186:                   _stricmp(full, "\\\\.\\COM1") == 0 ||
                   4187:                   _stricmp(full, "\\\\.\\COM2") == 0 ||
                   4188:                   _stricmp(full, "\\\\.\\COM3") == 0 ||
                   4189:                   _stricmp(full, "\\\\.\\COM4") == 0 ||
                   4190:                   _stricmp(full, "\\\\.\\COM5") == 0 ||
                   4191:                   _stricmp(full, "\\\\.\\COM6") == 0 ||
                   4192:                   _stricmp(full, "\\\\.\\COM7") == 0 ||
                   4193:                   _stricmp(full, "\\\\.\\COM8") == 0 ||
                   4194:                   _stricmp(full, "\\\\.\\COM9") == 0 ||
                   4195:                   _stricmp(full, "\\\\.\\LPT1") == 0 ||
                   4196:                   _stricmp(full, "\\\\.\\LPT2") == 0 ||
                   4197:                   _stricmp(full, "\\\\.\\LPT3") == 0 ||
                   4198:                   _stricmp(full, "\\\\.\\LPT4") == 0 ||
                   4199:                   _stricmp(full, "\\\\.\\LPT5") == 0 ||
                   4200:                   _stricmp(full, "\\\\.\\LPT6") == 0 ||
                   4201:                   _stricmp(full, "\\\\.\\LPT7") == 0 ||
                   4202:                   _stricmp(full, "\\\\.\\LPT8") == 0 ||
                   4203:                   _stricmp(full, "\\\\.\\LPT9") == 0) {
                   4204:                        return(true);
                   4205:                } else if(name != NULL) {
                   4206:                        if(_stricmp(name, "CLOCK$"  ) == 0 ||
                   4207:                           _stricmp(name, "CONFIG$" ) == 0 ||
1.1.1.30  root     4208:                           _stricmp(name, "EMMXXXX0") == 0 ||
1.1.1.43  root     4209: //                        _stricmp(name, "SCSIMGR$") == 0 ||
1.1.1.30  root     4210:                           _stricmp(name, "$IBMAIAS") == 0) {
1.1.1.29  root     4211:                                return(true);
                   4212:                        }
                   4213:                }
1.1.1.24  root     4214:        }
                   4215:        return(false);
1.1.1.11  root     4216: }
                   4217: 
1.1.1.45  root     4218: bool msdos_is_con_path(const char *path)
1.1.1.8   root     4219: {
1.1.1.14  root     4220:        char full[MAX_PATH], *name;
1.1.1.8   root     4221:        
1.1.1.24  root     4222:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4223:                return(_stricmp(full, "\\\\.\\CON") == 0);
1.1.1.24  root     4224:        }
                   4225:        return(false);
                   4226: }
                   4227: 
1.1.1.45  root     4228: int msdos_is_comm_path(const char *path)
1.1.1.24  root     4229: {
                   4230:        char full[MAX_PATH], *name;
                   4231:        
                   4232:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4233:                if(_stricmp(full, "\\\\.\\COM1") == 0) {
                   4234:                        return(1);
                   4235:                } else if(_stricmp(full, "\\\\.\\COM2") == 0) {
                   4236:                        return(2);
                   4237:                } else if(_stricmp(full, "\\\\.\\COM3") == 0) {
                   4238:                        return(3);
                   4239:                } else if(_stricmp(full, "\\\\.\\COM4") == 0) {
                   4240:                        return(4);
1.1.1.24  root     4241:                }
                   4242:        }
1.1.1.29  root     4243:        return(0);
                   4244: }
                   4245: 
1.1.1.45  root     4246: void msdos_set_comm_params(int sio_port, const char *path)
1.1.1.37  root     4247: {
                   4248:        // COM1:{110,150,300,600,1200,2400,4800,9600},{N,O,E,M,S},{8,7,6,5},{1,1.5,2}
1.1.1.45  root     4249:        const char *p = NULL;
1.1.1.37  root     4250:        
                   4251:        if((p = strstr(path, ":")) != NULL) {
                   4252:                UINT8 selector = sio_read(sio_port - 1, 3);
                   4253:                
                   4254:                // baud rate
                   4255:                int baud = max(110, min(9600, atoi(p + 1)));
                   4256:                UINT16 divisor = 115200 / baud;
                   4257:                
                   4258:                if((p = strstr(p + 1, ",")) != NULL) {
                   4259:                        // parity
                   4260:                        if(p[1] == 'N' || p[1] == 'n') {
                   4261:                                selector = (selector & ~0x38) | 0x00;
                   4262:                        } else if(p[1] == 'O' || p[1] == 'o') {
                   4263:                                selector = (selector & ~0x38) | 0x08;
                   4264:                        } else if(p[1] == 'E' || p[1] == 'e') {
                   4265:                                selector = (selector & ~0x38) | 0x18;
                   4266:                        } else if(p[1] == 'M' || p[1] == 'm') {
                   4267:                                selector = (selector & ~0x38) | 0x28;
                   4268:                        } else if(p[1] == 'S' || p[1] == 's') {
                   4269:                                selector = (selector & ~0x38) | 0x38;
                   4270:                        }
                   4271:                        if((p = strstr(p + 1, ",")) != NULL) {
                   4272:                                // word length
                   4273:                                if(p[1] == '8') {
                   4274:                                        selector = (selector & ~0x03) | 0x03;
                   4275:                                } else if(p[1] == '7') {
                   4276:                                        selector = (selector & ~0x03) | 0x02;
                   4277:                                } else if(p[1] == '6') {
                   4278:                                        selector = (selector & ~0x03) | 0x01;
                   4279:                                } else if(p[1] == '5') {
                   4280:                                        selector = (selector & ~0x03) | 0x00;
                   4281:                                }
                   4282:                                if((p = strstr(p + 1, ",")) != NULL) {
                   4283:                                        // stop bits
                   4284:                                        float bits = atof(p + 1);
                   4285:                                        if(bits > 1.0F) {
                   4286:                                                selector |= 0x04;
                   4287:                                        } else {
                   4288:                                                selector &= ~0x04;
                   4289:                                        }
                   4290:                                }
                   4291:                        }
                   4292:                }
                   4293:                sio_write(sio_port - 1, 3, selector | 0x80);
                   4294:                sio_write(sio_port - 1, 0, divisor & 0xff);
                   4295:                sio_write(sio_port - 1, 1, divisor >> 8);
                   4296:                sio_write(sio_port - 1, 3, selector);
                   4297:        }
                   4298: }
                   4299: 
1.1.1.45  root     4300: int msdos_is_prn_path(const char *path)
1.1.1.30  root     4301: {
                   4302:        char full[MAX_PATH], *name;
                   4303:        
                   4304:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
                   4305:                if(_stricmp(full, "\\\\.\\PRN") == 0) {
                   4306:                        return(1);
                   4307:                } else if(_stricmp(full, "\\\\.\\LPT1") == 0) {
                   4308:                        return(1);
                   4309:                } else if(_stricmp(full, "\\\\.\\LPT2") == 0) {
                   4310:                        return(2);
                   4311:                } else if(_stricmp(full, "\\\\.\\LPT3") == 0) {
                   4312:                        return(3);
                   4313:                }
                   4314:        }
                   4315:        return(0);
                   4316: }
                   4317: 
1.1.1.44  root     4318: bool msdos_is_valid_drive(int drv)
                   4319: {
                   4320:        return(drv >= 0 && drv < 26 && (GetLogicalDrives() & (1 << drv)) != 0);
                   4321: }
                   4322: 
                   4323: bool msdos_is_removable_drive(int drv)
                   4324: {
                   4325:        char volume[] = "A:\\";
                   4326:        
                   4327:        volume[0] = 'A' + drv;
                   4328:        
                   4329:        return(GetDriveType(volume) == DRIVE_REMOVABLE);
                   4330: }
                   4331: 
                   4332: bool msdos_is_cdrom_drive(int drv)
                   4333: {
                   4334:        char volume[] = "A:\\";
                   4335:        
                   4336:        volume[0] = 'A' + drv;
                   4337:        
                   4338:        return(GetDriveType(volume) == DRIVE_CDROM);
                   4339: }
                   4340: 
                   4341: bool msdos_is_remote_drive(int drv)
                   4342: {
                   4343:        char volume[] = "A:\\";
                   4344:        
                   4345:        volume[0] = 'A' + drv;
                   4346:        
                   4347:        return(GetDriveType(volume) == DRIVE_REMOTE);
                   4348: }
                   4349: 
                   4350: bool msdos_is_subst_drive(int drv)
                   4351: {
                   4352:        char device[] = "A:", path[MAX_PATH];
                   4353:        
                   4354:        device[0] = 'A' + drv;
                   4355:        
                   4356:        if(QueryDosDevice(device, path, MAX_PATH)) {
                   4357:                if(strncmp(path, "\\??\\", 4) == 0) {
                   4358:                        return(true);
                   4359:                }
                   4360:        }
                   4361:        return(false);
                   4362: }
                   4363: 
1.1.1.45  root     4364: bool msdos_is_existing_file(const char *path)
1.1.1.24  root     4365: {
                   4366:        // http://d.hatena.ne.jp/yu-hr/20100317/1268826458
                   4367:        WIN32_FIND_DATA FindData;
                   4368:        HANDLE hFind;
                   4369:        
                   4370:        if((hFind = FindFirstFile(path, &FindData)) != INVALID_HANDLE_VALUE) {
                   4371:                FindClose(hFind);
                   4372:                return(!(FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY));
                   4373:        }
                   4374:        return(false);
1.1.1.8   root     4375: }
                   4376: 
1.1.1.45  root     4377: const char *msdos_search_command_com(const char *command_path, const char *env_path)
1.1.1.9   root     4378: {
                   4379:        static char tmp[MAX_PATH];
1.1.1.28  root     4380:        char path[ENV_SIZE], *file_name;
1.1.1.9   root     4381:        
1.1.1.28  root     4382:        // check if COMMAND.COM is in the same directory as the target program file
1.1.1.9   root     4383:        if(GetFullPathName(command_path, MAX_PATH, tmp, &file_name) != 0) {
                   4384:                sprintf(file_name, "COMMAND.COM");
                   4385:                if(_access(tmp, 0) == 0) {
                   4386:                        return(tmp);
                   4387:                }
                   4388:        }
1.1.1.28  root     4389:        
                   4390:        // check if COMMAND.COM is in the same directory as the running msdos.exe
1.1.1.9   root     4391:        if(GetModuleFileName(NULL, path, MAX_PATH) != 0 && GetFullPathName(path, MAX_PATH, tmp, &file_name) != 0) {
                   4392:                sprintf(file_name, "COMMAND.COM");
                   4393:                if(_access(tmp, 0) == 0) {
                   4394:                        return(tmp);
                   4395:                }
                   4396:        }
1.1.1.28  root     4397:        
                   4398:        // check if COMMAND.COM is in the current directory
1.1.1.9   root     4399:        if(GetFullPathName("COMMAND.COM", MAX_PATH, tmp, &file_name) != 0) {
                   4400:                if(_access(tmp, 0) == 0) {
                   4401:                        return(tmp);
                   4402:                }
                   4403:        }
1.1.1.28  root     4404:        
                   4405:        // cehck if COMMAND.COM is in the directory that is in MSDOS_PATH and PATH environment variables
                   4406:        strcpy(path, env_path);
                   4407:        char *token = my_strtok(path, ";");
1.1.1.9   root     4408:        while(token != NULL) {
1.1.1.14  root     4409:                if(strlen(token) != 0 && token[0] != '%') {
1.1.1.9   root     4410:                        strcpy(tmp, msdos_combine_path(token, "COMMAND.COM"));
                   4411:                        if(_access(tmp, 0) == 0) {
                   4412:                                return(tmp);
                   4413:                        }
                   4414:                }
                   4415:                token = my_strtok(NULL, ";");
                   4416:        }
                   4417:        return(NULL);
                   4418: }
                   4419: 
1.1.1.14  root     4420: int msdos_drive_number(const char *path)
1.1       root     4421: {
                   4422:        char tmp[MAX_PATH], *name;
                   4423:        
1.1.1.45  root     4424:        if(GetFullPathName(path, MAX_PATH, tmp, &name) >= 2 && tmp[1] == ':') {
                   4425:                if(tmp[0] >= 'a' && tmp[0] <= 'z') {
                   4426:                        return(tmp[0] - 'a');
                   4427:                } else if(tmp[0] >= 'A' && tmp[0] <= 'Z') {
                   4428:                        return(tmp[0] - 'A');
                   4429:                }
1.1       root     4430:        }
1.1.1.45  root     4431: //     return(msdos_drive_number("."));
                   4432:        return(_getdrive() - 1);
1.1       root     4433: }
                   4434: 
1.1.1.45  root     4435: const char *msdos_volume_label(const char *path)
1.1       root     4436: {
                   4437:        static char tmp[MAX_PATH];
                   4438:        char volume[] = "A:\\";
                   4439:        
                   4440:        if(path[1] == ':') {
                   4441:                volume[0] = path[0];
                   4442:        } else {
                   4443:                volume[0] = 'A' + _getdrive() - 1;
                   4444:        }
                   4445:        if(!GetVolumeInformation(volume, tmp, MAX_PATH, NULL, NULL, NULL, NULL, 0)) {
                   4446:                memset(tmp, 0, sizeof(tmp));
                   4447:        }
                   4448:        return(tmp);
                   4449: }
                   4450: 
1.1.1.45  root     4451: const char *msdos_short_volume_label(const char *label)
1.1       root     4452: {
                   4453:        static char tmp[(8 + 1 + 3) + 1];
1.1.1.45  root     4454:        const char *src = label;
1.1       root     4455:        int remain = strlen(label);
                   4456:        char *dst_n = tmp;
                   4457:        char *dst_e = tmp + 9;
                   4458:        
                   4459:        strcpy(tmp, "        .   ");
                   4460:        for(int i = 0; i < 8 && remain > 0; i++) {
                   4461:                if(msdos_lead_byte_check(*src)) {
                   4462:                        if(++i == 8) {
                   4463:                                break;
                   4464:                        }
                   4465:                        *dst_n++ = *src++;
                   4466:                        remain--;
                   4467:                }
                   4468:                *dst_n++ = *src++;
                   4469:                remain--;
                   4470:        }
                   4471:        if(remain > 0) {
                   4472:                for(int i = 0; i < 3 && remain > 0; i++) {
                   4473:                        if(msdos_lead_byte_check(*src)) {
                   4474:                                if(++i == 3) {
                   4475:                                        break;
                   4476:                                }
                   4477:                                *dst_e++ = *src++;
                   4478:                                remain--;
                   4479:                        }
                   4480:                        *dst_e++ = *src++;
                   4481:                        remain--;
                   4482:                }
                   4483:                *dst_e = '\0';
                   4484:        } else {
                   4485:                *dst_n = '\0';
                   4486:        }
                   4487:        my_strupr(tmp);
                   4488:        return(tmp);
                   4489: }
                   4490: 
1.1.1.13  root     4491: errno_t msdos_maperr(unsigned long oserrno)
                   4492: {
                   4493:        _doserrno = oserrno;
1.1.1.14  root     4494:        switch(oserrno) {
1.1.1.13  root     4495:        case ERROR_FILE_NOT_FOUND:         // 2
                   4496:        case ERROR_PATH_NOT_FOUND:         // 3
                   4497:        case ERROR_INVALID_DRIVE:          // 15
                   4498:        case ERROR_NO_MORE_FILES:          // 18
                   4499:        case ERROR_BAD_NETPATH:            // 53
                   4500:        case ERROR_BAD_NET_NAME:           // 67
                   4501:        case ERROR_BAD_PATHNAME:           // 161
                   4502:        case ERROR_FILENAME_EXCED_RANGE:   // 206
                   4503:                return ENOENT;
                   4504:        case ERROR_TOO_MANY_OPEN_FILES:    // 4
                   4505:                return EMFILE;
                   4506:        case ERROR_ACCESS_DENIED:          // 5
                   4507:        case ERROR_CURRENT_DIRECTORY:      // 16
                   4508:        case ERROR_NETWORK_ACCESS_DENIED:  // 65
                   4509:        case ERROR_CANNOT_MAKE:            // 82
                   4510:        case ERROR_FAIL_I24:               // 83
                   4511:        case ERROR_DRIVE_LOCKED:           // 108
                   4512:        case ERROR_SEEK_ON_DEVICE:         // 132
                   4513:        case ERROR_NOT_LOCKED:             // 158
                   4514:        case ERROR_LOCK_FAILED:            // 167
                   4515:                return EACCES;
                   4516:        case ERROR_INVALID_HANDLE:         // 6
                   4517:        case ERROR_INVALID_TARGET_HANDLE:  // 114
                   4518:        case ERROR_DIRECT_ACCESS_HANDLE:   // 130
                   4519:                return EBADF;
                   4520:        case ERROR_ARENA_TRASHED:          // 7
                   4521:        case ERROR_NOT_ENOUGH_MEMORY:      // 8
                   4522:        case ERROR_INVALID_BLOCK:          // 9
                   4523:        case ERROR_NOT_ENOUGH_QUOTA:       // 1816
                   4524:                return ENOMEM;
                   4525:        case ERROR_BAD_ENVIRONMENT:        // 10
                   4526:                return E2BIG;
                   4527:        case ERROR_BAD_FORMAT:             // 11
                   4528:                return ENOEXEC;
                   4529:        case ERROR_NOT_SAME_DEVICE:        // 17
                   4530:                return EXDEV;
                   4531:        case ERROR_FILE_EXISTS:            // 80
                   4532:        case ERROR_ALREADY_EXISTS:         // 183
                   4533:                return EEXIST;
                   4534:        case ERROR_NO_PROC_SLOTS:          // 89
                   4535:        case ERROR_MAX_THRDS_REACHED:      // 164
                   4536:        case ERROR_NESTING_NOT_ALLOWED:    // 215
                   4537:                return EAGAIN;
                   4538:        case ERROR_BROKEN_PIPE:            // 109
                   4539:                return EPIPE;
                   4540:        case ERROR_DISK_FULL:              // 112
                   4541:                return ENOSPC;
                   4542:        case ERROR_WAIT_NO_CHILDREN:       // 128
                   4543:        case ERROR_CHILD_NOT_COMPLETE:     // 129
                   4544:                return ECHILD;
                   4545:        case ERROR_DIR_NOT_EMPTY:          // 145
                   4546:                return ENOTEMPTY;
                   4547:        }
1.1.1.14  root     4548:        if(oserrno >= ERROR_WRITE_PROTECT /* 19 */ &&
1.1.1.13  root     4549:                oserrno <= ERROR_SHARING_BUFFER_EXCEEDED /* 36 */) {
                   4550:                return EACCES;
                   4551:        }
1.1.1.14  root     4552:        if(oserrno >= ERROR_INVALID_STARTING_CODESEG /* 188 */ &&
1.1.1.13  root     4553:                oserrno <= ERROR_INFLOOP_IN_RELOC_CHAIN /* 202 */) {
                   4554:                return ENOEXEC;
                   4555:        }
                   4556:        return EINVAL;
                   4557: }
                   4558: 
1.1.1.45  root     4559: int msdos_open(const char *path, int oflag)
1.1.1.13  root     4560: {
1.1.1.14  root     4561:        if((oflag & (_O_RDONLY | _O_WRONLY | _O_RDWR)) != _O_RDONLY) {
1.1.1.45  root     4562:                return(_open(path, oflag));
1.1.1.13  root     4563:        }
1.1.1.14  root     4564:        
                   4565:        SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, !(oflag & _O_NOINHERIT) };
1.1.1.13  root     4566:        DWORD disposition;
1.1.1.14  root     4567:        switch(oflag & (_O_CREAT | _O_EXCL | _O_TRUNC)) {
                   4568:        default:
1.1.1.13  root     4569:        case _O_EXCL:
                   4570:                disposition = OPEN_EXISTING;
                   4571:                break;
                   4572:        case _O_CREAT:
                   4573:                disposition = OPEN_ALWAYS;
                   4574:                break;
                   4575:        case _O_CREAT | _O_EXCL:
                   4576:        case _O_CREAT | _O_TRUNC | _O_EXCL:
                   4577:                disposition = CREATE_NEW;
                   4578:                break;
                   4579:        case _O_TRUNC:
                   4580:        case _O_TRUNC | _O_EXCL:
                   4581:                disposition = TRUNCATE_EXISTING;
                   4582:                break;
                   4583:        case _O_CREAT | _O_TRUNC:
                   4584:                disposition = CREATE_ALWAYS;
                   4585:                break;
                   4586:        }
1.1.1.14  root     4587:        
1.1.1.45  root     4588:        HANDLE h = CreateFile(path, GENERIC_READ | FILE_WRITE_ATTRIBUTES,
1.1.1.13  root     4589:                FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
                   4590:                FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14  root     4591:        if(h == INVALID_HANDLE_VALUE) {
1.1.1.13  root     4592:                // FILE_WRITE_ATTRIBUTES may not be granted for standard users.
                   4593:                // Retry without FILE_WRITE_ATTRIBUTES.
1.1.1.45  root     4594:                h = CreateFile(path, GENERIC_READ,
1.1.1.13  root     4595:                        FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
                   4596:                        FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14  root     4597:                if(h == INVALID_HANDLE_VALUE) {
1.1.1.13  root     4598:                        errno = msdos_maperr(GetLastError());
1.1.1.45  root     4599:                        return(-1);
1.1.1.13  root     4600:                }
                   4601:        }
1.1.1.14  root     4602:        
1.1.1.13  root     4603:        int fd = _open_osfhandle((intptr_t) h, oflag);
1.1.1.14  root     4604:        if(fd == -1) {
1.1.1.13  root     4605:                CloseHandle(h);
                   4606:        }
1.1.1.45  root     4607:        return(fd);
                   4608: }
                   4609: 
                   4610: int msdos_open_device(const char *path, int oflag, int *sio_port, int *lpt_port)
                   4611: {
                   4612:        int fd = -1;
                   4613:        
                   4614:        *sio_port = *lpt_port = 0;
                   4615:        
                   4616:        if(msdos_is_con_path(path)) {
                   4617:                // MODE.COM opens CON device with read/write mode :-(
                   4618:                if((oflag & (_O_RDONLY | _O_WRONLY | _O_RDWR)) == _O_RDWR) {
                   4619:                        oflag &= ~(_O_RDONLY | _O_WRONLY | _O_RDWR);
                   4620:                        oflag |= _O_RDONLY;
                   4621:                }
                   4622:                if((fd = msdos_open("CON", oflag)) == -1) {
                   4623: //                     fd = msdos_open("NUL", oflag);
                   4624:                }
                   4625:        } else if((*sio_port = msdos_is_comm_path(path)) != 0) {
                   4626:                fd = msdos_open("NUL", oflag);
                   4627:                msdos_set_comm_params(*sio_port, path);
                   4628:        } else if((*lpt_port = msdos_is_prn_path(path)) != 0) {
                   4629:                fd = msdos_open("NUL", oflag);
                   4630:        } else if(msdos_is_device_path(path)) {
                   4631:                fd = msdos_open("NUL", oflag);
                   4632: //     } else if(oflag & _O_CREAT) {
                   4633: //             fd = _open(path, oflag, _S_IREAD | _S_IWRITE);
                   4634: //     } else {
                   4635: //             fd = _open(path, oflag);
                   4636:        }
                   4637:        return(fd);
                   4638: }
                   4639: 
                   4640: UINT16 msdos_device_info(const char *path)
                   4641: {
                   4642:        if(msdos_is_con_path(path)) {
                   4643:                return(0x80d3);
                   4644:        } else if(msdos_is_comm_path(path)) {
                   4645:                return(0x80a0);
                   4646:        } else if(msdos_is_prn_path(path)) {
                   4647: //             return(0xa8c0);
                   4648:                return(0x80a0);
                   4649:        } else if(msdos_is_device_path(path)) {
                   4650:                if(strstr(path, "EMMXXXX0") != NULL) {
                   4651:                        return(0xc0c0);
                   4652:                } else if(strstr(path, "MSCD001") != NULL) {
                   4653:                        return(0xc880);
                   4654:                } else {
                   4655:                        return(0x8084);
                   4656:                }
                   4657:        } else {
                   4658:                return(msdos_drive_number(path));
                   4659:        }
1.1.1.13  root     4660: }
                   4661: 
1.1.1.37  root     4662: void msdos_file_handler_open(int fd, const char *path, int atty, int mode, UINT16 info, UINT16 psp_seg, int sio_port, int lpt_port)
1.1       root     4663: {
                   4664:        static int id = 0;
                   4665:        char full[MAX_PATH], *name;
                   4666:        
                   4667:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
                   4668:                strcpy(file_handler[fd].path, full);
                   4669:        } else {
                   4670:                strcpy(file_handler[fd].path, path);
                   4671:        }
1.1.1.14  root     4672:        // isatty makes no distinction between CON & NUL
                   4673:        // GetFileSize fails on CON, succeeds on NUL
                   4674:        if(atty && (info != 0x80d3 || GetFileSize((HANDLE)_get_osfhandle(fd), NULL) == 0)) {
1.1.1.45  root     4675:                if(info == 0x80d3) {
                   4676:                        info = 0x8084;
                   4677:                }
1.1.1.14  root     4678:                atty = 0;
                   4679:        } else if(!atty && info == 0x80d3) {
1.1.1.45  root     4680: //             info = msdos_drive_number(".");
                   4681:                info = msdos_drive_number(path);
1.1.1.14  root     4682:        }
1.1       root     4683:        file_handler[fd].valid = 1;
                   4684:        file_handler[fd].id = id++;     // dummy id for int 21h ax=71a6h
1.1.1.37  root     4685:        file_handler[fd].atty = (sio_port == 0 && lpt_port == 0) ? atty : 0;
1.1       root     4686:        file_handler[fd].mode = mode;
                   4687:        file_handler[fd].info = info;
                   4688:        file_handler[fd].psp = psp_seg;
1.1.1.37  root     4689:        file_handler[fd].sio_port = sio_port;
                   4690:        file_handler[fd].lpt_port = lpt_port;
1.1.1.21  root     4691:        
                   4692:        // init system file table
                   4693:        if(fd < 20) {
                   4694:                UINT8 *sft = mem + SFT_TOP + 6 + 0x3b * fd;
                   4695:                
                   4696:                memset(sft, 0, 0x3b);
                   4697:                
                   4698:                *(UINT16 *)(sft + 0x00) = 1;
                   4699:                *(UINT16 *)(sft + 0x02) = file_handler[fd].mode;
                   4700:                *(UINT8  *)(sft + 0x04) = GetFileAttributes(file_handler[fd].path) & 0xff;
                   4701:                *(UINT16 *)(sft + 0x05) = file_handler[fd].info & 0xff;
                   4702:                
                   4703:                if(!(file_handler[fd].info & 0x80)) {
                   4704:                        *(UINT16 *)(sft + 0x07) = sizeof(dpb_t) * (file_handler[fd].info & 0x1f);
                   4705:                        *(UINT16 *)(sft + 0x09) = DPB_TOP >> 4;
                   4706:                        
                   4707:                        FILETIME time, local;
                   4708:                        HANDLE hHandle;
                   4709:                        WORD dos_date = 0, dos_time = 0;
                   4710:                        DWORD file_size = 0;
                   4711:                        if((hHandle = (HANDLE)_get_osfhandle(fd)) != INVALID_HANDLE_VALUE) {
                   4712:                                if(GetFileTime(hHandle, NULL, NULL, &time)) {
                   4713:                                        FileTimeToLocalFileTime(&time, &local);
                   4714:                                        FileTimeToDosDateTime(&local, &dos_date, &dos_time);
                   4715:                                }
                   4716:                                file_size = GetFileSize(hHandle, NULL);
                   4717:                        }
                   4718:                        *(UINT16 *)(sft + 0x0d) = dos_time;
                   4719:                        *(UINT16 *)(sft + 0x0f) = dos_date;
                   4720:                        *(UINT32 *)(sft + 0x11) = file_size;
                   4721:                }
                   4722:                
                   4723:                char fname[MAX_PATH] = {0}, ext[MAX_PATH] = {0};
                   4724:                _splitpath(file_handler[fd].path, NULL, NULL, fname, ext);
                   4725:                my_strupr(fname);
                   4726:                my_strupr(ext);
                   4727:                memset(sft + 0x20, 0x20, 11);
                   4728:                memcpy(sft + 0x20, fname, min(strlen(fname), 8));
                   4729:                memcpy(sft + 0x28, ext + 1, min(strlen(ext + 1), 3));
                   4730:                
                   4731:                *(UINT16 *)(sft + 0x31) = psp_seg;
                   4732:        }
1.1       root     4733: }
                   4734: 
1.1.1.37  root     4735: void msdos_file_handler_open(int fd, const char *path, int atty, int mode, UINT16 info, UINT16 psp_seg)
                   4736: {
                   4737:         msdos_file_handler_open(fd, path, atty, mode, info, psp_seg, 0, 0);
                   4738: }
                   4739: 
1.1       root     4740: void msdos_file_handler_dup(int dst, int src, UINT16 psp_seg)
                   4741: {
                   4742:        strcpy(file_handler[dst].path, file_handler[src].path);
                   4743:        file_handler[dst].valid = 1;
                   4744:        file_handler[dst].id = file_handler[src].id;
                   4745:        file_handler[dst].atty = file_handler[src].atty;
                   4746:        file_handler[dst].mode = file_handler[src].mode;
                   4747:        file_handler[dst].info = file_handler[src].info;
                   4748:        file_handler[dst].psp = psp_seg;
1.1.1.37  root     4749:        file_handler[dst].sio_port = file_handler[src].sio_port;
                   4750:        file_handler[dst].lpt_port = file_handler[src].lpt_port;
1.1       root     4751: }
                   4752: 
1.1.1.20  root     4753: void msdos_file_handler_close(int fd)
1.1       root     4754: {
                   4755:        file_handler[fd].valid = 0;
1.1.1.21  root     4756:        
                   4757:        if(fd < 20) {
                   4758:                memset(mem + SFT_TOP + 6 + 0x3b * fd, 0, 0x3b);
                   4759:        }
1.1       root     4760: }
                   4761: 
1.1.1.14  root     4762: inline int msdos_file_attribute_create(UINT16 new_attr)
1.1       root     4763: {
1.1.1.14  root     4764:        return(new_attr & (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN |
                   4765:                           FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE |
                   4766:                           FILE_ATTRIBUTE_DIRECTORY));
1.1       root     4767: }
                   4768: 
                   4769: // find file
                   4770: 
                   4771: int msdos_find_file_check_attribute(int attribute, int allowed_mask, int required_mask)
                   4772: {
                   4773:        if((allowed_mask & 0x08) && !(attribute & FILE_ATTRIBUTE_DIRECTORY)) {
                   4774:                return(0);      // search directory only !!!
                   4775:        } else if(!(allowed_mask & 0x02) && (attribute & FILE_ATTRIBUTE_HIDDEN)) {
                   4776:                return(0);
                   4777:        } else if(!(allowed_mask & 0x04) && (attribute & FILE_ATTRIBUTE_SYSTEM)) {
                   4778:                return(0);
                   4779:        } else if(!(allowed_mask & 0x10) && (attribute & FILE_ATTRIBUTE_DIRECTORY)) {
                   4780:                return(0);
                   4781:        } else if((attribute & required_mask) != required_mask) {
                   4782:                return(0);
                   4783:        } else {
                   4784:                return(1);
                   4785:        }
                   4786: }
                   4787: 
1.1.1.13  root     4788: int msdos_find_file_has_8dot3name(WIN32_FIND_DATA *fd)
                   4789: {
1.1.1.14  root     4790:        if(fd->cAlternateFileName[0]) {
1.1.1.42  root     4791:                return(1);
1.1.1.13  root     4792:        }
                   4793:        size_t len = strlen(fd->cFileName);
1.1.1.14  root     4794:        if(len > 12) {
1.1.1.42  root     4795:                return(0);
1.1.1.13  root     4796:        }
                   4797:        const char *ext = strrchr(fd->cFileName, '.');
1.1.1.14  root     4798:        if((ext ? ext - fd->cFileName : len) > 8) {
1.1.1.42  root     4799:                return(0);
1.1.1.13  root     4800:        }
1.1.1.42  root     4801:        return(1);
1.1.1.13  root     4802: }
                   4803: 
1.1       root     4804: void msdos_find_file_conv_local_time(WIN32_FIND_DATA *fd)
                   4805: {
                   4806:        FILETIME local;
                   4807:        
                   4808:        FileTimeToLocalFileTime(&fd->ftCreationTime, &local);
                   4809:        fd->ftCreationTime.dwLowDateTime = local.dwLowDateTime;
                   4810:        fd->ftCreationTime.dwHighDateTime = local.dwHighDateTime;
                   4811:        
                   4812:        FileTimeToLocalFileTime(&fd->ftLastAccessTime, &local);
                   4813:        fd->ftLastAccessTime.dwLowDateTime = local.dwLowDateTime;
                   4814:        fd->ftLastAccessTime.dwHighDateTime = local.dwHighDateTime;
                   4815:        
                   4816:        FileTimeToLocalFileTime(&fd->ftLastWriteTime, &local);
                   4817:        fd->ftLastWriteTime.dwLowDateTime = local.dwLowDateTime;
                   4818:        fd->ftLastWriteTime.dwHighDateTime = local.dwHighDateTime;
                   4819: }
                   4820: 
                   4821: // i/o
                   4822: 
                   4823: void msdos_stdio_reopen()
                   4824: {
                   4825:        if(!file_handler[0].valid) {
                   4826:                _dup2(DUP_STDIN, 0);
                   4827:                msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   4828:        }
                   4829:        if(!file_handler[1].valid) {
                   4830:                _dup2(DUP_STDOUT, 1);
                   4831:                msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   4832:        }
                   4833:        if(!file_handler[2].valid) {
                   4834:                _dup2(DUP_STDERR, 2);
                   4835:                msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
                   4836:        }
1.1.1.21  root     4837:        if(!file_handler[3].valid) {
                   4838:                _dup2(DUP_STDAUX, 3);
                   4839:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
                   4840:        }
                   4841:        if(!file_handler[4].valid) {
                   4842:                _dup2(DUP_STDPRN, 4);
1.1.1.45  root     4843: //             msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0, 0, 1); // LPT1
                   4844:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0x80a0, 0, 0, 1); // LPT1
1.1.1.21  root     4845:        }
                   4846:        for(int i = 0; i < 5; i++) {
                   4847:                if(msdos_psp_get_file_table(i, current_psp) == 0xff) {
                   4848:                        msdos_psp_set_file_table(i, i, current_psp);
                   4849:                }
                   4850:        }
1.1       root     4851: }
                   4852: 
1.1.1.37  root     4853: int msdos_read(int fd, void *buffer, unsigned int count)
                   4854: {
                   4855:        if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].sio_port >= 1 && file_handler[fd].sio_port <= 4) {
                   4856:                // read from serial port
                   4857:                int read = 0;
                   4858:                if(sio_port_number[file_handler[fd].sio_port - 1] != 0) {
                   4859:                        UINT8 *buf = (UINT8 *)buffer;
                   4860:                        UINT8 selector = sio_read(file_handler[fd].sio_port - 1, 3);
                   4861:                        sio_write(file_handler[fd].sio_port - 1, 3, selector & ~0x80);
1.1.1.38  root     4862:                        DWORD timeout = timeGetTime() + 1000;
                   4863:                        while(read < count) {
                   4864:                                if(sio_read(file_handler[fd].sio_port - 1, 5) & 0x01) {
                   4865:                                        buf[read++] = sio_read(file_handler[fd].sio_port - 1, 0);
                   4866:                                        timeout = timeGetTime() + 1000;
                   4867:                                } else {
                   4868:                                        if(timeGetTime() > timeout) {
                   4869:                                                break;
                   4870:                                        }
                   4871:                                        Sleep(10);
1.1.1.37  root     4872:                                }
                   4873:                        }
                   4874:                        sio_write(file_handler[fd].sio_port - 1, 3, selector);
                   4875:                }
                   4876:                return(read);
                   4877:        }
                   4878:        return(_read(fd, buffer, count));
                   4879: }
                   4880: 
1.1       root     4881: int msdos_kbhit()
                   4882: {
                   4883:        msdos_stdio_reopen();
                   4884:        
1.1.1.20  root     4885:        process_t *process = msdos_process_info_get(current_psp);
                   4886:        int fd = msdos_psp_get_file_table(0, current_psp);
                   4887:        
                   4888:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     4889:                // stdin is redirected to file
1.1.1.20  root     4890:                return(eof(fd) == 0);
1.1       root     4891:        }
                   4892:        
                   4893:        // check keyboard status
1.1.1.35  root     4894:        if(key_recv != 0) {
1.1       root     4895:                return(1);
                   4896:        }
1.1.1.35  root     4897:        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   4898: #ifdef USE_SERVICE_THREAD
                   4899:                EnterCriticalSection(&key_buf_crit_sect);
                   4900: #endif
                   4901:                bool empty = key_buf_char->empty();
                   4902: #ifdef USE_SERVICE_THREAD
                   4903:                LeaveCriticalSection(&key_buf_crit_sect);
                   4904: #endif
                   4905:                if(!empty) return(1);
                   4906:        }
                   4907:        return(_kbhit());
1.1       root     4908: }
                   4909: 
                   4910: int msdos_getch_ex(int echo)
                   4911: {
                   4912:        static char prev = 0;
                   4913:        
                   4914:        msdos_stdio_reopen();
                   4915:        
1.1.1.20  root     4916:        process_t *process = msdos_process_info_get(current_psp);
                   4917:        int fd = msdos_psp_get_file_table(0, current_psp);
                   4918:        
                   4919:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     4920:                // stdin is redirected to file
                   4921: retry:
                   4922:                char data;
1.1.1.37  root     4923:                if(msdos_read(fd, &data, 1) == 1) {
1.1       root     4924:                        char tmp = data;
                   4925:                        if(data == 0x0a) {
                   4926:                                if(prev == 0x0d) {
                   4927:                                        goto retry; // CRLF -> skip LF
                   4928:                                } else {
                   4929:                                        data = 0x0d; // LF only -> CR
                   4930:                                }
                   4931:                        }
                   4932:                        prev = tmp;
                   4933:                        return(data);
                   4934:                }
                   4935:                return(EOF);
                   4936:        }
                   4937:        
                   4938:        // input from console
1.1.1.5   root     4939:        int key_char, key_scan;
1.1.1.33  root     4940:        if(key_recv != 0) {
1.1.1.5   root     4941:                key_char = (key_code >> 0) & 0xff;
                   4942:                key_scan = (key_code >> 8) & 0xff;
                   4943:                key_code >>= 16;
1.1.1.33  root     4944:                key_recv >>= 16;
1.1.1.5   root     4945:        } else {
1.1.1.35  root     4946:                while(key_buf_char != NULL && key_buf_scan != NULL && !m_halted) {
                   4947:                        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   4948: #ifdef USE_SERVICE_THREAD
                   4949:                                EnterCriticalSection(&key_buf_crit_sect);
                   4950: #endif
                   4951:                                bool empty = key_buf_char->empty();
                   4952: #ifdef USE_SERVICE_THREAD
                   4953:                                LeaveCriticalSection(&key_buf_crit_sect);
                   4954: #endif
                   4955:                                if(!empty) break;
                   4956:                        }
1.1.1.23  root     4957:                        if(!(fd < process->max_files && file_handler[fd].valid && file_handler[fd].atty && file_mode[file_handler[fd].mode].in)) {
                   4958:                                // NOTE: stdin is redirected to stderr when we do "type (file) | more" on freedos's command.com
                   4959:                                if(_kbhit()) {
1.1.1.32  root     4960:                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     4961: #ifdef USE_SERVICE_THREAD
                   4962:                                                EnterCriticalSection(&key_buf_crit_sect);
                   4963: #endif
1.1.1.32  root     4964:                                                key_buf_char->write(_getch());
1.1.1.35  root     4965:                                                key_buf_scan->write(0x00);
                   4966: #ifdef USE_SERVICE_THREAD
                   4967:                                                LeaveCriticalSection(&key_buf_crit_sect);
                   4968: #endif
1.1.1.32  root     4969:                                        }
1.1.1.23  root     4970:                                } else {
                   4971:                                        Sleep(10);
                   4972:                                }
                   4973:                        } else {
                   4974:                                if(!update_key_buffer()) {
                   4975:                                        Sleep(10);
                   4976:                                }
1.1.1.14  root     4977:                        }
                   4978:                }
                   4979:                if(m_halted) {
1.1.1.33  root     4980:                        // insert CR to terminate input loops
1.1.1.14  root     4981:                        key_char = 0x0d;
                   4982:                        key_scan = 0;
1.1.1.32  root     4983:                } else if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     4984: #ifdef USE_SERVICE_THREAD
                   4985:                        EnterCriticalSection(&key_buf_crit_sect);
                   4986: #endif
1.1.1.14  root     4987:                        key_char = key_buf_char->read();
                   4988:                        key_scan = key_buf_scan->read();
1.1.1.41  root     4989:                        // write to bottom of key buffer
                   4990:                        mem[0x43c] = (UINT8)key_char;
                   4991:                        mem[0x43d] = (UINT8)key_scan;
1.1.1.35  root     4992: #ifdef USE_SERVICE_THREAD
                   4993:                        LeaveCriticalSection(&key_buf_crit_sect);
                   4994: #endif
1.1.1.5   root     4995:                }
1.1       root     4996:        }
                   4997:        if(echo && key_char) {
                   4998:                msdos_putch(key_char);
                   4999:        }
                   5000:        return key_char ? key_char : (key_scan != 0xe0) ? key_scan : 0;
                   5001: }
                   5002: 
                   5003: inline int msdos_getch()
                   5004: {
                   5005:        return(msdos_getch_ex(0));
                   5006: }
                   5007: 
                   5008: inline int msdos_getche()
                   5009: {
                   5010:        return(msdos_getch_ex(1));
                   5011: }
                   5012: 
                   5013: int msdos_write(int fd, const void *buffer, unsigned int count)
                   5014: {
1.1.1.37  root     5015:        if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].sio_port >= 1 && file_handler[fd].sio_port <= 4) {
                   5016:                // write to serial port
1.1.1.38  root     5017:                int written = 0;
1.1.1.37  root     5018:                if(sio_port_number[file_handler[fd].sio_port - 1] != 0) {
                   5019:                        UINT8 *buf = (UINT8 *)buffer;
                   5020:                        UINT8 selector = sio_read(file_handler[fd].sio_port - 1, 3);
                   5021:                        sio_write(file_handler[fd].sio_port - 1, 3, selector & ~0x80);
1.1.1.38  root     5022:                        DWORD timeout = timeGetTime() + 1000;
                   5023:                        while(written < count) {
                   5024:                                if(sio_read(file_handler[fd].sio_port - 1, 5) & 0x20) {
                   5025:                                        sio_write(file_handler[fd].sio_port - 1, 0, buf[written++]);
                   5026:                                        timeout = timeGetTime() + 1000;
                   5027:                                } else {
                   5028:                                        if(timeGetTime() > timeout) {
                   5029:                                                break;
                   5030:                                        }
                   5031:                                        Sleep(10);
                   5032:                                }
1.1.1.37  root     5033:                        }
                   5034:                        sio_write(file_handler[fd].sio_port - 1, 3, selector);
                   5035:                }
1.1.1.38  root     5036:                return(written);
1.1.1.37  root     5037:        } else if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].lpt_port >= 1 && file_handler[fd].lpt_port <= 3) {
                   5038:                // write to printer port
                   5039:                UINT8 *buf = (UINT8 *)buffer;
                   5040:                for(unsigned int i = 0; i < count; i++) {
                   5041: //                     printer_out(file_handler[fd].lpt_port - 1, buf[i]);
                   5042:                        pcbios_printer_out(file_handler[fd].lpt_port - 1, buf[i]);
                   5043:                }
                   5044:                return(count);
                   5045:        } else if(fd == 1 && file_handler[1].valid && !file_handler[1].atty) {
1.1       root     5046:                // CR+LF -> LF
1.1.1.37  root     5047:                static int is_cr = 0;
1.1       root     5048:                UINT8 *buf = (UINT8 *)buffer;
                   5049:                for(unsigned int i = 0; i < count; i++) {
                   5050:                        UINT8 data = buf[i];
                   5051:                        if(is_cr) {
                   5052:                                if(data != 0x0a) {
                   5053:                                        UINT8 tmp = 0x0d;
                   5054:                                        _write(1, &tmp, 1);
                   5055:                                }
                   5056:                                _write(1, &data, 1);
                   5057:                                is_cr = 0;
                   5058:                        } else if(data == 0x0d) {
                   5059:                                is_cr = 1;
                   5060:                        } else {
                   5061:                                _write(1, &data, 1);
                   5062:                        }
                   5063:                }
                   5064:                return(count);
                   5065:        }
1.1.1.14  root     5066:        vram_flush();
1.1       root     5067:        return(_write(fd, buffer, count));
                   5068: }
                   5069: 
                   5070: void msdos_putch(UINT8 data)
1.1.1.35  root     5071: #ifdef USE_SERVICE_THREAD
                   5072: {
                   5073:        EnterCriticalSection(&putch_crit_sect);
                   5074:        msdos_putch_tmp(data);
                   5075:        LeaveCriticalSection(&putch_crit_sect);
                   5076: }
                   5077: void msdos_putch_tmp(UINT8 data)
                   5078: #endif
1.1       root     5079: {
1.1.1.34  root     5080:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   5081:        SMALL_RECT rect;
                   5082:        COORD co;
1.1       root     5083:        static int p = 0;
                   5084:        static int is_kanji = 0;
                   5085:        static int is_esc = 0;
                   5086:        static int stored_x;
                   5087:        static int stored_y;
                   5088:        static WORD stored_a;
1.1.1.20  root     5089:        static char tmp[64], out[64];
1.1       root     5090:        
                   5091:        msdos_stdio_reopen();
                   5092:        
1.1.1.20  root     5093:        process_t *process = msdos_process_info_get(current_psp);
                   5094:        int fd = msdos_psp_get_file_table(1, current_psp);
                   5095:        
                   5096:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     5097:                // stdout is redirected to file
1.1.1.20  root     5098:                msdos_write(fd, &data, 1);
1.1       root     5099:                return;
                   5100:        }
1.1.1.23  root     5101:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     5102:        
                   5103:        // output to console
                   5104:        tmp[p++] = data;
                   5105:        
1.1.1.14  root     5106:        vram_flush();
                   5107:        
1.1       root     5108:        if(is_kanji) {
                   5109:                // kanji character
                   5110:                is_kanji = 0;
                   5111:        } else if(is_esc) {
                   5112:                // escape sequense
                   5113:                if((tmp[1] == ')' || tmp[1] == '(') && p == 3) {
                   5114:                        p = is_esc = 0;
                   5115:                } else if(tmp[1] == '=' && p == 4) {
                   5116:                        co.X = tmp[3] - 0x20;
1.1.1.14  root     5117:                        co.Y = tmp[2] - 0x20 + scr_top;
1.1       root     5118:                        SetConsoleCursorPosition(hStdout, co);
                   5119:                        mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14  root     5120:                        mem[0x451 + mem[0x462] * 2] = co.Y - scr_top;
1.1       root     5121:                        cursor_moved = false;
                   5122:                        p = is_esc = 0;
                   5123:                } else if((data >= 'a' && data <= 'z') || (data >= 'A' && data <= 'Z') || data == '*') {
                   5124:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5125:                        co.X = csbi.dwCursorPosition.X;
                   5126:                        co.Y = csbi.dwCursorPosition.Y;
                   5127:                        WORD wAttributes = csbi.wAttributes;
                   5128:                        
                   5129:                        if(tmp[1] == 'D') {
                   5130:                                co.Y++;
                   5131:                        } else if(tmp[1] == 'E') {
                   5132:                                co.X = 0;
                   5133:                                co.Y++;
                   5134:                        } else if(tmp[1] == 'M') {
                   5135:                                co.Y--;
                   5136:                        } else if(tmp[1] == '*') {
1.1.1.14  root     5137:                                SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5138:                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5139:                                co.X = 0;
                   5140:                                co.Y = csbi.srWindow.Top;
1.1       root     5141:                        } else if(tmp[1] == '[') {
                   5142:                                int param[256], params = 0;
                   5143:                                memset(param, 0, sizeof(param));
                   5144:                                for(int i = 2; i < p; i++) {
                   5145:                                        if(tmp[i] >= '0' && tmp[i] <= '9') {
                   5146:                                                param[params] *= 10;
                   5147:                                                param[params] += tmp[i] - '0';
                   5148:                                        } else {
                   5149:                                                params++;
                   5150:                                        }
                   5151:                                }
                   5152:                                if(data == 'A') {
1.1.1.14  root     5153:                                        co.Y -= (params == 0) ? 1 : param[0];
1.1       root     5154:                                } else if(data == 'B') {
1.1.1.14  root     5155:                                        co.Y += (params == 0) ? 1 : param[0];
1.1       root     5156:                                } else if(data == 'C') {
1.1.1.14  root     5157:                                        co.X += (params == 0) ? 1 : param[0];
1.1       root     5158:                                } else if(data == 'D') {
1.1.1.14  root     5159:                                        co.X -= (params == 0) ? 1 : param[0];
1.1       root     5160:                                } else if(data == 'H' || data == 'f') {
1.1.1.14  root     5161:                                        co.X = (param[1] == 0 ? 1 : param[1]) - 1;
                   5162:                                        co.Y = (param[0] == 0 ? 1 : param[0]) - 1 + csbi.srWindow.Top;
1.1       root     5163:                                } else if(data == 'J') {
1.1.1.14  root     5164:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5165:                                        if(param[0] == 0) {
                   5166:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     5167:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5168:                                                if(co.Y < csbi.srWindow.Bottom) {
                   5169:                                                        SET_RECT(rect, 0, co.Y + 1, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5170:                                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5171:                                                }
                   5172:                                        } else if(param[0] == 1) {
1.1.1.14  root     5173:                                                if(co.Y > csbi.srWindow.Top) {
                   5174:                                                        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, co.Y - 1);
                   5175:                                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5176:                                                }
                   5177:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.14  root     5178:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5179:                                        } else if(param[0] == 2) {
1.1.1.14  root     5180:                                                SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5181:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5182:                                                co.X = co.Y = 0;
                   5183:                                        }
                   5184:                                } else if(data == 'K') {
1.1.1.14  root     5185:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5186:                                        if(param[0] == 0) {
                   5187:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     5188:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5189:                                        } else if(param[0] == 1) {
                   5190:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.14  root     5191:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5192:                                        } else if(param[0] == 2) {
                   5193:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     5194:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5195:                                        }
                   5196:                                } else if(data == 'L') {
1.1.1.14  root     5197:                                        if(params == 0) {
                   5198:                                                param[0] = 1;
1.1       root     5199:                                        }
1.1.1.14  root     5200:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5201:                                        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5202:                                        SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5203:                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5204:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5205:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y + param[0] - 1);
1.1.1.14  root     5206:                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5207:                                        co.X = 0;
                   5208:                                } else if(data == 'M') {
1.1.1.14  root     5209:                                        if(params == 0) {
                   5210:                                                param[0] = 1;
                   5211:                                        }
                   5212:                                        if(co.Y + param[0] > csbi.srWindow.Bottom) {
                   5213:                                                clear_scr_buffer(csbi.wAttributes);
                   5214:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5215:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5216:                                        } else {
1.1.1.14  root     5217:                                                SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5218:                                                ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5219:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5220:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5221:                                                clear_scr_buffer(csbi.wAttributes);
1.1       root     5222:                                        }
                   5223:                                        co.X = 0;
                   5224:                                } else if(data == 'h') {
                   5225:                                        if(tmp[2] == '>' && tmp[3] == '5') {
                   5226:                                                CONSOLE_CURSOR_INFO cur;
                   5227:                                                GetConsoleCursorInfo(hStdout, &cur);
                   5228:                                                if(cur.bVisible) {
                   5229:                                                        cur.bVisible = FALSE;
1.1.1.14  root     5230: //                                                     SetConsoleCursorInfo(hStdout, &cur);
1.1       root     5231:                                                }
                   5232:                                        }
                   5233:                                } else if(data == 'l') {
                   5234:                                        if(tmp[2] == '>' && tmp[3] == '5') {
                   5235:                                                CONSOLE_CURSOR_INFO cur;
                   5236:                                                GetConsoleCursorInfo(hStdout, &cur);
                   5237:                                                if(!cur.bVisible) {
                   5238:                                                        cur.bVisible = TRUE;
1.1.1.14  root     5239: //                                                     SetConsoleCursorInfo(hStdout, &cur);
1.1       root     5240:                                                }
                   5241:                                        }
                   5242:                                } else if(data == 'm') {
                   5243:                                        wAttributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
                   5244:                                        int reverse = 0, hidden = 0;
                   5245:                                        for(int i = 0; i < params; i++) {
                   5246:                                                if(param[i] == 1) {
                   5247:                                                        wAttributes |= FOREGROUND_INTENSITY;
                   5248:                                                } else if(param[i] == 4) {
                   5249:                                                        wAttributes |= COMMON_LVB_UNDERSCORE;
                   5250:                                                } else if(param[i] == 7) {
                   5251:                                                        reverse = 1;
                   5252:                                                } else if(param[i] == 8 || param[i] == 16) {
                   5253:                                                        hidden = 1;
                   5254:                                                } else if((param[i] >= 17 && param[i] <= 23) || (param[i] >= 30 && param[i] <= 37)) {
                   5255:                                                        wAttributes &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
                   5256:                                                        if(param[i] >= 17 && param[i] <= 23) {
                   5257:                                                                param[i] -= 16;
                   5258:                                                        } else {
                   5259:                                                                param[i] -= 30;
                   5260:                                                        }
                   5261:                                                        if(param[i] & 1) {
                   5262:                                                                wAttributes |= FOREGROUND_RED;
                   5263:                                                        }
                   5264:                                                        if(param[i] & 2) {
                   5265:                                                                wAttributes |= FOREGROUND_GREEN;
                   5266:                                                        }
                   5267:                                                        if(param[i] & 4) {
                   5268:                                                                wAttributes |= FOREGROUND_BLUE;
                   5269:                                                        }
                   5270:                                                } else if(param[i] >= 40 && param[i] <= 47) {
                   5271:                                                        wAttributes &= ~(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
                   5272:                                                        if((param[i] - 40) & 1) {
                   5273:                                                                wAttributes |= BACKGROUND_RED;
                   5274:                                                        }
                   5275:                                                        if((param[i] - 40) & 2) {
                   5276:                                                                wAttributes |= BACKGROUND_GREEN;
                   5277:                                                        }
                   5278:                                                        if((param[i] - 40) & 4) {
                   5279:                                                                wAttributes |= BACKGROUND_BLUE;
                   5280:                                                        }
                   5281:                                                }
                   5282:                                        }
                   5283:                                        if(reverse) {
                   5284:                                                wAttributes &= ~0xff;
                   5285:                                                wAttributes |= BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
                   5286:                                        }
                   5287:                                        if(hidden) {
                   5288:                                                wAttributes &= ~0x0f;
                   5289:                                                wAttributes |= (wAttributes >> 4) & 0x0f;
                   5290:                                        }
                   5291:                                } else if(data == 'n') {
                   5292:                                        if(param[0] == 6) {
                   5293:                                                char tmp[16];
                   5294:                                                sprintf(tmp, "\x1b[%d;%dR", co.Y + 1, co.X + 1);
                   5295:                                                int len = strlen(tmp);
1.1.1.32  root     5296:                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     5297: #ifdef USE_SERVICE_THREAD
                   5298:                                                        EnterCriticalSection(&key_buf_crit_sect);
                   5299: #endif
1.1.1.32  root     5300:                                                        for(int i = 0; i < len; i++) {
                   5301:                                                                key_buf_char->write(tmp[i]);
                   5302:                                                                key_buf_scan->write(0x00);
                   5303:                                                        }
1.1.1.35  root     5304: #ifdef USE_SERVICE_THREAD
                   5305:                                                        LeaveCriticalSection(&key_buf_crit_sect);
                   5306: #endif
1.1       root     5307:                                                }
                   5308:                                        }
                   5309:                                } else if(data == 's') {
                   5310:                                        stored_x = co.X;
                   5311:                                        stored_y = co.Y;
                   5312:                                        stored_a = wAttributes;
                   5313:                                } else if(data == 'u') {
                   5314:                                        co.X = stored_x;
                   5315:                                        co.Y = stored_y;
                   5316:                                        wAttributes = stored_a;
                   5317:                                }
                   5318:                        }
                   5319:                        if(co.X < 0) {
                   5320:                                co.X = 0;
                   5321:                        } else if(co.X >= csbi.dwSize.X) {
                   5322:                                co.X = csbi.dwSize.X - 1;
                   5323:                        }
1.1.1.14  root     5324:                        if(co.Y < csbi.srWindow.Top) {
                   5325:                                co.Y = csbi.srWindow.Top;
                   5326:                        } else if(co.Y > csbi.srWindow.Bottom) {
                   5327:                                co.Y = csbi.srWindow.Bottom;
1.1       root     5328:                        }
                   5329:                        if(co.X != csbi.dwCursorPosition.X || co.Y != csbi.dwCursorPosition.Y) {
                   5330:                                SetConsoleCursorPosition(hStdout, co);
                   5331:                                mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14  root     5332:                                mem[0x451 + mem[0x462] * 2] = co.Y - csbi.srWindow.Top;
1.1       root     5333:                                cursor_moved = false;
                   5334:                        }
                   5335:                        if(wAttributes != csbi.wAttributes) {
                   5336:                                SetConsoleTextAttribute(hStdout, wAttributes);
                   5337:                        }
                   5338:                        p = is_esc = 0;
                   5339:                }
                   5340:                return;
                   5341:        } else {
                   5342:                if(msdos_lead_byte_check(data)) {
                   5343:                        is_kanji = 1;
                   5344:                        return;
                   5345:                } else if(data == 0x1b) {
                   5346:                        is_esc = 1;
                   5347:                        return;
                   5348:                }
                   5349:        }
1.1.1.20  root     5350:        
                   5351:        DWORD q = 0, num;
                   5352:        is_kanji = 0;
                   5353:        for(int i = 0; i < p; i++) {
                   5354:                UINT8 c = tmp[i];
                   5355:                if(is_kanji) {
                   5356:                        is_kanji = 0;
                   5357:                } else if(msdos_lead_byte_check(data)) {
                   5358:                        is_kanji = 1;
                   5359:                } else if(msdos_ctrl_code_check(data)) {
                   5360:                        out[q++] = '^';
                   5361:                        c += 'A' - 1;
                   5362:                }
                   5363:                out[q++] = c;
                   5364:        }
1.1.1.34  root     5365:        if(q == 1 && out[0] == 0x08) {
                   5366:                // back space
                   5367:                GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5368:                if(csbi.dwCursorPosition.X > 0) {
                   5369:                        co.X = csbi.dwCursorPosition.X - 1;
                   5370:                        co.Y = csbi.dwCursorPosition.Y;
                   5371:                        SetConsoleCursorPosition(hStdout, co);
                   5372:                } else if(csbi.dwCursorPosition.Y > 0) {
                   5373:                        co.X = csbi.dwSize.X - 1;
                   5374:                        co.Y = csbi.dwCursorPosition.Y - 1;
                   5375:                        SetConsoleCursorPosition(hStdout, co);
                   5376:                } else {
                   5377:                        WriteConsole(hStdout, out, q, &num, NULL); // to make sure
                   5378:                }
                   5379:        } else {
                   5380:                WriteConsole(hStdout, out, q, &num, NULL);
                   5381:        }
1.1       root     5382:        p = 0;
1.1.1.14  root     5383:        
1.1.1.15  root     5384:        if(!restore_console_on_exit) {
                   5385:                GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5386:                scr_top = csbi.srWindow.Top;
                   5387:        }
1.1       root     5388:        cursor_moved = true;
                   5389: }
                   5390: 
                   5391: int msdos_aux_in()
                   5392: {
1.1.1.21  root     5393:        msdos_stdio_reopen();
                   5394:        
1.1.1.20  root     5395:        process_t *process = msdos_process_info_get(current_psp);
                   5396:        int fd = msdos_psp_get_file_table(3, current_psp);
                   5397:        
                   5398:        if(fd < process->max_files && file_handler[fd].valid && !eof(fd)) {
1.1       root     5399:                char data = 0;
1.1.1.37  root     5400:                msdos_read(fd, &data, 1);
1.1       root     5401:                return(data);
                   5402:        } else {
                   5403:                return(EOF);
                   5404:        }
                   5405: }
                   5406: 
                   5407: void msdos_aux_out(char data)
                   5408: {
1.1.1.21  root     5409:        msdos_stdio_reopen();
                   5410:        
1.1.1.20  root     5411:        process_t *process = msdos_process_info_get(current_psp);
                   5412:        int fd = msdos_psp_get_file_table(3, current_psp);
                   5413:        
                   5414:        if(fd < process->max_files && file_handler[fd].valid) {
                   5415:                msdos_write(fd, &data, 1);
1.1       root     5416:        }
                   5417: }
                   5418: 
                   5419: void msdos_prn_out(char data)
                   5420: {
1.1.1.21  root     5421:        msdos_stdio_reopen();
                   5422:        
1.1.1.20  root     5423:        process_t *process = msdos_process_info_get(current_psp);
                   5424:        int fd = msdos_psp_get_file_table(4, current_psp);
                   5425:        
                   5426:        if(fd < process->max_files && file_handler[fd].valid) {
                   5427:                msdos_write(fd, &data, 1);
1.1       root     5428:        }
                   5429: }
                   5430: 
                   5431: // memory control
                   5432: 
1.1.1.45  root     5433: mcb_t *msdos_mcb_create(int mcb_seg, UINT8 mz, UINT16 psp, int paragraphs, const char *prog_name)
1.1       root     5434: {
                   5435:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5436:        
                   5437:        mcb->mz = mz;
                   5438:        mcb->psp = psp;
1.1.1.30  root     5439:        mcb->paragraphs = paragraphs;
1.1.1.39  root     5440:        
                   5441:        if(prog_name != NULL) {
                   5442:                memset(mcb->prog_name, 0, 8);
                   5443:                memcpy(mcb->prog_name, prog_name, min(8, strlen(prog_name)));
                   5444:        }
1.1       root     5445:        return(mcb);
                   5446: }
                   5447: 
1.1.1.39  root     5448: mcb_t *msdos_mcb_create(int mcb_seg, UINT8 mz, UINT16 psp, int paragraphs)
                   5449: {
                   5450:        return(msdos_mcb_create(mcb_seg, mz, psp, paragraphs, NULL));
                   5451: }
                   5452: 
1.1       root     5453: void msdos_mcb_check(mcb_t *mcb)
                   5454: {
                   5455:        if(!(mcb->mz == 'M' || mcb->mz == 'Z')) {
1.1.1.28  root     5456:                #if 0
                   5457:                        // shutdown now !!!
                   5458:                        fatalerror("broken memory control block\n");
                   5459:                #else
                   5460:                        // return error code and continue
                   5461:                        throw(0x07); // broken memory control block
                   5462:                #endif
1.1       root     5463:        }
                   5464: }
                   5465: 
1.1.1.39  root     5466: void msdos_mem_split(int seg, int paragraphs)
1.1       root     5467: {
                   5468:        int mcb_seg = seg - 1;
                   5469:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5470:        msdos_mcb_check(mcb);
                   5471:        
1.1.1.30  root     5472:        if(mcb->paragraphs > paragraphs) {
1.1       root     5473:                int new_seg = mcb_seg + 1 + paragraphs;
1.1.1.30  root     5474:                int new_paragraphs = mcb->paragraphs - paragraphs - 1;
1.1       root     5475:                
                   5476:                msdos_mcb_create(new_seg, mcb->mz, 0, new_paragraphs);
                   5477:                mcb->mz = 'M';
1.1.1.30  root     5478:                mcb->paragraphs = paragraphs;
1.1       root     5479:        }
                   5480: }
                   5481: 
                   5482: void msdos_mem_merge(int seg)
                   5483: {
                   5484:        int mcb_seg = seg - 1;
                   5485:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5486:        msdos_mcb_check(mcb);
                   5487:        
                   5488:        while(1) {
                   5489:                if(mcb->mz == 'Z') {
                   5490:                        break;
                   5491:                }
1.1.1.30  root     5492:                int next_seg = mcb_seg + 1 + mcb->paragraphs;
1.1       root     5493:                mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5494:                msdos_mcb_check(next_mcb);
                   5495:                
                   5496:                if(next_mcb->psp != 0) {
                   5497:                        break;
                   5498:                }
                   5499:                mcb->mz = next_mcb->mz;
1.1.1.30  root     5500:                mcb->paragraphs = mcb->paragraphs + 1 + next_mcb->paragraphs;
1.1       root     5501:        }
                   5502: }
                   5503: 
1.1.1.8   root     5504: int msdos_mem_alloc(int mcb_seg, int paragraphs, int new_process)
1.1       root     5505: {
                   5506:        while(1) {
                   5507:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     5508:                bool last_block;
1.1       root     5509:                
1.1.1.14  root     5510:                if(mcb->psp == 0) {
                   5511:                        msdos_mem_merge(mcb_seg + 1);
                   5512:                } else {
                   5513:                        msdos_mcb_check(mcb);
                   5514:                }
1.1.1.33  root     5515:                if(!(last_block = (mcb->mz == 'Z'))) {
                   5516:                        // check if the next is dummy mcb to link to umb
                   5517:                        int next_seg = mcb_seg + 1 + mcb->paragraphs;
                   5518:                        mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5519:                        last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
                   5520:                }
                   5521:                if(!(new_process && !last_block)) {
1.1.1.30  root     5522:                        if(mcb->psp == 0 && mcb->paragraphs >= paragraphs) {
1.1       root     5523:                                msdos_mem_split(mcb_seg + 1, paragraphs);
                   5524:                                mcb->psp = current_psp;
                   5525:                                return(mcb_seg + 1);
                   5526:                        }
                   5527:                }
                   5528:                if(mcb->mz == 'Z') {
                   5529:                        break;
                   5530:                }
1.1.1.30  root     5531:                mcb_seg += 1 + mcb->paragraphs;
1.1       root     5532:        }
                   5533:        return(-1);
                   5534: }
                   5535: 
                   5536: int msdos_mem_realloc(int seg, int paragraphs, int *max_paragraphs)
                   5537: {
                   5538:        int mcb_seg = seg - 1;
                   5539:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5540:        msdos_mcb_check(mcb);
1.1.1.30  root     5541:        int current_paragraphs = mcb->paragraphs;
1.1       root     5542:        
                   5543:        msdos_mem_merge(seg);
1.1.1.30  root     5544:        if(paragraphs > mcb->paragraphs) {
1.1.1.14  root     5545:                if(max_paragraphs) {
1.1.1.30  root     5546:                        *max_paragraphs = mcb->paragraphs;
1.1.1.14  root     5547:                }
1.1       root     5548:                msdos_mem_split(seg, current_paragraphs);
                   5549:                return(-1);
                   5550:        }
                   5551:        msdos_mem_split(seg, paragraphs);
                   5552:        return(0);
                   5553: }
                   5554: 
                   5555: void msdos_mem_free(int seg)
                   5556: {
                   5557:        int mcb_seg = seg - 1;
                   5558:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5559:        msdos_mcb_check(mcb);
                   5560:        
                   5561:        mcb->psp = 0;
                   5562:        msdos_mem_merge(seg);
                   5563: }
                   5564: 
1.1.1.8   root     5565: int msdos_mem_get_free(int mcb_seg, int new_process)
1.1       root     5566: {
                   5567:        int max_paragraphs = 0;
                   5568:        
                   5569:        while(1) {
                   5570:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     5571:                bool last_block;
                   5572:                
1.1       root     5573:                msdos_mcb_check(mcb);
                   5574:                
1.1.1.33  root     5575:                if(!(last_block = (mcb->mz == 'Z'))) {
                   5576:                        // check if the next is dummy mcb to link to umb
                   5577:                        int next_seg = mcb_seg + 1 + mcb->paragraphs;
                   5578:                        mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5579:                        last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
                   5580:                }
                   5581:                if(!(new_process && !last_block)) {
1.1.1.30  root     5582:                        if(mcb->psp == 0 && mcb->paragraphs > max_paragraphs) {
                   5583:                                max_paragraphs = mcb->paragraphs;
1.1       root     5584:                        }
                   5585:                }
                   5586:                if(mcb->mz == 'Z') {
                   5587:                        break;
                   5588:                }
1.1.1.30  root     5589:                mcb_seg += 1 + mcb->paragraphs;
1.1       root     5590:        }
1.1.1.14  root     5591:        return(max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs);
1.1       root     5592: }
                   5593: 
1.1.1.8   root     5594: int msdos_mem_get_last_mcb(int mcb_seg, UINT16 psp)
                   5595: {
                   5596:        int last_seg = -1;
                   5597:        
                   5598:        while(1) {
                   5599:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5600:                msdos_mcb_check(mcb);
                   5601:                
1.1.1.14  root     5602:                if(mcb->psp == psp) {
1.1.1.8   root     5603:                        last_seg = mcb_seg;
                   5604:                }
1.1.1.14  root     5605:                if(mcb->mz == 'Z') {
                   5606:                        break;
                   5607:                }
1.1.1.30  root     5608:                mcb_seg += 1 + mcb->paragraphs;
1.1.1.8   root     5609:        }
                   5610:        return(last_seg);
                   5611: }
                   5612: 
1.1.1.19  root     5613: int msdos_mem_get_umb_linked()
                   5614: {
1.1.1.33  root     5615:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   5616:        msdos_mcb_check(mcb);
1.1.1.19  root     5617:        
1.1.1.33  root     5618:        if(mcb->mz == 'M') {
                   5619:                return(-1);
1.1.1.19  root     5620:        }
                   5621:        return(0);
                   5622: }
                   5623: 
1.1.1.33  root     5624: void msdos_mem_link_umb()
1.1.1.19  root     5625: {
1.1.1.33  root     5626:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   5627:        msdos_mcb_check(mcb);
1.1.1.19  root     5628:        
1.1.1.33  root     5629:        mcb->mz = 'M';
                   5630:        mcb->paragraphs = (UMB_TOP >> 4) - (MEMORY_END >> 4);
1.1.1.39  root     5631:        
                   5632:        ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked |= 0x01;
1.1.1.19  root     5633: }
                   5634: 
1.1.1.33  root     5635: void msdos_mem_unlink_umb()
1.1.1.19  root     5636: {
1.1.1.33  root     5637:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   5638:        msdos_mcb_check(mcb);
1.1.1.19  root     5639:        
1.1.1.33  root     5640:        mcb->mz = 'Z';
                   5641:        mcb->paragraphs = 0;
1.1.1.39  root     5642:        
                   5643:        ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked &= ~0x01;
1.1.1.19  root     5644: }
                   5645: 
1.1.1.29  root     5646: #ifdef SUPPORT_HMA
                   5647: 
                   5648: hma_mcb_t *msdos_hma_mcb_create(int offset, int owner, int size, int next)
                   5649: {
                   5650:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5651:        
                   5652:        mcb->ms[0] = 'M';
                   5653:        mcb->ms[1] = 'S';
                   5654:        mcb->owner = owner;
                   5655:        mcb->size = size;
                   5656:        mcb->next = next;
                   5657:        return(mcb);
                   5658: }
                   5659: 
                   5660: bool msdos_is_hma_mcb_valid(hma_mcb_t *mcb)
                   5661: {
                   5662:        return(mcb->ms[0] == 'M' && mcb->ms[1] == 'S');
                   5663: }
                   5664: 
                   5665: int msdos_hma_mem_split(int offset, int size)
                   5666: {
                   5667:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5668:        
                   5669:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5670:                return(-1);
                   5671:        }
                   5672:        if(mcb->size >= size + 0x10) {
                   5673:                int new_offset = offset + 0x10 + size;
                   5674:                int new_size = mcb->size - 0x10 - size;
                   5675:                
                   5676:                msdos_hma_mcb_create(new_offset, 0, new_size, mcb->next);
                   5677:                mcb->size = size;
                   5678:                mcb->next = new_offset;
                   5679:                return(0);
                   5680:        }
                   5681:        return(-1);
                   5682: }
                   5683: 
                   5684: void msdos_hma_mem_merge(int offset)
                   5685: {
                   5686:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5687:        
                   5688:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5689:                return;
                   5690:        }
                   5691:        while(1) {
                   5692:                if(mcb->next == 0) {
                   5693:                        break;
                   5694:                }
                   5695:                hma_mcb_t *next_mcb = (hma_mcb_t *)(mem + 0xffff0 + mcb->next);
                   5696:                
                   5697:                if(!msdos_is_hma_mcb_valid(next_mcb)) {
                   5698:                        return;
                   5699:                }
                   5700:                if(next_mcb->owner != 0) {
                   5701:                        break;
                   5702:                }
                   5703:                mcb->size += 0x10 + next_mcb->size;
                   5704:                mcb->next = next_mcb->next;
                   5705:        }
                   5706: }
                   5707: 
                   5708: int msdos_hma_mem_alloc(int size, UINT16 owner)
                   5709: {
                   5710:        int offset = 0x10; // first mcb in HMA
                   5711:        
                   5712:        while(1) {
                   5713:                hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5714:                
                   5715:                if(!msdos_is_hma_mcb_valid(mcb)) {
                   5716:                        return(-1);
                   5717:                }
                   5718:                if(mcb->owner == 0) {
                   5719:                        msdos_hma_mem_merge(offset);
                   5720:                }
                   5721:                if(mcb->owner == 0 && mcb->size >= size) {
                   5722:                        msdos_hma_mem_split(offset, size);
                   5723:                        mcb->owner = owner;
                   5724:                        return(offset);
                   5725:                }
                   5726:                if(mcb->next == 0) {
                   5727:                        break;
                   5728:                }
                   5729:                offset = mcb->next;
                   5730:        }
                   5731:        return(-1);
                   5732: }
                   5733: 
                   5734: int msdos_hma_mem_realloc(int offset, int size)
                   5735: {
                   5736:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5737:        
                   5738:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5739:                return(-1);
                   5740:        }
                   5741:        if(mcb->size < size) {
                   5742:                return(-1);
                   5743:        }
                   5744:        msdos_hma_mem_split(offset, size);
                   5745:        return(0);
                   5746: }
                   5747: 
                   5748: void msdos_hma_mem_free(int offset)
                   5749: {
                   5750:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5751:        
                   5752:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5753:                return;
                   5754:        }
                   5755:        mcb->owner = 0;
                   5756:        msdos_hma_mem_merge(offset);
                   5757: }
                   5758: 
                   5759: int msdos_hma_mem_get_free(int *available_offset)
                   5760: {
                   5761:        int offset = 0x10; // first mcb in HMA
                   5762:        int size = 0;
                   5763:        
                   5764:        while(1) {
                   5765:                hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5766:                
                   5767:                if(!msdos_is_hma_mcb_valid(mcb)) {
                   5768:                        return(0);
                   5769:                }
                   5770:                if(mcb->owner == 0 && size < mcb->size) {
                   5771:                        if(available_offset != NULL) {
                   5772:                                *available_offset = offset;
                   5773:                        }
                   5774:                        size = mcb->size;
                   5775:                }
                   5776:                if(mcb->next == 0) {
                   5777:                        break;
                   5778:                }
                   5779:                offset = mcb->next;
                   5780:        }
                   5781:        return(size);
                   5782: }
                   5783: 
                   5784: #endif
                   5785: 
1.1       root     5786: // environment
                   5787: 
1.1.1.45  root     5788: void msdos_env_set_argv(int env_seg, const char *argv)
1.1       root     5789: {
                   5790:        char *dst = (char *)(mem + (env_seg << 4));
                   5791:        
                   5792:        while(1) {
                   5793:                if(dst[0] == 0) {
                   5794:                        break;
                   5795:                }
                   5796:                dst += strlen(dst) + 1;
                   5797:        }
                   5798:        *dst++ = 0; // end of environment
                   5799:        *dst++ = 1; // top of argv[0]
                   5800:        *dst++ = 0;
                   5801:        memcpy(dst, argv, strlen(argv));
                   5802:        dst += strlen(argv);
                   5803:        *dst++ = 0;
                   5804:        *dst++ = 0;
                   5805: }
                   5806: 
1.1.1.45  root     5807: const char *msdos_env_get_argv(int env_seg)
1.1       root     5808: {
                   5809:        static char env[ENV_SIZE];
                   5810:        char *src = env;
                   5811:        
                   5812:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   5813:        while(1) {
                   5814:                if(src[0] == 0) {
                   5815:                        if(src[1] == 1) {
                   5816:                                return(src + 3);
                   5817:                        }
                   5818:                        break;
                   5819:                }
                   5820:                src += strlen(src) + 1;
                   5821:        }
                   5822:        return(NULL);
                   5823: }
                   5824: 
1.1.1.45  root     5825: const char *msdos_env_get(int env_seg, const char *name)
1.1       root     5826: {
                   5827:        static char env[ENV_SIZE];
                   5828:        char *src = env;
                   5829:        
                   5830:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   5831:        while(1) {
                   5832:                if(src[0] == 0) {
                   5833:                        break;
                   5834:                }
                   5835:                int len = strlen(src);
                   5836:                char *n = my_strtok(src, "=");
                   5837:                char *v = src + strlen(n) + 1;
                   5838:                
                   5839:                if(_stricmp(name, n) == 0) {
                   5840:                        return(v);
                   5841:                }
                   5842:                src += len + 1;
                   5843:        }
                   5844:        return(NULL);
                   5845: }
                   5846: 
1.1.1.45  root     5847: void msdos_env_set(int env_seg, const char *name, const char *value)
1.1       root     5848: {
                   5849:        char env[ENV_SIZE];
                   5850:        char *src = env;
                   5851:        char *dst = (char *)(mem + (env_seg << 4));
1.1.1.45  root     5852:        const char *argv = msdos_env_get_argv(env_seg);
1.1       root     5853:        int done = 0;
                   5854:        
                   5855:        memcpy(src, dst, ENV_SIZE);
                   5856:        memset(dst, 0, ENV_SIZE);
                   5857:        while(1) {
                   5858:                if(src[0] == 0) {
                   5859:                        break;
                   5860:                }
                   5861:                int len = strlen(src);
                   5862:                char *n = my_strtok(src, "=");
                   5863:                char *v = src + strlen(n) + 1;
                   5864:                char tmp[1024];
                   5865:                
                   5866:                if(_stricmp(name, n) == 0) {
                   5867:                        sprintf(tmp, "%s=%s", n, value);
                   5868:                        done = 1;
                   5869:                } else {
                   5870:                        sprintf(tmp, "%s=%s", n, v);
                   5871:                }
                   5872:                memcpy(dst, tmp, strlen(tmp));
                   5873:                dst += strlen(tmp) + 1;
                   5874:                src += len + 1;
                   5875:        }
                   5876:        if(!done) {
                   5877:                char tmp[1024];
                   5878:                
                   5879:                sprintf(tmp, "%s=%s", name, value);
                   5880:                memcpy(dst, tmp, strlen(tmp));
                   5881:                dst += strlen(tmp) + 1;
                   5882:        }
                   5883:        if(argv) {
                   5884:                *dst++ = 0; // end of environment
                   5885:                *dst++ = 1; // top of argv[0]
                   5886:                *dst++ = 0;
                   5887:                memcpy(dst, argv, strlen(argv));
                   5888:                dst += strlen(argv);
                   5889:                *dst++ = 0;
                   5890:                *dst++ = 0;
                   5891:        }
                   5892: }
                   5893: 
                   5894: // process
                   5895: 
1.1.1.8   root     5896: psp_t *msdos_psp_create(int psp_seg, UINT16 mcb_seg, UINT16 parent_psp, UINT16 env_seg)
1.1       root     5897: {
                   5898:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   5899:        
                   5900:        memset(psp, 0, PSP_SIZE);
                   5901:        psp->exit[0] = 0xcd;
                   5902:        psp->exit[1] = 0x20;
1.1.1.8   root     5903:        psp->first_mcb = mcb_seg;
1.1.1.46  root     5904: #if 1
                   5905:        psp->call5[0] = 0xcd;   // int 6fh
                   5906:        psp->call5[1] = 0x6f;
                   5907:        psp->call5[2] = 0xc3;   // ret
                   5908: #else
                   5909:        psp->call5[0] = 0x8a;   // mov ah, cl
                   5910:        psp->call5[1] = 0xe1;
                   5911:        psp->call5[2] = 0xcd;   // int 21h
                   5912:        psp->call5[3] = 0x21;
                   5913:        psp->call5[4] = 0xc3;   // ret
                   5914: #endif
1.1       root     5915:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   5916:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   5917:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   5918:        psp->parent_psp = parent_psp;
1.1.1.20  root     5919:        if(parent_psp == (UINT16)-1) {
                   5920:                for(int i = 0; i < 20; i++) {
                   5921:                        if(file_handler[i].valid) {
                   5922:                                psp->file_table[i] = i;
                   5923:                        } else {
                   5924:                                psp->file_table[i] = 0xff;
                   5925:                        }
1.1       root     5926:                }
1.1.1.20  root     5927:        } else {
                   5928:                memcpy(psp->file_table, ((psp_t *)(mem + (parent_psp << 4)))->file_table, 20);
1.1       root     5929:        }
                   5930:        psp->env_seg = env_seg;
                   5931:        psp->stack.w.l = REG16(SP);
1.1.1.3   root     5932:        psp->stack.w.h = SREG(SS);
1.1.1.14  root     5933:        psp->file_table_size = 20;
                   5934:        psp->file_table_ptr.w.l = 0x18;
                   5935:        psp->file_table_ptr.w.h = psp_seg;
1.1       root     5936:        psp->service[0] = 0xcd;
                   5937:        psp->service[1] = 0x21;
                   5938:        psp->service[2] = 0xcb;
                   5939:        return(psp);
                   5940: }
                   5941: 
1.1.1.20  root     5942: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg)
                   5943: {
                   5944:        if(psp_seg && fd < 20) {
                   5945:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   5946:                psp->file_table[fd] = value;
                   5947:        }
                   5948: }
                   5949: 
                   5950: int msdos_psp_get_file_table(int fd, int psp_seg)
                   5951: {
                   5952:        if(psp_seg && fd < 20) {
                   5953:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   5954:                fd = psp->file_table[fd];
                   5955:        }
                   5956:        return fd;
                   5957: }
                   5958: 
1.1.1.45  root     5959: int msdos_process_exec(const char *cmd, param_block_t *param, UINT8 al)
1.1       root     5960: {
                   5961:        // load command file
                   5962:        int fd = -1;
1.1.1.45  root     5963:        int sio_port = 0;
                   5964:        int lpt_port = 0;
1.1       root     5965:        int dos_command = 0;
1.1.1.24  root     5966:        char command[MAX_PATH], path[MAX_PATH], opt[MAX_PATH], *name = NULL, name_tmp[MAX_PATH];
1.1.1.38  root     5967:        char pipe_stdin_path[MAX_PATH] = {0};
                   5968:        char pipe_stdout_path[MAX_PATH] = {0};
1.1.1.39  root     5969:        char pipe_stderr_path[MAX_PATH] = {0};
1.1       root     5970:        
                   5971:        int opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   5972:        int opt_len = mem[opt_ofs];
                   5973:        memset(opt, 0, sizeof(opt));
                   5974:        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   5975:        
1.1.1.14  root     5976:        if(strlen(cmd) >= 5 && _stricmp(&cmd[strlen(cmd) - 4], ".BAT") == 0) {
                   5977:                // this is a batch file, run command.com
                   5978:                char tmp[MAX_PATH];
                   5979:                if(opt_len != 0) {
                   5980:                        sprintf(tmp, "/C %s %s", cmd, opt);
                   5981:                } else {
                   5982:                        sprintf(tmp, "/C %s", cmd);
                   5983:                }
                   5984:                strcpy(opt, tmp);
                   5985:                opt_len = strlen(opt);
                   5986:                mem[opt_ofs] = opt_len;
                   5987:                sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   5988:                strcpy(command, comspec_path);
                   5989:                strcpy(name_tmp, "COMMAND.COM");
                   5990:        } else {
                   5991:                if(_stricmp(cmd, "C:\\COMMAND.COM") == 0) {
                   5992:                        // redirect C:\COMMAND.COM to comspec_path
                   5993:                        strcpy(command, comspec_path);
                   5994:                } else {
                   5995:                        strcpy(command, cmd);
                   5996:                }
1.1.1.24  root     5997:                if(GetFullPathName(command, MAX_PATH, path, &name) == 0) {
                   5998:                        return(-1);
                   5999:                }
1.1.1.14  root     6000:                memset(name_tmp, 0, sizeof(name_tmp));
                   6001:                strcpy(name_tmp, name);
                   6002:                
                   6003:                // check command.com
1.1.1.38  root     6004:                if((_stricmp(name, "COMMAND.COM") == 0 || _stricmp(name, "COMMAND") == 0) && _access(comspec_path, 0) != 0) {
                   6005:                        // we can not load command.com, so run program directly if "command /c (program)" is specified
1.1.1.14  root     6006:                        if(opt_len == 0) {
                   6007: //                             process_t *current_process = msdos_process_info_get(current_psp);
                   6008:                                process_t *current_process = NULL;
                   6009:                                for(int i = 0; i < MAX_PROCESS; i++) {
                   6010:                                        if(process[i].psp == current_psp) {
                   6011:                                                current_process = &process[i];
                   6012:                                                break;
                   6013:                                        }
                   6014:                                }
                   6015:                                if(current_process != NULL) {
                   6016:                                        param->cmd_line.dw = current_process->dta.dw;
                   6017:                                        opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   6018:                                        opt_len = mem[opt_ofs];
                   6019:                                        memset(opt, 0, sizeof(opt));
                   6020:                                        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   6021:                                }
                   6022:                        }
                   6023:                        for(int i = 0; i < opt_len; i++) {
                   6024:                                if(opt[i] == ' ') {
                   6025:                                        continue;
                   6026:                                }
                   6027:                                if(opt[i] == '/' && (opt[i + 1] == 'c' || opt[i + 1] == 'C') && opt[i + 2] == ' ') {
                   6028:                                        for(int j = i + 3; j < opt_len; j++) {
                   6029:                                                if(opt[j] == ' ') {
                   6030:                                                        continue;
                   6031:                                                }
                   6032:                                                char *token = my_strtok(opt + j, " ");
                   6033:                                                
1.1.1.38  root     6034:                                                strcpy(command, token);
                   6035:                                                char tmp[MAX_PATH];
                   6036:                                                strcpy(tmp, token + strlen(token) + 1);
1.1.1.39  root     6037:                                                strcpy(opt, "");
                   6038:                                                for(int i = 0; i < strlen(tmp); i++) {
                   6039:                                                        if(tmp[i] != ' ') {
                   6040:                                                                strcpy(opt, tmp + i);
                   6041:                                                                break;
                   6042:                                                        }
                   6043:                                                }
                   6044:                                                strcpy(tmp, opt);
1.1.1.38  root     6045:                                                
                   6046:                                                if(al == 0x00) {
1.1.1.39  root     6047:                                                        #define GET_FILE_PATH() { \
                   6048:                                                                if(token[0] != '>' && token[0] != '<') { \
                   6049:                                                                        token++; \
                   6050:                                                                } \
                   6051:                                                                token++; \
                   6052:                                                                while(*token == ' ') { \
                   6053:                                                                        token++; \
                   6054:                                                                } \
                   6055:                                                                char *ptr = token; \
                   6056:                                                                while(*ptr != ' ' && *ptr != '\r' && *ptr != '\0') { \
                   6057:                                                                        ptr++; \
                   6058:                                                                } \
                   6059:                                                                *ptr = '\0'; \
                   6060:                                                        }
                   6061:                                                        if((token = strstr(opt, "0<")) != NULL || (token = strstr(opt, "<")) != NULL) {
                   6062:                                                                GET_FILE_PATH();
1.1.1.38  root     6063:                                                                strcpy(pipe_stdin_path, token);
                   6064:                                                                strcpy(opt, tmp);
                   6065:                                                        }
1.1.1.39  root     6066:                                                        if((token = strstr(opt, "1>")) != NULL || (token = strstr(opt, ">")) != NULL) {
                   6067:                                                                GET_FILE_PATH();
1.1.1.38  root     6068:                                                                strcpy(pipe_stdout_path, token);
                   6069:                                                                strcpy(opt, tmp);
                   6070:                                                        }
1.1.1.39  root     6071:                                                        if((token = strstr(opt, "2>")) != NULL) {
                   6072:                                                                GET_FILE_PATH();
                   6073:                                                                strcpy(pipe_stderr_path, token);
                   6074:                                                                strcpy(opt, tmp);
                   6075:                                                        }
                   6076:                                                        #undef GET_FILE_PATH
                   6077:                                                        
                   6078:                                                        if((token = strstr(opt, "0<")) != NULL) {
                   6079:                                                                *token = '\0';
                   6080:                                                        }
                   6081:                                                        if((token = strstr(opt, "1>")) != NULL) {
                   6082:                                                                *token = '\0';
                   6083:                                                        }
                   6084:                                                        if((token = strstr(opt, "2>")) != NULL) {
                   6085:                                                                *token = '\0';
                   6086:                                                        }
1.1.1.38  root     6087:                                                        if((token = strstr(opt, "<")) != NULL) {
                   6088:                                                                *token = '\0';
                   6089:                                                        }
                   6090:                                                        if((token = strstr(opt, ">")) != NULL) {
                   6091:                                                                *token = '\0';
                   6092:                                                        }
1.1.1.14  root     6093:                                                }
1.1.1.39  root     6094:                                                for(int i = strlen(opt) - 1; i >= 0 && opt[i] == ' '; i--) {
                   6095:                                                        opt[i] = '\0';
                   6096:                                                }
1.1.1.38  root     6097:                                                opt_len = strlen(opt);
                   6098:                                                mem[opt_ofs] = opt_len;
                   6099:                                                sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6100:                                                dos_command = 1;
1.1.1.14  root     6101:                                                break;
1.1       root     6102:                                        }
                   6103:                                }
1.1.1.14  root     6104:                                break;
1.1       root     6105:                        }
                   6106:                }
                   6107:        }
                   6108:        
                   6109:        // load command file
                   6110:        strcpy(path, command);
                   6111:        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   6112:                sprintf(path, "%s.COM", command);
                   6113:                if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   6114:                        sprintf(path, "%s.EXE", command);
                   6115:                        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
1.1.1.14  root     6116:                                sprintf(path, "%s.BAT", command);
                   6117:                                if(_access(path, 0) == 0) {
                   6118:                                        // this is a batch file, run command.com
                   6119:                                        char tmp[MAX_PATH];
                   6120:                                        if(opt_len != 0) {
                   6121:                                                sprintf(tmp, "/C %s %s", path, opt);
                   6122:                                        } else {
                   6123:                                                sprintf(tmp, "/C %s", path);
                   6124:                                        }
                   6125:                                        strcpy(opt, tmp);
                   6126:                                        opt_len = strlen(opt);
                   6127:                                        mem[opt_ofs] = opt_len;
                   6128:                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6129:                                        strcpy(path, comspec_path);
                   6130:                                        strcpy(name_tmp, "COMMAND.COM");
                   6131:                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6132:                                } else {
                   6133:                                        // search path in parent environments
                   6134:                                        psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
1.1.1.45  root     6135:                                        const char *env = msdos_env_get(parent_psp->env_seg, "PATH");
1.1.1.14  root     6136:                                        if(env != NULL) {
                   6137:                                                char env_path[4096];
                   6138:                                                strcpy(env_path, env);
                   6139:                                                char *token = my_strtok(env_path, ";");
                   6140:                                                
                   6141:                                                while(token != NULL) {
                   6142:                                                        if(strlen(token) != 0) {
                   6143:                                                                sprintf(path, "%s", msdos_combine_path(token, command));
                   6144:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6145:                                                                        break;
                   6146:                                                                }
                   6147:                                                                sprintf(path, "%s.COM", msdos_combine_path(token, command));
                   6148:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6149:                                                                        break;
                   6150:                                                                }
                   6151:                                                                sprintf(path, "%s.EXE", msdos_combine_path(token, command));
                   6152:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6153:                                                                        break;
                   6154:                                                                }
                   6155:                                                                sprintf(path, "%s.BAT", msdos_combine_path(token, command));
                   6156:                                                                if(_access(path, 0) == 0) {
                   6157:                                                                        // this is a batch file, run command.com
                   6158:                                                                        char tmp[MAX_PATH];
                   6159:                                                                        if(opt_len != 0) {
                   6160:                                                                                sprintf(tmp, "/C %s %s", path, opt);
                   6161:                                                                        } else {
                   6162:                                                                                sprintf(tmp, "/C %s", path);
                   6163:                                                                        }
                   6164:                                                                        strcpy(opt, tmp);
                   6165:                                                                        opt_len = strlen(opt);
                   6166:                                                                        mem[opt_ofs] = opt_len;
                   6167:                                                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6168:                                                                        strcpy(path, comspec_path);
                   6169:                                                                        strcpy(name_tmp, "COMMAND.COM");
                   6170:                                                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6171:                                                                        break;
                   6172:                                                                }
1.1.1.8   root     6173:                                                        }
1.1.1.14  root     6174:                                                        token = my_strtok(NULL, ";");
1.1       root     6175:                                                }
                   6176:                                        }
                   6177:                                }
                   6178:                        }
                   6179:                }
                   6180:        }
                   6181:        if(fd == -1) {
1.1.1.38  root     6182:                // we can not find command.com in the path, so open comspec_path
                   6183:                if(_stricmp(command, "COMMAND.COM") == 0 || _stricmp(command, "COMMAND") == 0) {
                   6184:                        strcpy(command, comspec_path);
                   6185:                        strcpy(path, command);
                   6186:                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6187:                }
                   6188:        }
                   6189:        if(fd == -1) {
1.1       root     6190:                if(dos_command) {
                   6191:                        // may be dos command
                   6192:                        char tmp[MAX_PATH];
                   6193:                        sprintf(tmp, "%s %s", command, opt);
                   6194:                        system(tmp);
                   6195:                        return(0);
                   6196:                } else {
                   6197:                        return(-1);
                   6198:                }
                   6199:        }
                   6200:        _read(fd, file_buffer, sizeof(file_buffer));
                   6201:        _close(fd);
                   6202:        
                   6203:        // copy environment
1.1.1.29  root     6204:        int umb_linked, env_seg, psp_seg;
1.1       root     6205:        
1.1.1.29  root     6206:        if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   6207:                msdos_mem_unlink_umb();
                   6208:        }
1.1.1.8   root     6209:        if((env_seg = msdos_mem_alloc(first_mcb, ENV_SIZE >> 4, 1)) == -1) {
1.1.1.29  root     6210:                if((env_seg = msdos_mem_alloc(UMB_TOP >> 4, ENV_SIZE >> 4, 1)) == -1) {
                   6211:                        if(umb_linked != 0) {
                   6212:                                msdos_mem_link_umb();
                   6213:                        }
                   6214:                        return(-1);
                   6215:                }
1.1       root     6216:        }
                   6217:        if(param->env_seg == 0) {
                   6218:                psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
                   6219:                memcpy(mem + (env_seg << 4), mem + (parent_psp->env_seg << 4), ENV_SIZE);
                   6220:        } else {
                   6221:                memcpy(mem + (env_seg << 4), mem + (param->env_seg << 4), ENV_SIZE);
                   6222:        }
                   6223:        msdos_env_set_argv(env_seg, msdos_short_full_path(path));
                   6224:        
                   6225:        // check exe header
                   6226:        exe_header_t *header = (exe_header_t *)file_buffer;
1.1.1.8   root     6227:        int paragraphs, free_paragraphs = msdos_mem_get_free(first_mcb, 1);
1.1       root     6228:        UINT16 cs, ss, ip, sp;
                   6229:        
                   6230:        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   6231:                // memory allocation
                   6232:                int header_size = header->header_size * 16;
                   6233:                int load_size = header->pages * 512 - header_size;
                   6234:                if(header_size + load_size < 512) {
                   6235:                        load_size = 512 - header_size;
                   6236:                }
                   6237:                paragraphs = (PSP_SIZE + load_size) >> 4;
                   6238:                if(paragraphs + header->min_alloc > free_paragraphs) {
                   6239:                        msdos_mem_free(env_seg);
                   6240:                        return(-1);
                   6241:                }
                   6242:                paragraphs += header->max_alloc ? header->max_alloc : header->min_alloc;
                   6243:                if(paragraphs > free_paragraphs) {
                   6244:                        paragraphs = free_paragraphs;
                   6245:                }
1.1.1.8   root     6246:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29  root     6247:                        if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
                   6248:                                if(umb_linked != 0) {
                   6249:                                        msdos_mem_link_umb();
                   6250:                                }
                   6251:                                msdos_mem_free(env_seg);
                   6252:                                return(-1);
                   6253:                        }
1.1       root     6254:                }
                   6255:                // relocation
                   6256:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   6257:                for(int i = 0; i < header->relocations; i++) {
                   6258:                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   6259:                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   6260:                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   6261:                }
                   6262:                memcpy(mem + (start_seg << 4), file_buffer + header_size, load_size);
                   6263:                // segments
                   6264:                cs = header->init_cs + start_seg;
                   6265:                ss = header->init_ss + start_seg;
                   6266:                ip = header->init_ip;
                   6267:                sp = header->init_sp - 2; // for symdeb
                   6268:        } else {
                   6269:                // memory allocation
                   6270:                paragraphs = free_paragraphs;
1.1.1.8   root     6271:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29  root     6272:                        if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
                   6273:                                if(umb_linked != 0) {
                   6274:                                        msdos_mem_link_umb();
                   6275:                                }
                   6276:                                msdos_mem_free(env_seg);
                   6277:                                return(-1);
                   6278:                        }
1.1       root     6279:                }
                   6280:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   6281:                memcpy(mem + (start_seg << 4), file_buffer, 0x10000 - PSP_SIZE);
                   6282:                // segments
                   6283:                cs = ss = psp_seg;
                   6284:                ip = 0x100;
                   6285:                sp = 0xfffe;
                   6286:        }
1.1.1.29  root     6287:        if(umb_linked != 0) {
                   6288:                msdos_mem_link_umb();
                   6289:        }
1.1       root     6290:        
                   6291:        // create psp
1.1.1.3   root     6292:        *(UINT16 *)(mem + 4 * 0x22 + 0) = m_eip;
                   6293:        *(UINT16 *)(mem + 4 * 0x22 + 2) = SREG(CS);
1.1       root     6294:        psp_t *psp = msdos_psp_create(psp_seg, psp_seg + paragraphs, current_psp, env_seg);
                   6295:        memcpy(psp->fcb1, mem + (param->fcb1.w.h << 4) + param->fcb1.w.l, sizeof(psp->fcb1));
                   6296:        memcpy(psp->fcb2, mem + (param->fcb2.w.h << 4) + param->fcb2.w.l, sizeof(psp->fcb2));
                   6297:        memcpy(psp->buffer, mem + (param->cmd_line.w.h << 4) + param->cmd_line.w.l, sizeof(psp->buffer));
                   6298:        
                   6299:        mcb_t *mcb_env = (mcb_t *)(mem + ((env_seg - 1) << 4));
                   6300:        mcb_t *mcb_psp = (mcb_t *)(mem + ((psp_seg - 1) << 4));
                   6301:        mcb_psp->psp = mcb_env->psp = psp_seg;
                   6302:        
1.1.1.4   root     6303:        for(int i = 0; i < 8; i++) {
                   6304:                if(name_tmp[i] == '.') {
                   6305:                        mcb_psp->prog_name[i] = '\0';
                   6306:                        break;
                   6307:                } else if(i < 7 && msdos_lead_byte_check(name_tmp[i])) {
                   6308:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6309:                        i++;
                   6310:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6311:                } else if(name_tmp[i] >= 'a' && name_tmp[i] <= 'z') {
                   6312:                        mcb_psp->prog_name[i] = name_tmp[i] - 'a' + 'A';
                   6313:                } else {
                   6314:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6315:                }
                   6316:        }
                   6317:        
1.1       root     6318:        // process info
                   6319:        process_t *process = msdos_process_info_create(psp_seg);
                   6320:        strcpy(process->module_dir, msdos_short_full_dir(path));
1.1.1.33  root     6321: #ifdef USE_DEBUGGER
                   6322:        strcpy(process->module_path, path);
                   6323: #endif
1.1       root     6324:        process->dta.w.l = 0x80;
                   6325:        process->dta.w.h = psp_seg;
                   6326:        process->switchar = '/';
                   6327:        process->max_files = 20;
                   6328:        process->parent_int_10h_feh_called = int_10h_feh_called;
                   6329:        process->parent_int_10h_ffh_called = int_10h_ffh_called;
1.1.1.14  root     6330:        process->parent_ds = SREG(DS);
1.1.1.31  root     6331:        process->parent_es = SREG(ES);
1.1       root     6332:        
                   6333:        current_psp = psp_seg;
1.1.1.23  root     6334:        msdos_sda_update(current_psp);
1.1       root     6335:        
                   6336:        if(al == 0x00) {
                   6337:                int_10h_feh_called = int_10h_ffh_called = false;
                   6338:                
1.1.1.38  root     6339:                // pipe
                   6340:                if(pipe_stdin_path[0] != '\0') {
1.1.1.45  root     6341: //                     if((fd = _open(pipe_stdin_path, _O_RDONLY | _O_BINARY)) != -1) {
                   6342:                        if(msdos_is_device_path(pipe_stdin_path)) {
                   6343:                                fd = msdos_open_device(pipe_stdin_path, _O_RDONLY | _O_BINARY, &sio_port, &lpt_port);
                   6344:                        } else {
                   6345:                                fd = _open(pipe_stdin_path, _O_RDONLY | _O_BINARY);
                   6346:                        }
                   6347:                        if(fd != -1) {
                   6348:                                msdos_file_handler_open(fd, pipe_stdin_path, _isatty(fd), 0, msdos_device_info(pipe_stdin_path), current_psp, sio_port, lpt_port);
1.1.1.38  root     6349:                                psp->file_table[0] = fd;
                   6350:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6351:                        }
                   6352:                }
                   6353:                if(pipe_stdout_path[0] != '\0') {
                   6354:                        if(_access(pipe_stdout_path, 0) == 0) {
                   6355:                                SetFileAttributes(pipe_stdout_path, FILE_ATTRIBUTE_NORMAL);
                   6356:                                DeleteFile(pipe_stdout_path);
                   6357:                        }
1.1.1.45  root     6358: //                     if((fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE)) != -1) {
                   6359:                        if(msdos_is_device_path(pipe_stdout_path)) {
                   6360:                                fd = msdos_open_device(pipe_stdout_path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
                   6361:                        } else {
                   6362:                                fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   6363:                        }
                   6364:                        if(fd != -1) {
                   6365:                                msdos_file_handler_open(fd, pipe_stdout_path, _isatty(fd), 1, msdos_device_info(pipe_stdout_path), current_psp, sio_port, lpt_port);
1.1.1.38  root     6366:                                psp->file_table[1] = fd;
                   6367:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6368:                        }
                   6369:                }
1.1.1.39  root     6370:                if(pipe_stderr_path[0] != '\0') {
                   6371:                        if(_access(pipe_stderr_path, 0) == 0) {
                   6372:                                SetFileAttributes(pipe_stderr_path, FILE_ATTRIBUTE_NORMAL);
                   6373:                                DeleteFile(pipe_stderr_path);
                   6374:                        }
1.1.1.45  root     6375: //                     if((fd = _open(pipe_stderr_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE)) != -1) {
                   6376:                        if(msdos_is_device_path(pipe_stderr_path)) {
                   6377:                                fd = msdos_open_device(pipe_stderr_path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
                   6378:                        } else {
                   6379:                                fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   6380:                        }
                   6381:                        if(fd != -1) {
                   6382:                                msdos_file_handler_open(fd, pipe_stderr_path, _isatty(fd), 1, msdos_device_info(pipe_stderr_path), current_psp, sio_port, lpt_port);
1.1.1.39  root     6383:                                psp->file_table[2] = fd;
                   6384:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6385:                        }
                   6386:                }
1.1.1.38  root     6387:                
1.1       root     6388:                // registers and segments
                   6389:                REG16(AX) = REG16(BX) = 0x00;
                   6390:                REG16(CX) = 0xff;
                   6391:                REG16(DX) = psp_seg;
                   6392:                REG16(SI) = ip;
                   6393:                REG16(DI) = sp;
                   6394:                REG16(SP) = sp;
1.1.1.3   root     6395:                SREG(DS) = SREG(ES) = psp_seg;
                   6396:                SREG(SS) = ss;
                   6397:                i386_load_segment_descriptor(DS);
                   6398:                i386_load_segment_descriptor(ES);
                   6399:                i386_load_segment_descriptor(SS);
1.1       root     6400:                
                   6401:                *(UINT16 *)(mem + (ss << 4) + sp) = 0;
                   6402:                i386_jmp_far(cs, ip);
                   6403:        } else if(al == 0x01) {
                   6404:                // copy ss:sp and cs:ip to param block
                   6405:                param->sp = sp;
                   6406:                param->ss = ss;
                   6407:                param->ip = ip;
                   6408:                param->cs = cs;
1.1.1.31  root     6409:                
                   6410:                // the AX value to be passed to the child program is put on top of the child's stack
                   6411:                *(UINT16 *)(mem + (ss << 4) + sp) = REG16(AX);
1.1       root     6412:        }
                   6413:        return(0);
                   6414: }
                   6415: 
                   6416: void msdos_process_terminate(int psp_seg, int ret, int mem_free)
                   6417: {
                   6418:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6419:        
                   6420:        *(UINT32 *)(mem + 4 * 0x22) = psp->int_22h.dw;
                   6421:        *(UINT32 *)(mem + 4 * 0x23) = psp->int_23h.dw;
                   6422:        *(UINT32 *)(mem + 4 * 0x24) = psp->int_24h.dw;
                   6423:        
1.1.1.3   root     6424:        SREG(SS) = psp->stack.w.h;
                   6425:        i386_load_segment_descriptor(SS);
1.1       root     6426:        REG16(SP) = psp->stack.w.l;
                   6427:        i386_jmp_far(psp->int_22h.w.h, psp->int_22h.w.l);
                   6428:        
1.1.1.28  root     6429: //     process_t *current_process = msdos_process_info_get(psp_seg);
                   6430:        process_t *current_process = NULL;
                   6431:        for(int i = 0; i < MAX_PROCESS; i++) {
                   6432:                if(process[i].psp == psp_seg) {
                   6433:                        current_process = &process[i];
                   6434:                        break;
                   6435:                }
                   6436:        }
                   6437:        if(current_process == NULL) {
                   6438:                throw(0x1f); // general failure
                   6439:        }
                   6440:        int_10h_feh_called = current_process->parent_int_10h_feh_called;
                   6441:        int_10h_ffh_called = current_process->parent_int_10h_ffh_called;
                   6442:        if(current_process->called_by_int2eh) {
                   6443:                REG16(AX) = ret;
                   6444:        }
                   6445:        SREG(DS) = current_process->parent_ds;
1.1.1.31  root     6446:        SREG(ES) = current_process->parent_es;
1.1.1.14  root     6447:        i386_load_segment_descriptor(DS);
1.1.1.31  root     6448:        i386_load_segment_descriptor(ES);
1.1       root     6449:        
                   6450:        if(mem_free) {
1.1.1.8   root     6451:                int mcb_seg;
                   6452:                while((mcb_seg = msdos_mem_get_last_mcb(first_mcb, psp_seg)) != -1) {
                   6453:                        msdos_mem_free(mcb_seg + 1);
                   6454:                }
                   6455:                while((mcb_seg = msdos_mem_get_last_mcb(UMB_TOP >> 4, psp_seg)) != -1) {
                   6456:                        msdos_mem_free(mcb_seg + 1);
                   6457:                }
1.1       root     6458:                
                   6459:                for(int i = 0; i < MAX_FILES; i++) {
                   6460:                        if(file_handler[i].valid && file_handler[i].psp == psp_seg) {
                   6461:                                _close(i);
1.1.1.20  root     6462:                                msdos_file_handler_close(i);
                   6463:                                msdos_psp_set_file_table(i, 0x0ff, psp_seg); // FIXME: consider duplicated file handles
1.1       root     6464:                        }
                   6465:                }
1.1.1.13  root     6466:                msdos_dta_info_free(psp_seg);
1.1       root     6467:        }
1.1.1.14  root     6468:        msdos_stdio_reopen();
1.1       root     6469:        
1.1.1.28  root     6470:        memset(current_process, 0, sizeof(process_t));
1.1       root     6471:        
                   6472:        current_psp = psp->parent_psp;
                   6473:        retval = ret;
1.1.1.23  root     6474:        msdos_sda_update(current_psp);
1.1       root     6475: }
                   6476: 
                   6477: // drive
                   6478: 
1.1.1.42  root     6479: int pcbios_update_drive_param(int drive_num, int force_update);
                   6480: 
1.1       root     6481: int msdos_drive_param_block_update(int drive_num, UINT16 *seg, UINT16 *ofs, int force_update)
                   6482: {
1.1.1.41  root     6483:        if(!(drive_num >= 0 && drive_num < 26)) {
                   6484:                return(0);
                   6485:        }
1.1.1.42  root     6486:        pcbios_update_drive_param(drive_num, force_update);
                   6487:        
                   6488:        drive_param_t *drive_param = &drive_params[drive_num];
1.1       root     6489:        *seg = DPB_TOP >> 4;
                   6490:        *ofs = sizeof(dpb_t) * drive_num;
                   6491:        dpb_t *dpb = (dpb_t *)(mem + (*seg << 4) + *ofs);
                   6492:        
                   6493:        memset(dpb, 0, sizeof(dpb_t));
                   6494:        
1.1.1.41  root     6495:        dpb->drive_num = drive_num;
                   6496:        dpb->unit_num = drive_num;
1.1.1.42  root     6497:        
                   6498:        if(drive_param->valid) {
                   6499:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   6500:                
                   6501:                dpb->bytes_per_sector = (UINT16)geo->BytesPerSector;
                   6502:                dpb->highest_sector_num = (UINT8)(geo->SectorsPerTrack - 1);
                   6503:                dpb->highest_cluster_num = (UINT16)(geo->TracksPerCylinder * geo->Cylinders.QuadPart + 1);
                   6504:                dpb->maximum_cluster_num = (UINT32)(geo->TracksPerCylinder * geo->Cylinders.QuadPart + 1);
                   6505:                switch(geo->MediaType) {
                   6506:                case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   6507:                        dpb->media_type = 0xff;
                   6508:                        break;
                   6509:                case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   6510:                        dpb->media_type = 0xfe;
                   6511:                        break;
                   6512:                case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   6513:                        dpb->media_type = 0xfd;
                   6514:                        break;
                   6515:                case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   6516:                        dpb->media_type = 0xfc;
                   6517:                        break;
                   6518:                case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   6519:                case F3_1Pt2_512:
                   6520:                case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   6521:                case F5_720_512:
                   6522:                        dpb->media_type = 0xf9;
                   6523:                        break;
                   6524:                case FixedMedia:        // hard disk
                   6525:                case RemovableMedia:
                   6526:                case Unknown:
                   6527:                        dpb->media_type = 0xf8;
                   6528:                        break;
                   6529:                default:
                   6530:                        dpb->media_type = 0xf0;
                   6531:                        break;
                   6532:                }
                   6533:        }
1.1.1.41  root     6534:        dpb->next_dpb_ofs = (drive_num == 25) ? 0xffff : *ofs + sizeof(dpb_t);
                   6535:        dpb->next_dpb_seg = (drive_num == 25) ? 0xffff : *seg;
1.1.1.42  root     6536:        dpb->info_sector = 0xffff;
                   6537:        dpb->backup_boot_sector = 0xffff;
                   6538:        dpb->free_clusters = 0xffff;
                   6539:        dpb->free_search_cluster = 0xffffffff;
                   6540:        
                   6541:        return(drive_param->valid);
1.1       root     6542: }
                   6543: 
                   6544: // pc bios
                   6545: 
1.1.1.35  root     6546: #ifdef USE_SERVICE_THREAD
                   6547: void start_service_loop(LPTHREAD_START_ROUTINE lpStartAddress)
                   6548: {
                   6549: #if defined(HAS_I386)
                   6550:        if(m_SF != 0) {
                   6551:                m_SF = 0;
                   6552:                mem[0xfffd0 + 0x15] = 0x79;     // jns -4
                   6553:        } else {
                   6554:                m_SF = 1;
                   6555:                mem[0xfffd0 + 0x15] = 0x78;     // js -4
                   6556:        }
                   6557: #else
                   6558:        if(m_SignVal < 0) {
                   6559:                m_SignVal = 0;
                   6560:                mem[0xfffd0 + 0x15] = 0x79;     // jns -4
                   6561:        } else {
                   6562:                m_SignVal = -1;
                   6563:                mem[0xfffd0 + 0x15] = 0x78;     // js -4
                   6564:        }
                   6565: #endif
                   6566:        i386_call_far(0xfffd, 0x0013);
                   6567:        in_service = true;
                   6568:        service_exit = false;
                   6569:        CloseHandle(CreateThread(NULL, 0, lpStartAddress, NULL, 0, NULL));
                   6570: }
                   6571: 
                   6572: void finish_service_loop()
                   6573: {
                   6574:        if(in_service && service_exit) {
                   6575: #if defined(HAS_I386)
                   6576:                if(m_SF != 0) {
                   6577:                        m_SF = 0;
                   6578:                } else {
                   6579:                        m_SF = 1;
                   6580:                }
                   6581: #else
                   6582:                if(m_SignVal < 0) {
                   6583:                        m_SignVal = 0;
                   6584:                } else {
                   6585:                        m_SignVal = -1;
                   6586:                }
                   6587: #endif
                   6588:                in_service = false;
                   6589:        }
                   6590: }
                   6591: #endif
                   6592: 
1.1.1.19  root     6593: UINT32 get_ticks_since_midnight(UINT32 cur_msec)
                   6594: {
                   6595:        static unsigned __int64 start_msec_since_midnight = 0;
                   6596:        static unsigned __int64 start_msec_since_hostboot = 0;
                   6597:        
                   6598:        if(start_msec_since_midnight == 0) {
                   6599:                SYSTEMTIME time;
                   6600:                GetLocalTime(&time);
                   6601:                start_msec_since_midnight = ((time.wHour * 60 + time.wMinute) * 60 + time.wSecond) * 1000 + time.wMilliseconds;
                   6602:                start_msec_since_hostboot = cur_msec;
                   6603:        }
                   6604:        unsigned __int64 msec = (start_msec_since_midnight + cur_msec - start_msec_since_hostboot) % (24 * 60 * 60 * 1000);
                   6605:        unsigned __int64 tick = msec * 0x1800b0 / (24 * 60 * 60 * 1000);
                   6606:        return (UINT32)tick;
                   6607: }
                   6608: 
                   6609: void pcbios_update_daily_timer_counter(UINT32 cur_msec)
                   6610: {
                   6611:        UINT32 prev_tick = *(UINT32 *)(mem + 0x46c);
                   6612:        UINT32 next_tick = get_ticks_since_midnight(cur_msec);
                   6613:        
                   6614:        if(prev_tick > next_tick) {
                   6615:                mem[0x470] = 1;
                   6616:        }
                   6617:        *(UINT32 *)(mem + 0x46c) = next_tick;
                   6618: }
                   6619: 
1.1.1.14  root     6620: inline void pcbios_irq0()
                   6621: {
                   6622:        //++*(UINT32 *)(mem + 0x46c);
1.1.1.19  root     6623:        pcbios_update_daily_timer_counter(timeGetTime());
1.1.1.14  root     6624: }
                   6625: 
1.1.1.16  root     6626: int pcbios_get_text_vram_address(int page)
1.1       root     6627: {
                   6628:        if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     6629:                return TEXT_VRAM_TOP;
1.1       root     6630:        } else {
1.1.1.14  root     6631:                return TEXT_VRAM_TOP + VIDEO_REGEN * (page % vram_pages);
1.1       root     6632:        }
                   6633: }
                   6634: 
1.1.1.16  root     6635: int pcbios_get_shadow_buffer_address(int page)
1.1       root     6636: {
1.1.1.14  root     6637:        if(!int_10h_feh_called) {
1.1.1.16  root     6638:                return pcbios_get_text_vram_address(page);
1.1.1.14  root     6639:        } else if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     6640:                return SHADOW_BUF_TOP;
                   6641:        } else {
1.1.1.14  root     6642:                return SHADOW_BUF_TOP + VIDEO_REGEN * (page % vram_pages);
1.1.1.8   root     6643:        }
                   6644: }
                   6645: 
1.1.1.16  root     6646: int pcbios_get_shadow_buffer_address(int page, int x, int y)
1.1.1.8   root     6647: {
1.1.1.16  root     6648:        return pcbios_get_shadow_buffer_address(page) + (x + y * scr_width) * 2;
1.1       root     6649: }
                   6650: 
1.1.1.16  root     6651: void pcbios_set_console_size(int width, int height, bool clr_screen)
1.1       root     6652: {
1.1.1.14  root     6653:        // clear the existing screen, not just the new one
                   6654:        int clr_height = max(height, scr_height);
                   6655:        
1.1.1.16  root     6656:        if(scr_width != width || scr_height != height) {
                   6657:                change_console_size(width, height);
1.1.1.14  root     6658:        }
                   6659:        mem[0x462] = 0;
                   6660:        *(UINT16 *)(mem + 0x44e) = 0;
                   6661:        
1.1.1.16  root     6662:        text_vram_top_address = pcbios_get_text_vram_address(0);
                   6663:        text_vram_end_address = text_vram_top_address + width * height * 2;
                   6664:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(0);
                   6665:        shadow_buffer_end_address = shadow_buffer_top_address + width * height * 2;
1.1       root     6666:        
1.1.1.23  root     6667:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.16  root     6668:        if(clr_screen) {
1.1.1.14  root     6669:                for(int ofs = shadow_buffer_top_address; ofs < shadow_buffer_end_address;) {
                   6670:                        mem[ofs++] = 0x20;
                   6671:                        mem[ofs++] = 0x07;
                   6672:                }
                   6673:                
1.1.1.35  root     6674: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6675:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     6676: #endif
1.1.1.14  root     6677:                for(int y = 0; y < clr_height; y++) {
                   6678:                        for(int x = 0; x < scr_width; x++) {
                   6679:                                SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6680:                                SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1       root     6681:                        }
                   6682:                }
                   6683:                SMALL_RECT rect;
1.1.1.14  root     6684:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + clr_height - 1);
                   6685:                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   6686:                vram_length_char = vram_last_length_char = 0;
                   6687:                vram_length_attr = vram_last_length_attr = 0;
1.1.1.35  root     6688: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6689:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     6690: #endif
1.1       root     6691:        }
1.1.1.14  root     6692:        COORD co;
                   6693:        co.X = 0;
                   6694:        co.Y = scr_top;
                   6695:        SetConsoleCursorPosition(hStdout, co);
                   6696:        cursor_moved = true;
                   6697:        SetConsoleTextAttribute(hStdout, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
1.1       root     6698: }
                   6699: 
1.1.1.36  root     6700: void pcbios_update_cursor_position()
                   6701: {
                   6702:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   6703:        GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
                   6704:        if(!restore_console_on_exit) {
                   6705:                scr_top = csbi.srWindow.Top;
                   6706:        }
                   6707:        mem[0x450 + mem[0x462] * 2] = csbi.dwCursorPosition.X;
                   6708:        mem[0x451 + mem[0x462] * 2] = csbi.dwCursorPosition.Y - scr_top;
                   6709: }
                   6710: 
1.1.1.16  root     6711: inline void pcbios_int_10h_00h()
                   6712: {
                   6713:        switch(REG8(AL) & 0x7f) {
                   6714:        case 0x70: // v-text mode
                   6715:        case 0x71: // extended cga v-text mode
                   6716:                pcbios_set_console_size(scr_width, scr_height, !(REG8(AL) & 0x80));
                   6717:                break;
                   6718:        default:
                   6719:                pcbios_set_console_size(80, 25, !(REG8(AL) & 0x80));
                   6720:                break;
                   6721:        }
                   6722:        if(REG8(AL) & 0x80) {
                   6723:                mem[0x487] |= 0x80;
                   6724:        } else {
                   6725:                mem[0x487] &= ~0x80;
                   6726:        }
                   6727:        mem[0x449] = REG8(AL) & 0x7f;
                   6728: }
                   6729: 
1.1       root     6730: inline void pcbios_int_10h_01h()
                   6731: {
1.1.1.13  root     6732:        mem[0x460] = REG8(CL);
                   6733:        mem[0x461] = REG8(CH);
1.1.1.14  root     6734:        
1.1.1.23  root     6735:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6736:        CONSOLE_CURSOR_INFO ci;
                   6737:        GetConsoleCursorInfo(hStdout, &ci);
                   6738: //     ci.bVisible = !(REG8(CH) & 0x60) && REG8(CH) <= REG8(CL);
                   6739: //     if(ci.bVisible) {
                   6740:                int lines = max(8, REG8(CL) + 1);
                   6741:                ci.dwSize = (REG8(CL) - REG8(CH) + 1) * 100 / lines;
                   6742: //     }
                   6743:        SetConsoleCursorInfo(hStdout, &ci);
1.1       root     6744: }
                   6745: 
                   6746: inline void pcbios_int_10h_02h()
                   6747: {
1.1.1.14  root     6748:        // continuously setting the cursor effectively stops it blinking
                   6749:        if(mem[0x462] == REG8(BH) && (REG8(DL) != mem[0x450 + REG8(BH) * 2] || REG8(DH) != mem[0x451 + REG8(BH) * 2])) {
1.1       root     6750:                COORD co;
                   6751:                co.X = REG8(DL);
1.1.1.14  root     6752:                co.Y = REG8(DH) + scr_top;
                   6753:                
                   6754:                // some programs hide the cursor by moving it off screen
                   6755:                static bool hidden = false;
1.1.1.23  root     6756:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6757:                CONSOLE_CURSOR_INFO ci;
                   6758:                GetConsoleCursorInfo(hStdout, &ci);
                   6759:                
                   6760:                if(REG8(DH) >= scr_height || !SetConsoleCursorPosition(hStdout, co)) {
                   6761:                        if(ci.bVisible) {
                   6762:                                ci.bVisible = FALSE;
                   6763: //                             SetConsoleCursorInfo(hStdout, &ci);
                   6764:                                hidden = true;
                   6765:                        }
                   6766:                } else if(hidden) {
                   6767:                        if(!ci.bVisible) {
                   6768:                                ci.bVisible = TRUE;
                   6769: //                             SetConsoleCursorInfo(hStdout, &ci);
                   6770:                        }
                   6771:                        hidden = false;
                   6772:                }
1.1       root     6773:        }
1.1.1.14  root     6774:        mem[0x450 + (REG8(BH) % vram_pages) * 2] = REG8(DL);
                   6775:        mem[0x451 + (REG8(BH) % vram_pages) * 2] = REG8(DH);
1.1       root     6776: }
                   6777: 
                   6778: inline void pcbios_int_10h_03h()
                   6779: {
1.1.1.14  root     6780:        REG8(DL) = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   6781:        REG8(DH) = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     6782:        REG8(CL) = mem[0x460];
                   6783:        REG8(CH) = mem[0x461];
                   6784: }
                   6785: 
                   6786: inline void pcbios_int_10h_05h()
                   6787: {
1.1.1.14  root     6788:        if(REG8(AL) >= vram_pages) {
                   6789:                return;
                   6790:        }
                   6791:        if(mem[0x462] != REG8(AL)) {
                   6792:                vram_flush();
                   6793:                
1.1.1.23  root     6794:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6795:                SMALL_RECT rect;
1.1.1.14  root     6796:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   6797:                ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     6798:                
1.1.1.16  root     6799:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(mem[0x462]); y < scr_height; y++) {
1.1.1.14  root     6800:                        for(int x = 0; x < scr_width; x++) {
                   6801:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   6802:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     6803:                        }
                   6804:                }
1.1.1.16  root     6805:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(REG8(AL)); y < scr_height; y++) {
1.1.1.14  root     6806:                        for(int x = 0; x < scr_width; x++) {
                   6807:                                SCR_BUF(y,x).Char.AsciiChar = mem[ofs++];
                   6808:                                SCR_BUF(y,x).Attributes = mem[ofs++];
1.1       root     6809:                        }
                   6810:                }
1.1.1.14  root     6811:                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     6812:                
                   6813:                COORD co;
1.1.1.14  root     6814:                co.X = mem[0x450 + REG8(AL) * 2];
                   6815:                co.Y = mem[0x451 + REG8(AL) * 2] + scr_top;
                   6816:                if(co.Y < scr_top + scr_height) {
                   6817:                        SetConsoleCursorPosition(hStdout, co);
                   6818:                }
1.1       root     6819:        }
1.1.1.14  root     6820:        mem[0x462] = REG8(AL);
                   6821:        *(UINT16 *)(mem + 0x44e) = REG8(AL) * VIDEO_REGEN;
                   6822:        int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.16  root     6823:        text_vram_top_address = pcbios_get_text_vram_address(mem[0x462]);
1.1.1.14  root     6824:        text_vram_end_address = text_vram_top_address + regen;
1.1.1.16  root     6825:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(mem[0x462]);
1.1.1.14  root     6826:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
1.1       root     6827: }
                   6828: 
                   6829: inline void pcbios_int_10h_06h()
                   6830: {
1.1.1.14  root     6831:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   6832:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   6833:                return;
                   6834:        }
                   6835:        vram_flush();
                   6836:        
1.1.1.23  root     6837:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6838:        SMALL_RECT rect;
1.1.1.14  root     6839:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   6840:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   6841:        
                   6842:        int right = min(REG8(DL), scr_width - 1);
                   6843:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     6844:        
                   6845:        if(REG8(AL) == 0) {
1.1.1.14  root     6846:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     6847:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     6848:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6849:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     6850:                        }
                   6851:                }
                   6852:        } else {
1.1.1.14  root     6853:                for(int y = REG8(CH), y2 = min(REG8(CH) + REG8(AL), bottom + 1); y <= bottom; y++, y2++) {
1.1.1.16  root     6854:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     6855:                                if(y2 <= bottom) {
                   6856:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     6857:                                } else {
1.1.1.14  root     6858:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6859:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     6860:                                }
1.1.1.14  root     6861:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   6862:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     6863:                        }
                   6864:                }
                   6865:        }
1.1.1.14  root     6866:        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     6867: }
                   6868: 
                   6869: inline void pcbios_int_10h_07h()
                   6870: {
1.1.1.14  root     6871:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   6872:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   6873:                return;
                   6874:        }
                   6875:        vram_flush();
                   6876:        
1.1.1.23  root     6877:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6878:        SMALL_RECT rect;
1.1.1.14  root     6879:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   6880:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   6881:        
                   6882:        int right = min(REG8(DL), scr_width - 1);
                   6883:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     6884:        
                   6885:        if(REG8(AL) == 0) {
1.1.1.14  root     6886:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     6887:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     6888:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6889:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     6890:                        }
                   6891:                }
                   6892:        } else {
1.1.1.14  root     6893:                for(int y = bottom, y2 = max(REG8(CH) - 1, bottom - REG8(AL)); y >= REG8(CH); y--, y2--) {
1.1.1.16  root     6894:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     6895:                                if(y2 >= REG8(CH)) {
                   6896:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     6897:                                } else {
1.1.1.14  root     6898:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6899:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     6900:                                }
1.1.1.14  root     6901:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   6902:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     6903:                        }
                   6904:                }
                   6905:        }
1.1.1.14  root     6906:        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     6907: }
                   6908: 
                   6909: inline void pcbios_int_10h_08h()
                   6910: {
                   6911:        COORD co;
                   6912:        DWORD num;
                   6913:        
1.1.1.14  root     6914:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   6915:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     6916:        
                   6917:        if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     6918:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6919:                co.Y += scr_top;
                   6920:                vram_flush();
1.1       root     6921:                ReadConsoleOutputCharacter(hStdout, scr_char, 1, co, &num);
                   6922:                ReadConsoleOutputAttribute(hStdout, scr_attr, 1, co, &num);
                   6923:                REG8(AL) = scr_char[0];
                   6924:                REG8(AH) = scr_attr[0];
                   6925:        } else {
1.1.1.16  root     6926:                REG16(AX) = *(UINT16 *)(mem + pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y));
1.1       root     6927:        }
                   6928: }
                   6929: 
                   6930: inline void pcbios_int_10h_09h()
                   6931: {
                   6932:        COORD co;
                   6933:        
1.1.1.14  root     6934:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   6935:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   6936:        
1.1.1.16  root     6937:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   6938:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     6939:        
                   6940:        if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     6941: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6942:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     6943: #endif
1.1.1.16  root     6944:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     6945:                while(dest < end) {
                   6946:                        write_text_vram_char(dest - vram, REG8(AL));
                   6947:                        mem[dest++] = REG8(AL);
                   6948:                        write_text_vram_attr(dest - vram, REG8(BL));
                   6949:                        mem[dest++] = REG8(BL);
1.1       root     6950:                }
1.1.1.35  root     6951: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6952:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     6953: #endif
1.1       root     6954:        } else {
1.1.1.14  root     6955:                while(dest < end) {
1.1       root     6956:                        mem[dest++] = REG8(AL);
                   6957:                        mem[dest++] = REG8(BL);
                   6958:                }
                   6959:        }
                   6960: }
                   6961: 
                   6962: inline void pcbios_int_10h_0ah()
                   6963: {
                   6964:        COORD co;
                   6965:        
1.1.1.14  root     6966:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   6967:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   6968:        
1.1.1.16  root     6969:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   6970:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     6971:        
                   6972:        if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     6973: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6974:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     6975: #endif
1.1.1.16  root     6976:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     6977:                while(dest < end) {
                   6978:                        write_text_vram_char(dest - vram, REG8(AL));
                   6979:                        mem[dest++] = REG8(AL);
                   6980:                        dest++;
1.1       root     6981:                }
1.1.1.35  root     6982: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6983:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     6984: #endif
1.1       root     6985:        } else {
1.1.1.14  root     6986:                while(dest < end) {
1.1       root     6987:                        mem[dest++] = REG8(AL);
                   6988:                        dest++;
                   6989:                }
                   6990:        }
                   6991: }
                   6992: 
1.1.1.40  root     6993: HDC get_console_window_device_context()
                   6994: {
                   6995:        static HWND hwndFound = 0;
                   6996:        
                   6997:        if(hwndFound == 0) {
                   6998:                // https://support.microsoft.com/en-us/help/124103/how-to-obtain-a-console-window-handle-hwnd
                   6999:                char pszNewWindowTitle[1024];
                   7000:                char pszOldWindowTitle[1024];
                   7001:                
                   7002:                GetConsoleTitle(pszOldWindowTitle, 1024);
                   7003:                wsprintf(pszNewWindowTitle,"%d/%d", GetTickCount(), GetCurrentProcessId());
                   7004:                SetConsoleTitle(pszNewWindowTitle);
                   7005:                Sleep(100);
                   7006:                hwndFound = FindWindow(NULL, pszNewWindowTitle);
                   7007:                SetConsoleTitle(pszOldWindowTitle);
                   7008:        }
                   7009:        return GetDC(hwndFound);
                   7010: }
                   7011: 
                   7012: inline void pcbios_int_10h_0ch()
                   7013: {
                   7014:        HDC hdc = get_console_window_device_context();
                   7015:        
                   7016:        if(hdc != NULL) {
                   7017:                BYTE r = (REG8(AL) & 2) ? 255 : 0;
                   7018:                BYTE g = (REG8(AL) & 4) ? 255 : 0;
                   7019:                BYTE b = (REG8(AL) & 1) ? 255 : 0;
                   7020:                
                   7021:                if(REG8(AL) & 0x80) {
                   7022:                        COLORREF color = GetPixel(hdc, REG16(CX), REG16(DX));
                   7023:                        if(color != CLR_INVALID) {
                   7024:                                r ^= ((DWORD)color & 0x0000ff) ? 255 : 0;
                   7025:                                g ^= ((DWORD)color & 0x00ff00) ? 255 : 0;
                   7026:                                b ^= ((DWORD)color & 0xff0000) ? 255 : 0;
                   7027:                        }
                   7028:                }
                   7029:                SetPixel(hdc, REG16(CX), REG16(DX), RGB(r, g, b));
                   7030:        }
                   7031: }
                   7032: 
                   7033: inline void pcbios_int_10h_0dh()
                   7034: {
                   7035:        HDC hdc = get_console_window_device_context();
                   7036:        BYTE r = 0;
                   7037:        BYTE g = 0;
                   7038:        BYTE b = 0;
                   7039:        
                   7040:        if(hdc != NULL) {
                   7041:                COLORREF color = GetPixel(hdc, REG16(CX), REG16(DX));
                   7042:                if(color != CLR_INVALID) {
                   7043:                        r = ((DWORD)color & 0x0000ff) ? 255 : 0;
                   7044:                        g = ((DWORD)color & 0x00ff00) ? 255 : 0;
                   7045:                        b = ((DWORD)color & 0xff0000) ? 255 : 0;
                   7046:                }
                   7047:        }
                   7048:        REG8(AL) = ((b != 0) ? 1 : 0) | ((r != 0) ? 2 : 0) | ((g != 0) ? 4 : 0);
                   7049: }
                   7050: 
1.1       root     7051: inline void pcbios_int_10h_0eh()
                   7052: {
1.1.1.14  root     7053:        DWORD num;
                   7054:        COORD co;
                   7055:        
                   7056:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7057:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   7058:        
                   7059:        if(REG8(AL) == 7) {
                   7060:                //MessageBeep(-1);
                   7061:        } else if(REG8(AL) == 8 || REG8(AL) == 10 || REG8(AL) == 13) {
                   7062:                if(REG8(AL) == 10) {
                   7063:                        vram_flush();
                   7064:                }
1.1.1.23  root     7065:                WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), &REG8(AL), 1, &num, NULL);
1.1.1.14  root     7066:                cursor_moved = true;
                   7067:        } else {
1.1.1.16  root     7068:                int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
1.1.1.14  root     7069:                if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     7070: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7071:                        EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     7072: #endif
1.1.1.16  root     7073:                        int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     7074:                        write_text_vram_char(dest - vram, REG8(AL));
1.1.1.35  root     7075: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7076:                        LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     7077: #endif
1.1.1.14  root     7078:                        
1.1.1.23  root     7079:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     7080:                        if(++co.X == scr_width) {
                   7081:                                co.X = 0;
                   7082:                                if(++co.Y == scr_height) {
                   7083:                                        vram_flush();
                   7084:                                        WriteConsole(hStdout, "\n", 1, &num, NULL);
                   7085:                                        cursor_moved = true;
                   7086:                                }
                   7087:                        }
                   7088:                        if(!cursor_moved) {
                   7089:                                co.Y += scr_top;
                   7090:                                SetConsoleCursorPosition(hStdout, co);
                   7091:                                cursor_moved = true;
                   7092:                        }
                   7093:                }
                   7094:                mem[dest] = REG8(AL);
                   7095:        }
1.1       root     7096: }
                   7097: 
                   7098: inline void pcbios_int_10h_0fh()
                   7099: {
                   7100:        REG8(AL) = mem[0x449];
                   7101:        REG8(AH) = mem[0x44a];
                   7102:        REG8(BH) = mem[0x462];
                   7103: }
                   7104: 
1.1.1.14  root     7105: inline void pcbios_int_10h_11h()
                   7106: {
                   7107:        switch(REG8(AL)) {
1.1.1.16  root     7108:        case 0x01:
1.1.1.14  root     7109:        case 0x11:
1.1.1.16  root     7110:                pcbios_set_console_size(80, 28, true);
1.1.1.14  root     7111:                break;
1.1.1.16  root     7112:        case 0x02:
1.1.1.14  root     7113:        case 0x12:
1.1.1.16  root     7114:                pcbios_set_console_size(80, 50, true);
1.1.1.14  root     7115:                break;
1.1.1.16  root     7116:        case 0x04:
1.1.1.14  root     7117:        case 0x14:
1.1.1.16  root     7118:                pcbios_set_console_size(80, 25, true);
                   7119:                break;
                   7120:        case 0x18:
                   7121:                pcbios_set_console_size(80, 50, true);
1.1.1.14  root     7122:                break;
                   7123:        case 0x30:
                   7124:                SREG(ES) = 0;
                   7125:                i386_load_segment_descriptor(ES);
                   7126:                REG16(BP) = 0;
                   7127:                REG16(CX) = mem[0x485];
                   7128:                REG8(DL) = mem[0x484];
                   7129:                break;
                   7130:        }
                   7131: }
                   7132: 
                   7133: inline void pcbios_int_10h_12h()
                   7134: {
1.1.1.16  root     7135:        switch(REG8(BL)) {
                   7136:        case 0x10:
1.1.1.14  root     7137:                REG16(BX) = 0x0003;
                   7138:                REG16(CX) = 0x0009;
1.1.1.16  root     7139:                break;
1.1.1.14  root     7140:        }
                   7141: }
                   7142: 
1.1       root     7143: inline void pcbios_int_10h_13h()
                   7144: {
1.1.1.3   root     7145:        int ofs = SREG_BASE(ES) + REG16(BP);
1.1       root     7146:        COORD co;
                   7147:        DWORD num;
                   7148:        
                   7149:        co.X = REG8(DL);
1.1.1.14  root     7150:        co.Y = REG8(DH) + scr_top;
                   7151:        
                   7152:        vram_flush();
1.1       root     7153:        
                   7154:        switch(REG8(AL)) {
                   7155:        case 0x00:
                   7156:        case 0x01:
                   7157:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7158:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7159:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   7160:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7161:                        SetConsoleCursorPosition(hStdout, co);
                   7162:                        
                   7163:                        if(csbi.wAttributes != REG8(BL)) {
                   7164:                                SetConsoleTextAttribute(hStdout, REG8(BL));
                   7165:                        }
1.1.1.14  root     7166:                        WriteConsole(hStdout, &mem[ofs], REG16(CX), &num, NULL);
                   7167:                        
1.1       root     7168:                        if(csbi.wAttributes != REG8(BL)) {
                   7169:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   7170:                        }
                   7171:                        if(REG8(AL) == 0x00) {
1.1.1.15  root     7172:                                if(!restore_console_on_exit) {
                   7173:                                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7174:                                        scr_top = csbi.srWindow.Top;
                   7175:                                }
1.1.1.14  root     7176:                                co.X = mem[0x450 + REG8(BH) * 2];
                   7177:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     7178:                                SetConsoleCursorPosition(hStdout, co);
                   7179:                        } else {
                   7180:                                cursor_moved = true;
                   7181:                        }
                   7182:                } else {
1.1.1.3   root     7183:                        m_CF = 1;
1.1       root     7184:                }
                   7185:                break;
                   7186:        case 0x02:
                   7187:        case 0x03:
                   7188:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7189:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7190:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   7191:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7192:                        SetConsoleCursorPosition(hStdout, co);
                   7193:                        
                   7194:                        WORD wAttributes = csbi.wAttributes;
                   7195:                        for(int i = 0; i < REG16(CX); i++, ofs += 2) {
                   7196:                                if(wAttributes != mem[ofs + 1]) {
                   7197:                                        SetConsoleTextAttribute(hStdout, mem[ofs + 1]);
                   7198:                                        wAttributes = mem[ofs + 1];
                   7199:                                }
1.1.1.14  root     7200:                                WriteConsole(hStdout, &mem[ofs], 1, &num, NULL);
1.1       root     7201:                        }
                   7202:                        if(csbi.wAttributes != wAttributes) {
                   7203:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   7204:                        }
                   7205:                        if(REG8(AL) == 0x02) {
1.1.1.14  root     7206:                                co.X = mem[0x450 + REG8(BH) * 2];
                   7207:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     7208:                                SetConsoleCursorPosition(hStdout, co);
                   7209:                        } else {
                   7210:                                cursor_moved = true;
                   7211:                        }
                   7212:                } else {
1.1.1.3   root     7213:                        m_CF = 1;
1.1       root     7214:                }
                   7215:                break;
                   7216:        case 0x10:
                   7217:        case 0x11:
                   7218:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7219:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7220:                        ReadConsoleOutputCharacter(hStdout, scr_char, REG16(CX), co, &num);
                   7221:                        ReadConsoleOutputAttribute(hStdout, scr_attr, REG16(CX), co, &num);
                   7222:                        for(int i = 0; i < num; i++) {
                   7223:                                mem[ofs++] = scr_char[i];
                   7224:                                mem[ofs++] = scr_attr[i];
1.1.1.45  root     7225:                                if(REG8(AL) & 0x01) {
1.1       root     7226:                                        mem[ofs++] = 0;
                   7227:                                        mem[ofs++] = 0;
                   7228:                                }
                   7229:                        }
                   7230:                } else {
1.1.1.16  root     7231:                        for(int i = 0, src = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y - scr_top); i < REG16(CX); i++) {
1.1       root     7232:                                mem[ofs++] = mem[src++];
                   7233:                                mem[ofs++] = mem[src++];
1.1.1.45  root     7234:                                if(REG8(AL) & 0x01) {
1.1       root     7235:                                        mem[ofs++] = 0;
                   7236:                                        mem[ofs++] = 0;
                   7237:                                }
1.1.1.14  root     7238:                                if(++co.X == scr_width) {
                   7239:                                        if(++co.Y == scr_height) {
1.1       root     7240:                                                break;
                   7241:                                        }
                   7242:                                        co.X = 0;
                   7243:                                }
                   7244:                        }
                   7245:                }
                   7246:                break;
1.1.1.45  root     7247:        case 0x12: // ???
                   7248:        case 0x13: // ???
1.1       root     7249:        case 0x20:
                   7250:        case 0x21:
                   7251:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7252:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     7253:                        int len = min(REG16(CX), scr_width * scr_height);
                   7254:                        for(int i = 0; i < len; i++) {
1.1       root     7255:                                scr_char[i] = mem[ofs++];
                   7256:                                scr_attr[i] = mem[ofs++];
1.1.1.45  root     7257:                                if(REG8(AL) & 0x01) {
1.1       root     7258:                                        ofs += 2;
                   7259:                                }
                   7260:                        }
1.1.1.14  root     7261:                        WriteConsoleOutputCharacter(hStdout, scr_char, len, co, &num);
                   7262:                        WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     7263:                } else {
1.1.1.16  root     7264:                        for(int i = 0, dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y - scr_top); i < REG16(CX); i++) {
1.1       root     7265:                                mem[dest++] = mem[ofs++];
                   7266:                                mem[dest++] = mem[ofs++];
1.1.1.45  root     7267:                                if(REG8(AL) & 0x01) {
1.1       root     7268:                                        ofs += 2;
                   7269:                                }
1.1.1.14  root     7270:                                if(++co.X == scr_width) {
                   7271:                                        if(++co.Y == scr_height) {
1.1       root     7272:                                                break;
                   7273:                                        }
                   7274:                                        co.X = 0;
                   7275:                                }
                   7276:                        }
                   7277:                }
                   7278:                break;
                   7279:        default:
1.1.1.22  root     7280:                unimplemented_10h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x10, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.3   root     7281:                m_CF = 1;
1.1       root     7282:                break;
                   7283:        }
                   7284: }
                   7285: 
1.1.1.30  root     7286: inline void pcbios_int_10h_18h()
                   7287: {
                   7288:        switch(REG8(AL)) {
                   7289:        case 0x00:
                   7290:        case 0x01:
                   7291: //             REG8(AL) = 0x86;
                   7292:                REG8(AL) = 0x00;
                   7293:                break;
                   7294:        default:
                   7295:                unimplemented_10h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x10, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   7296:                m_CF = 1;
                   7297:                break;
                   7298:        }
                   7299: }
                   7300: 
1.1.1.14  root     7301: inline void pcbios_int_10h_1ah()
                   7302: {
                   7303:        switch(REG8(AL)) {
                   7304:        case 0x00:
                   7305:                REG8(AL) = 0x1a;
                   7306:                REG8(BL) = 0x08;
                   7307:                REG8(BH) = 0x00;
                   7308:                break;
                   7309:        default:
1.1.1.22  root     7310:                unimplemented_10h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x10, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.14  root     7311:                m_CF = 1;
                   7312:                break;
                   7313:        }
                   7314: }
                   7315: 
1.1       root     7316: inline void pcbios_int_10h_1dh()
                   7317: {
                   7318:        switch(REG8(AL)) {
1.1.1.43  root     7319:        case 0x00:
                   7320:                // DOS/V Shift Status Line Control is not supported
                   7321:                m_CF = 1;
                   7322:                break;
1.1       root     7323:        case 0x01:
                   7324:                break;
                   7325:        case 0x02:
                   7326:                REG16(BX) = 0;
                   7327:                break;
                   7328:        default:
1.1.1.22  root     7329:                unimplemented_10h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x10, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   7330:                m_CF = 1;
                   7331:                break;
                   7332:        }
                   7333: }
                   7334: 
                   7335: inline void pcbios_int_10h_4fh()
                   7336: {
                   7337:        switch(REG8(AL)) {
                   7338:        case 0x00:
                   7339:                REG8(AH) = 0x02; // not supported
                   7340:                break;
                   7341:        case 0x01:
                   7342:        case 0x02:
                   7343:        case 0x03:
                   7344:        case 0x04:
                   7345:        case 0x05:
                   7346:        case 0x06:
                   7347:        case 0x07:
                   7348:        case 0x08:
                   7349:        case 0x09:
                   7350:        case 0x0a:
                   7351:        case 0x0b:
                   7352:        case 0x0c:
                   7353:                REG8(AH) = 0x01; // failed
                   7354:                break;
                   7355:        default:
                   7356:                unimplemented_10h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x10, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.3   root     7357:                m_CF = 1;
1.1       root     7358:                break;
                   7359:        }
                   7360: }
                   7361: 
                   7362: inline void pcbios_int_10h_82h()
                   7363: {
                   7364:        static UINT8 mode = 0;
                   7365:        
                   7366:        switch(REG8(AL)) {
1.1.1.22  root     7367:        case 0x00:
1.1       root     7368:                if(REG8(BL) != 0xff) {
                   7369:                        mode = REG8(BL);
                   7370:                }
                   7371:                REG8(AL) = mode;
                   7372:                break;
                   7373:        default:
1.1.1.22  root     7374:                unimplemented_10h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x10, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.3   root     7375:                m_CF = 1;
1.1       root     7376:                break;
                   7377:        }
                   7378: }
                   7379: 
1.1.1.22  root     7380: inline void pcbios_int_10h_83h()
                   7381: {
                   7382:        static UINT8 mode = 0;
                   7383:        
                   7384:        switch(REG8(AL)) {
                   7385:        case 0x00:
                   7386:                REG16(AX) = 0; // offset???
                   7387:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
                   7388:                i386_load_segment_descriptor(ES);
                   7389:                REG16(BX) = (SHADOW_BUF_TOP & 0x0f);
                   7390:                break;
                   7391:        default:
                   7392:                unimplemented_10h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x10, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   7393:                m_CF = 1;
                   7394:                break;
                   7395:        }
                   7396: }
                   7397: 
                   7398: inline void pcbios_int_10h_90h()
                   7399: {
                   7400:        REG8(AL) = mem[0x449];
                   7401: }
                   7402: 
                   7403: inline void pcbios_int_10h_91h()
                   7404: {
                   7405:        REG8(AL) = 0x04; // VGA
                   7406: }
                   7407: 
                   7408: inline void pcbios_int_10h_efh()
                   7409: {
                   7410:        REG16(DX) = 0xffff;
                   7411: }
                   7412: 
1.1       root     7413: inline void pcbios_int_10h_feh()
                   7414: {
                   7415:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     7416:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
1.1.1.3   root     7417:                i386_load_segment_descriptor(ES);
1.1.1.8   root     7418:                REG16(DI) = (SHADOW_BUF_TOP & 0x0f);
1.1       root     7419:        }
                   7420:        int_10h_feh_called = true;
                   7421: }
                   7422: 
                   7423: inline void pcbios_int_10h_ffh()
                   7424: {
                   7425:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.23  root     7426:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7427:                COORD co;
                   7428:                DWORD num;
                   7429:                
1.1.1.14  root     7430:                vram_flush();
                   7431:                
                   7432:                co.X = (REG16(DI) >> 1) % scr_width;
                   7433:                co.Y = (REG16(DI) >> 1) / scr_width;
1.1.1.16  root     7434:                int ofs = pcbios_get_shadow_buffer_address(0, co.X, co.Y);
                   7435:                int end = min(ofs + REG16(CX) * 2, pcbios_get_shadow_buffer_address(0, 0, scr_height));
1.1.1.14  root     7436:                int len;
                   7437:                for(len = 0; ofs < end; len++) {
                   7438:                        scr_char[len] = mem[ofs++];
                   7439:                        scr_attr[len] = mem[ofs++];
                   7440:                }
                   7441:                co.Y += scr_top;
                   7442:                WriteConsoleOutputCharacter(hStdout, scr_char, len, co, &num);
                   7443:                WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     7444:        }
                   7445:        int_10h_ffh_called = true;
                   7446: }
                   7447: 
1.1.1.42  root     7448: int pcbios_update_drive_param(int drive_num, int force_update)
                   7449: {
                   7450:        if(drive_num >= 0 && drive_num < 26) {
                   7451:                drive_param_t *drive_param = &drive_params[drive_num];
                   7452:                
                   7453:                if(force_update || !drive_param->initialized) {
                   7454:                        drive_param->valid = 0;
                   7455:                        char dev[64];
                   7456:                        sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7457:                        
                   7458:                        HANDLE hFile = CreateFile(dev, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   7459:                        if(hFile != INVALID_HANDLE_VALUE) {
                   7460:                                DWORD dwSize;
                   7461:                                if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &drive_param->geometry, sizeof(DISK_GEOMETRY), &dwSize, NULL)) {
                   7462:                                        drive_param->valid = 1;
                   7463:                                }
                   7464:                                CloseHandle(hFile);
                   7465:                        }
                   7466:                        drive_param->initialized = 1;
                   7467:                }
                   7468:                return(drive_param->valid);
                   7469:        }
                   7470:        return(0);
                   7471: }
                   7472: 
                   7473: inline void pcbios_int_13h_00h()
                   7474: {
                   7475:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7476:        
                   7477:        if(pcbios_update_drive_param(drive_num, 1)) {
                   7478:                REG8(AH) = 0x00; // successful completion
                   7479:        } else {
                   7480:                if(REG8(DL) & 0x80) {
                   7481:                        REG8(AH) = 0x05; // reset failed (hard disk)
                   7482:                } else {
                   7483:                        REG8(AH) = 0x80; // timeout (not ready)
                   7484:                }
                   7485:                m_CF = 1;
                   7486:        }
                   7487: }
                   7488: 
                   7489: inline void pcbios_int_13h_02h()
                   7490: {
                   7491:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7492:        
                   7493:        if(REG8(AL) == 0) {
                   7494:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   7495:                m_CF = 1;
                   7496:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   7497:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7498:                m_CF = 1;
                   7499:        } else {
                   7500:                drive_param_t *drive_param = &drive_params[drive_num];
                   7501:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   7502:                char dev[64];
                   7503:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7504:                
                   7505:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   7506:                if(hFile == INVALID_HANDLE_VALUE) {
                   7507:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7508:                        m_CF = 1;
                   7509:                } else {
                   7510:                        UINT32 sector_num = REG8(AL);
                   7511:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   7512:                        UINT32 head = REG8(DH);
                   7513:                        UINT32 sector = REG8(CL) & 0x3f;
                   7514:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   7515:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   7516:                        DWORD dwSize;
                   7517:                        
                   7518: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   7519: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7520: //                             m_CF = 1;
                   7521: //                     } else 
                   7522:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   7523:                                REG8(AH) = 0x04; // sector not found/read error
                   7524:                                m_CF = 1;
                   7525:                        } else if(ReadFile(hFile, mem + buffer_addr, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   7526:                                REG8(AH) = 0x04; // sector not found/read error
                   7527:                                m_CF = 1;
                   7528:                        } else {
                   7529:                                REG8(AH) = 0x00; // successful completion
                   7530:                        }
                   7531:                        CloseHandle(hFile);
                   7532:                }
                   7533:        }
                   7534: }
                   7535: 
                   7536: inline void pcbios_int_13h_03h()
                   7537: {
                   7538:        // this operation may cause serious damage for drives, so support only floppy disk...
                   7539:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7540:        
                   7541:        if(REG8(AL) == 0) {
                   7542:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   7543:                m_CF = 1;
                   7544:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   7545:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7546:                m_CF = 1;
                   7547:        } else if(!drive_params[drive_num].is_fdd()) {
                   7548:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7549:                m_CF = 1;
                   7550:        } else {
                   7551:                drive_param_t *drive_param = &drive_params[drive_num];
                   7552:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   7553:                char dev[64];
                   7554:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7555:                
                   7556:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   7557:                if(hFile == INVALID_HANDLE_VALUE) {
                   7558:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7559:                        m_CF = 1;
                   7560:                } else {
                   7561:                        UINT32 sector_num = REG8(AL);
                   7562:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   7563:                        UINT32 head = REG8(DH);
                   7564:                        UINT32 sector = REG8(CL) & 0x3f;
                   7565:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   7566:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   7567:                        DWORD dwSize;
                   7568:                        
                   7569: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   7570: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7571: //                             m_CF = 1;
                   7572: //                     } else 
                   7573:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   7574:                                REG8(AH) = 0x04; // sector not found/read error
                   7575:                                m_CF = 1;
                   7576:                        } else if(WriteFile(hFile, mem + buffer_addr, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   7577:                                REG8(AH) = 0x04; // sector not found/read error
                   7578:                                m_CF = 1;
                   7579:                        } else {
                   7580:                                REG8(AH) = 0x00; // successful completion
                   7581:                        }
                   7582:                        CloseHandle(hFile);
                   7583:                }
                   7584:        }
                   7585: }
                   7586: 
                   7587: inline void pcbios_int_13h_04h()
                   7588: {
                   7589:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7590:        
                   7591:        if(REG8(AL) == 0) {
                   7592:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   7593:                m_CF = 1;
                   7594:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   7595:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7596:                m_CF = 1;
                   7597:        } else {
                   7598:                drive_param_t *drive_param = &drive_params[drive_num];
                   7599:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   7600:                char dev[64];
                   7601:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7602:                
                   7603:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   7604:                if(hFile == INVALID_HANDLE_VALUE) {
                   7605:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7606:                        m_CF = 1;
                   7607:                } else {
                   7608:                        UINT32 sector_num = REG8(AL);
                   7609:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   7610:                        UINT32 head = REG8(DH);
                   7611:                        UINT32 sector = REG8(CL) & 0x3f;
                   7612:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   7613:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   7614:                        DWORD dwSize;
                   7615:                        UINT8 *tmp_buffer = (UINT8 *)malloc(sector_num * geo->BytesPerSector);
                   7616:                        
                   7617: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   7618: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7619: //                             m_CF = 1;
                   7620: //                     } else 
                   7621:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   7622:                                REG8(AH) = 0x04; // sector not found/read error
                   7623:                                m_CF = 1;
                   7624:                        } else if(ReadFile(hFile, tmp_buffer, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   7625:                                REG8(AH) = 0x04; // sector not found/read error
                   7626:                                m_CF = 1;
                   7627:                        } else if(memcmp(mem + buffer_addr, tmp_buffer, sector_num * geo->BytesPerSector) != 0) {
                   7628:                                REG8(AH) = 0x05; // data did not verify correctly (TI Professional PC)
                   7629:                                m_CF = 1;
                   7630:                        } else {
                   7631:                                REG8(AH) = 0x00; // successful completion
                   7632:                        }
                   7633:                        free(tmp_buffer);
                   7634:                        CloseHandle(hFile);
                   7635:                }
                   7636:        }
                   7637: }
                   7638: 
                   7639: inline void pcbios_int_13h_08h()
                   7640: {
                   7641:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7642:        
                   7643:        if(pcbios_update_drive_param(drive_num, 1)) {
                   7644:                drive_param_t *drive_param = &drive_params[drive_num];
                   7645:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   7646:                
                   7647:                REG16(AX) = 0x0000;
                   7648:                switch(geo->MediaType) {
                   7649:                case F5_360_512:
                   7650:                case F5_320_512:
                   7651:                case F5_320_1024:
                   7652:                case F5_180_512:
                   7653:                case F5_160_512:
                   7654:                        REG8(BL) = 0x01; // 320K/360K disk
                   7655:                        break;
                   7656:                case F5_1Pt2_512:
                   7657:                case F3_1Pt2_512:
                   7658:                case F3_1Pt23_1024:
                   7659:                case F5_1Pt23_1024:
                   7660:                        REG8(BL) = 0x02; // 1.2M disk
                   7661:                        break;
                   7662:                case F3_720_512:
                   7663:                case F3_640_512:
                   7664:                case F5_640_512:
                   7665:                case F5_720_512:
                   7666:                        REG8(BL) = 0x03; // 720K disk
                   7667:                        break;
                   7668:                case F3_1Pt44_512:
                   7669:                        REG8(BL) = 0x04; // 1.44M disk
                   7670:                        break;
                   7671:                case F3_2Pt88_512:
                   7672:                        REG8(BL) = 0x06; // 2.88M disk
                   7673:                        break;
                   7674:                case RemovableMedia:
                   7675:                        REG8(BL) = 0x10; // ATAPI Removable Media Device
                   7676:                        break;
                   7677:                default:
                   7678:                        REG8(BL) = 0x00; // unknown
                   7679:                        break;
                   7680:                }
                   7681:                if(REG8(DL) & 0x80) {
                   7682:                        switch(GetLogicalDrives() & 0x0c) {
                   7683:                        case 0x00: REG8(DL) = 0x00; break;
                   7684:                        case 0x04:
                   7685:                        case 0x08: REG8(DL) = 0x01; break;
                   7686:                        case 0x0c: REG8(DL) = 0x02; break;
                   7687:                        }
                   7688:                } else {
                   7689:                        switch(GetLogicalDrives() & 0x03) {
                   7690:                        case 0x00: REG8(DL) = 0x00; break;
                   7691:                        case 0x01:
                   7692:                        case 0x02: REG8(DL) = 0x01; break;
                   7693:                        case 0x03: REG8(DL) = 0x02; break;
                   7694:                        }
                   7695:                }
                   7696:                REG8(DH) = drive_param->head_num();
                   7697:                int cyl = (geo->Cylinders.QuadPart > 0x3ff) ? 0x3ff : geo->Cylinders.QuadPart;
                   7698:                int sec = (geo->SectorsPerTrack > 0x3f) ? 0x3f : geo->SectorsPerTrack;
                   7699:                REG8(CH) = cyl & 0xff;
                   7700:                REG8(CL) = (sec & 0x3f) | ((cyl & 0x300) >> 2);
                   7701:        } else {
                   7702:                REG8(AH) = 0x07;
                   7703:                m_CF = 1;
                   7704:        }
                   7705: }
                   7706: 
                   7707: inline void pcbios_int_13h_10h()
                   7708: {
                   7709:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7710:        
                   7711:        if(pcbios_update_drive_param(drive_num, 1)) {
                   7712:                REG8(AH) = 0x00; // successful completion
                   7713:        } else {
                   7714:                if(REG8(DL) & 0x80) {
                   7715:                        REG8(AH) = 0xaa; // drive not ready (hard disk)
                   7716:                } else {
                   7717:                        REG8(AH) = 0x80; // timeout (not ready)
                   7718:                }
                   7719:                m_CF = 1;
                   7720:        }
                   7721: }
                   7722: 
                   7723: inline void pcbios_int_13h_15h()
                   7724: {
                   7725:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7726:        
                   7727:        if(pcbios_update_drive_param(drive_num, 1)) {
                   7728:                if(REG8(DL) & 0x80) {
                   7729:                        REG8(AH) = 0x02; // floppy (or other removable drive) with change-line support
                   7730:                } else {
                   7731:                        REG8(AH) = 0x03; // hard disk
                   7732:                }
                   7733:        } else {
                   7734:                REG8(AH) = 0x00; // no such drive
                   7735:        }
                   7736: }
                   7737: 
1.1.1.43  root     7738: inline void pcbios_int_13h_41h()
                   7739: {
                   7740:        if(REG16(BX) == 0x55aa) {
                   7741:                // IBM/MS INT 13 Extensions is not installed
                   7742:                REG8(AH) = 0x01;
                   7743:                m_CF = 1;
                   7744:        } else {
                   7745:                unimplemented_13h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x13, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   7746:                REG8(AH) = 0x01;
                   7747:                m_CF = 1;
                   7748:        }
                   7749: }
                   7750: 
1.1.1.25  root     7751: inline void pcbios_int_14h_00h()
                   7752: {
1.1.1.29  root     7753:        if(REG16(DX) < 4) {
1.1.1.25  root     7754:                static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600};
                   7755:                UINT8 selector = sio_read(REG16(DX), 3);
                   7756:                selector &= ~0x3f;
                   7757:                selector |= REG8(AL) & 0x1f;
                   7758:                UINT16 divisor = 115200 / rate[REG8(AL) >> 5];
                   7759:                sio_write(REG16(DX), 3, selector | 0x80);
                   7760:                sio_write(REG16(DX), 0, divisor & 0xff);
                   7761:                sio_write(REG16(DX), 1, divisor >> 8);
                   7762:                sio_write(REG16(DX), 3, selector);
                   7763:                REG8(AH) = sio_read(REG16(DX), 5);
                   7764:                REG8(AL) = sio_read(REG16(DX), 6);
                   7765:        } else {
                   7766:                REG8(AH) = 0x80;
                   7767:        }
                   7768: }
                   7769: 
                   7770: inline void pcbios_int_14h_01h()
                   7771: {
1.1.1.29  root     7772:        if(REG16(DX) < 4) {
1.1.1.25  root     7773:                UINT8 selector = sio_read(REG16(DX), 3);
                   7774:                sio_write(REG16(DX), 3, selector & ~0x80);
                   7775:                sio_write(REG16(DX), 0, REG8(AL));
                   7776:                sio_write(REG16(DX), 3, selector);
                   7777:                REG8(AH) = sio_read(REG16(DX), 5);
                   7778:        } else {
                   7779:                REG8(AH) = 0x80;
                   7780:        }
                   7781: }
                   7782: 
                   7783: inline void pcbios_int_14h_02h()
                   7784: {
1.1.1.29  root     7785:        if(REG16(DX) < 4) {
1.1.1.25  root     7786:                UINT8 selector = sio_read(REG16(DX), 3);
                   7787:                sio_write(REG16(DX), 3, selector & ~0x80);
                   7788:                REG8(AL) = sio_read(REG16(DX), 0);
                   7789:                sio_write(REG16(DX), 3, selector);
                   7790:                REG8(AH) = sio_read(REG16(DX), 5);
                   7791:        } else {
                   7792:                REG8(AH) = 0x80;
                   7793:        }
                   7794: }
                   7795: 
                   7796: inline void pcbios_int_14h_03h()
                   7797: {
1.1.1.29  root     7798:        if(REG16(DX) < 4) {
1.1.1.25  root     7799:                REG8(AH) = sio_read(REG16(DX), 5);
                   7800:                REG8(AL) = sio_read(REG16(DX), 6);
                   7801:        } else {
                   7802:                REG8(AH) = 0x80;
                   7803:        }
                   7804: }
                   7805: 
                   7806: inline void pcbios_int_14h_04h()
                   7807: {
1.1.1.29  root     7808:        if(REG16(DX) < 4) {
1.1.1.25  root     7809:                UINT8 selector = sio_read(REG16(DX), 3);
                   7810:                if(REG8(CH) <= 0x03) {
                   7811:                        selector = (selector & ~0x03) | REG8(CH);
                   7812:                }
                   7813:                if(REG8(BL) == 0x00) {
                   7814:                        selector &= ~0x04;
                   7815:                } else if(REG8(BL) == 0x01) {
                   7816:                        selector |= 0x04;
                   7817:                }
                   7818:                if(REG8(BH) == 0x00) {
                   7819:                        selector = (selector & ~0x38) | 0x00;
                   7820:                } else if(REG8(BH) == 0x01) {
                   7821:                        selector = (selector & ~0x38) | 0x08;
                   7822:                } else if(REG8(BH) == 0x02) {
                   7823:                        selector = (selector & ~0x38) | 0x18;
                   7824:                } else if(REG8(BH) == 0x03) {
                   7825:                        selector = (selector & ~0x38) | 0x28;
                   7826:                } else if(REG8(BH) == 0x04) {
                   7827:                        selector = (selector & ~0x38) | 0x38;
                   7828:                }
                   7829:                if(REG8(AL) == 0x00) {
                   7830:                        selector |= 0x40;
                   7831:                } else if(REG8(AL) == 0x01) {
                   7832:                        selector &= ~0x40;
                   7833:                }
                   7834:                if(REG8(CL) <= 0x0b) {
                   7835:                        static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200};
                   7836:                        UINT16 divisor = 115200 / rate[REG8(CL)];
                   7837:                        sio_write(REG16(DX), 3, selector | 0x80);
                   7838:                        sio_write(REG16(DX), 0, divisor & 0xff);
                   7839:                        sio_write(REG16(DX), 1, divisor >> 8);
                   7840:                }
                   7841:                sio_write(REG16(DX), 3, selector);
                   7842:                REG8(AH) = sio_read(REG16(DX), 5);
                   7843:                REG8(AL) = sio_read(REG16(DX), 6);
                   7844:        } else {
                   7845:                REG8(AH) = 0x80;
                   7846:        }
                   7847: }
                   7848: 
                   7849: inline void pcbios_int_14h_05h()
                   7850: {
1.1.1.29  root     7851:        if(REG16(DX) < 4) {
1.1.1.25  root     7852:                if(REG8(AL) == 0x00) {
                   7853:                        REG8(BL) = sio_read(REG16(DX), 4);
                   7854:                        REG8(AH) = sio_read(REG16(DX), 5);
                   7855:                        REG8(AL) = sio_read(REG16(DX), 6);
                   7856:                } else if(REG8(AL) == 0x01) {
                   7857:                        sio_write(REG16(DX), 4, REG8(BL));
                   7858:                        REG8(AH) = sio_read(REG16(DX), 5);
                   7859:                        REG8(AL) = sio_read(REG16(DX), 6);
                   7860:                } else {
                   7861:                        unimplemented_14h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x14, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   7862:                }
                   7863:        } else {
                   7864:                REG8(AH) = 0x80;
                   7865:        }
                   7866: }
                   7867: 
1.1.1.14  root     7868: inline void pcbios_int_15h_10h()
                   7869: {
1.1.1.22  root     7870:        switch(REG8(AL)) {
                   7871:        case 0x00:
1.1.1.14  root     7872:                Sleep(10);
1.1.1.35  root     7873:                REQUEST_HARDWRE_UPDATE();
1.1.1.22  root     7874:                break;
                   7875:        default:
                   7876:                unimplemented_15h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x15, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.14  root     7877:                REG8(AH) = 0x86;
                   7878:                m_CF = 1;
                   7879:        }
                   7880: }
                   7881: 
1.1       root     7882: inline void pcbios_int_15h_23h()
                   7883: {
                   7884:        switch(REG8(AL)) {
1.1.1.22  root     7885:        case 0x00:
1.1.1.8   root     7886:                REG8(CL) = cmos_read(0x2d);
                   7887:                REG8(CH) = cmos_read(0x2e);
1.1       root     7888:                break;
1.1.1.22  root     7889:        case 0x01:
1.1.1.8   root     7890:                cmos_write(0x2d, REG8(CL));
                   7891:                cmos_write(0x2e, REG8(CH));
1.1       root     7892:                break;
                   7893:        default:
1.1.1.22  root     7894:                unimplemented_15h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x15, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1       root     7895:                REG8(AH) = 0x86;
1.1.1.3   root     7896:                m_CF = 1;
1.1       root     7897:                break;
                   7898:        }
                   7899: }
                   7900: 
                   7901: inline void pcbios_int_15h_24h()
                   7902: {
                   7903:        switch(REG8(AL)) {
1.1.1.22  root     7904:        case 0x00:
1.1.1.3   root     7905:                i386_set_a20_line(0);
1.1       root     7906:                REG8(AH) = 0;
                   7907:                break;
1.1.1.22  root     7908:        case 0x01:
1.1.1.3   root     7909:                i386_set_a20_line(1);
1.1       root     7910:                REG8(AH) = 0;
                   7911:                break;
1.1.1.22  root     7912:        case 0x02:
1.1       root     7913:                REG8(AH) = 0;
1.1.1.3   root     7914:                REG8(AL) = (m_a20_mask >> 20) & 1;
1.1       root     7915:                REG16(CX) = 0;
                   7916:                break;
1.1.1.22  root     7917:        case 0x03:
1.1       root     7918:                REG16(AX) = 0;
                   7919:                REG16(BX) = 0;
                   7920:                break;
1.1.1.22  root     7921:        default:
                   7922:                unimplemented_15h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x15, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   7923:                REG8(AH) = 0x86;
                   7924:                m_CF = 1;
                   7925:                break;
1.1       root     7926:        }
                   7927: }
                   7928: 
                   7929: inline void pcbios_int_15h_49h()
                   7930: {
1.1.1.27  root     7931:        REG8(AH) = 0x00;
                   7932:        REG8(BL) = 0x00; // DOS/V
1.1       root     7933: }
                   7934: 
1.1.1.22  root     7935: inline void pcbios_int_15h_50h()
                   7936: {
                   7937:        switch(REG8(AL)) {
                   7938:        case 0x00:
                   7939:        case 0x01:
                   7940:                if(REG8(BH) != 0x00 && REG8(BH) != 0x01) {
                   7941:                        REG8(AH) = 0x01; // invalid font type in bh
                   7942:                        m_CF = 1;
1.1.1.27  root     7943:                } else if(REG8(BL) != 0x00) {
1.1.1.22  root     7944:                        REG8(AH) = 0x02; // bl not zero
                   7945:                        m_CF = 1;
                   7946:                } else if(REG16(BP) != 0 && REG16(BP) != 437 && REG16(BP) != 932 && REG16(BP) != 934 && REG16(BP) != 936 && REG16(BP) != 938) {
                   7947:                        REG8(AH) = 0x04; // invalid code page
                   7948:                        m_CF = 1;
1.1.1.27  root     7949:                } else if(REG8(AL) == 0x01) {
                   7950:                        REG8(AH) = 0x06; // font is read only
1.1.1.22  root     7951:                        m_CF = 1;
1.1.1.27  root     7952:                } else {
                   7953:                        // dummy font read routine is at fffd:000d
                   7954:                        SREG(ES) = 0xfffd;
                   7955:                        i386_load_segment_descriptor(ES);
1.1.1.32  root     7956:                        REG16(BX) = 0x000d;
1.1.1.27  root     7957:                        REG8(AH) = 0x00; // success
1.1.1.22  root     7958:                }
                   7959:                break;
                   7960:        default:
                   7961:                unimplemented_15h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x15, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   7962:                REG8(AH) = 0x86;
                   7963:                m_CF = 1;
                   7964:                break;
                   7965:        }
                   7966: }
                   7967: 
1.1.1.30  root     7968: inline void pcbios_int_15h_53h()
                   7969: {
                   7970:        switch(REG8(AL)) {
                   7971:        case 0x00:
                   7972:                // APM is not installed
                   7973:                REG8(AH) = 0x86;
                   7974:                m_CF = 1;
                   7975:                break;
                   7976:        default:
                   7977:                unimplemented_15h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x15, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   7978:                REG8(AH) = 0x86;
                   7979:                m_CF = 1;
                   7980:                break;
                   7981:        }
                   7982: }
                   7983: 
1.1.1.43  root     7984: inline void pcbios_int_15h_84h()
                   7985: {
                   7986:        // joystick support (from DOSBox)
                   7987:        switch(REG16(DX)) {
                   7988:        case 0x00:
                   7989:                REG16(AX) = 0x00f0;
                   7990:                REG16(DX) = 0x0201;
                   7991:                m_CF = 1;
                   7992:                break;
                   7993:        case 0x01:
                   7994:                REG16(AX) = REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   7995:                m_CF = 1;
                   7996:                break;
                   7997:        default:
                   7998:                unimplemented_15h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x15, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   7999:                REG8(AH) = 0x86;
                   8000:                m_CF = 1;
                   8001:                break;
                   8002:        }
                   8003: }
1.1.1.35  root     8004: 
                   8005: DWORD WINAPI pcbios_int_15h_86h_thread(LPVOID)
1.1       root     8006: {
                   8007:        UINT32 usec = (REG16(CX) << 16) | REG16(DX);
1.1.1.14  root     8008:        UINT32 msec = usec / 1000;
                   8009:        
1.1.1.35  root     8010:        while(msec && !m_halted) {
1.1.1.14  root     8011:                UINT32 tmp = min(msec, 100);
                   8012:                if(msec - tmp < 10) {
                   8013:                        tmp = msec;
                   8014:                }
                   8015:                Sleep(tmp);
                   8016:                msec -= tmp;
                   8017:        }
1.1.1.35  root     8018:        
                   8019: #ifdef USE_SERVICE_THREAD
                   8020:        service_exit = true;
                   8021: #endif
                   8022:        return(0);
                   8023: }
                   8024: 
                   8025: inline void pcbios_int_15h_86h()
                   8026: {
                   8027:        if(!(REG16(CX) == 0 && REG16(DX) == 0)) {
                   8028: #ifdef USE_SERVICE_THREAD
                   8029:                start_service_loop(pcbios_int_15h_86h_thread);
                   8030: #else
                   8031:                pcbios_int_15h_86h_thread(NULL);
                   8032:                REQUEST_HARDWRE_UPDATE();
                   8033: #endif
                   8034:        }
1.1       root     8035: }
                   8036: 
                   8037: inline void pcbios_int_15h_87h()
                   8038: {
                   8039:        // copy extended memory (from DOSBox)
                   8040:        int len = REG16(CX) * 2;
1.1.1.3   root     8041:        int ofs = SREG_BASE(ES) + REG16(SI);
1.1       root     8042:        int src = (*(UINT32 *)(mem + ofs + 0x12) & 0xffffff); // + (mem[ofs + 0x16] << 24);
                   8043:        int dst = (*(UINT32 *)(mem + ofs + 0x1a) & 0xffffff); // + (mem[ofs + 0x1e] << 24);
                   8044:        memcpy(mem + dst, mem + src, len);
                   8045:        REG16(AX) = 0x00;
                   8046: }
                   8047: 
                   8048: inline void pcbios_int_15h_88h()
                   8049: {
1.1.1.17  root     8050:        REG16(AX) = ((min(MAX_MEM, 0x4000000) - 0x100000) >> 10);
1.1       root     8051: }
                   8052: 
                   8053: inline void pcbios_int_15h_89h()
                   8054: {
1.1.1.21  root     8055: #if defined(HAS_I286) || defined(HAS_I386)
1.1       root     8056:        // switch to protected mode (from DOSBox)
                   8057:        write_io_byte(0x20, 0x10);
                   8058:        write_io_byte(0x21, REG8(BH));
                   8059:        write_io_byte(0x21, 0x00);
                   8060:        write_io_byte(0xa0, 0x10);
                   8061:        write_io_byte(0xa1, REG8(BL));
                   8062:        write_io_byte(0xa1, 0x00);
1.1.1.3   root     8063:        i386_set_a20_line(1);
                   8064:        int ofs = SREG_BASE(ES) + REG16(SI);
                   8065:        m_gdtr.limit = *(UINT16 *)(mem + ofs + 0x08);
                   8066:        m_gdtr.base = *(UINT32 *)(mem + ofs + 0x08 + 0x02) & 0xffffff;
                   8067:        m_idtr.limit = *(UINT16 *)(mem + ofs + 0x10);
                   8068:        m_idtr.base = *(UINT32 *)(mem + ofs + 0x10 + 0x02) & 0xffffff;
                   8069: #if defined(HAS_I386)
                   8070:        m_cr[0] |= 1;
                   8071: #else
                   8072:        m_msw |= 1;
                   8073: #endif
                   8074:        SREG(DS) = 0x18;
                   8075:        SREG(ES) = 0x20;
                   8076:        SREG(SS) = 0x28;
                   8077:        i386_load_segment_descriptor(DS);
                   8078:        i386_load_segment_descriptor(ES);
                   8079:        i386_load_segment_descriptor(SS);
1.1.1.21  root     8080:        UINT16 offset = *(UINT16 *)(mem + SREG_BASE(SS) + REG16(SP));
1.1       root     8081:        REG16(SP) += 6;
1.1.1.3   root     8082: #if defined(HAS_I386)
1.1.1.21  root     8083:        UINT32 flags = get_flags();
                   8084:        flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
                   8085:        set_flags(flags);
1.1.1.3   root     8086: #else
1.1.1.21  root     8087:        UINT32 flags = CompressFlags();
                   8088:        flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
                   8089:        ExpandFlags(flags);
1.1.1.3   root     8090: #endif
1.1       root     8091:        REG16(AX) = 0x00;
1.1.1.21  root     8092:        i386_jmp_far(0x30, /*REG16(CX)*/offset);
1.1       root     8093: #else
1.1.1.21  root     8094:        // i86/i186/v30: protected mode is not supported
1.1       root     8095:        REG8(AH) = 0x86;
1.1.1.3   root     8096:        m_CF = 1;
1.1       root     8097: #endif
                   8098: }
                   8099: 
1.1.1.21  root     8100: inline void pcbios_int_15h_8ah()
                   8101: {
                   8102:        UINT32 size = MAX_MEM - 0x100000;
                   8103:        REG16(AX) = size & 0xffff;
                   8104:        REG16(DX) = size >> 16;
                   8105: }
                   8106: 
1.1.1.3   root     8107: #if defined(HAS_I386)
1.1       root     8108: inline void pcbios_int_15h_c9h()
                   8109: {
                   8110:        REG8(AH) = 0x00;
                   8111:        REG8(CH) = cpu_type;
                   8112:        REG8(CL) = cpu_step;
                   8113: }
1.1.1.3   root     8114: #endif
1.1       root     8115: 
                   8116: inline void pcbios_int_15h_cah()
                   8117: {
                   8118:        switch(REG8(AL)) {
1.1.1.22  root     8119:        case 0x00:
1.1       root     8120:                if(REG8(BL) > 0x3f) {
                   8121:                        REG8(AH) = 0x03;
1.1.1.3   root     8122:                        m_CF = 1;
1.1       root     8123:                } else if(REG8(BL) < 0x0e) {
                   8124:                        REG8(AH) = 0x04;
1.1.1.3   root     8125:                        m_CF = 1;
1.1       root     8126:                } else {
1.1.1.8   root     8127:                        REG8(CL) = cmos_read(REG8(BL));
1.1       root     8128:                }
                   8129:                break;
1.1.1.22  root     8130:        case 0x01:
1.1       root     8131:                if(REG8(BL) > 0x3f) {
                   8132:                        REG8(AH) = 0x03;
1.1.1.3   root     8133:                        m_CF = 1;
1.1       root     8134:                } else if(REG8(BL) < 0x0e) {
                   8135:                        REG8(AH) = 0x04;
1.1.1.3   root     8136:                        m_CF = 1;
1.1       root     8137:                } else {
1.1.1.8   root     8138:                        cmos_write(REG8(BL), REG8(CL));
1.1       root     8139:                }
                   8140:                break;
                   8141:        default:
1.1.1.22  root     8142:                unimplemented_15h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x15, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1       root     8143:                REG8(AH) = 0x86;
1.1.1.3   root     8144:                m_CF = 1;
1.1       root     8145:                break;
                   8146:        }
                   8147: }
                   8148: 
1.1.1.22  root     8149: inline void pcbios_int_15h_e8h()
1.1.1.17  root     8150: {
1.1.1.22  root     8151:        switch(REG8(AL)) {
                   8152: #if defined(HAS_I386)
                   8153:        case 0x01:
                   8154:                REG16(AX) = REG16(CX) = ((min(MAX_MEM, 0x1000000) - 0x100000) >> 10);
                   8155:                REG16(BX) = REG16(DX) = ((max(MAX_MEM, 0x1000000) - 0x1000000) >> 16);
                   8156:                break;
1.1.1.17  root     8157: #endif
1.1.1.22  root     8158:        default:
                   8159:                unimplemented_15h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x15, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   8160:                REG8(AH) = 0x86;
                   8161:                m_CF = 1;
                   8162:                break;
                   8163:        }
                   8164: }
1.1.1.17  root     8165: 
1.1.1.33  root     8166: void pcbios_update_key_code(bool wait)
1.1       root     8167: {
1.1.1.32  root     8168:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8169: #ifdef USE_SERVICE_THREAD
                   8170:                EnterCriticalSection(&key_buf_crit_sect);
                   8171: #endif
                   8172:                bool empty = key_buf_char->empty();
                   8173: #ifdef USE_SERVICE_THREAD
                   8174:                LeaveCriticalSection(&key_buf_crit_sect);
                   8175: #endif
                   8176:                if(empty) {
1.1.1.32  root     8177:                        if(!update_key_buffer()) {
1.1.1.33  root     8178:                                if(wait) {
1.1.1.32  root     8179:                                        Sleep(10);
                   8180:                                } else {
                   8181:                                        maybe_idle();
                   8182:                                }
1.1.1.14  root     8183:                        }
                   8184:                }
1.1.1.34  root     8185:        }
                   8186:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8187: #ifdef USE_SERVICE_THREAD
                   8188:                EnterCriticalSection(&key_buf_crit_sect);
                   8189: #endif
1.1.1.32  root     8190:                if(key_buf_char->count() != 0) {
1.1.1.41  root     8191:                        int key_char = key_buf_char->read();
                   8192:                        int key_scan = key_buf_scan->read();
                   8193:                        key_code  = key_char << 0;
                   8194:                        key_code |= key_scan << 8;
1.1.1.35  root     8195:                        key_recv  = 0x0000ffff;
1.1.1.41  root     8196:                        // write to bottom of key buffer
                   8197:                        mem[0x43c] = (UINT8)key_char;
                   8198:                        mem[0x43d] = (UINT8)key_scan;
1.1.1.32  root     8199:                }
                   8200:                if(key_buf_char->count() != 0) {
1.1.1.41  root     8201:                        int key_char = key_buf_char->read();
                   8202:                        int key_scan = key_buf_scan->read();
                   8203:                        key_code |= key_char << 16;
                   8204:                        key_code |= key_scan << 24;
1.1.1.33  root     8205:                        key_recv |= 0xffff0000;
1.1.1.41  root     8206:                        // write to bottom of key buffer
                   8207:                        mem[0x43c] = (UINT8)key_char;
                   8208:                        mem[0x43d] = (UINT8)key_scan;
1.1.1.32  root     8209:                }
1.1.1.35  root     8210: #ifdef USE_SERVICE_THREAD
                   8211:                LeaveCriticalSection(&key_buf_crit_sect);
                   8212: #endif
1.1       root     8213:        }
                   8214: }
                   8215: 
1.1.1.35  root     8216: DWORD WINAPI pcbios_int_16h_00h_thread(LPVOID)
1.1       root     8217: {
1.1.1.33  root     8218:        while(key_recv == 0 && !m_halted) {
                   8219:                pcbios_update_key_code(true);
1.1       root     8220:        }
1.1.1.33  root     8221:        if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
                   8222:                if((key_code & 0xffff) == 0x0000 || (key_code & 0xffff) == 0xe000) {
                   8223:                        if(REG8(AH) == 0x10) {
                   8224:                                key_code = ((key_code >> 8) & 0xff) | ((key_code >> 16) & 0xff00);
                   8225:                        } else {
                   8226:                                key_code = ((key_code >> 16) & 0xff00);
                   8227:                        }
                   8228:                        key_recv >>= 16;
1.1       root     8229:                }
                   8230:        }
                   8231:        REG16(AX) = key_code & 0xffff;
                   8232:        key_code >>= 16;
1.1.1.33  root     8233:        key_recv >>= 16;
1.1.1.35  root     8234:        
                   8235: #ifdef USE_SERVICE_THREAD
                   8236:        service_exit = true;
                   8237: #endif
                   8238:        return(0);
                   8239: }
                   8240: 
                   8241: inline void pcbios_int_16h_00h()
                   8242: {
                   8243: #ifdef USE_SERVICE_THREAD
                   8244:        start_service_loop(pcbios_int_16h_00h_thread);
                   8245: #else
                   8246:        pcbios_int_16h_00h_thread(NULL);
                   8247:        REQUEST_HARDWRE_UPDATE();
                   8248: #endif
1.1       root     8249: }
                   8250: 
                   8251: inline void pcbios_int_16h_01h()
                   8252: {
1.1.1.33  root     8253:        if(key_recv == 0) {
                   8254:                pcbios_update_key_code(false);
1.1.1.5   root     8255:        }
1.1.1.33  root     8256:        if(key_recv != 0) {
                   8257:                UINT32 key_code_tmp = key_code;
                   8258:                if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
                   8259:                        if((key_code_tmp & 0xffff) == 0x0000 || (key_code_tmp & 0xffff) == 0xe000) {
                   8260:                                if(REG8(AH) == 0x11) {
                   8261:                                        key_code_tmp = ((key_code_tmp >> 8) & 0xff) | ((key_code_tmp >> 16) & 0xff00);
                   8262:                                } else {
                   8263:                                        key_code_tmp = ((key_code_tmp >> 16) & 0xff00);
                   8264:                                }
                   8265:                        }
1.1       root     8266:                }
1.1.1.5   root     8267:                REG16(AX) = key_code_tmp & 0xffff;
1.1.1.3   root     8268: #if defined(HAS_I386)
1.1.1.33  root     8269:                m_ZF = 0;
                   8270: #else
                   8271:                m_ZeroVal = 1;
                   8272: #endif
                   8273:        } else {
                   8274: #if defined(HAS_I386)
                   8275:                m_ZF = 1;
1.1.1.3   root     8276: #else
1.1.1.33  root     8277:                m_ZeroVal = 0;
1.1.1.3   root     8278: #endif
1.1.1.33  root     8279:        }
1.1       root     8280: }
                   8281: 
                   8282: inline void pcbios_int_16h_02h()
                   8283: {
                   8284:        REG8(AL)  = (GetAsyncKeyState(VK_INSERT ) & 0x0001) ? 0x80 : 0;
                   8285:        REG8(AL) |= (GetAsyncKeyState(VK_CAPITAL) & 0x0001) ? 0x40 : 0;
                   8286:        REG8(AL) |= (GetAsyncKeyState(VK_NUMLOCK) & 0x0001) ? 0x20 : 0;
                   8287:        REG8(AL) |= (GetAsyncKeyState(VK_SCROLL ) & 0x0001) ? 0x10 : 0;
                   8288:        REG8(AL) |= (GetAsyncKeyState(VK_MENU   ) & 0x8000) ? 0x08 : 0;
                   8289:        REG8(AL) |= (GetAsyncKeyState(VK_CONTROL) & 0x8000) ? 0x04 : 0;
                   8290:        REG8(AL) |= (GetAsyncKeyState(VK_LSHIFT ) & 0x8000) ? 0x02 : 0;
                   8291:        REG8(AL) |= (GetAsyncKeyState(VK_RSHIFT ) & 0x8000) ? 0x01 : 0;
                   8292: }
                   8293: 
                   8294: inline void pcbios_int_16h_03h()
                   8295: {
                   8296:        static UINT16 status = 0;
                   8297:        
                   8298:        switch(REG8(AL)) {
                   8299:        case 0x05:
                   8300:                status = REG16(BX);
                   8301:                break;
                   8302:        case 0x06:
                   8303:                REG16(BX) = status;
                   8304:                break;
                   8305:        default:
1.1.1.3   root     8306:                m_CF = 1;
1.1       root     8307:                break;
                   8308:        }
                   8309: }
                   8310: 
                   8311: inline void pcbios_int_16h_05h()
                   8312: {
1.1.1.32  root     8313:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8314: #ifdef USE_SERVICE_THREAD
                   8315:                EnterCriticalSection(&key_buf_crit_sect);
                   8316: #endif
1.1.1.32  root     8317:                key_buf_char->write(REG8(CL));
                   8318:                key_buf_scan->write(REG8(CH));
1.1.1.35  root     8319: #ifdef USE_SERVICE_THREAD
                   8320:                LeaveCriticalSection(&key_buf_crit_sect);
                   8321: #endif
1.1.1.32  root     8322:        }
1.1       root     8323:        REG8(AL) = 0x00;
                   8324: }
                   8325: 
                   8326: inline void pcbios_int_16h_12h()
                   8327: {
                   8328:        pcbios_int_16h_02h();
                   8329:        
                   8330:        REG8(AH)  = 0;//(GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x80 : 0;
                   8331:        REG8(AH) |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   8332:        REG8(AH) |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   8333:        REG8(AH) |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
                   8334:        REG8(AH) |= (GetAsyncKeyState(VK_RMENU   ) & 0x8000) ? 0x08 : 0;
                   8335:        REG8(AH) |= (GetAsyncKeyState(VK_RCONTROL) & 0x8000) ? 0x04 : 0;
                   8336:        REG8(AH) |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   8337:        REG8(AH) |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   8338: }
                   8339: 
                   8340: inline void pcbios_int_16h_13h()
                   8341: {
                   8342:        static UINT16 status = 0;
                   8343:        
                   8344:        switch(REG8(AL)) {
                   8345:        case 0x00:
                   8346:                status = REG16(DX);
                   8347:                break;
                   8348:        case 0x01:
                   8349:                REG16(DX) = status;
                   8350:                break;
                   8351:        default:
1.1.1.22  root     8352:                unimplemented_16h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x16, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.3   root     8353:                m_CF = 1;
1.1       root     8354:                break;
                   8355:        }
                   8356: }
                   8357: 
                   8358: inline void pcbios_int_16h_14h()
                   8359: {
                   8360:        static UINT8 status = 0;
                   8361:        
                   8362:        switch(REG8(AL)) {
                   8363:        case 0x00:
                   8364:        case 0x01:
                   8365:                status = REG8(AL);
                   8366:                break;
                   8367:        case 0x02:
                   8368:                REG8(AL) = status;
                   8369:                break;
                   8370:        default:
1.1.1.22  root     8371:                unimplemented_16h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x16, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.3   root     8372:                m_CF = 1;
1.1       root     8373:                break;
                   8374:        }
                   8375: }
                   8376: 
1.1.1.24  root     8377: inline void pcbios_int_16h_55h()
                   8378: {
                   8379:        switch(REG8(AL)) {
                   8380:        case 0x00:
                   8381:                // keyboard tsr is not present
                   8382:                break;
                   8383:        case 0xfe:
                   8384:                // handlers for INT 08, INT 09, INT 16, INT 1B, and INT 1C are installed
                   8385:                break;
                   8386:        case 0xff:
                   8387:                break;
                   8388:        default:
                   8389:                unimplemented_16h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x16, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   8390:                m_CF = 1;
                   8391:                break;
                   8392:        }
                   8393: }
                   8394: 
1.1.1.30  root     8395: inline void pcbios_int_16h_6fh()
                   8396: {
                   8397:        switch(REG8(AL)) {
                   8398:        case 0x00:
                   8399:                // HP Vectra EX-BIOS - "F16_INQUIRE" - Extended BIOS is not installed
                   8400:                break;
                   8401:        default:
                   8402:                unimplemented_16h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x16, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   8403:                m_CF = 1;
                   8404:                break;
                   8405:        }
                   8406: }
                   8407: 
1.1.1.37  root     8408: UINT16 pcbios_printer_jis2sjis(UINT16 jis)
                   8409: {
                   8410:        UINT8 hi = jis >> 8;
                   8411:        UINT8 lo = jis & 0xff;
                   8412:        
                   8413:        lo = (hi & 0x01) ? lo - 0x1f : lo + 0x7d;
                   8414:        hi = (hi - 0x21) / 2 + 0x81;
                   8415:        hi = (hi >= 0xa0) ? hi + 0x40 : hi;
                   8416:        lo = (lo >= 0x7f) ? lo + 0x01 : lo;
                   8417:        
                   8418:        return((hi << 8) + lo);
                   8419: }
                   8420: 
                   8421: UINT16 pcbios_printer_sjis2jis(UINT16 sjis)
                   8422: {
                   8423:        UINT8 hi = sjis >> 8;
                   8424:        UINT8 lo = sjis & 0xff;
                   8425:        
                   8426:        if(hi == 0x80 || (hi >= 0xeb && hi <= 0xef) || (hi >= 0xf4 && hi <= 0xff)) {
                   8427:                return(0x2121);
                   8428:        }
                   8429:        if((lo >= 0x00 && lo <= 0x3f) || lo == 0x7f || (lo >= 0xfd && lo <= 0xff)) {
                   8430:                return(0x2121);
                   8431:        }
                   8432:        if(hi >= 0xf0 && hi <= 0xf3) {
                   8433:                // gaiji
                   8434:                if(lo >= 0x40 && lo <= 0x7e) {
                   8435:                        return(0x7721 + lo - 0x40 + (hi - 0xf0) * 0x200);
                   8436:                }
                   8437:                if(lo >= 0x80 && lo <= 0x9e) {
                   8438:                        return(0x7760 + lo - 0x80 + (hi - 0xf0) * 0x200);
                   8439:                }
                   8440:                if(lo >= 0x9f && lo <= 0xfc) {
                   8441:                        return(0x7821 + lo - 0x9f + (hi - 0xf0) * 0x200);
                   8442:                }
                   8443:        }
                   8444:        hi = (hi >= 0xe0) ? hi - 0x40 : hi;
                   8445:        lo = (lo >= 0x80) ? lo - 0x01 : lo;
                   8446:        hi = ((lo >= 0x9e) ? 1 : 0) + (hi - 0x81) * 2 + 0x21;
                   8447:        lo = ((lo >= 0x9e) ? lo - 0x9e : lo - 0x40) + 0x21;
                   8448:        
                   8449:        return((hi << 8) + lo);
                   8450: }
                   8451: 
1.1.1.38  root     8452: // AX�e�N�j�J�����t�@�����X�K�C�h 1989
                   8453: // �t�^10 ���{��g��PRINTER DRIVER NIOS�T�d�l
                   8454: // 6. �R���g���[���R�[�h�̉��
1.1.1.37  root     8455: 
                   8456: void pcbios_printer_out(int c, UINT8 data)
                   8457: {
                   8458:        if(pio[c].conv_mode) {
                   8459:                if(pio[c].sjis_hi != 0) {
                   8460:                        if(!pio[c].jis_mode) {
                   8461:                                printer_out(c, 0x1c);
                   8462:                                printer_out(c, 0x26);
                   8463:                                pio[c].jis_mode = true;
                   8464:                        }
                   8465:                        UINT16 jis = pcbios_printer_sjis2jis((pio[c].sjis_hi << 8) | data);
                   8466:                        printer_out(c, jis >> 8);
                   8467:                        printer_out(c, jis & 0xff);
                   8468:                        pio[c].sjis_hi = 0;
                   8469:                } else if(pio[c].esc_buf[0] == 0x1b) {
                   8470:                        printer_out(c, data);
                   8471:                        if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
                   8472:                                pio[c].esc_buf[pio[c].esc_len] = data;
                   8473:                        }
                   8474:                        pio[c].esc_len++;
                   8475:                        
                   8476:                        switch(pio[c].esc_buf[1]) {
1.1.1.38  root     8477:                        case 0x33: // 1Bh 33h XX
                   8478:                        case 0x4a: // 1Bh 4Ah XX
                   8479:                        case 0x4e: // 1Bh 4Eh XX
                   8480:                        case 0x51: // 1Bh 51h XX
                   8481:                        case 0x55: // 1Bh 55h XX
                   8482:                        case 0x6c: // 1Bh 6Ch XX
                   8483:                        case 0x71: // 1Bh 71h XX
                   8484:                        case 0x72: // 1Bh 72h XX
1.1.1.37  root     8485:                                if(pio[c].esc_len == 3) {
                   8486:                                        pio[c].esc_buf[0] = 0x00;
                   8487:                                }
                   8488:                                break;
1.1.1.38  root     8489:                        case 0x24: // 1Bh 24h XX XX
                   8490:                        case 0x5c: // 1Bh 5Ch XX XX
1.1.1.37  root     8491:                                if(pio[c].esc_len == 4) {
                   8492:                                        pio[c].esc_buf[0] = 0x00;
                   8493:                                }
                   8494:                                break;
1.1.1.38  root     8495:                        case 0x2a: // 1Bh 2Ah XX XX XX data
1.1.1.37  root     8496:                                if(pio[c].esc_len >= 3) {
                   8497:                                        switch(pio[c].esc_buf[2]) {
                   8498:                                        case 0: case 1: case 2: case 3: case 4: case 6:
                   8499:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 1) {
                   8500:                                                        pio[c].esc_buf[0] = 0x00;
                   8501:                                                }
                   8502:                                                break;
                   8503:                                        case 32: case 33: case 38: case 39: case 40:
                   8504:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 3) {
                   8505:                                                        pio[c].esc_buf[0] = 0x00;
                   8506:                                                }
                   8507:                                                break;
1.1.1.38  root     8508:                                        case 71: case 72: case 73:
                   8509:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 6) {
                   8510:                                                        pio[c].esc_buf[0] = 0x00;
                   8511:                                                }
                   8512:                                                break;
1.1.1.37  root     8513:                                        default:
                   8514:                                                pio[c].esc_buf[0] = 0x00;
                   8515:                                                break;
                   8516:                                        }
                   8517:                                }
                   8518:                                break;
1.1.1.38  root     8519:                        case 0x40: // 1Bh 40h
1.1.1.37  root     8520:                                if(pio[c].jis_mode) {
                   8521:                                        printer_out(c, 0x1c);
                   8522:                                        printer_out(c, 0x2e);
                   8523:                                        pio[c].jis_mode = false;
                   8524:                                }
                   8525:                                pio[c].esc_buf[0] = 0x00;
                   8526:                                break;
1.1.1.38  root     8527:                        case 0x42: // 1Bh 42h data 00h
                   8528:                        case 0x44: // 1Bh 44h data 00h
1.1.1.37  root     8529:                                if(pio[c].esc_len >= 3 && data == 0) {
                   8530:                                        pio[c].esc_buf[0] = 0x00;
                   8531:                                }
                   8532:                                break;
1.1.1.38  root     8533:                        case 0x43: // 1Bh 43h (00h) XX
1.1.1.37  root     8534:                                if(pio[c].esc_len >= 3 && data != 0) {
                   8535:                                        pio[c].esc_buf[0] = 0x00;
                   8536:                                }
                   8537:                                break;
1.1.1.38  root     8538:                        default: // 1Bh XX
1.1.1.37  root     8539:                                pio[c].esc_buf[0] = 0x00;
                   8540:                                break;
                   8541:                        }
                   8542:                } else if(pio[c].esc_buf[0] == 0x1c) {
                   8543:                        printer_out(c, data);
                   8544:                        if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
                   8545:                                pio[c].esc_buf[pio[c].esc_len] = data;
                   8546:                        }
                   8547:                        pio[c].esc_len++;
                   8548:                        
                   8549:                        switch(pio[c].esc_buf[1]) {
1.1.1.38  root     8550:                        case 0x21: // 1Ch 21h XX
                   8551:                        case 0x2d: // 1Ch 2Dh XX
                   8552:                        case 0x57: // 1Ch 57h XX
                   8553:                        case 0x6b: // 1Ch 6Bh XX
                   8554:                        case 0x72: // 1Ch 72h XX
                   8555:                        case 0x78: // 1Ch 78h XX
1.1.1.37  root     8556:                                if(pio[c].esc_len == 3) {
                   8557:                                        pio[c].esc_buf[0] = 0x00;
                   8558:                                }
                   8559:                                break;
1.1.1.38  root     8560:                        case 0x26: // 1Ch 26h
1.1.1.37  root     8561:                                pio[c].jis_mode = true;
                   8562:                                pio[c].esc_buf[0] = 0x00;
                   8563:                                break;
1.1.1.38  root     8564:                        case 0x2e: // 1Ch 2Eh
1.1.1.37  root     8565:                                pio[c].jis_mode = false;
                   8566:                                pio[c].esc_buf[0] = 0x00;
                   8567:                                break;
1.1.1.38  root     8568:                        case 0x32: // 1Ch 32h XX XX data
1.1.1.37  root     8569:                                if(pio[c].esc_len == 76) {
                   8570:                                        pio[c].esc_buf[0] = 0x00;
                   8571:                                }
                   8572:                                break;
1.1.1.38  root     8573:                        case 0x44: // 1Bh 44h data 00h
1.1.1.37  root     8574:                                if(pio[c].esc_len == 6) {
                   8575:                                        pio[c].esc_buf[0] = 0x00;
                   8576:                                }
                   8577:                                break;
1.1.1.38  root     8578:                        case 0x53: // 1Ch 53h XX XX
                   8579:                        case 0x54: // 1Ch 54h XX XX
1.1.1.37  root     8580:                                if(pio[c].esc_len == 4) {
                   8581:                                        pio[c].esc_buf[0] = 0x00;
                   8582:                                }
                   8583:                                break;
1.1.1.38  root     8584:                        default: // 1Ch XX
1.1.1.37  root     8585:                                pio[c].esc_buf[0] = 0x00;
                   8586:                                break;
                   8587:                        }
                   8588:                } else if(data == 0x1b || data == 0x1c) {
                   8589:                        printer_out(c, data);
                   8590:                        pio[c].esc_buf[0] = data;
                   8591:                        pio[c].esc_len = 1;
                   8592:                } else if((data >= 0x80 && data <= 0x9f) || (data >= 0xe0 && data <= 0xff)) {
                   8593:                        pio[c].sjis_hi = data;
                   8594:                } else {
                   8595:                        if(pio[c].jis_mode) {
                   8596:                                printer_out(c, 0x1c);
                   8597:                                printer_out(c, 0x2e);
                   8598:                                pio[c].jis_mode = false;
                   8599:                        }
                   8600:                        printer_out(c, data);
                   8601:                }
                   8602:        } else {
                   8603:                if(pio[c].jis_mode) {
                   8604:                        printer_out(c, 0x1c);
                   8605:                        printer_out(c, 0x2e);
                   8606:                        pio[c].jis_mode = false;
                   8607:                }
                   8608:                printer_out(c, data);
                   8609:        }
                   8610: }
                   8611: 
                   8612: inline void pcbios_int_17h_00h()
                   8613: {
                   8614:        if(REG16(DX) < 3) {
                   8615:                pcbios_printer_out(REG16(DX), REG8(AL));
                   8616:                REG8(AH) = 0xd0;
                   8617:        }
                   8618: }
                   8619: 
                   8620: inline void pcbios_int_17h_01h()
                   8621: {
                   8622:        if(REG16(DX) < 3) {
                   8623:                REG8(AH) = 0xd0;
                   8624:        }
                   8625: }
                   8626: 
                   8627: inline void pcbios_int_17h_02h()
                   8628: {
                   8629:        if(REG16(DX) < 3) {
                   8630:                REG8(AH) = 0xd0;
                   8631:        }
                   8632: }
                   8633: 
                   8634: inline void pcbios_int_17h_03h()
                   8635: {
                   8636:        switch(REG8(AL)) {
                   8637:        case 0x00:
                   8638:                if(REG16(DX) < 3) {
                   8639:                        if(pio[REG16(DX)].jis_mode) {
                   8640:                                printer_out(REG16(DX), 0x1c);
                   8641:                                printer_out(REG16(DX), 0x2e);
                   8642:                                pio[REG16(DX)].jis_mode = false;
                   8643:                        }
                   8644:                        for(UINT16 i = 0; i < REG16(CX); i++) {
                   8645:                                printer_out(REG16(DX), mem[SREG_BASE(ES) + REG16(BX) + i]);
                   8646:                        }
                   8647:                        REG16(CX) = 0x0000;
                   8648:                        REG8(AH) = 0xd0;
                   8649:                }
                   8650:                break;
                   8651:        default:
                   8652:                unimplemented_17h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x17, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   8653:                break;
                   8654:        }
                   8655: }
                   8656: 
                   8657: inline void pcbios_int_17h_50h()
                   8658: {
                   8659:        switch(REG8(AL)) {
                   8660:        case 0x00:
                   8661:                if(REG16(DX) < 3) {
                   8662:                        if(REG16(BX) = 0x0001) {
                   8663:                                pio[REG16(DX)].conv_mode = false;
                   8664:                                REG8(AL) = 0x00;
                   8665:                        } else if(REG16(BX) = 0x0051) {
                   8666:                                pio[REG16(DX)].conv_mode = true;
                   8667:                                REG8(AL) = 0x00;
                   8668:                        } else {
                   8669:                                REG8(AL) = 0x01;
                   8670:                        }
                   8671:                } else {
                   8672:                        REG8(AL) = 0x02;
                   8673:                }
                   8674:                break;
                   8675:        case 0x01:
                   8676:                if(REG16(DX) < 3) {
                   8677:                        if(pio[REG16(DX)].conv_mode) {
                   8678:                                REG16(BX) = 0x0051;
                   8679:                        } else {
                   8680:                                REG16(BX) = 0x0001;
                   8681:                        }
                   8682:                        REG8(AL) = 0x00;
                   8683:                } else {
                   8684:                        REG8(AL) = 0x02;
                   8685:                }
                   8686:                break;
                   8687:        default:
                   8688:                unimplemented_17h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x17, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   8689:                break;
                   8690:        }
                   8691: }
                   8692: 
                   8693: inline void pcbios_int_17h_51h()
                   8694: {
                   8695:        if(REG8(DH) >= 0x21 && REG8(DH) <= 0x7e && REG8(DL) >= 0x21 && REG8(DL) <= 0x7e) {
                   8696:                REG16(DX) = pcbios_printer_jis2sjis(REG16(DX));
                   8697:        } else {
                   8698:                REG16(DX) = 0x0000;
                   8699:        }
                   8700: }
                   8701: 
                   8702: inline void pcbios_int_17h_52h()
                   8703: {
                   8704:        if(((REG8(DH) >= 0x81 && REG8(DH) <= 0x9f) || (REG8(DH) >= 0xe0 && REG8(DH) <= 0xfc)) && (REG8(DL) >= 0x40 && REG8(DL) <= 0xfc && REG8(DL) != 0x7f)) {
                   8705:                REG16(DX) = pcbios_printer_sjis2jis(REG16(DX));
                   8706:        } else {
                   8707:                REG16(DX) = 0x0000;
                   8708:        }
                   8709: }
                   8710: 
                   8711: inline void pcbios_int_17h_84h()
                   8712: {
                   8713:        if(REG16(DX) < 3) {
                   8714:                if(pio[REG16(DX)].jis_mode) {
                   8715:                        printer_out(REG16(DX), 0x1c);
                   8716:                        printer_out(REG16(DX), 0x2e);
                   8717:                        pio[REG16(DX)].jis_mode = false;
                   8718:                }
                   8719:                printer_out(REG16(DX), REG8(AL));
                   8720:                REG8(AH) = 0xd0;
                   8721:        }
                   8722: }
                   8723: 
                   8724: inline void pcbios_int_17h_85h()
                   8725: {
                   8726:        pio[0].conv_mode = (REG8(AL) == 0x00);
                   8727: }
                   8728: 
1.1       root     8729: inline void pcbios_int_1ah_00h()
                   8730: {
1.1.1.19  root     8731:        pcbios_update_daily_timer_counter(timeGetTime());
                   8732:        REG16(CX) = *(UINT16 *)(mem + 0x46e);
                   8733:        REG16(DX) = *(UINT16 *)(mem + 0x46c);
                   8734:        REG8(AL) = mem[0x470];
                   8735:        mem[0x470] = 0;
1.1       root     8736: }
                   8737: 
                   8738: inline int to_bcd(int t)
                   8739: {
                   8740:        int u = (t % 100) / 10;
                   8741:        return (u << 4) | (t % 10);
                   8742: }
                   8743: 
                   8744: inline void pcbios_int_1ah_02h()
                   8745: {
                   8746:        SYSTEMTIME time;
                   8747:        
                   8748:        GetLocalTime(&time);
                   8749:        REG8(CH) = to_bcd(time.wHour);
                   8750:        REG8(CL) = to_bcd(time.wMinute);
                   8751:        REG8(DH) = to_bcd(time.wSecond);
                   8752:        REG8(DL) = 0x00;
                   8753: }
                   8754: 
                   8755: inline void pcbios_int_1ah_04h()
                   8756: {
                   8757:        SYSTEMTIME time;
                   8758:        
                   8759:        GetLocalTime(&time);
                   8760:        REG8(CH) = to_bcd(time.wYear / 100);
                   8761:        REG8(CL) = to_bcd(time.wYear);
                   8762:        REG8(DH) = to_bcd(time.wMonth);
                   8763:        REG8(DL) = to_bcd(time.wDay);
                   8764: }
                   8765: 
                   8766: inline void pcbios_int_1ah_0ah()
                   8767: {
                   8768:        SYSTEMTIME time;
                   8769:        FILETIME file_time;
                   8770:        WORD dos_date, dos_time;
                   8771:        
                   8772:        GetLocalTime(&time);
                   8773:        SystemTimeToFileTime(&time, &file_time);
                   8774:        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   8775:        REG16(CX) = dos_date;
                   8776: }
                   8777: 
                   8778: // msdos system call
                   8779: 
1.1.1.43  root     8780: inline void msdos_int_21h_56h(int lfn);
                   8781: 
1.1       root     8782: inline void msdos_int_21h_00h()
                   8783: {
1.1.1.3   root     8784:        msdos_process_terminate(SREG(CS), retval, 1);
1.1       root     8785: }
                   8786: 
1.1.1.35  root     8787: DWORD WINAPI msdos_int_21h_01h_thread(LPVOID)
1.1       root     8788: {
                   8789:        REG8(AL) = msdos_getche();
1.1.1.33  root     8790:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     8791:        
1.1.1.35  root     8792: #ifdef USE_SERVICE_THREAD
                   8793:        service_exit = true;
                   8794: #endif
                   8795:        return(0);
                   8796: }
                   8797: 
                   8798: inline void msdos_int_21h_01h()
                   8799: {
                   8800: #ifdef USE_SERVICE_THREAD
                   8801:        start_service_loop(msdos_int_21h_01h_thread);
                   8802: #else
                   8803:        msdos_int_21h_01h_thread(NULL);
                   8804:        REQUEST_HARDWRE_UPDATE();
                   8805: #endif
1.1       root     8806: }
                   8807: 
                   8808: inline void msdos_int_21h_02h()
                   8809: {
1.1.1.33  root     8810:        UINT8 data = REG8(DL);
                   8811:        msdos_putch(data);
                   8812:        REG8(AL) = data;
                   8813:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     8814: }
                   8815: 
                   8816: inline void msdos_int_21h_03h()
                   8817: {
                   8818:        REG8(AL) = msdos_aux_in();
                   8819: }
                   8820: 
                   8821: inline void msdos_int_21h_04h()
                   8822: {
                   8823:        msdos_aux_out(REG8(DL));
                   8824: }
                   8825: 
                   8826: inline void msdos_int_21h_05h()
                   8827: {
                   8828:        msdos_prn_out(REG8(DL));
                   8829: }
                   8830: 
                   8831: inline void msdos_int_21h_06h()
                   8832: {
                   8833:        if(REG8(DL) == 0xff) {
                   8834:                if(msdos_kbhit()) {
                   8835:                        REG8(AL) = msdos_getch();
1.1.1.3   root     8836: #if defined(HAS_I386)
                   8837:                        m_ZF = 0;
                   8838: #else
                   8839:                        m_ZeroVal = 1;
                   8840: #endif
1.1       root     8841:                } else {
                   8842:                        REG8(AL) = 0;
1.1.1.3   root     8843: #if defined(HAS_I386)
                   8844:                        m_ZF = 1;
                   8845: #else
                   8846:                        m_ZeroVal = 0;
                   8847: #endif
1.1.1.14  root     8848:                        maybe_idle();
1.1       root     8849:                }
                   8850:        } else {
1.1.1.33  root     8851:                UINT8 data = REG8(DL);
                   8852:                msdos_putch(data);
                   8853:                REG8(AL) = data;
1.1       root     8854:        }
                   8855: }
                   8856: 
1.1.1.35  root     8857: DWORD WINAPI msdos_int_21h_07h_thread(LPVOID)
1.1       root     8858: {
                   8859:        REG8(AL) = msdos_getch();
1.1.1.26  root     8860:        
1.1.1.35  root     8861: #ifdef USE_SERVICE_THREAD
                   8862:        service_exit = true;
                   8863: #endif
                   8864:        return(0);
1.1       root     8865: }
                   8866: 
1.1.1.35  root     8867: inline void msdos_int_21h_07h()
                   8868: {
                   8869: #ifdef USE_SERVICE_THREAD
                   8870:        start_service_loop(msdos_int_21h_07h_thread);
                   8871: #else
                   8872:        msdos_int_21h_07h_thread(NULL);
                   8873:        REQUEST_HARDWRE_UPDATE();
                   8874: #endif
                   8875: }
                   8876: 
                   8877: DWORD WINAPI msdos_int_21h_08h_thread(LPVOID)
1.1       root     8878: {
                   8879:        REG8(AL) = msdos_getch();
1.1.1.33  root     8880:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     8881:        
1.1.1.35  root     8882: #ifdef USE_SERVICE_THREAD
                   8883:        service_exit = true;
                   8884: #endif
                   8885:        return(0);
                   8886: }
                   8887: 
                   8888: inline void msdos_int_21h_08h()
                   8889: {
                   8890: #ifdef USE_SERVICE_THREAD
                   8891:        start_service_loop(msdos_int_21h_08h_thread);
                   8892: #else
                   8893:        msdos_int_21h_08h_thread(NULL);
                   8894:        REQUEST_HARDWRE_UPDATE();
                   8895: #endif
1.1       root     8896: }
                   8897: 
                   8898: inline void msdos_int_21h_09h()
                   8899: {
1.1.1.21  root     8900:        msdos_stdio_reopen();
                   8901:        
1.1.1.20  root     8902:        process_t *process = msdos_process_info_get(current_psp);
                   8903:        int fd = msdos_psp_get_file_table(1, current_psp);
                   8904:        
1.1.1.14  root     8905:        char *str = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   8906:        int len = 0;
1.1       root     8907:        
1.1.1.14  root     8908:        while(str[len] != '$' && len < 0x10000) {
                   8909:                len++;
                   8910:        }
1.1.1.20  root     8911:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     8912:                // stdout is redirected to file
1.1.1.20  root     8913:                msdos_write(fd, str, len);
1.1       root     8914:        } else {
                   8915:                for(int i = 0; i < len; i++) {
1.1.1.14  root     8916:                        msdos_putch(str[i]);
1.1       root     8917:                }
                   8918:        }
1.1.1.33  root     8919:        REG8(AL) = '$';
                   8920:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     8921: }
                   8922: 
1.1.1.35  root     8923: DWORD WINAPI msdos_int_21h_0ah_thread(LPVOID)
1.1       root     8924: {
1.1.1.3   root     8925:        int ofs = SREG_BASE(DS) + REG16(DX);
1.1       root     8926:        int max = mem[ofs] - 1;
                   8927:        UINT8 *buf = mem + ofs + 2;
                   8928:        int chr, p = 0;
                   8929:        
                   8930:        while((chr = msdos_getch()) != 0x0d) {
1.1.1.33  root     8931:                if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
1.1.1.26  root     8932:                        p = 0;
1.1.1.33  root     8933:                        msdos_putch(0x03);
                   8934:                        msdos_putch(0x0d);
                   8935:                        msdos_putch(0x0a);
1.1.1.26  root     8936:                        break;
1.1.1.33  root     8937:                } else if(ctrl_break_pressed) {
                   8938:                        // skip this byte
1.1.1.26  root     8939:                } else if(chr == 0x00) {
1.1       root     8940:                        // skip 2nd byte
                   8941:                        msdos_getch();
                   8942:                } else if(chr == 0x08) {
                   8943:                        // back space
                   8944:                        if(p > 0) {
                   8945:                                p--;
1.1.1.20  root     8946:                                if(msdos_ctrl_code_check(buf[p])) {
1.1.1.34  root     8947:                                        msdos_putch(0x08);
                   8948:                                        msdos_putch(0x08);
                   8949:                                        msdos_putch(0x20);
                   8950:                                        msdos_putch(0x20);
                   8951:                                        msdos_putch(0x08);
                   8952:                                        msdos_putch(0x08);
1.1.1.36  root     8953:                                } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
                   8954:                                        p--;
                   8955:                                        msdos_putch(0x08);
                   8956:                                        msdos_putch(0x08);
                   8957:                                        msdos_putch(0x20);
                   8958:                                        msdos_putch(0x20);
                   8959:                                        msdos_putch(0x08);
                   8960:                                        msdos_putch(0x08);
1.1.1.34  root     8961:                                } else {
                   8962:                                        msdos_putch(0x08);
                   8963:                                        msdos_putch(0x20);
                   8964:                                        msdos_putch(0x08);
                   8965:                                }
                   8966:                        }
                   8967:                } else if(chr == 0x1b) {
                   8968:                        // escape
                   8969:                        while(p > 0) {
                   8970:                                p--;
                   8971:                                if(msdos_ctrl_code_check(buf[p])) {
                   8972:                                        msdos_putch(0x08);
                   8973:                                        msdos_putch(0x08);
                   8974:                                        msdos_putch(0x20);
                   8975:                                        msdos_putch(0x20);
                   8976:                                        msdos_putch(0x08);
                   8977:                                        msdos_putch(0x08);
1.1.1.20  root     8978:                                } else {
1.1.1.34  root     8979:                                        msdos_putch(0x08);
                   8980:                                        msdos_putch(0x20);
                   8981:                                        msdos_putch(0x08);
1.1.1.20  root     8982:                                }
1.1       root     8983:                        }
                   8984:                } else if(p < max) {
                   8985:                        buf[p++] = chr;
                   8986:                        msdos_putch(chr);
                   8987:                }
                   8988:        }
                   8989:        buf[p] = 0x0d;
                   8990:        mem[ofs + 1] = p;
1.1.1.33  root     8991:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     8992:        
1.1.1.35  root     8993: #ifdef USE_SERVICE_THREAD
                   8994:        service_exit = true;
                   8995: #endif
                   8996:        return(0);
                   8997: }
                   8998: 
                   8999: inline void msdos_int_21h_0ah()
                   9000: {
                   9001:        if(mem[SREG_BASE(DS) + REG16(DX)] != 0x00) {
                   9002: #ifdef USE_SERVICE_THREAD
                   9003:                start_service_loop(msdos_int_21h_0ah_thread);
                   9004: #else
                   9005:                msdos_int_21h_0ah_thread(NULL);
                   9006:                REQUEST_HARDWRE_UPDATE();
                   9007: #endif
                   9008:        }
1.1       root     9009: }
                   9010: 
                   9011: inline void msdos_int_21h_0bh()
                   9012: {
                   9013:        if(msdos_kbhit()) {
                   9014:                REG8(AL) = 0xff;
                   9015:        } else {
                   9016:                REG8(AL) = 0x00;
1.1.1.14  root     9017:                maybe_idle();
1.1       root     9018:        }
1.1.1.33  root     9019:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     9020: }
                   9021: 
                   9022: inline void msdos_int_21h_0ch()
                   9023: {
                   9024:        // clear key buffer
1.1.1.21  root     9025:        msdos_stdio_reopen();
                   9026:        
1.1.1.20  root     9027:        process_t *process = msdos_process_info_get(current_psp);
                   9028:        int fd = msdos_psp_get_file_table(0, current_psp);
                   9029:        
                   9030:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     9031:                // stdin is redirected to file
                   9032:        } else {
                   9033:                while(msdos_kbhit()) {
                   9034:                        msdos_getch();
                   9035:                }
                   9036:        }
                   9037:        
                   9038:        switch(REG8(AL)) {
                   9039:        case 0x01:
                   9040:                msdos_int_21h_01h();
                   9041:                break;
                   9042:        case 0x06:
                   9043:                msdos_int_21h_06h();
                   9044:                break;
                   9045:        case 0x07:
                   9046:                msdos_int_21h_07h();
                   9047:                break;
                   9048:        case 0x08:
                   9049:                msdos_int_21h_08h();
                   9050:                break;
                   9051:        case 0x0a:
                   9052:                msdos_int_21h_0ah();
                   9053:                break;
                   9054:        default:
1.1.1.22  root     9055: //             unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   9056: //             REG16(AX) = 0x01;
                   9057: //             m_CF = 1;
1.1       root     9058:                break;
                   9059:        }
                   9060: }
                   9061: 
                   9062: inline void msdos_int_21h_0dh()
                   9063: {
                   9064: }
                   9065: 
                   9066: inline void msdos_int_21h_0eh()
                   9067: {
                   9068:        if(REG8(DL) < 26) {
                   9069:                _chdrive(REG8(DL) + 1);
                   9070:                msdos_cds_update(REG8(DL));
1.1.1.23  root     9071:                msdos_sda_update(current_psp);
1.1       root     9072:        }
                   9073:        REG8(AL) = 26; // zdrive
                   9074: }
                   9075: 
1.1.1.14  root     9076: inline void msdos_int_21h_0fh()
                   9077: {
                   9078:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9079:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     9080:        const char *path = msdos_fcb_path(fcb);
1.1.1.14  root     9081:        HANDLE hFile = CreateFile(path, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.16  root     9082:        
1.1.1.14  root     9083:        if(hFile == INVALID_HANDLE_VALUE) {
                   9084:                REG8(AL) = 0xff;
                   9085:        } else {
                   9086:                REG8(AL) = 0;
                   9087:                fcb->current_block = 0;
                   9088:                fcb->record_size = 128;
                   9089:                fcb->file_size = GetFileSize(hFile, NULL);
                   9090:                fcb->handle = hFile;
                   9091:                fcb->cur_record = 0;
                   9092:        }
                   9093: }
                   9094: 
                   9095: inline void msdos_int_21h_10h()
                   9096: {
                   9097:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9098:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9099:        
                   9100:        REG8(AL) = CloseHandle(fcb->handle) ? 0 : 0xff;
                   9101: }
                   9102: 
1.1       root     9103: inline void msdos_int_21h_11h()
                   9104: {
1.1.1.3   root     9105:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9106:        fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9107:        
                   9108:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     9109:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9110:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   9111:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1.1.45  root     9112:        const char *path = msdos_fcb_path(fcb);
1.1       root     9113:        WIN32_FIND_DATA fd;
                   9114:        
1.1.1.13  root     9115:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   9116:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   9117:                FindClose(dtainfo->find_handle);
                   9118:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9119:        }
                   9120:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     9121:        dtainfo->allowable_mask = (ext_fcb->flag == 0xff) ? ext_fcb->attribute : 0x20;
                   9122:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     9123:        
1.1.1.14  root     9124:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   9125:                dtainfo->allowable_mask &= ~8;
1.1       root     9126:        }
1.1.1.14  root     9127:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   9128:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     9129:                      !msdos_find_file_has_8dot3name(&fd)) {
                   9130:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   9131:                                FindClose(dtainfo->find_handle);
                   9132:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9133:                                break;
                   9134:                        }
                   9135:                }
                   9136:        }
1.1.1.13  root     9137:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     9138:                if(ext_fcb->flag == 0xff) {
                   9139:                        ext_find->flag = 0xff;
                   9140:                        memset(ext_find->reserved, 0, 5);
                   9141:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9142:                }
                   9143:                find->drive = _getdrive();
1.1.1.13  root     9144:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     9145:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9146:                find->nt_res = 0;
                   9147:                msdos_find_file_conv_local_time(&fd);
                   9148:                find->create_time_ms = 0;
                   9149:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9150:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9151:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9152:                find->cluster_hi = find->cluster_lo = 0;
                   9153:                find->file_size = fd.nFileSizeLow;
                   9154:                REG8(AL) = 0x00;
1.1.1.14  root     9155:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     9156:                if(ext_fcb->flag == 0xff) {
                   9157:                        ext_find->flag = 0xff;
                   9158:                        memset(ext_find->reserved, 0, 5);
                   9159:                        ext_find->attribute = 8;
                   9160:                }
                   9161:                find->drive = _getdrive();
                   9162:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   9163:                find->attribute = 8;
                   9164:                find->nt_res = 0;
                   9165:                msdos_find_file_conv_local_time(&fd);
                   9166:                find->create_time_ms = 0;
                   9167:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9168:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9169:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9170:                find->cluster_hi = find->cluster_lo = 0;
                   9171:                find->file_size = 0;
1.1.1.14  root     9172:                dtainfo->allowable_mask &= ~8;
1.1       root     9173:                REG8(AL) = 0x00;
                   9174:        } else {
                   9175:                REG8(AL) = 0xff;
                   9176:        }
                   9177: }
                   9178: 
                   9179: inline void msdos_int_21h_12h()
                   9180: {
1.1.1.3   root     9181:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1.1.14  root     9182: //     fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9183:        
                   9184:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     9185:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9186:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   9187:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9188:        WIN32_FIND_DATA fd;
                   9189:        
1.1.1.13  root     9190:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   9191:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   9192:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     9193:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     9194:                              !msdos_find_file_has_8dot3name(&fd)) {
                   9195:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   9196:                                        FindClose(dtainfo->find_handle);
                   9197:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9198:                                        break;
                   9199:                                }
                   9200:                        }
                   9201:                } else {
1.1.1.13  root     9202:                        FindClose(dtainfo->find_handle);
                   9203:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9204:                }
                   9205:        }
1.1.1.13  root     9206:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     9207:                if(ext_fcb->flag == 0xff) {
                   9208:                        ext_find->flag = 0xff;
                   9209:                        memset(ext_find->reserved, 0, 5);
                   9210:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9211:                }
                   9212:                find->drive = _getdrive();
1.1.1.13  root     9213:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     9214:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9215:                find->nt_res = 0;
                   9216:                msdos_find_file_conv_local_time(&fd);
                   9217:                find->create_time_ms = 0;
                   9218:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9219:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9220:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9221:                find->cluster_hi = find->cluster_lo = 0;
                   9222:                find->file_size = fd.nFileSizeLow;
                   9223:                REG8(AL) = 0x00;
1.1.1.14  root     9224:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     9225:                if(ext_fcb->flag == 0xff) {
                   9226:                        ext_find->flag = 0xff;
                   9227:                        memset(ext_find->reserved, 0, 5);
                   9228:                        ext_find->attribute = 8;
                   9229:                }
                   9230:                find->drive = _getdrive();
                   9231:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   9232:                find->attribute = 8;
                   9233:                find->nt_res = 0;
                   9234:                msdos_find_file_conv_local_time(&fd);
                   9235:                find->create_time_ms = 0;
                   9236:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9237:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9238:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9239:                find->cluster_hi = find->cluster_lo = 0;
                   9240:                find->file_size = 0;
1.1.1.14  root     9241:                dtainfo->allowable_mask &= ~8;
1.1       root     9242:                REG8(AL) = 0x00;
                   9243:        } else {
                   9244:                REG8(AL) = 0xff;
                   9245:        }
                   9246: }
                   9247: 
                   9248: inline void msdos_int_21h_13h()
                   9249: {
1.1.1.3   root     9250:        if(remove(msdos_fcb_path((fcb_t *)(mem + SREG_BASE(DS) + REG16(DX))))) {
1.1       root     9251:                REG8(AL) = 0xff;
                   9252:        } else {
                   9253:                REG8(AL) = 0x00;
                   9254:        }
                   9255: }
                   9256: 
1.1.1.16  root     9257: inline void msdos_int_21h_14h()
                   9258: {
                   9259:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9260:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9261:        process_t *process = msdos_process_info_get(current_psp);
                   9262:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9263:        DWORD num = 0;
                   9264:        
                   9265:        memset(mem + dta_laddr, 0, fcb->record_size);
                   9266:        if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   9267:                REG8(AL) = 1;
                   9268:        } else {
                   9269:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   9270:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   9271:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   9272:                REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   9273:        }
                   9274: }
                   9275: 
                   9276: inline void msdos_int_21h_15h()
1.1.1.14  root     9277: {
                   9278:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9279:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.16  root     9280:        process_t *process = msdos_process_info_get(current_psp);
                   9281:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9282:        DWORD num = 0;
1.1.1.14  root     9283:        
1.1.1.16  root     9284:        if(!WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   9285:                REG8(AL) = 1;
                   9286:        } else {
                   9287:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   9288:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   9289:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   9290:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   9291:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   9292:        }
                   9293: }
                   9294: 
                   9295: inline void msdos_int_21h_16h()
                   9296: {
                   9297:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9298:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     9299:        const char *path = msdos_fcb_path(fcb);
1.1.1.14  root     9300:        HANDLE hFile = CreateFile(path, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, CREATE_ALWAYS, ext_fcb->flag == 0xff ? ext_fcb->attribute : FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.16  root     9301:        
1.1.1.14  root     9302:        if(hFile == INVALID_HANDLE_VALUE) {
                   9303:                REG8(AL) = 0xff;
                   9304:        } else {
                   9305:                REG8(AL) = 0;
                   9306:                fcb->current_block = 0;
                   9307:                fcb->record_size = 128;
                   9308:                fcb->file_size = 0;
                   9309:                fcb->handle = hFile;
                   9310:                fcb->cur_record = 0;
                   9311:        }
                   9312: }
                   9313: 
1.1.1.16  root     9314: inline void msdos_int_21h_17h()
                   9315: {
                   9316:        ext_fcb_t *ext_fcb_src = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9317:        fcb_t *fcb_src = (fcb_t *)(ext_fcb_src + (ext_fcb_src->flag == 0xff ? 1 : 0));
1.1.1.45  root     9318: //     const char *path_src = msdos_fcb_path(fcb_src);
                   9319:        char path_src[MAX_PATH];
                   9320:        strcpy(path_src, msdos_fcb_path(fcb_src));
                   9321:        
1.1.1.16  root     9322:        ext_fcb_t *ext_fcb_dst = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + 16);
                   9323:        fcb_t *fcb_dst = (fcb_t *)(ext_fcb_dst + (ext_fcb_dst->flag == 0xff ? 1 : 0));
1.1.1.45  root     9324: //     const char *path_dst = msdos_fcb_path(fcb_dst);
                   9325:        char path_dst[MAX_PATH];
                   9326:        strcpy(path_dst, msdos_fcb_path(fcb_dst));
1.1.1.16  root     9327:        
                   9328:        if(rename(path_src, path_dst)) {
                   9329:                REG8(AL) = 0xff;
                   9330:        } else {
                   9331:                REG8(AL) = 0;
                   9332:        }
                   9333: }
                   9334: 
1.1       root     9335: inline void msdos_int_21h_18h()
                   9336: {
                   9337:        REG8(AL) = 0x00;
                   9338: }
                   9339: 
                   9340: inline void msdos_int_21h_19h()
                   9341: {
                   9342:        REG8(AL) = _getdrive() - 1;
                   9343: }
                   9344: 
                   9345: inline void msdos_int_21h_1ah()
                   9346: {
                   9347:        process_t *process = msdos_process_info_get(current_psp);
                   9348:        
                   9349:        process->dta.w.l = REG16(DX);
1.1.1.3   root     9350:        process->dta.w.h = SREG(DS);
1.1.1.23  root     9351:        msdos_sda_update(current_psp);
1.1       root     9352: }
                   9353: 
                   9354: inline void msdos_int_21h_1bh()
                   9355: {
                   9356:        int drive_num = _getdrive() - 1;
                   9357:        UINT16 seg, ofs;
                   9358:        
                   9359:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9360:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   9361:                REG8(AL) = dpb->highest_sector_num + 1;
                   9362:                REG16(CX) = dpb->bytes_per_sector;
                   9363:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     9364:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     9365:        } else {
                   9366:                REG8(AL) = 0xff;
1.1.1.3   root     9367:                m_CF = 1;
1.1       root     9368:        }
                   9369: 
                   9370: }
                   9371: 
                   9372: inline void msdos_int_21h_1ch()
                   9373: {
1.1.1.41  root     9374:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
1.1       root     9375:        UINT16 seg, ofs;
                   9376:        
                   9377:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9378:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   9379:                REG8(AL) = dpb->highest_sector_num + 1;
                   9380:                REG16(CX) = dpb->bytes_per_sector;
                   9381:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     9382:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     9383:        } else {
                   9384:                REG8(AL) = 0xff;
1.1.1.3   root     9385:                m_CF = 1;
1.1       root     9386:        }
                   9387: 
                   9388: }
                   9389: 
                   9390: inline void msdos_int_21h_1dh()
                   9391: {
                   9392:        REG8(AL) = 0;
                   9393: }
                   9394: 
                   9395: inline void msdos_int_21h_1eh()
                   9396: {
                   9397:        REG8(AL) = 0;
                   9398: }
                   9399: 
                   9400: inline void msdos_int_21h_1fh()
                   9401: {
                   9402:        int drive_num = _getdrive() - 1;
                   9403:        UINT16 seg, ofs;
                   9404:        
                   9405:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9406:                REG8(AL) = 0;
1.1.1.3   root     9407:                SREG(DS) = seg;
                   9408:                i386_load_segment_descriptor(DS);
1.1       root     9409:                REG16(BX) = ofs;
                   9410:        } else {
                   9411:                REG8(AL) = 0xff;
1.1.1.3   root     9412:                m_CF = 1;
1.1       root     9413:        }
                   9414: }
                   9415: 
                   9416: inline void msdos_int_21h_20h()
                   9417: {
                   9418:        REG8(AL) = 0;
                   9419: }
                   9420: 
1.1.1.14  root     9421: inline void msdos_int_21h_21h()
                   9422: {
                   9423:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9424:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9425:        
                   9426:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   9427:                REG8(AL) = 1;
                   9428:        } else {
                   9429:                process_t *process = msdos_process_info_get(current_psp);
                   9430:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9431:                memset(mem + dta_laddr, 0, fcb->record_size);
1.1.1.16  root     9432:                DWORD num = 0;
1.1.1.14  root     9433:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   9434:                        REG8(AL) = 1;
                   9435:                } else {
                   9436:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   9437:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     9438:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
1.1.1.14  root     9439:                }
                   9440:        }
                   9441: }
                   9442: 
                   9443: inline void msdos_int_21h_22h()
                   9444: {
                   9445:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9446:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9447:        
                   9448:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   9449:                REG8(AL) = 0xff;
                   9450:        } else {
                   9451:                process_t *process = msdos_process_info_get(current_psp);
                   9452:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
1.1.1.16  root     9453:                DWORD num = 0;
1.1.1.14  root     9454:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL);
                   9455:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   9456:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   9457:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     9458:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
1.1.1.14  root     9459:        }
                   9460: }
                   9461: 
1.1.1.16  root     9462: inline void msdos_int_21h_23h()
                   9463: {
                   9464:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9465:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     9466:        const char *path = msdos_fcb_path(fcb);
1.1.1.16  root     9467:        HANDLE hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   9468:        
                   9469:        if(hFile == INVALID_HANDLE_VALUE) {
                   9470:                REG8(AL) = 0xff;
                   9471:        } else {
                   9472:                UINT32 size = GetFileSize(hFile, NULL);
                   9473:                fcb->rand_record = size / fcb->record_size + ((size % fcb->record_size) != 0);
                   9474:                REG8(AL) = 0;
                   9475:        }
                   9476: }
                   9477: 
                   9478: inline void msdos_int_21h_24h()
                   9479: {
                   9480:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9481:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9482:        
                   9483:        fcb->rand_record = fcb->current_block * fcb->record_size + fcb->cur_record;
                   9484: }
                   9485: 
1.1       root     9486: inline void msdos_int_21h_25h()
                   9487: {
                   9488:        *(UINT16 *)(mem + 4 * REG8(AL) + 0) = REG16(DX);
1.1.1.3   root     9489:        *(UINT16 *)(mem + 4 * REG8(AL) + 2) = SREG(DS);
1.1       root     9490: }
                   9491: 
                   9492: inline void msdos_int_21h_26h()
                   9493: {
                   9494:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   9495:        
                   9496:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
                   9497:        psp->first_mcb = REG16(DX) + 16;
                   9498:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   9499:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   9500:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   9501:        psp->parent_psp = 0;
                   9502: }
                   9503: 
1.1.1.16  root     9504: inline void msdos_int_21h_27h()
                   9505: {
                   9506:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9507:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9508:        
                   9509:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   9510:                REG8(AL) = 1;
                   9511:        } else {
                   9512:                process_t *process = msdos_process_info_get(current_psp);
                   9513:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9514:                memset(mem + dta_laddr, 0, fcb->record_size * REG16(CX));
                   9515:                DWORD num = 0;
                   9516:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL) || num == 0) {
                   9517:                        REG8(AL) = 1;
                   9518:                } else {
                   9519:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   9520:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   9521:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   9522:                        REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   9523:                }
                   9524:        }
                   9525: }
                   9526: 
                   9527: inline void msdos_int_21h_28h()
                   9528: {
                   9529:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9530:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9531:        
                   9532:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   9533:                REG8(AL) = 0xff;
                   9534:        } else {
                   9535:                process_t *process = msdos_process_info_get(current_psp);
                   9536:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9537:                DWORD num = 0;
                   9538:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL);
                   9539:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   9540:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   9541:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   9542:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   9543:                REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   9544:        }
                   9545: }
                   9546: 
1.1       root     9547: inline void msdos_int_21h_29h()
                   9548: {
1.1.1.20  root     9549:        int ofs = 0;//SREG_BASE(DS) + REG16(SI);
                   9550:        char buffer[1024], name[MAX_PATH], ext[MAX_PATH];
1.1       root     9551:        UINT8 drv = 0;
                   9552:        char sep_chars[] = ":.;,=+";
                   9553:        char end_chars[] = "\\<>|/\"[]";
                   9554:        char spc_chars[] = " \t";
                   9555:        
1.1.1.20  root     9556:        memcpy(buffer, mem + SREG_BASE(DS) + REG16(SI), 1023);
                   9557:        buffer[1023] = 0;
                   9558:        memset(name, 0x20, sizeof(name));
                   9559:        memset(ext, 0x20, sizeof(ext));
                   9560:        
1.1       root     9561:        if(REG8(AL) & 1) {
1.1.1.20  root     9562:                ofs += strspn((char *)(buffer + ofs), spc_chars);
                   9563:                if(my_strchr(sep_chars, buffer[ofs]) && buffer[ofs] != '\0') {
1.1       root     9564:                        ofs++;
                   9565:                }
                   9566:        }
1.1.1.20  root     9567:        ofs += strspn((char *)(buffer + ofs), spc_chars);
1.1       root     9568:        
1.1.1.24  root     9569:        if(buffer[ofs + 1] == ':') {
                   9570:                if(buffer[ofs] >= 'a' && buffer[ofs] <= 'z') {
                   9571:                        drv = buffer[ofs] - 'a' + 1;
1.1.1.20  root     9572:                        ofs += 2;
1.1.1.24  root     9573:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   9574:                                ofs++;
                   9575:                        }
                   9576:                } else if(buffer[ofs] >= 'A' && buffer[ofs] <= 'Z') {
                   9577:                        drv = buffer[ofs] - 'A' + 1;
1.1       root     9578:                        ofs += 2;
1.1.1.24  root     9579:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   9580:                                ofs++;
                   9581:                        }
1.1       root     9582:                }
                   9583:        }
1.1.1.20  root     9584:        for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   9585:                UINT8 c = buffer[ofs];
                   9586:                if(is_kanji) {
                   9587:                        is_kanji = 0;
                   9588:                } else if(msdos_lead_byte_check(c)) {
                   9589:                        is_kanji = 1;
                   9590:                } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     9591:                        break;
                   9592:                } else if(c >= 'a' && c <= 'z') {
                   9593:                        c -= 0x20;
                   9594:                }
                   9595:                ofs++;
                   9596:                name[i] = c;
                   9597:        }
1.1.1.20  root     9598:        if(buffer[ofs] == '.') {
1.1       root     9599:                ofs++;
1.1.1.20  root     9600:                for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   9601:                        UINT8 c = buffer[ofs];
                   9602:                        if(is_kanji) {
                   9603:                                is_kanji = 0;
                   9604:                        } else if(msdos_lead_byte_check(c)) {
                   9605:                                is_kanji = 1;
                   9606:                        } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     9607:                                break;
                   9608:                        } else if(c >= 'a' && c <= 'z') {
                   9609:                                c -= 0x20;
                   9610:                        }
                   9611:                        ofs++;
                   9612:                        ext[i] = c;
                   9613:                }
                   9614:        }
1.1.1.20  root     9615:        int si = REG16(SI) + ofs;
1.1.1.3   root     9616:        int ds = SREG(DS);
1.1       root     9617:        while(si > 0xffff) {
                   9618:                si -= 0x10;
                   9619:                ds++;
                   9620:        }
                   9621:        REG16(SI) = si;
1.1.1.3   root     9622:        SREG(DS) = ds;
                   9623:        i386_load_segment_descriptor(DS);
1.1       root     9624:        
1.1.1.3   root     9625:        UINT8 *fcb = mem + SREG_BASE(ES) + REG16(DI);
1.1.1.20  root     9626:        if(!(REG8(AL) & 2) || drv != 0) {
                   9627:                fcb[0] = drv;
                   9628:        }
                   9629:        if(!(REG8(AL) & 4) || name[0] != 0x20) {
                   9630:                memcpy(fcb + 1, name, 8);
                   9631:        }
                   9632:        if(!(REG8(AL) & 8) || ext[0] != 0x20) {
                   9633:                memcpy(fcb + 9, ext, 3);
                   9634:        }
                   9635:        for(int i = 1, found_star = 0; i < 1 + 8; i++) {
1.1       root     9636:                if(fcb[i] == '*') {
                   9637:                        found_star = 1;
                   9638:                }
                   9639:                if(found_star) {
                   9640:                        fcb[i] = '?';
                   9641:                }
                   9642:        }
1.1.1.20  root     9643:        for(int i = 9, found_star = 0; i < 9 + 3; i++) {
1.1       root     9644:                if(fcb[i] == '*') {
                   9645:                        found_star = 1;
                   9646:                }
                   9647:                if(found_star) {
                   9648:                        fcb[i] = '?';
                   9649:                }
                   9650:        }
                   9651:        
1.1.1.44  root     9652:        if(drv == 0 || msdos_is_valid_drive(drv - 1)) {
1.1       root     9653:                if(memchr(fcb + 1, '?', 8 + 3)) {
                   9654:                        REG8(AL) = 0x01;
1.1.1.20  root     9655:                } else {
                   9656:                        REG8(AL) = 0x00;
1.1       root     9657:                }
                   9658:        } else {
                   9659:                REG8(AL) = 0xff;
                   9660:        }
                   9661: }
                   9662: 
                   9663: inline void msdos_int_21h_2ah()
                   9664: {
                   9665:        SYSTEMTIME sTime;
                   9666:        
                   9667:        GetLocalTime(&sTime);
                   9668:        REG16(CX) = sTime.wYear;
                   9669:        REG8(DH) = (UINT8)sTime.wMonth;
                   9670:        REG8(DL) = (UINT8)sTime.wDay;
                   9671:        REG8(AL) = (UINT8)sTime.wDayOfWeek;
                   9672: }
                   9673: 
                   9674: inline void msdos_int_21h_2bh()
                   9675: {
1.1.1.14  root     9676:        REG8(AL) = 0xff;
1.1       root     9677: }
                   9678: 
                   9679: inline void msdos_int_21h_2ch()
                   9680: {
                   9681:        SYSTEMTIME sTime;
                   9682:        
                   9683:        GetLocalTime(&sTime);
                   9684:        REG8(CH) = (UINT8)sTime.wHour;
                   9685:        REG8(CL) = (UINT8)sTime.wMinute;
                   9686:        REG8(DH) = (UINT8)sTime.wSecond;
1.1.1.14  root     9687:        REG8(DL) = (UINT8)sTime.wMilliseconds / 10;
1.1       root     9688: }
                   9689: 
                   9690: inline void msdos_int_21h_2dh()
                   9691: {
                   9692:        REG8(AL) = 0x00;
                   9693: }
                   9694: 
                   9695: inline void msdos_int_21h_2eh()
                   9696: {
                   9697:        process_t *process = msdos_process_info_get(current_psp);
                   9698:        
                   9699:        process->verify = REG8(AL);
                   9700: }
                   9701: 
                   9702: inline void msdos_int_21h_2fh()
                   9703: {
                   9704:        process_t *process = msdos_process_info_get(current_psp);
                   9705:        
                   9706:        REG16(BX) = process->dta.w.l;
1.1.1.3   root     9707:        SREG(ES) = process->dta.w.h;
                   9708:        i386_load_segment_descriptor(ES);
1.1       root     9709: }
                   9710: 
                   9711: inline void msdos_int_21h_30h()
                   9712: {
                   9713:        // Version Flag / OEM
1.1.1.27  root     9714:        if(REG8(AL) == 0x01) {
1.1.1.29  root     9715: #ifdef SUPPORT_HMA
                   9716:                REG16(BX) = 0x0000;
                   9717: #else
                   9718:                REG16(BX) = 0x1000; // DOS is in HMA
                   9719: #endif
1.1       root     9720:        } else {
1.1.1.27  root     9721:                // NOTE: EXDEB invites BL shows the machine type (0=PC-98, 1=PC/AT, 2=FMR),
                   9722:                // but this is not correct on Windows 98 SE
                   9723: //             REG16(BX) = 0xff01;     // OEM = Microsoft, PC/AT
                   9724:                REG16(BX) = 0xff00;     // OEM = Microsoft
1.1       root     9725:        }
1.1.1.27  root     9726:        REG16(CX) = 0x0000;
1.1.1.30  root     9727:        REG8(AL) = dos_major_version;   // 7
                   9728:        REG8(AH) = dos_minor_version;   // 10
1.1       root     9729: }
                   9730: 
                   9731: inline void msdos_int_21h_31h()
                   9732: {
1.1.1.29  root     9733:        try {
                   9734:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   9735:        } catch(...) {
                   9736:                // recover the broken mcb
                   9737:                int mcb_seg = current_psp - 1;
                   9738:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     9739:                
1.1.1.29  root     9740:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     9741:                        mcb->mz = 'M';
                   9742:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   9743:                        
1.1.1.29  root     9744:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39  root     9745:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29  root     9746:                        } else {
1.1.1.39  root     9747:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29  root     9748:                        }
                   9749:                } else {
                   9750:                        mcb->mz = 'Z';
1.1.1.30  root     9751:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     9752:                }
                   9753:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   9754:        }
1.1       root     9755:        msdos_process_terminate(current_psp, REG8(AL) | 0x300, 0);
                   9756: }
                   9757: 
                   9758: inline void msdos_int_21h_32h()
                   9759: {
                   9760:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   9761:        UINT16 seg, ofs;
                   9762:        
                   9763:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9764:                REG8(AL) = 0;
1.1.1.3   root     9765:                SREG(DS) = seg;
                   9766:                i386_load_segment_descriptor(DS);
1.1       root     9767:                REG16(BX) = ofs;
                   9768:        } else {
                   9769:                REG8(AL) = 0xff;
1.1.1.3   root     9770:                m_CF = 1;
1.1       root     9771:        }
                   9772: }
                   9773: 
                   9774: inline void msdos_int_21h_33h()
                   9775: {
                   9776:        char path[MAX_PATH];
                   9777:        
                   9778:        switch(REG8(AL)) {
                   9779:        case 0x00:
1.1.1.33  root     9780:                REG8(DL) = ctrl_break_checking;
1.1       root     9781:                break;
                   9782:        case 0x01:
1.1.1.33  root     9783:                ctrl_break_checking = REG8(DL);
                   9784:                break;
                   9785:        case 0x02:
                   9786:                {
                   9787:                        UINT8 old = ctrl_break_checking;
                   9788:                        ctrl_break_checking = REG8(DL);
                   9789:                        REG8(DL) = old;
                   9790:                }
                   9791:                break;
                   9792:        case 0x03:
                   9793:        case 0x04:
                   9794:                // DOS 4.0+ - Unused
1.1       root     9795:                break;
                   9796:        case 0x05:
                   9797:                GetSystemDirectory(path, MAX_PATH);
                   9798:                if(path[0] >= 'a' && path[0] <= 'z') {
                   9799:                        REG8(DL) = path[0] - 'a' + 1;
                   9800:                } else {
                   9801:                        REG8(DL) = path[0] - 'A' + 1;
                   9802:                }
                   9803:                break;
                   9804:        case 0x06:
1.1.1.2   root     9805:                // MS-DOS version (7.10)
1.1       root     9806:                REG8(BL) = 7;
1.1.1.2   root     9807:                REG8(BH) = 10;
1.1       root     9808:                REG8(DL) = 0;
1.1.1.29  root     9809: #ifdef SUPPORT_HMA
                   9810:                REG8(DH) = 0x00;
                   9811: #else
                   9812:                REG8(DH) = 0x10; // DOS is in HMA
                   9813: #endif
1.1       root     9814:                break;
1.1.1.6   root     9815:        case 0x07:
                   9816:                if(REG8(DL) == 0) {
                   9817:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag &= ~0x20;
                   9818:                } else if(REG8(DL) == 1) {
                   9819:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag |= 0x20;
                   9820:                }
                   9821:                break;
1.1       root     9822:        default:
1.1.1.22  root     9823:                unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1       root     9824:                REG16(AX) = 0x01;
1.1.1.3   root     9825:                m_CF = 1;
1.1       root     9826:                break;
                   9827:        }
                   9828: }
                   9829: 
1.1.1.23  root     9830: inline void msdos_int_21h_34h()
                   9831: {
                   9832:        SREG(ES) = SDA_TOP >> 4;
                   9833:        i386_load_segment_descriptor(ES);
                   9834:        REG16(BX) = offsetof(sda_t, indos_flag);;
                   9835: }
                   9836: 
1.1       root     9837: inline void msdos_int_21h_35h()
                   9838: {
                   9839:        REG16(BX) = *(UINT16 *)(mem + 4 * REG8(AL) + 0);
1.1.1.3   root     9840:        SREG(ES) = *(UINT16 *)(mem + 4 * REG8(AL) + 2);
                   9841:        i386_load_segment_descriptor(ES);
1.1       root     9842: }
                   9843: 
                   9844: inline void msdos_int_21h_36h()
                   9845: {
                   9846:        struct _diskfree_t df = {0};
                   9847:        
                   9848:        if(_getdiskfree(REG8(DL), &df) == 0) {
                   9849:                REG16(AX) = (UINT16)df.sectors_per_cluster;
                   9850:                REG16(CX) = (UINT16)df.bytes_per_sector;
1.1.1.13  root     9851:                REG16(BX) = df.avail_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.avail_clusters;
                   9852:                REG16(DX) = df.total_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.total_clusters;
1.1       root     9853:        } else {
                   9854:                REG16(AX) = 0xffff;
                   9855:        }
                   9856: }
                   9857: 
                   9858: inline void msdos_int_21h_37h()
                   9859: {
1.1.1.22  root     9860:        static UINT8 dev_flag = 0xff;
1.1       root     9861:        
                   9862:        switch(REG8(AL)) {
                   9863:        case 0x00:
1.1.1.22  root     9864:                {
                   9865:                        process_t *process = msdos_process_info_get(current_psp);
                   9866:                        REG8(AL) = 0x00;
                   9867:                        REG8(DL) = process->switchar;
                   9868:                }
1.1       root     9869:                break;
                   9870:        case 0x01:
1.1.1.22  root     9871:                {
                   9872:                        process_t *process = msdos_process_info_get(current_psp);
                   9873:                        REG8(AL) = 0x00;
                   9874:                        process->switchar = REG8(DL);
1.1.1.23  root     9875:                        msdos_sda_update(current_psp);
1.1.1.22  root     9876:                }
                   9877:                break;
                   9878:        case 0x02:
                   9879:                REG8(DL) = dev_flag;
                   9880:                break;
                   9881:        case 0x03:
                   9882:                dev_flag = REG8(DL);
                   9883:                break;
                   9884:        case 0xd0:
                   9885:        case 0xd1:
                   9886:        case 0xd2:
                   9887:        case 0xd3:
                   9888:        case 0xd4:
                   9889:        case 0xd5:
                   9890:        case 0xd6:
                   9891:        case 0xd7:
                   9892:        case 0xdc:
                   9893:        case 0xdd:
                   9894:        case 0xde:
                   9895:        case 0xdf:
                   9896:                // diet ???
                   9897:                REG16(AX) = 1;
1.1       root     9898:                break;
                   9899:        default:
1.1.1.22  root     9900:                unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1       root     9901:                REG16(AX) = 1;
                   9902:                break;
                   9903:        }
                   9904: }
                   9905: 
1.1.1.42  root     9906: int get_country_info(country_info_t *ci, LCID locale)
1.1.1.17  root     9907: {
                   9908:        char LCdata[80];
                   9909:        
1.1.1.19  root     9910:        ZeroMemory(ci, offsetof(country_info_t, reserved));
1.1.1.42  root     9911:        GetLocaleInfo(locale, LOCALE_ICURRDIGITS, LCdata, sizeof(LCdata));
1.1.1.17  root     9912:        ci->currency_dec_digits = atoi(LCdata);
1.1.1.42  root     9913:        GetLocaleInfo(locale, LOCALE_ICURRENCY, LCdata, sizeof(LCdata));
1.1.1.17  root     9914:        ci->currency_format = *LCdata - '0';
1.1.1.42  root     9915:        GetLocaleInfo(locale, LOCALE_IDATE, LCdata, sizeof(LCdata));
1.1.1.17  root     9916:        ci->date_format = *LCdata - '0';
1.1.1.42  root     9917:        GetLocaleInfo(locale, LOCALE_SCURRENCY, LCdata, sizeof(LCdata));
1.1.1.17  root     9918:        memcpy(&ci->currency_symbol, LCdata, 4);
1.1.1.42  root     9919:        GetLocaleInfo(locale, LOCALE_SDATE, LCdata, sizeof(LCdata));
1.1.1.17  root     9920:        *ci->date_sep = *LCdata;
1.1.1.42  root     9921:        GetLocaleInfo(locale, LOCALE_SDECIMAL, LCdata, sizeof(LCdata));
1.1.1.17  root     9922:        *ci->dec_sep = *LCdata;
1.1.1.42  root     9923:        GetLocaleInfo(locale, LOCALE_SLIST, LCdata, sizeof(LCdata));
1.1.1.17  root     9924:        *ci->list_sep = *LCdata;
1.1.1.42  root     9925:        GetLocaleInfo(locale, LOCALE_STHOUSAND, LCdata, sizeof(LCdata));
1.1.1.17  root     9926:        *ci->thou_sep = *LCdata;
1.1.1.42  root     9927:        GetLocaleInfo(locale, LOCALE_STIME, LCdata, sizeof(LCdata));
1.1.1.17  root     9928:        *ci->time_sep = *LCdata;
1.1.1.42  root     9929:        GetLocaleInfo(locale, LOCALE_STIMEFORMAT, LCdata, sizeof(LCdata));
1.1.1.17  root     9930:        if(strchr(LCdata, 'H') != NULL) {
                   9931:                ci->time_format = 1;
                   9932:        }
1.1.1.27  root     9933:        ci->case_map.w.l = 0x000a; // dummy case map routine is at fffd:000a
1.1.1.24  root     9934:        ci->case_map.w.h = 0xfffd;
1.1.1.42  root     9935:        GetLocaleInfo(locale, LOCALE_ICOUNTRY, LCdata, sizeof(LCdata));
1.1.1.17  root     9936:        return atoi(LCdata);
                   9937: }
                   9938: 
1.1.1.42  root     9939: int get_country_info(country_info_t *ci, USHORT usPrimaryLanguage, USHORT usSubLanguage)
                   9940: {
                   9941:        return get_country_info(ci, MAKELCID(MAKELANGID(usPrimaryLanguage, usSubLanguage), SORT_DEFAULT));
                   9942: }
                   9943: 
                   9944: int get_country_info(country_info_t *ci)
                   9945: {
                   9946:        return get_country_info(ci, LOCALE_USER_DEFAULT);
                   9947: }
                   9948: 
1.1.1.43  root     9949: void set_country_info(country_info_t *ci, int size)
                   9950: {
                   9951:        char LCdata[80];
                   9952:        
                   9953:        if(size >= 0x00 + 2) {
                   9954:                memset(LCdata, 0, sizeof(LCdata));
                   9955:                *LCdata = '0' + ci->date_format;
                   9956:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IDATE, LCdata);
                   9957:        }
                   9958:        if(size >= 0x02 + 5) {
                   9959:                memset(LCdata, 0, sizeof(LCdata));
                   9960:                memcpy(LCdata, &ci->currency_symbol, 4);
                   9961:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SCURRENCY, LCdata);
                   9962:        }
                   9963:        if(size >= 0x07 + 2) {
                   9964:                memset(LCdata, 0, sizeof(LCdata));
                   9965:                *LCdata = *ci->thou_sep;
                   9966:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, LCdata);
                   9967:        }
                   9968:        if(size >= 0x09 + 2) {
                   9969:                memset(LCdata, 0, sizeof(LCdata));
                   9970:                *LCdata = *ci->dec_sep;
                   9971:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, LCdata);
                   9972:        }
                   9973:        if(size >= 0x0b + 2) {
                   9974:                memset(LCdata, 0, sizeof(LCdata));
                   9975:                *LCdata = *ci->date_sep;
                   9976:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDATE, LCdata);
                   9977:        }
                   9978:        if(size >= 0x0d + 2) {
                   9979:                memset(LCdata, 0, sizeof(LCdata));
                   9980:                *LCdata = *ci->time_sep;
                   9981:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIME, LCdata);
                   9982:        }
                   9983:        if(size >= 0x0f + 1) {
                   9984:                memset(LCdata, 0, sizeof(LCdata));
                   9985:                *LCdata = '0' + ci->currency_format;
                   9986:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRENCY, LCdata);
                   9987:        }
                   9988:        if(size >= 0x10 + 1) {
                   9989:                sprintf(LCdata, "%d", ci->currency_dec_digits);
                   9990:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRDIGITS, LCdata);
                   9991:        }
                   9992:        if(size >= 0x11 + 1) {
                   9993:                // FIXME: is time format always H/h:mm:ss ???
                   9994:                if(ci->time_format & 1) {
                   9995:                        sprintf(LCdata, "H%cmm%css", *ci->time_sep, *ci->time_sep);
                   9996:                } else {
                   9997:                        sprintf(LCdata, "h%cmm%css", *ci->time_sep, *ci->time_sep);
                   9998:                }
                   9999:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, LCdata);
                   10000:        }
                   10001:        if(size >= 0x12 + 4) {
                   10002:                // 12h  DWORD   address of case map routine
                   10003:                //              (FAR CALL, AL = character to map to upper case [>= 80h])
                   10004:        }
                   10005:        if(size >= 0x16 + 2) {
                   10006:                memset(LCdata, 0, sizeof(LCdata));
                   10007:                *LCdata = *ci->list_sep;
                   10008:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SLIST, LCdata);
                   10009:        }
                   10010: }
                   10011: 
1.1.1.42  root     10012: #ifndef SUBLANG_SWAHILI
                   10013:        #define SUBLANG_SWAHILI 0x01
                   10014: #endif
                   10015: #ifndef SUBLANG_TSWANA_BOTSWANA
                   10016:        #define SUBLANG_TSWANA_BOTSWANA 0x02
                   10017: #endif
                   10018: #ifndef SUBLANG_LITHUANIAN_LITHUANIA
                   10019:        #define SUBLANG_LITHUANIAN_LITHUANIA 0x01
                   10020: #endif
                   10021: #ifndef LANG_BANGLA
                   10022:        #define LANG_BANGLA 0x45
                   10023: #endif
                   10024: #ifndef SUBLANG_BANGLA_BANGLADESH
                   10025:        #define SUBLANG_BANGLA_BANGLADESH 0x02
                   10026: #endif
                   10027: 
                   10028: static const struct {
                   10029:        int code;
                   10030:        USHORT usPrimaryLanguage;
                   10031:        USHORT usSubLanguage;
                   10032: } country_table[] = {
                   10033:        {0x001, LANG_ENGLISH, SUBLANG_ENGLISH_US},                              // United States
                   10034:        {0x002, LANG_FRENCH, SUBLANG_FRENCH_CANADIAN},                          // Canadian-French
                   10035:        {0x004, LANG_ENGLISH, SUBLANG_ENGLISH_CAN},                             // Canada (English)
                   10036:        {0x007, LANG_RUSSIAN, SUBLANG_RUSSIAN_RUSSIA},                          // Russia
                   10037:        {0x014, LANG_ARABIC, SUBLANG_ARABIC_EGYPT},                             // Egypt
                   10038:        {0x01B, LANG_ZULU, SUBLANG_ZULU_SOUTH_AFRICA},                          // South Africa
                   10039: //     {0x01B, LANG_XHOSA, SUBLANG_XHOSA_SOUTH_AFRICA},                        // South Africa
                   10040: //     {0x01B, LANG_AFRIKAANS, SUBLANG_AFRIKAANS_SOUTH_AFRICA},                // South Africa
                   10041: //     {0x01B, LANG_ENGLISH, SUBLANG_ENGLISH_SOUTH_AFRICA},                    // South Africa
                   10042: //     {0x01B, LANG_SOTHO, SUBLANG_SOTHO_NORTHERN_SOUTH_AFRICA},               // South Africa
                   10043: //     {0x01B, LANG_TSWANA, SUBLANG_TSWANA_SOUTH_AFRICA},                      // South Africa
                   10044:        {0x01E, LANG_GREEK, SUBLANG_GREEK_GREECE},                              // Greece
                   10045:        {0x01F, LANG_DUTCH, SUBLANG_DUTCH},                                     // Netherlands
                   10046: //     {0x01F, LANG_FRISIAN, SUBLANG_FRISIAN_NETHERLANDS},                     // Netherlands
                   10047:        {0x020, LANG_DUTCH, SUBLANG_DUTCH_BELGIAN},                             // Belgium
                   10048: //     {0x020, LANG_FRENCH, SUBLANG_FRENCH_BELGIAN},                           // Belgium
                   10049:        {0x021, LANG_FRENCH, SUBLANG_FRENCH},                                   // France
                   10050:        {0x022, LANG_SPANISH, SUBLANG_SPANISH},                                 // Spain
                   10051:        {0x023, LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA},                    // Bulgaria???
                   10052:        {0x024, LANG_HUNGARIAN, SUBLANG_HUNGARIAN_HUNGARY},                     // Hungary (not supported by DR DOS 5.0)
                   10053:        {0x027, LANG_ITALIAN, SUBLANG_ITALIAN},                                 // Italy / San Marino / Vatican City
                   10054:        {0x028, LANG_ROMANIAN, SUBLANG_ROMANIAN_ROMANIA},                       // Romania
                   10055:        {0x029, LANG_GERMAN, SUBLANG_GERMAN_SWISS},                             // Switzerland / Liechtenstein
                   10056: //     {0x029, LANG_FRENCH, SUBLANG_FRENCH_SWISS},                             // Switzerland / Liechtenstein
                   10057: //     {0x029, LANG_ITALIAN, SUBLANG_ITALIAN_SWISS},                           // Switzerland / Liechtenstein
                   10058:        {0x02A, LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA},                          // Czechoslovakia / Tjekia / Slovakia (not supported by DR DOS 5.0)
                   10059:        {0x02B, LANG_GERMAN, SUBLANG_GERMAN_AUSTRIAN},                          // Austria (DR DOS 5.0)
                   10060:        {0x02C, LANG_ENGLISH, SUBLANG_ENGLISH_UK},                              // United Kingdom
                   10061:        {0x02D, LANG_DANISH, SUBLANG_DANISH_DENMARK},                           // Denmark
                   10062:        {0x02E, LANG_SWEDISH, SUBLANG_SWEDISH},                                 // Sweden
                   10063: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_NORTHERN_SWEDEN},                       // Sweden
                   10064: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_LULE_SWEDEN},                           // Sweden
                   10065: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_SOUTHERN_SWEDEN},                       // Sweden
                   10066:        {0x02F, LANG_NORWEGIAN, SUBLANG_NORWEGIAN_BOKMAL},                      // Norway
                   10067: //     {0x02F, LANG_NORWEGIAN, SUBLANG_NORWEGIAN_NYNORSK},                     // Norway
                   10068: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_NORTHERN_NORWAY},                       // Norway
                   10069: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_LULE_NORWAY},                           // Norway
                   10070: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_SOUTHERN_NORWAY},                       // Norway
                   10071:        {0x030, LANG_POLISH, SUBLANG_POLISH_POLAND},                            // Poland (not supported by DR DOS 5.0)
                   10072:        {0x031, LANG_GERMAN, SUBLANG_GERMAN},                                   // Germany
                   10073:        {0x033, LANG_SPANISH, SUBLANG_SPANISH_PERU},                            // Peru
                   10074: //     {0x033, LANG_QUECHUA, SUBLANG_QUECHUA_PERU},                            // Peru
                   10075:        {0x034, LANG_SPANISH, SUBLANG_SPANISH_MEXICAN},                         // Mexico
                   10076:        {0x035, LANG_SPANISH, SUBLANG_NEUTRAL},                                 // Cuba
                   10077:        {0x036, LANG_SPANISH, SUBLANG_SPANISH_ARGENTINA},                       // Argentina
                   10078:        {0x037, LANG_PORTUGUESE, SUBLANG_PORTUGUESE_BRAZILIAN},                 // Brazil (not supported by DR DOS 5.0)
                   10079:        {0x038, LANG_SPANISH, SUBLANG_SPANISH_CHILE},                           // Chile
                   10080:        {0x039, LANG_SPANISH, SUBLANG_SPANISH_COLOMBIA},                        // Columbia
                   10081:        {0x03A, LANG_SPANISH, SUBLANG_SPANISH_VENEZUELA},                       // Venezuela
                   10082:        {0x03C, LANG_MALAY, SUBLANG_MALAY_MALAYSIA},                            // Malaysia
                   10083:        {0x03D, LANG_ENGLISH, SUBLANG_ENGLISH_AUS},                             // International English / Australia
                   10084:        {0x03E, LANG_INDONESIAN, SUBLANG_INDONESIAN_INDONESIA},                 // Indonesia / East Timor
                   10085:        {0x03F, LANG_ENGLISH, SUBLANG_ENGLISH_PHILIPPINES},                     // Philippines
                   10086:        {0x040, LANG_ENGLISH, SUBLANG_ENGLISH_NZ},                              // New Zealand
                   10087:        {0x041, LANG_CHINESE, SUBLANG_CHINESE_SINGAPORE},                       // Singapore
                   10088: //     {0x041, LANG_ENGLISH, SUBLANG_ENGLISH_SINGAPORE},                       // Singapore
                   10089:        {0x042, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan???
                   10090:        {0x051, LANG_JAPANESE, SUBLANG_JAPANESE_JAPAN},                         // Japan (DR DOS 5.0, MS-DOS 5.0+)
                   10091:        {0x052, LANG_KOREAN, SUBLANG_KOREAN},                                   // South Korea (DR DOS 5.0)
                   10092:        {0x054, LANG_VIETNAMESE, SUBLANG_VIETNAMESE_VIETNAM},                   // Vietnam
                   10093:        {0x056, LANG_CHINESE_SIMPLIFIED, SUBLANG_CHINESE_SIMPLIFIED},           // China (MS-DOS 5.0+)
                   10094:        {0x058, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan (MS-DOS 5.0+)
                   10095:        {0x05A, LANG_TURKISH, SUBLANG_TURKISH_TURKEY},                          // Turkey (MS-DOS 5.0+)
                   10096:        {0x05B, LANG_HINDI, SUBLANG_HINDI_INDIA},                               // India
                   10097:        {0x05C, LANG_URDU, SUBLANG_URDU_PAKISTAN},                              // Pakistan
                   10098:        {0x05D, LANG_PASHTO, SUBLANG_PASHTO_AFGHANISTAN},                       // Afghanistan
                   10099: //     {0x05D, LANG_DARI, SUBLANG_DARI_AFGHANISTAN},                           // Afghanistan
                   10100:        {0x05E, LANG_SINHALESE, SUBLANG_SINHALESE_SRI_LANKA},                   // Sri Lanka
                   10101: //     {0x05E, LANG_TAMIL, SUBLANG_TAMIL_SRI_LANKA},                           // Sri Lanka
                   10102:        {0x062, LANG_PERSIAN, SUBLANG_PERSIAN_IRAN},                            // Iran
                   10103:        {0x070, LANG_BELARUSIAN, SUBLANG_BELARUSIAN_BELARUS},                   // Belarus
                   10104:        {0x0C8, LANG_THAI, SUBLANG_THAI_THAILAND},                              // Thailand
                   10105:        {0x0D4, LANG_ARABIC, SUBLANG_ARABIC_MOROCCO},                           // Morocco
                   10106:        {0x0D5, LANG_ARABIC, SUBLANG_ARABIC_ALGERIA},                           // Algeria
                   10107:        {0x0D8, LANG_ARABIC, SUBLANG_ARABIC_TUNISIA},                           // Tunisia
                   10108:        {0x0DA, LANG_ARABIC, SUBLANG_ARABIC_LIBYA},                             // Libya
                   10109:        {0x0DD, LANG_WOLOF, SUBLANG_WOLOF_SENEGAL},                             // Senegal
                   10110: //     {0x0DD, LANG_PULAR, SUBLANG_PULAR_SENEGAL},                             // Senegal
                   10111:        {0x0EA, LANG_HAUSA, SUBLANG_HAUSA_NIGERIA_LATIN},                       // Nigeria
                   10112: //     {0x0EA, LANG_YORUBA, SUBLANG_YORUBA_NIGERIA},                           // Nigeria
                   10113: //     {0x0EA, LANG_IGBO, SUBLANG_IGBO_NIGERIA},                               // Nigeria
                   10114:        {0x0FB, LANG_AMHARIC, SUBLANG_AMHARIC_ETHIOPIA},                        // Ethiopia
                   10115: //     {0x0FB, LANG_TIGRINYA, SUBLANG_TIGRINYA_ETHIOPIA},                      // Ethiopia
                   10116:        {0x0FE, LANG_SWAHILI, SUBLANG_SWAHILI},                                 // Kenya
                   10117:        {0x107, LANG_ENGLISH, SUBLANG_ENGLISH_ZIMBABWE},                        // Zimbabwe
                   10118:        {0x10B, LANG_TSWANA, SUBLANG_TSWANA_BOTSWANA},                          // Botswana
                   10119:        {0x12A, LANG_FAEROESE, SUBLANG_FAEROESE_FAROE_ISLANDS},                 // Faroe Islands
                   10120:        {0x12B, LANG_GREENLANDIC, SUBLANG_GREENLANDIC_GREENLAND},               // Greenland
                   10121:        {0x15F, LANG_PORTUGUESE, SUBLANG_PORTUGUESE},                           // Portugal
                   10122:        {0x160, LANG_LUXEMBOURGISH, SUBLANG_LUXEMBOURGISH_LUXEMBOURG},          // Luxembourg
                   10123: //     {0x160, LANG_GERMAN, SUBLANG_GERMAN_LUXEMBOURG},                        // Luxembourg
                   10124: //     {0x160, LANG_FRENCH, SUBLANG_FRENCH_LUXEMBOURG},                        // Luxembourg
                   10125:        {0x161, LANG_IRISH, SUBLANG_IRISH_IRELAND},                             // Ireland
                   10126: //     {0x161, LANG_ENGLISH, SUBLANG_ENGLISH_IRELAND},                         // Ireland
                   10127:        {0x162, LANG_ICELANDIC, SUBLANG_ICELANDIC_ICELAND},                     // Iceland
                   10128:        {0x163, LANG_ALBANIAN, SUBLANG_ALBANIAN_ALBANIA},                       // Albania
                   10129:        {0x164, LANG_MALTESE, SUBLANG_MALTESE_MALTA},                           // Malta
                   10130:        {0x166, LANG_FINNISH, SUBLANG_FINNISH_FINLAND},                         // Finland
                   10131: //     {0x166, LANG_SAMI, SUBLANG_SAMI_NORTHERN_FINLAND},                      // Finland
                   10132: //     {0x166, LANG_SAMI, SUBLANG_SAMI_SKOLT_FINLAND},                         // Finland
                   10133: //     {0x166, LANG_SAMI, SUBLANG_SAMI_INARI_FINLAND},                         // Finland
                   10134:        {0x167, LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA},                    // Bulgaria
                   10135:        {0x172, LANG_LITHUANIAN, SUBLANG_LITHUANIAN_LITHUANIA},                 // Lithuania
                   10136:        {0x173, LANG_LATVIAN, SUBLANG_LATVIAN_LATVIA},                          // Latvia
                   10137:        {0x174, LANG_ESTONIAN, SUBLANG_ESTONIAN_ESTONIA},                       // Estonia
                   10138:        {0x17D, LANG_SERBIAN, SUBLANG_SERBIAN_LATIN},                           // Serbia / Montenegro
                   10139:        {0x180, LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA},                         // Croatia???
                   10140:        {0x181, LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA},                         // Croatia
                   10141:        {0x182, LANG_SLOVENIAN, SUBLANG_SLOVENIAN_SLOVENIA},                    // Slovenia
                   10142:        {0x183, LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_LATIN},        // Bosnia-Herzegovina (Latin)
                   10143:        {0x184, LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_CYRILLIC},     // Bosnia-Herzegovina (Cyrillic)
                   10144:        {0x185, LANG_MACEDONIAN, SUBLANG_MACEDONIAN_MACEDONIA},                 // FYR Macedonia
                   10145:        {0x1A5, LANG_CZECH, SUBLANG_CZECH_CZECH_REPUBLIC},                      // Czech Republic
                   10146:        {0x1A6, LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA},                          // Slovakia
                   10147:        {0x1F5, LANG_ENGLISH, SUBLANG_ENGLISH_BELIZE},                          // Belize
                   10148:        {0x1F6, LANG_SPANISH, SUBLANG_SPANISH_GUATEMALA},                       // Guatemala
                   10149:        {0x1F7, LANG_SPANISH, SUBLANG_SPANISH_EL_SALVADOR},                     // El Salvador
                   10150:        {0x1F8, LANG_SPANISH, SUBLANG_SPANISH_HONDURAS},                        // Honduras
                   10151:        {0x1F9, LANG_SPANISH, SUBLANG_SPANISH_NICARAGUA},                       // Nicraragua
                   10152:        {0x1FA, LANG_SPANISH, SUBLANG_SPANISH_COSTA_RICA},                      // Costa Rica
                   10153:        {0x1FB, LANG_SPANISH, SUBLANG_SPANISH_PANAMA},                          // Panama
                   10154:        {0x24F, LANG_SPANISH, SUBLANG_SPANISH_BOLIVIA},                         // Bolivia
1.1.1.43  root     10155: //     {0x24F, LANG_QUECHUA, SUBLANG_QUECHUA_BOLIVIA},                         // Bolivia
1.1.1.42  root     10156:        {0x251, LANG_SPANISH, SUBLANG_SPANISH_ECUADOR},                         // Ecuador
                   10157: //     {0x251, LANG_QUECHUA, SUBLANG_QUECHUA_ECUADOR},                         // Ecuador
                   10158:        {0x253, LANG_SPANISH, SUBLANG_SPANISH_PARAGUAY},                        // Paraguay
                   10159:        {0x256, LANG_SPANISH, SUBLANG_SPANISH_URUGUAY},                         // Uruguay
                   10160:        {0x2A1, LANG_MALAY, SUBLANG_MALAY_BRUNEI_DARUSSALAM},                   // Brunei Darussalam
                   10161:        {0x311, LANG_ARABIC, SUBLANG_NEUTRAL},                                  // Arabic (Middle East/Saudi Arabia/etc.)
                   10162:        {0x324, LANG_UKRAINIAN, SUBLANG_UKRAINIAN_UKRAINE},                     // Ukraine
                   10163:        {0x352, LANG_KOREAN, SUBLANG_KOREAN},                                   // North Korea
                   10164:        {0x354, LANG_CHINESE, SUBLANG_CHINESE_HONGKONG},                        // Hong Kong
                   10165:        {0x355, LANG_CHINESE, SUBLANG_CHINESE_MACAU},                           // Macao
                   10166:        {0x357, LANG_KHMER, SUBLANG_KHMER_CAMBODIA},                            // Cambodia
                   10167:        {0x370, LANG_BANGLA, SUBLANG_BANGLA_BANGLADESH},                        // Bangladesh
                   10168:        {0x376, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan (DOS 6.22+)
                   10169:        {0x3C0, LANG_DIVEHI, SUBLANG_DIVEHI_MALDIVES},                          // Maldives
                   10170:        {0x3C1, LANG_ARABIC, SUBLANG_ARABIC_LEBANON},                           // Lebanon
                   10171:        {0x3C2, LANG_ARABIC, SUBLANG_ARABIC_JORDAN},                            // Jordan
                   10172:        {0x3C3, LANG_ARABIC, SUBLANG_ARABIC_SYRIA},                             // Syrian Arab Republic
                   10173:        {0x3C4, LANG_ARABIC, SUBLANG_ARABIC_IRAQ},                              // Ireq
                   10174:        {0x3C5, LANG_ARABIC, SUBLANG_ARABIC_KUWAIT},                            // Kuwait
                   10175:        {0x3C6, LANG_ARABIC, SUBLANG_ARABIC_SAUDI_ARABIA},                      // Saudi Arabia
                   10176:        {0x3C7, LANG_ARABIC, SUBLANG_ARABIC_YEMEN},                             // Yemen
                   10177:        {0x3C8, LANG_ARABIC, SUBLANG_ARABIC_OMAN},                              // Oman
                   10178:        {0x3CB, LANG_ARABIC, SUBLANG_ARABIC_UAE},                               // United Arab Emirates
                   10179:        {0x3CC, LANG_HEBREW, SUBLANG_HEBREW_ISRAEL},                            // Israel (Hebrew) (DR DOS 5.0,MS-DOS 5.0+)
                   10180:        {0x3CD, LANG_ARABIC, SUBLANG_ARABIC_BAHRAIN},                           // Bahrain
                   10181:        {0x3CE, LANG_ARABIC, SUBLANG_ARABIC_QATAR},                             // Qatar
                   10182:        {0x3D0, LANG_MONGOLIAN, SUBLANG_MONGOLIAN_CYRILLIC_MONGOLIA},           // Mongolia
                   10183: //     {0x3D0, LANG_MONGOLIAN, SUBLANG_MONGOLIAN_PRC},                         // Mongolia
                   10184:        {0x3D1, LANG_NEPALI, SUBLANG_NEPALI_NEPAL},                             // Nepal
                   10185:        {-1, 0, 0},
                   10186: };
                   10187: 
1.1.1.14  root     10188: inline void msdos_int_21h_38h()
                   10189: {
                   10190:        switch(REG8(AL)) {
                   10191:        case 0x00:
1.1.1.19  root     10192:                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1.1.14  root     10193:                break;
                   10194:        default:
1.1.1.42  root     10195:                for(int i = 0;; i++) {
                   10196:                        if(country_table[i].code == (REG8(AL) != 0xff ? REG8(AL) : REG16(BX))) {
                   10197:                                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)), country_table[i].usPrimaryLanguage, country_table[i].usSubLanguage);
                   10198:                                break;
                   10199:                        } else if(country_table[i].code == -1) {
                   10200: //                             unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   10201: //                             REG16(AX) = 2;
                   10202: //                             m_CF = 1;
                   10203:                                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
                   10204:                                break;
                   10205:                        }
                   10206:                }
1.1.1.14  root     10207:                break;
                   10208:        }
                   10209: }
                   10210: 
1.1       root     10211: inline void msdos_int_21h_39h(int lfn)
                   10212: {
1.1.1.3   root     10213:        if(_mkdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     10214:                REG16(AX) = errno;
1.1.1.3   root     10215:                m_CF = 1;
1.1       root     10216:        }
                   10217: }
                   10218: 
                   10219: inline void msdos_int_21h_3ah(int lfn)
                   10220: {
1.1.1.3   root     10221:        if(_rmdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     10222:                REG16(AX) = errno;
1.1.1.3   root     10223:                m_CF = 1;
1.1       root     10224:        }
                   10225: }
                   10226: 
                   10227: inline void msdos_int_21h_3bh(int lfn)
                   10228: {
1.1.1.45  root     10229:        const char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1.1.44  root     10230:        
                   10231:        if(_chdir(path)) {
1.1.1.17  root     10232:                REG16(AX) = 3;  // must be 3 (path not found)
1.1.1.3   root     10233:                m_CF = 1;
1.1.1.44  root     10234:        } else {
                   10235:                int drv = _getdrive() - 1;
                   10236:                if(path[1] == ':') {
                   10237:                        if(path[0] >= 'A' && path[0] <= 'Z') {
                   10238:                                drv = path[0] - 'A';
                   10239:                        } else if(path[0] >= 'a' && path[0] <= 'z') {
                   10240:                                drv = path[0] - 'a';
                   10241:                        }
                   10242:                }
                   10243:                msdos_cds_update(drv, path);
1.1       root     10244:        }
                   10245: }
                   10246: 
                   10247: inline void msdos_int_21h_3ch()
                   10248: {
1.1.1.45  root     10249:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     10250:        int attr = GetFileAttributes(path);
1.1.1.37  root     10251:        int fd = -1;
                   10252:        int sio_port = 0;
                   10253:        int lpt_port = 0;
1.1       root     10254:        
1.1.1.45  root     10255:        if(msdos_is_device_path(path)) {
                   10256:                fd = msdos_open_device(path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
1.1       root     10257:        } else {
                   10258:                fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   10259:        }
                   10260:        if(fd != -1) {
                   10261:                if(attr == -1) {
                   10262:                        attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   10263:                }
                   10264:                SetFileAttributes(path, attr);
                   10265:                REG16(AX) = fd;
1.1.1.45  root     10266:                msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     10267:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     10268:        } else {
                   10269:                REG16(AX) = errno;
1.1.1.3   root     10270:                m_CF = 1;
1.1       root     10271:        }
                   10272: }
                   10273: 
                   10274: inline void msdos_int_21h_3dh()
                   10275: {
1.1.1.45  root     10276:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     10277:        int mode = REG8(AL) & 0x03;
1.1.1.37  root     10278:        int fd = -1;
                   10279:        int sio_port = 0;
                   10280:        int lpt_port = 0;
1.1       root     10281:        
                   10282:        if(mode < 0x03) {
1.1.1.45  root     10283:                if(msdos_is_device_path(path)) {
                   10284:                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1.1.11  root     10285:                } else {
1.1.1.13  root     10286:                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     10287:                }
1.1       root     10288:                if(fd != -1) {
                   10289:                        REG16(AX) = fd;
1.1.1.45  root     10290:                        msdos_file_handler_open(fd, path, _isatty(fd), mode, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     10291:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     10292:                } else {
                   10293:                        REG16(AX) = errno;
1.1.1.3   root     10294:                        m_CF = 1;
1.1       root     10295:                }
                   10296:        } else {
                   10297:                REG16(AX) = 0x0c;
1.1.1.3   root     10298:                m_CF = 1;
1.1       root     10299:        }
                   10300: }
                   10301: 
                   10302: inline void msdos_int_21h_3eh()
                   10303: {
                   10304:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10305:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10306:        
1.1.1.20  root     10307:        if(fd < process->max_files && file_handler[fd].valid) {
                   10308:                _close(fd);
                   10309:                msdos_file_handler_close(fd);
                   10310:                msdos_psp_set_file_table(REG16(BX), 0x0ff, current_psp);
1.1       root     10311:        } else {
                   10312:                REG16(AX) = 0x06;
1.1.1.3   root     10313:                m_CF = 1;
1.1       root     10314:        }
                   10315: }
                   10316: 
1.1.1.35  root     10317: DWORD WINAPI msdos_int_21h_3fh_thread(LPVOID)
                   10318: {
                   10319:        UINT8 *buf = mem + SREG_BASE(DS) + REG16(DX);
                   10320:        int max = REG16(CX);
                   10321:        int p = 0;
                   10322:        
                   10323:        while(max > p) {
                   10324:                int chr = msdos_getch();
                   10325:                
                   10326:                if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
                   10327:                        p = 0;
                   10328:                        buf[p++] = 0x0d;
                   10329:                        if(max > p) {
                   10330:                                buf[p++] = 0x0a;
                   10331:                        }
                   10332:                        msdos_putch(0x03);
                   10333:                        msdos_putch(0x0d);
                   10334:                        msdos_putch(0x0a);
                   10335:                        break;
                   10336:                } else if(ctrl_break_pressed) {
                   10337:                        // skip this byte
                   10338:                } else if(chr == 0x00) {
                   10339:                        // skip 2nd byte
                   10340:                        msdos_getch();
                   10341:                } else if(chr == 0x0d) {
                   10342:                        // carriage return
                   10343:                        buf[p++] = 0x0d;
                   10344:                        if(max > p) {
                   10345:                                buf[p++] = 0x0a;
                   10346:                        }
                   10347:                        msdos_putch('\n');
                   10348:                        break;
                   10349:                } else if(chr == 0x08) {
                   10350:                        // back space
                   10351:                        if(p > 0) {
                   10352:                                p--;
                   10353:                                if(msdos_ctrl_code_check(buf[p])) {
                   10354:                                        msdos_putch(0x08);
                   10355:                                        msdos_putch(0x08);
                   10356:                                        msdos_putch(0x20);
                   10357:                                        msdos_putch(0x20);
                   10358:                                        msdos_putch(0x08);
                   10359:                                        msdos_putch(0x08);
1.1.1.36  root     10360:                                } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
                   10361:                                        p--;
                   10362:                                        msdos_putch(0x08);
                   10363:                                        msdos_putch(0x08);
                   10364:                                        msdos_putch(0x20);
                   10365:                                        msdos_putch(0x20);
                   10366:                                        msdos_putch(0x08);
                   10367:                                        msdos_putch(0x08);
1.1.1.35  root     10368:                                } else {
                   10369:                                        msdos_putch(0x08);
                   10370:                                        msdos_putch(0x20);
                   10371:                                        msdos_putch(0x08);
                   10372:                                }
                   10373:                        }
                   10374:                } else if(chr == 0x1b) {
                   10375:                        // escape
                   10376:                        while(p > 0) {
                   10377:                                p--;
                   10378:                                if(msdos_ctrl_code_check(buf[p])) {
                   10379:                                        msdos_putch(0x08);
                   10380:                                        msdos_putch(0x08);
                   10381:                                        msdos_putch(0x20);
                   10382:                                        msdos_putch(0x20);
                   10383:                                        msdos_putch(0x08);
                   10384:                                        msdos_putch(0x08);
                   10385:                                } else {
                   10386:                                        msdos_putch(0x08);
                   10387:                                        msdos_putch(0x20);
                   10388:                                        msdos_putch(0x08);
                   10389:                                }
                   10390:                        }
                   10391:                } else {
                   10392:                        buf[p++] = chr;
                   10393:                        msdos_putch(chr);
                   10394:                }
                   10395:        }
                   10396:        REG16(AX) = p;
                   10397:        
                   10398: #ifdef USE_SERVICE_THREAD
                   10399:        service_exit = true;
                   10400: #endif
                   10401:        return(0);
                   10402: }
                   10403: 
1.1       root     10404: inline void msdos_int_21h_3fh()
                   10405: {
                   10406:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10407:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10408:        
1.1.1.20  root     10409:        if(fd < process->max_files && file_handler[fd].valid) {
                   10410:                if(file_mode[file_handler[fd].mode].in) {
                   10411:                        if(file_handler[fd].atty) {
1.1       root     10412:                                // BX is stdin or is redirected to stdin
1.1.1.35  root     10413:                                if(REG16(CX) != 0) {
                   10414: #ifdef USE_SERVICE_THREAD
                   10415:                                        start_service_loop(msdos_int_21h_3fh_thread);
                   10416: #else
                   10417:                                        msdos_int_21h_3fh_thread(NULL);
                   10418:                                        REQUEST_HARDWRE_UPDATE();
                   10419: #endif
                   10420:                                } else {
                   10421:                                        REG16(AX) = 0;
1.1       root     10422:                                }
                   10423:                        } else {
1.1.1.37  root     10424:                                REG16(AX) = msdos_read(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     10425:                        }
                   10426:                } else {
                   10427:                        REG16(AX) = 0x05;
1.1.1.3   root     10428:                        m_CF = 1;
1.1       root     10429:                }
                   10430:        } else {
                   10431:                REG16(AX) = 0x06;
1.1.1.3   root     10432:                m_CF = 1;
1.1       root     10433:        }
                   10434: }
                   10435: 
                   10436: inline void msdos_int_21h_40h()
                   10437: {
                   10438:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10439:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10440:        
1.1.1.20  root     10441:        if(fd < process->max_files && file_handler[fd].valid) {
                   10442:                if(file_mode[file_handler[fd].mode].out) {
1.1       root     10443:                        if(REG16(CX)) {
1.1.1.20  root     10444:                                if(file_handler[fd].atty) {
1.1       root     10445:                                        // BX is stdout/stderr or is redirected to stdout
                   10446:                                        for(int i = 0; i < REG16(CX); i++) {
1.1.1.3   root     10447:                                                msdos_putch(mem[SREG_BASE(DS) + REG16(DX) + i]);
1.1       root     10448:                                        }
                   10449:                                        REG16(AX) = REG16(CX);
                   10450:                                } else {
1.1.1.20  root     10451:                                        REG16(AX) = msdos_write(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     10452:                                }
                   10453:                        } else {
1.1.1.20  root     10454:                                UINT32 pos = _tell(fd);
                   10455:                                _lseek(fd, 0, SEEK_END);
                   10456:                                UINT32 size = _tell(fd);
1.1.1.12  root     10457:                                if(pos < size) {
1.1.1.20  root     10458:                                        _lseek(fd, pos, SEEK_SET);
                   10459:                                        SetEndOfFile((HANDLE)_get_osfhandle(fd));
1.1.1.12  root     10460:                                } else {
                   10461:                                        for(UINT32 i = size; i < pos; i++) {
                   10462:                                                UINT8 tmp = 0;
1.1.1.23  root     10463:                                                msdos_write(fd, &tmp, 1);
1.1.1.12  root     10464:                                        }
1.1.1.20  root     10465:                                        _lseek(fd, pos, SEEK_SET);
1.1       root     10466:                                }
1.1.1.23  root     10467:                                REG16(AX) = 0;
1.1       root     10468:                        }
                   10469:                } else {
                   10470:                        REG16(AX) = 0x05;
1.1.1.3   root     10471:                        m_CF = 1;
1.1       root     10472:                }
                   10473:        } else {
                   10474:                REG16(AX) = 0x06;
1.1.1.3   root     10475:                m_CF = 1;
1.1       root     10476:        }
                   10477: }
                   10478: 
                   10479: inline void msdos_int_21h_41h(int lfn)
                   10480: {
1.1.1.3   root     10481:        if(remove(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     10482:                REG16(AX) = errno;
1.1.1.3   root     10483:                m_CF = 1;
1.1       root     10484:        }
                   10485: }
                   10486: 
                   10487: inline void msdos_int_21h_42h()
                   10488: {
                   10489:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10490:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10491:        
1.1.1.20  root     10492:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     10493:                if(REG8(AL) < 0x03) {
1.1.1.35  root     10494:                        static const int ptrname[] = { SEEK_SET, SEEK_CUR, SEEK_END };
1.1.1.20  root     10495:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), ptrname[REG8(AL)]);
                   10496:                        UINT32 pos = _tell(fd);
1.1       root     10497:                        REG16(AX) = pos & 0xffff;
                   10498:                        REG16(DX) = (pos >> 16);
                   10499:                } else {
                   10500:                        REG16(AX) = 0x01;
1.1.1.3   root     10501:                        m_CF = 1;
1.1       root     10502:                }
                   10503:        } else {
                   10504:                REG16(AX) = 0x06;
1.1.1.3   root     10505:                m_CF = 1;
1.1       root     10506:        }
                   10507: }
                   10508: 
                   10509: inline void msdos_int_21h_43h(int lfn)
                   10510: {
1.1.1.45  root     10511:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1       root     10512:        int attr;
                   10513:        
1.1.1.14  root     10514:        if(!lfn && REG8(AL) > 2) {
                   10515:                REG16(AX) = 0x01;
                   10516:                m_CF = 1;
                   10517:                return;
                   10518:        }
                   10519:        switch(REG8(lfn ? BL : AL)) {
1.1       root     10520:        case 0x00:
                   10521:                if((attr = GetFileAttributes(path)) != -1) {
1.1.1.14  root     10522:                        REG16(CX) = (UINT16)msdos_file_attribute_create((UINT16)attr);
                   10523:                } else {
                   10524:                        REG16(AX) = (UINT16)GetLastError();
                   10525:                        m_CF = 1;
                   10526:                }
                   10527:                break;
                   10528:        case 0x01:
                   10529:                if(!SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)))) {
                   10530:                        REG16(AX) = (UINT16)GetLastError();
                   10531:                        m_CF = 1;
                   10532:                }
                   10533:                break;
                   10534:        case 0x02:
                   10535:                {
1.1.1.45  root     10536:                        DWORD compressed_size = GetCompressedFileSize(path, NULL), file_size = 0;
                   10537:                        if(compressed_size != INVALID_FILE_SIZE) {
                   10538:                                if(compressed_size != 0) {
                   10539:                                        HANDLE hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   10540:                                        if(hFile != INVALID_HANDLE_VALUE) {
                   10541:                                                file_size = GetFileSize(hFile, NULL);
                   10542:                                                CloseHandle(hFile);
                   10543:                                        }
                   10544:                                        if(compressed_size == file_size) {
                   10545:                                                DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   10546:                                                // this isn't correct if the file is in the NTFS MFT
                   10547:                                                if(GetDiskFreeSpace(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
                   10548:                                                        compressed_size = ((compressed_size - 1) | (sectors_per_cluster * bytes_per_sector - 1)) + 1;
                   10549:                                                }
1.1.1.14  root     10550:                                        }
                   10551:                                }
1.1.1.45  root     10552:                                REG16(AX) = LOWORD(compressed_size);
                   10553:                                REG16(DX) = HIWORD(compressed_size);
1.1.1.14  root     10554:                        } else {
                   10555:                                REG16(AX) = (UINT16)GetLastError();
                   10556:                                m_CF = 1;
1.1       root     10557:                        }
1.1.1.14  root     10558:                }
                   10559:                break;
                   10560:        case 0x03:
                   10561:        case 0x05:
                   10562:        case 0x07:
                   10563:                {
                   10564:                        HANDLE hFile = CreateFile(path, FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   10565:                        if(hFile != INVALID_HANDLE_VALUE) {
                   10566:                                FILETIME local, time;
                   10567:                                DosDateTimeToFileTime(REG16(DI), /*REG8(BL) == 5 ? 0 : */REG16(CX), &local);
                   10568:                                if(REG8(BL) == 7) {
                   10569:                                        ULARGE_INTEGER hund;
                   10570:                                        hund.LowPart = local.dwLowDateTime;
                   10571:                                        hund.HighPart = local.dwHighDateTime;
                   10572:                                        hund.QuadPart += REG16(SI) * 100000;
                   10573:                                        local.dwLowDateTime = hund.LowPart;
                   10574:                                        local.dwHighDateTime = hund.HighPart;
                   10575:                                }
                   10576:                                LocalFileTimeToFileTime(&local, &time);
                   10577:                                if(!SetFileTime(hFile, REG8(BL) == 0x07 ? &time : NULL,
                   10578:                                                       REG8(BL) == 0x05 ? &time : NULL,
                   10579:                                                       REG8(BL) == 0x03 ? &time : NULL)) {
                   10580:                                        REG16(AX) = (UINT16)GetLastError();
                   10581:                                        m_CF = 1;
                   10582:                                }
                   10583:                                CloseHandle(hFile);
                   10584:                        } else {
                   10585:                                REG16(AX) = (UINT16)GetLastError();
                   10586:                                m_CF = 1;
1.1       root     10587:                        }
1.1.1.14  root     10588:                }
                   10589:                break;
                   10590:        case 0x04:
                   10591:        case 0x06:
                   10592:        case 0x08:
                   10593:                {
                   10594:                        WIN32_FILE_ATTRIBUTE_DATA fad;
                   10595:                        if(GetFileAttributesEx(path, GetFileExInfoStandard, (LPVOID)&fad)) {
                   10596:                                FILETIME *time, local;
                   10597:                                time = REG8(BL) == 0x04 ? &fad.ftLastWriteTime :
                   10598:                                                   0x06 ? &fad.ftLastAccessTime :
                   10599:                                                          &fad.ftCreationTime;
                   10600:                                FileTimeToLocalFileTime(time, &local);
                   10601:                                FileTimeToDosDateTime(&local, &REG16(DI), &REG16(CX));
                   10602:                                if(REG8(BL) == 0x08) {
                   10603:                                        ULARGE_INTEGER hund;
                   10604:                                        hund.LowPart = local.dwLowDateTime;
                   10605:                                        hund.HighPart = local.dwHighDateTime;
                   10606:                                        hund.QuadPart /= 100000;
                   10607:                                        REG16(SI) = (UINT16)(hund.QuadPart % 200);
                   10608:                                }
                   10609:                        } else {
                   10610:                                REG16(AX) = (UINT16)GetLastError();
                   10611:                                m_CF = 1;
1.1       root     10612:                        }
1.1.1.14  root     10613:                }
                   10614:                break;
1.1.1.43  root     10615:        case 0xff:
                   10616:                if(REG16(BP) == 0x5053) {
                   10617:                        if(REG8(CL) == 0x39) {
                   10618:                                msdos_int_21h_39h(1);
                   10619:                                break;
                   10620:                        } else if(REG8(CL) == 0x56) {
                   10621:                                msdos_int_21h_56h(1);
                   10622:                                break;
                   10623:                        }
                   10624:                }
1.1.1.14  root     10625:        default:
1.1.1.22  root     10626:                unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.14  root     10627:                REG16(AX) = 0x01;
                   10628:                m_CF = 1;
                   10629:                break;
                   10630:        }
                   10631: }
                   10632: 
                   10633: inline void msdos_int_21h_44h()
                   10634: {
1.1.1.22  root     10635:        static UINT16 iteration_count = 0;
                   10636:        
1.1.1.44  root     10637:        process_t *process;
                   10638:        int fd, drv;
1.1.1.14  root     10639:        
                   10640:        switch(REG8(AL)) {
                   10641:        case 0x00:
                   10642:        case 0x01:
                   10643:        case 0x02:
                   10644:        case 0x03:
                   10645:        case 0x04:
                   10646:        case 0x05:
                   10647:        case 0x06:
                   10648:        case 0x07:
1.1.1.44  root     10649:                process = msdos_process_info_get(current_psp);
                   10650:                fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1.1.20  root     10651:                if(fd >= process->max_files || !file_handler[fd].valid) {
                   10652:                        REG16(AX) = 0x06;
                   10653:                        m_CF = 1;
                   10654:                        return;
1.1.1.14  root     10655:                }
                   10656:                break;
                   10657:        case 0x08:
                   10658:        case 0x09:
1.1.1.44  root     10659:                drv = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   10660:                if(!msdos_is_valid_drive(drv)) {
                   10661:                        // invalid drive
1.1.1.14  root     10662:                        REG16(AX) = 0x0f;
                   10663:                        m_CF = 1;
                   10664:                        return;
1.1       root     10665:                }
                   10666:                break;
                   10667:        }
                   10668:        switch(REG8(AL)) {
                   10669:        case 0x00: // get ioctrl data
1.1.1.20  root     10670:                REG16(DX) = file_handler[fd].info;
1.1       root     10671:                break;
                   10672:        case 0x01: // set ioctrl data
1.1.1.45  root     10673:                if(REG8(DH) != 0) {
                   10674: //                     REG16(AX) = 0x0d; // data invalid
                   10675: //                     m_CF = 1;
                   10676:                        file_handler[fd].info = REG16(DX);
                   10677:                } else {
                   10678:                        file_handler[fd].info &= 0xff00;
                   10679:                        file_handler[fd].info |= REG8(DL);
                   10680:                }
1.1       root     10681:                break;
                   10682:        case 0x02: // recv from character device
1.1.1.45  root     10683:                if(strstr(file_handler[fd].path, "EMMXXXX0") != NULL && support_ems) {
                   10684:                        // from DOSBox
                   10685:                        switch(*(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX))) {
                   10686:                        case 0x00:
                   10687:                                if(REG16(CX) >= 6) {
                   10688:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x0023;
                   10689:                                        *(UINT32 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = 0x0000;
                   10690:                                        REG16(AX) = 6; // number of bytes actually read
                   10691:                                } else {
                   10692:                                        REG16(AX) = 0x0d; // data invalid
                   10693:                                        m_CF = 1;
                   10694:                                }
                   10695:                                break;
                   10696:                        case 0x01:
                   10697:                                if(REG16(CX) >= 6) {
                   10698:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x000) = 0x0004;     // flags
                   10699:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x002) = 0x019d;     // size of this structure
                   10700:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x004) = 0x0001;     // version 1.0
                   10701:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x006) = 0x00000000; // reserved
                   10702:                                        for(int addr = 0, ofs = 0x00a; addr < 0x100000; addr += 0x4000, ofs += 6) {
                   10703:                                                if(addr >= EMS_TOP && addr < EMS_TOP + EMS_SIZE) {
                   10704:                                                        int page = (addr - EMS_TOP) / 0x4000;
                   10705:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 0) = 0x03;     // frame type: EMS frame in 64k page
                   10706:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 1) = 0xff;     // owner: NONE
                   10707:                                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + ofs + 2) = 0x7fff;   // no logical page number
                   10708:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 4) = page;     // physical EMS page number
                   10709:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 5) = 0x00;     // flags: EMS frame
                   10710:                                                } else {
                   10711:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 0) = 0x00;     // frame type: NONE
                   10712:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 1) = 0xff;     // owner: NONE
                   10713:                                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + ofs + 2) = 0xffff;   // non-EMS frame
                   10714:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 4) = 0xff;     // EMS page number (NONE)
                   10715:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 5) = 0xaa;     // flags: direct mapping
                   10716:                                                }
                   10717:                                        }
                   10718:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18a) = 0x74;       // ??
                   10719:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18b) = 0x00;       // no UMB descriptors following
                   10720:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18c) = 0x01;       // 1 EMS handle info record
                   10721:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x17d) = 0x0000;     // system handle
                   10722:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x18f) = 0x00000000; // handle name
                   10723:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x193) = 0x00000000; // handle name
                   10724:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x197) = 0x0001;     // system handle
                   10725:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x199) = 0x00110000; // physical address
                   10726:                                        
                   10727:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x0018;
                   10728:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = XMS_TOP >> 4;;
                   10729:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4) = 0x0001; // version 1.0
                   10730:                                        REG16(AX) = 6; // number of bytes actually read
                   10731:                                } else {
                   10732:                                        REG16(AX) = 0x0d; // data invalid
                   10733:                                        m_CF = 1;
                   10734:                                }
                   10735:                                break;
                   10736:                        case 0x02:
                   10737:                                if(REG16(CX) >= 2) {
                   10738:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x40; // EMS 4.0
                   10739:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 1) = 0x00;
                   10740:                                        REG16(AX) = 2; // number of bytes actually read
                   10741:                                } else {
                   10742:                                        REG16(AX) = 0x0d; // data invalid
                   10743:                                        m_CF = 1;
                   10744:                                }
                   10745:                                break;
                   10746:                        case 0x03:
                   10747:                                if(REG16(CX) >= 4) {
                   10748:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = MAX_EMS_PAGES * 16; // max size (kb)
                   10749:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = 0x0080; // min size (kb)
                   10750:                                        REG16(AX) = 4; // number of bytes actually read
                   10751:                                } else {
                   10752:                                        REG16(AX) = 0x0d; // data invalid
                   10753:                                        m_CF = 1;
                   10754:                                }
                   10755:                                break;
                   10756:                        default:
                   10757:                                REG16(AX) = 0x01; // function number invalid
                   10758:                                m_CF = 1;
                   10759:                        }
                   10760:                } else if(strstr(file_handler[fd].path, "CONFIG$") != NULL) {
                   10761:                        if(REG16(CX) >= 5) {
                   10762:                                memset(mem + SREG_BASE(DS) + REG16(DX), 0, 5);
                   10763:                                REG16(AX) = 5; // number of bytes actually read
                   10764:                        } else {
                   10765:                                REG16(AX) = 0x0d; // data invalid
                   10766:                                m_CF = 1;
                   10767:                        }
                   10768:                } else {
                   10769: //                     memset(mem + SREG_BASE(DS) + REG16(DX), 0, REG16(CX));
                   10770: //                     REG16(AX) = REG16(CX);
                   10771:                        REG16(AX) = 0x05; // access denied
                   10772:                        m_CF = 1;
                   10773:                }
                   10774:                break;
1.1       root     10775:        case 0x03: // send to character device
1.1.1.45  root     10776: //             REG16(AX) = 0x05;
                   10777: //             m_CF = 1;
                   10778:                REG16(AX) = 0x00; // success
                   10779:                break;
1.1       root     10780:        case 0x04: // recv from block device
                   10781:        case 0x05: // send to block device
                   10782:                REG16(AX) = 0x05;
1.1.1.3   root     10783:                m_CF = 1;
1.1       root     10784:                break;
                   10785:        case 0x06: // get read status
1.1.1.20  root     10786:                if(file_mode[file_handler[fd].mode].in) {
                   10787:                        if(file_handler[fd].atty) {
1.1.1.14  root     10788:                                REG8(AL) = msdos_kbhit() ? 0xff : 0x00;
1.1       root     10789:                        } else {
1.1.1.20  root     10790:                                REG8(AL) = eof(fd) ? 0x00 : 0xff;
1.1       root     10791:                        }
1.1.1.14  root     10792:                } else {
                   10793:                        REG8(AL) = 0x00;
1.1       root     10794:                }
                   10795:                break;
                   10796:        case 0x07: // get write status
1.1.1.20  root     10797:                if(file_mode[file_handler[fd].mode].out) {
1.1.1.14  root     10798:                        REG8(AL) = 0xff;
                   10799:                } else {
                   10800:                        REG8(AL) = 0x00;
1.1       root     10801:                }
                   10802:                break;
                   10803:        case 0x08: // check removable drive
1.1.1.44  root     10804:                if(msdos_is_removable_drive(drv) || msdos_is_cdrom_drive(drv)) {
1.1.1.14  root     10805:                        // removable drive
                   10806:                        REG16(AX) = 0x00;
1.1       root     10807:                } else {
1.1.1.14  root     10808:                        // fixed drive
                   10809:                        REG16(AX) = 0x01;
1.1       root     10810:                }
                   10811:                break;
                   10812:        case 0x09: // check remote drive
1.1.1.44  root     10813:                if(msdos_is_remote_drive(drv)) {
1.1.1.14  root     10814:                        // remote drive
                   10815:                        REG16(DX) = 0x1000;
1.1.1.44  root     10816:                } else if(msdos_is_subst_drive(drv)) {
                   10817:                        // subst drive
                   10818:                        REG16(DX) = 0x8000;
1.1       root     10819:                } else {
1.1.1.14  root     10820:                        // local drive
1.1.1.44  root     10821:                        REG16(DX) = 0x0000;
1.1       root     10822:                }
                   10823:                break;
1.1.1.21  root     10824:        case 0x0a: // check remote handle
1.1.1.45  root     10825:                if(!(file_handler[fd].info & 0x8000) && msdos_is_remote_drive(msdos_drive_number(file_handler[fd].path))) {
                   10826:                        REG16(DX) = 0x8000;
                   10827:                } else {
                   10828:                        REG16(DX) = 0x0000;
                   10829:                }
1.1.1.21  root     10830:                break;
1.1       root     10831:        case 0x0b: // set retry count
                   10832:                break;
1.1.1.22  root     10833:        case 0x0c: // generic character device request
                   10834:                if(REG8(CL) == 0x45) {
                   10835:                        // set iteration (retry) count
                   10836:                        iteration_count = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
                   10837:                } else if(REG8(CL) == 0x4a) {
                   10838:                        // select code page
                   10839:                        active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2);
                   10840:                        msdos_nls_tables_update();
1.1.1.44  root     10841:                } else if(REG8(CL) == 0x4c) {
                   10842:                        // start code-page preparation
                   10843:                        int ids[3] = {437, 0, 0}; // 437: US English
                   10844:                        int count = 1, offset = 0;
                   10845:                        if(active_code_page != 437) {
                   10846:                                ids[count++] = active_code_page;
                   10847:                        }
                   10848:                        if(system_code_page != 437 && system_code_page != active_code_page) {
                   10849:                                ids[count++] = system_code_page;
                   10850:                        }
                   10851:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 0;
                   10852:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 2 + 2 * count;
                   10853:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   10854:                        for(int i = 0; i < count; i++) {
                   10855:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   10856:                        }
                   10857:                } else if(REG8(CL) == 0x4d) {
                   10858:                        // end code-page preparation
                   10859:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2;
                   10860:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
1.1.1.22  root     10861:                } else if(REG8(CL) == 0x65) {
                   10862:                        // get iteration (retry) count
                   10863:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX)) = iteration_count;
                   10864:                } else if(REG8(CL) == 0x6a) {
                   10865:                        // query selected code page
                   10866:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2; // FIXME
                   10867:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
                   10868:                        
                   10869:                        CPINFO info;
                   10870:                        GetCPInfo(active_code_page, &info);
                   10871:                        
                   10872:                        if(info.MaxCharSize != 1) {
                   10873:                                for(int i = 0;; i++) {
                   10874:                                        UINT8 lo = info.LeadByte[2 * i + 0];
                   10875:                                        UINT8 hi = info.LeadByte[2 * i + 1];
                   10876:                                        
                   10877:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 0) = lo;
                   10878:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 2) = hi;
                   10879:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) + 4;
                   10880:                                        
                   10881:                                        if(lo == 0 && hi == 0) {
                   10882:                                                break;
                   10883:                                        }
                   10884:                                }
                   10885:                        }
1.1.1.44  root     10886:                } else if(REG8(CL) == 0x6b) {
                   10887:                        // query prepare list
                   10888:                        int ids[3] = {437, 0, 0}; // 437: US English
                   10889:                        int count = 1, offset = 0;
                   10890:                        if(active_code_page != 437) {
                   10891:                                ids[count++] = active_code_page;
                   10892:                        }
                   10893:                        if(system_code_page != 437 && system_code_page != active_code_page) {
                   10894:                                ids[count++] = system_code_page;
                   10895:                        }
                   10896:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 2 * (2 + 2 * count);
                   10897:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   10898:                        for(int i = 0; i < count; i++) {
                   10899:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   10900:                        }
                   10901:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   10902:                        for(int i = 0; i < count; i++) {
                   10903:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   10904:                        }
1.1.1.22  root     10905:                } else if(REG8(CL) == 0x7f) {
1.1.1.44  root     10906:                        // get display information
1.1.1.22  root     10907:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00) = 0;
                   10908:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x01) = 0;
                   10909:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) = 14;
                   10910:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04) = 1;
                   10911:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06) = 1;
                   10912:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x07) = 0;
                   10913:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08) = 4;
                   10914:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a) =  8 * (*(UINT16 *)(mem + 0x44a));
                   10915:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c) = 16 * (*(UINT8  *)(mem + 0x484) + 1);
                   10916:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e) = *(UINT16 *)(mem + 0x44a);
                   10917:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10) = *(UINT8  *)(mem + 0x484) + 1;
                   10918:                } else {
                   10919:                        unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   10920:                        REG16(AX) = 0x01; // invalid function
                   10921:                        m_CF = 1;
                   10922:                }
                   10923:                break;
                   10924:        case 0x0d: // generic block device request
                   10925:                if(REG8(CL) == 0x40) {
                   10926:                        // set device parameters
                   10927:                } else if(REG8(CL) == 0x46) {
                   10928:                        // set volume serial number
                   10929:                } else if(REG8(CL) == 0x4a) {
                   10930:                        // lock logical volume
                   10931:                } else if(REG8(CL) == 0x4b) {
                   10932:                        // lock physical volume
                   10933:                } else if(REG8(CL) == 0x60) {
                   10934:                        // get device parameters
1.1.1.42  root     10935:                        int drive_num = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
1.1.1.22  root     10936:                        
1.1.1.42  root     10937:                        if(pcbios_update_drive_param(drive_num, 1)) {
                   10938:                                drive_param_t *drive_param = &drive_params[drive_num];
                   10939:                                DISK_GEOMETRY *geo = &drive_param->geometry;
                   10940:                                
                   10941:                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x07; // ???
                   10942:                                switch(geo->MediaType) {
                   10943:                                case F5_360_512:
                   10944:                                case F5_320_512:
                   10945:                                case F5_320_1024:
                   10946:                                case F5_180_512:
                   10947:                                case F5_160_512:
                   10948:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // 320K/360K disk
                   10949:                                        break;
                   10950:                                case F5_1Pt2_512:
                   10951:                                case F3_1Pt2_512:
                   10952:                                case F3_1Pt23_1024:
                   10953:                                case F5_1Pt23_1024:
                   10954:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x01; // 1.2M disk
                   10955:                                        break;
                   10956:                                case F3_720_512:
                   10957:                                case F3_640_512:
                   10958:                                case F5_640_512:
                   10959:                                case F5_720_512:
                   10960:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02; // 720K disk
                   10961:                                        break;
                   10962:                                case F8_256_128:
                   10963:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x03; // single-density 8-inch disk
                   10964:                                        break;
                   10965:                                case FixedMedia:
                   10966:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   10967:                                        break;
                   10968:                                case F3_1Pt44_512:
                   10969:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   10970:                                        break;
                   10971:                                case F3_2Pt88_512:
                   10972:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09; // (DOS 5+) 2.88M floppy
                   10973:                                        break;
                   10974:                                default:
                   10975:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   10976: //                                     *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   10977:                                        break;
1.1.1.22  root     10978:                                }
1.1.1.42  root     10979:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = (geo->MediaType == FixedMedia) ? 0x01 : 0x00;
                   10980:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04) = (geo->Cylinders.QuadPart > 0xffff) ? 0xffff : geo->Cylinders.QuadPart;
                   10981:                                switch(geo->MediaType) {
                   10982:                                case F5_360_512:
                   10983:                                case F5_320_512:
                   10984:                                case F5_320_1024:
                   10985:                                case F5_180_512:
                   10986:                                case F5_160_512:
                   10987:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x01; // 320K/360K disk
                   10988:                                        break;
                   10989:                                default:
                   10990:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x00; // other drive types
                   10991:                                        break;
                   10992:                                }
                   10993:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x00) = geo->BytesPerSector;
                   10994:                                *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x02) = geo->SectorsPerTrack * geo->TracksPerCylinder;
                   10995:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x03) = 0;
                   10996:                                *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x05) = 0;
                   10997:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x06) = 0;
                   10998:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x08) = 0;
                   10999:                                switch(geo->MediaType) {
                   11000:                                case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   11001:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xff;
                   11002:                                        break;
                   11003:                                case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   11004:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfe;
                   11005:                                        break;
                   11006:                                case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   11007:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfd;
                   11008:                                        break;
                   11009:                                case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   11010:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfc;
                   11011:                                        break;
                   11012:                                case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   11013:                                case F3_1Pt2_512:
                   11014:                                case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   11015:                                case F5_720_512:
                   11016:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf9;
                   11017:                                        break;
                   11018:                                case FixedMedia:        // hard disk
                   11019:                                case RemovableMedia:
                   11020:                                case Unknown:
                   11021:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf8;
                   11022:                                        break;
                   11023:                                default:
                   11024:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf0;
                   11025:                                        break;
                   11026:                                }
                   11027:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0d) = geo->SectorsPerTrack;
                   11028:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0f) = 1;
                   11029:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x11) = 0;
                   11030:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x15) = geo->TracksPerCylinder * geo->Cylinders.QuadPart;
                   11031:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x1f) = geo->Cylinders.QuadPart;
                   11032:                                // 21h  BYTE    device type
                   11033:                                // 22h  WORD    device attributes (removable or not, etc)
1.1.1.22  root     11034:                        } else {
                   11035:                                REG16(AX) = 0x0f; // invalid drive
                   11036:                                m_CF = 1;
                   11037:                        }
                   11038:                } else if(REG8(CL) == 0x66) {
                   11039:                        // get volume serial number
                   11040:                        char path[] = "A:\\";
                   11041:                        char volume_label[MAX_PATH];
                   11042:                        DWORD serial_number = 0;
                   11043:                        char file_system[MAX_PATH];
                   11044:                        
                   11045:                        path[0] = 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   11046:                        
                   11047:                        if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
                   11048:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   11049:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = serial_number;
                   11050:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x06, 0x20, 11);
                   11051:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x06, volume_label, min(strlen(volume_label), 11));
                   11052:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x11, 0x20,  8);
                   11053:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x11, file_system, min(strlen(file_system), 8));
                   11054:                        } else {
                   11055:                                REG16(AX) = 0x0f; // invalid drive
                   11056:                                m_CF = 1;
                   11057:                        }
                   11058:                } else if(REG8(CL) == 0x67) {
                   11059:                        // get access flag
                   11060:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   11061:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 1;
                   11062:                } else if(REG8(CL) == 0x68) {
                   11063:                        // sense media type
1.1.1.42  root     11064:                        int drive_num = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
1.1.1.22  root     11065:                        
1.1.1.42  root     11066:                        if(pcbios_update_drive_param(drive_num, 1)) {
                   11067:                                drive_param_t *drive_param = &drive_params[drive_num];
                   11068:                                DISK_GEOMETRY *geo = &drive_param->geometry;
                   11069:                                
                   11070:                                switch(geo->MediaType) {
                   11071:                                case F3_720_512:
                   11072:                                case F5_720_512:
                   11073:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11074:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02;
                   11075:                                        break;
                   11076:                                case F3_1Pt44_512:
                   11077:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11078:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07;
                   11079:                                        break;
                   11080:                                case F3_2Pt88_512:
                   11081:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11082:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09;
                   11083:                                        break;
                   11084:                                default:
                   11085:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x00;
                   11086:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // ???
                   11087:                                        break;
1.1.1.22  root     11088:                                }
                   11089:                        } else {
                   11090:                                REG16(AX) = 0x0f; // invalid drive
                   11091:                                m_CF = 1;
                   11092:                        }
                   11093:                } else if(REG8(CL) == 0x6a) {
                   11094:                        // unlock logical volume
                   11095:                } else if(REG8(CL) == 0x6b) {
                   11096:                        // unlock physical volume
                   11097:                } else {
                   11098:                        unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   11099:                        REG16(AX) = 0x01; // invalid function
                   11100:                        m_CF = 1;
                   11101:                }
                   11102:                break;
                   11103:        case 0x0e: // get logical drive map
1.1.1.44  root     11104:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   11105:                        REG16(AX) = 0x0f; // invalid drive
                   11106:                        m_CF = 1;
                   11107:                } else {
                   11108:                        REG8(AL) = 0;
1.1.1.22  root     11109:                }
                   11110:                break;
                   11111:        case 0x0f: // set logical drive map
1.1.1.44  root     11112:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   11113:                        REG16(AX) = 0x0f; // invalid drive
                   11114:                        m_CF = 1;
1.1.1.22  root     11115:                }
                   11116:                break;
                   11117:        case 0x10: // query generic ioctrl capability (handle)
                   11118:                switch(REG8(CL)) {
                   11119:                case 0x45:
                   11120:                case 0x4a:
                   11121:                case 0x65:
                   11122:                case 0x6a:
                   11123:                case 0x7f:
                   11124:                        REG16(AX) = 0x0000; // supported
                   11125:                        break;
                   11126:                default:
                   11127:                        REG8(AL) = 0x01; // ioctl capability not available
                   11128:                        m_CF = 1;
                   11129:                        break;
                   11130:                }
                   11131:                break;
                   11132:        case 0x11: // query generic ioctrl capability (drive)
                   11133:                switch(REG8(CL)) {
                   11134:                case 0x40:
                   11135:                case 0x46:
                   11136:                case 0x4a:
                   11137:                case 0x4b:
                   11138:                case 0x60:
                   11139:                case 0x66:
                   11140:                case 0x67:
                   11141:                case 0x68:
                   11142:                case 0x6a:
                   11143:                case 0x6b:
                   11144:                        REG16(AX) = 0x0000; // supported
                   11145:                        break;
                   11146:                default:
                   11147:                        REG8(AL) = 0x01; // ioctl capability not available
                   11148:                        m_CF = 1;
                   11149:                        break;
                   11150:                }
                   11151:                break;
                   11152:        case 0x12: // determine dos type
                   11153:        case 0x51: // concurrent dos v3.2+ - installation check
                   11154:        case 0x52: // determine dos type/get dr dos versuin
                   11155:                REG16(AX) = 0x01; // this  is not DR-DOS
                   11156:                m_CF = 1;
                   11157:                break;
1.1       root     11158:        default:
1.1.1.22  root     11159:                unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1       root     11160:                REG16(AX) = 0x01;
1.1.1.3   root     11161:                m_CF = 1;
1.1       root     11162:                break;
                   11163:        }
                   11164: }
                   11165: 
                   11166: inline void msdos_int_21h_45h()
                   11167: {
                   11168:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11169:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11170:        
1.1.1.20  root     11171:        if(fd < process->max_files && file_handler[fd].valid) {
                   11172:                int dup_fd = _dup(fd);
                   11173:                if(dup_fd != -1) {
                   11174:                        REG16(AX) = dup_fd;
                   11175:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   11176: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   11177:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     11178:                } else {
                   11179:                        REG16(AX) = errno;
1.1.1.3   root     11180:                        m_CF = 1;
1.1       root     11181:                }
                   11182:        } else {
                   11183:                REG16(AX) = 0x06;
1.1.1.3   root     11184:                m_CF = 1;
1.1       root     11185:        }
                   11186: }
                   11187: 
                   11188: inline void msdos_int_21h_46h()
                   11189: {
                   11190:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11191:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   11192:        int dup_fd = REG16(CX);
                   11193:        int tmp_fd = msdos_psp_get_file_table(REG16(CX), current_psp);
1.1       root     11194:        
1.1.1.20  root     11195:        if(REG16(BX) == REG16(CX)) {
                   11196:                REG16(AX) = 0x06;
                   11197:                m_CF = 1;
                   11198:        } else if(fd < process->max_files && file_handler[fd].valid && dup_fd < process->max_files) {
                   11199:                if(tmp_fd < process->max_files && file_handler[tmp_fd].valid) {
                   11200:                        _close(tmp_fd);
                   11201:                        msdos_file_handler_close(tmp_fd);
                   11202:                        msdos_psp_set_file_table(dup_fd, 0x0ff, current_psp);
                   11203:                }
                   11204:                if(_dup2(fd, dup_fd) != -1) {
                   11205:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   11206: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   11207:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     11208:                } else {
                   11209:                        REG16(AX) = errno;
1.1.1.3   root     11210:                        m_CF = 1;
1.1       root     11211:                }
                   11212:        } else {
                   11213:                REG16(AX) = 0x06;
1.1.1.3   root     11214:                m_CF = 1;
1.1       root     11215:        }
                   11216: }
                   11217: 
                   11218: inline void msdos_int_21h_47h(int lfn)
                   11219: {
                   11220:        char path[MAX_PATH];
                   11221:        
                   11222:        if(_getdcwd(REG8(DL), path, MAX_PATH) != NULL) {
1.1.1.45  root     11223:                if(!lfn) {
                   11224:                        strcpy(path, msdos_short_path(path));
                   11225:                }
1.1       root     11226:                if(path[1] == ':') {
                   11227:                        // the returned path does not include a drive or the initial backslash
1.1.1.45  root     11228:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), path + 3);
1.1       root     11229:                } else {
1.1.1.45  root     11230:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), path);
1.1       root     11231:                }
                   11232:        } else {
                   11233:                REG16(AX) = errno;
1.1.1.3   root     11234:                m_CF = 1;
1.1       root     11235:        }
                   11236: }
                   11237: 
                   11238: inline void msdos_int_21h_48h()
                   11239: {
1.1.1.19  root     11240:        int seg, umb_linked;
1.1       root     11241:        
1.1.1.8   root     11242:        if((malloc_strategy & 0xf0) == 0x00) {
1.1.1.19  root     11243:                // unlink umb not to allocate memory in umb
                   11244:                if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   11245:                        msdos_mem_unlink_umb();
                   11246:                }
1.1.1.8   root     11247:                if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   11248:                        REG16(AX) = seg;
                   11249:                } else {
                   11250:                        REG16(AX) = 0x08;
                   11251:                        REG16(BX) = msdos_mem_get_free(first_mcb, 0);
                   11252:                        m_CF = 1;
                   11253:                }
1.1.1.19  root     11254:                if(umb_linked != 0) {
                   11255:                        msdos_mem_link_umb();
                   11256:                }
1.1.1.8   root     11257:        } else if((malloc_strategy & 0xf0) == 0x40) {
                   11258:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   11259:                        REG16(AX) = seg;
                   11260:                } else {
                   11261:                        REG16(AX) = 0x08;
                   11262:                        REG16(BX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   11263:                        m_CF = 1;
                   11264:                }
                   11265:        } else if((malloc_strategy & 0xf0) == 0x80) {
                   11266:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   11267:                        REG16(AX) = seg;
                   11268:                } else if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   11269:                        REG16(AX) = seg;
                   11270:                } else {
                   11271:                        REG16(AX) = 0x08;
                   11272:                        REG16(BX) = max(msdos_mem_get_free(UMB_TOP >> 4, 0), msdos_mem_get_free(first_mcb, 0));
                   11273:                        m_CF = 1;
                   11274:                }
1.1       root     11275:        }
                   11276: }
                   11277: 
                   11278: inline void msdos_int_21h_49h()
                   11279: {
1.1.1.14  root     11280:        int mcb_seg = SREG(ES) - 1;
                   11281:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   11282:        
                   11283:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   11284:                msdos_mem_free(SREG(ES));
                   11285:        } else {
1.1.1.33  root     11286:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.14  root     11287:                m_CF = 1;
                   11288:        }
1.1       root     11289: }
                   11290: 
                   11291: inline void msdos_int_21h_4ah()
                   11292: {
1.1.1.14  root     11293:        int mcb_seg = SREG(ES) - 1;
                   11294:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1       root     11295:        int max_paragraphs;
                   11296:        
1.1.1.14  root     11297:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   11298:                if(msdos_mem_realloc(SREG(ES), REG16(BX), &max_paragraphs)) {
                   11299:                        REG16(AX) = 0x08;
                   11300:                        REG16(BX) = max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs;
                   11301:                        m_CF = 1;
                   11302:                }
                   11303:        } else {
1.1.1.33  root     11304:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.3   root     11305:                m_CF = 1;
1.1       root     11306:        }
                   11307: }
                   11308: 
                   11309: inline void msdos_int_21h_4bh()
                   11310: {
1.1.1.3   root     11311:        char *command = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   11312:        param_block_t *param = (param_block_t *)(mem + SREG_BASE(ES) + REG16(BX));
1.1       root     11313:        
                   11314:        switch(REG8(AL)) {
                   11315:        case 0x00:
                   11316:        case 0x01:
                   11317:                if(msdos_process_exec(command, param, REG8(AL))) {
                   11318:                        REG16(AX) = 0x02;
1.1.1.3   root     11319:                        m_CF = 1;
1.1       root     11320:                }
                   11321:                break;
1.1.1.14  root     11322:        case 0x03:
                   11323:                {
                   11324:                        int fd;
                   11325:                        if((fd = _open(command, _O_RDONLY | _O_BINARY)) == -1) {
                   11326:                                REG16(AX) = 0x02;
                   11327:                                m_CF = 1;
                   11328:                                break;
                   11329:                        }
                   11330:                        int size = _read(fd, file_buffer, sizeof(file_buffer));
                   11331:                        _close(fd);
                   11332:                        
                   11333:                        UINT16 *overlay = (UINT16 *)param;
                   11334:                        
                   11335:                        // check exe header
                   11336:                        exe_header_t *header = (exe_header_t *)file_buffer;
                   11337:                        int header_size = 0;
                   11338:                        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   11339:                                header_size = header->header_size * 16;
                   11340:                                // relocation
                   11341:                                int start_seg = overlay[1];
                   11342:                                for(int i = 0; i < header->relocations; i++) {
                   11343:                                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   11344:                                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   11345:                                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   11346:                                }
                   11347:                        }
                   11348:                        memcpy(mem + (overlay[0] << 4), file_buffer + header_size, size - header_size);
                   11349:                }
                   11350:                break;
1.1.1.43  root     11351:        case 0xfd:
                   11352:        case 0xfe:
                   11353:                // unknown function called in FreeCOM
                   11354:                REG16(AX) = 0x01;
                   11355:                m_CF = 1;
                   11356:                break;
1.1       root     11357:        default:
1.1.1.22  root     11358:                unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1       root     11359:                REG16(AX) = 0x01;
1.1.1.3   root     11360:                m_CF = 1;
1.1       root     11361:                break;
                   11362:        }
                   11363: }
                   11364: 
                   11365: inline void msdos_int_21h_4ch()
                   11366: {
                   11367:        msdos_process_terminate(current_psp, REG8(AL), 1);
                   11368: }
                   11369: 
                   11370: inline void msdos_int_21h_4dh()
                   11371: {
                   11372:        REG16(AX) = retval;
                   11373: }
                   11374: 
                   11375: inline void msdos_int_21h_4eh()
                   11376: {
                   11377:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     11378:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   11379:        find_t *find = (find_t *)(mem + dta_laddr);
1.1.1.45  root     11380:        const char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     11381:        WIN32_FIND_DATA fd;
                   11382:        
1.1.1.14  root     11383:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   11384:        find->find_magic = FIND_MAGIC;
                   11385:        find->dta_index = dtainfo - dtalist;
1.1       root     11386:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     11387:        dtainfo->allowable_mask = REG8(CL);
                   11388:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     11389:        
1.1.1.14  root     11390:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   11391:                dtainfo->allowable_mask &= ~8;
1.1       root     11392:        }
1.1.1.14  root     11393:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   11394:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     11395:                      !msdos_find_file_has_8dot3name(&fd)) {
                   11396:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   11397:                                FindClose(dtainfo->find_handle);
                   11398:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11399:                                break;
                   11400:                        }
                   11401:                }
                   11402:        }
1.1.1.13  root     11403:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     11404:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   11405:                msdos_find_file_conv_local_time(&fd);
                   11406:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   11407:                find->size = fd.nFileSizeLow;
1.1.1.13  root     11408:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     11409:                REG16(AX) = 0;
1.1.1.14  root     11410:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     11411:                find->attrib = 8;
                   11412:                find->size = 0;
                   11413:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     11414:                dtainfo->allowable_mask &= ~8;
1.1       root     11415:                REG16(AX) = 0;
                   11416:        } else {
                   11417:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     11418:                m_CF = 1;
1.1       root     11419:        }
                   11420: }
                   11421: 
                   11422: inline void msdos_int_21h_4fh()
                   11423: {
                   11424:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     11425:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   11426:        find_t *find = (find_t *)(mem + dta_laddr);
1.1       root     11427:        WIN32_FIND_DATA fd;
                   11428:        
1.1.1.14  root     11429:        if(find->find_magic != FIND_MAGIC || find->dta_index >= MAX_DTAINFO) {
                   11430:                REG16(AX) = 0x12;
                   11431:                m_CF = 1;
                   11432:                return;
                   11433:        }
                   11434:        dtainfo_t *dtainfo = &dtalist[find->dta_index];
1.1.1.13  root     11435:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   11436:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     11437:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     11438:                              !msdos_find_file_has_8dot3name(&fd)) {
                   11439:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   11440:                                        FindClose(dtainfo->find_handle);
                   11441:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11442:                                        break;
                   11443:                                }
                   11444:                        }
                   11445:                } else {
1.1.1.13  root     11446:                        FindClose(dtainfo->find_handle);
                   11447:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11448:                }
                   11449:        }
1.1.1.13  root     11450:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     11451:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   11452:                msdos_find_file_conv_local_time(&fd);
                   11453:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   11454:                find->size = fd.nFileSizeLow;
1.1.1.13  root     11455:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     11456:                REG16(AX) = 0;
1.1.1.14  root     11457:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     11458:                find->attrib = 8;
                   11459:                find->size = 0;
                   11460:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     11461:                dtainfo->allowable_mask &= ~8;
1.1       root     11462:                REG16(AX) = 0;
                   11463:        } else {
                   11464:                REG16(AX) = 0x12;
1.1.1.3   root     11465:                m_CF = 1;
1.1       root     11466:        }
                   11467: }
                   11468: 
                   11469: inline void msdos_int_21h_50h()
                   11470: {
1.1.1.8   root     11471:        if(current_psp != REG16(BX)) {
                   11472:                process_t *process = msdos_process_info_get(current_psp);
                   11473:                if(process != NULL) {
                   11474:                        process->psp = REG16(BX);
                   11475:                }
                   11476:                current_psp = REG16(BX);
1.1.1.23  root     11477:                msdos_sda_update(current_psp);
1.1.1.8   root     11478:        }
1.1       root     11479: }
                   11480: 
                   11481: inline void msdos_int_21h_51h()
                   11482: {
                   11483:        REG16(BX) = current_psp;
                   11484: }
                   11485: 
                   11486: inline void msdos_int_21h_52h()
                   11487: {
1.1.1.25  root     11488:        SREG(ES) = DOS_INFO_TOP >> 4;
1.1.1.3   root     11489:        i386_load_segment_descriptor(ES);
1.1.1.25  root     11490:        REG16(BX) = offsetof(dos_info_t, first_dpb);
1.1       root     11491: }
                   11492: 
1.1.1.43  root     11493: inline void msdos_int_21h_53h()
                   11494: {
                   11495:        dpb_t *dpb = (dpb_t *)(mem + SREG_BASE(ES) + REG16(BP));
                   11496:        bpb_t *bpb = (bpb_t *)(mem + SREG_BASE(DS) + REG16(SI));
                   11497:        
                   11498:        dpb->bytes_per_sector = bpb->bytes_per_sector;
                   11499:        dpb->highest_sector_num = bpb->sectors_per_track - 1;
                   11500:        dpb->shift_count = 0;
                   11501:        dpb->reserved_sectors = 0;
                   11502:        dpb->fat_num = bpb->fat_num;
                   11503:        dpb->root_entries = bpb->root_entries;
                   11504:        dpb->first_data_sector = 0;
                   11505:        if(bpb->sectors_per_cluster != 0) {
                   11506:                dpb->highest_cluster_num = (UINT16)(((REG16(CX) == 0x4558 && bpb->total_sectors == 0) ? bpb->ext_total_sectors : bpb->total_sectors) / bpb->sectors_per_cluster + 1);
                   11507:        } else {
                   11508:                dpb->highest_cluster_num = 0;
                   11509:        }
                   11510:        dpb->sectors_per_fat = (REG16(CX) == 0x4558 && bpb->sectors_per_fat == 0) ? bpb->ext_sectors_per_fat : bpb->sectors_per_fat;
                   11511:        dpb->first_dir_sector = 0;
                   11512:        dpb->device_driver_header = 0;
                   11513:        dpb->media_type = bpb->media_type;
                   11514:        dpb->drive_accessed = 0;
                   11515:        dpb->next_dpb_ofs = 0xffff;
                   11516:        dpb->next_dpb_seg = 0xffff;
                   11517:        dpb->first_free_cluster = 0;
                   11518:        dpb->free_clusters = 0xffff;
                   11519: }
                   11520: 
1.1       root     11521: inline void msdos_int_21h_54h()
                   11522: {
                   11523:        process_t *process = msdos_process_info_get(current_psp);
                   11524:        
                   11525:        REG8(AL) = process->verify;
                   11526: }
                   11527: 
                   11528: inline void msdos_int_21h_55h()
                   11529: {
                   11530:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   11531:        
                   11532:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
                   11533:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   11534:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   11535:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   11536:        psp->parent_psp = current_psp;
                   11537: }
                   11538: 
                   11539: inline void msdos_int_21h_56h(int lfn)
                   11540: {
                   11541:        char src[MAX_PATH], dst[MAX_PATH];
1.1.1.3   root     11542:        strcpy(src, msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn));
                   11543:        strcpy(dst, msdos_trimmed_path((char *)(mem + SREG_BASE(ES) + REG16(DI)), lfn));
1.1       root     11544:        
                   11545:        if(rename(src, dst)) {
                   11546:                REG16(AX) = errno;
1.1.1.3   root     11547:                m_CF = 1;
1.1       root     11548:        }
                   11549: }
                   11550: 
                   11551: inline void msdos_int_21h_57h()
                   11552: {
                   11553:        FILETIME time, local;
1.1.1.14  root     11554:        FILETIME *ctime, *atime, *mtime;
1.1.1.21  root     11555:        HANDLE hHandle;
1.1       root     11556:        
1.1.1.21  root     11557:        if((hHandle = (HANDLE)_get_osfhandle(REG16(BX))) == INVALID_HANDLE_VALUE) {
1.1.1.14  root     11558:                REG16(AX) = (UINT16)GetLastError();
                   11559:                m_CF = 1;
                   11560:                return;
                   11561:        }
                   11562:        ctime = atime = mtime = NULL;
                   11563:        
1.1       root     11564:        switch(REG8(AL)) {
                   11565:        case 0x00:
1.1.1.6   root     11566:        case 0x01:
1.1.1.14  root     11567:                mtime = &time;
1.1.1.6   root     11568:                break;
                   11569:        case 0x04:
                   11570:        case 0x05:
1.1.1.14  root     11571:                atime = &time;
1.1       root     11572:                break;
1.1.1.6   root     11573:        case 0x06:
                   11574:        case 0x07:
1.1.1.14  root     11575:                ctime = &time;
                   11576:                break;
                   11577:        default:
1.1.1.22  root     11578:                unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.14  root     11579:                REG16(AX) = 0x01;
                   11580:                m_CF = 1;
                   11581:                return;
                   11582:        }
                   11583:        if(REG8(AL) & 1) {
1.1       root     11584:                DosDateTimeToFileTime(REG16(DX), REG16(CX), &local);
                   11585:                LocalFileTimeToFileTime(&local, &time);
1.1.1.21  root     11586:                if(!SetFileTime(hHandle, ctime, atime, mtime)) {
1.1       root     11587:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     11588:                        m_CF = 1;
1.1       root     11589:                }
1.1.1.14  root     11590:        } else {
1.1.1.21  root     11591:                if(!GetFileTime(hHandle, ctime, atime, mtime)) {
1.1.1.14  root     11592:                        // assume a device and use the current time
                   11593:                        GetSystemTimeAsFileTime(&time);
                   11594:                }
                   11595:                FileTimeToLocalFileTime(&time, &local);
                   11596:                FileTimeToDosDateTime(&local, &REG16(DX), &REG16(CX));
1.1       root     11597:        }
                   11598: }
                   11599: 
                   11600: inline void msdos_int_21h_58h()
                   11601: {
                   11602:        switch(REG8(AL)) {
                   11603:        case 0x00:
1.1.1.7   root     11604:                REG16(AX) = malloc_strategy;
                   11605:                break;
                   11606:        case 0x01:
1.1.1.24  root     11607: //             switch(REG16(BX)) {
                   11608:                switch(REG8(BL)) {
1.1.1.7   root     11609:                case 0x0000:
                   11610:                case 0x0001:
                   11611:                case 0x0002:
                   11612:                case 0x0040:
                   11613:                case 0x0041:
                   11614:                case 0x0042:
                   11615:                case 0x0080:
                   11616:                case 0x0081:
                   11617:                case 0x0082:
                   11618:                        malloc_strategy = REG16(BX);
1.1.1.23  root     11619:                        msdos_sda_update(current_psp);
1.1.1.7   root     11620:                        break;
                   11621:                default:
1.1.1.22  root     11622:                        unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.7   root     11623:                        REG16(AX) = 0x01;
                   11624:                        m_CF = 1;
                   11625:                        break;
                   11626:                }
                   11627:                break;
                   11628:        case 0x02:
1.1.1.19  root     11629:                REG8(AL) = msdos_mem_get_umb_linked() ? 1 : 0;
1.1.1.7   root     11630:                break;
                   11631:        case 0x03:
1.1.1.24  root     11632: //             switch(REG16(BX)) {
                   11633:                switch(REG8(BL)) {
1.1.1.7   root     11634:                case 0x0000:
1.1.1.19  root     11635:                        msdos_mem_unlink_umb();
                   11636:                        break;
1.1.1.7   root     11637:                case 0x0001:
1.1.1.19  root     11638:                        msdos_mem_link_umb();
1.1.1.7   root     11639:                        break;
                   11640:                default:
1.1.1.22  root     11641:                        unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.7   root     11642:                        REG16(AX) = 0x01;
                   11643:                        m_CF = 1;
                   11644:                        break;
                   11645:                }
1.1       root     11646:                break;
                   11647:        default:
1.1.1.22  root     11648:                unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1       root     11649:                REG16(AX) = 0x01;
1.1.1.3   root     11650:                m_CF = 1;
1.1       root     11651:                break;
                   11652:        }
                   11653: }
                   11654: 
                   11655: inline void msdos_int_21h_59h()
                   11656: {
1.1.1.47! root     11657:        if(REG16(BX) == 0x0000) {
        !          11658:                sda_t *sda = (sda_t *)(mem + SDA_TOP);
        !          11659:                
        !          11660:                REG16(AX) = sda->extended_error_code;
        !          11661:                REG8(BH)  = sda->error_class;
        !          11662:                REG8(BL)  = sda->suggested_action;
        !          11663:                REG8(CH)  = sda->locus_of_last_error;
        !          11664:                // XXX: GW-BASIC 3.23 invites the registers contents of CL, DX, SI, DI, DS, and ES are not destroyed
        !          11665:                if(sda->int21h_5d0ah_called != 0) {
        !          11666:                        REG8(CL)  = sda->int21h_5d0ah_cl;
        !          11667:                        REG16(DX) = sda->int21h_5d0ah_dx;
        !          11668: //                     REG16(SI) = sda->int21h_5d0ah_si;
        !          11669:                        REG16(DI) = sda->last_error_pointer.w.l;
        !          11670: //                     SREG(DS)  = sda->int21h_5d0ah_ds;
        !          11671: //                     i386_load_segment_descriptor(DS);
        !          11672:                        SREG(ES)  = sda->last_error_pointer.w.h;
        !          11673:                        i386_load_segment_descriptor(ES);
        !          11674:                }
        !          11675:                sda->int21h_5d0ah_called = 0;
        !          11676: //     } else if(REG16(BX) == 0x0001) {
        !          11677: //             // European MS-DOS 4.0 - Get Hard Error Information
        !          11678:        } else {
        !          11679:                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));
        !          11680:                REG16(AX) = 0x01;
        !          11681:                m_CF = 1;
        !          11682:        }
1.1       root     11683: }
                   11684: 
                   11685: inline void msdos_int_21h_5ah()
                   11686: {
1.1.1.3   root     11687:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     11688:        int len = strlen(path);
                   11689:        char tmp[MAX_PATH];
                   11690:        
                   11691:        if(GetTempFileName(path, "TMP", 0, tmp)) {
                   11692:                int fd = _open(tmp, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   11693:                
                   11694:                SetFileAttributes(tmp, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   11695:                REG16(AX) = fd;
                   11696:                msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     11697:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     11698:                
                   11699:                strcpy(path, tmp);
                   11700:                int dx = REG16(DX) + len;
1.1.1.3   root     11701:                int ds = SREG(DS);
1.1       root     11702:                while(dx > 0xffff) {
                   11703:                        dx -= 0x10;
                   11704:                        ds++;
                   11705:                }
                   11706:                REG16(DX) = dx;
1.1.1.3   root     11707:                SREG(DS) = ds;
                   11708:                i386_load_segment_descriptor(DS);
1.1       root     11709:        } else {
                   11710:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     11711:                m_CF = 1;
1.1       root     11712:        }
                   11713: }
                   11714: 
                   11715: inline void msdos_int_21h_5bh()
                   11716: {
1.1.1.45  root     11717:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     11718:        
1.1.1.45  root     11719: //     if(msdos_is_existing_file(path)) {
                   11720:        if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1       root     11721:                // already exists
                   11722:                REG16(AX) = 0x50;
1.1.1.3   root     11723:                m_CF = 1;
1.1       root     11724:        } else {
                   11725:                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   11726:                
                   11727:                if(fd != -1) {
                   11728:                        SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   11729:                        REG16(AX) = fd;
                   11730:                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     11731:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     11732:                } else {
                   11733:                        REG16(AX) = errno;
1.1.1.3   root     11734:                        m_CF = 1;
1.1       root     11735:                }
                   11736:        }
                   11737: }
                   11738: 
                   11739: inline void msdos_int_21h_5ch()
                   11740: {
                   11741:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11742:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11743:        
1.1.1.20  root     11744:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     11745:                if(REG8(AL) == 0 || REG8(AL) == 1) {
1.1.1.35  root     11746:                        static const int modes[2] = {_LK_LOCK, _LK_UNLCK};
1.1.1.20  root     11747:                        UINT32 pos = _tell(fd);
                   11748:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), SEEK_SET);
                   11749:                        if(_locking(fd, modes[REG8(AL)], (REG16(SI) << 16) | REG16(DI))) {
1.1       root     11750:                                REG16(AX) = errno;
1.1.1.3   root     11751:                                m_CF = 1;
1.1       root     11752:                        }
1.1.1.20  root     11753:                        _lseek(fd, pos, SEEK_SET);
1.1.1.26  root     11754:                        
1.1       root     11755:                        // some seconds may be passed in _locking()
1.1.1.35  root     11756:                        REQUEST_HARDWRE_UPDATE();
1.1       root     11757:                } else {
                   11758:                        REG16(AX) = 0x01;
1.1.1.3   root     11759:                        m_CF = 1;
1.1       root     11760:                }
                   11761:        } else {
                   11762:                REG16(AX) = 0x06;
1.1.1.3   root     11763:                m_CF = 1;
1.1       root     11764:        }
                   11765: }
                   11766: 
1.1.1.22  root     11767: inline void msdos_int_21h_5dh()
                   11768: {
                   11769:        switch(REG8(AL)) {
1.1.1.45  root     11770:        case 0x00:
                   11771:                if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x12) == 0x0000) {
                   11772:                        // current system
                   11773:                        static bool reenter = false;
                   11774:                        if(!reenter) {
                   11775:                                UINT32 offset = SREG_BASE(DS) + REG16(DX);
                   11776:                                REG16(AX) = *(UINT16 *)(mem + offset + 0x00);
                   11777:                                REG16(BX) = *(UINT16 *)(mem + offset + 0x02);
                   11778:                                REG16(CX) = *(UINT16 *)(mem + offset + 0x04);
                   11779:                                REG16(DX) = *(UINT16 *)(mem + offset + 0x06);
                   11780:                                REG16(SI) = *(UINT16 *)(mem + offset + 0x08);
                   11781:                                REG16(DI) = *(UINT16 *)(mem + offset + 0x0a);
                   11782:                                SREG(DS)  = *(UINT16 *)(mem + offset + 0x0c);
                   11783:                                SREG(ES)  = *(UINT16 *)(mem + offset + 0x0e);
                   11784:                                i386_load_segment_descriptor(DS);
                   11785:                                i386_load_segment_descriptor(ES);
                   11786:                                reenter = true;
                   11787:                                try {
                   11788:                                        msdos_syscall(0x21);
                   11789:                                } catch(...) {
                   11790:                                }
                   11791:                                reenter = false;
                   11792:                        }
                   11793:                } else {
                   11794:                        REG16(AX) = 0x49; //  network software not installed
                   11795:                        m_CF = 1;
                   11796:                }
                   11797:                break;
1.1.1.22  root     11798:        case 0x06: // get address of dos swappable data area
1.1.1.23  root     11799:                SREG(DS) = (SDA_TOP >> 4);
                   11800:                i386_load_segment_descriptor(DS);
                   11801:                REG16(SI) = offsetof(sda_t, crit_error_flag);
                   11802:                REG16(CX) = 0x80;
                   11803:                REG16(DX) = 0x1a;
                   11804:                break;
1.1.1.45  root     11805:        case 0x07: // get redirected printer mode
                   11806:        case 0x08: // set redirected printer mode
                   11807:        case 0x09: // flush redirected printer output
                   11808:                REG16(AX) = 0x49; //  network software not installed
                   11809:                m_CF = 1;
                   11810:                break;
1.1.1.43  root     11811:        case 0x0a: // set extended error information
                   11812:                {
                   11813:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
1.1.1.47! root     11814:                        sda->int21h_5d0ah_called    = 1;
1.1.1.43  root     11815:                        sda->extended_error_code    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00); // AX
1.1.1.47! root     11816:                        // XXX: which one is correct ???
        !          11817: #if 1
        !          11818:                        // Ralf Brown's Interrupt List and DR-DOS System and Programmer's Guide
1.1.1.43  root     11819:                        sda->suggested_action       = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02); // BL
                   11820:                        sda->error_class            = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x03); // BH
1.1.1.47! root     11821:                        sda->int21h_5d0ah_cl        = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04); // CL
1.1.1.43  root     11822:                        sda->locus_of_last_error    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x05); // CH
1.1.1.47! root     11823: #else
        !          11824:                        // PC DOS 7 Technical Update
        !          11825:                        sda->error_class            = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02); // BH
        !          11826:                        sda->suggested_action       = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x03); // BL
        !          11827:                        sda->locus_of_last_error    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04); // CH
        !          11828:                        sda->int21h_5d0ah_cl        = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x05); // CL
        !          11829: #endif
        !          11830:                        sda->int21h_5d0ah_dx        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06); // DX
        !          11831: //                     sda->int21h_5d0ah_si        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08); // SI
1.1.1.43  root     11832:                        sda->last_error_pointer.w.l = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a); // DI
1.1.1.47! root     11833: //                     sda->int21h_5d0ah_ds        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c); // DS
1.1.1.43  root     11834:                        sda->last_error_pointer.w.h = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e); // ES
                   11835:                }
                   11836:                break;
1.1.1.23  root     11837:        case 0x0b: // get dos swappable data areas
1.1.1.22  root     11838:                REG16(AX) = 0x01;
                   11839:                m_CF = 1;
                   11840:                break;
                   11841:        default:
                   11842:                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));
                   11843:                REG16(AX) = 0x01;
                   11844:                m_CF = 1;
                   11845:                break;
                   11846:        }
                   11847: }
                   11848: 
1.1.1.42  root     11849: inline void msdos_int_21h_5eh()
                   11850: {
                   11851:        switch(REG8(AL)) {
                   11852:        case 0x00:
                   11853:                {
                   11854:                        char name[256] = {0};
                   11855:                        DWORD dwSize = 256;
                   11856:                        
                   11857:                        if(GetComputerName(name, &dwSize)) {
                   11858:                                char *dest = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   11859:                                for(int i = 0; i < 15; i++) {
                   11860:                                        dest[i] = (i < strlen(name)) ? name[i] : ' ';
                   11861:                                }
                   11862:                                dest[15] = '\0';
                   11863:                                REG8(CH) = 0x01; // nonzero valid
                   11864:                                REG8(CL) = 0x01; // NetBIOS number for machine name ???
                   11865:                        } else {
                   11866:                                REG16(AX) = 0x01;
                   11867:                                m_CF = 1;
                   11868:                        }
                   11869:                }
                   11870:                break;
                   11871:        default:
1.1.1.45  root     11872: //             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));
                   11873: //             REG16(AX) = 0x01;
                   11874:                REG16(AX) = 0x49; //  network software not installed
1.1.1.42  root     11875:                m_CF = 1;
                   11876:                break;
                   11877:        }
                   11878: }
                   11879: 
1.1.1.30  root     11880: inline void msdos_int_21h_5fh()
                   11881: {
                   11882:        switch(REG8(AL)) {
1.1.1.42  root     11883:        case 0x05:
1.1.1.44  root     11884:                REG16(BP) = 0;
                   11885:                for(int i = 0; i < 26; i++) {
                   11886:                        if(msdos_is_remote_drive(i)) {
                   11887:                                REG16(BP)++;
1.1.1.42  root     11888:                        }
                   11889:                }
1.1.1.30  root     11890:        case 0x02:
1.1.1.44  root     11891:                for(int i = 0, index = 0; i < 26; i++) {
                   11892:                        if(msdos_is_remote_drive(i)) {
                   11893:                                if(index == REG16(BX)) {
                   11894:                                        char volume[] = "A:";
1.1.1.30  root     11895:                                        volume[0] = 'A' + i;
1.1.1.44  root     11896:                                        DWORD dwSize = 128;
                   11897:                                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), volume);
                   11898:                                        WNetGetConnection(volume, (char *)(mem + SREG_BASE(ES) + REG16(DI)), &dwSize);
                   11899:                                        REG8(BH) = 0x00; // valid
                   11900:                                        REG8(BL) = 0x04; // disk drive
                   11901:                                        REG16(CX) = 0x00; // LANtastic
                   11902:                                        return;
1.1.1.30  root     11903:                                }
1.1.1.44  root     11904:                                index++;
1.1.1.30  root     11905:                        }
                   11906:                }
                   11907:                REG16(AX) = 0x12; // no more files
                   11908:                m_CF = 1;
                   11909:                break;
1.1.1.44  root     11910:        case 0x07:
                   11911:                if(msdos_is_valid_drive(REG8(DL))) {
                   11912:                        msdos_cds_update(REG8(DL));
                   11913:                } else {
                   11914:                        REG16(AX) = 0x0f; // invalid drive
                   11915:                        m_CF = 1;
                   11916:                }
                   11917:                break;
                   11918:        case 0x08:
                   11919:                if(msdos_is_valid_drive(REG8(DL))) {
                   11920:                        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * REG8(DL));
                   11921:                        cds->drive_attrib = 0x0000;
                   11922:                } else {
                   11923:                        REG16(AX) = 0x0f; // invalid drive
                   11924:                        m_CF = 1;
                   11925:                }
                   11926:                break;
1.1.1.30  root     11927:        default:
1.1.1.45  root     11928: //             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));
                   11929: //             REG16(AX) = 0x01;
                   11930:                REG16(AX) = 0x49; //  network software not installed
1.1.1.30  root     11931:                m_CF = 1;
                   11932:                break;
                   11933:        }
                   11934: }
                   11935: 
1.1       root     11936: inline void msdos_int_21h_60h(int lfn)
                   11937: {
1.1.1.45  root     11938:        char full[MAX_PATH];
                   11939:        const char *path = NULL;
1.1.1.14  root     11940:        
1.1       root     11941:        if(lfn) {
1.1.1.14  root     11942:                char *name;
                   11943:                *full = '\0';
1.1.1.3   root     11944:                GetFullPathName((char *)(mem + SREG_BASE(DS) + REG16(SI)), MAX_PATH, full, &name);
1.1.1.14  root     11945:                switch(REG8(CL)) {
                   11946:                case 1:
                   11947:                        GetShortPathName(full, full, MAX_PATH);
                   11948:                        my_strupr(full);
                   11949:                        break;
                   11950:                case 2:
                   11951:                        GetLongPathName(full, full, MAX_PATH);
                   11952:                        break;
                   11953:                }
                   11954:                path = full;
                   11955:        } else {
                   11956:                path = msdos_short_full_path((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   11957:        }
                   11958:        if(*path != '\0') {
                   11959:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
1.1       root     11960:        } else {
1.1.1.14  root     11961:                REG16(AX) = (UINT16)GetLastError();
                   11962:                m_CF = 1;
1.1       root     11963:        }
                   11964: }
                   11965: 
                   11966: inline void msdos_int_21h_61h()
                   11967: {
                   11968:        REG8(AL) = 0;
                   11969: }
                   11970: 
                   11971: inline void msdos_int_21h_62h()
                   11972: {
                   11973:        REG16(BX) = current_psp;
                   11974: }
                   11975: 
                   11976: inline void msdos_int_21h_63h()
                   11977: {
                   11978:        switch(REG8(AL)) {
                   11979:        case 0x00:
1.1.1.3   root     11980:                SREG(DS) = (DBCS_TABLE >> 4);
                   11981:                i386_load_segment_descriptor(DS);
1.1       root     11982:                REG16(SI) = (DBCS_TABLE & 0x0f);
                   11983:                REG8(AL) = 0x00;
                   11984:                break;
1.1.1.22  root     11985:        case 0x01: // set korean input mode
                   11986:        case 0x02: // get korean input mode
                   11987:                REG8(AL) = 0xff; // not supported
                   11988:                break;
1.1       root     11989:        default:
1.1.1.22  root     11990:                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     11991:                REG16(AX) = 0x01;
1.1.1.3   root     11992:                m_CF = 1;
1.1       root     11993:                break;
                   11994:        }
                   11995: }
                   11996: 
1.1.1.25  root     11997: UINT16 get_extended_country_info(UINT8 func)
1.1       root     11998: {
1.1.1.25  root     11999:        switch(func) {
1.1.1.17  root     12000:        case 0x01:
                   12001:                if(REG16(CX) >= 5) {
1.1.1.19  root     12002:                        UINT8 data[1 + 2 + 2 + 2 + sizeof(country_info_t)];
1.1.1.17  root     12003:                        if(REG16(CX) > sizeof(data))            // cx = actual transfer size
                   12004:                                REG16(CX) = sizeof(data);
                   12005:                        ZeroMemory(data, sizeof(data));
                   12006:                        data[0] = 0x01;
                   12007:                        *(UINT16 *)(data + 1) = REG16(CX) - 3;
1.1.1.19  root     12008:                        *(UINT16 *)(data + 3) = get_country_info((country_info_t*)(data + 7));
1.1.1.17  root     12009:                        *(UINT16 *)(data + 5) = active_code_page;
                   12010:                        memcpy(mem + SREG_BASE(ES) + REG16(DI), data, REG16(CX));
1.1.1.25  root     12011: //                     REG16(AX) = active_code_page;
1.1.1.17  root     12012:                } else {
1.1.1.25  root     12013:                        return(0x08); // insufficient memory
1.1.1.17  root     12014:                }
                   12015:                break;
                   12016:        case 0x02:
                   12017:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   12018:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (UPPERTABLE_TOP & 0x0f);
                   12019:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (UPPERTABLE_TOP >> 4);
1.1.1.25  root     12020: //             REG16(AX) = active_code_page;
1.1.1.17  root     12021:                REG16(CX) = 0x05;
                   12022:                break;
1.1.1.23  root     12023:        case 0x03:
                   12024:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   12025:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (LOWERTABLE_TOP & 0x0f);
                   12026:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (LOWERTABLE_TOP >> 4);
1.1.1.25  root     12027: //             REG16(AX) = active_code_page;
1.1.1.23  root     12028:                REG16(CX) = 0x05;
                   12029:                break;
1.1.1.17  root     12030:        case 0x04:
                   12031:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x04;
                   12032:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_UPPERTABLE_TOP & 0x0f);
                   12033:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_UPPERTABLE_TOP >> 4);
1.1.1.25  root     12034: //             REG16(AX) = active_code_page;
1.1.1.17  root     12035:                REG16(CX) = 0x05;
                   12036:                break;
                   12037:        case 0x05:
                   12038:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x05;
                   12039:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_TERMINATOR_TOP & 0x0f);
                   12040:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_TERMINATOR_TOP >> 4);
1.1.1.25  root     12041: //             REG16(AX) = active_code_page;
1.1.1.17  root     12042:                REG16(CX) = 0x05;
                   12043:                break;
                   12044:        case 0x06:
                   12045:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x06;
                   12046:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (COLLATING_TABLE_TOP & 0x0f);
                   12047:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (COLLATING_TABLE_TOP >> 4);
1.1.1.25  root     12048: //             REG16(AX) = active_code_page;
1.1.1.17  root     12049:                REG16(CX) = 0x05;
                   12050:                break;
1.1       root     12051:        case 0x07:
1.1.1.3   root     12052:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x07;
                   12053:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (DBCS_TOP & 0x0f);
                   12054:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (DBCS_TOP >> 4);
1.1.1.25  root     12055: //             REG16(AX) = active_code_page;
1.1       root     12056:                REG16(CX) = 0x05;
                   12057:                break;
1.1.1.25  root     12058:        default:
                   12059:                return(0x01); // function number invalid
                   12060:        }
                   12061:        return(0x00);
                   12062: }
                   12063: 
                   12064: inline void msdos_int_21h_65h()
                   12065: {
                   12066:        char tmp[0x10000];
                   12067:        
                   12068:        switch(REG8(AL)) {
1.1.1.43  root     12069:        case 0x00:
                   12070:                if(REG16(CX) >= 7) {
                   12071:                        set_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)), REG16(CX) - 7);
                   12072:                        REG16(AX) = system_code_page;
                   12073:                } else {
                   12074:                        REG16(AX) = 0x0c;
                   12075:                        m_CF = 1;
                   12076:                }
                   12077:                break;
1.1.1.25  root     12078:        case 0x01:
                   12079:        case 0x02:
                   12080:        case 0x03:
                   12081:        case 0x04:
                   12082:        case 0x05:
                   12083:        case 0x06:
                   12084:        case 0x07:
                   12085:                {
                   12086:                        UINT16 result = get_extended_country_info(REG8(AL));
                   12087:                        if(result) {
                   12088:                                REG16(AX) = result;
                   12089:                                m_CF = 1;
                   12090:                        } else {
                   12091:                                REG16(AX) = active_code_page; // FIXME: is this correct???
                   12092:                        }
                   12093:                }
                   12094:                break;
1.1       root     12095:        case 0x20:
1.1.1.25  root     12096:        case 0xa0:
1.1.1.19  root     12097:                memset(tmp, 0, sizeof(tmp));
                   12098:                tmp[0] = REG8(DL);
1.1       root     12099:                my_strupr(tmp);
                   12100:                REG8(DL) = tmp[0];
                   12101:                break;
                   12102:        case 0x21:
1.1.1.25  root     12103:        case 0xa1:
1.1       root     12104:                memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     12105:                memcpy(tmp, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     12106:                my_strupr(tmp);
1.1.1.3   root     12107:                memcpy(mem + SREG_BASE(DS) + REG16(DX), tmp, REG16(CX));
1.1       root     12108:                break;
                   12109:        case 0x22:
1.1.1.25  root     12110:        case 0xa2:
1.1.1.3   root     12111:                my_strupr((char *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1       root     12112:                break;
1.1.1.25  root     12113:        case 0x23:
                   12114:                // FIXME: need to check multi-byte (kanji) charactre?
1.1.1.45  root     12115:                if(REG8(DL) == 'N' || REG8(DL) == 'n' || (REG8(DL) == 0x82 && REG8(DH) == 0x6d) || (REG8(DL) == 0x82 && REG8(DH) == 0x8e)) {
1.1.1.25  root     12116:                        // 826dh/828eh: multi-byte (kanji) N and n
1.1.1.45  root     12117:                        REG16(AX) = 0x00;
                   12118:                } else if(REG8(DL) == 'Y' || REG8(DL) == 'y' || (REG8(DL) == 0x82 && REG8(DH) == 0x78) || (REG8(DL) == 0x82 && REG8(DH) == 0x99)) {
                   12119:                        // 8278h/8299h: multi-byte (kanji) Y and y
1.1.1.25  root     12120:                        REG16(AX) = 0x01;
                   12121:                } else {
                   12122:                        REG16(AX) = 0x02;
                   12123:                }
                   12124:                break;
1.1       root     12125:        default:
1.1.1.22  root     12126:                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     12127:                REG16(AX) = 0x01;
1.1.1.3   root     12128:                m_CF = 1;
1.1       root     12129:                break;
                   12130:        }
                   12131: }
                   12132: 
                   12133: inline void msdos_int_21h_66h()
                   12134: {
                   12135:        switch(REG8(AL)) {
                   12136:        case 0x01:
                   12137:                REG16(BX) = active_code_page;
                   12138:                REG16(DX) = system_code_page;
                   12139:                break;
                   12140:        case 0x02:
                   12141:                if(active_code_page == REG16(BX)) {
                   12142:                        REG16(AX) = 0xeb41;
                   12143:                } else if(_setmbcp(REG16(BX)) == 0) {
                   12144:                        active_code_page = REG16(BX);
1.1.1.17  root     12145:                        msdos_nls_tables_update();
1.1       root     12146:                        REG16(AX) = 0xeb41;
1.1.1.32  root     12147:                        SetConsoleCP(active_code_page);
                   12148:                        SetConsoleOutputCP(active_code_page);
1.1       root     12149:                } else {
                   12150:                        REG16(AX) = 0x25;
1.1.1.3   root     12151:                        m_CF = 1;
1.1       root     12152:                }
                   12153:                break;
                   12154:        default:
1.1.1.22  root     12155:                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     12156:                REG16(AX) = 0x01;
1.1.1.3   root     12157:                m_CF = 1;
1.1       root     12158:                break;
                   12159:        }
                   12160: }
                   12161: 
                   12162: inline void msdos_int_21h_67h()
                   12163: {
                   12164:        process_t *process = msdos_process_info_get(current_psp);
                   12165:        
                   12166:        if(REG16(BX) <= MAX_FILES) {
                   12167:                process->max_files = max(REG16(BX), 20);
                   12168:        } else {
                   12169:                REG16(AX) = 0x08;
1.1.1.3   root     12170:                m_CF = 1;
1.1       root     12171:        }
                   12172: }
                   12173: 
                   12174: inline void msdos_int_21h_68h()
                   12175: {
                   12176:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     12177:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     12178:        
1.1.1.20  root     12179:        if(fd < process->max_files && file_handler[fd].valid) {
                   12180:                // fflush(_fdopen(fd, ""));
1.1       root     12181:        } else {
                   12182:                REG16(AX) = 0x06;
1.1.1.3   root     12183:                m_CF = 1;
1.1       root     12184:        }
                   12185: }
                   12186: 
                   12187: inline void msdos_int_21h_69h()
                   12188: {
1.1.1.3   root     12189:        drive_info_t *info = (drive_info_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     12190:        char path[] = "A:\\";
                   12191:        char volume_label[MAX_PATH];
                   12192:        DWORD serial_number = 0;
                   12193:        char file_system[MAX_PATH];
                   12194:        
                   12195:        if(REG8(BL) == 0) {
                   12196:                path[0] = 'A' + _getdrive() - 1;
                   12197:        } else {
                   12198:                path[0] = 'A' + REG8(BL) - 1;
                   12199:        }
                   12200:        
                   12201:        switch(REG8(AL)) {
                   12202:        case 0x00:
                   12203:                if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
                   12204:                        info->info_level = 0;
                   12205:                        info->serial_number = serial_number;
                   12206:                        memset(info->volume_label, 0x20, 11);
                   12207:                        memcpy(info->volume_label, volume_label, min(strlen(volume_label), 11));
                   12208:                        memset(info->file_system, 0x20, 8);
                   12209:                        memcpy(info->file_system, file_system, min(strlen(file_system), 8));
                   12210:                } else {
                   12211:                        REG16(AX) = errno;
1.1.1.3   root     12212:                        m_CF = 1;
1.1       root     12213:                }
                   12214:                break;
                   12215:        case 0x01:
                   12216:                REG16(AX) = 0x03;
1.1.1.3   root     12217:                m_CF = 1;
1.1.1.45  root     12218:                break;
                   12219:        default:
                   12220:                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));
                   12221:                REG16(AX) = 0x01;
                   12222:                m_CF = 1;
                   12223:                break;
1.1       root     12224:        }
                   12225: }
                   12226: 
                   12227: inline void msdos_int_21h_6ah()
                   12228: {
                   12229:        REG8(AH) = 0x68;
                   12230:        msdos_int_21h_68h();
                   12231: }
                   12232: 
                   12233: inline void msdos_int_21h_6bh()
                   12234: {
1.1.1.45  root     12235:        REG8(AL) = 0x00;
1.1       root     12236: }
                   12237: 
                   12238: inline void msdos_int_21h_6ch(int lfn)
                   12239: {
1.1.1.45  root     12240:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(SI)), lfn);
1.1       root     12241:        int mode = REG8(BL) & 0x03;
                   12242:        
                   12243:        if(mode < 0x03) {
1.1.1.29  root     12244:                if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1       root     12245:                        // file exists
                   12246:                        if(REG8(DL) & 1) {
1.1.1.37  root     12247:                                int fd = -1;
                   12248:                                int sio_port = 0;
                   12249:                                int lpt_port = 0;
1.1       root     12250:                                
1.1.1.45  root     12251:                                if(msdos_is_device_path(path)) {
                   12252:                                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1.1.11  root     12253:                                } else {
1.1.1.13  root     12254:                                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     12255:                                }
1.1       root     12256:                                if(fd != -1) {
                   12257:                                        REG16(AX) = fd;
                   12258:                                        REG16(CX) = 1;
1.1.1.45  root     12259:                                        msdos_file_handler_open(fd, path, _isatty(fd), mode, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     12260:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12261:                                } else {
                   12262:                                        REG16(AX) = errno;
1.1.1.3   root     12263:                                        m_CF = 1;
1.1       root     12264:                                }
                   12265:                        } else if(REG8(DL) & 2) {
                   12266:                                int attr = GetFileAttributes(path);
1.1.1.37  root     12267:                                int fd = -1;
                   12268:                                int sio_port = 0;
                   12269:                                int lpt_port = 0;
1.1       root     12270:                                
1.1.1.45  root     12271:                                if(msdos_is_device_path(path)) {
                   12272:                                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1       root     12273:                                } else {
                   12274:                                        fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12275:                                }
                   12276:                                if(fd != -1) {
                   12277:                                        if(attr == -1) {
                   12278:                                                attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   12279:                                        }
                   12280:                                        SetFileAttributes(path, attr);
                   12281:                                        REG16(AX) = fd;
                   12282:                                        REG16(CX) = 3;
1.1.1.45  root     12283:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     12284:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12285:                                } else {
                   12286:                                        REG16(AX) = errno;
1.1.1.3   root     12287:                                        m_CF = 1;
1.1       root     12288:                                }
                   12289:                        } else {
                   12290:                                REG16(AX) = 0x50;
1.1.1.3   root     12291:                                m_CF = 1;
1.1       root     12292:                        }
                   12293:                } else {
                   12294:                        // file not exists
                   12295:                        if(REG8(DL) & 0x10) {
                   12296:                                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12297:                                
                   12298:                                if(fd != -1) {
                   12299:                                        SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   12300:                                        REG16(AX) = fd;
                   12301:                                        REG16(CX) = 2;
                   12302:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     12303:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12304:                                } else {
                   12305:                                        REG16(AX) = errno;
1.1.1.3   root     12306:                                        m_CF = 1;
1.1       root     12307:                                }
                   12308:                        } else {
                   12309:                                REG16(AX) = 0x02;
1.1.1.3   root     12310:                                m_CF = 1;
1.1       root     12311:                        }
                   12312:                }
                   12313:        } else {
                   12314:                REG16(AX) = 0x0c;
1.1.1.3   root     12315:                m_CF = 1;
1.1       root     12316:        }
                   12317: }
                   12318: 
1.1.1.43  root     12319: inline void msdos_int_21h_70h()
                   12320: {
                   12321:        switch(REG8(AL)) {
                   12322:        case 0x02:
                   12323:                if(REG16(CX) >= 7) {
                   12324:                        active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 5);
                   12325:                        msdos_nls_tables_update();
                   12326:                        set_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(SI) + 7), REG16(CX) - 7);
                   12327:                        REG16(AX) = system_code_page;
                   12328:                } else {
                   12329:                        REG16(AX) = 0x0c;
                   12330:                        m_CF = 1;
                   12331:                }
                   12332:                break;
                   12333:        default:
                   12334:                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));
                   12335:                REG16(AX) = 0x01;
                   12336:                m_CF = 1;
                   12337:                break;
                   12338:        }
                   12339: }
                   12340: 
1.1       root     12341: inline void msdos_int_21h_710dh()
                   12342: {
                   12343:        // reset drive
                   12344: }
                   12345: 
1.1.1.17  root     12346: inline void msdos_int_21h_7141h(int lfn)
                   12347: {
                   12348:        if(REG16(SI) == 0) {
                   12349:                msdos_int_21h_41h(lfn);
                   12350:                return;
                   12351:        }
                   12352:        if(REG16(SI) != 1) {
                   12353:                REG16(AX) = 5;
                   12354:                m_CF = 1;
                   12355:        }
                   12356:        /* wild card and matching attributes... */
                   12357:        char tmp[MAX_PATH * 2];
                   12358:        // copy search pathname (and quick check overrun)
                   12359:        ZeroMemory(tmp, sizeof(tmp));
                   12360:        tmp[MAX_PATH - 1] = '\0';
                   12361:        tmp[MAX_PATH] = 1;
                   12362:        strncpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(DX)), MAX_PATH);
                   12363:        
                   12364:        if(tmp[MAX_PATH - 1] != '\0' || tmp[MAX_PATH] != 1) {
                   12365:                REG16(AX) = 1;
                   12366:                m_CF = 1;
                   12367:                return;
                   12368:        }
                   12369:        for(char *s = tmp; *s; ++s) {
                   12370:                if(*s == '/') {
                   12371:                        *s = '\\';
                   12372:                }
                   12373:        }
                   12374:        char *tmp_name = (char *)_mbsrchr((unsigned char *)tmp, '\\');
                   12375:        if(tmp_name) {
                   12376:                ++tmp_name;
                   12377:        } else {
                   12378:                tmp_name = strchr(tmp, ':');
                   12379:                tmp_name = tmp_name ? tmp_name + 1 : tmp;
                   12380:        }
                   12381:        
                   12382:        WIN32_FIND_DATAA fd;
                   12383:        HANDLE fh = FindFirstFileA(tmp, &fd);
                   12384:        if(fh == INVALID_HANDLE_VALUE) {
                   12385:                REG16(AX) = 2;
                   12386:                m_CF = 1;
                   12387:                return;
                   12388:        }
                   12389:        do {
                   12390:                if(msdos_find_file_check_attribute(fd.dwFileAttributes, REG8(CL), REG8(CH)) && msdos_find_file_has_8dot3name(&fd)) {
                   12391:                        strcpy(tmp_name, fd.cFileName);
                   12392:                        if(remove(msdos_trimmed_path(tmp, lfn))) {
                   12393:                                REG16(AX) = 5;
                   12394:                                m_CF = 1;
                   12395:                                break;
                   12396:                        }
                   12397:                }
                   12398:        } while(FindNextFileA(fh, &fd));
                   12399:        if(!m_CF) {
                   12400:                if(GetLastError() != ERROR_NO_MORE_FILES) {
                   12401:                        m_CF = 1;
                   12402:                        REG16(AX) = 2;
                   12403:                }
                   12404:        }
                   12405:        FindClose(fh);
                   12406: }
                   12407: 
1.1       root     12408: inline void msdos_int_21h_714eh()
                   12409: {
                   12410:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     12411:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
                   12412:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     12413:        WIN32_FIND_DATA fd;
                   12414:        
1.1.1.13  root     12415:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   12416:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   12417:                FindClose(dtainfo->find_handle);
                   12418:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12419:        }
                   12420:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     12421:        dtainfo->allowable_mask = REG8(CL);
                   12422:        dtainfo->required_mask = REG8(CH);
                   12423:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     12424:        
1.1.1.14  root     12425:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   12426:                dtainfo->allowable_mask &= ~8;
1.1       root     12427:        }
1.1.1.14  root     12428:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   12429:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13  root     12430:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   12431:                                FindClose(dtainfo->find_handle);
                   12432:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12433:                                break;
                   12434:                        }
                   12435:                }
                   12436:        }
1.1.1.13  root     12437:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     12438:                find->attrib = fd.dwFileAttributes;
                   12439:                msdos_find_file_conv_local_time(&fd);
                   12440:                if(REG16(SI) == 0) {
                   12441:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   12442:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   12443:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   12444:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   12445:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   12446:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   12447:                } else {
                   12448:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   12449:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   12450:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   12451:                }
                   12452:                find->size_hi = fd.nFileSizeHigh;
                   12453:                find->size_lo = fd.nFileSizeLow;
                   12454:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     12455:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     12456:                REG16(AX) = dtainfo - dtalist + 1;
                   12457:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     12458:                // volume label
                   12459:                find->attrib = 8;
                   12460:                find->size_hi = find->size_lo = 0;
                   12461:                strcpy(find->full_name, process->volume_label);
                   12462:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     12463:                dtainfo->allowable_mask &= ~8;
                   12464:                REG16(AX) = dtainfo - dtalist + 1;
1.1       root     12465:        } else {
                   12466:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     12467:                m_CF = 1;
1.1       root     12468:        }
                   12469: }
                   12470: 
                   12471: inline void msdos_int_21h_714fh()
                   12472: {
                   12473:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     12474:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     12475:        WIN32_FIND_DATA fd;
                   12476:        
1.1.1.14  root     12477:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   12478:                REG16(AX) = 6;
1.1.1.13  root     12479:                m_CF = 1;
                   12480:                return;
                   12481:        }
1.1.1.14  root     12482:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     12483:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   12484:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     12485:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13  root     12486:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   12487:                                        FindClose(dtainfo->find_handle);
                   12488:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12489:                                        break;
                   12490:                                }
                   12491:                        }
                   12492:                } else {
1.1.1.13  root     12493:                        FindClose(dtainfo->find_handle);
                   12494:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12495:                }
                   12496:        }
1.1.1.13  root     12497:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     12498:                find->attrib = fd.dwFileAttributes;
                   12499:                msdos_find_file_conv_local_time(&fd);
                   12500:                if(REG16(SI) == 0) {
                   12501:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   12502:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   12503:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   12504:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   12505:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   12506:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   12507:                } else {
                   12508:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   12509:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   12510:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   12511:                }
                   12512:                find->size_hi = fd.nFileSizeHigh;
                   12513:                find->size_lo = fd.nFileSizeLow;
                   12514:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     12515:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     12516:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     12517:                // volume label
                   12518:                find->attrib = 8;
                   12519:                find->size_hi = find->size_lo = 0;
                   12520:                strcpy(find->full_name, process->volume_label);
                   12521:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     12522:                dtainfo->allowable_mask &= ~8;
1.1       root     12523:        } else {
                   12524:                REG16(AX) = 0x12;
1.1.1.3   root     12525:                m_CF = 1;
1.1       root     12526:        }
                   12527: }
                   12528: 
                   12529: inline void msdos_int_21h_71a0h()
                   12530: {
                   12531:        DWORD max_component_len, file_sys_flag;
                   12532:        
1.1.1.14  root     12533:        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))) {
                   12534:                REG16(BX) = (UINT16)file_sys_flag & (FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES | FILE_UNICODE_ON_DISK | FILE_VOLUME_IS_COMPRESSED);
                   12535:                REG16(BX) |= 0x4000;                            // supports LFN functions
1.1       root     12536:                REG16(CX) = (UINT16)max_component_len;          // 255
                   12537:                REG16(DX) = (UINT16)max_component_len + 5;      // 260
                   12538:        } else {
                   12539:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     12540:                m_CF = 1;
1.1       root     12541:        }
                   12542: }
                   12543: 
                   12544: inline void msdos_int_21h_71a1h()
                   12545: {
1.1.1.14  root     12546:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   12547:                REG16(AX) = 6;
1.1.1.13  root     12548:                m_CF = 1;
                   12549:                return;
                   12550:        }
1.1.1.14  root     12551:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     12552:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   12553:                FindClose(dtainfo->find_handle);
                   12554:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12555:        }
                   12556: }
                   12557: 
                   12558: inline void msdos_int_21h_71a6h()
                   12559: {
                   12560:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     12561:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   12562:        
1.1.1.3   root     12563:        UINT8 *buffer = (UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     12564:        struct _stat64 status;
                   12565:        DWORD serial_number = 0;
                   12566:        
1.1.1.20  root     12567:        if(fd < process->max_files && file_handler[fd].valid) {
                   12568:                if(_fstat64(fd, &status) == 0) {
                   12569:                        if(file_handler[fd].path[1] == ':') {
1.1       root     12570:                                // NOTE: we need to consider the network file path "\\host\share\"
                   12571:                                char volume[] = "A:\\";
1.1.1.20  root     12572:                                volume[0] = file_handler[fd].path[1];
1.1       root     12573:                                GetVolumeInformation(volume, NULL, 0, &serial_number, NULL, NULL, NULL, 0);
                   12574:                        }
1.1.1.20  root     12575:                        *(UINT32 *)(buffer + 0x00) = GetFileAttributes(file_handler[fd].path);
1.1       root     12576:                        *(UINT32 *)(buffer + 0x04) = (UINT32)(status.st_ctime & 0xffffffff);
                   12577:                        *(UINT32 *)(buffer + 0x08) = (UINT32)((status.st_ctime >> 32) & 0xffffffff);
                   12578:                        *(UINT32 *)(buffer + 0x0c) = (UINT32)(status.st_atime & 0xffffffff);
                   12579:                        *(UINT32 *)(buffer + 0x10) = (UINT32)((status.st_atime >> 32) & 0xffffffff);
                   12580:                        *(UINT32 *)(buffer + 0x14) = (UINT32)(status.st_mtime & 0xffffffff);
                   12581:                        *(UINT32 *)(buffer + 0x18) = (UINT32)((status.st_mtime >> 32) & 0xffffffff);
                   12582:                        *(UINT32 *)(buffer + 0x1c) = serial_number;
                   12583:                        *(UINT32 *)(buffer + 0x20) = (UINT32)((status.st_size >> 32) & 0xffffffff);
                   12584:                        *(UINT32 *)(buffer + 0x24) = (UINT32)(status.st_size & 0xffffffff);
                   12585:                        *(UINT32 *)(buffer + 0x28) = status.st_nlink;
1.1.1.14  root     12586:                        // this is dummy id and it will be changed when it is reopened...
1.1       root     12587:                        *(UINT32 *)(buffer + 0x2c) = 0;
1.1.1.20  root     12588:                        *(UINT32 *)(buffer + 0x30) = file_handler[fd].id;
1.1       root     12589:                } else {
                   12590:                        REG16(AX) = errno;
1.1.1.3   root     12591:                        m_CF = 1;
1.1       root     12592:                }
                   12593:        } else {
                   12594:                REG16(AX) = 0x06;
1.1.1.3   root     12595:                m_CF = 1;
1.1       root     12596:        }
                   12597: }
                   12598: 
                   12599: inline void msdos_int_21h_71a7h()
                   12600: {
                   12601:        switch(REG8(BL)) {
                   12602:        case 0x00:
1.1.1.3   root     12603:                if(!FileTimeToDosDateTime((FILETIME *)(mem + SREG_BASE(DS) + REG16(SI)), &REG16(DX), &REG16(CX))) {
1.1       root     12604:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     12605:                        m_CF = 1;
1.1       root     12606:                }
                   12607:                break;
                   12608:        case 0x01:
                   12609:                // NOTE: we need to check BH that shows 10-millisecond untils past time in CX
1.1.1.3   root     12610:                if(!DosDateTimeToFileTime(REG16(DX), REG16(CX), (FILETIME *)(mem + SREG_BASE(ES) + REG16(DI)))) {
1.1       root     12611:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     12612:                        m_CF = 1;
1.1       root     12613:                }
                   12614:                break;
                   12615:        default:
1.1.1.22  root     12616:                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     12617:                REG16(AX) = 0x01;
1.1.1.3   root     12618:                m_CF = 1;
1.1       root     12619:                break;
                   12620:        }
                   12621: }
                   12622: 
                   12623: inline void msdos_int_21h_71a8h()
                   12624: {
                   12625:        if(REG8(DH) == 0) {
                   12626:                char tmp[MAX_PATH], fcb[MAX_PATH];
1.1.1.3   root     12627:                strcpy(tmp, msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     12628:                memset(fcb, 0x20, sizeof(fcb));
                   12629:                int len = strlen(tmp);
1.1.1.21  root     12630:                for(int i = 0, pos = 0; i < len; i++) {
1.1       root     12631:                        if(tmp[i] == '.') {
                   12632:                                pos = 8;
                   12633:                        } else {
                   12634:                                if(msdos_lead_byte_check(tmp[i])) {
                   12635:                                        fcb[pos++] = tmp[i++];
                   12636:                                }
                   12637:                                fcb[pos++] = tmp[i];
                   12638:                        }
                   12639:                }
1.1.1.3   root     12640:                memcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), fcb, 11);
1.1       root     12641:        } else {
1.1.1.3   root     12642:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     12643:        }
                   12644: }
                   12645: 
1.1.1.22  root     12646: inline void msdos_int_21h_71aah()
                   12647: {
                   12648:        char drv[] = "A:", path[MAX_PATH];
                   12649:        char *hoge=(char *)(mem + SREG_BASE(DS) + REG16(DX));
                   12650:        
                   12651:        if(REG8(BL) == 0) {
                   12652:                drv[0] = 'A' + _getdrive() - 1;
                   12653:        } else {
                   12654:                drv[0] = 'A' + REG8(BL) - 1;
                   12655:        }
                   12656:        switch(REG8(BH)) {
                   12657:        case 0x00:
1.1.1.44  root     12658:                if(msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   12659:                        REG16(AX) = 0x0f; // invalid drive
                   12660:                        m_CF = 1;
                   12661:                } else if(DefineDosDevice(0, drv, (char *)(mem + SREG_BASE(DS) + REG16(DX))) == 0) {
                   12662:                        REG16(AX) = 0x03; // path not found
1.1.1.22  root     12663:                        m_CF = 1;
                   12664:                }
                   12665:                break;
                   12666:        case 0x01:
1.1.1.44  root     12667:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   12668:                        REG16(AX) = 0x0f; // invalid drive
                   12669:                        m_CF = 1;
                   12670:                } else if(DefineDosDevice(DDD_REMOVE_DEFINITION, drv, NULL) == 0) {
1.1.1.22  root     12671:                        REG16(AX) = 0x0f; // invalid drive
                   12672:                        m_CF = 1;
                   12673:                }
                   12674:                break;
                   12675:        case 0x02:
1.1.1.44  root     12676:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   12677:                        REG16(AX) = 0x0f; // invalid drive
                   12678:                        m_CF = 1;
                   12679:                } else if(QueryDosDevice(drv, path, MAX_PATH) == 0) {
1.1.1.22  root     12680:                        REG16(AX) = 0x0f; // invalid drive
                   12681:                        m_CF = 1;
                   12682:                } else if(strncmp(path, "\\??\\", 4) != 0) {
                   12683:                        REG16(AX) = 0x0f; // invalid drive
                   12684:                        m_CF = 1;
                   12685:                } else {
                   12686:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(DX)), path + 4);
                   12687:                }
                   12688:                break;
                   12689:        default:
                   12690:                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));
                   12691:                REG16(AX) = 0x01;
                   12692:                m_CF = 1;
                   12693:                break;
                   12694:        }
                   12695: }
                   12696: 
1.1.1.14  root     12697: inline void msdos_int_21h_7300h()
                   12698: {
1.1.1.44  root     12699:        REG8(AL) = REG8(CL);
                   12700:        REG8(AH) = 0;
1.1.1.14  root     12701: }
                   12702: 
                   12703: inline void msdos_int_21h_7302h()
                   12704: {
                   12705:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   12706:        UINT16 seg, ofs;
                   12707:        
                   12708:        if(REG16(CX) < 0x3f) {
                   12709:                REG8(AL) = 0x18;
                   12710:                m_CF = 1;
                   12711:        } else if(!msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   12712:                REG8(AL) = 0xff;
                   12713:                m_CF = 1;
                   12714:        } else {
                   12715:                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 2, mem + (seg << 4) + ofs, sizeof(dpb_t));
                   12716:        }
                   12717: }
                   12718: 
1.1       root     12719: inline void msdos_int_21h_7303h()
                   12720: {
1.1.1.3   root     12721:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   12722:        ext_space_info_t *info = (ext_space_info_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     12723:        DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   12724:        
                   12725:        if(GetDiskFreeSpace(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
                   12726:                info->size_of_structure = sizeof(ext_space_info_t);
                   12727:                info->structure_version = 0;
                   12728:                info->sectors_per_cluster = sectors_per_cluster;
                   12729:                info->bytes_per_sector = bytes_per_sector;
                   12730:                info->available_clusters_on_drive = free_clusters;
                   12731:                info->total_clusters_on_drive = total_clusters;
                   12732:                info->available_sectors_on_drive = sectors_per_cluster * free_clusters;
                   12733:                info->total_sectors_on_drive = sectors_per_cluster * total_clusters;
                   12734:                info->available_allocation_units = free_clusters;       // ???
                   12735:                info->total_allocation_units = total_clusters;          // ???
                   12736:        } else {
                   12737:                REG16(AX) = errno;
1.1.1.3   root     12738:                m_CF = 1;
1.1       root     12739:        }
                   12740: }
                   12741: 
1.1.1.30  root     12742: inline void msdos_int_21h_dbh()
                   12743: {
                   12744:        // Novell NetWare - Workstation - Get Number of Local Drives
                   12745:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   12746:        REG8(AL) = dos_info->last_drive;
                   12747: }
                   12748: 
                   12749: inline void msdos_int_21h_dch()
                   12750: {
                   12751:        // Novell NetWare - Connection Services - Get Connection Number
                   12752:        REG8(AL) = 0x00;
                   12753: }
                   12754: 
1.1.1.32  root     12755: inline void msdos_int_24h()
                   12756: {
                   12757:        const char *message = NULL;
                   12758:        int key = 0;
                   12759:        
                   12760:        for(int i = 0; i < array_length(critical_error_table); i++) {
                   12761:                if(critical_error_table[i].code == (REG16(DI) & 0xff) || critical_error_table[i].code == (UINT16)-1) {
                   12762:                        if(active_code_page == 932) {
                   12763:                                message = critical_error_table[i].message_japanese;
                   12764:                        }
                   12765:                        if(message == NULL) {
                   12766:                                message = critical_error_table[i].message_english;
                   12767:                        }
                   12768:                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   12769:                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   12770:                        
                   12771:                        SREG(ES) = WORK_TOP >> 4;
                   12772:                        i386_load_segment_descriptor(ES);
                   12773:                        REG16(DI) = 0x0000;
                   12774:                        break;
                   12775:                }
                   12776:        }
                   12777:        fprintf(stderr, "\n%s", message);
                   12778:        if(!(REG8(AH) & 0x80)) {
                   12779:                if(REG8(AH) & 0x01) {
                   12780:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�������ݒ� �h���C�u" : "writing drive", 'A' + REG8(AL));
                   12781:                } else {
                   12782:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�ǂݎ�蒆 �h���C�u" : "reading drive", 'A' + REG8(AL));
                   12783:                }
                   12784:        }
                   12785:        fprintf(stderr, "\n");
                   12786:        
1.1.1.33  root     12787:        {
1.1.1.32  root     12788:                fprintf(stderr, "%s",   (active_code_page == 932) ? "���~ (A)" : "Abort");
1.1.1.33  root     12789:        }
1.1.1.32  root     12790:        if(REG8(AH) & 0x10) {
                   12791:                fprintf(stderr, ", %s", (active_code_page == 932) ? "�Ď��s (R)" : "Retry");
                   12792:        }
                   12793:        if(REG8(AH) & 0x20) {
                   12794:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���� (I)" : "Ignore");
                   12795:        }
                   12796:        if(REG8(AH) & 0x08) {
                   12797:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���s (F)" : "Fail");
                   12798:        }
                   12799:        fprintf(stderr, "? ");
                   12800:        
                   12801:        while(1) {
                   12802:                while(!_kbhit()) {
                   12803:                        Sleep(10);
                   12804:                }
                   12805:                key = _getch();
                   12806:                
                   12807:                if(key == 'I' || key == 'i') {
                   12808:                        if(REG8(AH) & 0x20) {
                   12809:                                REG8(AL) = 0;
                   12810:                                break;
                   12811:                        }
                   12812:                } else if(key == 'R' || key == 'r') {
                   12813:                        if(REG8(AH) & 0x10) {
                   12814:                                REG8(AL) = 1;
                   12815:                                break;
                   12816:                        }
                   12817:                } else if(key == 'A' || key == 'a') {
                   12818:                        REG8(AL) = 2;
                   12819:                        break;
                   12820:                } else if(key == 'F' || key == 'f') {
                   12821:                        if(REG8(AH) & 0x08) {
                   12822:                                REG8(AL) = 3;
                   12823:                                break;
                   12824:                        }
                   12825:                }
                   12826:        }
                   12827:        fprintf(stderr, "%c\n", key);
                   12828: }
                   12829: 
1.1       root     12830: inline void msdos_int_25h()
                   12831: {
                   12832:        UINT16 seg, ofs;
                   12833:        DWORD dwSize;
                   12834:        
1.1.1.3   root     12835: #if defined(HAS_I386)
                   12836:        I386OP(pushf)();
                   12837: #else
                   12838:        PREFIX86(_pushf());
                   12839: #endif
1.1       root     12840:        
                   12841:        if(!(REG8(AL) < 26)) {
                   12842:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     12843:                m_CF = 1;
1.1       root     12844:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   12845:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     12846:                m_CF = 1;
1.1       root     12847:        } else {
                   12848:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   12849:                char dev[64];
                   12850:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   12851:                
                   12852:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   12853:                if(hFile == INVALID_HANDLE_VALUE) {
                   12854:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     12855:                        m_CF = 1;
1.1       root     12856:                } else {
1.1.1.19  root     12857:                        UINT32 top_sector  = REG16(DX);
                   12858:                        UINT16 sector_num  = REG16(CX);
                   12859:                        UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   12860:                        
                   12861:                        if(sector_num == 0xffff) {
                   12862:                                top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   12863:                                sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   12864:                                UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   12865:                                UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   12866:                                buffer_addr = (seg << 4) + ofs;
                   12867:                        }
                   12868: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   12869: //                             REG8(AL) = 0x02; // drive not ready
                   12870: //                             m_CF = 1;
                   12871: //                     } else 
                   12872:                        if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     12873:                                REG8(AL) = 0x08; // sector not found
1.1.1.3   root     12874:                                m_CF = 1;
1.1.1.19  root     12875:                        } else if(ReadFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     12876:                                REG8(AL) = 0x0b; // read error
1.1.1.3   root     12877:                                m_CF = 1;
1.1       root     12878:                        }
                   12879:                        CloseHandle(hFile);
                   12880:                }
                   12881:        }
                   12882: }
                   12883: 
                   12884: inline void msdos_int_26h()
                   12885: {
1.1.1.42  root     12886:        // this operation may cause serious damage for drives, so support only floppy disk...
1.1       root     12887:        UINT16 seg, ofs;
                   12888:        DWORD dwSize;
                   12889:        
1.1.1.3   root     12890: #if defined(HAS_I386)
                   12891:        I386OP(pushf)();
                   12892: #else
                   12893:        PREFIX86(_pushf());
                   12894: #endif
1.1       root     12895:        
                   12896:        if(!(REG8(AL) < 26)) {
                   12897:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     12898:                m_CF = 1;
1.1       root     12899:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   12900:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     12901:                m_CF = 1;
1.1       root     12902:        } else {
                   12903:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   12904:                char dev[64];
                   12905:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   12906:                
                   12907:                if(dpb->media_type == 0xf8) {
                   12908:                        // this drive is not a floppy
1.1.1.6   root     12909: //                     if(!(((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag & 0x40)) {
                   12910: //                             fatalerror("This application tried the absolute disk write to drive %c:\n", 'A' + REG8(AL));
                   12911: //                     }
1.1       root     12912:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     12913:                        m_CF = 1;
1.1       root     12914:                } else {
                   12915:                        HANDLE hFile = CreateFile(dev, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   12916:                        if(hFile == INVALID_HANDLE_VALUE) {
                   12917:                                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     12918:                                m_CF = 1;
1.1       root     12919:                        } else {
1.1.1.19  root     12920:                                UINT32 top_sector  = REG16(DX);
                   12921:                                UINT16 sector_num  = REG16(CX);
                   12922:                                UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   12923:                                
                   12924:                                if(sector_num == 0xffff) {
                   12925:                                        top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   12926:                                        sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   12927:                                        UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   12928:                                        UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   12929:                                        buffer_addr = (seg << 4) + ofs;
                   12930:                                }
1.1       root     12931:                                if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   12932:                                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     12933:                                        m_CF = 1;
1.1.1.19  root     12934:                                } else if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     12935:                                        REG8(AL) = 0x08; // sector not found
1.1.1.3   root     12936:                                        m_CF = 1;
1.1.1.19  root     12937:                                } else if(WriteFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     12938:                                        REG8(AL) = 0x0a; // write error
1.1.1.3   root     12939:                                        m_CF = 1;
1.1       root     12940:                                }
                   12941:                                CloseHandle(hFile);
                   12942:                        }
                   12943:                }
                   12944:        }
                   12945: }
                   12946: 
                   12947: inline void msdos_int_27h()
                   12948: {
1.1.1.29  root     12949:        int paragraphs = (min(REG16(DX), 0xfff0) + 15) >> 4;
                   12950:        try {
                   12951:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   12952:        } catch(...) {
                   12953:                // recover the broken mcb
                   12954:                int mcb_seg = SREG(CS) - 1;
                   12955:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     12956:                
1.1.1.29  root     12957:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     12958:                        mcb->mz = 'M';
                   12959:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   12960:                        
1.1.1.29  root     12961:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39  root     12962:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29  root     12963:                        } else {
1.1.1.39  root     12964:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29  root     12965:                        }
                   12966:                } else {
                   12967:                        mcb->mz = 'Z';
1.1.1.30  root     12968:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     12969:                }
                   12970:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   12971:        }
1.1.1.3   root     12972:        msdos_process_terminate(SREG(CS), retval | 0x300, 0);
1.1       root     12973: }
                   12974: 
                   12975: inline void msdos_int_29h()
                   12976: {
1.1.1.14  root     12977: #if 1
                   12978:        // need to check escape sequences
1.1       root     12979:        msdos_putch(REG8(AL));
1.1.1.14  root     12980: #else
                   12981:        DWORD num;
                   12982:        vram_flush();
1.1.1.23  root     12983:        WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), &REG8(AL), 1, &num, NULL);
1.1.1.14  root     12984:        cursor_moved = true;
                   12985: #endif
1.1       root     12986: }
                   12987: 
                   12988: inline void msdos_int_2eh()
                   12989: {
                   12990:        char tmp[MAX_PATH], command[MAX_PATH], opt[MAX_PATH];
                   12991:        memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     12992:        strcpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
1.1       root     12993:        char *token = my_strtok(tmp, " ");
                   12994:        strcpy(command, token);
                   12995:        strcpy(opt, token + strlen(token) + 1);
                   12996:        
                   12997:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   12998:        param->env_seg = 0;
                   12999:        param->cmd_line.w.l = 44;
                   13000:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   13001:        param->fcb1.w.l = 24;
                   13002:        param->fcb1.w.h = (WORK_TOP >> 4);
                   13003:        param->fcb2.w.l = 24;
                   13004:        param->fcb2.w.h = (WORK_TOP >> 4);
                   13005:        
                   13006:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   13007:        
                   13008:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   13009:        cmd_line->len = strlen(opt);
                   13010:        strcpy(cmd_line->cmd, opt);
                   13011:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   13012:        
1.1.1.28  root     13013:        try {
                   13014:                if(msdos_process_exec(command, param, 0)) {
                   13015:                        REG16(AX) = 0xffff; // error before processing command
                   13016:                } else {
                   13017:                        // set flag to set retval to ax when the started process is terminated
                   13018:                        process_t *process = msdos_process_info_get(current_psp);
                   13019:                        process->called_by_int2eh = true;
                   13020:                }
                   13021:        } catch(...) {
                   13022:                REG16(AX) = 0xffff; // error before processing command
                   13023:        }
1.1       root     13024: }
                   13025: 
1.1.1.29  root     13026: inline void msdos_int_2fh_05h()
                   13027: {
                   13028:        switch(REG8(AL)) {
                   13029:        case 0x00:
1.1.1.32  root     13030:                REG8(AL) = 0xff;
                   13031:                break;
                   13032:        case 0x01:
                   13033:        case 0x02:
                   13034:                for(int i = 0; i < array_length(standard_error_table); i++) {
                   13035:                        if(standard_error_table[i].code == REG16(BX) || standard_error_table[i].code == (UINT16)-1) {
                   13036:                                const char *message = NULL;
                   13037:                                if(active_code_page == 932) {
                   13038:                                        message = standard_error_table[i].message_japanese;
                   13039:                                }
                   13040:                                if(message == NULL) {
                   13041:                                        message = standard_error_table[i].message_english;
                   13042:                                }
                   13043:                                strcpy((char *)(mem + WORK_TOP), message);
                   13044:                                
                   13045:                                SREG(ES) = WORK_TOP >> 4;
                   13046:                                i386_load_segment_descriptor(ES);
                   13047:                                REG16(DI) = 0x0000;
                   13048:                                REG8(AL) = 0x01;
                   13049:                                break;
                   13050:                        }
                   13051:                }
1.1.1.29  root     13052:                break;
                   13053:        default:
                   13054:                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));
                   13055:                m_CF = 1;
                   13056:        }
                   13057: }
                   13058: 
1.1.1.44  root     13059: inline void msdos_int_2fh_06h()
                   13060: {
                   13061:        switch(REG8(AL)) {
                   13062:        case 0x00:
                   13063:                // ASSIGN is not installed
                   13064:                break;
                   13065:        case 0x01:
                   13066:                // this call is available from within MIRROR.COM even if ASSIGN is not installed
                   13067:                REG16(AX) = 0x01;
                   13068:                m_CF = 1;
                   13069:                break;
                   13070:        default:
                   13071:                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));
                   13072:                REG16(AX) = 0x01;
                   13073:                m_CF = 1;
                   13074:                break;
                   13075:        }
                   13076: }
                   13077: 
1.1.1.22  root     13078: inline void msdos_int_2fh_11h()
                   13079: {
                   13080:        switch(REG8(AL)) {
                   13081:        case 0x00:
1.1.1.29  root     13082:                if(i386_read_stack() == 0xdada) {
                   13083:                        // MSCDEX is not installed
                   13084: //                     REG8(AL) = 0x00;
                   13085:                } else {
                   13086:                        // Network Redirector is not installed
                   13087: //                     REG8(AL) = 0x00;
                   13088:                }
1.1.1.22  root     13089:                break;
                   13090:        default:
1.1.1.43  root     13091: //             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     13092:                REG16(AX) = 0x49; //  network software not installed
1.1.1.22  root     13093:                m_CF = 1;
                   13094:                break;
                   13095:        }
                   13096: }
                   13097: 
1.1.1.21  root     13098: inline void msdos_int_2fh_12h()
                   13099: {
                   13100:        switch(REG8(AL)) {
1.1.1.22  root     13101:        case 0x00:
1.1.1.29  root     13102:                // DOS 3.0+ internal functions are installed
1.1.1.22  root     13103:                REG8(AL) = 0xff;
                   13104:                break;
1.1.1.29  root     13105: //     case 0x01: // DOS 3.0+ internal - Close Current File
                   13106:        case 0x02:
                   13107:                {
                   13108:                        UINT16 stack = i386_read_stack();
                   13109:                        REG16(BX) = *(UINT16 *)(mem + 4 * stack + 0);
                   13110:                        SREG(ES) = *(UINT16 *)(mem + 4 * stack + 2);
                   13111:                        i386_load_segment_descriptor(ES);
                   13112:                }
                   13113:                break;
1.1.1.30  root     13114:        case 0x03:
                   13115:                SREG(DS) = (DEVICE_TOP >> 4);
                   13116:                i386_load_segment_descriptor(DS);
                   13117:                break;
1.1.1.29  root     13118:        case 0x04:
                   13119:                {
                   13120:                        UINT16 stack = i386_read_stack();
                   13121:                        REG8(AL) = (stack == '/') ? '\\' : stack;
                   13122: #if defined(HAS_I386)
                   13123:                        m_ZF = (REG8(AL) == '\\');
                   13124: #else
                   13125:                        m_ZeroVal = (REG8(AL) != '\\');
                   13126: #endif
                   13127:                }
                   13128:                break;
                   13129:        case 0x05:
                   13130:                msdos_putch(i386_read_stack());
                   13131:                break;
                   13132: //     case 0x06: // DOS 3.0+ internal - Invole Critical Error
                   13133: //     case 0x07: // DOS 3.0+ internal - Make Disk Buffer Most Recentry Used
                   13134: //     case 0x08: // DOS 3.0+ internal - Decrement SFT Reference Count
                   13135: //     case 0x09: // DOS 3.0+ internal - Flush and FREE Disk Buffer
                   13136: //     case 0x0a: // DOS 3.0+ internal - Perform Critical Error Interrupt
                   13137: //     case 0x0b: // DOS 3.0+ internal - Signal Sharing Violation to User
                   13138: //     case 0x0c: // DOS 3.0+ internal - Open Device and Set SFT Owner/Mode
                   13139:        case 0x0d:
                   13140:                {
                   13141:                        SYSTEMTIME time;
                   13142:                        FILETIME file_time;
                   13143:                        WORD dos_date, dos_time;
                   13144:                        GetLocalTime(&time);
                   13145:                        SystemTimeToFileTime(&time, &file_time);
                   13146:                        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   13147:                        REG16(AX) = dos_date;
                   13148:                        REG16(DX) = dos_time;
                   13149:                }
                   13150:                break;
                   13151: //     case 0x0e: // DOS 3.0+ internal - Mark All Disk Buffers Unreferenced
                   13152: //     case 0x0f: // DOS 3.0+ internal - Make Buffer Most Recentry Used
                   13153: //     case 0x10: // DOS 3.0+ internal - Find Unreferenced Disk Buffer
                   13154:        case 0x11:
                   13155:                {
                   13156:                        char path[MAX_PATH], *p;
                   13157:                        strcpy(path, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   13158:                        my_strupr(path);
                   13159:                        while((p = my_strchr(path, '/')) != NULL) {
                   13160:                                *p = '\\';
                   13161:                        }
                   13162:                        strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
                   13163:                }
                   13164:                break;
                   13165:        case 0x12:
                   13166:                REG16(CX) = strlen((char *)(mem + SREG_BASE(ES) + REG16(DI)));
                   13167:                break;
                   13168:        case 0x13:
                   13169:                {
                   13170:                        char tmp[2] = {0};
                   13171:                        tmp[0] = i386_read_stack();
                   13172:                        my_strupr(tmp);
                   13173:                        REG8(AL) = tmp[0];
                   13174:                }
                   13175:                break;
                   13176:        case 0x14:
                   13177: #if defined(HAS_I386)
                   13178:                m_ZF = (SREG_BASE(DS) + REG16(SI) == SREG_BASE(ES) + REG16(DI));
                   13179: #else
                   13180:                m_ZeroVal = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   13181: #endif
                   13182:                m_CF = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   13183:                break;
                   13184: //     case 0x15: // DOS 3.0+ internal - Flush Buffer
1.1.1.21  root     13185:        case 0x16:
                   13186:                if(REG16(BX) < 20) {
                   13187:                        SREG(ES) = SFT_TOP >> 4;
                   13188:                        i386_load_segment_descriptor(ES);
                   13189:                        REG16(DI) = 6 + 0x3b * REG16(BX);
                   13190:                        
                   13191:                        // update system file table
                   13192:                        UINT8* sft = mem + SFT_TOP + 6 + 0x3b * REG16(BX);
                   13193:                        if(file_handler[REG16(BX)].valid) {
                   13194:                                int count = 0;
                   13195:                                for(int i = 0; i < 20; i++) {
                   13196:                                        if(msdos_psp_get_file_table(i, current_psp) == REG16(BX)) {
                   13197:                                                count++;
                   13198:                                        }
                   13199:                                }
                   13200:                                *(UINT16 *)(sft + 0x00) = count ? count : 0xffff;
                   13201:                                *(UINT32 *)(sft + 0x15) = _tell(REG16(BX));
                   13202:                                _lseek(REG16(BX), 0, SEEK_END);
                   13203:                                *(UINT32 *)(sft + 0x11) = _tell(REG16(BX));
                   13204:                                _lseek(REG16(BX), *(UINT32 *)(sft + 0x15), SEEK_SET);
                   13205:                        } else {
                   13206:                                memset(sft, 0, 0x3b);
                   13207:                        }
                   13208:                } else {
                   13209:                        REG16(AX) = 0x06;
                   13210:                        m_CF = 1;
                   13211:                }
                   13212:                break;
1.1.1.29  root     13213: //     case 0x17: // DOS 3.0+ internal - Get Current Directory Structure for Drive
                   13214: //     case 0x18: // DOS 3.0+ internal - Get Caller's Registers
                   13215: //     case 0x19: // DOS 3.0+ internal - Set Drive???
                   13216:        case 0x1a:
                   13217:                {
                   13218:                        char *path = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full[MAX_PATH];
                   13219:                        if(path[1] == ':') {
                   13220:                                if(path[0] >= 'a' && path[0] <= 'z') {
                   13221:                                        REG8(AL) = path[0] - 'a' + 1;
                   13222:                                } else if(path[0] >= 'A' && path[0] <= 'Z') {
                   13223:                                        REG8(AL) = path[0] - 'A' + 1;
                   13224:                                } else {
                   13225:                                        REG8(AL) = 0xff; // invalid
                   13226:                                }
                   13227:                                strcpy(full, path);
                   13228:                                strcpy(path, full + 2);
                   13229:                        } else if(GetFullPathName(path, MAX_PATH, full, NULL) != 0 && full[1] == ':') {
                   13230:                                if(full[0] >= 'a' && full[0] <= 'z') {
                   13231:                                        REG8(AL) = full[0] - 'a' + 1;
                   13232:                                } else if(full[0] >= 'A' && full[0] <= 'Z') {
                   13233:                                        REG8(AL) = full[0] - 'A' + 1;
                   13234:                                } else {
                   13235:                                        REG8(AL) = 0xff; // invalid
                   13236:                                }
                   13237:                        } else {
                   13238:                                REG8(AL) = 0x00; // default
                   13239:                        }
                   13240:                }
                   13241:                break;
                   13242:        case 0x1b:
                   13243:                {
                   13244:                        int year = REG16(CX) + 1980;
                   13245:                        REG8(AL) = ((year % 4) == 0 && (year % 100) != 0 && (year % 400) == 0) ? 29 : 28;
                   13246:                }
                   13247:                break;
                   13248: //     case 0x1c: // DOS 3.0+ internal - Check Sum Memory
                   13249: //     case 0x1d: // DOS 3.0+ internal - Sum Memory
                   13250:        case 0x1e:
                   13251:                {
                   13252:                        char *path_1st = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full_1st[MAX_PATH];
                   13253:                        char *path_2nd = (char *)(mem + SREG_BASE(ES) + REG16(DI)), full_2nd[MAX_PATH];
                   13254:                        if(GetFullPathName(path_1st, MAX_PATH, full_1st, NULL) != 0 && GetFullPathName(path_2nd, MAX_PATH, full_2nd, NULL) != 0) {
                   13255: #if defined(HAS_I386)
                   13256:                                m_ZF = (strcmp(full_1st, full_2nd) == 0);
                   13257: #else
                   13258:                                m_ZeroVal = (strcmp(full_1st, full_2nd) != 0);
                   13259: #endif
                   13260:                        } else {
                   13261: #if defined(HAS_I386)
                   13262:                                m_ZF = (strcmp(path_1st, path_2nd) == 0);
                   13263: #else
                   13264:                                m_ZeroVal = (strcmp(path_1st, path_2nd) != 0);
                   13265: #endif
                   13266:                        }
                   13267:                }
                   13268:                break;
                   13269: //     case 0x1f: // DOS 3.0+ internal - Build Current Directory Structure
1.1.1.21  root     13270:        case 0x20:
                   13271:                {
                   13272:                        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   13273:                        
                   13274:                        if(fd < 20) {
                   13275:                                SREG(ES) = current_psp;
                   13276:                                i386_load_segment_descriptor(ES);
                   13277:                                REG16(DI) = offsetof(psp_t, file_table) + fd;
                   13278:                        } else {
                   13279:                                REG16(AX) = 0x06;
                   13280:                                m_CF = 1;
                   13281:                        }
                   13282:                }
                   13283:                break;
1.1.1.29  root     13284:        case 0x21:
                   13285:                msdos_int_21h_60h(0);
                   13286:                break;
                   13287: //     case 0x22: // DOS 3.0+ internal - Set Extended Error Info
                   13288: //     case 0x23: // DOS 3.0+ internal - Check If Character Device
                   13289: //     case 0x24: // DOS 3.0+ internal - Sharing Retry Delay
                   13290:        case 0x25:
                   13291:                REG16(CX) = strlen((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   13292:                break;
                   13293:        case 0x26:
                   13294:                REG8(AL) = REG8(CL);
                   13295:                msdos_int_21h_3dh();
                   13296:                break;
                   13297:        case 0x27:
                   13298:                msdos_int_21h_3eh();
                   13299:                break;
                   13300:        case 0x28:
                   13301:                REG16(AX) = REG16(BP);
                   13302:                msdos_int_21h_42h();
                   13303:                break;
                   13304:        case 0x29:
                   13305:                msdos_int_21h_3fh();
                   13306:                break;
                   13307: //     case 0x2a: // DOS 3.0+ internal - Set Fast Open Entry Point
                   13308:        case 0x2b:
                   13309:                REG16(AX) = REG16(BP);
                   13310:                msdos_int_21h_44h();
                   13311:                break;
                   13312:        case 0x2c:
                   13313:                REG16(BX) = DEVICE_TOP >> 4;
                   13314:                REG16(AX) = 22;
                   13315:                break;
                   13316:        case 0x2d:
                   13317:                {
                   13318:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   13319:                        REG16(AX) = sda->extended_error_code;
                   13320:                }
                   13321:                break;
                   13322:        case 0x2e:
                   13323:                if(REG8(DL) == 0x00 || REG8(DL) == 0x02 || REG8(DL) == 0x04 || REG8(DL) == 0x06) {
1.1.1.32  root     13324:                        SREG(ES) = 0x0001;
                   13325:                        i386_load_segment_descriptor(ES);
                   13326:                        REG16(DI) = 0x00;
                   13327:                } else if(REG8(DL) == 0x08) {
                   13328:                        // dummy parameter error message read routine is at fffd:0010
                   13329:                        SREG(ES) = 0xfffd;
1.1.1.22  root     13330:                        i386_load_segment_descriptor(ES);
1.1.1.32  root     13331:                        REG16(DI) = 0x0010;
1.1.1.22  root     13332:                }
                   13333:                break;
1.1.1.29  root     13334:        case 0x2f:
                   13335:                if(REG16(DX) != 0) {
1.1.1.30  root     13336:                        dos_major_version = REG8(DL);
                   13337:                        dos_minor_version = REG8(DH);
1.1.1.29  root     13338:                } else {
                   13339:                        REG8(DL) = 7;
                   13340:                        REG8(DH) = 10;
                   13341:                }
                   13342:                break;
                   13343: //     case 0x30: // Windows95 - Find SFT Entry in Internal File Tables
                   13344: //     case 0x31: // Windows95 - Set/Clear "Report Windows to DOS Programs" Flag
1.1.1.22  root     13345:        default:
                   13346:                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));
                   13347:                REG16(AX) = 0x01;
                   13348:                m_CF = 1;
                   13349:                break;
                   13350:        }
                   13351: }
                   13352: 
1.1.1.30  root     13353: inline void msdos_int_2fh_13h()
                   13354: {
                   13355:        static UINT16 prevDS = 0, prevDX = 0;
                   13356:        static UINT16 prevES = 0, prevBX = 0;
                   13357:        UINT16 tmp;
                   13358:        
                   13359:        tmp = SREG(DS); SREG(DS) = prevDS; prevDS = tmp;
                   13360:        i386_load_segment_descriptor(DS);
                   13361:        tmp = REG16(DX); REG16(DX) = prevDX; prevDX = tmp;
                   13362:        
                   13363:        tmp = SREG(ES); SREG(ES) = prevES; prevES = tmp;
                   13364:        i386_load_segment_descriptor(ES);
                   13365:        tmp = REG16(BX); REG16(BX) = prevBX; prevBX = tmp;
                   13366: }
                   13367: 
1.1.1.22  root     13368: inline void msdos_int_2fh_14h()
                   13369: {
                   13370:        switch(REG8(AL)) {
                   13371:        case 0x00:
1.1.1.29  root     13372:                // NLSFUNC.COM is installed
                   13373:                REG8(AL) = 0xff;
1.1.1.25  root     13374:                break;
                   13375:        case 0x01:
                   13376:        case 0x03:
                   13377:                REG8(AL) = 0x00;
                   13378:                active_code_page = REG16(BX);
                   13379:                msdos_nls_tables_update();
                   13380:                break;
                   13381:        case 0x02:
                   13382:                REG8(AL) = get_extended_country_info(REG16(BP));
                   13383:                break;
                   13384:        case 0x04:
1.1.1.42  root     13385:                for(int i = 0;; i++) {
                   13386:                        if(country_table[i].code == REG16(DX)) {
                   13387:                                get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)), country_table[i].usPrimaryLanguage, country_table[i].usSubLanguage);
                   13388:                                break;
                   13389:                        } else if(country_table[i].code == -1) {
                   13390:                                get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)));
                   13391:                                break;
                   13392:                        }
                   13393:                }
1.1.1.25  root     13394:                REG8(AL) = 0x00;
1.1.1.22  root     13395:                break;
                   13396:        default:
                   13397:                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));
                   13398:                REG16(AX) = 0x01;
                   13399:                m_CF = 1;
                   13400:                break;
                   13401:        }
                   13402: }
                   13403: 
                   13404: inline void msdos_int_2fh_15h()
                   13405: {
                   13406:        switch(REG8(AL)) {
1.1.1.29  root     13407:        case 0x00: // CD-ROM - Installation Check
                   13408:                if(REG16(BX) == 0x0000) {
1.1.1.43  root     13409: #if 0
                   13410:                        // MSCDEX is installed
                   13411:                        REG16(BX) = 0;
                   13412:                        for(int i = 0, n = 0; i < 26; i++) {
1.1.1.44  root     13413:                                if(msdos_is_cdrom_drive(i)) {
                   13414:                                        if(REG16(BX) == 0) {
                   13415:                                                REG16(CX) = i;
1.1.1.43  root     13416:                                        }
1.1.1.44  root     13417:                                        REG16(BX)++;
1.1.1.43  root     13418:                                }
                   13419:                        }
                   13420: #else
1.1.1.29  root     13421:                        // MSCDEX is not installed
                   13422: //                     REG8(AL) = 0x00;
1.1.1.43  root     13423: #endif
1.1.1.29  root     13424:                } else {
                   13425:                        // GRAPHICS.COM is not installed
                   13426: //                     REG8(AL) = 0x00;
                   13427:                }
1.1.1.22  root     13428:                break;
1.1.1.43  root     13429:        case 0x0b:
1.1.1.44  root     13430:                // this call is available from within DOSSHELL even if MSCDEX is not installed
                   13431:                REG16(AX) = msdos_is_cdrom_drive(REG8(CL)) ? 0x5ad8 : 0x0000;
                   13432:                REG16(BX) = 0xadad;
1.1.1.43  root     13433:                break;
                   13434:        case 0x0d:
1.1.1.44  root     13435:                for(int i = 0, n = 0; i < 26; i++) {
                   13436:                        if(msdos_is_cdrom_drive(i)) {
                   13437:                                mem[SREG_BASE(ES) + REG16(BX) + (n++)] = i;
1.1.1.43  root     13438:                        }
                   13439:                }
                   13440:                break;
1.1.1.22  root     13441:        case 0xff:
1.1.1.29  root     13442:                if(REG16(BX) == 0x0000) {
                   13443:                        // CORELCDX is not installed
                   13444:                } else {
                   13445:                        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));
                   13446:                        REG16(AX) = 0x01;
                   13447:                        m_CF = 1;
                   13448:                }
1.1.1.22  root     13449:                break;
1.1.1.21  root     13450:        default:
1.1.1.22  root     13451:                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     13452:                REG16(AX) = 0x01;
                   13453:                m_CF = 1;
                   13454:                break;
                   13455:        }
                   13456: }
                   13457: 
1.1       root     13458: inline void msdos_int_2fh_16h()
                   13459: {
                   13460:        switch(REG8(AL)) {
                   13461:        case 0x00:
1.1.1.14  root     13462:                if(no_windows) {
1.1.1.29  root     13463:                        // neither Windows 3.x enhanced mode nor Windows/386 2.x running
                   13464: //                     REG8(AL) = 0x00;
1.1.1.14  root     13465:                } else {
1.1.1.30  root     13466:                        REG8(AL) = win_major_version;
                   13467:                        REG8(AH) = win_minor_version;
1.1       root     13468:                }
                   13469:                break;
1.1.1.43  root     13470:        case 0x05: // Windows Enhanced Mode & 286 DOSX Init Broadcast
1.1.1.30  root     13471:                // from DOSBox
                   13472:                i386_set_a20_line(1);
                   13473:                break;
1.1.1.43  root     13474:        case 0x06: // Windows Enhanced Mode & 286 DOSX exit Broadcast
                   13475:        case 0x08: // Windows Enhanced Mode Init Complete Broadcast
                   13476:        case 0x09: // Windows Enhanced Mode Begin Exit Broadcast
                   13477:                break;
                   13478:        case 0x07:
                   13479:                // Virtual Device Call API
                   13480:                break;
1.1.1.22  root     13481:        case 0x0a:
                   13482:                if(!no_windows) {
                   13483:                        REG16(AX) = 0x0000;
1.1.1.30  root     13484:                        REG8(BH) = win_major_version;
                   13485:                        REG8(BL) = win_minor_version;
1.1.1.22  root     13486:                        REG16(CX) = 0x0003; // enhanced
                   13487:                }
                   13488:                break;
1.1.1.30  root     13489:        case 0x0b:
                   13490:                // no TRS, keep ES:DI = 0000h:0000h
1.1.1.22  root     13491:        case 0x0e:
                   13492:        case 0x0f:
1.1.1.30  root     13493:        case 0x10:
1.1.1.22  root     13494:        case 0x11:
                   13495:        case 0x12:
                   13496:        case 0x13:
                   13497:        case 0x14:
1.1.1.30  root     13498:        case 0x15:
1.1.1.43  root     13499:        case 0x81:
                   13500:        case 0x82:
1.1.1.44  root     13501:        case 0x84:
1.1.1.33  root     13502:        case 0x86:
1.1.1.22  root     13503:        case 0x87:
1.1.1.30  root     13504:        case 0x89:
1.1.1.33  root     13505:        case 0x8a:
1.1.1.22  root     13506:                // function not supported, do not clear AX
                   13507:                break;
1.1.1.14  root     13508:        case 0x80:
                   13509:                Sleep(10);
1.1.1.35  root     13510:                REQUEST_HARDWRE_UPDATE();
1.1.1.29  root     13511:                REG8(AL) = 0x00;
1.1.1.14  root     13512:                break;
1.1.1.33  root     13513:        case 0x83:
                   13514:                REG16(BX) = 0x01; // system vm id
                   13515:                break;
1.1.1.22  root     13516:        case 0x8e:
                   13517:                REG16(AX) = 0x00; // failed
                   13518:                break;
1.1.1.20  root     13519:        case 0x8f:
                   13520:                switch(REG8(DH)) {
                   13521:                case 0x00:
                   13522:                case 0x02:
                   13523:                case 0x03:
                   13524:                        REG16(AX) = 0x00;
                   13525:                        break;
                   13526:                case 0x01:
                   13527:                        REG16(AX) = 0x168f;
                   13528:                        break;
                   13529:                }
                   13530:                break;
1.1       root     13531:        default:
1.1.1.22  root     13532:                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));
                   13533:                REG16(AX) = 0x01;
                   13534:                m_CF = 1;
                   13535:                break;
                   13536:        }
                   13537: }
                   13538: 
                   13539: inline void msdos_int_2fh_19h()
                   13540: {
                   13541:        switch(REG8(AL)) {
                   13542:        case 0x00:
1.1.1.29  root     13543:                // SHELLB.COM is not installed
                   13544: //             REG8(AL) = 0x00;
1.1.1.22  root     13545:                break;
                   13546:        case 0x01:
                   13547:        case 0x02:
                   13548:        case 0x03:
                   13549:        case 0x04:
                   13550:                REG16(AX) = 0x01;
                   13551:                m_CF = 1;
                   13552:                break;
1.1.1.29  root     13553:        case 0x80:
                   13554:                // IBM ROM-DOS v4.0 is not installed
                   13555: //             REG8(AL) = 0x00;
                   13556:                break;
1.1.1.22  root     13557:        default:
                   13558:                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     13559:                REG16(AX) = 0x01;
1.1.1.3   root     13560:                m_CF = 1;
1.1       root     13561:                break;
                   13562:        }
                   13563: }
                   13564: 
                   13565: inline void msdos_int_2fh_1ah()
                   13566: {
                   13567:        switch(REG8(AL)) {
                   13568:        case 0x00:
1.1.1.29  root     13569:                // ANSI.SYS is installed
1.1       root     13570:                REG8(AL) = 0xff;
                   13571:                break;
                   13572:        default:
1.1.1.22  root     13573:                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));
                   13574:                REG16(AX) = 0x01;
                   13575:                m_CF = 1;
                   13576:                break;
                   13577:        }
                   13578: }
                   13579: 
1.1.1.30  root     13580: inline void msdos_int_2fh_40h()
1.1.1.22  root     13581: {
                   13582:        switch(REG8(AL)) {
                   13583:        case 0x00:
1.1.1.30  root     13584:                // Windows 3+ - Get Virtual Device Driver (VDD) Capabilities
                   13585:                REG8(AL) = 0x01; // does not virtualize video access
1.1.1.22  root     13586:                break;
1.1.1.43  root     13587:        case 0x10:
                   13588:                // OS/2 v2.0+ - Installation Check
                   13589:                REG16(AX) = 0x01;
                   13590:                m_CF = 1;
                   13591:                break;
1.1.1.22  root     13592:        default:
                   13593:                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     13594:                REG16(AX) = 0x01;
1.1.1.3   root     13595:                m_CF = 1;
1.1       root     13596:                break;
                   13597:        }
                   13598: }
                   13599: 
                   13600: inline void msdos_int_2fh_43h()
                   13601: {
                   13602:        switch(REG8(AL)) {
                   13603:        case 0x00:
1.1.1.29  root     13604:                // XMS is installed ?
1.1.1.19  root     13605: #ifdef SUPPORT_XMS
                   13606:                if(support_xms) {
                   13607:                        REG8(AL) = 0x80;
1.1.1.44  root     13608:                }
                   13609: #endif
                   13610:                break;
                   13611:        case 0x08:
                   13612: #ifdef SUPPORT_XMS
                   13613:                if(support_xms) {
                   13614:                        REG8(AL) = 0x43;
                   13615:                        REG8(BL) = 0x01; // IBM PC/AT
                   13616:                        REG8(BH) = 0x01; // Fast AT A20 switch time
                   13617:                }
1.1.1.19  root     13618: #endif
                   13619:                break;
                   13620:        case 0x10:
                   13621:                SREG(ES) = XMS_TOP >> 4;
                   13622:                i386_load_segment_descriptor(ES);
1.1.1.26  root     13623:                REG16(BX) = 0x15;
1.1       root     13624:                break;
1.1.1.44  root     13625:        case 0xe0:
                   13626:                // DOS Protected Mode Services (DPMS) v1.0 is not installed
                   13627:                if(REG16(BX) == 0x0000 && REG16(CX) == 0x4450 && REG16(DX) == 0x4d53) {
                   13628:                        break;
                   13629:                }
1.1       root     13630:        default:
1.1.1.22  root     13631:                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));
                   13632:                REG16(AX) = 0x01;
                   13633:                m_CF = 1;
                   13634:                break;
                   13635:        }
                   13636: }
                   13637: 
                   13638: inline void msdos_int_2fh_46h()
                   13639: {
                   13640:        switch(REG8(AL)) {
                   13641:        case 0x80:
1.1.1.29  root     13642:                // Windows v3.0 is not installed
                   13643: //             REG8(AL) = 0x00;
1.1.1.22  root     13644:                break;
                   13645:        default:
                   13646:                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));
                   13647:                REG16(AX) = 0x01;
                   13648:                m_CF = 1;
                   13649:                break;
                   13650:        }
                   13651: }
                   13652: 
                   13653: inline void msdos_int_2fh_48h()
                   13654: {
                   13655:        switch(REG8(AL)) {
                   13656:        case 0x00:
1.1.1.29  root     13657:                // DOSKEY is not installed
                   13658: //             REG8(AL) = 0x00;
1.1.1.22  root     13659:                break;
                   13660:        case 0x10:
                   13661:                msdos_int_21h_0ah();
                   13662:                REG16(AX) = 0x00;
                   13663:                break;
                   13664:        default:
                   13665:                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     13666:                REG16(AX) = 0x01;
1.1.1.3   root     13667:                m_CF = 1;
1.1       root     13668:                break;
                   13669:        }
                   13670: }
                   13671: 
                   13672: inline void msdos_int_2fh_4ah()
                   13673: {
                   13674:        switch(REG8(AL)) {
1.1.1.29  root     13675: #ifdef SUPPORT_HMA
                   13676:        case 0x01: // DOS 5.0+ - Query Free HMA Space
                   13677:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   13678:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   13679:                                // restore first free mcb in high memory area
                   13680:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   13681:                        }
                   13682:                        int offset = 0xffff;
                   13683:                        if((REG16(BX) = msdos_hma_mem_get_free(&offset)) != 0) {
                   13684:                                REG16(DI) = offset + 0x10;
                   13685:                        } else {
                   13686:                                REG16(DI) = 0xffff;
                   13687:                        }
                   13688:                } else {
                   13689:                        // HMA is already used
                   13690:                        REG16(BX) = 0;
                   13691:                        REG16(DI) = 0xffff;
                   13692:                }
                   13693:                SREG(ES) = 0xffff;
                   13694:                i386_load_segment_descriptor(ES);
                   13695:                break;
                   13696:        case 0x02: // DOS 5.0+ - Allocate HMA Space
                   13697:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   13698:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   13699:                                // restore first free mcb in high memory area
                   13700:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   13701:                        }
                   13702:                        int size = REG16(BX), offset;
                   13703:                        if((size % 16) != 0) {
                   13704:                                size &= ~15;
                   13705:                                size += 16;
                   13706:                        }
                   13707:                        if((offset = msdos_hma_mem_alloc(size, current_psp)) != -1) {
                   13708:                                REG16(BX) = size;
                   13709:                                REG16(DI) = offset + 0x10;
                   13710:                                is_hma_used_by_int_2fh = true;
                   13711:                        } else {
                   13712:                                REG16(BX) = 0;
                   13713:                                REG16(DI) = 0xffff;
                   13714:                        }
                   13715:                } else {
                   13716:                        // HMA is already used
                   13717:                        REG16(BX) = 0;
                   13718:                        REG16(DI) = 0xffff;
                   13719:                }
                   13720:                SREG(ES) = 0xffff;
                   13721:                i386_load_segment_descriptor(ES);
                   13722:                break;
                   13723:        case 0x03: // Windows95 - (De)Allocate HMA Memory Block
                   13724:                if(REG8(DL) == 0x00) {
                   13725:                        if(!is_hma_used_by_xms) {
                   13726:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   13727:                                        // restore first free mcb in high memory area
                   13728:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   13729:                                        is_hma_used_by_int_2fh = false;
                   13730:                                }
                   13731:                                int size = REG16(BX), offset;
                   13732:                                if((size % 16) != 0) {
                   13733:                                        size &= ~15;
                   13734:                                        size += 16;
                   13735:                                }
                   13736:                                if((offset = msdos_hma_mem_alloc(size, REG16(CX))) != -1) {
                   13737: //                                     REG16(BX) = size;
                   13738:                                        SREG(ES) = 0xffff;
                   13739:                                        i386_load_segment_descriptor(ES);
                   13740:                                        REG16(DI) = offset + 0x10;
                   13741:                                        is_hma_used_by_int_2fh = true;
                   13742:                                } else {
                   13743:                                        REG16(DI) = 0xffff;
                   13744:                                }
                   13745:                        } else {
                   13746:                                REG16(DI) = 0xffff;
                   13747:                        }
                   13748:                } else if(REG8(DL) == 0x01) {
                   13749:                        if(!is_hma_used_by_xms) {
                   13750:                                int size = REG16(BX);
                   13751:                                if((size % 16) != 0) {
                   13752:                                        size &= ~15;
                   13753:                                        size += 16;
                   13754:                                }
                   13755:                                if(msdos_hma_mem_realloc(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10, size) != -1) {
                   13756:                                        // memory block address is not changed
                   13757:                                } else {
                   13758:                                        REG16(DI) = 0xffff;
                   13759:                                }
                   13760:                        } else {
                   13761:                                REG16(DI) = 0xffff;
                   13762:                        }
                   13763:                } else if(REG8(DL) == 0x02) {
                   13764:                        if(!is_hma_used_by_xms) {
                   13765:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   13766:                                        // restore first free mcb in high memory area
                   13767:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   13768:                                        is_hma_used_by_int_2fh = false;
                   13769:                                } else {
                   13770:                                        msdos_hma_mem_free(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10);
                   13771:                                        if(msdos_hma_mem_get_free(NULL) == 0xffe0) {
                   13772:                                                is_hma_used_by_int_2fh = false;
                   13773:                                        }
                   13774:                                }
                   13775:                        }
                   13776:                } else {
                   13777:                        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));
                   13778:                        REG16(AX) = 0x01;
                   13779:                        m_CF = 1;
                   13780:                }
                   13781:                break;
                   13782:        case 0x04: // Windows95 - Get Start of HMA Memory Chain
                   13783:                if(!is_hma_used_by_xms) {
                   13784:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   13785:                                // restore first free mcb in high memory area
                   13786:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   13787:                                is_hma_used_by_int_2fh = false;
                   13788:                        }
                   13789:                        REG16(AX) = 0x0000;
                   13790:                        SREG(ES) = 0xffff;
                   13791:                        i386_load_segment_descriptor(ES);
                   13792:                        REG16(DI) = 0x10;
                   13793:                }
                   13794:                break;
                   13795: #else
1.1       root     13796:        case 0x01:
                   13797:        case 0x02:
1.1.1.29  root     13798:                // HMA is already used
1.1.1.27  root     13799:                REG16(BX) = 0x0000;
1.1.1.3   root     13800:                SREG(ES) = 0xffff;
                   13801:                i386_load_segment_descriptor(ES);
1.1       root     13802:                REG16(DI) = 0xffff;
                   13803:                break;
1.1.1.19  root     13804:        case 0x03:
                   13805:                // unable to allocate
                   13806:                REG16(DI) = 0xffff;
                   13807:                break;
                   13808:        case 0x04:
                   13809:                // function not supported, do not clear AX
                   13810:                break;
1.1.1.29  root     13811: #endif
                   13812:        case 0x10:
1.1.1.42  root     13813:                switch(REG16(BX)) {
                   13814:                case 0x0000:
                   13815:                case 0x0001:
                   13816:                case 0x0002:
                   13817:                case 0x0003:
                   13818:                case 0x0004:
                   13819:                case 0x0005:
                   13820:                case 0x0006:
                   13821:                case 0x0007:
                   13822:                case 0x0008:
                   13823:                case 0x000a:
                   13824:                case 0x1234:
                   13825:                        // SMARTDRV v4.00+ is not installed
                   13826:                        break;
                   13827:                default:
1.1.1.29  root     13828:                        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));
                   13829:                        REG16(AX) = 0x01;
                   13830:                        m_CF = 1;
1.1.1.42  root     13831:                        break;
1.1.1.29  root     13832:                }
                   13833:                break;
                   13834:        case 0x11:
1.1.1.42  root     13835:                switch(REG16(BX)) {
                   13836:                case 0x0000:
                   13837:                case 0x0001:
                   13838:                case 0x0002:
                   13839:                case 0x0003:
                   13840:                case 0x0004:
                   13841:                case 0x0005:
                   13842:                case 0x0006:
                   13843:                case 0x0007:
                   13844:                case 0x0008:
                   13845:                case 0x0009:
                   13846:                case 0x000a:
                   13847:                case 0x000b:
                   13848:                case 0xfffe:
                   13849:                case 0xffff:
1.1.1.29  root     13850:                        // DBLSPACE.BIN is not installed
1.1.1.42  root     13851:                        break;
                   13852:                default:
                   13853:                        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));
                   13854:                        REG16(AX) = 0x01;
                   13855:                        m_CF = 1;
                   13856:                        break;
                   13857:                }
                   13858:                break;
                   13859:        case 0x12:
                   13860:                if(REG16(CX) == 0x4d52 && REG16(DX) == 0x4349) {
                   13861:                        // Microsoft Realtime Compression Interface (MRCI) is not installed
                   13862:                } else if(REG16(CX) == 0x5354 && REG16(DX) == 0x4143) {
                   13863:                        // Stacker 4 LZS Compression Interface (LZSAPI) is not installed
1.1.1.29  root     13864:                } else {
                   13865:                        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));
                   13866:                        REG16(AX) = 0x01;
                   13867:                        m_CF = 1;
                   13868:                }
1.1.1.22  root     13869:                break;
1.1.1.42  root     13870:        case 0x13:
                   13871:                // DBLSPACE.BIN is not installed
                   13872:                break;
1.1.1.22  root     13873:        default:
                   13874:                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));
                   13875:                REG16(AX) = 0x01;
                   13876:                m_CF = 1;
                   13877:                break;
                   13878:        }
                   13879: }
                   13880: 
                   13881: inline void msdos_int_2fh_4bh()
                   13882: {
                   13883:        switch(REG8(AL)) {
1.1.1.24  root     13884:        case 0x01:
1.1.1.22  root     13885:        case 0x02:
1.1.1.29  root     13886:                // Task Switcher is not installed
1.1.1.24  root     13887:                break;
                   13888:        case 0x03:
                   13889:                // this call is available from within DOSSHELL even if the task switcher is not installed
                   13890:                REG16(AX) = REG16(BX) = 0x0000; // no more avaiable switcher id
1.1.1.22  root     13891:                break;
1.1.1.30  root     13892:        case 0x04:
                   13893:                REG16(BX) = 0x0000; // free switcher id successfully
                   13894:                break;
1.1.1.43  root     13895:        case 0x05:
                   13896:                REG16(BX) = 0x0000; // no instance data chain
                   13897:                SREG(ES) = 0x0000;
                   13898:                i386_load_segment_descriptor(ES);
                   13899:                break;
1.1       root     13900:        default:
1.1.1.22  root     13901:                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     13902:                REG16(AX) = 0x01;
1.1.1.3   root     13903:                m_CF = 1;
1.1       root     13904:                break;
                   13905:        }
                   13906: }
                   13907: 
1.1.1.44  root     13908: inline void msdos_int_2fh_4dh()
                   13909: {
                   13910:        switch(REG8(AL)) {
                   13911:        case 0x00:
                   13912:                // KKCFUNC is not installed ???
                   13913:                break;
                   13914:        default:
                   13915: //             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));
                   13916:                REG16(AX) = 0x01; // invalid function
                   13917:                m_CF = 1;
                   13918:                break;
                   13919:        }
                   13920: }
                   13921: 
1.1       root     13922: inline void msdos_int_2fh_4fh()
                   13923: {
                   13924:        switch(REG8(AL)) {
                   13925:        case 0x00:
1.1.1.29  root     13926:                // BILING is installed
1.1.1.27  root     13927:                REG16(AX) = 0x0000;
                   13928:                REG8(DL) = 0x01;        // major version
                   13929:                REG8(DH) = 0x00;        // minor version
1.1       root     13930:                break;
                   13931:        case 0x01:
1.1.1.27  root     13932:                REG16(AX) = 0x0000;
1.1       root     13933:                REG16(BX) = active_code_page;
                   13934:                break;
                   13935:        default:
1.1.1.22  root     13936:                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));
                   13937:                REG16(AX) = 0x01;
                   13938:                m_CF = 1;
                   13939:                break;
                   13940:        }
                   13941: }
                   13942: 
                   13943: inline void msdos_int_2fh_55h()
                   13944: {
                   13945:        switch(REG8(AL)) {
                   13946:        case 0x00:
                   13947:        case 0x01:
                   13948: //             unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   13949:                break;
                   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));
1.1       root     13952:                REG16(AX) = 0x01;
1.1.1.3   root     13953:                m_CF = 1;
1.1       root     13954:                break;
                   13955:        }
                   13956: }
                   13957: 
1.1.1.44  root     13958: inline void msdos_int_2fh_56h()
                   13959: {
                   13960:        switch(REG8(AL)) {
                   13961:        case 0x00:
                   13962:                // INTERLNK is not installed
                   13963:                break;
                   13964:        case 0x01:
                   13965:                // this call is available from within SCANDISK even if INTERLNK is not installed
                   13966: //             if(msdos_is_remote_drive(REG8(BH))) {
                   13967: //                     REG8(AL) = 0x00;
                   13968: //             }
                   13969:                break;
                   13970:        default:
                   13971:                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));
                   13972:                REG16(AX) = 0x01;
                   13973:                m_CF = 1;
                   13974:                break;
                   13975:        }
                   13976: }
                   13977: 
1.1.1.24  root     13978: inline void msdos_int_2fh_adh()
                   13979: {
                   13980:        switch(REG8(AL)) {
                   13981:        case 0x00:
1.1.1.29  root     13982:                // DISPLAY.SYS is installed
1.1.1.24  root     13983:                REG8(AL) = 0xff;
                   13984:                REG16(BX) = 0x100; // ???
                   13985:                break;
                   13986:        case 0x01:
                   13987:                active_code_page = REG16(BX);
                   13988:                msdos_nls_tables_update();
                   13989:                REG16(AX) = 0x01;
                   13990:                break;
                   13991:        case 0x02:
                   13992:                REG16(BX) = active_code_page;
                   13993:                break;
                   13994:        case 0x03:
                   13995:                // FIXME
                   13996:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 1;
                   13997:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2) = 3;
                   13998:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4) = 1;
                   13999:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 6) = active_code_page;
                   14000:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 8) = active_code_page;
                   14001:                break;
                   14002:        case 0x80:
                   14003:                break; // keyb.com is not installed
                   14004:        default:
                   14005:                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));
                   14006:                REG16(AX) = 0x01;
                   14007:                m_CF = 1;
                   14008:                break;
                   14009:        }
                   14010: }
                   14011: 
1.1       root     14012: inline void msdos_int_2fh_aeh()
                   14013: {
                   14014:        switch(REG8(AL)) {
                   14015:        case 0x00:
1.1.1.28  root     14016:                // FIXME: we need to check the given command line
                   14017:                REG8(AL) = 0x00; // the command should be executed as usual
                   14018: //             REG8(AL) = 0xff; // this command is a TSR extension to COMMAND.COM
1.1       root     14019:                break;
                   14020:        case 0x01:
                   14021:                {
                   14022:                        char command[MAX_PATH];
                   14023:                        memset(command, 0, sizeof(command));
1.1.1.3   root     14024:                        memcpy(command, mem + SREG_BASE(DS) + REG16(SI) + 1, mem[SREG_BASE(DS) + REG16(SI)]);
1.1       root     14025:                        
                   14026:                        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   14027:                        param->env_seg = 0;
                   14028:                        param->cmd_line.w.l = 44;
                   14029:                        param->cmd_line.w.h = (WORK_TOP >> 4);
                   14030:                        param->fcb1.w.l = 24;
                   14031:                        param->fcb1.w.h = (WORK_TOP >> 4);
                   14032:                        param->fcb2.w.l = 24;
                   14033:                        param->fcb2.w.h = (WORK_TOP >> 4);
                   14034:                        
                   14035:                        memset(mem + WORK_TOP + 24, 0x20, 20);
                   14036:                        
                   14037:                        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
1.1.1.3   root     14038:                        cmd_line->len = mem[SREG_BASE(DS) + REG16(BX) + 1];
                   14039:                        memcpy(cmd_line->cmd, mem + SREG_BASE(DS) + REG16(BX) + 2, cmd_line->len);
1.1       root     14040:                        cmd_line->cmd[cmd_line->len] = 0x0d;
                   14041:                        
1.1.1.28  root     14042:                        try {
                   14043:                                msdos_process_exec(command, param, 0);
                   14044:                        } catch(...) {
                   14045:                                fatalerror("failed to start '%s' by int 2Fh, AX=AE01h\n", command);
1.1       root     14046:                        }
                   14047:                }
                   14048:                break;
                   14049:        default:
1.1.1.22  root     14050:                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     14051:                REG16(AX) = 0x01;
1.1.1.3   root     14052:                m_CF = 1;
1.1       root     14053:                break;
                   14054:        }
                   14055: }
                   14056: 
1.1.1.34  root     14057: inline void msdos_int_2fh_b7h()
                   14058: {
                   14059:        switch(REG8(AL)) {
                   14060:        case 0x00:
                   14061:                // APPEND is not installed
                   14062: //             REG8(AL) = 0x00;
                   14063:                break;
1.1.1.44  root     14064:        case 0x06:
                   14065:                REG16(BX) = 0x0000;
                   14066:                break;
1.1.1.34  root     14067:        case 0x07:
1.1.1.43  root     14068:        case 0x11:
1.1.1.34  root     14069:                // COMMAND.COM calls this service without checking APPEND is installed
                   14070:                break;
                   14071:        default:
                   14072:                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));
                   14073:                REG16(AX) = 0x01;
                   14074:                m_CF = 1;
                   14075:                break;
                   14076:        }
                   14077: }
                   14078: 
1.1.1.24  root     14079: inline void msdos_int_33h_0000h()
                   14080: {
                   14081:        REG16(AX) = 0xffff; // hardware/driver installed
                   14082:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   14083: }
                   14084: 
                   14085: inline void msdos_int_33h_0001h()
                   14086: {
1.1.1.34  root     14087:        if(mouse.hidden > 0) {
                   14088:                mouse.hidden--;
                   14089:        }
                   14090:        if(mouse.hidden == 0) {
1.1.1.24  root     14091:                if(!(dwConsoleMode & ENABLE_MOUSE_INPUT)) {
                   14092:                        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode | ENABLE_MOUSE_INPUT);
                   14093:                }
                   14094:                pic[1].imr &= ~0x10;    // enable irq12
                   14095:        }
                   14096: }
                   14097: 
                   14098: inline void msdos_int_33h_0002h()
                   14099: {
1.1.1.34  root     14100:        mouse.hidden++;
                   14101: //     SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode & ~ENABLE_MOUSE_INPUT);
                   14102:        pic[1].imr |= 0x10;     // enable irq12
1.1.1.24  root     14103: }
                   14104: 
                   14105: inline void msdos_int_33h_0003h()
                   14106: {
1.1.1.34  root     14107: //     if(mouse.hidden > 0) {
                   14108:                update_console_input();
                   14109: //     }
1.1.1.24  root     14110:        REG16(BX) = mouse.get_buttons();
1.1.1.34  root     14111:        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   14112:        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
                   14113: }
                   14114: 
                   14115: inline void msdos_int_33h_0004h()
                   14116: {
                   14117:        mouse.position.x = REG16(CX);
                   14118:        mouse.position.x = REG16(DX);
1.1.1.24  root     14119: }
                   14120: 
                   14121: inline void msdos_int_33h_0005h()
                   14122: {
1.1.1.34  root     14123: //     if(mouse.hidden > 0) {
                   14124:                update_console_input();
                   14125: //     }
1.1.1.24  root     14126:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   14127:                int idx = REG16(BX);
1.1.1.34  root     14128:                REG16(BX) = min(mouse.buttons[idx].pressed_times, 0x7fff);
                   14129:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].pressed_position.x));
                   14130:                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     14131:                mouse.buttons[idx].pressed_times = 0;
                   14132:        } else {
                   14133:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   14134:        }
                   14135:        REG16(AX) = mouse.get_buttons();
                   14136: }
                   14137: 
                   14138: inline void msdos_int_33h_0006h()
                   14139: {
1.1.1.34  root     14140: //     if(mouse.hidden > 0) {
                   14141:                update_console_input();
                   14142: //     }
1.1.1.24  root     14143:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   14144:                int idx = REG16(BX);
1.1.1.34  root     14145:                REG16(BX) = min(mouse.buttons[idx].released_times, 0x7fff);
                   14146:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].released_position.x));
                   14147:                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     14148:                mouse.buttons[idx].released_times = 0;
                   14149:        } else {
                   14150:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   14151:        }
                   14152:        REG16(AX) = mouse.get_buttons();
                   14153: }
                   14154: 
                   14155: inline void msdos_int_33h_0007h()
                   14156: {
                   14157:        mouse.min_position.x = min(REG16(CX), REG16(DX));
                   14158:        mouse.max_position.x = max(REG16(CX), REG16(DX));
                   14159: }
                   14160: 
                   14161: inline void msdos_int_33h_0008h()
                   14162: {
                   14163:        mouse.min_position.y = min(REG16(CX), REG16(DX));
                   14164:        mouse.max_position.y = max(REG16(CX), REG16(DX));
                   14165: }
                   14166: 
                   14167: inline void msdos_int_33h_0009h()
                   14168: {
                   14169:        mouse.hot_spot[0] = REG16(BX);
                   14170:        mouse.hot_spot[1] = REG16(CX);
                   14171: }
                   14172: 
                   14173: inline void msdos_int_33h_000bh()
                   14174: {
1.1.1.34  root     14175: //     if(mouse.hidden > 0) {
                   14176:                update_console_input();
                   14177: //     }
1.1.1.24  root     14178:        int dx = (mouse.position.x - mouse.prev_position.x) * mouse.mickey.x / 8;
                   14179:        int dy = (mouse.position.y - mouse.prev_position.y) * mouse.mickey.y / 8;
                   14180:        mouse.prev_position.x = mouse.position.x;
                   14181:        mouse.prev_position.y = mouse.position.y;
                   14182:        REG16(CX) = dx;
                   14183:        REG16(DX) = dy;
                   14184: }
                   14185: 
                   14186: inline void msdos_int_33h_000ch()
                   14187: {
                   14188:        mouse.call_mask = REG16(CX);
                   14189:        mouse.call_addr.w.l = REG16(DX);
                   14190:        mouse.call_addr.w.h = SREG(ES);
                   14191: }
                   14192: 
                   14193: inline void msdos_int_33h_000fh()
                   14194: {
                   14195:        mouse.mickey.x = REG16(CX);
                   14196:        mouse.mickey.y = REG16(DX);
                   14197: }
                   14198: 
                   14199: inline void msdos_int_33h_0011h()
                   14200: {
                   14201:        REG16(AX) = 0xffff;
                   14202:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   14203: }
                   14204: 
                   14205: inline void msdos_int_33h_0014h()
                   14206: {
                   14207:        UINT16 old_mask = mouse.call_mask;
                   14208:        UINT16 old_ofs = mouse.call_addr.w.l;
                   14209:        UINT16 old_seg = mouse.call_addr.w.h;
                   14210:        
                   14211:        mouse.call_mask = REG16(CX);
                   14212:        mouse.call_addr.w.l = REG16(DX);
                   14213:        mouse.call_addr.w.h = SREG(ES);
                   14214:        
                   14215:        REG16(CX) = old_mask;
                   14216:        REG16(DX) = old_ofs;
                   14217:        SREG(ES) = old_seg;
                   14218:        i386_load_segment_descriptor(ES);
                   14219: }
                   14220: 
                   14221: inline void msdos_int_33h_0015h()
                   14222: {
                   14223:        REG16(BX) = sizeof(mouse);
                   14224: }
                   14225: 
                   14226: inline void msdos_int_33h_0016h()
                   14227: {
                   14228:        memcpy(mem + SREG_BASE(ES) + REG16(DX), &mouse, sizeof(mouse));
                   14229: }
                   14230: 
                   14231: inline void msdos_int_33h_0017h()
                   14232: {
                   14233:        memcpy(&mouse, mem + SREG_BASE(ES) + REG16(DX), sizeof(mouse));
                   14234: }
                   14235: 
1.1.1.43  root     14236: inline void msdos_int_33h_0018h()
                   14237: {
                   14238:        for(int i = 0; i < 8; i++) {
                   14239:                if(REG16(CX) & (1 << i)) {
                   14240:                        if(mouse.call_addr_alt[i].dw && !(REG16(DX) == 0 && SREG(ES) == 0)) {
                   14241:                                // event handler already exists
                   14242:                                REG16(AX) = 0xffff;
                   14243:                                break;
                   14244:                        }
                   14245:                        mouse.call_addr_alt[i].w.l = REG16(DX);
                   14246:                        mouse.call_addr_alt[i].w.h = SREG(ES);
                   14247:                }
                   14248:        }
                   14249: }
                   14250: 
                   14251: inline void msdos_int_33h_0019h()
                   14252: {
                   14253:        UINT16 call_mask = REG16(CX);
                   14254:        
                   14255:        REG16(CX) = 0;
                   14256:        
                   14257:        for(int i = 0; i < 8; i++) {
                   14258:                if((call_mask & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   14259:                        for(int j = 0; j < 8; j++) {
                   14260:                                if((call_mask & (1 << j)) && mouse.call_addr_alt[i].dw == mouse.call_addr_alt[j].dw) {
                   14261:                                        REG16(CX) |= (1 << j);
                   14262:                                }
                   14263:                        }
                   14264:                        REG16(DX) = mouse.call_addr_alt[i].w.l;
                   14265:                        REG16(BX) = mouse.call_addr_alt[i].w.h;
                   14266:                        break;
                   14267:                }
                   14268:        }
                   14269: }
                   14270: 
1.1.1.24  root     14271: inline void msdos_int_33h_001ah()
                   14272: {
                   14273:        mouse.sensitivity[0] = REG16(BX);
                   14274:        mouse.sensitivity[1] = REG16(CX);
                   14275:        mouse.sensitivity[2] = REG16(DX);
                   14276: }
                   14277: 
                   14278: inline void msdos_int_33h_001bh()
                   14279: {
                   14280:        REG16(BX) = mouse.sensitivity[0];
                   14281:        REG16(CX) = mouse.sensitivity[1];
                   14282:        REG16(DX) = mouse.sensitivity[2];
                   14283: }
                   14284: 
                   14285: inline void msdos_int_33h_001dh()
                   14286: {
                   14287:        mouse.display_page = REG16(BX);
                   14288: }
                   14289: 
                   14290: inline void msdos_int_33h_001eh()
                   14291: {
                   14292:        REG16(BX) = mouse.display_page;
                   14293: }
                   14294: 
1.1.1.34  root     14295: inline void msdos_int_33h_001fh()
                   14296: {
                   14297:        // from DOSBox
                   14298:        REG16(BX) = 0x0000;
                   14299:        SREG(ES) = 0x0000;
                   14300:        i386_load_segment_descriptor(ES);
                   14301:        mouse.enabled = false;
                   14302:        mouse.old_hidden = mouse.hidden;
                   14303:        mouse.hidden = 1;
                   14304: }
                   14305: 
                   14306: inline void msdos_int_33h_0020h()
                   14307: {
                   14308:        // from DOSBox
                   14309:        mouse.enabled = true;
                   14310:        mouse.hidden = mouse.old_hidden;
                   14311: }
                   14312: 
1.1.1.24  root     14313: inline void msdos_int_33h_0021h()
                   14314: {
                   14315:        REG16(AX) = 0xffff;
                   14316:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   14317: }
                   14318: 
                   14319: inline void msdos_int_33h_0022h()
                   14320: {
                   14321:        mouse.language = REG16(BX);
                   14322: }
                   14323: 
                   14324: inline void msdos_int_33h_0023h()
                   14325: {
                   14326:        REG16(BX) = mouse.language;
                   14327: }
                   14328: 
                   14329: inline void msdos_int_33h_0024h()
                   14330: {
                   14331:        REG16(BX) = 0x0805; // V8.05
                   14332:        REG16(CX) = 0x0400; // PS/2
                   14333: }
                   14334: 
                   14335: inline void msdos_int_33h_0026h()
                   14336: {
                   14337:        REG16(BX) = 0x0000;
                   14338:        REG16(CX) = mouse.max_position.x;
                   14339:        REG16(DX) = mouse.max_position.y;
                   14340: }
                   14341: 
                   14342: inline void msdos_int_33h_002ah()
                   14343: {
1.1.1.34  root     14344:        REG16(AX) = -mouse.hidden;
1.1.1.24  root     14345:        REG16(BX) = mouse.hot_spot[0];
                   14346:        REG16(CX) = mouse.hot_spot[1];
                   14347:        REG16(DX) = 4; // PS/2
                   14348: }
                   14349: 
                   14350: inline void msdos_int_33h_0031h()
                   14351: {
                   14352:        REG16(AX) = mouse.min_position.x;
                   14353:        REG16(BX) = mouse.min_position.y;
                   14354:        REG16(CX) = mouse.max_position.x;
                   14355:        REG16(DX) = mouse.max_position.y;
                   14356: }
                   14357: 
                   14358: inline void msdos_int_33h_0032h()
                   14359: {
                   14360:        REG16(AX) = 0;
                   14361: //     REG16(AX) |= 0x8000; // 0025h
                   14362:        REG16(AX) |= 0x4000; // 0026h
                   14363: //     REG16(AX) |= 0x2000; // 0027h
                   14364: //     REG16(AX) |= 0x1000; // 0028h
                   14365: //     REG16(AX) |= 0x0800; // 0029h
                   14366:        REG16(AX) |= 0x0400; // 002ah
                   14367: //     REG16(AX) |= 0x0200; // 002bh
                   14368: //     REG16(AX) |= 0x0100; // 002ch
                   14369: //     REG16(AX) |= 0x0080; // 002dh
                   14370: //     REG16(AX) |= 0x0040; // 002eh
                   14371:        REG16(AX) |= 0x0020; // 002fh
                   14372: //     REG16(AX) |= 0x0010; // 0030h
                   14373:        REG16(AX) |= 0x0008; // 0031h
                   14374:        REG16(AX) |= 0x0004; // 0032h
                   14375: //     REG16(AX) |= 0x0002; // 0033h
                   14376: //     REG16(AX) |= 0x0001; // 0034h
                   14377: }
                   14378: 
1.1.1.19  root     14379: inline void msdos_int_67h_40h()
                   14380: {
                   14381:        if(!support_ems) {
                   14382:                REG8(AH) = 0x84;
                   14383:        } else {
                   14384:                REG8(AH) = 0x00;
                   14385:        }
                   14386: }
                   14387: 
                   14388: inline void msdos_int_67h_41h()
                   14389: {
                   14390:        if(!support_ems) {
                   14391:                REG8(AH) = 0x84;
                   14392:        } else {
                   14393:                REG8(AH) = 0x00;
                   14394:                REG16(BX) = EMS_TOP >> 4;
                   14395:        }
                   14396: }
                   14397: 
                   14398: inline void msdos_int_67h_42h()
                   14399: {
                   14400:        if(!support_ems) {
                   14401:                REG8(AH) = 0x84;
                   14402:        } else {
                   14403:                REG8(AH) = 0x00;
                   14404:                REG16(BX) = free_ems_pages;
                   14405:                REG16(DX) = MAX_EMS_PAGES;
                   14406:        }
                   14407: }
                   14408: 
                   14409: inline void msdos_int_67h_43h()
                   14410: {
                   14411:        if(!support_ems) {
                   14412:                REG8(AH) = 0x84;
                   14413:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   14414:                REG8(AH) = 0x87;
                   14415:        } else if(REG16(BX) > free_ems_pages) {
                   14416:                REG8(AH) = 0x88;
                   14417:        } else if(REG16(BX) == 0) {
                   14418:                REG8(AH) = 0x89;
                   14419:        } else {
1.1.1.31  root     14420:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     14421:                        if(!ems_handles[i].allocated) {
                   14422:                                ems_allocate_pages(i, REG16(BX));
                   14423:                                REG8(AH) = 0x00;
                   14424:                                REG16(DX) = i;
                   14425:                                return;
                   14426:                        }
                   14427:                }
                   14428:                REG8(AH) = 0x85;
                   14429:        }
                   14430: }
                   14431: 
                   14432: inline void msdos_int_67h_44h()
                   14433: {
                   14434:        if(!support_ems) {
                   14435:                REG8(AH) = 0x84;
1.1.1.31  root     14436:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     14437:                REG8(AH) = 0x83;
                   14438:        } else if(!(REG16(BX) == 0xffff || REG16(BX) < ems_handles[REG16(DX)].pages)) {
                   14439:                REG8(AH) = 0x8a;
                   14440: //     } else if(!(REG8(AL) < 4)) {
                   14441: //             REG8(AH) = 0x8b;
                   14442:        } else if(REG16(BX) == 0xffff) {
                   14443:                ems_unmap_page(REG8(AL) & 3);
                   14444:                REG8(AH) = 0x00;
                   14445:        } else {
                   14446:                ems_map_page(REG8(AL) & 3, REG16(DX), REG16(BX));
                   14447:                REG8(AH) = 0x00;
                   14448:        }
                   14449: }
                   14450: 
                   14451: inline void msdos_int_67h_45h()
                   14452: {
                   14453:        if(!support_ems) {
                   14454:                REG8(AH) = 0x84;
1.1.1.31  root     14455:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     14456:                REG8(AH) = 0x83;
                   14457:        } else {
                   14458:                ems_release_pages(REG16(DX));
                   14459:                REG8(AH) = 0x00;
                   14460:        }
                   14461: }
                   14462: 
                   14463: inline void msdos_int_67h_46h()
                   14464: {
                   14465:        if(!support_ems) {
                   14466:                REG8(AH) = 0x84;
                   14467:        } else {
1.1.1.29  root     14468: //             REG16(AX) = 0x0032; // EMS 3.2
                   14469:                REG16(AX) = 0x0040; // EMS 4.0
1.1.1.19  root     14470:        }
                   14471: }
                   14472: 
                   14473: inline void msdos_int_67h_47h()
                   14474: {
                   14475:        // NOTE: the map data should be stored in the specified ems page, not process data
                   14476:        process_t *process = msdos_process_info_get(current_psp);
                   14477:        
                   14478:        if(!support_ems) {
                   14479:                REG8(AH) = 0x84;
1.1.1.31  root     14480: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     14481: //             REG8(AH) = 0x83;
                   14482:        } else if(process->ems_pages_stored) {
                   14483:                REG8(AH) = 0x8d;
                   14484:        } else {
                   14485:                for(int i = 0; i < 4; i++) {
                   14486:                        process->ems_pages[i].handle = ems_pages[i].handle;
                   14487:                        process->ems_pages[i].page   = ems_pages[i].page;
                   14488:                        process->ems_pages[i].mapped = ems_pages[i].mapped;
                   14489:                }
                   14490:                process->ems_pages_stored = true;
                   14491:                REG8(AH) = 0x00;
                   14492:        }
                   14493: }
                   14494: 
                   14495: inline void msdos_int_67h_48h()
                   14496: {
                   14497:        // NOTE: the map data should be restored from the specified ems page, not process data
                   14498:        process_t *process = msdos_process_info_get(current_psp);
                   14499:        
                   14500:        if(!support_ems) {
                   14501:                REG8(AH) = 0x84;
1.1.1.31  root     14502: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     14503: //             REG8(AH) = 0x83;
                   14504:        } else if(!process->ems_pages_stored) {
                   14505:                REG8(AH) = 0x8e;
                   14506:        } else {
                   14507:                for(int i = 0; i < 4; i++) {
                   14508:                        if(process->ems_pages[i].mapped) {
                   14509:                                ems_map_page(i, process->ems_pages[i].handle, process->ems_pages[i].page);
                   14510:                        } else {
                   14511:                                ems_unmap_page(i);
                   14512:                        }
                   14513:                }
                   14514:                process->ems_pages_stored = false;
                   14515:                REG8(AH) = 0x00;
                   14516:        }
                   14517: }
                   14518: 
                   14519: inline void msdos_int_67h_4bh()
                   14520: {
                   14521:        if(!support_ems) {
                   14522:                REG8(AH) = 0x84;
                   14523:        } else {
                   14524:                REG8(AH) = 0x00;
                   14525:                REG16(BX) = 0;
1.1.1.31  root     14526:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     14527:                        if(ems_handles[i].allocated) {
                   14528:                                REG16(BX)++;
                   14529:                        }
                   14530:                }
                   14531:        }
                   14532: }
                   14533: 
                   14534: inline void msdos_int_67h_4ch()
                   14535: {
                   14536:        if(!support_ems) {
                   14537:                REG8(AH) = 0x84;
1.1.1.31  root     14538:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     14539:                REG8(AH) = 0x83;
                   14540:        } else {
                   14541:                REG8(AH) = 0x00;
                   14542:                REG16(BX) = ems_handles[REG16(DX)].pages;
                   14543:        }
                   14544: }
                   14545: 
                   14546: inline void msdos_int_67h_4dh()
                   14547: {
                   14548:        if(!support_ems) {
                   14549:                REG8(AH) = 0x84;
                   14550:        } else {
                   14551:                REG8(AH) = 0x00;
                   14552:                REG16(BX) = 0;
1.1.1.31  root     14553:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     14554:                        if(ems_handles[i].allocated) {
                   14555:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 0) = i;
                   14556:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 2) = ems_handles[i].pages;
                   14557:                                REG16(BX)++;
                   14558:                        }
                   14559:                }
                   14560:        }
                   14561: }
                   14562: 
1.1.1.20  root     14563: inline void msdos_int_67h_4eh()
                   14564: {
                   14565:        if(!support_ems) {
                   14566:                REG8(AH) = 0x84;
                   14567:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   14568:                if(REG8(AL) == 0x00 || REG8(AL) == 0x02) {
                   14569:                        // save page map
                   14570:                        for(int i = 0; i < 4; i++) {
                   14571:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   14572:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   14573:                        }
                   14574:                }
                   14575:                if(REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   14576:                        // restore page map
                   14577:                        for(int i = 0; i < 4; i++) {
                   14578:                                UINT16 handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   14579:                                UINT16 page   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   14580:                                
1.1.1.31  root     14581:                                if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
1.1.1.20  root     14582:                                        ems_map_page(i, handle, page);
                   14583:                                } else {
                   14584:                                        ems_unmap_page(i);
                   14585:                                }
                   14586:                        }
                   14587:                }
                   14588:                REG8(AH) = 0x00;
                   14589:        } else if(REG8(AL) == 0x03) {
                   14590:                REG8(AH) = 0x00;
1.1.1.21  root     14591:                REG8(AL) = 4 * 4;
                   14592:        } else {
1.1.1.22  root     14593:                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     14594:                REG8(AH) = 0x8f;
                   14595:        }
                   14596: }
                   14597: 
                   14598: inline void msdos_int_67h_4fh()
                   14599: {
                   14600:        if(!support_ems) {
                   14601:                REG8(AH) = 0x84;
                   14602:        } else if(REG8(AL) == 0x00) {
                   14603:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   14604:                
                   14605:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI)) = count;
                   14606:                for(int i = 0; i < count; i++) {
                   14607:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 2 * i);
                   14608:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   14609:                        
                   14610: //                     if(!(physical < 4)) {
                   14611: //                             REG8(AH) = 0x8b;
                   14612: //                             return;
                   14613: //                     }
                   14614:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 0) = segment;
1.1.1.41  root     14615:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 2) = ems_pages[physical & 3].mapped ? ems_pages[physical & 3].handle : 0xffff;
                   14616:                        *(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     14617:                }
                   14618:                REG8(AH) = 0x00;
                   14619:        } else if(REG8(AL) == 0x01) {
                   14620:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   14621:                
                   14622:                for(int i = 0; i < count; i++) {
                   14623:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 0);
                   14624:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   14625:                        UINT16 handle   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 2);
                   14626:                        UINT16 logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 4);
                   14627:                        
                   14628: //                     if(!(physical < 4)) {
                   14629: //                             REG8(AH) = 0x8b;
                   14630: //                             return;
                   14631: //                     } else
1.1.1.41  root     14632:                        if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && logical < ems_handles[handle].pages) {
1.1.1.21  root     14633:                                ems_map_page(physical & 3, handle, logical);
                   14634:                        } else {
1.1.1.41  root     14635:                                ems_unmap_page(physical & 3);
1.1.1.21  root     14636:                        }
                   14637:                }
                   14638:                REG8(AH) = 0x00;
                   14639:        } else if(REG8(AL) == 0x02) {
                   14640:                REG8(AH) = 0x00;
                   14641:                REG8(AL) = 2 + REG16(BX) * 6;
1.1.1.20  root     14642:        } else {
1.1.1.22  root     14643:                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     14644:                REG8(AH) = 0x8f;
                   14645:        }
                   14646: }
                   14647: 
                   14648: inline void msdos_int_67h_50h()
                   14649: {
                   14650:        if(!support_ems) {
                   14651:                REG8(AH) = 0x84;
1.1.1.31  root     14652:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.20  root     14653:                REG8(AH) = 0x83;
                   14654:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   14655:                for(int i = 0; i < REG16(CX); i++) {
                   14656:                        int logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   14657:                        int physical = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   14658:                        
                   14659:                        if(REG8(AL) == 0x01) {
                   14660:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   14661:                        }
                   14662: //                     if(!(physical < 4)) {
                   14663: //                             REG8(AH) = 0x8b;
                   14664: //                             return;
                   14665: //                     } else
                   14666:                        if(logical == 0xffff) {
                   14667:                                ems_unmap_page(physical & 3);
                   14668:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   14669:                                ems_map_page(physical & 3, REG16(DX), logical);
                   14670:                        } else {
                   14671:                                REG8(AH) = 0x8a;
                   14672:                                return;
                   14673:                        }
                   14674:                }
                   14675:                REG8(AH) = 0x00;
                   14676:        } else {
1.1.1.22  root     14677:                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     14678:                REG8(AH) = 0x8f;
                   14679:        }
                   14680: }
                   14681: 
1.1.1.19  root     14682: inline void msdos_int_67h_51h()
                   14683: {
                   14684:        if(!support_ems) {
                   14685:                REG8(AH) = 0x84;
1.1.1.31  root     14686:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     14687:                REG8(AH) = 0x83;
                   14688:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   14689:                REG8(AH) = 0x87;
                   14690:        } else if(REG16(BX) > free_ems_pages + ems_handles[REG16(DX)].pages) {
                   14691:                REG8(AH) = 0x88;
                   14692:        } else {
                   14693:                ems_reallocate_pages(REG16(DX), REG16(BX));
                   14694:                REG8(AH) = 0x00;
                   14695:        }
                   14696: }
                   14697: 
1.1.1.20  root     14698: inline void msdos_int_67h_52h()
                   14699: {
                   14700:        if(!support_ems) {
                   14701:                REG8(AH) = 0x84;
1.1.1.31  root     14702: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   14703: //             REG8(AH) = 0x83;
1.1.1.20  root     14704:        } else if(REG8(AL) == 0x00) {
                   14705:                REG8(AL) = 0x00; // handle is volatile
                   14706:                REG8(AH) = 0x00;
                   14707:        } else if(REG8(AL) == 0x01) {
                   14708:                if(REG8(BL) == 0x00) {
                   14709:                        REG8(AH) = 0x00;
                   14710:                } else {
                   14711:                        REG8(AH) = 0x90; // undefined attribute type
                   14712:                }
                   14713:        } else if(REG8(AL) == 0x02) {
                   14714:                REG8(AL) = 0x00; // only volatile handles supported
                   14715:                REG8(AH) = 0x00;
                   14716:        } else {
1.1.1.22  root     14717:                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     14718:                REG8(AH) = 0x8f;
                   14719:        }
                   14720: }
                   14721: 
1.1.1.19  root     14722: inline void msdos_int_67h_53h()
                   14723: {
                   14724:        if(!support_ems) {
                   14725:                REG8(AH) = 0x84;
1.1.1.31  root     14726:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     14727:                REG8(AH) = 0x83;
                   14728:        } else if(REG8(AL) == 0x00) {
                   14729:                memcpy(mem + SREG_BASE(ES) + REG16(DI), ems_handles[REG16(DX)].name, 8);
                   14730:                REG8(AH) = 0x00;
                   14731:        } else if(REG8(AL) == 0x01) {
1.1.1.31  root     14732:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     14733:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   14734:                                REG8(AH) = 0xa1;
                   14735:                                return;
                   14736:                        }
                   14737:                }
                   14738:                REG8(AH) = 0x00;
                   14739:                memcpy(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8);
                   14740:        } else {
1.1.1.22  root     14741:                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     14742:                REG8(AH) = 0x8f;
1.1.1.19  root     14743:        }
                   14744: }
                   14745: 
                   14746: inline void msdos_int_67h_54h()
                   14747: {
                   14748:        if(!support_ems) {
                   14749:                REG8(AH) = 0x84;
                   14750:        } else if(REG8(AL) == 0x00) {
1.1.1.31  root     14751:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     14752:                        if(ems_handles[i].allocated) {
                   14753:                                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, ems_handles[i].name, 10);
                   14754:                        } else {
                   14755:                                memset(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, 0, 10);
                   14756:                        }
                   14757:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 0) = i;
                   14758:                }
                   14759:                REG8(AH) = 0x00;
                   14760:                REG8(AL) = MAX_EMS_HANDLES;
                   14761:        } else if(REG8(AL) == 0x01) {
                   14762:                REG8(AH) = 0xa0; // not found
1.1.1.31  root     14763:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     14764:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   14765:                                REG8(AH) = 0x00;
                   14766:                                REG16(DX) = i;
                   14767:                                break;
                   14768:                        }
                   14769:                }
                   14770:        } else if(REG8(AL) == 0x02) {
                   14771:                REG8(AH) = 0x00;
                   14772:                REG16(BX) = MAX_EMS_HANDLES;
                   14773:        } else {
1.1.1.22  root     14774:                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     14775:                REG8(AH) = 0x8f;
                   14776:        }
                   14777: }
                   14778: 
                   14779: inline void msdos_int_67h_57h_tmp()
                   14780: {
                   14781:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   14782:        UINT8  src_type    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   14783:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x05);
                   14784:        UINT16 src_ofs     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07);
                   14785:        UINT16 src_seg     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x09);
                   14786:        UINT8  dest_type   = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0b);
                   14787:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   14788:        UINT16 dest_ofs    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0e);
                   14789:        UINT16 dest_seg    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x10);
                   14790:        
1.1.1.32  root     14791:        UINT8 *src_buffer = NULL, *dest_buffer = NULL;
1.1.1.20  root     14792:        UINT32 src_addr, dest_addr;
                   14793:        UINT32 src_addr_max, dest_addr_max;
                   14794:        
                   14795:        if(src_type == 0) {
                   14796:                src_buffer = mem;
                   14797:                src_addr = (src_seg << 4) + src_ofs;
                   14798:                src_addr_max = MAX_MEM;
                   14799:        } else {
1.1.1.31  root     14800:                if(!(src_handle >= 1 && src_handle <= MAX_EMS_HANDLES && ems_handles[src_handle].allocated)) {
1.1.1.20  root     14801:                        REG8(AH) = 0x83;
                   14802:                        return;
                   14803:                } else if(!(src_seg < ems_handles[src_handle].pages)) {
                   14804:                        REG8(AH) = 0x8a;
                   14805:                        return;
                   14806:                }
1.1.1.32  root     14807:                if(ems_handles[src_handle].buffer != NULL) {
                   14808:                        src_buffer = ems_handles[src_handle].buffer + 0x4000 * src_seg;
                   14809:                }
1.1.1.20  root     14810:                src_addr = src_ofs;
1.1.1.32  root     14811:                src_addr_max = 0x4000 * (ems_handles[src_handle].pages - src_seg);
1.1.1.20  root     14812:        }
                   14813:        if(dest_type == 0) {
                   14814:                dest_buffer = mem;
                   14815:                dest_addr = (dest_seg << 4) + dest_ofs;
                   14816:                dest_addr_max = MAX_MEM;
                   14817:        } else {
1.1.1.31  root     14818:                if(!(dest_handle >= 1 && dest_handle <= MAX_EMS_HANDLES && ems_handles[dest_handle].allocated)) {
1.1.1.20  root     14819:                        REG8(AH) = 0x83;
                   14820:                        return;
                   14821:                } else if(!(dest_seg < ems_handles[dest_handle].pages)) {
                   14822:                        REG8(AH) = 0x8a;
                   14823:                        return;
                   14824:                }
1.1.1.32  root     14825:                if(ems_handles[dest_handle].buffer != NULL) {
                   14826:                        dest_buffer = ems_handles[dest_handle].buffer + 0x4000 * dest_seg;
                   14827:                }
1.1.1.20  root     14828:                dest_addr = dest_ofs;
1.1.1.32  root     14829:                dest_addr_max = 0x4000 * (ems_handles[dest_handle].pages - dest_seg);
1.1.1.20  root     14830:        }
1.1.1.32  root     14831:        if(src_buffer != NULL && dest_buffer != NULL) {
                   14832:                for(int i = 0; i < copy_length; i++) {
                   14833:                        if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   14834:                                if(REG8(AL) == 0x00) {
                   14835:                                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   14836:                                } else if(REG8(AL) == 0x01) {
                   14837:                                        UINT8 tmp = dest_buffer[dest_addr];
                   14838:                                        dest_buffer[dest_addr++] = src_buffer[src_addr];
                   14839:                                        src_buffer[src_addr++] = tmp;
                   14840:                                }
                   14841:                        } else {
                   14842:                                REG8(AH) = 0x93;
                   14843:                                return;
1.1.1.20  root     14844:                        }
                   14845:                }
1.1.1.32  root     14846:                REG8(AH) = 0x00;
                   14847:        } else {
                   14848:                REG8(AH) = 0x80;
1.1.1.20  root     14849:        }
                   14850: }
                   14851: 
                   14852: inline void msdos_int_67h_57h()
                   14853: {
                   14854:        if(!support_ems) {
                   14855:                REG8(AH) = 0x84;
                   14856:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   14857:                struct {
                   14858:                        UINT16 handle;
                   14859:                        UINT16 page;
                   14860:                        bool mapped;
                   14861:                } tmp_pages[4];
                   14862:                
                   14863:                // unmap pages to copy memory data to ems buffer
                   14864:                for(int i = 0; i < 4; i++) {
                   14865:                        tmp_pages[i].handle = ems_pages[i].handle;
                   14866:                        tmp_pages[i].page   = ems_pages[i].page;
                   14867:                        tmp_pages[i].mapped = ems_pages[i].mapped;
                   14868:                        ems_unmap_page(i);
                   14869:                }
                   14870:                
                   14871:                // run move/exchange operation
                   14872:                msdos_int_67h_57h_tmp();
                   14873:                
                   14874:                // restore unmapped pages
                   14875:                for(int i = 0; i < 4; i++) {
                   14876:                        if(tmp_pages[i].mapped) {
                   14877:                                ems_map_page(i, tmp_pages[i].handle, tmp_pages[i].page);
                   14878:                        }
                   14879:                }
                   14880:        } else {
1.1.1.22  root     14881:                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     14882:                REG8(AH) = 0x8f;
                   14883:        }
                   14884: }
                   14885: 
                   14886: inline void msdos_int_67h_58h()
                   14887: {
                   14888:        if(!support_ems) {
                   14889:                REG8(AH) = 0x84;
                   14890:        } else if(REG8(AL) == 0x00) {
                   14891:                for(int i = 0; i < 4; i++) {
1.1.1.30  root     14892:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = (EMS_TOP + 0x4000 * i) >> 4;
                   14893:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = i;
1.1.1.20  root     14894:                }
                   14895:                REG8(AH) = 0x00;
                   14896:                REG16(CX) = 4;
                   14897:        } else if(REG8(AL) == 0x01) {
                   14898:                REG8(AH) = 0x00;
                   14899:                REG16(CX) = 4;
                   14900:        } else {
1.1.1.22  root     14901:                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     14902:                REG8(AH) = 0x8f;
                   14903:        }
                   14904: }
                   14905: 
1.1.1.42  root     14906: inline void msdos_int_67h_59h()
                   14907: {
                   14908:        if(!support_ems) {
                   14909:                REG8(AH) = 0x84;
                   14910:        } else if(REG8(AL) == 0x00) {
                   14911:                REG8(AH) = 0xa4; // access denied by operating system
                   14912:        } else if(REG8(AL) == 0x01) {
                   14913:                REG8(AH) = 0x00;
                   14914:                REG16(BX) = free_ems_pages;
                   14915:                REG16(DX) = MAX_EMS_PAGES;
                   14916:        } else {
                   14917:                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));
                   14918:                REG8(AH) = 0x8f;
                   14919:        }
                   14920: }
                   14921: 
1.1.1.20  root     14922: inline void msdos_int_67h_5ah()
                   14923: {
                   14924:        if(!support_ems) {
1.1.1.19  root     14925:                REG8(AH) = 0x84;
1.1.1.20  root     14926:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   14927:                REG8(AH) = 0x87;
                   14928:        } else if(REG16(BX) > free_ems_pages) {
                   14929:                REG8(AH) = 0x88;
                   14930: //     } else if(REG16(BX) == 0) {
                   14931: //             REG8(AH) = 0x89;
                   14932:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
1.1.1.31  root     14933:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.20  root     14934:                        if(!ems_handles[i].allocated) {
                   14935:                                ems_allocate_pages(i, REG16(BX));
                   14936:                                REG8(AH) = 0x00;
                   14937:                                REG16(DX) = i;
                   14938:                                return;
                   14939:                        }
                   14940:                }
                   14941:                REG8(AH) = 0x85;
                   14942:        } else {
1.1.1.22  root     14943:                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     14944:                REG8(AH) = 0x8f;
1.1.1.19  root     14945:        }
                   14946: }
                   14947: 
1.1.1.43  root     14948: inline void msdos_int_67h_5dh()
                   14949: {
                   14950:        if(!support_ems) {
                   14951:                REG8(AH) = 0x84;
                   14952:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   14953:                REG8(AH) = 0xa4; // operating system denied access
                   14954:        } else {
                   14955:                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));
                   14956:                REG8(AH) = 0x8f;
                   14957:        }
                   14958: }
                   14959: 
1.1.1.30  root     14960: inline void msdos_int_67h_deh()
                   14961: {
                   14962:        REG8(AH) = 0x84;
                   14963: }
                   14964: 
1.1.1.19  root     14965: #ifdef SUPPORT_XMS
                   14966: 
1.1.1.32  root     14967: void msdos_xms_init()
1.1.1.26  root     14968: {
1.1.1.30  root     14969:        emb_handle_top = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   14970:        emb_handle_top->address = EMB_TOP;
                   14971:        emb_handle_top->size_kb = (EMB_END - EMB_TOP) >> 10;
1.1.1.26  root     14972:        xms_a20_local_enb_count = 0;
                   14973: }
                   14974: 
1.1.1.32  root     14975: void msdos_xms_finish()
                   14976: {
                   14977:        msdos_xms_release();
                   14978: }
                   14979: 
                   14980: void msdos_xms_release()
1.1.1.30  root     14981: {
                   14982:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL;) {
                   14983:                emb_handle_t *next_handle = emb_handle->next;
                   14984:                free(emb_handle);
                   14985:                emb_handle = next_handle;
                   14986:        }
                   14987: }
                   14988: 
                   14989: emb_handle_t *msdos_xms_get_emb_handle(int handle)
                   14990: {
                   14991:        if(handle != 0) {
                   14992:                for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   14993:                        if(emb_handle->handle == handle) {
                   14994:                                return(emb_handle);
                   14995:                        }
                   14996:                }
                   14997:        }
                   14998:        return(NULL);
                   14999: }
                   15000: 
                   15001: int msdos_xms_get_unused_emb_handle_id()
                   15002: {
                   15003:        for(int handle = 1;; handle++) {
                   15004:                if(msdos_xms_get_emb_handle(handle) == NULL) {
                   15005:                        return(handle);
                   15006:                }
                   15007:        }
                   15008:        return(0);
                   15009: }
                   15010: 
                   15011: int msdos_xms_get_unused_emb_handle_count()
                   15012: {
                   15013:        int count = 64; //255;
                   15014:        
                   15015:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   15016:                if(emb_handle->handle != 0) {
                   15017:                        if(--count == 1) {
                   15018:                                break;
                   15019:                        }
                   15020:                }
                   15021:        }
                   15022:        return(count);
                   15023: }
                   15024: 
                   15025: void msdos_xms_split_emb_handle(emb_handle_t *emb_handle, int size_kb)
                   15026: {
                   15027:        if(emb_handle->size_kb > size_kb) {
                   15028:                emb_handle_t *new_handle = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   15029:                
                   15030:                new_handle->address = emb_handle->address + size_kb * 1024;
                   15031:                new_handle->size_kb = emb_handle->size_kb - size_kb;
                   15032:                emb_handle->size_kb = size_kb;
                   15033:                
                   15034:                new_handle->prev = emb_handle;
                   15035:                new_handle->next = emb_handle->next;
                   15036:                if(emb_handle->next != NULL) {
                   15037:                        emb_handle->next->prev = new_handle;
                   15038:                }
                   15039:                emb_handle->next = new_handle;
                   15040:        }
                   15041: }
                   15042: 
                   15043: void msdos_xms_combine_emb_handles(emb_handle_t *emb_handle)
                   15044: {
                   15045:        emb_handle_t *next_handle = emb_handle->next;
                   15046:        
                   15047:        if(next_handle != NULL) {
                   15048:                emb_handle->size_kb += next_handle->size_kb;
                   15049:                
                   15050:                if(next_handle->next != NULL) {
                   15051:                        next_handle->next->prev = emb_handle;
                   15052:                }
                   15053:                emb_handle->next = next_handle->next;
                   15054:                free(next_handle);
                   15055:        }
                   15056: }
                   15057: 
                   15058: emb_handle_t *msdos_xms_alloc_emb_handle(int size_kb)
                   15059: {
                   15060:        emb_handle_t *target_handle = NULL;
                   15061:        
                   15062:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   15063:                if(emb_handle->handle == 0 && emb_handle->size_kb >= size_kb) {
                   15064:                        if(target_handle == NULL || target_handle->size_kb > emb_handle->size_kb) {
                   15065:                                target_handle = emb_handle;
                   15066:                        }
                   15067:                }
                   15068:        }
                   15069:        if(target_handle != NULL) {
                   15070:                if(target_handle->size_kb > size_kb) {
                   15071:                        msdos_xms_split_emb_handle(target_handle, size_kb);
                   15072:                }
                   15073: //             target_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   15074:                return(target_handle);
                   15075:        }
                   15076:        return(NULL);
                   15077: }
                   15078: 
                   15079: void msdos_xms_free_emb_handle(emb_handle_t *emb_handle)
                   15080: {
                   15081:        emb_handle_t *prev_handle = emb_handle->prev;
                   15082:        emb_handle_t *next_handle = emb_handle->next;
                   15083:        
                   15084:        if(prev_handle != NULL && prev_handle->handle == 0) {
                   15085:                msdos_xms_combine_emb_handles(prev_handle);
                   15086:                emb_handle = prev_handle;
                   15087:        }
                   15088:        if(next_handle != NULL && next_handle->handle == 0) {
                   15089:                msdos_xms_combine_emb_handles(emb_handle);
                   15090:        }
                   15091:        emb_handle->handle = 0;
                   15092: }
                   15093: 
1.1.1.19  root     15094: inline void msdos_call_xms_00h()
                   15095: {
1.1.1.29  root     15096: #if defined(HAS_I386)
                   15097:        REG16(AX) = 0x0300; // V3.00 (XMS Version)
1.1.1.45  root     15098: //     REG16(BX) = 0x0395; // V3.95 (Driver Revision in BCD)
1.1.1.29  root     15099:        REG16(BX) = 0x035f; // V3.95 (Driver Revision)
                   15100: #else
                   15101:        REG16(AX) = 0x0200; // V2.00 (XMS Version)
                   15102:        REG16(BX) = 0x0270; // V2.70 (Driver Revision)
                   15103: #endif
                   15104: //     REG16(DX) = 0x0000; // HMA does not exist
                   15105:        REG16(DX) = 0x0001; // HMA does exist
1.1.1.19  root     15106: }
                   15107: 
                   15108: inline void msdos_call_xms_01h()
                   15109: {
1.1.1.29  root     15110:        if(REG8(AL) == 0x40) {
                   15111:                // HIMEM.SYS will fail function 01h with error code 91h if AL=40h and
                   15112:                // DX=KB free extended memory returned by last call of function 08h
                   15113:                REG16(AX) = 0x0000;
                   15114:                REG8(BL) = 0x91;
                   15115:                REG16(DX) = xms_dx_after_call_08h;
                   15116:        } else if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   15117:                REG16(AX) = 0x0000;
                   15118:                REG8(BL) = 0x81; // Vdisk was detected
                   15119: #ifdef SUPPORT_HMA
                   15120:        } else if(is_hma_used_by_int_2fh) {
                   15121:                REG16(AX) = 0x0000;
                   15122:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   15123:        } else if(is_hma_used_by_xms) {
                   15124:                REG16(AX) = 0x0000;
                   15125:                REG8(BL) = 0x91; // HMA is already in use
                   15126:        } else {
                   15127:                REG16(AX) = 0x0001;
                   15128:                is_hma_used_by_xms = true;
                   15129: #else
                   15130:        } else {
                   15131:                REG16(AX) = 0x0000;
                   15132:                REG8(BL) = 0x91; // HMA is already in use
                   15133: #endif
                   15134:        }
1.1.1.19  root     15135: }
                   15136: 
                   15137: inline void msdos_call_xms_02h()
                   15138: {
1.1.1.29  root     15139:        if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   15140:                REG16(AX) = 0x0000;
                   15141:                REG8(BL) = 0x81; // Vdisk was detected
                   15142: #ifdef SUPPORT_HMA
                   15143:        } else if(is_hma_used_by_int_2fh) {
                   15144:                REG16(AX) = 0x0000;
                   15145:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   15146:        } else if(!is_hma_used_by_xms) {
                   15147:                REG16(AX) = 0x0000;
                   15148:                REG8(BL) = 0x93; // HMA is not allocated
                   15149:        } else {
                   15150:                REG16(AX) = 0x0001;
                   15151:                is_hma_used_by_xms = false;
                   15152:                // restore first free mcb in high memory area
                   15153:                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   15154: #else
                   15155:        } else {
                   15156:                REG16(AX) = 0x0000;
                   15157:                REG8(BL) = 0x91; // HMA is already in use
                   15158: #endif
                   15159:        }
1.1.1.19  root     15160: }
                   15161: 
                   15162: inline void msdos_call_xms_03h()
                   15163: {
                   15164:        i386_set_a20_line(1);
                   15165:        REG16(AX) = 0x0001;
                   15166:        REG8(BL) = 0x00;
                   15167: }
                   15168: 
                   15169: inline void msdos_call_xms_04h()
                   15170: {
1.1.1.21  root     15171:        i386_set_a20_line(0);
                   15172:        REG16(AX) = 0x0001;
                   15173:        REG8(BL) = 0x00;
1.1.1.19  root     15174: }
                   15175: 
                   15176: inline void msdos_call_xms_05h()
                   15177: {
                   15178:        i386_set_a20_line(1);
                   15179:        REG16(AX) = 0x0001;
                   15180:        REG8(BL) = 0x00;
1.1.1.21  root     15181:        xms_a20_local_enb_count++;
1.1.1.19  root     15182: }
                   15183: 
                   15184: void msdos_call_xms_06h()
                   15185: {
1.1.1.21  root     15186:        if(xms_a20_local_enb_count > 0) {
1.1.1.45  root     15187:                if(--xms_a20_local_enb_count == 0) {
                   15188:                        i386_set_a20_line(0);
                   15189:                        REG16(AX) = 0x0001;
                   15190:                        REG8(BL) = 0x00;
                   15191:                } else {
                   15192:                        REG16(AX) = 0x0000;
                   15193:                        REG8(BL) = 0x94;
                   15194:                }
1.1.1.21  root     15195:        } else {
1.1.1.45  root     15196:                i386_set_a20_line(0);
1.1.1.21  root     15197:                REG16(AX) = 0x0001;
                   15198:                REG8(BL) = 0x00;
1.1.1.19  root     15199:        }
                   15200: }
                   15201: 
                   15202: inline void msdos_call_xms_07h()
                   15203: {
                   15204:        REG16(AX) = (m_a20_mask >> 20) & 1;
                   15205:        REG8(BL) = 0x00;
                   15206: }
                   15207: 
                   15208: inline void msdos_call_xms_08h()
                   15209: {
1.1.1.45  root     15210:        UINT32 eax = 0, edx = 0;
1.1.1.19  root     15211:        
1.1.1.30  root     15212:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   15213:                if(emb_handle->handle == 0) {
1.1.1.45  root     15214:                        if(eax < emb_handle->size_kb) {
                   15215:                                eax = emb_handle->size_kb;
1.1.1.19  root     15216:                        }
1.1.1.45  root     15217:                        edx += emb_handle->size_kb;
1.1.1.19  root     15218:                }
                   15219:        }
1.1.1.45  root     15220:        if(eax > 65535) {
                   15221:                eax = 65535;
                   15222:        }
                   15223:        if(edx > 65535) {
                   15224:                edx = 65535;
                   15225:        }
                   15226:        if(eax == 0 && edx == 0) {
1.1.1.19  root     15227:                REG8(BL) = 0xa0;
                   15228:        } else {
                   15229:                REG8(BL) = 0x00;
                   15230:        }
1.1.1.45  root     15231: #if defined(HAS_I386)
                   15232:        REG32(EAX) = eax;
                   15233:        REG32(EDX) = edx;
                   15234: #else
                   15235:        REG16(AX) = (UINT16)eax;
                   15236:        REG16(DX) = (UINT16)edx;
                   15237: #endif
1.1.1.29  root     15238:        xms_dx_after_call_08h = REG16(DX);
1.1.1.19  root     15239: }
                   15240: 
1.1.1.30  root     15241: void msdos_call_xms_09h(int size_kb)
1.1.1.19  root     15242: {
1.1.1.30  root     15243:        emb_handle_t *emb_handle = msdos_xms_alloc_emb_handle(size_kb);
                   15244:        
                   15245:        if(emb_handle != NULL) {
                   15246:                emb_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   15247:                
                   15248:                REG16(AX) = 0x0001;
                   15249:                REG16(DX) = emb_handle->handle;
                   15250:                REG8(BL) = 0x00;
                   15251:        } else {
                   15252:                REG16(AX) = REG16(DX) = 0x0000;
                   15253:                REG8(BL) = 0xa0;
1.1.1.19  root     15254:        }
1.1.1.30  root     15255: }
                   15256: 
                   15257: inline void msdos_call_xms_09h()
                   15258: {
                   15259:        msdos_call_xms_09h(REG16(DX));
1.1.1.19  root     15260: }
                   15261: 
                   15262: inline void msdos_call_xms_0ah()
                   15263: {
1.1.1.30  root     15264:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   15265:        
                   15266:        if(emb_handle == NULL) {
1.1.1.19  root     15267:                REG16(AX) = 0x0000;
                   15268:                REG8(BL) = 0xa2;
1.1.1.45  root     15269: //     } else if(emb_handle->lock > 0) {
                   15270: //             REG16(AX) = 0x0000;
                   15271: //             REG8(BL) = 0xab;
1.1.1.19  root     15272:        } else {
1.1.1.30  root     15273:                msdos_xms_free_emb_handle(emb_handle);
1.1.1.19  root     15274:                
                   15275:                REG16(AX) = 0x0001;
                   15276:                REG8(BL) = 0x00;
                   15277:        }
                   15278: }
                   15279: 
                   15280: inline void msdos_call_xms_0bh()
                   15281: {
                   15282:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   15283:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   15284:        UINT32 src_addr    = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06);
                   15285:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0a);
                   15286:        UINT32 dest_addr   = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   15287:        
                   15288:        UINT8 *src_buffer, *dest_buffer;
                   15289:        UINT32 src_addr_max, dest_addr_max;
1.1.1.30  root     15290:        emb_handle_t *emb_handle;
1.1.1.19  root     15291:        
                   15292:        if(src_handle == 0) {
                   15293:                src_buffer = mem;
                   15294:                src_addr = (((src_addr >> 16) & 0xffff) << 4) + (src_addr & 0xffff);
                   15295:                src_addr_max = MAX_MEM;
                   15296:        } else {
1.1.1.30  root     15297:                if((emb_handle = msdos_xms_get_emb_handle(src_handle)) == NULL) {
1.1.1.19  root     15298:                        REG16(AX) = 0x0000;
                   15299:                        REG8(BL) = 0xa3;
                   15300:                        return;
                   15301:                }
1.1.1.30  root     15302:                src_buffer = mem + emb_handle->address;
                   15303:                src_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     15304:        }
                   15305:        if(dest_handle == 0) {
                   15306:                dest_buffer = mem;
                   15307:                dest_addr = (((dest_addr >> 16) & 0xffff) << 4) + (dest_addr & 0xffff);
                   15308:                dest_addr_max = MAX_MEM;
                   15309:        } else {
1.1.1.30  root     15310:                if((emb_handle = msdos_xms_get_emb_handle(dest_handle)) == NULL) {
1.1.1.19  root     15311:                        REG16(AX) = 0x0000;
                   15312:                        REG8(BL) = 0xa5;
                   15313:                        return;
                   15314:                }
1.1.1.30  root     15315:                dest_buffer = mem + emb_handle->address;
                   15316:                dest_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     15317:        }
                   15318:        for(int i = 0; i < copy_length; i++) {
                   15319:                if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   15320:                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   15321:                } else {
                   15322:                        break;
                   15323:                }
                   15324:        }
                   15325:        REG16(AX) = 0x0001;
                   15326:        REG8(BL) = 0x00;
                   15327: }
                   15328: 
                   15329: inline void msdos_call_xms_0ch()
                   15330: {
1.1.1.30  root     15331:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   15332:        
                   15333:        if(emb_handle == NULL) {
1.1.1.19  root     15334:                REG16(AX) = 0x0000;
                   15335:                REG8(BL) = 0xa2;
                   15336:        } else {
1.1.1.45  root     15337:                if(emb_handle->lock < 255) {
                   15338:                        emb_handle->lock++;
                   15339:                }
1.1.1.19  root     15340:                REG16(AX) = 0x0001;
1.1.1.30  root     15341:                REG16(DX) = (emb_handle->address >> 16) & 0xffff;
                   15342:                REG16(BX) = (emb_handle->address      ) & 0xffff;
1.1.1.19  root     15343:        }
                   15344: }
                   15345: 
                   15346: inline void msdos_call_xms_0dh()
                   15347: {
1.1.1.30  root     15348:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   15349:        
                   15350:        if(emb_handle == NULL) {
1.1.1.19  root     15351:                REG16(AX) = 0x0000;
                   15352:                REG8(BL) = 0xa2;
1.1.1.30  root     15353:        } else if(!(emb_handle->lock > 0)) {
1.1.1.19  root     15354:                REG16(AX) = 0x0000;
                   15355:                REG8(BL) = 0xaa;
                   15356:        } else {
1.1.1.30  root     15357:                emb_handle->lock--;
1.1.1.19  root     15358:                REG16(AX) = 0x0001;
                   15359:                REG8(BL) = 0x00;
                   15360:        }
                   15361: }
                   15362: 
                   15363: inline void msdos_call_xms_0eh()
                   15364: {
1.1.1.30  root     15365:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   15366:        
                   15367:        if(emb_handle == NULL) {
1.1.1.19  root     15368:                REG16(AX) = 0x0000;
                   15369:                REG8(BL) = 0xa2;
                   15370:        } else {
                   15371:                REG16(AX) = 0x0001;
1.1.1.30  root     15372:                REG8(BH) = emb_handle->lock;
                   15373:                REG8(BL) = msdos_xms_get_unused_emb_handle_count();
                   15374:                REG16(DX) = emb_handle->size_kb;
1.1.1.19  root     15375:        }
                   15376: }
                   15377: 
1.1.1.30  root     15378: void msdos_call_xms_0fh(int size_kb)
1.1.1.19  root     15379: {
1.1.1.30  root     15380:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   15381:        
                   15382:        if(emb_handle == NULL) {
1.1.1.19  root     15383:                REG16(AX) = 0x0000;
                   15384:                REG8(BL) = 0xa2;
1.1.1.30  root     15385:        } else if(emb_handle->lock > 0) {
1.1.1.19  root     15386:                REG16(AX) = 0x0000;
                   15387:                REG8(BL) = 0xab;
                   15388:        } else {
1.1.1.30  root     15389:                if(emb_handle->size_kb < size_kb) {
                   15390:                        if(emb_handle->next != NULL && emb_handle->next->handle == 0 && (emb_handle->size_kb + emb_handle->next->size_kb) >= size_kb) {
                   15391:                                msdos_xms_combine_emb_handles(emb_handle);
                   15392:                                if(emb_handle->size_kb > size_kb) {
                   15393:                                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   15394:                                }
                   15395:                        } else {
                   15396:                                int old_handle = emb_handle->handle;
                   15397:                                int old_size_kb = emb_handle->size_kb;
                   15398:                                UINT8 *buffer = (UINT8 *)malloc(old_size_kb * 1024);
                   15399:                                
                   15400:                                memcpy(buffer, mem + emb_handle->address, old_size_kb * 1024);
                   15401:                                msdos_xms_free_emb_handle(emb_handle);
                   15402:                                
                   15403:                                if((emb_handle = msdos_xms_alloc_emb_handle(size_kb)) == NULL) {
                   15404:                                        emb_handle = msdos_xms_alloc_emb_handle(old_size_kb); // should be always successed
                   15405:                                }
                   15406:                                emb_handle->handle = old_handle;
                   15407:                                memcpy(mem + emb_handle->address, buffer, old_size_kb * 1024);
                   15408:                                free(buffer);
                   15409:                        }
                   15410:                } else if(emb_handle->size_kb > size_kb) {
                   15411:                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   15412:                }
                   15413:                if(emb_handle->size_kb != size_kb) {
                   15414:                        REG16(AX) = 0x0000;
                   15415:                        REG8(BL) = 0xa0;
                   15416:                } else {
                   15417:                        REG16(AX) = 0x0001;
                   15418:                        REG8(BL) = 0x00;
                   15419:                }
1.1.1.19  root     15420:        }
                   15421: }
                   15422: 
1.1.1.30  root     15423: inline void msdos_call_xms_0fh()
                   15424: {
                   15425:        msdos_call_xms_0fh(REG16(BX));
                   15426: }
                   15427: 
1.1.1.19  root     15428: inline void msdos_call_xms_10h()
                   15429: {
                   15430:        int seg;
                   15431:        
                   15432:        if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(DX), 0)) != -1) {
                   15433:                REG16(AX) = 0x0001;
                   15434:                REG16(BX) = seg;
                   15435:        } else {
                   15436:                REG16(AX) = 0x0000;
                   15437:                REG8(BL) = 0xb0;
                   15438:                REG16(DX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   15439:        }
                   15440: }
                   15441: 
                   15442: inline void msdos_call_xms_11h()
                   15443: {
                   15444:        int mcb_seg = REG16(DX) - 1;
                   15445:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   15446:        
                   15447:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   15448:                msdos_mem_free(REG16(DX));
                   15449:                REG16(AX) = 0x0001;
                   15450:                REG8(BL) = 0x00;
                   15451:        } else {
                   15452:                REG16(AX) = 0x0000;
                   15453:                REG8(BL) = 0xb2;
                   15454:        }
                   15455: }
                   15456: 
                   15457: inline void msdos_call_xms_12h()
                   15458: {
                   15459:        int mcb_seg = REG16(DX) - 1;
                   15460:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   15461:        int max_paragraphs;
                   15462:        
                   15463:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   15464:                if(!msdos_mem_realloc(REG16(DX), REG16(BX), &max_paragraphs)) {
                   15465:                        REG16(AX) = 0x0001;
                   15466:                        REG8(BL) = 0x00;
                   15467:                } else {
                   15468:                        REG16(AX) = 0x0000;
                   15469:                        REG8(BL) = 0xb0;
                   15470:                        REG16(DX) = max_paragraphs;
                   15471:                }
                   15472:        } else {
                   15473:                REG16(AX) = 0x0000;
                   15474:                REG8(BL) = 0xb2;
                   15475:        }
                   15476: }
                   15477: 
1.1.1.29  root     15478: #if defined(HAS_I386)
                   15479: 
                   15480: inline void msdos_call_xms_88h()
                   15481: {
                   15482:        REG32(EAX) = REG32(EDX) = 0x0000;
                   15483:        
1.1.1.30  root     15484:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   15485:                if(emb_handle->handle == 0) {
                   15486:                        if(REG32(EAX) < emb_handle->size_kb) {
                   15487:                                REG32(EAX) = emb_handle->size_kb;
1.1.1.29  root     15488:                        }
1.1.1.30  root     15489:                        REG32(EDX) += emb_handle->size_kb;
1.1.1.29  root     15490:                }
                   15491:        }
                   15492:        if(REG32(EAX) == 0 && REG32(EDX) == 0) {
                   15493:                REG8(BL) = 0xa0;
                   15494:        } else {
                   15495:                REG8(BL) = 0x00;
                   15496:        }
                   15497:        REG32(ECX) = EMB_END - 1;
                   15498: }
                   15499: 
                   15500: inline void msdos_call_xms_89h()
                   15501: {
1.1.1.30  root     15502:        msdos_call_xms_09h(REG32(EDX));
1.1.1.29  root     15503: }
                   15504: 
                   15505: inline void msdos_call_xms_8eh()
                   15506: {
1.1.1.30  root     15507:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   15508:        
                   15509:        if(emb_handle == NULL) {
1.1.1.29  root     15510:                REG16(AX) = 0x0000;
                   15511:                REG8(BL) = 0xa2;
                   15512:        } else {
                   15513:                REG16(AX) = 0x0001;
1.1.1.30  root     15514:                REG8(BH) = emb_handle->lock;
                   15515:                REG16(CX) = msdos_xms_get_unused_emb_handle_count();
                   15516:                REG32(EDX) = emb_handle->size_kb;
1.1.1.29  root     15517:        }
                   15518: }
                   15519: 
                   15520: inline void msdos_call_xms_8fh()
                   15521: {
1.1.1.30  root     15522:        msdos_call_xms_0fh(REG32(EBX));
1.1.1.29  root     15523: }
                   15524: 
                   15525: #endif
1.1.1.19  root     15526: #endif
                   15527: 
1.1.1.26  root     15528: UINT16 msdos_get_equipment()
                   15529: {
                   15530:        static UINT16 equip = 0;
                   15531:        
                   15532:        if(equip == 0) {
                   15533: #ifdef SUPPORT_FPU
                   15534:                equip |= (1 << 1);      // 80x87 coprocessor installed
                   15535: #endif
                   15536:                equip |= (1 << 2);      // pointing device installed (PS/2)
                   15537:                equip |= (2 << 4);      // initial video mode (80x25 color)
                   15538: //             equip |= (1 << 8);      // 0 if DMA installed
                   15539:                equip |= (2 << 9);      // number of serial ports
                   15540:                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     15541:                
                   15542:                // check only A: and B: if it is floppy drive
                   15543:                int n = 0;
                   15544:                for(int i = 0; i < 2; i++) {
1.1.1.44  root     15545:                        if(msdos_is_valid_drive(i) && msdos_is_removable_drive(i)) {
                   15546:                                n++;
1.1.1.28  root     15547:                        }
                   15548:                }
                   15549:                if(n != 0) {
                   15550:                        equip |= (1 << 0);      // floppy disk(s) installed
                   15551:                        n--;
                   15552:                        equip |= (n << 6);      // number of floppies installed less 1
                   15553:                }
                   15554: //             if(joyGetNumDevs() != 0) {
                   15555: //                     equip |= (1 << 12);     // game port installed
                   15556: //             }
1.1.1.26  root     15557:        }
                   15558:        return(equip);
                   15559: }
                   15560: 
1.1       root     15561: void msdos_syscall(unsigned num)
                   15562: {
1.1.1.22  root     15563: #ifdef ENABLE_DEBUG_SYSCALL
1.1.1.43  root     15564:        if(num == 0x08 || num == 0x1c) {
                   15565:                // don't log the timer interrupts
1.1.1.45  root     15566: //             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     15567:        } else if(num == 0x68) {
1.1.1.22  root     15568:                // dummy interrupt for EMS (int 67h)
1.1.1.33  root     15569:                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     15570:        } else if(num == 0x69) {
                   15571:                // dummy interrupt for XMS (call far)
1.1.1.33  root     15572:                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     15573:        } else if(num >= 0x6a && num < 0x6f) {
1.1.1.45  root     15574:                // dummy interrupt
1.1.1.46  root     15575:        } else if(num == 0x6f) {
                   15576:                // dummy interrupt for call 0005h (call near)
                   15577:                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     15578:        } else {
1.1.1.33  root     15579:                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     15580:        }
                   15581: #endif
1.1.1.36  root     15582:        // update cursor position
                   15583:        if(cursor_moved) {
                   15584:                pcbios_update_cursor_position();
                   15585:                cursor_moved = false;
                   15586:        }
1.1.1.33  root     15587:        ctrl_break_detected = ctrl_break_pressed = ctrl_c_pressed = false;
1.1.1.22  root     15588:        
1.1       root     15589:        switch(num) {
                   15590:        case 0x00:
1.1.1.28  root     15591:                try {
                   15592:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   15593:                        error("division by zero\n");
                   15594:                } catch(...) {
                   15595:                        fatalerror("division by zero detected, and failed to terminate current process\n");
                   15596:                }
1.1       root     15597:                break;
                   15598:        case 0x04:
1.1.1.28  root     15599:                try {
                   15600:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   15601:                        error("overflow\n");
                   15602:                } catch(...) {
                   15603:                        fatalerror("overflow detected, and failed to terminate current process\n");
                   15604:                }
1.1       root     15605:                break;
                   15606:        case 0x06:
                   15607:                // NOTE: ish.com has illegal instruction...
1.1.1.14  root     15608:                if(!ignore_illegal_insn) {
1.1.1.28  root     15609:                        try {
                   15610:                                msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   15611:                                error("illegal instruction\n");
                   15612:                        } catch(...) {
                   15613:                                fatalerror("illegal instruction detected, and failed to terminate current process\n");
                   15614:                        }
1.1.1.14  root     15615:                } else {
                   15616: #if defined(HAS_I386)
1.1.1.39  root     15617:                        m_eip = m_int6h_skip_eip;
                   15618: #elif defined(HAS_I286)
                   15619:                        m_pc = m_int6h_skip_pc;
1.1.1.14  root     15620: #else
1.1.1.39  root     15621:                        // 8086/80186 ignore an invalid opcode
1.1.1.14  root     15622: #endif
                   15623:                }
1.1       root     15624:                break;
1.1.1.33  root     15625:        case 0x09:
                   15626:                // ctrl-break is pressed
                   15627:                if(raise_int_1bh) {
                   15628: #if defined(HAS_I386)
                   15629:                        m_ext = 0; // not an external interrupt
                   15630:                        i386_trap(0x1b, 1, 0);
                   15631:                        m_ext = 1;
                   15632: #else
                   15633:                        PREFIX86(_interrupt)(0x1b);
                   15634: #endif
                   15635:                        raise_int_1bh = false;
                   15636:                }
1.1.1.8   root     15637:        case 0x08:
1.1.1.14  root     15638: //             pcbios_irq0(); // this causes too slow emulation...
1.1.1.8   root     15639:        case 0x0b:
                   15640:        case 0x0c:
                   15641:        case 0x0d:
                   15642:        case 0x0e:
                   15643:        case 0x0f:
                   15644:                // EOI
                   15645:                pic[0].isr &= ~(1 << (num - 0x08));
                   15646:                pic_update();
                   15647:                break;
1.1       root     15648:        case 0x10:
                   15649:                // PC BIOS - Video
1.1.1.14  root     15650:                if(!restore_console_on_exit) {
1.1.1.15  root     15651:                        change_console_size(scr_width, scr_height);
1.1       root     15652:                }
1.1.1.3   root     15653:                m_CF = 0;
1.1       root     15654:                switch(REG8(AH)) {
1.1.1.16  root     15655:                case 0x00: pcbios_int_10h_00h(); break;
1.1       root     15656:                case 0x01: pcbios_int_10h_01h(); break;
                   15657:                case 0x02: pcbios_int_10h_02h(); break;
                   15658:                case 0x03: pcbios_int_10h_03h(); break;
                   15659:                case 0x05: pcbios_int_10h_05h(); break;
                   15660:                case 0x06: pcbios_int_10h_06h(); break;
                   15661:                case 0x07: pcbios_int_10h_07h(); break;
                   15662:                case 0x08: pcbios_int_10h_08h(); break;
                   15663:                case 0x09: pcbios_int_10h_09h(); break;
                   15664:                case 0x0a: pcbios_int_10h_0ah(); break;
                   15665:                case 0x0b: break;
1.1.1.40  root     15666:                case 0x0c: pcbios_int_10h_0ch(); break;
                   15667:                case 0x0d: pcbios_int_10h_0dh(); break;
1.1       root     15668:                case 0x0e: pcbios_int_10h_0eh(); break;
                   15669:                case 0x0f: pcbios_int_10h_0fh(); break;
                   15670:                case 0x10: break;
1.1.1.14  root     15671:                case 0x11: pcbios_int_10h_11h(); break;
                   15672:                case 0x12: pcbios_int_10h_12h(); break;
1.1       root     15673:                case 0x13: pcbios_int_10h_13h(); break;
1.1.1.30  root     15674:                case 0x18: pcbios_int_10h_18h(); break;
1.1.1.14  root     15675:                case 0x1a: pcbios_int_10h_1ah(); break;
1.1.1.24  root     15676:                case 0x1b: REG8(AL) = 0x00; break; // functionality/state information is not supported
                   15677:                case 0x1c: REG8(AL) = 0x00; break; // save/restore video state is not supported
1.1       root     15678:                case 0x1d: pcbios_int_10h_1dh(); break;
1.1.1.24  root     15679:                case 0x1e: REG8(AL) = 0x00; break; // flat-panel functions are not supported
                   15680:                case 0x1f: REG8(AL) = 0x00; break; // xga functions are not supported
1.1.1.22  root     15681:                case 0x4f: pcbios_int_10h_4fh(); break;
1.1.1.30  root     15682:                case 0x6f: break;
1.1.1.22  root     15683:                case 0x80: m_CF = 1; break; // unknown
                   15684:                case 0x81: m_CF = 1; break; // unknown
1.1       root     15685:                case 0x82: pcbios_int_10h_82h(); break;
1.1.1.22  root     15686:                case 0x83: pcbios_int_10h_83h(); break;
                   15687:                case 0x8b: break;
                   15688:                case 0x8c: m_CF = 1; break; // unknown
                   15689:                case 0x8d: m_CF = 1; break; // unknown
                   15690:                case 0x8e: m_CF = 1; break; // unknown
                   15691:                case 0x90: pcbios_int_10h_90h(); break;
                   15692:                case 0x91: pcbios_int_10h_91h(); break;
                   15693:                case 0x92: break;
                   15694:                case 0x93: break;
                   15695:                case 0xef: pcbios_int_10h_efh(); break;
1.1.1.24  root     15696:                case 0xfa: break; // ega register interface library is not installed
1.1       root     15697:                case 0xfe: pcbios_int_10h_feh(); break;
                   15698:                case 0xff: pcbios_int_10h_ffh(); break;
                   15699:                default:
1.1.1.22  root     15700:                        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));
                   15701:                        m_CF = 1;
1.1       root     15702:                        break;
                   15703:                }
                   15704:                break;
                   15705:        case 0x11:
                   15706:                // PC BIOS - Get Equipment List
1.1.1.26  root     15707:                REG16(AX) = msdos_get_equipment();
1.1       root     15708:                break;
                   15709:        case 0x12:
                   15710:                // PC BIOS - Get Memory Size
1.1.1.33  root     15711:                REG16(AX) = *(UINT16 *)(mem + 0x413);
1.1       root     15712:                break;
                   15713:        case 0x13:
1.1.1.42  root     15714:                // PC BIOS - Disk I/O
                   15715:                {
                   15716:                        static UINT8 last = 0x00;
                   15717:                        switch(REG8(AH)) {
                   15718:                        case 0x00: pcbios_int_13h_00h(); break;
                   15719:                        case 0x01: // get last status
                   15720:                                REG8(AH) = last;
                   15721:                                break;
                   15722:                        case 0x02: pcbios_int_13h_02h(); break;
                   15723:                        case 0x03: pcbios_int_13h_03h(); break;
                   15724:                        case 0x04: pcbios_int_13h_04h(); break;
                   15725:                        case 0x08: pcbios_int_13h_08h(); break;
                   15726:                        case 0x0a: pcbios_int_13h_02h(); break;
                   15727:                        case 0x0b: pcbios_int_13h_03h(); break;
                   15728:                        case 0x0d: pcbios_int_13h_00h(); break;
                   15729:                        case 0x10: pcbios_int_13h_10h(); break;
                   15730:                        case 0x15: pcbios_int_13h_15h(); break;
1.1.1.43  root     15731:                        case 0x41: pcbios_int_13h_41h(); break;
1.1.1.42  root     15732:                        case 0x05: // format
                   15733:                        case 0x06:
                   15734:                        case 0x07:
                   15735:                                REG8(AH) = 0x0c; // unsupported track or invalid media
                   15736:                                m_CF = 1;
                   15737:                                break;
                   15738:                        case 0x09:
                   15739:                        case 0x0c: // seek
                   15740:                        case 0x11: // recalib
                   15741:                        case 0x14:
                   15742:                        case 0x17:
                   15743:                                REG8(AH) = 0x00; // successful completion
                   15744:                                break;
1.1.1.43  root     15745:                        case 0x21: // QUICKCACHE II v4.20 - Flush Cache
                   15746:                        case 0xa1: // Super PC-Kwik v3.20+ - Flush Cache
                   15747:                                REG8(AH) = 0x01; // invalid function
                   15748:                                m_CF = 1;
                   15749:                                break;
1.1.1.42  root     15750:                        default:
                   15751:                                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));
                   15752:                                REG8(AH) = 0x01; // invalid function
                   15753:                                m_CF = 1;
                   15754:                                break;
                   15755:                        }
                   15756:                        last = REG8(AH);
                   15757:                }
1.1       root     15758:                break;
                   15759:        case 0x14:
                   15760:                // PC BIOS - Serial I/O
1.1.1.25  root     15761:                switch(REG8(AH)) {
                   15762:                case 0x00: pcbios_int_14h_00h(); break;
                   15763:                case 0x01: pcbios_int_14h_01h(); break;
                   15764:                case 0x02: pcbios_int_14h_02h(); break;
                   15765:                case 0x03: pcbios_int_14h_03h(); break;
                   15766:                case 0x04: pcbios_int_14h_04h(); break;
                   15767:                case 0x05: pcbios_int_14h_05h(); break;
                   15768:                default:
                   15769:                        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));
                   15770:                        break;
                   15771:                }
1.1       root     15772:                break;
                   15773:        case 0x15:
                   15774:                // PC BIOS
1.1.1.3   root     15775:                m_CF = 0;
1.1       root     15776:                switch(REG8(AH)) {
1.1.1.14  root     15777:                case 0x10: pcbios_int_15h_10h(); break;
1.1       root     15778:                case 0x23: pcbios_int_15h_23h(); break;
                   15779:                case 0x24: pcbios_int_15h_24h(); break;
1.1.1.24  root     15780:                case 0x41: break;
1.1       root     15781:                case 0x49: pcbios_int_15h_49h(); break;
1.1.1.22  root     15782:                case 0x50: pcbios_int_15h_50h(); break;
1.1.1.30  root     15783:                case 0x53: pcbios_int_15h_53h(); break;
1.1.1.43  root     15784:                case 0x84: pcbios_int_15h_84h(); break;
1.1       root     15785:                case 0x86: pcbios_int_15h_86h(); break;
                   15786:                case 0x87: pcbios_int_15h_87h(); break;
                   15787:                case 0x88: pcbios_int_15h_88h(); break;
                   15788:                case 0x89: pcbios_int_15h_89h(); break;
1.1.1.21  root     15789:                case 0x8a: pcbios_int_15h_8ah(); break;
1.1.1.22  root     15790:                case 0xc0: // PS/2 ???
                   15791:                case 0xc1:
                   15792:                case 0xc2:
1.1.1.30  root     15793:                case 0xc3: // PS50+ ???
                   15794:                case 0xc4:
1.1.1.22  root     15795:                        REG8(AH) = 0x86;
                   15796:                        m_CF = 1;
                   15797:                        break;
1.1.1.3   root     15798: #if defined(HAS_I386)
1.1       root     15799:                case 0xc9: pcbios_int_15h_c9h(); break;
1.1.1.3   root     15800: #endif
1.1       root     15801:                case 0xca: pcbios_int_15h_cah(); break;
1.1.1.22  root     15802:                case 0xe8: pcbios_int_15h_e8h(); break;
1.1       root     15803:                default:
1.1.1.22  root     15804:                        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));
                   15805:                        REG8(AH) = 0x86;
1.1.1.3   root     15806:                        m_CF = 1;
1.1       root     15807:                        break;
                   15808:                }
                   15809:                break;
                   15810:        case 0x16:
                   15811:                // PC BIOS - Keyboard
1.1.1.3   root     15812:                m_CF = 0;
1.1       root     15813:                switch(REG8(AH)) {
                   15814:                case 0x00: pcbios_int_16h_00h(); break;
                   15815:                case 0x01: pcbios_int_16h_01h(); break;
                   15816:                case 0x02: pcbios_int_16h_02h(); break;
                   15817:                case 0x03: pcbios_int_16h_03h(); break;
                   15818:                case 0x05: pcbios_int_16h_05h(); break;
                   15819:                case 0x10: pcbios_int_16h_00h(); break;
                   15820:                case 0x11: pcbios_int_16h_01h(); break;
                   15821:                case 0x12: pcbios_int_16h_12h(); break;
                   15822:                case 0x13: pcbios_int_16h_13h(); break;
                   15823:                case 0x14: pcbios_int_16h_14h(); break;
1.1.1.24  root     15824:                case 0x55: pcbios_int_16h_55h(); break;
1.1.1.30  root     15825:                case 0x6f: pcbios_int_16h_6fh(); break;
1.1.1.22  root     15826:                case 0xda: break; // unknown
1.1.1.43  root     15827:                case 0xdb: break; // unknown
1.1.1.22  root     15828:                case 0xff: break; // unknown
1.1       root     15829:                default:
1.1.1.22  root     15830:                        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     15831:                        break;
                   15832:                }
                   15833:                break;
                   15834:        case 0x17:
                   15835:                // PC BIOS - Printer
1.1.1.37  root     15836:                m_CF = 0;
                   15837:                switch(REG8(AH)) {
                   15838:                case 0x00: pcbios_int_17h_00h(); break;
                   15839:                case 0x01: pcbios_int_17h_01h(); break;
                   15840:                case 0x02: pcbios_int_17h_02h(); break;
                   15841:                case 0x03: pcbios_int_17h_03h(); break;
                   15842:                case 0x50: pcbios_int_17h_50h(); break;
                   15843:                case 0x51: pcbios_int_17h_51h(); break;
                   15844:                case 0x52: pcbios_int_17h_52h(); break;
                   15845:                case 0x84: pcbios_int_17h_84h(); break;
                   15846:                case 0x85: pcbios_int_17h_85h(); break;
                   15847:                default:
                   15848:                        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));
                   15849:                        break;
                   15850:                }
1.1       root     15851:                break;
                   15852:        case 0x1a:
                   15853:                // PC BIOS - Timer
1.1.1.3   root     15854:                m_CF = 0;
1.1       root     15855:                switch(REG8(AH)) {
                   15856:                case 0x00: pcbios_int_1ah_00h(); break;
                   15857:                case 0x01: break;
                   15858:                case 0x02: pcbios_int_1ah_02h(); break;
                   15859:                case 0x03: break;
                   15860:                case 0x04: pcbios_int_1ah_04h(); break;
                   15861:                case 0x05: break;
                   15862:                case 0x0a: pcbios_int_1ah_0ah(); break;
                   15863:                case 0x0b: break;
1.1.1.14  root     15864:                case 0x35: break; // Word Perfect Third Party Interface?
                   15865:                case 0x36: break; // Word Perfect Third Party Interface
                   15866:                case 0x70: break; // SNAP? (Simple Network Application Protocol)
1.1.1.44  root     15867:                case 0xb0: break; // Microsoft Real-Time Compression Interface (MRCI)
1.1.1.43  root     15868:                case 0xb1: break; // PCI BIOS v2.0c+
                   15869:                case 0xb4: break; // Intel Plug-and-Play Auto-Configuration
1.1       root     15870:                default:
1.1.1.22  root     15871:                        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     15872:                        break;
                   15873:                }
                   15874:                break;
1.1.1.33  root     15875:        case 0x1b:
                   15876:                mem[0x471] = 0x00;
                   15877:                break;
1.1       root     15878:        case 0x20:
1.1.1.28  root     15879:                try {
                   15880:                        msdos_process_terminate(SREG(CS), retval, 1);
                   15881:                } catch(...) {
                   15882:                        fatalerror("failed to terminate the process (PSP=%04X) by int 20h\n", SREG(CS));
                   15883:                }
1.1       root     15884:                break;
1.1.1.46  root     15885:        case 0x6f:
                   15886:                // dummy interrupt for case map routine pointed in the country info
                   15887: //             if(!(REG8(CL) >= 0x00 && REG8(CL) <= 0x24)) {
                   15888: //                     REG8(AL) = 0x00;
                   15889: //                     break;
                   15890: //             }
1.1       root     15891:        case 0x21:
                   15892:                // MS-DOS System Call
1.1.1.3   root     15893:                m_CF = 0;
1.1.1.28  root     15894:                try {
1.1.1.46  root     15895:                        switch(num == 0x21 ? REG8(AH) : REG8(CL)) {
1.1.1.28  root     15896:                        case 0x00: msdos_int_21h_00h(); break;
                   15897:                        case 0x01: msdos_int_21h_01h(); break;
                   15898:                        case 0x02: msdos_int_21h_02h(); break;
                   15899:                        case 0x03: msdos_int_21h_03h(); break;
                   15900:                        case 0x04: msdos_int_21h_04h(); break;
                   15901:                        case 0x05: msdos_int_21h_05h(); break;
                   15902:                        case 0x06: msdos_int_21h_06h(); break;
                   15903:                        case 0x07: msdos_int_21h_07h(); break;
                   15904:                        case 0x08: msdos_int_21h_08h(); break;
                   15905:                        case 0x09: msdos_int_21h_09h(); break;
                   15906:                        case 0x0a: msdos_int_21h_0ah(); break;
                   15907:                        case 0x0b: msdos_int_21h_0bh(); break;
                   15908:                        case 0x0c: msdos_int_21h_0ch(); break;
                   15909:                        case 0x0d: msdos_int_21h_0dh(); break;
                   15910:                        case 0x0e: msdos_int_21h_0eh(); break;
                   15911:                        case 0x0f: msdos_int_21h_0fh(); break;
                   15912:                        case 0x10: msdos_int_21h_10h(); break;
                   15913:                        case 0x11: msdos_int_21h_11h(); break;
                   15914:                        case 0x12: msdos_int_21h_12h(); break;
                   15915:                        case 0x13: msdos_int_21h_13h(); break;
                   15916:                        case 0x14: msdos_int_21h_14h(); break;
                   15917:                        case 0x15: msdos_int_21h_15h(); break;
                   15918:                        case 0x16: msdos_int_21h_16h(); break;
                   15919:                        case 0x17: msdos_int_21h_17h(); break;
                   15920:                        case 0x18: msdos_int_21h_18h(); break;
                   15921:                        case 0x19: msdos_int_21h_19h(); break;
                   15922:                        case 0x1a: msdos_int_21h_1ah(); break;
                   15923:                        case 0x1b: msdos_int_21h_1bh(); break;
                   15924:                        case 0x1c: msdos_int_21h_1ch(); break;
                   15925:                        case 0x1d: msdos_int_21h_1dh(); break;
                   15926:                        case 0x1e: msdos_int_21h_1eh(); break;
                   15927:                        case 0x1f: msdos_int_21h_1fh(); break;
                   15928:                        case 0x20: msdos_int_21h_20h(); break;
                   15929:                        case 0x21: msdos_int_21h_21h(); break;
                   15930:                        case 0x22: msdos_int_21h_22h(); break;
                   15931:                        case 0x23: msdos_int_21h_23h(); break;
                   15932:                        case 0x24: msdos_int_21h_24h(); break;
                   15933:                        case 0x25: msdos_int_21h_25h(); break;
                   15934:                        case 0x26: msdos_int_21h_26h(); break;
                   15935:                        case 0x27: msdos_int_21h_27h(); break;
                   15936:                        case 0x28: msdos_int_21h_28h(); break;
                   15937:                        case 0x29: msdos_int_21h_29h(); break;
                   15938:                        case 0x2a: msdos_int_21h_2ah(); break;
                   15939:                        case 0x2b: msdos_int_21h_2bh(); break;
                   15940:                        case 0x2c: msdos_int_21h_2ch(); break;
                   15941:                        case 0x2d: msdos_int_21h_2dh(); break;
                   15942:                        case 0x2e: msdos_int_21h_2eh(); break;
                   15943:                        case 0x2f: msdos_int_21h_2fh(); break;
                   15944:                        case 0x30: msdos_int_21h_30h(); break;
                   15945:                        case 0x31: msdos_int_21h_31h(); break;
                   15946:                        case 0x32: msdos_int_21h_32h(); break;
                   15947:                        case 0x33: msdos_int_21h_33h(); break;
                   15948:                        case 0x34: msdos_int_21h_34h(); break;
                   15949:                        case 0x35: msdos_int_21h_35h(); break;
                   15950:                        case 0x36: msdos_int_21h_36h(); break;
                   15951:                        case 0x37: msdos_int_21h_37h(); break;
                   15952:                        case 0x38: msdos_int_21h_38h(); break;
                   15953:                        case 0x39: msdos_int_21h_39h(0); break;
                   15954:                        case 0x3a: msdos_int_21h_3ah(0); break;
                   15955:                        case 0x3b: msdos_int_21h_3bh(0); break;
                   15956:                        case 0x3c: msdos_int_21h_3ch(); break;
                   15957:                        case 0x3d: msdos_int_21h_3dh(); break;
                   15958:                        case 0x3e: msdos_int_21h_3eh(); break;
                   15959:                        case 0x3f: msdos_int_21h_3fh(); break;
                   15960:                        case 0x40: msdos_int_21h_40h(); break;
                   15961:                        case 0x41: msdos_int_21h_41h(0); break;
                   15962:                        case 0x42: msdos_int_21h_42h(); break;
                   15963:                        case 0x43: msdos_int_21h_43h(0); break;
                   15964:                        case 0x44: msdos_int_21h_44h(); break;
                   15965:                        case 0x45: msdos_int_21h_45h(); break;
                   15966:                        case 0x46: msdos_int_21h_46h(); break;
                   15967:                        case 0x47: msdos_int_21h_47h(0); break;
                   15968:                        case 0x48: msdos_int_21h_48h(); break;
                   15969:                        case 0x49: msdos_int_21h_49h(); break;
                   15970:                        case 0x4a: msdos_int_21h_4ah(); break;
                   15971:                        case 0x4b: msdos_int_21h_4bh(); break;
                   15972:                        case 0x4c: msdos_int_21h_4ch(); break;
                   15973:                        case 0x4d: msdos_int_21h_4dh(); break;
                   15974:                        case 0x4e: msdos_int_21h_4eh(); break;
                   15975:                        case 0x4f: msdos_int_21h_4fh(); break;
                   15976:                        case 0x50: msdos_int_21h_50h(); break;
                   15977:                        case 0x51: msdos_int_21h_51h(); break;
                   15978:                        case 0x52: msdos_int_21h_52h(); break;
1.1.1.43  root     15979:                        case 0x53: msdos_int_21h_53h(); break;
1.1.1.28  root     15980:                        case 0x54: msdos_int_21h_54h(); break;
                   15981:                        case 0x55: msdos_int_21h_55h(); break;
                   15982:                        case 0x56: msdos_int_21h_56h(0); break;
                   15983:                        case 0x57: msdos_int_21h_57h(); break;
                   15984:                        case 0x58: msdos_int_21h_58h(); break;
                   15985:                        case 0x59: msdos_int_21h_59h(); break;
                   15986:                        case 0x5a: msdos_int_21h_5ah(); break;
                   15987:                        case 0x5b: msdos_int_21h_5bh(); break;
                   15988:                        case 0x5c: msdos_int_21h_5ch(); break;
                   15989:                        case 0x5d: msdos_int_21h_5dh(); break;
1.1.1.42  root     15990:                        case 0x5e: msdos_int_21h_5eh(); break;
1.1.1.30  root     15991:                        case 0x5f: msdos_int_21h_5fh(); break;
1.1.1.28  root     15992:                        case 0x60: msdos_int_21h_60h(0); break;
                   15993:                        case 0x61: msdos_int_21h_61h(); break;
                   15994:                        case 0x62: msdos_int_21h_62h(); break;
                   15995:                        case 0x63: msdos_int_21h_63h(); break;
1.1.1.33  root     15996:                        // 0x64: Set Device Driver Lockahead Flag
1.1.1.28  root     15997:                        case 0x65: msdos_int_21h_65h(); break;
                   15998:                        case 0x66: msdos_int_21h_66h(); break;
                   15999:                        case 0x67: msdos_int_21h_67h(); break;
                   16000:                        case 0x68: msdos_int_21h_68h(); break;
                   16001:                        case 0x69: msdos_int_21h_69h(); break;
                   16002:                        case 0x6a: msdos_int_21h_6ah(); break;
                   16003:                        case 0x6b: msdos_int_21h_6bh(); break;
                   16004:                        case 0x6c: msdos_int_21h_6ch(0); break;
1.1.1.33  root     16005:                        // 0x6d: Find First ROM Program
                   16006:                        // 0x6e: Find Next ROM Program
                   16007:                        // 0x6f: Get/Set ROM Scan Start Address
1.1.1.43  root     16008:                        case 0x70: msdos_int_21h_70h(); break;
1.1.1.28  root     16009:                        case 0x71:
1.1.1.33  root     16010:                                // Windows95 - Long Filename Functions
1.1.1.28  root     16011:                                switch(REG8(AL)) {
                   16012:                                case 0x0d: msdos_int_21h_710dh(); break;
                   16013:                                case 0x39: msdos_int_21h_39h(1); break;
                   16014:                                case 0x3a: msdos_int_21h_3ah(1); break;
                   16015:                                case 0x3b: msdos_int_21h_3bh(1); break;
                   16016:                                case 0x41: msdos_int_21h_7141h(1); break;
                   16017:                                case 0x43: msdos_int_21h_43h(1); break;
                   16018:                                case 0x47: msdos_int_21h_47h(1); break;
                   16019:                                case 0x4e: msdos_int_21h_714eh(); break;
                   16020:                                case 0x4f: msdos_int_21h_714fh(); break;
                   16021:                                case 0x56: msdos_int_21h_56h(1); break;
                   16022:                                case 0x60: msdos_int_21h_60h(1); break;
                   16023:                                case 0x6c: msdos_int_21h_6ch(1); break;
                   16024:                                case 0xa0: msdos_int_21h_71a0h(); break;
                   16025:                                case 0xa1: msdos_int_21h_71a1h(); break;
                   16026:                                case 0xa6: msdos_int_21h_71a6h(); break;
                   16027:                                case 0xa7: msdos_int_21h_71a7h(); break;
                   16028:                                case 0xa8: msdos_int_21h_71a8h(); break;
1.1.1.45  root     16029:                                case 0xa9: msdos_int_21h_6ch(1); break;
1.1.1.28  root     16030:                                case 0xaa: msdos_int_21h_71aah(); break;
                   16031:                                default:
                   16032:                                        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));
                   16033:                                        REG16(AX) = 0x7100;
                   16034:                                        m_CF = 1;
                   16035:                                        break;
                   16036:                                }
                   16037:                                break;
                   16038:                        // 0x72: Windows95 beta - LFN FindClose
                   16039:                        case 0x73:
1.1.1.33  root     16040:                                // Windows95 - FAT32 Functions
1.1.1.28  root     16041:                                switch(REG8(AL)) {
                   16042:                                case 0x00: msdos_int_21h_7300h(); break;
1.1.1.33  root     16043:                                // 0x01: Set Drive Locking ???
1.1.1.28  root     16044:                                case 0x02: msdos_int_21h_7302h(); break;
                   16045:                                case 0x03: msdos_int_21h_7303h(); break;
1.1.1.33  root     16046:                                // 0x04: Set DPB to Use for Formatting
                   16047:                                // 0x05: Extended Absolute Disk Read/Write
1.1.1.28  root     16048:                                default:
                   16049:                                        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));
                   16050:                                        REG16(AX) = 0x7300;
                   16051:                                        m_CF = 1;
                   16052:                                        break;
                   16053:                                }
1.1       root     16054:                                break;
1.1.1.30  root     16055:                        case 0xdb: msdos_int_21h_dbh(); break;
                   16056:                        case 0xdc: msdos_int_21h_dch(); break;
1.1       root     16057:                        default:
1.1.1.22  root     16058:                                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     16059:                                REG16(AX) = 0x01;
1.1.1.3   root     16060:                                m_CF = 1;
1.1       root     16061:                                break;
                   16062:                        }
1.1.1.28  root     16063:                } catch(int error) {
                   16064:                        REG16(AX) = error;
                   16065:                        m_CF = 1;
                   16066:                } catch(...) {
                   16067:                        REG16(AX) = 0x1f; // general failure
1.1.1.3   root     16068:                        m_CF = 1;
1.1       root     16069:                }
1.1.1.3   root     16070:                if(m_CF) {
1.1.1.23  root     16071:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
1.1.1.47! root     16072:                        sda->int21h_5d0ah_called = 0;
1.1.1.23  root     16073:                        sda->extended_error_code = REG16(AX);
                   16074:                        switch(sda->extended_error_code) {
                   16075:                        case  4: // Too many open files
                   16076:                        case  8: // Insufficient memory
                   16077:                                sda->error_class = 1; // Out of resource
                   16078:                                break;
                   16079:                        case  5: // Access denied
                   16080:                                sda->error_class = 3; // Authorization
                   16081:                                break;
                   16082:                        case  7: // Memory control block destroyed
                   16083:                                sda->error_class = 4; // Internal
                   16084:                                break;
                   16085:                        case  2: // File not found
                   16086:                        case  3: // Path not found
                   16087:                        case 15: // Invaid drive specified
                   16088:                        case 18: // No more files
                   16089:                                sda->error_class = 8; // Not found
                   16090:                                break;
                   16091:                        case 32: // Sharing violation
                   16092:                        case 33: // Lock violation
                   16093:                                sda->error_class = 10; // Locked
                   16094:                                break;
                   16095: //                     case 16: // Removal of current directory attempted
                   16096:                        case 19: // Attempted write on protected disk
                   16097:                        case 21: // Drive not ready
                   16098: //                     case 29: // Write failure
                   16099: //                     case 30: // Read failure
                   16100: //                     case 82: // Cannot create subdirectory
                   16101:                                sda->error_class = 11; // Media
                   16102:                                break;
                   16103:                        case 80: // File already exists
                   16104:                                sda->error_class = 12; // Already exist
                   16105:                                break;
                   16106:                        default:
                   16107:                                sda->error_class = 13; // Unknown
                   16108:                                break;
                   16109:                        }
                   16110:                        sda->suggested_action = 1; // Retry
                   16111:                        sda->locus_of_last_error = 1; // Unknown
1.1       root     16112:                }
1.1.1.33  root     16113:                if(ctrl_break_checking && ctrl_break_detected) {
1.1.1.26  root     16114:                        // raise int 23h
                   16115: #if defined(HAS_I386)
                   16116:                        m_ext = 0; // not an external interrupt
                   16117:                        i386_trap(0x23, 1, 0);
                   16118:                        m_ext = 1;
                   16119: #else
                   16120:                        PREFIX86(_interrupt)(0x23);
                   16121: #endif
                   16122:                }
1.1       root     16123:                break;
                   16124:        case 0x22:
                   16125:                fatalerror("int 22h (terminate address)\n");
                   16126:        case 0x23:
1.1.1.28  root     16127:                try {
                   16128:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x100, 1);
                   16129:                } catch(...) {
                   16130:                        fatalerror("failed to terminate the current process by int 23h\n");
                   16131:                }
1.1       root     16132:                break;
                   16133:        case 0x24:
1.1.1.32  root     16134: /*
1.1.1.28  root     16135:                try {
                   16136:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   16137:                } catch(...) {
                   16138:                        fatalerror("failed to terminate the current process by int 24h\n");
                   16139:                }
1.1.1.32  root     16140: */
                   16141:                msdos_int_24h();
1.1       root     16142:                break;
                   16143:        case 0x25:
                   16144:                msdos_int_25h();
                   16145:                break;
                   16146:        case 0x26:
                   16147:                msdos_int_26h();
                   16148:                break;
                   16149:        case 0x27:
1.1.1.28  root     16150:                try {
                   16151:                        msdos_int_27h();
                   16152:                } catch(...) {
                   16153:                        fatalerror("failed to terminate the process (PSP=%04X) by int 27h\n", SREG(CS));
                   16154:                }
1.1       root     16155:                break;
                   16156:        case 0x28:
                   16157:                Sleep(10);
1.1.1.35  root     16158:                REQUEST_HARDWRE_UPDATE();
1.1       root     16159:                break;
                   16160:        case 0x29:
                   16161:                msdos_int_29h();
                   16162:                break;
                   16163:        case 0x2e:
                   16164:                msdos_int_2eh();
                   16165:                break;
                   16166:        case 0x2f:
                   16167:                // multiplex interrupt
                   16168:                switch(REG8(AH)) {
1.1.1.22  root     16169:                case 0x05: msdos_int_2fh_05h(); break;
1.1.1.44  root     16170:                case 0x06: msdos_int_2fh_06h(); break;
1.1.1.22  root     16171:                case 0x11: msdos_int_2fh_11h(); break;
1.1.1.21  root     16172:                case 0x12: msdos_int_2fh_12h(); break;
1.1.1.30  root     16173:                case 0x13: msdos_int_2fh_13h(); break;
1.1.1.22  root     16174:                case 0x14: msdos_int_2fh_14h(); break;
                   16175:                case 0x15: msdos_int_2fh_15h(); break;
1.1       root     16176:                case 0x16: msdos_int_2fh_16h(); break;
1.1.1.22  root     16177:                case 0x19: msdos_int_2fh_19h(); break;
1.1       root     16178:                case 0x1a: msdos_int_2fh_1ah(); break;
1.1.1.30  root     16179:                case 0x40: msdos_int_2fh_40h(); break;
1.1       root     16180:                case 0x43: msdos_int_2fh_43h(); break;
1.1.1.22  root     16181:                case 0x46: msdos_int_2fh_46h(); break;
                   16182:                case 0x48: msdos_int_2fh_48h(); break;
1.1       root     16183:                case 0x4a: msdos_int_2fh_4ah(); break;
1.1.1.22  root     16184:                case 0x4b: msdos_int_2fh_4bh(); break;
1.1.1.44  root     16185:                case 0x4d: msdos_int_2fh_4dh(); break;
1.1       root     16186:                case 0x4f: msdos_int_2fh_4fh(); break;
1.1.1.22  root     16187:                case 0x55: msdos_int_2fh_55h(); break;
1.1.1.44  root     16188:                case 0x56: msdos_int_2fh_56h(); break;
1.1.1.24  root     16189:                case 0xad: msdos_int_2fh_adh(); break;
1.1       root     16190:                case 0xae: msdos_int_2fh_aeh(); break;
1.1.1.34  root     16191:                case 0xb7: msdos_int_2fh_b7h(); break;
1.1.1.43  root     16192:                default:
1.1.1.30  root     16193:                        switch(REG8(AL)) {
                   16194:                        case 0x00:
                   16195:                                // This is not installed
                   16196: //                             REG8(AL) = 0x00;
                   16197:                                break;
1.1.1.33  root     16198:                        case 0x01:
1.1.1.42  root     16199:                                // Quarterdeck RPCI - QEMM/QRAM - PCL-838.EXE is not installed
                   16200:                                if(REG8(AH) == 0xd2 && REG16(BX) == 0x5145 && REG16(CX) == 0x4d4d && REG16(DX) == 0x3432) {
                   16201:                                        break;
                   16202:                                }
1.1.1.33  root     16203:                                // Banyan VINES v4+ is not installed
                   16204:                                if(REG8(AH) == 0xd7 && REG16(BX) == 0x0000) {
                   16205:                                        break;
                   16206:                                }
1.1.1.42  root     16207:                                // Quarterdeck QDPMI.SYS v1.0 is not installed
                   16208:                                if(REG8(AH) == 0xde && REG16(BX) == 0x4450 && REG16(CX) == 0x4d49 && REG16(DX) == 0x8f4f) {
                   16209:                                        break;
                   16210:                                }
1.1.1.30  root     16211:                        default:
1.1.1.42  root     16212:                                // NORTON UTILITIES 5.0+
                   16213:                                if(REG8(AH) == 0xfe && REG16(DI) == 0x4e55) {
                   16214:                                        break;
                   16215:                                }
1.1.1.30  root     16216:                                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     16217:                                REG16(AX) = 0x01; // invalid function
1.1.1.30  root     16218:                                m_CF = 1;
                   16219:                                break;
                   16220:                        }
                   16221:                        break;
1.1       root     16222:                }
                   16223:                break;
1.1.1.24  root     16224:        case 0x33:
                   16225:                switch(REG8(AH)) {
                   16226:                case 0x00:
                   16227:                        // Mouse
                   16228:                        switch(REG8(AL)) {
                   16229:                        case 0x00: msdos_int_33h_0000h(); break;
                   16230:                        case 0x01: msdos_int_33h_0001h(); break;
                   16231:                        case 0x02: msdos_int_33h_0002h(); break;
                   16232:                        case 0x03: msdos_int_33h_0003h(); break;
1.1.1.34  root     16233:                        case 0x04: msdos_int_33h_0004h(); break;
1.1.1.24  root     16234:                        case 0x05: msdos_int_33h_0005h(); break;
                   16235:                        case 0x06: msdos_int_33h_0006h(); break;
                   16236:                        case 0x07: msdos_int_33h_0007h(); break;
                   16237:                        case 0x08: msdos_int_33h_0008h(); break;
                   16238:                        case 0x09: msdos_int_33h_0009h(); break;
1.1.1.34  root     16239:                        case 0x0a: break; // Define Text Cursor
1.1.1.24  root     16240:                        case 0x0b: msdos_int_33h_000bh(); break;
                   16241:                        case 0x0c: msdos_int_33h_000ch(); break;
1.1.1.34  root     16242:                        case 0x0d: break; // Light Pen Emulation On
                   16243:                        case 0x0e: break; // Light Pen Emulation Off
1.1.1.24  root     16244:                        case 0x0f: msdos_int_33h_000fh(); break;
1.1.1.34  root     16245:                        case 0x10: break; // Define Screen Region for Updating
1.1.1.24  root     16246:                        case 0x11: msdos_int_33h_0011h(); break;
1.1.1.34  root     16247:                        case 0x12: REG16(AX) = 0xffff; break; // Set Large Graphics Cursor Block
                   16248:                        case 0x13: break; // Define Double-Speed Threshold
1.1.1.24  root     16249:                        case 0x14: msdos_int_33h_0014h(); break;
                   16250:                        case 0x15: msdos_int_33h_0015h(); break;
                   16251:                        case 0x16: msdos_int_33h_0016h(); break;
                   16252:                        case 0x17: msdos_int_33h_0017h(); break;
1.1.1.43  root     16253:                        case 0x18: msdos_int_33h_0018h(); break;
                   16254:                        case 0x19: msdos_int_33h_0019h(); break;
1.1.1.24  root     16255:                        case 0x1a: msdos_int_33h_001ah(); break;
                   16256:                        case 0x1b: msdos_int_33h_001bh(); break;
                   16257:                        case 0x1d: msdos_int_33h_001dh(); break;
                   16258:                        case 0x1e: msdos_int_33h_001eh(); break;
1.1.1.34  root     16259:                        case 0x1f: msdos_int_33h_001fh(); break;
                   16260:                        case 0x20: msdos_int_33h_0020h(); break;
1.1.1.24  root     16261:                        case 0x21: msdos_int_33h_0021h(); break;
                   16262:                        case 0x22: msdos_int_33h_0022h(); break;
                   16263:                        case 0x23: msdos_int_33h_0023h(); break;
                   16264:                        case 0x24: msdos_int_33h_0024h(); break;
                   16265:                        case 0x26: msdos_int_33h_0026h(); break;
                   16266:                        case 0x2a: msdos_int_33h_002ah(); break;
1.1.1.34  root     16267:                        case 0x2f: break; // Mouse Hardware Reset
1.1.1.24  root     16268:                        case 0x31: msdos_int_33h_0031h(); break;
                   16269:                        case 0x32: msdos_int_33h_0032h(); break;
                   16270:                        default:
                   16271:                                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));
                   16272:                                break;
                   16273:                        }
                   16274:                        break;
                   16275:                default:
                   16276:                        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));
                   16277:                        break;
                   16278:                }
                   16279:                break;
1.1.1.19  root     16280:        case 0x68:
                   16281:                // dummy interrupt for EMS (int 67h)
                   16282:                switch(REG8(AH)) {
                   16283:                case 0x40: msdos_int_67h_40h(); break;
                   16284:                case 0x41: msdos_int_67h_41h(); break;
                   16285:                case 0x42: msdos_int_67h_42h(); break;
                   16286:                case 0x43: msdos_int_67h_43h(); break;
                   16287:                case 0x44: msdos_int_67h_44h(); break;
                   16288:                case 0x45: msdos_int_67h_45h(); break;
                   16289:                case 0x46: msdos_int_67h_46h(); break;
                   16290:                case 0x47: msdos_int_67h_47h(); break;
                   16291:                case 0x48: msdos_int_67h_48h(); break;
1.1.1.20  root     16292:                // 0x49: LIM EMS - Reserved - Get I/O Port Address (Undocumented in EMS 3.2)
                   16293:                // 0x4a: LIM EMS - Reserved - Get Translation Array (Undocumented in EMS 3.2)
1.1.1.19  root     16294:                case 0x4b: msdos_int_67h_4bh(); break;
                   16295:                case 0x4c: msdos_int_67h_4ch(); break;
                   16296:                case 0x4d: msdos_int_67h_4dh(); break;
1.1.1.20  root     16297:                case 0x4e: msdos_int_67h_4eh(); break;
1.1.1.21  root     16298:                case 0x4f: msdos_int_67h_4fh(); break;
1.1.1.20  root     16299:                case 0x50: msdos_int_67h_50h(); break;
1.1.1.19  root     16300:                case 0x51: msdos_int_67h_51h(); break;
1.1.1.20  root     16301:                case 0x52: msdos_int_67h_52h(); break;
1.1.1.19  root     16302:                case 0x53: msdos_int_67h_53h(); break;
                   16303:                case 0x54: msdos_int_67h_54h(); break;
1.1.1.20  root     16304:                // 0x55: LIM EMS 4.0 - Alter Page Map And JUMP
                   16305:                // 0x56: LIM EMS 4.0 - Alter Page Map And CALL
                   16306:                case 0x57: msdos_int_67h_57h(); break;
                   16307:                case 0x58: msdos_int_67h_58h(); break;
1.1.1.42  root     16308:                case 0x59: msdos_int_67h_59h(); break;
1.1.1.20  root     16309:                case 0x5a: msdos_int_67h_5ah(); break;
                   16310:                // 0x5b: LIM EMS 4.0 - Alternate Map Register Set (for DOS Kernel)
1.1.1.43  root     16311:                // 0x5c: LIM EMS 4.0 - Prepare Expanded Memory Hardware For Warm Boot
                   16312:                case 0x5d: msdos_int_67h_5dh(); break;
1.1.1.31  root     16313:                // 0x60: EEMS - Get Physical Window Array
                   16314:                // 0x61: EEMS - Generic Accelerator Card Support
                   16315:                // 0x68: EEMS - Get Address of All Pge Frames om System
                   16316:                // 0x69: EEMS - Map Page into Frame
                   16317:                // 0x6a: EEMS - Page Mapping
                   16318:                // 0xde: VCPI
1.1.1.30  root     16319:                case 0xde: msdos_int_67h_deh(); break;
1.1.1.19  root     16320:                default:
1.1.1.22  root     16321:                        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     16322:                        REG8(AH) = 0x84;
                   16323:                        break;
                   16324:                }
                   16325:                break;
                   16326: #ifdef SUPPORT_XMS
                   16327:        case 0x69:
                   16328:                // dummy interrupt for XMS (call far)
1.1.1.28  root     16329:                try {
                   16330:                        switch(REG8(AH)) {
                   16331:                        case 0x00: msdos_call_xms_00h(); break;
                   16332:                        case 0x01: msdos_call_xms_01h(); break;
                   16333:                        case 0x02: msdos_call_xms_02h(); break;
                   16334:                        case 0x03: msdos_call_xms_03h(); break;
                   16335:                        case 0x04: msdos_call_xms_04h(); break;
                   16336:                        case 0x05: msdos_call_xms_05h(); break;
                   16337:                        case 0x06: msdos_call_xms_06h(); break;
                   16338:                        case 0x07: msdos_call_xms_07h(); break;
                   16339:                        case 0x08: msdos_call_xms_08h(); break;
                   16340:                        case 0x09: msdos_call_xms_09h(); break;
                   16341:                        case 0x0a: msdos_call_xms_0ah(); break;
                   16342:                        case 0x0b: msdos_call_xms_0bh(); break;
                   16343:                        case 0x0c: msdos_call_xms_0ch(); break;
                   16344:                        case 0x0d: msdos_call_xms_0dh(); break;
                   16345:                        case 0x0e: msdos_call_xms_0eh(); break;
                   16346:                        case 0x0f: msdos_call_xms_0fh(); break;
                   16347:                        case 0x10: msdos_call_xms_10h(); break;
                   16348:                        case 0x11: msdos_call_xms_11h(); break;
                   16349:                        case 0x12: msdos_call_xms_12h(); break;
1.1.1.29  root     16350: #if defined(HAS_I386)
                   16351:                        case 0x88: msdos_call_xms_88h(); break;
                   16352:                        case 0x89: msdos_call_xms_89h(); break;
                   16353:                        case 0x8e: msdos_call_xms_8eh(); break;
                   16354:                        case 0x8f: msdos_call_xms_8fh(); break;
                   16355: #endif
1.1.1.28  root     16356:                        default:
                   16357:                                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));
                   16358:                                REG16(AX) = 0x0000;
                   16359:                                REG8(BL) = 0x80; // function not implemented
                   16360:                                break;
                   16361:                        }
                   16362:                } catch(...) {
1.1.1.19  root     16363:                        REG16(AX) = 0x0000;
1.1.1.28  root     16364:                        REG8(BL) = 0x8f; // unrecoverable driver error
1.1.1.19  root     16365:                }
                   16366:                break;
                   16367: #endif
                   16368:        case 0x6a:
1.1.1.24  root     16369:                // irq12 (mouse)
                   16370:                mouse_push_ax = REG16(AX);
                   16371:                mouse_push_bx = REG16(BX);
                   16372:                mouse_push_cx = REG16(CX);
                   16373:                mouse_push_dx = REG16(DX);
                   16374:                mouse_push_si = REG16(SI);
                   16375:                mouse_push_di = REG16(DI);
                   16376:                
1.1.1.43  root     16377:                if(mouse.status_irq && mouse.call_addr.dw) {
1.1.1.24  root     16378:                        REG16(AX) = mouse.status_irq;
                   16379:                        REG16(BX) = mouse.get_buttons();
1.1.1.34  root     16380:                        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   16381:                        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
1.1.1.24  root     16382:                        REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
                   16383:                        REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
                   16384:                        
                   16385:                        mem[0xfffd0 + 0x02] = 0x9a;     // call far
                   16386:                        mem[0xfffd0 + 0x03] = mouse.call_addr.w.l & 0xff;
                   16387:                        mem[0xfffd0 + 0x04] = mouse.call_addr.w.l >> 8;
                   16388:                        mem[0xfffd0 + 0x05] = mouse.call_addr.w.h & 0xff;
                   16389:                        mem[0xfffd0 + 0x06] = mouse.call_addr.w.h >> 8;
1.1.1.43  root     16390:                        break;
1.1.1.24  root     16391:                }
1.1.1.43  root     16392:                for(int i = 0; i < 8; i++) {
                   16393:                        if((mouse.status_irq_alt & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   16394:                                REG16(AX) = mouse.status_irq_alt;
                   16395:                                REG16(BX) = mouse.get_buttons();
                   16396:                                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   16397:                                REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
                   16398:                                REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
                   16399:                                REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
                   16400:                                
                   16401:                                mem[0xfffd0 + 0x02] = 0x9a;     // call far
                   16402:                                mem[0xfffd0 + 0x03] = mouse.call_addr_alt[i].w.l & 0xff;
                   16403:                                mem[0xfffd0 + 0x04] = mouse.call_addr_alt[i].w.l >> 8;
                   16404:                                mem[0xfffd0 + 0x05] = mouse.call_addr_alt[i].w.h & 0xff;
                   16405:                                mem[0xfffd0 + 0x06] = mouse.call_addr_alt[i].w.h >> 8;
                   16406:                                break;
                   16407:                        }
                   16408:                }
                   16409:                // invalid call addr :-(
                   16410:                mem[0xfffd0 + 0x02] = 0x90;     // nop
                   16411:                mem[0xfffd0 + 0x03] = 0x90;     // nop
                   16412:                mem[0xfffd0 + 0x04] = 0x90;     // nop
                   16413:                mem[0xfffd0 + 0x05] = 0x90;     // nop
                   16414:                mem[0xfffd0 + 0x06] = 0x90;     // nop
1.1.1.24  root     16415:                break;
                   16416:        case 0x6b:
                   16417:                // end of irq12 (mouse)
                   16418:                REG16(AX) = mouse_push_ax;
                   16419:                REG16(BX) = mouse_push_bx;
                   16420:                REG16(CX) = mouse_push_cx;
                   16421:                REG16(DX) = mouse_push_dx;
                   16422:                REG16(SI) = mouse_push_si;
                   16423:                REG16(DI) = mouse_push_di;
                   16424:                
                   16425:                // EOI
                   16426:                if((pic[1].isr &= ~(1 << 4)) == 0) {
                   16427:                        pic[0].isr &= ~(1 << 2); // master
                   16428:                }
                   16429:                pic_update();
                   16430:                break;
                   16431:        case 0x6c:
1.1.1.19  root     16432:                // dummy interrupt for case map routine pointed in the country info
                   16433:                if(REG8(AL) >= 0x80) {
                   16434:                        char tmp[2] = {0};
                   16435:                        tmp[0] = REG8(AL);
                   16436:                        my_strupr(tmp);
                   16437:                        REG8(AL) = tmp[0];
                   16438:                }
                   16439:                break;
1.1.1.27  root     16440:        case 0x6d:
                   16441:                // dummy interrupt for font read routine pointed by int 15h, ax=5000h
                   16442:                REG8(AL) = 0x86; // not supported
                   16443:                m_CF = 1;
                   16444:                break;
1.1.1.32  root     16445:        case 0x6e:
                   16446:                // dummy interrupt for parameter error message read routine pointed by int 2fh, ax=122eh, dl=08h
                   16447:                {
                   16448:                        UINT16 code = REG16(AX);
                   16449:                        if(code & 0xf0) {
                   16450:                                code = (code & 7) | ((code & 0x10) >> 1);
                   16451:                        }
                   16452:                        for(int i = 0; i < array_length(param_error_table); i++) {
                   16453:                                if(param_error_table[i].code == code || param_error_table[i].code == (UINT16)-1) {
                   16454:                                        const char *message = NULL;
                   16455:                                        if(active_code_page == 932) {
                   16456:                                                message = param_error_table[i].message_japanese;
                   16457:                                        }
                   16458:                                        if(message == NULL) {
                   16459:                                                message = param_error_table[i].message_english;
                   16460:                                        }
                   16461:                                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   16462:                                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   16463:                                        
                   16464:                                        SREG(ES) = WORK_TOP >> 4;
                   16465:                                        i386_load_segment_descriptor(ES);
                   16466:                                        REG16(DI) = 0x0000;
                   16467:                                        break;
                   16468:                                }
                   16469:                        }
                   16470:                }
                   16471:                break;
1.1.1.8   root     16472:        case 0x70:
                   16473:        case 0x71:
                   16474:        case 0x72:
                   16475:        case 0x73:
                   16476:        case 0x74:
                   16477:        case 0x75:
                   16478:        case 0x76:
                   16479:        case 0x77:
                   16480:                // EOI
                   16481:                if((pic[1].isr &= ~(1 << (num - 0x70))) == 0) {
                   16482:                        pic[0].isr &= ~(1 << 2); // master
                   16483:                }
                   16484:                pic_update();
                   16485:                break;
1.1       root     16486:        default:
1.1.1.22  root     16487: //             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     16488:                break;
                   16489:        }
                   16490:        
                   16491:        // update cursor position
                   16492:        if(cursor_moved) {
1.1.1.36  root     16493:                pcbios_update_cursor_position();
1.1       root     16494:                cursor_moved = false;
                   16495:        }
                   16496: }
                   16497: 
                   16498: // init
                   16499: 
                   16500: int msdos_init(int argc, char *argv[], char *envp[], int standard_env)
                   16501: {
                   16502:        // init file handler
                   16503:        memset(file_handler, 0, sizeof(file_handler));
                   16504:        msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   16505:        msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   16506:        msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
1.1.1.21  root     16507: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     16508:        if(_open("stdaux.txt", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
1.1.1.21  root     16509: #else
                   16510:        if(_open("NUL", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
                   16511: #endif
                   16512:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
1.1       root     16513:        }
1.1.1.21  root     16514:        if(_open("NUL", _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 4) {
1.1.1.45  root     16515: //             msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0, 0, 1); // LPT1
                   16516:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0x80a0, 0, 0, 1); // LPT1
1.1.1.21  root     16517:        }
1.1       root     16518:        _dup2(0, DUP_STDIN);
                   16519:        _dup2(1, DUP_STDOUT);
                   16520:        _dup2(2, DUP_STDERR);
1.1.1.21  root     16521:        _dup2(3, DUP_STDAUX);
                   16522:        _dup2(4, DUP_STDPRN);
1.1       root     16523:        
1.1.1.24  root     16524:        // init mouse
                   16525:        memset(&mouse, 0, sizeof(mouse));
1.1.1.34  root     16526:        mouse.enabled = true;   // from DOSBox
                   16527:        mouse.hidden = 1;       // hidden in default ???
                   16528:        mouse.old_hidden = 1;   // from DOSBox
                   16529:        mouse.max_position.x = 8 * (scr_width  - 1);
                   16530:        mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24  root     16531:        mouse.mickey.x = 8;
                   16532:        mouse.mickey.y = 16;
                   16533:        
1.1.1.26  root     16534: #ifdef SUPPORT_XMS
                   16535:        // init xms
                   16536:        msdos_xms_init();
                   16537: #endif
                   16538:        
1.1       root     16539:        // init process
                   16540:        memset(process, 0, sizeof(process));
                   16541:        
1.1.1.13  root     16542:        // init dtainfo
                   16543:        msdos_dta_info_init();
                   16544:        
1.1       root     16545:        // init memory
                   16546:        memset(mem, 0, sizeof(mem));
                   16547:        
                   16548:        // bios data area
1.1.1.23  root     16549:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     16550:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   16551:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.14  root     16552:        CONSOLE_FONT_INFO cfi;
                   16553:        GetCurrentConsoleFont(hStdout, FALSE, &cfi);
                   16554:        
                   16555:        int regen = min(scr_width * scr_height * 2, 0x8000);
                   16556:        text_vram_top_address = TEXT_VRAM_TOP;
                   16557:        text_vram_end_address = text_vram_top_address + regen;
                   16558:        shadow_buffer_top_address = SHADOW_BUF_TOP;
                   16559:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
                   16560:        
                   16561:        if(regen > 0x4000) {
                   16562:                regen = 0x8000;
                   16563:                vram_pages = 1;
                   16564:        } else if(regen > 0x2000) {
                   16565:                regen = 0x4000;
                   16566:                vram_pages = 2;
                   16567:        } else if(regen > 0x1000) {
                   16568:                regen = 0x2000;
                   16569:                vram_pages = 4;
                   16570:        } else {
                   16571:                regen = 0x1000;
                   16572:                vram_pages = 8;
                   16573:        }
1.1       root     16574:        
1.1.1.25  root     16575:        *(UINT16 *)(mem + 0x400) = 0x3f8; // com1 port address
                   16576:        *(UINT16 *)(mem + 0x402) = 0x2f8; // com2 port address
1.1.1.29  root     16577:        *(UINT16 *)(mem + 0x404) = 0x3e8; // com3 port address
                   16578:        *(UINT16 *)(mem + 0x406) = 0x2e8; // com4 port address
1.1.1.25  root     16579:        *(UINT16 *)(mem + 0x408) = 0x378; // lpt1 port address
1.1.1.37  root     16580:        *(UINT16 *)(mem + 0x40a) = 0x278; // lpt2 port address
                   16581:        *(UINT16 *)(mem + 0x40c) = 0x3bc; // lpt3 port address
1.1.1.32  root     16582: #ifdef EXT_BIOS_TOP
1.1.1.25  root     16583:        *(UINT16 *)(mem + 0x40e) = EXT_BIOS_TOP >> 4;
1.1.1.32  root     16584: #endif
1.1.1.26  root     16585:        *(UINT16 *)(mem + 0x410) = msdos_get_equipment();
1.1.1.33  root     16586:        *(UINT16 *)(mem + 0x413) = MEMORY_END >> 10;
1.1.1.41  root     16587:        *(UINT16 *)(mem + 0x41a) = 0x1e;
                   16588:        *(UINT16 *)(mem + 0x41c) = 0x1e;
1.1       root     16589:        *(UINT8  *)(mem + 0x449) = 0x03;//0x73;
1.1.1.14  root     16590:        *(UINT16 *)(mem + 0x44a) = csbi.dwSize.X;
                   16591:        *(UINT16 *)(mem + 0x44c) = regen;
1.1       root     16592:        *(UINT16 *)(mem + 0x44e) = 0;
                   16593:        *(UINT8  *)(mem + 0x450) = csbi.dwCursorPosition.X;
1.1.1.14  root     16594:        *(UINT8  *)(mem + 0x451) = csbi.dwCursorPosition.Y - scr_top;
1.1       root     16595:        *(UINT8  *)(mem + 0x460) = 7;
                   16596:        *(UINT8  *)(mem + 0x461) = 7;
                   16597:        *(UINT8  *)(mem + 0x462) = 0;
                   16598:        *(UINT16 *)(mem + 0x463) = 0x3d4;
1.1.1.19  root     16599:        *(UINT8  *)(mem + 0x465) = 0x09;
                   16600:        *(UINT32 *)(mem + 0x46c) = get_ticks_since_midnight(timeGetTime());
1.1.1.40  root     16601:        *(UINT16 *)(mem + 0x472) = 0x4321; // preserve memory in cpu reset
1.1.1.41  root     16602:        *(UINT16 *)(mem + 0x480) = 0x1e;
                   16603:        *(UINT16 *)(mem + 0x482) = 0x3e;
1.1.1.14  root     16604:        *(UINT8  *)(mem + 0x484) = csbi.srWindow.Bottom - csbi.srWindow.Top;
                   16605:        *(UINT8  *)(mem + 0x485) = cfi.dwFontSize.Y;
                   16606:        *(UINT8  *)(mem + 0x487) = 0x60;
                   16607:        *(UINT8  *)(mem + 0x496) = 0x10; // enhanced keyboard installed
1.1.1.32  root     16608: #ifdef EXT_BIOS_TOP
1.1.1.25  root     16609:        *(UINT16 *)(mem + EXT_BIOS_TOP) = 1;
1.1.1.32  root     16610: #endif
1.1.1.14  root     16611:        
                   16612:        // initial screen
                   16613:        SMALL_RECT rect;
                   16614:        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   16615:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   16616:        for(int y = 0, ofs1 = TEXT_VRAM_TOP, ofs2 = SHADOW_BUF_TOP; y < scr_height; y++) {
                   16617:                for(int x = 0; x < scr_width; x++) {
                   16618:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Char.AsciiChar;
                   16619:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Attributes;
                   16620:                }
                   16621:        }
1.1       root     16622:        
1.1.1.19  root     16623:        // init mcb
1.1       root     16624:        int seg = MEMORY_TOP >> 4;
1.1.1.19  root     16625:        
                   16626:        // iret table
                   16627:        // note: int 2eh vector should address the routine in command.com,
                   16628:        // and some softwares invite (int 2eh vector segment) - 1 must address the mcb of command.com.
                   16629:        // so move iret table into allocated memory block
                   16630:        // http://www5c.biglobe.ne.jp/~ecb/assembler2/2_6.html
1.1.1.33  root     16631:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, IRET_SIZE >> 4);
1.1.1.19  root     16632:        IRET_TOP = seg << 4;
                   16633:        seg += IRET_SIZE >> 4;
1.1.1.25  root     16634:        memset(mem + IRET_TOP, 0xcf, IRET_SIZE); // iret
1.1.1.19  root     16635:        
                   16636:        // dummy xms/ems device
1.1.1.33  root     16637:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, XMS_SIZE >> 4);
1.1.1.19  root     16638:        XMS_TOP = seg << 4;
                   16639:        seg += XMS_SIZE >> 4;
                   16640:        
                   16641:        // environment
1.1.1.33  root     16642:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, ENV_SIZE >> 4);
1.1       root     16643:        int env_seg = seg;
                   16644:        int ofs = 0;
1.1.1.32  root     16645:        char env_append[ENV_SIZE] = {0}, append_added = 0;
                   16646:        char comspec_added = 0;
1.1.1.33  root     16647:        char lastdrive_added = 0;
1.1.1.32  root     16648:        char env_msdos_path[ENV_SIZE] = {0};
                   16649:        char env_path[ENV_SIZE] = {0}, path_added = 0;
1.1.1.33  root     16650:        char prompt_added = 0;
1.1.1.32  root     16651:        char env_temp[ENV_SIZE] = {0}, temp_added = 0, tmp_added = 0;
1.1.1.33  root     16652:        char tz_added = 0;
1.1.1.45  root     16653:        const char *path, *short_path;
1.1.1.32  root     16654:        
                   16655:        if((path = getenv("MSDOS_APPEND")) != NULL) {
                   16656:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   16657:                        strcpy(env_append, short_path);
                   16658:                }
                   16659:        }
                   16660:        if((path = getenv("APPEND")) != NULL) {
                   16661:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   16662:                        if(env_append[0] != '\0') {
                   16663:                                strcat(env_append, ";");
                   16664:                        }
                   16665:                        strcat(env_append, short_path);
                   16666:                }
                   16667:        }
                   16668:        
                   16669:        if((path = msdos_search_command_com(argv[0], env_path)) != NULL) {
                   16670:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   16671:                        strcpy(comspec_path, short_path);
                   16672:                }
                   16673:        }
                   16674:        if((path = getenv("MSDOS_COMSPEC")) != NULL && _access(path, 0) == 0) {
                   16675:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   16676:                        strcpy(comspec_path, short_path);
                   16677:                }
                   16678:        }
1.1       root     16679:        
1.1.1.28  root     16680:        if((path = getenv("MSDOS_PATH")) != NULL) {
1.1.1.32  root     16681:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   16682:                        strcpy(env_msdos_path, short_path);
                   16683:                        strcpy(env_path, short_path);
1.1.1.14  root     16684:                }
                   16685:        }
1.1.1.28  root     16686:        if((path = getenv("PATH")) != NULL) {
1.1.1.32  root     16687:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   16688:                        if(env_path[0] != '\0') {
                   16689:                                strcat(env_path, ";");
                   16690:                        }
                   16691:                        strcat(env_path, short_path);
1.1.1.9   root     16692:                }
                   16693:        }
1.1.1.32  root     16694:        
                   16695:        if(GetTempPath(ENV_SIZE, env_temp) != 0) {
                   16696:                strcpy(env_temp, msdos_get_multiple_short_path(env_temp));
1.1.1.15  root     16697:        }
1.1.1.32  root     16698:        for(int i = 0; i < 4; i++) {
                   16699:                static const char *name[4] = {"MSDOS_TEMP", "MSDOS_TMP", "TEMP", "TMP"};
                   16700:                if((path = getenv(name[i])) != NULL && _access(path, 0) == 0) {
                   16701:                        if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   16702:                                strcpy(env_temp, short_path);
                   16703:                                break;
                   16704:                        }
                   16705:                }
1.1.1.24  root     16706:        }
1.1.1.32  root     16707:        
1.1.1.9   root     16708:        for(char **p = envp; p != NULL && *p != NULL; p++) {
1.1       root     16709:                // lower to upper
1.1.1.28  root     16710:                char tmp[ENV_SIZE], name[ENV_SIZE];
1.1       root     16711:                strcpy(tmp, *p);
                   16712:                for(int i = 0;; i++) {
                   16713:                        if(tmp[i] == '=') {
                   16714:                                tmp[i] = '\0';
                   16715:                                sprintf(name, ";%s;", tmp);
1.1.1.25  root     16716:                                my_strupr(name);
1.1       root     16717:                                tmp[i] = '=';
                   16718:                                break;
                   16719:                        } else if(tmp[i] >= 'a' && tmp[i] <= 'z') {
1.1.1.28  root     16720:                                tmp[i] = (tmp[i] - 'a') + 'A';
1.1       root     16721:                        }
                   16722:                }
1.1.1.33  root     16723:                if(strstr(";MSDOS_APPEND;MSDOS_COMSPEC;MSDOS_LASTDRIVE;MSDOS_TEMP;MSDOS_TMP;MSDOS_TZ;", name) != NULL) {
                   16724:                        // ignore MSDOS_(APPEND/COMSPEC/LASTDRIVE/TEMP/TMP/TZ)
                   16725:                } else if(standard_env && strstr(";APPEND;COMSPEC;LASTDRIVE;MSDOS_PATH;PATH;PROMPT;TEMP;TMP;TZ;", name) == NULL) {
1.1.1.18  root     16726:                        // ignore non standard environments
                   16727:                } else {
1.1.1.33  root     16728:                        if(strncmp(tmp, "APPEND=", 7) == 0) {
1.1.1.32  root     16729:                                if(env_append[0] != '\0') {
                   16730:                                        sprintf(tmp, "APPEND=%s", env_append);
                   16731:                                } else {
                   16732:                                        sprintf(tmp, "APPEND=%s", msdos_get_multiple_short_path(tmp + 7));
                   16733:                                }
                   16734:                                append_added = 1;
                   16735:                        } else if(strncmp(tmp, "COMSPEC=", 8) == 0) {
1.1.1.14  root     16736:                                strcpy(tmp, "COMSPEC=C:\\COMMAND.COM");
1.1.1.32  root     16737:                                comspec_added = 1;
1.1.1.33  root     16738:                        } else if(strncmp(tmp, "LASTDRIVE=", 10) == 0) {
                   16739:                                char *env = getenv("MSDOS_LASTDRIVE");
                   16740:                                if(env != NULL) {
                   16741:                                        sprintf(tmp, "LASTDRIVE=%s", env);
                   16742:                                }
                   16743:                                lastdrive_added = 1;
                   16744:                        } else if(strncmp(tmp, "MSDOS_PATH=", 11) == 0) {
1.1.1.28  root     16745:                                if(env_msdos_path[0] != '\0') {
                   16746:                                        sprintf(tmp, "MSDOS_PATH=%s", env_msdos_path);
                   16747:                                } else {
                   16748:                                        sprintf(tmp, "MSDOS_PATH=%s", msdos_get_multiple_short_path(tmp + 11));
                   16749:                                }
1.1.1.33  root     16750:                        } else if(strncmp(tmp, "PATH=", 5) == 0) {
1.1.1.28  root     16751:                                if(env_path[0] != '\0') {
                   16752:                                        sprintf(tmp, "PATH=%s", env_path);
                   16753:                                } else {
                   16754:                                        sprintf(tmp, "PATH=%s", msdos_get_multiple_short_path(tmp + 5));
                   16755:                                }
1.1.1.32  root     16756:                                path_added = 1;
1.1.1.33  root     16757:                        } else if(strncmp(tmp, "PROMPT=", 7) == 0) {
                   16758:                                prompt_added = 1;
1.1.1.28  root     16759:                        } else if(strncmp(tmp, "TEMP=", 5) == 0) {
                   16760:                                if(env_temp[0] != '\0') {
                   16761:                                        sprintf(tmp, "TEMP=%s", env_temp);
                   16762:                                } else {
                   16763:                                        sprintf(tmp, "TEMP=%s", msdos_get_multiple_short_path(tmp + 5));
                   16764:                                }
1.1.1.32  root     16765:                                temp_added = 1;
1.1.1.33  root     16766:                        } else if(strncmp(tmp, "TMP=", 4) == 0) {
1.1.1.28  root     16767:                                if(env_temp[0] != '\0') {
                   16768:                                        sprintf(tmp, "TMP=%s", env_temp);
                   16769:                                } else {
                   16770:                                        sprintf(tmp, "TMP=%s", msdos_get_multiple_short_path(tmp + 4));
1.1       root     16771:                                }
1.1.1.32  root     16772:                                tmp_added = 1;
1.1.1.33  root     16773:                        } else if(strncmp(tmp, "TZ=", 3) == 0) {
                   16774:                                char *env = getenv("MSDOS_TZ");
                   16775:                                if(env != NULL) {
                   16776:                                        sprintf(tmp, "TZ=%s", env);
                   16777:                                }
                   16778:                                tz_added = 1;
1.1       root     16779:                        }
                   16780:                        int len = strlen(tmp);
1.1.1.14  root     16781:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) {
1.1       root     16782:                                fatalerror("too many environments\n");
                   16783:                        }
                   16784:                        memcpy(mem + (seg << 4) + ofs, tmp, len);
                   16785:                        ofs += len + 1;
                   16786:                }
                   16787:        }
1.1.1.32  root     16788:        if(!append_added && env_append[0] != '\0') {
                   16789:                #define SET_ENV(name, value) { \
                   16790:                        char tmp[ENV_SIZE]; \
                   16791:                        sprintf(tmp, "%s=%s", name, value); \
                   16792:                        int len = strlen(tmp); \
                   16793:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) { \
                   16794:                                fatalerror("too many environments\n"); \
                   16795:                        } \
                   16796:                        memcpy(mem + (seg << 4) + ofs, tmp, len); \
                   16797:                        ofs += len + 1; \
                   16798:                }
                   16799:                SET_ENV("APPEND", env_append);
                   16800:        }
                   16801:        if(!comspec_added) {
                   16802:                SET_ENV("COMSPEC", "C:\\COMMAND.COM");
                   16803:        }
1.1.1.33  root     16804:        if(!lastdrive_added) {
                   16805:                SET_ENV("LASTDRIVE", "Z");
                   16806:        }
1.1.1.32  root     16807:        if(!path_added) {
                   16808:                SET_ENV("PATH", env_path);
                   16809:        }
1.1.1.33  root     16810:        if(!prompt_added) {
                   16811:                SET_ENV("PROMPT", "$P$G");
                   16812:        }
1.1.1.32  root     16813:        if(!temp_added) {
                   16814:                SET_ENV("TEMP", env_temp);
                   16815:        }
                   16816:        if(!tmp_added) {
                   16817:                SET_ENV("TMP", env_temp);
                   16818:        }
1.1.1.33  root     16819:        if(!tz_added) {
                   16820:                TIME_ZONE_INFORMATION tzi;
                   16821:                HKEY hKey, hSubKey;
                   16822:                char tzi_std_name[64];
                   16823:                char tz_std[8] = "GMT";
                   16824:                char tz_dlt[8] = "GST";
                   16825:                char tz_value[32];
                   16826:                
                   16827:                // timezone name from GetTimeZoneInformation may not be english
                   16828:                bool daylight = (GetTimeZoneInformation(&tzi) != TIME_ZONE_ID_UNKNOWN);
                   16829:                setlocale(LC_CTYPE, "");
                   16830:                wcstombs(tzi_std_name, tzi.StandardName, sizeof(tzi_std_name));
                   16831:                
                   16832:                // get english timezone name from registry
                   16833:                if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones", 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
                   16834:                        for(DWORD i = 0; !tz_added; i++) {
                   16835:                                char reg_name[256], sub_key[1024], std_name[256];
                   16836:                                DWORD size;
                   16837:                                FILETIME ftTime;
                   16838:                                LONG result = RegEnumKeyEx(hKey, i, reg_name, &(size = array_length(reg_name)), NULL, NULL, NULL, &ftTime);
                   16839:                                
                   16840:                                if(result == ERROR_SUCCESS) {
                   16841:                                        sprintf(sub_key, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\%s", reg_name);
                   16842:                                        if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, sub_key, 0, KEY_QUERY_VALUE, &hSubKey) == ERROR_SUCCESS) {
                   16843:                                                if(RegQueryValueEx(hSubKey, "Std", NULL, NULL, (LPBYTE)std_name, &(size = array_length(std_name))) == ERROR_SUCCESS) {
                   16844:                                                        // search english timezone name from table
1.1.1.37  root     16845:                                                        if(strcmp(std_name, tzi_std_name) == 0) {
1.1.1.33  root     16846:                                                                for(int j = 0; j < array_length(tz_table); j++) {
                   16847:                                                                        if(strcmp(reg_name, tz_table[j].name) == 0 && (tz_table[j].lcid == 0 || tz_table[j].lcid == GetUserDefaultLCID())) {
                   16848:                                                                                if(tz_table[j].std != NULL) {
                   16849:                                                                                        strcpy(tz_std, tz_table[j].std);
                   16850:                                                                                }
                   16851:                                                                                if(tz_table[j].dlt != NULL) {
                   16852:                                                                                        strcpy(tz_dlt, tz_table[j].dlt);
                   16853:                                                                                }
                   16854:                                                                                tz_added = 1;
                   16855:                                                                                break;
                   16856:                                                                        }
                   16857:                                                                }
                   16858:                                                        }
                   16859:                                                }
                   16860:                                                RegCloseKey(hSubKey);
                   16861:                                        }
                   16862:                                } else if(result == ERROR_NO_MORE_ITEMS) {
                   16863:                                        break;
                   16864:                                }
                   16865:                        }
                   16866:                        RegCloseKey(hKey);
                   16867:                }
                   16868:                if((tzi.Bias % 60) != 0) {
                   16869:                        sprintf(tz_value, "%s%d:%2d", tz_std, tzi.Bias / 60, abs(tzi.Bias % 60));
                   16870:                } else {
                   16871:                        sprintf(tz_value, "%s%d", tz_std, tzi.Bias / 60);
                   16872:                }
                   16873:                if(daylight) {
                   16874:                        strcat(tz_value, tz_dlt);
                   16875:                }
                   16876:                SET_ENV("TZ", tz_value);
                   16877:        }
1.1       root     16878:        seg += (ENV_SIZE >> 4);
                   16879:        
                   16880:        // psp
1.1.1.33  root     16881:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, PSP_SIZE >> 4);
1.1       root     16882:        current_psp = seg;
1.1.1.35  root     16883:        psp_t *psp = msdos_psp_create(seg, seg + (PSP_SIZE >> 4), -1, env_seg);
                   16884:        psp->parent_psp = current_psp;
1.1       root     16885:        seg += (PSP_SIZE >> 4);
                   16886:        
1.1.1.19  root     16887:        // first free mcb in conventional memory
1.1.1.33  root     16888:        msdos_mcb_create(seg, 'M', 0, (MEMORY_END >> 4) - seg - 2);
1.1.1.8   root     16889:        first_mcb = seg;
                   16890:        
1.1.1.19  root     16891:        // dummy mcb to link to umb
1.1.1.33  root     16892: #if 0
1.1.1.39  root     16893:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC"); // link umb
1.1.1.33  root     16894: #else
1.1.1.39  root     16895:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC"); // unlink umb
1.1.1.33  root     16896: #endif
1.1.1.19  root     16897:        
                   16898:        // first free mcb in upper memory block
1.1.1.8   root     16899:        msdos_mcb_create(UMB_TOP >> 4, 'Z', 0, (UMB_END >> 4) - (UMB_TOP >> 4) - 1);
1.1       root     16900:        
1.1.1.29  root     16901: #ifdef SUPPORT_HMA
                   16902:        // first free mcb in high memory area
                   16903:        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   16904: #endif
                   16905:        
1.1.1.26  root     16906:        // interrupt vector
                   16907:        for(int i = 0; i < 0x80; i++) {
                   16908:                *(UINT16 *)(mem + 4 * i + 0) = i;
                   16909:                *(UINT16 *)(mem + 4 * i + 2) = (IRET_TOP >> 4);
                   16910:        }
1.1.1.35  root     16911:        *(UINT16 *)(mem + 4 * 0x08 + 0) = 0x0018;       // fffd:0018 irq0 (system timer)
1.1.1.26  root     16912:        *(UINT16 *)(mem + 4 * 0x08 + 2) = 0xfffd;
                   16913:        *(UINT16 *)(mem + 4 * 0x22 + 0) = 0x0000;       // ffff:0000 boot
                   16914:        *(UINT16 *)(mem + 4 * 0x22 + 2) = 0xffff;
                   16915:        *(UINT16 *)(mem + 4 * 0x67 + 0) = 0x0012;       // xxxx:0012 ems
                   16916:        *(UINT16 *)(mem + 4 * 0x67 + 2) = XMS_TOP >> 4;
                   16917:        *(UINT16 *)(mem + 4 * 0x74 + 0) = 0x0000;       // fffd:0000 irq12 (mouse)
                   16918:        *(UINT16 *)(mem + 4 * 0x74 + 2) = 0xfffd;
                   16919:        
1.1.1.29  root     16920:        // dummy devices (NUL -> CON -> ... -> CONFIG$ -> EMMXXXX0)
1.1.1.26  root     16921:        static const struct {
                   16922:                UINT16 attributes;
                   16923:                char *dev_name;
                   16924:        } dummy_devices[] = {
                   16925:                {0x8013, "CON     "},
                   16926:                {0x8000, "AUX     "},
                   16927:                {0xa0c0, "PRN     "},
                   16928:                {0x8008, "CLOCK$  "},
                   16929:                {0x8000, "COM1    "},
                   16930:                {0xa0c0, "LPT1    "},
                   16931:                {0xa0c0, "LPT2    "},
                   16932:                {0xa0c0, "LPT3    "},
                   16933:                {0x8000, "COM2    "},
                   16934:                {0x8000, "COM3    "},
                   16935:                {0x8000, "COM4    "},
1.1.1.30  root     16936: //             {0xc000, "CONFIG$ "},
                   16937:                {0xc000, "$IBMADSP"}, // for windows3.1 setup.exe
1.1.1.26  root     16938:        };
                   16939:        static const UINT8 dummy_device_routine[] = {
                   16940:                // from NUL device of Windows 98 SE
                   16941:                // or word ptr ES:[BX+03],0100
                   16942:                0x26, 0x81, 0x4f, 0x03, 0x00, 0x01,
                   16943:                // retf
                   16944:                0xcb,
                   16945:        };
1.1.1.29  root     16946:        device_t *last = NULL;
1.1.1.32  root     16947:        for(int i = 0; i < array_length(dummy_devices); i++) {
1.1.1.26  root     16948:                device_t *device = (device_t *)(mem + DEVICE_TOP + 22 + 18 * i);
1.1.1.29  root     16949:                device->next_driver.w.l = 22 + 18 * (i + 1);
                   16950:                device->next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     16951:                device->attributes = dummy_devices[i].attributes;
1.1.1.29  root     16952:                device->strategy = DEVICE_SIZE - sizeof(dummy_device_routine);
                   16953:                device->interrupt = DEVICE_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.26  root     16954:                memcpy(device->dev_name, dummy_devices[i].dev_name, 8);
1.1.1.29  root     16955:                last = device;
                   16956:        }
                   16957:        if(last != NULL) {
                   16958:                last->next_driver.w.l = 0;
                   16959:                last->next_driver.w.h = XMS_TOP >> 4;
1.1.1.26  root     16960:        }
1.1.1.29  root     16961:        memcpy(mem + DEVICE_TOP + DEVICE_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.26  root     16962:        
1.1.1.25  root     16963:        // dos info
                   16964:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   16965:        dos_info->magic_word = 1;
                   16966:        dos_info->first_mcb = MEMORY_TOP >> 4;
                   16967:        dos_info->first_dpb.w.l = 0;
                   16968:        dos_info->first_dpb.w.h = DPB_TOP >> 4;
                   16969:        dos_info->first_sft.w.l = 0;
                   16970:        dos_info->first_sft.w.h = SFT_TOP >> 4;
1.1.1.41  root     16971:        dos_info->clock_device.w.l = 22 + 18 * 3;       // CLOCK$ is the 4th device in IO.SYS
1.1.1.25  root     16972:        dos_info->clock_device.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     16973:        dos_info->con_device.w.l = 22 + 18 * 0;         // CON is the 1st device in IO.SYS
1.1.1.25  root     16974:        dos_info->con_device.w.h = DEVICE_TOP >> 4;
                   16975:        dos_info->max_sector_len = 512;
                   16976:        dos_info->disk_buf_info.w.l = offsetof(dos_info_t, disk_buf_heads);
                   16977:        dos_info->disk_buf_info.w.h = DOS_INFO_TOP >> 4;
                   16978:        dos_info->cds.w.l = 0;
                   16979:        dos_info->cds.w.h = CDS_TOP >> 4;
                   16980:        dos_info->fcb_table.w.l = 0;
                   16981:        dos_info->fcb_table.w.h = FCB_TABLE_TOP >> 4;
                   16982:        dos_info->last_drive = 'Z' - 'A' + 1;
                   16983:        dos_info->buffers_x = 20;
                   16984:        dos_info->buffers_y = 0;
                   16985:        dos_info->boot_drive = 'C' - 'A' + 1;
1.1.1.29  root     16986:        dos_info->nul_device.next_driver.w.l = 22;
                   16987:        dos_info->nul_device.next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.25  root     16988:        dos_info->nul_device.attributes = 0x8004;
1.1.1.29  root     16989:        dos_info->nul_device.strategy = DOS_INFO_SIZE - sizeof(dummy_device_routine);
                   16990:        dos_info->nul_device.interrupt = DOS_INFO_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     16991:        memcpy(dos_info->nul_device.dev_name, "NUL     ", 8);
                   16992:        dos_info->disk_buf_heads.w.l = 0;
                   16993:        dos_info->disk_buf_heads.w.h = DISK_BUF_TOP >> 4;
1.1.1.39  root     16994:        dos_info->umb_linked = (((mcb_t *)(mem + MEMORY_END - 16))->mz == 'M' ? 0x01 : 0x00);
1.1.1.25  root     16995:        dos_info->first_umb_fcb = UMB_TOP >> 4;
                   16996:        dos_info->first_mcb_2 = MEMORY_TOP >> 4;
1.1.1.29  root     16997:        memcpy(mem + DOS_INFO_TOP + DOS_INFO_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.25  root     16998:        
                   16999:        char *env;
                   17000:        if((env = getenv("LASTDRIVE")) != NULL) {
                   17001:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   17002:                        dos_info->last_drive = env[0] - 'A' + 1;
                   17003:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   17004:                        dos_info->last_drive = env[0] - 'a' + 1;
                   17005:                }
                   17006:        }
                   17007:        if((env = getenv("windir")) != NULL) {
                   17008:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   17009:                        dos_info->boot_drive = env[0] - 'A' + 1;
                   17010:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   17011:                        dos_info->boot_drive = env[0] - 'a' + 1;
                   17012:                }
                   17013:        }
                   17014: #if defined(HAS_I386)
                   17015:        dos_info->i386_or_later = 1;
                   17016: #else
                   17017:        dos_info->i386_or_later = 0;
                   17018: #endif
                   17019:        dos_info->ext_mem_size = (min(MAX_MEM, 0x4000000) - 0x100000) >> 10;
                   17020:        
1.1.1.27  root     17021:        // ems (int 67h) and xms
1.1.1.25  root     17022:        device_t *xms_device = (device_t *)(mem + XMS_TOP);
                   17023:        xms_device->next_driver.w.l = 0xffff;
                   17024:        xms_device->next_driver.w.h = 0xffff;
                   17025:        xms_device->attributes = 0xc000;
1.1.1.29  root     17026:        xms_device->strategy = XMS_SIZE - sizeof(dummy_device_routine);
                   17027:        xms_device->interrupt = XMS_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     17028:        memcpy(xms_device->dev_name, "EMMXXXX0", 8);
                   17029:        
1.1.1.26  root     17030:        mem[XMS_TOP + 0x12] = 0xcd;     // int 68h (dummy)
                   17031:        mem[XMS_TOP + 0x13] = 0x68;
                   17032:        mem[XMS_TOP + 0x14] = 0xcf;     // iret
1.1.1.19  root     17033: #ifdef SUPPORT_XMS
                   17034:        if(support_xms) {
1.1.1.26  root     17035:                mem[XMS_TOP + 0x15] = 0xcd;     // int 69h (dummy)
                   17036:                mem[XMS_TOP + 0x16] = 0x69;
                   17037:                mem[XMS_TOP + 0x17] = 0xcb;     // retf
1.1.1.19  root     17038:        } else
                   17039: #endif
1.1.1.26  root     17040:        mem[XMS_TOP + 0x15] = 0xcb;     // retf
1.1.1.29  root     17041:        memcpy(mem + XMS_TOP + XMS_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.19  root     17042:        
1.1.1.26  root     17043:        // irq12 routine (mouse)
1.1.1.24  root     17044:        mem[0xfffd0 + 0x00] = 0xcd;     // int 6ah (dummy)
                   17045:        mem[0xfffd0 + 0x01] = 0x6a;
                   17046:        mem[0xfffd0 + 0x02] = 0x9a;     // call far mouse
                   17047:        mem[0xfffd0 + 0x03] = 0xff;
                   17048:        mem[0xfffd0 + 0x04] = 0xff;
                   17049:        mem[0xfffd0 + 0x05] = 0xff;
                   17050:        mem[0xfffd0 + 0x06] = 0xff;
                   17051:        mem[0xfffd0 + 0x07] = 0xcd;     // int 6bh (dummy)
                   17052:        mem[0xfffd0 + 0x08] = 0x6b;
                   17053:        mem[0xfffd0 + 0x09] = 0xcf;     // iret
                   17054:        
1.1.1.27  root     17055:        // case map routine
                   17056:        mem[0xfffd0 + 0x0a] = 0xcd;     // int 6ch (dummy)
                   17057:        mem[0xfffd0 + 0x0b] = 0x6c;
                   17058:        mem[0xfffd0 + 0x0c] = 0xcb;     // retf
                   17059:        
                   17060:        // font read routine
                   17061:        mem[0xfffd0 + 0x0d] = 0xcd;     // int 6dh (dummy)
                   17062:        mem[0xfffd0 + 0x0e] = 0x6d;
                   17063:        mem[0xfffd0 + 0x0f] = 0xcb;     // retf
1.1.1.19  root     17064:        
1.1.1.32  root     17065:        // error message read routine
                   17066:        mem[0xfffd0 + 0x10] = 0xcd;     // int 6eh (dummy)
                   17067:        mem[0xfffd0 + 0x11] = 0x6e;
                   17068:        mem[0xfffd0 + 0x12] = 0xcb;     // retf
                   17069:        
1.1.1.35  root     17070:        // dummy loop to wait BIOS/DOS service is done
                   17071:        mem[0xfffd0 + 0x13] = 0xe6;     // out f7h, al
                   17072:        mem[0xfffd0 + 0x14] = 0xf7;
                   17073:        mem[0xfffd0 + 0x15] = 0x78;     // js/jns -4
                   17074:        mem[0xfffd0 + 0x16] = 0xfc;
                   17075:        mem[0xfffd0 + 0x17] = 0xcb;     // retf
                   17076:        
1.1.1.26  root     17077:        // irq0 routine (system time)
1.1.1.35  root     17078:        mem[0xfffd0 + 0x18] = 0xcd;     // int 1ch
                   17079:        mem[0xfffd0 + 0x19] = 0x1c;
                   17080:        mem[0xfffd0 + 0x1a] = 0xea;     // jmp far (IRET_TOP >> 4):0008
                   17081:        mem[0xfffd0 + 0x1b] = 0x08;
                   17082:        mem[0xfffd0 + 0x1c] = 0x00;
                   17083:        mem[0xfffd0 + 0x1d] = ((IRET_TOP >> 4)     ) & 0xff;
                   17084:        mem[0xfffd0 + 0x1e] = ((IRET_TOP >> 4) >> 8) & 0xff;
1.1.1.14  root     17085:        
1.1.1.26  root     17086:        // boot routine
1.1       root     17087:        mem[0xffff0] = 0xf4;    // halt
                   17088:        
1.1.1.24  root     17089:        mem[0xffff5] = '0';     // rom date
                   17090:        mem[0xffff6] = '2';
                   17091:        mem[0xffff7] = '/';
                   17092:        mem[0xffff8] = '2';
                   17093:        mem[0xffff9] = '2';
                   17094:        mem[0xffffa] = '/';
                   17095:        mem[0xffffb] = '0';
                   17096:        mem[0xffffc] = '6';
                   17097:        mem[0xffffe] = 0xfc;    // machine id
                   17098:        mem[0xfffff] = 0x00;
                   17099:        
1.1       root     17100:        // param block
                   17101:        // + 0: param block (22bytes)
                   17102:        // +24: fcb1/2 (20bytes)
                   17103:        // +44: command tail (128bytes)
                   17104:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   17105:        param->env_seg = 0;
                   17106:        param->cmd_line.w.l = 44;
                   17107:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   17108:        param->fcb1.w.l = 24;
                   17109:        param->fcb1.w.h = (WORK_TOP >> 4);
                   17110:        param->fcb2.w.l = 24;
                   17111:        param->fcb2.w.h = (WORK_TOP >> 4);
                   17112:        
                   17113:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   17114:        
                   17115:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   17116:        if(argc > 1) {
                   17117:                sprintf(cmd_line->cmd, " %s", argv[1]);
                   17118:                for(int i = 2; i < argc; i++) {
                   17119:                        char tmp[128];
                   17120:                        sprintf(tmp, "%s %s", cmd_line->cmd, argv[i]);
                   17121:                        strcpy(cmd_line->cmd, tmp);
                   17122:                }
                   17123:                cmd_line->len = (UINT8)strlen(cmd_line->cmd);
                   17124:        } else {
                   17125:                cmd_line->len = 0;
                   17126:        }
                   17127:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   17128:        
                   17129:        // system file table
1.1.1.21  root     17130:        *(UINT32 *)(mem + SFT_TOP + 0) = 0xffffffff;
                   17131:        *(UINT16 *)(mem + SFT_TOP + 4) = 20;
1.1       root     17132:        
1.1.1.19  root     17133:        // disk buffer header (from DOSBox)
                   17134:        *(UINT16 *)(mem + DISK_BUF_TOP +  0) = 0xffff;          // forward ptr
                   17135:        *(UINT16 *)(mem + DISK_BUF_TOP +  2) = 0xffff;          // backward ptr
                   17136:        *(UINT8  *)(mem + DISK_BUF_TOP +  4) = 0xff;            // not in use
                   17137:        *(UINT8  *)(mem + DISK_BUF_TOP + 10) = 0x01;            // number of FATs
                   17138:        *(UINT32 *)(mem + DISK_BUF_TOP + 13) = 0xffffffff;      // pointer to DPB
                   17139:        
1.1       root     17140:        // fcb table
                   17141:        *(UINT32 *)(mem + FCB_TABLE_TOP + 0) = 0xffffffff;
1.1.1.14  root     17142:        *(UINT16 *)(mem + FCB_TABLE_TOP + 4) = 0;
1.1       root     17143:        
1.1.1.41  root     17144:        // drive parameter block
1.1.1.42  root     17145:        for(int i = 0; i < 2; i++) {
1.1.1.43  root     17146:                // may be a floppy drive
1.1.1.44  root     17147:                cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * i);
                   17148:                sprintf(cds->path_name, "%c:\\", 'A' + i);
                   17149:                cds->drive_attrib = 0x4000;     // physical drive
                   17150:                cds->dpb_ptr.w.l = sizeof(dpb_t) * i;
                   17151:                cds->dpb_ptr.w.h = DPB_TOP >> 4;
                   17152:                cds->word_1 = cds->word_2 = cds->word_3 = 0xffff;
                   17153:                cds->bs_offset = 2;
                   17154:                
1.1.1.41  root     17155:                dpb_t *dpb = (dpb_t *)(mem + DPB_TOP + sizeof(dpb_t) * i);
                   17156:                dpb->drive_num = i;
                   17157:                dpb->unit_num = i;
1.1.1.43  root     17158:                dpb->next_dpb_ofs = /*(i == 25) ? 0xffff : */sizeof(dpb_t) * (i + 1);
                   17159:                dpb->next_dpb_seg = /*(i == 25) ? 0xffff : */DPB_TOP >> 4;
1.1.1.42  root     17160:        }
                   17161:        for(int i = 2; i < 26; i++) {
1.1.1.44  root     17162:                msdos_cds_update(i);
1.1.1.42  root     17163:                UINT16 seg, ofs;
                   17164:                msdos_drive_param_block_update(i, &seg, &ofs, 1);
1.1.1.41  root     17165:        }
                   17166:        
1.1.1.17  root     17167:        // nls stuff
                   17168:        msdos_nls_tables_init();
1.1       root     17169:        
                   17170:        // execute command
1.1.1.28  root     17171:        try {
                   17172:                if(msdos_process_exec(argv[0], param, 0)) {
                   17173:                        fatalerror("'%s' not found\n", argv[0]);
                   17174:                }
                   17175:        } catch(...) {
                   17176:                // we should not reach here :-(
                   17177:                fatalerror("failed to start '%s' because of unexpected exeption\n", argv[0]);
1.1       root     17178:        }
                   17179:        retval = 0;
                   17180:        return(0);
                   17181: }
                   17182: 
                   17183: #define remove_std_file(path) { \
                   17184:        int fd = _open(path, _O_RDONLY | _O_BINARY); \
                   17185:        if(fd != -1) { \
                   17186:                _lseek(fd, 0, SEEK_END); \
                   17187:                int size = _tell(fd); \
                   17188:                _close(fd); \
                   17189:                if(size == 0) { \
                   17190:                        remove(path); \
                   17191:                } \
                   17192:        } \
                   17193: }
                   17194: 
                   17195: void msdos_finish()
                   17196: {
                   17197:        for(int i = 0; i < MAX_FILES; i++) {
                   17198:                if(file_handler[i].valid) {
                   17199:                        _close(i);
                   17200:                }
                   17201:        }
1.1.1.21  root     17202: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     17203:        remove_std_file("stdaux.txt");
1.1.1.21  root     17204: #endif
1.1.1.30  root     17205: #ifdef SUPPORT_XMS
                   17206:        msdos_xms_finish();
                   17207: #endif
1.1       root     17208:        msdos_dbcs_table_finish();
                   17209: }
                   17210: 
                   17211: /* ----------------------------------------------------------------------------
                   17212:        PC/AT hardware emulation
                   17213: ---------------------------------------------------------------------------- */
                   17214: 
                   17215: void hardware_init()
                   17216: {
1.1.1.3   root     17217:        CPU_INIT_CALL(CPU_MODEL);
1.1       root     17218:        CPU_RESET_CALL(CPU_MODEL);
1.1.1.14  root     17219:        m_IF = 1;
1.1.1.3   root     17220: #if defined(HAS_I386)
1.1       root     17221:        cpu_type = (REG32(EDX) >> 8) & 0x0f;
                   17222:        cpu_step = (REG32(EDX) >> 0) & 0x0f;
1.1.1.3   root     17223: #endif
                   17224:        i386_set_a20_line(0);
1.1.1.14  root     17225:        
1.1.1.19  root     17226:        ems_init();
1.1.1.25  root     17227:        dma_init();
1.1       root     17228:        pic_init();
1.1.1.25  root     17229:        pio_init();
1.1.1.8   root     17230: #ifdef PIT_ALWAYS_RUNNING
                   17231:        pit_init();
                   17232: #else
1.1       root     17233:        pit_active = 0;
                   17234: #endif
1.1.1.25  root     17235:        sio_init();
1.1.1.8   root     17236:        cmos_init();
                   17237:        kbd_init();
1.1       root     17238: }
                   17239: 
1.1.1.10  root     17240: void hardware_finish()
                   17241: {
                   17242: #if defined(HAS_I386)
                   17243:        vtlb_free(m_vtlb);
                   17244: #endif
1.1.1.19  root     17245:        ems_finish();
1.1.1.37  root     17246:        pio_finish();
1.1.1.25  root     17247:        sio_finish();
1.1.1.10  root     17248: }
                   17249: 
1.1.1.28  root     17250: void hardware_release()
                   17251: {
                   17252:        // release hardware resources when this program will be terminated abnormally
                   17253: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     17254:        if(fp_debug_log != NULL) {
                   17255:                fclose(fp_debug_log);
                   17256:                fp_debug_log = NULL;
1.1.1.28  root     17257:        }
                   17258: #endif
                   17259: #if defined(HAS_I386)
                   17260:        vtlb_free(m_vtlb);
                   17261: #endif
                   17262:        ems_release();
1.1.1.37  root     17263:        pio_release();
1.1.1.28  root     17264:        sio_release();
                   17265: }
                   17266: 
1.1       root     17267: void hardware_run()
                   17268: {
1.1.1.22  root     17269: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.28  root     17270:        // open debug log file after msdos_init() is done not to use the standard file handlers
1.1.1.33  root     17271:        fp_debug_log = fopen("debug.log", "w");
1.1.1.22  root     17272: #endif
1.1.1.3   root     17273:        while(!m_halted) {
                   17274: #if defined(HAS_I386)
1.1       root     17275:                CPU_EXECUTE_CALL(i386);
1.1.1.14  root     17276:                if(m_eip != m_prev_eip) {
1.1.1.35  root     17277:                        idle_ops++;
                   17278:                }
1.1.1.14  root     17279: #else
1.1.1.35  root     17280:                CPU_EXECUTE_CALL(CPU_MODEL);
1.1.1.14  root     17281:                if(m_pc != m_prevpc) {
1.1.1.35  root     17282:                        idle_ops++;
1.1.1.14  root     17283:                }
1.1.1.35  root     17284: #endif
                   17285:                if(++update_ops == UPDATE_OPS) {
1.1       root     17286:                        hardware_update();
1.1.1.35  root     17287:                        update_ops = 0;
1.1       root     17288:                }
                   17289:        }
1.1.1.22  root     17290: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     17291:        if(fp_debug_log != NULL) {
                   17292:                fclose(fp_debug_log);
                   17293:                fp_debug_log = NULL;
1.1.1.28  root     17294:        }
1.1.1.22  root     17295: #endif
1.1       root     17296: }
                   17297: 
                   17298: void hardware_update()
                   17299: {
1.1.1.8   root     17300:        static UINT32 prev_time = 0;
                   17301:        UINT32 cur_time = timeGetTime();
                   17302:        
                   17303:        if(prev_time != cur_time) {
                   17304:                // update pit and raise irq0
                   17305: #ifndef PIT_ALWAYS_RUNNING
                   17306:                if(pit_active)
                   17307: #endif
                   17308:                {
                   17309:                        if(pit_run(0, cur_time)) {
                   17310:                                pic_req(0, 0, 1);
                   17311:                        }
                   17312:                        pit_run(1, cur_time);
                   17313:                        pit_run(2, cur_time);
                   17314:                }
1.1.1.24  root     17315:                
1.1.1.25  root     17316:                // update sio and raise irq4/3
1.1.1.29  root     17317:                for(int c = 0; c < 4; c++) {
1.1.1.25  root     17318:                        sio_update(c);
                   17319:                }
                   17320:                
1.1.1.24  root     17321:                // update keyboard and mouse
1.1.1.14  root     17322:                static UINT32 prev_tick = 0;
                   17323:                UINT32 cur_tick = cur_time / 32;
1.1.1.25  root     17324:                
1.1.1.14  root     17325:                if(prev_tick != cur_tick) {
                   17326:                        // update keyboard flags
                   17327:                        UINT8 state;
1.1.1.24  root     17328:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x0001) ? 0x80 : 0;
                   17329:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x0001) ? 0x40 : 0;
                   17330:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x0001) ? 0x20 : 0;
                   17331:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x0001) ? 0x10 : 0;
                   17332:                        state |= (GetAsyncKeyState(VK_MENU    ) & 0x8000) ? 0x08 : 0;
                   17333:                        state |= (GetAsyncKeyState(VK_CONTROL ) & 0x8000) ? 0x04 : 0;
                   17334:                        state |= (GetAsyncKeyState(VK_LSHIFT  ) & 0x8000) ? 0x02 : 0;
                   17335:                        state |= (GetAsyncKeyState(VK_RSHIFT  ) & 0x8000) ? 0x01 : 0;
1.1.1.14  root     17336:                        mem[0x417] = state;
                   17337:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x8000) ? 0x80 : 0;
                   17338:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   17339:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   17340:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
1.1.1.24  root     17341: //                     state |= (GetAsyncKeyState(VK_PAUSE   ) & 0x0001) ? 0x08 : 0;
                   17342: //                     state |= (GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x04 : 0;
1.1.1.14  root     17343:                        state |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   17344:                        state |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   17345:                        mem[0x418] = state;
                   17346:                        
1.1.1.24  root     17347:                        // update console input if needed
1.1.1.34  root     17348:                        if(!key_changed || mouse.hidden == 0) {
1.1.1.24  root     17349:                                update_console_input();
                   17350:                        }
                   17351:                        
                   17352:                        // raise irq1 if key is pressed/released
                   17353:                        if(key_changed) {
1.1.1.8   root     17354:                                pic_req(0, 1, 1);
1.1.1.24  root     17355:                                key_changed = false;
                   17356:                        }
                   17357:                        
                   17358:                        // raise irq12 if mouse status is changed
1.1.1.43  root     17359:                        if((mouse.status & mouse.call_mask) && mouse.call_addr.dw) {
                   17360:                                mouse.status_irq = mouse.status & mouse.call_mask;
                   17361:                                mouse.status_irq_alt = 0; // ???
1.1.1.24  root     17362:                                mouse.status &= ~mouse.call_mask;
1.1.1.43  root     17363:                                pic_req(1, 4, 1);
                   17364:                        } else {
                   17365:                                for(int i = 0; i < 8; i++) {
                   17366:                                        if((mouse.status_alt & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   17367:                                                mouse.status_irq = 0; // ???
                   17368:                                                mouse.status_irq_alt = 0;
                   17369:                                                for(int j = 0; j < 8; j++) {
                   17370:                                                        if((mouse.status_alt & (1 << j)) && mouse.call_addr_alt[i].dw == mouse.call_addr_alt[j].dw) {
                   17371:                                                                mouse.status_irq_alt |=  (1 << j);
                   17372:                                                                mouse.status_alt     &= ~(1 << j);
                   17373:                                                        }
                   17374:                                                }
                   17375:                                                pic_req(1, 4, 1);
                   17376:                                                break;
                   17377:                                        }
                   17378:                                }
1.1.1.8   root     17379:                        }
1.1.1.24  root     17380:                        
1.1.1.14  root     17381:                        prev_tick = cur_tick;
1.1.1.8   root     17382:                }
1.1.1.24  root     17383:                
1.1.1.19  root     17384:                // update daily timer counter
                   17385:                pcbios_update_daily_timer_counter(cur_time);
1.1.1.25  root     17386:                
1.1.1.8   root     17387:                prev_time = cur_time;
1.1       root     17388:        }
                   17389: }
                   17390: 
1.1.1.19  root     17391: // ems
                   17392: 
                   17393: void ems_init()
                   17394: {
                   17395:        memset(ems_handles, 0, sizeof(ems_handles));
                   17396:        memset(ems_pages, 0, sizeof(ems_pages));
                   17397:        free_ems_pages = MAX_EMS_PAGES;
                   17398: }
                   17399: 
                   17400: void ems_finish()
                   17401: {
1.1.1.28  root     17402:        ems_release();
                   17403: }
                   17404: 
                   17405: void ems_release()
                   17406: {
1.1.1.31  root     17407:        for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.28  root     17408:                if(ems_handles[i].buffer != NULL) {
1.1.1.19  root     17409:                        free(ems_handles[i].buffer);
                   17410:                        ems_handles[i].buffer = NULL;
                   17411:                }
                   17412:        }
                   17413: }
                   17414: 
                   17415: void ems_allocate_pages(int handle, int pages)
                   17416: {
                   17417:        if(pages > 0) {
                   17418:                ems_handles[handle].buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   17419:        } else {
                   17420:                ems_handles[handle].buffer = NULL;
                   17421:        }
                   17422:        ems_handles[handle].pages = pages;
                   17423:        ems_handles[handle].allocated = true;
                   17424:        free_ems_pages -= pages;
                   17425: }
                   17426: 
                   17427: void ems_reallocate_pages(int handle, int pages)
                   17428: {
                   17429:        if(ems_handles[handle].allocated) {
                   17430:                if(ems_handles[handle].pages != pages) {
                   17431:                        UINT8 *new_buffer = NULL;
                   17432:                        
                   17433:                        if(pages > 0) {
                   17434:                                new_buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   17435:                        }
1.1.1.32  root     17436:                        if(ems_handles[handle].buffer != NULL) {
                   17437:                                if(new_buffer != NULL) {
1.1.1.19  root     17438:                                        if(pages > ems_handles[handle].pages) {
                   17439:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * ems_handles[handle].pages);
                   17440:                                        } else {
                   17441:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * pages);
                   17442:                                        }
                   17443:                                }
                   17444:                                free(ems_handles[handle].buffer);
                   17445:                                ems_handles[handle].buffer = NULL;
                   17446:                        }
                   17447:                        free_ems_pages += ems_handles[handle].pages;
                   17448:                        
                   17449:                        ems_handles[handle].buffer = new_buffer;
                   17450:                        ems_handles[handle].pages = pages;
                   17451:                        free_ems_pages -= pages;
                   17452:                }
                   17453:        } else {
                   17454:                ems_allocate_pages(handle, pages);
                   17455:        }
                   17456: }
                   17457: 
                   17458: void ems_release_pages(int handle)
                   17459: {
                   17460:        if(ems_handles[handle].allocated) {
1.1.1.32  root     17461:                if(ems_handles[handle].buffer != NULL) {
1.1.1.19  root     17462:                        free(ems_handles[handle].buffer);
                   17463:                        ems_handles[handle].buffer = NULL;
                   17464:                }
                   17465:                free_ems_pages += ems_handles[handle].pages;
                   17466:                ems_handles[handle].allocated = false;
                   17467:        }
                   17468: }
                   17469: 
                   17470: void ems_map_page(int physical, int handle, int logical)
                   17471: {
                   17472:        if(ems_pages[physical].mapped) {
                   17473:                if(ems_pages[physical].handle == handle && ems_pages[physical].page == logical) {
                   17474:                        return;
                   17475:                }
                   17476:                ems_unmap_page(physical);
                   17477:        }
1.1.1.32  root     17478:        if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     17479:                memcpy(mem + EMS_TOP + 0x4000 * physical, ems_handles[handle].buffer + 0x4000 * logical, 0x4000);
                   17480:        }
                   17481:        ems_pages[physical].handle = handle;
                   17482:        ems_pages[physical].page = logical;
                   17483:        ems_pages[physical].mapped = true;
                   17484: }
                   17485: 
                   17486: void ems_unmap_page(int physical)
                   17487: {
                   17488:        if(ems_pages[physical].mapped) {
                   17489:                int handle = ems_pages[physical].handle;
                   17490:                int logical = ems_pages[physical].page;
                   17491:                
1.1.1.32  root     17492:                if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     17493:                        memcpy(ems_handles[handle].buffer + 0x4000 * logical, mem + EMS_TOP + 0x4000 * physical, 0x4000);
                   17494:                }
                   17495:                ems_pages[physical].mapped = false;
                   17496:        }
                   17497: }
                   17498: 
1.1.1.25  root     17499: // dma
1.1       root     17500: 
1.1.1.25  root     17501: void dma_init()
1.1       root     17502: {
1.1.1.26  root     17503:        memset(dma, 0, sizeof(dma));
1.1.1.25  root     17504:        for(int c = 0; c < 2; c++) {
1.1.1.26  root     17505: //             for(int ch = 0; ch < 4; ch++) {
                   17506: //                     dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w = 0xffff;
                   17507: //             }
1.1.1.25  root     17508:                dma_reset(c);
                   17509:        }
1.1       root     17510: }
                   17511: 
1.1.1.25  root     17512: void dma_reset(int c)
1.1       root     17513: {
1.1.1.25  root     17514:        dma[c].low_high = false;
                   17515:        dma[c].cmd = dma[c].req = dma[c].tc = 0;
                   17516:        dma[c].mask = 0xff;
                   17517: }
                   17518: 
                   17519: void dma_write(int c, UINT32 addr, UINT8 data)
                   17520: {
                   17521:        int ch = (addr >> 1) & 3;
                   17522:        UINT8 bit = 1 << (data & 3);
                   17523:        
                   17524:        switch(addr & 0x0f) {
                   17525:        case 0x00: case 0x02: case 0x04: case 0x06:
                   17526:                if(dma[c].low_high) {
                   17527:                        dma[c].ch[ch].bareg.b.h = data;
1.1       root     17528:                } else {
1.1.1.25  root     17529:                        dma[c].ch[ch].bareg.b.l = data;
                   17530:                }
                   17531:                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   17532:                dma[c].low_high = !dma[c].low_high;
                   17533:                break;
                   17534:        case 0x01: case 0x03: case 0x05: case 0x07:
                   17535:                if(dma[c].low_high) {
                   17536:                        dma[c].ch[ch].bcreg.b.h = data;
                   17537:                } else {
                   17538:                        dma[c].ch[ch].bcreg.b.l = data;
                   17539:                }
                   17540:                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   17541:                dma[c].low_high = !dma[c].low_high;
                   17542:                break;
                   17543:        case 0x08:
                   17544:                // command register
                   17545:                dma[c].cmd = data;
                   17546:                break;
                   17547:        case 0x09:
                   17548:                // dma[c].request register
                   17549:                if(data & 4) {
                   17550:                        if(!(dma[c].req & bit)) {
                   17551:                                dma[c].req |= bit;
                   17552: //                             dma_run(c, ch);
                   17553:                        }
                   17554:                } else {
                   17555:                        dma[c].req &= ~bit;
                   17556:                }
                   17557:                break;
                   17558:        case 0x0a:
                   17559:                // single mask register
                   17560:                if(data & 4) {
                   17561:                        dma[c].mask |= bit;
                   17562:                } else {
                   17563:                        dma[c].mask &= ~bit;
                   17564:                }
                   17565:                break;
                   17566:        case 0x0b:
                   17567:                // mode register
                   17568:                dma[c].ch[data & 3].mode = data;
                   17569:                break;
                   17570:        case 0x0c:
                   17571:                dma[c].low_high = false;
                   17572:                break;
                   17573:        case 0x0d:
                   17574:                // clear master
                   17575:                dma_reset(c);
                   17576:                break;
                   17577:        case 0x0e:
                   17578:                // clear mask register
                   17579:                dma[c].mask = 0;
                   17580:                break;
                   17581:        case 0x0f:
                   17582:                // all mask register
                   17583:                dma[c].mask = data & 0x0f;
                   17584:                break;
                   17585:        }
                   17586: }
                   17587: 
                   17588: UINT8 dma_read(int c, UINT32 addr)
                   17589: {
                   17590:        int ch = (addr >> 1) & 3;
                   17591:        UINT8 val = 0xff;
                   17592:        
                   17593:        switch(addr & 0x0f) {
                   17594:        case 0x00: case 0x02: case 0x04: case 0x06:
                   17595:                if(dma[c].low_high) {
                   17596:                        val = dma[c].ch[ch].areg.b.h;
                   17597:                } else {
                   17598:                        val = dma[c].ch[ch].areg.b.l;
                   17599:                }
                   17600:                dma[c].low_high = !dma[c].low_high;
                   17601:                return(val);
                   17602:        case 0x01: case 0x03: case 0x05: case 0x07:
                   17603:                if(dma[c].low_high) {
                   17604:                        val = dma[c].ch[ch].creg.b.h;
                   17605:                } else {
                   17606:                        val = dma[c].ch[ch].creg.b.l;
                   17607:                }
                   17608:                dma[c].low_high = !dma[c].low_high;
                   17609:                return(val);
                   17610:        case 0x08:
                   17611:                // status register
                   17612:                val = (dma[c].req << 4) | dma[c].tc;
                   17613:                dma[c].tc = 0;
                   17614:                return(val);
                   17615:        case 0x0d:
1.1.1.26  root     17616:                // temporary register (intel 82374 does not support)
1.1.1.25  root     17617:                return(dma[c].tmp & 0xff);
1.1.1.26  root     17618:        case 0x0f:
                   17619:                // mask register (intel 82374 does support)
                   17620:                return(dma[c].mask);
1.1.1.25  root     17621:        }
                   17622:        return(0xff);
                   17623: }
                   17624: 
                   17625: void dma_page_write(int c, int ch, UINT8 data)
                   17626: {
                   17627:        dma[c].ch[ch].pagereg = data;
                   17628: }
                   17629: 
                   17630: UINT8 dma_page_read(int c, int ch)
                   17631: {
                   17632:        return(dma[c].ch[ch].pagereg);
                   17633: }
                   17634: 
                   17635: void dma_run(int c, int ch)
                   17636: {
                   17637:        UINT8 bit = 1 << ch;
                   17638:        
                   17639:        if((dma[c].req & bit) && !(dma[c].mask & bit)) {
                   17640:                // execute dma
                   17641:                while(dma[c].req & bit) {
                   17642:                        if(ch == 0 && (dma[c].cmd & 0x01)) {
                   17643:                                // memory -> memory
                   17644:                                UINT32 saddr = dma[c].ch[0].areg.w | (dma[c].ch[0].pagereg << 16);
                   17645:                                UINT32 daddr = dma[c].ch[1].areg.w | (dma[c].ch[1].pagereg << 16);
                   17646:                                
                   17647:                                if(c == 0) {
                   17648:                                        dma[c].tmp = read_byte(saddr);
                   17649:                                        write_byte(daddr, dma[c].tmp);
                   17650:                                } else {
                   17651:                                        dma[c].tmp = read_word(saddr << 1);
                   17652:                                        write_word(daddr << 1, dma[c].tmp);
                   17653:                                }
                   17654:                                if(!(dma[c].cmd & 0x02)) {
                   17655:                                        if(dma[c].ch[0].mode & 0x20) {
                   17656:                                                dma[c].ch[0].areg.w--;
                   17657:                                                if(dma[c].ch[0].areg.w == 0xffff) {
                   17658:                                                        dma[c].ch[0].pagereg--;
                   17659:                                                }
                   17660:                                        } else {
                   17661:                                                dma[c].ch[0].areg.w++;
                   17662:                                                if(dma[c].ch[0].areg.w == 0) {
                   17663:                                                        dma[c].ch[0].pagereg++;
                   17664:                                                }
                   17665:                                        }
                   17666:                                }
                   17667:                                if(dma[c].ch[1].mode & 0x20) {
                   17668:                                        dma[c].ch[1].areg.w--;
                   17669:                                        if(dma[c].ch[1].areg.w == 0xffff) {
                   17670:                                                dma[c].ch[1].pagereg--;
                   17671:                                        }
                   17672:                                } else {
                   17673:                                        dma[c].ch[1].areg.w++;
                   17674:                                        if(dma[c].ch[1].areg.w == 0) {
                   17675:                                                dma[c].ch[1].pagereg++;
                   17676:                                        }
                   17677:                                }
                   17678:                                
                   17679:                                // check dma condition
                   17680:                                if(dma[c].ch[0].creg.w-- == 0) {
                   17681:                                        if(dma[c].ch[0].mode & 0x10) {
                   17682:                                                // self initialize
                   17683:                                                dma[c].ch[0].areg.w = dma[c].ch[0].bareg.w;
                   17684:                                                dma[c].ch[0].creg.w = dma[c].ch[0].bcreg.w;
                   17685:                                        } else {
                   17686: //                                             dma[c].mask |= bit;
                   17687:                                        }
                   17688:                                }
                   17689:                                if(dma[c].ch[1].creg.w-- == 0) {
                   17690:                                        // terminal count
                   17691:                                        if(dma[c].ch[1].mode & 0x10) {
                   17692:                                                // self initialize
                   17693:                                                dma[c].ch[1].areg.w = dma[c].ch[1].bareg.w;
                   17694:                                                dma[c].ch[1].creg.w = dma[c].ch[1].bcreg.w;
                   17695:                                        } else {
                   17696:                                                dma[c].mask |= bit;
                   17697:                                        }
                   17698:                                        dma[c].req &= ~bit;
                   17699:                                        dma[c].tc |= bit;
                   17700:                                }
                   17701:                        } else {
                   17702:                                UINT32 addr = dma[c].ch[ch].areg.w | (dma[c].ch[ch].pagereg << 16);
                   17703:                                
                   17704:                                if((dma[c].ch[ch].mode & 0x0c) == 0x00) {
                   17705:                                        // verify
                   17706:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x04) {
                   17707:                                        // io -> memory
                   17708:                                        if(c == 0) {
                   17709:                                                dma[c].tmp = read_io_byte(dma[c].ch[ch].port);
                   17710:                                                write_byte(addr, dma[c].tmp);
                   17711:                                        } else {
                   17712:                                                dma[c].tmp = read_io_word(dma[c].ch[ch].port);
                   17713:                                                write_word(addr << 1, dma[c].tmp);
                   17714:                                        }
                   17715:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x08) {
                   17716:                                        // memory -> io
                   17717:                                        if(c == 0) {
                   17718:                                                dma[c].tmp = read_byte(addr);
                   17719:                                                write_io_byte(dma[c].ch[ch].port, dma[c].tmp);
                   17720:                                        } else {
                   17721:                                                dma[c].tmp = read_word(addr << 1);
                   17722:                                                write_io_word(dma[c].ch[ch].port, dma[c].tmp);
                   17723:                                        }
                   17724:                                }
                   17725:                                if(dma[c].ch[ch].mode & 0x20) {
                   17726:                                        dma[c].ch[ch].areg.w--;
                   17727:                                        if(dma[c].ch[ch].areg.w == 0xffff) {
                   17728:                                                dma[c].ch[ch].pagereg--;
                   17729:                                        }
                   17730:                                } else {
                   17731:                                        dma[c].ch[ch].areg.w++;
                   17732:                                        if(dma[c].ch[ch].areg.w == 0) {
                   17733:                                                dma[c].ch[ch].pagereg++;
                   17734:                                        }
                   17735:                                }
                   17736:                                
                   17737:                                // check dma condition
                   17738:                                if(dma[c].ch[ch].creg.w-- == 0) {
                   17739:                                        // terminal count
                   17740:                                        if(dma[c].ch[ch].mode & 0x10) {
                   17741:                                                // self initialize
                   17742:                                                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   17743:                                                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   17744:                                        } else {
                   17745:                                                dma[c].mask |= bit;
                   17746:                                        }
                   17747:                                        dma[c].req &= ~bit;
                   17748:                                        dma[c].tc |= bit;
                   17749:                                } else if((dma[c].ch[ch].mode & 0xc0) == 0x40) {
                   17750:                                        // single mode
                   17751:                                        break;
                   17752:                                }
                   17753:                        }
                   17754:                }
                   17755:        }
                   17756: }
                   17757: 
                   17758: // pic
                   17759: 
                   17760: void pic_init()
                   17761: {
                   17762:        memset(pic, 0, sizeof(pic));
                   17763:        pic[0].imr = pic[1].imr = 0xff;
                   17764:        
                   17765:        // from bochs bios
                   17766:        pic_write(0, 0, 0x11);  // icw1 = 11h
                   17767:        pic_write(0, 1, 0x08);  // icw2 = 08h
                   17768:        pic_write(0, 1, 0x04);  // icw3 = 04h
                   17769:        pic_write(0, 1, 0x01);  // icw4 = 01h
                   17770:        pic_write(0, 1, 0xb8);  // ocw1 = b8h
                   17771:        pic_write(1, 0, 0x11);  // icw1 = 11h
                   17772:        pic_write(1, 1, 0x70);  // icw2 = 70h
                   17773:        pic_write(1, 1, 0x02);  // icw3 = 02h
                   17774:        pic_write(1, 1, 0x01);  // icw4 = 01h
                   17775: }
                   17776: 
                   17777: void pic_write(int c, UINT32 addr, UINT8 data)
                   17778: {
                   17779:        if(addr & 1) {
                   17780:                if(pic[c].icw2_r) {
                   17781:                        // icw2
                   17782:                        pic[c].icw2 = data;
                   17783:                        pic[c].icw2_r = 0;
                   17784:                } else if(pic[c].icw3_r) {
                   17785:                        // icw3
                   17786:                        pic[c].icw3 = data;
                   17787:                        pic[c].icw3_r = 0;
                   17788:                } else if(pic[c].icw4_r) {
                   17789:                        // icw4
                   17790:                        pic[c].icw4 = data;
                   17791:                        pic[c].icw4_r = 0;
                   17792:                } else {
                   17793:                        // ocw1
1.1       root     17794:                        pic[c].imr = data;
                   17795:                }
                   17796:        } else {
                   17797:                if(data & 0x10) {
                   17798:                        // icw1
                   17799:                        pic[c].icw1 = data;
                   17800:                        pic[c].icw2_r = 1;
                   17801:                        pic[c].icw3_r = (data & 2) ? 0 : 1;
                   17802:                        pic[c].icw4_r = data & 1;
                   17803:                        pic[c].irr = 0;
                   17804:                        pic[c].isr = 0;
                   17805:                        pic[c].imr = 0;
                   17806:                        pic[c].prio = 0;
                   17807:                        if(!(pic[c].icw1 & 1)) {
                   17808:                                pic[c].icw4 = 0;
                   17809:                        }
                   17810:                        pic[c].ocw3 = 0;
                   17811:                } else if(data & 8) {
                   17812:                        // ocw3
                   17813:                        if(!(data & 2)) {
                   17814:                                data = (data & ~1) | (pic[c].ocw3 & 1);
                   17815:                        }
                   17816:                        if(!(data & 0x40)) {
                   17817:                                data = (data & ~0x20) | (pic[c].ocw3 & 0x20);
                   17818:                        }
                   17819:                        pic[c].ocw3 = data;
                   17820:                } else {
                   17821:                        // ocw2
                   17822:                        int level = 0;
                   17823:                        if(data & 0x40) {
                   17824:                                level = data & 7;
                   17825:                        } else {
                   17826:                                if(!pic[c].isr) {
                   17827:                                        return;
                   17828:                                }
                   17829:                                level = pic[c].prio;
                   17830:                                while(!(pic[c].isr & (1 << level))) {
                   17831:                                        level = (level + 1) & 7;
                   17832:                                }
                   17833:                        }
                   17834:                        if(data & 0x80) {
                   17835:                                pic[c].prio = (level + 1) & 7;
                   17836:                        }
                   17837:                        if(data & 0x20) {
                   17838:                                pic[c].isr &= ~(1 << level);
                   17839:                        }
                   17840:                }
                   17841:        }
                   17842:        pic_update();
                   17843: }
                   17844: 
                   17845: UINT8 pic_read(int c, UINT32 addr)
                   17846: {
                   17847:        if(addr & 1) {
                   17848:                return(pic[c].imr);
                   17849:        } else {
                   17850:                // polling mode is not supported...
                   17851:                //if(pic[c].ocw3 & 4) {
                   17852:                //      return ???;
                   17853:                //}
                   17854:                if(pic[c].ocw3 & 1) {
                   17855:                        return(pic[c].isr);
                   17856:                } else {
                   17857:                        return(pic[c].irr);
                   17858:                }
                   17859:        }
                   17860: }
                   17861: 
                   17862: void pic_req(int c, int level, int signal)
                   17863: {
                   17864:        if(signal) {
                   17865:                pic[c].irr |= (1 << level);
                   17866:        } else {
                   17867:                pic[c].irr &= ~(1 << level);
                   17868:        }
                   17869:        pic_update();
                   17870: }
                   17871: 
                   17872: int pic_ack()
                   17873: {
                   17874:        // ack (INTA=L)
                   17875:        pic[pic_req_chip].isr |= pic_req_bit;
                   17876:        pic[pic_req_chip].irr &= ~pic_req_bit;
                   17877:        if(pic_req_chip > 0) {
                   17878:                // update isr and irr of master
                   17879:                UINT8 slave = 1 << (pic[pic_req_chip].icw3 & 7);
                   17880:                pic[pic_req_chip - 1].isr |= slave;
                   17881:                pic[pic_req_chip - 1].irr &= ~slave;
                   17882:        }
                   17883:        //if(pic[pic_req_chip].icw4 & 1) {
                   17884:                // 8086 mode
                   17885:                int vector = (pic[pic_req_chip].icw2 & 0xf8) | pic_req_level;
                   17886:        //} else {
                   17887:        //      // 8080 mode
                   17888:        //      UINT16 addr = (UINT16)pic[pic_req_chip].icw2 << 8;
                   17889:        //      if(pic[pic_req_chip].icw1 & 4) {
                   17890:        //              addr |= (pic[pic_req_chip].icw1 & 0xe0) | (pic_req_level << 2);
                   17891:        //      } else {
                   17892:        //              addr |= (pic[pic_req_chip].icw1 & 0xc0) | (pic_req_level << 3);
                   17893:        //      }
                   17894:        //      vector = 0xcd | (addr << 8);
                   17895:        //}
                   17896:        if(pic[pic_req_chip].icw4 & 2) {
                   17897:                // auto eoi
                   17898:                pic[pic_req_chip].isr &= ~pic_req_bit;
                   17899:        }
                   17900:        return(vector);
                   17901: }
                   17902: 
                   17903: void pic_update()
                   17904: {
                   17905:        for(int c = 0; c < 2; c++) {
                   17906:                UINT8 irr = pic[c].irr;
                   17907:                if(c + 1 < 2) {
                   17908:                        // this is master
                   17909:                        if(pic[c + 1].irr & (~pic[c + 1].imr)) {
                   17910:                                // request from slave
                   17911:                                irr |= 1 << (pic[c + 1].icw3 & 7);
                   17912:                        }
                   17913:                }
                   17914:                irr &= (~pic[c].imr);
                   17915:                if(!irr) {
                   17916:                        break;
                   17917:                }
                   17918:                if(!(pic[c].ocw3 & 0x20)) {
                   17919:                        irr |= pic[c].isr;
                   17920:                }
                   17921:                int level = pic[c].prio;
                   17922:                UINT8 bit = 1 << level;
                   17923:                while(!(irr & bit)) {
                   17924:                        level = (level + 1) & 7;
                   17925:                        bit = 1 << level;
                   17926:                }
                   17927:                if((c + 1 < 2) && (pic[c].icw3 & bit)) {
                   17928:                        // check slave
                   17929:                        continue;
                   17930:                }
                   17931:                if(pic[c].isr & bit) {
                   17932:                        break;
                   17933:                }
                   17934:                // interrupt request
                   17935:                pic_req_chip = c;
                   17936:                pic_req_level = level;
                   17937:                pic_req_bit = bit;
1.1.1.3   root     17938:                i386_set_irq_line(INPUT_LINE_IRQ, HOLD_LINE);
1.1       root     17939:                return;
                   17940:        }
1.1.1.3   root     17941:        i386_set_irq_line(INPUT_LINE_IRQ, CLEAR_LINE);
1.1.1.2   root     17942: }
1.1       root     17943: 
1.1.1.25  root     17944: // pio
                   17945: 
                   17946: void pio_init()
                   17947: {
1.1.1.38  root     17948: //     bool conv_mode = (GetConsoleCP() == 932);
                   17949:        
1.1.1.26  root     17950:        memset(pio, 0, sizeof(pio));
1.1.1.37  root     17951:        
1.1.1.25  root     17952:        for(int c = 0; c < 2; c++) {
1.1.1.37  root     17953:                pio[c].stat = 0xdf;
1.1.1.25  root     17954:                pio[c].ctrl = 0x0c;
1.1.1.38  root     17955: //             pio[c].conv_mode = conv_mode;
1.1.1.25  root     17956:        }
                   17957: }
                   17958: 
1.1.1.37  root     17959: void pio_finish()
                   17960: {
                   17961:        pio_release();
                   17962: }
                   17963: 
                   17964: void pio_release()
                   17965: {
                   17966:        for(int c = 0; c < 2; c++) {
                   17967:                if(pio[c].fp != NULL) {
1.1.1.38  root     17968:                        if(pio[c].jis_mode) {
                   17969:                                fputc(0x1c, pio[c].fp);
                   17970:                                fputc(0x2e, pio[c].fp);
                   17971:                        }
1.1.1.37  root     17972:                        fclose(pio[c].fp);
                   17973:                        pio[c].fp = NULL;
                   17974:                }
                   17975:        }
                   17976: }
                   17977: 
1.1.1.25  root     17978: void pio_write(int c, UINT32 addr, UINT8 data)
                   17979: {
                   17980:        switch(addr & 3) {
                   17981:        case 0:
                   17982:                pio[c].data = data;
                   17983:                break;
                   17984:        case 2:
1.1.1.37  root     17985:                if((pio[c].ctrl & 0x01) && !(data & 0x01)) {
                   17986:                        // strobe H -> L
                   17987:                        if(pio[c].data == 0x0d && (data & 0x02)) {
                   17988:                                // auto feed
                   17989:                                printer_out(c, 0x0d);
                   17990:                                printer_out(c, 0x0a);
                   17991:                        } else {
                   17992:                                printer_out(c, pio[c].data);
                   17993:                        }
                   17994:                        pio[c].stat &= ~0x40; // set ack
                   17995:                }
1.1.1.25  root     17996:                pio[c].ctrl = data;
                   17997:                break;
                   17998:        }
                   17999: }
                   18000: 
                   18001: UINT8 pio_read(int c, UINT32 addr)
                   18002: {
                   18003:        switch(addr & 3) {
                   18004:        case 0:
1.1.1.37  root     18005:                if(pio[c].ctrl & 0x20) {
                   18006:                        // input mode
                   18007:                        return(0xff);
                   18008:                }
1.1.1.25  root     18009:                return(pio[c].data);
                   18010:        case 1:
1.1.1.37  root     18011:                {
                   18012:                        UINT8 stat = pio[c].stat;
                   18013:                        pio[c].stat |= 0x40; // clear ack
                   18014:                        return(stat);
                   18015:                }
1.1.1.25  root     18016:        case 2:
                   18017:                return(pio[c].ctrl);
                   18018:        }
                   18019:        return(0xff);
                   18020: }
                   18021: 
1.1.1.37  root     18022: void printer_out(int c, UINT8 data)
                   18023: {
                   18024:        SYSTEMTIME time;
1.1.1.38  root     18025:        bool jis_mode = false;
1.1.1.37  root     18026:        
                   18027:        GetLocalTime(&time);
                   18028:        
                   18029:        if(pio[c].fp != NULL) {
                   18030:                // if at least 1000ms passed from last written, close the current file
                   18031:                FILETIME ftime1;
                   18032:                FILETIME ftime2;
                   18033:                SystemTimeToFileTime(&pio[c].time, &ftime1);
                   18034:                SystemTimeToFileTime(&time, &ftime2);
                   18035:                INT64 *time1 = (INT64 *)&ftime1;
                   18036:                INT64 *time2 = (INT64 *)&ftime2;
                   18037:                INT64 msec = (*time2 - *time1) / 10000;
                   18038:                
                   18039:                if(msec >= 1000) {
1.1.1.38  root     18040:                        if(pio[c].jis_mode) {
                   18041:                                fputc(0x1c, pio[c].fp);
                   18042:                                fputc(0x2e, pio[c].fp);
                   18043:                                jis_mode = true;
                   18044:                        }
1.1.1.37  root     18045:                        fclose(pio[c].fp);
                   18046:                        pio[c].fp = NULL;
                   18047:                }
                   18048:        }
                   18049:        if(pio[c].fp == NULL) {
                   18050:                // create a new file in the temp folder
                   18051:                char file_name[MAX_PATH];
                   18052:                
                   18053:                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);
                   18054:                if(GetTempPath(MAX_PATH, pio[c].path)) {
                   18055:                        strcat(pio[c].path, file_name);
                   18056:                } else {
                   18057:                        strcpy(pio[c].path, file_name);
                   18058:                }
1.1.1.38  root     18059:                pio[c].fp = fopen(pio[c].path, "w+b");
1.1.1.37  root     18060:        }
                   18061:        if(pio[c].fp != NULL) {
1.1.1.38  root     18062:                if(jis_mode) {
                   18063:                        fputc(0x1c, pio[c].fp);
                   18064:                        fputc(0x26, pio[c].fp);
                   18065:                }
1.1.1.37  root     18066:                fputc(data, pio[c].fp);
1.1.1.38  root     18067:                
                   18068:                // reopen file if 1ch 26h 1ch 2eh (kanji-on  kanji-off) are written at the top
                   18069:                if(data == 0x2e && ftell(pio[c].fp) == 4) {
                   18070:                        UINT8 buffer[4];
                   18071:                        fseek(pio[c].fp, 0, SEEK_SET);
                   18072:                        fread(buffer, 4, 1, pio[c].fp);
                   18073:                        if(buffer[0] == 0x1c && buffer[1] == 0x26 && buffer[2] == 0x1c/* && buffer[3] == 0x2e*/) {
                   18074:                                fclose(pio[c].fp);
                   18075:                                pio[c].fp = fopen(pio[c].path, "w+b");
                   18076:                        }
                   18077:                }
1.1.1.37  root     18078:                pio[c].time = time;
                   18079:        }
                   18080: }
                   18081: 
1.1       root     18082: // pit
                   18083: 
1.1.1.22  root     18084: #define PIT_FREQ 1193182ULL
1.1       root     18085: #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)
                   18086: 
                   18087: void pit_init()
                   18088: {
1.1.1.8   root     18089:        memset(pit, 0, sizeof(pit));
1.1       root     18090:        for(int ch = 0; ch < 3; ch++) {
                   18091:                pit[ch].count = 0x10000;
                   18092:                pit[ch].ctrl_reg = 0x34;
                   18093:                pit[ch].mode = 3;
                   18094:        }
                   18095:        
                   18096:        // from bochs bios
                   18097:        pit_write(3, 0x34);
                   18098:        pit_write(0, 0x00);
                   18099:        pit_write(0, 0x00);
                   18100: }
                   18101: 
                   18102: void pit_write(int ch, UINT8 val)
                   18103: {
1.1.1.8   root     18104: #ifndef PIT_ALWAYS_RUNNING
1.1       root     18105:        if(!pit_active) {
                   18106:                pit_active = 1;
                   18107:                pit_init();
                   18108:        }
1.1.1.8   root     18109: #endif
1.1       root     18110:        switch(ch) {
                   18111:        case 0:
                   18112:        case 1:
                   18113:        case 2:
                   18114:                // write count register
                   18115:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   18116:                        if(pit[ch].ctrl_reg & 0x10) {
                   18117:                                pit[ch].low_write = 1;
                   18118:                        }
                   18119:                        if(pit[ch].ctrl_reg & 0x20) {
                   18120:                                pit[ch].high_write = 1;
                   18121:                        }
                   18122:                }
                   18123:                if(pit[ch].low_write) {
                   18124:                        pit[ch].count_reg = val;
                   18125:                        pit[ch].low_write = 0;
                   18126:                } else if(pit[ch].high_write) {
                   18127:                        if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   18128:                                pit[ch].count_reg = val << 8;
                   18129:                        } else {
                   18130:                                pit[ch].count_reg |= val << 8;
                   18131:                        }
                   18132:                        pit[ch].high_write = 0;
                   18133:                }
                   18134:                // start count
1.1.1.8   root     18135:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   18136:                        if(pit[ch].mode == 0 || pit[ch].mode == 4 || pit[ch].prev_time == 0) {
                   18137:                                pit[ch].count = PIT_COUNT_VALUE(ch);
                   18138:                                pit[ch].prev_time = timeGetTime();
                   18139:                                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     18140:                        }
                   18141:                }
                   18142:                break;
                   18143:        case 3: // ctrl reg
                   18144:                if((val & 0xc0) == 0xc0) {
                   18145:                        // i8254 read-back command
                   18146:                        for(ch = 0; ch < 3; ch++) {
                   18147:                                if(!(val & 0x10) && !pit[ch].status_latched) {
                   18148:                                        pit[ch].status = pit[ch].ctrl_reg & 0x3f;
                   18149:                                        pit[ch].status_latched = 1;
                   18150:                                }
                   18151:                                if(!(val & 0x20) && !pit[ch].count_latched) {
                   18152:                                        pit_latch_count(ch);
                   18153:                                }
                   18154:                        }
                   18155:                        break;
                   18156:                }
                   18157:                ch = (val >> 6) & 3;
                   18158:                if(val & 0x30) {
1.1.1.35  root     18159:                        static const int modes[8] = {0, 1, 2, 3, 4, 5, 2, 3};
1.1       root     18160:                        pit[ch].mode = modes[(val >> 1) & 7];
                   18161:                        pit[ch].count_latched = 0;
                   18162:                        pit[ch].low_read = pit[ch].high_read = 0;
                   18163:                        pit[ch].low_write = pit[ch].high_write = 0;
                   18164:                        pit[ch].ctrl_reg = val;
                   18165:                        // stop count
1.1.1.8   root     18166:                        pit[ch].prev_time = pit[ch].expired_time = 0;
1.1       root     18167:                        pit[ch].count_reg = 0;
                   18168:                } else if(!pit[ch].count_latched) {
                   18169:                        pit_latch_count(ch);
                   18170:                }
                   18171:                break;
                   18172:        }
                   18173: }
                   18174: 
                   18175: UINT8 pit_read(int ch)
                   18176: {
1.1.1.8   root     18177: #ifndef PIT_ALWAYS_RUNNING
1.1       root     18178:        if(!pit_active) {
                   18179:                pit_active = 1;
                   18180:                pit_init();
                   18181:        }
1.1.1.8   root     18182: #endif
1.1       root     18183:        switch(ch) {
                   18184:        case 0:
                   18185:        case 1:
                   18186:        case 2:
                   18187:                if(pit[ch].status_latched) {
                   18188:                        pit[ch].status_latched = 0;
                   18189:                        return(pit[ch].status);
                   18190:                }
                   18191:                // if not latched, through current count
                   18192:                if(!pit[ch].count_latched) {
                   18193:                        if(!pit[ch].low_read && !pit[ch].high_read) {
                   18194:                                pit_latch_count(ch);
                   18195:                        }
                   18196:                }
                   18197:                // return latched count
                   18198:                if(pit[ch].low_read) {
                   18199:                        pit[ch].low_read = 0;
                   18200:                        if(!pit[ch].high_read) {
                   18201:                                pit[ch].count_latched = 0;
                   18202:                        }
                   18203:                        return(pit[ch].latch & 0xff);
                   18204:                } else if(pit[ch].high_read) {
                   18205:                        pit[ch].high_read = 0;
                   18206:                        pit[ch].count_latched = 0;
                   18207:                        return((pit[ch].latch >> 8) & 0xff);
                   18208:                }
                   18209:        }
                   18210:        return(0xff);
                   18211: }
                   18212: 
1.1.1.8   root     18213: int pit_run(int ch, UINT32 cur_time)
1.1       root     18214: {
1.1.1.8   root     18215:        if(pit[ch].expired_time != 0 && cur_time >= pit[ch].expired_time) {
1.1       root     18216:                pit[ch].count = PIT_COUNT_VALUE(ch);
1.1.1.8   root     18217:                pit[ch].prev_time = pit[ch].expired_time;
                   18218:                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
                   18219:                if(cur_time >= pit[ch].expired_time) {
                   18220:                        pit[ch].prev_time = cur_time;
                   18221:                        pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     18222:                }
1.1.1.8   root     18223:                return(1);
1.1       root     18224:        }
1.1.1.8   root     18225:        return(0);
1.1       root     18226: }
                   18227: 
                   18228: void pit_latch_count(int ch)
                   18229: {
1.1.1.8   root     18230:        if(pit[ch].expired_time != 0) {
1.1.1.26  root     18231:                UINT32 cur_time = timeGetTime();
1.1.1.8   root     18232:                pit_run(ch, cur_time);
                   18233:                UINT32 tmp = (pit[ch].count * (pit[ch].expired_time - cur_time)) / (pit[ch].expired_time - pit[ch].prev_time);
1.1.1.26  root     18234:                UINT16 latch = (tmp != 0) ? (UINT16)tmp : 1;
                   18235:                
                   18236:                if(pit[ch].prev_latch == latch && pit[ch].expired_time > cur_time) {
                   18237:                        // decrement counter in 1msec period
                   18238:                        if(pit[ch].next_latch == 0) {
                   18239:                                tmp = (pit[ch].count * (pit[ch].expired_time - cur_time - 1)) / (pit[ch].expired_time - pit[ch].prev_time);
                   18240:                                pit[ch].next_latch = (tmp != 0) ? (UINT16)tmp : 1;
                   18241:                        }
                   18242:                        if(pit[ch].latch > pit[ch].next_latch) {
                   18243:                                pit[ch].latch--;
                   18244:                        }
                   18245:                } else {
                   18246:                        pit[ch].prev_latch = pit[ch].latch = latch;
                   18247:                        pit[ch].next_latch = 0;
                   18248:                }
1.1.1.8   root     18249:        } else {
                   18250:                pit[ch].latch = (UINT16)pit[ch].count;
1.1.1.26  root     18251:                pit[ch].prev_latch = pit[ch].next_latch = 0;
1.1       root     18252:        }
                   18253:        pit[ch].count_latched = 1;
                   18254:        if((pit[ch].ctrl_reg & 0x30) == 0x10) {
                   18255:                // lower byte
                   18256:                pit[ch].low_read = 1;
                   18257:                pit[ch].high_read = 0;
                   18258:        } else if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   18259:                // upper byte
                   18260:                pit[ch].low_read = 0;
                   18261:                pit[ch].high_read = 1;
                   18262:        } else {
                   18263:                // lower -> upper
1.1.1.14  root     18264:                pit[ch].low_read = pit[ch].high_read = 1;
1.1       root     18265:        }
                   18266: }
                   18267: 
1.1.1.8   root     18268: int pit_get_expired_time(int ch)
1.1       root     18269: {
1.1.1.22  root     18270:        pit[ch].accum += 1024ULL * 1000ULL * (UINT64)pit[ch].count / PIT_FREQ;
                   18271:        UINT64 val = pit[ch].accum >> 10;
                   18272:        pit[ch].accum -= val << 10;
                   18273:        return((val != 0) ? val : 1);
1.1.1.8   root     18274: }
                   18275: 
1.1.1.25  root     18276: // sio
                   18277: 
                   18278: void sio_init()
                   18279: {
1.1.1.26  root     18280:        memset(sio, 0, sizeof(sio));
                   18281:        memset(sio_mt, 0, sizeof(sio_mt));
                   18282:        
1.1.1.29  root     18283:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     18284:                sio[c].send_buffer = new FIFO(SIO_BUFFER_SIZE);
                   18285:                sio[c].recv_buffer = new FIFO(SIO_BUFFER_SIZE);
                   18286:                
                   18287:                sio[c].divisor.w = 12;          // 115200Hz / 9600Baud
                   18288:                sio[c].line_ctrl = 0x03;        // 8bit, stop 1bit, non parity
1.1.1.26  root     18289:                sio[c].modem_ctrl = 0x03;       // rts=on, dtr=on
                   18290:                sio[c].set_rts = sio[c].set_dtr = true;
1.1.1.25  root     18291:                sio[c].line_stat_buf = 0x60;    // send/recv buffers are empty
                   18292:                sio[c].irq_identify = 0x01;     // no pending irq
                   18293:                
                   18294:                InitializeCriticalSection(&sio_mt[c].csSendData);
                   18295:                InitializeCriticalSection(&sio_mt[c].csRecvData);
                   18296:                InitializeCriticalSection(&sio_mt[c].csLineCtrl);
                   18297:                InitializeCriticalSection(&sio_mt[c].csLineStat);
                   18298:                InitializeCriticalSection(&sio_mt[c].csModemCtrl);
                   18299:                InitializeCriticalSection(&sio_mt[c].csModemStat);
                   18300:                
1.1.1.26  root     18301:                if(sio_port_number[c] != 0) {
1.1.1.25  root     18302:                        sio[c].channel = c;
                   18303:                        sio_mt[c].hThread = CreateThread(NULL, 0, sio_thread, &sio[c], 0, NULL);
                   18304:                }
                   18305:        }
                   18306: }
                   18307: 
                   18308: void sio_finish()
                   18309: {
1.1.1.29  root     18310:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     18311:                if(sio_mt[c].hThread != NULL) {
                   18312:                        WaitForSingleObject(sio_mt[c].hThread, INFINITE);
                   18313:                        CloseHandle(sio_mt[c].hThread);
1.1.1.28  root     18314:                        sio_mt[c].hThread = NULL;
1.1.1.25  root     18315:                }
                   18316:                DeleteCriticalSection(&sio_mt[c].csSendData);
                   18317:                DeleteCriticalSection(&sio_mt[c].csRecvData);
                   18318:                DeleteCriticalSection(&sio_mt[c].csLineCtrl);
                   18319:                DeleteCriticalSection(&sio_mt[c].csLineStat);
                   18320:                DeleteCriticalSection(&sio_mt[c].csModemCtrl);
                   18321:                DeleteCriticalSection(&sio_mt[c].csModemStat);
1.1.1.28  root     18322:        }
                   18323:        sio_release();
                   18324: }
                   18325: 
                   18326: void sio_release()
                   18327: {
1.1.1.29  root     18328:        for(int c = 0; c < 4; c++) {
1.1.1.28  root     18329:                // sio_thread() may access the resources :-(
1.1.1.32  root     18330:                bool running = (sio_mt[c].hThread != NULL);
                   18331:                
                   18332:                if(running) {
                   18333:                        EnterCriticalSection(&sio_mt[c].csSendData);
                   18334:                }
                   18335:                if(sio[c].send_buffer != NULL) {
                   18336:                        sio[c].send_buffer->release();
                   18337:                        delete sio[c].send_buffer;
                   18338:                        sio[c].send_buffer = NULL;
                   18339:                }
                   18340:                if(running) {
                   18341:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   18342:                        EnterCriticalSection(&sio_mt[c].csRecvData);
                   18343:                }
                   18344:                if(sio[c].recv_buffer != NULL) {
                   18345:                        sio[c].recv_buffer->release();
                   18346:                        delete sio[c].recv_buffer;
                   18347:                        sio[c].recv_buffer = NULL;
                   18348:                }
                   18349:                if(running) {
                   18350:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
1.1.1.28  root     18351:                }
1.1.1.25  root     18352:        }
                   18353: }
                   18354: 
                   18355: void sio_write(int c, UINT32 addr, UINT8 data)
                   18356: {
                   18357:        switch(addr & 7) {
                   18358:        case 0:
                   18359:                if(sio[c].selector & 0x80) {
                   18360:                        if(sio[c].divisor.b.l != data) {
                   18361:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   18362:                                sio[c].divisor.b.l = data;
                   18363:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   18364:                        }
                   18365:                } else {
                   18366:                        EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     18367:                        if(sio[c].send_buffer != NULL) {
                   18368:                                sio[c].send_buffer->write(data);
                   18369:                        }
1.1.1.25  root     18370:                        // transmitter holding/shift registers are not empty
                   18371:                        sio[c].line_stat_buf &= ~0x60;
                   18372:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   18373:                        
                   18374:                        if(sio[c].irq_enable & 0x02) {
                   18375:                                sio_update_irq(c);
                   18376:                        }
                   18377:                }
                   18378:                break;
                   18379:        case 1:
                   18380:                if(sio[c].selector & 0x80) {
                   18381:                        if(sio[c].divisor.b.h != data) {
                   18382:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   18383:                                sio[c].divisor.b.h = data;
                   18384:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   18385:                        }
                   18386:                } else {
                   18387:                        if(sio[c].irq_enable != data) {
                   18388:                                sio[c].irq_enable = data;
                   18389:                                sio_update_irq(c);
                   18390:                        }
                   18391:                }
                   18392:                break;
                   18393:        case 3:
                   18394:                {
                   18395:                        UINT8 line_ctrl = data & 0x3f;
                   18396:                        bool set_brk = ((data & 0x40) != 0);
                   18397:                        
                   18398:                        if(sio[c].line_ctrl != line_ctrl) {
                   18399:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   18400:                                sio[c].line_ctrl = line_ctrl;
                   18401:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   18402:                        }
                   18403:                        if(sio[c].set_brk != set_brk) {
                   18404:                                EnterCriticalSection(&sio_mt[c].csModemCtrl);
                   18405:                                sio[c].set_brk = set_brk;
                   18406:                                LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   18407:                        }
                   18408:                }
                   18409:                sio[c].selector = data;
                   18410:                break;
                   18411:        case 4:
                   18412:                {
                   18413:                        bool set_dtr = ((data & 0x01) != 0);
                   18414:                        bool set_rts = ((data & 0x02) != 0);
                   18415:                        
                   18416:                        if(sio[c].set_dtr != set_dtr || sio[c].set_rts != set_rts) {
1.1.1.26  root     18417: //                             EnterCriticalSection(&sio_mt[c].csModemCtrl);
1.1.1.25  root     18418:                                sio[c].set_dtr = set_dtr;
                   18419:                                sio[c].set_rts = set_rts;
1.1.1.26  root     18420: //                             LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   18421:                                
                   18422:                                bool state_changed = false;
                   18423:                                
                   18424:                                EnterCriticalSection(&sio_mt[c].csModemStat);
                   18425:                                if(set_dtr) {
                   18426:                                        sio[c].modem_stat |= 0x20;      // dsr on
                   18427:                                } else {
                   18428:                                        sio[c].modem_stat &= ~0x20;     // dsr off
                   18429:                                }
                   18430:                                if(set_rts) {
                   18431:                                        sio[c].modem_stat |= 0x10;      // cts on
                   18432:                                } else {
                   18433:                                        sio[c].modem_stat &= ~0x10;     // cts off
                   18434:                                }
                   18435:                                if((sio[c].prev_modem_stat & 0x20) != (sio[c].modem_stat & 0x20)) {
                   18436:                                        if(!(sio[c].modem_stat & 0x02)) {
                   18437:                                                if(sio[c].irq_enable & 0x08) {
                   18438:                                                        state_changed = true;
                   18439:                                                }
                   18440:                                                sio[c].modem_stat |= 0x02;
                   18441:                                        }
                   18442:                                }
                   18443:                                if((sio[c].prev_modem_stat & 0x10) != (sio[c].modem_stat & 0x10)) {
                   18444:                                        if(!(sio[c].modem_stat & 0x01)) {
                   18445:                                                if(sio[c].irq_enable & 0x08) {
                   18446:                                                        state_changed = true;
                   18447:                                                }
                   18448:                                                sio[c].modem_stat |= 0x01;
                   18449:                                        }
                   18450:                                }
                   18451:                                LeaveCriticalSection(&sio_mt[c].csModemStat);
                   18452:                                
                   18453:                                if(state_changed) {
                   18454:                                        sio_update_irq(c);
                   18455:                                }
1.1.1.25  root     18456:                        }
                   18457:                }
                   18458:                sio[c].modem_ctrl = data;
                   18459:                break;
                   18460:        case 7:
                   18461:                sio[c].scratch = data;
                   18462:                break;
                   18463:        }
                   18464: }
                   18465: 
                   18466: UINT8 sio_read(int c, UINT32 addr)
                   18467: {
                   18468:        switch(addr & 7) {
                   18469:        case 0:
                   18470:                if(sio[c].selector & 0x80) {
                   18471:                        return(sio[c].divisor.b.l);
                   18472:                } else {
                   18473:                        EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     18474:                        UINT8 data = 0;
                   18475:                        if(sio[c].recv_buffer != NULL) {
                   18476:                                data = sio[c].recv_buffer->read();
                   18477:                        }
1.1.1.25  root     18478:                        // data is not ready
                   18479:                        sio[c].line_stat_buf &= ~0x01;
                   18480:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
                   18481:                        
                   18482:                        if(sio[c].irq_enable & 0x01) {
                   18483:                                sio_update_irq(c);
                   18484:                        }
                   18485:                        return(data);
                   18486:                }
                   18487:        case 1:
                   18488:                if(sio[c].selector & 0x80) {
                   18489:                        return(sio[c].divisor.b.h);
                   18490:                } else {
                   18491:                        return(sio[c].irq_enable);
                   18492:                }
                   18493:        case 2:
                   18494:                return(sio[c].irq_identify);
                   18495:        case 3:
                   18496:                return(sio[c].selector);
                   18497:        case 4:
                   18498:                return(sio[c].modem_ctrl);
                   18499:        case 5:
                   18500:                {
                   18501:                        EnterCriticalSection(&sio_mt[c].csLineStat);
                   18502:                        UINT8 val = sio[c].line_stat_err | sio[c].line_stat_buf;
                   18503:                        sio[c].line_stat_err = 0x00;
                   18504:                        LeaveCriticalSection(&sio_mt[c].csLineStat);
                   18505:                        
                   18506:                        bool state_changed = false;
                   18507:                        
                   18508:                        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   18509:                                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     18510:                                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     18511:                                        // transmitter holding register will be empty first
                   18512:                                        if(sio[c].irq_enable & 0x02) {
                   18513:                                                state_changed = true;
                   18514:                                        }
                   18515:                                        sio[c].line_stat_buf |= 0x20;
                   18516:                                }
                   18517:                                LeaveCriticalSection(&sio_mt[c].csSendData);
                   18518:                        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   18519:                                // transmitter shift register will be empty later
                   18520:                                sio[c].line_stat_buf |= 0x40;
                   18521:                        }
                   18522:                        if(!(sio[c].line_stat_buf & 0x01)) {
                   18523:                                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     18524:                                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     18525:                                        // data is ready
                   18526:                                        if(sio[c].irq_enable & 0x01) {
                   18527:                                                state_changed = true;
                   18528:                                        }
                   18529:                                        sio[c].line_stat_buf |= 0x01;
                   18530:                                }
                   18531:                                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   18532:                        }
                   18533:                        if(state_changed) {
                   18534:                                sio_update_irq(c);
                   18535:                        }
                   18536:                        return(val);
                   18537:                }
                   18538:        case 6:
                   18539:                {
                   18540:                        EnterCriticalSection(&sio_mt[c].csModemStat);
                   18541:                        UINT8 val = sio[c].modem_stat;
                   18542:                        sio[c].modem_stat &= 0xf0;
                   18543:                        sio[c].prev_modem_stat = sio[c].modem_stat;
                   18544:                        LeaveCriticalSection(&sio_mt[c].csModemStat);
                   18545:                        
                   18546:                        if(sio[c].modem_ctrl & 0x10) {
                   18547:                                // loop-back
                   18548:                                val &= 0x0f;
                   18549:                                val |= (sio[c].modem_ctrl & 0x0c) << 4;
                   18550:                                val |= (sio[c].modem_ctrl & 0x01) << 5;
                   18551:                                val |= (sio[c].modem_ctrl & 0x02) << 3;
                   18552:                        }
                   18553:                        return(val);
                   18554:                }
                   18555:        case 7:
                   18556:                return(sio[c].scratch);
                   18557:        }
                   18558:        return(0xff);
                   18559: }
                   18560: 
                   18561: void sio_update(int c)
                   18562: {
                   18563:        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   18564:                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     18565:                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     18566:                        // transmitter holding/shift registers will be empty
                   18567:                        sio[c].line_stat_buf |= 0x60;
                   18568:                }
                   18569:                LeaveCriticalSection(&sio_mt[c].csSendData);
                   18570:        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   18571:                // transmitter shift register will be empty
                   18572:                sio[c].line_stat_buf |= 0x40;
                   18573:        }
                   18574:        if(!(sio[c].line_stat_buf & 0x01)) {
                   18575:                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     18576:                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     18577:                        // data is ready
                   18578:                        sio[c].line_stat_buf |= 0x01;
                   18579:                }
                   18580:                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   18581:        }
                   18582:        sio_update_irq(c);
                   18583: }
                   18584: 
                   18585: void sio_update_irq(int c)
                   18586: {
                   18587:        int level = -1;
                   18588:        
                   18589:        if(sio[c].irq_enable & 0x08) {
                   18590:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   18591:                if((sio[c].modem_stat & 0x0f) != 0) {
                   18592:                        level = 0;
                   18593:                }
                   18594:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   18595:        }
                   18596:        if(sio[c].irq_enable & 0x02) {
                   18597:                if(sio[c].line_stat_buf & 0x20) {
                   18598:                        level = 1;
                   18599:                }
                   18600:        }
                   18601:        if(sio[c].irq_enable & 0x01) {
                   18602:                if(sio[c].line_stat_buf & 0x01) {
                   18603:                        level = 2;
                   18604:                }
                   18605:        }
                   18606:        if(sio[c].irq_enable & 0x04) {
                   18607:                EnterCriticalSection(&sio_mt[c].csLineStat);
                   18608:                if(sio[c].line_stat_err != 0) {
                   18609:                        level = 3;
                   18610:                }
                   18611:                LeaveCriticalSection(&sio_mt[c].csLineStat);
                   18612:        }
1.1.1.29  root     18613:        
                   18614:        // COM1 and COM3 shares IRQ4, COM2 and COM4 shares IRQ3
1.1.1.25  root     18615:        if(level != -1) {
                   18616:                sio[c].irq_identify = level << 1;
1.1.1.29  root     18617:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 1);
1.1.1.25  root     18618:        } else {
                   18619:                sio[c].irq_identify = 1;
1.1.1.29  root     18620:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 0);
1.1.1.25  root     18621:        }
                   18622: }
                   18623: 
                   18624: DWORD WINAPI sio_thread(void *lpx)
                   18625: {
                   18626:        volatile sio_t *p = (sio_t *)lpx;
                   18627:        sio_mt_t *q = &sio_mt[p->channel];
                   18628:        
                   18629:        char name[] = "COM1";
1.1.1.26  root     18630:        name[3] = '0' + sio_port_number[p->channel];
                   18631:        HANDLE hComm = NULL;
                   18632:        COMMPROP commProp;
                   18633:        DCB dcb;
                   18634:        DWORD dwSettableBaud = 0xffb; // 75, 110, 150, 300, 600, 1200, 1800, 2400, 4800, 7200, and 9600bps
                   18635:        BYTE bytBuffer[SIO_BUFFER_SIZE];
                   18636:        
                   18637:        if((hComm = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL)) != INVALID_HANDLE_VALUE) {
                   18638:                if(GetCommProperties(hComm, &commProp)) {
                   18639:                        dwSettableBaud = commProp.dwSettableBaud;
                   18640:                }
1.1.1.25  root     18641:                EscapeCommFunction(hComm, CLRBREAK);
1.1.1.26  root     18642: //             EscapeCommFunction(hComm, SETRTS);
                   18643: //             EscapeCommFunction(hComm, SETDTR);
1.1.1.25  root     18644:                
                   18645:                while(!m_halted) {
                   18646:                        // setup comm port
                   18647:                        bool comm_state_changed = false;
                   18648:                        
                   18649:                        EnterCriticalSection(&q->csLineCtrl);
                   18650:                        if((p->prev_divisor != p->divisor.w || p->prev_line_ctrl != p->line_ctrl) && p->divisor.w != 0) {
                   18651:                                p->prev_divisor = p->divisor.w;
                   18652:                                p->prev_line_ctrl = p->line_ctrl;
                   18653:                                comm_state_changed = true;
                   18654:                        }
                   18655:                        LeaveCriticalSection(&q->csLineCtrl);
                   18656:                        
                   18657:                        if(comm_state_changed) {
1.1.1.26  root     18658:                                if(GetCommState(hComm, &dcb)) {
                   18659: //                                     dcb.BaudRate = min(9600, 115200 / p->prev_divisor);
                   18660:                                        DWORD baud = 115200 / p->prev_divisor;
                   18661:                                        dcb.BaudRate = 9600; // default
                   18662:                                        
                   18663:                                        if((dwSettableBaud & BAUD_075  ) && baud >= 75   ) dcb.BaudRate = 75;
                   18664:                                        if((dwSettableBaud & BAUD_110  ) && baud >= 110  ) dcb.BaudRate = 110;
                   18665:                                        // 134.5bps is not supported ???
                   18666: //                                     if((dwSettableBaud & BAUD_134_5) && baud >= 134.5) dcb.BaudRate = 134.5;
                   18667:                                        if((dwSettableBaud & BAUD_150  ) && baud >= 150  ) dcb.BaudRate = 150;
                   18668:                                        if((dwSettableBaud & BAUD_300  ) && baud >= 300  ) dcb.BaudRate = 300;
                   18669:                                        if((dwSettableBaud & BAUD_600  ) && baud >= 600  ) dcb.BaudRate = 600;
                   18670:                                        if((dwSettableBaud & BAUD_1200 ) && baud >= 1200 ) dcb.BaudRate = 1200;
                   18671:                                        if((dwSettableBaud & BAUD_1800 ) && baud >= 1800 ) dcb.BaudRate = 1800;
                   18672:                                        if((dwSettableBaud & BAUD_2400 ) && baud >= 2400 ) dcb.BaudRate = 2400;
                   18673:                                        if((dwSettableBaud & BAUD_4800 ) && baud >= 4800 ) dcb.BaudRate = 4800;
                   18674:                                        if((dwSettableBaud & BAUD_7200 ) && baud >= 7200 ) dcb.BaudRate = 7200;
                   18675:                                        if((dwSettableBaud & BAUD_9600 ) && baud >= 9600 ) dcb.BaudRate = 9600;
                   18676: //                                     if((dwSettableBaud & BAUD_14400) && baud >= 14400) dcb.BaudRate = 14400;
                   18677: //                                     if((dwSettableBaud & BAUD_19200) && baud >= 19200) dcb.BaudRate = 19200;
                   18678: //                                     if((dwSettableBaud & BAUD_38400) && baud >= 38400) dcb.BaudRate = 38400;
                   18679:                                        
                   18680:                                        switch(p->prev_line_ctrl & 0x03) {
                   18681:                                        case 0x00: dcb.ByteSize = 5; break;
                   18682:                                        case 0x01: dcb.ByteSize = 6; break;
                   18683:                                        case 0x02: dcb.ByteSize = 7; break;
                   18684:                                        case 0x03: dcb.ByteSize = 8; break;
                   18685:                                        }
                   18686:                                        switch(p->prev_line_ctrl & 0x04) {
                   18687:                                        case 0x00: dcb.StopBits = ONESTOPBIT; break;
                   18688:                                        case 0x04: dcb.StopBits = (dcb.ByteSize == 5) ? ONE5STOPBITS : TWOSTOPBITS; break;
                   18689:                                        }
                   18690:                                        switch(p->prev_line_ctrl & 0x38) {
                   18691:                                        case 0x08: dcb.Parity = ODDPARITY;   break;
                   18692:                                        case 0x18: dcb.Parity = EVENPARITY;  break;
                   18693:                                        case 0x28: dcb.Parity = MARKPARITY;  break;
                   18694:                                        case 0x38: dcb.Parity = SPACEPARITY; break;
                   18695:                                        default:   dcb.Parity = NOPARITY;    break;
                   18696:                                        }
                   18697:                                        dcb.fBinary = TRUE;
                   18698:                                        dcb.fParity = (dcb.Parity != NOPARITY);
                   18699:                                        dcb.fOutxCtsFlow = dcb.fOutxDsrFlow = TRUE;
                   18700:                                        dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
                   18701:                                        dcb.fDsrSensitivity = FALSE;//TRUE;
                   18702:                                        dcb.fTXContinueOnXoff = TRUE;
                   18703:                                        dcb.fOutX = dcb.fInX = FALSE;
                   18704:                                        dcb.fErrorChar = FALSE;
                   18705:                                        dcb.fNull = FALSE;
                   18706:                                        dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
                   18707:                                        dcb.fAbortOnError = FALSE;
                   18708:                                        
                   18709:                                        SetCommState(hComm, &dcb);
1.1.1.25  root     18710:                                }
                   18711:                                
                   18712:                                // check again to apply all comm state changes
                   18713:                                Sleep(10);
                   18714:                                continue;
                   18715:                        }
                   18716:                        
                   18717:                        // set comm pins
                   18718:                        bool change_brk = false;
1.1.1.26  root     18719: //                     bool change_rts = false;
                   18720: //                     bool change_dtr = false;
1.1.1.25  root     18721:                        
                   18722:                        EnterCriticalSection(&q->csModemCtrl);
                   18723:                        if(p->prev_set_brk != p->set_brk) {
                   18724:                                p->prev_set_brk = p->set_brk;
                   18725:                                change_brk = true;
                   18726:                        }
1.1.1.26  root     18727: //                     if(p->prev_set_rts != p->set_rts) {
                   18728: //                             p->prev_set_rts = p->set_rts;
                   18729: //                             change_rts = true;
                   18730: //                     }
                   18731: //                     if(p->prev_set_dtr != p->set_dtr) {
                   18732: //                             p->prev_set_dtr = p->set_dtr;
                   18733: //                             change_dtr = true;
                   18734: //                     }
1.1.1.25  root     18735:                        LeaveCriticalSection(&q->csModemCtrl);
                   18736:                        
                   18737:                        if(change_brk) {
1.1.1.26  root     18738:                                static UINT32 clear_time = 0;
                   18739:                                if(p->prev_set_brk) {
                   18740:                                        EscapeCommFunction(hComm, SETBREAK);
                   18741:                                        clear_time = timeGetTime() + 200;
                   18742:                                } else {
                   18743:                                        // keep break for at least 200msec
                   18744:                                        UINT32 cur_time = timeGetTime();
                   18745:                                        if(clear_time > cur_time) {
                   18746:                                                Sleep(clear_time - cur_time);
                   18747:                                        }
                   18748:                                        EscapeCommFunction(hComm, CLRBREAK);
                   18749:                                }
1.1.1.25  root     18750:                        }
1.1.1.26  root     18751: //                     if(change_rts) {
                   18752: //                             if(p->prev_set_rts) {
                   18753: //                                     EscapeCommFunction(hComm, SETRTS);
                   18754: //                             } else {
                   18755: //                                     EscapeCommFunction(hComm, CLRRTS);
                   18756: //                             }
                   18757: //                     }
                   18758: //                     if(change_dtr) {
                   18759: //                             if(p->prev_set_dtr) {
                   18760: //                                     EscapeCommFunction(hComm, SETDTR);
                   18761: //                             } else {
                   18762: //                                     EscapeCommFunction(hComm, CLRDTR);
                   18763: //                             }
                   18764: //                     }
1.1.1.25  root     18765:                        
                   18766:                        // get comm pins
                   18767:                        DWORD dwModemStat = 0;
                   18768:                        
                   18769:                        if(GetCommModemStatus(hComm, &dwModemStat)) {
                   18770:                                EnterCriticalSection(&q->csModemStat);
                   18771:                                if(dwModemStat & MS_RLSD_ON) {
                   18772:                                        p->modem_stat |= 0x80;
                   18773:                                } else {
                   18774:                                        p->modem_stat &= ~0x80;
                   18775:                                }
                   18776:                                if(dwModemStat & MS_RING_ON) {
                   18777:                                        p->modem_stat |= 0x40;
                   18778:                                } else {
                   18779:                                        p->modem_stat &= ~0x40;
                   18780:                                }
1.1.1.26  root     18781: //                             if(dwModemStat & MS_DSR_ON) {
                   18782: //                                     p->modem_stat |= 0x20;
                   18783: //                             } else {
                   18784: //                                     p->modem_stat &= ~0x20;
                   18785: //                             }
                   18786: //                             if(dwModemStat & MS_CTS_ON) {
                   18787: //                                     p->modem_stat |= 0x10;
                   18788: //                             } else {
                   18789: //                                     p->modem_stat &= ~0x10;
                   18790: //                             }
1.1.1.25  root     18791:                                if((p->prev_modem_stat & 0x80) != (p->modem_stat & 0x80)) {
                   18792:                                        p->modem_stat |= 0x08;
                   18793:                                }
                   18794:                                if((p->prev_modem_stat & 0x40) && !(p->modem_stat & 0x40)) {
                   18795:                                        p->modem_stat |= 0x04;
                   18796:                                }
1.1.1.26  root     18797: //                             if((p->prev_modem_stat & 0x20) != (p->modem_stat & 0x20)) {
                   18798: //                                     p->modem_stat |= 0x02;
                   18799: //                             }
                   18800: //                             if((p->prev_modem_stat & 0x10) != (p->modem_stat & 0x10)) {
                   18801: //                                     p->modem_stat |= 0x01;
                   18802: //                             }
1.1.1.25  root     18803:                                LeaveCriticalSection(&q->csModemStat);
                   18804:                        }
                   18805:                        
                   18806:                        // send data
                   18807:                        DWORD dwSend = 0;
                   18808:                        
                   18809:                        EnterCriticalSection(&q->csSendData);
1.1.1.32  root     18810:                        while(p->send_buffer != NULL && !p->send_buffer->empty()) {
1.1.1.25  root     18811:                                bytBuffer[dwSend++] = p->send_buffer->read();
                   18812:                        }
                   18813:                        LeaveCriticalSection(&q->csSendData);
                   18814:                        
                   18815:                        if(dwSend != 0) {
                   18816:                                DWORD dwWritten = 0;
                   18817:                                WriteFile(hComm, bytBuffer, dwSend, &dwWritten, NULL);
                   18818:                        }
                   18819:                        
                   18820:                        // get line status and recv data
                   18821:                        DWORD dwLineStat = 0;
                   18822:                        COMSTAT comStat;
                   18823:                        
                   18824:                        if(ClearCommError(hComm, &dwLineStat, &comStat)) {
                   18825:                                EnterCriticalSection(&q->csLineStat);
                   18826:                                if(dwLineStat & CE_BREAK) {
                   18827:                                        p->line_stat_err |= 0x10;
                   18828:                                }
                   18829:                                if(dwLineStat & CE_FRAME) {
                   18830:                                        p->line_stat_err |= 0x08;
                   18831:                                }
                   18832:                                if(dwLineStat & CE_RXPARITY) {
                   18833:                                        p->line_stat_err |= 0x04;
                   18834:                                }
                   18835:                                if(dwLineStat & CE_OVERRUN) {
                   18836:                                        p->line_stat_err |= 0x02;
                   18837:                                }
                   18838:                                LeaveCriticalSection(&q->csLineStat);
                   18839:                                
                   18840:                                if(comStat.cbInQue != 0) {
                   18841:                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     18842:                                        DWORD dwRecv = 0;
                   18843:                                        if(p->recv_buffer != NULL) {
                   18844:                                                dwRecv = min(comStat.cbInQue, p->recv_buffer->remain());
                   18845:                                        }
1.1.1.25  root     18846:                                        LeaveCriticalSection(&q->csRecvData);
                   18847:                                        
                   18848:                                        if(dwRecv != 0) {
                   18849:                                                DWORD dwRead = 0;
                   18850:                                                if(ReadFile(hComm, bytBuffer, dwRecv, &dwRead, NULL) && dwRead != 0) {
                   18851:                                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     18852:                                                        if(p->recv_buffer != NULL) {
                   18853:                                                                for(int i = 0; i < dwRead; i++) {
                   18854:                                                                        p->recv_buffer->write(bytBuffer[i]);
                   18855:                                                                }
1.1.1.25  root     18856:                                                        }
                   18857:                                                        LeaveCriticalSection(&q->csRecvData);
                   18858:                                                }
                   18859:                                        }
                   18860:                                }
                   18861:                        }
                   18862:                        Sleep(10);
                   18863:                }
                   18864:                CloseHandle(hComm);
                   18865:        }
                   18866:        return 0;
                   18867: }
                   18868: 
1.1.1.8   root     18869: // cmos
                   18870: 
                   18871: void cmos_init()
                   18872: {
                   18873:        memset(cmos, 0, sizeof(cmos));
                   18874:        cmos_addr = 0;
1.1       root     18875:        
1.1.1.8   root     18876:        // from DOSBox
                   18877:        cmos_write(0x0a, 0x26);
                   18878:        cmos_write(0x0b, 0x02);
                   18879:        cmos_write(0x0d, 0x80);
1.1       root     18880: }
                   18881: 
1.1.1.8   root     18882: void cmos_write(int addr, UINT8 val)
1.1       root     18883: {
1.1.1.8   root     18884:        cmos[addr & 0x7f] = val;
                   18885: }
                   18886: 
                   18887: #define CMOS_GET_TIME() { \
                   18888:        UINT32 cur_sec = timeGetTime() / 1000 ; \
                   18889:        if(prev_sec != cur_sec) { \
                   18890:                GetLocalTime(&time); \
                   18891:                prev_sec = cur_sec; \
                   18892:        } \
1.1       root     18893: }
1.1.1.8   root     18894: #define CMOS_BCD(v) ((cmos[0x0b] & 4) ? (v) : to_bcd(v))
1.1       root     18895: 
1.1.1.8   root     18896: UINT8 cmos_read(int addr)
1.1       root     18897: {
1.1.1.8   root     18898:        static SYSTEMTIME time;
                   18899:        static UINT32 prev_sec = 0;
1.1       root     18900:        
1.1.1.8   root     18901:        switch(addr & 0x7f) {
                   18902:        case 0x00: CMOS_GET_TIME(); return(CMOS_BCD(time.wSecond));
                   18903:        case 0x02: CMOS_GET_TIME(); return(CMOS_BCD(time.wMinute));
                   18904:        case 0x04: CMOS_GET_TIME(); return(CMOS_BCD(time.wHour));
                   18905:        case 0x06: CMOS_GET_TIME(); return(time.wDayOfWeek + 1);
                   18906:        case 0x07: CMOS_GET_TIME(); return(CMOS_BCD(time.wDay));
                   18907:        case 0x08: CMOS_GET_TIME(); return(CMOS_BCD(time.wMonth));
                   18908:        case 0x09: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear));
                   18909: //     case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 2 ? 0x80 : 0));       // 2msec
                   18910:        case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 20 ? 0x80 : 0));      // precision of timeGetTime() may not be 1msec
                   18911:        case 0x15: return((MEMORY_END >> 10) & 0xff);
                   18912:        case 0x16: return((MEMORY_END >> 18) & 0xff);
                   18913:        case 0x17: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   18914:        case 0x18: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   18915:        case 0x30: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   18916:        case 0x31: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   18917:        case 0x32: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear / 100));
1.1       root     18918:        }
1.1.1.8   root     18919:        return(cmos[addr & 0x7f]);
1.1       root     18920: }
                   18921: 
1.1.1.7   root     18922: // kbd (a20)
                   18923: 
                   18924: void kbd_init()
                   18925: {
1.1.1.8   root     18926:        kbd_data = kbd_command = 0;
1.1.1.7   root     18927:        kbd_status = 0x18;
                   18928: }
                   18929: 
                   18930: UINT8 kbd_read_data()
                   18931: {
1.1.1.8   root     18932:        kbd_status &= ~1;
1.1.1.7   root     18933:        return(kbd_data);
                   18934: }
                   18935: 
                   18936: void kbd_write_data(UINT8 val)
                   18937: {
                   18938:        switch(kbd_command) {
                   18939:        case 0xd1:
                   18940:                i386_set_a20_line((val >> 1) & 1);
                   18941:                break;
                   18942:        }
                   18943:        kbd_command = 0;
1.1.1.8   root     18944:        kbd_status &= ~8;
1.1.1.7   root     18945: }
                   18946: 
                   18947: UINT8 kbd_read_status()
                   18948: {
                   18949:        return(kbd_status);
                   18950: }
                   18951: 
                   18952: void kbd_write_command(UINT8 val)
                   18953: {
                   18954:        switch(val) {
                   18955:        case 0xd0:
                   18956:                kbd_data = ((m_a20_mask >> 19) & 2) | 1;
1.1.1.8   root     18957:                kbd_status |= 1;
1.1.1.7   root     18958:                break;
                   18959:        case 0xdd:
                   18960:                i386_set_a20_line(0);
                   18961:                break;
                   18962:        case 0xdf:
                   18963:                i386_set_a20_line(1);
                   18964:                break;
1.1.1.26  root     18965:        case 0xf0: case 0xf1: case 0xf2: case 0xf3: case 0xf4: case 0xf5: case 0xf6: case 0xf7:
                   18966:        case 0xf8: case 0xf9: case 0xfa: case 0xfb: case 0xfc: case 0xfd: case 0xfe: case 0xff:
1.1.1.7   root     18967:                if(!(val & 1)) {
1.1.1.8   root     18968:                        if((cmos[0x0f] & 0x7f) == 5) {
1.1.1.7   root     18969:                                // reset pic
                   18970:                                pic_init();
                   18971:                                pic[0].irr = pic[1].irr = 0x00;
                   18972:                                pic[0].imr = pic[1].imr = 0xff;
                   18973:                        }
                   18974:                        CPU_RESET_CALL(CPU_MODEL);
1.1.1.40  root     18975:                        UINT16 address = *(UINT16 *)(mem + 0x467);
                   18976:                        UINT16 selector = *(UINT16 *)(mem + 0x469);
                   18977:                        i386_jmp_far(selector, address);
1.1.1.7   root     18978:                }
                   18979:                i386_set_a20_line((val >> 1) & 1);
                   18980:                break;
                   18981:        }
                   18982:        kbd_command = val;
1.1.1.8   root     18983:        kbd_status |= 8;
1.1.1.7   root     18984: }
                   18985: 
1.1.1.9   root     18986: // vga
                   18987: 
                   18988: UINT8 vga_read_status()
                   18989: {
                   18990:        // 60hz
                   18991:        static const int period[3] = {16, 17, 17};
                   18992:        static int index = 0;
                   18993:        UINT32 time = timeGetTime() % period[index];
                   18994:        
                   18995:        index = (index + 1) % 3;
1.1.1.14  root     18996:        return((time < 4 ? 0x08 : 0) | (time == 0 ? 0 : 0x01));
1.1.1.9   root     18997: }
                   18998: 
1.1       root     18999: // i/o bus
                   19000: 
1.1.1.29  root     19001: // this is ugly patch for SW1US.EXE, it sometimes mistakely read/write 01h-10h for serial I/O
                   19002: //#define SW1US_PATCH
                   19003: 
1.1.1.25  root     19004: UINT8 read_io_byte(offs_t addr)
1.1.1.33  root     19005: #ifdef USE_DEBUGGER
1.1.1.25  root     19006: {
1.1.1.33  root     19007:        if(now_debugging) {
                   19008:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   19009:                        if(in_break_point.table[i].status == 1) {
                   19010:                                if(addr == in_break_point.table[i].addr) {
                   19011:                                        in_break_point.hit = i + 1;
                   19012:                                        now_suspended = true;
                   19013:                                        break;
                   19014:                                }
                   19015:                        }
                   19016:                }
1.1.1.25  root     19017:        }
1.1.1.33  root     19018:        return(debugger_read_io_byte(addr));
1.1.1.25  root     19019: }
1.1.1.33  root     19020: UINT8 debugger_read_io_byte(offs_t addr)
1.1.1.25  root     19021: #endif
1.1       root     19022: {
1.1.1.33  root     19023:        UINT8 val = 0xff;
                   19024:        
1.1       root     19025:        switch(addr) {
1.1.1.29  root     19026: #ifdef SW1US_PATCH
                   19027:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   19028:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
1.1.1.33  root     19029:                val = sio_read(0, addr - 1);
                   19030:                break;
1.1.1.29  root     19031: #else
1.1.1.25  root     19032:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   19033:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
1.1.1.33  root     19034:                val = dma_read(0, addr);
                   19035:                break;
1.1.1.29  root     19036: #endif
1.1.1.25  root     19037:        case 0x20: case 0x21:
1.1.1.33  root     19038:                val = pic_read(0, addr);
                   19039:                break;
1.1.1.25  root     19040:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1.1.33  root     19041:                val = pit_read(addr & 0x03);
                   19042:                break;
1.1.1.7   root     19043:        case 0x60:
1.1.1.33  root     19044:                val = kbd_read_data();
                   19045:                break;
1.1.1.9   root     19046:        case 0x61:
1.1.1.33  root     19047:                val = system_port;
                   19048:                break;
1.1.1.7   root     19049:        case 0x64:
1.1.1.33  root     19050:                val = kbd_read_status();
                   19051:                break;
1.1       root     19052:        case 0x71:
1.1.1.33  root     19053:                val = cmos_read(cmos_addr);
                   19054:                break;
1.1.1.25  root     19055:        case 0x81:
1.1.1.33  root     19056:                val = dma_page_read(0, 2);
                   19057:                break;
1.1.1.25  root     19058:        case 0x82:
1.1.1.33  root     19059:                val = dma_page_read(0, 3);
                   19060:                break;
1.1.1.25  root     19061:        case 0x83:
1.1.1.33  root     19062:                val = dma_page_read(0, 1);
                   19063:                break;
1.1.1.25  root     19064:        case 0x87:
1.1.1.33  root     19065:                val = dma_page_read(0, 0);
                   19066:                break;
1.1.1.25  root     19067:        case 0x89:
1.1.1.33  root     19068:                val = dma_page_read(1, 2);
                   19069:                break;
1.1.1.25  root     19070:        case 0x8a:
1.1.1.33  root     19071:                val = dma_page_read(1, 3);
                   19072:                break;
1.1.1.25  root     19073:        case 0x8b:
1.1.1.33  root     19074:                val = dma_page_read(1, 1);
                   19075:                break;
1.1.1.25  root     19076:        case 0x8f:
1.1.1.33  root     19077:                val = dma_page_read(1, 0);
                   19078:                break;
1.1       root     19079:        case 0x92:
1.1.1.33  root     19080:                val = (m_a20_mask >> 19) & 2;
                   19081:                break;
1.1.1.25  root     19082:        case 0xa0: case 0xa1:
1.1.1.33  root     19083:                val = pic_read(1, addr);
                   19084:                break;
1.1.1.25  root     19085:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   19086:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.33  root     19087:                val = dma_read(1, (addr - 0xc0) >> 1);
                   19088:                break;
1.1.1.37  root     19089:        case 0x278: case 0x279: case 0x27a:
                   19090:                val = pio_read(1, addr);
                   19091:                break;
1.1.1.29  root     19092:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
1.1.1.33  root     19093:                val = sio_read(3, addr);
                   19094:                break;
1.1.1.25  root     19095:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
1.1.1.33  root     19096:                val = sio_read(1, addr);
                   19097:                break;
1.1.1.25  root     19098:        case 0x378: case 0x379: case 0x37a:
1.1.1.33  root     19099:                val = pio_read(0, addr);
                   19100:                break;
1.1.1.25  root     19101:        case 0x3ba: case 0x3da:
1.1.1.33  root     19102:                val = vga_read_status();
                   19103:                break;
1.1.1.37  root     19104:        case 0x3bc: case 0x3bd: case 0x3be:
                   19105:                val = pio_read(2, addr);
                   19106:                break;
1.1.1.29  root     19107:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
1.1.1.33  root     19108:                val = sio_read(2, addr);
                   19109:                break;
1.1.1.25  root     19110:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
1.1.1.33  root     19111:                val = sio_read(0, addr);
                   19112:                break;
1.1       root     19113:        default:
1.1.1.33  root     19114: //             fatalerror("unknown inb %4x\n", addr);
1.1       root     19115:                break;
                   19116:        }
1.1.1.33  root     19117: #ifdef ENABLE_DEBUG_IOPORT
                   19118:        if(fp_debug_log != NULL) {
                   19119:                fprintf(fp_debug_log, "inb %04X, %02X\n", addr, val);
                   19120:        }
                   19121: #endif
                   19122:        return(val);
1.1       root     19123: }
                   19124: 
                   19125: UINT16 read_io_word(offs_t addr)
                   19126: {
                   19127:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8));
                   19128: }
                   19129: 
1.1.1.33  root     19130: #ifdef USE_DEBUGGER
                   19131: UINT16 debugger_read_io_word(offs_t addr)
                   19132: {
                   19133:        return(debugger_read_io_byte(addr) | (debugger_read_io_byte(addr + 1) << 8));
                   19134: }
                   19135: #endif
                   19136: 
1.1       root     19137: UINT32 read_io_dword(offs_t addr)
                   19138: {
                   19139:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8) | (read_io_byte(addr + 2) << 16) | (read_io_byte(addr + 3) << 24));
                   19140: }
                   19141: 
1.1.1.33  root     19142: #ifdef USE_DEBUGGER
                   19143: UINT32 debugger_read_io_dword(offs_t addr)
                   19144: {
                   19145:        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));
                   19146: }
                   19147: #endif
                   19148: 
1.1       root     19149: void write_io_byte(offs_t addr, UINT8 val)
1.1.1.33  root     19150: #ifdef USE_DEBUGGER
                   19151: {
                   19152:        if(now_debugging) {
                   19153:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   19154:                        if(out_break_point.table[i].status == 1) {
                   19155:                                if(addr == out_break_point.table[i].addr) {
                   19156:                                        out_break_point.hit = i + 1;
                   19157:                                        now_suspended = true;
                   19158:                                        break;
                   19159:                                }
                   19160:                        }
                   19161:                }
                   19162:        }
                   19163:        debugger_write_io_byte(addr, val);
                   19164: }
                   19165: void debugger_write_io_byte(offs_t addr, UINT8 val)
                   19166: #endif
1.1       root     19167: {
1.1.1.25  root     19168: #ifdef ENABLE_DEBUG_IOPORT
1.1.1.33  root     19169:        if(fp_debug_log != NULL) {
1.1.1.43  root     19170: #ifdef USE_SERVICE_THREAD
                   19171:                if(addr != 0xf7)
                   19172: #endif
1.1.1.33  root     19173:                fprintf(fp_debug_log, "outb %04X, %02X\n", addr, val);
1.1.1.25  root     19174:        }
                   19175: #endif
1.1       root     19176:        switch(addr) {
1.1.1.29  root     19177: #ifdef SW1US_PATCH
                   19178:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   19179:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
                   19180:                sio_write(0, addr - 1, val);
                   19181:                break;
                   19182: #else
1.1.1.25  root     19183:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   19184:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
                   19185:                dma_write(0, addr, val);
                   19186:                break;
1.1.1.29  root     19187: #endif
1.1.1.25  root     19188:        case 0x20: case 0x21:
1.1       root     19189:                pic_write(0, addr, val);
                   19190:                break;
1.1.1.25  root     19191:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1       root     19192:                pit_write(addr & 0x03, val);
                   19193:                break;
1.1.1.7   root     19194:        case 0x60:
                   19195:                kbd_write_data(val);
                   19196:                break;
1.1.1.9   root     19197:        case 0x61:
                   19198:                if((system_port & 3) != 3 && (val & 3) == 3) {
                   19199:                        // beep on
                   19200: //                     MessageBeep(-1);
                   19201:                } else if((system_port & 3) == 3 && (val & 3) != 3) {
                   19202:                        // beep off
                   19203:                }
                   19204:                system_port = val;
                   19205:                break;
1.1       root     19206:        case 0x64:
1.1.1.7   root     19207:                kbd_write_command(val);
1.1       root     19208:                break;
                   19209:        case 0x70:
                   19210:                cmos_addr = val;
                   19211:                break;
                   19212:        case 0x71:
1.1.1.8   root     19213:                cmos_write(cmos_addr, val);
1.1       root     19214:                break;
1.1.1.25  root     19215:        case 0x81:
                   19216:                dma_page_write(0, 2, val);
                   19217:        case 0x82:
                   19218:                dma_page_write(0, 3, val);
                   19219:        case 0x83:
                   19220:                dma_page_write(0, 1, val);
                   19221:        case 0x87:
                   19222:                dma_page_write(0, 0, val);
                   19223:        case 0x89:
                   19224:                dma_page_write(1, 2, val);
                   19225:        case 0x8a:
                   19226:                dma_page_write(1, 3, val);
                   19227:        case 0x8b:
                   19228:                dma_page_write(1, 1, val);
                   19229:        case 0x8f:
                   19230:                dma_page_write(1, 0, val);
1.1       root     19231:        case 0x92:
1.1.1.7   root     19232:                i386_set_a20_line((val >> 1) & 1);
1.1       root     19233:                break;
1.1.1.25  root     19234:        case 0xa0: case 0xa1:
1.1       root     19235:                pic_write(1, addr, val);
                   19236:                break;
1.1.1.25  root     19237:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   19238:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.26  root     19239:                dma_write(1, (addr - 0xc0) >> 1, val);
1.1.1.25  root     19240:                break;
1.1.1.35  root     19241: #ifdef USE_SERVICE_THREAD
                   19242:        case 0xf7:
                   19243:                // dummy i/o for BIOS/DOS service
1.1.1.36  root     19244:                if(in_service && cursor_moved) {
                   19245:                        // update cursor position before service is done
                   19246:                        pcbios_update_cursor_position();
                   19247:                        cursor_moved = false;
                   19248:                }
1.1.1.35  root     19249:                finish_service_loop();
                   19250:                break;
                   19251: #endif
1.1.1.37  root     19252:        case 0x278: case 0x279: case 0x27a:
                   19253:                pio_write(1, addr, val);
                   19254:                break;
1.1.1.29  root     19255:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
                   19256:                sio_write(3, addr, val);
                   19257:                break;
1.1.1.25  root     19258:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
                   19259:                sio_write(1, addr, val);
                   19260:                break;
                   19261:        case 0x378: case 0x379: case 0x37a:
                   19262:                pio_write(0, addr, val);
                   19263:                break;
1.1.1.37  root     19264:        case 0x3bc: case 0x3bd: case 0x3be:
                   19265:                pio_write(2, addr, val);
                   19266:                break;
1.1.1.29  root     19267:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
                   19268:                sio_write(2, addr, val);
                   19269:                break;
1.1.1.25  root     19270:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
                   19271:                sio_write(0, addr, val);
                   19272:                break;
1.1       root     19273:        default:
1.1.1.33  root     19274: //             fatalerror("unknown outb %4x,%2x\n", addr, val);
1.1       root     19275:                break;
                   19276:        }
                   19277: }
                   19278: 
                   19279: void write_io_word(offs_t addr, UINT16 val)
                   19280: {
                   19281:        write_io_byte(addr + 0, (val >> 0) & 0xff);
                   19282:        write_io_byte(addr + 1, (val >> 8) & 0xff);
                   19283: }
                   19284: 
1.1.1.33  root     19285: #ifdef USE_DEBUGGER
                   19286: void debugger_write_io_word(offs_t addr, UINT16 val)
                   19287: {
                   19288:        debugger_write_io_byte(addr + 0, (val >> 0) & 0xff);
                   19289:        debugger_write_io_byte(addr + 1, (val >> 8) & 0xff);
                   19290: }
                   19291: #endif
                   19292: 
1.1       root     19293: void write_io_dword(offs_t addr, UINT32 val)
                   19294: {
                   19295:        write_io_byte(addr + 0, (val >>  0) & 0xff);
                   19296:        write_io_byte(addr + 1, (val >>  8) & 0xff);
                   19297:        write_io_byte(addr + 2, (val >> 16) & 0xff);
                   19298:        write_io_byte(addr + 3, (val >> 24) & 0xff);
                   19299: }
1.1.1.33  root     19300: 
                   19301: #ifdef USE_DEBUGGER
                   19302: void debugger_write_io_dword(offs_t addr, UINT32 val)
                   19303: {
                   19304:        debugger_write_io_byte(addr + 0, (val >>  0) & 0xff);
                   19305:        debugger_write_io_byte(addr + 1, (val >>  8) & 0xff);
                   19306:        debugger_write_io_byte(addr + 2, (val >> 16) & 0xff);
                   19307:        debugger_write_io_byte(addr + 3, (val >> 24) & 0xff);
                   19308: }
                   19309: #endif

unix.superglobalmegacorp.com

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