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

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
1.1.1.35  root      351:                if(key_buf_char != NULL && key_buf_scan != NULL) {
                    352: #ifdef USE_SERVICE_THREAD
                    353:                        EnterCriticalSection(&key_buf_crit_sect);
                    354: #endif
1.1.1.51  root      355:                        bool empty = key_buf_char->empty();
1.1.1.35  root      356: #ifdef USE_SERVICE_THREAD
                    357:                        LeaveCriticalSection(&key_buf_crit_sect);
                    358: #endif
1.1.1.51  root      359:                        if(empty) maybe_idle();
1.1.1.14  root      360:                }
                    361:        }
1.1.1.4   root      362: #if defined(HAS_I386)
1.1       root      363:        if(byteaddress < MAX_MEM - 1) {
                    364:                return *(UINT16 *)(mem + byteaddress);
1.1.1.3   root      365: //     } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
                    366: //             return read_word(byteaddress & 0xfffff);
1.1       root      367:        }
                    368:        return 0;
1.1.1.4   root      369: #else
                    370:        return *(UINT16 *)(mem + byteaddress);
                    371: #endif
1.1       root      372: }
                    373: 
                    374: UINT32 read_dword(offs_t byteaddress)
1.1.1.33  root      375: #ifdef USE_DEBUGGER
                    376: {
                    377:        if(now_debugging) {
                    378:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    379:                        if(rd_break_point.table[i].status == 1) {
                    380:                                if(byteaddress >= rd_break_point.table[i].addr && byteaddress < rd_break_point.table[i].addr + 4) {
                    381:                                        rd_break_point.hit = i + 1;
                    382:                                        now_suspended = true;
                    383:                                        break;
                    384:                                }
                    385:                        }
                    386:                }
                    387:        }
                    388:        return(debugger_read_dword(byteaddress));
                    389: }
                    390: UINT32 debugger_read_dword(offs_t byteaddress)
                    391: #endif
1.1       root      392: {
1.1.1.4   root      393: #if defined(HAS_I386)
1.1       root      394:        if(byteaddress < MAX_MEM - 3) {
                    395:                return *(UINT32 *)(mem + byteaddress);
1.1.1.3   root      396: //     } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
                    397: //             return read_dword(byteaddress & 0xfffff);
1.1       root      398:        }
                    399:        return 0;
1.1.1.4   root      400: #else
                    401:        return *(UINT32 *)(mem + byteaddress);
                    402: #endif
1.1       root      403: }
                    404: 
                    405: // write accessors
1.1.1.35  root      406: #ifdef USE_VRAM_THREAD
1.1.1.14  root      407: void vram_flush_char()
                    408: {
                    409:        if(vram_length_char != 0) {
                    410:                DWORD num;
1.1.1.23  root      411:                WriteConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), scr_char, vram_length_char, vram_coord_char, &num);
1.1.1.14  root      412:                vram_length_char = vram_last_length_char = 0;
                    413:        }
                    414: }
                    415: 
                    416: void vram_flush_attr()
                    417: {
                    418:        if(vram_length_attr != 0) {
                    419:                DWORD num;
1.1.1.23  root      420:                WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), scr_attr, vram_length_attr, vram_coord_attr, &num);
1.1.1.14  root      421:                vram_length_attr = vram_last_length_attr = 0;
                    422:        }
                    423: }
                    424: 
                    425: void vram_flush()
                    426: {
                    427:        if(vram_length_char != 0 || vram_length_attr != 0) {
                    428:                EnterCriticalSection(&vram_crit_sect);
                    429:                vram_flush_char();
                    430:                vram_flush_attr();
                    431:                LeaveCriticalSection(&vram_crit_sect);
                    432:        }
                    433: }
                    434: #endif
                    435: 
                    436: void write_text_vram_char(offs_t offset, UINT8 data)
1.1.1.8   root      437: {
1.1.1.35  root      438: #ifdef USE_VRAM_THREAD
1.1.1.14  root      439:        static offs_t first_offset_char, last_offset_char;
                    440:        
                    441:        if(vram_length_char != 0) {
                    442:                if(offset <= last_offset_char && offset >= first_offset_char) {
                    443:                        scr_char[(offset - first_offset_char) >> 1] = data;
                    444:                        return;
                    445:                }
                    446:                if(offset != last_offset_char + 2) {
                    447:                        vram_flush_char();
                    448:                }
                    449:        }
                    450:        if(vram_length_char == 0) {
                    451:                first_offset_char = offset;
                    452:                vram_coord_char.X = (offset >> 1) % scr_width;
                    453:                vram_coord_char.Y = (offset >> 1) / scr_width + scr_top;
                    454:        }
                    455:        scr_char[vram_length_char++] = data;
                    456:        last_offset_char = offset;
                    457: #else
1.1.1.8   root      458:        COORD co;
                    459:        DWORD num;
                    460:        
1.1.1.14  root      461:        co.X = (offset >> 1) % scr_width;
                    462:        co.Y = (offset >> 1) / scr_width;
                    463:        scr_char[0] = data;
1.1.1.23  root      464:        WriteConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), scr_char, 1, co, &num);
1.1.1.14  root      465: #endif
                    466: }
                    467: 
                    468: void write_text_vram_attr(offs_t offset, UINT8 data)
                    469: {
1.1.1.35  root      470: #ifdef USE_VRAM_THREAD
1.1.1.14  root      471:        static offs_t first_offset_attr, last_offset_attr;
                    472:        
                    473:        if(vram_length_attr != 0) {
                    474:                if(offset <= last_offset_attr && offset >= first_offset_attr) {
                    475:                        scr_attr[(offset - first_offset_attr) >> 1] = data;
                    476:                        return;
                    477:                }
                    478:                if(offset != last_offset_attr + 2) {
                    479:                        vram_flush_attr();
                    480:                }
                    481:        }
                    482:        if(vram_length_attr == 0) {
                    483:                first_offset_attr = offset;
                    484:                vram_coord_attr.X = (offset >> 1) % scr_width;
                    485:                vram_coord_attr.Y = (offset >> 1) / scr_width + scr_top;
                    486:        }
                    487:        scr_attr[vram_length_attr++] = data;
                    488:        last_offset_attr = offset;
                    489: #else
                    490:        COORD co;
                    491:        DWORD num;
1.1.1.8   root      492:        
1.1.1.14  root      493:        co.X = (offset >> 1) % scr_width;
                    494:        co.Y = (offset >> 1) / scr_width;
                    495:        scr_attr[0] = data;
1.1.1.23  root      496:        WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), scr_attr, 1, co, &num);
1.1.1.14  root      497: #endif
                    498: }
                    499: 
                    500: void write_text_vram_byte(offs_t offset, UINT8 data)
                    501: {
1.1.1.35  root      502: #ifdef USE_VRAM_THREAD
1.1.1.14  root      503:        EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root      504: #endif
1.1.1.8   root      505:        if(offset & 1) {
1.1.1.14  root      506:                write_text_vram_attr(offset, data);
1.1.1.8   root      507:        } else {
1.1.1.14  root      508:                write_text_vram_char(offset, data);
1.1.1.8   root      509:        }
1.1.1.35  root      510: #ifdef USE_VRAM_THREAD
1.1.1.14  root      511:        LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root      512: #endif
1.1.1.8   root      513: }
                    514: 
                    515: void write_text_vram_word(offs_t offset, UINT16 data)
                    516: {
1.1.1.35  root      517: #ifdef USE_VRAM_THREAD
1.1.1.14  root      518:        EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root      519: #endif
1.1.1.8   root      520:        if(offset & 1) {
1.1.1.14  root      521:                write_text_vram_attr(offset    , (data     ) & 0xff);
                    522:                write_text_vram_char(offset + 1, (data >> 8) & 0xff);
1.1.1.8   root      523:        } else {
1.1.1.14  root      524:                write_text_vram_char(offset    , (data     ) & 0xff);
                    525:                write_text_vram_attr(offset + 1, (data >> 8) & 0xff);
1.1.1.8   root      526:        }
1.1.1.35  root      527: #ifdef USE_VRAM_THREAD
1.1.1.14  root      528:        LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root      529: #endif
1.1.1.8   root      530: }
                    531: 
                    532: void write_text_vram_dword(offs_t offset, UINT32 data)
                    533: {
1.1.1.35  root      534: #ifdef USE_VRAM_THREAD
1.1.1.14  root      535:        EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root      536: #endif
1.1.1.8   root      537:        if(offset & 1) {
1.1.1.14  root      538:                write_text_vram_attr(offset    , (data      ) & 0xff);
                    539:                write_text_vram_char(offset + 1, (data >>  8) & 0xff);
                    540:                write_text_vram_attr(offset + 2, (data >> 16) & 0xff);
                    541:                write_text_vram_char(offset + 3, (data >> 24) & 0xff);
                    542:        } else {
                    543:                write_text_vram_char(offset    , (data      ) & 0xff);
                    544:                write_text_vram_attr(offset + 1, (data >>  8) & 0xff);
                    545:                write_text_vram_char(offset + 2, (data >> 16) & 0xff);
                    546:                write_text_vram_attr(offset + 3, (data >> 24) & 0xff);
1.1.1.8   root      547:        }
1.1.1.35  root      548: #ifdef USE_VRAM_THREAD
1.1.1.14  root      549:        LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root      550: #endif
1.1.1.8   root      551: }
                    552: 
1.1       root      553: void write_byte(offs_t byteaddress, UINT8 data)
1.1.1.33  root      554: #ifdef USE_DEBUGGER
                    555: {
                    556:        if(now_debugging) {
                    557:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    558:                        if(wr_break_point.table[i].status == 1) {
                    559:                                if(byteaddress == wr_break_point.table[i].addr) {
                    560:                                        wr_break_point.hit = i + 1;
                    561:                                        now_suspended = true;
                    562:                                        break;
                    563:                                }
                    564:                        }
                    565:                }
                    566:        }
                    567:        debugger_write_byte(byteaddress, data);
                    568: }
                    569: void debugger_write_byte(offs_t byteaddress, UINT8 data)
                    570: #endif
1.1       root      571: {
1.1.1.8   root      572:        if(byteaddress < MEMORY_END) {
1.1.1.3   root      573:                mem[byteaddress] = data;
1.1.1.8   root      574:        } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14  root      575:                if(!restore_console_on_exit) {
                    576:                        change_console_size(scr_width, scr_height);
1.1.1.12  root      577:                }
1.1.1.8   root      578:                write_text_vram_byte(byteaddress - text_vram_top_address, data);
                    579:                mem[byteaddress] = data;
                    580:        } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
                    581:                if(int_10h_feh_called && !int_10h_ffh_called) {
                    582:                        write_text_vram_byte(byteaddress - shadow_buffer_top_address, data);
1.1       root      583:                }
                    584:                mem[byteaddress] = data;
1.1.1.4   root      585: #if defined(HAS_I386)
1.1.1.3   root      586:        } else if(byteaddress < MAX_MEM) {
1.1.1.4   root      587: #else
                    588:        } else {
                    589: #endif
1.1.1.3   root      590:                mem[byteaddress] = data;
1.1       root      591:        }
                    592: }
                    593: 
                    594: void write_word(offs_t byteaddress, UINT16 data)
1.1.1.33  root      595: #ifdef USE_DEBUGGER
                    596: {
                    597:        if(now_debugging) {
                    598:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    599:                        if(wr_break_point.table[i].status == 1) {
                    600:                                if(byteaddress >= wr_break_point.table[i].addr && byteaddress < wr_break_point.table[i].addr + 2) {
                    601:                                        wr_break_point.hit = i + 1;
                    602:                                        now_suspended = true;
                    603:                                        break;
                    604:                                }
                    605:                        }
                    606:                }
                    607:        }
                    608:        debugger_write_word(byteaddress, data);
                    609: }
                    610: void debugger_write_word(offs_t byteaddress, UINT16 data)
                    611: #endif
1.1       root      612: {
1.1.1.8   root      613:        if(byteaddress < MEMORY_END) {
1.1.1.51  root      614:                if(byteaddress == cursor_position_address) {
                    615:                        if(*(UINT16 *)(mem + byteaddress) != data) {
                    616:                                COORD co;
                    617:                                co.X = data & 0xff;
                    618:                                co.Y = (data >> 8) + scr_top;
                    619:                                SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), co);
                    620:                        }
1.1.1.14  root      621:                }
1.1.1.3   root      622:                *(UINT16 *)(mem + byteaddress) = data;
1.1.1.8   root      623:        } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14  root      624:                if(!restore_console_on_exit) {
                    625:                        change_console_size(scr_width, scr_height);
1.1.1.12  root      626:                }
1.1.1.8   root      627:                write_text_vram_word(byteaddress - text_vram_top_address, data);
                    628:                *(UINT16 *)(mem + byteaddress) = data;
                    629:        } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
                    630:                if(int_10h_feh_called && !int_10h_ffh_called) {
                    631:                        write_text_vram_word(byteaddress - shadow_buffer_top_address, data);
1.1       root      632:                }
                    633:                *(UINT16 *)(mem + byteaddress) = data;
1.1.1.4   root      634: #if defined(HAS_I386)
1.1.1.3   root      635:        } else if(byteaddress < MAX_MEM - 1) {
1.1.1.4   root      636: #else
                    637:        } else {
                    638: #endif
1.1.1.3   root      639:                *(UINT16 *)(mem + byteaddress) = data;
1.1       root      640:        }
                    641: }
                    642: 
                    643: void write_dword(offs_t byteaddress, UINT32 data)
1.1.1.33  root      644: #ifdef USE_DEBUGGER
                    645: {
                    646:        if(now_debugging) {
                    647:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    648:                        if(wr_break_point.table[i].status == 1) {
                    649:                                if(byteaddress >= wr_break_point.table[i].addr && byteaddress < wr_break_point.table[i].addr + 4) {
                    650:                                        wr_break_point.hit = i + 1;
                    651:                                        now_suspended = true;
                    652:                                        break;
                    653:                                }
                    654:                        }
                    655:                }
                    656:        }
                    657:        debugger_write_dword(byteaddress, data);
                    658: }
                    659: void debugger_write_dword(offs_t byteaddress, UINT32 data)
                    660: #endif
1.1       root      661: {
1.1.1.8   root      662:        if(byteaddress < MEMORY_END) {
1.1.1.3   root      663:                *(UINT32 *)(mem + byteaddress) = data;
1.1.1.8   root      664:        } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14  root      665:                if(!restore_console_on_exit) {
                    666:                        change_console_size(scr_width, scr_height);
1.1.1.12  root      667:                }
1.1.1.8   root      668:                write_text_vram_dword(byteaddress - text_vram_top_address, data);
                    669:                *(UINT32 *)(mem + byteaddress) = data;
                    670:        } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
                    671:                if(int_10h_feh_called && !int_10h_ffh_called) {
                    672:                        write_text_vram_dword(byteaddress - shadow_buffer_top_address, data);
1.1       root      673:                }
                    674:                *(UINT32 *)(mem + byteaddress) = data;
1.1.1.4   root      675: #if defined(HAS_I386)
1.1.1.3   root      676:        } else if(byteaddress < MAX_MEM - 3) {
1.1.1.4   root      677: #else
                    678:        } else {
                    679: #endif
1.1.1.3   root      680:                *(UINT32 *)(mem + byteaddress) = data;
1.1       root      681:        }
                    682: }
                    683: 
                    684: #define read_decrypted_byte read_byte
                    685: #define read_decrypted_word read_word
                    686: #define read_decrypted_dword read_dword
                    687: 
1.1.1.3   root      688: #define read_raw_byte read_byte
                    689: #define write_raw_byte write_byte
                    690: 
                    691: #define read_word_unaligned read_word
                    692: #define write_word_unaligned write_word
                    693: 
                    694: #define read_io_word_unaligned read_io_word
                    695: #define write_io_word_unaligned write_io_word
                    696: 
1.1       root      697: UINT8 read_io_byte(offs_t byteaddress);
                    698: UINT16 read_io_word(offs_t byteaddress);
                    699: UINT32 read_io_dword(offs_t byteaddress);
                    700: 
                    701: void write_io_byte(offs_t byteaddress, UINT8 data);
                    702: void write_io_word(offs_t byteaddress, UINT16 data);
                    703: void write_io_dword(offs_t byteaddress, UINT32 data);
                    704: 
                    705: /*****************************************************************************/
                    706: /* src/osd/osdcomm.h */
                    707: 
                    708: /* Highly useful macro for compile-time knowledge of an array size */
                    709: #define ARRAY_LENGTH(x)     (sizeof(x) / sizeof(x[0]))
                    710: 
1.1.1.3   root      711: #if defined(HAS_I386)
1.1.1.10  root      712:        static CPU_TRANSLATE(i386);
                    713:        #include "mame/lib/softfloat/softfloat.c"
                    714:        #include "mame/emu/cpu/i386/i386.c"
1.1.1.12  root      715:        #include "mame/emu/cpu/vtlb.c"
1.1.1.3   root      716: #elif defined(HAS_I286)
1.1.1.10  root      717:        #include "mame/emu/cpu/i86/i286.c"
1.1.1.3   root      718: #else
1.1.1.10  root      719:        #include "mame/emu/cpu/i86/i86.c"
1.1.1.3   root      720: #endif
1.1.1.33  root      721: #ifdef USE_DEBUGGER
1.1.1.10  root      722:        #include "mame/emu/cpu/i386/i386dasm.c"
1.1       root      723: #endif
                    724: 
1.1.1.3   root      725: #if defined(HAS_I386)
                    726:        #define SREG(x)                         m_sreg[x].selector
                    727:        #define SREG_BASE(x)                    m_sreg[x].base
                    728:        int cpu_type, cpu_step;
                    729: #else
                    730:        #define REG8(x)                         m_regs.b[x]
                    731:        #define REG16(x)                        m_regs.w[x]
                    732:        #define SREG(x)                         m_sregs[x]
                    733:        #define SREG_BASE(x)                    m_base[x]
1.1.1.33  root      734:        #define m_eip                           (m_pc - m_base[CS])
1.1.1.3   root      735:        #define m_CF                            m_CarryVal
                    736:        #define m_a20_mask                      AMASK
                    737:        #define i386_load_segment_descriptor(x) m_base[x] = SegBase(x)
                    738:        #if defined(HAS_I286)
                    739:                #define i386_set_a20_line(x)    i80286_set_a20_line(x)
                    740:        #else
                    741:                #define i386_set_a20_line(x)
                    742:        #endif
                    743:        #define i386_set_irq_line(x, y)         set_irq_line(x, y)
                    744: #endif
1.1       root      745: 
                    746: void i386_jmp_far(UINT16 selector, UINT32 address)
                    747: {
1.1.1.3   root      748: #if defined(HAS_I386)
1.1       root      749:        if(PROTECTED_MODE && !V8086_MODE) {
1.1.1.3   root      750:                i386_protected_mode_jump(selector, address, 1, m_operand_size);
1.1       root      751:        } else {
1.1.1.3   root      752:                SREG(CS) = selector;
                    753:                m_performed_intersegment_jump = 1;
                    754:                i386_load_segment_descriptor(CS);
                    755:                m_eip = address;
                    756:                CHANGE_PC(m_eip);
1.1       root      757:        }
1.1.1.3   root      758: #elif defined(HAS_I286)
                    759:        i80286_code_descriptor(selector, address, 1);
                    760: #else
                    761:        SREG(CS) = selector;
                    762:        i386_load_segment_descriptor(CS);
                    763:        m_pc = (SREG_BASE(CS) + address) & m_a20_mask;
                    764: #endif
1.1       root      765: }
                    766: 
1.1.1.24  root      767: void i386_call_far(UINT16 selector, UINT32 address)
                    768: {
                    769: #if defined(HAS_I386)
                    770:        if(PROTECTED_MODE && !V8086_MODE) {
                    771:                i386_protected_mode_call(selector, address, 1, m_operand_size);
                    772:        } else {
                    773:                PUSH16(SREG(CS));
                    774:                PUSH16(m_eip);
                    775:                SREG(CS) = selector;
                    776:                m_performed_intersegment_jump = 1;
                    777:                i386_load_segment_descriptor(CS);
                    778:                m_eip = address;
                    779:                CHANGE_PC(m_eip);
                    780:        }
                    781: #else
                    782:        UINT16 ip = m_pc - SREG_BASE(CS);
                    783:        UINT16 cs = SREG(CS);
                    784: #if defined(HAS_I286)
                    785:        i80286_code_descriptor(selector, address, 2);
                    786: #else
                    787:        SREG(CS) = selector;
                    788:        i386_load_segment_descriptor(CS);
                    789:        m_pc = (SREG_BASE(CS) + address) & m_a20_mask;
                    790: #endif
                    791:        PUSH(cs);
                    792:        PUSH(ip);
                    793:        CHANGE_PC(m_pc);
                    794: #endif
                    795: }
1.1.1.49  root      796: 
                    797: void i386_push16(UINT16 value)
                    798: {
                    799: #if defined(HAS_I386)
                    800:        PUSH16(value);
                    801: #else
                    802:        PUSH(value);
1.1.1.35  root      803: #endif
1.1.1.49  root      804: }
                    805: 
                    806: UINT16 i386_pop16()
                    807: {
                    808: #if defined(HAS_I386)
                    809:        return POP16();
                    810: #else
                    811:        UINT16 value;
                    812:        POP(value);
                    813:        return value;
                    814: #endif
                    815: }
1.1.1.24  root      816: 
1.1.1.29  root      817: UINT16 i386_read_stack()
                    818: {
                    819: #if defined(HAS_I386)
                    820:        UINT32 ea, new_esp;
                    821:        if( STACK_32BIT ) {
                    822:                new_esp = REG32(ESP) + 2;
                    823:                ea = i386_translate(SS, new_esp - 2, 0);
                    824:        } else {
                    825:                new_esp = REG16(SP) + 2;
                    826:                ea = i386_translate(SS, (new_esp - 2) & 0xffff, 0);
                    827:        }
                    828:        return READ16(ea);
                    829: #else
                    830:        UINT16 sp = m_regs.w[SP] + 2;
                    831:        return ReadWord(((m_base[SS] + ((sp - 2) & 0xffff)) & AMASK));
                    832: #endif
                    833: }
                    834: 
1.1       root      835: /* ----------------------------------------------------------------------------
1.1.1.33  root      836:        debugger
                    837: ---------------------------------------------------------------------------- */
                    838: 
                    839: #ifdef USE_DEBUGGER
                    840: #define TELNET_BLUE      0x0004 // text color contains blue.
                    841: #define TELNET_GREEN     0x0002 // text color contains green.
                    842: #define TELNET_RED       0x0001 // text color contains red.
                    843: #define TELNET_INTENSITY 0x0008 // text color is intensified.
                    844: 
                    845: int svr_socket = 0;
                    846: int cli_socket = 0;
                    847: 
1.1.1.52! root      848: process_t *msdos_process_info_get(UINT16 psp_seg, bool show_error = true);
1.1.1.33  root      849: 
                    850: void debugger_init()
                    851: {
                    852:        now_debugging = false;
                    853:        now_going = false;
                    854:        now_suspended = false;
                    855:        force_suspend = false;
                    856:        
                    857:        memset(&break_point, 0, sizeof(break_point_t));
                    858:        memset(&rd_break_point, 0, sizeof(break_point_t));
                    859:        memset(&wr_break_point, 0, sizeof(break_point_t));
                    860:        memset(&in_break_point, 0, sizeof(break_point_t));
                    861:        memset(&out_break_point, 0, sizeof(break_point_t));
                    862:        memset(&int_break_point, 0, sizeof(int_break_point_t));
                    863: }
                    864: 
1.1.1.45  root      865: void telnet_send(const char *string)
1.1.1.33  root      866: {
                    867:        char buffer[8192], *ptr;
                    868:        strcpy(buffer, string);
                    869:        while((ptr = strstr(buffer, "\n")) != NULL) {
                    870:                char tmp[8192];
                    871:                *ptr = '\0';
                    872:                sprintf(tmp, "%s\033E%s", buffer, ptr + 1);
                    873:                strcpy(buffer, tmp);
                    874:        }
                    875:        
                    876:        int len = strlen(buffer), res;
                    877:        ptr = buffer;
                    878:        while(len > 0) {
                    879:                if((res = send(cli_socket, ptr, len, 0)) > 0) {
                    880:                        len -= res;
                    881:                        ptr += res;
                    882:                }
                    883:        }
                    884: }
                    885: 
                    886: void telnet_command(const char *format, ...)
                    887: {
                    888:        char buffer[1024];
                    889:        va_list ap;
                    890:        va_start(ap, format);
                    891:        vsprintf(buffer, format, ap);
                    892:        va_end(ap);
                    893:        
                    894:        telnet_send(buffer);
                    895: }
                    896: 
                    897: void telnet_printf(const char *format, ...)
                    898: {
                    899:        char buffer[1024];
                    900:        va_list ap;
                    901:        va_start(ap, format);
                    902:        vsprintf(buffer, format, ap);
                    903:        va_end(ap);
                    904:        
                    905:        if(fp_debugger != NULL) {
                    906:                fprintf(fp_debugger, "%s", buffer);
                    907:        }
                    908:        telnet_send(buffer);
                    909: }
                    910: 
                    911: bool telnet_gets(char *str, int n)
                    912: {
                    913:        char buffer[1024];
                    914:        int ptr = 0;
                    915:        
                    916:        telnet_command("\033[12l"); // local echo on
                    917:        telnet_command("\033[2l");  // key unlock
                    918:        
                    919:        while(!m_halted) {
                    920:                int len = recv(cli_socket, buffer, sizeof(buffer), 0);
                    921:                
                    922:                if(len > 0 && buffer[0] != 0xff) {
                    923:                        for(int i = 0; i < len; i++) {
                    924:                                if(buffer[i] == 0x0d || buffer[i] == 0x0a) {
                    925:                                        str[ptr] = 0;
                    926:                                        telnet_command("\033[2h");  // key lock
                    927:                                        telnet_command("\033[12h"); // local echo off
                    928:                                        return(!m_halted);
                    929:                                } else if(buffer[i] == 0x08) {
                    930:                                        if(ptr > 0) {
                    931:                                                telnet_command("\033[0K"); // erase from cursor position
                    932:                                                ptr--;
                    933:                                        } else {
                    934:                                                telnet_command("\033[1C"); // move cursor forward
                    935:                                        }
                    936:                                } else if(ptr < n - 1) {
1.1.1.37  root      937:                                        if(buffer[i] >= 0x20 && buffer[i] <= 0x7e) {
1.1.1.33  root      938:                                                str[ptr++] = buffer[i];
                    939:                                        }
                    940:                                } else {
                    941:                                        telnet_command("\033[1D\033[0K"); // move cursor backward and erase from cursor position
                    942:                                }
                    943:                        }
                    944:                } else if(len == -1) {
                    945:                        if(WSAGetLastError() != WSAEWOULDBLOCK) {
                    946:                                return(false);
                    947:                        }
                    948:                } else if(len == 0) {
                    949:                        return(false);
                    950:                }
                    951:                Sleep(10);
                    952:        }
                    953:        return(!m_halted);
                    954: }
                    955: 
                    956: bool telnet_kbhit()
                    957: {
                    958:        char buffer[1024];
                    959:        
                    960:        if(!m_halted) {
                    961:                int len = recv(cli_socket, buffer, sizeof(buffer), 0);
                    962:                
                    963:                if(len > 0) {
                    964:                        for(int i = 0; i < len; i++) {
                    965:                                if(buffer[i] == 0x0d || buffer[i] == 0x0a) {
                    966:                                        return(true);
                    967:                                }
                    968:                        }
                    969:                } else if(len == 0) {
                    970:                        return(true); // disconnected
                    971:                }
                    972:        }
                    973:        return(false);
                    974: }
                    975: 
                    976: bool telnet_disconnected()
                    977: {
                    978:        char buffer[1024];
                    979:        int len = recv(cli_socket, buffer, sizeof(buffer), 0);
                    980:        
                    981:        if(len == 0) {
                    982:                return(true);
                    983:        } else if(len == -1) {
                    984:                if(WSAGetLastError() != WSAEWOULDBLOCK) {
                    985:                        return(true);
                    986:                }
                    987:        }
                    988:        return(false);
                    989: }
                    990: 
                    991: void telnet_set_color(int color)
                    992: {
                    993:        telnet_command("\033[%dm\033[3%dm", (color >> 3) & 1, (color & 7));
                    994: }
                    995: 
                    996: int debugger_dasm(char *buffer, UINT32 cs, UINT32 eip)
                    997: {
                    998: //     UINT8 *oprom = mem + (((cs << 4) + eip) & (MAX_MEM - 1));
                    999:        UINT8 ops[16];
                   1000:        for(int i = 0; i < 16; i++) {
                   1001:                ops[i] = debugger_read_byte(((cs << 4) + (eip + i)) & ADDR_MASK);
                   1002:        }
                   1003:        UINT8 *oprom = ops;
                   1004:        
                   1005: #if defined(HAS_I386)
                   1006:        if(m_operand_size) {
                   1007:                return(CPU_DISASSEMBLE_CALL(x86_32) & DASMFLAG_LENGTHMASK);
                   1008:        } else
                   1009: #endif
                   1010:        return(CPU_DISASSEMBLE_CALL(x86_16) & DASMFLAG_LENGTHMASK);
                   1011: }
                   1012: 
                   1013: void debugger_regs_info(char *buffer)
                   1014: {
                   1015: #if defined(HAS_I386)
                   1016:        UINT32 flags = get_flags();
                   1017: #else
                   1018:        UINT32 flags = CompressFlags();
                   1019: #endif
                   1020: #if defined(HAS_I386)
                   1021:        if(m_operand_size) {
                   1022:                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",
                   1023:                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),
                   1024:                PROTECTED_MODE ? "PE" : "--",
                   1025:                (flags & 0x40000) ? 'A' : '-',
                   1026:                (flags & 0x20000) ? 'V' : '-',
                   1027:                (flags & 0x10000) ? 'R' : '-',
                   1028:                (flags & 0x04000) ? 'N' : '-',
                   1029:                (flags & 0x02000) ? '1' : '0',
                   1030:                (flags & 0x01000) ? '1' : '0',
                   1031:                (flags & 0x00800) ? 'O' : '-',
                   1032:                (flags & 0x00400) ? 'D' : '-',
                   1033:                (flags & 0x00200) ? 'I' : '-',
                   1034:                (flags & 0x00100) ? 'T' : '-',
                   1035:                (flags & 0x00080) ? 'S' : '-',
                   1036:                (flags & 0x00040) ? 'Z' : '-',
                   1037:                (flags & 0x00010) ? 'A' : '-',
                   1038:                (flags & 0x00004) ? 'P' : '-',
                   1039:                (flags & 0x00001) ? 'C' : '-');
                   1040:        } else {
                   1041: #endif
                   1042:                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",
                   1043:                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),
                   1044: #if defined(HAS_I386)
                   1045:                PROTECTED_MODE ? "PE" : "--",
                   1046: #else
                   1047:                "--",
                   1048: #endif
                   1049:                (flags & 0x40000) ? 'A' : '-',
                   1050:                (flags & 0x20000) ? 'V' : '-',
                   1051:                (flags & 0x10000) ? 'R' : '-',
                   1052:                (flags & 0x04000) ? 'N' : '-',
                   1053:                (flags & 0x02000) ? '1' : '0',
                   1054:                (flags & 0x01000) ? '1' : '0',
                   1055:                (flags & 0x00800) ? 'O' : '-',
                   1056:                (flags & 0x00400) ? 'D' : '-',
                   1057:                (flags & 0x00200) ? 'I' : '-',
                   1058:                (flags & 0x00100) ? 'T' : '-',
                   1059:                (flags & 0x00080) ? 'S' : '-',
                   1060:                (flags & 0x00040) ? 'Z' : '-',
                   1061:                (flags & 0x00010) ? 'A' : '-',
                   1062:                (flags & 0x00004) ? 'P' : '-',
                   1063:                (flags & 0x00001) ? 'C' : '-');
                   1064: #if defined(HAS_I386)
                   1065:        }
                   1066: #endif
                   1067: }
                   1068: 
                   1069: void debugger_process_info(char *buffer)
                   1070: {
                   1071:        UINT16 psp_seg = current_psp;
                   1072:        process_t *process;
                   1073:        bool check[0x10000] = {0};
                   1074:        
                   1075:        buffer[0] = '\0';
                   1076:        
                   1077:        while(!check[psp_seg] && (process  = msdos_process_info_get(psp_seg, false)) != NULL) {
                   1078:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   1079:                char *file = process->module_path, *s;
                   1080:                char tmp[8192];
                   1081:                
                   1082:                while((s = strstr(file, "\\")) != NULL) {
                   1083:                        file = s + 1;
                   1084:                }
                   1085:                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));
                   1086:                strcat(tmp, buffer);
                   1087:                strcpy(buffer, tmp);
                   1088:                
                   1089:                check[psp_seg] = true;
                   1090:                psp_seg = psp->parent_psp;
                   1091:        }
                   1092: }
                   1093: 
                   1094: UINT32 debugger_get_val(const char *str)
                   1095: {
                   1096:        char tmp[1024];
                   1097:        
                   1098:        if(str == NULL || strlen(str) == 0) {
                   1099:                return(0);
                   1100:        }
                   1101:        strcpy(tmp, str);
                   1102:        
                   1103:        if(strlen(tmp) == 3 && tmp[0] == '\'' && tmp[2] == '\'') {
                   1104:                // ank
                   1105:                return(tmp[1] & 0xff);
                   1106:        } else if(tmp[0] == '%') {
                   1107:                // decimal
                   1108:                return(strtoul(tmp + 1, NULL, 10));
                   1109:        }
                   1110:        return(strtoul(tmp, NULL, 16));
                   1111: }
                   1112: 
                   1113: UINT32 debugger_get_seg(const char *str, UINT32 val)
                   1114: {
                   1115:        char tmp[1024], *s;
                   1116:        
                   1117:        if(str == NULL || strlen(str) == 0) {
                   1118:                return(val);
                   1119:        }
                   1120:        strcpy(tmp, str);
                   1121:        
                   1122:        if((s = strstr(tmp, ":")) != NULL) {
                   1123:                // 0000:0000
                   1124:                *s = '\0';
                   1125:                return(debugger_get_val(tmp));
                   1126:        }
                   1127:        return(val);
                   1128: }
                   1129: 
                   1130: UINT32 debugger_get_ofs(const char *str)
                   1131: {
                   1132:        char tmp[1024], *s;
                   1133:        
                   1134:        if(str == NULL || strlen(str) == 0) {
                   1135:                return(0);
                   1136:        }
                   1137:        strcpy(tmp, str);
                   1138:        
                   1139:        if((s = strstr(tmp, ":")) != NULL) {
                   1140:                // 0000:0000
                   1141:                return(debugger_get_val(s + 1));
                   1142:        }
                   1143:        return(debugger_get_val(tmp));
                   1144: }
                   1145: 
                   1146: void debugger_main()
                   1147: {
                   1148:        telnet_command("\033[20h"); // cr-lf
                   1149:        
                   1150:        force_suspend = true;
                   1151:        now_going = false;
                   1152:        now_debugging = true;
                   1153:        Sleep(100);
                   1154:        
                   1155:        if(!m_halted && !now_suspended) {
                   1156:                telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1157:                telnet_printf("waiting until cpu is suspended...\n");
                   1158:        }
                   1159:        while(!m_halted && !now_suspended) {
                   1160:                if(telnet_disconnected()) {
                   1161:                        break;
                   1162:                }
                   1163:                Sleep(10);
                   1164:        }
                   1165:        
                   1166:        char buffer[8192];
                   1167:        
                   1168:        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1169:        debugger_process_info(buffer);
                   1170:        telnet_printf("%s", buffer);
                   1171:        debugger_regs_info(buffer);
                   1172:        telnet_printf("%s", buffer);
                   1173:        telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1174:        telnet_printf("breaked at %04X:%04X\n", SREG(CS), m_eip);
                   1175:        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1176:        debugger_dasm(buffer, SREG(CS), m_eip);
                   1177:        telnet_printf("next\t%04X:%04X  %s\n", SREG(CS), m_eip, buffer);
                   1178:        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1179:        
                   1180:        #define MAX_COMMAND_LEN 64
                   1181:        
                   1182:        char command[MAX_COMMAND_LEN + 1];
                   1183:        char prev_command[MAX_COMMAND_LEN + 1] = {0};
                   1184:        
                   1185:        UINT32 data_seg = SREG(DS);
                   1186:        UINT32 data_ofs = 0;
                   1187:        UINT32 dasm_seg = SREG(CS);
                   1188:        UINT32 dasm_ofs = m_eip;
                   1189:        
                   1190:        while(!m_halted) {
                   1191:                telnet_printf("- ");
                   1192:                command[0] = '\0';
                   1193:                
                   1194:                if(fi_debugger != NULL) {
                   1195:                        while(command[0] == '\0') {
                   1196:                                if(fgets(command, sizeof(command), fi_debugger) == NULL) {
                   1197:                                        break;
                   1198:                                }
                   1199:                                while(strlen(command) > 0 && (command[strlen(command) - 1] == 0x0d || command[strlen(command) - 1] == 0x0a)) {
                   1200:                                        command[strlen(command) - 1] = '\0';
                   1201:                                }
                   1202:                        }
                   1203:                        if(command[0] != '\0') {
                   1204:                                telnet_command("%s\n", command);
                   1205:                        }
                   1206:                }
                   1207:                if(command[0] == '\0') {
                   1208:                        if(!telnet_gets(command, sizeof(command))) {
                   1209:                                break;
                   1210:                        }
                   1211:                }
                   1212:                if(command[0] == '\0') {
                   1213:                        strcpy(command, prev_command);
                   1214:                } else {
                   1215:                        strcpy(prev_command, command);
                   1216:                }
                   1217:                if(fp_debugger != NULL) {
                   1218:                        fprintf(fp_debugger, "%s\n", command);
                   1219:                }
                   1220:                
                   1221:                if(!m_halted && command[0] != 0) {
                   1222:                        char *params[32], *token = NULL;
                   1223:                        int num = 0;
                   1224:                        
                   1225:                        if((token = strtok(command, " ")) != NULL) {
                   1226:                                params[num++] = token;
                   1227:                                while(num < 32 && (token = strtok(NULL, " ")) != NULL) {
                   1228:                                        params[num++] = token;
                   1229:                                }
                   1230:                        }
                   1231:                        if(stricmp(params[0], "D") == 0) {
                   1232:                                if(num <= 3) {
                   1233:                                        if(num >= 2) {
                   1234:                                                data_seg = debugger_get_seg(params[1], data_seg);
                   1235:                                                data_ofs = debugger_get_ofs(params[1]);
                   1236:                                        }
                   1237:                                        UINT32 end_seg = data_seg;
                   1238:                                        UINT32 end_ofs = data_ofs + 8 * 16 - 1;
                   1239:                                        if(num == 3) {
                   1240:                                                end_seg = debugger_get_seg(params[2], data_seg);
                   1241:                                                end_ofs = debugger_get_ofs(params[2]);
                   1242:                                        }
                   1243:                                        UINT64 start_addr = (data_seg << 4) + data_ofs;
                   1244:                                        UINT64 end_addr = (end_seg << 4) + end_ofs;
1.1.1.37  root     1245: //                                     bool is_sjis = false;
1.1.1.33  root     1246:                                        
                   1247:                                        for(UINT64 addr = (start_addr & ~0x0f); addr <= (end_addr | 0x0f); addr++) {
                   1248:                                                if((addr & 0x0f) == 0) {
                   1249:                                                        if((data_ofs = addr - (data_seg << 4)) > 0xffff) {
                   1250:                                                                data_seg += 0x1000;
                   1251:                                                                data_ofs -= 0x10000;
                   1252:                                                        }
                   1253:                                                        telnet_printf("%06X:%04X ", data_seg, data_ofs);
                   1254:                                                        memset(buffer, 0, sizeof(buffer));
                   1255:                                                }
                   1256:                                                if(addr < start_addr || addr > end_addr) {
                   1257:                                                        telnet_printf("   ");
                   1258:                                                        buffer[addr & 0x0f] = ' ';
                   1259:                                                } else {
                   1260:                                                        UINT8 data = debugger_read_byte(addr & ADDR_MASK);
                   1261:                                                        telnet_printf(" %02X", data);
1.1.1.37  root     1262: //                                                     if(is_sjis) {
1.1.1.33  root     1263: //                                                             buffer[addr & 0x0f] = data;
1.1.1.37  root     1264: //                                                             is_sjis = false;
1.1.1.33  root     1265: //                                                     } else if(((data >= 0x81 && data <= 0x9f) || (data >= 0xe0 && data <= 0xef)) && (addr & 0x0f) < 0x0f) {
                   1266: //                                                             buffer[addr & 0x0f] = data;
1.1.1.37  root     1267: //                                                             is_sjis = true;
1.1.1.33  root     1268: //                                                     } else
                   1269:                                                        if((data >= 0x20 && data <= 0x7e)/* || (data >= 0xa1 && data <= 0xdf)*/) {
                   1270:                                                                buffer[addr & 0x0f] = data;
                   1271:                                                        } else {
                   1272:                                                                buffer[addr & 0x0f] = '.';
                   1273:                                                        }
                   1274:                                                }
                   1275:                                                if((addr & 0x0f) == 0x0f) {
                   1276:                                                        telnet_printf("  %s\n", buffer);
                   1277:                                                }
                   1278:                                        }
                   1279:                                        if((data_ofs = (end_addr + 1) - (data_seg << 4)) > 0xffff) {
                   1280:                                                data_seg += 0x1000;
                   1281:                                                data_ofs -= 0x10000;
                   1282:                                        }
                   1283:                                        prev_command[1] = '\0'; // remove parameters to dump continuously
                   1284:                                } else {
                   1285:                                        telnet_printf("invalid parameter number\n");
                   1286:                                }
                   1287:                        } else if(stricmp(params[0], "E") == 0 || stricmp(params[0], "EB") == 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++) {
                   1292:                                                debugger_write_byte(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]) & 0xff);
                   1293:                                        }
                   1294:                                } else {
                   1295:                                        telnet_printf("invalid parameter number\n");
                   1296:                                }
                   1297:                        } else if(stricmp(params[0], "EW") == 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 += 2) {
                   1302:                                                debugger_write_word(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]) & 0xffff);
                   1303:                                        }
                   1304:                                } else {
                   1305:                                        telnet_printf("invalid parameter number\n");
                   1306:                                }
                   1307:                        } else if(stricmp(params[0], "ED") == 0) {
                   1308:                                if(num >= 3) {
                   1309:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1310:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1311:                                        for(int i = 2, j = 0; i < num; i++, j += 4) {
                   1312:                                                debugger_write_dword(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]));
                   1313:                                        }
                   1314:                                } else {
                   1315:                                        telnet_printf("invalid parameter number\n");
                   1316:                                }
                   1317:                        } else if(stricmp(params[0], "EA") == 0) {
                   1318:                                if(num >= 3) {
                   1319:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1320:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1321:                                        strcpy(buffer, prev_command);
                   1322:                                        if((token = strtok(buffer, "\"")) != NULL && (token = strtok(NULL, "\"")) != NULL) {
                   1323:                                                int len = strlen(token);
                   1324:                                                for(int i = 0; i < len; i++) {
                   1325:                                                        debugger_write_byte(((seg << 4) + (ofs + i)) & ADDR_MASK, token[i] & 0xff);
                   1326:                                                }
                   1327:                                        } else {
                   1328:                                                telnet_printf("invalid parameter\n");
                   1329:                                        }
                   1330:                                } else {
                   1331:                                        telnet_printf("invalid parameter number\n");
                   1332:                                }
                   1333:                        } else if(stricmp(params[0], "I") == 0 || stricmp(params[0], "IB") == 0) {
                   1334:                                if(num == 2) {
                   1335:                                        telnet_printf("%02X\n", debugger_read_io_byte(debugger_get_val(params[1])) & 0xff);
                   1336:                                } else {
                   1337:                                        telnet_printf("invalid parameter number\n");
                   1338:                                }
                   1339:                        } else if(stricmp(params[0], "IW") == 0) {
                   1340:                                if(num == 2) {
                   1341:                                        telnet_printf("%04X\n", debugger_read_io_word(debugger_get_val(params[1])) & 0xffff);
                   1342:                                } else {
                   1343:                                        telnet_printf("invalid parameter number\n");
                   1344:                                }
                   1345:                        } else if(stricmp(params[0], "ID") == 0) {
                   1346:                                if(num == 2) {
                   1347:                                        telnet_printf("%08X\n", debugger_read_io_dword(debugger_get_val(params[1])));
                   1348:                                } else {
                   1349:                                        telnet_printf("invalid parameter number\n");
                   1350:                                }
                   1351:                        } else if(stricmp(params[0], "O") == 0 || stricmp(params[0], "OB") == 0) {
                   1352:                                if(num == 3) {
                   1353:                                        debugger_write_io_byte(debugger_get_val(params[1]), debugger_get_val(params[2]) & 0xff);
                   1354:                                } else {
                   1355:                                        telnet_printf("invalid parameter number\n");
                   1356:                                }
                   1357:                        } else if(stricmp(params[0], "OW") == 0) {
                   1358:                                if(num == 3) {
                   1359:                                        debugger_write_io_word(debugger_get_val(params[1]), debugger_get_val(params[2]) & 0xffff);
                   1360:                                } else {
                   1361:                                        telnet_printf("invalid parameter number\n");
                   1362:                                }
                   1363:                        } else if(stricmp(params[0], "OD") == 0) {
                   1364:                                if(num == 3) {
                   1365:                                        debugger_write_io_dword(debugger_get_val(params[1]), debugger_get_val(params[2]));
                   1366:                                } else {
                   1367:                                        telnet_printf("invalid parameter number\n");
                   1368:                                }
                   1369:                        } else if(stricmp(params[0], "R") == 0) {
                   1370:                                if(num == 1) {
                   1371:                                        debugger_regs_info(buffer);
                   1372:                                        telnet_printf("%s", buffer);
                   1373:                                } else if(num == 3) {
                   1374: #if defined(HAS_I386)
                   1375:                                        if(stricmp(params[1], "EAX") == 0) {
                   1376:                                                REG32(EAX) = debugger_get_val(params[2]);
                   1377:                                        } else if(stricmp(params[1], "EBX") == 0) {
                   1378:                                                REG32(EBX) = debugger_get_val(params[2]);
                   1379:                                        } else if(stricmp(params[1], "ECX") == 0) {
                   1380:                                                REG32(ECX) = debugger_get_val(params[2]);
                   1381:                                        } else if(stricmp(params[1], "EDX") == 0) {
                   1382:                                                REG32(EDX) = debugger_get_val(params[2]);
                   1383:                                        } else if(stricmp(params[1], "ESP") == 0) {
                   1384:                                                REG32(ESP) = debugger_get_val(params[2]);
                   1385:                                        } else if(stricmp(params[1], "EBP") == 0) {
                   1386:                                                REG32(EBP) = debugger_get_val(params[2]);
                   1387:                                        } else if(stricmp(params[1], "ESI") == 0) {
                   1388:                                                REG32(ESI) = debugger_get_val(params[2]);
                   1389:                                        } else if(stricmp(params[1], "EDI") == 0) {
                   1390:                                                REG32(EDI) = debugger_get_val(params[2]);
                   1391:                                        } else
                   1392: #endif
                   1393:                                        if(stricmp(params[1], "AX") == 0) {
                   1394:                                                REG16(AX) = debugger_get_val(params[2]);
                   1395:                                        } else if(stricmp(params[1], "BX") == 0) {
                   1396:                                                REG16(BX) = debugger_get_val(params[2]);
                   1397:                                        } else if(stricmp(params[1], "CX") == 0) {
                   1398:                                                REG16(CX) = debugger_get_val(params[2]);
                   1399:                                        } else if(stricmp(params[1], "DX") == 0) {
                   1400:                                                REG16(DX) = debugger_get_val(params[2]);
                   1401:                                        } else if(stricmp(params[1], "SP") == 0) {
                   1402:                                                REG16(SP) = debugger_get_val(params[2]);
                   1403:                                        } else if(stricmp(params[1], "BP") == 0) {
                   1404:                                                REG16(BP) = debugger_get_val(params[2]);
                   1405:                                        } else if(stricmp(params[1], "SI") == 0) {
                   1406:                                                REG16(SI) = debugger_get_val(params[2]);
                   1407:                                        } else if(stricmp(params[1], "DI") == 0) {
                   1408:                                                REG16(DI) = debugger_get_val(params[2]);
                   1409:                                        } else if(stricmp(params[1], "IP") == 0 || stricmp(params[1], "EIP") == 0) {
                   1410: #if defined(HAS_I386)
                   1411:                                                if(m_operand_size) {
                   1412:                                                        m_eip = debugger_get_val(params[2]);
                   1413:                                                } else {
                   1414:                                                        m_eip = debugger_get_val(params[2]) & 0xffff;
                   1415:                                                }
                   1416:                                                CHANGE_PC(m_eip);
                   1417: #else
                   1418:                                                m_pc = (SREG_BASE(CS) + (debugger_get_val(params[2]) & 0xffff)) & ADDR_MASK;
                   1419:                                                CHANGE_PC(m_pc);
                   1420: #endif
                   1421:                                        } else if(stricmp(params[1], "AL") == 0) {
                   1422:                                                REG8(AL) = debugger_get_val(params[2]);
                   1423:                                        } else if(stricmp(params[1], "AH") == 0) {
                   1424:                                                REG8(AH) = debugger_get_val(params[2]);
                   1425:                                        } else if(stricmp(params[1], "BL") == 0) {
                   1426:                                                REG8(BL) = debugger_get_val(params[2]);
                   1427:                                        } else if(stricmp(params[1], "BH") == 0) {
                   1428:                                                REG8(BH) = debugger_get_val(params[2]);
                   1429:                                        } else if(stricmp(params[1], "CL") == 0) {
                   1430:                                                REG8(CL) = debugger_get_val(params[2]);
                   1431:                                        } else if(stricmp(params[1], "CH") == 0) {
                   1432:                                                REG8(CH) = debugger_get_val(params[2]);
                   1433:                                        } else if(stricmp(params[1], "DL") == 0) {
                   1434:                                                REG8(DL) = debugger_get_val(params[2]);
                   1435:                                        } else if(stricmp(params[1], "DH") == 0) {
                   1436:                                                REG8(DH) = debugger_get_val(params[2]);
                   1437:                                        } else {
                   1438:                                                telnet_printf("unknown register %s\n", params[1]);
                   1439:                                        }
                   1440:                                } else {
                   1441:                                        telnet_printf("invalid parameter number\n");
                   1442:                                }
                   1443:                        } else if(_tcsicmp(params[0], "S") == 0) {
                   1444:                                if(num >= 4) {
                   1445:                                        UINT32 cur_seg = debugger_get_seg(params[1], data_seg);
                   1446:                                        UINT32 cur_ofs = debugger_get_ofs(params[1]);
                   1447:                                        UINT32 end_seg = debugger_get_seg(params[2], cur_seg);
                   1448:                                        UINT32 end_ofs = debugger_get_ofs(params[2]);
                   1449:                                        UINT8 list[32];
                   1450:                                        
                   1451:                                        for(int i = 3, j = 0; i < num && j < 32; i++, j++) {
                   1452:                                                list[j] = debugger_get_val(params[i]);
                   1453:                                        }
                   1454:                                        while((cur_seg << 4) + cur_ofs <= (end_seg << 4) + end_ofs) {
                   1455:                                                bool found = true;
                   1456:                                                for(int i = 3, j = 0; i < num && j < 32; i++, j++) {
                   1457:                                                        if(debugger_read_byte(((cur_seg << 4) + (cur_ofs + j)) & ADDR_MASK) != list[j]) {
                   1458:                                                                found = false;
                   1459:                                                                break;
                   1460:                                                        }
                   1461:                                                }
                   1462:                                                if(found) {
                   1463:                                                        telnet_printf("%04X:%04X\n", cur_seg, cur_ofs);
                   1464:                                                }
                   1465:                                                if((cur_ofs += 1) > 0xffff) {
                   1466:                                                        cur_seg += 0x1000;
                   1467:                                                        cur_ofs -= 0x10000;
                   1468:                                                }
                   1469:                                        }
                   1470:                                } else {
                   1471:                                        telnet_printf("invalid parameter number\n");
                   1472:                                }
                   1473:                        } else if(stricmp(params[0], "U") == 0) {
                   1474:                                if(num <= 3) {
                   1475:                                        if(num >= 2) {
                   1476:                                                dasm_seg = debugger_get_seg(params[1], dasm_seg);
                   1477:                                                dasm_ofs = debugger_get_ofs(params[1]);
                   1478:                                        }
                   1479:                                        if(num == 3) {
                   1480:                                                UINT32 end_seg = debugger_get_seg(params[2], dasm_seg);
                   1481:                                                UINT32 end_ofs = debugger_get_ofs(params[2]);
                   1482:                                                
                   1483:                                                while((dasm_seg << 4) + dasm_ofs <= (end_seg << 4) + end_ofs) {
                   1484:                                                        int len = debugger_dasm(buffer, dasm_seg, dasm_ofs);
                   1485:                                                        telnet_printf("%04X:%04X  ", dasm_seg, dasm_ofs);
                   1486:                                                        for(int i = 0; i < len; i++) {
                   1487:                                                                telnet_printf("%02X", debugger_read_byte(((dasm_seg << 4) + (dasm_ofs + i)) & ADDR_MASK));
                   1488:                                                        }
                   1489:                                                        for(int i = len; i < 8; i++) {
                   1490:                                                                telnet_printf("  ");
                   1491:                                                        }
                   1492:                                                        telnet_printf("  %s\n", buffer);
                   1493:                                                        if((dasm_ofs += len) > 0xffff) {
                   1494:                                                                dasm_seg += 0x1000;
                   1495:                                                                dasm_ofs -= 0x10000;
                   1496:                                                        }
                   1497:                                                }
                   1498:                                        } else {
                   1499:                                                for(int i = 0; i < 16; i++) {
                   1500:                                                        int len = debugger_dasm(buffer, dasm_seg, dasm_ofs);
                   1501:                                                        telnet_printf("%04X:%04X  ", dasm_seg, dasm_ofs);
                   1502:                                                        for(int i = 0; i < len; i++) {
                   1503:                                                                telnet_printf("%02X", debugger_read_byte(((dasm_seg << 4) + (dasm_ofs + i)) & ADDR_MASK));
                   1504:                                                        }
                   1505:                                                        for(int i = len; i < 8; i++) {
                   1506:                                                                telnet_printf("  ");
                   1507:                                                        }
                   1508:                                                        telnet_printf("  %s\n", buffer);
                   1509:                                                        if((dasm_ofs += len) > 0xffff) {
                   1510:                                                                dasm_seg += 0x1000;
                   1511:                                                                dasm_ofs -= 0x10000;
                   1512:                                                        }
                   1513:                                                }
                   1514:                                        }
                   1515:                                        prev_command[1] = '\0'; // remove parameters to disassemble continuously
                   1516:                                } else {
                   1517:                                        telnet_printf("invalid parameter number\n");
                   1518:                                }
                   1519:                        } else if(stricmp(params[0], "H") == 0) {
                   1520:                                if(num == 3) {
                   1521:                                        UINT32 l = debugger_get_val(params[1]);
                   1522:                                        UINT32 r = debugger_get_val(params[2]);
                   1523:                                        telnet_printf("%08X  %08X\n", l + r, l - r);
                   1524:                                } else {
                   1525:                                        telnet_printf("invalid parameter number\n");
                   1526:                                }
                   1527:                        } else if(stricmp(params[0], "BP") == 0 || stricmp(params[0], "RBP") == 0 || stricmp(params[0], "WBP") == 0) {
                   1528:                                break_point_t *break_point_ptr;
                   1529:                                #define GET_BREAK_POINT_PTR() { \
                   1530:                                        if(params[0][0] == 'R') { \
                   1531:                                                break_point_ptr = &rd_break_point; \
                   1532:                                        } else if(params[0][0] == 'W') { \
                   1533:                                                break_point_ptr = &wr_break_point; \
                   1534:                                        } else if(params[0][0] == 'I') { \
                   1535:                                                break_point_ptr = &in_break_point; \
                   1536:                                        } else if(params[0][0] == 'O') { \
                   1537:                                                break_point_ptr = &out_break_point; \
                   1538:                                        } else { \
                   1539:                                                break_point_ptr = &break_point; \
                   1540:                                        } \
                   1541:                                }
                   1542:                                GET_BREAK_POINT_PTR();
                   1543:                                if(num == 2) {
                   1544:                                        UINT32 seg = debugger_get_seg(params[1], SREG(CS));
                   1545:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1546:                                        bool found = false;
                   1547:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1548:                                                if(break_point_ptr->table[i].status == 0 || break_point_ptr->table[i].addr == ((seg << 4) + ofs)) {
                   1549:                                                        break_point_ptr->table[i].addr = (seg << 4) + ofs;
                   1550:                                                        break_point_ptr->table[i].seg = seg;
                   1551:                                                        break_point_ptr->table[i].ofs = ofs;
                   1552:                                                        break_point_ptr->table[i].status = 1;
                   1553:                                                        found = true;
                   1554:                                                }
                   1555:                                        }
                   1556:                                        if(!found) {
                   1557:                                                telnet_printf("too many break points\n");
                   1558:                                        }
                   1559:                                } else {
                   1560:                                        telnet_printf("invalid parameter number\n");
                   1561:                                }
                   1562:                        } else if(stricmp(params[0], "IBP") == 0 || stricmp(params[0], "OBP") == 0) {
                   1563:                                break_point_t *break_point_ptr;
                   1564:                                GET_BREAK_POINT_PTR();
                   1565:                                if(num == 2) {
                   1566:                                        UINT32 addr = debugger_get_val(params[1]);
                   1567:                                        bool found = false;
                   1568:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1569:                                                if(break_point_ptr->table[i].status == 0 || break_point_ptr->table[i].addr == addr) {
                   1570:                                                        break_point_ptr->table[i].addr = addr;
                   1571:                                                        break_point_ptr->table[i].status = 1;
                   1572:                                                        found = true;
                   1573:                                                }
                   1574:                                        }
                   1575:                                        if(!found) {
                   1576:                                                telnet_printf("too many break points\n");
                   1577:                                        }
                   1578:                                } else {
                   1579:                                        telnet_printf("invalid parameter number\n");
                   1580:                                }
                   1581:                        } 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) {
                   1582:                                break_point_t *break_point_ptr;
                   1583:                                GET_BREAK_POINT_PTR();
                   1584:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1585:                                        memset(break_point_ptr, 0, sizeof(break_point_t));
                   1586:                                } else if(num >= 2) {
                   1587:                                        for(int i = 1; i < num; i++) {
                   1588:                                                int index = debugger_get_val(params[i]);
                   1589:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1590:                                                        telnet_printf("invalid index %x\n", index);
                   1591:                                                } else {
                   1592:                                                        break_point_ptr->table[index - 1].addr = 0;
                   1593:                                                        break_point_ptr->table[index - 1].seg = 0;
                   1594:                                                        break_point_ptr->table[index - 1].ofs = 0;
                   1595:                                                        break_point_ptr->table[index - 1].status = 0;
                   1596:                                                }
                   1597:                                        }
                   1598:                                } else {
                   1599:                                        telnet_printf("invalid parameter number\n");
                   1600:                                }
                   1601:                        } 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 ||
                   1602:                                  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) {
                   1603:                                break_point_t *break_point_ptr;
                   1604:                                GET_BREAK_POINT_PTR();
                   1605:                                bool enabled = (params[0][strlen(params[0]) - 1] == 'E' || params[0][strlen(params[0]) - 1] == 'e');
                   1606:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1607:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1608:                                                if(break_point_ptr->table[i].status != 0) {
                   1609:                                                        break_point_ptr->table[i].status = enabled ? 1 : -1;
                   1610:                                                }
                   1611:                                        }
                   1612:                                } else if(num >= 2) {
                   1613:                                        for(int i = 1; i < num; i++) {
                   1614:                                                int index = debugger_get_val(params[i]);
                   1615:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1616:                                                        telnet_printf("invalid index %x\n", index);
                   1617:                                                } else if(break_point_ptr->table[index - 1].status == 0) {
                   1618:                                                        telnet_printf("break point %x is null\n", index);
                   1619:                                                } else {
                   1620:                                                        break_point_ptr->table[index - 1].status = enabled ? 1 : -1;
                   1621:                                                }
                   1622:                                        }
                   1623:                                } else {
                   1624:                                        telnet_printf("invalid parameter number\n");
                   1625:                                }
                   1626:                        } else if(stricmp(params[0], "BL") == 0 || stricmp(params[0], "RBL") == 0 || stricmp(params[0], "WBL") == 0) {
                   1627:                                break_point_t *break_point_ptr;
                   1628:                                GET_BREAK_POINT_PTR();
                   1629:                                if(num == 1) {
                   1630:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1631:                                                if(break_point_ptr->table[i].status) {
                   1632:                                                        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);
                   1633:                                                }
                   1634:                                        }
                   1635:                                } else {
                   1636:                                        telnet_printf("invalid parameter number\n");
                   1637:                                }
                   1638:                        } else if(stricmp(params[0], "IBL") == 0 || stricmp(params[0], "OBL") == 0) {
                   1639:                                break_point_t *break_point_ptr;
                   1640:                                GET_BREAK_POINT_PTR();
                   1641:                                if(num == 1) {
                   1642:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1643:                                                if(break_point_ptr->table[i].status) {
                   1644:                                                        telnet_printf("%d %c %04X\n", i + 1, break_point_ptr->table[i].status == 1 ? 'e' : 'd', break_point_ptr->table[i].addr);
                   1645:                                                }
                   1646:                                        }
                   1647:                                } else {
                   1648:                                        telnet_printf("invalid parameter number\n");
                   1649:                                }
                   1650:                        } else if(stricmp(params[0], "INTBP") == 0) {
                   1651:                                if(num >= 2 && num <= 4) {
                   1652:                                        int int_num = debugger_get_val(params[1]);
                   1653:                                        UINT8 ah = 0, ah_registered = 0;
                   1654:                                        UINT8 al = 0, al_registered = 0;
                   1655:                                        if(num >= 3) {
                   1656:                                                ah = debugger_get_val(params[2]);
                   1657:                                                ah_registered = 1;
                   1658:                                        }
                   1659:                                        if(num == 4) {
                   1660:                                                al = debugger_get_val(params[3]);
                   1661:                                                al_registered = 1;
                   1662:                                        }
                   1663:                                        bool found = false;
                   1664:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1665:                                                if(int_break_point.table[i].status == 0 || (
                   1666:                                                   int_break_point.table[i].int_num == int_num &&
                   1667:                                                   int_break_point.table[i].ah == ah && int_break_point.table[i].ah_registered == ah_registered &&
                   1668:                                                   int_break_point.table[i].al == al && int_break_point.table[i].al_registered == al_registered)) {
                   1669:                                                        int_break_point.table[i].int_num = int_num;
                   1670:                                                        int_break_point.table[i].ah = ah;
                   1671:                                                        int_break_point.table[i].ah_registered = ah_registered;
                   1672:                                                        int_break_point.table[i].al = al;
                   1673:                                                        int_break_point.table[i].al_registered = al_registered;
                   1674:                                                        int_break_point.table[i].status = 1;
                   1675:                                                        found = true;
                   1676:                                                }
                   1677:                                        }
                   1678:                                        if(!found) {
                   1679:                                                telnet_printf("too many break points\n");
                   1680:                                        }
                   1681:                                } else {
                   1682:                                        telnet_printf("invalid parameter number\n");
                   1683:                                }
                   1684:                        } else if(stricmp(params[0], "INTBC") == 0) {
                   1685:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1686:                                        memset(&int_break_point, 0, sizeof(int_break_point_t));
                   1687:                                } else if(num >= 2) {
                   1688:                                        for(int i = 1; i < num; i++) {
                   1689:                                                int index = debugger_get_val(params[i]);
                   1690:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1691:                                                        telnet_printf("invalid index %x\n", index);
                   1692:                                                } else {
                   1693:                                                        int_break_point.table[index - 1].int_num = 0;
                   1694:                                                        int_break_point.table[index - 1].ah = 0;
                   1695:                                                        int_break_point.table[index - 1].ah_registered = 0;
                   1696:                                                        int_break_point.table[index - 1].al = 0;
                   1697:                                                        int_break_point.table[index - 1].al_registered = 0;
                   1698:                                                        int_break_point.table[index - 1].status = 0;
                   1699:                                                }
                   1700:                                        }
                   1701:                                } else {
                   1702:                                        telnet_printf("invalid parameter number\n");
                   1703:                                }
                   1704:                        } else if(stricmp(params[0], "INTBD") == 0 || stricmp(params[0], "INTBE") == 0) {
                   1705:                                bool enabled = (params[0][strlen(params[0]) - 1] == 'E' || params[0][strlen(params[0]) - 1] == 'e');
                   1706:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1707:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1708:                                                if(int_break_point.table[i].status != 0) {
                   1709:                                                        int_break_point.table[i].status = enabled ? 1 : -1;
                   1710:                                                }
                   1711:                                        }
                   1712:                                } else if(num >= 2) {
                   1713:                                        for(int i = 1; i < num; i++) {
                   1714:                                                int index = debugger_get_val(params[i]);
                   1715:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1716:                                                        telnet_printf("invalid index %x\n", index);
                   1717:                                                } else if(int_break_point.table[index - 1].status == 0) {
                   1718:                                                        telnet_printf("break point %x is null\n", index);
                   1719:                                                } else {
                   1720:                                                        int_break_point.table[index - 1].status = enabled ? 1 : -1;
                   1721:                                                }
                   1722:                                        }
                   1723:                                } else {
                   1724:                                        telnet_printf("invalid parameter number\n");
                   1725:                                }
                   1726:                        } else if(stricmp(params[0], "INTBL") == 0) {
                   1727:                                if(num == 1) {
                   1728:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1729:                                                if(int_break_point.table[i].status) {
                   1730:                                                        telnet_printf("%d %c %02X", i + 1, int_break_point.table[i].status == 1 ? 'e' : 'd', int_break_point.table[i].int_num);
                   1731:                                                        if(int_break_point.table[i].ah_registered) {
                   1732:                                                                telnet_printf(" %02X", int_break_point.table[i].ah);
                   1733:                                                        }
                   1734:                                                        if(int_break_point.table[i].al_registered) {
                   1735:                                                                telnet_printf(" %02X", int_break_point.table[i].al);
                   1736:                                                        }
                   1737:                                                        telnet_printf("\n");
                   1738:                                                }
                   1739:                                        }
                   1740:                                } else {
                   1741:                                        telnet_printf("invalid parameter number\n");
                   1742:                                }
                   1743:                        } else if(stricmp(params[0], "G") == 0 || stricmp(params[0], "P") == 0) {
                   1744:                                if(num == 1 || num == 2) {
                   1745:                                        break_point_t break_point_stored;
                   1746:                                        bool break_points_stored = false;
                   1747:                                        
                   1748:                                        if(stricmp(params[0], "P") == 0) {
                   1749:                                                memcpy(&break_point_stored, &break_point, sizeof(break_point_t));
                   1750:                                                memset(&break_point, 0, sizeof(break_point_t));
                   1751:                                                break_points_stored = true;
                   1752:                                                
                   1753:                                                break_point.table[0].addr = (SREG(CS) << 4) + m_eip + debugger_dasm(buffer, SREG(CS), m_eip);
                   1754:                                                break_point.table[0].status = 1;
                   1755:                                        } else if(num >= 2) {
                   1756:                                                memcpy(&break_point_stored, &break_point, sizeof(break_point_t));
                   1757:                                                memset(&break_point, 0, sizeof(break_point_t));
                   1758:                                                break_points_stored = true;
                   1759:                                                
                   1760:                                                UINT32 seg = debugger_get_seg(params[1], SREG(CS));
                   1761:                                                UINT32 ofs = debugger_get_ofs(params[1]);
                   1762:                                                break_point.table[0].addr = (seg << 4) + ofs;
                   1763:                                                break_point.table[0].seg = seg;
                   1764:                                                break_point.table[0].ofs = ofs;
                   1765:                                                break_point.table[0].status = 1;
                   1766:                                        }
                   1767:                                        break_point.hit = rd_break_point.hit = wr_break_point.hit = in_break_point.hit = out_break_point.hit = int_break_point.hit = 0;
                   1768:                                        now_going = true;
                   1769:                                        now_suspended = false;
                   1770:                                        
                   1771:                                        telnet_command("\033[2l"); // key unlock
                   1772:                                        while(!m_halted && !now_suspended) {
                   1773:                                                if(telnet_kbhit()) {
                   1774:                                                        break;
                   1775:                                                }
                   1776:                                                Sleep(10);
                   1777:                                        }
                   1778:                                        now_going = false;
                   1779:                                        telnet_command("\033[2h"); // key lock
                   1780:                                        
                   1781:                                        if(!(break_point.hit || rd_break_point.hit || wr_break_point.hit || in_break_point.hit || out_break_point.hit || int_break_point.hit)) {
                   1782:                                                Sleep(100);
                   1783:                                                if(!m_halted && !now_suspended) {
                   1784:                                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1785:                                                        telnet_printf("waiting until cpu is suspended...\n");
                   1786:                                                }
                   1787:                                        }
                   1788:                                        while(!m_halted && !now_suspended) {
                   1789:                                                if(telnet_disconnected()) {
                   1790:                                                        break;
                   1791:                                                }
                   1792:                                                Sleep(10);
                   1793:                                        }
                   1794:                                        dasm_seg = SREG(CS);
                   1795:                                        dasm_ofs = m_eip;
                   1796:                                        
                   1797:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1798:                                        debugger_dasm(buffer, m_prev_cs, m_prev_eip);
                   1799:                                        telnet_printf("done\t%04X:%04X  %s\n", m_prev_cs, m_prev_eip, buffer);
                   1800:                                        
                   1801:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1802:                                        debugger_regs_info(buffer);
                   1803:                                        telnet_printf("%s", buffer);
                   1804:                                        
                   1805:                                        if(break_point.hit) {
                   1806:                                                if(stricmp(params[0], "G") == 0 && num == 1) {
                   1807:                                                        telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1808:                                                        telnet_printf("breaked at %04X:%04X: break point is hit\n", SREG(CS), m_eip);
                   1809:                                                }
                   1810:                                        } else if(rd_break_point.hit) {
                   1811:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1812:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1813:                                                rd_break_point.table[rd_break_point.hit - 1].seg, rd_break_point.table[rd_break_point.hit - 1].ofs,
                   1814:                                                m_prev_cs, m_prev_eip);
                   1815:                                        } else if(wr_break_point.hit) {
                   1816:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1817:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1818:                                                wr_break_point.table[wr_break_point.hit - 1].seg, wr_break_point.table[wr_break_point.hit - 1].ofs,
                   1819:                                                m_prev_cs, m_prev_eip);
                   1820:                                        } else if(in_break_point.hit) {
                   1821:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1822:                                                telnet_printf("breaked at %04X:%04X: port %04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1823:                                                in_break_point.table[in_break_point.hit - 1].addr,
                   1824:                                                m_prev_cs, m_prev_eip);
                   1825:                                        } else if(out_break_point.hit) {
                   1826:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1827:                                                telnet_printf("breaked at %04X:%04X: port %04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1828:                                                out_break_point.table[out_break_point.hit - 1].addr,
                   1829:                                                m_prev_cs, m_prev_eip);
                   1830:                                        } else if(int_break_point.hit) {
                   1831:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1832:                                                telnet_printf("breaked at %04X:%04X: INT %02x", SREG(CS), m_eip, int_break_point.table[int_break_point.hit - 1].int_num);
                   1833:                                                if(int_break_point.table[int_break_point.hit - 1].ah_registered) {
                   1834:                                                        telnet_printf(" AH=%02x", int_break_point.table[int_break_point.hit - 1].ah);
                   1835:                                                }
                   1836:                                                if(int_break_point.table[int_break_point.hit - 1].al_registered) {
                   1837:                                                        telnet_printf(" AL=%02x", int_break_point.table[int_break_point.hit - 1].al);
                   1838:                                                }
                   1839:                                                telnet_printf(" is raised at %04X:%04X\n", m_prev_cs, m_prev_eip);
                   1840:                                        } else {
                   1841:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1842:                                                telnet_printf("breaked at %04X:%04X: enter key was pressed\n", SREG(CS), m_eip);
                   1843:                                        }
                   1844:                                        if(break_points_stored) {
                   1845:                                                memcpy(&break_point, &break_point_stored, sizeof(break_point_t));
                   1846:                                        }
                   1847:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1848:                                        debugger_dasm(buffer, SREG(CS), m_eip);
                   1849:                                        telnet_printf("next\t%04X:%04X  %s\n", SREG(CS), m_eip, buffer);
                   1850:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1851:                                } else {
                   1852:                                        telnet_printf("invalid parameter number\n");
                   1853:                                }
                   1854:                        } else if(stricmp(params[0], "T") == 0) {
                   1855:                                if(num == 1 || num == 2) {
                   1856:                                        int steps = 1;
                   1857:                                        if(num >= 2) {
                   1858:                                                steps = debugger_get_val(params[1]);
                   1859:                                        }
                   1860:                                        
                   1861:                                        telnet_command("\033[2l"); // key unlock
                   1862:                                        while(steps-- > 0) {
                   1863:                                                break_point.hit = rd_break_point.hit = wr_break_point.hit = in_break_point.hit = out_break_point.hit = int_break_point.hit = 0;
                   1864:                                                now_going = false;
                   1865:                                                now_suspended = false;
                   1866:                                                
                   1867:                                                while(!m_halted && !now_suspended) {
                   1868:                                                        if(telnet_disconnected()) {
                   1869:                                                                break;
                   1870:                                                        }
                   1871:                                                        Sleep(10);
                   1872:                                                }
                   1873:                                                dasm_seg = SREG(CS);
                   1874:                                                dasm_ofs = m_eip;
                   1875:                                                
                   1876:                                                telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1877:                                                debugger_dasm(buffer, m_prev_cs, m_prev_eip);
                   1878:                                                telnet_printf("done\t%04X:%04X  %s\n", m_prev_cs, m_prev_eip, buffer);
                   1879:                                                
                   1880:                                                telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1881:                                                debugger_regs_info(buffer);
                   1882:                                                telnet_printf("%s", buffer);
                   1883:                                                
                   1884:                                                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()) {
                   1885:                                                        break;
                   1886:                                                }
                   1887:                                        }
                   1888:                                        telnet_command("\033[2h"); // key lock
                   1889:                                        
                   1890:                                        if(!(break_point.hit || rd_break_point.hit || wr_break_point.hit || in_break_point.hit || out_break_point.hit || int_break_point.hit)) {
                   1891:                                                Sleep(100);
                   1892:                                                if(!m_halted && !now_suspended) {
                   1893:                                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1894:                                                        telnet_printf("waiting until cpu is suspended...\n");
                   1895:                                                }
                   1896:                                        }
                   1897:                                        while(!m_halted && !now_suspended) {
                   1898:                                                if(telnet_disconnected()) {
                   1899:                                                        break;
                   1900:                                                }
                   1901:                                                Sleep(10);
                   1902:                                        }
                   1903:                                        if(break_point.hit) {
                   1904:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1905:                                                telnet_printf("breaked at %04X:%04X: break point is hit\n", SREG(CS), m_eip);
                   1906:                                        } else if(rd_break_point.hit) {
                   1907:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1908:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1909:                                                rd_break_point.table[rd_break_point.hit - 1].seg, rd_break_point.table[rd_break_point.hit - 1].ofs,
                   1910:                                                m_prev_cs, m_prev_eip);
                   1911:                                        } else if(wr_break_point.hit) {
                   1912:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1913:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1914:                                                wr_break_point.table[wr_break_point.hit - 1].seg, wr_break_point.table[wr_break_point.hit - 1].ofs,
                   1915:                                                m_prev_cs, m_prev_eip);
                   1916:                                        } else if(in_break_point.hit) {
                   1917:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1918:                                                telnet_printf("breaked at %04X:%04X: port %04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1919:                                                in_break_point.table[in_break_point.hit - 1].addr,
                   1920:                                                m_prev_cs, m_prev_eip);
                   1921:                                        } else if(out_break_point.hit) {
                   1922:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1923:                                                telnet_printf("breaked at %04X:%04X: port %04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1924:                                                out_break_point.table[out_break_point.hit - 1].addr,
                   1925:                                                m_prev_cs, m_prev_eip);
                   1926:                                        } else if(int_break_point.hit) {
                   1927:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1928:                                                telnet_printf("breaked at %04X:%04X: INT %02x", SREG(CS), m_eip, int_break_point.table[int_break_point.hit - 1].int_num);
                   1929:                                                if(int_break_point.table[int_break_point.hit - 1].ah_registered) {
                   1930:                                                        telnet_printf(" AH=%02x", int_break_point.table[int_break_point.hit - 1].ah);
                   1931:                                                }
                   1932:                                                if(int_break_point.table[int_break_point.hit - 1].al_registered) {
                   1933:                                                        telnet_printf(" AL=%02x", int_break_point.table[int_break_point.hit - 1].al);
                   1934:                                                }
                   1935:                                                telnet_printf(" is raised at %04X:%04X\n", m_prev_cs, m_prev_eip);
                   1936:                                        } else if(steps > 0) {
                   1937:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1938:                                                telnet_printf("breaked at %04X:%04X: enter key was pressed\n", SREG(CS), m_eip);
                   1939:                                        }
                   1940:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1941:                                        debugger_dasm(buffer, SREG(CS), m_eip);
                   1942:                                        telnet_printf("next\t%04X:%04X  %s\n", SREG(CS), m_eip, buffer);
                   1943:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1944:                                } else {
                   1945:                                        telnet_printf("invalid parameter number\n");
                   1946:                                }
                   1947:                        } else if(stricmp(params[0], "Q") == 0) {
                   1948:                                break;
                   1949:                        } else if(stricmp(params[0], "X") == 0) {
                   1950:                                debugger_process_info(buffer);
                   1951:                                telnet_printf("%s", buffer);
                   1952:                        } else if(stricmp(params[0], ">") == 0) {
                   1953:                                if(num == 2) {
                   1954:                                        if(fp_debugger != NULL) {
                   1955:                                                fclose(fp_debugger);
                   1956:                                                fp_debugger = NULL;
                   1957:                                        }
                   1958:                                        fp_debugger = fopen(params[1], "w");
                   1959:                                } else {
                   1960:                                        telnet_printf("invalid parameter number\n");
                   1961:                                }
                   1962:                        } else if(stricmp(params[0], "<") == 0) {
                   1963:                                if(num == 2) {
                   1964:                                        if(fi_debugger != NULL) {
                   1965:                                                fclose(fi_debugger);
                   1966:                                                fi_debugger = NULL;
                   1967:                                        }
                   1968:                                        fi_debugger = fopen(params[1], "r");
                   1969:                                } else {
                   1970:                                        telnet_printf("invalid parameter number\n");
                   1971:                                }
                   1972:                        } else if(stricmp(params[0], "?") == 0) {
                   1973:                                telnet_printf("D [<start> [<end>]] - dump memory\n");
                   1974:                                telnet_printf("E[{B,W,D}] <address> <list> - edit memory (byte,word,dword)\n");
                   1975:                                telnet_printf("EA <address> \"<value>\" - edit memory (ascii)\n");
                   1976:                                telnet_printf("I[{B,W,D}] <port> - input port (byte,word,dword)\n");
                   1977:                                telnet_printf("O[{B,W,D}] <port> <value> - output port (byte,word,dword)\n");
                   1978:                                
                   1979:                                telnet_printf("R - show registers\n");
                   1980:                                telnet_printf("R <reg> <value> - edit register\n");
                   1981:                                telnet_printf("S <start> <end> <list> - search\n");
                   1982:                                telnet_printf("U [<start> [<end>]] - unassemble\n");
                   1983:                                
                   1984:                                telnet_printf("H <value> <value> - hexadd\n");
                   1985:                                
                   1986:                                telnet_printf("BP <address> - set breakpoint\n");
                   1987:                                telnet_printf("{R,W}BP <address> - set breakpoint (break at memory access)\n");
                   1988:                                telnet_printf("{I,O}BP <port> - set breakpoint (break at i/o access)\n");
                   1989:                                telnet_printf("INTBP <num> [<ah> [<al>]]- set breakpoint (break at interrupt)\n");
                   1990:                                telnet_printf("[{R,W,I,O,INT}]B{C,D,E} {*,<list>} - clear/disable/enable breakpoint(s)\n");
                   1991:                                telnet_printf("[{R,W,I,O,INT}]BL - list breakpoint(s)\n");
                   1992:                                
                   1993:                                telnet_printf("G - go (press enter key to break)\n");
                   1994:                                telnet_printf("G <address> - go and break at address\n");
                   1995:                                telnet_printf("P - trace one opcode (step over)\n");
                   1996:                                telnet_printf("T [<count>] - trace (step in)\n");
                   1997:                                telnet_printf("Q - quit\n");
                   1998:                                telnet_printf("X - show dos process info\n");
                   1999:                                
                   2000:                                telnet_printf("> <filename> - output logfile\n");
                   2001:                                telnet_printf("< <filename> - input commands from file\n");
                   2002:                                
                   2003:                                telnet_printf("<value> - hexa, decimal(%%d), ascii('a')\n");
                   2004:                                telnet_printf("<list> - <value> [<value> [<value> [...]]]\n");
                   2005:                        } else {
                   2006:                                telnet_printf("unknown command %s\n", params[0]);
                   2007:                        }
                   2008:                }
                   2009:        }
                   2010:        if(fp_debugger != NULL) {
                   2011:                fclose(fp_debugger);
                   2012:                fp_debugger = NULL;
                   2013:        }
                   2014:        if(fi_debugger != NULL) {
                   2015:                fclose(fi_debugger);
                   2016:                fi_debugger = NULL;
                   2017:        }
                   2018:        now_debugging = now_going = now_suspended = force_suspend = false;
                   2019:        closesocket(cli_socket);
                   2020: }
                   2021: 
                   2022: const char *debugger_get_ttermpro_path()
                   2023: {
                   2024:        static char path[MAX_PATH] = {0};
                   2025:        
                   2026:        if(getenv("ProgramFiles")) {
                   2027:                sprintf(path, "%s\\teraterm\\ttermpro.exe", getenv("ProgramFiles"));
                   2028:        }
                   2029:        return(path);
                   2030: }
                   2031: 
                   2032: const char *debugger_get_ttermpro_x86_path()
                   2033: {
                   2034:        static char path[MAX_PATH] = {0};
                   2035:        
                   2036:        if(getenv("ProgramFiles(x86)")) {
                   2037:                sprintf(path, "%s\\teraterm\\ttermpro.exe", getenv("ProgramFiles(x86)"));
                   2038:        }
                   2039:        return(path);
                   2040: }
                   2041: 
                   2042: const char *debugger_get_putty_path()
                   2043: {
                   2044:        static char path[MAX_PATH] = {0};
                   2045:        
                   2046:        if(getenv("ProgramFiles")) {
                   2047:                sprintf(path, "%s\\PuTTY\\putty.exe", getenv("ProgramFiles"));
                   2048:        }
                   2049:        return(path);
                   2050: }
                   2051: 
                   2052: const char *debugger_get_putty_x86_path()
                   2053: {
                   2054:        static char path[MAX_PATH] = {0};
                   2055:        
                   2056:        if(getenv("ProgramFiles(x86)")) {
                   2057:                sprintf(path, "%s\\PuTTY\\putty.exe", getenv("ProgramFiles(x86)"));
                   2058:        }
                   2059:        return(path);
                   2060: }
                   2061: 
                   2062: const char *debugger_get_telnet_path()
                   2063: {
                   2064:        // NOTE: When you run 32bit version of msdos.exe on Windows x64,
                   2065:        // C:\Windows\System32\telnet.exe is redirected to telnet.exe in SysWOW64.
                   2066:        // But 32bit version of telnet.exe will not be installed in SysWOW64
                   2067:        // and 64bit version of telnet.exe will be installed in System32.
                   2068:        static char path[MAX_PATH] = {0};
                   2069:        
                   2070:        if(getenv("windir") != NULL) {
                   2071:                sprintf(path, "%s\\System32\\telnet.exe", getenv("windir"));
                   2072:        }
                   2073:        return(path);
                   2074: }
                   2075: 
                   2076: DWORD WINAPI debugger_thread(LPVOID)
                   2077: {
                   2078:        WSADATA was_data;
                   2079:        struct sockaddr_in svr_addr;
                   2080:        struct sockaddr_in cli_addr;
                   2081:        int cli_addr_len = sizeof(cli_addr);
                   2082:        int port = 23;
                   2083:        int bind_stat = SOCKET_ERROR;
                   2084:        struct timeval timeout;
                   2085:        
                   2086:        WSAStartup(MAKEWORD(2,0), &was_data);
                   2087:        
                   2088:        if((svr_socket = socket(AF_INET, SOCK_STREAM, 0)) != INVALID_SOCKET) {
                   2089:                memset(&svr_addr, 0, sizeof(svr_addr));
                   2090:                svr_addr.sin_family = AF_INET;
                   2091:                svr_addr.sin_addr.s_addr = htonl(INADDR_ANY);
                   2092:                
                   2093:                while(!m_halted && port < 10000) {
                   2094:                        svr_addr.sin_port = htons(port);
                   2095:                        if((bind_stat = bind(svr_socket, (struct sockaddr *)&svr_addr, sizeof(svr_addr))) == 0) {
                   2096:                                break;
                   2097:                        } else {
                   2098:                                port = (port == 23) ? 9000 : (port + 1);
                   2099:                        }
                   2100:                }
                   2101:                if(bind_stat == 0) {
                   2102:                        timeout.tv_sec = 1;
                   2103:                        timeout.tv_usec = 0;
1.1.1.45  root     2104:                        setsockopt(svr_socket, SOL_SOCKET, SO_RCVTIMEO, (char *)&timeout, sizeof(timeout));
1.1.1.33  root     2105:                        
                   2106:                        listen(svr_socket, 1);
                   2107:                        
                   2108:                        char command[MAX_PATH] = {0};
                   2109:                        STARTUPINFO si;
                   2110:                        PROCESS_INFORMATION pi;
                   2111:                        
                   2112:                        if(_access(debugger_get_ttermpro_path(), 0) == 0) {
                   2113:                                sprintf(command, "%s localhost:%d /T=1", debugger_get_ttermpro_path(), port);
                   2114:                        } else if(_access(debugger_get_ttermpro_x86_path(), 0) == 0) {
                   2115:                                sprintf(command, "%s localhost:%d /T=1", debugger_get_ttermpro_x86_path(), port);
                   2116:                        } else if(_access(debugger_get_putty_path(), 0) == 0) {
                   2117:                                sprintf(command, "%s -telnet localhost %d", debugger_get_putty_path(), port);
                   2118:                        } else if(_access(debugger_get_putty_x86_path(), 0) == 0) {
                   2119:                                sprintf(command, "%s -telnet localhost %d", debugger_get_putty_x86_path(), port);
                   2120:                        } else if(_access(debugger_get_telnet_path(), 0) == 0) {
                   2121:                                sprintf(command, "%s -t vt100 localhost %d", debugger_get_telnet_path(), port);
                   2122:                        }
                   2123:                        if(command[0] != '\0') {
                   2124:                                memset(&si, 0, sizeof(STARTUPINFO));
                   2125:                                memset(&pi, 0, sizeof(PROCESS_INFORMATION));
                   2126:                                CreateProcess(NULL, command, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
                   2127:                        }
                   2128:                        
                   2129:                        while(!m_halted) {
                   2130:                                if((cli_socket = accept(svr_socket, (struct sockaddr *) &cli_addr, &cli_addr_len)) != INVALID_SOCKET) {
                   2131:                                        u_long val = 1;
                   2132:                                        ioctlsocket(cli_socket, FIONBIO, &val);
                   2133:                                        debugger_main();
                   2134:                                }
                   2135:                        }
                   2136:                }
                   2137:        }
                   2138:        WSACleanup();
                   2139:        return(0);
                   2140: }
                   2141: #endif
                   2142: 
                   2143: /* ----------------------------------------------------------------------------
1.1       root     2144:        main
                   2145: ---------------------------------------------------------------------------- */
                   2146: 
1.1.1.28  root     2147: BOOL WINAPI ctrl_handler(DWORD dwCtrlType)
                   2148: {
                   2149:        if(dwCtrlType == CTRL_BREAK_EVENT) {
1.1.1.33  root     2150:                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     2151: #ifdef USE_SERVICE_THREAD
                   2152:                        EnterCriticalSection(&key_buf_crit_sect);
                   2153: #endif
1.1.1.51  root     2154:                        pcbios_clear_key_buffer();
1.1.1.35  root     2155: #ifdef USE_SERVICE_THREAD
                   2156:                        LeaveCriticalSection(&key_buf_crit_sect);
                   2157: #endif
1.1.1.33  root     2158:                }
                   2159: //             key_code = key_recv = 0;
1.1.1.28  root     2160:                return TRUE;
                   2161:        } else if(dwCtrlType == CTRL_C_EVENT) {
                   2162:                return TRUE;
                   2163:        } else if(dwCtrlType == CTRL_CLOSE_EVENT) {
                   2164:                // this program will be terminated abnormally, do minimum end process
                   2165:                exit_handler();
                   2166:                exit(1);
                   2167:        }
                   2168:        return FALSE;
                   2169: }
                   2170: 
                   2171: void exit_handler()
                   2172: {
                   2173:        if(temp_file_created) {
                   2174:                DeleteFile(temp_file_path);
                   2175:                temp_file_created = false;
                   2176:        }
                   2177:        if(key_buf_char != NULL) {
                   2178:                key_buf_char->release();
                   2179:                delete key_buf_char;
                   2180:                key_buf_char = NULL;
                   2181:        }
                   2182:        if(key_buf_scan != NULL) {
                   2183:                key_buf_scan->release();
                   2184:                delete key_buf_scan;
                   2185:                key_buf_scan = NULL;
                   2186:        }
1.1.1.32  root     2187: #ifdef SUPPORT_XMS
                   2188:        msdos_xms_release();
                   2189: #endif
1.1.1.28  root     2190:        hardware_release();
                   2191: }
                   2192: 
1.1.1.35  root     2193: #ifdef USE_VRAM_THREAD
1.1.1.28  root     2194: DWORD WINAPI vram_thread(LPVOID)
                   2195: {
                   2196:        while(!m_halted) {
                   2197:                EnterCriticalSection(&vram_crit_sect);
                   2198:                if(vram_length_char != 0 && vram_length_char == vram_last_length_char) {
                   2199:                        vram_flush_char();
                   2200:                }
                   2201:                if(vram_length_attr != 0 && vram_length_attr == vram_last_length_attr) {
                   2202:                        vram_flush_attr();
                   2203:                }
                   2204:                vram_last_length_char = vram_length_char;
                   2205:                vram_last_length_attr = vram_length_attr;
                   2206:                LeaveCriticalSection(&vram_crit_sect);
                   2207:                // this is about half the maximum keyboard repeat rate - any
                   2208:                // lower tends to be jerky, any higher misses updates
                   2209:                Sleep(15);
                   2210:        }
                   2211:        return 0;
                   2212: }
                   2213: #endif
                   2214: 
1.1.1.45  root     2215: long get_section_in_exec_file(FILE *fp, const char *name)
1.1.1.28  root     2216: {
                   2217:        UINT8 header[0x400];
                   2218:        
                   2219:        long position = ftell(fp);
                   2220:        fseek(fp, 0, SEEK_SET);
                   2221:        fread(header, sizeof(header), 1, fp);
                   2222:        fseek(fp, position, SEEK_SET);
                   2223:        
                   2224:        try {
                   2225:                _IMAGE_DOS_HEADER *dosHeader = (_IMAGE_DOS_HEADER *)(header + 0);
                   2226:                DWORD dwTopOfSignature = dosHeader->e_lfanew;
                   2227:                DWORD dwTopOfCoffHeader = dwTopOfSignature + 4;
                   2228:                _IMAGE_FILE_HEADER *coffHeader = (_IMAGE_FILE_HEADER *)(header + dwTopOfCoffHeader);
                   2229:                DWORD dwTopOfOptionalHeader = dwTopOfCoffHeader + sizeof(_IMAGE_FILE_HEADER);
                   2230:                DWORD dwTopOfFirstSectionHeader = dwTopOfOptionalHeader + coffHeader->SizeOfOptionalHeader;
                   2231:                
                   2232:                for(int i = 0; i < coffHeader->NumberOfSections; i++) {
                   2233:                        _IMAGE_SECTION_HEADER *sectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * i);
                   2234:                        if(memcmp(sectionHeader->Name, name, strlen(name)) == 0) {
                   2235:                                return(sectionHeader->PointerToRawData);
                   2236:                        }
                   2237:                }
                   2238:        } catch(...) {
                   2239:        }
                   2240:        return(0);
                   2241: }
                   2242: 
1.1.1.10  root     2243: bool is_started_from_command_prompt()
                   2244: {
1.1.1.18  root     2245:        bool ret = false;
                   2246:        
                   2247:        HMODULE hLibrary = LoadLibrary(_T("Kernel32.dll"));
                   2248:        if(hLibrary) {
                   2249:                typedef DWORD (WINAPI *GetConsoleProcessListFunction)(__out LPDWORD lpdwProcessList, __in DWORD dwProcessCount);
                   2250:                GetConsoleProcessListFunction lpfnGetConsoleProcessList;
                   2251:                lpfnGetConsoleProcessList = reinterpret_cast<GetConsoleProcessListFunction>(::GetProcAddress(hLibrary, "GetConsoleProcessList"));
                   2252:                if(lpfnGetConsoleProcessList) {
                   2253:                        DWORD pl;
                   2254:                        ret = (lpfnGetConsoleProcessList(&pl, 1) > 1);
                   2255:                        FreeLibrary(hLibrary);
                   2256:                        return(ret);
                   2257:                }
                   2258:                FreeLibrary(hLibrary);
                   2259:        }
                   2260:        
                   2261:        HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
                   2262:        if(hSnapshot != INVALID_HANDLE_VALUE) {
                   2263:                DWORD dwParentProcessID = 0;
                   2264:                PROCESSENTRY32 pe32;
                   2265:                pe32.dwSize = sizeof(PROCESSENTRY32);
                   2266:                if(Process32First(hSnapshot, &pe32)) {
                   2267:                        do {
                   2268:                                if(pe32.th32ProcessID == GetCurrentProcessId()) {
                   2269:                                        dwParentProcessID = pe32.th32ParentProcessID;
                   2270:                                        break;
                   2271:                                }
                   2272:                        } while(Process32Next(hSnapshot, &pe32));
                   2273:                }
                   2274:                CloseHandle(hSnapshot);
                   2275:                if(dwParentProcessID != 0) {
                   2276:                        HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwParentProcessID);
                   2277:                        if(hProcess != NULL) {
                   2278:                                HMODULE hMod;
                   2279:                                DWORD cbNeeded;
                   2280:                                if(EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded)) {
                   2281:                                        char module_name[MAX_PATH];
                   2282:                                        if(GetModuleBaseName(hProcess, hMod, module_name, sizeof(module_name))) {
                   2283:                                                ret = (_strnicmp(module_name, "cmd.exe", 7) == 0);
                   2284:                                        }
                   2285:                                }
                   2286:                                CloseHandle(hProcess);
                   2287:                        }
                   2288:                }
                   2289:        }
                   2290:        return(ret);
1.1.1.14  root     2291: }
                   2292: 
                   2293: BOOL is_greater_windows_version(DWORD dwMajorVersion, DWORD dwMinorVersion, WORD wServicePackMajor, WORD wServicePackMinor)
                   2294: {
1.1.1.24  root     2295:        // https://msdn.microsoft.com/en-us/library/windows/desktop/ms725491(v=vs.85).aspx
1.1.1.14  root     2296:        OSVERSIONINFOEX osvi;
                   2297:        DWORDLONG dwlConditionMask = 0;
                   2298:        int op = VER_GREATER_EQUAL;
                   2299:        
                   2300:        // Initialize the OSVERSIONINFOEX structure.
                   2301:        ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
                   2302:        osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
                   2303:        osvi.dwMajorVersion = dwMajorVersion;
                   2304:        osvi.dwMinorVersion = dwMinorVersion;
                   2305:        osvi.wServicePackMajor = wServicePackMajor;
                   2306:        osvi.wServicePackMinor = wServicePackMinor;
                   2307:        
                   2308:         // Initialize the condition mask.
                   2309:        VER_SET_CONDITION( dwlConditionMask, VER_MAJORVERSION, op );
                   2310:        VER_SET_CONDITION( dwlConditionMask, VER_MINORVERSION, op );
                   2311:        VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMAJOR, op );
                   2312:        VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMINOR, op );
                   2313:        
                   2314:        // Perform the test.
                   2315:        return VerifyVersionInfo(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR, dwlConditionMask);
                   2316: }
                   2317: 
1.1.1.27  root     2318: void get_sio_port_numbers()
                   2319: {
                   2320:        SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)};
                   2321:        HDEVINFO hDevInfo = 0;
                   2322:        HKEY hKey = 0;
                   2323:        if((hDevInfo = SetupDiGetClassDevs(&GUID_DEVINTERFACE_COMPORT, NULL, NULL, (DIGCF_PRESENT | DIGCF_DEVICEINTERFACE))) != 0) {
                   2324:                for(int i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++) {
                   2325:                        if((hKey = SetupDiOpenDevRegKey(hDevInfo, &DeviceInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_QUERY_VALUE)) != INVALID_HANDLE_VALUE) {
                   2326:                                char chData[256];
                   2327:                                DWORD dwType = 0;
                   2328:                                DWORD dwSize = sizeof(chData);
                   2329:                                int port_number = 0;
                   2330:                                
                   2331:                                if(RegQueryValueEx(hKey, _T("PortName"), NULL, &dwType, (BYTE *)chData, &dwSize) == ERROR_SUCCESS) {
                   2332:                                        if(_strnicmp(chData, "COM", 3) == 0) {
                   2333:                                                port_number = atoi(chData + 3);
                   2334:                                        }
                   2335:                                }
                   2336:                                RegCloseKey(hKey);
                   2337:                                
1.1.1.29  root     2338:                                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     2339:                                        continue;
                   2340:                                }
                   2341:                                if(sio_port_number[0] == 0) {
                   2342:                                        sio_port_number[0] = port_number;
                   2343:                                } else if(sio_port_number[1] == 0) {
                   2344:                                        sio_port_number[1] = port_number;
1.1.1.29  root     2345:                                } else if(sio_port_number[2] == 0) {
                   2346:                                        sio_port_number[2] = port_number;
                   2347:                                } else if(sio_port_number[3] == 0) {
                   2348:                                        sio_port_number[3] = port_number;
1.1.1.27  root     2349:                                }
1.1.1.29  root     2350:                                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     2351:                                        break;
                   2352:                                }
                   2353:                        }
                   2354:                }
                   2355:        }
                   2356: }
                   2357: 
1.1.1.28  root     2358: #define IS_NUMERIC(c) ((c) >= '0' && (c) <= '9')
                   2359: 
1.1       root     2360: int main(int argc, char *argv[], char *envp[])
                   2361: {
1.1.1.9   root     2362:        int arg_offset = 0;
                   2363:        int standard_env = 0;
1.1.1.14  root     2364:        int buf_width = 0, buf_height = 0;
1.1.1.28  root     2365:        bool get_console_info_success = false;
                   2366:        bool screen_size_changed = false;
                   2367:        
                   2368:        _TCHAR path[MAX_PATH], full[MAX_PATH], *name = NULL;
                   2369:        GetModuleFileName(NULL, path, MAX_PATH);
                   2370:        GetFullPathName(path, MAX_PATH, full, &name);
1.1       root     2371:        
1.1.1.27  root     2372:        char dummy_argv_0[] = "msdos.exe";
                   2373:        char dummy_argv_1[MAX_PATH];
                   2374:        char *dummy_argv[256] = {dummy_argv_0, dummy_argv_1, 0};
                   2375:        char new_exec_file[MAX_PATH];
                   2376:        bool convert_cmd_file = false;
1.1.1.28  root     2377:        unsigned int code_page = 0;
1.1.1.27  root     2378:        
                   2379:        if(name != NULL && stricmp(name, "msdos.exe") != 0) {
1.1.1.28  root     2380:                // check if command file is embedded to this execution file
                   2381:                // if this execution file name is msdos.exe, don't check
1.1.1.27  root     2382:                FILE* fp = fopen(full, "rb");
1.1.1.28  root     2383:                long offset = get_section_in_exec_file(fp, ".msdos");
                   2384:                if(offset != 0) {
1.1.1.30  root     2385:                        UINT8 buffer[16];
1.1.1.28  root     2386:                        fseek(fp, offset, SEEK_SET);
                   2387:                        fread(buffer, sizeof(buffer), 1, fp);
                   2388:                        
                   2389:                        // restore flags
                   2390:                        stay_busy           = ((buffer[0] & 0x01) != 0);
                   2391:                        no_windows          = ((buffer[0] & 0x02) != 0);
                   2392:                        standard_env        = ((buffer[0] & 0x04) != 0);
                   2393:                        ignore_illegal_insn = ((buffer[0] & 0x08) != 0);
                   2394:                        limit_max_memory    = ((buffer[0] & 0x10) != 0);
                   2395:                        if((buffer[0] & 0x20) != 0) {
                   2396:                                get_sio_port_numbers();
                   2397:                        }
                   2398:                        if((buffer[0] & 0x40) != 0) {
                   2399:                                UMB_TOP = EMS_TOP + EMS_SIZE;
                   2400:                                support_ems = true;
1.1.1.30  root     2401:                        }
1.1.1.27  root     2402: #ifdef SUPPORT_XMS
1.1.1.30  root     2403:                        if((buffer[0] & 0x80) != 0) {
1.1.1.28  root     2404:                                support_xms = true;
                   2405:                        }
1.1.1.30  root     2406: #endif
1.1.1.28  root     2407:                        if((buffer[1] != 0 || buffer[2] != 0) && (buffer[3] != 0 || buffer[4] != 0)) {
                   2408:                                buf_width  = buffer[1] | (buffer[2] << 8);
                   2409:                                buf_height = buffer[3] | (buffer[4] << 8);
                   2410:                        }
                   2411:                        if(buffer[5] != 0) {
1.1.1.30  root     2412:                                dos_major_version = buffer[5];
                   2413:                                dos_minor_version = buffer[6];
                   2414:                        }
                   2415:                        if(buffer[7] != 0) {
                   2416:                                win_major_version = buffer[7];
                   2417:                                win_minor_version = buffer[8];
1.1.1.28  root     2418:                        }
1.1.1.30  root     2419:                        if((code_page = buffer[9] | (buffer[10] << 8)) != 0) {
1.1.1.28  root     2420:                                SetConsoleCP(code_page);
                   2421:                                SetConsoleOutputCP(code_page);
                   2422:                        }
1.1.1.30  root     2423:                        int name_len = buffer[11];
                   2424:                        int file_len = buffer[12] | (buffer[13] << 8) | (buffer[14] << 16) | (buffer[15] << 24);
1.1.1.28  root     2425:                        
                   2426:                        // restore command file name
                   2427:                        memset(dummy_argv_1, 0, sizeof(dummy_argv_1));
                   2428:                        fread(dummy_argv_1, name_len, 1, fp);
                   2429:                        
                   2430:                        if(name_len != 0 && _access(dummy_argv_1, 0) == 0) {
                   2431:                                // if original command file exists, create a temporary file name
                   2432:                                if(GetTempFileName(_T("."), _T("DOS"), 0, dummy_argv_1) != 0) {
                   2433:                                        // create a temporary command file in the current director
                   2434:                                        DeleteFile(dummy_argv_1);
1.1.1.27  root     2435:                                } else {
1.1.1.28  root     2436:                                        // create a temporary command file in the temporary folder
                   2437:                                        GetTempPath(MAX_PATH, path);
                   2438:                                        if(GetTempFileName(path, _T("DOS"), 0, dummy_argv_1) != 0) {
                   2439:                                                DeleteFile(dummy_argv_1);
                   2440:                                        } else {
                   2441:                                                sprintf(dummy_argv_1, "%s$DOSPRG$.TMP", path);
                   2442:                                        }
1.1.1.27  root     2443:                                }
1.1.1.28  root     2444:                                // check the command file type
                   2445:                                fread(buffer, 2, 1, fp);
                   2446:                                fseek(fp, -2, SEEK_CUR);
                   2447:                                if(memcmp(buffer, "MZ", 2) != 0) {
                   2448:                                        memcpy(dummy_argv_1 + strlen(dummy_argv_1) - 4, ".COM", 4);
                   2449:                                } else {
                   2450:                                        memcpy(dummy_argv_1 + strlen(dummy_argv_1) - 4, ".EXE", 4);
1.1.1.27  root     2451:                                }
                   2452:                        }
1.1.1.28  root     2453:                        
                   2454:                        // restore command file
                   2455:                        FILE* fo = fopen(dummy_argv_1, "wb");
                   2456:                        for(int i = 0; i < file_len; i++) {
                   2457:                                fputc(fgetc(fp), fo);
                   2458:                        }
                   2459:                        fclose(fo);
                   2460:                        
                   2461:                        GetFullPathName(dummy_argv_1, MAX_PATH, temp_file_path, NULL);
                   2462:                        temp_file_created = true;
                   2463:                        SetFileAttributes(temp_file_path, FILE_ATTRIBUTE_HIDDEN);
                   2464:                        
                   2465:                        // adjust argc/argv
                   2466:                        for(int i = 1; i < argc && (i + 1) < 256; i++) {
                   2467:                                dummy_argv[i + 1] = argv[i];
                   2468:                        }
                   2469:                        argc++;
                   2470:                        argv = dummy_argv;
1.1.1.27  root     2471:                }
                   2472:                fclose(fp);
                   2473:        }
1.1.1.9   root     2474:        for(int i = 1; i < argc; i++) {
1.1.1.25  root     2475:                if(_strnicmp(argv[i], "-b", 2) == 0) {
                   2476:                        stay_busy = true;
                   2477:                        arg_offset++;
1.1.1.27  root     2478:                } else if(_strnicmp(argv[i], "-c", 2) == 0) {
                   2479:                        if(argv[i][2] != '\0') {
                   2480:                                strcpy(new_exec_file, &argv[i][2]);
                   2481:                        } else {
                   2482:                                strcpy(new_exec_file, "new_exec_file.exe");
                   2483:                        }
                   2484:                        convert_cmd_file = true;
                   2485:                        arg_offset++;
1.1.1.28  root     2486:                } else if(_strnicmp(argv[i], "-p", 2) == 0) {
                   2487:                        if(IS_NUMERIC(argv[i][2])) {
                   2488:                                code_page = atoi(&argv[i][2]);
                   2489:                        } else {
                   2490:                                code_page = GetConsoleCP();
                   2491:                        }
                   2492:                        arg_offset++;
1.1.1.25  root     2493:                } else if(_strnicmp(argv[i], "-d", 2) == 0) {
                   2494:                        no_windows = true;
                   2495:                        arg_offset++;
                   2496:                } else if(_strnicmp(argv[i], "-e", 2) == 0) {
1.1.1.9   root     2497:                        standard_env = 1;
                   2498:                        arg_offset++;
1.1.1.14  root     2499:                } else if(_strnicmp(argv[i], "-i", 2) == 0) {
                   2500:                        ignore_illegal_insn = true;
                   2501:                        arg_offset++;
                   2502:                } else if(_strnicmp(argv[i], "-m", 2) == 0) {
                   2503:                        limit_max_memory = true;
                   2504:                        arg_offset++;
                   2505:                } else if(_strnicmp(argv[i], "-n", 2) == 0) {
1.1.1.51  root     2506:                        int result = sscanf(argv[i] + 2, "%d,%d", &buf_height, &buf_width);
                   2507:                        if(result == 1) {
                   2508:                                buf_width = 0;
                   2509:                        } else if(result != 2) {
1.1.1.17  root     2510:                                buf_width = buf_height = 0;
                   2511:                        }
                   2512:                        if(buf_width <= 0 || buf_width > 0x7fff) {
                   2513:                                buf_width = 80;
                   2514:                        }
                   2515:                        if(buf_height <= 0 || buf_height > 0x7fff) {
                   2516:                                buf_height = 25;
                   2517:                        }
1.1.1.14  root     2518:                        arg_offset++;
1.1.1.25  root     2519:                } else if(_strnicmp(argv[i], "-s", 2) == 0) {
1.1.1.28  root     2520:                        if(IS_NUMERIC(argv[i][2])) {
1.1.1.29  root     2521:                                char *p0 = &argv[i][2], *p1, *p2, *p3;
                   2522:                                if((p1 = strchr(p0, ',')) != NULL && IS_NUMERIC(p1[1])) {
                   2523:                                        sio_port_number[1] = atoi(p1 + 1);
                   2524:                                        if((p2 = strchr(p1, ',')) != NULL && IS_NUMERIC(p2[1])) {
                   2525:                                                sio_port_number[2] = atoi(p2 + 1);
                   2526:                                                if((p3 = strchr(p2, ',')) != NULL && IS_NUMERIC(p3[1])) {
                   2527:                                                        sio_port_number[3] = atoi(p3 + 1);
                   2528:                                                }
                   2529:                                        }
1.1.1.25  root     2530:                                }
1.1.1.29  root     2531:                                sio_port_number[0] = atoi(p0);
1.1.1.25  root     2532:                        }
1.1.1.29  root     2533:                        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     2534:                                get_sio_port_numbers();
1.1.1.25  root     2535:                        }
                   2536:                        arg_offset++;
1.1.1.9   root     2537:                } else if(_strnicmp(argv[i], "-v", 2) == 0) {
1.1.1.17  root     2538:                        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     2539:                                dos_major_version = argv[i][2] - '0';
                   2540:                                dos_minor_version = (argv[i][4] - '0') * 10 + (argv[i][5] ? (argv[i][5] - '0') : 0);
                   2541:                        }
                   2542:                        arg_offset++;
                   2543:                } else if(_strnicmp(argv[i], "-w", 2) == 0) {
                   2544:                        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]))) {
                   2545:                                win_major_version = argv[i][2] - '0';
                   2546:                                win_minor_version = (argv[i][4] - '0') * 10 + (argv[i][5] ? (argv[i][5] - '0') : 0);
1.1.1.9   root     2547:                        }
                   2548:                        arg_offset++;
1.1.1.25  root     2549:                } else if(_strnicmp(argv[i], "-x", 2) == 0) {
                   2550:                        UMB_TOP = EMS_TOP + EMS_SIZE;
                   2551:                        support_ems = true;
                   2552: #ifdef SUPPORT_XMS
                   2553:                        support_xms = true;
                   2554: #endif
                   2555:                        arg_offset++;
1.1.1.9   root     2556:                } else {
                   2557:                        break;
                   2558:                }
                   2559:        }
                   2560:        if(argc < 2 + arg_offset) {
1.1       root     2561: #ifdef _WIN64
1.1.1.14  root     2562:                fprintf(stderr, "MS-DOS Player (" CPU_MODEL_NAME(CPU_MODEL) ") for Win32-x64 console\n\n");
1.1       root     2563: #else
1.1.1.14  root     2564:                fprintf(stderr, "MS-DOS Player (" CPU_MODEL_NAME(CPU_MODEL) ") for Win32 console\n\n");
1.1       root     2565: #endif
1.1.1.25  root     2566:                fprintf(stderr,
1.1.1.28  root     2567:                        "Usage: MSDOS [-b] [-c[(new exec file)] [-p[P]]] [-d] [-e] [-i] [-m] [-n[L[,C]]]\n"
1.1.1.30  root     2568:                        "             [-s[P1[,P2[,P3[,P4]]]]] [-vX.XX] [-wX.XX] [-x] (command) [options]\n"
1.1.1.25  root     2569:                        "\n"
                   2570:                        "\t-b\tstay busy during keyboard polling\n"
1.1.1.28  root     2571: #ifdef _WIN64
1.1.1.27  root     2572:                        "\t-c\tconvert command file to 64bit execution file\n"
1.1.1.28  root     2573: #else
1.1.1.27  root     2574:                        "\t-c\tconvert command file to 32bit execution file\n"
                   2575: #endif
1.1.1.28  root     2576:                        "\t-p\trecord current code page when convert command file\n"
1.1.1.25  root     2577:                        "\t-d\tpretend running under straight DOS, not Windows\n"
                   2578:                        "\t-e\tuse a reduced environment block\n"
                   2579:                        "\t-i\tignore invalid instructions\n"
                   2580:                        "\t-m\trestrict free memory to 0x7FFF paragraphs\n"
                   2581:                        "\t-n\tcreate a new buffer (25 lines, 80 columns by default)\n"
                   2582:                        "\t-s\tenable serial I/O and set host's COM port numbers\n"
                   2583:                        "\t-v\tset the DOS version\n"
1.1.1.30  root     2584:                        "\t-w\tset the Windows version\n"
1.1.1.19  root     2585: #ifdef SUPPORT_XMS
1.1.1.28  root     2586:                        "\t-x\tenable XMS and LIM EMS\n"
1.1.1.19  root     2587: #else
1.1.1.28  root     2588:                        "\t-x\tenable LIM EMS\n"
1.1.1.19  root     2589: #endif
                   2590:                );
1.1.1.10  root     2591:                
                   2592:                if(!is_started_from_command_prompt()) {
                   2593:                        fprintf(stderr, "\nStart this program from a command prompt!\n\nHit any key to quit...");
                   2594:                        while(!_kbhit()) {
                   2595:                                Sleep(10);
                   2596:                        }
                   2597:                }
1.1.1.20  root     2598: #ifdef _DEBUG
                   2599:                _CrtDumpMemoryLeaks();
                   2600: #endif
1.1       root     2601:                return(EXIT_FAILURE);
                   2602:        }
1.1.1.27  root     2603:        if(convert_cmd_file) {
                   2604:                retval = EXIT_FAILURE;
1.1.1.28  root     2605:                if(name != NULL/* && stricmp(name, "msdos.exe") == 0*/) {
1.1.1.27  root     2606:                        FILE *fp = NULL, *fs = NULL, *fo = NULL;
1.1.1.28  root     2607:                        int len = strlen(argv[arg_offset + 1]), data;
1.1.1.27  root     2608:                        
1.1.1.28  root     2609:                        if(!(len > 4 && (stricmp(argv[arg_offset + 1] + len - 4, ".COM") == 0 || stricmp(argv[arg_offset + 1] + len - 4, ".EXE") == 0))) {
                   2610:                                fprintf(stderr, "Specify command file with extenstion (.COM or .EXE)\n");
                   2611:                        } else if((fp = fopen(full, "rb")) == NULL) {
                   2612:                                fprintf(stderr, "Can't open '%s'\n", name);
1.1.1.27  root     2613:                        } else {
1.1.1.28  root     2614:                                long offset = get_section_in_exec_file(fp, ".msdos");
                   2615:                                if(offset != 0) {
                   2616:                                        UINT8 buffer[14];
                   2617:                                        fseek(fp, offset, SEEK_SET);
                   2618:                                        fread(buffer, sizeof(buffer), 1, fp);
                   2619:                                        memset(path, 0, sizeof(path));
                   2620:                                        fread(path, buffer[9], 1, fp);
                   2621:                                        fprintf(stderr, "Command file '%s' was already embedded to '%s'\n", path, name);
                   2622:                                } else if((fs = fopen(argv[arg_offset + 1], "rb")) == NULL) {
                   2623:                                        fprintf(stderr, "Can't open '%s'\n", argv[arg_offset + 1]);
                   2624:                                } else if((fo = fopen(new_exec_file, "wb")) == NULL) {
                   2625:                                        fprintf(stderr, "Can't open '%s'\n", new_exec_file);
                   2626:                                } else {
                   2627:                                        // read pe header of msdos.exe
                   2628:                                        UINT8 header[0x400];
                   2629:                                        fseek(fp, 0, SEEK_SET);
                   2630:                                        fread(header, sizeof(header), 1, fp);
                   2631:                                        
                   2632:                                        _IMAGE_DOS_HEADER *dosHeader = (_IMAGE_DOS_HEADER *)(header + 0);
                   2633:                                        DWORD dwTopOfSignature = dosHeader->e_lfanew;
                   2634:                                        DWORD dwTopOfCoffHeader = dwTopOfSignature + 4;
                   2635:                                        _IMAGE_FILE_HEADER *coffHeader = (_IMAGE_FILE_HEADER *)(header + dwTopOfCoffHeader);
                   2636:                                        DWORD dwTopOfOptionalHeader = dwTopOfCoffHeader + sizeof(_IMAGE_FILE_HEADER);
                   2637:                                        _IMAGE_OPTIONAL_HEADER *optionalHeader = (_IMAGE_OPTIONAL_HEADER *)(header + dwTopOfOptionalHeader);
                   2638:                                        DWORD dwTopOfFirstSectionHeader = dwTopOfOptionalHeader + coffHeader->SizeOfOptionalHeader;
                   2639:                                        
                   2640:                                        _IMAGE_SECTION_HEADER *lastSectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * (coffHeader->NumberOfSections - 1));
                   2641:                                        DWORD dwEndOfFile = lastSectionHeader->PointerToRawData + lastSectionHeader->SizeOfRawData;
                   2642:                                        DWORD dwLastSectionSize = lastSectionHeader->SizeOfRawData;
                   2643:                                        DWORD dwExtraLastSectionBytes = dwLastSectionSize % optionalHeader->SectionAlignment;
                   2644:                                        if(dwExtraLastSectionBytes != 0) {
                   2645:                                                DWORD dwRemain = optionalHeader->SectionAlignment - dwExtraLastSectionBytes;
                   2646:                                                dwLastSectionSize += dwRemain;
                   2647:                                        }
                   2648:                                        DWORD dwVirtualAddress = lastSectionHeader->VirtualAddress + dwLastSectionSize;
                   2649:                                        
                   2650:                                        // store msdos.exe
                   2651:                                        fseek(fp, 0, SEEK_SET);
                   2652:                                        for(int i = 0; i < dwEndOfFile; i++) {
                   2653:                                                if((data = fgetc(fp)) != EOF) {
                   2654:                                                        fputc(data, fo);
                   2655:                                                } else {
                   2656:                                                        // we should not reach here :-(
                   2657:                                                        fputc(0, fo);
                   2658:                                                }
                   2659:                                        }
                   2660:                                        
                   2661:                                        // store options
                   2662:                                        UINT8 flags = 0;
                   2663:                                        if(stay_busy) {
                   2664:                                                flags |= 0x01;
                   2665:                                        }
                   2666:                                        if(no_windows) {
                   2667:                                                flags |= 0x02;
                   2668:                                        }
                   2669:                                        if(standard_env) {
                   2670:                                                flags |= 0x04;
                   2671:                                        }
                   2672:                                        if(ignore_illegal_insn) {
                   2673:                                                flags |= 0x08;
                   2674:                                        }
                   2675:                                        if(limit_max_memory) {
                   2676:                                                flags |= 0x10;
                   2677:                                        }
1.1.1.29  root     2678:                                        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     2679:                                                flags |= 0x20;
                   2680:                                        }
                   2681:                                        if(support_ems) {
                   2682:                                                flags |= 0x40;
                   2683:                                        }
1.1.1.30  root     2684: #ifdef SUPPORT_XMS
                   2685:                                        if(support_xms) {
                   2686:                                                flags |= 0x80;
                   2687:                                        }
                   2688: #endif
1.1.1.28  root     2689:                                        
                   2690:                                        fputc(flags, fo);
                   2691:                                        fputc((buf_width  >> 0) & 0xff, fo);
                   2692:                                        fputc((buf_width  >> 8) & 0xff, fo);
                   2693:                                        fputc((buf_height >> 0) & 0xff, fo);
                   2694:                                        fputc((buf_height >> 8) & 0xff, fo);
1.1.1.30  root     2695:                                        fputc(dos_major_version, fo);
                   2696:                                        fputc(dos_minor_version, fo);
                   2697:                                        fputc(win_major_version, fo);
                   2698:                                        fputc(win_minor_version, fo);
1.1.1.28  root     2699:                                        fputc((code_page >> 0) & 0xff, fo);
                   2700:                                        fputc((code_page >> 8) & 0xff, fo);
                   2701:                                        
                   2702:                                        // store command file info
                   2703:                                        GetFullPathName(argv[arg_offset + 1], MAX_PATH, full, &name);
                   2704:                                        int name_len = strlen(name);
                   2705:                                        fseek(fs, 0, SEEK_END);
                   2706:                                        long file_size = ftell(fs);
                   2707:                                        
                   2708:                                        fputc(name_len, fo);
                   2709:                                        fputc((file_size >>  0) & 0xff, fo);
                   2710:                                        fputc((file_size >>  8) & 0xff, fo);
                   2711:                                        fputc((file_size >> 16) & 0xff, fo);
                   2712:                                        fputc((file_size >> 24) & 0xff, fo);
                   2713:                                        fwrite(name, name_len, 1, fo);
                   2714:                                        
                   2715:                                        // store command file
                   2716:                                        fseek(fs, 0, SEEK_SET);
                   2717:                                        for(int i = 0; i < file_size; i++) {
                   2718:                                                if((data = fgetc(fs)) != EOF) {
                   2719:                                                        fputc(data, fo);
                   2720:                                                } else {
                   2721:                                                        // we should not reach here :-(
                   2722:                                                        fputc(0, fo);
                   2723:                                                }
                   2724:                                        }
                   2725:                                        
                   2726:                                        // store padding data and update pe header
1.1.1.29  root     2727:                                        _IMAGE_SECTION_HEADER *newSectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * coffHeader->NumberOfSections);
                   2728:                                        coffHeader->NumberOfSections++;
                   2729:                                        memset(newSectionHeader, 0, IMAGE_SIZEOF_SECTION_HEADER);
                   2730:                                        memcpy(newSectionHeader->Name, ".msdos", 6);
                   2731:                                        newSectionHeader->VirtualAddress = dwVirtualAddress;
                   2732:                                        newSectionHeader->PointerToRawData = dwEndOfFile;
                   2733:                                        newSectionHeader->Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_DISCARDABLE;
1.1.1.28  root     2734:                                        newSectionHeader->SizeOfRawData = 14 + name_len + file_size;
                   2735:                                        DWORD dwExtraRawBytes = newSectionHeader->SizeOfRawData % optionalHeader->FileAlignment;
                   2736:                                        if(dwExtraRawBytes != 0) {
1.1.1.29  root     2737:                                                static const char padding[] = "PADDINGXXPADDING";
1.1.1.28  root     2738:                                                DWORD dwRemain = optionalHeader->FileAlignment - dwExtraRawBytes;
                   2739:                                                for(int i = 0; i < dwRemain; i++) {
1.1.1.29  root     2740:                                                        if(i < 2) {
                   2741:                                                                fputc(padding[i & 15], fo);
                   2742:                                                        } else {
                   2743:                                                                fputc(padding[(i - 2) & 15], fo);
                   2744:                                                        }
1.1.1.28  root     2745:                                                }
                   2746:                                                newSectionHeader->SizeOfRawData += dwRemain;
                   2747:                                        }
                   2748:                                        newSectionHeader->Misc.VirtualSize = newSectionHeader->SizeOfRawData;
                   2749:                                        
                   2750:                                        DWORD dwNewSectionSize = newSectionHeader->SizeOfRawData;
                   2751:                                        DWORD dwExtraNewSectionBytes = dwNewSectionSize % optionalHeader->SectionAlignment;
                   2752:                                        if(dwExtraNewSectionBytes != 0) {
                   2753:                                                DWORD dwRemain = optionalHeader->SectionAlignment - dwExtraNewSectionBytes;
                   2754:                                                dwNewSectionSize += dwRemain;
                   2755:                                        }
                   2756:                                        optionalHeader->SizeOfImage += dwNewSectionSize;
                   2757:                                        
                   2758:                                        fseek(fo, 0, SEEK_SET);
                   2759:                                        fwrite(header, sizeof(header), 1, fo);
                   2760:                                        
                   2761:                                        fprintf(stderr, "'%s' is successfully created\n", new_exec_file);
                   2762:                                        retval = EXIT_SUCCESS;
1.1.1.27  root     2763:                                }
                   2764:                        }
                   2765:                        if(fp != NULL) {
                   2766:                                fclose(fp);
                   2767:                        }
                   2768:                        if(fs != NULL) {
                   2769:                                fclose(fs);
                   2770:                        }
                   2771:                        if(fo != NULL) {
                   2772:                                fclose(fo);
                   2773:                        }
                   2774:                }
                   2775: #ifdef _DEBUG
                   2776:                _CrtDumpMemoryLeaks();
                   2777: #endif
                   2778:                return(retval);
                   2779:        }
1.1       root     2780:        
1.1.1.14  root     2781:        is_vista_or_later = is_greater_windows_version(6, 0, 0, 0);
                   2782:        
1.1.1.23  root     2783:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     2784:        CONSOLE_SCREEN_BUFFER_INFO csbi;
1.1.1.14  root     2785:        CONSOLE_CURSOR_INFO ci;
1.1.1.23  root     2786:        
1.1.1.28  root     2787:        get_console_info_success = (GetConsoleScreenBufferInfo(hStdout, &csbi) != 0);
1.1.1.14  root     2788:        GetConsoleCursorInfo(hStdout, &ci);
1.1.1.24  root     2789:        GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &dwConsoleMode);
1.1       root     2790:        
1.1.1.14  root     2791:        for(int y = 0; y < SCR_BUF_WIDTH; y++) {
                   2792:                for(int x = 0; x < SCR_BUF_HEIGHT; x++) {
                   2793:                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   2794:                        SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1       root     2795:                }
                   2796:        }
1.1.1.28  root     2797:        if(get_console_info_success) {
1.1.1.12  root     2798:                scr_width = csbi.dwSize.X;
1.1.1.14  root     2799:                scr_height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
                   2800:                
1.1.1.28  root     2801:                // v-text shadow buffer size must be lesser than 0x7fd0
                   2802:                if((scr_width > SCR_BUF_WIDTH) || (scr_height > SCR_BUF_HEIGHT) || (scr_width * scr_height * 2 > 0x7fd0) ||
1.1.1.14  root     2803:                   (buf_width != 0 && buf_width != scr_width) || (buf_height != 0 && buf_height != scr_height)) {
                   2804:                        scr_width = min(buf_width != 0 ? buf_width : scr_width, SCR_BUF_WIDTH);
                   2805:                        scr_height = min(buf_height != 0 ? buf_height : scr_height, SCR_BUF_HEIGHT);
1.1.1.28  root     2806:                        if(scr_width * scr_height * 2 > 0x7fd0) {
1.1.1.14  root     2807:                                scr_width = 80;
                   2808:                                scr_height = 25;
                   2809:                        }
1.1.1.28  root     2810:                        screen_size_changed = true;
1.1.1.14  root     2811:                }
1.1.1.12  root     2812:        } else {
                   2813:                // for a proof (not a console)
                   2814:                scr_width = 80;
                   2815:                scr_height = 25;
                   2816:        }
1.1.1.14  root     2817:        scr_buf_size.X = scr_width;
                   2818:        scr_buf_size.Y = scr_height;
                   2819:        scr_buf_pos.X = scr_buf_pos.Y = 0;
                   2820:        scr_top = csbi.srWindow.Top;
1.1       root     2821:        cursor_moved = false;
                   2822:        
1.1.1.35  root     2823: #ifdef USE_SERVICE_THREAD
                   2824:        InitializeCriticalSection(&input_crit_sect);
                   2825:        InitializeCriticalSection(&key_buf_crit_sect);
                   2826:        InitializeCriticalSection(&putch_crit_sect);
1.1.1.50  root     2827:        main_thread_id = GetCurrentThreadId();
1.1.1.35  root     2828: #endif
1.1.1.50  root     2829:        
1.1.1.25  root     2830:        key_buf_char = new FIFO(256);
                   2831:        key_buf_scan = new FIFO(256);
1.1       root     2832:        
                   2833:        hardware_init();
                   2834:        
1.1.1.33  root     2835: #ifdef USE_DEBUGGER
                   2836:        debugger_init();
                   2837: #endif
                   2838:        
1.1.1.9   root     2839:        if(msdos_init(argc - (arg_offset + 1), argv + (arg_offset + 1), envp, standard_env)) {
1.1       root     2840:                retval = EXIT_FAILURE;
                   2841:        } else {
1.1.1.27  root     2842: #if defined(_MSC_VER) && (_MSC_VER >= 1400)
1.1.1.14  root     2843:                _set_invalid_parameter_handler((_invalid_parameter_handler)ignore_invalid_parameters);
                   2844: #endif
                   2845:                SetConsoleCtrlHandler(ctrl_handler, TRUE);
                   2846:                
1.1.1.28  root     2847:                if(screen_size_changed) {
1.1.1.24  root     2848:                        change_console_size(scr_width, scr_height);
                   2849:                }
1.1.1.8   root     2850:                TIMECAPS caps;
                   2851:                timeGetDevCaps(&caps, sizeof(TIMECAPS));
                   2852:                timeBeginPeriod(caps.wPeriodMin);
1.1.1.35  root     2853: #ifdef USE_VRAM_THREAD
1.1.1.14  root     2854:                InitializeCriticalSection(&vram_crit_sect);
                   2855:                CloseHandle(CreateThread(NULL, 4096, vram_thread, NULL, 0, NULL));
                   2856: #endif
1.1.1.33  root     2857: #ifdef USE_DEBUGGER
                   2858:                CloseHandle(CreateThread(NULL, 0, debugger_thread, NULL, 0, NULL));
                   2859:                // wait until telnet client starts and connects to me
                   2860:                if(_access(debugger_get_ttermpro_path(), 0) == 0 ||
                   2861:                   _access(debugger_get_ttermpro_x86_path(), 0) == 0 ||
                   2862:                   _access(debugger_get_putty_path(), 0) == 0 ||
                   2863:                   _access(debugger_get_putty_x86_path(), 0) == 0 ||
                   2864:                   _access(debugger_get_telnet_path(), 0) == 0) {
                   2865:                        for(int i = 0; i < 100 && cli_socket == 0; i++) {
                   2866:                                Sleep(100);
                   2867:                        }
                   2868:                }
                   2869: #endif
1.1       root     2870:                hardware_run();
1.1.1.35  root     2871: #ifdef USE_VRAM_THREAD
1.1.1.14  root     2872:                vram_flush();
                   2873:                DeleteCriticalSection(&vram_crit_sect);
                   2874: #endif
1.1.1.24  root     2875:                timeEndPeriod(caps.wPeriodMin);
1.1.1.14  root     2876:                
1.1.1.24  root     2877:                // hStdin/hStdout (and all handles) will be closed in msdos_finish()...
1.1.1.28  root     2878:                if(get_console_info_success) {
1.1.1.23  root     2879:                        hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.12  root     2880:                        if(restore_console_on_exit) {
1.1.1.14  root     2881:                                // window can't be bigger than buffer,
                   2882:                                // buffer can't be smaller than window,
                   2883:                                // so make a tiny window,
                   2884:                                // set the required buffer,
                   2885:                                // then set the required window
                   2886:                                SMALL_RECT rect;
                   2887:                                SET_RECT(rect, 0, csbi.srWindow.Top, 0, csbi.srWindow.Top);
                   2888:                                SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.12  root     2889:                                SetConsoleScreenBufferSize(hStdout, csbi.dwSize);
1.1.1.14  root     2890:                                SET_RECT(rect, 0, 0, csbi.srWindow.Right - csbi.srWindow.Left, csbi.srWindow.Bottom - csbi.srWindow.Top);
1.1.1.12  root     2891:                                SetConsoleWindowInfo(hStdout, TRUE, &rect);
                   2892:                        }
1.1.1.14  root     2893:                        SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   2894:                        SetConsoleCursorInfo(hStdout, &ci);
1.1.1.12  root     2895:                }
1.1.1.24  root     2896:                SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode);
                   2897:                
1.1       root     2898:                msdos_finish();
1.1.1.14  root     2899:                
                   2900:                SetConsoleCtrlHandler(ctrl_handler, FALSE);
1.1       root     2901:        }
1.1.1.35  root     2902:        if(temp_file_created) {
                   2903:                DeleteFile(temp_file_path);
                   2904:                temp_file_created = false;
                   2905:        }
1.1.1.10  root     2906:        hardware_finish();
                   2907:        
1.1.1.28  root     2908:        if(key_buf_char != NULL) {
                   2909:                key_buf_char->release();
                   2910:                delete key_buf_char;
                   2911:                key_buf_char = NULL;
                   2912:        }
                   2913:        if(key_buf_scan != NULL) {
                   2914:                key_buf_scan->release();
                   2915:                delete key_buf_scan;
                   2916:                key_buf_scan = NULL;
                   2917:        }
1.1.1.35  root     2918: #ifdef USE_SERVICE_THREAD
                   2919:        DeleteCriticalSection(&input_crit_sect);
                   2920:        DeleteCriticalSection(&key_buf_crit_sect);
                   2921:        DeleteCriticalSection(&putch_crit_sect);
                   2922: #endif
1.1.1.20  root     2923: #ifdef _DEBUG
                   2924:        _CrtDumpMemoryLeaks();
                   2925: #endif
1.1       root     2926:        return(retval);
                   2927: }
                   2928: 
1.1.1.20  root     2929: /* ----------------------------------------------------------------------------
                   2930:        console
                   2931: ---------------------------------------------------------------------------- */
                   2932: 
1.1.1.14  root     2933: void change_console_size(int width, int height)
1.1.1.12  root     2934: {
1.1.1.23  root     2935:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.12  root     2936:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   2937:        SMALL_RECT rect;
                   2938:        COORD co;
                   2939:        
                   2940:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.14  root     2941:        if(csbi.srWindow.Top != 0 || csbi.dwCursorPosition.Y > height - 1) {
                   2942:                if(csbi.srWindow.Right - csbi.srWindow.Left + 1 == width && csbi.srWindow.Bottom - csbi.srWindow.Top + 1 == height) {
                   2943:                        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &csbi.srWindow);
                   2944:                        SET_RECT(rect, 0, 0, width - 1, height - 1);
                   2945:                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   2946:                } else if(csbi.dwCursorPosition.Y > height - 1) {
                   2947:                        SET_RECT(rect, 0, csbi.dwCursorPosition.Y - (height - 1), width - 1, csbi.dwCursorPosition.Y);
                   2948:                        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   2949:                        SET_RECT(rect, 0, 0, width - 1, height - 1);
                   2950:                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.12  root     2951:                }
                   2952:        }
1.1.1.14  root     2953:        if(csbi.dwCursorPosition.Y > height - 1) {
1.1.1.12  root     2954:                co.X = csbi.dwCursorPosition.X;
1.1.1.14  root     2955:                co.Y = min(height - 1, csbi.dwCursorPosition.Y - csbi.srWindow.Top);
1.1.1.12  root     2956:                SetConsoleCursorPosition(hStdout, co);
                   2957:                cursor_moved = true;
                   2958:        }
1.1.1.14  root     2959:        
                   2960:        // window can't be bigger than buffer,
                   2961:        // buffer can't be smaller than window,
                   2962:        // so make a tiny window,
                   2963:        // set the required buffer,
                   2964:        // then set the required window
                   2965:        SET_RECT(rect, 0, csbi.srWindow.Top, 0, csbi.srWindow.Top);
1.1.1.12  root     2966:        SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.14  root     2967:        co.X = width;
                   2968:        co.Y = height;
1.1.1.12  root     2969:        SetConsoleScreenBufferSize(hStdout, co);
1.1.1.14  root     2970:        SET_RECT(rect, 0, 0, width - 1, height - 1);
                   2971:        SetConsoleWindowInfo(hStdout, TRUE, &rect);
                   2972:        
                   2973:        scr_width = scr_buf_size.X = width;
                   2974:        scr_height = scr_buf_size.Y = height;
                   2975:        scr_top = 0;
                   2976:        
                   2977:        clear_scr_buffer(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
                   2978:        
                   2979:        int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.15  root     2980:        text_vram_end_address = text_vram_top_address + regen;
                   2981:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
                   2982:        
1.1.1.14  root     2983:        if(regen > 0x4000) {
                   2984:                regen = 0x8000;
                   2985:                vram_pages = 1;
                   2986:        } else if(regen > 0x2000) {
                   2987:                regen = 0x4000;
                   2988:                vram_pages = 2;
                   2989:        } else if(regen > 0x1000) {
                   2990:                regen = 0x2000;
                   2991:                vram_pages = 4;
                   2992:        } else {
                   2993:                regen = 0x1000;
                   2994:                vram_pages = 8;
                   2995:        }
1.1.1.15  root     2996:        *(UINT16 *)(mem + 0x44a) = scr_width;
                   2997:        *(UINT16 *)(mem + 0x44c) = regen;
                   2998:        *(UINT8  *)(mem + 0x484) = scr_height - 1;
                   2999:        
1.1.1.24  root     3000:        mouse.min_position.x = 0;
                   3001:        mouse.min_position.y = 0;
1.1.1.34  root     3002:        mouse.max_position.x = 8 * (scr_width  - 1);
                   3003:        mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24  root     3004:        
1.1.1.15  root     3005:        restore_console_on_exit = true;
1.1.1.14  root     3006: }
                   3007: 
                   3008: void clear_scr_buffer(WORD attr)
                   3009: {
                   3010:        for(int y = 0; y < scr_height; y++) {
                   3011:                for(int x = 0; x < scr_width; x++) {
                   3012:                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   3013:                        SCR_BUF(y,x).Attributes = attr;
                   3014:                }
                   3015:        }
1.1.1.12  root     3016: }
                   3017: 
1.1.1.24  root     3018: bool update_console_input()
1.1       root     3019: {
1.1.1.35  root     3020: #ifdef USE_SERVICE_THREAD
                   3021:        EnterCriticalSection(&input_crit_sect);
                   3022: #endif
1.1.1.23  root     3023:        HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
1.1.1.8   root     3024:        DWORD dwNumberOfEvents = 0;
1.1       root     3025:        DWORD dwRead;
                   3026:        INPUT_RECORD ir[16];
1.1.1.24  root     3027:        CONSOLE_SCREEN_BUFFER_INFO csbi = {0};
                   3028:        bool result = false;
1.1       root     3029:        
1.1.1.8   root     3030:        if(GetNumberOfConsoleInputEvents(hStdin, &dwNumberOfEvents) && dwNumberOfEvents != 0) {
                   3031:                if(ReadConsoleInputA(hStdin, ir, 16, &dwRead)) {
                   3032:                        for(int i = 0; i < dwRead; i++) {
1.1.1.24  root     3033:                                if(ir[i].EventType & MOUSE_EVENT) {
1.1.1.34  root     3034:                                        if(mouse.hidden == 0) {
                   3035:                                                // NOTE: if restore_console_on_exit, console is not scrolled
                   3036:                                                if(!restore_console_on_exit && csbi.srWindow.Bottom == 0) {
                   3037:                                                        GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
                   3038:                                                }
                   3039:                                                // FIXME: character size is always 8x8 ???
                   3040:                                                int x = 8 * (ir[i].Event.MouseEvent.dwMousePosition.X);
                   3041:                                                int y = 8 * (ir[i].Event.MouseEvent.dwMousePosition.Y - csbi.srWindow.Top);
                   3042:                                                
                   3043:                                                if(mouse.position.x != x || mouse.position.y != y) {
                   3044:                                                        mouse.position.x = x;
                   3045:                                                        mouse.position.y = y;
                   3046:                                                        mouse.status |= 1;
1.1.1.43  root     3047:                                                        mouse.status_alt |= 1;
1.1.1.34  root     3048:                                                }
                   3049:                                        }
                   3050:                                        if(ir[i].Event.MouseEvent.dwEventFlags == 0) {
                   3051:                                                for(int i = 0; i < MAX_MOUSE_BUTTONS; i++) {
                   3052:                                                        static const DWORD bits[] = {
                   3053:                                                                FROM_LEFT_1ST_BUTTON_PRESSED,   // left
                   3054:                                                                RIGHTMOST_BUTTON_PRESSED,       // right
                   3055:                                                                FROM_LEFT_2ND_BUTTON_PRESSED,   // middle
                   3056:                                                        };
                   3057:                                                        bool prev_status = mouse.buttons[i].status;
                   3058:                                                        mouse.buttons[i].status = ((ir[i].Event.MouseEvent.dwButtonState & bits[i]) != 0);
                   3059:                                                        
                   3060:                                                        if(!prev_status && mouse.buttons[i].status) {
                   3061:                                                                mouse.buttons[i].pressed_times++;
                   3062:                                                                mouse.buttons[i].pressed_position.x = mouse.position.x;
                   3063:                                                                mouse.buttons[i].pressed_position.y = mouse.position.x;
1.1.1.43  root     3064:                                                                if(i < 2) {
                   3065:                                                                        mouse.status_alt |= 2 << (i * 2);
                   3066:                                                                }
1.1.1.34  root     3067:                                                                mouse.status |= 2 << (i * 2);
                   3068:                                                        } else if(prev_status && !mouse.buttons[i].status) {
                   3069:                                                                mouse.buttons[i].released_times++;
                   3070:                                                                mouse.buttons[i].released_position.x = mouse.position.x;
                   3071:                                                                mouse.buttons[i].released_position.y = mouse.position.x;
1.1.1.43  root     3072:                                                                if(i < 2) {
                   3073:                                                                        mouse.status_alt |= 4 << (i * 2);
                   3074:                                                                }
1.1.1.34  root     3075:                                                                mouse.status |= 4 << (i * 2);
1.1.1.14  root     3076:                                                        }
                   3077:                                                }
                   3078:                                        }
1.1.1.24  root     3079:                                } else if(ir[i].EventType & KEY_EVENT) {
1.1.1.33  root     3080:                                        // update keyboard flags in bios data area
1.1.1.35  root     3081:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & CAPSLOCK_ON) {
                   3082:                                                mem[0x417] |= 0x40;
1.1.1.33  root     3083:                                        } else {
1.1.1.35  root     3084:                                                mem[0x417] &= ~0x40;
1.1.1.33  root     3085:                                        }
1.1.1.35  root     3086:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & NUMLOCK_ON) {
                   3087:                                                mem[0x417] |= 0x20;
1.1.1.33  root     3088:                                        } else {
1.1.1.35  root     3089:                                                mem[0x417] &= ~0x20;
                   3090:                                        }
                   3091:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & SCROLLLOCK_ON) {
                   3092:                                                mem[0x417] |= 0x10;
                   3093:                                        } else {
                   3094:                                                mem[0x417] &= ~0x10;
1.1.1.33  root     3095:                                        }
                   3096:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
1.1.1.43  root     3097:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3098:                                                        mouse.status_alt |= 0x80;
                   3099:                                                }
1.1.1.33  root     3100:                                                mem[0x417] |= 0x08;
                   3101:                                        } else {
                   3102:                                                mem[0x417] &= ~0x08;
                   3103:                                        }
1.1.1.35  root     3104:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
1.1.1.43  root     3105:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3106:                                                        mouse.status_alt |= 0x40;
                   3107:                                                }
1.1.1.35  root     3108:                                                mem[0x417] |= 0x04;
1.1.1.33  root     3109:                                        } else {
1.1.1.35  root     3110:                                                mem[0x417] &= ~0x04;
1.1.1.33  root     3111:                                        }
                   3112:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) {
1.1.1.43  root     3113:                                                if(mouse.buttons[0].status || mouse.buttons[1].status) {
                   3114:                                                        mouse.status_alt |= 0x20;
                   3115:                                                }
1.1.1.33  root     3116:                                                if(!(mem[0x417] & 0x03)) {
                   3117:                                                        mem[0x417] |= 0x02; // left shift
                   3118:                                                }
                   3119:                                        } else {
                   3120:                                                mem[0x417] &= ~0x03;
                   3121:                                        }
1.1.1.35  root     3122:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & LEFT_ALT_PRESSED) {
                   3123:                                                mem[0x418] |= 0x02;
                   3124:                                        } else {
                   3125:                                                mem[0x418] &= ~0x02;
                   3126:                                        }
                   3127:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & LEFT_CTRL_PRESSED) {
                   3128:                                                mem[0x418] |= 0x01;
                   3129:                                        } else {
                   3130:                                                mem[0x418] &= ~0x01;
                   3131:                                        }
1.1.1.33  root     3132:                                        
1.1.1.28  root     3133:                                        // set scan code of last pressed/release key to kbd_data (in-port 60h)
1.1.1.24  root     3134:                                        kbd_data = ir[i].Event.KeyEvent.wVirtualScanCode;
1.1.1.33  root     3135:                                        kbd_status |= 1;
                   3136:                                        
                   3137:                                        // update dos key buffer
                   3138:                                        UINT8 chr = ir[i].Event.KeyEvent.uChar.AsciiChar;
                   3139:                                        UINT8 scn = ir[i].Event.KeyEvent.wVirtualScanCode & 0xff;
1.1.1.51  root     3140:                                        UINT8 scn_old = scn;
1.1.1.33  root     3141:                                        
                   3142:                                        if(ir[i].Event.KeyEvent.bKeyDown) {
1.1.1.28  root     3143:                                                // make
1.1.1.24  root     3144:                                                kbd_data &= 0x7f;
                   3145:                                                
1.1.1.33  root     3146:                                                if(chr == 0x00) {
1.1.1.24  root     3147:                                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
                   3148:                                                                if(scn >= 0x3b && scn <= 0x44) {
                   3149:                                                                        scn += 0x68 - 0x3b;     // F1 to F10
                   3150:                                                                } else if(scn == 0x57 || scn == 0x58) {
                   3151:                                                                        scn += 0x8b - 0x57;     // F11 & F12
                   3152:                                                                } else if(scn >= 0x47 && scn <= 0x53) {
                   3153:                                                                        scn += 0x97 - 0x47;     // edit/arrow clusters
                   3154:                                                                } else if(scn == 0x35) {
                   3155:                                                                        scn = 0xa4;             // keypad /
                   3156:                                                                }
                   3157:                                                        } else if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
                   3158:                                                                if(scn == 0x07) {
                   3159:                                                                        chr = 0x1e;     // Ctrl+^
                   3160:                                                                } else if(scn == 0x0c) {
                   3161:                                                                        chr = 0x1f;     // Ctrl+_
                   3162:                                                                } else if(scn >= 0x35 && scn <= 0x58) {
                   3163:                                                                        static const UINT8 ctrl_map[] = {
                   3164:                                                                                0x95,   // keypad /
                   3165:                                                                                0,
                   3166:                                                                                0x96,   // keypad *
                   3167:                                                                                0, 0, 0,
                   3168:                                                                                0x5e,   // F1
                   3169:                                                                                0x5f,   // F2
                   3170:                                                                                0x60,   // F3
                   3171:                                                                                0x61,   // F4
                   3172:                                                                                0x62,   // F5
                   3173:                                                                                0x63,   // F6
                   3174:                                                                                0x64,   // F7
                   3175:                                                                                0x65,   // F8
                   3176:                                                                                0x66,   // F9
                   3177:                                                                                0x67,   // F10
                   3178:                                                                                0,
                   3179:                                                                                0,
                   3180:                                                                                0x77,   // Home
                   3181:                                                                                0x8d,   // Up
                   3182:                                                                                0x84,   // PgUp
                   3183:                                                                                0x8e,   // keypad -
                   3184:                                                                                0x73,   // Left
                   3185:                                                                                0x8f,   // keypad center
                   3186:                                                                                0x74,   // Right
                   3187:                                                                                0x90,   // keyapd +
                   3188:                                                                                0x75,   // End
                   3189:                                                                                0x91,   // Down
                   3190:                                                                                0x76,   // PgDn
                   3191:                                                                                0x92,   // Insert
                   3192:                                                                                0x93,   // Delete
                   3193:                                                                                0, 0, 0,
                   3194:                                                                                0x89,   // F11
                   3195:                                                                                0x8a,   // F12
                   3196:                                                                        };
                   3197:                                                                        scn = ctrl_map[scn - 0x35];
                   3198:                                                                }
                   3199:                                                        } else if(ir[i].Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) {
                   3200:                                                                if(scn >= 0x3b && scn <= 0x44) {
                   3201:                                                                        scn += 0x54 - 0x3b;     // F1 to F10
                   3202:                                                                } else if(scn == 0x57 || scn == 0x58) {
                   3203:                                                                        scn += 0x87 - 0x57;     // F11 & F12
                   3204:                                                                }
                   3205:                                                        } else if(scn == 0x57 || scn == 0x58) {
                   3206:                                                                scn += 0x85 - 0x57;
                   3207:                                                        }
                   3208:                                                        // ignore shift, ctrl, alt, win and menu keys
1.1.1.51  root     3209:                                                        if(scn != 0x1d && scn != 0x2a && scn != 0x36 && scn != 0x38 && !(scn >= 0x5b && scn <= 0x5d && scn == scn_old)) {
1.1.1.32  root     3210:                                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3211: #ifdef USE_SERVICE_THREAD
                   3212:                                                                        EnterCriticalSection(&key_buf_crit_sect);
                   3213: #endif
1.1.1.32  root     3214:                                                                        if(chr == 0) {
1.1.1.51  root     3215:                                                                                pcbios_set_key_buffer(0x00, ir[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY ? 0xe0 : 0x00);
1.1.1.32  root     3216:                                                                        }
1.1.1.51  root     3217:                                                                        pcbios_set_key_buffer(chr, scn);
1.1.1.35  root     3218: #ifdef USE_SERVICE_THREAD
                   3219:                                                                        LeaveCriticalSection(&key_buf_crit_sect);
                   3220: #endif
1.1.1.24  root     3221:                                                                }
                   3222:                                                        }
                   3223:                                                } else {
                   3224:                                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
                   3225:                                                                chr = 0;
                   3226:                                                                if(scn >= 0x02 && scn <= 0x0e) {
                   3227:                                                                        scn += 0x78 - 0x02;     // 1 to 0 - =
                   3228:                                                                }
                   3229:                                                        }
1.1.1.32  root     3230:                                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3231: #ifdef USE_SERVICE_THREAD
                   3232:                                                                EnterCriticalSection(&key_buf_crit_sect);
                   3233: #endif
1.1.1.51  root     3234:                                                                pcbios_set_key_buffer(chr, scn);
1.1.1.35  root     3235: #ifdef USE_SERVICE_THREAD
                   3236:                                                                LeaveCriticalSection(&key_buf_crit_sect);
                   3237: #endif
1.1.1.32  root     3238:                                                        }
1.1.1.24  root     3239:                                                }
1.1.1.33  root     3240:                                        } else if(chr == 0x03 && (ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))) {
                   3241:                                                // ctrl-break, ctrl-c
                   3242:                                                if(scn == 0x46) {
                   3243:                                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3244: #ifdef USE_SERVICE_THREAD
                   3245:                                                                EnterCriticalSection(&key_buf_crit_sect);
                   3246: #endif
1.1.1.51  root     3247:                                                                pcbios_set_key_buffer(0x00, 0x00);
1.1.1.35  root     3248: #ifdef USE_SERVICE_THREAD
                   3249:                                                                LeaveCriticalSection(&key_buf_crit_sect);
                   3250: #endif
1.1.1.33  root     3251:                                                        }
                   3252:                                                        ctrl_break_pressed = true;
                   3253:                                                        mem[0x471] = 0x80;
                   3254:                                                        raise_int_1bh = true;
                   3255:                                                } else {
                   3256:                                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3257: #ifdef USE_SERVICE_THREAD
                   3258:                                                                EnterCriticalSection(&key_buf_crit_sect);
                   3259: #endif
1.1.1.51  root     3260:                                                                pcbios_set_key_buffer(chr, scn);
1.1.1.35  root     3261: #ifdef USE_SERVICE_THREAD
                   3262:                                                                LeaveCriticalSection(&key_buf_crit_sect);
                   3263: #endif
1.1.1.33  root     3264:                                                        }
                   3265:                                                        ctrl_c_pressed = (scn == 0x2e);
                   3266:                                                }
                   3267:                                        } else {
                   3268:                                                // break
                   3269:                                                kbd_data |= 0x80;
1.1       root     3270:                                        }
1.1.1.24  root     3271:                                        result = key_changed = true;
1.1.1.36  root     3272:                                        // IME may be on and it may causes screen scroll up and cursor position change
                   3273:                                        cursor_moved = true;
1.1       root     3274:                                }
                   3275:                        }
                   3276:                }
                   3277:        }
1.1.1.35  root     3278: #ifdef USE_SERVICE_THREAD
                   3279:        LeaveCriticalSection(&input_crit_sect);
                   3280: #endif
1.1.1.24  root     3281:        return(result);
1.1.1.8   root     3282: }
                   3283: 
1.1.1.14  root     3284: bool update_key_buffer()
1.1.1.8   root     3285: {
1.1.1.35  root     3286:        if(update_console_input()) {
                   3287:                return(true);
                   3288:        }
                   3289:        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   3290: #ifdef USE_SERVICE_THREAD
                   3291:                EnterCriticalSection(&key_buf_crit_sect);
                   3292: #endif
                   3293:                bool empty = key_buf_char->empty();
                   3294: #ifdef USE_SERVICE_THREAD
                   3295:                LeaveCriticalSection(&key_buf_crit_sect);
                   3296: #endif
                   3297:                if(!empty) return(true);
                   3298:        }
                   3299:        return(false);
1.1.1.8   root     3300: }
                   3301: 
1.1.1.20  root     3302: /* ----------------------------------------------------------------------------
                   3303:        MS-DOS virtual machine
                   3304: ---------------------------------------------------------------------------- */
                   3305: 
1.1.1.32  root     3306: static const struct {
1.1.1.33  root     3307:        char *name;
                   3308:        DWORD lcid;
                   3309:        char *std;
                   3310:        char *dlt;
                   3311: } tz_table[] = {
                   3312:        // https://science.ksc.nasa.gov/software/winvn/userguide/3_1_4.htm
                   3313: //     0       GMT             Greenwich Mean Time             GMT0
                   3314:        {"GMT Standard Time",                   0x0809, "GMT", "BST"},          // (UTC+00:00) GB London (en-gb)
                   3315:        {"GMT Standard Time",                   0x1809, "GMT", "IST"},          // (UTC+00:00) IE Dublin (en-ie)
                   3316:        {"GMT Standard Time",                   0x0000, "WET", "WES"},          // (UTC+00:00) PT Lisbon
                   3317:        {"Greenwich Standard Time",             0x0000, "GMT", "GST"},          // (UTC+00:00) IS Reykjavik
                   3318: //     2       FST     FDT     Fernando De Noronha Std         FST2FDT
                   3319:        {"Mid-Atlantic Standard Time",          0x0416, "FST", "FDT"},          // (UTC-02:00) BR Noronha (pt-br)
                   3320:        {"UTC-02",                              0x0416, "FST", "FDT"},          // (UTC-02:00) BR Noronha (pt-br)
                   3321: //     3       BST             Brazil Standard Time            BST3
                   3322:        {"Bahia Standard Time",                 0x0000, "BST", "BDT"},          // (UTC-03:00) BR Bahia
                   3323:        {"SA Eastern Standard Time",            0x0000, "BST", "BDT"},          // (UTC-03:00) BR Fortaleza
                   3324:        {"Tocantins Standard Time",             0x0000, "BST", "BDT"},          // (UTC-03:00) BR Palmas
                   3325: //     3       EST     EDT     Eastern Standard (Brazil)       EST3EDT
                   3326:        {"E. South America Standard Time",      0x0000, "EST", "EDT"},          // (UTC-03:00) BR Sao Paulo
                   3327: //     3       GST             Greenland Standard Time         GST3
                   3328:        {"Greenland Standard Time",             0x0000, "GST", "GDT"},          // (UTC-03:00) GL Godthab
                   3329: //     3:30    NST     NDT     Newfoundland Standard Time      NST3:30NDT
                   3330:        {"Newfoundland Standard Time",          0x0000, "NST", "NDT"},          // (UTC-03:30) CA St.Johns
                   3331: //     4       AST     ADT     Atlantic Standard Time          AST4ADT
                   3332:        {"Atlantic Standard Time",              0x0000, "AST", "ADT"},          // (UTC-04:00) CA Halifax
                   3333: //     4       WST     WDT     Western Standard (Brazil)       WST4WDT
                   3334:        {"Central Brazilian Standard Time",     0x0000, "WST", "WDT"},          // (UTC-04:00) BR Cuiaba
                   3335:        {"SA Western Standard Time",            0x0000, "WST", "WDT"},          // (UTC-04:00) BR Manaus
                   3336: //     5       EST     EDT     Eastern Standard Time           EST5EDT
                   3337:        {"Eastern Standard Time",               0x0000, "EST", "EDT"},          // (UTC-05:00) US New York
                   3338:        {"Eastern Standard Time (Mexico)",      0x0000, "EST", "EDT"},          // (UTC-05:00) MX Cancun
                   3339:        {"US Eastern Standard Time",            0x0000, "EST", "EDT"},          // (UTC-05:00) US Indianapolis
                   3340: //     5       CST     CDT     Chile Standard Time             CST5CDT
                   3341:        {"Pacific SA Standard Time",            0x0000, "CST", "CDT"},          // (UTC-04:00) CL Santiago
                   3342: //     5       AST     ADT     Acre Standard Time              AST5ADT
                   3343:        {"SA Pacific Standard Time",            0x0000, "AST", "ADT"},          // (UTC-05:00) BR Rio Branco
                   3344: //     5       CST     CDT     Cuba Standard Time              CST5CDT
                   3345:        {"Cuba Standard Time",                  0x0000, "CST", "CDT"},          // (UTC-05:00) CU Havana
                   3346: //     6       CST     CDT     Central Standard Time           CST6CDT
                   3347:        {"Canada Central Standard Time",        0x0000, "CST", "CDT"},          // (UTC-06:00) CA Regina
                   3348:        {"Central Standard Time",               0x0000, "CST", "CDT"},          // (UTC-06:00) US Chicago
                   3349:        {"Central Standard Time (Mexico)",      0x0000, "CST", "CDT"},          // (UTC-06:00) MX Mexico City
                   3350: //     6       EST     EDT     Easter Island Standard          EST6EDT
                   3351:        {"Easter Island Standard Time",         0x0000, "EST", "EDT"},          // (UTC-06:00) CL Easter
                   3352: //     7       MST     MDT     Mountain Standard Time          MST7MDT
                   3353:        {"Mountain Standard Time",              0x0000, "MST", "MDT"},          // (UTC-07:00) US Denver
                   3354:        {"Mountain Standard Time (Mexico)",     0x0000, "MST", "MDT"},          // (UTC-07:00) MX Chihuahua
                   3355:        {"US Mountain Standard Time",           0x0000, "MST", "MDT"},          // (UTC-07:00) US Phoenix
                   3356: //     8       PST     PDT     Pacific Standard Time           PST8PDT
                   3357:        {"Pacific Standard Time",               0x0000, "PST", "PDT"},          // (UTC-08:00) US Los Angeles
                   3358:        {"Pacific Standard Time (Mexico)",      0x0000, "PST", "PDT"},          // (UTC-08:00) MX Tijuana
                   3359: //     9       AKS     AKD     Alaska Standard Time            AKS9AKD
                   3360: //     9       YST     YDT     Yukon Standard Time             YST9YST
                   3361:        {"Alaskan Standard Time",               0x0000, "AKS", "AKD"},          // (UTC-09:00) US Anchorage
                   3362: //     10      HST     HDT     Hawaii Standard Time            HST10HDT
                   3363:        {"Aleutian Standard Time",              0x0000, "HST", "HDT"},          // (UTC-10:00) US Aleutian
                   3364:        {"Hawaiian Standard Time",              0x0000, "HST", "HDT"},          // (UTC-10:00) US Honolulu
                   3365: //     11      SST             Samoa Standard Time             SST11
                   3366:        {"Samoa Standard Time",                 0x0000, "SST", "SDT"},          // (UTC-11:00) US Samoa
                   3367: //     -12     NZS     NZD     New Zealand Standard Time       NZS-12NZD
                   3368:        {"New Zealand Standard Time",           0x0000, "NZS", "NZD"},          // (UTC+12:00) NZ Auckland
                   3369: //     -10     GST             Guam Standard Time              GST-10
                   3370:        {"West Pacific Standard Time",          0x0000, "GST", "GDT"},          // (UTC+10:00) GU Guam
                   3371: //     -10     EAS     EAD     Eastern Australian Standard     EAS-10EAD
                   3372:        {"AUS Eastern Standard Time",           0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Sydney
                   3373:        {"E. Australia Standard Time",          0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Brisbane
                   3374:        {"Tasmania Standard Time",              0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Hobart
                   3375: //     -9:30   CAS     CAD     Central Australian Standard     CAS-9:30CAD
                   3376:        {"AUS Central Standard Time",           0x0000, "CAS", "CAD"},          // (UTC+09:30) AU Darwin
                   3377:        {"Cen. Australia Standard Time",        0x0000, "CAS", "CAD"},          // (UTC+09:30) AU Adelaide
                   3378: //     -9      JST             Japan Standard Time             JST-9
                   3379:        {"Tokyo Standard Time",                 0x0000, "JST", "JDT"},          // (UTC+09:00) JP Tokyo
                   3380: //     -9      KST     KDT     Korean Standard Time            KST-9KDT
                   3381:        {"Korea Standard Time",                 0x0000, "KST", "KDT"},          // (UTC+09:00) KR Seoul
                   3382:        {"North Korea Standard Time",           0x0000, "KST", "KDT"},          // (UTC+08:30) KP Pyongyang
                   3383: //     -8      HKT             Hong Kong Time                  HKT-8
                   3384:        {"China Standard Time",                 0x0C04, "HKT", "HKS"},          // (UTC+08:00) HK Hong Kong (zh-hk)
                   3385: //     -8      CCT             China Coast Time                CCT-8
                   3386:        {"China Standard Time",                 0x0000, "CCT", "CDT"},          // (UTC+08:00) CN Shanghai
                   3387:        {"Taipei Standard Time",                0x0000, "CCT", "CDT"},          // (UTC+08:00) TW Taipei
                   3388: //     -8      SST             Singapore Standard Time         SST-8
                   3389:        {"Singapore Standard Time",             0x0000, "SST", "SDT"},          // (UTC+08:00) SG Singapore
                   3390: //     -8      WAS     WAD     Western Australian Standard     WAS-8WAD
                   3391:        {"Aus Central W. Standard Time",        0x0000, "WAS", "WAD"},          // (UTC+08:45) AU Eucla
                   3392:        {"W. Australia Standard Time",          0x0000, "WAS", "WAD"},          // (UTC+08:00) AU Perth
                   3393: //     -7:30   JT              Java Standard Time              JST-7:30
                   3394: //     -7      NST             North Sumatra Time              NST-7
                   3395:        {"SE Asia Standard Time",               0x0000, "NST", "NDT"},          // (UTC+07:00) ID Jakarta
                   3396: //     -5:30   IST             Indian Standard Time            IST-5:30
                   3397:        {"India Standard Time",                 0x0000, "IST", "IDT"},          // (UTC+05:30) IN Calcutta
                   3398: //     -3:30   IST     IDT     Iran Standard Time              IST-3:30IDT
                   3399:        {"Iran Standard Time",                  0x0000, "IST", "IDT"},          // (UTC+03:30) IR Tehran
                   3400: //     -3      MSK     MSD     Moscow Winter Time              MSK-3MSD
                   3401:        {"Belarus Standard Time",               0x0000, "MSK", "MSD"},          // (UTC+03:00) BY Minsk
                   3402:        {"Russian Standard Time",               0x0000, "MSK", "MSD"},          // (UTC+03:00) RU Moscow
                   3403: //     -2      EET             Eastern Europe Time             EET-2
                   3404:        {"E. Europe Standard Time",             0x0000, "EET", "EES"},          // (UTC+02:00) MD Chisinau
                   3405:        {"FLE Standard Time",                   0x0000, "EET", "EES"},          // (UTC+02:00) UA Kiev
                   3406:        {"GTB Standard Time",                   0x0000, "EET", "EES"},          // (UTC+02:00) RO Bucharest
                   3407:        {"Kaliningrad Standard Time",           0x0000, "EET", "EES"},          // (UTC+02:00) RU Kaliningrad
                   3408: //     -2      IST     IDT     Israel Standard Time            IST-2IDT
                   3409:        {"Israel Standard Time",                0x0000, "IST", "IDT"},          // (UTC+02:00) IL Jerusalem
                   3410: //     -1      MEZ     MES     Middle European Time            MEZ-1MES
                   3411: //     -1      SWT     SST     Swedish Winter Time             SWT-1SST
                   3412: //     -1      FWT     FST     French Winter Time              FWT-1FST
                   3413: //     -1      CET     CES     Central European Time           CET-1CES
                   3414:        {"Central Europe Standard Time",        0x0000, "CET", "CES"},          // (UTC+01:00) HU Budapest
                   3415:        {"Central European Standard Time",      0x0000, "CET", "CES"},          // (UTC+01:00) PL Warsaw
                   3416:        {"Romance Standard Time",               0x0000, "CET", "CES"},          // (UTC+01:00) FR Paris
                   3417:        {"W. Europe Standard Time",             0x0000, "CET", "CES"},          // (UTC+01:00) DE Berlin
                   3418: //     -1      WAT             West African Time               WAT-1
                   3419:        {"Namibia Standard Time",               0x0000, "WAT", "WAS"},          // (UTC+01:00) NA Windhoek
                   3420:        {"W. Central Africa Standard Time",     0x0000, "WAT", "WAS"},          // (UTC+01:00) NG Lagos
                   3421: //     0       UTC             Universal Coordinated Time      UTC0
                   3422:        {"UTC",                                 0x0000, "UTC", ""   },          // (UTC+00:00) GMT+0
                   3423:        {"UTC-02",                              0x0000, "UTC", ""   },          // (UTC-02:00) GMT+2
                   3424:        {"UTC-08",                              0x0000, "UTC", ""   },          // (UTC-08:00) GMT+8
                   3425:        {"UTC-09",                              0x0000, "UTC", ""   },          // (UTC-09:00) GMT+9
                   3426:        {"UTC-11",                              0x0000, "UTC", ""   },          // (UTC-11:00) GMT+11
                   3427:        {"UTC+12",                              0x0000, "UTC", ""   },          // (UTC+12:00) GMT-12
                   3428: };
                   3429: 
                   3430: static const struct {
1.1.1.32  root     3431:        UINT16 code;
                   3432:        char *message_english;
                   3433:        char *message_japanese;
                   3434: } standard_error_table[] = {
                   3435:        {0x01,  "Invalid function", "�����ȃt�@���N�V�����ł�."},
                   3436:        {0x02,  "File not found", "�t�@�C�������‚���܂���."},
                   3437:        {0x03,  "Path not found", "�p�X�����‚���܂���."},
                   3438:        {0x04,  "Too many open files", "�J����Ă���t�@�C�����������܂�."},
                   3439:        {0x05,  "Access denied", "�A�N�Z�X�͋��ۂ���܂���."},
                   3440:        {0x06,  "Invalid handle", "�����ȃn���h���ł�."},
                   3441:        {0x07,  "Memory control blocks destroyed", "����������u���b�N���j������܂���."},
                   3442:        {0x08,  "Insufficient memory", "������������܂���."},
                   3443:        {0x09,  "Invalid memory block address", "�����ȃ������u���b�N�̃A�h���X�ł�."},
                   3444:        {0x0A,  "Invalid Environment", "�����Ȋ‹��ł�."},
                   3445:        {0x0B,  "Invalid format", "�����ȃt�H�[�}�b�g�ł�."},
                   3446:        {0x0C,  "Invalid function parameter", "�����ȃt�@���N�V�����p�����[�^�ł�."},
                   3447:        {0x0D,  "Invalid data", "�����ȃf�[�^�ł�."},
                   3448:        {0x0F,  "Invalid drive specification", "�����ȃh���C�u�̎w��ł�."},
                   3449:        {0x10,  "Attempt to remove current directory", "���݂̃f�B���N�g�����폜���悤�Ƃ��܂���."},
                   3450:        {0x11,  "Not same device", "�����f�o�C�X�ł͂���܂���."},
                   3451:        {0x12,  "No more files", "�t�@�C���͂���ȏ゠��܂���."},
                   3452:        {0x13,  "Write protect error", "�������ݕی�G���[�ł�."},
                   3453:        {0x14,  "Invalid unit", "�����ȃ��j�b�g�ł�."},
                   3454:        {0x15,  "Not ready", "�������ł��Ă��܂���."},
                   3455:        {0x16,  "Invalid device request", "�����ȃf�o�C�X�v���ł�."},
                   3456:        {0x17,  "Data error", "�f�[�^�G���[�ł�."},
                   3457:        {0x18,  "Invalid device request parameters", "�����ȃf�o�C�X�v���̃p�����[�^�ł�."},
                   3458:        {0x19,  "Seek error", "�V�[�N�G���[�ł�."},
                   3459:        {0x1A,  "Invalid media type", "�����ȃ��f�B�A�̎�ނł�."},
                   3460:        {0x1B,  "Sector not found", "�Z�N�^�����‚���܂���."},
                   3461:        {0x1C,  "Printer out of paper error", "�v�����^�̗p��������܂���."},
                   3462:        {0x1D,  "Write fault error", "�������݃G���[�ł�."},
                   3463:        {0x1E,  "Read fault error", "�ǂݎ��G���[�ł�."},
                   3464:        {0x1F,  "General failure", "�G���[�ł�."},
                   3465:        {0x20,  "Sharing violation", "���L�ᔽ�ł�."},
                   3466:        {0x21,  "Lock violation", "���b�N�ᔽ�ł�."},
                   3467:        {0x22,  "Invalid disk change", "�����ȃf�B�X�N�̌����ł�."},
                   3468:        {0x23,  "FCB unavailable", "FCB �͎g���܂���."},
                   3469:        {0x24,  "System resource exhausted", "�V�X�e�����\�[�X�͂����g���܂���."},
                   3470:        {0x25,  "Code page mismatch", "�R�[�h �y�[�W����v���܂���."},
                   3471:        {0x26,  "Out of input", "���͂��I���܂���."},
                   3472:        {0x27,  "Insufficient disk space", "�f�B�X�N�̋󂫗̈悪����܂���."},
                   3473: /*
                   3474:        {0x32,  "Network request not supported", NULL},
                   3475:        {0x33,  "Remote computer not listening", NULL},
                   3476:        {0x34,  "Duplicate name on network", NULL},
                   3477:        {0x35,  "Network name not found", NULL},
                   3478:        {0x36,  "Network busy", NULL},
                   3479:        {0x37,  "Network device no longer exists", NULL},
                   3480:        {0x38,  "Network BIOS command limit exceeded", NULL},
                   3481:        {0x39,  "Network adapter hardware error", NULL},
                   3482:        {0x3A,  "Incorrect response from network", NULL},
                   3483:        {0x3B,  "Unexpected network error", NULL},
                   3484:        {0x3C,  "Incompatible remote adapter", NULL},
                   3485:        {0x3D,  "Print queue full", NULL},
                   3486:        {0x3E,  "Queue not full", NULL},
                   3487:        {0x3F,  "Not enough space to print file", NULL},
                   3488:        {0x40,  "Network name was deleted", NULL},
                   3489:        {0x41,  "Network: Access denied", NULL},
                   3490:        {0x42,  "Network device type incorrect", NULL},
                   3491:        {0x43,  "Network name not found", NULL},
                   3492:        {0x44,  "Network name limit exceeded", NULL},
                   3493:        {0x45,  "Network BIOS session limit exceeded", NULL},
                   3494:        {0x46,  "Temporarily paused", NULL},
                   3495:        {0x47,  "Network request not accepted", NULL},
                   3496:        {0x48,  "Network print/disk redirection paused", NULL},
                   3497:        {0x49,  "Network software not installed", NULL},
                   3498:        {0x4A,  "Unexpected adapter close", NULL},
                   3499: */
                   3500:        {0x50,  "File exists", "�t�@�C���͑��݂��܂�."},
1.1.1.33  root     3501:        {0x52,  "Cannot make directory entry", "�f�B���N�g���G���g�����쐬�ł��܂���."},
1.1.1.32  root     3502:        {0x53,  "Fail on INT 24", "INT 24 �Ŏ��s���܂���."},
                   3503:        {0x54,  "Too many redirections", "���_�C���N�g���������܂�."},
                   3504:        {0x55,  "Duplicate redirection", "���_�C���N�g���d�����Ă��܂�."},
                   3505:        {0x56,  "Invalid password", "�p�X���[�h���Ⴂ�܂�."},
                   3506:        {0x57,  "Invalid parameter", "�p�����[�^�̎w�肪�Ⴂ�܂�."},
                   3507:        {0x58,  "Network data fault", "�l�b�g���[�N�f�[�^�̃G���[�ł�."},
                   3508:        {0x59,  "Function not supported by network", "�t�@���N�V�����̓l�b�g���[�N�ŃT�|�[�g����Ă��܂���."},
                   3509:        {0x5A,  "Required system component not installe", "�K�v�ȃV�X�e�� �R���|�[�l���g���g�ݍ��܂�Ă��܂���."},
                   3510: /*
                   3511:        {0x64,  "Unknown error", "�s���ȃG���[�ł�."},
                   3512:        {0x65,  "Not ready", "�������ł��Ă��܂���."},
                   3513:        {0x66,  "EMS memory no longer valid", "EMS �������͂����L���ł͂���܂���."},
                   3514:        {0x67,  "CDROM not High Sierra or ISO-9660 format", "CDROM �� High Sierra �܂��� ISO-9660 �t�H�[�}�b�g�ł͂���܂���."},
                   3515:        {0x68,  "Door open", "���o�[���‚܂��Ă��܂���."
                   3516: */
                   3517:        {0xB0,  "Volume is not locked", "�{�����[�������b�N����Ă��܂���."},
                   3518:        {0xB1,  "Volume is locked in drive", "�{�����[�������b�N����Ă��܂�."},
                   3519:        {0xB2,  "Volume is not removable", "�{�����[���͎��O���ł��܂���."},
                   3520:        {0xB4,  "Lock count has been exceeded", "�{�����[��������ȏネ�b�N�ł��܂���."},
                   3521:        {0xB5,  "A valid eject request failed", "���o���Ɏ��s���܂���."},
                   3522:        {-1  ,  "Unknown error", "�s���ȃG���[�ł�."},
                   3523: };
                   3524: 
                   3525: static const struct {
                   3526:        UINT16 code;
                   3527:        char *message_english;
                   3528:        char *message_japanese;
                   3529: } param_error_table[] = {
                   3530:        {0x01,  "Too many parameters", "�p�����[�^���������܂�."},
                   3531:        {0x02,  "Required parameter missing", "�p�����[�^������܂���."},
                   3532:        {0x03,  "Invalid switch", "�����ȃX�C�b�`�ł�."},
                   3533:        {0x04,  "Invalid keyword", "�����ȃL�[���[�h�ł�."},
                   3534:        {0x06,  "Parameter value not in allowed range", "�p�����[�^�̒l���͈͂𒴂��Ă��܂�."},
                   3535:        {0x07,  "Parameter value not allowed", "���̃p�����[�^�̒l�͎g���܂���."},
                   3536:        {0x08,  "Parameter value not allowed", "���̃p�����[�^�̒l�͎g���܂���."},
                   3537:        {0x09,  "Parameter format not correct", "�p�����[�^�̏������Ⴂ�܂�."},
                   3538:        {0x0A,  "Invalid parameter", "�����ȃp�����[�^�ł�."},
                   3539:        {0x0B,  "Invalid parameter combination", "�����ȃp�����[�^�̑g�ݍ��킹�ł�."},
                   3540:        {-1  ,  "Unknown error", "�s���ȃG���[�ł�."},
                   3541: };
                   3542: 
                   3543: static const struct {
                   3544:        UINT16 code;
                   3545:        char *message_english;
                   3546:        char *message_japanese;
                   3547: } critical_error_table[] = {
                   3548:        {0x00,  "Write protect error", "�������ݕی�G���[�ł�."},
                   3549:        {0x01,  "Invalid unit", "�����ȃ��j�b�g�ł�."},
                   3550:        {0x02,  "Not ready", "�������ł��Ă��܂���."},
                   3551:        {0x03,  "Invalid device request", "�����ȃf�o�C�X�v���ł�."},
                   3552:        {0x04,  "Data error", "�f�[�^�G���[�ł�."},
                   3553:        {0x05,  "Invalid device request parameters", "�����ȃf�o�C�X�v���̃p�����[�^�ł�."},
                   3554:        {0x06,  "Seek error", "�V�[�N�G���[�ł�."},
                   3555:        {0x07,  "Invalid media type", "�����ȃ��f�B�A�̎�ނł�."},
                   3556:        {0x08,  "Sector not found", "�Z�N�^�����‚���܂���."},
                   3557:        {0x09,  "Printer out of paper error", "�v�����^�̗p��������܂���."},
                   3558:        {0x0A,  "Write fault error", "�������݃G���[�ł�."},
                   3559:        {0x0B,  "Read fault error", "�ǂݎ��G���[�ł�."},
                   3560:        {0x0C,  "General failure", "�G���[�ł�."},
                   3561:        {0x0D,  "Sharing violation", "���L�ᔽ�ł�."},
                   3562:        {0x0E,  "Lock violation", "���b�N�ᔽ�ł�."},
                   3563:        {0x0F,  "Invalid disk change", "�����ȃf�B�X�N�̌����ł�."},
                   3564:        {0x10,  "FCB unavailable", "FCB �͎g���܂���."},
                   3565:        {0x11,  "System resource exhausted", "�V�X�e�����\�[�X�͂����g���܂���."},
                   3566:        {0x12,  "Code page mismatch", "�R�[�h �y�[�W����v���܂���."},
                   3567:        {0x13,  "Out of input", "���͂��I���܂���."},
                   3568:        {0x14,  "Insufficient disk space", "�f�B�X�N�̋󂫗̈悪����܂���."},
                   3569:        {-1  ,  "Critical error", "�v���I�ȃG���[�ł�."},
                   3570: };
                   3571: 
1.1.1.20  root     3572: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg);
                   3573: int msdos_psp_get_file_table(int fd, int psp_seg);
                   3574: void msdos_putch(UINT8 data);
1.1.1.50  root     3575: void msdos_putch_fast(UINT8 data);
1.1.1.35  root     3576: #ifdef USE_SERVICE_THREAD
                   3577: void msdos_putch_tmp(UINT8 data);
                   3578: #endif
1.1.1.45  root     3579: const char *msdos_short_path(const char *path);
1.1.1.44  root     3580: bool msdos_is_valid_drive(int drv);
                   3581: bool msdos_is_removable_drive(int drv);
                   3582: bool msdos_is_cdrom_drive(int drv);
                   3583: bool msdos_is_remote_drive(int drv);
                   3584: bool msdos_is_subst_drive(int drv);
1.1.1.20  root     3585: 
1.1       root     3586: // process info
                   3587: 
                   3588: process_t *msdos_process_info_create(UINT16 psp_seg)
                   3589: {
                   3590:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3591:                if(process[i].psp == 0 || process[i].psp == psp_seg) {
                   3592:                        memset(&process[i], 0, sizeof(process_t));
                   3593:                        process[i].psp = psp_seg;
                   3594:                        return(&process[i]);
                   3595:                }
                   3596:        }
                   3597:        fatalerror("too many processes\n");
                   3598:        return(NULL);
                   3599: }
                   3600: 
1.1.1.52! root     3601: process_t *msdos_process_info_get(UINT16 psp_seg, bool show_error = true)
1.1       root     3602: {
                   3603:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3604:                if(process[i].psp == psp_seg) {
                   3605:                        return(&process[i]);
                   3606:                }
                   3607:        }
1.1.1.33  root     3608:        if(show_error) {
                   3609:                fatalerror("invalid psp address\n");
                   3610:        }
1.1       root     3611:        return(NULL);
                   3612: }
                   3613: 
1.1.1.23  root     3614: void msdos_sda_update(int psp_seg)
                   3615: {
                   3616:        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   3617:        
                   3618:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3619:                if(process[i].psp == psp_seg) {
                   3620:                        sda->switchar = process[i].switchar;
                   3621:                        sda->current_dta.w.l = process[i].dta.w.l;
                   3622:                        sda->current_dta.w.h = process[i].dta.w.h;
                   3623:                        sda->current_psp = process[i].psp;
                   3624:                        break;
                   3625:                }
                   3626:        }
                   3627:        sda->malloc_strategy = malloc_strategy;
                   3628:        sda->return_code = retval;
                   3629:        sda->current_drive = _getdrive();
                   3630: }
                   3631: 
1.1.1.13  root     3632: // dta info
                   3633: 
                   3634: void msdos_dta_info_init()
                   3635: {
1.1.1.14  root     3636:        for(int i = 0; i < MAX_DTAINFO; i++) {
1.1.1.13  root     3637:                dtalist[i].find_handle = INVALID_HANDLE_VALUE;
                   3638:        }
                   3639: }
                   3640: 
                   3641: dtainfo_t *msdos_dta_info_get(UINT16 psp_seg, UINT32 dta_laddr)
                   3642: {
                   3643:        dtainfo_t *free_dta = NULL;
1.1.1.14  root     3644:        for(int i = 0; i < MAX_DTAINFO; i++) {
                   3645:                if(dtalist[i].find_handle == INVALID_HANDLE_VALUE) {
                   3646:                        if(free_dta == NULL) {
1.1.1.13  root     3647:                                free_dta = &dtalist[i];
                   3648:                        }
1.1.1.14  root     3649:                } else if(dta_laddr < LFN_DTA_LADDR && dtalist[i].dta == dta_laddr) {
1.1.1.13  root     3650:                        return(&dtalist[i]);
                   3651:                }
                   3652:        }
1.1.1.14  root     3653:        if(free_dta) {
1.1.1.13  root     3654:                free_dta->psp = psp_seg;
                   3655:                free_dta->dta = dta_laddr;
                   3656:                return(free_dta);
                   3657:        }
                   3658:        fatalerror("too many dta\n");
                   3659:        return(NULL);
                   3660: }
                   3661: 
                   3662: void msdos_dta_info_free(UINT16 psp_seg)
                   3663: {
1.1.1.14  root     3664:        for(int i = 0; i < MAX_DTAINFO; i++) {
                   3665:                if(dtalist[i].psp == psp_seg && dtalist[i].find_handle != INVALID_HANDLE_VALUE) {
1.1.1.13  root     3666:                        FindClose(dtalist[i].find_handle);
                   3667:                        dtalist[i].find_handle = INVALID_HANDLE_VALUE;
                   3668:                }
                   3669:        }
                   3670: }
                   3671: 
1.1       root     3672: void msdos_cds_update(int drv)
                   3673: {
1.1.1.44  root     3674:        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * drv);
1.1       root     3675:        
1.1.1.44  root     3676:        memset(cds, 0, 88);
                   3677:        
                   3678:        if(msdos_is_valid_drive(drv)) {
                   3679:                char path[MAX_PATH];
                   3680:                if(msdos_is_remote_drive(drv)) {
                   3681:                        cds->drive_attrib = 0xc000;     // network drive
                   3682:                } else if(msdos_is_subst_drive(drv)) {
                   3683:                        cds->drive_attrib = 0x5000;     // subst drive
                   3684:                } else {
                   3685:                        cds->drive_attrib = 0x4000;     // physical drive
                   3686:                }
                   3687:                if(_getdcwd(drv + 1, path, MAX_PATH) != NULL) {
                   3688:                        strcpy_s(cds->path_name, sizeof(cds->path_name), msdos_short_path(path));
                   3689:                }
                   3690:        }
                   3691:        if(cds->path_name[0] == '\0') {
                   3692:                sprintf(cds->path_name, "%c:\\", 'A' + drv);
                   3693:        }
                   3694:        cds->dpb_ptr.w.h = DPB_TOP >> 4;
                   3695:        cds->dpb_ptr.w.l = sizeof(dpb_t) * drv;
                   3696:        cds->word_1 = cds->word_2 = 0xffff;
                   3697:        cds->word_3 = 0xffff; // stored user data from INT 21/AX=5F03h if this is network drive
                   3698:        cds->bs_offset = 2;
                   3699: }
                   3700: 
1.1.1.45  root     3701: void msdos_cds_update(int drv, const char *path)
1.1.1.44  root     3702: {
                   3703:        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * drv);
                   3704:        
                   3705:        strcpy_s(cds->path_name, sizeof(cds->path_name), msdos_short_path(path));
1.1       root     3706: }
                   3707: 
1.1.1.17  root     3708: // nls information tables
                   3709: 
                   3710: // uppercase table (func 6502h)
                   3711: void msdos_upper_table_update()
                   3712: {
                   3713:        *(UINT16 *)(mem + UPPERTABLE_TOP) = 0x80;
1.1.1.22  root     3714:        for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17  root     3715:                UINT8 c[4];
1.1.1.33  root     3716:                *(UINT32 *)c = 0;               // reset internal conversion state
                   3717:                CharUpperBuffA((LPSTR)c, 4);    // (workaround for MBCS codepage)
1.1.1.17  root     3718:                c[0] = 0x80 + i;
                   3719:                DWORD rc = CharUpperBuffA((LPSTR)c, 1);
                   3720:                mem[UPPERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
                   3721:        }
                   3722: }
                   3723: 
1.1.1.23  root     3724: // lowercase table (func 6503h)
                   3725: void msdos_lower_table_update()
                   3726: {
                   3727:        *(UINT16 *)(mem + LOWERTABLE_TOP) = 0x80;
                   3728:        for(unsigned i = 0; i < 0x80; ++i) {
                   3729:                UINT8 c[4];
1.1.1.33  root     3730:                *(UINT32 *)c = 0;               // reset internal conversion state
                   3731:                CharLowerBuffA((LPSTR)c, 4);    // (workaround for MBCS codepage)
1.1.1.23  root     3732:                c[0] = 0x80 + i;
                   3733:                DWORD rc = CharLowerBuffA((LPSTR)c, 1);
                   3734:                mem[LOWERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
                   3735:        }
                   3736: }
                   3737: 
1.1.1.17  root     3738: // filename uppercase table (func 6504h)
                   3739: void msdos_filename_upper_table_init()
                   3740: {
                   3741:        // depended on (file)system, not on active codepage
                   3742:        // temporary solution: just filling data
                   3743:        *(UINT16 *)(mem + FILENAME_UPPERTABLE_TOP) = 0x80;
1.1.1.22  root     3744:        for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17  root     3745:                mem[FILENAME_UPPERTABLE_TOP + 2 + i] = 0x80 + i;
                   3746:        }
                   3747: }
                   3748: 
                   3749: // filaname terminator table (func 6505h)
                   3750: void msdos_filename_terminator_table_init()
                   3751: {
                   3752:        const char illegal_chars[] = ".\"/\\[]:|<>+=;,";        // for standard MS-DOS fs.
                   3753:        UINT8 *data = mem + FILENAME_TERMINATOR_TOP;
                   3754:        
                   3755:        data[2] = 1;            // marker? (permissible character value)
                   3756:        data[3] = 0x00;         // 00h...FFh
                   3757:        data[4] = 0xff;
                   3758:        data[5] = 0;            // marker? (excluded character)
                   3759:        data[6] = 0x00;         // 00h...20h
                   3760:        data[7] = 0x20;
                   3761:        data[8] = 2;            // marker? (illegal characters for filename)
                   3762:        data[9] = (UINT8)strlen(illegal_chars);
                   3763:        memcpy(data + 10, illegal_chars, data[9]);
                   3764:        
                   3765:        // total length
                   3766:        *(UINT16 *)data = (10 - 2) + data[9];
                   3767: }
                   3768: 
                   3769: // collating table (func 6506h)
                   3770: void msdos_collating_table_update()
                   3771: {
                   3772:        // temporary solution: just filling data
                   3773:        *(UINT16 *)(mem + COLLATING_TABLE_TOP) = 0x100;
1.1.1.22  root     3774:        for(unsigned i = 0; i < 256; ++i) {
1.1.1.17  root     3775:                mem[COLLATING_TABLE_TOP + 2 + i] = i;
                   3776:        }
                   3777: }
                   3778: 
1.1       root     3779: // dbcs
                   3780: 
                   3781: void msdos_dbcs_table_update()
                   3782: {
                   3783:        UINT8 dbcs_data[DBCS_SIZE];
                   3784:        memset(dbcs_data, 0, sizeof(dbcs_data));
                   3785:        
                   3786:        CPINFO info;
                   3787:        GetCPInfo(active_code_page, &info);
                   3788:        
                   3789:        if(info.MaxCharSize != 1) {
                   3790:                for(int i = 0;; i += 2) {
                   3791:                        UINT8 lo = info.LeadByte[i + 0];
                   3792:                        UINT8 hi = info.LeadByte[i + 1];
                   3793:                        dbcs_data[2 + i + 0] = lo;
                   3794:                        dbcs_data[2 + i + 1] = hi;
                   3795:                        if(lo == 0 && hi == 0) {
                   3796:                                dbcs_data[0] = i + 2;
                   3797:                                break;
                   3798:                        }
                   3799:                }
                   3800:        } else {
                   3801:                dbcs_data[0] = 2;       // ???
                   3802:        }
                   3803:        memcpy(mem + DBCS_TOP, dbcs_data, sizeof(dbcs_data));
                   3804: }
                   3805: 
1.1.1.17  root     3806: void msdos_dbcs_table_finish()
                   3807: {
1.1.1.32  root     3808:        if(system_code_page != _getmbcp()) {
1.1.1.17  root     3809:                _setmbcp(system_code_page);
                   3810:        }
1.1.1.32  root     3811:        if(console_code_page != GetConsoleCP()) {
                   3812:                SetConsoleCP(console_code_page);
                   3813:                SetConsoleOutputCP(console_code_page);
                   3814:        }
1.1.1.17  root     3815: }
                   3816: 
                   3817: void msdos_nls_tables_init()
1.1       root     3818: {
1.1.1.32  root     3819:        active_code_page = console_code_page = GetConsoleCP();
                   3820:        system_code_page = _getmbcp();
                   3821:        
                   3822:        if(active_code_page != system_code_page) {
                   3823:                if(_setmbcp(active_code_page) != 0) {
                   3824:                        active_code_page = system_code_page;
                   3825:                }
                   3826:        }
                   3827:        
1.1.1.17  root     3828:        msdos_upper_table_update();
1.1.1.23  root     3829:        msdos_lower_table_update();
1.1.1.17  root     3830:        msdos_filename_terminator_table_init();
                   3831:        msdos_filename_upper_table_init();
                   3832:        msdos_collating_table_update();
1.1       root     3833:        msdos_dbcs_table_update();
                   3834: }
                   3835: 
1.1.1.17  root     3836: void msdos_nls_tables_update()
1.1       root     3837: {
1.1.1.17  root     3838:        msdos_dbcs_table_update();
                   3839:        msdos_upper_table_update();
1.1.1.23  root     3840:        msdos_lower_table_update();
                   3841: //     msdos_collating_table_update();
1.1       root     3842: }
                   3843: 
                   3844: int msdos_lead_byte_check(UINT8 code)
                   3845: {
                   3846:        UINT8 *dbcs_table = mem + DBCS_TABLE;
                   3847:        
                   3848:        for(int i = 0;; i += 2) {
                   3849:                UINT8 lo = dbcs_table[i + 0];
                   3850:                UINT8 hi = dbcs_table[i + 1];
                   3851:                if(lo == 0 && hi == 0) {
                   3852:                        break;
                   3853:                }
                   3854:                if(lo <= code && code <= hi) {
                   3855:                        return(1);
                   3856:                }
                   3857:        }
                   3858:        return(0);
                   3859: }
                   3860: 
1.1.1.20  root     3861: int msdos_ctrl_code_check(UINT8 code)
                   3862: {
1.1.1.22  root     3863:        return (code >= 0x01 && code <= 0x1a && code != 0x07 && code != 0x08 && code != 0x09 && code != 0x0a && code != 0x0d);
1.1.1.20  root     3864: }
                   3865: 
1.1.1.36  root     3866: int msdos_kanji_2nd_byte_check(UINT8 *buf, int n)
                   3867: {
                   3868:        int is_kanji_1st = 0;
                   3869:        int is_kanji_2nd = 0;
                   3870:        
                   3871:        for(int p = 0;; p++) {
                   3872:                if(is_kanji_1st) {
                   3873:                        is_kanji_1st = 0;
                   3874:                        is_kanji_2nd = 1;
                   3875:                } else if(msdos_lead_byte_check(buf[p])) {
                   3876:                        is_kanji_1st = 1;
                   3877:                }
                   3878:                if(p == n) {
                   3879:                        return(is_kanji_2nd);
                   3880:                }
                   3881:                is_kanji_2nd = 0;
                   3882:        }
                   3883: }
                   3884: 
1.1       root     3885: // file control
                   3886: 
1.1.1.45  root     3887: const char *msdos_remove_double_quote(const char *path)
1.1.1.14  root     3888: {
                   3889:        static char tmp[MAX_PATH];
                   3890:        
                   3891:        if(strlen(path) >= 2 && path[0] == '"' && path[strlen(path) - 1] == '"') {
1.1.1.45  root     3892:                memset(tmp, 0, sizeof(tmp));
1.1.1.14  root     3893:                memcpy(tmp, path + 1, strlen(path) - 2);
                   3894:        } else {
                   3895:                strcpy(tmp, path);
                   3896:        }
                   3897:        return(tmp);
                   3898: }
                   3899: 
1.1.1.45  root     3900: const char *msdos_remove_end_separator(const char *path)
1.1.1.32  root     3901: {
                   3902:        static char tmp[MAX_PATH];
                   3903:        
                   3904:        strcpy(tmp, path);
1.1.1.45  root     3905:        
                   3906:        // for example "C:\" case, the end separator should not be removed
                   3907:        if(strlen(tmp) > 3 && tmp[strlen(tmp) - 1] == '\\') {
                   3908:                tmp[strlen(tmp) - 1] = '\0';
1.1.1.32  root     3909:        }
                   3910:        return(tmp);
                   3911: }
                   3912: 
1.1.1.45  root     3913: const char *msdos_combine_path(const char *dir, const char *file)
1.1.1.14  root     3914: {
                   3915:        static char tmp[MAX_PATH];
1.1.1.45  root     3916:        const char *tmp_dir = msdos_remove_double_quote(dir);
1.1.1.14  root     3917:        
                   3918:        if(strlen(tmp_dir) == 0) {
                   3919:                strcpy(tmp, file);
                   3920:        } else if(tmp_dir[strlen(tmp_dir) - 1] == '\\') {
                   3921:                sprintf(tmp, "%s%s", tmp_dir, file);
                   3922:        } else {
                   3923:                sprintf(tmp, "%s\\%s", tmp_dir, file);
                   3924:        }
                   3925:        return(tmp);
                   3926: }
                   3927: 
1.1.1.45  root     3928: const char *msdos_trimmed_path(const char *path, int lfn)
1.1       root     3929: {
                   3930:        static char tmp[MAX_PATH];
                   3931:        
                   3932:        if(lfn) {
                   3933:                strcpy(tmp, path);
                   3934:        } else {
                   3935:                // remove space in the path
1.1.1.45  root     3936:                const char *src = path;
                   3937:                char *dst = tmp;
1.1       root     3938:                
                   3939:                while(*src != '\0') {
                   3940:                        if(msdos_lead_byte_check(*src)) {
                   3941:                                *dst++ = *src++;
                   3942:                                *dst++ = *src++;
                   3943:                        } else if(*src != ' ') {
                   3944:                                *dst++ = *src++;
                   3945:                        } else {
                   3946:                                src++;  // skip space
                   3947:                        }
                   3948:                }
                   3949:                *dst = '\0';
                   3950:        }
1.1.1.14  root     3951:        if(_stricmp(tmp, "C:\\COMMAND.COM") == 0) {
                   3952:                // redirect C:\COMMAND.COM to comspec_path
                   3953:                strcpy(tmp, comspec_path);
                   3954:        } else if(is_vista_or_later && _access(tmp, 0) != 0) {
                   3955:                // redirect new files (without wildcards) in C:\ to %TEMP%, since C:\ is not usually writable
                   3956:                static int root_drive_protected = -1;
                   3957:                char temp[MAX_PATH], name[MAX_PATH], *name_temp = NULL;
                   3958:                dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   3959:                
                   3960:                if(GetFullPathName(tmp, MAX_PATH, temp, &name_temp) != 0 &&
                   3961:                   name_temp != NULL && strstr(name_temp, "?") == NULL && strstr(name_temp, "*") == NULL) {
                   3962:                        strcpy(name, name_temp);
                   3963:                        name_temp[0] = '\0';
                   3964:                        
                   3965:                        if((temp[0] == 'A' + dos_info->boot_drive - 1 || temp[0] == 'a' + dos_info->boot_drive - 1) &&
                   3966:                           (temp[1] == ':') && (temp[2] == '\\' || temp[2] == '/') && (temp[3] == '\0')) {
                   3967:                                if(root_drive_protected == -1) {
                   3968:                                        FILE *fp = NULL;
                   3969:                                        
                   3970:                                        sprintf(temp, "%c:\\MS-DOS_Player.$$$", 'A' + dos_info->boot_drive - 1);
                   3971:                                        root_drive_protected = 1;
                   3972:                                        try {
                   3973:                                                if((fp = fopen(temp, "w")) != NULL) {
                   3974:                                                        if(fprintf(fp, "TEST") == 4) {
                   3975:                                                                root_drive_protected = 0;
                   3976:                                                        }
                   3977:                                                }
                   3978:                                        } catch(...) {
                   3979:                                        }
                   3980:                                        if(fp != NULL) {
                   3981:                                                fclose(fp);
                   3982:                                        }
                   3983:                                        if(_access(temp, 0) == 0) {
                   3984:                                                remove(temp);
                   3985:                                        }
                   3986:                                }
                   3987:                                if(root_drive_protected == 1) {
                   3988:                                        if(GetEnvironmentVariable("TEMP", temp, MAX_PATH) != 0 ||
                   3989:                                           GetEnvironmentVariable("TMP",  temp, MAX_PATH) != 0) {
                   3990:                                                strcpy(tmp, msdos_combine_path(temp, name));
                   3991:                                        }
                   3992:                                }
                   3993:                        }
                   3994:                }
                   3995:        }
1.1       root     3996:        return(tmp);
                   3997: }
                   3998: 
1.1.1.45  root     3999: const char *msdos_get_multiple_short_path(const char *src)
1.1.1.28  root     4000: {
1.1.1.32  root     4001:        // "LONGPATH\";"LONGPATH\";"LONGPATH\" to SHORTPATH;SHORTPATH;SHORTPATH
1.1.1.28  root     4002:        static char env_path[ENV_SIZE];
                   4003:        char tmp[ENV_SIZE], *token;
                   4004:        
                   4005:        memset(env_path, 0, sizeof(env_path));
                   4006:        strcpy(tmp, src);
                   4007:        token = my_strtok(tmp, ";");
                   4008:        
                   4009:        while(token != NULL) {
                   4010:                if(token[0] != '\0') {
1.1.1.45  root     4011:                        const char *path = msdos_remove_double_quote(token);
                   4012:                        char short_path[MAX_PATH];
1.1.1.32  root     4013:                        if(path != NULL && strlen(path) != 0) {
                   4014:                                if(env_path[0] != '\0') {
                   4015:                                        strcat(env_path, ";");
                   4016:                                }
1.1.1.28  root     4017:                                if(GetShortPathName(path, short_path, MAX_PATH) == 0) {
1.1.1.32  root     4018:                                        strcat(env_path, msdos_remove_end_separator(path));
1.1.1.28  root     4019:                                } else {
                   4020:                                        my_strupr(short_path);
1.1.1.32  root     4021:                                        strcat(env_path, msdos_remove_end_separator(short_path));
1.1.1.28  root     4022:                                }
                   4023:                        }
                   4024:                }
                   4025:                token = my_strtok(NULL, ";");
                   4026:        }
                   4027:        return(env_path);
                   4028: }
                   4029: 
1.1.1.45  root     4030: bool match(const char *text, const char *pattern)
1.1       root     4031: {
1.1.1.24  root     4032:        // http://www.prefield.com/algorithm/string/wildcard.html
1.1.1.14  root     4033:        switch(*pattern) {
1.1       root     4034:        case '\0':
                   4035:                return !*text;
                   4036:        case '*':
1.1.1.14  root     4037:                return match(text, pattern + 1) || (*text && match(text + 1, pattern));
1.1       root     4038:        case '?':
                   4039:                return *text && match(text + 1, pattern + 1);
                   4040:        default:
                   4041:                return (*text == *pattern) && match(text + 1, pattern + 1);
                   4042:        }
                   4043: }
                   4044: 
1.1.1.45  root     4045: bool msdos_match_volume_label(const char *path, const char *volume)
1.1       root     4046: {
1.1.1.45  root     4047:        const char *p = NULL;
1.1       root     4048:        
1.1.1.14  root     4049:        if(!*volume) {
                   4050:                return false;
                   4051:        } else if((p = my_strchr(path, ':')) != NULL) {
1.1       root     4052:                return msdos_match_volume_label(p + 1, volume);
                   4053:        } else if((p = my_strchr(path, '\\')) != NULL) {
                   4054:                return msdos_match_volume_label(p + 1, volume);
                   4055:        } else if((p = my_strchr(path, '.')) != NULL) {
1.1.1.14  root     4056:                char tmp[MAX_PATH];
                   4057:                sprintf(tmp, "%.*s%s", (int)(p - path), path, p + 1);
                   4058:                return match(volume, tmp);
1.1       root     4059:        } else {
                   4060:                return match(volume, path);
                   4061:        }
                   4062: }
                   4063: 
1.1.1.45  root     4064: const char *msdos_fcb_path(fcb_t *fcb)
1.1       root     4065: {
                   4066:        static char tmp[MAX_PATH];
                   4067:        char name[9], ext[4];
                   4068:        
                   4069:        memset(name, 0, sizeof(name));
                   4070:        memcpy(name, fcb->file_name, 8);
                   4071:        strcpy(name, msdos_trimmed_path(name, 0));
                   4072:        
                   4073:        memset(ext, 0, sizeof(ext));
                   4074:        memcpy(ext, fcb->file_name + 8, 3);
                   4075:        strcpy(ext, msdos_trimmed_path(ext, 0));
                   4076:        
                   4077:        if(name[0] == '\0' || strcmp(name, "????????") == 0) {
                   4078:                strcpy(name, "*");
                   4079:        }
                   4080:        if(ext[0] == '\0') {
                   4081:                strcpy(tmp, name);
                   4082:        } else {
                   4083:                if(strcmp(ext, "???") == 0) {
                   4084:                        strcpy(ext, "*");
                   4085:                }
                   4086:                sprintf(tmp, "%s.%s", name, ext);
                   4087:        }
                   4088:        return(tmp);
                   4089: }
                   4090: 
1.1.1.45  root     4091: void msdos_set_fcb_path(fcb_t *fcb, const char *path)
1.1       root     4092: {
                   4093:        char *ext = my_strchr(path, '.');
                   4094:        
                   4095:        memset(fcb->file_name, 0x20, 8 + 3);
                   4096:        if(ext != NULL && path[0] != '.') {
                   4097:                *ext = '\0';
                   4098:                memcpy(fcb->file_name + 8, ext + 1, strlen(ext + 1));
                   4099:        }
                   4100:        memcpy(fcb->file_name, path, strlen(path));
                   4101: }
                   4102: 
1.1.1.45  root     4103: const char *msdos_short_path(const char *path)
1.1       root     4104: {
                   4105:        static char tmp[MAX_PATH];
                   4106:        
1.1.1.24  root     4107:        if(GetShortPathName(path, tmp, MAX_PATH) == 0) {
                   4108:                strcpy(tmp, path);
                   4109:        }
1.1       root     4110:        my_strupr(tmp);
                   4111:        return(tmp);
                   4112: }
                   4113: 
1.1.1.45  root     4114: const char *msdos_short_name(WIN32_FIND_DATA *fd)
1.1.1.13  root     4115: {
                   4116:        static char tmp[MAX_PATH];
1.1.1.45  root     4117:        
1.1.1.14  root     4118:        if(fd->cAlternateFileName[0]) {
1.1.1.13  root     4119:                strcpy(tmp, fd->cAlternateFileName);
                   4120:        } else {
                   4121:                strcpy(tmp, fd->cFileName);
                   4122:        }
                   4123:        my_strupr(tmp);
                   4124:        return(tmp);
                   4125: }
                   4126: 
1.1.1.45  root     4127: const char *msdos_short_full_path(const char *path)
1.1       root     4128: {
                   4129:        static char tmp[MAX_PATH];
                   4130:        char full[MAX_PATH], *name;
                   4131:        
1.1.1.14  root     4132:        // Full works with non-existent files, but Short does not
1.1       root     4133:        GetFullPathName(path, MAX_PATH, full, &name);
1.1.1.14  root     4134:        *tmp = '\0';
                   4135:        if(GetShortPathName(full, tmp, MAX_PATH) == 0 && name > path) {
                   4136:                name[-1] = '\0';
                   4137:                DWORD len = GetShortPathName(full, tmp, MAX_PATH);
                   4138:                if(len == 0) {
                   4139:                        strcpy(tmp, full);
                   4140:                } else {
                   4141:                        tmp[len++] = '\\';
                   4142:                        strcpy(tmp + len, name);
                   4143:                }
                   4144:        }
1.1       root     4145:        my_strupr(tmp);
                   4146:        return(tmp);
                   4147: }
                   4148: 
1.1.1.45  root     4149: const char *msdos_short_full_dir(const char *path)
1.1       root     4150: {
                   4151:        static char tmp[MAX_PATH];
                   4152:        char full[MAX_PATH], *name;
                   4153:        
                   4154:        GetFullPathName(path, MAX_PATH, full, &name);
                   4155:        name[-1] = '\0';
1.1.1.24  root     4156:        if(GetShortPathName(full, tmp, MAX_PATH) == 0) {
                   4157:                strcpy(tmp, full);
                   4158:        }
1.1       root     4159:        my_strupr(tmp);
                   4160:        return(tmp);
                   4161: }
                   4162: 
1.1.1.45  root     4163: const char *msdos_local_file_path(const char *path, int lfn)
1.1       root     4164: {
1.1.1.45  root     4165:        static char trimmed[MAX_PATH];
                   4166:        
                   4167:        strcpy(trimmed, msdos_trimmed_path(path, lfn));
1.1.1.14  root     4168: #if 0
                   4169:        // I have forgotten the reason of this routine... :-(
1.1       root     4170:        if(_access(trimmed, 0) != 0) {
                   4171:                process_t *process = msdos_process_info_get(current_psp);
                   4172:                static char tmp[MAX_PATH];
                   4173:                
                   4174:                sprintf(tmp, "%s\\%s", process->module_dir, trimmed);
                   4175:                if(_access(tmp, 0) == 0) {
                   4176:                        return(tmp);
                   4177:                }
                   4178:        }
1.1.1.14  root     4179: #endif
1.1       root     4180:        return(trimmed);
                   4181: }
                   4182: 
1.1.1.45  root     4183: bool msdos_is_device_path(const char *path)
1.1.1.11  root     4184: {
                   4185:        char full[MAX_PATH], *name;
                   4186:        
1.1.1.24  root     4187:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4188:                if(_stricmp(full, "\\\\.\\AUX" ) == 0 ||
                   4189:                   _stricmp(full, "\\\\.\\CON" ) == 0 ||
                   4190:                   _stricmp(full, "\\\\.\\NUL" ) == 0 ||
                   4191:                   _stricmp(full, "\\\\.\\PRN" ) == 0 ||
                   4192:                   _stricmp(full, "\\\\.\\COM1") == 0 ||
                   4193:                   _stricmp(full, "\\\\.\\COM2") == 0 ||
                   4194:                   _stricmp(full, "\\\\.\\COM3") == 0 ||
                   4195:                   _stricmp(full, "\\\\.\\COM4") == 0 ||
                   4196:                   _stricmp(full, "\\\\.\\COM5") == 0 ||
                   4197:                   _stricmp(full, "\\\\.\\COM6") == 0 ||
                   4198:                   _stricmp(full, "\\\\.\\COM7") == 0 ||
                   4199:                   _stricmp(full, "\\\\.\\COM8") == 0 ||
                   4200:                   _stricmp(full, "\\\\.\\COM9") == 0 ||
                   4201:                   _stricmp(full, "\\\\.\\LPT1") == 0 ||
                   4202:                   _stricmp(full, "\\\\.\\LPT2") == 0 ||
                   4203:                   _stricmp(full, "\\\\.\\LPT3") == 0 ||
                   4204:                   _stricmp(full, "\\\\.\\LPT4") == 0 ||
                   4205:                   _stricmp(full, "\\\\.\\LPT5") == 0 ||
                   4206:                   _stricmp(full, "\\\\.\\LPT6") == 0 ||
                   4207:                   _stricmp(full, "\\\\.\\LPT7") == 0 ||
                   4208:                   _stricmp(full, "\\\\.\\LPT8") == 0 ||
                   4209:                   _stricmp(full, "\\\\.\\LPT9") == 0) {
                   4210:                        return(true);
                   4211:                } else if(name != NULL) {
                   4212:                        if(_stricmp(name, "CLOCK$"  ) == 0 ||
                   4213:                           _stricmp(name, "CONFIG$" ) == 0 ||
1.1.1.30  root     4214:                           _stricmp(name, "EMMXXXX0") == 0 ||
1.1.1.43  root     4215: //                        _stricmp(name, "SCSIMGR$") == 0 ||
1.1.1.30  root     4216:                           _stricmp(name, "$IBMAIAS") == 0) {
1.1.1.29  root     4217:                                return(true);
                   4218:                        }
                   4219:                }
1.1.1.24  root     4220:        }
                   4221:        return(false);
1.1.1.11  root     4222: }
                   4223: 
1.1.1.45  root     4224: bool msdos_is_con_path(const char *path)
1.1.1.8   root     4225: {
1.1.1.14  root     4226:        char full[MAX_PATH], *name;
1.1.1.8   root     4227:        
1.1.1.24  root     4228:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4229:                return(_stricmp(full, "\\\\.\\CON") == 0);
1.1.1.24  root     4230:        }
                   4231:        return(false);
                   4232: }
                   4233: 
1.1.1.45  root     4234: int msdos_is_comm_path(const char *path)
1.1.1.24  root     4235: {
                   4236:        char full[MAX_PATH], *name;
                   4237:        
                   4238:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4239:                if(_stricmp(full, "\\\\.\\COM1") == 0) {
                   4240:                        return(1);
                   4241:                } else if(_stricmp(full, "\\\\.\\COM2") == 0) {
                   4242:                        return(2);
                   4243:                } else if(_stricmp(full, "\\\\.\\COM3") == 0) {
                   4244:                        return(3);
                   4245:                } else if(_stricmp(full, "\\\\.\\COM4") == 0) {
                   4246:                        return(4);
1.1.1.24  root     4247:                }
                   4248:        }
1.1.1.29  root     4249:        return(0);
                   4250: }
                   4251: 
1.1.1.45  root     4252: void msdos_set_comm_params(int sio_port, const char *path)
1.1.1.37  root     4253: {
                   4254:        // 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     4255:        const char *p = NULL;
1.1.1.37  root     4256:        
                   4257:        if((p = strstr(path, ":")) != NULL) {
                   4258:                UINT8 selector = sio_read(sio_port - 1, 3);
                   4259:                
                   4260:                // baud rate
                   4261:                int baud = max(110, min(9600, atoi(p + 1)));
                   4262:                UINT16 divisor = 115200 / baud;
                   4263:                
                   4264:                if((p = strstr(p + 1, ",")) != NULL) {
                   4265:                        // parity
                   4266:                        if(p[1] == 'N' || p[1] == 'n') {
                   4267:                                selector = (selector & ~0x38) | 0x00;
                   4268:                        } else if(p[1] == 'O' || p[1] == 'o') {
                   4269:                                selector = (selector & ~0x38) | 0x08;
                   4270:                        } else if(p[1] == 'E' || p[1] == 'e') {
                   4271:                                selector = (selector & ~0x38) | 0x18;
                   4272:                        } else if(p[1] == 'M' || p[1] == 'm') {
                   4273:                                selector = (selector & ~0x38) | 0x28;
                   4274:                        } else if(p[1] == 'S' || p[1] == 's') {
                   4275:                                selector = (selector & ~0x38) | 0x38;
                   4276:                        }
                   4277:                        if((p = strstr(p + 1, ",")) != NULL) {
                   4278:                                // word length
                   4279:                                if(p[1] == '8') {
                   4280:                                        selector = (selector & ~0x03) | 0x03;
                   4281:                                } else if(p[1] == '7') {
                   4282:                                        selector = (selector & ~0x03) | 0x02;
                   4283:                                } else if(p[1] == '6') {
                   4284:                                        selector = (selector & ~0x03) | 0x01;
                   4285:                                } else if(p[1] == '5') {
                   4286:                                        selector = (selector & ~0x03) | 0x00;
                   4287:                                }
                   4288:                                if((p = strstr(p + 1, ",")) != NULL) {
                   4289:                                        // stop bits
                   4290:                                        float bits = atof(p + 1);
                   4291:                                        if(bits > 1.0F) {
                   4292:                                                selector |= 0x04;
                   4293:                                        } else {
                   4294:                                                selector &= ~0x04;
                   4295:                                        }
                   4296:                                }
                   4297:                        }
                   4298:                }
                   4299:                sio_write(sio_port - 1, 3, selector | 0x80);
                   4300:                sio_write(sio_port - 1, 0, divisor & 0xff);
                   4301:                sio_write(sio_port - 1, 1, divisor >> 8);
                   4302:                sio_write(sio_port - 1, 3, selector);
                   4303:        }
                   4304: }
                   4305: 
1.1.1.45  root     4306: int msdos_is_prn_path(const char *path)
1.1.1.30  root     4307: {
                   4308:        char full[MAX_PATH], *name;
                   4309:        
                   4310:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
                   4311:                if(_stricmp(full, "\\\\.\\PRN") == 0) {
                   4312:                        return(1);
                   4313:                } else if(_stricmp(full, "\\\\.\\LPT1") == 0) {
                   4314:                        return(1);
                   4315:                } else if(_stricmp(full, "\\\\.\\LPT2") == 0) {
                   4316:                        return(2);
                   4317:                } else if(_stricmp(full, "\\\\.\\LPT3") == 0) {
                   4318:                        return(3);
                   4319:                }
                   4320:        }
                   4321:        return(0);
                   4322: }
                   4323: 
1.1.1.44  root     4324: bool msdos_is_valid_drive(int drv)
                   4325: {
                   4326:        return(drv >= 0 && drv < 26 && (GetLogicalDrives() & (1 << drv)) != 0);
                   4327: }
                   4328: 
                   4329: bool msdos_is_removable_drive(int drv)
                   4330: {
                   4331:        char volume[] = "A:\\";
                   4332:        
                   4333:        volume[0] = 'A' + drv;
                   4334:        
                   4335:        return(GetDriveType(volume) == DRIVE_REMOVABLE);
                   4336: }
                   4337: 
                   4338: bool msdos_is_cdrom_drive(int drv)
                   4339: {
                   4340:        char volume[] = "A:\\";
                   4341:        
                   4342:        volume[0] = 'A' + drv;
                   4343:        
                   4344:        return(GetDriveType(volume) == DRIVE_CDROM);
                   4345: }
                   4346: 
                   4347: bool msdos_is_remote_drive(int drv)
                   4348: {
                   4349:        char volume[] = "A:\\";
                   4350:        
                   4351:        volume[0] = 'A' + drv;
                   4352:        
                   4353:        return(GetDriveType(volume) == DRIVE_REMOTE);
                   4354: }
                   4355: 
                   4356: bool msdos_is_subst_drive(int drv)
                   4357: {
                   4358:        char device[] = "A:", path[MAX_PATH];
                   4359:        
                   4360:        device[0] = 'A' + drv;
                   4361:        
                   4362:        if(QueryDosDevice(device, path, MAX_PATH)) {
                   4363:                if(strncmp(path, "\\??\\", 4) == 0) {
                   4364:                        return(true);
                   4365:                }
                   4366:        }
                   4367:        return(false);
                   4368: }
                   4369: 
1.1.1.45  root     4370: bool msdos_is_existing_file(const char *path)
1.1.1.24  root     4371: {
                   4372:        // http://d.hatena.ne.jp/yu-hr/20100317/1268826458
                   4373:        WIN32_FIND_DATA FindData;
                   4374:        HANDLE hFind;
                   4375:        
                   4376:        if((hFind = FindFirstFile(path, &FindData)) != INVALID_HANDLE_VALUE) {
                   4377:                FindClose(hFind);
                   4378:                return(!(FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY));
                   4379:        }
                   4380:        return(false);
1.1.1.8   root     4381: }
                   4382: 
1.1.1.45  root     4383: const char *msdos_search_command_com(const char *command_path, const char *env_path)
1.1.1.9   root     4384: {
                   4385:        static char tmp[MAX_PATH];
1.1.1.28  root     4386:        char path[ENV_SIZE], *file_name;
1.1.1.9   root     4387:        
1.1.1.28  root     4388:        // check if COMMAND.COM is in the same directory as the target program file
1.1.1.9   root     4389:        if(GetFullPathName(command_path, MAX_PATH, tmp, &file_name) != 0) {
                   4390:                sprintf(file_name, "COMMAND.COM");
                   4391:                if(_access(tmp, 0) == 0) {
                   4392:                        return(tmp);
                   4393:                }
                   4394:        }
1.1.1.28  root     4395:        
                   4396:        // check if COMMAND.COM is in the same directory as the running msdos.exe
1.1.1.9   root     4397:        if(GetModuleFileName(NULL, path, MAX_PATH) != 0 && GetFullPathName(path, MAX_PATH, tmp, &file_name) != 0) {
                   4398:                sprintf(file_name, "COMMAND.COM");
                   4399:                if(_access(tmp, 0) == 0) {
                   4400:                        return(tmp);
                   4401:                }
                   4402:        }
1.1.1.28  root     4403:        
                   4404:        // check if COMMAND.COM is in the current directory
1.1.1.9   root     4405:        if(GetFullPathName("COMMAND.COM", MAX_PATH, tmp, &file_name) != 0) {
                   4406:                if(_access(tmp, 0) == 0) {
                   4407:                        return(tmp);
                   4408:                }
                   4409:        }
1.1.1.28  root     4410:        
                   4411:        // cehck if COMMAND.COM is in the directory that is in MSDOS_PATH and PATH environment variables
                   4412:        strcpy(path, env_path);
                   4413:        char *token = my_strtok(path, ";");
1.1.1.9   root     4414:        while(token != NULL) {
1.1.1.14  root     4415:                if(strlen(token) != 0 && token[0] != '%') {
1.1.1.9   root     4416:                        strcpy(tmp, msdos_combine_path(token, "COMMAND.COM"));
                   4417:                        if(_access(tmp, 0) == 0) {
                   4418:                                return(tmp);
                   4419:                        }
                   4420:                }
                   4421:                token = my_strtok(NULL, ";");
                   4422:        }
                   4423:        return(NULL);
                   4424: }
                   4425: 
1.1.1.14  root     4426: int msdos_drive_number(const char *path)
1.1       root     4427: {
                   4428:        char tmp[MAX_PATH], *name;
                   4429:        
1.1.1.45  root     4430:        if(GetFullPathName(path, MAX_PATH, tmp, &name) >= 2 && tmp[1] == ':') {
                   4431:                if(tmp[0] >= 'a' && tmp[0] <= 'z') {
                   4432:                        return(tmp[0] - 'a');
                   4433:                } else if(tmp[0] >= 'A' && tmp[0] <= 'Z') {
                   4434:                        return(tmp[0] - 'A');
                   4435:                }
1.1       root     4436:        }
1.1.1.45  root     4437: //     return(msdos_drive_number("."));
                   4438:        return(_getdrive() - 1);
1.1       root     4439: }
                   4440: 
1.1.1.45  root     4441: const char *msdos_volume_label(const char *path)
1.1       root     4442: {
                   4443:        static char tmp[MAX_PATH];
                   4444:        char volume[] = "A:\\";
                   4445:        
                   4446:        if(path[1] == ':') {
                   4447:                volume[0] = path[0];
                   4448:        } else {
                   4449:                volume[0] = 'A' + _getdrive() - 1;
                   4450:        }
                   4451:        if(!GetVolumeInformation(volume, tmp, MAX_PATH, NULL, NULL, NULL, NULL, 0)) {
                   4452:                memset(tmp, 0, sizeof(tmp));
                   4453:        }
                   4454:        return(tmp);
                   4455: }
                   4456: 
1.1.1.45  root     4457: const char *msdos_short_volume_label(const char *label)
1.1       root     4458: {
                   4459:        static char tmp[(8 + 1 + 3) + 1];
1.1.1.45  root     4460:        const char *src = label;
1.1       root     4461:        int remain = strlen(label);
                   4462:        char *dst_n = tmp;
                   4463:        char *dst_e = tmp + 9;
                   4464:        
                   4465:        strcpy(tmp, "        .   ");
                   4466:        for(int i = 0; i < 8 && remain > 0; i++) {
                   4467:                if(msdos_lead_byte_check(*src)) {
                   4468:                        if(++i == 8) {
                   4469:                                break;
                   4470:                        }
                   4471:                        *dst_n++ = *src++;
                   4472:                        remain--;
                   4473:                }
                   4474:                *dst_n++ = *src++;
                   4475:                remain--;
                   4476:        }
                   4477:        if(remain > 0) {
                   4478:                for(int i = 0; i < 3 && remain > 0; i++) {
                   4479:                        if(msdos_lead_byte_check(*src)) {
                   4480:                                if(++i == 3) {
                   4481:                                        break;
                   4482:                                }
                   4483:                                *dst_e++ = *src++;
                   4484:                                remain--;
                   4485:                        }
                   4486:                        *dst_e++ = *src++;
                   4487:                        remain--;
                   4488:                }
                   4489:                *dst_e = '\0';
                   4490:        } else {
                   4491:                *dst_n = '\0';
                   4492:        }
                   4493:        my_strupr(tmp);
                   4494:        return(tmp);
                   4495: }
                   4496: 
1.1.1.13  root     4497: errno_t msdos_maperr(unsigned long oserrno)
                   4498: {
                   4499:        _doserrno = oserrno;
1.1.1.14  root     4500:        switch(oserrno) {
1.1.1.13  root     4501:        case ERROR_FILE_NOT_FOUND:         // 2
                   4502:        case ERROR_PATH_NOT_FOUND:         // 3
                   4503:        case ERROR_INVALID_DRIVE:          // 15
                   4504:        case ERROR_NO_MORE_FILES:          // 18
                   4505:        case ERROR_BAD_NETPATH:            // 53
                   4506:        case ERROR_BAD_NET_NAME:           // 67
                   4507:        case ERROR_BAD_PATHNAME:           // 161
                   4508:        case ERROR_FILENAME_EXCED_RANGE:   // 206
                   4509:                return ENOENT;
                   4510:        case ERROR_TOO_MANY_OPEN_FILES:    // 4
                   4511:                return EMFILE;
                   4512:        case ERROR_ACCESS_DENIED:          // 5
                   4513:        case ERROR_CURRENT_DIRECTORY:      // 16
                   4514:        case ERROR_NETWORK_ACCESS_DENIED:  // 65
                   4515:        case ERROR_CANNOT_MAKE:            // 82
                   4516:        case ERROR_FAIL_I24:               // 83
                   4517:        case ERROR_DRIVE_LOCKED:           // 108
                   4518:        case ERROR_SEEK_ON_DEVICE:         // 132
                   4519:        case ERROR_NOT_LOCKED:             // 158
                   4520:        case ERROR_LOCK_FAILED:            // 167
                   4521:                return EACCES;
                   4522:        case ERROR_INVALID_HANDLE:         // 6
                   4523:        case ERROR_INVALID_TARGET_HANDLE:  // 114
                   4524:        case ERROR_DIRECT_ACCESS_HANDLE:   // 130
                   4525:                return EBADF;
                   4526:        case ERROR_ARENA_TRASHED:          // 7
                   4527:        case ERROR_NOT_ENOUGH_MEMORY:      // 8
                   4528:        case ERROR_INVALID_BLOCK:          // 9
                   4529:        case ERROR_NOT_ENOUGH_QUOTA:       // 1816
                   4530:                return ENOMEM;
                   4531:        case ERROR_BAD_ENVIRONMENT:        // 10
                   4532:                return E2BIG;
                   4533:        case ERROR_BAD_FORMAT:             // 11
                   4534:                return ENOEXEC;
                   4535:        case ERROR_NOT_SAME_DEVICE:        // 17
                   4536:                return EXDEV;
                   4537:        case ERROR_FILE_EXISTS:            // 80
                   4538:        case ERROR_ALREADY_EXISTS:         // 183
                   4539:                return EEXIST;
                   4540:        case ERROR_NO_PROC_SLOTS:          // 89
                   4541:        case ERROR_MAX_THRDS_REACHED:      // 164
                   4542:        case ERROR_NESTING_NOT_ALLOWED:    // 215
                   4543:                return EAGAIN;
                   4544:        case ERROR_BROKEN_PIPE:            // 109
                   4545:                return EPIPE;
                   4546:        case ERROR_DISK_FULL:              // 112
                   4547:                return ENOSPC;
                   4548:        case ERROR_WAIT_NO_CHILDREN:       // 128
                   4549:        case ERROR_CHILD_NOT_COMPLETE:     // 129
                   4550:                return ECHILD;
                   4551:        case ERROR_DIR_NOT_EMPTY:          // 145
                   4552:                return ENOTEMPTY;
                   4553:        }
1.1.1.14  root     4554:        if(oserrno >= ERROR_WRITE_PROTECT /* 19 */ &&
1.1.1.13  root     4555:                oserrno <= ERROR_SHARING_BUFFER_EXCEEDED /* 36 */) {
                   4556:                return EACCES;
                   4557:        }
1.1.1.14  root     4558:        if(oserrno >= ERROR_INVALID_STARTING_CODESEG /* 188 */ &&
1.1.1.13  root     4559:                oserrno <= ERROR_INFLOOP_IN_RELOC_CHAIN /* 202 */) {
                   4560:                return ENOEXEC;
                   4561:        }
                   4562:        return EINVAL;
                   4563: }
                   4564: 
1.1.1.45  root     4565: int msdos_open(const char *path, int oflag)
1.1.1.13  root     4566: {
1.1.1.14  root     4567:        if((oflag & (_O_RDONLY | _O_WRONLY | _O_RDWR)) != _O_RDONLY) {
1.1.1.45  root     4568:                return(_open(path, oflag));
1.1.1.13  root     4569:        }
1.1.1.14  root     4570:        
                   4571:        SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, !(oflag & _O_NOINHERIT) };
1.1.1.13  root     4572:        DWORD disposition;
1.1.1.14  root     4573:        switch(oflag & (_O_CREAT | _O_EXCL | _O_TRUNC)) {
                   4574:        default:
1.1.1.13  root     4575:        case _O_EXCL:
                   4576:                disposition = OPEN_EXISTING;
                   4577:                break;
                   4578:        case _O_CREAT:
                   4579:                disposition = OPEN_ALWAYS;
                   4580:                break;
                   4581:        case _O_CREAT | _O_EXCL:
                   4582:        case _O_CREAT | _O_TRUNC | _O_EXCL:
                   4583:                disposition = CREATE_NEW;
                   4584:                break;
                   4585:        case _O_TRUNC:
                   4586:        case _O_TRUNC | _O_EXCL:
                   4587:                disposition = TRUNCATE_EXISTING;
                   4588:                break;
                   4589:        case _O_CREAT | _O_TRUNC:
                   4590:                disposition = CREATE_ALWAYS;
                   4591:                break;
                   4592:        }
1.1.1.14  root     4593:        
1.1.1.45  root     4594:        HANDLE h = CreateFile(path, GENERIC_READ | FILE_WRITE_ATTRIBUTES,
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:                // FILE_WRITE_ATTRIBUTES may not be granted for standard users.
                   4599:                // Retry without FILE_WRITE_ATTRIBUTES.
1.1.1.45  root     4600:                h = CreateFile(path, GENERIC_READ,
1.1.1.13  root     4601:                        FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
                   4602:                        FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14  root     4603:                if(h == INVALID_HANDLE_VALUE) {
1.1.1.13  root     4604:                        errno = msdos_maperr(GetLastError());
1.1.1.45  root     4605:                        return(-1);
1.1.1.13  root     4606:                }
                   4607:        }
1.1.1.14  root     4608:        
1.1.1.13  root     4609:        int fd = _open_osfhandle((intptr_t) h, oflag);
1.1.1.14  root     4610:        if(fd == -1) {
1.1.1.13  root     4611:                CloseHandle(h);
                   4612:        }
1.1.1.45  root     4613:        return(fd);
                   4614: }
                   4615: 
                   4616: int msdos_open_device(const char *path, int oflag, int *sio_port, int *lpt_port)
                   4617: {
                   4618:        int fd = -1;
                   4619:        
                   4620:        *sio_port = *lpt_port = 0;
                   4621:        
                   4622:        if(msdos_is_con_path(path)) {
                   4623:                // MODE.COM opens CON device with read/write mode :-(
                   4624:                if((oflag & (_O_RDONLY | _O_WRONLY | _O_RDWR)) == _O_RDWR) {
                   4625:                        oflag &= ~(_O_RDONLY | _O_WRONLY | _O_RDWR);
                   4626:                        oflag |= _O_RDONLY;
                   4627:                }
                   4628:                if((fd = msdos_open("CON", oflag)) == -1) {
                   4629: //                     fd = msdos_open("NUL", oflag);
                   4630:                }
                   4631:        } else if((*sio_port = msdos_is_comm_path(path)) != 0) {
                   4632:                fd = msdos_open("NUL", oflag);
                   4633:                msdos_set_comm_params(*sio_port, path);
                   4634:        } else if((*lpt_port = msdos_is_prn_path(path)) != 0) {
                   4635:                fd = msdos_open("NUL", oflag);
                   4636:        } else if(msdos_is_device_path(path)) {
                   4637:                fd = msdos_open("NUL", oflag);
                   4638: //     } else if(oflag & _O_CREAT) {
                   4639: //             fd = _open(path, oflag, _S_IREAD | _S_IWRITE);
                   4640: //     } else {
                   4641: //             fd = _open(path, oflag);
                   4642:        }
                   4643:        return(fd);
                   4644: }
                   4645: 
                   4646: UINT16 msdos_device_info(const char *path)
                   4647: {
                   4648:        if(msdos_is_con_path(path)) {
                   4649:                return(0x80d3);
                   4650:        } else if(msdos_is_comm_path(path)) {
                   4651:                return(0x80a0);
                   4652:        } else if(msdos_is_prn_path(path)) {
                   4653: //             return(0xa8c0);
                   4654:                return(0x80a0);
                   4655:        } else if(msdos_is_device_path(path)) {
                   4656:                if(strstr(path, "EMMXXXX0") != NULL) {
                   4657:                        return(0xc0c0);
                   4658:                } else if(strstr(path, "MSCD001") != NULL) {
                   4659:                        return(0xc880);
                   4660:                } else {
                   4661:                        return(0x8084);
                   4662:                }
                   4663:        } else {
                   4664:                return(msdos_drive_number(path));
                   4665:        }
1.1.1.13  root     4666: }
                   4667: 
1.1.1.52! root     4668: void msdos_file_handler_open(int fd, const char *path, int atty, int mode, UINT16 info, UINT16 psp_seg, int sio_port = 0, int lpt_port = 0)
1.1       root     4669: {
                   4670:        static int id = 0;
                   4671:        char full[MAX_PATH], *name;
                   4672:        
                   4673:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
                   4674:                strcpy(file_handler[fd].path, full);
                   4675:        } else {
                   4676:                strcpy(file_handler[fd].path, path);
                   4677:        }
1.1.1.14  root     4678:        // isatty makes no distinction between CON & NUL
                   4679:        // GetFileSize fails on CON, succeeds on NUL
                   4680:        if(atty && (info != 0x80d3 || GetFileSize((HANDLE)_get_osfhandle(fd), NULL) == 0)) {
1.1.1.45  root     4681:                if(info == 0x80d3) {
                   4682:                        info = 0x8084;
                   4683:                }
1.1.1.14  root     4684:                atty = 0;
                   4685:        } else if(!atty && info == 0x80d3) {
1.1.1.45  root     4686: //             info = msdos_drive_number(".");
                   4687:                info = msdos_drive_number(path);
1.1.1.14  root     4688:        }
1.1       root     4689:        file_handler[fd].valid = 1;
                   4690:        file_handler[fd].id = id++;     // dummy id for int 21h ax=71a6h
1.1.1.37  root     4691:        file_handler[fd].atty = (sio_port == 0 && lpt_port == 0) ? atty : 0;
1.1       root     4692:        file_handler[fd].mode = mode;
                   4693:        file_handler[fd].info = info;
                   4694:        file_handler[fd].psp = psp_seg;
1.1.1.37  root     4695:        file_handler[fd].sio_port = sio_port;
                   4696:        file_handler[fd].lpt_port = lpt_port;
1.1.1.21  root     4697:        
                   4698:        // init system file table
                   4699:        if(fd < 20) {
                   4700:                UINT8 *sft = mem + SFT_TOP + 6 + 0x3b * fd;
                   4701:                
                   4702:                memset(sft, 0, 0x3b);
                   4703:                
                   4704:                *(UINT16 *)(sft + 0x00) = 1;
                   4705:                *(UINT16 *)(sft + 0x02) = file_handler[fd].mode;
                   4706:                *(UINT8  *)(sft + 0x04) = GetFileAttributes(file_handler[fd].path) & 0xff;
                   4707:                *(UINT16 *)(sft + 0x05) = file_handler[fd].info & 0xff;
                   4708:                
                   4709:                if(!(file_handler[fd].info & 0x80)) {
                   4710:                        *(UINT16 *)(sft + 0x07) = sizeof(dpb_t) * (file_handler[fd].info & 0x1f);
                   4711:                        *(UINT16 *)(sft + 0x09) = DPB_TOP >> 4;
                   4712:                        
                   4713:                        FILETIME time, local;
                   4714:                        HANDLE hHandle;
                   4715:                        WORD dos_date = 0, dos_time = 0;
                   4716:                        DWORD file_size = 0;
                   4717:                        if((hHandle = (HANDLE)_get_osfhandle(fd)) != INVALID_HANDLE_VALUE) {
                   4718:                                if(GetFileTime(hHandle, NULL, NULL, &time)) {
                   4719:                                        FileTimeToLocalFileTime(&time, &local);
                   4720:                                        FileTimeToDosDateTime(&local, &dos_date, &dos_time);
                   4721:                                }
                   4722:                                file_size = GetFileSize(hHandle, NULL);
                   4723:                        }
                   4724:                        *(UINT16 *)(sft + 0x0d) = dos_time;
                   4725:                        *(UINT16 *)(sft + 0x0f) = dos_date;
                   4726:                        *(UINT32 *)(sft + 0x11) = file_size;
                   4727:                }
                   4728:                
                   4729:                char fname[MAX_PATH] = {0}, ext[MAX_PATH] = {0};
                   4730:                _splitpath(file_handler[fd].path, NULL, NULL, fname, ext);
                   4731:                my_strupr(fname);
                   4732:                my_strupr(ext);
                   4733:                memset(sft + 0x20, 0x20, 11);
                   4734:                memcpy(sft + 0x20, fname, min(strlen(fname), 8));
                   4735:                memcpy(sft + 0x28, ext + 1, min(strlen(ext + 1), 3));
                   4736:                
                   4737:                *(UINT16 *)(sft + 0x31) = psp_seg;
                   4738:        }
1.1       root     4739: }
                   4740: 
                   4741: void msdos_file_handler_dup(int dst, int src, UINT16 psp_seg)
                   4742: {
                   4743:        strcpy(file_handler[dst].path, file_handler[src].path);
                   4744:        file_handler[dst].valid = 1;
                   4745:        file_handler[dst].id = file_handler[src].id;
                   4746:        file_handler[dst].atty = file_handler[src].atty;
                   4747:        file_handler[dst].mode = file_handler[src].mode;
                   4748:        file_handler[dst].info = file_handler[src].info;
                   4749:        file_handler[dst].psp = psp_seg;
1.1.1.37  root     4750:        file_handler[dst].sio_port = file_handler[src].sio_port;
                   4751:        file_handler[dst].lpt_port = file_handler[src].lpt_port;
1.1       root     4752: }
                   4753: 
1.1.1.20  root     4754: void msdos_file_handler_close(int fd)
1.1       root     4755: {
                   4756:        file_handler[fd].valid = 0;
1.1.1.21  root     4757:        
                   4758:        if(fd < 20) {
                   4759:                memset(mem + SFT_TOP + 6 + 0x3b * fd, 0, 0x3b);
                   4760:        }
1.1       root     4761: }
                   4762: 
1.1.1.14  root     4763: inline int msdos_file_attribute_create(UINT16 new_attr)
1.1       root     4764: {
1.1.1.14  root     4765:        return(new_attr & (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN |
                   4766:                           FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE |
                   4767:                           FILE_ATTRIBUTE_DIRECTORY));
1.1       root     4768: }
                   4769: 
                   4770: // find file
                   4771: 
                   4772: int msdos_find_file_check_attribute(int attribute, int allowed_mask, int required_mask)
                   4773: {
                   4774:        if((allowed_mask & 0x08) && !(attribute & FILE_ATTRIBUTE_DIRECTORY)) {
                   4775:                return(0);      // search directory only !!!
                   4776:        } else if(!(allowed_mask & 0x02) && (attribute & FILE_ATTRIBUTE_HIDDEN)) {
                   4777:                return(0);
                   4778:        } else if(!(allowed_mask & 0x04) && (attribute & FILE_ATTRIBUTE_SYSTEM)) {
                   4779:                return(0);
                   4780:        } else if(!(allowed_mask & 0x10) && (attribute & FILE_ATTRIBUTE_DIRECTORY)) {
                   4781:                return(0);
                   4782:        } else if((attribute & required_mask) != required_mask) {
                   4783:                return(0);
                   4784:        } else {
                   4785:                return(1);
                   4786:        }
                   4787: }
                   4788: 
1.1.1.13  root     4789: int msdos_find_file_has_8dot3name(WIN32_FIND_DATA *fd)
                   4790: {
1.1.1.14  root     4791:        if(fd->cAlternateFileName[0]) {
1.1.1.42  root     4792:                return(1);
1.1.1.13  root     4793:        }
                   4794:        size_t len = strlen(fd->cFileName);
1.1.1.14  root     4795:        if(len > 12) {
1.1.1.42  root     4796:                return(0);
1.1.1.13  root     4797:        }
                   4798:        const char *ext = strrchr(fd->cFileName, '.');
1.1.1.14  root     4799:        if((ext ? ext - fd->cFileName : len) > 8) {
1.1.1.42  root     4800:                return(0);
1.1.1.13  root     4801:        }
1.1.1.42  root     4802:        return(1);
1.1.1.13  root     4803: }
                   4804: 
1.1       root     4805: void msdos_find_file_conv_local_time(WIN32_FIND_DATA *fd)
                   4806: {
                   4807:        FILETIME local;
                   4808:        
                   4809:        FileTimeToLocalFileTime(&fd->ftCreationTime, &local);
                   4810:        fd->ftCreationTime.dwLowDateTime = local.dwLowDateTime;
                   4811:        fd->ftCreationTime.dwHighDateTime = local.dwHighDateTime;
                   4812:        
                   4813:        FileTimeToLocalFileTime(&fd->ftLastAccessTime, &local);
                   4814:        fd->ftLastAccessTime.dwLowDateTime = local.dwLowDateTime;
                   4815:        fd->ftLastAccessTime.dwHighDateTime = local.dwHighDateTime;
                   4816:        
                   4817:        FileTimeToLocalFileTime(&fd->ftLastWriteTime, &local);
                   4818:        fd->ftLastWriteTime.dwLowDateTime = local.dwLowDateTime;
                   4819:        fd->ftLastWriteTime.dwHighDateTime = local.dwHighDateTime;
                   4820: }
                   4821: 
                   4822: // i/o
                   4823: 
                   4824: void msdos_stdio_reopen()
                   4825: {
                   4826:        if(!file_handler[0].valid) {
                   4827:                _dup2(DUP_STDIN, 0);
                   4828:                msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   4829:        }
                   4830:        if(!file_handler[1].valid) {
                   4831:                _dup2(DUP_STDOUT, 1);
                   4832:                msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   4833:        }
                   4834:        if(!file_handler[2].valid) {
                   4835:                _dup2(DUP_STDERR, 2);
                   4836:                msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
                   4837:        }
1.1.1.21  root     4838:        if(!file_handler[3].valid) {
                   4839:                _dup2(DUP_STDAUX, 3);
                   4840:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
                   4841:        }
                   4842:        if(!file_handler[4].valid) {
                   4843:                _dup2(DUP_STDPRN, 4);
1.1.1.45  root     4844: //             msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0, 0, 1); // LPT1
                   4845:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0x80a0, 0, 0, 1); // LPT1
1.1.1.21  root     4846:        }
                   4847:        for(int i = 0; i < 5; i++) {
                   4848:                if(msdos_psp_get_file_table(i, current_psp) == 0xff) {
                   4849:                        msdos_psp_set_file_table(i, i, current_psp);
                   4850:                }
                   4851:        }
1.1       root     4852: }
                   4853: 
1.1.1.37  root     4854: int msdos_read(int fd, void *buffer, unsigned int count)
                   4855: {
                   4856:        if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].sio_port >= 1 && file_handler[fd].sio_port <= 4) {
                   4857:                // read from serial port
                   4858:                int read = 0;
                   4859:                if(sio_port_number[file_handler[fd].sio_port - 1] != 0) {
                   4860:                        UINT8 *buf = (UINT8 *)buffer;
                   4861:                        UINT8 selector = sio_read(file_handler[fd].sio_port - 1, 3);
                   4862:                        sio_write(file_handler[fd].sio_port - 1, 3, selector & ~0x80);
1.1.1.38  root     4863:                        DWORD timeout = timeGetTime() + 1000;
                   4864:                        while(read < count) {
                   4865:                                if(sio_read(file_handler[fd].sio_port - 1, 5) & 0x01) {
                   4866:                                        buf[read++] = sio_read(file_handler[fd].sio_port - 1, 0);
                   4867:                                        timeout = timeGetTime() + 1000;
                   4868:                                } else {
                   4869:                                        if(timeGetTime() > timeout) {
                   4870:                                                break;
                   4871:                                        }
                   4872:                                        Sleep(10);
1.1.1.37  root     4873:                                }
                   4874:                        }
                   4875:                        sio_write(file_handler[fd].sio_port - 1, 3, selector);
                   4876:                }
                   4877:                return(read);
                   4878:        }
                   4879:        return(_read(fd, buffer, count));
                   4880: }
                   4881: 
1.1       root     4882: int msdos_kbhit()
                   4883: {
                   4884:        msdos_stdio_reopen();
                   4885:        
1.1.1.20  root     4886:        process_t *process = msdos_process_info_get(current_psp);
                   4887:        int fd = msdos_psp_get_file_table(0, current_psp);
                   4888:        
                   4889:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     4890:                // stdin is redirected to file
1.1.1.20  root     4891:                return(eof(fd) == 0);
1.1       root     4892:        }
                   4893:        
                   4894:        // check keyboard status
1.1.1.35  root     4895:        if(key_recv != 0) {
1.1       root     4896:                return(1);
                   4897:        }
1.1.1.35  root     4898:        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   4899: #ifdef USE_SERVICE_THREAD
                   4900:                EnterCriticalSection(&key_buf_crit_sect);
                   4901: #endif
                   4902:                bool empty = key_buf_char->empty();
                   4903: #ifdef USE_SERVICE_THREAD
                   4904:                LeaveCriticalSection(&key_buf_crit_sect);
                   4905: #endif
                   4906:                if(!empty) return(1);
                   4907:        }
                   4908:        return(_kbhit());
1.1       root     4909: }
                   4910: 
                   4911: int msdos_getch_ex(int echo)
                   4912: {
                   4913:        static char prev = 0;
                   4914:        
                   4915:        msdos_stdio_reopen();
                   4916:        
1.1.1.20  root     4917:        process_t *process = msdos_process_info_get(current_psp);
                   4918:        int fd = msdos_psp_get_file_table(0, current_psp);
                   4919:        
                   4920:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     4921:                // stdin is redirected to file
                   4922: retry:
                   4923:                char data;
1.1.1.37  root     4924:                if(msdos_read(fd, &data, 1) == 1) {
1.1       root     4925:                        char tmp = data;
                   4926:                        if(data == 0x0a) {
                   4927:                                if(prev == 0x0d) {
                   4928:                                        goto retry; // CRLF -> skip LF
                   4929:                                } else {
                   4930:                                        data = 0x0d; // LF only -> CR
                   4931:                                }
                   4932:                        }
                   4933:                        prev = tmp;
                   4934:                        return(data);
                   4935:                }
                   4936:                return(EOF);
                   4937:        }
                   4938:        
                   4939:        // input from console
1.1.1.5   root     4940:        int key_char, key_scan;
1.1.1.33  root     4941:        if(key_recv != 0) {
1.1.1.5   root     4942:                key_char = (key_code >> 0) & 0xff;
                   4943:                key_scan = (key_code >> 8) & 0xff;
                   4944:                key_code >>= 16;
1.1.1.33  root     4945:                key_recv >>= 16;
1.1.1.5   root     4946:        } else {
1.1.1.35  root     4947:                while(key_buf_char != NULL && key_buf_scan != NULL && !m_halted) {
                   4948:                        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   4949: #ifdef USE_SERVICE_THREAD
                   4950:                                EnterCriticalSection(&key_buf_crit_sect);
                   4951: #endif
                   4952:                                bool empty = key_buf_char->empty();
                   4953: #ifdef USE_SERVICE_THREAD
                   4954:                                LeaveCriticalSection(&key_buf_crit_sect);
                   4955: #endif
                   4956:                                if(!empty) break;
                   4957:                        }
1.1.1.23  root     4958:                        if(!(fd < process->max_files && file_handler[fd].valid && file_handler[fd].atty && file_mode[file_handler[fd].mode].in)) {
                   4959:                                // NOTE: stdin is redirected to stderr when we do "type (file) | more" on freedos's command.com
                   4960:                                if(_kbhit()) {
1.1.1.32  root     4961:                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     4962: #ifdef USE_SERVICE_THREAD
                   4963:                                                EnterCriticalSection(&key_buf_crit_sect);
                   4964: #endif
1.1.1.51  root     4965:                                                pcbios_set_key_buffer(_getch(), 0x00);
1.1.1.35  root     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.51  root     4987:                        pcbios_get_key_buffer(&key_char, &key_scan);
1.1.1.35  root     4988: #ifdef USE_SERVICE_THREAD
                   4989:                        LeaveCriticalSection(&key_buf_crit_sect);
                   4990: #endif
1.1.1.5   root     4991:                }
1.1       root     4992:        }
                   4993:        if(echo && key_char) {
                   4994:                msdos_putch(key_char);
                   4995:        }
                   4996:        return key_char ? key_char : (key_scan != 0xe0) ? key_scan : 0;
                   4997: }
                   4998: 
                   4999: inline int msdos_getch()
                   5000: {
                   5001:        return(msdos_getch_ex(0));
                   5002: }
                   5003: 
                   5004: inline int msdos_getche()
                   5005: {
                   5006:        return(msdos_getch_ex(1));
                   5007: }
                   5008: 
                   5009: int msdos_write(int fd, const void *buffer, unsigned int count)
                   5010: {
1.1.1.37  root     5011:        if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].sio_port >= 1 && file_handler[fd].sio_port <= 4) {
                   5012:                // write to serial port
1.1.1.38  root     5013:                int written = 0;
1.1.1.37  root     5014:                if(sio_port_number[file_handler[fd].sio_port - 1] != 0) {
                   5015:                        UINT8 *buf = (UINT8 *)buffer;
                   5016:                        UINT8 selector = sio_read(file_handler[fd].sio_port - 1, 3);
                   5017:                        sio_write(file_handler[fd].sio_port - 1, 3, selector & ~0x80);
1.1.1.38  root     5018:                        DWORD timeout = timeGetTime() + 1000;
                   5019:                        while(written < count) {
                   5020:                                if(sio_read(file_handler[fd].sio_port - 1, 5) & 0x20) {
                   5021:                                        sio_write(file_handler[fd].sio_port - 1, 0, buf[written++]);
                   5022:                                        timeout = timeGetTime() + 1000;
                   5023:                                } else {
                   5024:                                        if(timeGetTime() > timeout) {
                   5025:                                                break;
                   5026:                                        }
                   5027:                                        Sleep(10);
                   5028:                                }
1.1.1.37  root     5029:                        }
                   5030:                        sio_write(file_handler[fd].sio_port - 1, 3, selector);
                   5031:                }
1.1.1.38  root     5032:                return(written);
1.1.1.37  root     5033:        } else if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].lpt_port >= 1 && file_handler[fd].lpt_port <= 3) {
                   5034:                // write to printer port
                   5035:                UINT8 *buf = (UINT8 *)buffer;
                   5036:                for(unsigned int i = 0; i < count; i++) {
                   5037: //                     printer_out(file_handler[fd].lpt_port - 1, buf[i]);
                   5038:                        pcbios_printer_out(file_handler[fd].lpt_port - 1, buf[i]);
                   5039:                }
                   5040:                return(count);
                   5041:        } else if(fd == 1 && file_handler[1].valid && !file_handler[1].atty) {
1.1       root     5042:                // CR+LF -> LF
1.1.1.37  root     5043:                static int is_cr = 0;
1.1       root     5044:                UINT8 *buf = (UINT8 *)buffer;
                   5045:                for(unsigned int i = 0; i < count; i++) {
                   5046:                        UINT8 data = buf[i];
                   5047:                        if(is_cr) {
                   5048:                                if(data != 0x0a) {
                   5049:                                        UINT8 tmp = 0x0d;
                   5050:                                        _write(1, &tmp, 1);
                   5051:                                }
                   5052:                                _write(1, &data, 1);
                   5053:                                is_cr = 0;
                   5054:                        } else if(data == 0x0d) {
                   5055:                                is_cr = 1;
                   5056:                        } else {
                   5057:                                _write(1, &data, 1);
                   5058:                        }
                   5059:                }
                   5060:                return(count);
                   5061:        }
1.1.1.14  root     5062:        vram_flush();
1.1       root     5063:        return(_write(fd, buffer, count));
                   5064: }
                   5065: 
                   5066: void msdos_putch(UINT8 data)
1.1.1.50  root     5067: {
                   5068:        msdos_stdio_reopen();
                   5069:        
                   5070:        process_t *process = msdos_process_info_get(current_psp);
                   5071:        int fd = msdos_psp_get_file_table(1, current_psp);
                   5072:        
                   5073:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
                   5074:                // stdout is redirected to file
                   5075:                msdos_write(fd, &data, 1);
                   5076:                return;
                   5077:        }
                   5078:        
                   5079:        // call int 29h ?
                   5080:        if(*(UINT16 *)(mem + 4 * 0x29 + 0) == 0x29 &&
                   5081:           *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   5082:                // int 29h is not hooked, no need to call int 29h
                   5083:                msdos_putch_fast(data);
                   5084: #ifdef USE_SERVICE_THREAD
                   5085:        } else if(in_service && main_thread_id != GetCurrentThreadId()) {
                   5086:                // XXX: in usually we should not reach here
                   5087:                // this is called from service thread to echo the input
                   5088:                // we can not call int 29h because it causes a critial issue to control cpu running in main thread :-(
                   5089:                msdos_putch_fast(data);
                   5090: #endif
1.1.1.51  root     5091:        } else if(in_service_29h) {
1.1.1.50  root     5092:                // disallow reentering call int 29h routine to prevent an infinite loop :-(
                   5093:                msdos_putch_fast(data);
                   5094:        } else {
                   5095:                // this is called from main thread, so we can call int 29h :-)
1.1.1.51  root     5096:                in_service_29h = true;
1.1.1.50  root     5097:                try {
                   5098:                        UINT32 tmp_pc = m_pc;
                   5099:                        UINT16 tmp_ax = REG16(AX);
                   5100:                        UINT32 tmp_bx = REG16(BX); // BX may be destroyed by some versions of DOS 3.3
                   5101:                        
                   5102:                        // call int 29h routine is at fffc:0027
                   5103:                        i386_call_far(DUMMY_TOP >> 4, 0x0027);
                   5104:                        REG8(AL) = data;
                   5105:                        
                   5106:                        // run cpu until call int 29h routine is done
                   5107:                        while(!m_halted && tmp_pc != m_pc) {
                   5108:                                try {
                   5109:                                        hardware_run_cpu();
                   5110:                                } catch(...) {
                   5111:                                }
                   5112:                        }
                   5113:                        REG16(AX) = tmp_ax;
                   5114:                        REG16(BX) = tmp_bx;
                   5115:                } catch(...) {
                   5116:                }
1.1.1.51  root     5117:                in_service_29h = false;
1.1.1.50  root     5118:        }
                   5119: }
                   5120: 
                   5121: void msdos_putch_fast(UINT8 data)
1.1.1.35  root     5122: #ifdef USE_SERVICE_THREAD
                   5123: {
                   5124:        EnterCriticalSection(&putch_crit_sect);
                   5125:        msdos_putch_tmp(data);
                   5126:        LeaveCriticalSection(&putch_crit_sect);
                   5127: }
                   5128: void msdos_putch_tmp(UINT8 data)
                   5129: #endif
1.1       root     5130: {
1.1.1.34  root     5131:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   5132:        SMALL_RECT rect;
                   5133:        COORD co;
1.1       root     5134:        static int p = 0;
                   5135:        static int is_kanji = 0;
                   5136:        static int is_esc = 0;
                   5137:        static int stored_x;
                   5138:        static int stored_y;
                   5139:        static WORD stored_a;
1.1.1.20  root     5140:        static char tmp[64], out[64];
1.1       root     5141:        
1.1.1.23  root     5142:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     5143:        
                   5144:        // output to console
                   5145:        tmp[p++] = data;
                   5146:        
1.1.1.14  root     5147:        vram_flush();
                   5148:        
1.1       root     5149:        if(is_kanji) {
                   5150:                // kanji character
                   5151:                is_kanji = 0;
                   5152:        } else if(is_esc) {
                   5153:                // escape sequense
                   5154:                if((tmp[1] == ')' || tmp[1] == '(') && p == 3) {
                   5155:                        p = is_esc = 0;
                   5156:                } else if(tmp[1] == '=' && p == 4) {
                   5157:                        co.X = tmp[3] - 0x20;
1.1.1.14  root     5158:                        co.Y = tmp[2] - 0x20 + scr_top;
1.1       root     5159:                        SetConsoleCursorPosition(hStdout, co);
                   5160:                        mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14  root     5161:                        mem[0x451 + mem[0x462] * 2] = co.Y - scr_top;
1.1       root     5162:                        cursor_moved = false;
                   5163:                        p = is_esc = 0;
                   5164:                } else if((data >= 'a' && data <= 'z') || (data >= 'A' && data <= 'Z') || data == '*') {
                   5165:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5166:                        co.X = csbi.dwCursorPosition.X;
                   5167:                        co.Y = csbi.dwCursorPosition.Y;
                   5168:                        WORD wAttributes = csbi.wAttributes;
                   5169:                        
                   5170:                        if(tmp[1] == 'D') {
                   5171:                                co.Y++;
                   5172:                        } else if(tmp[1] == 'E') {
                   5173:                                co.X = 0;
                   5174:                                co.Y++;
                   5175:                        } else if(tmp[1] == 'M') {
                   5176:                                co.Y--;
                   5177:                        } else if(tmp[1] == '*') {
1.1.1.14  root     5178:                                SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5179:                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5180:                                co.X = 0;
                   5181:                                co.Y = csbi.srWindow.Top;
1.1       root     5182:                        } else if(tmp[1] == '[') {
                   5183:                                int param[256], params = 0;
                   5184:                                memset(param, 0, sizeof(param));
                   5185:                                for(int i = 2; i < p; i++) {
                   5186:                                        if(tmp[i] >= '0' && tmp[i] <= '9') {
                   5187:                                                param[params] *= 10;
                   5188:                                                param[params] += tmp[i] - '0';
                   5189:                                        } else {
                   5190:                                                params++;
                   5191:                                        }
                   5192:                                }
                   5193:                                if(data == 'A') {
1.1.1.14  root     5194:                                        co.Y -= (params == 0) ? 1 : param[0];
1.1       root     5195:                                } else if(data == 'B') {
1.1.1.14  root     5196:                                        co.Y += (params == 0) ? 1 : param[0];
1.1       root     5197:                                } else if(data == 'C') {
1.1.1.14  root     5198:                                        co.X += (params == 0) ? 1 : param[0];
1.1       root     5199:                                } else if(data == 'D') {
1.1.1.14  root     5200:                                        co.X -= (params == 0) ? 1 : param[0];
1.1       root     5201:                                } else if(data == 'H' || data == 'f') {
1.1.1.14  root     5202:                                        co.X = (param[1] == 0 ? 1 : param[1]) - 1;
                   5203:                                        co.Y = (param[0] == 0 ? 1 : param[0]) - 1 + csbi.srWindow.Top;
1.1       root     5204:                                } else if(data == 'J') {
1.1.1.14  root     5205:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5206:                                        if(param[0] == 0) {
                   5207:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     5208:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5209:                                                if(co.Y < csbi.srWindow.Bottom) {
                   5210:                                                        SET_RECT(rect, 0, co.Y + 1, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5211:                                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5212:                                                }
                   5213:                                        } else if(param[0] == 1) {
1.1.1.14  root     5214:                                                if(co.Y > csbi.srWindow.Top) {
                   5215:                                                        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, co.Y - 1);
                   5216:                                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5217:                                                }
                   5218:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.14  root     5219:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5220:                                        } else if(param[0] == 2) {
1.1.1.14  root     5221:                                                SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5222:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5223:                                                co.X = co.Y = 0;
                   5224:                                        }
                   5225:                                } else if(data == 'K') {
1.1.1.14  root     5226:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5227:                                        if(param[0] == 0) {
                   5228:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     5229:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5230:                                        } else if(param[0] == 1) {
                   5231:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.14  root     5232:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5233:                                        } else if(param[0] == 2) {
                   5234:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     5235:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5236:                                        }
                   5237:                                } else if(data == 'L') {
1.1.1.14  root     5238:                                        if(params == 0) {
                   5239:                                                param[0] = 1;
1.1       root     5240:                                        }
1.1.1.14  root     5241:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5242:                                        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5243:                                        SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5244:                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5245:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5246:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y + param[0] - 1);
1.1.1.14  root     5247:                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5248:                                        co.X = 0;
                   5249:                                } else if(data == 'M') {
1.1.1.14  root     5250:                                        if(params == 0) {
                   5251:                                                param[0] = 1;
                   5252:                                        }
                   5253:                                        if(co.Y + param[0] > csbi.srWindow.Bottom) {
                   5254:                                                clear_scr_buffer(csbi.wAttributes);
                   5255:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5256:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5257:                                        } else {
1.1.1.14  root     5258:                                                SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5259:                                                ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5260:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5261:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5262:                                                clear_scr_buffer(csbi.wAttributes);
1.1       root     5263:                                        }
                   5264:                                        co.X = 0;
                   5265:                                } else if(data == 'h') {
                   5266:                                        if(tmp[2] == '>' && tmp[3] == '5') {
                   5267:                                                CONSOLE_CURSOR_INFO cur;
                   5268:                                                GetConsoleCursorInfo(hStdout, &cur);
                   5269:                                                if(cur.bVisible) {
                   5270:                                                        cur.bVisible = FALSE;
1.1.1.14  root     5271: //                                                     SetConsoleCursorInfo(hStdout, &cur);
1.1       root     5272:                                                }
                   5273:                                        }
                   5274:                                } else if(data == 'l') {
                   5275:                                        if(tmp[2] == '>' && tmp[3] == '5') {
                   5276:                                                CONSOLE_CURSOR_INFO cur;
                   5277:                                                GetConsoleCursorInfo(hStdout, &cur);
                   5278:                                                if(!cur.bVisible) {
                   5279:                                                        cur.bVisible = TRUE;
1.1.1.14  root     5280: //                                                     SetConsoleCursorInfo(hStdout, &cur);
1.1       root     5281:                                                }
                   5282:                                        }
                   5283:                                } else if(data == 'm') {
                   5284:                                        wAttributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
                   5285:                                        int reverse = 0, hidden = 0;
                   5286:                                        for(int i = 0; i < params; i++) {
                   5287:                                                if(param[i] == 1) {
                   5288:                                                        wAttributes |= FOREGROUND_INTENSITY;
                   5289:                                                } else if(param[i] == 4) {
                   5290:                                                        wAttributes |= COMMON_LVB_UNDERSCORE;
                   5291:                                                } else if(param[i] == 7) {
                   5292:                                                        reverse = 1;
                   5293:                                                } else if(param[i] == 8 || param[i] == 16) {
                   5294:                                                        hidden = 1;
                   5295:                                                } else if((param[i] >= 17 && param[i] <= 23) || (param[i] >= 30 && param[i] <= 37)) {
                   5296:                                                        wAttributes &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
                   5297:                                                        if(param[i] >= 17 && param[i] <= 23) {
                   5298:                                                                param[i] -= 16;
                   5299:                                                        } else {
                   5300:                                                                param[i] -= 30;
                   5301:                                                        }
                   5302:                                                        if(param[i] & 1) {
                   5303:                                                                wAttributes |= FOREGROUND_RED;
                   5304:                                                        }
                   5305:                                                        if(param[i] & 2) {
                   5306:                                                                wAttributes |= FOREGROUND_GREEN;
                   5307:                                                        }
                   5308:                                                        if(param[i] & 4) {
                   5309:                                                                wAttributes |= FOREGROUND_BLUE;
                   5310:                                                        }
                   5311:                                                } else if(param[i] >= 40 && param[i] <= 47) {
                   5312:                                                        wAttributes &= ~(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
                   5313:                                                        if((param[i] - 40) & 1) {
                   5314:                                                                wAttributes |= BACKGROUND_RED;
                   5315:                                                        }
                   5316:                                                        if((param[i] - 40) & 2) {
                   5317:                                                                wAttributes |= BACKGROUND_GREEN;
                   5318:                                                        }
                   5319:                                                        if((param[i] - 40) & 4) {
                   5320:                                                                wAttributes |= BACKGROUND_BLUE;
                   5321:                                                        }
                   5322:                                                }
                   5323:                                        }
                   5324:                                        if(reverse) {
                   5325:                                                wAttributes &= ~0xff;
                   5326:                                                wAttributes |= BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
                   5327:                                        }
                   5328:                                        if(hidden) {
                   5329:                                                wAttributes &= ~0x0f;
                   5330:                                                wAttributes |= (wAttributes >> 4) & 0x0f;
                   5331:                                        }
                   5332:                                } else if(data == 'n') {
                   5333:                                        if(param[0] == 6) {
                   5334:                                                char tmp[16];
                   5335:                                                sprintf(tmp, "\x1b[%d;%dR", co.Y + 1, co.X + 1);
                   5336:                                                int len = strlen(tmp);
1.1.1.32  root     5337:                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     5338: #ifdef USE_SERVICE_THREAD
                   5339:                                                        EnterCriticalSection(&key_buf_crit_sect);
                   5340: #endif
1.1.1.32  root     5341:                                                        for(int i = 0; i < len; i++) {
1.1.1.51  root     5342:                                                                pcbios_set_key_buffer(tmp[i], 0x00);
1.1.1.32  root     5343:                                                        }
1.1.1.35  root     5344: #ifdef USE_SERVICE_THREAD
                   5345:                                                        LeaveCriticalSection(&key_buf_crit_sect);
                   5346: #endif
1.1       root     5347:                                                }
                   5348:                                        }
                   5349:                                } else if(data == 's') {
                   5350:                                        stored_x = co.X;
                   5351:                                        stored_y = co.Y;
                   5352:                                        stored_a = wAttributes;
                   5353:                                } else if(data == 'u') {
                   5354:                                        co.X = stored_x;
                   5355:                                        co.Y = stored_y;
                   5356:                                        wAttributes = stored_a;
                   5357:                                }
                   5358:                        }
                   5359:                        if(co.X < 0) {
                   5360:                                co.X = 0;
                   5361:                        } else if(co.X >= csbi.dwSize.X) {
                   5362:                                co.X = csbi.dwSize.X - 1;
                   5363:                        }
1.1.1.14  root     5364:                        if(co.Y < csbi.srWindow.Top) {
                   5365:                                co.Y = csbi.srWindow.Top;
                   5366:                        } else if(co.Y > csbi.srWindow.Bottom) {
                   5367:                                co.Y = csbi.srWindow.Bottom;
1.1       root     5368:                        }
                   5369:                        if(co.X != csbi.dwCursorPosition.X || co.Y != csbi.dwCursorPosition.Y) {
                   5370:                                SetConsoleCursorPosition(hStdout, co);
                   5371:                                mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14  root     5372:                                mem[0x451 + mem[0x462] * 2] = co.Y - csbi.srWindow.Top;
1.1       root     5373:                                cursor_moved = false;
                   5374:                        }
                   5375:                        if(wAttributes != csbi.wAttributes) {
                   5376:                                SetConsoleTextAttribute(hStdout, wAttributes);
                   5377:                        }
                   5378:                        p = is_esc = 0;
                   5379:                }
                   5380:                return;
                   5381:        } else {
                   5382:                if(msdos_lead_byte_check(data)) {
                   5383:                        is_kanji = 1;
                   5384:                        return;
                   5385:                } else if(data == 0x1b) {
                   5386:                        is_esc = 1;
                   5387:                        return;
                   5388:                }
                   5389:        }
1.1.1.20  root     5390:        
                   5391:        DWORD q = 0, num;
                   5392:        is_kanji = 0;
                   5393:        for(int i = 0; i < p; i++) {
                   5394:                UINT8 c = tmp[i];
                   5395:                if(is_kanji) {
                   5396:                        is_kanji = 0;
                   5397:                } else if(msdos_lead_byte_check(data)) {
                   5398:                        is_kanji = 1;
                   5399:                } else if(msdos_ctrl_code_check(data)) {
                   5400:                        out[q++] = '^';
                   5401:                        c += 'A' - 1;
                   5402:                }
                   5403:                out[q++] = c;
                   5404:        }
1.1.1.34  root     5405:        if(q == 1 && out[0] == 0x08) {
                   5406:                // back space
                   5407:                GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5408:                if(csbi.dwCursorPosition.X > 0) {
                   5409:                        co.X = csbi.dwCursorPosition.X - 1;
                   5410:                        co.Y = csbi.dwCursorPosition.Y;
                   5411:                        SetConsoleCursorPosition(hStdout, co);
                   5412:                } else if(csbi.dwCursorPosition.Y > 0) {
                   5413:                        co.X = csbi.dwSize.X - 1;
                   5414:                        co.Y = csbi.dwCursorPosition.Y - 1;
                   5415:                        SetConsoleCursorPosition(hStdout, co);
                   5416:                } else {
                   5417:                        WriteConsole(hStdout, out, q, &num, NULL); // to make sure
                   5418:                }
                   5419:        } else {
                   5420:                WriteConsole(hStdout, out, q, &num, NULL);
                   5421:        }
1.1       root     5422:        p = 0;
1.1.1.14  root     5423:        
1.1.1.15  root     5424:        if(!restore_console_on_exit) {
                   5425:                GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5426:                scr_top = csbi.srWindow.Top;
                   5427:        }
1.1       root     5428:        cursor_moved = true;
                   5429: }
                   5430: 
                   5431: int msdos_aux_in()
                   5432: {
1.1.1.21  root     5433:        msdos_stdio_reopen();
                   5434:        
1.1.1.20  root     5435:        process_t *process = msdos_process_info_get(current_psp);
                   5436:        int fd = msdos_psp_get_file_table(3, current_psp);
                   5437:        
                   5438:        if(fd < process->max_files && file_handler[fd].valid && !eof(fd)) {
1.1       root     5439:                char data = 0;
1.1.1.37  root     5440:                msdos_read(fd, &data, 1);
1.1       root     5441:                return(data);
                   5442:        } else {
                   5443:                return(EOF);
                   5444:        }
                   5445: }
                   5446: 
                   5447: void msdos_aux_out(char data)
                   5448: {
1.1.1.21  root     5449:        msdos_stdio_reopen();
                   5450:        
1.1.1.20  root     5451:        process_t *process = msdos_process_info_get(current_psp);
                   5452:        int fd = msdos_psp_get_file_table(3, current_psp);
                   5453:        
                   5454:        if(fd < process->max_files && file_handler[fd].valid) {
                   5455:                msdos_write(fd, &data, 1);
1.1       root     5456:        }
                   5457: }
                   5458: 
                   5459: void msdos_prn_out(char data)
                   5460: {
1.1.1.21  root     5461:        msdos_stdio_reopen();
                   5462:        
1.1.1.20  root     5463:        process_t *process = msdos_process_info_get(current_psp);
                   5464:        int fd = msdos_psp_get_file_table(4, current_psp);
                   5465:        
                   5466:        if(fd < process->max_files && file_handler[fd].valid) {
                   5467:                msdos_write(fd, &data, 1);
1.1       root     5468:        }
                   5469: }
                   5470: 
                   5471: // memory control
                   5472: 
1.1.1.52! root     5473: mcb_t *msdos_mcb_create(int mcb_seg, UINT8 mz, UINT16 psp, int paragraphs, const char *prog_name = NULL)
1.1       root     5474: {
                   5475:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5476:        
                   5477:        mcb->mz = mz;
                   5478:        mcb->psp = psp;
1.1.1.30  root     5479:        mcb->paragraphs = paragraphs;
1.1.1.39  root     5480:        
                   5481:        if(prog_name != NULL) {
                   5482:                memset(mcb->prog_name, 0, 8);
                   5483:                memcpy(mcb->prog_name, prog_name, min(8, strlen(prog_name)));
                   5484:        }
1.1       root     5485:        return(mcb);
                   5486: }
                   5487: 
                   5488: void msdos_mcb_check(mcb_t *mcb)
                   5489: {
                   5490:        if(!(mcb->mz == 'M' || mcb->mz == 'Z')) {
1.1.1.28  root     5491:                #if 0
                   5492:                        // shutdown now !!!
                   5493:                        fatalerror("broken memory control block\n");
                   5494:                #else
                   5495:                        // return error code and continue
                   5496:                        throw(0x07); // broken memory control block
                   5497:                #endif
1.1       root     5498:        }
                   5499: }
                   5500: 
1.1.1.39  root     5501: void msdos_mem_split(int seg, int paragraphs)
1.1       root     5502: {
                   5503:        int mcb_seg = seg - 1;
                   5504:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5505:        msdos_mcb_check(mcb);
                   5506:        
1.1.1.30  root     5507:        if(mcb->paragraphs > paragraphs) {
1.1       root     5508:                int new_seg = mcb_seg + 1 + paragraphs;
1.1.1.30  root     5509:                int new_paragraphs = mcb->paragraphs - paragraphs - 1;
1.1       root     5510:                
                   5511:                msdos_mcb_create(new_seg, mcb->mz, 0, new_paragraphs);
                   5512:                mcb->mz = 'M';
1.1.1.30  root     5513:                mcb->paragraphs = paragraphs;
1.1       root     5514:        }
                   5515: }
                   5516: 
                   5517: void msdos_mem_merge(int seg)
                   5518: {
                   5519:        int mcb_seg = seg - 1;
                   5520:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5521:        msdos_mcb_check(mcb);
                   5522:        
                   5523:        while(1) {
                   5524:                if(mcb->mz == 'Z') {
                   5525:                        break;
                   5526:                }
1.1.1.30  root     5527:                int next_seg = mcb_seg + 1 + mcb->paragraphs;
1.1       root     5528:                mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5529:                msdos_mcb_check(next_mcb);
                   5530:                
                   5531:                if(next_mcb->psp != 0) {
                   5532:                        break;
                   5533:                }
                   5534:                mcb->mz = next_mcb->mz;
1.1.1.30  root     5535:                mcb->paragraphs = mcb->paragraphs + 1 + next_mcb->paragraphs;
1.1       root     5536:        }
                   5537: }
                   5538: 
1.1.1.8   root     5539: int msdos_mem_alloc(int mcb_seg, int paragraphs, int new_process)
1.1       root     5540: {
                   5541:        while(1) {
                   5542:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     5543:                bool last_block;
1.1       root     5544:                
1.1.1.14  root     5545:                if(mcb->psp == 0) {
                   5546:                        msdos_mem_merge(mcb_seg + 1);
                   5547:                } else {
                   5548:                        msdos_mcb_check(mcb);
                   5549:                }
1.1.1.33  root     5550:                if(!(last_block = (mcb->mz == 'Z'))) {
                   5551:                        // check if the next is dummy mcb to link to umb
                   5552:                        int next_seg = mcb_seg + 1 + mcb->paragraphs;
                   5553:                        mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5554:                        last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
                   5555:                }
                   5556:                if(!(new_process && !last_block)) {
1.1.1.30  root     5557:                        if(mcb->psp == 0 && mcb->paragraphs >= paragraphs) {
1.1       root     5558:                                msdos_mem_split(mcb_seg + 1, paragraphs);
                   5559:                                mcb->psp = current_psp;
                   5560:                                return(mcb_seg + 1);
                   5561:                        }
                   5562:                }
                   5563:                if(mcb->mz == 'Z') {
                   5564:                        break;
                   5565:                }
1.1.1.30  root     5566:                mcb_seg += 1 + mcb->paragraphs;
1.1       root     5567:        }
                   5568:        return(-1);
                   5569: }
                   5570: 
                   5571: int msdos_mem_realloc(int seg, int paragraphs, int *max_paragraphs)
                   5572: {
                   5573:        int mcb_seg = seg - 1;
                   5574:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5575:        msdos_mcb_check(mcb);
1.1.1.30  root     5576:        int current_paragraphs = mcb->paragraphs;
1.1       root     5577:        
                   5578:        msdos_mem_merge(seg);
1.1.1.30  root     5579:        if(paragraphs > mcb->paragraphs) {
1.1.1.14  root     5580:                if(max_paragraphs) {
1.1.1.30  root     5581:                        *max_paragraphs = mcb->paragraphs;
1.1.1.14  root     5582:                }
1.1       root     5583:                msdos_mem_split(seg, current_paragraphs);
                   5584:                return(-1);
                   5585:        }
                   5586:        msdos_mem_split(seg, paragraphs);
                   5587:        return(0);
                   5588: }
                   5589: 
                   5590: void msdos_mem_free(int seg)
                   5591: {
                   5592:        int mcb_seg = seg - 1;
                   5593:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5594:        msdos_mcb_check(mcb);
                   5595:        
                   5596:        mcb->psp = 0;
                   5597:        msdos_mem_merge(seg);
                   5598: }
                   5599: 
1.1.1.8   root     5600: int msdos_mem_get_free(int mcb_seg, int new_process)
1.1       root     5601: {
                   5602:        int max_paragraphs = 0;
                   5603:        
                   5604:        while(1) {
                   5605:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     5606:                bool last_block;
                   5607:                
1.1       root     5608:                msdos_mcb_check(mcb);
                   5609:                
1.1.1.33  root     5610:                if(!(last_block = (mcb->mz == 'Z'))) {
                   5611:                        // check if the next is dummy mcb to link to umb
                   5612:                        int next_seg = mcb_seg + 1 + mcb->paragraphs;
                   5613:                        mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5614:                        last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
                   5615:                }
                   5616:                if(!(new_process && !last_block)) {
1.1.1.30  root     5617:                        if(mcb->psp == 0 && mcb->paragraphs > max_paragraphs) {
                   5618:                                max_paragraphs = mcb->paragraphs;
1.1       root     5619:                        }
                   5620:                }
                   5621:                if(mcb->mz == 'Z') {
                   5622:                        break;
                   5623:                }
1.1.1.30  root     5624:                mcb_seg += 1 + mcb->paragraphs;
1.1       root     5625:        }
1.1.1.14  root     5626:        return(max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs);
1.1       root     5627: }
                   5628: 
1.1.1.8   root     5629: int msdos_mem_get_last_mcb(int mcb_seg, UINT16 psp)
                   5630: {
                   5631:        int last_seg = -1;
                   5632:        
                   5633:        while(1) {
                   5634:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5635:                msdos_mcb_check(mcb);
                   5636:                
1.1.1.14  root     5637:                if(mcb->psp == psp) {
1.1.1.8   root     5638:                        last_seg = mcb_seg;
                   5639:                }
1.1.1.14  root     5640:                if(mcb->mz == 'Z') {
                   5641:                        break;
                   5642:                }
1.1.1.30  root     5643:                mcb_seg += 1 + mcb->paragraphs;
1.1.1.8   root     5644:        }
                   5645:        return(last_seg);
                   5646: }
                   5647: 
1.1.1.19  root     5648: int msdos_mem_get_umb_linked()
                   5649: {
1.1.1.33  root     5650:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   5651:        msdos_mcb_check(mcb);
1.1.1.19  root     5652:        
1.1.1.33  root     5653:        if(mcb->mz == 'M') {
                   5654:                return(-1);
1.1.1.19  root     5655:        }
                   5656:        return(0);
                   5657: }
                   5658: 
1.1.1.33  root     5659: void msdos_mem_link_umb()
1.1.1.19  root     5660: {
1.1.1.33  root     5661:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   5662:        msdos_mcb_check(mcb);
1.1.1.19  root     5663:        
1.1.1.33  root     5664:        mcb->mz = 'M';
                   5665:        mcb->paragraphs = (UMB_TOP >> 4) - (MEMORY_END >> 4);
1.1.1.39  root     5666:        
                   5667:        ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked |= 0x01;
1.1.1.19  root     5668: }
                   5669: 
1.1.1.33  root     5670: void msdos_mem_unlink_umb()
1.1.1.19  root     5671: {
1.1.1.33  root     5672:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   5673:        msdos_mcb_check(mcb);
1.1.1.19  root     5674:        
1.1.1.33  root     5675:        mcb->mz = 'Z';
                   5676:        mcb->paragraphs = 0;
1.1.1.39  root     5677:        
                   5678:        ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked &= ~0x01;
1.1.1.19  root     5679: }
                   5680: 
1.1.1.29  root     5681: #ifdef SUPPORT_HMA
                   5682: 
                   5683: hma_mcb_t *msdos_hma_mcb_create(int offset, int owner, int size, int next)
                   5684: {
                   5685:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5686:        
                   5687:        mcb->ms[0] = 'M';
                   5688:        mcb->ms[1] = 'S';
                   5689:        mcb->owner = owner;
                   5690:        mcb->size = size;
                   5691:        mcb->next = next;
                   5692:        return(mcb);
                   5693: }
                   5694: 
                   5695: bool msdos_is_hma_mcb_valid(hma_mcb_t *mcb)
                   5696: {
                   5697:        return(mcb->ms[0] == 'M' && mcb->ms[1] == 'S');
                   5698: }
                   5699: 
                   5700: int msdos_hma_mem_split(int offset, int size)
                   5701: {
                   5702:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5703:        
                   5704:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5705:                return(-1);
                   5706:        }
                   5707:        if(mcb->size >= size + 0x10) {
                   5708:                int new_offset = offset + 0x10 + size;
                   5709:                int new_size = mcb->size - 0x10 - size;
                   5710:                
                   5711:                msdos_hma_mcb_create(new_offset, 0, new_size, mcb->next);
                   5712:                mcb->size = size;
                   5713:                mcb->next = new_offset;
                   5714:                return(0);
                   5715:        }
                   5716:        return(-1);
                   5717: }
                   5718: 
                   5719: void msdos_hma_mem_merge(int offset)
                   5720: {
                   5721:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5722:        
                   5723:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5724:                return;
                   5725:        }
                   5726:        while(1) {
                   5727:                if(mcb->next == 0) {
                   5728:                        break;
                   5729:                }
                   5730:                hma_mcb_t *next_mcb = (hma_mcb_t *)(mem + 0xffff0 + mcb->next);
                   5731:                
                   5732:                if(!msdos_is_hma_mcb_valid(next_mcb)) {
                   5733:                        return;
                   5734:                }
                   5735:                if(next_mcb->owner != 0) {
                   5736:                        break;
                   5737:                }
                   5738:                mcb->size += 0x10 + next_mcb->size;
                   5739:                mcb->next = next_mcb->next;
                   5740:        }
                   5741: }
                   5742: 
                   5743: int msdos_hma_mem_alloc(int size, UINT16 owner)
                   5744: {
                   5745:        int offset = 0x10; // first mcb in HMA
                   5746:        
                   5747:        while(1) {
                   5748:                hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5749:                
                   5750:                if(!msdos_is_hma_mcb_valid(mcb)) {
                   5751:                        return(-1);
                   5752:                }
                   5753:                if(mcb->owner == 0) {
                   5754:                        msdos_hma_mem_merge(offset);
                   5755:                }
                   5756:                if(mcb->owner == 0 && mcb->size >= size) {
                   5757:                        msdos_hma_mem_split(offset, size);
                   5758:                        mcb->owner = owner;
                   5759:                        return(offset);
                   5760:                }
                   5761:                if(mcb->next == 0) {
                   5762:                        break;
                   5763:                }
                   5764:                offset = mcb->next;
                   5765:        }
                   5766:        return(-1);
                   5767: }
                   5768: 
                   5769: int msdos_hma_mem_realloc(int offset, int size)
                   5770: {
                   5771:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5772:        
                   5773:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5774:                return(-1);
                   5775:        }
                   5776:        if(mcb->size < size) {
                   5777:                return(-1);
                   5778:        }
                   5779:        msdos_hma_mem_split(offset, size);
                   5780:        return(0);
                   5781: }
                   5782: 
                   5783: void msdos_hma_mem_free(int offset)
                   5784: {
                   5785:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5786:        
                   5787:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5788:                return;
                   5789:        }
                   5790:        mcb->owner = 0;
                   5791:        msdos_hma_mem_merge(offset);
                   5792: }
                   5793: 
                   5794: int msdos_hma_mem_get_free(int *available_offset)
                   5795: {
                   5796:        int offset = 0x10; // first mcb in HMA
                   5797:        int size = 0;
                   5798:        
                   5799:        while(1) {
                   5800:                hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5801:                
                   5802:                if(!msdos_is_hma_mcb_valid(mcb)) {
                   5803:                        return(0);
                   5804:                }
                   5805:                if(mcb->owner == 0 && size < mcb->size) {
                   5806:                        if(available_offset != NULL) {
                   5807:                                *available_offset = offset;
                   5808:                        }
                   5809:                        size = mcb->size;
                   5810:                }
                   5811:                if(mcb->next == 0) {
                   5812:                        break;
                   5813:                }
                   5814:                offset = mcb->next;
                   5815:        }
                   5816:        return(size);
                   5817: }
                   5818: 
                   5819: #endif
                   5820: 
1.1       root     5821: // environment
                   5822: 
1.1.1.45  root     5823: void msdos_env_set_argv(int env_seg, const char *argv)
1.1       root     5824: {
                   5825:        char *dst = (char *)(mem + (env_seg << 4));
                   5826:        
                   5827:        while(1) {
                   5828:                if(dst[0] == 0) {
                   5829:                        break;
                   5830:                }
                   5831:                dst += strlen(dst) + 1;
                   5832:        }
                   5833:        *dst++ = 0; // end of environment
                   5834:        *dst++ = 1; // top of argv[0]
                   5835:        *dst++ = 0;
                   5836:        memcpy(dst, argv, strlen(argv));
                   5837:        dst += strlen(argv);
                   5838:        *dst++ = 0;
                   5839:        *dst++ = 0;
                   5840: }
                   5841: 
1.1.1.45  root     5842: const char *msdos_env_get_argv(int env_seg)
1.1       root     5843: {
                   5844:        static char env[ENV_SIZE];
                   5845:        char *src = env;
                   5846:        
                   5847:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   5848:        while(1) {
                   5849:                if(src[0] == 0) {
                   5850:                        if(src[1] == 1) {
                   5851:                                return(src + 3);
                   5852:                        }
                   5853:                        break;
                   5854:                }
                   5855:                src += strlen(src) + 1;
                   5856:        }
                   5857:        return(NULL);
                   5858: }
                   5859: 
1.1.1.45  root     5860: const char *msdos_env_get(int env_seg, const char *name)
1.1       root     5861: {
                   5862:        static char env[ENV_SIZE];
                   5863:        char *src = env;
                   5864:        
                   5865:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   5866:        while(1) {
                   5867:                if(src[0] == 0) {
                   5868:                        break;
                   5869:                }
                   5870:                int len = strlen(src);
                   5871:                char *n = my_strtok(src, "=");
                   5872:                char *v = src + strlen(n) + 1;
                   5873:                
                   5874:                if(_stricmp(name, n) == 0) {
                   5875:                        return(v);
                   5876:                }
                   5877:                src += len + 1;
                   5878:        }
                   5879:        return(NULL);
                   5880: }
                   5881: 
1.1.1.45  root     5882: void msdos_env_set(int env_seg, const char *name, const char *value)
1.1       root     5883: {
                   5884:        char env[ENV_SIZE];
                   5885:        char *src = env;
                   5886:        char *dst = (char *)(mem + (env_seg << 4));
1.1.1.45  root     5887:        const char *argv = msdos_env_get_argv(env_seg);
1.1       root     5888:        int done = 0;
                   5889:        
                   5890:        memcpy(src, dst, ENV_SIZE);
                   5891:        memset(dst, 0, ENV_SIZE);
                   5892:        while(1) {
                   5893:                if(src[0] == 0) {
                   5894:                        break;
                   5895:                }
                   5896:                int len = strlen(src);
                   5897:                char *n = my_strtok(src, "=");
                   5898:                char *v = src + strlen(n) + 1;
                   5899:                char tmp[1024];
                   5900:                
                   5901:                if(_stricmp(name, n) == 0) {
                   5902:                        sprintf(tmp, "%s=%s", n, value);
                   5903:                        done = 1;
                   5904:                } else {
                   5905:                        sprintf(tmp, "%s=%s", n, v);
                   5906:                }
                   5907:                memcpy(dst, tmp, strlen(tmp));
                   5908:                dst += strlen(tmp) + 1;
                   5909:                src += len + 1;
                   5910:        }
                   5911:        if(!done) {
                   5912:                char tmp[1024];
                   5913:                
                   5914:                sprintf(tmp, "%s=%s", name, value);
                   5915:                memcpy(dst, tmp, strlen(tmp));
                   5916:                dst += strlen(tmp) + 1;
                   5917:        }
                   5918:        if(argv) {
                   5919:                *dst++ = 0; // end of environment
                   5920:                *dst++ = 1; // top of argv[0]
                   5921:                *dst++ = 0;
                   5922:                memcpy(dst, argv, strlen(argv));
                   5923:                dst += strlen(argv);
                   5924:                *dst++ = 0;
                   5925:                *dst++ = 0;
                   5926:        }
                   5927: }
                   5928: 
                   5929: // process
                   5930: 
1.1.1.8   root     5931: psp_t *msdos_psp_create(int psp_seg, UINT16 mcb_seg, UINT16 parent_psp, UINT16 env_seg)
1.1       root     5932: {
                   5933:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   5934:        
                   5935:        memset(psp, 0, PSP_SIZE);
                   5936:        psp->exit[0] = 0xcd;
                   5937:        psp->exit[1] = 0x20;
1.1.1.8   root     5938:        psp->first_mcb = mcb_seg;
1.1.1.46  root     5939: #if 1
1.1.1.49  root     5940:        psp->call5[0] = 0xcd;   // int 30h
                   5941:        psp->call5[1] = 0x30;
1.1.1.46  root     5942:        psp->call5[2] = 0xc3;   // ret
                   5943: #else
                   5944:        psp->call5[0] = 0x8a;   // mov ah, cl
                   5945:        psp->call5[1] = 0xe1;
                   5946:        psp->call5[2] = 0xcd;   // int 21h
                   5947:        psp->call5[3] = 0x21;
                   5948:        psp->call5[4] = 0xc3;   // ret
                   5949: #endif
1.1       root     5950:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   5951:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   5952:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   5953:        psp->parent_psp = parent_psp;
1.1.1.20  root     5954:        if(parent_psp == (UINT16)-1) {
                   5955:                for(int i = 0; i < 20; i++) {
                   5956:                        if(file_handler[i].valid) {
                   5957:                                psp->file_table[i] = i;
                   5958:                        } else {
                   5959:                                psp->file_table[i] = 0xff;
                   5960:                        }
1.1       root     5961:                }
1.1.1.20  root     5962:        } else {
                   5963:                memcpy(psp->file_table, ((psp_t *)(mem + (parent_psp << 4)))->file_table, 20);
1.1       root     5964:        }
                   5965:        psp->env_seg = env_seg;
                   5966:        psp->stack.w.l = REG16(SP);
1.1.1.3   root     5967:        psp->stack.w.h = SREG(SS);
1.1.1.14  root     5968:        psp->file_table_size = 20;
                   5969:        psp->file_table_ptr.w.l = 0x18;
                   5970:        psp->file_table_ptr.w.h = psp_seg;
1.1       root     5971:        psp->service[0] = 0xcd;
                   5972:        psp->service[1] = 0x21;
                   5973:        psp->service[2] = 0xcb;
                   5974:        return(psp);
                   5975: }
                   5976: 
1.1.1.20  root     5977: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg)
                   5978: {
                   5979:        if(psp_seg && fd < 20) {
                   5980:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   5981:                psp->file_table[fd] = value;
                   5982:        }
                   5983: }
                   5984: 
                   5985: int msdos_psp_get_file_table(int fd, int psp_seg)
                   5986: {
                   5987:        if(psp_seg && fd < 20) {
                   5988:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   5989:                fd = psp->file_table[fd];
                   5990:        }
                   5991:        return fd;
                   5992: }
                   5993: 
1.1.1.52! root     5994: int msdos_process_exec(const char *cmd, param_block_t *param, UINT8 al, bool first_process = false)
1.1       root     5995: {
                   5996:        // load command file
                   5997:        int fd = -1;
1.1.1.45  root     5998:        int sio_port = 0;
                   5999:        int lpt_port = 0;
1.1       root     6000:        int dos_command = 0;
1.1.1.24  root     6001:        char command[MAX_PATH], path[MAX_PATH], opt[MAX_PATH], *name = NULL, name_tmp[MAX_PATH];
1.1.1.38  root     6002:        char pipe_stdin_path[MAX_PATH] = {0};
                   6003:        char pipe_stdout_path[MAX_PATH] = {0};
1.1.1.39  root     6004:        char pipe_stderr_path[MAX_PATH] = {0};
1.1       root     6005:        
                   6006:        int opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   6007:        int opt_len = mem[opt_ofs];
                   6008:        memset(opt, 0, sizeof(opt));
                   6009:        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   6010:        
1.1.1.14  root     6011:        if(strlen(cmd) >= 5 && _stricmp(&cmd[strlen(cmd) - 4], ".BAT") == 0) {
                   6012:                // this is a batch file, run command.com
                   6013:                char tmp[MAX_PATH];
                   6014:                if(opt_len != 0) {
                   6015:                        sprintf(tmp, "/C %s %s", cmd, opt);
                   6016:                } else {
                   6017:                        sprintf(tmp, "/C %s", cmd);
                   6018:                }
                   6019:                strcpy(opt, tmp);
                   6020:                opt_len = strlen(opt);
                   6021:                mem[opt_ofs] = opt_len;
                   6022:                sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6023:                strcpy(command, comspec_path);
                   6024:                strcpy(name_tmp, "COMMAND.COM");
                   6025:        } else {
                   6026:                if(_stricmp(cmd, "C:\\COMMAND.COM") == 0) {
                   6027:                        // redirect C:\COMMAND.COM to comspec_path
                   6028:                        strcpy(command, comspec_path);
                   6029:                } else {
                   6030:                        strcpy(command, cmd);
                   6031:                }
1.1.1.24  root     6032:                if(GetFullPathName(command, MAX_PATH, path, &name) == 0) {
                   6033:                        return(-1);
                   6034:                }
1.1.1.14  root     6035:                memset(name_tmp, 0, sizeof(name_tmp));
                   6036:                strcpy(name_tmp, name);
                   6037:                
                   6038:                // check command.com
1.1.1.38  root     6039:                if((_stricmp(name, "COMMAND.COM") == 0 || _stricmp(name, "COMMAND") == 0) && _access(comspec_path, 0) != 0) {
                   6040:                        // we can not load command.com, so run program directly if "command /c (program)" is specified
1.1.1.14  root     6041:                        if(opt_len == 0) {
                   6042: //                             process_t *current_process = msdos_process_info_get(current_psp);
                   6043:                                process_t *current_process = NULL;
                   6044:                                for(int i = 0; i < MAX_PROCESS; i++) {
                   6045:                                        if(process[i].psp == current_psp) {
                   6046:                                                current_process = &process[i];
                   6047:                                                break;
                   6048:                                        }
                   6049:                                }
                   6050:                                if(current_process != NULL) {
                   6051:                                        param->cmd_line.dw = current_process->dta.dw;
                   6052:                                        opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   6053:                                        opt_len = mem[opt_ofs];
                   6054:                                        memset(opt, 0, sizeof(opt));
                   6055:                                        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   6056:                                }
                   6057:                        }
                   6058:                        for(int i = 0; i < opt_len; i++) {
                   6059:                                if(opt[i] == ' ') {
                   6060:                                        continue;
                   6061:                                }
                   6062:                                if(opt[i] == '/' && (opt[i + 1] == 'c' || opt[i + 1] == 'C') && opt[i + 2] == ' ') {
                   6063:                                        for(int j = i + 3; j < opt_len; j++) {
                   6064:                                                if(opt[j] == ' ') {
                   6065:                                                        continue;
                   6066:                                                }
                   6067:                                                char *token = my_strtok(opt + j, " ");
                   6068:                                                
1.1.1.38  root     6069:                                                strcpy(command, token);
                   6070:                                                char tmp[MAX_PATH];
                   6071:                                                strcpy(tmp, token + strlen(token) + 1);
1.1.1.39  root     6072:                                                strcpy(opt, "");
                   6073:                                                for(int i = 0; i < strlen(tmp); i++) {
                   6074:                                                        if(tmp[i] != ' ') {
                   6075:                                                                strcpy(opt, tmp + i);
                   6076:                                                                break;
                   6077:                                                        }
                   6078:                                                }
                   6079:                                                strcpy(tmp, opt);
1.1.1.38  root     6080:                                                
                   6081:                                                if(al == 0x00) {
1.1.1.39  root     6082:                                                        #define GET_FILE_PATH() { \
                   6083:                                                                if(token[0] != '>' && token[0] != '<') { \
                   6084:                                                                        token++; \
                   6085:                                                                } \
                   6086:                                                                token++; \
                   6087:                                                                while(*token == ' ') { \
                   6088:                                                                        token++; \
                   6089:                                                                } \
                   6090:                                                                char *ptr = token; \
                   6091:                                                                while(*ptr != ' ' && *ptr != '\r' && *ptr != '\0') { \
                   6092:                                                                        ptr++; \
                   6093:                                                                } \
                   6094:                                                                *ptr = '\0'; \
                   6095:                                                        }
                   6096:                                                        if((token = strstr(opt, "0<")) != NULL || (token = strstr(opt, "<")) != NULL) {
                   6097:                                                                GET_FILE_PATH();
1.1.1.38  root     6098:                                                                strcpy(pipe_stdin_path, token);
                   6099:                                                                strcpy(opt, tmp);
                   6100:                                                        }
1.1.1.39  root     6101:                                                        if((token = strstr(opt, "1>")) != NULL || (token = strstr(opt, ">")) != NULL) {
                   6102:                                                                GET_FILE_PATH();
1.1.1.38  root     6103:                                                                strcpy(pipe_stdout_path, token);
                   6104:                                                                strcpy(opt, tmp);
                   6105:                                                        }
1.1.1.39  root     6106:                                                        if((token = strstr(opt, "2>")) != NULL) {
                   6107:                                                                GET_FILE_PATH();
                   6108:                                                                strcpy(pipe_stderr_path, token);
                   6109:                                                                strcpy(opt, tmp);
                   6110:                                                        }
                   6111:                                                        #undef GET_FILE_PATH
                   6112:                                                        
                   6113:                                                        if((token = strstr(opt, "0<")) != NULL) {
                   6114:                                                                *token = '\0';
                   6115:                                                        }
                   6116:                                                        if((token = strstr(opt, "1>")) != NULL) {
                   6117:                                                                *token = '\0';
                   6118:                                                        }
                   6119:                                                        if((token = strstr(opt, "2>")) != NULL) {
                   6120:                                                                *token = '\0';
                   6121:                                                        }
1.1.1.38  root     6122:                                                        if((token = strstr(opt, "<")) != NULL) {
                   6123:                                                                *token = '\0';
                   6124:                                                        }
                   6125:                                                        if((token = strstr(opt, ">")) != NULL) {
                   6126:                                                                *token = '\0';
                   6127:                                                        }
1.1.1.14  root     6128:                                                }
1.1.1.39  root     6129:                                                for(int i = strlen(opt) - 1; i >= 0 && opt[i] == ' '; i--) {
                   6130:                                                        opt[i] = '\0';
                   6131:                                                }
1.1.1.38  root     6132:                                                opt_len = strlen(opt);
                   6133:                                                mem[opt_ofs] = opt_len;
                   6134:                                                sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6135:                                                dos_command = 1;
1.1.1.14  root     6136:                                                break;
1.1       root     6137:                                        }
                   6138:                                }
1.1.1.14  root     6139:                                break;
1.1       root     6140:                        }
                   6141:                }
                   6142:        }
                   6143:        
                   6144:        // load command file
                   6145:        strcpy(path, command);
                   6146:        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   6147:                sprintf(path, "%s.COM", command);
                   6148:                if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   6149:                        sprintf(path, "%s.EXE", command);
                   6150:                        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
1.1.1.14  root     6151:                                sprintf(path, "%s.BAT", command);
                   6152:                                if(_access(path, 0) == 0) {
                   6153:                                        // this is a batch file, run command.com
                   6154:                                        char tmp[MAX_PATH];
                   6155:                                        if(opt_len != 0) {
                   6156:                                                sprintf(tmp, "/C %s %s", path, opt);
                   6157:                                        } else {
                   6158:                                                sprintf(tmp, "/C %s", path);
                   6159:                                        }
                   6160:                                        strcpy(opt, tmp);
                   6161:                                        opt_len = strlen(opt);
                   6162:                                        mem[opt_ofs] = opt_len;
                   6163:                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6164:                                        strcpy(path, comspec_path);
                   6165:                                        strcpy(name_tmp, "COMMAND.COM");
                   6166:                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6167:                                } else {
                   6168:                                        // search path in parent environments
                   6169:                                        psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
1.1.1.45  root     6170:                                        const char *env = msdos_env_get(parent_psp->env_seg, "PATH");
1.1.1.14  root     6171:                                        if(env != NULL) {
                   6172:                                                char env_path[4096];
                   6173:                                                strcpy(env_path, env);
                   6174:                                                char *token = my_strtok(env_path, ";");
                   6175:                                                
                   6176:                                                while(token != NULL) {
                   6177:                                                        if(strlen(token) != 0) {
                   6178:                                                                sprintf(path, "%s", msdos_combine_path(token, command));
                   6179:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6180:                                                                        break;
                   6181:                                                                }
                   6182:                                                                sprintf(path, "%s.COM", msdos_combine_path(token, command));
                   6183:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6184:                                                                        break;
                   6185:                                                                }
                   6186:                                                                sprintf(path, "%s.EXE", msdos_combine_path(token, command));
                   6187:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   6188:                                                                        break;
                   6189:                                                                }
                   6190:                                                                sprintf(path, "%s.BAT", msdos_combine_path(token, command));
                   6191:                                                                if(_access(path, 0) == 0) {
                   6192:                                                                        // this is a batch file, run command.com
                   6193:                                                                        char tmp[MAX_PATH];
                   6194:                                                                        if(opt_len != 0) {
                   6195:                                                                                sprintf(tmp, "/C %s %s", path, opt);
                   6196:                                                                        } else {
                   6197:                                                                                sprintf(tmp, "/C %s", path);
                   6198:                                                                        }
                   6199:                                                                        strcpy(opt, tmp);
                   6200:                                                                        opt_len = strlen(opt);
                   6201:                                                                        mem[opt_ofs] = opt_len;
                   6202:                                                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   6203:                                                                        strcpy(path, comspec_path);
                   6204:                                                                        strcpy(name_tmp, "COMMAND.COM");
                   6205:                                                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6206:                                                                        break;
                   6207:                                                                }
1.1.1.8   root     6208:                                                        }
1.1.1.14  root     6209:                                                        token = my_strtok(NULL, ";");
1.1       root     6210:                                                }
                   6211:                                        }
                   6212:                                }
                   6213:                        }
                   6214:                }
                   6215:        }
                   6216:        if(fd == -1) {
1.1.1.38  root     6217:                // we can not find command.com in the path, so open comspec_path
                   6218:                if(_stricmp(command, "COMMAND.COM") == 0 || _stricmp(command, "COMMAND") == 0) {
                   6219:                        strcpy(command, comspec_path);
                   6220:                        strcpy(path, command);
                   6221:                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6222:                }
                   6223:        }
                   6224:        if(fd == -1) {
1.1.1.52! root     6225:                if(!first_process && al == 0 && dos_command) {
1.1       root     6226:                        // may be dos command
                   6227:                        char tmp[MAX_PATH];
1.1.1.52! root     6228:                        if(opt_len != 0) {
        !          6229:                                sprintf(tmp, "%s %s", command, opt);
        !          6230:                        } else {
        !          6231:                                sprintf(tmp, "%s", command);
        !          6232:                        }
        !          6233:                        retval = system(tmp);
1.1       root     6234:                        return(0);
                   6235:                } else {
                   6236:                        return(-1);
                   6237:                }
                   6238:        }
1.1.1.52! root     6239:        memset(file_buffer, 0, sizeof(file_buffer));
1.1       root     6240:        _read(fd, file_buffer, sizeof(file_buffer));
                   6241:        _close(fd);
                   6242:        
1.1.1.52! root     6243:        // check if this is win32 program
        !          6244:        if(!first_process && al == 0) {
        !          6245:                UINT16 sign_dos = *(UINT16 *)(file_buffer + 0x00);
        !          6246:                UINT32 e_lfanew = *(UINT32 *)(file_buffer + 0x3c);
        !          6247:                if(sign_dos == IMAGE_DOS_SIGNATURE && e_lfanew >= 0x40 && e_lfanew < 0x400) {
        !          6248:                        UINT32 sign_nt = *(UINT32 *)(file_buffer + e_lfanew + 0x00);
        !          6249:                        UINT16 machine = *(UINT16 *)(file_buffer + e_lfanew + 0x04);
        !          6250:                        if(sign_nt == IMAGE_NT_SIGNATURE && (machine == IMAGE_FILE_MACHINE_I386 || machine == IMAGE_FILE_MACHINE_AMD64)) {
        !          6251:                                char tmp[MAX_PATH];
        !          6252:                                if(opt_len != 0) {
        !          6253:                                        sprintf(tmp, "\"%s\" %s", path, opt);
        !          6254:                                } else {
        !          6255:                                        sprintf(tmp, "\"%s\"", path);
        !          6256:                                }
        !          6257:                                retval = system(tmp);
        !          6258:                                return(0);
        !          6259:                        }
        !          6260:                }
        !          6261:        }
        !          6262:        
1.1       root     6263:        // copy environment
1.1.1.29  root     6264:        int umb_linked, env_seg, psp_seg;
1.1       root     6265:        
1.1.1.29  root     6266:        if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   6267:                msdos_mem_unlink_umb();
                   6268:        }
1.1.1.8   root     6269:        if((env_seg = msdos_mem_alloc(first_mcb, ENV_SIZE >> 4, 1)) == -1) {
1.1.1.29  root     6270:                if((env_seg = msdos_mem_alloc(UMB_TOP >> 4, ENV_SIZE >> 4, 1)) == -1) {
                   6271:                        if(umb_linked != 0) {
                   6272:                                msdos_mem_link_umb();
                   6273:                        }
                   6274:                        return(-1);
                   6275:                }
1.1       root     6276:        }
                   6277:        if(param->env_seg == 0) {
                   6278:                psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
                   6279:                memcpy(mem + (env_seg << 4), mem + (parent_psp->env_seg << 4), ENV_SIZE);
                   6280:        } else {
                   6281:                memcpy(mem + (env_seg << 4), mem + (param->env_seg << 4), ENV_SIZE);
                   6282:        }
                   6283:        msdos_env_set_argv(env_seg, msdos_short_full_path(path));
                   6284:        
                   6285:        // check exe header
                   6286:        exe_header_t *header = (exe_header_t *)file_buffer;
1.1.1.8   root     6287:        int paragraphs, free_paragraphs = msdos_mem_get_free(first_mcb, 1);
1.1       root     6288:        UINT16 cs, ss, ip, sp;
                   6289:        
                   6290:        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   6291:                // memory allocation
                   6292:                int header_size = header->header_size * 16;
                   6293:                int load_size = header->pages * 512 - header_size;
                   6294:                if(header_size + load_size < 512) {
                   6295:                        load_size = 512 - header_size;
                   6296:                }
                   6297:                paragraphs = (PSP_SIZE + load_size) >> 4;
                   6298:                if(paragraphs + header->min_alloc > free_paragraphs) {
                   6299:                        msdos_mem_free(env_seg);
                   6300:                        return(-1);
                   6301:                }
                   6302:                paragraphs += header->max_alloc ? header->max_alloc : header->min_alloc;
                   6303:                if(paragraphs > free_paragraphs) {
                   6304:                        paragraphs = free_paragraphs;
                   6305:                }
1.1.1.8   root     6306:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29  root     6307:                        if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
                   6308:                                if(umb_linked != 0) {
                   6309:                                        msdos_mem_link_umb();
                   6310:                                }
                   6311:                                msdos_mem_free(env_seg);
                   6312:                                return(-1);
                   6313:                        }
1.1       root     6314:                }
                   6315:                // relocation
                   6316:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   6317:                for(int i = 0; i < header->relocations; i++) {
                   6318:                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   6319:                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   6320:                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   6321:                }
                   6322:                memcpy(mem + (start_seg << 4), file_buffer + header_size, load_size);
                   6323:                // segments
                   6324:                cs = header->init_cs + start_seg;
                   6325:                ss = header->init_ss + start_seg;
                   6326:                ip = header->init_ip;
                   6327:                sp = header->init_sp - 2; // for symdeb
                   6328:        } else {
                   6329:                // memory allocation
                   6330:                paragraphs = free_paragraphs;
1.1.1.8   root     6331:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29  root     6332:                        if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
                   6333:                                if(umb_linked != 0) {
                   6334:                                        msdos_mem_link_umb();
                   6335:                                }
                   6336:                                msdos_mem_free(env_seg);
                   6337:                                return(-1);
                   6338:                        }
1.1       root     6339:                }
                   6340:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   6341:                memcpy(mem + (start_seg << 4), file_buffer, 0x10000 - PSP_SIZE);
                   6342:                // segments
                   6343:                cs = ss = psp_seg;
                   6344:                ip = 0x100;
                   6345:                sp = 0xfffe;
                   6346:        }
1.1.1.29  root     6347:        if(umb_linked != 0) {
                   6348:                msdos_mem_link_umb();
                   6349:        }
1.1       root     6350:        
                   6351:        // create psp
1.1.1.3   root     6352:        *(UINT16 *)(mem + 4 * 0x22 + 0) = m_eip;
                   6353:        *(UINT16 *)(mem + 4 * 0x22 + 2) = SREG(CS);
1.1       root     6354:        psp_t *psp = msdos_psp_create(psp_seg, psp_seg + paragraphs, current_psp, env_seg);
                   6355:        memcpy(psp->fcb1, mem + (param->fcb1.w.h << 4) + param->fcb1.w.l, sizeof(psp->fcb1));
                   6356:        memcpy(psp->fcb2, mem + (param->fcb2.w.h << 4) + param->fcb2.w.l, sizeof(psp->fcb2));
                   6357:        memcpy(psp->buffer, mem + (param->cmd_line.w.h << 4) + param->cmd_line.w.l, sizeof(psp->buffer));
                   6358:        
                   6359:        mcb_t *mcb_env = (mcb_t *)(mem + ((env_seg - 1) << 4));
                   6360:        mcb_t *mcb_psp = (mcb_t *)(mem + ((psp_seg - 1) << 4));
                   6361:        mcb_psp->psp = mcb_env->psp = psp_seg;
                   6362:        
1.1.1.4   root     6363:        for(int i = 0; i < 8; i++) {
                   6364:                if(name_tmp[i] == '.') {
                   6365:                        mcb_psp->prog_name[i] = '\0';
                   6366:                        break;
                   6367:                } else if(i < 7 && msdos_lead_byte_check(name_tmp[i])) {
                   6368:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6369:                        i++;
                   6370:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6371:                } else if(name_tmp[i] >= 'a' && name_tmp[i] <= 'z') {
                   6372:                        mcb_psp->prog_name[i] = name_tmp[i] - 'a' + 'A';
                   6373:                } else {
                   6374:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6375:                }
                   6376:        }
                   6377:        
1.1       root     6378:        // process info
                   6379:        process_t *process = msdos_process_info_create(psp_seg);
                   6380:        strcpy(process->module_dir, msdos_short_full_dir(path));
1.1.1.33  root     6381: #ifdef USE_DEBUGGER
                   6382:        strcpy(process->module_path, path);
                   6383: #endif
1.1       root     6384:        process->dta.w.l = 0x80;
                   6385:        process->dta.w.h = psp_seg;
                   6386:        process->switchar = '/';
                   6387:        process->max_files = 20;
                   6388:        process->parent_int_10h_feh_called = int_10h_feh_called;
                   6389:        process->parent_int_10h_ffh_called = int_10h_ffh_called;
1.1.1.14  root     6390:        process->parent_ds = SREG(DS);
1.1.1.31  root     6391:        process->parent_es = SREG(ES);
1.1       root     6392:        
                   6393:        current_psp = psp_seg;
1.1.1.23  root     6394:        msdos_sda_update(current_psp);
1.1       root     6395:        
                   6396:        if(al == 0x00) {
                   6397:                int_10h_feh_called = int_10h_ffh_called = false;
                   6398:                
1.1.1.38  root     6399:                // pipe
                   6400:                if(pipe_stdin_path[0] != '\0') {
1.1.1.45  root     6401: //                     if((fd = _open(pipe_stdin_path, _O_RDONLY | _O_BINARY)) != -1) {
                   6402:                        if(msdos_is_device_path(pipe_stdin_path)) {
                   6403:                                fd = msdos_open_device(pipe_stdin_path, _O_RDONLY | _O_BINARY, &sio_port, &lpt_port);
                   6404:                        } else {
                   6405:                                fd = _open(pipe_stdin_path, _O_RDONLY | _O_BINARY);
                   6406:                        }
                   6407:                        if(fd != -1) {
                   6408:                                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     6409:                                psp->file_table[0] = fd;
                   6410:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6411:                        }
                   6412:                }
                   6413:                if(pipe_stdout_path[0] != '\0') {
                   6414:                        if(_access(pipe_stdout_path, 0) == 0) {
                   6415:                                SetFileAttributes(pipe_stdout_path, FILE_ATTRIBUTE_NORMAL);
                   6416:                                DeleteFile(pipe_stdout_path);
                   6417:                        }
1.1.1.45  root     6418: //                     if((fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE)) != -1) {
                   6419:                        if(msdos_is_device_path(pipe_stdout_path)) {
                   6420:                                fd = msdos_open_device(pipe_stdout_path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
                   6421:                        } else {
                   6422:                                fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   6423:                        }
                   6424:                        if(fd != -1) {
                   6425:                                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     6426:                                psp->file_table[1] = fd;
                   6427:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6428:                        }
                   6429:                }
1.1.1.39  root     6430:                if(pipe_stderr_path[0] != '\0') {
                   6431:                        if(_access(pipe_stderr_path, 0) == 0) {
                   6432:                                SetFileAttributes(pipe_stderr_path, FILE_ATTRIBUTE_NORMAL);
                   6433:                                DeleteFile(pipe_stderr_path);
                   6434:                        }
1.1.1.45  root     6435: //                     if((fd = _open(pipe_stderr_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE)) != -1) {
                   6436:                        if(msdos_is_device_path(pipe_stderr_path)) {
                   6437:                                fd = msdos_open_device(pipe_stderr_path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
                   6438:                        } else {
                   6439:                                fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   6440:                        }
                   6441:                        if(fd != -1) {
                   6442:                                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     6443:                                psp->file_table[2] = fd;
                   6444:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6445:                        }
                   6446:                }
1.1.1.38  root     6447:                
1.1       root     6448:                // registers and segments
                   6449:                REG16(AX) = REG16(BX) = 0x00;
                   6450:                REG16(CX) = 0xff;
                   6451:                REG16(DX) = psp_seg;
                   6452:                REG16(SI) = ip;
                   6453:                REG16(DI) = sp;
                   6454:                REG16(SP) = sp;
1.1.1.3   root     6455:                SREG(DS) = SREG(ES) = psp_seg;
                   6456:                SREG(SS) = ss;
                   6457:                i386_load_segment_descriptor(DS);
                   6458:                i386_load_segment_descriptor(ES);
                   6459:                i386_load_segment_descriptor(SS);
1.1       root     6460:                
                   6461:                *(UINT16 *)(mem + (ss << 4) + sp) = 0;
                   6462:                i386_jmp_far(cs, ip);
                   6463:        } else if(al == 0x01) {
                   6464:                // copy ss:sp and cs:ip to param block
                   6465:                param->sp = sp;
                   6466:                param->ss = ss;
                   6467:                param->ip = ip;
                   6468:                param->cs = cs;
1.1.1.31  root     6469:                
                   6470:                // the AX value to be passed to the child program is put on top of the child's stack
                   6471:                *(UINT16 *)(mem + (ss << 4) + sp) = REG16(AX);
1.1       root     6472:        }
                   6473:        return(0);
                   6474: }
                   6475: 
                   6476: void msdos_process_terminate(int psp_seg, int ret, int mem_free)
                   6477: {
                   6478:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6479:        
                   6480:        *(UINT32 *)(mem + 4 * 0x22) = psp->int_22h.dw;
                   6481:        *(UINT32 *)(mem + 4 * 0x23) = psp->int_23h.dw;
                   6482:        *(UINT32 *)(mem + 4 * 0x24) = psp->int_24h.dw;
                   6483:        
1.1.1.3   root     6484:        SREG(SS) = psp->stack.w.h;
                   6485:        i386_load_segment_descriptor(SS);
1.1       root     6486:        REG16(SP) = psp->stack.w.l;
                   6487:        i386_jmp_far(psp->int_22h.w.h, psp->int_22h.w.l);
                   6488:        
1.1.1.28  root     6489: //     process_t *current_process = msdos_process_info_get(psp_seg);
                   6490:        process_t *current_process = NULL;
                   6491:        for(int i = 0; i < MAX_PROCESS; i++) {
                   6492:                if(process[i].psp == psp_seg) {
                   6493:                        current_process = &process[i];
                   6494:                        break;
                   6495:                }
                   6496:        }
                   6497:        if(current_process == NULL) {
                   6498:                throw(0x1f); // general failure
                   6499:        }
                   6500:        int_10h_feh_called = current_process->parent_int_10h_feh_called;
                   6501:        int_10h_ffh_called = current_process->parent_int_10h_ffh_called;
                   6502:        if(current_process->called_by_int2eh) {
                   6503:                REG16(AX) = ret;
                   6504:        }
                   6505:        SREG(DS) = current_process->parent_ds;
1.1.1.31  root     6506:        SREG(ES) = current_process->parent_es;
1.1.1.14  root     6507:        i386_load_segment_descriptor(DS);
1.1.1.31  root     6508:        i386_load_segment_descriptor(ES);
1.1       root     6509:        
                   6510:        if(mem_free) {
1.1.1.8   root     6511:                int mcb_seg;
                   6512:                while((mcb_seg = msdos_mem_get_last_mcb(first_mcb, psp_seg)) != -1) {
                   6513:                        msdos_mem_free(mcb_seg + 1);
                   6514:                }
                   6515:                while((mcb_seg = msdos_mem_get_last_mcb(UMB_TOP >> 4, psp_seg)) != -1) {
                   6516:                        msdos_mem_free(mcb_seg + 1);
                   6517:                }
1.1       root     6518:                
                   6519:                for(int i = 0; i < MAX_FILES; i++) {
                   6520:                        if(file_handler[i].valid && file_handler[i].psp == psp_seg) {
                   6521:                                _close(i);
1.1.1.20  root     6522:                                msdos_file_handler_close(i);
                   6523:                                msdos_psp_set_file_table(i, 0x0ff, psp_seg); // FIXME: consider duplicated file handles
1.1       root     6524:                        }
                   6525:                }
1.1.1.13  root     6526:                msdos_dta_info_free(psp_seg);
1.1       root     6527:        }
1.1.1.14  root     6528:        msdos_stdio_reopen();
1.1       root     6529:        
1.1.1.28  root     6530:        memset(current_process, 0, sizeof(process_t));
1.1       root     6531:        
                   6532:        current_psp = psp->parent_psp;
                   6533:        retval = ret;
1.1.1.23  root     6534:        msdos_sda_update(current_psp);
1.1       root     6535: }
                   6536: 
                   6537: // drive
                   6538: 
1.1.1.42  root     6539: int pcbios_update_drive_param(int drive_num, int force_update);
                   6540: 
1.1       root     6541: int msdos_drive_param_block_update(int drive_num, UINT16 *seg, UINT16 *ofs, int force_update)
                   6542: {
1.1.1.41  root     6543:        if(!(drive_num >= 0 && drive_num < 26)) {
                   6544:                return(0);
                   6545:        }
1.1.1.42  root     6546:        pcbios_update_drive_param(drive_num, force_update);
                   6547:        
                   6548:        drive_param_t *drive_param = &drive_params[drive_num];
1.1       root     6549:        *seg = DPB_TOP >> 4;
                   6550:        *ofs = sizeof(dpb_t) * drive_num;
                   6551:        dpb_t *dpb = (dpb_t *)(mem + (*seg << 4) + *ofs);
                   6552:        
                   6553:        memset(dpb, 0, sizeof(dpb_t));
                   6554:        
1.1.1.41  root     6555:        dpb->drive_num = drive_num;
                   6556:        dpb->unit_num = drive_num;
1.1.1.42  root     6557:        
                   6558:        if(drive_param->valid) {
                   6559:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   6560:                
                   6561:                dpb->bytes_per_sector = (UINT16)geo->BytesPerSector;
                   6562:                dpb->highest_sector_num = (UINT8)(geo->SectorsPerTrack - 1);
                   6563:                dpb->highest_cluster_num = (UINT16)(geo->TracksPerCylinder * geo->Cylinders.QuadPart + 1);
                   6564:                dpb->maximum_cluster_num = (UINT32)(geo->TracksPerCylinder * geo->Cylinders.QuadPart + 1);
                   6565:                switch(geo->MediaType) {
                   6566:                case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   6567:                        dpb->media_type = 0xff;
                   6568:                        break;
                   6569:                case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   6570:                        dpb->media_type = 0xfe;
                   6571:                        break;
                   6572:                case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   6573:                        dpb->media_type = 0xfd;
                   6574:                        break;
                   6575:                case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   6576:                        dpb->media_type = 0xfc;
                   6577:                        break;
                   6578:                case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   6579:                case F3_1Pt2_512:
                   6580:                case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   6581:                case F5_720_512:
                   6582:                        dpb->media_type = 0xf9;
                   6583:                        break;
                   6584:                case FixedMedia:        // hard disk
                   6585:                case RemovableMedia:
                   6586:                case Unknown:
                   6587:                        dpb->media_type = 0xf8;
                   6588:                        break;
                   6589:                default:
                   6590:                        dpb->media_type = 0xf0;
                   6591:                        break;
                   6592:                }
                   6593:        }
1.1.1.41  root     6594:        dpb->next_dpb_ofs = (drive_num == 25) ? 0xffff : *ofs + sizeof(dpb_t);
                   6595:        dpb->next_dpb_seg = (drive_num == 25) ? 0xffff : *seg;
1.1.1.42  root     6596:        dpb->info_sector = 0xffff;
                   6597:        dpb->backup_boot_sector = 0xffff;
                   6598:        dpb->free_clusters = 0xffff;
                   6599:        dpb->free_search_cluster = 0xffffffff;
                   6600:        
                   6601:        return(drive_param->valid);
1.1       root     6602: }
                   6603: 
                   6604: // pc bios
                   6605: 
1.1.1.35  root     6606: #ifdef USE_SERVICE_THREAD
                   6607: void start_service_loop(LPTHREAD_START_ROUTINE lpStartAddress)
                   6608: {
                   6609: #if defined(HAS_I386)
                   6610:        if(m_SF != 0) {
                   6611:                m_SF = 0;
1.1.1.49  root     6612:                mem[DUMMY_TOP + 0x15] = 0x79;   // jns -4
1.1.1.35  root     6613:        } else {
                   6614:                m_SF = 1;
1.1.1.49  root     6615:                mem[DUMMY_TOP + 0x15] = 0x78;   // js -4
1.1.1.35  root     6616:        }
                   6617: #else
                   6618:        if(m_SignVal < 0) {
                   6619:                m_SignVal = 0;
1.1.1.49  root     6620:                mem[DUMMY_TOP + 0x15] = 0x79;   // jns -4
1.1.1.35  root     6621:        } else {
                   6622:                m_SignVal = -1;
1.1.1.49  root     6623:                mem[DUMMY_TOP + 0x15] = 0x78;   // js -4
1.1.1.35  root     6624:        }
                   6625: #endif
1.1.1.49  root     6626:        i386_call_far(DUMMY_TOP >> 4, 0x0013);
1.1.1.35  root     6627:        in_service = true;
                   6628:        service_exit = false;
                   6629:        CloseHandle(CreateThread(NULL, 0, lpStartAddress, NULL, 0, NULL));
                   6630: }
                   6631: 
                   6632: void finish_service_loop()
                   6633: {
                   6634:        if(in_service && service_exit) {
                   6635: #if defined(HAS_I386)
                   6636:                if(m_SF != 0) {
                   6637:                        m_SF = 0;
                   6638:                } else {
                   6639:                        m_SF = 1;
                   6640:                }
                   6641: #else
                   6642:                if(m_SignVal < 0) {
                   6643:                        m_SignVal = 0;
                   6644:                } else {
                   6645:                        m_SignVal = -1;
                   6646:                }
                   6647: #endif
                   6648:                in_service = false;
                   6649:        }
                   6650: }
                   6651: #endif
                   6652: 
1.1.1.19  root     6653: UINT32 get_ticks_since_midnight(UINT32 cur_msec)
                   6654: {
                   6655:        static unsigned __int64 start_msec_since_midnight = 0;
                   6656:        static unsigned __int64 start_msec_since_hostboot = 0;
                   6657:        
                   6658:        if(start_msec_since_midnight == 0) {
                   6659:                SYSTEMTIME time;
                   6660:                GetLocalTime(&time);
                   6661:                start_msec_since_midnight = ((time.wHour * 60 + time.wMinute) * 60 + time.wSecond) * 1000 + time.wMilliseconds;
                   6662:                start_msec_since_hostboot = cur_msec;
                   6663:        }
                   6664:        unsigned __int64 msec = (start_msec_since_midnight + cur_msec - start_msec_since_hostboot) % (24 * 60 * 60 * 1000);
                   6665:        unsigned __int64 tick = msec * 0x1800b0 / (24 * 60 * 60 * 1000);
                   6666:        return (UINT32)tick;
                   6667: }
                   6668: 
                   6669: void pcbios_update_daily_timer_counter(UINT32 cur_msec)
                   6670: {
                   6671:        UINT32 prev_tick = *(UINT32 *)(mem + 0x46c);
                   6672:        UINT32 next_tick = get_ticks_since_midnight(cur_msec);
                   6673:        
                   6674:        if(prev_tick > next_tick) {
                   6675:                mem[0x470] = 1;
                   6676:        }
                   6677:        *(UINT32 *)(mem + 0x46c) = next_tick;
                   6678: }
                   6679: 
1.1.1.14  root     6680: inline void pcbios_irq0()
                   6681: {
                   6682:        //++*(UINT32 *)(mem + 0x46c);
1.1.1.19  root     6683:        pcbios_update_daily_timer_counter(timeGetTime());
1.1.1.14  root     6684: }
                   6685: 
1.1.1.16  root     6686: int pcbios_get_text_vram_address(int page)
1.1       root     6687: {
                   6688:        if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     6689:                return TEXT_VRAM_TOP;
1.1       root     6690:        } else {
1.1.1.14  root     6691:                return TEXT_VRAM_TOP + VIDEO_REGEN * (page % vram_pages);
1.1       root     6692:        }
                   6693: }
                   6694: 
1.1.1.16  root     6695: int pcbios_get_shadow_buffer_address(int page)
1.1       root     6696: {
1.1.1.14  root     6697:        if(!int_10h_feh_called) {
1.1.1.16  root     6698:                return pcbios_get_text_vram_address(page);
1.1.1.14  root     6699:        } else if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     6700:                return SHADOW_BUF_TOP;
                   6701:        } else {
1.1.1.14  root     6702:                return SHADOW_BUF_TOP + VIDEO_REGEN * (page % vram_pages);
1.1.1.8   root     6703:        }
                   6704: }
                   6705: 
1.1.1.16  root     6706: int pcbios_get_shadow_buffer_address(int page, int x, int y)
1.1.1.8   root     6707: {
1.1.1.16  root     6708:        return pcbios_get_shadow_buffer_address(page) + (x + y * scr_width) * 2;
1.1       root     6709: }
                   6710: 
1.1.1.16  root     6711: void pcbios_set_console_size(int width, int height, bool clr_screen)
1.1       root     6712: {
1.1.1.14  root     6713:        // clear the existing screen, not just the new one
                   6714:        int clr_height = max(height, scr_height);
                   6715:        
1.1.1.16  root     6716:        if(scr_width != width || scr_height != height) {
                   6717:                change_console_size(width, height);
1.1.1.14  root     6718:        }
                   6719:        mem[0x462] = 0;
                   6720:        *(UINT16 *)(mem + 0x44e) = 0;
                   6721:        
1.1.1.16  root     6722:        text_vram_top_address = pcbios_get_text_vram_address(0);
                   6723:        text_vram_end_address = text_vram_top_address + width * height * 2;
                   6724:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(0);
                   6725:        shadow_buffer_end_address = shadow_buffer_top_address + width * height * 2;
1.1.1.51  root     6726:        cursor_position_address = 0x450 + mem[0x462] * 2;
1.1       root     6727:        
1.1.1.23  root     6728:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.16  root     6729:        if(clr_screen) {
1.1.1.14  root     6730:                for(int ofs = shadow_buffer_top_address; ofs < shadow_buffer_end_address;) {
                   6731:                        mem[ofs++] = 0x20;
                   6732:                        mem[ofs++] = 0x07;
                   6733:                }
                   6734:                
1.1.1.35  root     6735: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6736:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     6737: #endif
1.1.1.14  root     6738:                for(int y = 0; y < clr_height; y++) {
                   6739:                        for(int x = 0; x < scr_width; x++) {
                   6740:                                SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6741:                                SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1       root     6742:                        }
                   6743:                }
                   6744:                SMALL_RECT rect;
1.1.1.14  root     6745:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + clr_height - 1);
                   6746:                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   6747:                vram_length_char = vram_last_length_char = 0;
                   6748:                vram_length_attr = vram_last_length_attr = 0;
1.1.1.35  root     6749: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6750:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     6751: #endif
1.1       root     6752:        }
1.1.1.14  root     6753:        COORD co;
                   6754:        co.X = 0;
                   6755:        co.Y = scr_top;
                   6756:        SetConsoleCursorPosition(hStdout, co);
                   6757:        cursor_moved = true;
                   6758:        SetConsoleTextAttribute(hStdout, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
1.1       root     6759: }
                   6760: 
1.1.1.36  root     6761: void pcbios_update_cursor_position()
                   6762: {
                   6763:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   6764:        GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
                   6765:        if(!restore_console_on_exit) {
                   6766:                scr_top = csbi.srWindow.Top;
                   6767:        }
                   6768:        mem[0x450 + mem[0x462] * 2] = csbi.dwCursorPosition.X;
                   6769:        mem[0x451 + mem[0x462] * 2] = csbi.dwCursorPosition.Y - scr_top;
                   6770: }
                   6771: 
1.1.1.16  root     6772: inline void pcbios_int_10h_00h()
                   6773: {
                   6774:        switch(REG8(AL) & 0x7f) {
                   6775:        case 0x70: // v-text mode
                   6776:        case 0x71: // extended cga v-text mode
                   6777:                pcbios_set_console_size(scr_width, scr_height, !(REG8(AL) & 0x80));
                   6778:                break;
                   6779:        default:
                   6780:                pcbios_set_console_size(80, 25, !(REG8(AL) & 0x80));
                   6781:                break;
                   6782:        }
                   6783:        if(REG8(AL) & 0x80) {
                   6784:                mem[0x487] |= 0x80;
                   6785:        } else {
                   6786:                mem[0x487] &= ~0x80;
                   6787:        }
                   6788:        mem[0x449] = REG8(AL) & 0x7f;
                   6789: }
                   6790: 
1.1       root     6791: inline void pcbios_int_10h_01h()
                   6792: {
1.1.1.13  root     6793:        mem[0x460] = REG8(CL);
                   6794:        mem[0x461] = REG8(CH);
1.1.1.14  root     6795:        
1.1.1.23  root     6796:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6797:        CONSOLE_CURSOR_INFO ci;
                   6798:        GetConsoleCursorInfo(hStdout, &ci);
                   6799: //     ci.bVisible = !(REG8(CH) & 0x60) && REG8(CH) <= REG8(CL);
                   6800: //     if(ci.bVisible) {
                   6801:                int lines = max(8, REG8(CL) + 1);
                   6802:                ci.dwSize = (REG8(CL) - REG8(CH) + 1) * 100 / lines;
                   6803: //     }
                   6804:        SetConsoleCursorInfo(hStdout, &ci);
1.1       root     6805: }
                   6806: 
                   6807: inline void pcbios_int_10h_02h()
                   6808: {
1.1.1.14  root     6809:        // continuously setting the cursor effectively stops it blinking
                   6810:        if(mem[0x462] == REG8(BH) && (REG8(DL) != mem[0x450 + REG8(BH) * 2] || REG8(DH) != mem[0x451 + REG8(BH) * 2])) {
1.1       root     6811:                COORD co;
                   6812:                co.X = REG8(DL);
1.1.1.14  root     6813:                co.Y = REG8(DH) + scr_top;
                   6814:                
                   6815:                // some programs hide the cursor by moving it off screen
                   6816:                static bool hidden = false;
1.1.1.23  root     6817:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6818:                CONSOLE_CURSOR_INFO ci;
                   6819:                GetConsoleCursorInfo(hStdout, &ci);
                   6820:                
                   6821:                if(REG8(DH) >= scr_height || !SetConsoleCursorPosition(hStdout, co)) {
                   6822:                        if(ci.bVisible) {
                   6823:                                ci.bVisible = FALSE;
                   6824: //                             SetConsoleCursorInfo(hStdout, &ci);
                   6825:                                hidden = true;
                   6826:                        }
                   6827:                } else if(hidden) {
                   6828:                        if(!ci.bVisible) {
                   6829:                                ci.bVisible = TRUE;
                   6830: //                             SetConsoleCursorInfo(hStdout, &ci);
                   6831:                        }
                   6832:                        hidden = false;
                   6833:                }
1.1       root     6834:        }
1.1.1.14  root     6835:        mem[0x450 + (REG8(BH) % vram_pages) * 2] = REG8(DL);
                   6836:        mem[0x451 + (REG8(BH) % vram_pages) * 2] = REG8(DH);
1.1       root     6837: }
                   6838: 
                   6839: inline void pcbios_int_10h_03h()
                   6840: {
1.1.1.14  root     6841:        REG8(DL) = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   6842:        REG8(DH) = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     6843:        REG8(CL) = mem[0x460];
                   6844:        REG8(CH) = mem[0x461];
                   6845: }
                   6846: 
                   6847: inline void pcbios_int_10h_05h()
                   6848: {
1.1.1.14  root     6849:        if(REG8(AL) >= vram_pages) {
                   6850:                return;
                   6851:        }
                   6852:        if(mem[0x462] != REG8(AL)) {
                   6853:                vram_flush();
                   6854:                
1.1.1.23  root     6855:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6856:                SMALL_RECT rect;
1.1.1.14  root     6857:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   6858:                ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     6859:                
1.1.1.16  root     6860:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(mem[0x462]); y < scr_height; y++) {
1.1.1.14  root     6861:                        for(int x = 0; x < scr_width; x++) {
                   6862:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   6863:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     6864:                        }
                   6865:                }
1.1.1.16  root     6866:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(REG8(AL)); y < scr_height; y++) {
1.1.1.14  root     6867:                        for(int x = 0; x < scr_width; x++) {
                   6868:                                SCR_BUF(y,x).Char.AsciiChar = mem[ofs++];
                   6869:                                SCR_BUF(y,x).Attributes = mem[ofs++];
1.1       root     6870:                        }
                   6871:                }
1.1.1.14  root     6872:                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     6873:                
                   6874:                COORD co;
1.1.1.14  root     6875:                co.X = mem[0x450 + REG8(AL) * 2];
                   6876:                co.Y = mem[0x451 + REG8(AL) * 2] + scr_top;
                   6877:                if(co.Y < scr_top + scr_height) {
                   6878:                        SetConsoleCursorPosition(hStdout, co);
                   6879:                }
1.1       root     6880:        }
1.1.1.14  root     6881:        mem[0x462] = REG8(AL);
                   6882:        *(UINT16 *)(mem + 0x44e) = REG8(AL) * VIDEO_REGEN;
                   6883:        int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.16  root     6884:        text_vram_top_address = pcbios_get_text_vram_address(mem[0x462]);
1.1.1.14  root     6885:        text_vram_end_address = text_vram_top_address + regen;
1.1.1.16  root     6886:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(mem[0x462]);
1.1.1.14  root     6887:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
1.1.1.51  root     6888:        cursor_position_address = 0x450 + mem[0x462] * 2;
1.1       root     6889: }
                   6890: 
                   6891: inline void pcbios_int_10h_06h()
                   6892: {
1.1.1.14  root     6893:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   6894:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   6895:                return;
                   6896:        }
                   6897:        vram_flush();
                   6898:        
1.1.1.23  root     6899:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6900:        SMALL_RECT rect;
1.1.1.14  root     6901:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   6902:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   6903:        
                   6904:        int right = min(REG8(DL), scr_width - 1);
                   6905:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     6906:        
                   6907:        if(REG8(AL) == 0) {
1.1.1.14  root     6908:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     6909:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     6910:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6911:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     6912:                        }
                   6913:                }
                   6914:        } else {
1.1.1.14  root     6915:                for(int y = REG8(CH), y2 = min(REG8(CH) + REG8(AL), bottom + 1); y <= bottom; y++, y2++) {
1.1.1.16  root     6916:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     6917:                                if(y2 <= bottom) {
                   6918:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     6919:                                } else {
1.1.1.14  root     6920:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6921:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     6922:                                }
1.1.1.14  root     6923:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   6924:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     6925:                        }
                   6926:                }
                   6927:        }
1.1.1.14  root     6928:        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     6929: }
                   6930: 
                   6931: inline void pcbios_int_10h_07h()
                   6932: {
1.1.1.14  root     6933:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   6934:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   6935:                return;
                   6936:        }
                   6937:        vram_flush();
                   6938:        
1.1.1.23  root     6939:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6940:        SMALL_RECT rect;
1.1.1.14  root     6941:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   6942:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   6943:        
                   6944:        int right = min(REG8(DL), scr_width - 1);
                   6945:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     6946:        
                   6947:        if(REG8(AL) == 0) {
1.1.1.14  root     6948:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     6949:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     6950:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6951:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     6952:                        }
                   6953:                }
                   6954:        } else {
1.1.1.14  root     6955:                for(int y = bottom, y2 = max(REG8(CH) - 1, bottom - REG8(AL)); y >= REG8(CH); y--, y2--) {
1.1.1.16  root     6956:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     6957:                                if(y2 >= REG8(CH)) {
                   6958:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     6959:                                } else {
1.1.1.14  root     6960:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6961:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     6962:                                }
1.1.1.14  root     6963:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   6964:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     6965:                        }
                   6966:                }
                   6967:        }
1.1.1.14  root     6968:        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     6969: }
                   6970: 
                   6971: inline void pcbios_int_10h_08h()
                   6972: {
                   6973:        COORD co;
                   6974:        DWORD num;
                   6975:        
1.1.1.14  root     6976:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   6977:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     6978:        
                   6979:        if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     6980:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6981:                co.Y += scr_top;
                   6982:                vram_flush();
1.1       root     6983:                ReadConsoleOutputCharacter(hStdout, scr_char, 1, co, &num);
                   6984:                ReadConsoleOutputAttribute(hStdout, scr_attr, 1, co, &num);
                   6985:                REG8(AL) = scr_char[0];
                   6986:                REG8(AH) = scr_attr[0];
                   6987:        } else {
1.1.1.16  root     6988:                REG16(AX) = *(UINT16 *)(mem + pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y));
1.1       root     6989:        }
                   6990: }
                   6991: 
                   6992: inline void pcbios_int_10h_09h()
                   6993: {
                   6994:        COORD co;
                   6995:        
1.1.1.14  root     6996:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   6997:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   6998:        
1.1.1.16  root     6999:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   7000:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     7001:        
                   7002:        if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     7003: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7004:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     7005: #endif
1.1.1.16  root     7006:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     7007:                while(dest < end) {
                   7008:                        write_text_vram_char(dest - vram, REG8(AL));
                   7009:                        mem[dest++] = REG8(AL);
                   7010:                        write_text_vram_attr(dest - vram, REG8(BL));
                   7011:                        mem[dest++] = REG8(BL);
1.1       root     7012:                }
1.1.1.35  root     7013: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7014:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     7015: #endif
1.1       root     7016:        } else {
1.1.1.14  root     7017:                while(dest < end) {
1.1       root     7018:                        mem[dest++] = REG8(AL);
                   7019:                        mem[dest++] = REG8(BL);
                   7020:                }
                   7021:        }
                   7022: }
                   7023: 
                   7024: inline void pcbios_int_10h_0ah()
                   7025: {
                   7026:        COORD co;
                   7027:        
1.1.1.14  root     7028:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7029:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   7030:        
1.1.1.16  root     7031:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   7032:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     7033:        
                   7034:        if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     7035: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7036:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     7037: #endif
1.1.1.16  root     7038:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     7039:                while(dest < end) {
                   7040:                        write_text_vram_char(dest - vram, REG8(AL));
                   7041:                        mem[dest++] = REG8(AL);
                   7042:                        dest++;
1.1       root     7043:                }
1.1.1.35  root     7044: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7045:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     7046: #endif
1.1       root     7047:        } else {
1.1.1.14  root     7048:                while(dest < end) {
1.1       root     7049:                        mem[dest++] = REG8(AL);
                   7050:                        dest++;
                   7051:                }
                   7052:        }
                   7053: }
                   7054: 
1.1.1.40  root     7055: HDC get_console_window_device_context()
                   7056: {
                   7057:        static HWND hwndFound = 0;
                   7058:        
                   7059:        if(hwndFound == 0) {
                   7060:                // https://support.microsoft.com/en-us/help/124103/how-to-obtain-a-console-window-handle-hwnd
                   7061:                char pszNewWindowTitle[1024];
                   7062:                char pszOldWindowTitle[1024];
                   7063:                
                   7064:                GetConsoleTitle(pszOldWindowTitle, 1024);
                   7065:                wsprintf(pszNewWindowTitle,"%d/%d", GetTickCount(), GetCurrentProcessId());
                   7066:                SetConsoleTitle(pszNewWindowTitle);
                   7067:                Sleep(100);
                   7068:                hwndFound = FindWindow(NULL, pszNewWindowTitle);
                   7069:                SetConsoleTitle(pszOldWindowTitle);
                   7070:        }
                   7071:        return GetDC(hwndFound);
                   7072: }
                   7073: 
                   7074: inline void pcbios_int_10h_0ch()
                   7075: {
                   7076:        HDC hdc = get_console_window_device_context();
                   7077:        
                   7078:        if(hdc != NULL) {
                   7079:                BYTE r = (REG8(AL) & 2) ? 255 : 0;
                   7080:                BYTE g = (REG8(AL) & 4) ? 255 : 0;
                   7081:                BYTE b = (REG8(AL) & 1) ? 255 : 0;
                   7082:                
                   7083:                if(REG8(AL) & 0x80) {
                   7084:                        COLORREF color = GetPixel(hdc, REG16(CX), REG16(DX));
                   7085:                        if(color != CLR_INVALID) {
                   7086:                                r ^= ((DWORD)color & 0x0000ff) ? 255 : 0;
                   7087:                                g ^= ((DWORD)color & 0x00ff00) ? 255 : 0;
                   7088:                                b ^= ((DWORD)color & 0xff0000) ? 255 : 0;
                   7089:                        }
                   7090:                }
                   7091:                SetPixel(hdc, REG16(CX), REG16(DX), RGB(r, g, b));
                   7092:        }
                   7093: }
                   7094: 
                   7095: inline void pcbios_int_10h_0dh()
                   7096: {
                   7097:        HDC hdc = get_console_window_device_context();
                   7098:        BYTE r = 0;
                   7099:        BYTE g = 0;
                   7100:        BYTE b = 0;
                   7101:        
                   7102:        if(hdc != NULL) {
                   7103:                COLORREF color = GetPixel(hdc, REG16(CX), REG16(DX));
                   7104:                if(color != CLR_INVALID) {
                   7105:                        r = ((DWORD)color & 0x0000ff) ? 255 : 0;
                   7106:                        g = ((DWORD)color & 0x00ff00) ? 255 : 0;
                   7107:                        b = ((DWORD)color & 0xff0000) ? 255 : 0;
                   7108:                }
                   7109:        }
                   7110:        REG8(AL) = ((b != 0) ? 1 : 0) | ((r != 0) ? 2 : 0) | ((g != 0) ? 4 : 0);
                   7111: }
                   7112: 
1.1       root     7113: inline void pcbios_int_10h_0eh()
                   7114: {
1.1.1.14  root     7115:        DWORD num;
                   7116:        COORD co;
                   7117:        
                   7118:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   7119:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   7120:        
                   7121:        if(REG8(AL) == 7) {
                   7122:                //MessageBeep(-1);
                   7123:        } else if(REG8(AL) == 8 || REG8(AL) == 10 || REG8(AL) == 13) {
                   7124:                if(REG8(AL) == 10) {
                   7125:                        vram_flush();
                   7126:                }
1.1.1.23  root     7127:                WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), &REG8(AL), 1, &num, NULL);
1.1.1.14  root     7128:                cursor_moved = true;
                   7129:        } else {
1.1.1.16  root     7130:                int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
1.1.1.14  root     7131:                if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     7132: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7133:                        EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     7134: #endif
1.1.1.16  root     7135:                        int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     7136:                        write_text_vram_char(dest - vram, REG8(AL));
1.1.1.35  root     7137: #ifdef USE_VRAM_THREAD
1.1.1.14  root     7138:                        LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     7139: #endif
1.1.1.14  root     7140:                        
1.1.1.23  root     7141:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     7142:                        if(++co.X == scr_width) {
                   7143:                                co.X = 0;
                   7144:                                if(++co.Y == scr_height) {
                   7145:                                        vram_flush();
                   7146:                                        WriteConsole(hStdout, "\n", 1, &num, NULL);
                   7147:                                        cursor_moved = true;
                   7148:                                }
                   7149:                        }
                   7150:                        if(!cursor_moved) {
                   7151:                                co.Y += scr_top;
                   7152:                                SetConsoleCursorPosition(hStdout, co);
                   7153:                                cursor_moved = true;
                   7154:                        }
                   7155:                }
                   7156:                mem[dest] = REG8(AL);
                   7157:        }
1.1       root     7158: }
                   7159: 
                   7160: inline void pcbios_int_10h_0fh()
                   7161: {
                   7162:        REG8(AL) = mem[0x449];
                   7163:        REG8(AH) = mem[0x44a];
                   7164:        REG8(BH) = mem[0x462];
                   7165: }
                   7166: 
1.1.1.14  root     7167: inline void pcbios_int_10h_11h()
                   7168: {
                   7169:        switch(REG8(AL)) {
1.1.1.16  root     7170:        case 0x01:
1.1.1.14  root     7171:        case 0x11:
1.1.1.16  root     7172:                pcbios_set_console_size(80, 28, true);
1.1.1.14  root     7173:                break;
1.1.1.16  root     7174:        case 0x02:
1.1.1.14  root     7175:        case 0x12:
1.1.1.16  root     7176:                pcbios_set_console_size(80, 50, true);
1.1.1.14  root     7177:                break;
1.1.1.16  root     7178:        case 0x04:
1.1.1.14  root     7179:        case 0x14:
1.1.1.16  root     7180:                pcbios_set_console_size(80, 25, true);
                   7181:                break;
                   7182:        case 0x18:
                   7183:                pcbios_set_console_size(80, 50, true);
1.1.1.14  root     7184:                break;
                   7185:        case 0x30:
                   7186:                SREG(ES) = 0;
                   7187:                i386_load_segment_descriptor(ES);
                   7188:                REG16(BP) = 0;
                   7189:                REG16(CX) = mem[0x485];
                   7190:                REG8(DL) = mem[0x484];
                   7191:                break;
                   7192:        }
                   7193: }
                   7194: 
                   7195: inline void pcbios_int_10h_12h()
                   7196: {
1.1.1.16  root     7197:        switch(REG8(BL)) {
                   7198:        case 0x10:
1.1.1.14  root     7199:                REG16(BX) = 0x0003;
                   7200:                REG16(CX) = 0x0009;
1.1.1.16  root     7201:                break;
1.1.1.14  root     7202:        }
                   7203: }
                   7204: 
1.1       root     7205: inline void pcbios_int_10h_13h()
                   7206: {
1.1.1.3   root     7207:        int ofs = SREG_BASE(ES) + REG16(BP);
1.1       root     7208:        COORD co;
                   7209:        DWORD num;
                   7210:        
                   7211:        co.X = REG8(DL);
1.1.1.14  root     7212:        co.Y = REG8(DH) + scr_top;
                   7213:        
                   7214:        vram_flush();
1.1       root     7215:        
                   7216:        switch(REG8(AL)) {
                   7217:        case 0x00:
                   7218:        case 0x01:
                   7219:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7220:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7221:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   7222:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7223:                        SetConsoleCursorPosition(hStdout, co);
                   7224:                        
                   7225:                        if(csbi.wAttributes != REG8(BL)) {
                   7226:                                SetConsoleTextAttribute(hStdout, REG8(BL));
                   7227:                        }
1.1.1.14  root     7228:                        WriteConsole(hStdout, &mem[ofs], REG16(CX), &num, NULL);
                   7229:                        
1.1       root     7230:                        if(csbi.wAttributes != REG8(BL)) {
                   7231:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   7232:                        }
                   7233:                        if(REG8(AL) == 0x00) {
1.1.1.15  root     7234:                                if(!restore_console_on_exit) {
                   7235:                                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7236:                                        scr_top = csbi.srWindow.Top;
                   7237:                                }
1.1.1.14  root     7238:                                co.X = mem[0x450 + REG8(BH) * 2];
                   7239:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     7240:                                SetConsoleCursorPosition(hStdout, co);
                   7241:                        } else {
                   7242:                                cursor_moved = true;
                   7243:                        }
                   7244:                } else {
1.1.1.3   root     7245:                        m_CF = 1;
1.1       root     7246:                }
                   7247:                break;
                   7248:        case 0x02:
                   7249:        case 0x03:
                   7250:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7251:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7252:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   7253:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   7254:                        SetConsoleCursorPosition(hStdout, co);
                   7255:                        
                   7256:                        WORD wAttributes = csbi.wAttributes;
                   7257:                        for(int i = 0; i < REG16(CX); i++, ofs += 2) {
                   7258:                                if(wAttributes != mem[ofs + 1]) {
                   7259:                                        SetConsoleTextAttribute(hStdout, mem[ofs + 1]);
                   7260:                                        wAttributes = mem[ofs + 1];
                   7261:                                }
1.1.1.14  root     7262:                                WriteConsole(hStdout, &mem[ofs], 1, &num, NULL);
1.1       root     7263:                        }
                   7264:                        if(csbi.wAttributes != wAttributes) {
                   7265:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   7266:                        }
                   7267:                        if(REG8(AL) == 0x02) {
1.1.1.14  root     7268:                                co.X = mem[0x450 + REG8(BH) * 2];
                   7269:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     7270:                                SetConsoleCursorPosition(hStdout, co);
                   7271:                        } else {
                   7272:                                cursor_moved = true;
                   7273:                        }
                   7274:                } else {
1.1.1.3   root     7275:                        m_CF = 1;
1.1       root     7276:                }
                   7277:                break;
                   7278:        case 0x10:
                   7279:        case 0x11:
                   7280:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7281:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7282:                        ReadConsoleOutputCharacter(hStdout, scr_char, REG16(CX), co, &num);
                   7283:                        ReadConsoleOutputAttribute(hStdout, scr_attr, REG16(CX), co, &num);
                   7284:                        for(int i = 0; i < num; i++) {
                   7285:                                mem[ofs++] = scr_char[i];
                   7286:                                mem[ofs++] = scr_attr[i];
1.1.1.45  root     7287:                                if(REG8(AL) & 0x01) {
1.1       root     7288:                                        mem[ofs++] = 0;
                   7289:                                        mem[ofs++] = 0;
                   7290:                                }
                   7291:                        }
                   7292:                } else {
1.1.1.16  root     7293:                        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     7294:                                mem[ofs++] = mem[src++];
                   7295:                                mem[ofs++] = mem[src++];
1.1.1.45  root     7296:                                if(REG8(AL) & 0x01) {
1.1       root     7297:                                        mem[ofs++] = 0;
                   7298:                                        mem[ofs++] = 0;
                   7299:                                }
1.1.1.14  root     7300:                                if(++co.X == scr_width) {
                   7301:                                        if(++co.Y == scr_height) {
1.1       root     7302:                                                break;
                   7303:                                        }
                   7304:                                        co.X = 0;
                   7305:                                }
                   7306:                        }
                   7307:                }
                   7308:                break;
1.1.1.45  root     7309:        case 0x12: // ???
                   7310:        case 0x13: // ???
1.1       root     7311:        case 0x20:
                   7312:        case 0x21:
                   7313:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7314:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     7315:                        int len = min(REG16(CX), scr_width * scr_height);
                   7316:                        for(int i = 0; i < len; i++) {
1.1       root     7317:                                scr_char[i] = mem[ofs++];
                   7318:                                scr_attr[i] = mem[ofs++];
1.1.1.45  root     7319:                                if(REG8(AL) & 0x01) {
1.1       root     7320:                                        ofs += 2;
                   7321:                                }
                   7322:                        }
1.1.1.14  root     7323:                        WriteConsoleOutputCharacter(hStdout, scr_char, len, co, &num);
                   7324:                        WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     7325:                } else {
1.1.1.16  root     7326:                        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     7327:                                mem[dest++] = mem[ofs++];
                   7328:                                mem[dest++] = mem[ofs++];
1.1.1.45  root     7329:                                if(REG8(AL) & 0x01) {
1.1       root     7330:                                        ofs += 2;
                   7331:                                }
1.1.1.14  root     7332:                                if(++co.X == scr_width) {
                   7333:                                        if(++co.Y == scr_height) {
1.1       root     7334:                                                break;
                   7335:                                        }
                   7336:                                        co.X = 0;
                   7337:                                }
                   7338:                        }
                   7339:                }
                   7340:                break;
                   7341:        default:
1.1.1.22  root     7342:                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     7343:                m_CF = 1;
1.1       root     7344:                break;
                   7345:        }
                   7346: }
                   7347: 
1.1.1.30  root     7348: inline void pcbios_int_10h_18h()
                   7349: {
                   7350:        switch(REG8(AL)) {
                   7351:        case 0x00:
                   7352:        case 0x01:
                   7353: //             REG8(AL) = 0x86;
                   7354:                REG8(AL) = 0x00;
                   7355:                break;
                   7356:        default:
                   7357:                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));
                   7358:                m_CF = 1;
                   7359:                break;
                   7360:        }
                   7361: }
                   7362: 
1.1.1.14  root     7363: inline void pcbios_int_10h_1ah()
                   7364: {
                   7365:        switch(REG8(AL)) {
                   7366:        case 0x00:
                   7367:                REG8(AL) = 0x1a;
                   7368:                REG8(BL) = 0x08;
                   7369:                REG8(BH) = 0x00;
                   7370:                break;
                   7371:        default:
1.1.1.22  root     7372:                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     7373:                m_CF = 1;
                   7374:                break;
                   7375:        }
                   7376: }
                   7377: 
1.1       root     7378: inline void pcbios_int_10h_1dh()
                   7379: {
                   7380:        switch(REG8(AL)) {
1.1.1.43  root     7381:        case 0x00:
                   7382:                // DOS/V Shift Status Line Control is not supported
                   7383:                m_CF = 1;
                   7384:                break;
1.1       root     7385:        case 0x01:
                   7386:                break;
                   7387:        case 0x02:
                   7388:                REG16(BX) = 0;
                   7389:                break;
                   7390:        default:
1.1.1.22  root     7391:                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));
                   7392:                m_CF = 1;
                   7393:                break;
                   7394:        }
                   7395: }
                   7396: 
                   7397: inline void pcbios_int_10h_4fh()
                   7398: {
                   7399:        switch(REG8(AL)) {
                   7400:        case 0x00:
                   7401:                REG8(AH) = 0x02; // not supported
                   7402:                break;
                   7403:        case 0x01:
                   7404:        case 0x02:
                   7405:        case 0x03:
                   7406:        case 0x04:
                   7407:        case 0x05:
                   7408:        case 0x06:
                   7409:        case 0x07:
                   7410:        case 0x08:
                   7411:        case 0x09:
                   7412:        case 0x0a:
                   7413:        case 0x0b:
                   7414:        case 0x0c:
                   7415:                REG8(AH) = 0x01; // failed
                   7416:                break;
                   7417:        default:
                   7418:                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     7419:                m_CF = 1;
1.1       root     7420:                break;
                   7421:        }
                   7422: }
                   7423: 
                   7424: inline void pcbios_int_10h_82h()
                   7425: {
                   7426:        static UINT8 mode = 0;
                   7427:        
                   7428:        switch(REG8(AL)) {
1.1.1.22  root     7429:        case 0x00:
1.1       root     7430:                if(REG8(BL) != 0xff) {
                   7431:                        mode = REG8(BL);
                   7432:                }
                   7433:                REG8(AL) = mode;
                   7434:                break;
                   7435:        default:
1.1.1.22  root     7436:                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     7437:                m_CF = 1;
1.1       root     7438:                break;
                   7439:        }
                   7440: }
                   7441: 
1.1.1.22  root     7442: inline void pcbios_int_10h_83h()
                   7443: {
                   7444:        static UINT8 mode = 0;
                   7445:        
                   7446:        switch(REG8(AL)) {
                   7447:        case 0x00:
                   7448:                REG16(AX) = 0; // offset???
                   7449:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
                   7450:                i386_load_segment_descriptor(ES);
                   7451:                REG16(BX) = (SHADOW_BUF_TOP & 0x0f);
                   7452:                break;
                   7453:        default:
                   7454:                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));
                   7455:                m_CF = 1;
                   7456:                break;
                   7457:        }
                   7458: }
                   7459: 
                   7460: inline void pcbios_int_10h_90h()
                   7461: {
                   7462:        REG8(AL) = mem[0x449];
                   7463: }
                   7464: 
                   7465: inline void pcbios_int_10h_91h()
                   7466: {
                   7467:        REG8(AL) = 0x04; // VGA
                   7468: }
                   7469: 
                   7470: inline void pcbios_int_10h_efh()
                   7471: {
                   7472:        REG16(DX) = 0xffff;
                   7473: }
                   7474: 
1.1       root     7475: inline void pcbios_int_10h_feh()
                   7476: {
                   7477:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     7478:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
1.1.1.3   root     7479:                i386_load_segment_descriptor(ES);
1.1.1.8   root     7480:                REG16(DI) = (SHADOW_BUF_TOP & 0x0f);
1.1       root     7481:        }
                   7482:        int_10h_feh_called = true;
                   7483: }
                   7484: 
                   7485: inline void pcbios_int_10h_ffh()
                   7486: {
                   7487:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.23  root     7488:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7489:                COORD co;
                   7490:                DWORD num;
                   7491:                
1.1.1.14  root     7492:                vram_flush();
                   7493:                
                   7494:                co.X = (REG16(DI) >> 1) % scr_width;
                   7495:                co.Y = (REG16(DI) >> 1) / scr_width;
1.1.1.16  root     7496:                int ofs = pcbios_get_shadow_buffer_address(0, co.X, co.Y);
                   7497:                int end = min(ofs + REG16(CX) * 2, pcbios_get_shadow_buffer_address(0, 0, scr_height));
1.1.1.14  root     7498:                int len;
                   7499:                for(len = 0; ofs < end; len++) {
                   7500:                        scr_char[len] = mem[ofs++];
                   7501:                        scr_attr[len] = mem[ofs++];
                   7502:                }
                   7503:                co.Y += scr_top;
                   7504:                WriteConsoleOutputCharacter(hStdout, scr_char, len, co, &num);
                   7505:                WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     7506:        }
                   7507:        int_10h_ffh_called = true;
                   7508: }
                   7509: 
1.1.1.42  root     7510: int pcbios_update_drive_param(int drive_num, int force_update)
                   7511: {
                   7512:        if(drive_num >= 0 && drive_num < 26) {
                   7513:                drive_param_t *drive_param = &drive_params[drive_num];
                   7514:                
                   7515:                if(force_update || !drive_param->initialized) {
                   7516:                        drive_param->valid = 0;
                   7517:                        char dev[64];
                   7518:                        sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7519:                        
                   7520:                        HANDLE hFile = CreateFile(dev, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   7521:                        if(hFile != INVALID_HANDLE_VALUE) {
                   7522:                                DWORD dwSize;
                   7523:                                if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &drive_param->geometry, sizeof(DISK_GEOMETRY), &dwSize, NULL)) {
                   7524:                                        drive_param->valid = 1;
                   7525:                                }
                   7526:                                CloseHandle(hFile);
                   7527:                        }
                   7528:                        drive_param->initialized = 1;
                   7529:                }
                   7530:                return(drive_param->valid);
                   7531:        }
                   7532:        return(0);
                   7533: }
                   7534: 
                   7535: inline void pcbios_int_13h_00h()
                   7536: {
                   7537:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7538:        
                   7539:        if(pcbios_update_drive_param(drive_num, 1)) {
                   7540:                REG8(AH) = 0x00; // successful completion
                   7541:        } else {
                   7542:                if(REG8(DL) & 0x80) {
                   7543:                        REG8(AH) = 0x05; // reset failed (hard disk)
                   7544:                } else {
                   7545:                        REG8(AH) = 0x80; // timeout (not ready)
                   7546:                }
                   7547:                m_CF = 1;
                   7548:        }
                   7549: }
                   7550: 
                   7551: inline void pcbios_int_13h_02h()
                   7552: {
                   7553:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7554:        
                   7555:        if(REG8(AL) == 0) {
                   7556:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   7557:                m_CF = 1;
                   7558:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   7559:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7560:                m_CF = 1;
                   7561:        } else {
                   7562:                drive_param_t *drive_param = &drive_params[drive_num];
                   7563:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   7564:                char dev[64];
                   7565:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7566:                
                   7567:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   7568:                if(hFile == INVALID_HANDLE_VALUE) {
                   7569:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7570:                        m_CF = 1;
                   7571:                } else {
                   7572:                        UINT32 sector_num = REG8(AL);
                   7573:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   7574:                        UINT32 head = REG8(DH);
                   7575:                        UINT32 sector = REG8(CL) & 0x3f;
                   7576:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   7577:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   7578:                        DWORD dwSize;
                   7579:                        
                   7580: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   7581: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7582: //                             m_CF = 1;
                   7583: //                     } else 
                   7584:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   7585:                                REG8(AH) = 0x04; // sector not found/read error
                   7586:                                m_CF = 1;
                   7587:                        } else if(ReadFile(hFile, mem + buffer_addr, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   7588:                                REG8(AH) = 0x04; // sector not found/read error
                   7589:                                m_CF = 1;
                   7590:                        } else {
                   7591:                                REG8(AH) = 0x00; // successful completion
                   7592:                        }
                   7593:                        CloseHandle(hFile);
                   7594:                }
                   7595:        }
                   7596: }
                   7597: 
                   7598: inline void pcbios_int_13h_03h()
                   7599: {
                   7600:        // this operation may cause serious damage for drives, so support only floppy disk...
                   7601:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7602:        
                   7603:        if(REG8(AL) == 0) {
                   7604:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   7605:                m_CF = 1;
                   7606:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   7607:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7608:                m_CF = 1;
                   7609:        } else if(!drive_params[drive_num].is_fdd()) {
                   7610:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7611:                m_CF = 1;
                   7612:        } else {
                   7613:                drive_param_t *drive_param = &drive_params[drive_num];
                   7614:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   7615:                char dev[64];
                   7616:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7617:                
                   7618:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   7619:                if(hFile == INVALID_HANDLE_VALUE) {
                   7620:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7621:                        m_CF = 1;
                   7622:                } else {
                   7623:                        UINT32 sector_num = REG8(AL);
                   7624:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   7625:                        UINT32 head = REG8(DH);
                   7626:                        UINT32 sector = REG8(CL) & 0x3f;
                   7627:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   7628:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   7629:                        DWORD dwSize;
                   7630:                        
                   7631: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   7632: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7633: //                             m_CF = 1;
                   7634: //                     } else 
                   7635:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   7636:                                REG8(AH) = 0x04; // sector not found/read error
                   7637:                                m_CF = 1;
                   7638:                        } else if(WriteFile(hFile, mem + buffer_addr, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   7639:                                REG8(AH) = 0x04; // sector not found/read error
                   7640:                                m_CF = 1;
                   7641:                        } else {
                   7642:                                REG8(AH) = 0x00; // successful completion
                   7643:                        }
                   7644:                        CloseHandle(hFile);
                   7645:                }
                   7646:        }
                   7647: }
                   7648: 
                   7649: inline void pcbios_int_13h_04h()
                   7650: {
                   7651:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7652:        
                   7653:        if(REG8(AL) == 0) {
                   7654:                REG8(AH) = 0x01; // invalid function in AH or invalid parameter
                   7655:                m_CF = 1;
                   7656:        } else if(!pcbios_update_drive_param(drive_num, 0)) {
                   7657:                REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7658:                m_CF = 1;
                   7659:        } else {
                   7660:                drive_param_t *drive_param = &drive_params[drive_num];
                   7661:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   7662:                char dev[64];
                   7663:                sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   7664:                
                   7665:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   7666:                if(hFile == INVALID_HANDLE_VALUE) {
                   7667:                        REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7668:                        m_CF = 1;
                   7669:                } else {
                   7670:                        UINT32 sector_num = REG8(AL);
                   7671:                        UINT32 cylinder = REG8(CH) | ((REG8(CL) & 0xc0) << 2);
                   7672:                        UINT32 head = REG8(DH);
                   7673:                        UINT32 sector = REG8(CL) & 0x3f;
                   7674:                        UINT32 top_sector = ((cylinder * drive_param->head_num() + head) * geo->SectorsPerTrack) + sector - 1;
                   7675:                        UINT32 buffer_addr = SREG_BASE(ES) + REG16(BX);
                   7676:                        DWORD dwSize;
                   7677:                        UINT8 *tmp_buffer = (UINT8 *)malloc(sector_num * geo->BytesPerSector);
                   7678:                        
                   7679: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   7680: //                             REG8(AH) = 0xff; // sense operation failed (hard disk)
                   7681: //                             m_CF = 1;
                   7682: //                     } else 
                   7683:                        if(SetFilePointer(hFile, top_sector * geo->BytesPerSector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   7684:                                REG8(AH) = 0x04; // sector not found/read error
                   7685:                                m_CF = 1;
                   7686:                        } else if(ReadFile(hFile, tmp_buffer, sector_num * geo->BytesPerSector, &dwSize, NULL) == 0) {
                   7687:                                REG8(AH) = 0x04; // sector not found/read error
                   7688:                                m_CF = 1;
                   7689:                        } else if(memcmp(mem + buffer_addr, tmp_buffer, sector_num * geo->BytesPerSector) != 0) {
                   7690:                                REG8(AH) = 0x05; // data did not verify correctly (TI Professional PC)
                   7691:                                m_CF = 1;
                   7692:                        } else {
                   7693:                                REG8(AH) = 0x00; // successful completion
                   7694:                        }
                   7695:                        free(tmp_buffer);
                   7696:                        CloseHandle(hFile);
                   7697:                }
                   7698:        }
                   7699: }
                   7700: 
                   7701: inline void pcbios_int_13h_08h()
                   7702: {
                   7703:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7704:        
                   7705:        if(pcbios_update_drive_param(drive_num, 1)) {
                   7706:                drive_param_t *drive_param = &drive_params[drive_num];
                   7707:                DISK_GEOMETRY *geo = &drive_param->geometry;
                   7708:                
                   7709:                REG16(AX) = 0x0000;
                   7710:                switch(geo->MediaType) {
                   7711:                case F5_360_512:
                   7712:                case F5_320_512:
                   7713:                case F5_320_1024:
                   7714:                case F5_180_512:
                   7715:                case F5_160_512:
                   7716:                        REG8(BL) = 0x01; // 320K/360K disk
                   7717:                        break;
                   7718:                case F5_1Pt2_512:
                   7719:                case F3_1Pt2_512:
                   7720:                case F3_1Pt23_1024:
                   7721:                case F5_1Pt23_1024:
                   7722:                        REG8(BL) = 0x02; // 1.2M disk
                   7723:                        break;
                   7724:                case F3_720_512:
                   7725:                case F3_640_512:
                   7726:                case F5_640_512:
                   7727:                case F5_720_512:
                   7728:                        REG8(BL) = 0x03; // 720K disk
                   7729:                        break;
                   7730:                case F3_1Pt44_512:
                   7731:                        REG8(BL) = 0x04; // 1.44M disk
                   7732:                        break;
                   7733:                case F3_2Pt88_512:
                   7734:                        REG8(BL) = 0x06; // 2.88M disk
                   7735:                        break;
                   7736:                case RemovableMedia:
                   7737:                        REG8(BL) = 0x10; // ATAPI Removable Media Device
                   7738:                        break;
                   7739:                default:
                   7740:                        REG8(BL) = 0x00; // unknown
                   7741:                        break;
                   7742:                }
                   7743:                if(REG8(DL) & 0x80) {
                   7744:                        switch(GetLogicalDrives() & 0x0c) {
                   7745:                        case 0x00: REG8(DL) = 0x00; break;
                   7746:                        case 0x04:
                   7747:                        case 0x08: REG8(DL) = 0x01; break;
                   7748:                        case 0x0c: REG8(DL) = 0x02; break;
                   7749:                        }
                   7750:                } else {
                   7751:                        switch(GetLogicalDrives() & 0x03) {
                   7752:                        case 0x00: REG8(DL) = 0x00; break;
                   7753:                        case 0x01:
                   7754:                        case 0x02: REG8(DL) = 0x01; break;
                   7755:                        case 0x03: REG8(DL) = 0x02; break;
                   7756:                        }
                   7757:                }
                   7758:                REG8(DH) = drive_param->head_num();
                   7759:                int cyl = (geo->Cylinders.QuadPart > 0x3ff) ? 0x3ff : geo->Cylinders.QuadPart;
                   7760:                int sec = (geo->SectorsPerTrack > 0x3f) ? 0x3f : geo->SectorsPerTrack;
                   7761:                REG8(CH) = cyl & 0xff;
                   7762:                REG8(CL) = (sec & 0x3f) | ((cyl & 0x300) >> 2);
                   7763:        } else {
                   7764:                REG8(AH) = 0x07;
                   7765:                m_CF = 1;
                   7766:        }
                   7767: }
                   7768: 
                   7769: inline void pcbios_int_13h_10h()
                   7770: {
                   7771:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7772:        
                   7773:        if(pcbios_update_drive_param(drive_num, 1)) {
                   7774:                REG8(AH) = 0x00; // successful completion
                   7775:        } else {
                   7776:                if(REG8(DL) & 0x80) {
                   7777:                        REG8(AH) = 0xaa; // drive not ready (hard disk)
                   7778:                } else {
                   7779:                        REG8(AH) = 0x80; // timeout (not ready)
                   7780:                }
                   7781:                m_CF = 1;
                   7782:        }
                   7783: }
                   7784: 
                   7785: inline void pcbios_int_13h_15h()
                   7786: {
                   7787:        int drive_num = (REG8(DL) & 0x80) ? ((REG8(DL) & 0x7f) + 2) : (REG8(DL) < 2) ? REG8(DL) : -1;
                   7788:        
                   7789:        if(pcbios_update_drive_param(drive_num, 1)) {
                   7790:                if(REG8(DL) & 0x80) {
                   7791:                        REG8(AH) = 0x02; // floppy (or other removable drive) with change-line support
                   7792:                } else {
                   7793:                        REG8(AH) = 0x03; // hard disk
                   7794:                }
                   7795:        } else {
                   7796:                REG8(AH) = 0x00; // no such drive
                   7797:        }
                   7798: }
                   7799: 
1.1.1.43  root     7800: inline void pcbios_int_13h_41h()
                   7801: {
                   7802:        if(REG16(BX) == 0x55aa) {
                   7803:                // IBM/MS INT 13 Extensions is not installed
                   7804:                REG8(AH) = 0x01;
                   7805:                m_CF = 1;
                   7806:        } else {
                   7807:                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));
                   7808:                REG8(AH) = 0x01;
                   7809:                m_CF = 1;
                   7810:        }
                   7811: }
                   7812: 
1.1.1.25  root     7813: inline void pcbios_int_14h_00h()
                   7814: {
1.1.1.29  root     7815:        if(REG16(DX) < 4) {
1.1.1.25  root     7816:                static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600};
                   7817:                UINT8 selector = sio_read(REG16(DX), 3);
                   7818:                selector &= ~0x3f;
                   7819:                selector |= REG8(AL) & 0x1f;
                   7820:                UINT16 divisor = 115200 / rate[REG8(AL) >> 5];
                   7821:                sio_write(REG16(DX), 3, selector | 0x80);
                   7822:                sio_write(REG16(DX), 0, divisor & 0xff);
                   7823:                sio_write(REG16(DX), 1, divisor >> 8);
                   7824:                sio_write(REG16(DX), 3, selector);
                   7825:                REG8(AH) = sio_read(REG16(DX), 5);
                   7826:                REG8(AL) = sio_read(REG16(DX), 6);
                   7827:        } else {
                   7828:                REG8(AH) = 0x80;
                   7829:        }
                   7830: }
                   7831: 
                   7832: inline void pcbios_int_14h_01h()
                   7833: {
1.1.1.29  root     7834:        if(REG16(DX) < 4) {
1.1.1.25  root     7835:                UINT8 selector = sio_read(REG16(DX), 3);
                   7836:                sio_write(REG16(DX), 3, selector & ~0x80);
                   7837:                sio_write(REG16(DX), 0, REG8(AL));
                   7838:                sio_write(REG16(DX), 3, selector);
                   7839:                REG8(AH) = sio_read(REG16(DX), 5);
                   7840:        } else {
                   7841:                REG8(AH) = 0x80;
                   7842:        }
                   7843: }
                   7844: 
                   7845: inline void pcbios_int_14h_02h()
                   7846: {
1.1.1.29  root     7847:        if(REG16(DX) < 4) {
1.1.1.25  root     7848:                UINT8 selector = sio_read(REG16(DX), 3);
                   7849:                sio_write(REG16(DX), 3, selector & ~0x80);
                   7850:                REG8(AL) = sio_read(REG16(DX), 0);
                   7851:                sio_write(REG16(DX), 3, selector);
                   7852:                REG8(AH) = sio_read(REG16(DX), 5);
                   7853:        } else {
                   7854:                REG8(AH) = 0x80;
                   7855:        }
                   7856: }
                   7857: 
                   7858: inline void pcbios_int_14h_03h()
                   7859: {
1.1.1.29  root     7860:        if(REG16(DX) < 4) {
1.1.1.25  root     7861:                REG8(AH) = sio_read(REG16(DX), 5);
                   7862:                REG8(AL) = sio_read(REG16(DX), 6);
                   7863:        } else {
                   7864:                REG8(AH) = 0x80;
                   7865:        }
                   7866: }
                   7867: 
                   7868: inline void pcbios_int_14h_04h()
                   7869: {
1.1.1.29  root     7870:        if(REG16(DX) < 4) {
1.1.1.25  root     7871:                UINT8 selector = sio_read(REG16(DX), 3);
                   7872:                if(REG8(CH) <= 0x03) {
                   7873:                        selector = (selector & ~0x03) | REG8(CH);
                   7874:                }
                   7875:                if(REG8(BL) == 0x00) {
                   7876:                        selector &= ~0x04;
                   7877:                } else if(REG8(BL) == 0x01) {
                   7878:                        selector |= 0x04;
                   7879:                }
                   7880:                if(REG8(BH) == 0x00) {
                   7881:                        selector = (selector & ~0x38) | 0x00;
                   7882:                } else if(REG8(BH) == 0x01) {
                   7883:                        selector = (selector & ~0x38) | 0x08;
                   7884:                } else if(REG8(BH) == 0x02) {
                   7885:                        selector = (selector & ~0x38) | 0x18;
                   7886:                } else if(REG8(BH) == 0x03) {
                   7887:                        selector = (selector & ~0x38) | 0x28;
                   7888:                } else if(REG8(BH) == 0x04) {
                   7889:                        selector = (selector & ~0x38) | 0x38;
                   7890:                }
                   7891:                if(REG8(AL) == 0x00) {
                   7892:                        selector |= 0x40;
                   7893:                } else if(REG8(AL) == 0x01) {
                   7894:                        selector &= ~0x40;
                   7895:                }
                   7896:                if(REG8(CL) <= 0x0b) {
                   7897:                        static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200};
                   7898:                        UINT16 divisor = 115200 / rate[REG8(CL)];
                   7899:                        sio_write(REG16(DX), 3, selector | 0x80);
                   7900:                        sio_write(REG16(DX), 0, divisor & 0xff);
                   7901:                        sio_write(REG16(DX), 1, divisor >> 8);
                   7902:                }
                   7903:                sio_write(REG16(DX), 3, selector);
                   7904:                REG8(AH) = sio_read(REG16(DX), 5);
                   7905:                REG8(AL) = sio_read(REG16(DX), 6);
                   7906:        } else {
                   7907:                REG8(AH) = 0x80;
                   7908:        }
                   7909: }
                   7910: 
                   7911: inline void pcbios_int_14h_05h()
                   7912: {
1.1.1.29  root     7913:        if(REG16(DX) < 4) {
1.1.1.25  root     7914:                if(REG8(AL) == 0x00) {
                   7915:                        REG8(BL) = sio_read(REG16(DX), 4);
                   7916:                        REG8(AH) = sio_read(REG16(DX), 5);
                   7917:                        REG8(AL) = sio_read(REG16(DX), 6);
                   7918:                } else if(REG8(AL) == 0x01) {
                   7919:                        sio_write(REG16(DX), 4, REG8(BL));
                   7920:                        REG8(AH) = sio_read(REG16(DX), 5);
                   7921:                        REG8(AL) = sio_read(REG16(DX), 6);
                   7922:                } else {
                   7923:                        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));
                   7924:                }
                   7925:        } else {
                   7926:                REG8(AH) = 0x80;
                   7927:        }
                   7928: }
                   7929: 
1.1.1.14  root     7930: inline void pcbios_int_15h_10h()
                   7931: {
1.1.1.22  root     7932:        switch(REG8(AL)) {
                   7933:        case 0x00:
1.1.1.14  root     7934:                Sleep(10);
1.1.1.35  root     7935:                REQUEST_HARDWRE_UPDATE();
1.1.1.22  root     7936:                break;
                   7937:        default:
                   7938:                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     7939:                REG8(AH) = 0x86;
                   7940:                m_CF = 1;
                   7941:        }
                   7942: }
                   7943: 
1.1       root     7944: inline void pcbios_int_15h_23h()
                   7945: {
                   7946:        switch(REG8(AL)) {
1.1.1.22  root     7947:        case 0x00:
1.1.1.8   root     7948:                REG8(CL) = cmos_read(0x2d);
                   7949:                REG8(CH) = cmos_read(0x2e);
1.1       root     7950:                break;
1.1.1.22  root     7951:        case 0x01:
1.1.1.8   root     7952:                cmos_write(0x2d, REG8(CL));
                   7953:                cmos_write(0x2e, REG8(CH));
1.1       root     7954:                break;
                   7955:        default:
1.1.1.22  root     7956:                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     7957:                REG8(AH) = 0x86;
1.1.1.3   root     7958:                m_CF = 1;
1.1       root     7959:                break;
                   7960:        }
                   7961: }
                   7962: 
                   7963: inline void pcbios_int_15h_24h()
                   7964: {
                   7965:        switch(REG8(AL)) {
1.1.1.22  root     7966:        case 0x00:
1.1.1.3   root     7967:                i386_set_a20_line(0);
1.1       root     7968:                REG8(AH) = 0;
                   7969:                break;
1.1.1.22  root     7970:        case 0x01:
1.1.1.3   root     7971:                i386_set_a20_line(1);
1.1       root     7972:                REG8(AH) = 0;
                   7973:                break;
1.1.1.22  root     7974:        case 0x02:
1.1       root     7975:                REG8(AH) = 0;
1.1.1.3   root     7976:                REG8(AL) = (m_a20_mask >> 20) & 1;
1.1       root     7977:                REG16(CX) = 0;
                   7978:                break;
1.1.1.22  root     7979:        case 0x03:
1.1       root     7980:                REG16(AX) = 0;
                   7981:                REG16(BX) = 0;
                   7982:                break;
1.1.1.22  root     7983:        default:
                   7984:                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));
                   7985:                REG8(AH) = 0x86;
                   7986:                m_CF = 1;
                   7987:                break;
1.1       root     7988:        }
                   7989: }
                   7990: 
                   7991: inline void pcbios_int_15h_49h()
                   7992: {
1.1.1.27  root     7993:        REG8(AH) = 0x00;
                   7994:        REG8(BL) = 0x00; // DOS/V
1.1       root     7995: }
                   7996: 
1.1.1.22  root     7997: inline void pcbios_int_15h_50h()
                   7998: {
                   7999:        switch(REG8(AL)) {
                   8000:        case 0x00:
                   8001:        case 0x01:
                   8002:                if(REG8(BH) != 0x00 && REG8(BH) != 0x01) {
                   8003:                        REG8(AH) = 0x01; // invalid font type in bh
                   8004:                        m_CF = 1;
1.1.1.27  root     8005:                } else if(REG8(BL) != 0x00) {
1.1.1.22  root     8006:                        REG8(AH) = 0x02; // bl not zero
                   8007:                        m_CF = 1;
                   8008:                } else if(REG16(BP) != 0 && REG16(BP) != 437 && REG16(BP) != 932 && REG16(BP) != 934 && REG16(BP) != 936 && REG16(BP) != 938) {
                   8009:                        REG8(AH) = 0x04; // invalid code page
                   8010:                        m_CF = 1;
1.1.1.27  root     8011:                } else if(REG8(AL) == 0x01) {
                   8012:                        REG8(AH) = 0x06; // font is read only
1.1.1.22  root     8013:                        m_CF = 1;
1.1.1.27  root     8014:                } else {
1.1.1.49  root     8015:                        // dummy font read routine is at fffc:000d
                   8016:                        SREG(ES) = DUMMY_TOP >> 4;
1.1.1.27  root     8017:                        i386_load_segment_descriptor(ES);
1.1.1.32  root     8018:                        REG16(BX) = 0x000d;
1.1.1.27  root     8019:                        REG8(AH) = 0x00; // success
1.1.1.22  root     8020:                }
                   8021:                break;
                   8022:        default:
                   8023:                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));
                   8024:                REG8(AH) = 0x86;
                   8025:                m_CF = 1;
                   8026:                break;
                   8027:        }
                   8028: }
                   8029: 
1.1.1.30  root     8030: inline void pcbios_int_15h_53h()
                   8031: {
                   8032:        switch(REG8(AL)) {
                   8033:        case 0x00:
                   8034:                // APM is not installed
                   8035:                REG8(AH) = 0x86;
                   8036:                m_CF = 1;
                   8037:                break;
                   8038:        default:
                   8039:                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));
                   8040:                REG8(AH) = 0x86;
                   8041:                m_CF = 1;
                   8042:                break;
                   8043:        }
                   8044: }
                   8045: 
1.1.1.43  root     8046: inline void pcbios_int_15h_84h()
                   8047: {
                   8048:        // joystick support (from DOSBox)
                   8049:        switch(REG16(DX)) {
                   8050:        case 0x00:
                   8051:                REG16(AX) = 0x00f0;
                   8052:                REG16(DX) = 0x0201;
                   8053:                m_CF = 1;
                   8054:                break;
                   8055:        case 0x01:
                   8056:                REG16(AX) = REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   8057:                m_CF = 1;
                   8058:                break;
                   8059:        default:
                   8060:                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));
                   8061:                REG8(AH) = 0x86;
                   8062:                m_CF = 1;
                   8063:                break;
                   8064:        }
                   8065: }
1.1.1.35  root     8066: 
                   8067: DWORD WINAPI pcbios_int_15h_86h_thread(LPVOID)
1.1       root     8068: {
                   8069:        UINT32 usec = (REG16(CX) << 16) | REG16(DX);
1.1.1.14  root     8070:        UINT32 msec = usec / 1000;
                   8071:        
1.1.1.35  root     8072:        while(msec && !m_halted) {
1.1.1.14  root     8073:                UINT32 tmp = min(msec, 100);
                   8074:                if(msec - tmp < 10) {
                   8075:                        tmp = msec;
                   8076:                }
                   8077:                Sleep(tmp);
                   8078:                msec -= tmp;
                   8079:        }
1.1.1.35  root     8080:        
                   8081: #ifdef USE_SERVICE_THREAD
                   8082:        service_exit = true;
                   8083: #endif
                   8084:        return(0);
                   8085: }
                   8086: 
                   8087: inline void pcbios_int_15h_86h()
                   8088: {
                   8089:        if(!(REG16(CX) == 0 && REG16(DX) == 0)) {
                   8090: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     8091:                if(!in_service && !in_service_29h) {
                   8092:                        start_service_loop(pcbios_int_15h_86h_thread);
                   8093:                } else {
                   8094: #endif
                   8095:                        pcbios_int_15h_86h_thread(NULL);
                   8096:                        REQUEST_HARDWRE_UPDATE();
                   8097: #ifdef USE_SERVICE_THREAD
                   8098:                }
1.1.1.35  root     8099: #endif
                   8100:        }
1.1       root     8101: }
                   8102: 
                   8103: inline void pcbios_int_15h_87h()
                   8104: {
                   8105:        // copy extended memory (from DOSBox)
                   8106:        int len = REG16(CX) * 2;
1.1.1.3   root     8107:        int ofs = SREG_BASE(ES) + REG16(SI);
1.1       root     8108:        int src = (*(UINT32 *)(mem + ofs + 0x12) & 0xffffff); // + (mem[ofs + 0x16] << 24);
                   8109:        int dst = (*(UINT32 *)(mem + ofs + 0x1a) & 0xffffff); // + (mem[ofs + 0x1e] << 24);
                   8110:        memcpy(mem + dst, mem + src, len);
                   8111:        REG16(AX) = 0x00;
                   8112: }
                   8113: 
                   8114: inline void pcbios_int_15h_88h()
                   8115: {
1.1.1.17  root     8116:        REG16(AX) = ((min(MAX_MEM, 0x4000000) - 0x100000) >> 10);
1.1       root     8117: }
                   8118: 
                   8119: inline void pcbios_int_15h_89h()
                   8120: {
1.1.1.21  root     8121: #if defined(HAS_I286) || defined(HAS_I386)
1.1       root     8122:        // switch to protected mode (from DOSBox)
                   8123:        write_io_byte(0x20, 0x10);
                   8124:        write_io_byte(0x21, REG8(BH));
                   8125:        write_io_byte(0x21, 0x00);
                   8126:        write_io_byte(0xa0, 0x10);
                   8127:        write_io_byte(0xa1, REG8(BL));
                   8128:        write_io_byte(0xa1, 0x00);
1.1.1.3   root     8129:        i386_set_a20_line(1);
                   8130:        int ofs = SREG_BASE(ES) + REG16(SI);
                   8131:        m_gdtr.limit = *(UINT16 *)(mem + ofs + 0x08);
                   8132:        m_gdtr.base = *(UINT32 *)(mem + ofs + 0x08 + 0x02) & 0xffffff;
                   8133:        m_idtr.limit = *(UINT16 *)(mem + ofs + 0x10);
                   8134:        m_idtr.base = *(UINT32 *)(mem + ofs + 0x10 + 0x02) & 0xffffff;
                   8135: #if defined(HAS_I386)
                   8136:        m_cr[0] |= 1;
                   8137: #else
                   8138:        m_msw |= 1;
                   8139: #endif
                   8140:        SREG(DS) = 0x18;
                   8141:        SREG(ES) = 0x20;
                   8142:        SREG(SS) = 0x28;
                   8143:        i386_load_segment_descriptor(DS);
                   8144:        i386_load_segment_descriptor(ES);
                   8145:        i386_load_segment_descriptor(SS);
1.1.1.21  root     8146:        UINT16 offset = *(UINT16 *)(mem + SREG_BASE(SS) + REG16(SP));
1.1       root     8147:        REG16(SP) += 6;
1.1.1.3   root     8148: #if defined(HAS_I386)
1.1.1.21  root     8149:        UINT32 flags = get_flags();
                   8150:        flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
                   8151:        set_flags(flags);
1.1.1.3   root     8152: #else
1.1.1.21  root     8153:        UINT32 flags = CompressFlags();
                   8154:        flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
                   8155:        ExpandFlags(flags);
1.1.1.3   root     8156: #endif
1.1       root     8157:        REG16(AX) = 0x00;
1.1.1.21  root     8158:        i386_jmp_far(0x30, /*REG16(CX)*/offset);
1.1       root     8159: #else
1.1.1.21  root     8160:        // i86/i186/v30: protected mode is not supported
1.1       root     8161:        REG8(AH) = 0x86;
1.1.1.3   root     8162:        m_CF = 1;
1.1       root     8163: #endif
                   8164: }
                   8165: 
1.1.1.21  root     8166: inline void pcbios_int_15h_8ah()
                   8167: {
                   8168:        UINT32 size = MAX_MEM - 0x100000;
                   8169:        REG16(AX) = size & 0xffff;
                   8170:        REG16(DX) = size >> 16;
                   8171: }
                   8172: 
1.1.1.3   root     8173: #if defined(HAS_I386)
1.1       root     8174: inline void pcbios_int_15h_c9h()
                   8175: {
                   8176:        REG8(AH) = 0x00;
                   8177:        REG8(CH) = cpu_type;
                   8178:        REG8(CL) = cpu_step;
                   8179: }
1.1.1.3   root     8180: #endif
1.1       root     8181: 
                   8182: inline void pcbios_int_15h_cah()
                   8183: {
                   8184:        switch(REG8(AL)) {
1.1.1.22  root     8185:        case 0x00:
1.1       root     8186:                if(REG8(BL) > 0x3f) {
                   8187:                        REG8(AH) = 0x03;
1.1.1.3   root     8188:                        m_CF = 1;
1.1       root     8189:                } else if(REG8(BL) < 0x0e) {
                   8190:                        REG8(AH) = 0x04;
1.1.1.3   root     8191:                        m_CF = 1;
1.1       root     8192:                } else {
1.1.1.8   root     8193:                        REG8(CL) = cmos_read(REG8(BL));
1.1       root     8194:                }
                   8195:                break;
1.1.1.22  root     8196:        case 0x01:
1.1       root     8197:                if(REG8(BL) > 0x3f) {
                   8198:                        REG8(AH) = 0x03;
1.1.1.3   root     8199:                        m_CF = 1;
1.1       root     8200:                } else if(REG8(BL) < 0x0e) {
                   8201:                        REG8(AH) = 0x04;
1.1.1.3   root     8202:                        m_CF = 1;
1.1       root     8203:                } else {
1.1.1.8   root     8204:                        cmos_write(REG8(BL), REG8(CL));
1.1       root     8205:                }
                   8206:                break;
                   8207:        default:
1.1.1.22  root     8208:                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     8209:                REG8(AH) = 0x86;
1.1.1.3   root     8210:                m_CF = 1;
1.1       root     8211:                break;
                   8212:        }
                   8213: }
                   8214: 
1.1.1.22  root     8215: inline void pcbios_int_15h_e8h()
1.1.1.17  root     8216: {
1.1.1.22  root     8217:        switch(REG8(AL)) {
                   8218: #if defined(HAS_I386)
                   8219:        case 0x01:
                   8220:                REG16(AX) = REG16(CX) = ((min(MAX_MEM, 0x1000000) - 0x100000) >> 10);
                   8221:                REG16(BX) = REG16(DX) = ((max(MAX_MEM, 0x1000000) - 0x1000000) >> 16);
                   8222:                break;
1.1.1.17  root     8223: #endif
1.1.1.22  root     8224:        default:
                   8225:                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));
                   8226:                REG8(AH) = 0x86;
                   8227:                m_CF = 1;
                   8228:                break;
                   8229:        }
                   8230: }
1.1.1.17  root     8231: 
1.1.1.51  root     8232: void pcbios_clear_key_buffer()
                   8233: {
                   8234:        key_buf_char->clear();
                   8235:        key_buf_scan->clear();
                   8236:        
                   8237:        // update key buffer
                   8238:        *(UINT16 *)(mem + 0x41a) = *(UINT16 *)(mem + 0x41c); // head = tail
                   8239: }
                   8240: 
                   8241: void pcbios_set_key_buffer(int key_char, int key_scan)
                   8242: {
                   8243:        key_buf_char->write(key_char);
                   8244:        key_buf_scan->write(key_scan);
                   8245:        
                   8246:        // update key buffer
                   8247:        UINT16 head = *(UINT16 *)(mem + 0x41a);
                   8248:        UINT16 tail = *(UINT16 *)(mem + 0x41c);
                   8249:        UINT16 next = tail + 2;
                   8250:        if(next >= *(UINT16 *)(mem + 0x482)) {
                   8251:                next = *(UINT16 *)(mem + 0x480);
                   8252:        }
                   8253:        if(next != head) {
                   8254:                *(UINT16 *)(mem + 0x41c) = next;
                   8255:                mem[0x400 + (tail++)] = key_char;
                   8256:                mem[0x400 + (tail++)] = key_scan;
                   8257:        }
                   8258: }
                   8259: 
                   8260: bool pcbios_get_key_buffer(int *key_char, int *key_scan)
                   8261: {
                   8262:        if(key_buf_char->empty()) {
                   8263:                *key_char = 0x00;
                   8264:                *key_scan = 0x00;
                   8265:                return(false);
                   8266:        }
                   8267:        *key_char = key_buf_char->read();
                   8268:        *key_scan = key_buf_scan->read();
                   8269:        
                   8270:        // update key buffer
                   8271:        UINT16 head = *(UINT16 *)(mem + 0x41a);
                   8272:        UINT16 tail = *(UINT16 *)(mem + 0x41c);
                   8273:        UINT16 next = head + 2;
                   8274:        if(next >= *(UINT16 *)(mem + 0x482)) {
                   8275:                next = *(UINT16 *)(mem + 0x480);
                   8276:        }
                   8277:        if(head != tail) {
                   8278:                *(UINT16 *)(mem + 0x41a) = next;
                   8279: //             *key_char = mem[0x400 + (head++)];
                   8280: //             *key_scan = mem[0x400 + (head++)];
                   8281:        }
                   8282:        return(true);
                   8283: }
                   8284: 
1.1.1.33  root     8285: void pcbios_update_key_code(bool wait)
1.1       root     8286: {
1.1.1.32  root     8287:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8288: #ifdef USE_SERVICE_THREAD
                   8289:                EnterCriticalSection(&key_buf_crit_sect);
                   8290: #endif
                   8291:                bool empty = key_buf_char->empty();
                   8292: #ifdef USE_SERVICE_THREAD
                   8293:                LeaveCriticalSection(&key_buf_crit_sect);
                   8294: #endif
                   8295:                if(empty) {
1.1.1.32  root     8296:                        if(!update_key_buffer()) {
1.1.1.33  root     8297:                                if(wait) {
1.1.1.32  root     8298:                                        Sleep(10);
                   8299:                                } else {
                   8300:                                        maybe_idle();
                   8301:                                }
1.1.1.14  root     8302:                        }
                   8303:                }
1.1.1.34  root     8304:        }
                   8305:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8306: #ifdef USE_SERVICE_THREAD
                   8307:                EnterCriticalSection(&key_buf_crit_sect);
                   8308: #endif
1.1.1.51  root     8309:                int key_char, key_scan;
                   8310:                if(pcbios_get_key_buffer(&key_char, &key_scan)) {
1.1.1.41  root     8311:                        key_code  = key_char << 0;
                   8312:                        key_code |= key_scan << 8;
1.1.1.35  root     8313:                        key_recv  = 0x0000ffff;
1.1.1.51  root     8314:                }
                   8315:                if(pcbios_get_key_buffer(&key_char, &key_scan)) {
1.1.1.41  root     8316:                        key_code |= key_char << 16;
                   8317:                        key_code |= key_scan << 24;
1.1.1.33  root     8318:                        key_recv |= 0xffff0000;
1.1.1.32  root     8319:                }
1.1.1.35  root     8320: #ifdef USE_SERVICE_THREAD
                   8321:                LeaveCriticalSection(&key_buf_crit_sect);
                   8322: #endif
1.1       root     8323:        }
                   8324: }
                   8325: 
1.1.1.35  root     8326: DWORD WINAPI pcbios_int_16h_00h_thread(LPVOID)
1.1       root     8327: {
1.1.1.33  root     8328:        while(key_recv == 0 && !m_halted) {
                   8329:                pcbios_update_key_code(true);
1.1       root     8330:        }
1.1.1.33  root     8331:        if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
                   8332:                if((key_code & 0xffff) == 0x0000 || (key_code & 0xffff) == 0xe000) {
                   8333:                        if(REG8(AH) == 0x10) {
                   8334:                                key_code = ((key_code >> 8) & 0xff) | ((key_code >> 16) & 0xff00);
                   8335:                        } else {
                   8336:                                key_code = ((key_code >> 16) & 0xff00);
                   8337:                        }
                   8338:                        key_recv >>= 16;
1.1       root     8339:                }
                   8340:        }
                   8341:        REG16(AX) = key_code & 0xffff;
                   8342:        key_code >>= 16;
1.1.1.33  root     8343:        key_recv >>= 16;
1.1.1.35  root     8344:        
                   8345: #ifdef USE_SERVICE_THREAD
                   8346:        service_exit = true;
                   8347: #endif
                   8348:        return(0);
                   8349: }
                   8350: 
                   8351: inline void pcbios_int_16h_00h()
                   8352: {
                   8353: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     8354:        if(!in_service && !in_service_29h) {
                   8355:                start_service_loop(pcbios_int_16h_00h_thread);
                   8356:        } else {
                   8357: #endif
                   8358:                pcbios_int_16h_00h_thread(NULL);
                   8359:                REQUEST_HARDWRE_UPDATE();
                   8360: #ifdef USE_SERVICE_THREAD
                   8361:        }
1.1.1.35  root     8362: #endif
1.1       root     8363: }
                   8364: 
                   8365: inline void pcbios_int_16h_01h()
                   8366: {
1.1.1.33  root     8367:        if(key_recv == 0) {
                   8368:                pcbios_update_key_code(false);
1.1.1.5   root     8369:        }
1.1.1.33  root     8370:        if(key_recv != 0) {
                   8371:                UINT32 key_code_tmp = key_code;
                   8372:                if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
                   8373:                        if((key_code_tmp & 0xffff) == 0x0000 || (key_code_tmp & 0xffff) == 0xe000) {
                   8374:                                if(REG8(AH) == 0x11) {
                   8375:                                        key_code_tmp = ((key_code_tmp >> 8) & 0xff) | ((key_code_tmp >> 16) & 0xff00);
                   8376:                                } else {
                   8377:                                        key_code_tmp = ((key_code_tmp >> 16) & 0xff00);
                   8378:                                }
                   8379:                        }
1.1       root     8380:                }
1.1.1.5   root     8381:                REG16(AX) = key_code_tmp & 0xffff;
1.1.1.3   root     8382: #if defined(HAS_I386)
1.1.1.33  root     8383:                m_ZF = 0;
                   8384: #else
                   8385:                m_ZeroVal = 1;
                   8386: #endif
                   8387:        } else {
                   8388: #if defined(HAS_I386)
                   8389:                m_ZF = 1;
1.1.1.3   root     8390: #else
1.1.1.33  root     8391:                m_ZeroVal = 0;
1.1.1.3   root     8392: #endif
1.1.1.33  root     8393:        }
1.1       root     8394: }
                   8395: 
                   8396: inline void pcbios_int_16h_02h()
                   8397: {
                   8398:        REG8(AL)  = (GetAsyncKeyState(VK_INSERT ) & 0x0001) ? 0x80 : 0;
                   8399:        REG8(AL) |= (GetAsyncKeyState(VK_CAPITAL) & 0x0001) ? 0x40 : 0;
                   8400:        REG8(AL) |= (GetAsyncKeyState(VK_NUMLOCK) & 0x0001) ? 0x20 : 0;
                   8401:        REG8(AL) |= (GetAsyncKeyState(VK_SCROLL ) & 0x0001) ? 0x10 : 0;
                   8402:        REG8(AL) |= (GetAsyncKeyState(VK_MENU   ) & 0x8000) ? 0x08 : 0;
                   8403:        REG8(AL) |= (GetAsyncKeyState(VK_CONTROL) & 0x8000) ? 0x04 : 0;
                   8404:        REG8(AL) |= (GetAsyncKeyState(VK_LSHIFT ) & 0x8000) ? 0x02 : 0;
                   8405:        REG8(AL) |= (GetAsyncKeyState(VK_RSHIFT ) & 0x8000) ? 0x01 : 0;
                   8406: }
                   8407: 
                   8408: inline void pcbios_int_16h_03h()
                   8409: {
                   8410:        static UINT16 status = 0;
                   8411:        
                   8412:        switch(REG8(AL)) {
                   8413:        case 0x05:
                   8414:                status = REG16(BX);
                   8415:                break;
                   8416:        case 0x06:
                   8417:                REG16(BX) = status;
                   8418:                break;
                   8419:        default:
1.1.1.3   root     8420:                m_CF = 1;
1.1       root     8421:                break;
                   8422:        }
                   8423: }
                   8424: 
                   8425: inline void pcbios_int_16h_05h()
                   8426: {
1.1.1.32  root     8427:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     8428: #ifdef USE_SERVICE_THREAD
                   8429:                EnterCriticalSection(&key_buf_crit_sect);
                   8430: #endif
1.1.1.51  root     8431:                pcbios_set_key_buffer(REG8(CL), REG8(CH));
1.1.1.35  root     8432: #ifdef USE_SERVICE_THREAD
                   8433:                LeaveCriticalSection(&key_buf_crit_sect);
                   8434: #endif
1.1.1.32  root     8435:        }
1.1       root     8436:        REG8(AL) = 0x00;
                   8437: }
                   8438: 
                   8439: inline void pcbios_int_16h_12h()
                   8440: {
                   8441:        pcbios_int_16h_02h();
                   8442:        
                   8443:        REG8(AH)  = 0;//(GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x80 : 0;
                   8444:        REG8(AH) |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   8445:        REG8(AH) |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   8446:        REG8(AH) |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
                   8447:        REG8(AH) |= (GetAsyncKeyState(VK_RMENU   ) & 0x8000) ? 0x08 : 0;
                   8448:        REG8(AH) |= (GetAsyncKeyState(VK_RCONTROL) & 0x8000) ? 0x04 : 0;
                   8449:        REG8(AH) |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   8450:        REG8(AH) |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   8451: }
                   8452: 
                   8453: inline void pcbios_int_16h_13h()
                   8454: {
                   8455:        static UINT16 status = 0;
                   8456:        
                   8457:        switch(REG8(AL)) {
                   8458:        case 0x00:
                   8459:                status = REG16(DX);
                   8460:                break;
                   8461:        case 0x01:
                   8462:                REG16(DX) = status;
                   8463:                break;
                   8464:        default:
1.1.1.22  root     8465:                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     8466:                m_CF = 1;
1.1       root     8467:                break;
                   8468:        }
                   8469: }
                   8470: 
                   8471: inline void pcbios_int_16h_14h()
                   8472: {
                   8473:        static UINT8 status = 0;
                   8474:        
                   8475:        switch(REG8(AL)) {
                   8476:        case 0x00:
                   8477:        case 0x01:
                   8478:                status = REG8(AL);
                   8479:                break;
                   8480:        case 0x02:
                   8481:                REG8(AL) = status;
                   8482:                break;
                   8483:        default:
1.1.1.22  root     8484:                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     8485:                m_CF = 1;
1.1       root     8486:                break;
                   8487:        }
                   8488: }
                   8489: 
1.1.1.24  root     8490: inline void pcbios_int_16h_55h()
                   8491: {
                   8492:        switch(REG8(AL)) {
                   8493:        case 0x00:
                   8494:                // keyboard tsr is not present
                   8495:                break;
                   8496:        case 0xfe:
                   8497:                // handlers for INT 08, INT 09, INT 16, INT 1B, and INT 1C are installed
                   8498:                break;
                   8499:        case 0xff:
                   8500:                break;
                   8501:        default:
                   8502:                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));
                   8503:                m_CF = 1;
                   8504:                break;
                   8505:        }
                   8506: }
                   8507: 
1.1.1.30  root     8508: inline void pcbios_int_16h_6fh()
                   8509: {
                   8510:        switch(REG8(AL)) {
                   8511:        case 0x00:
                   8512:                // HP Vectra EX-BIOS - "F16_INQUIRE" - Extended BIOS is not installed
                   8513:                break;
                   8514:        default:
                   8515:                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));
                   8516:                m_CF = 1;
                   8517:                break;
                   8518:        }
                   8519: }
                   8520: 
1.1.1.37  root     8521: UINT16 pcbios_printer_jis2sjis(UINT16 jis)
                   8522: {
                   8523:        UINT8 hi = jis >> 8;
                   8524:        UINT8 lo = jis & 0xff;
                   8525:        
                   8526:        lo = (hi & 0x01) ? lo - 0x1f : lo + 0x7d;
                   8527:        hi = (hi - 0x21) / 2 + 0x81;
                   8528:        hi = (hi >= 0xa0) ? hi + 0x40 : hi;
                   8529:        lo = (lo >= 0x7f) ? lo + 0x01 : lo;
                   8530:        
                   8531:        return((hi << 8) + lo);
                   8532: }
                   8533: 
                   8534: UINT16 pcbios_printer_sjis2jis(UINT16 sjis)
                   8535: {
                   8536:        UINT8 hi = sjis >> 8;
                   8537:        UINT8 lo = sjis & 0xff;
                   8538:        
                   8539:        if(hi == 0x80 || (hi >= 0xeb && hi <= 0xef) || (hi >= 0xf4 && hi <= 0xff)) {
                   8540:                return(0x2121);
                   8541:        }
                   8542:        if((lo >= 0x00 && lo <= 0x3f) || lo == 0x7f || (lo >= 0xfd && lo <= 0xff)) {
                   8543:                return(0x2121);
                   8544:        }
                   8545:        if(hi >= 0xf0 && hi <= 0xf3) {
                   8546:                // gaiji
                   8547:                if(lo >= 0x40 && lo <= 0x7e) {
                   8548:                        return(0x7721 + lo - 0x40 + (hi - 0xf0) * 0x200);
                   8549:                }
                   8550:                if(lo >= 0x80 && lo <= 0x9e) {
                   8551:                        return(0x7760 + lo - 0x80 + (hi - 0xf0) * 0x200);
                   8552:                }
                   8553:                if(lo >= 0x9f && lo <= 0xfc) {
                   8554:                        return(0x7821 + lo - 0x9f + (hi - 0xf0) * 0x200);
                   8555:                }
                   8556:        }
                   8557:        hi = (hi >= 0xe0) ? hi - 0x40 : hi;
                   8558:        lo = (lo >= 0x80) ? lo - 0x01 : lo;
                   8559:        hi = ((lo >= 0x9e) ? 1 : 0) + (hi - 0x81) * 2 + 0x21;
                   8560:        lo = ((lo >= 0x9e) ? lo - 0x9e : lo - 0x40) + 0x21;
                   8561:        
                   8562:        return((hi << 8) + lo);
                   8563: }
                   8564: 
1.1.1.38  root     8565: // AX�e�N�j�J�����t�@�����X�K�C�h 1989
                   8566: // �t�^10 ���{��g��PRINTER DRIVER NIOS�T�d�l
                   8567: // 6. �R���g���[���R�[�h�̉��
1.1.1.37  root     8568: 
                   8569: void pcbios_printer_out(int c, UINT8 data)
                   8570: {
                   8571:        if(pio[c].conv_mode) {
                   8572:                if(pio[c].sjis_hi != 0) {
                   8573:                        if(!pio[c].jis_mode) {
                   8574:                                printer_out(c, 0x1c);
                   8575:                                printer_out(c, 0x26);
                   8576:                                pio[c].jis_mode = true;
                   8577:                        }
                   8578:                        UINT16 jis = pcbios_printer_sjis2jis((pio[c].sjis_hi << 8) | data);
                   8579:                        printer_out(c, jis >> 8);
                   8580:                        printer_out(c, jis & 0xff);
                   8581:                        pio[c].sjis_hi = 0;
                   8582:                } else if(pio[c].esc_buf[0] == 0x1b) {
                   8583:                        printer_out(c, data);
                   8584:                        if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
                   8585:                                pio[c].esc_buf[pio[c].esc_len] = data;
                   8586:                        }
                   8587:                        pio[c].esc_len++;
                   8588:                        
                   8589:                        switch(pio[c].esc_buf[1]) {
1.1.1.38  root     8590:                        case 0x33: // 1Bh 33h XX
                   8591:                        case 0x4a: // 1Bh 4Ah XX
                   8592:                        case 0x4e: // 1Bh 4Eh XX
                   8593:                        case 0x51: // 1Bh 51h XX
                   8594:                        case 0x55: // 1Bh 55h XX
                   8595:                        case 0x6c: // 1Bh 6Ch XX
                   8596:                        case 0x71: // 1Bh 71h XX
                   8597:                        case 0x72: // 1Bh 72h XX
1.1.1.37  root     8598:                                if(pio[c].esc_len == 3) {
                   8599:                                        pio[c].esc_buf[0] = 0x00;
                   8600:                                }
                   8601:                                break;
1.1.1.38  root     8602:                        case 0x24: // 1Bh 24h XX XX
                   8603:                        case 0x5c: // 1Bh 5Ch XX XX
1.1.1.37  root     8604:                                if(pio[c].esc_len == 4) {
                   8605:                                        pio[c].esc_buf[0] = 0x00;
                   8606:                                }
                   8607:                                break;
1.1.1.38  root     8608:                        case 0x2a: // 1Bh 2Ah XX XX XX data
1.1.1.37  root     8609:                                if(pio[c].esc_len >= 3) {
                   8610:                                        switch(pio[c].esc_buf[2]) {
                   8611:                                        case 0: case 1: case 2: case 3: case 4: case 6:
                   8612:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 1) {
                   8613:                                                        pio[c].esc_buf[0] = 0x00;
                   8614:                                                }
                   8615:                                                break;
                   8616:                                        case 32: case 33: case 38: case 39: case 40:
                   8617:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 3) {
                   8618:                                                        pio[c].esc_buf[0] = 0x00;
                   8619:                                                }
                   8620:                                                break;
1.1.1.38  root     8621:                                        case 71: case 72: case 73:
                   8622:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 6) {
                   8623:                                                        pio[c].esc_buf[0] = 0x00;
                   8624:                                                }
                   8625:                                                break;
1.1.1.37  root     8626:                                        default:
                   8627:                                                pio[c].esc_buf[0] = 0x00;
                   8628:                                                break;
                   8629:                                        }
                   8630:                                }
                   8631:                                break;
1.1.1.38  root     8632:                        case 0x40: // 1Bh 40h
1.1.1.37  root     8633:                                if(pio[c].jis_mode) {
                   8634:                                        printer_out(c, 0x1c);
                   8635:                                        printer_out(c, 0x2e);
                   8636:                                        pio[c].jis_mode = false;
                   8637:                                }
                   8638:                                pio[c].esc_buf[0] = 0x00;
                   8639:                                break;
1.1.1.38  root     8640:                        case 0x42: // 1Bh 42h data 00h
                   8641:                        case 0x44: // 1Bh 44h data 00h
1.1.1.37  root     8642:                                if(pio[c].esc_len >= 3 && data == 0) {
                   8643:                                        pio[c].esc_buf[0] = 0x00;
                   8644:                                }
                   8645:                                break;
1.1.1.38  root     8646:                        case 0x43: // 1Bh 43h (00h) XX
1.1.1.37  root     8647:                                if(pio[c].esc_len >= 3 && data != 0) {
                   8648:                                        pio[c].esc_buf[0] = 0x00;
                   8649:                                }
                   8650:                                break;
1.1.1.38  root     8651:                        default: // 1Bh XX
1.1.1.37  root     8652:                                pio[c].esc_buf[0] = 0x00;
                   8653:                                break;
                   8654:                        }
                   8655:                } else if(pio[c].esc_buf[0] == 0x1c) {
                   8656:                        printer_out(c, data);
                   8657:                        if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
                   8658:                                pio[c].esc_buf[pio[c].esc_len] = data;
                   8659:                        }
                   8660:                        pio[c].esc_len++;
                   8661:                        
                   8662:                        switch(pio[c].esc_buf[1]) {
1.1.1.38  root     8663:                        case 0x21: // 1Ch 21h XX
                   8664:                        case 0x2d: // 1Ch 2Dh XX
                   8665:                        case 0x57: // 1Ch 57h XX
                   8666:                        case 0x6b: // 1Ch 6Bh XX
                   8667:                        case 0x72: // 1Ch 72h XX
                   8668:                        case 0x78: // 1Ch 78h XX
1.1.1.37  root     8669:                                if(pio[c].esc_len == 3) {
                   8670:                                        pio[c].esc_buf[0] = 0x00;
                   8671:                                }
                   8672:                                break;
1.1.1.38  root     8673:                        case 0x26: // 1Ch 26h
1.1.1.37  root     8674:                                pio[c].jis_mode = true;
                   8675:                                pio[c].esc_buf[0] = 0x00;
                   8676:                                break;
1.1.1.38  root     8677:                        case 0x2e: // 1Ch 2Eh
1.1.1.37  root     8678:                                pio[c].jis_mode = false;
                   8679:                                pio[c].esc_buf[0] = 0x00;
                   8680:                                break;
1.1.1.38  root     8681:                        case 0x32: // 1Ch 32h XX XX data
1.1.1.37  root     8682:                                if(pio[c].esc_len == 76) {
                   8683:                                        pio[c].esc_buf[0] = 0x00;
                   8684:                                }
                   8685:                                break;
1.1.1.38  root     8686:                        case 0x44: // 1Bh 44h data 00h
1.1.1.37  root     8687:                                if(pio[c].esc_len == 6) {
                   8688:                                        pio[c].esc_buf[0] = 0x00;
                   8689:                                }
                   8690:                                break;
1.1.1.38  root     8691:                        case 0x53: // 1Ch 53h XX XX
                   8692:                        case 0x54: // 1Ch 54h XX XX
1.1.1.37  root     8693:                                if(pio[c].esc_len == 4) {
                   8694:                                        pio[c].esc_buf[0] = 0x00;
                   8695:                                }
                   8696:                                break;
1.1.1.38  root     8697:                        default: // 1Ch XX
1.1.1.37  root     8698:                                pio[c].esc_buf[0] = 0x00;
                   8699:                                break;
                   8700:                        }
                   8701:                } else if(data == 0x1b || data == 0x1c) {
                   8702:                        printer_out(c, data);
                   8703:                        pio[c].esc_buf[0] = data;
                   8704:                        pio[c].esc_len = 1;
                   8705:                } else if((data >= 0x80 && data <= 0x9f) || (data >= 0xe0 && data <= 0xff)) {
                   8706:                        pio[c].sjis_hi = data;
                   8707:                } else {
                   8708:                        if(pio[c].jis_mode) {
                   8709:                                printer_out(c, 0x1c);
                   8710:                                printer_out(c, 0x2e);
                   8711:                                pio[c].jis_mode = false;
                   8712:                        }
                   8713:                        printer_out(c, data);
                   8714:                }
                   8715:        } else {
                   8716:                if(pio[c].jis_mode) {
                   8717:                        printer_out(c, 0x1c);
                   8718:                        printer_out(c, 0x2e);
                   8719:                        pio[c].jis_mode = false;
                   8720:                }
                   8721:                printer_out(c, data);
                   8722:        }
                   8723: }
                   8724: 
                   8725: inline void pcbios_int_17h_00h()
                   8726: {
                   8727:        if(REG16(DX) < 3) {
                   8728:                pcbios_printer_out(REG16(DX), REG8(AL));
                   8729:                REG8(AH) = 0xd0;
                   8730:        }
                   8731: }
                   8732: 
                   8733: inline void pcbios_int_17h_01h()
                   8734: {
                   8735:        if(REG16(DX) < 3) {
                   8736:                REG8(AH) = 0xd0;
                   8737:        }
                   8738: }
                   8739: 
                   8740: inline void pcbios_int_17h_02h()
                   8741: {
                   8742:        if(REG16(DX) < 3) {
                   8743:                REG8(AH) = 0xd0;
                   8744:        }
                   8745: }
                   8746: 
                   8747: inline void pcbios_int_17h_03h()
                   8748: {
                   8749:        switch(REG8(AL)) {
                   8750:        case 0x00:
                   8751:                if(REG16(DX) < 3) {
                   8752:                        if(pio[REG16(DX)].jis_mode) {
                   8753:                                printer_out(REG16(DX), 0x1c);
                   8754:                                printer_out(REG16(DX), 0x2e);
                   8755:                                pio[REG16(DX)].jis_mode = false;
                   8756:                        }
                   8757:                        for(UINT16 i = 0; i < REG16(CX); i++) {
                   8758:                                printer_out(REG16(DX), mem[SREG_BASE(ES) + REG16(BX) + i]);
                   8759:                        }
                   8760:                        REG16(CX) = 0x0000;
                   8761:                        REG8(AH) = 0xd0;
                   8762:                }
                   8763:                break;
                   8764:        default:
                   8765:                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));
                   8766:                break;
                   8767:        }
                   8768: }
                   8769: 
                   8770: inline void pcbios_int_17h_50h()
                   8771: {
                   8772:        switch(REG8(AL)) {
                   8773:        case 0x00:
                   8774:                if(REG16(DX) < 3) {
                   8775:                        if(REG16(BX) = 0x0001) {
                   8776:                                pio[REG16(DX)].conv_mode = false;
                   8777:                                REG8(AL) = 0x00;
                   8778:                        } else if(REG16(BX) = 0x0051) {
                   8779:                                pio[REG16(DX)].conv_mode = true;
                   8780:                                REG8(AL) = 0x00;
                   8781:                        } else {
                   8782:                                REG8(AL) = 0x01;
                   8783:                        }
                   8784:                } else {
                   8785:                        REG8(AL) = 0x02;
                   8786:                }
                   8787:                break;
                   8788:        case 0x01:
                   8789:                if(REG16(DX) < 3) {
                   8790:                        if(pio[REG16(DX)].conv_mode) {
                   8791:                                REG16(BX) = 0x0051;
                   8792:                        } else {
                   8793:                                REG16(BX) = 0x0001;
                   8794:                        }
                   8795:                        REG8(AL) = 0x00;
                   8796:                } else {
                   8797:                        REG8(AL) = 0x02;
                   8798:                }
                   8799:                break;
                   8800:        default:
                   8801:                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));
                   8802:                break;
                   8803:        }
                   8804: }
                   8805: 
                   8806: inline void pcbios_int_17h_51h()
                   8807: {
                   8808:        if(REG8(DH) >= 0x21 && REG8(DH) <= 0x7e && REG8(DL) >= 0x21 && REG8(DL) <= 0x7e) {
                   8809:                REG16(DX) = pcbios_printer_jis2sjis(REG16(DX));
                   8810:        } else {
                   8811:                REG16(DX) = 0x0000;
                   8812:        }
                   8813: }
                   8814: 
                   8815: inline void pcbios_int_17h_52h()
                   8816: {
                   8817:        if(((REG8(DH) >= 0x81 && REG8(DH) <= 0x9f) || (REG8(DH) >= 0xe0 && REG8(DH) <= 0xfc)) && (REG8(DL) >= 0x40 && REG8(DL) <= 0xfc && REG8(DL) != 0x7f)) {
                   8818:                REG16(DX) = pcbios_printer_sjis2jis(REG16(DX));
                   8819:        } else {
                   8820:                REG16(DX) = 0x0000;
                   8821:        }
                   8822: }
                   8823: 
                   8824: inline void pcbios_int_17h_84h()
                   8825: {
                   8826:        if(REG16(DX) < 3) {
                   8827:                if(pio[REG16(DX)].jis_mode) {
                   8828:                        printer_out(REG16(DX), 0x1c);
                   8829:                        printer_out(REG16(DX), 0x2e);
                   8830:                        pio[REG16(DX)].jis_mode = false;
                   8831:                }
                   8832:                printer_out(REG16(DX), REG8(AL));
                   8833:                REG8(AH) = 0xd0;
                   8834:        }
                   8835: }
                   8836: 
                   8837: inline void pcbios_int_17h_85h()
                   8838: {
                   8839:        pio[0].conv_mode = (REG8(AL) == 0x00);
                   8840: }
                   8841: 
1.1       root     8842: inline void pcbios_int_1ah_00h()
                   8843: {
1.1.1.19  root     8844:        pcbios_update_daily_timer_counter(timeGetTime());
                   8845:        REG16(CX) = *(UINT16 *)(mem + 0x46e);
                   8846:        REG16(DX) = *(UINT16 *)(mem + 0x46c);
                   8847:        REG8(AL) = mem[0x470];
                   8848:        mem[0x470] = 0;
1.1       root     8849: }
                   8850: 
                   8851: inline int to_bcd(int t)
                   8852: {
                   8853:        int u = (t % 100) / 10;
                   8854:        return (u << 4) | (t % 10);
                   8855: }
                   8856: 
                   8857: inline void pcbios_int_1ah_02h()
                   8858: {
                   8859:        SYSTEMTIME time;
                   8860:        
                   8861:        GetLocalTime(&time);
                   8862:        REG8(CH) = to_bcd(time.wHour);
                   8863:        REG8(CL) = to_bcd(time.wMinute);
                   8864:        REG8(DH) = to_bcd(time.wSecond);
                   8865:        REG8(DL) = 0x00;
                   8866: }
                   8867: 
                   8868: inline void pcbios_int_1ah_04h()
                   8869: {
                   8870:        SYSTEMTIME time;
                   8871:        
                   8872:        GetLocalTime(&time);
                   8873:        REG8(CH) = to_bcd(time.wYear / 100);
                   8874:        REG8(CL) = to_bcd(time.wYear);
                   8875:        REG8(DH) = to_bcd(time.wMonth);
                   8876:        REG8(DL) = to_bcd(time.wDay);
                   8877: }
                   8878: 
                   8879: inline void pcbios_int_1ah_0ah()
                   8880: {
                   8881:        SYSTEMTIME time;
                   8882:        FILETIME file_time;
                   8883:        WORD dos_date, dos_time;
                   8884:        
                   8885:        GetLocalTime(&time);
                   8886:        SystemTimeToFileTime(&time, &file_time);
                   8887:        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   8888:        REG16(CX) = dos_date;
                   8889: }
                   8890: 
                   8891: // msdos system call
                   8892: 
1.1.1.43  root     8893: inline void msdos_int_21h_56h(int lfn);
                   8894: 
1.1       root     8895: inline void msdos_int_21h_00h()
                   8896: {
1.1.1.3   root     8897:        msdos_process_terminate(SREG(CS), retval, 1);
1.1       root     8898: }
                   8899: 
1.1.1.35  root     8900: DWORD WINAPI msdos_int_21h_01h_thread(LPVOID)
1.1       root     8901: {
                   8902:        REG8(AL) = msdos_getche();
1.1.1.33  root     8903:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     8904:        
1.1.1.35  root     8905: #ifdef USE_SERVICE_THREAD
                   8906:        service_exit = true;
                   8907: #endif
                   8908:        return(0);
                   8909: }
                   8910: 
                   8911: inline void msdos_int_21h_01h()
                   8912: {
                   8913: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     8914:        if(!in_service && !in_service_29h &&
                   8915:           *(UINT16 *)(mem + 4 * 0x29 + 0) == 0x29 &&
1.1.1.50  root     8916:           *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   8917:                // msdos_putch() will be used in this service
                   8918:                // if int 29h is hooked, run this service in main thread to call int 29h
                   8919:                start_service_loop(msdos_int_21h_01h_thread);
                   8920:        } else {
                   8921: #endif
                   8922:                msdos_int_21h_01h_thread(NULL);
                   8923:                REQUEST_HARDWRE_UPDATE();
                   8924: #ifdef USE_SERVICE_THREAD
                   8925:        }
1.1.1.35  root     8926: #endif
1.1       root     8927: }
                   8928: 
                   8929: inline void msdos_int_21h_02h()
                   8930: {
1.1.1.33  root     8931:        UINT8 data = REG8(DL);
                   8932:        msdos_putch(data);
                   8933:        REG8(AL) = data;
                   8934:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     8935: }
                   8936: 
                   8937: inline void msdos_int_21h_03h()
                   8938: {
                   8939:        REG8(AL) = msdos_aux_in();
                   8940: }
                   8941: 
                   8942: inline void msdos_int_21h_04h()
                   8943: {
                   8944:        msdos_aux_out(REG8(DL));
                   8945: }
                   8946: 
                   8947: inline void msdos_int_21h_05h()
                   8948: {
                   8949:        msdos_prn_out(REG8(DL));
                   8950: }
                   8951: 
                   8952: inline void msdos_int_21h_06h()
                   8953: {
                   8954:        if(REG8(DL) == 0xff) {
                   8955:                if(msdos_kbhit()) {
                   8956:                        REG8(AL) = msdos_getch();
1.1.1.3   root     8957: #if defined(HAS_I386)
                   8958:                        m_ZF = 0;
                   8959: #else
                   8960:                        m_ZeroVal = 1;
                   8961: #endif
1.1       root     8962:                } else {
                   8963:                        REG8(AL) = 0;
1.1.1.3   root     8964: #if defined(HAS_I386)
                   8965:                        m_ZF = 1;
                   8966: #else
                   8967:                        m_ZeroVal = 0;
                   8968: #endif
1.1.1.14  root     8969:                        maybe_idle();
1.1       root     8970:                }
                   8971:        } else {
1.1.1.33  root     8972:                UINT8 data = REG8(DL);
                   8973:                msdos_putch(data);
                   8974:                REG8(AL) = data;
1.1       root     8975:        }
                   8976: }
                   8977: 
1.1.1.35  root     8978: DWORD WINAPI msdos_int_21h_07h_thread(LPVOID)
1.1       root     8979: {
                   8980:        REG8(AL) = msdos_getch();
1.1.1.26  root     8981:        
1.1.1.35  root     8982: #ifdef USE_SERVICE_THREAD
                   8983:        service_exit = true;
                   8984: #endif
                   8985:        return(0);
1.1       root     8986: }
                   8987: 
1.1.1.35  root     8988: inline void msdos_int_21h_07h()
                   8989: {
                   8990: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     8991:        if(!in_service && !in_service_29h) {
                   8992:                start_service_loop(msdos_int_21h_07h_thread);
                   8993:        } else {
                   8994: #endif
                   8995:                msdos_int_21h_07h_thread(NULL);
                   8996:                REQUEST_HARDWRE_UPDATE();
                   8997: #ifdef USE_SERVICE_THREAD
                   8998:        }
1.1.1.35  root     8999: #endif
                   9000: }
                   9001: 
                   9002: DWORD WINAPI msdos_int_21h_08h_thread(LPVOID)
1.1       root     9003: {
                   9004:        REG8(AL) = msdos_getch();
1.1.1.33  root     9005:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     9006:        
1.1.1.35  root     9007: #ifdef USE_SERVICE_THREAD
                   9008:        service_exit = true;
                   9009: #endif
                   9010:        return(0);
                   9011: }
                   9012: 
                   9013: inline void msdos_int_21h_08h()
                   9014: {
                   9015: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9016:        if(!in_service && !in_service_29h) {
                   9017:                start_service_loop(msdos_int_21h_08h_thread);
                   9018:        } else {
                   9019: #endif
                   9020:                msdos_int_21h_08h_thread(NULL);
                   9021:                REQUEST_HARDWRE_UPDATE();
                   9022: #ifdef USE_SERVICE_THREAD
                   9023:        }
1.1.1.35  root     9024: #endif
1.1       root     9025: }
                   9026: 
                   9027: inline void msdos_int_21h_09h()
                   9028: {
1.1.1.21  root     9029:        msdos_stdio_reopen();
                   9030:        
1.1.1.20  root     9031:        process_t *process = msdos_process_info_get(current_psp);
                   9032:        int fd = msdos_psp_get_file_table(1, current_psp);
                   9033:        
1.1.1.14  root     9034:        char *str = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   9035:        int len = 0;
1.1       root     9036:        
1.1.1.14  root     9037:        while(str[len] != '$' && len < 0x10000) {
                   9038:                len++;
                   9039:        }
1.1.1.20  root     9040:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     9041:                // stdout is redirected to file
1.1.1.20  root     9042:                msdos_write(fd, str, len);
1.1       root     9043:        } else {
                   9044:                for(int i = 0; i < len; i++) {
1.1.1.14  root     9045:                        msdos_putch(str[i]);
1.1       root     9046:                }
                   9047:        }
1.1.1.33  root     9048:        REG8(AL) = '$';
                   9049:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     9050: }
                   9051: 
1.1.1.35  root     9052: DWORD WINAPI msdos_int_21h_0ah_thread(LPVOID)
1.1       root     9053: {
1.1.1.3   root     9054:        int ofs = SREG_BASE(DS) + REG16(DX);
1.1       root     9055:        int max = mem[ofs] - 1;
                   9056:        UINT8 *buf = mem + ofs + 2;
                   9057:        int chr, p = 0;
                   9058:        
                   9059:        while((chr = msdos_getch()) != 0x0d) {
1.1.1.33  root     9060:                if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
1.1.1.26  root     9061:                        p = 0;
1.1.1.33  root     9062:                        msdos_putch(0x03);
                   9063:                        msdos_putch(0x0d);
                   9064:                        msdos_putch(0x0a);
1.1.1.26  root     9065:                        break;
1.1.1.33  root     9066:                } else if(ctrl_break_pressed) {
                   9067:                        // skip this byte
1.1.1.26  root     9068:                } else if(chr == 0x00) {
1.1       root     9069:                        // skip 2nd byte
                   9070:                        msdos_getch();
                   9071:                } else if(chr == 0x08) {
                   9072:                        // back space
                   9073:                        if(p > 0) {
                   9074:                                p--;
1.1.1.20  root     9075:                                if(msdos_ctrl_code_check(buf[p])) {
1.1.1.34  root     9076:                                        msdos_putch(0x08);
                   9077:                                        msdos_putch(0x08);
                   9078:                                        msdos_putch(0x20);
                   9079:                                        msdos_putch(0x20);
                   9080:                                        msdos_putch(0x08);
                   9081:                                        msdos_putch(0x08);
1.1.1.36  root     9082:                                } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
                   9083:                                        p--;
                   9084:                                        msdos_putch(0x08);
                   9085:                                        msdos_putch(0x08);
                   9086:                                        msdos_putch(0x20);
                   9087:                                        msdos_putch(0x20);
                   9088:                                        msdos_putch(0x08);
                   9089:                                        msdos_putch(0x08);
1.1.1.34  root     9090:                                } else {
                   9091:                                        msdos_putch(0x08);
                   9092:                                        msdos_putch(0x20);
                   9093:                                        msdos_putch(0x08);
                   9094:                                }
                   9095:                        }
                   9096:                } else if(chr == 0x1b) {
                   9097:                        // escape
                   9098:                        while(p > 0) {
                   9099:                                p--;
                   9100:                                if(msdos_ctrl_code_check(buf[p])) {
                   9101:                                        msdos_putch(0x08);
                   9102:                                        msdos_putch(0x08);
                   9103:                                        msdos_putch(0x20);
                   9104:                                        msdos_putch(0x20);
                   9105:                                        msdos_putch(0x08);
                   9106:                                        msdos_putch(0x08);
1.1.1.20  root     9107:                                } else {
1.1.1.34  root     9108:                                        msdos_putch(0x08);
                   9109:                                        msdos_putch(0x20);
                   9110:                                        msdos_putch(0x08);
1.1.1.20  root     9111:                                }
1.1       root     9112:                        }
                   9113:                } else if(p < max) {
                   9114:                        buf[p++] = chr;
                   9115:                        msdos_putch(chr);
                   9116:                }
                   9117:        }
                   9118:        buf[p] = 0x0d;
                   9119:        mem[ofs + 1] = p;
1.1.1.33  root     9120:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     9121:        
1.1.1.35  root     9122: #ifdef USE_SERVICE_THREAD
                   9123:        service_exit = true;
                   9124: #endif
                   9125:        return(0);
                   9126: }
                   9127: 
                   9128: inline void msdos_int_21h_0ah()
                   9129: {
                   9130:        if(mem[SREG_BASE(DS) + REG16(DX)] != 0x00) {
                   9131: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     9132:                if(!in_service && !in_service_29h &&
                   9133:                   *(UINT16 *)(mem + 4 * 0x29 + 0) == 0x29 &&
1.1.1.50  root     9134:                   *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   9135:                        // msdos_putch() will be used in this service
                   9136:                        // if int 29h is hooked, run this service in main thread to call int 29h
                   9137:                        start_service_loop(msdos_int_21h_0ah_thread);
                   9138:                } else {
                   9139: #endif
                   9140:                        msdos_int_21h_0ah_thread(NULL);
                   9141:                        REQUEST_HARDWRE_UPDATE();
                   9142: #ifdef USE_SERVICE_THREAD
                   9143:                }
1.1.1.35  root     9144: #endif
                   9145:        }
1.1       root     9146: }
                   9147: 
                   9148: inline void msdos_int_21h_0bh()
                   9149: {
                   9150:        if(msdos_kbhit()) {
                   9151:                REG8(AL) = 0xff;
                   9152:        } else {
                   9153:                REG8(AL) = 0x00;
1.1.1.14  root     9154:                maybe_idle();
1.1       root     9155:        }
1.1.1.33  root     9156:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     9157: }
                   9158: 
                   9159: inline void msdos_int_21h_0ch()
                   9160: {
                   9161:        // clear key buffer
1.1.1.21  root     9162:        msdos_stdio_reopen();
                   9163:        
1.1.1.20  root     9164:        process_t *process = msdos_process_info_get(current_psp);
                   9165:        int fd = msdos_psp_get_file_table(0, current_psp);
                   9166:        
                   9167:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     9168:                // stdin is redirected to file
                   9169:        } else {
                   9170:                while(msdos_kbhit()) {
                   9171:                        msdos_getch();
                   9172:                }
                   9173:        }
                   9174:        
                   9175:        switch(REG8(AL)) {
                   9176:        case 0x01:
                   9177:                msdos_int_21h_01h();
                   9178:                break;
                   9179:        case 0x06:
                   9180:                msdos_int_21h_06h();
                   9181:                break;
                   9182:        case 0x07:
                   9183:                msdos_int_21h_07h();
                   9184:                break;
                   9185:        case 0x08:
                   9186:                msdos_int_21h_08h();
                   9187:                break;
                   9188:        case 0x0a:
                   9189:                msdos_int_21h_0ah();
                   9190:                break;
                   9191:        default:
1.1.1.48  root     9192:                // the buffer is flushed but no input is attempted
1.1       root     9193:                break;
                   9194:        }
                   9195: }
                   9196: 
                   9197: inline void msdos_int_21h_0dh()
                   9198: {
                   9199: }
                   9200: 
                   9201: inline void msdos_int_21h_0eh()
                   9202: {
                   9203:        if(REG8(DL) < 26) {
                   9204:                _chdrive(REG8(DL) + 1);
                   9205:                msdos_cds_update(REG8(DL));
1.1.1.23  root     9206:                msdos_sda_update(current_psp);
1.1       root     9207:        }
                   9208:        REG8(AL) = 26; // zdrive
                   9209: }
                   9210: 
1.1.1.14  root     9211: inline void msdos_int_21h_0fh()
                   9212: {
                   9213:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9214:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     9215:        const char *path = msdos_fcb_path(fcb);
1.1.1.14  root     9216:        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     9217:        
1.1.1.14  root     9218:        if(hFile == INVALID_HANDLE_VALUE) {
                   9219:                REG8(AL) = 0xff;
                   9220:        } else {
                   9221:                REG8(AL) = 0;
                   9222:                fcb->current_block = 0;
                   9223:                fcb->record_size = 128;
                   9224:                fcb->file_size = GetFileSize(hFile, NULL);
                   9225:                fcb->handle = hFile;
                   9226:                fcb->cur_record = 0;
                   9227:        }
                   9228: }
                   9229: 
                   9230: inline void msdos_int_21h_10h()
                   9231: {
                   9232:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9233:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9234:        
                   9235:        REG8(AL) = CloseHandle(fcb->handle) ? 0 : 0xff;
                   9236: }
                   9237: 
1.1       root     9238: inline void msdos_int_21h_11h()
                   9239: {
1.1.1.3   root     9240:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9241:        fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9242:        
                   9243:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     9244:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9245:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   9246:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1.1.45  root     9247:        const char *path = msdos_fcb_path(fcb);
1.1       root     9248:        WIN32_FIND_DATA fd;
                   9249:        
1.1.1.13  root     9250:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   9251:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   9252:                FindClose(dtainfo->find_handle);
                   9253:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9254:        }
                   9255:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     9256:        dtainfo->allowable_mask = (ext_fcb->flag == 0xff) ? ext_fcb->attribute : 0x20;
                   9257:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     9258:        
1.1.1.14  root     9259:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   9260:                dtainfo->allowable_mask &= ~8;
1.1       root     9261:        }
1.1.1.14  root     9262:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   9263:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     9264:                      !msdos_find_file_has_8dot3name(&fd)) {
                   9265:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   9266:                                FindClose(dtainfo->find_handle);
                   9267:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9268:                                break;
                   9269:                        }
                   9270:                }
                   9271:        }
1.1.1.13  root     9272:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     9273:                if(ext_fcb->flag == 0xff) {
                   9274:                        ext_find->flag = 0xff;
                   9275:                        memset(ext_find->reserved, 0, 5);
                   9276:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9277:                }
                   9278:                find->drive = _getdrive();
1.1.1.13  root     9279:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     9280:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9281:                find->nt_res = 0;
                   9282:                msdos_find_file_conv_local_time(&fd);
                   9283:                find->create_time_ms = 0;
                   9284:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9285:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9286:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9287:                find->cluster_hi = find->cluster_lo = 0;
                   9288:                find->file_size = fd.nFileSizeLow;
                   9289:                REG8(AL) = 0x00;
1.1.1.14  root     9290:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     9291:                if(ext_fcb->flag == 0xff) {
                   9292:                        ext_find->flag = 0xff;
                   9293:                        memset(ext_find->reserved, 0, 5);
                   9294:                        ext_find->attribute = 8;
                   9295:                }
                   9296:                find->drive = _getdrive();
                   9297:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   9298:                find->attribute = 8;
                   9299:                find->nt_res = 0;
                   9300:                msdos_find_file_conv_local_time(&fd);
                   9301:                find->create_time_ms = 0;
                   9302:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9303:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9304:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9305:                find->cluster_hi = find->cluster_lo = 0;
                   9306:                find->file_size = 0;
1.1.1.14  root     9307:                dtainfo->allowable_mask &= ~8;
1.1       root     9308:                REG8(AL) = 0x00;
                   9309:        } else {
                   9310:                REG8(AL) = 0xff;
                   9311:        }
                   9312: }
                   9313: 
                   9314: inline void msdos_int_21h_12h()
                   9315: {
1.1.1.3   root     9316:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1.1.14  root     9317: //     fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9318:        
                   9319:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     9320:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9321:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   9322:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     9323:        WIN32_FIND_DATA fd;
                   9324:        
1.1.1.13  root     9325:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   9326:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   9327:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     9328:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     9329:                              !msdos_find_file_has_8dot3name(&fd)) {
                   9330:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   9331:                                        FindClose(dtainfo->find_handle);
                   9332:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9333:                                        break;
                   9334:                                }
                   9335:                        }
                   9336:                } else {
1.1.1.13  root     9337:                        FindClose(dtainfo->find_handle);
                   9338:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9339:                }
                   9340:        }
1.1.1.13  root     9341:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     9342:                if(ext_fcb->flag == 0xff) {
                   9343:                        ext_find->flag = 0xff;
                   9344:                        memset(ext_find->reserved, 0, 5);
                   9345:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9346:                }
                   9347:                find->drive = _getdrive();
1.1.1.13  root     9348:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     9349:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9350:                find->nt_res = 0;
                   9351:                msdos_find_file_conv_local_time(&fd);
                   9352:                find->create_time_ms = 0;
                   9353:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9354:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9355:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9356:                find->cluster_hi = find->cluster_lo = 0;
                   9357:                find->file_size = fd.nFileSizeLow;
                   9358:                REG8(AL) = 0x00;
1.1.1.14  root     9359:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     9360:                if(ext_fcb->flag == 0xff) {
                   9361:                        ext_find->flag = 0xff;
                   9362:                        memset(ext_find->reserved, 0, 5);
                   9363:                        ext_find->attribute = 8;
                   9364:                }
                   9365:                find->drive = _getdrive();
                   9366:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   9367:                find->attribute = 8;
                   9368:                find->nt_res = 0;
                   9369:                msdos_find_file_conv_local_time(&fd);
                   9370:                find->create_time_ms = 0;
                   9371:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   9372:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   9373:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   9374:                find->cluster_hi = find->cluster_lo = 0;
                   9375:                find->file_size = 0;
1.1.1.14  root     9376:                dtainfo->allowable_mask &= ~8;
1.1       root     9377:                REG8(AL) = 0x00;
                   9378:        } else {
                   9379:                REG8(AL) = 0xff;
                   9380:        }
                   9381: }
                   9382: 
                   9383: inline void msdos_int_21h_13h()
                   9384: {
1.1.1.3   root     9385:        if(remove(msdos_fcb_path((fcb_t *)(mem + SREG_BASE(DS) + REG16(DX))))) {
1.1       root     9386:                REG8(AL) = 0xff;
                   9387:        } else {
                   9388:                REG8(AL) = 0x00;
                   9389:        }
                   9390: }
                   9391: 
1.1.1.16  root     9392: inline void msdos_int_21h_14h()
                   9393: {
                   9394:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9395:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9396:        process_t *process = msdos_process_info_get(current_psp);
                   9397:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9398:        DWORD num = 0;
                   9399:        
                   9400:        memset(mem + dta_laddr, 0, fcb->record_size);
                   9401:        if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   9402:                REG8(AL) = 1;
                   9403:        } else {
                   9404:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   9405:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   9406:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   9407:                REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   9408:        }
                   9409: }
                   9410: 
                   9411: inline void msdos_int_21h_15h()
1.1.1.14  root     9412: {
                   9413:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9414:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.16  root     9415:        process_t *process = msdos_process_info_get(current_psp);
                   9416:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9417:        DWORD num = 0;
1.1.1.14  root     9418:        
1.1.1.16  root     9419:        if(!WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   9420:                REG8(AL) = 1;
                   9421:        } else {
                   9422:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   9423:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   9424:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   9425:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   9426:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   9427:        }
                   9428: }
                   9429: 
                   9430: inline void msdos_int_21h_16h()
                   9431: {
                   9432:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9433:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     9434:        const char *path = msdos_fcb_path(fcb);
1.1.1.14  root     9435:        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     9436:        
1.1.1.14  root     9437:        if(hFile == INVALID_HANDLE_VALUE) {
                   9438:                REG8(AL) = 0xff;
                   9439:        } else {
                   9440:                REG8(AL) = 0;
                   9441:                fcb->current_block = 0;
                   9442:                fcb->record_size = 128;
                   9443:                fcb->file_size = 0;
                   9444:                fcb->handle = hFile;
                   9445:                fcb->cur_record = 0;
                   9446:        }
                   9447: }
                   9448: 
1.1.1.16  root     9449: inline void msdos_int_21h_17h()
                   9450: {
                   9451:        ext_fcb_t *ext_fcb_src = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9452:        fcb_t *fcb_src = (fcb_t *)(ext_fcb_src + (ext_fcb_src->flag == 0xff ? 1 : 0));
1.1.1.45  root     9453: //     const char *path_src = msdos_fcb_path(fcb_src);
                   9454:        char path_src[MAX_PATH];
                   9455:        strcpy(path_src, msdos_fcb_path(fcb_src));
                   9456:        
1.1.1.16  root     9457:        ext_fcb_t *ext_fcb_dst = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + 16);
                   9458:        fcb_t *fcb_dst = (fcb_t *)(ext_fcb_dst + (ext_fcb_dst->flag == 0xff ? 1 : 0));
1.1.1.45  root     9459: //     const char *path_dst = msdos_fcb_path(fcb_dst);
                   9460:        char path_dst[MAX_PATH];
                   9461:        strcpy(path_dst, msdos_fcb_path(fcb_dst));
1.1.1.16  root     9462:        
                   9463:        if(rename(path_src, path_dst)) {
                   9464:                REG8(AL) = 0xff;
                   9465:        } else {
                   9466:                REG8(AL) = 0;
                   9467:        }
                   9468: }
                   9469: 
1.1       root     9470: inline void msdos_int_21h_18h()
                   9471: {
                   9472:        REG8(AL) = 0x00;
                   9473: }
                   9474: 
                   9475: inline void msdos_int_21h_19h()
                   9476: {
                   9477:        REG8(AL) = _getdrive() - 1;
                   9478: }
                   9479: 
                   9480: inline void msdos_int_21h_1ah()
                   9481: {
                   9482:        process_t *process = msdos_process_info_get(current_psp);
                   9483:        
                   9484:        process->dta.w.l = REG16(DX);
1.1.1.3   root     9485:        process->dta.w.h = SREG(DS);
1.1.1.23  root     9486:        msdos_sda_update(current_psp);
1.1       root     9487: }
                   9488: 
                   9489: inline void msdos_int_21h_1bh()
                   9490: {
                   9491:        int drive_num = _getdrive() - 1;
                   9492:        UINT16 seg, ofs;
                   9493:        
                   9494:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9495:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   9496:                REG8(AL) = dpb->highest_sector_num + 1;
                   9497:                REG16(CX) = dpb->bytes_per_sector;
                   9498:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     9499:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     9500:        } else {
                   9501:                REG8(AL) = 0xff;
1.1.1.3   root     9502:                m_CF = 1;
1.1       root     9503:        }
                   9504: 
                   9505: }
                   9506: 
                   9507: inline void msdos_int_21h_1ch()
                   9508: {
1.1.1.41  root     9509:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
1.1       root     9510:        UINT16 seg, ofs;
                   9511:        
                   9512:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9513:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   9514:                REG8(AL) = dpb->highest_sector_num + 1;
                   9515:                REG16(CX) = dpb->bytes_per_sector;
                   9516:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     9517:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     9518:        } else {
                   9519:                REG8(AL) = 0xff;
1.1.1.3   root     9520:                m_CF = 1;
1.1       root     9521:        }
                   9522: 
                   9523: }
                   9524: 
                   9525: inline void msdos_int_21h_1dh()
                   9526: {
                   9527:        REG8(AL) = 0;
                   9528: }
                   9529: 
                   9530: inline void msdos_int_21h_1eh()
                   9531: {
                   9532:        REG8(AL) = 0;
                   9533: }
                   9534: 
                   9535: inline void msdos_int_21h_1fh()
                   9536: {
                   9537:        int drive_num = _getdrive() - 1;
                   9538:        UINT16 seg, ofs;
                   9539:        
                   9540:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9541:                REG8(AL) = 0;
1.1.1.3   root     9542:                SREG(DS) = seg;
                   9543:                i386_load_segment_descriptor(DS);
1.1       root     9544:                REG16(BX) = ofs;
                   9545:        } else {
                   9546:                REG8(AL) = 0xff;
1.1.1.3   root     9547:                m_CF = 1;
1.1       root     9548:        }
                   9549: }
                   9550: 
                   9551: inline void msdos_int_21h_20h()
                   9552: {
                   9553:        REG8(AL) = 0;
                   9554: }
                   9555: 
1.1.1.14  root     9556: inline void msdos_int_21h_21h()
                   9557: {
                   9558:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9559:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9560:        
                   9561:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   9562:                REG8(AL) = 1;
                   9563:        } else {
                   9564:                process_t *process = msdos_process_info_get(current_psp);
                   9565:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9566:                memset(mem + dta_laddr, 0, fcb->record_size);
1.1.1.16  root     9567:                DWORD num = 0;
1.1.1.14  root     9568:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   9569:                        REG8(AL) = 1;
                   9570:                } else {
                   9571:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   9572:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     9573:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
1.1.1.14  root     9574:                }
                   9575:        }
                   9576: }
                   9577: 
                   9578: inline void msdos_int_21h_22h()
                   9579: {
                   9580:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9581:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9582:        
                   9583:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   9584:                REG8(AL) = 0xff;
                   9585:        } else {
                   9586:                process_t *process = msdos_process_info_get(current_psp);
                   9587:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
1.1.1.16  root     9588:                DWORD num = 0;
1.1.1.14  root     9589:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL);
                   9590:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   9591:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   9592:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     9593:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
1.1.1.14  root     9594:        }
                   9595: }
                   9596: 
1.1.1.16  root     9597: inline void msdos_int_21h_23h()
                   9598: {
                   9599:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9600:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.45  root     9601:        const char *path = msdos_fcb_path(fcb);
1.1.1.16  root     9602:        HANDLE hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   9603:        
                   9604:        if(hFile == INVALID_HANDLE_VALUE) {
                   9605:                REG8(AL) = 0xff;
                   9606:        } else {
                   9607:                UINT32 size = GetFileSize(hFile, NULL);
                   9608:                fcb->rand_record = size / fcb->record_size + ((size % fcb->record_size) != 0);
                   9609:                REG8(AL) = 0;
                   9610:        }
                   9611: }
                   9612: 
                   9613: inline void msdos_int_21h_24h()
                   9614: {
                   9615:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9616:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9617:        
                   9618:        fcb->rand_record = fcb->current_block * fcb->record_size + fcb->cur_record;
                   9619: }
                   9620: 
1.1       root     9621: inline void msdos_int_21h_25h()
                   9622: {
                   9623:        *(UINT16 *)(mem + 4 * REG8(AL) + 0) = REG16(DX);
1.1.1.3   root     9624:        *(UINT16 *)(mem + 4 * REG8(AL) + 2) = SREG(DS);
1.1       root     9625: }
                   9626: 
                   9627: inline void msdos_int_21h_26h()
                   9628: {
                   9629:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   9630:        
                   9631:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
1.1.1.48  root     9632:        psp->first_mcb = REG16(DX) + (PSP_SIZE >> 4);
1.1       root     9633:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   9634:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   9635:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   9636:        psp->parent_psp = 0;
                   9637: }
                   9638: 
1.1.1.16  root     9639: inline void msdos_int_21h_27h()
                   9640: {
                   9641:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9642:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9643:        
                   9644:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   9645:                REG8(AL) = 1;
                   9646:        } else {
                   9647:                process_t *process = msdos_process_info_get(current_psp);
                   9648:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9649:                memset(mem + dta_laddr, 0, fcb->record_size * REG16(CX));
                   9650:                DWORD num = 0;
                   9651:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL) || num == 0) {
                   9652:                        REG8(AL) = 1;
                   9653:                } else {
                   9654:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   9655:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   9656:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   9657:                        REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   9658:                }
                   9659:        }
                   9660: }
                   9661: 
                   9662: inline void msdos_int_21h_28h()
                   9663: {
                   9664:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   9665:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   9666:        
                   9667:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   9668:                REG8(AL) = 0xff;
                   9669:        } else {
                   9670:                process_t *process = msdos_process_info_get(current_psp);
                   9671:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9672:                DWORD num = 0;
                   9673:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL);
                   9674:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   9675:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   9676:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   9677:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   9678:                REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   9679:        }
                   9680: }
                   9681: 
1.1       root     9682: inline void msdos_int_21h_29h()
                   9683: {
1.1.1.20  root     9684:        int ofs = 0;//SREG_BASE(DS) + REG16(SI);
                   9685:        char buffer[1024], name[MAX_PATH], ext[MAX_PATH];
1.1       root     9686:        UINT8 drv = 0;
                   9687:        char sep_chars[] = ":.;,=+";
                   9688:        char end_chars[] = "\\<>|/\"[]";
                   9689:        char spc_chars[] = " \t";
                   9690:        
1.1.1.20  root     9691:        memcpy(buffer, mem + SREG_BASE(DS) + REG16(SI), 1023);
                   9692:        buffer[1023] = 0;
                   9693:        memset(name, 0x20, sizeof(name));
                   9694:        memset(ext, 0x20, sizeof(ext));
                   9695:        
1.1       root     9696:        if(REG8(AL) & 1) {
1.1.1.20  root     9697:                ofs += strspn((char *)(buffer + ofs), spc_chars);
                   9698:                if(my_strchr(sep_chars, buffer[ofs]) && buffer[ofs] != '\0') {
1.1       root     9699:                        ofs++;
                   9700:                }
                   9701:        }
1.1.1.20  root     9702:        ofs += strspn((char *)(buffer + ofs), spc_chars);
1.1       root     9703:        
1.1.1.24  root     9704:        if(buffer[ofs + 1] == ':') {
                   9705:                if(buffer[ofs] >= 'a' && buffer[ofs] <= 'z') {
                   9706:                        drv = buffer[ofs] - 'a' + 1;
1.1.1.20  root     9707:                        ofs += 2;
1.1.1.24  root     9708:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   9709:                                ofs++;
                   9710:                        }
                   9711:                } else if(buffer[ofs] >= 'A' && buffer[ofs] <= 'Z') {
                   9712:                        drv = buffer[ofs] - 'A' + 1;
1.1       root     9713:                        ofs += 2;
1.1.1.24  root     9714:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   9715:                                ofs++;
                   9716:                        }
1.1       root     9717:                }
                   9718:        }
1.1.1.20  root     9719:        for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   9720:                UINT8 c = buffer[ofs];
                   9721:                if(is_kanji) {
                   9722:                        is_kanji = 0;
                   9723:                } else if(msdos_lead_byte_check(c)) {
                   9724:                        is_kanji = 1;
                   9725:                } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     9726:                        break;
                   9727:                } else if(c >= 'a' && c <= 'z') {
                   9728:                        c -= 0x20;
                   9729:                }
                   9730:                ofs++;
                   9731:                name[i] = c;
                   9732:        }
1.1.1.20  root     9733:        if(buffer[ofs] == '.') {
1.1       root     9734:                ofs++;
1.1.1.20  root     9735:                for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   9736:                        UINT8 c = buffer[ofs];
                   9737:                        if(is_kanji) {
                   9738:                                is_kanji = 0;
                   9739:                        } else if(msdos_lead_byte_check(c)) {
                   9740:                                is_kanji = 1;
                   9741:                        } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     9742:                                break;
                   9743:                        } else if(c >= 'a' && c <= 'z') {
                   9744:                                c -= 0x20;
                   9745:                        }
                   9746:                        ofs++;
                   9747:                        ext[i] = c;
                   9748:                }
                   9749:        }
1.1.1.20  root     9750:        int si = REG16(SI) + ofs;
1.1.1.3   root     9751:        int ds = SREG(DS);
1.1       root     9752:        while(si > 0xffff) {
                   9753:                si -= 0x10;
                   9754:                ds++;
                   9755:        }
                   9756:        REG16(SI) = si;
1.1.1.3   root     9757:        SREG(DS) = ds;
                   9758:        i386_load_segment_descriptor(DS);
1.1       root     9759:        
1.1.1.3   root     9760:        UINT8 *fcb = mem + SREG_BASE(ES) + REG16(DI);
1.1.1.20  root     9761:        if(!(REG8(AL) & 2) || drv != 0) {
                   9762:                fcb[0] = drv;
                   9763:        }
                   9764:        if(!(REG8(AL) & 4) || name[0] != 0x20) {
                   9765:                memcpy(fcb + 1, name, 8);
                   9766:        }
                   9767:        if(!(REG8(AL) & 8) || ext[0] != 0x20) {
                   9768:                memcpy(fcb + 9, ext, 3);
                   9769:        }
                   9770:        for(int i = 1, found_star = 0; i < 1 + 8; i++) {
1.1       root     9771:                if(fcb[i] == '*') {
                   9772:                        found_star = 1;
                   9773:                }
                   9774:                if(found_star) {
                   9775:                        fcb[i] = '?';
                   9776:                }
                   9777:        }
1.1.1.20  root     9778:        for(int i = 9, found_star = 0; i < 9 + 3; i++) {
1.1       root     9779:                if(fcb[i] == '*') {
                   9780:                        found_star = 1;
                   9781:                }
                   9782:                if(found_star) {
                   9783:                        fcb[i] = '?';
                   9784:                }
                   9785:        }
                   9786:        
1.1.1.44  root     9787:        if(drv == 0 || msdos_is_valid_drive(drv - 1)) {
1.1       root     9788:                if(memchr(fcb + 1, '?', 8 + 3)) {
                   9789:                        REG8(AL) = 0x01;
1.1.1.20  root     9790:                } else {
                   9791:                        REG8(AL) = 0x00;
1.1       root     9792:                }
                   9793:        } else {
                   9794:                REG8(AL) = 0xff;
                   9795:        }
                   9796: }
                   9797: 
                   9798: inline void msdos_int_21h_2ah()
                   9799: {
                   9800:        SYSTEMTIME sTime;
                   9801:        
                   9802:        GetLocalTime(&sTime);
                   9803:        REG16(CX) = sTime.wYear;
                   9804:        REG8(DH) = (UINT8)sTime.wMonth;
                   9805:        REG8(DL) = (UINT8)sTime.wDay;
                   9806:        REG8(AL) = (UINT8)sTime.wDayOfWeek;
                   9807: }
                   9808: 
                   9809: inline void msdos_int_21h_2bh()
                   9810: {
1.1.1.14  root     9811:        REG8(AL) = 0xff;
1.1       root     9812: }
                   9813: 
                   9814: inline void msdos_int_21h_2ch()
                   9815: {
                   9816:        SYSTEMTIME sTime;
                   9817:        
                   9818:        GetLocalTime(&sTime);
                   9819:        REG8(CH) = (UINT8)sTime.wHour;
                   9820:        REG8(CL) = (UINT8)sTime.wMinute;
                   9821:        REG8(DH) = (UINT8)sTime.wSecond;
1.1.1.14  root     9822:        REG8(DL) = (UINT8)sTime.wMilliseconds / 10;
1.1       root     9823: }
                   9824: 
                   9825: inline void msdos_int_21h_2dh()
                   9826: {
                   9827:        REG8(AL) = 0x00;
                   9828: }
                   9829: 
                   9830: inline void msdos_int_21h_2eh()
                   9831: {
                   9832:        process_t *process = msdos_process_info_get(current_psp);
                   9833:        
                   9834:        process->verify = REG8(AL);
                   9835: }
                   9836: 
                   9837: inline void msdos_int_21h_2fh()
                   9838: {
                   9839:        process_t *process = msdos_process_info_get(current_psp);
                   9840:        
                   9841:        REG16(BX) = process->dta.w.l;
1.1.1.3   root     9842:        SREG(ES) = process->dta.w.h;
                   9843:        i386_load_segment_descriptor(ES);
1.1       root     9844: }
                   9845: 
                   9846: inline void msdos_int_21h_30h()
                   9847: {
                   9848:        // Version Flag / OEM
1.1.1.27  root     9849:        if(REG8(AL) == 0x01) {
1.1.1.29  root     9850: #ifdef SUPPORT_HMA
                   9851:                REG16(BX) = 0x0000;
                   9852: #else
                   9853:                REG16(BX) = 0x1000; // DOS is in HMA
                   9854: #endif
1.1       root     9855:        } else {
1.1.1.27  root     9856:                // NOTE: EXDEB invites BL shows the machine type (0=PC-98, 1=PC/AT, 2=FMR),
                   9857:                // but this is not correct on Windows 98 SE
                   9858: //             REG16(BX) = 0xff01;     // OEM = Microsoft, PC/AT
                   9859:                REG16(BX) = 0xff00;     // OEM = Microsoft
1.1       root     9860:        }
1.1.1.27  root     9861:        REG16(CX) = 0x0000;
1.1.1.30  root     9862:        REG8(AL) = dos_major_version;   // 7
                   9863:        REG8(AH) = dos_minor_version;   // 10
1.1       root     9864: }
                   9865: 
                   9866: inline void msdos_int_21h_31h()
                   9867: {
1.1.1.29  root     9868:        try {
                   9869:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   9870:        } catch(...) {
                   9871:                // recover the broken mcb
                   9872:                int mcb_seg = current_psp - 1;
                   9873:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     9874:                
1.1.1.29  root     9875:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     9876:                        mcb->mz = 'M';
                   9877:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   9878:                        
1.1.1.29  root     9879:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39  root     9880:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29  root     9881:                        } else {
1.1.1.39  root     9882:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29  root     9883:                        }
                   9884:                } else {
                   9885:                        mcb->mz = 'Z';
1.1.1.30  root     9886:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     9887:                }
                   9888:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   9889:        }
1.1       root     9890:        msdos_process_terminate(current_psp, REG8(AL) | 0x300, 0);
                   9891: }
                   9892: 
                   9893: inline void msdos_int_21h_32h()
                   9894: {
                   9895:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   9896:        UINT16 seg, ofs;
                   9897:        
                   9898:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9899:                REG8(AL) = 0;
1.1.1.3   root     9900:                SREG(DS) = seg;
                   9901:                i386_load_segment_descriptor(DS);
1.1       root     9902:                REG16(BX) = ofs;
                   9903:        } else {
                   9904:                REG8(AL) = 0xff;
1.1.1.3   root     9905:                m_CF = 1;
1.1       root     9906:        }
                   9907: }
                   9908: 
                   9909: inline void msdos_int_21h_33h()
                   9910: {
                   9911:        char path[MAX_PATH];
1.1.1.48  root     9912:        char drive = 3; // C:
1.1       root     9913:        
                   9914:        switch(REG8(AL)) {
                   9915:        case 0x00:
1.1.1.33  root     9916:                REG8(DL) = ctrl_break_checking;
1.1       root     9917:                break;
                   9918:        case 0x01:
1.1.1.33  root     9919:                ctrl_break_checking = REG8(DL);
                   9920:                break;
                   9921:        case 0x02:
                   9922:                {
                   9923:                        UINT8 old = ctrl_break_checking;
                   9924:                        ctrl_break_checking = REG8(DL);
                   9925:                        REG8(DL) = old;
                   9926:                }
                   9927:                break;
                   9928:        case 0x03:
                   9929:        case 0x04:
                   9930:                // DOS 4.0+ - Unused
1.1       root     9931:                break;
                   9932:        case 0x05:
1.1.1.48  root     9933:                if(GetSystemDirectory(path, MAX_PATH) != 0) {
                   9934:                        if(path[0] >= 'a' && path[0] <= 'z') {
                   9935:                                drive = path[0] - 'a' + 1;
                   9936:                        } else if(path[0] >= 'A' && path[0] <= 'Z') {
                   9937:                                drive = path[0] - 'A' + 1;
                   9938:                        }
1.1       root     9939:                }
1.1.1.48  root     9940:                REG8(DL) = (UINT8)drive;
1.1       root     9941:                break;
                   9942:        case 0x06:
1.1.1.2   root     9943:                // MS-DOS version (7.10)
1.1       root     9944:                REG8(BL) = 7;
1.1.1.2   root     9945:                REG8(BH) = 10;
1.1       root     9946:                REG8(DL) = 0;
1.1.1.29  root     9947: #ifdef SUPPORT_HMA
                   9948:                REG8(DH) = 0x00;
                   9949: #else
                   9950:                REG8(DH) = 0x10; // DOS is in HMA
                   9951: #endif
1.1       root     9952:                break;
1.1.1.6   root     9953:        case 0x07:
                   9954:                if(REG8(DL) == 0) {
                   9955:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag &= ~0x20;
                   9956:                } else if(REG8(DL) == 1) {
                   9957:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag |= 0x20;
                   9958:                }
                   9959:                break;
1.1       root     9960:        default:
1.1.1.22  root     9961:                unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.48  root     9962: //             REG16(AX) = 0x01;
                   9963: //             m_CF = 1;
                   9964:                REG8(AL) = 0xff;
1.1       root     9965:                break;
                   9966:        }
                   9967: }
                   9968: 
1.1.1.23  root     9969: inline void msdos_int_21h_34h()
                   9970: {
                   9971:        SREG(ES) = SDA_TOP >> 4;
                   9972:        i386_load_segment_descriptor(ES);
                   9973:        REG16(BX) = offsetof(sda_t, indos_flag);;
                   9974: }
                   9975: 
1.1       root     9976: inline void msdos_int_21h_35h()
                   9977: {
                   9978:        REG16(BX) = *(UINT16 *)(mem + 4 * REG8(AL) + 0);
1.1.1.3   root     9979:        SREG(ES) = *(UINT16 *)(mem + 4 * REG8(AL) + 2);
                   9980:        i386_load_segment_descriptor(ES);
1.1       root     9981: }
                   9982: 
                   9983: inline void msdos_int_21h_36h()
                   9984: {
                   9985:        struct _diskfree_t df = {0};
                   9986:        
                   9987:        if(_getdiskfree(REG8(DL), &df) == 0) {
                   9988:                REG16(AX) = (UINT16)df.sectors_per_cluster;
                   9989:                REG16(CX) = (UINT16)df.bytes_per_sector;
1.1.1.13  root     9990:                REG16(BX) = df.avail_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.avail_clusters;
                   9991:                REG16(DX) = df.total_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.total_clusters;
1.1       root     9992:        } else {
                   9993:                REG16(AX) = 0xffff;
                   9994:        }
                   9995: }
                   9996: 
                   9997: inline void msdos_int_21h_37h()
                   9998: {
1.1.1.22  root     9999:        static UINT8 dev_flag = 0xff;
1.1       root     10000:        
                   10001:        switch(REG8(AL)) {
                   10002:        case 0x00:
1.1.1.22  root     10003:                {
                   10004:                        process_t *process = msdos_process_info_get(current_psp);
                   10005:                        REG8(AL) = 0x00;
                   10006:                        REG8(DL) = process->switchar;
                   10007:                }
1.1       root     10008:                break;
                   10009:        case 0x01:
1.1.1.22  root     10010:                {
                   10011:                        process_t *process = msdos_process_info_get(current_psp);
                   10012:                        REG8(AL) = 0x00;
                   10013:                        process->switchar = REG8(DL);
1.1.1.23  root     10014:                        msdos_sda_update(current_psp);
1.1.1.22  root     10015:                }
                   10016:                break;
                   10017:        case 0x02:
                   10018:                REG8(DL) = dev_flag;
                   10019:                break;
                   10020:        case 0x03:
                   10021:                dev_flag = REG8(DL);
                   10022:                break;
                   10023:        case 0xd0:
                   10024:        case 0xd1:
                   10025:        case 0xd2:
                   10026:        case 0xd3:
                   10027:        case 0xd4:
                   10028:        case 0xd5:
                   10029:        case 0xd6:
                   10030:        case 0xd7:
                   10031:        case 0xdc:
                   10032:        case 0xdd:
                   10033:        case 0xde:
                   10034:        case 0xdf:
1.1.1.48  root     10035:                // DIET v1.43e
                   10036: //             REG16(AX) = 1;
                   10037:                REG8(AL) = 0xff;
1.1       root     10038:                break;
                   10039:        default:
1.1.1.22  root     10040:                unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.48  root     10041: //             REG16(AX) = 1;
                   10042:                REG8(AL) = 0xff;
1.1       root     10043:                break;
                   10044:        }
                   10045: }
                   10046: 
1.1.1.52! root     10047: int get_country_info(country_info_t *ci, LCID locale = LOCALE_USER_DEFAULT)
1.1.1.17  root     10048: {
                   10049:        char LCdata[80];
                   10050:        
1.1.1.19  root     10051:        ZeroMemory(ci, offsetof(country_info_t, reserved));
1.1.1.42  root     10052:        GetLocaleInfo(locale, LOCALE_ICURRDIGITS, LCdata, sizeof(LCdata));
1.1.1.17  root     10053:        ci->currency_dec_digits = atoi(LCdata);
1.1.1.42  root     10054:        GetLocaleInfo(locale, LOCALE_ICURRENCY, LCdata, sizeof(LCdata));
1.1.1.17  root     10055:        ci->currency_format = *LCdata - '0';
1.1.1.42  root     10056:        GetLocaleInfo(locale, LOCALE_IDATE, LCdata, sizeof(LCdata));
1.1.1.17  root     10057:        ci->date_format = *LCdata - '0';
1.1.1.42  root     10058:        GetLocaleInfo(locale, LOCALE_SCURRENCY, LCdata, sizeof(LCdata));
1.1.1.17  root     10059:        memcpy(&ci->currency_symbol, LCdata, 4);
1.1.1.42  root     10060:        GetLocaleInfo(locale, LOCALE_SDATE, LCdata, sizeof(LCdata));
1.1.1.17  root     10061:        *ci->date_sep = *LCdata;
1.1.1.42  root     10062:        GetLocaleInfo(locale, LOCALE_SDECIMAL, LCdata, sizeof(LCdata));
1.1.1.17  root     10063:        *ci->dec_sep = *LCdata;
1.1.1.42  root     10064:        GetLocaleInfo(locale, LOCALE_SLIST, LCdata, sizeof(LCdata));
1.1.1.17  root     10065:        *ci->list_sep = *LCdata;
1.1.1.42  root     10066:        GetLocaleInfo(locale, LOCALE_STHOUSAND, LCdata, sizeof(LCdata));
1.1.1.17  root     10067:        *ci->thou_sep = *LCdata;
1.1.1.42  root     10068:        GetLocaleInfo(locale, LOCALE_STIME, LCdata, sizeof(LCdata));
1.1.1.17  root     10069:        *ci->time_sep = *LCdata;
1.1.1.42  root     10070:        GetLocaleInfo(locale, LOCALE_STIMEFORMAT, LCdata, sizeof(LCdata));
1.1.1.17  root     10071:        if(strchr(LCdata, 'H') != NULL) {
                   10072:                ci->time_format = 1;
                   10073:        }
1.1.1.49  root     10074:        ci->case_map.w.l = 0x000a; // dummy case map routine is at fffc:000a
                   10075:        ci->case_map.w.h = DUMMY_TOP >> 4;
1.1.1.42  root     10076:        GetLocaleInfo(locale, LOCALE_ICOUNTRY, LCdata, sizeof(LCdata));
1.1.1.17  root     10077:        return atoi(LCdata);
                   10078: }
                   10079: 
1.1.1.42  root     10080: int get_country_info(country_info_t *ci, USHORT usPrimaryLanguage, USHORT usSubLanguage)
                   10081: {
                   10082:        return get_country_info(ci, MAKELCID(MAKELANGID(usPrimaryLanguage, usSubLanguage), SORT_DEFAULT));
                   10083: }
                   10084: 
1.1.1.43  root     10085: void set_country_info(country_info_t *ci, int size)
                   10086: {
                   10087:        char LCdata[80];
                   10088:        
                   10089:        if(size >= 0x00 + 2) {
                   10090:                memset(LCdata, 0, sizeof(LCdata));
                   10091:                *LCdata = '0' + ci->date_format;
                   10092:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IDATE, LCdata);
                   10093:        }
                   10094:        if(size >= 0x02 + 5) {
                   10095:                memset(LCdata, 0, sizeof(LCdata));
                   10096:                memcpy(LCdata, &ci->currency_symbol, 4);
                   10097:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SCURRENCY, LCdata);
                   10098:        }
                   10099:        if(size >= 0x07 + 2) {
                   10100:                memset(LCdata, 0, sizeof(LCdata));
                   10101:                *LCdata = *ci->thou_sep;
                   10102:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, LCdata);
                   10103:        }
                   10104:        if(size >= 0x09 + 2) {
                   10105:                memset(LCdata, 0, sizeof(LCdata));
                   10106:                *LCdata = *ci->dec_sep;
                   10107:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, LCdata);
                   10108:        }
                   10109:        if(size >= 0x0b + 2) {
                   10110:                memset(LCdata, 0, sizeof(LCdata));
                   10111:                *LCdata = *ci->date_sep;
                   10112:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDATE, LCdata);
                   10113:        }
                   10114:        if(size >= 0x0d + 2) {
                   10115:                memset(LCdata, 0, sizeof(LCdata));
                   10116:                *LCdata = *ci->time_sep;
                   10117:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIME, LCdata);
                   10118:        }
                   10119:        if(size >= 0x0f + 1) {
                   10120:                memset(LCdata, 0, sizeof(LCdata));
                   10121:                *LCdata = '0' + ci->currency_format;
                   10122:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRENCY, LCdata);
                   10123:        }
                   10124:        if(size >= 0x10 + 1) {
                   10125:                sprintf(LCdata, "%d", ci->currency_dec_digits);
                   10126:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRDIGITS, LCdata);
                   10127:        }
                   10128:        if(size >= 0x11 + 1) {
                   10129:                // FIXME: is time format always H/h:mm:ss ???
                   10130:                if(ci->time_format & 1) {
                   10131:                        sprintf(LCdata, "H%cmm%css", *ci->time_sep, *ci->time_sep);
                   10132:                } else {
                   10133:                        sprintf(LCdata, "h%cmm%css", *ci->time_sep, *ci->time_sep);
                   10134:                }
                   10135:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, LCdata);
                   10136:        }
                   10137:        if(size >= 0x12 + 4) {
                   10138:                // 12h  DWORD   address of case map routine
                   10139:                //              (FAR CALL, AL = character to map to upper case [>= 80h])
                   10140:        }
                   10141:        if(size >= 0x16 + 2) {
                   10142:                memset(LCdata, 0, sizeof(LCdata));
                   10143:                *LCdata = *ci->list_sep;
                   10144:                SetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SLIST, LCdata);
                   10145:        }
                   10146: }
                   10147: 
1.1.1.42  root     10148: #ifndef SUBLANG_SWAHILI
                   10149:        #define SUBLANG_SWAHILI 0x01
                   10150: #endif
                   10151: #ifndef SUBLANG_TSWANA_BOTSWANA
                   10152:        #define SUBLANG_TSWANA_BOTSWANA 0x02
                   10153: #endif
                   10154: #ifndef SUBLANG_LITHUANIAN_LITHUANIA
                   10155:        #define SUBLANG_LITHUANIAN_LITHUANIA 0x01
                   10156: #endif
                   10157: #ifndef LANG_BANGLA
                   10158:        #define LANG_BANGLA 0x45
                   10159: #endif
                   10160: #ifndef SUBLANG_BANGLA_BANGLADESH
                   10161:        #define SUBLANG_BANGLA_BANGLADESH 0x02
                   10162: #endif
                   10163: 
                   10164: static const struct {
                   10165:        int code;
                   10166:        USHORT usPrimaryLanguage;
                   10167:        USHORT usSubLanguage;
                   10168: } country_table[] = {
                   10169:        {0x001, LANG_ENGLISH, SUBLANG_ENGLISH_US},                              // United States
                   10170:        {0x002, LANG_FRENCH, SUBLANG_FRENCH_CANADIAN},                          // Canadian-French
                   10171:        {0x004, LANG_ENGLISH, SUBLANG_ENGLISH_CAN},                             // Canada (English)
                   10172:        {0x007, LANG_RUSSIAN, SUBLANG_RUSSIAN_RUSSIA},                          // Russia
                   10173:        {0x014, LANG_ARABIC, SUBLANG_ARABIC_EGYPT},                             // Egypt
                   10174:        {0x01B, LANG_ZULU, SUBLANG_ZULU_SOUTH_AFRICA},                          // South Africa
                   10175: //     {0x01B, LANG_XHOSA, SUBLANG_XHOSA_SOUTH_AFRICA},                        // South Africa
                   10176: //     {0x01B, LANG_AFRIKAANS, SUBLANG_AFRIKAANS_SOUTH_AFRICA},                // South Africa
                   10177: //     {0x01B, LANG_ENGLISH, SUBLANG_ENGLISH_SOUTH_AFRICA},                    // South Africa
                   10178: //     {0x01B, LANG_SOTHO, SUBLANG_SOTHO_NORTHERN_SOUTH_AFRICA},               // South Africa
                   10179: //     {0x01B, LANG_TSWANA, SUBLANG_TSWANA_SOUTH_AFRICA},                      // South Africa
                   10180:        {0x01E, LANG_GREEK, SUBLANG_GREEK_GREECE},                              // Greece
                   10181:        {0x01F, LANG_DUTCH, SUBLANG_DUTCH},                                     // Netherlands
                   10182: //     {0x01F, LANG_FRISIAN, SUBLANG_FRISIAN_NETHERLANDS},                     // Netherlands
                   10183:        {0x020, LANG_DUTCH, SUBLANG_DUTCH_BELGIAN},                             // Belgium
                   10184: //     {0x020, LANG_FRENCH, SUBLANG_FRENCH_BELGIAN},                           // Belgium
                   10185:        {0x021, LANG_FRENCH, SUBLANG_FRENCH},                                   // France
                   10186:        {0x022, LANG_SPANISH, SUBLANG_SPANISH},                                 // Spain
                   10187:        {0x023, LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA},                    // Bulgaria???
                   10188:        {0x024, LANG_HUNGARIAN, SUBLANG_HUNGARIAN_HUNGARY},                     // Hungary (not supported by DR DOS 5.0)
                   10189:        {0x027, LANG_ITALIAN, SUBLANG_ITALIAN},                                 // Italy / San Marino / Vatican City
                   10190:        {0x028, LANG_ROMANIAN, SUBLANG_ROMANIAN_ROMANIA},                       // Romania
                   10191:        {0x029, LANG_GERMAN, SUBLANG_GERMAN_SWISS},                             // Switzerland / Liechtenstein
                   10192: //     {0x029, LANG_FRENCH, SUBLANG_FRENCH_SWISS},                             // Switzerland / Liechtenstein
                   10193: //     {0x029, LANG_ITALIAN, SUBLANG_ITALIAN_SWISS},                           // Switzerland / Liechtenstein
                   10194:        {0x02A, LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA},                          // Czechoslovakia / Tjekia / Slovakia (not supported by DR DOS 5.0)
                   10195:        {0x02B, LANG_GERMAN, SUBLANG_GERMAN_AUSTRIAN},                          // Austria (DR DOS 5.0)
                   10196:        {0x02C, LANG_ENGLISH, SUBLANG_ENGLISH_UK},                              // United Kingdom
                   10197:        {0x02D, LANG_DANISH, SUBLANG_DANISH_DENMARK},                           // Denmark
                   10198:        {0x02E, LANG_SWEDISH, SUBLANG_SWEDISH},                                 // Sweden
                   10199: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_NORTHERN_SWEDEN},                       // Sweden
                   10200: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_LULE_SWEDEN},                           // Sweden
                   10201: //     {0x02E, LANG_SAMI, SUBLANG_SAMI_SOUTHERN_SWEDEN},                       // Sweden
                   10202:        {0x02F, LANG_NORWEGIAN, SUBLANG_NORWEGIAN_BOKMAL},                      // Norway
                   10203: //     {0x02F, LANG_NORWEGIAN, SUBLANG_NORWEGIAN_NYNORSK},                     // Norway
                   10204: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_NORTHERN_NORWAY},                       // Norway
                   10205: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_LULE_NORWAY},                           // Norway
                   10206: //     {0x02F, LANG_SAMI, SUBLANG_SAMI_SOUTHERN_NORWAY},                       // Norway
                   10207:        {0x030, LANG_POLISH, SUBLANG_POLISH_POLAND},                            // Poland (not supported by DR DOS 5.0)
                   10208:        {0x031, LANG_GERMAN, SUBLANG_GERMAN},                                   // Germany
                   10209:        {0x033, LANG_SPANISH, SUBLANG_SPANISH_PERU},                            // Peru
                   10210: //     {0x033, LANG_QUECHUA, SUBLANG_QUECHUA_PERU},                            // Peru
                   10211:        {0x034, LANG_SPANISH, SUBLANG_SPANISH_MEXICAN},                         // Mexico
                   10212:        {0x035, LANG_SPANISH, SUBLANG_NEUTRAL},                                 // Cuba
                   10213:        {0x036, LANG_SPANISH, SUBLANG_SPANISH_ARGENTINA},                       // Argentina
                   10214:        {0x037, LANG_PORTUGUESE, SUBLANG_PORTUGUESE_BRAZILIAN},                 // Brazil (not supported by DR DOS 5.0)
                   10215:        {0x038, LANG_SPANISH, SUBLANG_SPANISH_CHILE},                           // Chile
                   10216:        {0x039, LANG_SPANISH, SUBLANG_SPANISH_COLOMBIA},                        // Columbia
                   10217:        {0x03A, LANG_SPANISH, SUBLANG_SPANISH_VENEZUELA},                       // Venezuela
                   10218:        {0x03C, LANG_MALAY, SUBLANG_MALAY_MALAYSIA},                            // Malaysia
                   10219:        {0x03D, LANG_ENGLISH, SUBLANG_ENGLISH_AUS},                             // International English / Australia
                   10220:        {0x03E, LANG_INDONESIAN, SUBLANG_INDONESIAN_INDONESIA},                 // Indonesia / East Timor
                   10221:        {0x03F, LANG_ENGLISH, SUBLANG_ENGLISH_PHILIPPINES},                     // Philippines
                   10222:        {0x040, LANG_ENGLISH, SUBLANG_ENGLISH_NZ},                              // New Zealand
                   10223:        {0x041, LANG_CHINESE, SUBLANG_CHINESE_SINGAPORE},                       // Singapore
                   10224: //     {0x041, LANG_ENGLISH, SUBLANG_ENGLISH_SINGAPORE},                       // Singapore
                   10225:        {0x042, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan???
                   10226:        {0x051, LANG_JAPANESE, SUBLANG_JAPANESE_JAPAN},                         // Japan (DR DOS 5.0, MS-DOS 5.0+)
                   10227:        {0x052, LANG_KOREAN, SUBLANG_KOREAN},                                   // South Korea (DR DOS 5.0)
                   10228:        {0x054, LANG_VIETNAMESE, SUBLANG_VIETNAMESE_VIETNAM},                   // Vietnam
                   10229:        {0x056, LANG_CHINESE_SIMPLIFIED, SUBLANG_CHINESE_SIMPLIFIED},           // China (MS-DOS 5.0+)
                   10230:        {0x058, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan (MS-DOS 5.0+)
                   10231:        {0x05A, LANG_TURKISH, SUBLANG_TURKISH_TURKEY},                          // Turkey (MS-DOS 5.0+)
                   10232:        {0x05B, LANG_HINDI, SUBLANG_HINDI_INDIA},                               // India
                   10233:        {0x05C, LANG_URDU, SUBLANG_URDU_PAKISTAN},                              // Pakistan
                   10234:        {0x05D, LANG_PASHTO, SUBLANG_PASHTO_AFGHANISTAN},                       // Afghanistan
                   10235: //     {0x05D, LANG_DARI, SUBLANG_DARI_AFGHANISTAN},                           // Afghanistan
                   10236:        {0x05E, LANG_SINHALESE, SUBLANG_SINHALESE_SRI_LANKA},                   // Sri Lanka
                   10237: //     {0x05E, LANG_TAMIL, SUBLANG_TAMIL_SRI_LANKA},                           // Sri Lanka
                   10238:        {0x062, LANG_PERSIAN, SUBLANG_PERSIAN_IRAN},                            // Iran
                   10239:        {0x070, LANG_BELARUSIAN, SUBLANG_BELARUSIAN_BELARUS},                   // Belarus
                   10240:        {0x0C8, LANG_THAI, SUBLANG_THAI_THAILAND},                              // Thailand
                   10241:        {0x0D4, LANG_ARABIC, SUBLANG_ARABIC_MOROCCO},                           // Morocco
                   10242:        {0x0D5, LANG_ARABIC, SUBLANG_ARABIC_ALGERIA},                           // Algeria
                   10243:        {0x0D8, LANG_ARABIC, SUBLANG_ARABIC_TUNISIA},                           // Tunisia
                   10244:        {0x0DA, LANG_ARABIC, SUBLANG_ARABIC_LIBYA},                             // Libya
                   10245:        {0x0DD, LANG_WOLOF, SUBLANG_WOLOF_SENEGAL},                             // Senegal
                   10246: //     {0x0DD, LANG_PULAR, SUBLANG_PULAR_SENEGAL},                             // Senegal
                   10247:        {0x0EA, LANG_HAUSA, SUBLANG_HAUSA_NIGERIA_LATIN},                       // Nigeria
                   10248: //     {0x0EA, LANG_YORUBA, SUBLANG_YORUBA_NIGERIA},                           // Nigeria
                   10249: //     {0x0EA, LANG_IGBO, SUBLANG_IGBO_NIGERIA},                               // Nigeria
                   10250:        {0x0FB, LANG_AMHARIC, SUBLANG_AMHARIC_ETHIOPIA},                        // Ethiopia
                   10251: //     {0x0FB, LANG_TIGRINYA, SUBLANG_TIGRINYA_ETHIOPIA},                      // Ethiopia
                   10252:        {0x0FE, LANG_SWAHILI, SUBLANG_SWAHILI},                                 // Kenya
                   10253:        {0x107, LANG_ENGLISH, SUBLANG_ENGLISH_ZIMBABWE},                        // Zimbabwe
                   10254:        {0x10B, LANG_TSWANA, SUBLANG_TSWANA_BOTSWANA},                          // Botswana
                   10255:        {0x12A, LANG_FAEROESE, SUBLANG_FAEROESE_FAROE_ISLANDS},                 // Faroe Islands
                   10256:        {0x12B, LANG_GREENLANDIC, SUBLANG_GREENLANDIC_GREENLAND},               // Greenland
                   10257:        {0x15F, LANG_PORTUGUESE, SUBLANG_PORTUGUESE},                           // Portugal
                   10258:        {0x160, LANG_LUXEMBOURGISH, SUBLANG_LUXEMBOURGISH_LUXEMBOURG},          // Luxembourg
                   10259: //     {0x160, LANG_GERMAN, SUBLANG_GERMAN_LUXEMBOURG},                        // Luxembourg
                   10260: //     {0x160, LANG_FRENCH, SUBLANG_FRENCH_LUXEMBOURG},                        // Luxembourg
                   10261:        {0x161, LANG_IRISH, SUBLANG_IRISH_IRELAND},                             // Ireland
                   10262: //     {0x161, LANG_ENGLISH, SUBLANG_ENGLISH_IRELAND},                         // Ireland
                   10263:        {0x162, LANG_ICELANDIC, SUBLANG_ICELANDIC_ICELAND},                     // Iceland
                   10264:        {0x163, LANG_ALBANIAN, SUBLANG_ALBANIAN_ALBANIA},                       // Albania
                   10265:        {0x164, LANG_MALTESE, SUBLANG_MALTESE_MALTA},                           // Malta
                   10266:        {0x166, LANG_FINNISH, SUBLANG_FINNISH_FINLAND},                         // Finland
                   10267: //     {0x166, LANG_SAMI, SUBLANG_SAMI_NORTHERN_FINLAND},                      // Finland
                   10268: //     {0x166, LANG_SAMI, SUBLANG_SAMI_SKOLT_FINLAND},                         // Finland
                   10269: //     {0x166, LANG_SAMI, SUBLANG_SAMI_INARI_FINLAND},                         // Finland
                   10270:        {0x167, LANG_BULGARIAN, SUBLANG_BULGARIAN_BULGARIA},                    // Bulgaria
                   10271:        {0x172, LANG_LITHUANIAN, SUBLANG_LITHUANIAN_LITHUANIA},                 // Lithuania
                   10272:        {0x173, LANG_LATVIAN, SUBLANG_LATVIAN_LATVIA},                          // Latvia
                   10273:        {0x174, LANG_ESTONIAN, SUBLANG_ESTONIAN_ESTONIA},                       // Estonia
                   10274:        {0x17D, LANG_SERBIAN, SUBLANG_SERBIAN_LATIN},                           // Serbia / Montenegro
                   10275:        {0x180, LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA},                         // Croatia???
                   10276:        {0x181, LANG_SERBIAN, SUBLANG_SERBIAN_CROATIA},                         // Croatia
                   10277:        {0x182, LANG_SLOVENIAN, SUBLANG_SLOVENIAN_SLOVENIA},                    // Slovenia
                   10278:        {0x183, LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_LATIN},        // Bosnia-Herzegovina (Latin)
                   10279:        {0x184, LANG_BOSNIAN, SUBLANG_BOSNIAN_BOSNIA_HERZEGOVINA_CYRILLIC},     // Bosnia-Herzegovina (Cyrillic)
                   10280:        {0x185, LANG_MACEDONIAN, SUBLANG_MACEDONIAN_MACEDONIA},                 // FYR Macedonia
                   10281:        {0x1A5, LANG_CZECH, SUBLANG_CZECH_CZECH_REPUBLIC},                      // Czech Republic
                   10282:        {0x1A6, LANG_SLOVAK, SUBLANG_SLOVAK_SLOVAKIA},                          // Slovakia
                   10283:        {0x1F5, LANG_ENGLISH, SUBLANG_ENGLISH_BELIZE},                          // Belize
                   10284:        {0x1F6, LANG_SPANISH, SUBLANG_SPANISH_GUATEMALA},                       // Guatemala
                   10285:        {0x1F7, LANG_SPANISH, SUBLANG_SPANISH_EL_SALVADOR},                     // El Salvador
                   10286:        {0x1F8, LANG_SPANISH, SUBLANG_SPANISH_HONDURAS},                        // Honduras
                   10287:        {0x1F9, LANG_SPANISH, SUBLANG_SPANISH_NICARAGUA},                       // Nicraragua
                   10288:        {0x1FA, LANG_SPANISH, SUBLANG_SPANISH_COSTA_RICA},                      // Costa Rica
                   10289:        {0x1FB, LANG_SPANISH, SUBLANG_SPANISH_PANAMA},                          // Panama
                   10290:        {0x24F, LANG_SPANISH, SUBLANG_SPANISH_BOLIVIA},                         // Bolivia
1.1.1.43  root     10291: //     {0x24F, LANG_QUECHUA, SUBLANG_QUECHUA_BOLIVIA},                         // Bolivia
1.1.1.42  root     10292:        {0x251, LANG_SPANISH, SUBLANG_SPANISH_ECUADOR},                         // Ecuador
                   10293: //     {0x251, LANG_QUECHUA, SUBLANG_QUECHUA_ECUADOR},                         // Ecuador
                   10294:        {0x253, LANG_SPANISH, SUBLANG_SPANISH_PARAGUAY},                        // Paraguay
                   10295:        {0x256, LANG_SPANISH, SUBLANG_SPANISH_URUGUAY},                         // Uruguay
                   10296:        {0x2A1, LANG_MALAY, SUBLANG_MALAY_BRUNEI_DARUSSALAM},                   // Brunei Darussalam
                   10297:        {0x311, LANG_ARABIC, SUBLANG_NEUTRAL},                                  // Arabic (Middle East/Saudi Arabia/etc.)
                   10298:        {0x324, LANG_UKRAINIAN, SUBLANG_UKRAINIAN_UKRAINE},                     // Ukraine
                   10299:        {0x352, LANG_KOREAN, SUBLANG_KOREAN},                                   // North Korea
                   10300:        {0x354, LANG_CHINESE, SUBLANG_CHINESE_HONGKONG},                        // Hong Kong
                   10301:        {0x355, LANG_CHINESE, SUBLANG_CHINESE_MACAU},                           // Macao
                   10302:        {0x357, LANG_KHMER, SUBLANG_KHMER_CAMBODIA},                            // Cambodia
                   10303:        {0x370, LANG_BANGLA, SUBLANG_BANGLA_BANGLADESH},                        // Bangladesh
                   10304:        {0x376, LANG_CHINESE_TRADITIONAL, SUBLANG_CHINESE_TRADITIONAL},         // Taiwan (DOS 6.22+)
                   10305:        {0x3C0, LANG_DIVEHI, SUBLANG_DIVEHI_MALDIVES},                          // Maldives
                   10306:        {0x3C1, LANG_ARABIC, SUBLANG_ARABIC_LEBANON},                           // Lebanon
                   10307:        {0x3C2, LANG_ARABIC, SUBLANG_ARABIC_JORDAN},                            // Jordan
                   10308:        {0x3C3, LANG_ARABIC, SUBLANG_ARABIC_SYRIA},                             // Syrian Arab Republic
                   10309:        {0x3C4, LANG_ARABIC, SUBLANG_ARABIC_IRAQ},                              // Ireq
                   10310:        {0x3C5, LANG_ARABIC, SUBLANG_ARABIC_KUWAIT},                            // Kuwait
                   10311:        {0x3C6, LANG_ARABIC, SUBLANG_ARABIC_SAUDI_ARABIA},                      // Saudi Arabia
                   10312:        {0x3C7, LANG_ARABIC, SUBLANG_ARABIC_YEMEN},                             // Yemen
                   10313:        {0x3C8, LANG_ARABIC, SUBLANG_ARABIC_OMAN},                              // Oman
                   10314:        {0x3CB, LANG_ARABIC, SUBLANG_ARABIC_UAE},                               // United Arab Emirates
                   10315:        {0x3CC, LANG_HEBREW, SUBLANG_HEBREW_ISRAEL},                            // Israel (Hebrew) (DR DOS 5.0,MS-DOS 5.0+)
                   10316:        {0x3CD, LANG_ARABIC, SUBLANG_ARABIC_BAHRAIN},                           // Bahrain
                   10317:        {0x3CE, LANG_ARABIC, SUBLANG_ARABIC_QATAR},                             // Qatar
                   10318:        {0x3D0, LANG_MONGOLIAN, SUBLANG_MONGOLIAN_CYRILLIC_MONGOLIA},           // Mongolia
                   10319: //     {0x3D0, LANG_MONGOLIAN, SUBLANG_MONGOLIAN_PRC},                         // Mongolia
                   10320:        {0x3D1, LANG_NEPALI, SUBLANG_NEPALI_NEPAL},                             // Nepal
                   10321:        {-1, 0, 0},
                   10322: };
                   10323: 
1.1.1.14  root     10324: inline void msdos_int_21h_38h()
                   10325: {
                   10326:        switch(REG8(AL)) {
                   10327:        case 0x00:
1.1.1.19  root     10328:                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1.1.14  root     10329:                break;
                   10330:        default:
1.1.1.42  root     10331:                for(int i = 0;; i++) {
                   10332:                        if(country_table[i].code == (REG8(AL) != 0xff ? REG8(AL) : REG16(BX))) {
                   10333:                                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)), country_table[i].usPrimaryLanguage, country_table[i].usSubLanguage);
                   10334:                                break;
                   10335:                        } else if(country_table[i].code == -1) {
                   10336: //                             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));
                   10337: //                             REG16(AX) = 2;
                   10338: //                             m_CF = 1;
1.1.1.48  root     10339:                                // get current coutry info
1.1.1.42  root     10340:                                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
                   10341:                                break;
                   10342:                        }
                   10343:                }
1.1.1.14  root     10344:                break;
                   10345:        }
                   10346: }
                   10347: 
1.1       root     10348: inline void msdos_int_21h_39h(int lfn)
                   10349: {
1.1.1.3   root     10350:        if(_mkdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     10351:                REG16(AX) = errno;
1.1.1.3   root     10352:                m_CF = 1;
1.1       root     10353:        }
                   10354: }
                   10355: 
                   10356: inline void msdos_int_21h_3ah(int lfn)
                   10357: {
1.1.1.3   root     10358:        if(_rmdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     10359:                REG16(AX) = errno;
1.1.1.3   root     10360:                m_CF = 1;
1.1       root     10361:        }
                   10362: }
                   10363: 
                   10364: inline void msdos_int_21h_3bh(int lfn)
                   10365: {
1.1.1.45  root     10366:        const char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1.1.44  root     10367:        
                   10368:        if(_chdir(path)) {
1.1.1.17  root     10369:                REG16(AX) = 3;  // must be 3 (path not found)
1.1.1.3   root     10370:                m_CF = 1;
1.1.1.44  root     10371:        } else {
                   10372:                int drv = _getdrive() - 1;
                   10373:                if(path[1] == ':') {
                   10374:                        if(path[0] >= 'A' && path[0] <= 'Z') {
                   10375:                                drv = path[0] - 'A';
                   10376:                        } else if(path[0] >= 'a' && path[0] <= 'z') {
                   10377:                                drv = path[0] - 'a';
                   10378:                        }
                   10379:                }
                   10380:                msdos_cds_update(drv, path);
1.1       root     10381:        }
                   10382: }
                   10383: 
                   10384: inline void msdos_int_21h_3ch()
                   10385: {
1.1.1.45  root     10386:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     10387:        int attr = GetFileAttributes(path);
1.1.1.37  root     10388:        int fd = -1;
                   10389:        int sio_port = 0;
                   10390:        int lpt_port = 0;
1.1       root     10391:        
1.1.1.45  root     10392:        if(msdos_is_device_path(path)) {
                   10393:                fd = msdos_open_device(path, _O_WRONLY | _O_BINARY, &sio_port, &lpt_port);
1.1       root     10394:        } else {
                   10395:                fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   10396:        }
                   10397:        if(fd != -1) {
                   10398:                if(attr == -1) {
                   10399:                        attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   10400:                }
                   10401:                SetFileAttributes(path, attr);
                   10402:                REG16(AX) = fd;
1.1.1.45  root     10403:                msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     10404:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     10405:        } else {
                   10406:                REG16(AX) = errno;
1.1.1.3   root     10407:                m_CF = 1;
1.1       root     10408:        }
                   10409: }
                   10410: 
                   10411: inline void msdos_int_21h_3dh()
                   10412: {
1.1.1.45  root     10413:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     10414:        int mode = REG8(AL) & 0x03;
1.1.1.37  root     10415:        int fd = -1;
                   10416:        int sio_port = 0;
                   10417:        int lpt_port = 0;
1.1       root     10418:        
                   10419:        if(mode < 0x03) {
1.1.1.45  root     10420:                if(msdos_is_device_path(path)) {
                   10421:                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1.1.11  root     10422:                } else {
1.1.1.13  root     10423:                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     10424:                }
1.1       root     10425:                if(fd != -1) {
                   10426:                        REG16(AX) = fd;
1.1.1.45  root     10427:                        msdos_file_handler_open(fd, path, _isatty(fd), mode, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     10428:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     10429:                } else {
                   10430:                        REG16(AX) = errno;
1.1.1.3   root     10431:                        m_CF = 1;
1.1       root     10432:                }
                   10433:        } else {
                   10434:                REG16(AX) = 0x0c;
1.1.1.3   root     10435:                m_CF = 1;
1.1       root     10436:        }
                   10437: }
                   10438: 
                   10439: inline void msdos_int_21h_3eh()
                   10440: {
                   10441:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10442:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10443:        
1.1.1.20  root     10444:        if(fd < process->max_files && file_handler[fd].valid) {
                   10445:                _close(fd);
                   10446:                msdos_file_handler_close(fd);
                   10447:                msdos_psp_set_file_table(REG16(BX), 0x0ff, current_psp);
1.1       root     10448:        } else {
                   10449:                REG16(AX) = 0x06;
1.1.1.3   root     10450:                m_CF = 1;
1.1       root     10451:        }
                   10452: }
                   10453: 
1.1.1.35  root     10454: DWORD WINAPI msdos_int_21h_3fh_thread(LPVOID)
                   10455: {
                   10456:        UINT8 *buf = mem + SREG_BASE(DS) + REG16(DX);
                   10457:        int max = REG16(CX);
                   10458:        int p = 0;
                   10459:        
                   10460:        while(max > p) {
                   10461:                int chr = msdos_getch();
                   10462:                
                   10463:                if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
                   10464:                        p = 0;
                   10465:                        buf[p++] = 0x0d;
                   10466:                        if(max > p) {
                   10467:                                buf[p++] = 0x0a;
                   10468:                        }
                   10469:                        msdos_putch(0x03);
                   10470:                        msdos_putch(0x0d);
                   10471:                        msdos_putch(0x0a);
                   10472:                        break;
                   10473:                } else if(ctrl_break_pressed) {
                   10474:                        // skip this byte
                   10475:                } else if(chr == 0x00) {
                   10476:                        // skip 2nd byte
                   10477:                        msdos_getch();
                   10478:                } else if(chr == 0x0d) {
                   10479:                        // carriage return
                   10480:                        buf[p++] = 0x0d;
                   10481:                        if(max > p) {
                   10482:                                buf[p++] = 0x0a;
                   10483:                        }
                   10484:                        msdos_putch('\n');
                   10485:                        break;
                   10486:                } else if(chr == 0x08) {
                   10487:                        // back space
                   10488:                        if(p > 0) {
                   10489:                                p--;
                   10490:                                if(msdos_ctrl_code_check(buf[p])) {
                   10491:                                        msdos_putch(0x08);
                   10492:                                        msdos_putch(0x08);
                   10493:                                        msdos_putch(0x20);
                   10494:                                        msdos_putch(0x20);
                   10495:                                        msdos_putch(0x08);
                   10496:                                        msdos_putch(0x08);
1.1.1.36  root     10497:                                } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
                   10498:                                        p--;
                   10499:                                        msdos_putch(0x08);
                   10500:                                        msdos_putch(0x08);
                   10501:                                        msdos_putch(0x20);
                   10502:                                        msdos_putch(0x20);
                   10503:                                        msdos_putch(0x08);
                   10504:                                        msdos_putch(0x08);
1.1.1.35  root     10505:                                } else {
                   10506:                                        msdos_putch(0x08);
                   10507:                                        msdos_putch(0x20);
                   10508:                                        msdos_putch(0x08);
                   10509:                                }
                   10510:                        }
                   10511:                } else if(chr == 0x1b) {
                   10512:                        // escape
                   10513:                        while(p > 0) {
                   10514:                                p--;
                   10515:                                if(msdos_ctrl_code_check(buf[p])) {
                   10516:                                        msdos_putch(0x08);
                   10517:                                        msdos_putch(0x08);
                   10518:                                        msdos_putch(0x20);
                   10519:                                        msdos_putch(0x20);
                   10520:                                        msdos_putch(0x08);
                   10521:                                        msdos_putch(0x08);
                   10522:                                } else {
                   10523:                                        msdos_putch(0x08);
                   10524:                                        msdos_putch(0x20);
                   10525:                                        msdos_putch(0x08);
                   10526:                                }
                   10527:                        }
                   10528:                } else {
                   10529:                        buf[p++] = chr;
                   10530:                        msdos_putch(chr);
                   10531:                }
                   10532:        }
                   10533:        REG16(AX) = p;
                   10534:        
                   10535: #ifdef USE_SERVICE_THREAD
                   10536:        service_exit = true;
                   10537: #endif
                   10538:        return(0);
                   10539: }
                   10540: 
1.1       root     10541: inline void msdos_int_21h_3fh()
                   10542: {
                   10543:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10544:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10545:        
1.1.1.20  root     10546:        if(fd < process->max_files && file_handler[fd].valid) {
                   10547:                if(file_mode[file_handler[fd].mode].in) {
                   10548:                        if(file_handler[fd].atty) {
1.1       root     10549:                                // BX is stdin or is redirected to stdin
1.1.1.35  root     10550:                                if(REG16(CX) != 0) {
                   10551: #ifdef USE_SERVICE_THREAD
1.1.1.51  root     10552:                                        if(!in_service && !in_service_29h &&
                   10553:                                           *(UINT16 *)(mem + 4 * 0x29 + 0) == 0x29 &&
1.1.1.50  root     10554:                                           *(UINT16 *)(mem + 4 * 0x29 + 2) == (IRET_TOP >> 4)) {
                   10555:                                                // msdos_putch() will be used in this service
                   10556:                                                // if int 29h is hooked, run this service in main thread to call int 29h
                   10557:                                                start_service_loop(msdos_int_21h_3fh_thread);
                   10558:                                        } else {
                   10559: #endif
                   10560:                                                msdos_int_21h_3fh_thread(NULL);
                   10561:                                                REQUEST_HARDWRE_UPDATE();
                   10562: #ifdef USE_SERVICE_THREAD
                   10563:                                        }
1.1.1.35  root     10564: #endif
                   10565:                                } else {
                   10566:                                        REG16(AX) = 0;
1.1       root     10567:                                }
                   10568:                        } else {
1.1.1.37  root     10569:                                REG16(AX) = msdos_read(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     10570:                        }
                   10571:                } else {
                   10572:                        REG16(AX) = 0x05;
1.1.1.3   root     10573:                        m_CF = 1;
1.1       root     10574:                }
                   10575:        } else {
                   10576:                REG16(AX) = 0x06;
1.1.1.3   root     10577:                m_CF = 1;
1.1       root     10578:        }
                   10579: }
                   10580: 
                   10581: inline void msdos_int_21h_40h()
                   10582: {
                   10583:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10584:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10585:        
1.1.1.20  root     10586:        if(fd < process->max_files && file_handler[fd].valid) {
                   10587:                if(file_mode[file_handler[fd].mode].out) {
1.1       root     10588:                        if(REG16(CX)) {
1.1.1.20  root     10589:                                if(file_handler[fd].atty) {
1.1       root     10590:                                        // BX is stdout/stderr or is redirected to stdout
                   10591:                                        for(int i = 0; i < REG16(CX); i++) {
1.1.1.3   root     10592:                                                msdos_putch(mem[SREG_BASE(DS) + REG16(DX) + i]);
1.1       root     10593:                                        }
                   10594:                                        REG16(AX) = REG16(CX);
                   10595:                                } else {
1.1.1.20  root     10596:                                        REG16(AX) = msdos_write(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     10597:                                }
                   10598:                        } else {
1.1.1.20  root     10599:                                UINT32 pos = _tell(fd);
                   10600:                                _lseek(fd, 0, SEEK_END);
                   10601:                                UINT32 size = _tell(fd);
1.1.1.12  root     10602:                                if(pos < size) {
1.1.1.20  root     10603:                                        _lseek(fd, pos, SEEK_SET);
                   10604:                                        SetEndOfFile((HANDLE)_get_osfhandle(fd));
1.1.1.12  root     10605:                                } else {
                   10606:                                        for(UINT32 i = size; i < pos; i++) {
                   10607:                                                UINT8 tmp = 0;
1.1.1.23  root     10608:                                                msdos_write(fd, &tmp, 1);
1.1.1.12  root     10609:                                        }
1.1.1.20  root     10610:                                        _lseek(fd, pos, SEEK_SET);
1.1       root     10611:                                }
1.1.1.23  root     10612:                                REG16(AX) = 0;
1.1       root     10613:                        }
                   10614:                } else {
                   10615:                        REG16(AX) = 0x05;
1.1.1.3   root     10616:                        m_CF = 1;
1.1       root     10617:                }
                   10618:        } else {
                   10619:                REG16(AX) = 0x06;
1.1.1.3   root     10620:                m_CF = 1;
1.1       root     10621:        }
                   10622: }
                   10623: 
                   10624: inline void msdos_int_21h_41h(int lfn)
                   10625: {
1.1.1.3   root     10626:        if(remove(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     10627:                REG16(AX) = errno;
1.1.1.3   root     10628:                m_CF = 1;
1.1       root     10629:        }
                   10630: }
                   10631: 
                   10632: inline void msdos_int_21h_42h()
                   10633: {
                   10634:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10635:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10636:        
1.1.1.20  root     10637:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     10638:                if(REG8(AL) < 0x03) {
1.1.1.35  root     10639:                        static const int ptrname[] = { SEEK_SET, SEEK_CUR, SEEK_END };
1.1.1.20  root     10640:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), ptrname[REG8(AL)]);
                   10641:                        UINT32 pos = _tell(fd);
1.1       root     10642:                        REG16(AX) = pos & 0xffff;
                   10643:                        REG16(DX) = (pos >> 16);
                   10644:                } else {
                   10645:                        REG16(AX) = 0x01;
1.1.1.3   root     10646:                        m_CF = 1;
1.1       root     10647:                }
                   10648:        } else {
                   10649:                REG16(AX) = 0x06;
1.1.1.3   root     10650:                m_CF = 1;
1.1       root     10651:        }
                   10652: }
                   10653: 
                   10654: inline void msdos_int_21h_43h(int lfn)
                   10655: {
1.1.1.45  root     10656:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1       root     10657:        int attr;
                   10658:        
1.1.1.14  root     10659:        if(!lfn && REG8(AL) > 2) {
                   10660:                REG16(AX) = 0x01;
                   10661:                m_CF = 1;
                   10662:                return;
                   10663:        }
                   10664:        switch(REG8(lfn ? BL : AL)) {
1.1       root     10665:        case 0x00:
                   10666:                if((attr = GetFileAttributes(path)) != -1) {
1.1.1.14  root     10667:                        REG16(CX) = (UINT16)msdos_file_attribute_create((UINT16)attr);
                   10668:                } else {
                   10669:                        REG16(AX) = (UINT16)GetLastError();
                   10670:                        m_CF = 1;
                   10671:                }
                   10672:                break;
                   10673:        case 0x01:
                   10674:                if(!SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)))) {
                   10675:                        REG16(AX) = (UINT16)GetLastError();
                   10676:                        m_CF = 1;
                   10677:                }
                   10678:                break;
                   10679:        case 0x02:
                   10680:                {
1.1.1.45  root     10681:                        DWORD compressed_size = GetCompressedFileSize(path, NULL), file_size = 0;
                   10682:                        if(compressed_size != INVALID_FILE_SIZE) {
                   10683:                                if(compressed_size != 0) {
                   10684:                                        HANDLE hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   10685:                                        if(hFile != INVALID_HANDLE_VALUE) {
                   10686:                                                file_size = GetFileSize(hFile, NULL);
                   10687:                                                CloseHandle(hFile);
                   10688:                                        }
                   10689:                                        if(compressed_size == file_size) {
                   10690:                                                DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   10691:                                                // this isn't correct if the file is in the NTFS MFT
                   10692:                                                if(GetDiskFreeSpace(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
                   10693:                                                        compressed_size = ((compressed_size - 1) | (sectors_per_cluster * bytes_per_sector - 1)) + 1;
                   10694:                                                }
1.1.1.14  root     10695:                                        }
                   10696:                                }
1.1.1.45  root     10697:                                REG16(AX) = LOWORD(compressed_size);
                   10698:                                REG16(DX) = HIWORD(compressed_size);
1.1.1.14  root     10699:                        } else {
                   10700:                                REG16(AX) = (UINT16)GetLastError();
                   10701:                                m_CF = 1;
1.1       root     10702:                        }
1.1.1.14  root     10703:                }
                   10704:                break;
                   10705:        case 0x03:
                   10706:        case 0x05:
                   10707:        case 0x07:
1.1.1.48  root     10708:                if(lfn) {
1.1.1.14  root     10709:                        HANDLE hFile = CreateFile(path, FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   10710:                        if(hFile != INVALID_HANDLE_VALUE) {
                   10711:                                FILETIME local, time;
                   10712:                                DosDateTimeToFileTime(REG16(DI), /*REG8(BL) == 5 ? 0 : */REG16(CX), &local);
                   10713:                                if(REG8(BL) == 7) {
                   10714:                                        ULARGE_INTEGER hund;
                   10715:                                        hund.LowPart = local.dwLowDateTime;
                   10716:                                        hund.HighPart = local.dwHighDateTime;
                   10717:                                        hund.QuadPart += REG16(SI) * 100000;
                   10718:                                        local.dwLowDateTime = hund.LowPart;
                   10719:                                        local.dwHighDateTime = hund.HighPart;
                   10720:                                }
                   10721:                                LocalFileTimeToFileTime(&local, &time);
                   10722:                                if(!SetFileTime(hFile, REG8(BL) == 0x07 ? &time : NULL,
                   10723:                                                       REG8(BL) == 0x05 ? &time : NULL,
                   10724:                                                       REG8(BL) == 0x03 ? &time : NULL)) {
                   10725:                                        REG16(AX) = (UINT16)GetLastError();
                   10726:                                        m_CF = 1;
                   10727:                                }
                   10728:                                CloseHandle(hFile);
                   10729:                        } else {
                   10730:                                REG16(AX) = (UINT16)GetLastError();
                   10731:                                m_CF = 1;
1.1       root     10732:                        }
1.1.1.48  root     10733:                } else {
                   10734:                        // 214303 DR DOS 3.41+ internal - Set Access Rights And Password
                   10735:                        // 214305 DR DOS 5.0-6.0 internal - Set Extended File Attributes
                   10736:                        // 214307 DR DOS 6.0 - Set File Owner
                   10737: //                     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));
                   10738:                        REG16(AX) = 0x01;
                   10739:                        m_CF = 1;
1.1.1.14  root     10740:                }
                   10741:                break;
                   10742:        case 0x04:
                   10743:        case 0x06:
                   10744:        case 0x08:
1.1.1.48  root     10745:                if(lfn) {
1.1.1.14  root     10746:                        WIN32_FILE_ATTRIBUTE_DATA fad;
                   10747:                        if(GetFileAttributesEx(path, GetFileExInfoStandard, (LPVOID)&fad)) {
                   10748:                                FILETIME *time, local;
                   10749:                                time = REG8(BL) == 0x04 ? &fad.ftLastWriteTime :
                   10750:                                                   0x06 ? &fad.ftLastAccessTime :
                   10751:                                                          &fad.ftCreationTime;
                   10752:                                FileTimeToLocalFileTime(time, &local);
                   10753:                                FileTimeToDosDateTime(&local, &REG16(DI), &REG16(CX));
                   10754:                                if(REG8(BL) == 0x08) {
                   10755:                                        ULARGE_INTEGER hund;
                   10756:                                        hund.LowPart = local.dwLowDateTime;
                   10757:                                        hund.HighPart = local.dwHighDateTime;
                   10758:                                        hund.QuadPart /= 100000;
                   10759:                                        REG16(SI) = (UINT16)(hund.QuadPart % 200);
                   10760:                                }
                   10761:                        } else {
                   10762:                                REG16(AX) = (UINT16)GetLastError();
                   10763:                                m_CF = 1;
1.1       root     10764:                        }
1.1.1.48  root     10765:                } else {
                   10766:                        // 214304 DR DOS 5.0-6.0 internal - Get Encrypted Password
                   10767:                        // 214306 DR DOS 6.0 - Get File Owner
                   10768: //                     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));
                   10769:                        REG16(AX) = 0x01;
                   10770:                        m_CF = 1;
1.1.1.14  root     10771:                }
                   10772:                break;
1.1.1.43  root     10773:        case 0xff:
1.1.1.48  root     10774:                if(!lfn && REG16(BP) == 0x5053) {
1.1.1.43  root     10775:                        if(REG8(CL) == 0x39) {
                   10776:                                msdos_int_21h_39h(1);
                   10777:                                break;
                   10778:                        } else if(REG8(CL) == 0x56) {
                   10779:                                msdos_int_21h_56h(1);
                   10780:                                break;
                   10781:                        }
                   10782:                }
1.1.1.14  root     10783:        default:
1.1.1.22  root     10784:                unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.48  root     10785:                REG16(AX) = lfn ? 0x7100 : 0x01;
1.1.1.14  root     10786:                m_CF = 1;
                   10787:                break;
                   10788:        }
                   10789: }
                   10790: 
                   10791: inline void msdos_int_21h_44h()
                   10792: {
1.1.1.22  root     10793:        static UINT16 iteration_count = 0;
                   10794:        
1.1.1.44  root     10795:        process_t *process;
                   10796:        int fd, drv;
1.1.1.14  root     10797:        
                   10798:        switch(REG8(AL)) {
                   10799:        case 0x00:
                   10800:        case 0x01:
                   10801:        case 0x02:
                   10802:        case 0x03:
                   10803:        case 0x04:
                   10804:        case 0x05:
                   10805:        case 0x06:
                   10806:        case 0x07:
1.1.1.44  root     10807:                process = msdos_process_info_get(current_psp);
                   10808:                fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1.1.20  root     10809:                if(fd >= process->max_files || !file_handler[fd].valid) {
                   10810:                        REG16(AX) = 0x06;
                   10811:                        m_CF = 1;
                   10812:                        return;
1.1.1.14  root     10813:                }
                   10814:                break;
                   10815:        case 0x08:
                   10816:        case 0x09:
1.1.1.44  root     10817:                drv = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   10818:                if(!msdos_is_valid_drive(drv)) {
                   10819:                        // invalid drive
1.1.1.14  root     10820:                        REG16(AX) = 0x0f;
                   10821:                        m_CF = 1;
                   10822:                        return;
1.1       root     10823:                }
                   10824:                break;
                   10825:        }
                   10826:        switch(REG8(AL)) {
1.1.1.48  root     10827:        case 0x00: // Get Device Information
1.1.1.20  root     10828:                REG16(DX) = file_handler[fd].info;
1.1       root     10829:                break;
1.1.1.48  root     10830:        case 0x01: // Set Device Information
1.1.1.45  root     10831:                if(REG8(DH) != 0) {
                   10832: //                     REG16(AX) = 0x0d; // data invalid
                   10833: //                     m_CF = 1;
                   10834:                        file_handler[fd].info = REG16(DX);
                   10835:                } else {
                   10836:                        file_handler[fd].info &= 0xff00;
                   10837:                        file_handler[fd].info |= REG8(DL);
                   10838:                }
1.1       root     10839:                break;
1.1.1.48  root     10840:        case 0x02: // Read From Character Device Control Channel
1.1.1.45  root     10841:                if(strstr(file_handler[fd].path, "EMMXXXX0") != NULL && support_ems) {
                   10842:                        // from DOSBox
                   10843:                        switch(*(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX))) {
                   10844:                        case 0x00:
                   10845:                                if(REG16(CX) >= 6) {
                   10846:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x0023;
                   10847:                                        *(UINT32 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = 0x0000;
                   10848:                                        REG16(AX) = 6; // number of bytes actually read
                   10849:                                } else {
                   10850:                                        REG16(AX) = 0x0d; // data invalid
                   10851:                                        m_CF = 1;
                   10852:                                }
                   10853:                                break;
                   10854:                        case 0x01:
                   10855:                                if(REG16(CX) >= 6) {
                   10856:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x000) = 0x0004;     // flags
                   10857:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x002) = 0x019d;     // size of this structure
                   10858:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x004) = 0x0001;     // version 1.0
                   10859:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x006) = 0x00000000; // reserved
                   10860:                                        for(int addr = 0, ofs = 0x00a; addr < 0x100000; addr += 0x4000, ofs += 6) {
                   10861:                                                if(addr >= EMS_TOP && addr < EMS_TOP + EMS_SIZE) {
                   10862:                                                        int page = (addr - EMS_TOP) / 0x4000;
                   10863:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 0) = 0x03;     // frame type: EMS frame in 64k page
                   10864:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 1) = 0xff;     // owner: NONE
                   10865:                                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + ofs + 2) = 0x7fff;   // no logical page number
                   10866:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 4) = page;     // physical EMS page number
                   10867:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 5) = 0x00;     // flags: EMS frame
                   10868:                                                } else {
                   10869:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 0) = 0x00;     // frame type: NONE
                   10870:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 1) = 0xff;     // owner: NONE
                   10871:                                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + ofs + 2) = 0xffff;   // non-EMS frame
                   10872:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 4) = 0xff;     // EMS page number (NONE)
                   10873:                                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + ofs + 5) = 0xaa;     // flags: direct mapping
                   10874:                                                }
                   10875:                                        }
                   10876:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18a) = 0x74;       // ??
                   10877:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18b) = 0x00;       // no UMB descriptors following
                   10878:                                        *(UINT8  *)(mem + XMS_TOP + 0x18 + 0x18c) = 0x01;       // 1 EMS handle info record
                   10879:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x17d) = 0x0000;     // system handle
                   10880:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x18f) = 0x00000000; // handle name
                   10881:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x193) = 0x00000000; // handle name
                   10882:                                        *(UINT16 *)(mem + XMS_TOP + 0x18 + 0x197) = 0x0001;     // system handle
                   10883:                                        *(UINT32 *)(mem + XMS_TOP + 0x18 + 0x199) = 0x00110000; // physical address
                   10884:                                        
                   10885:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x0018;
                   10886:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = XMS_TOP >> 4;;
                   10887:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4) = 0x0001; // version 1.0
                   10888:                                        REG16(AX) = 6; // number of bytes actually read
                   10889:                                } else {
                   10890:                                        REG16(AX) = 0x0d; // data invalid
                   10891:                                        m_CF = 1;
                   10892:                                }
                   10893:                                break;
                   10894:                        case 0x02:
                   10895:                                if(REG16(CX) >= 2) {
                   10896:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 0x40; // EMS 4.0
                   10897:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX) + 1) = 0x00;
                   10898:                                        REG16(AX) = 2; // number of bytes actually read
                   10899:                                } else {
                   10900:                                        REG16(AX) = 0x0d; // data invalid
                   10901:                                        m_CF = 1;
                   10902:                                }
                   10903:                                break;
                   10904:                        case 0x03:
                   10905:                                if(REG16(CX) >= 4) {
                   10906:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = MAX_EMS_PAGES * 16; // max size (kb)
                   10907:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = 0x0080; // min size (kb)
                   10908:                                        REG16(AX) = 4; // number of bytes actually read
                   10909:                                } else {
                   10910:                                        REG16(AX) = 0x0d; // data invalid
                   10911:                                        m_CF = 1;
                   10912:                                }
                   10913:                                break;
                   10914:                        default:
                   10915:                                REG16(AX) = 0x01; // function number invalid
                   10916:                                m_CF = 1;
                   10917:                        }
                   10918:                } else if(strstr(file_handler[fd].path, "CONFIG$") != NULL) {
                   10919:                        if(REG16(CX) >= 5) {
                   10920:                                memset(mem + SREG_BASE(DS) + REG16(DX), 0, 5);
                   10921:                                REG16(AX) = 5; // number of bytes actually read
                   10922:                        } else {
                   10923:                                REG16(AX) = 0x0d; // data invalid
                   10924:                                m_CF = 1;
                   10925:                        }
                   10926:                } else {
                   10927: //                     memset(mem + SREG_BASE(DS) + REG16(DX), 0, REG16(CX));
                   10928: //                     REG16(AX) = REG16(CX);
                   10929:                        REG16(AX) = 0x05; // access denied
                   10930:                        m_CF = 1;
                   10931:                }
                   10932:                break;
1.1.1.48  root     10933:        case 0x03: // Write To Character Device Control Channel
1.1.1.45  root     10934: //             REG16(AX) = 0x05;
                   10935: //             m_CF = 1;
                   10936:                REG16(AX) = 0x00; // success
                   10937:                break;
1.1.1.48  root     10938:        case 0x04: // Read From Block Device Control Channel
                   10939:        case 0x05: // Write To Block Device Control Channel
1.1       root     10940:                REG16(AX) = 0x05;
1.1.1.3   root     10941:                m_CF = 1;
1.1       root     10942:                break;
1.1.1.48  root     10943:        case 0x06: // Get Input Status
1.1.1.20  root     10944:                if(file_mode[file_handler[fd].mode].in) {
                   10945:                        if(file_handler[fd].atty) {
1.1.1.14  root     10946:                                REG8(AL) = msdos_kbhit() ? 0xff : 0x00;
1.1       root     10947:                        } else {
1.1.1.20  root     10948:                                REG8(AL) = eof(fd) ? 0x00 : 0xff;
1.1       root     10949:                        }
1.1.1.14  root     10950:                } else {
                   10951:                        REG8(AL) = 0x00;
1.1       root     10952:                }
                   10953:                break;
1.1.1.48  root     10954:        case 0x07: // Get Output Status
1.1.1.20  root     10955:                if(file_mode[file_handler[fd].mode].out) {
1.1.1.14  root     10956:                        REG8(AL) = 0xff;
                   10957:                } else {
                   10958:                        REG8(AL) = 0x00;
1.1       root     10959:                }
                   10960:                break;
1.1.1.48  root     10961:        case 0x08: // Check If Block Device Removable
1.1.1.44  root     10962:                if(msdos_is_removable_drive(drv) || msdos_is_cdrom_drive(drv)) {
1.1.1.14  root     10963:                        // removable drive
                   10964:                        REG16(AX) = 0x00;
1.1       root     10965:                } else {
1.1.1.14  root     10966:                        // fixed drive
                   10967:                        REG16(AX) = 0x01;
1.1       root     10968:                }
                   10969:                break;
1.1.1.48  root     10970:        case 0x09: // Check If Block Device Remote
1.1.1.44  root     10971:                if(msdos_is_remote_drive(drv)) {
1.1.1.14  root     10972:                        // remote drive
                   10973:                        REG16(DX) = 0x1000;
1.1.1.44  root     10974:                } else if(msdos_is_subst_drive(drv)) {
                   10975:                        // subst drive
                   10976:                        REG16(DX) = 0x8000;
1.1       root     10977:                } else {
1.1.1.14  root     10978:                        // local drive
1.1.1.44  root     10979:                        REG16(DX) = 0x0000;
1.1       root     10980:                }
                   10981:                break;
1.1.1.48  root     10982:        case 0x0a: // Check If Handle Is Remote
1.1.1.45  root     10983:                if(!(file_handler[fd].info & 0x8000) && msdos_is_remote_drive(msdos_drive_number(file_handler[fd].path))) {
                   10984:                        REG16(DX) = 0x8000;
                   10985:                } else {
                   10986:                        REG16(DX) = 0x0000;
                   10987:                }
1.1.1.21  root     10988:                break;
1.1.1.48  root     10989:        case 0x0b: // Set Sharing Retry Count
1.1       root     10990:                break;
1.1.1.48  root     10991:        case 0x0c: // Generic Character Device Request
1.1.1.22  root     10992:                if(REG8(CL) == 0x45) {
                   10993:                        // set iteration (retry) count
                   10994:                        iteration_count = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
                   10995:                } else if(REG8(CL) == 0x4a) {
                   10996:                        // select code page
                   10997:                        active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2);
                   10998:                        msdos_nls_tables_update();
1.1.1.44  root     10999:                } else if(REG8(CL) == 0x4c) {
                   11000:                        // start code-page preparation
                   11001:                        int ids[3] = {437, 0, 0}; // 437: US English
                   11002:                        int count = 1, offset = 0;
                   11003:                        if(active_code_page != 437) {
                   11004:                                ids[count++] = active_code_page;
                   11005:                        }
                   11006:                        if(system_code_page != 437 && system_code_page != active_code_page) {
                   11007:                                ids[count++] = system_code_page;
                   11008:                        }
                   11009:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 0;
                   11010:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 2 + 2 * count;
                   11011:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   11012:                        for(int i = 0; i < count; i++) {
                   11013:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   11014:                        }
                   11015:                } else if(REG8(CL) == 0x4d) {
                   11016:                        // end code-page preparation
                   11017:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2;
                   11018:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
1.1.1.50  root     11019:                } else if(REG8(CL) == 0x5f) {
                   11020:                        // set display information
                   11021:                        if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) >= 14) {
                   11022:                                int cur_width  = *(UINT16 *)(mem + 0x44a) + 0;
                   11023:                                int cur_height = *(UINT8  *)(mem + 0x484) + 1;
                   11024:                                int new_width  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e);   // character columns
                   11025:                                int new_height = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10);   // character rows
                   11026:                                
                   11027:                                if(cur_width != new_width || cur_height != new_height) {
                   11028:                                        pcbios_set_console_size(new_width, new_height, true);
                   11029:                                }
                   11030:                        }
1.1.1.22  root     11031:                } else if(REG8(CL) == 0x65) {
                   11032:                        // get iteration (retry) count
                   11033:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX)) = iteration_count;
                   11034:                } else if(REG8(CL) == 0x6a) {
                   11035:                        // query selected code page
                   11036:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2; // FIXME
                   11037:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
                   11038:                        
                   11039:                        CPINFO info;
                   11040:                        GetCPInfo(active_code_page, &info);
                   11041:                        
                   11042:                        if(info.MaxCharSize != 1) {
                   11043:                                for(int i = 0;; i++) {
                   11044:                                        UINT8 lo = info.LeadByte[2 * i + 0];
                   11045:                                        UINT8 hi = info.LeadByte[2 * i + 1];
                   11046:                                        
                   11047:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 0) = lo;
                   11048:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 2) = hi;
                   11049:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) + 4;
                   11050:                                        
                   11051:                                        if(lo == 0 && hi == 0) {
                   11052:                                                break;
                   11053:                                        }
                   11054:                                }
                   11055:                        }
1.1.1.44  root     11056:                } else if(REG8(CL) == 0x6b) {
                   11057:                        // query prepare list
                   11058:                        int ids[3] = {437, 0, 0}; // 437: US English
                   11059:                        int count = 1, offset = 0;
                   11060:                        if(active_code_page != 437) {
                   11061:                                ids[count++] = active_code_page;
                   11062:                        }
                   11063:                        if(system_code_page != 437 && system_code_page != active_code_page) {
                   11064:                                ids[count++] = system_code_page;
                   11065:                        }
                   11066:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = 2 * (2 + 2 * count);
                   11067:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   11068:                        for(int i = 0; i < count; i++) {
                   11069:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   11070:                        }
                   11071:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = count;
                   11072:                        for(int i = 0; i < count; i++) {
                   11073:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2 * (offset++)) = ids[i];
                   11074:                        }
1.1.1.22  root     11075:                } else if(REG8(CL) == 0x7f) {
1.1.1.44  root     11076:                        // get display information
1.1.1.50  root     11077:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00) = 0;        // level (0 for DOS 4.x-6.0)
                   11078:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x01) = 0;        // reserved (0)
                   11079:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) = 14;       // length of following data (14)
                   11080:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04) = 1;        // bit 0 set for blink, clear for intensity
                   11081:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06) = 1;        // mode type (1=text, 2=graphics)
                   11082:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x07) = 0;        // reserved (0)
                   11083:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08) = 4;        // 4 bits per pixel
                   11084:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a) =  8 * (*(UINT16 *)(mem + 0x44a) + 0);      // pixel columns
                   11085:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c) = 16 * (*(UINT8  *)(mem + 0x484) + 1);      // pixel rows
                   11086:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e) = *(UINT16 *)(mem + 0x44a) + 0;             // character columns
                   11087:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10) = *(UINT8  *)(mem + 0x484) + 1;             // character rows
1.1.1.22  root     11088:                } else {
                   11089:                        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));
                   11090:                        REG16(AX) = 0x01; // invalid function
                   11091:                        m_CF = 1;
                   11092:                }
                   11093:                break;
1.1.1.48  root     11094:        case 0x0d: // Generic Block Device Request
1.1.1.22  root     11095:                if(REG8(CL) == 0x40) {
                   11096:                        // set device parameters
1.1.1.48  root     11097: //             } else if(REG8(CL) == 0x41) {
                   11098: //                     // write logical device track
                   11099: //             } else if(REG8(CL) == 0x42) {
                   11100: //                     // format and verify logical device track
1.1.1.22  root     11101:                } else if(REG8(CL) == 0x46) {
                   11102:                        // set volume serial number
1.1.1.48  root     11103:                } else if(REG8(CL) == 0x47) {
                   11104:                        // set access flag
                   11105: //             } else if(REG8(CL) == 0x48) {
                   11106: //                     // set media lock state
                   11107: //             } else if(REG8(CL) == 0x49) {
                   11108: //                     // eject media in drive
1.1.1.22  root     11109:                } else if(REG8(CL) == 0x4a) {
                   11110:                        // lock logical volume
                   11111:                } else if(REG8(CL) == 0x4b) {
                   11112:                        // lock physical volume
                   11113:                } else if(REG8(CL) == 0x60) {
                   11114:                        // get device parameters
1.1.1.42  root     11115:                        int drive_num = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
1.1.1.22  root     11116:                        
1.1.1.42  root     11117:                        if(pcbios_update_drive_param(drive_num, 1)) {
                   11118:                                drive_param_t *drive_param = &drive_params[drive_num];
                   11119:                                DISK_GEOMETRY *geo = &drive_param->geometry;
                   11120:                                
                   11121:                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x07; // ???
                   11122:                                switch(geo->MediaType) {
                   11123:                                case F5_360_512:
                   11124:                                case F5_320_512:
                   11125:                                case F5_320_1024:
                   11126:                                case F5_180_512:
                   11127:                                case F5_160_512:
                   11128:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // 320K/360K disk
                   11129:                                        break;
                   11130:                                case F5_1Pt2_512:
                   11131:                                case F3_1Pt2_512:
                   11132:                                case F3_1Pt23_1024:
                   11133:                                case F5_1Pt23_1024:
                   11134:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x01; // 1.2M disk
                   11135:                                        break;
                   11136:                                case F3_720_512:
                   11137:                                case F3_640_512:
                   11138:                                case F5_640_512:
                   11139:                                case F5_720_512:
                   11140:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02; // 720K disk
                   11141:                                        break;
                   11142:                                case F8_256_128:
                   11143:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x03; // single-density 8-inch disk
                   11144:                                        break;
                   11145:                                case FixedMedia:
                   11146:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   11147:                                        break;
                   11148:                                case F3_1Pt44_512:
                   11149:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   11150:                                        break;
                   11151:                                case F3_2Pt88_512:
                   11152:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09; // (DOS 5+) 2.88M floppy
                   11153:                                        break;
                   11154:                                default:
                   11155:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   11156: //                                     *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   11157:                                        break;
1.1.1.22  root     11158:                                }
1.1.1.42  root     11159:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = (geo->MediaType == FixedMedia) ? 0x01 : 0x00;
                   11160:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04) = (geo->Cylinders.QuadPart > 0xffff) ? 0xffff : geo->Cylinders.QuadPart;
                   11161:                                switch(geo->MediaType) {
                   11162:                                case F5_360_512:
                   11163:                                case F5_320_512:
                   11164:                                case F5_320_1024:
                   11165:                                case F5_180_512:
                   11166:                                case F5_160_512:
                   11167:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x01; // 320K/360K disk
                   11168:                                        break;
                   11169:                                default:
                   11170:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x00; // other drive types
                   11171:                                        break;
                   11172:                                }
                   11173:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x00) = geo->BytesPerSector;
                   11174:                                *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x02) = geo->SectorsPerTrack * geo->TracksPerCylinder;
                   11175:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x03) = 0;
                   11176:                                *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x05) = 0;
                   11177:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x06) = 0;
                   11178:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x08) = 0;
                   11179:                                switch(geo->MediaType) {
                   11180:                                case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   11181:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xff;
                   11182:                                        break;
                   11183:                                case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   11184:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfe;
                   11185:                                        break;
                   11186:                                case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   11187:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfd;
                   11188:                                        break;
                   11189:                                case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   11190:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfc;
                   11191:                                        break;
                   11192:                                case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   11193:                                case F3_1Pt2_512:
                   11194:                                case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   11195:                                case F5_720_512:
                   11196:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf9;
                   11197:                                        break;
                   11198:                                case FixedMedia:        // hard disk
                   11199:                                case RemovableMedia:
                   11200:                                case Unknown:
                   11201:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf8;
                   11202:                                        break;
                   11203:                                default:
                   11204:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf0;
                   11205:                                        break;
                   11206:                                }
                   11207:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0d) = geo->SectorsPerTrack;
                   11208:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0f) = 1;
                   11209:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x11) = 0;
                   11210:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x15) = geo->TracksPerCylinder * geo->Cylinders.QuadPart;
                   11211:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x1f) = geo->Cylinders.QuadPart;
                   11212:                                // 21h  BYTE    device type
                   11213:                                // 22h  WORD    device attributes (removable or not, etc)
1.1.1.22  root     11214:                        } else {
                   11215:                                REG16(AX) = 0x0f; // invalid drive
                   11216:                                m_CF = 1;
                   11217:                        }
1.1.1.48  root     11218: //             } else if(REG8(CL) == 0x61) {
                   11219: //                     // read logical device track
                   11220: //             } else if(REG8(CL) == 0x62) {
                   11221: //                     // verify logical device track
1.1.1.22  root     11222:                } else if(REG8(CL) == 0x66) {
                   11223:                        // get volume serial number
                   11224:                        char path[] = "A:\\";
                   11225:                        char volume_label[MAX_PATH];
                   11226:                        DWORD serial_number = 0;
                   11227:                        char file_system[MAX_PATH];
                   11228:                        
                   11229:                        path[0] = 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   11230:                        
                   11231:                        if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
                   11232:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   11233:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = serial_number;
                   11234:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x06, 0x20, 11);
                   11235:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x06, volume_label, min(strlen(volume_label), 11));
                   11236:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x11, 0x20,  8);
                   11237:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x11, file_system, min(strlen(file_system), 8));
                   11238:                        } else {
                   11239:                                REG16(AX) = 0x0f; // invalid drive
                   11240:                                m_CF = 1;
                   11241:                        }
                   11242:                } else if(REG8(CL) == 0x67) {
                   11243:                        // get access flag
                   11244:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   11245:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 1;
                   11246:                } else if(REG8(CL) == 0x68) {
                   11247:                        // sense media type
1.1.1.42  root     11248:                        int drive_num = (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
1.1.1.22  root     11249:                        
1.1.1.42  root     11250:                        if(pcbios_update_drive_param(drive_num, 1)) {
                   11251:                                drive_param_t *drive_param = &drive_params[drive_num];
                   11252:                                DISK_GEOMETRY *geo = &drive_param->geometry;
                   11253:                                
                   11254:                                switch(geo->MediaType) {
                   11255:                                case F3_720_512:
                   11256:                                case F5_720_512:
                   11257:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11258:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02;
                   11259:                                        break;
                   11260:                                case F3_1Pt44_512:
                   11261:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11262:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07;
                   11263:                                        break;
                   11264:                                case F3_2Pt88_512:
                   11265:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   11266:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09;
                   11267:                                        break;
                   11268:                                default:
                   11269:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x00;
                   11270:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // ???
                   11271:                                        break;
1.1.1.22  root     11272:                                }
                   11273:                        } else {
                   11274:                                REG16(AX) = 0x0f; // invalid drive
                   11275:                                m_CF = 1;
                   11276:                        }
                   11277:                } else if(REG8(CL) == 0x6a) {
                   11278:                        // unlock logical volume
                   11279:                } else if(REG8(CL) == 0x6b) {
                   11280:                        // unlock physical volume
1.1.1.48  root     11281: //             } else if(REG8(CL) == 0x6c) {
                   11282: //                     // get lock flag
                   11283: //             } else if(REG8(CL) == 0x6d) {
                   11284: //                     // enumerate open files
                   11285: //             } else if(REG8(CL) == 0x6e) {
                   11286: //                     // find swap file
                   11287: //             } else if(REG8(CL) == 0x6f) {
                   11288: //                     // get drive map information
                   11289: //             } else if(REG8(CL) == 0x70) {
                   11290: //                     // get current lock state
                   11291: //             } else if(REG8(CL) == 0x71) {
                   11292: //                     // get first cluster
1.1.1.22  root     11293:                } else {
                   11294:                        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));
                   11295:                        REG16(AX) = 0x01; // invalid function
                   11296:                        m_CF = 1;
                   11297:                }
                   11298:                break;
1.1.1.48  root     11299:        case 0x0e: // Get Lgical Drive Map
1.1.1.44  root     11300:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   11301:                        REG16(AX) = 0x0f; // invalid drive
                   11302:                        m_CF = 1;
                   11303:                } else {
                   11304:                        REG8(AL) = 0;
1.1.1.22  root     11305:                }
                   11306:                break;
1.1.1.48  root     11307:        case 0x0f: // Set Logical Drive Map
1.1.1.44  root     11308:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   11309:                        REG16(AX) = 0x0f; // invalid drive
                   11310:                        m_CF = 1;
1.1.1.22  root     11311:                }
                   11312:                break;
1.1.1.48  root     11313:        case 0x10: // Query Generic IOCTRL Capability (Handle)
1.1.1.22  root     11314:                switch(REG8(CL)) {
                   11315:                case 0x45:
                   11316:                case 0x4a:
1.1.1.48  root     11317:                case 0x4c:
                   11318:                case 0x4d:
1.1.1.22  root     11319:                case 0x65:
                   11320:                case 0x6a:
1.1.1.48  root     11321:                case 0x6b:
1.1.1.22  root     11322:                case 0x7f:
                   11323:                        REG16(AX) = 0x0000; // supported
                   11324:                        break;
                   11325:                default:
                   11326:                        REG8(AL) = 0x01; // ioctl capability not available
                   11327:                        m_CF = 1;
                   11328:                        break;
                   11329:                }
                   11330:                break;
1.1.1.48  root     11331:        case 0x11: // Query Generic IOCTRL Capability (Drive)
1.1.1.22  root     11332:                switch(REG8(CL)) {
                   11333:                case 0x40:
                   11334:                case 0x46:
                   11335:                case 0x4a:
                   11336:                case 0x4b:
                   11337:                case 0x60:
                   11338:                case 0x66:
                   11339:                case 0x67:
                   11340:                case 0x68:
                   11341:                case 0x6a:
                   11342:                case 0x6b:
1.1.1.48  root     11343:                        if(REG8(CH) == 0x00 || REG8(CH) == 0x01 || REG8(CH) == 0x03 || REG8(CH) == 0x05) {
                   11344:                                // CH = 00h     Unknown
                   11345:                                // CH = 01h     COMn:
                   11346:                                // CH = 03h     CON
                   11347:                                // CH = 05h     LPTn:
                   11348:                                REG16(AX) = 0x0000; // supported
                   11349:                                break;
                   11350:                        }
1.1.1.22  root     11351:                default:
                   11352:                        REG8(AL) = 0x01; // ioctl capability not available
                   11353:                        m_CF = 1;
                   11354:                        break;
                   11355:                }
                   11356:                break;
1.1.1.48  root     11357:        case 0x12: // DR DOS 5.0-6.0 - Determine DOS Type
                   11358:        case 0x14: // DR DOS 5.0-6.0 - Set Global Password
                   11359:        case 0x16: // DR DOS 5.0-6.0 - History Buffer, Share, And Hiload Control
                   11360:        case 0x51: // Concurrent DOS v3.2+ - Installation Check
                   11361:        case 0x52: // DR DOS 3.41+ - Determine DOS tTpe/Get DR DOS Version
                   11362:        case 0x54: // DR DOS 3.41+ - Set Global Password
                   11363:        case 0x56: // DR DOS 5.0+ - History Buffer Control
                   11364:        case 0x57: // DR DOS 5.0-6.0 - Share/Hiload Control
                   11365:        case 0x58: // DR DOS 5.0+ internal - Get Pointer To Internal Variable Table
                   11366:        case 0x59: // DR Multiuser DOS 5.0 - API
                   11367:                REG16(AX) = 0x01; // this is not DR-DOS
1.1.1.22  root     11368:                m_CF = 1;
                   11369:                break;
1.1       root     11370:        default:
1.1.1.22  root     11371:                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     11372:                REG16(AX) = 0x01;
1.1.1.3   root     11373:                m_CF = 1;
1.1       root     11374:                break;
                   11375:        }
                   11376: }
                   11377: 
                   11378: inline void msdos_int_21h_45h()
                   11379: {
                   11380:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11381:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11382:        
1.1.1.20  root     11383:        if(fd < process->max_files && file_handler[fd].valid) {
                   11384:                int dup_fd = _dup(fd);
                   11385:                if(dup_fd != -1) {
                   11386:                        REG16(AX) = dup_fd;
                   11387:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   11388: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   11389:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     11390:                } else {
                   11391:                        REG16(AX) = errno;
1.1.1.3   root     11392:                        m_CF = 1;
1.1       root     11393:                }
                   11394:        } else {
                   11395:                REG16(AX) = 0x06;
1.1.1.3   root     11396:                m_CF = 1;
1.1       root     11397:        }
                   11398: }
                   11399: 
                   11400: inline void msdos_int_21h_46h()
                   11401: {
                   11402:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11403:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   11404:        int dup_fd = REG16(CX);
                   11405:        int tmp_fd = msdos_psp_get_file_table(REG16(CX), current_psp);
1.1       root     11406:        
1.1.1.20  root     11407:        if(REG16(BX) == REG16(CX)) {
                   11408:                REG16(AX) = 0x06;
                   11409:                m_CF = 1;
                   11410:        } else if(fd < process->max_files && file_handler[fd].valid && dup_fd < process->max_files) {
                   11411:                if(tmp_fd < process->max_files && file_handler[tmp_fd].valid) {
                   11412:                        _close(tmp_fd);
                   11413:                        msdos_file_handler_close(tmp_fd);
                   11414:                        msdos_psp_set_file_table(dup_fd, 0x0ff, current_psp);
                   11415:                }
                   11416:                if(_dup2(fd, dup_fd) != -1) {
                   11417:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   11418: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   11419:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     11420:                } else {
                   11421:                        REG16(AX) = errno;
1.1.1.3   root     11422:                        m_CF = 1;
1.1       root     11423:                }
                   11424:        } else {
                   11425:                REG16(AX) = 0x06;
1.1.1.3   root     11426:                m_CF = 1;
1.1       root     11427:        }
                   11428: }
                   11429: 
                   11430: inline void msdos_int_21h_47h(int lfn)
                   11431: {
                   11432:        char path[MAX_PATH];
                   11433:        
                   11434:        if(_getdcwd(REG8(DL), path, MAX_PATH) != NULL) {
1.1.1.45  root     11435:                if(!lfn) {
                   11436:                        strcpy(path, msdos_short_path(path));
                   11437:                }
1.1       root     11438:                if(path[1] == ':') {
                   11439:                        // the returned path does not include a drive or the initial backslash
1.1.1.45  root     11440:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), path + 3);
1.1       root     11441:                } else {
1.1.1.45  root     11442:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), path);
1.1       root     11443:                }
                   11444:        } else {
                   11445:                REG16(AX) = errno;
1.1.1.3   root     11446:                m_CF = 1;
1.1       root     11447:        }
                   11448: }
                   11449: 
                   11450: inline void msdos_int_21h_48h()
                   11451: {
1.1.1.19  root     11452:        int seg, umb_linked;
1.1       root     11453:        
1.1.1.8   root     11454:        if((malloc_strategy & 0xf0) == 0x00) {
1.1.1.19  root     11455:                // unlink umb not to allocate memory in umb
                   11456:                if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   11457:                        msdos_mem_unlink_umb();
                   11458:                }
1.1.1.8   root     11459:                if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   11460:                        REG16(AX) = seg;
                   11461:                } else {
                   11462:                        REG16(AX) = 0x08;
                   11463:                        REG16(BX) = msdos_mem_get_free(first_mcb, 0);
                   11464:                        m_CF = 1;
                   11465:                }
1.1.1.19  root     11466:                if(umb_linked != 0) {
                   11467:                        msdos_mem_link_umb();
                   11468:                }
1.1.1.8   root     11469:        } else if((malloc_strategy & 0xf0) == 0x40) {
                   11470:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   11471:                        REG16(AX) = seg;
                   11472:                } else {
                   11473:                        REG16(AX) = 0x08;
                   11474:                        REG16(BX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   11475:                        m_CF = 1;
                   11476:                }
                   11477:        } else if((malloc_strategy & 0xf0) == 0x80) {
                   11478:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   11479:                        REG16(AX) = seg;
                   11480:                } else if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   11481:                        REG16(AX) = seg;
                   11482:                } else {
                   11483:                        REG16(AX) = 0x08;
                   11484:                        REG16(BX) = max(msdos_mem_get_free(UMB_TOP >> 4, 0), msdos_mem_get_free(first_mcb, 0));
                   11485:                        m_CF = 1;
                   11486:                }
1.1       root     11487:        }
                   11488: }
                   11489: 
                   11490: inline void msdos_int_21h_49h()
                   11491: {
1.1.1.14  root     11492:        int mcb_seg = SREG(ES) - 1;
                   11493:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   11494:        
                   11495:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   11496:                msdos_mem_free(SREG(ES));
                   11497:        } else {
1.1.1.33  root     11498:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.14  root     11499:                m_CF = 1;
                   11500:        }
1.1       root     11501: }
                   11502: 
                   11503: inline void msdos_int_21h_4ah()
                   11504: {
1.1.1.14  root     11505:        int mcb_seg = SREG(ES) - 1;
                   11506:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1       root     11507:        int max_paragraphs;
                   11508:        
1.1.1.14  root     11509:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   11510:                if(msdos_mem_realloc(SREG(ES), REG16(BX), &max_paragraphs)) {
                   11511:                        REG16(AX) = 0x08;
                   11512:                        REG16(BX) = max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs;
                   11513:                        m_CF = 1;
                   11514:                }
                   11515:        } else {
1.1.1.33  root     11516:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.3   root     11517:                m_CF = 1;
1.1       root     11518:        }
                   11519: }
                   11520: 
                   11521: inline void msdos_int_21h_4bh()
                   11522: {
1.1.1.3   root     11523:        char *command = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   11524:        param_block_t *param = (param_block_t *)(mem + SREG_BASE(ES) + REG16(BX));
1.1       root     11525:        
                   11526:        switch(REG8(AL)) {
                   11527:        case 0x00:
                   11528:        case 0x01:
                   11529:                if(msdos_process_exec(command, param, REG8(AL))) {
                   11530:                        REG16(AX) = 0x02;
1.1.1.3   root     11531:                        m_CF = 1;
1.1       root     11532:                }
                   11533:                break;
1.1.1.14  root     11534:        case 0x03:
                   11535:                {
                   11536:                        int fd;
                   11537:                        if((fd = _open(command, _O_RDONLY | _O_BINARY)) == -1) {
                   11538:                                REG16(AX) = 0x02;
                   11539:                                m_CF = 1;
                   11540:                                break;
                   11541:                        }
                   11542:                        int size = _read(fd, file_buffer, sizeof(file_buffer));
                   11543:                        _close(fd);
                   11544:                        
                   11545:                        UINT16 *overlay = (UINT16 *)param;
                   11546:                        
                   11547:                        // check exe header
                   11548:                        exe_header_t *header = (exe_header_t *)file_buffer;
                   11549:                        int header_size = 0;
                   11550:                        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   11551:                                header_size = header->header_size * 16;
                   11552:                                // relocation
                   11553:                                int start_seg = overlay[1];
                   11554:                                for(int i = 0; i < header->relocations; i++) {
                   11555:                                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   11556:                                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   11557:                                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   11558:                                }
                   11559:                        }
                   11560:                        memcpy(mem + (overlay[0] << 4), file_buffer + header_size, size - header_size);
                   11561:                }
                   11562:                break;
1.1.1.48  root     11563:        case 0x04:
                   11564:                // Load And Execute In Background (European MS-DOS 4.0 only)
                   11565: //     case 0x05:
                   11566: //             // DOS 5+ - Set Execution State
                   11567:        case 0x80:
                   11568:                // DR DOS v3.41 - Run Already-Loaded Kernel File
                   11569:        case 0xf0:
                   11570:        case 0xf1:
                   11571:                // DIET v1.10+
1.1.1.43  root     11572:        case 0xfd:
                   11573:        case 0xfe:
                   11574:                // unknown function called in FreeCOM
                   11575:                REG16(AX) = 0x01;
                   11576:                m_CF = 1;
                   11577:                break;
1.1       root     11578:        default:
1.1.1.22  root     11579:                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     11580:                REG16(AX) = 0x01;
1.1.1.3   root     11581:                m_CF = 1;
1.1       root     11582:                break;
                   11583:        }
                   11584: }
                   11585: 
                   11586: inline void msdos_int_21h_4ch()
                   11587: {
                   11588:        msdos_process_terminate(current_psp, REG8(AL), 1);
                   11589: }
                   11590: 
                   11591: inline void msdos_int_21h_4dh()
                   11592: {
                   11593:        REG16(AX) = retval;
                   11594: }
                   11595: 
                   11596: inline void msdos_int_21h_4eh()
                   11597: {
                   11598:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     11599:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   11600:        find_t *find = (find_t *)(mem + dta_laddr);
1.1.1.45  root     11601:        const char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     11602:        WIN32_FIND_DATA fd;
                   11603:        
1.1.1.14  root     11604:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   11605:        find->find_magic = FIND_MAGIC;
                   11606:        find->dta_index = dtainfo - dtalist;
1.1       root     11607:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     11608:        dtainfo->allowable_mask = REG8(CL);
                   11609:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     11610:        
1.1.1.14  root     11611:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   11612:                dtainfo->allowable_mask &= ~8;
1.1       root     11613:        }
1.1.1.14  root     11614:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   11615:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     11616:                      !msdos_find_file_has_8dot3name(&fd)) {
                   11617:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   11618:                                FindClose(dtainfo->find_handle);
                   11619:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11620:                                break;
                   11621:                        }
                   11622:                }
                   11623:        }
1.1.1.13  root     11624:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     11625:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   11626:                msdos_find_file_conv_local_time(&fd);
                   11627:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   11628:                find->size = fd.nFileSizeLow;
1.1.1.13  root     11629:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     11630:                REG16(AX) = 0;
1.1.1.14  root     11631:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     11632:                find->attrib = 8;
                   11633:                find->size = 0;
                   11634:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     11635:                dtainfo->allowable_mask &= ~8;
1.1       root     11636:                REG16(AX) = 0;
                   11637:        } else {
                   11638:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     11639:                m_CF = 1;
1.1       root     11640:        }
                   11641: }
                   11642: 
                   11643: inline void msdos_int_21h_4fh()
                   11644: {
                   11645:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     11646:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   11647:        find_t *find = (find_t *)(mem + dta_laddr);
1.1       root     11648:        WIN32_FIND_DATA fd;
                   11649:        
1.1.1.14  root     11650:        if(find->find_magic != FIND_MAGIC || find->dta_index >= MAX_DTAINFO) {
                   11651:                REG16(AX) = 0x12;
                   11652:                m_CF = 1;
                   11653:                return;
                   11654:        }
                   11655:        dtainfo_t *dtainfo = &dtalist[find->dta_index];
1.1.1.13  root     11656:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   11657:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     11658:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     11659:                              !msdos_find_file_has_8dot3name(&fd)) {
                   11660:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   11661:                                        FindClose(dtainfo->find_handle);
                   11662:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11663:                                        break;
                   11664:                                }
                   11665:                        }
                   11666:                } else {
1.1.1.13  root     11667:                        FindClose(dtainfo->find_handle);
                   11668:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11669:                }
                   11670:        }
1.1.1.13  root     11671:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     11672:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   11673:                msdos_find_file_conv_local_time(&fd);
                   11674:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   11675:                find->size = fd.nFileSizeLow;
1.1.1.13  root     11676:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     11677:                REG16(AX) = 0;
1.1.1.14  root     11678:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     11679:                find->attrib = 8;
                   11680:                find->size = 0;
                   11681:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     11682:                dtainfo->allowable_mask &= ~8;
1.1       root     11683:                REG16(AX) = 0;
                   11684:        } else {
                   11685:                REG16(AX) = 0x12;
1.1.1.3   root     11686:                m_CF = 1;
1.1       root     11687:        }
                   11688: }
                   11689: 
                   11690: inline void msdos_int_21h_50h()
                   11691: {
1.1.1.8   root     11692:        if(current_psp != REG16(BX)) {
                   11693:                process_t *process = msdos_process_info_get(current_psp);
                   11694:                if(process != NULL) {
                   11695:                        process->psp = REG16(BX);
                   11696:                }
                   11697:                current_psp = REG16(BX);
1.1.1.23  root     11698:                msdos_sda_update(current_psp);
1.1.1.8   root     11699:        }
1.1       root     11700: }
                   11701: 
                   11702: inline void msdos_int_21h_51h()
                   11703: {
                   11704:        REG16(BX) = current_psp;
                   11705: }
                   11706: 
                   11707: inline void msdos_int_21h_52h()
                   11708: {
1.1.1.25  root     11709:        SREG(ES) = DOS_INFO_TOP >> 4;
1.1.1.3   root     11710:        i386_load_segment_descriptor(ES);
1.1.1.25  root     11711:        REG16(BX) = offsetof(dos_info_t, first_dpb);
1.1       root     11712: }
                   11713: 
1.1.1.43  root     11714: inline void msdos_int_21h_53h()
                   11715: {
                   11716:        dpb_t *dpb = (dpb_t *)(mem + SREG_BASE(ES) + REG16(BP));
                   11717:        bpb_t *bpb = (bpb_t *)(mem + SREG_BASE(DS) + REG16(SI));
                   11718:        
                   11719:        dpb->bytes_per_sector = bpb->bytes_per_sector;
                   11720:        dpb->highest_sector_num = bpb->sectors_per_track - 1;
                   11721:        dpb->shift_count = 0;
                   11722:        dpb->reserved_sectors = 0;
                   11723:        dpb->fat_num = bpb->fat_num;
                   11724:        dpb->root_entries = bpb->root_entries;
                   11725:        dpb->first_data_sector = 0;
                   11726:        if(bpb->sectors_per_cluster != 0) {
                   11727:                dpb->highest_cluster_num = (UINT16)(((REG16(CX) == 0x4558 && bpb->total_sectors == 0) ? bpb->ext_total_sectors : bpb->total_sectors) / bpb->sectors_per_cluster + 1);
                   11728:        } else {
                   11729:                dpb->highest_cluster_num = 0;
                   11730:        }
                   11731:        dpb->sectors_per_fat = (REG16(CX) == 0x4558 && bpb->sectors_per_fat == 0) ? bpb->ext_sectors_per_fat : bpb->sectors_per_fat;
                   11732:        dpb->first_dir_sector = 0;
                   11733:        dpb->device_driver_header = 0;
                   11734:        dpb->media_type = bpb->media_type;
                   11735:        dpb->drive_accessed = 0;
                   11736:        dpb->next_dpb_ofs = 0xffff;
                   11737:        dpb->next_dpb_seg = 0xffff;
                   11738:        dpb->first_free_cluster = 0;
                   11739:        dpb->free_clusters = 0xffff;
                   11740: }
                   11741: 
1.1       root     11742: inline void msdos_int_21h_54h()
                   11743: {
                   11744:        process_t *process = msdos_process_info_get(current_psp);
                   11745:        
                   11746:        REG8(AL) = process->verify;
                   11747: }
                   11748: 
                   11749: inline void msdos_int_21h_55h()
                   11750: {
                   11751:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   11752:        
                   11753:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
                   11754:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   11755:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   11756:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   11757:        psp->parent_psp = current_psp;
                   11758: }
                   11759: 
                   11760: inline void msdos_int_21h_56h(int lfn)
                   11761: {
                   11762:        char src[MAX_PATH], dst[MAX_PATH];
1.1.1.3   root     11763:        strcpy(src, msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn));
                   11764:        strcpy(dst, msdos_trimmed_path((char *)(mem + SREG_BASE(ES) + REG16(DI)), lfn));
1.1       root     11765:        
                   11766:        if(rename(src, dst)) {
                   11767:                REG16(AX) = errno;
1.1.1.3   root     11768:                m_CF = 1;
1.1       root     11769:        }
                   11770: }
                   11771: 
                   11772: inline void msdos_int_21h_57h()
                   11773: {
                   11774:        FILETIME time, local;
1.1.1.14  root     11775:        FILETIME *ctime, *atime, *mtime;
1.1.1.21  root     11776:        HANDLE hHandle;
1.1       root     11777:        
1.1.1.21  root     11778:        if((hHandle = (HANDLE)_get_osfhandle(REG16(BX))) == INVALID_HANDLE_VALUE) {
1.1.1.14  root     11779:                REG16(AX) = (UINT16)GetLastError();
                   11780:                m_CF = 1;
                   11781:                return;
                   11782:        }
                   11783:        ctime = atime = mtime = NULL;
                   11784:        
1.1       root     11785:        switch(REG8(AL)) {
                   11786:        case 0x00:
1.1.1.6   root     11787:        case 0x01:
1.1.1.14  root     11788:                mtime = &time;
1.1.1.6   root     11789:                break;
                   11790:        case 0x04:
                   11791:        case 0x05:
1.1.1.14  root     11792:                atime = &time;
1.1       root     11793:                break;
1.1.1.6   root     11794:        case 0x06:
                   11795:        case 0x07:
1.1.1.14  root     11796:                ctime = &time;
                   11797:                break;
                   11798:        default:
1.1.1.22  root     11799:                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     11800:                REG16(AX) = 0x01;
                   11801:                m_CF = 1;
                   11802:                return;
                   11803:        }
                   11804:        if(REG8(AL) & 1) {
1.1       root     11805:                DosDateTimeToFileTime(REG16(DX), REG16(CX), &local);
                   11806:                LocalFileTimeToFileTime(&local, &time);
1.1.1.21  root     11807:                if(!SetFileTime(hHandle, ctime, atime, mtime)) {
1.1       root     11808:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     11809:                        m_CF = 1;
1.1       root     11810:                }
1.1.1.14  root     11811:        } else {
1.1.1.21  root     11812:                if(!GetFileTime(hHandle, ctime, atime, mtime)) {
1.1.1.14  root     11813:                        // assume a device and use the current time
                   11814:                        GetSystemTimeAsFileTime(&time);
                   11815:                }
                   11816:                FileTimeToLocalFileTime(&time, &local);
                   11817:                FileTimeToDosDateTime(&local, &REG16(DX), &REG16(CX));
1.1       root     11818:        }
                   11819: }
                   11820: 
                   11821: inline void msdos_int_21h_58h()
                   11822: {
                   11823:        switch(REG8(AL)) {
                   11824:        case 0x00:
1.1.1.7   root     11825:                REG16(AX) = malloc_strategy;
                   11826:                break;
                   11827:        case 0x01:
1.1.1.24  root     11828: //             switch(REG16(BX)) {
                   11829:                switch(REG8(BL)) {
1.1.1.7   root     11830:                case 0x0000:
                   11831:                case 0x0001:
                   11832:                case 0x0002:
                   11833:                case 0x0040:
                   11834:                case 0x0041:
                   11835:                case 0x0042:
                   11836:                case 0x0080:
                   11837:                case 0x0081:
                   11838:                case 0x0082:
                   11839:                        malloc_strategy = REG16(BX);
1.1.1.23  root     11840:                        msdos_sda_update(current_psp);
1.1.1.7   root     11841:                        break;
                   11842:                default:
1.1.1.22  root     11843:                        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     11844:                        REG16(AX) = 0x01;
                   11845:                        m_CF = 1;
                   11846:                        break;
                   11847:                }
                   11848:                break;
                   11849:        case 0x02:
1.1.1.19  root     11850:                REG8(AL) = msdos_mem_get_umb_linked() ? 1 : 0;
1.1.1.7   root     11851:                break;
                   11852:        case 0x03:
1.1.1.24  root     11853: //             switch(REG16(BX)) {
                   11854:                switch(REG8(BL)) {
1.1.1.7   root     11855:                case 0x0000:
1.1.1.19  root     11856:                        msdos_mem_unlink_umb();
                   11857:                        break;
1.1.1.7   root     11858:                case 0x0001:
1.1.1.19  root     11859:                        msdos_mem_link_umb();
1.1.1.7   root     11860:                        break;
                   11861:                default:
1.1.1.22  root     11862:                        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     11863:                        REG16(AX) = 0x01;
                   11864:                        m_CF = 1;
                   11865:                        break;
                   11866:                }
1.1       root     11867:                break;
                   11868:        default:
1.1.1.22  root     11869:                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     11870:                REG16(AX) = 0x01;
1.1.1.3   root     11871:                m_CF = 1;
1.1       root     11872:                break;
                   11873:        }
                   11874: }
                   11875: 
                   11876: inline void msdos_int_21h_59h()
                   11877: {
1.1.1.47  root     11878:        if(REG16(BX) == 0x0000) {
                   11879:                sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   11880:                
                   11881:                REG16(AX) = sda->extended_error_code;
                   11882:                REG8(BH)  = sda->error_class;
                   11883:                REG8(BL)  = sda->suggested_action;
                   11884:                REG8(CH)  = sda->locus_of_last_error;
                   11885:                // XXX: GW-BASIC 3.23 invites the registers contents of CL, DX, SI, DI, DS, and ES are not destroyed
                   11886:                if(sda->int21h_5d0ah_called != 0) {
                   11887:                        REG8(CL)  = sda->int21h_5d0ah_cl;
                   11888:                        REG16(DX) = sda->int21h_5d0ah_dx;
                   11889: //                     REG16(SI) = sda->int21h_5d0ah_si;
                   11890:                        REG16(DI) = sda->last_error_pointer.w.l;
                   11891: //                     SREG(DS)  = sda->int21h_5d0ah_ds;
                   11892: //                     i386_load_segment_descriptor(DS);
                   11893:                        SREG(ES)  = sda->last_error_pointer.w.h;
                   11894:                        i386_load_segment_descriptor(ES);
                   11895:                }
                   11896:                sda->int21h_5d0ah_called = 0;
                   11897: //     } else if(REG16(BX) == 0x0001) {
                   11898: //             // European MS-DOS 4.0 - Get Hard Error Information
                   11899:        } else {
                   11900:                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));
                   11901:                REG16(AX) = 0x01;
                   11902:                m_CF = 1;
                   11903:        }
1.1       root     11904: }
                   11905: 
                   11906: inline void msdos_int_21h_5ah()
                   11907: {
1.1.1.3   root     11908:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     11909:        int len = strlen(path);
                   11910:        char tmp[MAX_PATH];
                   11911:        
                   11912:        if(GetTempFileName(path, "TMP", 0, tmp)) {
                   11913:                int fd = _open(tmp, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   11914:                
                   11915:                SetFileAttributes(tmp, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   11916:                REG16(AX) = fd;
                   11917:                msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     11918:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     11919:                
                   11920:                strcpy(path, tmp);
                   11921:                int dx = REG16(DX) + len;
1.1.1.3   root     11922:                int ds = SREG(DS);
1.1       root     11923:                while(dx > 0xffff) {
                   11924:                        dx -= 0x10;
                   11925:                        ds++;
                   11926:                }
                   11927:                REG16(DX) = dx;
1.1.1.3   root     11928:                SREG(DS) = ds;
                   11929:                i386_load_segment_descriptor(DS);
1.1       root     11930:        } else {
                   11931:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     11932:                m_CF = 1;
1.1       root     11933:        }
                   11934: }
                   11935: 
                   11936: inline void msdos_int_21h_5bh()
                   11937: {
1.1.1.45  root     11938:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     11939:        
1.1.1.45  root     11940: //     if(msdos_is_existing_file(path)) {
                   11941:        if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1       root     11942:                // already exists
                   11943:                REG16(AX) = 0x50;
1.1.1.3   root     11944:                m_CF = 1;
1.1       root     11945:        } else {
                   11946:                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   11947:                
                   11948:                if(fd != -1) {
                   11949:                        SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   11950:                        REG16(AX) = fd;
                   11951:                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     11952:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     11953:                } else {
                   11954:                        REG16(AX) = errno;
1.1.1.3   root     11955:                        m_CF = 1;
1.1       root     11956:                }
                   11957:        }
                   11958: }
                   11959: 
                   11960: inline void msdos_int_21h_5ch()
                   11961: {
                   11962:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11963:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11964:        
1.1.1.20  root     11965:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     11966:                if(REG8(AL) == 0 || REG8(AL) == 1) {
1.1.1.35  root     11967:                        static const int modes[2] = {_LK_LOCK, _LK_UNLCK};
1.1.1.20  root     11968:                        UINT32 pos = _tell(fd);
                   11969:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), SEEK_SET);
                   11970:                        if(_locking(fd, modes[REG8(AL)], (REG16(SI) << 16) | REG16(DI))) {
1.1       root     11971:                                REG16(AX) = errno;
1.1.1.3   root     11972:                                m_CF = 1;
1.1       root     11973:                        }
1.1.1.20  root     11974:                        _lseek(fd, pos, SEEK_SET);
1.1.1.26  root     11975:                        
1.1       root     11976:                        // some seconds may be passed in _locking()
1.1.1.35  root     11977:                        REQUEST_HARDWRE_UPDATE();
1.1       root     11978:                } else {
                   11979:                        REG16(AX) = 0x01;
1.1.1.3   root     11980:                        m_CF = 1;
1.1       root     11981:                }
                   11982:        } else {
                   11983:                REG16(AX) = 0x06;
1.1.1.3   root     11984:                m_CF = 1;
1.1       root     11985:        }
                   11986: }
                   11987: 
1.1.1.22  root     11988: inline void msdos_int_21h_5dh()
                   11989: {
                   11990:        switch(REG8(AL)) {
1.1.1.45  root     11991:        case 0x00:
                   11992:                if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x12) == 0x0000) {
                   11993:                        // current system
                   11994:                        static bool reenter = false;
                   11995:                        if(!reenter) {
                   11996:                                UINT32 offset = SREG_BASE(DS) + REG16(DX);
                   11997:                                REG16(AX) = *(UINT16 *)(mem + offset + 0x00);
                   11998:                                REG16(BX) = *(UINT16 *)(mem + offset + 0x02);
                   11999:                                REG16(CX) = *(UINT16 *)(mem + offset + 0x04);
                   12000:                                REG16(DX) = *(UINT16 *)(mem + offset + 0x06);
                   12001:                                REG16(SI) = *(UINT16 *)(mem + offset + 0x08);
                   12002:                                REG16(DI) = *(UINT16 *)(mem + offset + 0x0a);
                   12003:                                SREG(DS)  = *(UINT16 *)(mem + offset + 0x0c);
                   12004:                                SREG(ES)  = *(UINT16 *)(mem + offset + 0x0e);
                   12005:                                i386_load_segment_descriptor(DS);
                   12006:                                i386_load_segment_descriptor(ES);
                   12007:                                reenter = true;
                   12008:                                try {
                   12009:                                        msdos_syscall(0x21);
                   12010:                                } catch(...) {
                   12011:                                }
                   12012:                                reenter = false;
                   12013:                        }
                   12014:                } else {
                   12015:                        REG16(AX) = 0x49; //  network software not installed
                   12016:                        m_CF = 1;
                   12017:                }
                   12018:                break;
1.1.1.22  root     12019:        case 0x06: // get address of dos swappable data area
1.1.1.23  root     12020:                SREG(DS) = (SDA_TOP >> 4);
                   12021:                i386_load_segment_descriptor(DS);
                   12022:                REG16(SI) = offsetof(sda_t, crit_error_flag);
                   12023:                REG16(CX) = 0x80;
                   12024:                REG16(DX) = 0x1a;
                   12025:                break;
1.1.1.45  root     12026:        case 0x07: // get redirected printer mode
                   12027:        case 0x08: // set redirected printer mode
                   12028:        case 0x09: // flush redirected printer output
                   12029:                REG16(AX) = 0x49; //  network software not installed
                   12030:                m_CF = 1;
                   12031:                break;
1.1.1.43  root     12032:        case 0x0a: // set extended error information
                   12033:                {
                   12034:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
1.1.1.47  root     12035:                        sda->int21h_5d0ah_called    = 1;
1.1.1.43  root     12036:                        sda->extended_error_code    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00); // AX
1.1.1.47  root     12037:                        // XXX: which one is correct ???
                   12038: #if 1
                   12039:                        // Ralf Brown's Interrupt List and DR-DOS System and Programmer's Guide
1.1.1.43  root     12040:                        sda->suggested_action       = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02); // BL
                   12041:                        sda->error_class            = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x03); // BH
1.1.1.47  root     12042:                        sda->int21h_5d0ah_cl        = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04); // CL
1.1.1.43  root     12043:                        sda->locus_of_last_error    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x05); // CH
1.1.1.47  root     12044: #else
                   12045:                        // PC DOS 7 Technical Update
                   12046:                        sda->error_class            = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02); // BH
                   12047:                        sda->suggested_action       = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x03); // BL
                   12048:                        sda->locus_of_last_error    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04); // CH
                   12049:                        sda->int21h_5d0ah_cl        = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x05); // CL
                   12050: #endif
                   12051:                        sda->int21h_5d0ah_dx        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06); // DX
                   12052: //                     sda->int21h_5d0ah_si        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08); // SI
1.1.1.43  root     12053:                        sda->last_error_pointer.w.l = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a); // DI
1.1.1.47  root     12054: //                     sda->int21h_5d0ah_ds        = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c); // DS
1.1.1.43  root     12055:                        sda->last_error_pointer.w.h = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e); // ES
                   12056:                }
                   12057:                break;
1.1.1.23  root     12058:        case 0x0b: // get dos swappable data areas
1.1.1.22  root     12059:                REG16(AX) = 0x01;
                   12060:                m_CF = 1;
                   12061:                break;
                   12062:        default:
                   12063:                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));
                   12064:                REG16(AX) = 0x01;
                   12065:                m_CF = 1;
                   12066:                break;
                   12067:        }
                   12068: }
                   12069: 
1.1.1.42  root     12070: inline void msdos_int_21h_5eh()
                   12071: {
                   12072:        switch(REG8(AL)) {
                   12073:        case 0x00:
                   12074:                {
                   12075:                        char name[256] = {0};
                   12076:                        DWORD dwSize = 256;
                   12077:                        
                   12078:                        if(GetComputerName(name, &dwSize)) {
                   12079:                                char *dest = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   12080:                                for(int i = 0; i < 15; i++) {
                   12081:                                        dest[i] = (i < strlen(name)) ? name[i] : ' ';
                   12082:                                }
                   12083:                                dest[15] = '\0';
                   12084:                                REG8(CH) = 0x01; // nonzero valid
                   12085:                                REG8(CL) = 0x01; // NetBIOS number for machine name ???
                   12086:                        } else {
                   12087:                                REG16(AX) = 0x01;
                   12088:                                m_CF = 1;
                   12089:                        }
                   12090:                }
                   12091:                break;
                   12092:        default:
1.1.1.45  root     12093: //             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));
                   12094: //             REG16(AX) = 0x01;
                   12095:                REG16(AX) = 0x49; //  network software not installed
1.1.1.42  root     12096:                m_CF = 1;
                   12097:                break;
                   12098:        }
                   12099: }
                   12100: 
1.1.1.30  root     12101: inline void msdos_int_21h_5fh()
                   12102: {
                   12103:        switch(REG8(AL)) {
1.1.1.42  root     12104:        case 0x05:
1.1.1.44  root     12105:                REG16(BP) = 0;
                   12106:                for(int i = 0; i < 26; i++) {
                   12107:                        if(msdos_is_remote_drive(i)) {
                   12108:                                REG16(BP)++;
1.1.1.42  root     12109:                        }
                   12110:                }
1.1.1.30  root     12111:        case 0x02:
1.1.1.44  root     12112:                for(int i = 0, index = 0; i < 26; i++) {
                   12113:                        if(msdos_is_remote_drive(i)) {
                   12114:                                if(index == REG16(BX)) {
                   12115:                                        char volume[] = "A:";
1.1.1.30  root     12116:                                        volume[0] = 'A' + i;
1.1.1.44  root     12117:                                        DWORD dwSize = 128;
                   12118:                                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), volume);
                   12119:                                        WNetGetConnection(volume, (char *)(mem + SREG_BASE(ES) + REG16(DI)), &dwSize);
                   12120:                                        REG8(BH) = 0x00; // valid
                   12121:                                        REG8(BL) = 0x04; // disk drive
                   12122:                                        REG16(CX) = 0x00; // LANtastic
                   12123:                                        return;
1.1.1.30  root     12124:                                }
1.1.1.44  root     12125:                                index++;
1.1.1.30  root     12126:                        }
                   12127:                }
                   12128:                REG16(AX) = 0x12; // no more files
                   12129:                m_CF = 1;
                   12130:                break;
1.1.1.44  root     12131:        case 0x07:
                   12132:                if(msdos_is_valid_drive(REG8(DL))) {
                   12133:                        msdos_cds_update(REG8(DL));
                   12134:                } else {
                   12135:                        REG16(AX) = 0x0f; // invalid drive
                   12136:                        m_CF = 1;
                   12137:                }
                   12138:                break;
                   12139:        case 0x08:
                   12140:                if(msdos_is_valid_drive(REG8(DL))) {
                   12141:                        cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * REG8(DL));
                   12142:                        cds->drive_attrib = 0x0000;
                   12143:                } else {
                   12144:                        REG16(AX) = 0x0f; // invalid drive
                   12145:                        m_CF = 1;
                   12146:                }
                   12147:                break;
1.1.1.30  root     12148:        default:
1.1.1.45  root     12149: //             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));
                   12150: //             REG16(AX) = 0x01;
                   12151:                REG16(AX) = 0x49; //  network software not installed
1.1.1.30  root     12152:                m_CF = 1;
                   12153:                break;
                   12154:        }
                   12155: }
                   12156: 
1.1       root     12157: inline void msdos_int_21h_60h(int lfn)
                   12158: {
1.1.1.45  root     12159:        char full[MAX_PATH];
                   12160:        const char *path = NULL;
1.1.1.14  root     12161:        
1.1       root     12162:        if(lfn) {
1.1.1.14  root     12163:                char *name;
                   12164:                *full = '\0';
1.1.1.3   root     12165:                GetFullPathName((char *)(mem + SREG_BASE(DS) + REG16(SI)), MAX_PATH, full, &name);
1.1.1.14  root     12166:                switch(REG8(CL)) {
                   12167:                case 1:
                   12168:                        GetShortPathName(full, full, MAX_PATH);
                   12169:                        my_strupr(full);
                   12170:                        break;
                   12171:                case 2:
                   12172:                        GetLongPathName(full, full, MAX_PATH);
                   12173:                        break;
                   12174:                }
                   12175:                path = full;
                   12176:        } else {
                   12177:                path = msdos_short_full_path((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   12178:        }
                   12179:        if(*path != '\0') {
                   12180:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
1.1       root     12181:        } else {
1.1.1.14  root     12182:                REG16(AX) = (UINT16)GetLastError();
                   12183:                m_CF = 1;
1.1       root     12184:        }
                   12185: }
                   12186: 
                   12187: inline void msdos_int_21h_61h()
                   12188: {
                   12189:        REG8(AL) = 0;
                   12190: }
                   12191: 
                   12192: inline void msdos_int_21h_62h()
                   12193: {
                   12194:        REG16(BX) = current_psp;
                   12195: }
                   12196: 
                   12197: inline void msdos_int_21h_63h()
                   12198: {
                   12199:        switch(REG8(AL)) {
                   12200:        case 0x00:
1.1.1.3   root     12201:                SREG(DS) = (DBCS_TABLE >> 4);
                   12202:                i386_load_segment_descriptor(DS);
1.1       root     12203:                REG16(SI) = (DBCS_TABLE & 0x0f);
                   12204:                REG8(AL) = 0x00;
                   12205:                break;
1.1.1.22  root     12206:        case 0x01: // set korean input mode
                   12207:        case 0x02: // get korean input mode
                   12208:                REG8(AL) = 0xff; // not supported
                   12209:                break;
1.1       root     12210:        default:
1.1.1.22  root     12211:                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     12212:                REG16(AX) = 0x01;
1.1.1.3   root     12213:                m_CF = 1;
1.1       root     12214:                break;
                   12215:        }
                   12216: }
                   12217: 
1.1.1.25  root     12218: UINT16 get_extended_country_info(UINT8 func)
1.1       root     12219: {
1.1.1.25  root     12220:        switch(func) {
1.1.1.17  root     12221:        case 0x01:
                   12222:                if(REG16(CX) >= 5) {
1.1.1.19  root     12223:                        UINT8 data[1 + 2 + 2 + 2 + sizeof(country_info_t)];
1.1.1.17  root     12224:                        if(REG16(CX) > sizeof(data))            // cx = actual transfer size
                   12225:                                REG16(CX) = sizeof(data);
                   12226:                        ZeroMemory(data, sizeof(data));
                   12227:                        data[0] = 0x01;
                   12228:                        *(UINT16 *)(data + 1) = REG16(CX) - 3;
1.1.1.19  root     12229:                        *(UINT16 *)(data + 3) = get_country_info((country_info_t*)(data + 7));
1.1.1.17  root     12230:                        *(UINT16 *)(data + 5) = active_code_page;
                   12231:                        memcpy(mem + SREG_BASE(ES) + REG16(DI), data, REG16(CX));
1.1.1.25  root     12232: //                     REG16(AX) = active_code_page;
1.1.1.17  root     12233:                } else {
1.1.1.25  root     12234:                        return(0x08); // insufficient memory
1.1.1.17  root     12235:                }
                   12236:                break;
                   12237:        case 0x02:
                   12238:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   12239:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (UPPERTABLE_TOP & 0x0f);
                   12240:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (UPPERTABLE_TOP >> 4);
1.1.1.25  root     12241: //             REG16(AX) = active_code_page;
1.1.1.17  root     12242:                REG16(CX) = 0x05;
                   12243:                break;
1.1.1.23  root     12244:        case 0x03:
                   12245:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   12246:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (LOWERTABLE_TOP & 0x0f);
                   12247:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (LOWERTABLE_TOP >> 4);
1.1.1.25  root     12248: //             REG16(AX) = active_code_page;
1.1.1.23  root     12249:                REG16(CX) = 0x05;
                   12250:                break;
1.1.1.17  root     12251:        case 0x04:
                   12252:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x04;
                   12253:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_UPPERTABLE_TOP & 0x0f);
                   12254:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_UPPERTABLE_TOP >> 4);
1.1.1.25  root     12255: //             REG16(AX) = active_code_page;
1.1.1.17  root     12256:                REG16(CX) = 0x05;
                   12257:                break;
                   12258:        case 0x05:
                   12259:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x05;
                   12260:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_TERMINATOR_TOP & 0x0f);
                   12261:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_TERMINATOR_TOP >> 4);
1.1.1.25  root     12262: //             REG16(AX) = active_code_page;
1.1.1.17  root     12263:                REG16(CX) = 0x05;
                   12264:                break;
                   12265:        case 0x06:
                   12266:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x06;
                   12267:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (COLLATING_TABLE_TOP & 0x0f);
                   12268:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (COLLATING_TABLE_TOP >> 4);
1.1.1.25  root     12269: //             REG16(AX) = active_code_page;
1.1.1.17  root     12270:                REG16(CX) = 0x05;
                   12271:                break;
1.1       root     12272:        case 0x07:
1.1.1.3   root     12273:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x07;
                   12274:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (DBCS_TOP & 0x0f);
                   12275:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (DBCS_TOP >> 4);
1.1.1.25  root     12276: //             REG16(AX) = active_code_page;
1.1       root     12277:                REG16(CX) = 0x05;
                   12278:                break;
1.1.1.25  root     12279:        default:
                   12280:                return(0x01); // function number invalid
                   12281:        }
                   12282:        return(0x00);
                   12283: }
                   12284: 
                   12285: inline void msdos_int_21h_65h()
                   12286: {
                   12287:        char tmp[0x10000];
                   12288:        
                   12289:        switch(REG8(AL)) {
1.1.1.43  root     12290:        case 0x00:
                   12291:                if(REG16(CX) >= 7) {
                   12292:                        set_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)), REG16(CX) - 7);
                   12293:                        REG16(AX) = system_code_page;
                   12294:                } else {
                   12295:                        REG16(AX) = 0x0c;
                   12296:                        m_CF = 1;
                   12297:                }
                   12298:                break;
1.1.1.25  root     12299:        case 0x01:
                   12300:        case 0x02:
                   12301:        case 0x03:
                   12302:        case 0x04:
                   12303:        case 0x05:
                   12304:        case 0x06:
                   12305:        case 0x07:
                   12306:                {
                   12307:                        UINT16 result = get_extended_country_info(REG8(AL));
                   12308:                        if(result) {
                   12309:                                REG16(AX) = result;
                   12310:                                m_CF = 1;
                   12311:                        } else {
                   12312:                                REG16(AX) = active_code_page; // FIXME: is this correct???
                   12313:                        }
                   12314:                }
                   12315:                break;
1.1       root     12316:        case 0x20:
1.1.1.25  root     12317:        case 0xa0:
1.1.1.19  root     12318:                memset(tmp, 0, sizeof(tmp));
                   12319:                tmp[0] = REG8(DL);
1.1       root     12320:                my_strupr(tmp);
                   12321:                REG8(DL) = tmp[0];
                   12322:                break;
                   12323:        case 0x21:
1.1.1.25  root     12324:        case 0xa1:
1.1       root     12325:                memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     12326:                memcpy(tmp, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     12327:                my_strupr(tmp);
1.1.1.3   root     12328:                memcpy(mem + SREG_BASE(DS) + REG16(DX), tmp, REG16(CX));
1.1       root     12329:                break;
                   12330:        case 0x22:
1.1.1.25  root     12331:        case 0xa2:
1.1.1.3   root     12332:                my_strupr((char *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1       root     12333:                break;
1.1.1.25  root     12334:        case 0x23:
                   12335:                // FIXME: need to check multi-byte (kanji) charactre?
1.1.1.45  root     12336:                if(REG8(DL) == 'N' || REG8(DL) == 'n' || (REG8(DL) == 0x82 && REG8(DH) == 0x6d) || (REG8(DL) == 0x82 && REG8(DH) == 0x8e)) {
1.1.1.25  root     12337:                        // 826dh/828eh: multi-byte (kanji) N and n
1.1.1.45  root     12338:                        REG16(AX) = 0x00;
                   12339:                } else if(REG8(DL) == 'Y' || REG8(DL) == 'y' || (REG8(DL) == 0x82 && REG8(DH) == 0x78) || (REG8(DL) == 0x82 && REG8(DH) == 0x99)) {
                   12340:                        // 8278h/8299h: multi-byte (kanji) Y and y
1.1.1.25  root     12341:                        REG16(AX) = 0x01;
                   12342:                } else {
                   12343:                        REG16(AX) = 0x02;
                   12344:                }
                   12345:                break;
1.1       root     12346:        default:
1.1.1.22  root     12347:                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     12348:                REG16(AX) = 0x01;
1.1.1.3   root     12349:                m_CF = 1;
1.1       root     12350:                break;
                   12351:        }
                   12352: }
                   12353: 
                   12354: inline void msdos_int_21h_66h()
                   12355: {
                   12356:        switch(REG8(AL)) {
                   12357:        case 0x01:
                   12358:                REG16(BX) = active_code_page;
                   12359:                REG16(DX) = system_code_page;
                   12360:                break;
                   12361:        case 0x02:
                   12362:                if(active_code_page == REG16(BX)) {
                   12363:                        REG16(AX) = 0xeb41;
                   12364:                } else if(_setmbcp(REG16(BX)) == 0) {
                   12365:                        active_code_page = REG16(BX);
1.1.1.17  root     12366:                        msdos_nls_tables_update();
1.1       root     12367:                        REG16(AX) = 0xeb41;
1.1.1.32  root     12368:                        SetConsoleCP(active_code_page);
                   12369:                        SetConsoleOutputCP(active_code_page);
1.1       root     12370:                } else {
                   12371:                        REG16(AX) = 0x25;
1.1.1.3   root     12372:                        m_CF = 1;
1.1       root     12373:                }
                   12374:                break;
                   12375:        default:
1.1.1.22  root     12376:                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     12377:                REG16(AX) = 0x01;
1.1.1.3   root     12378:                m_CF = 1;
1.1       root     12379:                break;
                   12380:        }
                   12381: }
                   12382: 
                   12383: inline void msdos_int_21h_67h()
                   12384: {
                   12385:        process_t *process = msdos_process_info_get(current_psp);
                   12386:        
                   12387:        if(REG16(BX) <= MAX_FILES) {
                   12388:                process->max_files = max(REG16(BX), 20);
                   12389:        } else {
                   12390:                REG16(AX) = 0x08;
1.1.1.3   root     12391:                m_CF = 1;
1.1       root     12392:        }
                   12393: }
                   12394: 
                   12395: inline void msdos_int_21h_68h()
                   12396: {
                   12397:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     12398:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     12399:        
1.1.1.20  root     12400:        if(fd < process->max_files && file_handler[fd].valid) {
                   12401:                // fflush(_fdopen(fd, ""));
1.1       root     12402:        } else {
                   12403:                REG16(AX) = 0x06;
1.1.1.3   root     12404:                m_CF = 1;
1.1       root     12405:        }
                   12406: }
                   12407: 
                   12408: inline void msdos_int_21h_69h()
                   12409: {
1.1.1.3   root     12410:        drive_info_t *info = (drive_info_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     12411:        char path[] = "A:\\";
                   12412:        char volume_label[MAX_PATH];
                   12413:        DWORD serial_number = 0;
                   12414:        char file_system[MAX_PATH];
                   12415:        
                   12416:        if(REG8(BL) == 0) {
                   12417:                path[0] = 'A' + _getdrive() - 1;
                   12418:        } else {
                   12419:                path[0] = 'A' + REG8(BL) - 1;
                   12420:        }
                   12421:        
                   12422:        switch(REG8(AL)) {
                   12423:        case 0x00:
                   12424:                if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
                   12425:                        info->info_level = 0;
                   12426:                        info->serial_number = serial_number;
                   12427:                        memset(info->volume_label, 0x20, 11);
                   12428:                        memcpy(info->volume_label, volume_label, min(strlen(volume_label), 11));
                   12429:                        memset(info->file_system, 0x20, 8);
                   12430:                        memcpy(info->file_system, file_system, min(strlen(file_system), 8));
                   12431:                } else {
                   12432:                        REG16(AX) = errno;
1.1.1.3   root     12433:                        m_CF = 1;
1.1       root     12434:                }
                   12435:                break;
                   12436:        case 0x01:
                   12437:                REG16(AX) = 0x03;
1.1.1.3   root     12438:                m_CF = 1;
1.1.1.45  root     12439:                break;
                   12440:        default:
                   12441:                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));
                   12442:                REG16(AX) = 0x01;
                   12443:                m_CF = 1;
                   12444:                break;
1.1       root     12445:        }
                   12446: }
                   12447: 
                   12448: inline void msdos_int_21h_6ah()
                   12449: {
                   12450:        REG8(AH) = 0x68;
                   12451:        msdos_int_21h_68h();
                   12452: }
                   12453: 
                   12454: inline void msdos_int_21h_6bh()
                   12455: {
1.1.1.45  root     12456:        REG8(AL) = 0x00;
1.1       root     12457: }
                   12458: 
                   12459: inline void msdos_int_21h_6ch(int lfn)
                   12460: {
1.1.1.45  root     12461:        const char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(SI)), lfn);
1.1       root     12462:        int mode = REG8(BL) & 0x03;
                   12463:        
                   12464:        if(mode < 0x03) {
1.1.1.29  root     12465:                if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1       root     12466:                        // file exists
                   12467:                        if(REG8(DL) & 1) {
1.1.1.37  root     12468:                                int fd = -1;
                   12469:                                int sio_port = 0;
                   12470:                                int lpt_port = 0;
1.1       root     12471:                                
1.1.1.45  root     12472:                                if(msdos_is_device_path(path)) {
                   12473:                                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1.1.11  root     12474:                                } else {
1.1.1.13  root     12475:                                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     12476:                                }
1.1       root     12477:                                if(fd != -1) {
                   12478:                                        REG16(AX) = fd;
                   12479:                                        REG16(CX) = 1;
1.1.1.45  root     12480:                                        msdos_file_handler_open(fd, path, _isatty(fd), mode, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     12481:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12482:                                } else {
                   12483:                                        REG16(AX) = errno;
1.1.1.3   root     12484:                                        m_CF = 1;
1.1       root     12485:                                }
                   12486:                        } else if(REG8(DL) & 2) {
                   12487:                                int attr = GetFileAttributes(path);
1.1.1.37  root     12488:                                int fd = -1;
                   12489:                                int sio_port = 0;
                   12490:                                int lpt_port = 0;
1.1       root     12491:                                
1.1.1.45  root     12492:                                if(msdos_is_device_path(path)) {
                   12493:                                        fd = msdos_open_device(path, file_mode[mode].mode, &sio_port, &lpt_port);
1.1       root     12494:                                } else {
                   12495:                                        fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12496:                                }
                   12497:                                if(fd != -1) {
                   12498:                                        if(attr == -1) {
                   12499:                                                attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   12500:                                        }
                   12501:                                        SetFileAttributes(path, attr);
                   12502:                                        REG16(AX) = fd;
                   12503:                                        REG16(CX) = 3;
1.1.1.45  root     12504:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_device_info(path), current_psp, sio_port, lpt_port);
1.1.1.20  root     12505:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12506:                                } else {
                   12507:                                        REG16(AX) = errno;
1.1.1.3   root     12508:                                        m_CF = 1;
1.1       root     12509:                                }
                   12510:                        } else {
                   12511:                                REG16(AX) = 0x50;
1.1.1.3   root     12512:                                m_CF = 1;
1.1       root     12513:                        }
                   12514:                } else {
                   12515:                        // file not exists
                   12516:                        if(REG8(DL) & 0x10) {
                   12517:                                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   12518:                                
                   12519:                                if(fd != -1) {
                   12520:                                        SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   12521:                                        REG16(AX) = fd;
                   12522:                                        REG16(CX) = 2;
                   12523:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     12524:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     12525:                                } else {
                   12526:                                        REG16(AX) = errno;
1.1.1.3   root     12527:                                        m_CF = 1;
1.1       root     12528:                                }
                   12529:                        } else {
                   12530:                                REG16(AX) = 0x02;
1.1.1.3   root     12531:                                m_CF = 1;
1.1       root     12532:                        }
                   12533:                }
                   12534:        } else {
                   12535:                REG16(AX) = 0x0c;
1.1.1.3   root     12536:                m_CF = 1;
1.1       root     12537:        }
                   12538: }
                   12539: 
1.1.1.43  root     12540: inline void msdos_int_21h_70h()
                   12541: {
                   12542:        switch(REG8(AL)) {
1.1.1.48  root     12543:        case 0x00: // get ??? info
                   12544:        case 0x01: // set above info
                   12545: //             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));
                   12546:                REG16(AX) = 0x7000;
                   12547:                m_CF = 1;
                   12548:                break;
                   12549:        case 0x02: // set general internationalization info
1.1.1.43  root     12550:                if(REG16(CX) >= 7) {
                   12551:                        active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 5);
                   12552:                        msdos_nls_tables_update();
                   12553:                        set_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(SI) + 7), REG16(CX) - 7);
                   12554:                        REG16(AX) = system_code_page;
                   12555:                } else {
                   12556:                        REG16(AX) = 0x0c;
                   12557:                        m_CF = 1;
                   12558:                }
                   12559:                break;
                   12560:        default:
                   12561:                unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.48  root     12562:                REG16(AX) = 0x7000;
1.1.1.43  root     12563:                m_CF = 1;
                   12564:                break;
                   12565:        }
                   12566: }
                   12567: 
1.1       root     12568: inline void msdos_int_21h_710dh()
                   12569: {
                   12570:        // reset drive
                   12571: }
                   12572: 
1.1.1.48  root     12573: inline void msdos_int_21h_7141h()
1.1.1.17  root     12574: {
                   12575:        if(REG16(SI) == 0) {
1.1.1.48  root     12576:                msdos_int_21h_41h(1);
1.1.1.17  root     12577:                return;
                   12578:        }
                   12579:        if(REG16(SI) != 1) {
                   12580:                REG16(AX) = 5;
                   12581:                m_CF = 1;
                   12582:        }
                   12583:        /* wild card and matching attributes... */
                   12584:        char tmp[MAX_PATH * 2];
                   12585:        // copy search pathname (and quick check overrun)
                   12586:        ZeroMemory(tmp, sizeof(tmp));
                   12587:        tmp[MAX_PATH - 1] = '\0';
                   12588:        tmp[MAX_PATH] = 1;
                   12589:        strncpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(DX)), MAX_PATH);
                   12590:        
                   12591:        if(tmp[MAX_PATH - 1] != '\0' || tmp[MAX_PATH] != 1) {
                   12592:                REG16(AX) = 1;
                   12593:                m_CF = 1;
                   12594:                return;
                   12595:        }
                   12596:        for(char *s = tmp; *s; ++s) {
                   12597:                if(*s == '/') {
                   12598:                        *s = '\\';
                   12599:                }
                   12600:        }
                   12601:        char *tmp_name = (char *)_mbsrchr((unsigned char *)tmp, '\\');
                   12602:        if(tmp_name) {
                   12603:                ++tmp_name;
                   12604:        } else {
                   12605:                tmp_name = strchr(tmp, ':');
                   12606:                tmp_name = tmp_name ? tmp_name + 1 : tmp;
                   12607:        }
                   12608:        
                   12609:        WIN32_FIND_DATAA fd;
                   12610:        HANDLE fh = FindFirstFileA(tmp, &fd);
                   12611:        if(fh == INVALID_HANDLE_VALUE) {
                   12612:                REG16(AX) = 2;
                   12613:                m_CF = 1;
                   12614:                return;
                   12615:        }
                   12616:        do {
                   12617:                if(msdos_find_file_check_attribute(fd.dwFileAttributes, REG8(CL), REG8(CH)) && msdos_find_file_has_8dot3name(&fd)) {
                   12618:                        strcpy(tmp_name, fd.cFileName);
1.1.1.48  root     12619:                        if(remove(msdos_trimmed_path(tmp, 1))) {
1.1.1.17  root     12620:                                REG16(AX) = 5;
                   12621:                                m_CF = 1;
                   12622:                                break;
                   12623:                        }
                   12624:                }
                   12625:        } while(FindNextFileA(fh, &fd));
                   12626:        if(!m_CF) {
                   12627:                if(GetLastError() != ERROR_NO_MORE_FILES) {
                   12628:                        m_CF = 1;
                   12629:                        REG16(AX) = 2;
                   12630:                }
                   12631:        }
                   12632:        FindClose(fh);
                   12633: }
                   12634: 
1.1       root     12635: inline void msdos_int_21h_714eh()
                   12636: {
                   12637:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     12638:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
                   12639:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     12640:        WIN32_FIND_DATA fd;
                   12641:        
1.1.1.13  root     12642:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   12643:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   12644:                FindClose(dtainfo->find_handle);
                   12645:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12646:        }
                   12647:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     12648:        dtainfo->allowable_mask = REG8(CL);
                   12649:        dtainfo->required_mask = REG8(CH);
                   12650:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     12651:        
1.1.1.14  root     12652:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   12653:                dtainfo->allowable_mask &= ~8;
1.1       root     12654:        }
1.1.1.14  root     12655:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   12656:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13  root     12657:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   12658:                                FindClose(dtainfo->find_handle);
                   12659:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12660:                                break;
                   12661:                        }
                   12662:                }
                   12663:        }
1.1.1.13  root     12664:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     12665:                find->attrib = fd.dwFileAttributes;
                   12666:                msdos_find_file_conv_local_time(&fd);
                   12667:                if(REG16(SI) == 0) {
                   12668:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   12669:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   12670:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   12671:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   12672:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   12673:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   12674:                } else {
                   12675:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   12676:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   12677:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   12678:                }
                   12679:                find->size_hi = fd.nFileSizeHigh;
                   12680:                find->size_lo = fd.nFileSizeLow;
                   12681:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     12682:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     12683:                REG16(AX) = dtainfo - dtalist + 1;
                   12684:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     12685:                // volume label
                   12686:                find->attrib = 8;
                   12687:                find->size_hi = find->size_lo = 0;
                   12688:                strcpy(find->full_name, process->volume_label);
                   12689:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     12690:                dtainfo->allowable_mask &= ~8;
                   12691:                REG16(AX) = dtainfo - dtalist + 1;
1.1       root     12692:        } else {
                   12693:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     12694:                m_CF = 1;
1.1       root     12695:        }
                   12696: }
                   12697: 
                   12698: inline void msdos_int_21h_714fh()
                   12699: {
                   12700:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     12701:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     12702:        WIN32_FIND_DATA fd;
                   12703:        
1.1.1.14  root     12704:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   12705:                REG16(AX) = 6;
1.1.1.13  root     12706:                m_CF = 1;
                   12707:                return;
                   12708:        }
1.1.1.14  root     12709:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     12710:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   12711:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     12712:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13  root     12713:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   12714:                                        FindClose(dtainfo->find_handle);
                   12715:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12716:                                        break;
                   12717:                                }
                   12718:                        }
                   12719:                } else {
1.1.1.13  root     12720:                        FindClose(dtainfo->find_handle);
                   12721:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12722:                }
                   12723:        }
1.1.1.13  root     12724:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     12725:                find->attrib = fd.dwFileAttributes;
                   12726:                msdos_find_file_conv_local_time(&fd);
                   12727:                if(REG16(SI) == 0) {
                   12728:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   12729:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   12730:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   12731:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   12732:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   12733:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   12734:                } else {
                   12735:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   12736:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   12737:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   12738:                }
                   12739:                find->size_hi = fd.nFileSizeHigh;
                   12740:                find->size_lo = fd.nFileSizeLow;
                   12741:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     12742:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     12743:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     12744:                // volume label
                   12745:                find->attrib = 8;
                   12746:                find->size_hi = find->size_lo = 0;
                   12747:                strcpy(find->full_name, process->volume_label);
                   12748:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     12749:                dtainfo->allowable_mask &= ~8;
1.1       root     12750:        } else {
                   12751:                REG16(AX) = 0x12;
1.1.1.3   root     12752:                m_CF = 1;
1.1       root     12753:        }
                   12754: }
                   12755: 
                   12756: inline void msdos_int_21h_71a0h()
                   12757: {
                   12758:        DWORD max_component_len, file_sys_flag;
                   12759:        
1.1.1.14  root     12760:        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))) {
                   12761:                REG16(BX) = (UINT16)file_sys_flag & (FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES | FILE_UNICODE_ON_DISK | FILE_VOLUME_IS_COMPRESSED);
                   12762:                REG16(BX) |= 0x4000;                            // supports LFN functions
1.1       root     12763:                REG16(CX) = (UINT16)max_component_len;          // 255
                   12764:                REG16(DX) = (UINT16)max_component_len + 5;      // 260
                   12765:        } else {
                   12766:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     12767:                m_CF = 1;
1.1       root     12768:        }
                   12769: }
                   12770: 
                   12771: inline void msdos_int_21h_71a1h()
                   12772: {
1.1.1.14  root     12773:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   12774:                REG16(AX) = 6;
1.1.1.13  root     12775:                m_CF = 1;
                   12776:                return;
                   12777:        }
1.1.1.14  root     12778:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     12779:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   12780:                FindClose(dtainfo->find_handle);
                   12781:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     12782:        }
                   12783: }
                   12784: 
                   12785: inline void msdos_int_21h_71a6h()
                   12786: {
                   12787:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     12788:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   12789:        
1.1.1.3   root     12790:        UINT8 *buffer = (UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     12791:        struct _stat64 status;
                   12792:        DWORD serial_number = 0;
                   12793:        
1.1.1.20  root     12794:        if(fd < process->max_files && file_handler[fd].valid) {
                   12795:                if(_fstat64(fd, &status) == 0) {
                   12796:                        if(file_handler[fd].path[1] == ':') {
1.1       root     12797:                                // NOTE: we need to consider the network file path "\\host\share\"
                   12798:                                char volume[] = "A:\\";
1.1.1.20  root     12799:                                volume[0] = file_handler[fd].path[1];
1.1       root     12800:                                GetVolumeInformation(volume, NULL, 0, &serial_number, NULL, NULL, NULL, 0);
                   12801:                        }
1.1.1.20  root     12802:                        *(UINT32 *)(buffer + 0x00) = GetFileAttributes(file_handler[fd].path);
1.1       root     12803:                        *(UINT32 *)(buffer + 0x04) = (UINT32)(status.st_ctime & 0xffffffff);
                   12804:                        *(UINT32 *)(buffer + 0x08) = (UINT32)((status.st_ctime >> 32) & 0xffffffff);
                   12805:                        *(UINT32 *)(buffer + 0x0c) = (UINT32)(status.st_atime & 0xffffffff);
                   12806:                        *(UINT32 *)(buffer + 0x10) = (UINT32)((status.st_atime >> 32) & 0xffffffff);
                   12807:                        *(UINT32 *)(buffer + 0x14) = (UINT32)(status.st_mtime & 0xffffffff);
                   12808:                        *(UINT32 *)(buffer + 0x18) = (UINT32)((status.st_mtime >> 32) & 0xffffffff);
                   12809:                        *(UINT32 *)(buffer + 0x1c) = serial_number;
                   12810:                        *(UINT32 *)(buffer + 0x20) = (UINT32)((status.st_size >> 32) & 0xffffffff);
                   12811:                        *(UINT32 *)(buffer + 0x24) = (UINT32)(status.st_size & 0xffffffff);
                   12812:                        *(UINT32 *)(buffer + 0x28) = status.st_nlink;
1.1.1.14  root     12813:                        // this is dummy id and it will be changed when it is reopened...
1.1       root     12814:                        *(UINT32 *)(buffer + 0x2c) = 0;
1.1.1.20  root     12815:                        *(UINT32 *)(buffer + 0x30) = file_handler[fd].id;
1.1       root     12816:                } else {
                   12817:                        REG16(AX) = errno;
1.1.1.3   root     12818:                        m_CF = 1;
1.1       root     12819:                }
                   12820:        } else {
                   12821:                REG16(AX) = 0x06;
1.1.1.3   root     12822:                m_CF = 1;
1.1       root     12823:        }
                   12824: }
                   12825: 
                   12826: inline void msdos_int_21h_71a7h()
                   12827: {
                   12828:        switch(REG8(BL)) {
                   12829:        case 0x00:
1.1.1.3   root     12830:                if(!FileTimeToDosDateTime((FILETIME *)(mem + SREG_BASE(DS) + REG16(SI)), &REG16(DX), &REG16(CX))) {
1.1       root     12831:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     12832:                        m_CF = 1;
1.1       root     12833:                }
                   12834:                break;
                   12835:        case 0x01:
                   12836:                // NOTE: we need to check BH that shows 10-millisecond untils past time in CX
1.1.1.3   root     12837:                if(!DosDateTimeToFileTime(REG16(DX), REG16(CX), (FILETIME *)(mem + SREG_BASE(ES) + REG16(DI)))) {
1.1       root     12838:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     12839:                        m_CF = 1;
1.1       root     12840:                }
                   12841:                break;
                   12842:        default:
1.1.1.22  root     12843:                unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.48  root     12844:                REG16(AX) = 0x7100;
1.1.1.3   root     12845:                m_CF = 1;
1.1       root     12846:                break;
                   12847:        }
                   12848: }
                   12849: 
                   12850: inline void msdos_int_21h_71a8h()
                   12851: {
                   12852:        if(REG8(DH) == 0) {
                   12853:                char tmp[MAX_PATH], fcb[MAX_PATH];
1.1.1.3   root     12854:                strcpy(tmp, msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     12855:                memset(fcb, 0x20, sizeof(fcb));
                   12856:                int len = strlen(tmp);
1.1.1.21  root     12857:                for(int i = 0, pos = 0; i < len; i++) {
1.1       root     12858:                        if(tmp[i] == '.') {
                   12859:                                pos = 8;
                   12860:                        } else {
                   12861:                                if(msdos_lead_byte_check(tmp[i])) {
                   12862:                                        fcb[pos++] = tmp[i++];
                   12863:                                }
                   12864:                                fcb[pos++] = tmp[i];
                   12865:                        }
                   12866:                }
1.1.1.3   root     12867:                memcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), fcb, 11);
1.1       root     12868:        } else {
1.1.1.3   root     12869:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     12870:        }
                   12871: }
                   12872: 
1.1.1.22  root     12873: inline void msdos_int_21h_71aah()
                   12874: {
                   12875:        char drv[] = "A:", path[MAX_PATH];
                   12876:        char *hoge=(char *)(mem + SREG_BASE(DS) + REG16(DX));
                   12877:        
                   12878:        if(REG8(BL) == 0) {
                   12879:                drv[0] = 'A' + _getdrive() - 1;
                   12880:        } else {
                   12881:                drv[0] = 'A' + REG8(BL) - 1;
                   12882:        }
                   12883:        switch(REG8(BH)) {
                   12884:        case 0x00:
1.1.1.44  root     12885:                if(msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   12886:                        REG16(AX) = 0x0f; // invalid drive
                   12887:                        m_CF = 1;
                   12888:                } else if(DefineDosDevice(0, drv, (char *)(mem + SREG_BASE(DS) + REG16(DX))) == 0) {
                   12889:                        REG16(AX) = 0x03; // path not found
1.1.1.22  root     12890:                        m_CF = 1;
                   12891:                }
                   12892:                break;
                   12893:        case 0x01:
1.1.1.44  root     12894:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   12895:                        REG16(AX) = 0x0f; // invalid drive
                   12896:                        m_CF = 1;
                   12897:                } else if(DefineDosDevice(DDD_REMOVE_DEFINITION, drv, NULL) == 0) {
1.1.1.22  root     12898:                        REG16(AX) = 0x0f; // invalid drive
                   12899:                        m_CF = 1;
                   12900:                }
                   12901:                break;
                   12902:        case 0x02:
1.1.1.44  root     12903:                if(!msdos_is_valid_drive((REG8(BL) ? REG8(BL) : _getdrive()) - 1)) {
                   12904:                        REG16(AX) = 0x0f; // invalid drive
                   12905:                        m_CF = 1;
                   12906:                } else if(QueryDosDevice(drv, path, MAX_PATH) == 0) {
1.1.1.22  root     12907:                        REG16(AX) = 0x0f; // invalid drive
                   12908:                        m_CF = 1;
                   12909:                } else if(strncmp(path, "\\??\\", 4) != 0) {
                   12910:                        REG16(AX) = 0x0f; // invalid drive
                   12911:                        m_CF = 1;
                   12912:                } else {
                   12913:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(DX)), path + 4);
                   12914:                }
                   12915:                break;
                   12916:        default:
                   12917:                unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.48  root     12918:                REG16(AX) = 0x7100;
1.1.1.22  root     12919:                m_CF = 1;
                   12920:                break;
                   12921:        }
                   12922: }
                   12923: 
1.1.1.14  root     12924: inline void msdos_int_21h_7300h()
                   12925: {
1.1.1.44  root     12926:        REG8(AL) = REG8(CL);
                   12927:        REG8(AH) = 0;
1.1.1.14  root     12928: }
                   12929: 
                   12930: inline void msdos_int_21h_7302h()
                   12931: {
                   12932:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   12933:        UINT16 seg, ofs;
                   12934:        
                   12935:        if(REG16(CX) < 0x3f) {
                   12936:                REG8(AL) = 0x18;
                   12937:                m_CF = 1;
                   12938:        } else if(!msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   12939:                REG8(AL) = 0xff;
                   12940:                m_CF = 1;
                   12941:        } else {
                   12942:                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 2, mem + (seg << 4) + ofs, sizeof(dpb_t));
                   12943:        }
                   12944: }
                   12945: 
1.1       root     12946: inline void msdos_int_21h_7303h()
                   12947: {
1.1.1.3   root     12948:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   12949:        ext_space_info_t *info = (ext_space_info_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     12950:        DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   12951:        
                   12952:        if(GetDiskFreeSpace(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
                   12953:                info->size_of_structure = sizeof(ext_space_info_t);
                   12954:                info->structure_version = 0;
                   12955:                info->sectors_per_cluster = sectors_per_cluster;
                   12956:                info->bytes_per_sector = bytes_per_sector;
                   12957:                info->available_clusters_on_drive = free_clusters;
                   12958:                info->total_clusters_on_drive = total_clusters;
                   12959:                info->available_sectors_on_drive = sectors_per_cluster * free_clusters;
                   12960:                info->total_sectors_on_drive = sectors_per_cluster * total_clusters;
                   12961:                info->available_allocation_units = free_clusters;       // ???
                   12962:                info->total_allocation_units = total_clusters;          // ???
                   12963:        } else {
                   12964:                REG16(AX) = errno;
1.1.1.3   root     12965:                m_CF = 1;
1.1       root     12966:        }
                   12967: }
                   12968: 
1.1.1.30  root     12969: inline void msdos_int_21h_dbh()
                   12970: {
                   12971:        // Novell NetWare - Workstation - Get Number of Local Drives
                   12972:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   12973:        REG8(AL) = dos_info->last_drive;
                   12974: }
                   12975: 
                   12976: inline void msdos_int_21h_dch()
                   12977: {
                   12978:        // Novell NetWare - Connection Services - Get Connection Number
                   12979:        REG8(AL) = 0x00;
                   12980: }
                   12981: 
1.1.1.32  root     12982: inline void msdos_int_24h()
                   12983: {
                   12984:        const char *message = NULL;
                   12985:        int key = 0;
                   12986:        
                   12987:        for(int i = 0; i < array_length(critical_error_table); i++) {
                   12988:                if(critical_error_table[i].code == (REG16(DI) & 0xff) || critical_error_table[i].code == (UINT16)-1) {
                   12989:                        if(active_code_page == 932) {
                   12990:                                message = critical_error_table[i].message_japanese;
                   12991:                        }
                   12992:                        if(message == NULL) {
                   12993:                                message = critical_error_table[i].message_english;
                   12994:                        }
                   12995:                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   12996:                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   12997:                        
                   12998:                        SREG(ES) = WORK_TOP >> 4;
                   12999:                        i386_load_segment_descriptor(ES);
                   13000:                        REG16(DI) = 0x0000;
                   13001:                        break;
                   13002:                }
                   13003:        }
                   13004:        fprintf(stderr, "\n%s", message);
                   13005:        if(!(REG8(AH) & 0x80)) {
                   13006:                if(REG8(AH) & 0x01) {
                   13007:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�������ݒ� �h���C�u" : "writing drive", 'A' + REG8(AL));
                   13008:                } else {
                   13009:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�ǂݎ�蒆 �h���C�u" : "reading drive", 'A' + REG8(AL));
                   13010:                }
                   13011:        }
                   13012:        fprintf(stderr, "\n");
                   13013:        
1.1.1.33  root     13014:        {
1.1.1.32  root     13015:                fprintf(stderr, "%s",   (active_code_page == 932) ? "���~ (A)" : "Abort");
1.1.1.33  root     13016:        }
1.1.1.32  root     13017:        if(REG8(AH) & 0x10) {
                   13018:                fprintf(stderr, ", %s", (active_code_page == 932) ? "�Ď��s (R)" : "Retry");
                   13019:        }
                   13020:        if(REG8(AH) & 0x20) {
                   13021:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���� (I)" : "Ignore");
                   13022:        }
                   13023:        if(REG8(AH) & 0x08) {
                   13024:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���s (F)" : "Fail");
                   13025:        }
                   13026:        fprintf(stderr, "? ");
                   13027:        
                   13028:        while(1) {
                   13029:                while(!_kbhit()) {
                   13030:                        Sleep(10);
                   13031:                }
                   13032:                key = _getch();
                   13033:                
                   13034:                if(key == 'I' || key == 'i') {
                   13035:                        if(REG8(AH) & 0x20) {
                   13036:                                REG8(AL) = 0;
                   13037:                                break;
                   13038:                        }
                   13039:                } else if(key == 'R' || key == 'r') {
                   13040:                        if(REG8(AH) & 0x10) {
                   13041:                                REG8(AL) = 1;
                   13042:                                break;
                   13043:                        }
                   13044:                } else if(key == 'A' || key == 'a') {
                   13045:                        REG8(AL) = 2;
                   13046:                        break;
                   13047:                } else if(key == 'F' || key == 'f') {
                   13048:                        if(REG8(AH) & 0x08) {
                   13049:                                REG8(AL) = 3;
                   13050:                                break;
                   13051:                        }
                   13052:                }
                   13053:        }
                   13054:        fprintf(stderr, "%c\n", key);
                   13055: }
                   13056: 
1.1       root     13057: inline void msdos_int_25h()
                   13058: {
                   13059:        UINT16 seg, ofs;
                   13060:        DWORD dwSize;
                   13061:        
1.1.1.3   root     13062: #if defined(HAS_I386)
                   13063:        I386OP(pushf)();
                   13064: #else
                   13065:        PREFIX86(_pushf());
                   13066: #endif
1.1       root     13067:        
                   13068:        if(!(REG8(AL) < 26)) {
                   13069:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     13070:                m_CF = 1;
1.1       root     13071:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   13072:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13073:                m_CF = 1;
1.1       root     13074:        } else {
                   13075:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   13076:                char dev[64];
                   13077:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   13078:                
                   13079:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   13080:                if(hFile == INVALID_HANDLE_VALUE) {
                   13081:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13082:                        m_CF = 1;
1.1       root     13083:                } else {
1.1.1.19  root     13084:                        UINT32 top_sector  = REG16(DX);
                   13085:                        UINT16 sector_num  = REG16(CX);
                   13086:                        UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   13087:                        
                   13088:                        if(sector_num == 0xffff) {
                   13089:                                top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   13090:                                sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   13091:                                UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   13092:                                UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   13093:                                buffer_addr = (seg << 4) + ofs;
                   13094:                        }
                   13095: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   13096: //                             REG8(AL) = 0x02; // drive not ready
                   13097: //                             m_CF = 1;
                   13098: //                     } else 
                   13099:                        if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     13100:                                REG8(AL) = 0x08; // sector not found
1.1.1.3   root     13101:                                m_CF = 1;
1.1.1.19  root     13102:                        } else if(ReadFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     13103:                                REG8(AL) = 0x0b; // read error
1.1.1.3   root     13104:                                m_CF = 1;
1.1       root     13105:                        }
                   13106:                        CloseHandle(hFile);
                   13107:                }
                   13108:        }
                   13109: }
                   13110: 
                   13111: inline void msdos_int_26h()
                   13112: {
1.1.1.42  root     13113:        // this operation may cause serious damage for drives, so support only floppy disk...
1.1       root     13114:        UINT16 seg, ofs;
                   13115:        DWORD dwSize;
                   13116:        
1.1.1.3   root     13117: #if defined(HAS_I386)
                   13118:        I386OP(pushf)();
                   13119: #else
                   13120:        PREFIX86(_pushf());
                   13121: #endif
1.1       root     13122:        
                   13123:        if(!(REG8(AL) < 26)) {
                   13124:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     13125:                m_CF = 1;
1.1       root     13126:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   13127:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13128:                m_CF = 1;
1.1       root     13129:        } else {
                   13130:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   13131:                char dev[64];
                   13132:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   13133:                
                   13134:                if(dpb->media_type == 0xf8) {
                   13135:                        // this drive is not a floppy
1.1.1.6   root     13136: //                     if(!(((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag & 0x40)) {
                   13137: //                             fatalerror("This application tried the absolute disk write to drive %c:\n", 'A' + REG8(AL));
                   13138: //                     }
1.1       root     13139:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13140:                        m_CF = 1;
1.1       root     13141:                } else {
                   13142:                        HANDLE hFile = CreateFile(dev, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   13143:                        if(hFile == INVALID_HANDLE_VALUE) {
                   13144:                                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13145:                                m_CF = 1;
1.1       root     13146:                        } else {
1.1.1.19  root     13147:                                UINT32 top_sector  = REG16(DX);
                   13148:                                UINT16 sector_num  = REG16(CX);
                   13149:                                UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   13150:                                
                   13151:                                if(sector_num == 0xffff) {
                   13152:                                        top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   13153:                                        sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   13154:                                        UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   13155:                                        UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   13156:                                        buffer_addr = (seg << 4) + ofs;
                   13157:                                }
1.1       root     13158:                                if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   13159:                                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     13160:                                        m_CF = 1;
1.1.1.19  root     13161:                                } else if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     13162:                                        REG8(AL) = 0x08; // sector not found
1.1.1.3   root     13163:                                        m_CF = 1;
1.1.1.19  root     13164:                                } else if(WriteFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     13165:                                        REG8(AL) = 0x0a; // write error
1.1.1.3   root     13166:                                        m_CF = 1;
1.1       root     13167:                                }
                   13168:                                CloseHandle(hFile);
                   13169:                        }
                   13170:                }
                   13171:        }
                   13172: }
                   13173: 
                   13174: inline void msdos_int_27h()
                   13175: {
1.1.1.29  root     13176:        int paragraphs = (min(REG16(DX), 0xfff0) + 15) >> 4;
                   13177:        try {
                   13178:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   13179:        } catch(...) {
                   13180:                // recover the broken mcb
                   13181:                int mcb_seg = SREG(CS) - 1;
                   13182:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     13183:                
1.1.1.29  root     13184:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     13185:                        mcb->mz = 'M';
                   13186:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   13187:                        
1.1.1.29  root     13188:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39  root     13189:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29  root     13190:                        } else {
1.1.1.39  root     13191:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29  root     13192:                        }
                   13193:                } else {
                   13194:                        mcb->mz = 'Z';
1.1.1.30  root     13195:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     13196:                }
                   13197:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   13198:        }
1.1.1.3   root     13199:        msdos_process_terminate(SREG(CS), retval | 0x300, 0);
1.1       root     13200: }
                   13201: 
                   13202: inline void msdos_int_29h()
                   13203: {
1.1.1.50  root     13204:        msdos_putch_fast(REG8(AL));
1.1       root     13205: }
                   13206: 
                   13207: inline void msdos_int_2eh()
                   13208: {
                   13209:        char tmp[MAX_PATH], command[MAX_PATH], opt[MAX_PATH];
                   13210:        memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     13211:        strcpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
1.1       root     13212:        char *token = my_strtok(tmp, " ");
                   13213:        strcpy(command, token);
                   13214:        strcpy(opt, token + strlen(token) + 1);
                   13215:        
                   13216:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   13217:        param->env_seg = 0;
                   13218:        param->cmd_line.w.l = 44;
                   13219:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   13220:        param->fcb1.w.l = 24;
                   13221:        param->fcb1.w.h = (WORK_TOP >> 4);
                   13222:        param->fcb2.w.l = 24;
                   13223:        param->fcb2.w.h = (WORK_TOP >> 4);
                   13224:        
                   13225:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   13226:        
                   13227:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   13228:        cmd_line->len = strlen(opt);
                   13229:        strcpy(cmd_line->cmd, opt);
                   13230:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   13231:        
1.1.1.28  root     13232:        try {
                   13233:                if(msdos_process_exec(command, param, 0)) {
                   13234:                        REG16(AX) = 0xffff; // error before processing command
                   13235:                } else {
                   13236:                        // set flag to set retval to ax when the started process is terminated
                   13237:                        process_t *process = msdos_process_info_get(current_psp);
                   13238:                        process->called_by_int2eh = true;
                   13239:                }
                   13240:        } catch(...) {
                   13241:                REG16(AX) = 0xffff; // error before processing command
                   13242:        }
1.1       root     13243: }
                   13244: 
1.1.1.29  root     13245: inline void msdos_int_2fh_05h()
                   13246: {
                   13247:        switch(REG8(AL)) {
                   13248:        case 0x00:
1.1.1.49  root     13249:                // critical error handler is installed
1.1.1.32  root     13250:                REG8(AL) = 0xff;
                   13251:                break;
                   13252:        case 0x01:
                   13253:        case 0x02:
                   13254:                for(int i = 0; i < array_length(standard_error_table); i++) {
                   13255:                        if(standard_error_table[i].code == REG16(BX) || standard_error_table[i].code == (UINT16)-1) {
                   13256:                                const char *message = NULL;
                   13257:                                if(active_code_page == 932) {
                   13258:                                        message = standard_error_table[i].message_japanese;
                   13259:                                }
                   13260:                                if(message == NULL) {
                   13261:                                        message = standard_error_table[i].message_english;
                   13262:                                }
                   13263:                                strcpy((char *)(mem + WORK_TOP), message);
                   13264:                                
                   13265:                                SREG(ES) = WORK_TOP >> 4;
                   13266:                                i386_load_segment_descriptor(ES);
                   13267:                                REG16(DI) = 0x0000;
                   13268:                                REG8(AL) = 0x01;
                   13269:                                break;
                   13270:                        }
                   13271:                }
1.1.1.29  root     13272:                break;
                   13273:        default:
                   13274:                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.49  root     13275:                REG16(AX) = 0x01;
1.1.1.29  root     13276:                m_CF = 1;
                   13277:        }
                   13278: }
                   13279: 
1.1.1.44  root     13280: inline void msdos_int_2fh_06h()
                   13281: {
                   13282:        switch(REG8(AL)) {
                   13283:        case 0x00:
                   13284:                // ASSIGN is not installed
1.1.1.49  root     13285: //             REG8(AL) = 0x00;
1.1.1.44  root     13286:                break;
                   13287:        case 0x01:
                   13288:                // this call is available from within MIRROR.COM even if ASSIGN is not installed
                   13289:                REG16(AX) = 0x01;
                   13290:                m_CF = 1;
                   13291:                break;
                   13292:        default:
                   13293:                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));
                   13294:                REG16(AX) = 0x01;
                   13295:                m_CF = 1;
                   13296:                break;
                   13297:        }
                   13298: }
                   13299: 
1.1.1.22  root     13300: inline void msdos_int_2fh_11h()
                   13301: {
                   13302:        switch(REG8(AL)) {
                   13303:        case 0x00:
1.1.1.29  root     13304:                if(i386_read_stack() == 0xdada) {
                   13305:                        // MSCDEX is not installed
                   13306: //                     REG8(AL) = 0x00;
                   13307:                } else {
                   13308:                        // Network Redirector is not installed
                   13309: //                     REG8(AL) = 0x00;
                   13310:                }
1.1.1.22  root     13311:                break;
                   13312:        default:
1.1.1.43  root     13313: //             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     13314:                REG16(AX) = 0x49; //  network software not installed
1.1.1.22  root     13315:                m_CF = 1;
                   13316:                break;
                   13317:        }
                   13318: }
                   13319: 
1.1.1.21  root     13320: inline void msdos_int_2fh_12h()
                   13321: {
                   13322:        switch(REG8(AL)) {
1.1.1.22  root     13323:        case 0x00:
1.1.1.29  root     13324:                // DOS 3.0+ internal functions are installed
1.1.1.22  root     13325:                REG8(AL) = 0xff;
                   13326:                break;
1.1.1.29  root     13327: //     case 0x01: // DOS 3.0+ internal - Close Current File
                   13328:        case 0x02:
                   13329:                {
                   13330:                        UINT16 stack = i386_read_stack();
                   13331:                        REG16(BX) = *(UINT16 *)(mem + 4 * stack + 0);
                   13332:                        SREG(ES) = *(UINT16 *)(mem + 4 * stack + 2);
                   13333:                        i386_load_segment_descriptor(ES);
                   13334:                }
                   13335:                break;
1.1.1.30  root     13336:        case 0x03:
                   13337:                SREG(DS) = (DEVICE_TOP >> 4);
                   13338:                i386_load_segment_descriptor(DS);
                   13339:                break;
1.1.1.29  root     13340:        case 0x04:
                   13341:                {
                   13342:                        UINT16 stack = i386_read_stack();
                   13343:                        REG8(AL) = (stack == '/') ? '\\' : stack;
                   13344: #if defined(HAS_I386)
                   13345:                        m_ZF = (REG8(AL) == '\\');
                   13346: #else
                   13347:                        m_ZeroVal = (REG8(AL) != '\\');
                   13348: #endif
                   13349:                }
                   13350:                break;
                   13351:        case 0x05:
1.1.1.49  root     13352:                {
                   13353:                        UINT16 c = i386_read_stack();
                   13354:                        if((c >> 0) & 0xff) {
                   13355:                                msdos_putch((c >> 0) & 0xff);
                   13356:                        }
                   13357:                        if((c >> 8) & 0xff) {
                   13358:                                msdos_putch((c >> 8) & 0xff);
                   13359:                        }
                   13360:                }
1.1.1.29  root     13361:                break;
1.1.1.49  root     13362: //     case 0x06: // DOS 3.0+ internal - Invoke Critical Error
1.1.1.29  root     13363: //     case 0x07: // DOS 3.0+ internal - Make Disk Buffer Most Recentry Used
                   13364: //     case 0x08: // DOS 3.0+ internal - Decrement SFT Reference Count
1.1.1.49  root     13365: //     case 0x09: // DOS 3.0+ internal - Flush and Free Disk Buffer
1.1.1.29  root     13366: //     case 0x0a: // DOS 3.0+ internal - Perform Critical Error Interrupt
                   13367: //     case 0x0b: // DOS 3.0+ internal - Signal Sharing Violation to User
                   13368: //     case 0x0c: // DOS 3.0+ internal - Open Device and Set SFT Owner/Mode
                   13369:        case 0x0d:
                   13370:                {
                   13371:                        SYSTEMTIME time;
                   13372:                        FILETIME file_time;
                   13373:                        WORD dos_date, dos_time;
                   13374:                        GetLocalTime(&time);
                   13375:                        SystemTimeToFileTime(&time, &file_time);
                   13376:                        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   13377:                        REG16(AX) = dos_date;
                   13378:                        REG16(DX) = dos_time;
                   13379:                }
                   13380:                break;
                   13381: //     case 0x0e: // DOS 3.0+ internal - Mark All Disk Buffers Unreferenced
                   13382: //     case 0x0f: // DOS 3.0+ internal - Make Buffer Most Recentry Used
                   13383: //     case 0x10: // DOS 3.0+ internal - Find Unreferenced Disk Buffer
                   13384:        case 0x11:
                   13385:                {
                   13386:                        char path[MAX_PATH], *p;
                   13387:                        strcpy(path, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   13388:                        my_strupr(path);
                   13389:                        while((p = my_strchr(path, '/')) != NULL) {
                   13390:                                *p = '\\';
                   13391:                        }
                   13392:                        strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
                   13393:                }
                   13394:                break;
                   13395:        case 0x12:
                   13396:                REG16(CX) = strlen((char *)(mem + SREG_BASE(ES) + REG16(DI)));
                   13397:                break;
                   13398:        case 0x13:
                   13399:                {
                   13400:                        char tmp[2] = {0};
                   13401:                        tmp[0] = i386_read_stack();
                   13402:                        my_strupr(tmp);
                   13403:                        REG8(AL) = tmp[0];
                   13404:                }
                   13405:                break;
                   13406:        case 0x14:
                   13407: #if defined(HAS_I386)
                   13408:                m_ZF = (SREG_BASE(DS) + REG16(SI) == SREG_BASE(ES) + REG16(DI));
                   13409: #else
                   13410:                m_ZeroVal = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   13411: #endif
                   13412:                m_CF = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   13413:                break;
                   13414: //     case 0x15: // DOS 3.0+ internal - Flush Buffer
1.1.1.21  root     13415:        case 0x16:
                   13416:                if(REG16(BX) < 20) {
                   13417:                        SREG(ES) = SFT_TOP >> 4;
                   13418:                        i386_load_segment_descriptor(ES);
                   13419:                        REG16(DI) = 6 + 0x3b * REG16(BX);
                   13420:                        
                   13421:                        // update system file table
                   13422:                        UINT8* sft = mem + SFT_TOP + 6 + 0x3b * REG16(BX);
                   13423:                        if(file_handler[REG16(BX)].valid) {
                   13424:                                int count = 0;
                   13425:                                for(int i = 0; i < 20; i++) {
                   13426:                                        if(msdos_psp_get_file_table(i, current_psp) == REG16(BX)) {
                   13427:                                                count++;
                   13428:                                        }
                   13429:                                }
                   13430:                                *(UINT16 *)(sft + 0x00) = count ? count : 0xffff;
                   13431:                                *(UINT32 *)(sft + 0x15) = _tell(REG16(BX));
                   13432:                                _lseek(REG16(BX), 0, SEEK_END);
                   13433:                                *(UINT32 *)(sft + 0x11) = _tell(REG16(BX));
                   13434:                                _lseek(REG16(BX), *(UINT32 *)(sft + 0x15), SEEK_SET);
                   13435:                        } else {
                   13436:                                memset(sft, 0, 0x3b);
                   13437:                        }
                   13438:                } else {
                   13439:                        REG16(AX) = 0x06;
                   13440:                        m_CF = 1;
                   13441:                }
                   13442:                break;
1.1.1.49  root     13443:        case 0x17:
                   13444:                {
                   13445:                        UINT16 drive = i386_read_stack();
                   13446:                        if(msdos_is_valid_drive(drive)) {
                   13447:                                msdos_cds_update(drive);
                   13448:                        }
                   13449:                        REG16(SI) = 88 * drive;
                   13450:                        SREG(DS) = (CDS_TOP >> 4);
                   13451:                        i386_load_segment_descriptor(DS);
                   13452:                }
                   13453:                break;
1.1.1.29  root     13454: //     case 0x18: // DOS 3.0+ internal - Get Caller's Registers
                   13455: //     case 0x19: // DOS 3.0+ internal - Set Drive???
                   13456:        case 0x1a:
                   13457:                {
                   13458:                        char *path = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full[MAX_PATH];
                   13459:                        if(path[1] == ':') {
                   13460:                                if(path[0] >= 'a' && path[0] <= 'z') {
                   13461:                                        REG8(AL) = path[0] - 'a' + 1;
                   13462:                                } else if(path[0] >= 'A' && path[0] <= 'Z') {
                   13463:                                        REG8(AL) = path[0] - 'A' + 1;
                   13464:                                } else {
                   13465:                                        REG8(AL) = 0xff; // invalid
                   13466:                                }
                   13467:                                strcpy(full, path);
                   13468:                                strcpy(path, full + 2);
                   13469:                        } else if(GetFullPathName(path, MAX_PATH, full, NULL) != 0 && full[1] == ':') {
                   13470:                                if(full[0] >= 'a' && full[0] <= 'z') {
                   13471:                                        REG8(AL) = full[0] - 'a' + 1;
                   13472:                                } else if(full[0] >= 'A' && full[0] <= 'Z') {
                   13473:                                        REG8(AL) = full[0] - 'A' + 1;
                   13474:                                } else {
                   13475:                                        REG8(AL) = 0xff; // invalid
                   13476:                                }
                   13477:                        } else {
                   13478:                                REG8(AL) = 0x00; // default
                   13479:                        }
                   13480:                }
                   13481:                break;
                   13482:        case 0x1b:
                   13483:                {
                   13484:                        int year = REG16(CX) + 1980;
                   13485:                        REG8(AL) = ((year % 4) == 0 && (year % 100) != 0 && (year % 400) == 0) ? 29 : 28;
                   13486:                }
                   13487:                break;
                   13488: //     case 0x1c: // DOS 3.0+ internal - Check Sum Memory
                   13489: //     case 0x1d: // DOS 3.0+ internal - Sum Memory
                   13490:        case 0x1e:
                   13491:                {
                   13492:                        char *path_1st = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full_1st[MAX_PATH];
                   13493:                        char *path_2nd = (char *)(mem + SREG_BASE(ES) + REG16(DI)), full_2nd[MAX_PATH];
                   13494:                        if(GetFullPathName(path_1st, MAX_PATH, full_1st, NULL) != 0 && GetFullPathName(path_2nd, MAX_PATH, full_2nd, NULL) != 0) {
                   13495: #if defined(HAS_I386)
                   13496:                                m_ZF = (strcmp(full_1st, full_2nd) == 0);
                   13497: #else
                   13498:                                m_ZeroVal = (strcmp(full_1st, full_2nd) != 0);
                   13499: #endif
                   13500:                        } else {
                   13501: #if defined(HAS_I386)
                   13502:                                m_ZF = (strcmp(path_1st, path_2nd) == 0);
                   13503: #else
                   13504:                                m_ZeroVal = (strcmp(path_1st, path_2nd) != 0);
                   13505: #endif
                   13506:                        }
                   13507:                }
                   13508:                break;
1.1.1.49  root     13509:        case 0x1f:
                   13510:                {
                   13511:                        UINT16 drive = i386_read_stack();
                   13512:                        if(msdos_is_valid_drive(drive)) {
                   13513:                                msdos_cds_update(drive);
                   13514:                        }
                   13515:                        REG16(SI) = 88 * drive;
                   13516:                        SREG(ES) = (CDS_TOP >> 4);
                   13517:                        i386_load_segment_descriptor(ES);
                   13518:                }
                   13519:                break;
1.1.1.21  root     13520:        case 0x20:
                   13521:                {
                   13522:                        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   13523:                        
                   13524:                        if(fd < 20) {
                   13525:                                SREG(ES) = current_psp;
                   13526:                                i386_load_segment_descriptor(ES);
                   13527:                                REG16(DI) = offsetof(psp_t, file_table) + fd;
                   13528:                        } else {
                   13529:                                REG16(AX) = 0x06;
                   13530:                                m_CF = 1;
                   13531:                        }
                   13532:                }
                   13533:                break;
1.1.1.29  root     13534:        case 0x21:
                   13535:                msdos_int_21h_60h(0);
                   13536:                break;
1.1.1.49  root     13537:        case 0x22:
                   13538:                {
                   13539:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   13540:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x00) != 0xff) {
                   13541:                                sda->extended_error_code = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x00);
                   13542:                        }
                   13543:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x01) != 0xff) {
                   13544:                                sda->error_class         = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x01);
                   13545:                        }
                   13546:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x02) != 0xff) {
                   13547:                                sda->suggested_action    = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x02);
                   13548:                        }
                   13549:                        if(*(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x03) != 0xff) {
                   13550:                                sda->locus_of_last_error = *(UINT8 *)(mem + SREG_BASE(SS) + REG16(SI) + 0x03);
                   13551:                        }
                   13552:                }
                   13553:                break;
1.1.1.29  root     13554: //     case 0x23: // DOS 3.0+ internal - Check If Character Device
                   13555: //     case 0x24: // DOS 3.0+ internal - Sharing Retry Delay
                   13556:        case 0x25:
                   13557:                REG16(CX) = strlen((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   13558:                break;
                   13559:        case 0x26:
                   13560:                REG8(AL) = REG8(CL);
                   13561:                msdos_int_21h_3dh();
                   13562:                break;
                   13563:        case 0x27:
                   13564:                msdos_int_21h_3eh();
                   13565:                break;
                   13566:        case 0x28:
                   13567:                REG16(AX) = REG16(BP);
                   13568:                msdos_int_21h_42h();
                   13569:                break;
                   13570:        case 0x29:
                   13571:                msdos_int_21h_3fh();
                   13572:                break;
                   13573: //     case 0x2a: // DOS 3.0+ internal - Set Fast Open Entry Point
                   13574:        case 0x2b:
                   13575:                REG16(AX) = REG16(BP);
                   13576:                msdos_int_21h_44h();
                   13577:                break;
                   13578:        case 0x2c:
                   13579:                REG16(BX) = DEVICE_TOP >> 4;
                   13580:                REG16(AX) = 22;
                   13581:                break;
                   13582:        case 0x2d:
                   13583:                {
                   13584:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   13585:                        REG16(AX) = sda->extended_error_code;
                   13586:                }
                   13587:                break;
                   13588:        case 0x2e:
                   13589:                if(REG8(DL) == 0x00 || REG8(DL) == 0x02 || REG8(DL) == 0x04 || REG8(DL) == 0x06) {
1.1.1.32  root     13590:                        SREG(ES) = 0x0001;
                   13591:                        i386_load_segment_descriptor(ES);
                   13592:                        REG16(DI) = 0x00;
                   13593:                } else if(REG8(DL) == 0x08) {
1.1.1.49  root     13594:                        // dummy parameter error message read routine is at fffc:0010
                   13595:                        SREG(ES) = DUMMY_TOP >> 4;
1.1.1.22  root     13596:                        i386_load_segment_descriptor(ES);
1.1.1.32  root     13597:                        REG16(DI) = 0x0010;
1.1.1.22  root     13598:                }
                   13599:                break;
1.1.1.29  root     13600:        case 0x2f:
                   13601:                if(REG16(DX) != 0) {
1.1.1.30  root     13602:                        dos_major_version = REG8(DL);
                   13603:                        dos_minor_version = REG8(DH);
1.1.1.29  root     13604:                } else {
                   13605:                        REG8(DL) = 7;
                   13606:                        REG8(DH) = 10;
                   13607:                }
                   13608:                break;
                   13609: //     case 0x30: // Windows95 - Find SFT Entry in Internal File Tables
                   13610: //     case 0x31: // Windows95 - Set/Clear "Report Windows to DOS Programs" Flag
1.1.1.22  root     13611:        default:
                   13612:                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));
                   13613:                REG16(AX) = 0x01;
                   13614:                m_CF = 1;
                   13615:                break;
                   13616:        }
                   13617: }
                   13618: 
1.1.1.30  root     13619: inline void msdos_int_2fh_13h()
                   13620: {
                   13621:        static UINT16 prevDS = 0, prevDX = 0;
                   13622:        static UINT16 prevES = 0, prevBX = 0;
                   13623:        UINT16 tmp;
                   13624:        
                   13625:        tmp = SREG(DS); SREG(DS) = prevDS; prevDS = tmp;
                   13626:        i386_load_segment_descriptor(DS);
                   13627:        tmp = REG16(DX); REG16(DX) = prevDX; prevDX = tmp;
                   13628:        
                   13629:        tmp = SREG(ES); SREG(ES) = prevES; prevES = tmp;
                   13630:        i386_load_segment_descriptor(ES);
                   13631:        tmp = REG16(BX); REG16(BX) = prevBX; prevBX = tmp;
                   13632: }
                   13633: 
1.1.1.22  root     13634: inline void msdos_int_2fh_14h()
                   13635: {
                   13636:        switch(REG8(AL)) {
                   13637:        case 0x00:
1.1.1.29  root     13638:                // NLSFUNC.COM is installed
                   13639:                REG8(AL) = 0xff;
1.1.1.25  root     13640:                break;
                   13641:        case 0x01:
                   13642:        case 0x03:
                   13643:                REG8(AL) = 0x00;
                   13644:                active_code_page = REG16(BX);
                   13645:                msdos_nls_tables_update();
                   13646:                break;
                   13647:        case 0x02:
                   13648:                REG8(AL) = get_extended_country_info(REG16(BP));
                   13649:                break;
                   13650:        case 0x04:
1.1.1.42  root     13651:                for(int i = 0;; i++) {
                   13652:                        if(country_table[i].code == REG16(DX)) {
                   13653:                                get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)), country_table[i].usPrimaryLanguage, country_table[i].usSubLanguage);
                   13654:                                break;
                   13655:                        } else if(country_table[i].code == -1) {
                   13656:                                get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)));
                   13657:                                break;
                   13658:                        }
                   13659:                }
1.1.1.25  root     13660:                REG8(AL) = 0x00;
1.1.1.22  root     13661:                break;
                   13662:        default:
                   13663:                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));
                   13664:                REG16(AX) = 0x01;
                   13665:                m_CF = 1;
                   13666:                break;
                   13667:        }
                   13668: }
                   13669: 
                   13670: inline void msdos_int_2fh_15h()
                   13671: {
                   13672:        switch(REG8(AL)) {
1.1.1.29  root     13673:        case 0x00: // CD-ROM - Installation Check
                   13674:                if(REG16(BX) == 0x0000) {
1.1.1.43  root     13675: #if 0
                   13676:                        // MSCDEX is installed
                   13677:                        REG16(BX) = 0;
                   13678:                        for(int i = 0, n = 0; i < 26; i++) {
1.1.1.44  root     13679:                                if(msdos_is_cdrom_drive(i)) {
                   13680:                                        if(REG16(BX) == 0) {
                   13681:                                                REG16(CX) = i;
1.1.1.43  root     13682:                                        }
1.1.1.44  root     13683:                                        REG16(BX)++;
1.1.1.43  root     13684:                                }
                   13685:                        }
                   13686: #else
1.1.1.29  root     13687:                        // MSCDEX is not installed
                   13688: //                     REG8(AL) = 0x00;
1.1.1.43  root     13689: #endif
1.1.1.29  root     13690:                } else {
                   13691:                        // GRAPHICS.COM is not installed
                   13692: //                     REG8(AL) = 0x00;
                   13693:                }
1.1.1.22  root     13694:                break;
1.1.1.43  root     13695:        case 0x0b:
1.1.1.44  root     13696:                // this call is available from within DOSSHELL even if MSCDEX is not installed
                   13697:                REG16(AX) = msdos_is_cdrom_drive(REG8(CL)) ? 0x5ad8 : 0x0000;
                   13698:                REG16(BX) = 0xadad;
1.1.1.43  root     13699:                break;
                   13700:        case 0x0d:
1.1.1.44  root     13701:                for(int i = 0, n = 0; i < 26; i++) {
                   13702:                        if(msdos_is_cdrom_drive(i)) {
                   13703:                                mem[SREG_BASE(ES) + REG16(BX) + (n++)] = i;
1.1.1.43  root     13704:                        }
                   13705:                }
                   13706:                break;
1.1.1.22  root     13707:        case 0xff:
1.1.1.29  root     13708:                if(REG16(BX) == 0x0000) {
                   13709:                        // CORELCDX is not installed
                   13710:                } else {
                   13711:                        unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   13712:                        REG16(AX) = 0x01;
                   13713:                        m_CF = 1;
                   13714:                }
1.1.1.22  root     13715:                break;
1.1.1.21  root     13716:        default:
1.1.1.22  root     13717:                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     13718:                REG16(AX) = 0x01;
                   13719:                m_CF = 1;
                   13720:                break;
                   13721:        }
                   13722: }
                   13723: 
1.1       root     13724: inline void msdos_int_2fh_16h()
                   13725: {
                   13726:        switch(REG8(AL)) {
                   13727:        case 0x00:
1.1.1.14  root     13728:                if(no_windows) {
1.1.1.29  root     13729:                        // neither Windows 3.x enhanced mode nor Windows/386 2.x running
                   13730: //                     REG8(AL) = 0x00;
1.1.1.14  root     13731:                } else {
1.1.1.30  root     13732:                        REG8(AL) = win_major_version;
                   13733:                        REG8(AH) = win_minor_version;
1.1       root     13734:                }
                   13735:                break;
1.1.1.43  root     13736:        case 0x05: // Windows Enhanced Mode & 286 DOSX Init Broadcast
1.1.1.30  root     13737:                // from DOSBox
                   13738:                i386_set_a20_line(1);
                   13739:                break;
1.1.1.49  root     13740:        case 0x06: // Windows Enhanced Mode & 286 DOSX Exit Broadcast
1.1.1.43  root     13741:        case 0x08: // Windows Enhanced Mode Init Complete Broadcast
                   13742:        case 0x09: // Windows Enhanced Mode Begin Exit Broadcast
                   13743:                break;
                   13744:        case 0x07:
                   13745:                // Virtual Device Call API
                   13746:                break;
1.1.1.22  root     13747:        case 0x0a:
                   13748:                if(!no_windows) {
                   13749:                        REG16(AX) = 0x0000;
1.1.1.30  root     13750:                        REG8(BH) = win_major_version;
                   13751:                        REG8(BL) = win_minor_version;
1.1.1.49  root     13752: //                     REG16(CX) = 0x0002; // standard
1.1.1.22  root     13753:                        REG16(CX) = 0x0003; // enhanced
                   13754:                }
                   13755:                break;
1.1.1.30  root     13756:        case 0x0b:
                   13757:                // no TRS, keep ES:DI = 0000h:0000h
1.1.1.22  root     13758:        case 0x0e:
                   13759:        case 0x0f:
1.1.1.30  root     13760:        case 0x10:
1.1.1.22  root     13761:        case 0x11:
                   13762:        case 0x12:
                   13763:        case 0x13:
                   13764:        case 0x14:
1.1.1.30  root     13765:        case 0x15:
1.1.1.43  root     13766:        case 0x81:
                   13767:        case 0x82:
1.1.1.44  root     13768:        case 0x84:
1.1.1.49  root     13769:        case 0x85:
1.1.1.33  root     13770:        case 0x86:
1.1.1.22  root     13771:        case 0x87:
1.1.1.30  root     13772:        case 0x89:
1.1.1.33  root     13773:        case 0x8a:
1.1.1.22  root     13774:                // function not supported, do not clear AX
                   13775:                break;
1.1.1.14  root     13776:        case 0x80:
                   13777:                Sleep(10);
1.1.1.35  root     13778:                REQUEST_HARDWRE_UPDATE();
1.1.1.29  root     13779:                REG8(AL) = 0x00;
1.1.1.14  root     13780:                break;
1.1.1.33  root     13781:        case 0x83:
                   13782:                REG16(BX) = 0x01; // system vm id
                   13783:                break;
1.1.1.22  root     13784:        case 0x8e:
                   13785:                REG16(AX) = 0x00; // failed
                   13786:                break;
1.1.1.20  root     13787:        case 0x8f:
                   13788:                switch(REG8(DH)) {
                   13789:                case 0x01:
1.1.1.49  root     13790: //                     REG16(AX) = 0x0000; // close command selected but not yet acknowledged
                   13791: //                     REG16(AX) = 0x0001; // close command issued and acknowledged
                   13792:                        REG16(AX) = 0x168f; // close command not selected -- application should continue
                   13793:                        break;
                   13794:                default:
                   13795:                        REG16(AX) = 0x0000; // successful
1.1.1.20  root     13796:                        break;
                   13797:                }
                   13798:                break;
1.1       root     13799:        default:
1.1.1.22  root     13800:                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));
                   13801:                REG16(AX) = 0x01;
                   13802:                m_CF = 1;
                   13803:                break;
                   13804:        }
                   13805: }
                   13806: 
                   13807: inline void msdos_int_2fh_19h()
                   13808: {
                   13809:        switch(REG8(AL)) {
                   13810:        case 0x00:
1.1.1.29  root     13811:                // SHELLB.COM is not installed
                   13812: //             REG8(AL) = 0x00;
1.1.1.22  root     13813:                break;
                   13814:        case 0x01:
                   13815:        case 0x02:
                   13816:        case 0x03:
                   13817:        case 0x04:
                   13818:                REG16(AX) = 0x01;
                   13819:                m_CF = 1;
                   13820:                break;
1.1.1.29  root     13821:        case 0x80:
                   13822:                // IBM ROM-DOS v4.0 is not installed
                   13823: //             REG8(AL) = 0x00;
                   13824:                break;
1.1.1.22  root     13825:        default:
                   13826:                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     13827:                REG16(AX) = 0x01;
1.1.1.3   root     13828:                m_CF = 1;
1.1       root     13829:                break;
                   13830:        }
                   13831: }
                   13832: 
                   13833: inline void msdos_int_2fh_1ah()
                   13834: {
                   13835:        switch(REG8(AL)) {
                   13836:        case 0x00:
1.1.1.29  root     13837:                // ANSI.SYS is installed
1.1       root     13838:                REG8(AL) = 0xff;
                   13839:                break;
1.1.1.49  root     13840:        case 0x01:
1.1.1.50  root     13841:                if(REG8(CL) == 0x5f) {
                   13842:                        // set display information
                   13843:                        if(*(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) >= 14) {
                   13844:                                int cur_width  = *(UINT16 *)(mem + 0x44a) + 0;
                   13845:                                int cur_height = *(UINT8  *)(mem + 0x484) + 1;
                   13846:                                int new_width  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e);   // character columns
                   13847:                                int new_height = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10);   // character rows
                   13848:                                
                   13849:                                if(cur_width != new_width || cur_height != new_height) {
                   13850:                                        pcbios_set_console_size(new_width, new_height, true);
                   13851:                                }
                   13852:                        }
                   13853:                } else if(REG8(CL) == 0x7f) {
1.1.1.49  root     13854:                        // get display information
1.1.1.50  root     13855:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00) = 0;        // level (0 for DOS 4.x-6.0)
                   13856:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x01) = 0;        // reserved (0)
                   13857:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) = 14;       // length of following data (14)
                   13858:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04) = 1;        // bit 0 set for blink, clear for intensity
                   13859:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06) = 1;        // mode type (1=text, 2=graphics)
                   13860:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x07) = 0;        // reserved (0)
                   13861:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08) = 4;        // 4 bits per pixel
                   13862:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a) =  8 * (*(UINT16 *)(mem + 0x44a) + 0);      // pixel columns
                   13863:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c) = 16 * (*(UINT8  *)(mem + 0x484) + 1);      // pixel rows
                   13864:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e) = *(UINT16 *)(mem + 0x44a) + 0;             // character columns
                   13865:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10) = *(UINT8  *)(mem + 0x484) + 1;             // character rows
1.1.1.49  root     13866:                } else {
                   13867:                        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));
                   13868:                        REG16(AX) = 0x01;
                   13869:                        m_CF = 1;
                   13870:                }
                   13871:                break;
1.1       root     13872:        default:
1.1.1.22  root     13873:                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));
                   13874:                REG16(AX) = 0x01;
                   13875:                m_CF = 1;
                   13876:                break;
                   13877:        }
                   13878: }
                   13879: 
1.1.1.30  root     13880: inline void msdos_int_2fh_40h()
1.1.1.22  root     13881: {
                   13882:        switch(REG8(AL)) {
                   13883:        case 0x00:
1.1.1.30  root     13884:                // Windows 3+ - Get Virtual Device Driver (VDD) Capabilities
                   13885:                REG8(AL) = 0x01; // does not virtualize video access
1.1.1.22  root     13886:                break;
1.1.1.43  root     13887:        case 0x10:
                   13888:                // OS/2 v2.0+ - Installation Check
                   13889:                REG16(AX) = 0x01;
                   13890:                m_CF = 1;
                   13891:                break;
1.1.1.22  root     13892:        default:
                   13893:                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     13894:                REG16(AX) = 0x01;
1.1.1.3   root     13895:                m_CF = 1;
1.1       root     13896:                break;
                   13897:        }
                   13898: }
                   13899: 
                   13900: inline void msdos_int_2fh_43h()
                   13901: {
                   13902:        switch(REG8(AL)) {
                   13903:        case 0x00:
1.1.1.29  root     13904:                // XMS is installed ?
1.1.1.19  root     13905: #ifdef SUPPORT_XMS
                   13906:                if(support_xms) {
                   13907:                        REG8(AL) = 0x80;
1.1.1.44  root     13908:                }
                   13909: #endif
                   13910:                break;
                   13911:        case 0x08:
                   13912: #ifdef SUPPORT_XMS
                   13913:                if(support_xms) {
                   13914:                        REG8(AL) = 0x43;
                   13915:                        REG8(BL) = 0x01; // IBM PC/AT
                   13916:                        REG8(BH) = 0x01; // Fast AT A20 switch time
                   13917:                }
1.1.1.19  root     13918: #endif
                   13919:                break;
                   13920:        case 0x10:
                   13921:                SREG(ES) = XMS_TOP >> 4;
                   13922:                i386_load_segment_descriptor(ES);
1.1.1.26  root     13923:                REG16(BX) = 0x15;
1.1       root     13924:                break;
1.1.1.44  root     13925:        case 0xe0:
                   13926:                // DOS Protected Mode Services (DPMS) v1.0 is not installed
                   13927:                if(REG16(BX) == 0x0000 && REG16(CX) == 0x4450 && REG16(DX) == 0x4d53) {
                   13928:                        break;
                   13929:                }
1.1       root     13930:        default:
1.1.1.22  root     13931:                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));
                   13932:                REG16(AX) = 0x01;
                   13933:                m_CF = 1;
                   13934:                break;
                   13935:        }
                   13936: }
                   13937: 
                   13938: inline void msdos_int_2fh_46h()
                   13939: {
                   13940:        switch(REG8(AL)) {
                   13941:        case 0x80:
1.1.1.29  root     13942:                // Windows v3.0 is not installed
                   13943: //             REG8(AL) = 0x00;
1.1.1.22  root     13944:                break;
                   13945:        default:
                   13946:                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));
                   13947:                REG16(AX) = 0x01;
                   13948:                m_CF = 1;
                   13949:                break;
                   13950:        }
                   13951: }
                   13952: 
                   13953: inline void msdos_int_2fh_48h()
                   13954: {
                   13955:        switch(REG8(AL)) {
                   13956:        case 0x00:
1.1.1.29  root     13957:                // DOSKEY is not installed
                   13958: //             REG8(AL) = 0x00;
1.1.1.22  root     13959:                break;
                   13960:        case 0x10:
                   13961:                msdos_int_21h_0ah();
                   13962:                REG16(AX) = 0x00;
                   13963:                break;
                   13964:        default:
                   13965:                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     13966:                REG16(AX) = 0x01;
1.1.1.3   root     13967:                m_CF = 1;
1.1       root     13968:                break;
                   13969:        }
                   13970: }
                   13971: 
                   13972: inline void msdos_int_2fh_4ah()
                   13973: {
                   13974:        switch(REG8(AL)) {
1.1.1.29  root     13975: #ifdef SUPPORT_HMA
                   13976:        case 0x01: // DOS 5.0+ - Query Free HMA Space
                   13977:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   13978:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   13979:                                // restore first free mcb in high memory area
                   13980:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   13981:                        }
                   13982:                        int offset = 0xffff;
                   13983:                        if((REG16(BX) = msdos_hma_mem_get_free(&offset)) != 0) {
                   13984:                                REG16(DI) = offset + 0x10;
                   13985:                        } else {
                   13986:                                REG16(DI) = 0xffff;
                   13987:                        }
                   13988:                } else {
                   13989:                        // HMA is already used
                   13990:                        REG16(BX) = 0;
                   13991:                        REG16(DI) = 0xffff;
                   13992:                }
                   13993:                SREG(ES) = 0xffff;
                   13994:                i386_load_segment_descriptor(ES);
                   13995:                break;
                   13996:        case 0x02: // DOS 5.0+ - Allocate HMA Space
                   13997:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   13998:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   13999:                                // restore first free mcb in high memory area
                   14000:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14001:                        }
                   14002:                        int size = REG16(BX), offset;
                   14003:                        if((size % 16) != 0) {
                   14004:                                size &= ~15;
                   14005:                                size += 16;
                   14006:                        }
                   14007:                        if((offset = msdos_hma_mem_alloc(size, current_psp)) != -1) {
                   14008:                                REG16(BX) = size;
                   14009:                                REG16(DI) = offset + 0x10;
                   14010:                                is_hma_used_by_int_2fh = true;
                   14011:                        } else {
                   14012:                                REG16(BX) = 0;
                   14013:                                REG16(DI) = 0xffff;
                   14014:                        }
                   14015:                } else {
                   14016:                        // HMA is already used
                   14017:                        REG16(BX) = 0;
                   14018:                        REG16(DI) = 0xffff;
                   14019:                }
                   14020:                SREG(ES) = 0xffff;
                   14021:                i386_load_segment_descriptor(ES);
                   14022:                break;
                   14023:        case 0x03: // Windows95 - (De)Allocate HMA Memory Block
                   14024:                if(REG8(DL) == 0x00) {
                   14025:                        if(!is_hma_used_by_xms) {
                   14026:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14027:                                        // restore first free mcb in high memory area
                   14028:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14029:                                        is_hma_used_by_int_2fh = false;
                   14030:                                }
                   14031:                                int size = REG16(BX), offset;
                   14032:                                if((size % 16) != 0) {
                   14033:                                        size &= ~15;
                   14034:                                        size += 16;
                   14035:                                }
                   14036:                                if((offset = msdos_hma_mem_alloc(size, REG16(CX))) != -1) {
                   14037: //                                     REG16(BX) = size;
                   14038:                                        SREG(ES) = 0xffff;
                   14039:                                        i386_load_segment_descriptor(ES);
                   14040:                                        REG16(DI) = offset + 0x10;
                   14041:                                        is_hma_used_by_int_2fh = true;
                   14042:                                } else {
                   14043:                                        REG16(DI) = 0xffff;
                   14044:                                }
                   14045:                        } else {
                   14046:                                REG16(DI) = 0xffff;
                   14047:                        }
                   14048:                } else if(REG8(DL) == 0x01) {
                   14049:                        if(!is_hma_used_by_xms) {
                   14050:                                int size = REG16(BX);
                   14051:                                if((size % 16) != 0) {
                   14052:                                        size &= ~15;
                   14053:                                        size += 16;
                   14054:                                }
                   14055:                                if(msdos_hma_mem_realloc(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10, size) != -1) {
                   14056:                                        // memory block address is not changed
                   14057:                                } else {
                   14058:                                        REG16(DI) = 0xffff;
                   14059:                                }
                   14060:                        } else {
                   14061:                                REG16(DI) = 0xffff;
                   14062:                        }
                   14063:                } else if(REG8(DL) == 0x02) {
                   14064:                        if(!is_hma_used_by_xms) {
                   14065:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14066:                                        // restore first free mcb in high memory area
                   14067:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14068:                                        is_hma_used_by_int_2fh = false;
                   14069:                                } else {
                   14070:                                        msdos_hma_mem_free(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10);
                   14071:                                        if(msdos_hma_mem_get_free(NULL) == 0xffe0) {
                   14072:                                                is_hma_used_by_int_2fh = false;
                   14073:                                        }
                   14074:                                }
                   14075:                        }
                   14076:                } else {
                   14077:                        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));
                   14078:                        REG16(AX) = 0x01;
                   14079:                        m_CF = 1;
                   14080:                }
                   14081:                break;
                   14082:        case 0x04: // Windows95 - Get Start of HMA Memory Chain
                   14083:                if(!is_hma_used_by_xms) {
                   14084:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   14085:                                // restore first free mcb in high memory area
                   14086:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14087:                                is_hma_used_by_int_2fh = false;
                   14088:                        }
                   14089:                        REG16(AX) = 0x0000;
                   14090:                        SREG(ES) = 0xffff;
                   14091:                        i386_load_segment_descriptor(ES);
                   14092:                        REG16(DI) = 0x10;
                   14093:                }
                   14094:                break;
                   14095: #else
1.1       root     14096:        case 0x01:
                   14097:        case 0x02:
1.1.1.29  root     14098:                // HMA is already used
1.1.1.27  root     14099:                REG16(BX) = 0x0000;
1.1.1.3   root     14100:                SREG(ES) = 0xffff;
                   14101:                i386_load_segment_descriptor(ES);
1.1       root     14102:                REG16(DI) = 0xffff;
                   14103:                break;
1.1.1.19  root     14104:        case 0x03:
                   14105:                // unable to allocate
                   14106:                REG16(DI) = 0xffff;
                   14107:                break;
                   14108:        case 0x04:
                   14109:                // function not supported, do not clear AX
                   14110:                break;
1.1.1.29  root     14111: #endif
                   14112:        case 0x10:
1.1.1.42  root     14113:                switch(REG16(BX)) {
                   14114:                case 0x0000:
                   14115:                case 0x0001:
                   14116:                case 0x0002:
                   14117:                case 0x0003:
                   14118:                case 0x0004:
                   14119:                case 0x0005:
                   14120:                case 0x0006:
                   14121:                case 0x0007:
                   14122:                case 0x0008:
                   14123:                case 0x000a:
                   14124:                case 0x1234:
                   14125:                        // SMARTDRV v4.00+ is not installed
                   14126:                        break;
                   14127:                default:
1.1.1.29  root     14128:                        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));
                   14129:                        REG16(AX) = 0x01;
                   14130:                        m_CF = 1;
1.1.1.42  root     14131:                        break;
1.1.1.29  root     14132:                }
                   14133:                break;
                   14134:        case 0x11:
1.1.1.42  root     14135:                switch(REG16(BX)) {
                   14136:                case 0x0000:
                   14137:                case 0x0001:
                   14138:                case 0x0002:
                   14139:                case 0x0003:
                   14140:                case 0x0004:
                   14141:                case 0x0005:
                   14142:                case 0x0006:
                   14143:                case 0x0007:
                   14144:                case 0x0008:
                   14145:                case 0x0009:
                   14146:                case 0x000a:
                   14147:                case 0x000b:
                   14148:                case 0xfffe:
                   14149:                case 0xffff:
1.1.1.29  root     14150:                        // DBLSPACE.BIN is not installed
1.1.1.42  root     14151:                        break;
                   14152:                default:
                   14153:                        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));
                   14154:                        REG16(AX) = 0x01;
                   14155:                        m_CF = 1;
                   14156:                        break;
                   14157:                }
                   14158:                break;
                   14159:        case 0x12:
                   14160:                if(REG16(CX) == 0x4d52 && REG16(DX) == 0x4349) {
                   14161:                        // Microsoft Realtime Compression Interface (MRCI) is not installed
                   14162:                } else if(REG16(CX) == 0x5354 && REG16(DX) == 0x4143) {
                   14163:                        // Stacker 4 LZS Compression Interface (LZSAPI) is not installed
1.1.1.29  root     14164:                } else {
                   14165:                        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));
                   14166:                        REG16(AX) = 0x01;
                   14167:                        m_CF = 1;
                   14168:                }
1.1.1.22  root     14169:                break;
1.1.1.42  root     14170:        case 0x13:
                   14171:                // DBLSPACE.BIN is not installed
                   14172:                break;
1.1.1.22  root     14173:        default:
                   14174:                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));
                   14175:                REG16(AX) = 0x01;
                   14176:                m_CF = 1;
                   14177:                break;
                   14178:        }
                   14179: }
                   14180: 
                   14181: inline void msdos_int_2fh_4bh()
                   14182: {
                   14183:        switch(REG8(AL)) {
1.1.1.24  root     14184:        case 0x01:
1.1.1.22  root     14185:        case 0x02:
1.1.1.29  root     14186:                // Task Switcher is not installed
1.1.1.24  root     14187:                break;
                   14188:        case 0x03:
                   14189:                // this call is available from within DOSSHELL even if the task switcher is not installed
                   14190:                REG16(AX) = REG16(BX) = 0x0000; // no more avaiable switcher id
1.1.1.22  root     14191:                break;
1.1.1.30  root     14192:        case 0x04:
                   14193:                REG16(BX) = 0x0000; // free switcher id successfully
                   14194:                break;
1.1.1.43  root     14195:        case 0x05:
                   14196:                REG16(BX) = 0x0000; // no instance data chain
                   14197:                SREG(ES) = 0x0000;
                   14198:                i386_load_segment_descriptor(ES);
                   14199:                break;
1.1       root     14200:        default:
1.1.1.22  root     14201:                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     14202:                REG16(AX) = 0x01;
1.1.1.3   root     14203:                m_CF = 1;
1.1       root     14204:                break;
                   14205:        }
                   14206: }
                   14207: 
1.1.1.44  root     14208: inline void msdos_int_2fh_4dh()
                   14209: {
                   14210:        switch(REG8(AL)) {
                   14211:        case 0x00:
                   14212:                // KKCFUNC is not installed ???
                   14213:                break;
                   14214:        default:
                   14215: //             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));
                   14216:                REG16(AX) = 0x01; // invalid function
                   14217:                m_CF = 1;
                   14218:                break;
                   14219:        }
                   14220: }
                   14221: 
1.1       root     14222: inline void msdos_int_2fh_4fh()
                   14223: {
                   14224:        switch(REG8(AL)) {
                   14225:        case 0x00:
1.1.1.29  root     14226:                // BILING is installed
1.1.1.27  root     14227:                REG16(AX) = 0x0000;
                   14228:                REG8(DL) = 0x01;        // major version
                   14229:                REG8(DH) = 0x00;        // minor version
1.1       root     14230:                break;
                   14231:        case 0x01:
1.1.1.27  root     14232:                REG16(AX) = 0x0000;
1.1       root     14233:                REG16(BX) = active_code_page;
                   14234:                break;
                   14235:        default:
1.1.1.22  root     14236:                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));
                   14237:                REG16(AX) = 0x01;
                   14238:                m_CF = 1;
                   14239:                break;
                   14240:        }
                   14241: }
                   14242: 
                   14243: inline void msdos_int_2fh_55h()
                   14244: {
                   14245:        switch(REG8(AL)) {
                   14246:        case 0x00:
                   14247:        case 0x01:
                   14248: //             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));
                   14249:                break;
                   14250:        default:
                   14251:                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     14252:                REG16(AX) = 0x01;
1.1.1.3   root     14253:                m_CF = 1;
1.1       root     14254:                break;
                   14255:        }
                   14256: }
                   14257: 
1.1.1.44  root     14258: inline void msdos_int_2fh_56h()
                   14259: {
                   14260:        switch(REG8(AL)) {
                   14261:        case 0x00:
                   14262:                // INTERLNK is not installed
                   14263:                break;
                   14264:        case 0x01:
                   14265:                // this call is available from within SCANDISK even if INTERLNK is not installed
                   14266: //             if(msdos_is_remote_drive(REG8(BH))) {
                   14267: //                     REG8(AL) = 0x00;
                   14268: //             }
                   14269:                break;
                   14270:        default:
                   14271:                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));
                   14272:                REG16(AX) = 0x01;
                   14273:                m_CF = 1;
                   14274:                break;
                   14275:        }
                   14276: }
                   14277: 
1.1.1.24  root     14278: inline void msdos_int_2fh_adh()
                   14279: {
                   14280:        switch(REG8(AL)) {
                   14281:        case 0x00:
1.1.1.29  root     14282:                // DISPLAY.SYS is installed
1.1.1.24  root     14283:                REG8(AL) = 0xff;
                   14284:                REG16(BX) = 0x100; // ???
                   14285:                break;
                   14286:        case 0x01:
                   14287:                active_code_page = REG16(BX);
                   14288:                msdos_nls_tables_update();
                   14289:                REG16(AX) = 0x01;
                   14290:                break;
                   14291:        case 0x02:
                   14292:                REG16(BX) = active_code_page;
                   14293:                break;
                   14294:        case 0x03:
                   14295:                // FIXME
                   14296:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 1;
                   14297:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2) = 3;
                   14298:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4) = 1;
                   14299:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 6) = active_code_page;
                   14300:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 8) = active_code_page;
                   14301:                break;
                   14302:        case 0x80:
1.1.1.49  root     14303:                // KEYB.COM is not installed
                   14304:                break;
1.1.1.24  root     14305:        default:
                   14306:                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));
                   14307:                REG16(AX) = 0x01;
                   14308:                m_CF = 1;
                   14309:                break;
                   14310:        }
                   14311: }
                   14312: 
1.1       root     14313: inline void msdos_int_2fh_aeh()
                   14314: {
                   14315:        switch(REG8(AL)) {
                   14316:        case 0x00:
1.1.1.28  root     14317:                // FIXME: we need to check the given command line
                   14318:                REG8(AL) = 0x00; // the command should be executed as usual
                   14319: //             REG8(AL) = 0xff; // this command is a TSR extension to COMMAND.COM
1.1       root     14320:                break;
                   14321:        case 0x01:
                   14322:                {
                   14323:                        char command[MAX_PATH];
                   14324:                        memset(command, 0, sizeof(command));
1.1.1.3   root     14325:                        memcpy(command, mem + SREG_BASE(DS) + REG16(SI) + 1, mem[SREG_BASE(DS) + REG16(SI)]);
1.1       root     14326:                        
                   14327:                        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   14328:                        param->env_seg = 0;
                   14329:                        param->cmd_line.w.l = 44;
                   14330:                        param->cmd_line.w.h = (WORK_TOP >> 4);
                   14331:                        param->fcb1.w.l = 24;
                   14332:                        param->fcb1.w.h = (WORK_TOP >> 4);
                   14333:                        param->fcb2.w.l = 24;
                   14334:                        param->fcb2.w.h = (WORK_TOP >> 4);
                   14335:                        
                   14336:                        memset(mem + WORK_TOP + 24, 0x20, 20);
                   14337:                        
                   14338:                        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
1.1.1.3   root     14339:                        cmd_line->len = mem[SREG_BASE(DS) + REG16(BX) + 1];
                   14340:                        memcpy(cmd_line->cmd, mem + SREG_BASE(DS) + REG16(BX) + 2, cmd_line->len);
1.1       root     14341:                        cmd_line->cmd[cmd_line->len] = 0x0d;
                   14342:                        
1.1.1.28  root     14343:                        try {
                   14344:                                msdos_process_exec(command, param, 0);
                   14345:                        } catch(...) {
                   14346:                                fatalerror("failed to start '%s' by int 2Fh, AX=AE01h\n", command);
1.1       root     14347:                        }
                   14348:                }
                   14349:                break;
                   14350:        default:
1.1.1.22  root     14351:                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     14352:                REG16(AX) = 0x01;
1.1.1.3   root     14353:                m_CF = 1;
1.1       root     14354:                break;
                   14355:        }
                   14356: }
                   14357: 
1.1.1.34  root     14358: inline void msdos_int_2fh_b7h()
                   14359: {
                   14360:        switch(REG8(AL)) {
                   14361:        case 0x00:
                   14362:                // APPEND is not installed
                   14363: //             REG8(AL) = 0x00;
                   14364:                break;
1.1.1.44  root     14365:        case 0x06:
                   14366:                REG16(BX) = 0x0000;
                   14367:                break;
1.1.1.34  root     14368:        case 0x07:
1.1.1.43  root     14369:        case 0x11:
1.1.1.34  root     14370:                // COMMAND.COM calls this service without checking APPEND is installed
                   14371:                break;
                   14372:        default:
                   14373:                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));
                   14374:                REG16(AX) = 0x01;
                   14375:                m_CF = 1;
                   14376:                break;
                   14377:        }
                   14378: }
                   14379: 
1.1.1.24  root     14380: inline void msdos_int_33h_0000h()
                   14381: {
                   14382:        REG16(AX) = 0xffff; // hardware/driver installed
                   14383:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   14384: }
                   14385: 
                   14386: inline void msdos_int_33h_0001h()
                   14387: {
1.1.1.34  root     14388:        if(mouse.hidden > 0) {
                   14389:                mouse.hidden--;
                   14390:        }
                   14391:        if(mouse.hidden == 0) {
1.1.1.24  root     14392:                if(!(dwConsoleMode & ENABLE_MOUSE_INPUT)) {
                   14393:                        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode | ENABLE_MOUSE_INPUT);
                   14394:                }
                   14395:                pic[1].imr &= ~0x10;    // enable irq12
                   14396:        }
                   14397: }
                   14398: 
                   14399: inline void msdos_int_33h_0002h()
                   14400: {
1.1.1.34  root     14401:        mouse.hidden++;
                   14402: //     SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode & ~ENABLE_MOUSE_INPUT);
                   14403:        pic[1].imr |= 0x10;     // enable irq12
1.1.1.24  root     14404: }
                   14405: 
                   14406: inline void msdos_int_33h_0003h()
                   14407: {
1.1.1.34  root     14408: //     if(mouse.hidden > 0) {
                   14409:                update_console_input();
                   14410: //     }
1.1.1.24  root     14411:        REG16(BX) = mouse.get_buttons();
1.1.1.34  root     14412:        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   14413:        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
                   14414: }
                   14415: 
                   14416: inline void msdos_int_33h_0004h()
                   14417: {
                   14418:        mouse.position.x = REG16(CX);
                   14419:        mouse.position.x = REG16(DX);
1.1.1.24  root     14420: }
                   14421: 
                   14422: inline void msdos_int_33h_0005h()
                   14423: {
1.1.1.34  root     14424: //     if(mouse.hidden > 0) {
                   14425:                update_console_input();
                   14426: //     }
1.1.1.24  root     14427:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   14428:                int idx = REG16(BX);
1.1.1.34  root     14429:                REG16(BX) = min(mouse.buttons[idx].pressed_times, 0x7fff);
                   14430:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].pressed_position.x));
                   14431:                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     14432:                mouse.buttons[idx].pressed_times = 0;
                   14433:        } else {
                   14434:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   14435:        }
                   14436:        REG16(AX) = mouse.get_buttons();
                   14437: }
                   14438: 
                   14439: inline void msdos_int_33h_0006h()
                   14440: {
1.1.1.34  root     14441: //     if(mouse.hidden > 0) {
                   14442:                update_console_input();
                   14443: //     }
1.1.1.24  root     14444:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   14445:                int idx = REG16(BX);
1.1.1.34  root     14446:                REG16(BX) = min(mouse.buttons[idx].released_times, 0x7fff);
                   14447:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].released_position.x));
                   14448:                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     14449:                mouse.buttons[idx].released_times = 0;
                   14450:        } else {
                   14451:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   14452:        }
                   14453:        REG16(AX) = mouse.get_buttons();
                   14454: }
                   14455: 
                   14456: inline void msdos_int_33h_0007h()
                   14457: {
                   14458:        mouse.min_position.x = min(REG16(CX), REG16(DX));
                   14459:        mouse.max_position.x = max(REG16(CX), REG16(DX));
                   14460: }
                   14461: 
                   14462: inline void msdos_int_33h_0008h()
                   14463: {
                   14464:        mouse.min_position.y = min(REG16(CX), REG16(DX));
                   14465:        mouse.max_position.y = max(REG16(CX), REG16(DX));
                   14466: }
                   14467: 
                   14468: inline void msdos_int_33h_0009h()
                   14469: {
                   14470:        mouse.hot_spot[0] = REG16(BX);
                   14471:        mouse.hot_spot[1] = REG16(CX);
                   14472: }
                   14473: 
1.1.1.49  root     14474: inline void msdos_int_33h_000ah()
                   14475: {
                   14476:        mouse.screen_mask = REG16(CX);
                   14477:        mouse.cursor_mask = REG16(DX);
                   14478: }
                   14479: 
1.1.1.24  root     14480: inline void msdos_int_33h_000bh()
                   14481: {
1.1.1.34  root     14482: //     if(mouse.hidden > 0) {
                   14483:                update_console_input();
                   14484: //     }
1.1.1.24  root     14485:        int dx = (mouse.position.x - mouse.prev_position.x) * mouse.mickey.x / 8;
                   14486:        int dy = (mouse.position.y - mouse.prev_position.y) * mouse.mickey.y / 8;
                   14487:        mouse.prev_position.x = mouse.position.x;
                   14488:        mouse.prev_position.y = mouse.position.y;
                   14489:        REG16(CX) = dx;
                   14490:        REG16(DX) = dy;
                   14491: }
                   14492: 
                   14493: inline void msdos_int_33h_000ch()
                   14494: {
                   14495:        mouse.call_mask = REG16(CX);
                   14496:        mouse.call_addr.w.l = REG16(DX);
                   14497:        mouse.call_addr.w.h = SREG(ES);
                   14498: }
                   14499: 
                   14500: inline void msdos_int_33h_000fh()
                   14501: {
                   14502:        mouse.mickey.x = REG16(CX);
                   14503:        mouse.mickey.y = REG16(DX);
                   14504: }
                   14505: 
                   14506: inline void msdos_int_33h_0011h()
                   14507: {
                   14508:        REG16(AX) = 0xffff;
                   14509:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   14510: }
                   14511: 
                   14512: inline void msdos_int_33h_0014h()
                   14513: {
                   14514:        UINT16 old_mask = mouse.call_mask;
                   14515:        UINT16 old_ofs = mouse.call_addr.w.l;
                   14516:        UINT16 old_seg = mouse.call_addr.w.h;
                   14517:        
                   14518:        mouse.call_mask = REG16(CX);
                   14519:        mouse.call_addr.w.l = REG16(DX);
                   14520:        mouse.call_addr.w.h = SREG(ES);
                   14521:        
                   14522:        REG16(CX) = old_mask;
                   14523:        REG16(DX) = old_ofs;
                   14524:        SREG(ES) = old_seg;
                   14525:        i386_load_segment_descriptor(ES);
                   14526: }
                   14527: 
                   14528: inline void msdos_int_33h_0015h()
                   14529: {
                   14530:        REG16(BX) = sizeof(mouse);
                   14531: }
                   14532: 
                   14533: inline void msdos_int_33h_0016h()
                   14534: {
                   14535:        memcpy(mem + SREG_BASE(ES) + REG16(DX), &mouse, sizeof(mouse));
                   14536: }
                   14537: 
                   14538: inline void msdos_int_33h_0017h()
                   14539: {
                   14540:        memcpy(&mouse, mem + SREG_BASE(ES) + REG16(DX), sizeof(mouse));
                   14541: }
                   14542: 
1.1.1.43  root     14543: inline void msdos_int_33h_0018h()
                   14544: {
                   14545:        for(int i = 0; i < 8; i++) {
                   14546:                if(REG16(CX) & (1 << i)) {
                   14547:                        if(mouse.call_addr_alt[i].dw && !(REG16(DX) == 0 && SREG(ES) == 0)) {
                   14548:                                // event handler already exists
                   14549:                                REG16(AX) = 0xffff;
                   14550:                                break;
                   14551:                        }
                   14552:                        mouse.call_addr_alt[i].w.l = REG16(DX);
                   14553:                        mouse.call_addr_alt[i].w.h = SREG(ES);
                   14554:                }
                   14555:        }
                   14556: }
                   14557: 
                   14558: inline void msdos_int_33h_0019h()
                   14559: {
                   14560:        UINT16 call_mask = REG16(CX);
                   14561:        
                   14562:        REG16(CX) = 0;
                   14563:        
                   14564:        for(int i = 0; i < 8; i++) {
                   14565:                if((call_mask & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   14566:                        for(int j = 0; j < 8; j++) {
                   14567:                                if((call_mask & (1 << j)) && mouse.call_addr_alt[i].dw == mouse.call_addr_alt[j].dw) {
                   14568:                                        REG16(CX) |= (1 << j);
                   14569:                                }
                   14570:                        }
                   14571:                        REG16(DX) = mouse.call_addr_alt[i].w.l;
                   14572:                        REG16(BX) = mouse.call_addr_alt[i].w.h;
                   14573:                        break;
                   14574:                }
                   14575:        }
                   14576: }
                   14577: 
1.1.1.24  root     14578: inline void msdos_int_33h_001ah()
                   14579: {
                   14580:        mouse.sensitivity[0] = REG16(BX);
                   14581:        mouse.sensitivity[1] = REG16(CX);
                   14582:        mouse.sensitivity[2] = REG16(DX);
                   14583: }
                   14584: 
                   14585: inline void msdos_int_33h_001bh()
                   14586: {
                   14587:        REG16(BX) = mouse.sensitivity[0];
                   14588:        REG16(CX) = mouse.sensitivity[1];
                   14589:        REG16(DX) = mouse.sensitivity[2];
                   14590: }
                   14591: 
                   14592: inline void msdos_int_33h_001dh()
                   14593: {
                   14594:        mouse.display_page = REG16(BX);
                   14595: }
                   14596: 
                   14597: inline void msdos_int_33h_001eh()
                   14598: {
                   14599:        REG16(BX) = mouse.display_page;
                   14600: }
                   14601: 
1.1.1.34  root     14602: inline void msdos_int_33h_001fh()
                   14603: {
                   14604:        // from DOSBox
                   14605:        REG16(BX) = 0x0000;
                   14606:        SREG(ES) = 0x0000;
                   14607:        i386_load_segment_descriptor(ES);
                   14608:        mouse.enabled = false;
                   14609:        mouse.old_hidden = mouse.hidden;
                   14610:        mouse.hidden = 1;
                   14611: }
                   14612: 
                   14613: inline void msdos_int_33h_0020h()
                   14614: {
                   14615:        // from DOSBox
                   14616:        mouse.enabled = true;
                   14617:        mouse.hidden = mouse.old_hidden;
                   14618: }
                   14619: 
1.1.1.24  root     14620: inline void msdos_int_33h_0021h()
                   14621: {
                   14622:        REG16(AX) = 0xffff;
                   14623:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   14624: }
                   14625: 
                   14626: inline void msdos_int_33h_0022h()
                   14627: {
                   14628:        mouse.language = REG16(BX);
                   14629: }
                   14630: 
                   14631: inline void msdos_int_33h_0023h()
                   14632: {
                   14633:        REG16(BX) = mouse.language;
                   14634: }
                   14635: 
                   14636: inline void msdos_int_33h_0024h()
                   14637: {
                   14638:        REG16(BX) = 0x0805; // V8.05
                   14639:        REG16(CX) = 0x0400; // PS/2
                   14640: }
                   14641: 
1.1.1.49  root     14642: inline void msdos_int_33h_0025h()
                   14643: {
                   14644:        REG16(AX) = 0x8000; // driver (not TSR), software text cursor
                   14645: }
                   14646: 
1.1.1.24  root     14647: inline void msdos_int_33h_0026h()
                   14648: {
                   14649:        REG16(BX) = 0x0000;
                   14650:        REG16(CX) = mouse.max_position.x;
                   14651:        REG16(DX) = mouse.max_position.y;
                   14652: }
                   14653: 
1.1.1.49  root     14654: inline void msdos_int_33h_0027h()
                   14655: {
                   14656: //     if(mouse.hidden > 0) {
                   14657:                update_console_input();
                   14658: //     }
                   14659:        int dx = (mouse.position.x - mouse.prev_position.x) * mouse.mickey.x / 8;
                   14660:        int dy = (mouse.position.y - mouse.prev_position.y) * mouse.mickey.y / 8;
                   14661:        mouse.prev_position.x = mouse.position.x;
                   14662:        mouse.prev_position.y = mouse.position.y;
                   14663:        REG16(AX) = mouse.screen_mask;
                   14664:        REG16(BX) = mouse.cursor_mask;
                   14665:        REG16(CX) = dx;
                   14666:        REG16(DX) = dy;
                   14667: }
                   14668: 
                   14669: inline void msdos_int_33h_0028h()
                   14670: {
                   14671:        if(REG16(CX) != 0) {
                   14672:                UINT8 tmp = REG8(AL);
                   14673:                REG8(AL) = REG8(CL);
                   14674:                pcbios_int_10h_00h();
                   14675:                REG8(AL) = tmp;
                   14676:        }
                   14677:        REG8(CL) = 0x00; // successful
                   14678: }
                   14679: 
                   14680: inline void msdos_int_33h_0029h()
                   14681: {
                   14682:        switch(REG16(CX)) {
                   14683:        case 0x0000:
                   14684:                REG16(CX) = 0x0003;
                   14685:                sprintf((char *)(mem + WORK_TOP), "TEXT Mode (80x25)$");
                   14686:                break;
                   14687:        case 0x0003:
                   14688:                REG16(CX) = 0x0070;
                   14689:                sprintf((char *)(mem + WORK_TOP), "V-TEXT Mode (%dx%d)$", scr_width, scr_height);
                   14690:                break;
                   14691:        case 0x0070:
                   14692:                REG16(CX) = 0x0071;
                   14693:                sprintf((char *)(mem + WORK_TOP), "Extended CGA V-TEXT Mode (%dx%d)$", scr_width, scr_height);
                   14694:                break;
                   14695:        case 0x0071:
                   14696:                REG16(CX) = 0x0073;
                   14697:                sprintf((char *)(mem + WORK_TOP), "Extended CGA TEXT Mode (80x25)$");
                   14698:                break;
                   14699:        default:
                   14700:                REG16(CX) = 0x0000;
                   14701:                break;
                   14702:        }
                   14703:        if(REG16(CX) != 0) {
                   14704:                SREG(DS) = (WORK_TOP >> 4);
                   14705:        } else {
                   14706:                SREG(DS) = 0x0000;
                   14707:        }
                   14708:        i386_load_segment_descriptor(DS);
                   14709:        REG16(DX) = 0x0000;
                   14710: }
                   14711: 
1.1.1.24  root     14712: inline void msdos_int_33h_002ah()
                   14713: {
1.1.1.34  root     14714:        REG16(AX) = -mouse.hidden;
1.1.1.24  root     14715:        REG16(BX) = mouse.hot_spot[0];
                   14716:        REG16(CX) = mouse.hot_spot[1];
                   14717:        REG16(DX) = 4; // PS/2
                   14718: }
                   14719: 
                   14720: inline void msdos_int_33h_0031h()
                   14721: {
                   14722:        REG16(AX) = mouse.min_position.x;
                   14723:        REG16(BX) = mouse.min_position.y;
                   14724:        REG16(CX) = mouse.max_position.x;
                   14725:        REG16(DX) = mouse.max_position.y;
                   14726: }
                   14727: 
                   14728: inline void msdos_int_33h_0032h()
                   14729: {
                   14730:        REG16(AX) = 0;
1.1.1.49  root     14731:        REG16(AX) |= 0x8000; // 0025h
1.1.1.24  root     14732:        REG16(AX) |= 0x4000; // 0026h
1.1.1.49  root     14733:        REG16(AX) |= 0x2000; // 0027h
1.1.1.24  root     14734: //     REG16(AX) |= 0x1000; // 0028h
                   14735: //     REG16(AX) |= 0x0800; // 0029h
                   14736:        REG16(AX) |= 0x0400; // 002ah
                   14737: //     REG16(AX) |= 0x0200; // 002bh
                   14738: //     REG16(AX) |= 0x0100; // 002ch
                   14739: //     REG16(AX) |= 0x0080; // 002dh
                   14740: //     REG16(AX) |= 0x0040; // 002eh
                   14741:        REG16(AX) |= 0x0020; // 002fh
                   14742: //     REG16(AX) |= 0x0010; // 0030h
                   14743:        REG16(AX) |= 0x0008; // 0031h
                   14744:        REG16(AX) |= 0x0004; // 0032h
                   14745: //     REG16(AX) |= 0x0002; // 0033h
                   14746: //     REG16(AX) |= 0x0001; // 0034h
                   14747: }
                   14748: 
1.1.1.49  root     14749: inline void msdos_int_33h_004dh()
                   14750: {
                   14751:        strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), "Copyright 2017 MS-DOS Player");
                   14752: }
                   14753: 
                   14754: inline void msdos_int_33h_006dh()
                   14755: {
                   14756:        *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x08; // V8.05
                   14757:        *(UINT8 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = 0x05;
                   14758: }
                   14759: 
1.1.1.19  root     14760: inline void msdos_int_67h_40h()
                   14761: {
                   14762:        if(!support_ems) {
                   14763:                REG8(AH) = 0x84;
                   14764:        } else {
                   14765:                REG8(AH) = 0x00;
                   14766:        }
                   14767: }
                   14768: 
                   14769: inline void msdos_int_67h_41h()
                   14770: {
                   14771:        if(!support_ems) {
                   14772:                REG8(AH) = 0x84;
                   14773:        } else {
                   14774:                REG8(AH) = 0x00;
                   14775:                REG16(BX) = EMS_TOP >> 4;
                   14776:        }
                   14777: }
                   14778: 
                   14779: inline void msdos_int_67h_42h()
                   14780: {
                   14781:        if(!support_ems) {
                   14782:                REG8(AH) = 0x84;
                   14783:        } else {
                   14784:                REG8(AH) = 0x00;
                   14785:                REG16(BX) = free_ems_pages;
                   14786:                REG16(DX) = MAX_EMS_PAGES;
                   14787:        }
                   14788: }
                   14789: 
                   14790: inline void msdos_int_67h_43h()
                   14791: {
                   14792:        if(!support_ems) {
                   14793:                REG8(AH) = 0x84;
                   14794:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   14795:                REG8(AH) = 0x87;
                   14796:        } else if(REG16(BX) > free_ems_pages) {
                   14797:                REG8(AH) = 0x88;
                   14798:        } else if(REG16(BX) == 0) {
                   14799:                REG8(AH) = 0x89;
                   14800:        } else {
1.1.1.31  root     14801:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     14802:                        if(!ems_handles[i].allocated) {
                   14803:                                ems_allocate_pages(i, REG16(BX));
                   14804:                                REG8(AH) = 0x00;
                   14805:                                REG16(DX) = i;
                   14806:                                return;
                   14807:                        }
                   14808:                }
                   14809:                REG8(AH) = 0x85;
                   14810:        }
                   14811: }
                   14812: 
                   14813: inline void msdos_int_67h_44h()
                   14814: {
                   14815:        if(!support_ems) {
                   14816:                REG8(AH) = 0x84;
1.1.1.31  root     14817:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     14818:                REG8(AH) = 0x83;
                   14819:        } else if(!(REG16(BX) == 0xffff || REG16(BX) < ems_handles[REG16(DX)].pages)) {
                   14820:                REG8(AH) = 0x8a;
                   14821: //     } else if(!(REG8(AL) < 4)) {
                   14822: //             REG8(AH) = 0x8b;
                   14823:        } else if(REG16(BX) == 0xffff) {
                   14824:                ems_unmap_page(REG8(AL) & 3);
                   14825:                REG8(AH) = 0x00;
                   14826:        } else {
                   14827:                ems_map_page(REG8(AL) & 3, REG16(DX), REG16(BX));
                   14828:                REG8(AH) = 0x00;
                   14829:        }
                   14830: }
                   14831: 
                   14832: inline void msdos_int_67h_45h()
                   14833: {
                   14834:        if(!support_ems) {
                   14835:                REG8(AH) = 0x84;
1.1.1.31  root     14836:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     14837:                REG8(AH) = 0x83;
                   14838:        } else {
                   14839:                ems_release_pages(REG16(DX));
                   14840:                REG8(AH) = 0x00;
                   14841:        }
                   14842: }
                   14843: 
                   14844: inline void msdos_int_67h_46h()
                   14845: {
                   14846:        if(!support_ems) {
                   14847:                REG8(AH) = 0x84;
                   14848:        } else {
1.1.1.29  root     14849: //             REG16(AX) = 0x0032; // EMS 3.2
                   14850:                REG16(AX) = 0x0040; // EMS 4.0
1.1.1.19  root     14851:        }
                   14852: }
                   14853: 
                   14854: inline void msdos_int_67h_47h()
                   14855: {
                   14856:        // NOTE: the map data should be stored in the specified ems page, not process data
                   14857:        process_t *process = msdos_process_info_get(current_psp);
                   14858:        
                   14859:        if(!support_ems) {
                   14860:                REG8(AH) = 0x84;
1.1.1.31  root     14861: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     14862: //             REG8(AH) = 0x83;
                   14863:        } else if(process->ems_pages_stored) {
                   14864:                REG8(AH) = 0x8d;
                   14865:        } else {
                   14866:                for(int i = 0; i < 4; i++) {
                   14867:                        process->ems_pages[i].handle = ems_pages[i].handle;
                   14868:                        process->ems_pages[i].page   = ems_pages[i].page;
                   14869:                        process->ems_pages[i].mapped = ems_pages[i].mapped;
                   14870:                }
                   14871:                process->ems_pages_stored = true;
                   14872:                REG8(AH) = 0x00;
                   14873:        }
                   14874: }
                   14875: 
                   14876: inline void msdos_int_67h_48h()
                   14877: {
                   14878:        // NOTE: the map data should be restored from the specified ems page, not process data
                   14879:        process_t *process = msdos_process_info_get(current_psp);
                   14880:        
                   14881:        if(!support_ems) {
                   14882:                REG8(AH) = 0x84;
1.1.1.31  root     14883: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     14884: //             REG8(AH) = 0x83;
                   14885:        } else if(!process->ems_pages_stored) {
                   14886:                REG8(AH) = 0x8e;
                   14887:        } else {
                   14888:                for(int i = 0; i < 4; i++) {
                   14889:                        if(process->ems_pages[i].mapped) {
                   14890:                                ems_map_page(i, process->ems_pages[i].handle, process->ems_pages[i].page);
                   14891:                        } else {
                   14892:                                ems_unmap_page(i);
                   14893:                        }
                   14894:                }
                   14895:                process->ems_pages_stored = false;
                   14896:                REG8(AH) = 0x00;
                   14897:        }
                   14898: }
                   14899: 
                   14900: inline void msdos_int_67h_4bh()
                   14901: {
                   14902:        if(!support_ems) {
                   14903:                REG8(AH) = 0x84;
                   14904:        } else {
                   14905:                REG8(AH) = 0x00;
                   14906:                REG16(BX) = 0;
1.1.1.31  root     14907:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     14908:                        if(ems_handles[i].allocated) {
                   14909:                                REG16(BX)++;
                   14910:                        }
                   14911:                }
                   14912:        }
                   14913: }
                   14914: 
                   14915: inline void msdos_int_67h_4ch()
                   14916: {
                   14917:        if(!support_ems) {
                   14918:                REG8(AH) = 0x84;
1.1.1.31  root     14919:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     14920:                REG8(AH) = 0x83;
                   14921:        } else {
                   14922:                REG8(AH) = 0x00;
                   14923:                REG16(BX) = ems_handles[REG16(DX)].pages;
                   14924:        }
                   14925: }
                   14926: 
                   14927: inline void msdos_int_67h_4dh()
                   14928: {
                   14929:        if(!support_ems) {
                   14930:                REG8(AH) = 0x84;
                   14931:        } else {
                   14932:                REG8(AH) = 0x00;
                   14933:                REG16(BX) = 0;
1.1.1.31  root     14934:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     14935:                        if(ems_handles[i].allocated) {
                   14936:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 0) = i;
                   14937:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 2) = ems_handles[i].pages;
                   14938:                                REG16(BX)++;
                   14939:                        }
                   14940:                }
                   14941:        }
                   14942: }
                   14943: 
1.1.1.20  root     14944: inline void msdos_int_67h_4eh()
                   14945: {
                   14946:        if(!support_ems) {
                   14947:                REG8(AH) = 0x84;
                   14948:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   14949:                if(REG8(AL) == 0x00 || REG8(AL) == 0x02) {
                   14950:                        // save page map
                   14951:                        for(int i = 0; i < 4; i++) {
                   14952:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   14953:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   14954:                        }
                   14955:                }
                   14956:                if(REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   14957:                        // restore page map
                   14958:                        for(int i = 0; i < 4; i++) {
                   14959:                                UINT16 handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   14960:                                UINT16 page   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   14961:                                
1.1.1.31  root     14962:                                if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
1.1.1.20  root     14963:                                        ems_map_page(i, handle, page);
                   14964:                                } else {
                   14965:                                        ems_unmap_page(i);
                   14966:                                }
                   14967:                        }
                   14968:                }
                   14969:                REG8(AH) = 0x00;
                   14970:        } else if(REG8(AL) == 0x03) {
                   14971:                REG8(AH) = 0x00;
1.1.1.21  root     14972:                REG8(AL) = 4 * 4;
                   14973:        } else {
1.1.1.22  root     14974:                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     14975:                REG8(AH) = 0x8f;
                   14976:        }
                   14977: }
                   14978: 
                   14979: inline void msdos_int_67h_4fh()
                   14980: {
                   14981:        if(!support_ems) {
                   14982:                REG8(AH) = 0x84;
                   14983:        } else if(REG8(AL) == 0x00) {
                   14984:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   14985:                
                   14986:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI)) = count;
                   14987:                for(int i = 0; i < count; i++) {
                   14988:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 2 * i);
                   14989:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   14990:                        
                   14991: //                     if(!(physical < 4)) {
                   14992: //                             REG8(AH) = 0x8b;
                   14993: //                             return;
                   14994: //                     }
                   14995:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 0) = segment;
1.1.1.41  root     14996:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 2) = ems_pages[physical & 3].mapped ? ems_pages[physical & 3].handle : 0xffff;
                   14997:                        *(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     14998:                }
                   14999:                REG8(AH) = 0x00;
                   15000:        } else if(REG8(AL) == 0x01) {
                   15001:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   15002:                
                   15003:                for(int i = 0; i < count; i++) {
                   15004:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 0);
                   15005:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   15006:                        UINT16 handle   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 2);
                   15007:                        UINT16 logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 4);
                   15008:                        
                   15009: //                     if(!(physical < 4)) {
                   15010: //                             REG8(AH) = 0x8b;
                   15011: //                             return;
                   15012: //                     } else
1.1.1.41  root     15013:                        if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && logical < ems_handles[handle].pages) {
1.1.1.21  root     15014:                                ems_map_page(physical & 3, handle, logical);
                   15015:                        } else {
1.1.1.41  root     15016:                                ems_unmap_page(physical & 3);
1.1.1.21  root     15017:                        }
                   15018:                }
                   15019:                REG8(AH) = 0x00;
                   15020:        } else if(REG8(AL) == 0x02) {
                   15021:                REG8(AH) = 0x00;
                   15022:                REG8(AL) = 2 + REG16(BX) * 6;
1.1.1.20  root     15023:        } else {
1.1.1.22  root     15024:                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     15025:                REG8(AH) = 0x8f;
                   15026:        }
                   15027: }
                   15028: 
                   15029: inline void msdos_int_67h_50h()
                   15030: {
                   15031:        if(!support_ems) {
                   15032:                REG8(AH) = 0x84;
1.1.1.31  root     15033:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.20  root     15034:                REG8(AH) = 0x83;
                   15035:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15036:                for(int i = 0; i < REG16(CX); i++) {
                   15037:                        int logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   15038:                        int physical = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   15039:                        
                   15040:                        if(REG8(AL) == 0x01) {
                   15041:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15042:                        }
                   15043: //                     if(!(physical < 4)) {
                   15044: //                             REG8(AH) = 0x8b;
                   15045: //                             return;
                   15046: //                     } else
                   15047:                        if(logical == 0xffff) {
                   15048:                                ems_unmap_page(physical & 3);
                   15049:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15050:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15051:                        } else {
                   15052:                                REG8(AH) = 0x8a;
                   15053:                                return;
                   15054:                        }
                   15055:                }
                   15056:                REG8(AH) = 0x00;
                   15057:        } else {
1.1.1.22  root     15058:                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     15059:                REG8(AH) = 0x8f;
                   15060:        }
                   15061: }
                   15062: 
1.1.1.19  root     15063: inline void msdos_int_67h_51h()
                   15064: {
                   15065:        if(!support_ems) {
                   15066:                REG8(AH) = 0x84;
1.1.1.31  root     15067:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15068:                REG8(AH) = 0x83;
                   15069:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   15070:                REG8(AH) = 0x87;
                   15071:        } else if(REG16(BX) > free_ems_pages + ems_handles[REG16(DX)].pages) {
                   15072:                REG8(AH) = 0x88;
                   15073:        } else {
                   15074:                ems_reallocate_pages(REG16(DX), REG16(BX));
                   15075:                REG8(AH) = 0x00;
                   15076:        }
                   15077: }
                   15078: 
1.1.1.20  root     15079: inline void msdos_int_67h_52h()
                   15080: {
                   15081:        if(!support_ems) {
                   15082:                REG8(AH) = 0x84;
1.1.1.31  root     15083: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   15084: //             REG8(AH) = 0x83;
1.1.1.20  root     15085:        } else if(REG8(AL) == 0x00) {
                   15086:                REG8(AL) = 0x00; // handle is volatile
                   15087:                REG8(AH) = 0x00;
                   15088:        } else if(REG8(AL) == 0x01) {
                   15089:                if(REG8(BL) == 0x00) {
                   15090:                        REG8(AH) = 0x00;
                   15091:                } else {
                   15092:                        REG8(AH) = 0x90; // undefined attribute type
                   15093:                }
                   15094:        } else if(REG8(AL) == 0x02) {
                   15095:                REG8(AL) = 0x00; // only volatile handles supported
                   15096:                REG8(AH) = 0x00;
                   15097:        } else {
1.1.1.22  root     15098:                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     15099:                REG8(AH) = 0x8f;
                   15100:        }
                   15101: }
                   15102: 
1.1.1.19  root     15103: inline void msdos_int_67h_53h()
                   15104: {
                   15105:        if(!support_ems) {
                   15106:                REG8(AH) = 0x84;
1.1.1.31  root     15107:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     15108:                REG8(AH) = 0x83;
                   15109:        } else if(REG8(AL) == 0x00) {
                   15110:                memcpy(mem + SREG_BASE(ES) + REG16(DI), ems_handles[REG16(DX)].name, 8);
                   15111:                REG8(AH) = 0x00;
                   15112:        } else if(REG8(AL) == 0x01) {
1.1.1.31  root     15113:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15114:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   15115:                                REG8(AH) = 0xa1;
                   15116:                                return;
                   15117:                        }
                   15118:                }
                   15119:                REG8(AH) = 0x00;
                   15120:                memcpy(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8);
                   15121:        } else {
1.1.1.22  root     15122:                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     15123:                REG8(AH) = 0x8f;
1.1.1.19  root     15124:        }
                   15125: }
                   15126: 
                   15127: inline void msdos_int_67h_54h()
                   15128: {
                   15129:        if(!support_ems) {
                   15130:                REG8(AH) = 0x84;
                   15131:        } else if(REG8(AL) == 0x00) {
1.1.1.31  root     15132:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15133:                        if(ems_handles[i].allocated) {
                   15134:                                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, ems_handles[i].name, 10);
                   15135:                        } else {
                   15136:                                memset(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, 0, 10);
                   15137:                        }
                   15138:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 0) = i;
                   15139:                }
                   15140:                REG8(AH) = 0x00;
                   15141:                REG8(AL) = MAX_EMS_HANDLES;
                   15142:        } else if(REG8(AL) == 0x01) {
                   15143:                REG8(AH) = 0xa0; // not found
1.1.1.31  root     15144:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     15145:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   15146:                                REG8(AH) = 0x00;
                   15147:                                REG16(DX) = i;
                   15148:                                break;
                   15149:                        }
                   15150:                }
                   15151:        } else if(REG8(AL) == 0x02) {
                   15152:                REG8(AH) = 0x00;
                   15153:                REG16(BX) = MAX_EMS_HANDLES;
                   15154:        } else {
1.1.1.22  root     15155:                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     15156:                REG8(AH) = 0x8f;
                   15157:        }
                   15158: }
                   15159: 
1.1.1.49  root     15160: inline void msdos_int_67h_55h()
                   15161: {
                   15162:        if(!support_ems) {
                   15163:                REG8(AH) = 0x84;
                   15164:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   15165:                REG8(AH) = 0x83;
                   15166:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15167:                UINT16 jump_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0);
                   15168:                UINT16 jump_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2);
                   15169:                UINT8  entries  = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 4);
                   15170:                UINT16 map_ofs  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 5);
                   15171:                UINT16 map_seg  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 7);
                   15172:                
                   15173:                for(int i = 0; i < (int)entries; i++) {
                   15174:                        int logical  = *(UINT16 *)(mem + (map_seg << 4) + map_ofs + 4 * i + 0);
                   15175:                        int physical = *(UINT16 *)(mem + (map_seg << 4) + map_ofs + 4 * i + 2);
                   15176:                        
                   15177:                        if(REG8(AL) == 0x01) {
                   15178:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15179:                        }
                   15180: //                     if(!(physical < 4)) {
                   15181: //                             REG8(AH) = 0x8b;
                   15182: //                             return;
                   15183: //                     } else
                   15184:                        if(logical == 0xffff) {
                   15185:                                ems_unmap_page(physical & 3);
                   15186:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15187:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15188:                        } else {
                   15189:                                REG8(AH) = 0x8a;
                   15190:                                return;
                   15191:                        }
                   15192:                }
                   15193:                i386_jmp_far(jump_seg, jump_ofs);
                   15194:                REG8(AH) = 0x00;
                   15195:        } else {
                   15196:                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));
                   15197:                REG8(AH) = 0x8f;
                   15198:        }
                   15199: }
                   15200: 
                   15201: inline void msdos_int_67h_56h()
                   15202: {
                   15203:        if(!support_ems) {
                   15204:                REG8(AH) = 0x84;
                   15205:        } else if(REG8(AL) == 0x02) {
                   15206:                REG16(BX) = (2 + 2) * 4;
                   15207:                REG8(AH) = 0x00;
                   15208:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   15209:                REG8(AH) = 0x83;
                   15210:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15211:                UINT16 call_ofs    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  0);
                   15212:                UINT16 call_seg    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  2);
                   15213:                UINT8  new_entries = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) +  4);
                   15214:                UINT16 new_map_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  5);
                   15215:                UINT16 new_map_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) +  7);
                   15216: #if 0
                   15217:                UINT8  old_entries = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) +  9);
                   15218:                UINT16 old_map_ofs = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 10);
                   15219:                UINT16 old_map_seg = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 12);
                   15220: #endif
                   15221:                UINT16 handles[4], pages[4];
                   15222:                
                   15223:                // alter page map and call routine is at fffc:001f
                   15224:                if(!(call_seg == 0 && call_ofs == 0)) {
                   15225:                        mem[DUMMY_TOP + 0x1f] = 0x9a;   // call far
                   15226:                        mem[DUMMY_TOP + 0x20] = (call_ofs >> 0) & 0xff;
                   15227:                        mem[DUMMY_TOP + 0x21] = (call_ofs >> 8) & 0xff;
                   15228:                        mem[DUMMY_TOP + 0x22] = (call_seg >> 0) & 0xff;
                   15229:                        mem[DUMMY_TOP + 0x23] = (call_seg >> 8) & 0xff;
                   15230:                } else {
                   15231:                        // invalid call addr :-(
                   15232:                        mem[DUMMY_TOP + 0x1f] = 0x90;   // nop
                   15233:                        mem[DUMMY_TOP + 0x20] = 0x90;   // nop
                   15234:                        mem[DUMMY_TOP + 0x21] = 0x90;   // nop
                   15235:                        mem[DUMMY_TOP + 0x22] = 0x90;   // nop
                   15236:                        mem[DUMMY_TOP + 0x23] = 0x90;   // nop
                   15237:                }
                   15238:                // do call far (push cs/ip) in old mapping
                   15239:                i386_call_far(DUMMY_TOP >> 4, 0x001f);
                   15240:                
                   15241:                // get old mapping data
                   15242: #if 0
                   15243:                for(int i = 0; i < (int)old_entries; i++) {
                   15244:                        int logical  = *(UINT16 *)(mem + (old_map_seg << 4) + old_map_ofs + 4 * i + 0);
                   15245:                        int physical = *(UINT16 *)(mem + (old_map_seg << 4) + old_map_ofs + 4 * i + 2);
                   15246:                        
                   15247:                        if(REG8(AL) == 0x01) {
                   15248:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15249:                        }
                   15250: //                     if(!(physical < 4)) {
                   15251: //                             REG8(AH) = 0x8b;
                   15252: //                             return;
                   15253: //                     } else
                   15254:                        if(logical == 0xffff) {
                   15255:                                ems_unmap_page(physical & 3);
                   15256:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15257:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15258:                        } else {
                   15259:                                REG8(AH) = 0x8a;
                   15260:                                return;
                   15261:                        }
                   15262:                }
                   15263: #endif
                   15264:                for(int i = 0; i < 4; i++) {
                   15265:                        handles[i] = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   15266:                        pages  [i] = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   15267:                }
                   15268:                
                   15269:                // set new mapping
                   15270:                for(int i = 0; i < (int)new_entries; i++) {
                   15271:                        int logical  = *(UINT16 *)(mem + (new_map_seg << 4) + new_map_ofs + 4 * i + 0);
                   15272:                        int physical = *(UINT16 *)(mem + (new_map_seg << 4) + new_map_ofs + 4 * i + 2);
                   15273:                        
                   15274:                        if(REG8(AL) == 0x01) {
                   15275:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   15276:                        }
                   15277: //                     if(!(physical < 4)) {
                   15278: //                             REG8(AH) = 0x8b;
                   15279: //                             return;
                   15280: //                     } else
                   15281:                        if(logical == 0xffff) {
                   15282:                                ems_unmap_page(physical & 3);
                   15283:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   15284:                                ems_map_page(physical & 3, REG16(DX), logical);
                   15285:                        } else {
                   15286:                                REG8(AH) = 0x8a;
                   15287:                                return;
                   15288:                        }
                   15289:                }
                   15290:                
                   15291:                // push old mapping data in new mapping
                   15292:                for(int i = 0; i < 4; i++) {
                   15293:                        i386_push16(handles[i]);
                   15294:                        i386_push16(pages  [i]);
                   15295:                }
                   15296:                REG8(AH) = 0x00;
                   15297:        } else {
                   15298:                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));
                   15299:                REG8(AH) = 0x8f;
                   15300:        }
                   15301: }
                   15302: 
1.1.1.20  root     15303: inline void msdos_int_67h_57h_tmp()
                   15304: {
                   15305:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   15306:        UINT8  src_type    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   15307:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x05);
                   15308:        UINT16 src_ofs     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07);
                   15309:        UINT16 src_seg     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x09);
                   15310:        UINT8  dest_type   = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0b);
                   15311:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   15312:        UINT16 dest_ofs    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0e);
                   15313:        UINT16 dest_seg    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x10);
                   15314:        
1.1.1.32  root     15315:        UINT8 *src_buffer = NULL, *dest_buffer = NULL;
1.1.1.20  root     15316:        UINT32 src_addr, dest_addr;
                   15317:        UINT32 src_addr_max, dest_addr_max;
                   15318:        
                   15319:        if(src_type == 0) {
                   15320:                src_buffer = mem;
                   15321:                src_addr = (src_seg << 4) + src_ofs;
                   15322:                src_addr_max = MAX_MEM;
                   15323:        } else {
1.1.1.31  root     15324:                if(!(src_handle >= 1 && src_handle <= MAX_EMS_HANDLES && ems_handles[src_handle].allocated)) {
1.1.1.20  root     15325:                        REG8(AH) = 0x83;
                   15326:                        return;
                   15327:                } else if(!(src_seg < ems_handles[src_handle].pages)) {
                   15328:                        REG8(AH) = 0x8a;
                   15329:                        return;
                   15330:                }
1.1.1.32  root     15331:                if(ems_handles[src_handle].buffer != NULL) {
                   15332:                        src_buffer = ems_handles[src_handle].buffer + 0x4000 * src_seg;
                   15333:                }
1.1.1.20  root     15334:                src_addr = src_ofs;
1.1.1.32  root     15335:                src_addr_max = 0x4000 * (ems_handles[src_handle].pages - src_seg);
1.1.1.20  root     15336:        }
                   15337:        if(dest_type == 0) {
                   15338:                dest_buffer = mem;
                   15339:                dest_addr = (dest_seg << 4) + dest_ofs;
                   15340:                dest_addr_max = MAX_MEM;
                   15341:        } else {
1.1.1.31  root     15342:                if(!(dest_handle >= 1 && dest_handle <= MAX_EMS_HANDLES && ems_handles[dest_handle].allocated)) {
1.1.1.20  root     15343:                        REG8(AH) = 0x83;
                   15344:                        return;
                   15345:                } else if(!(dest_seg < ems_handles[dest_handle].pages)) {
                   15346:                        REG8(AH) = 0x8a;
                   15347:                        return;
                   15348:                }
1.1.1.32  root     15349:                if(ems_handles[dest_handle].buffer != NULL) {
                   15350:                        dest_buffer = ems_handles[dest_handle].buffer + 0x4000 * dest_seg;
                   15351:                }
1.1.1.20  root     15352:                dest_addr = dest_ofs;
1.1.1.32  root     15353:                dest_addr_max = 0x4000 * (ems_handles[dest_handle].pages - dest_seg);
1.1.1.20  root     15354:        }
1.1.1.32  root     15355:        if(src_buffer != NULL && dest_buffer != NULL) {
                   15356:                for(int i = 0; i < copy_length; i++) {
                   15357:                        if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   15358:                                if(REG8(AL) == 0x00) {
                   15359:                                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   15360:                                } else if(REG8(AL) == 0x01) {
                   15361:                                        UINT8 tmp = dest_buffer[dest_addr];
                   15362:                                        dest_buffer[dest_addr++] = src_buffer[src_addr];
                   15363:                                        src_buffer[src_addr++] = tmp;
                   15364:                                }
                   15365:                        } else {
                   15366:                                REG8(AH) = 0x93;
                   15367:                                return;
1.1.1.20  root     15368:                        }
                   15369:                }
1.1.1.32  root     15370:                REG8(AH) = 0x00;
                   15371:        } else {
                   15372:                REG8(AH) = 0x80;
1.1.1.20  root     15373:        }
                   15374: }
                   15375: 
                   15376: inline void msdos_int_67h_57h()
                   15377: {
                   15378:        if(!support_ems) {
                   15379:                REG8(AH) = 0x84;
                   15380:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   15381:                struct {
                   15382:                        UINT16 handle;
                   15383:                        UINT16 page;
                   15384:                        bool mapped;
                   15385:                } tmp_pages[4];
                   15386:                
                   15387:                // unmap pages to copy memory data to ems buffer
                   15388:                for(int i = 0; i < 4; i++) {
                   15389:                        tmp_pages[i].handle = ems_pages[i].handle;
                   15390:                        tmp_pages[i].page   = ems_pages[i].page;
                   15391:                        tmp_pages[i].mapped = ems_pages[i].mapped;
                   15392:                        ems_unmap_page(i);
                   15393:                }
                   15394:                
                   15395:                // run move/exchange operation
                   15396:                msdos_int_67h_57h_tmp();
                   15397:                
                   15398:                // restore unmapped pages
                   15399:                for(int i = 0; i < 4; i++) {
                   15400:                        if(tmp_pages[i].mapped) {
                   15401:                                ems_map_page(i, tmp_pages[i].handle, tmp_pages[i].page);
                   15402:                        }
                   15403:                }
                   15404:        } else {
1.1.1.22  root     15405:                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     15406:                REG8(AH) = 0x8f;
                   15407:        }
                   15408: }
                   15409: 
                   15410: inline void msdos_int_67h_58h()
                   15411: {
                   15412:        if(!support_ems) {
                   15413:                REG8(AH) = 0x84;
                   15414:        } else if(REG8(AL) == 0x00) {
                   15415:                for(int i = 0; i < 4; i++) {
1.1.1.30  root     15416:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = (EMS_TOP + 0x4000 * i) >> 4;
                   15417:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = i;
1.1.1.20  root     15418:                }
                   15419:                REG8(AH) = 0x00;
                   15420:                REG16(CX) = 4;
                   15421:        } else if(REG8(AL) == 0x01) {
                   15422:                REG8(AH) = 0x00;
                   15423:                REG16(CX) = 4;
                   15424:        } else {
1.1.1.22  root     15425:                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     15426:                REG8(AH) = 0x8f;
                   15427:        }
                   15428: }
                   15429: 
1.1.1.42  root     15430: inline void msdos_int_67h_59h()
                   15431: {
                   15432:        if(!support_ems) {
                   15433:                REG8(AH) = 0x84;
                   15434:        } else if(REG8(AL) == 0x00) {
1.1.1.49  root     15435:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 1024;
                   15436:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2) = 0;
                   15437:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4) = 4 * 4;
                   15438:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 6) = 0;
                   15439:                REG8(AH) = 0x00;
                   15440: //             REG8(AH) = 0xa4; // access denied by operating system
1.1.1.42  root     15441:        } else if(REG8(AL) == 0x01) {
                   15442:                REG8(AH) = 0x00;
                   15443:                REG16(BX) = free_ems_pages;
                   15444:                REG16(DX) = MAX_EMS_PAGES;
                   15445:        } else {
                   15446:                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));
                   15447:                REG8(AH) = 0x8f;
                   15448:        }
                   15449: }
                   15450: 
1.1.1.20  root     15451: inline void msdos_int_67h_5ah()
                   15452: {
                   15453:        if(!support_ems) {
1.1.1.19  root     15454:                REG8(AH) = 0x84;
1.1.1.20  root     15455:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   15456:                REG8(AH) = 0x87;
                   15457:        } else if(REG16(BX) > free_ems_pages) {
                   15458:                REG8(AH) = 0x88;
                   15459: //     } else if(REG16(BX) == 0) {
                   15460: //             REG8(AH) = 0x89;
                   15461:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
1.1.1.31  root     15462:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.20  root     15463:                        if(!ems_handles[i].allocated) {
                   15464:                                ems_allocate_pages(i, REG16(BX));
                   15465:                                REG8(AH) = 0x00;
                   15466:                                REG16(DX) = i;
                   15467:                                return;
                   15468:                        }
                   15469:                }
                   15470:                REG8(AH) = 0x85;
                   15471:        } else {
1.1.1.22  root     15472:                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     15473:                REG8(AH) = 0x8f;
1.1.1.19  root     15474:        }
                   15475: }
                   15476: 
1.1.1.49  root     15477: inline void msdos_int_67h_5bh()
                   15478: {
                   15479:        static UINT8  stored_bl = 0x00;
                   15480:        static UINT16 stored_es = 0x0000;
                   15481:        static UINT16 stored_di = 0x0000;
                   15482:        
                   15483:        if(!support_ems) {
                   15484:                REG8(AH) = 0x84;
                   15485:        } else if(REG8(AL) == 0x00) {
                   15486:                if(stored_bl == 0x00) {
                   15487:                        if(!(stored_es == 0 && stored_di == 0)) {
                   15488:                                for(int i = 0; i < 4; i++) {
                   15489:                                        *(UINT16 *)(mem + (stored_es << 4) + stored_di + 4 * i + 0) = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   15490:                                        *(UINT16 *)(mem + (stored_es << 4) + stored_di + 4 * i + 2) = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   15491:                                }
                   15492:                        }
                   15493:                        SREG(ES) = stored_es;
                   15494:                        i386_load_segment_descriptor(ES);
                   15495:                        REG16(DI) = stored_di;
                   15496:                } else {
                   15497:                        REG8(BL) = stored_bl;
                   15498:                }
                   15499:                REG8(AH) = 0x00;
                   15500:        } else if(REG8(AL) == 0x01) {
                   15501:                if(REG8(BL) == 0x00) {
                   15502:                        if(!(SREG(ES) == 0 && REG16(DI) == 0)) {
                   15503:                                for(int i = 0; i < 4; i++) {
                   15504:                                        UINT16 handle = *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0);
                   15505:                                        UINT16 page   = *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2);
                   15506:                                        
                   15507:                                        if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
                   15508:                                                ems_map_page(i, handle, page);
                   15509:                                        } else {
                   15510:                                                ems_unmap_page(i);
                   15511:                                        }
                   15512:                                }
                   15513:                        }
                   15514:                }
                   15515:                stored_bl = REG8(BL);
                   15516:                stored_es = SREG(ES);
                   15517:                stored_di = REG16(DI);
                   15518:                REG8(AH) = 0x00;
                   15519:        } else if(REG8(AL) == 0x02) {
                   15520:                REG16(DX) = 4 * 4;
                   15521:                REG8(AH) = 0x00;
                   15522:        } else if(REG8(AL) == 0x03) {
                   15523:                REG8(BL) = 0x00; // not supported
                   15524:                REG8(AH) = 0x00;
                   15525:        } else if(REG8(AL) == 0x04) {
                   15526:                REG8(AH) = 0x00;
                   15527:        } else if(REG8(AL) == 0x05) {
                   15528:                REG8(BL) = 0x00; // not supported
                   15529:                REG8(AH) = 0x00;
                   15530:        } else {
                   15531:                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));
                   15532:                REG8(AH) = 0x8f;
                   15533:        }
                   15534: }
                   15535: 
1.1.1.43  root     15536: inline void msdos_int_67h_5dh()
                   15537: {
                   15538:        if(!support_ems) {
                   15539:                REG8(AH) = 0x84;
                   15540:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   15541:                REG8(AH) = 0xa4; // operating system denied access
                   15542:        } else {
                   15543:                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));
                   15544:                REG8(AH) = 0x8f;
                   15545:        }
                   15546: }
                   15547: 
1.1.1.49  root     15548: inline void msdos_int_67h_70h()
                   15549: {
                   15550:        if(!support_ems) {
                   15551:                REG8(AH) = 0x84;
                   15552:        } else if(REG8(AL) == 0x00) {
                   15553:                REG8(AL) = 0x00;
                   15554:                REG8(AH) = 0x00;
                   15555:        } else if(REG8(AL) == 0x01) {
                   15556:                REG8(AL) = 0x00;
                   15557: //             REG8(AH) = (REG8(BL) == 0x00) ? 0x00 : 0x80;
                   15558:                REG8(AH) = 0x00;
                   15559:        } else {
                   15560:                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));
                   15561:                REG8(AH) = 0x8f;
                   15562:        }
                   15563: }
                   15564: 
1.1.1.30  root     15565: inline void msdos_int_67h_deh()
                   15566: {
                   15567:        REG8(AH) = 0x84;
                   15568: }
                   15569: 
1.1.1.19  root     15570: #ifdef SUPPORT_XMS
                   15571: 
1.1.1.32  root     15572: void msdos_xms_init()
1.1.1.26  root     15573: {
1.1.1.30  root     15574:        emb_handle_top = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   15575:        emb_handle_top->address = EMB_TOP;
                   15576:        emb_handle_top->size_kb = (EMB_END - EMB_TOP) >> 10;
1.1.1.26  root     15577:        xms_a20_local_enb_count = 0;
                   15578: }
                   15579: 
1.1.1.32  root     15580: void msdos_xms_finish()
                   15581: {
                   15582:        msdos_xms_release();
                   15583: }
                   15584: 
                   15585: void msdos_xms_release()
1.1.1.30  root     15586: {
                   15587:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL;) {
                   15588:                emb_handle_t *next_handle = emb_handle->next;
                   15589:                free(emb_handle);
                   15590:                emb_handle = next_handle;
                   15591:        }
                   15592: }
                   15593: 
                   15594: emb_handle_t *msdos_xms_get_emb_handle(int handle)
                   15595: {
                   15596:        if(handle != 0) {
                   15597:                for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   15598:                        if(emb_handle->handle == handle) {
                   15599:                                return(emb_handle);
                   15600:                        }
                   15601:                }
                   15602:        }
                   15603:        return(NULL);
                   15604: }
                   15605: 
                   15606: int msdos_xms_get_unused_emb_handle_id()
                   15607: {
                   15608:        for(int handle = 1;; handle++) {
                   15609:                if(msdos_xms_get_emb_handle(handle) == NULL) {
                   15610:                        return(handle);
                   15611:                }
                   15612:        }
                   15613:        return(0);
                   15614: }
                   15615: 
                   15616: int msdos_xms_get_unused_emb_handle_count()
                   15617: {
                   15618:        int count = 64; //255;
                   15619:        
                   15620:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   15621:                if(emb_handle->handle != 0) {
                   15622:                        if(--count == 1) {
                   15623:                                break;
                   15624:                        }
                   15625:                }
                   15626:        }
                   15627:        return(count);
                   15628: }
                   15629: 
                   15630: void msdos_xms_split_emb_handle(emb_handle_t *emb_handle, int size_kb)
                   15631: {
                   15632:        if(emb_handle->size_kb > size_kb) {
                   15633:                emb_handle_t *new_handle = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   15634:                
                   15635:                new_handle->address = emb_handle->address + size_kb * 1024;
                   15636:                new_handle->size_kb = emb_handle->size_kb - size_kb;
                   15637:                emb_handle->size_kb = size_kb;
                   15638:                
                   15639:                new_handle->prev = emb_handle;
                   15640:                new_handle->next = emb_handle->next;
                   15641:                if(emb_handle->next != NULL) {
                   15642:                        emb_handle->next->prev = new_handle;
                   15643:                }
                   15644:                emb_handle->next = new_handle;
                   15645:        }
                   15646: }
                   15647: 
                   15648: void msdos_xms_combine_emb_handles(emb_handle_t *emb_handle)
                   15649: {
                   15650:        emb_handle_t *next_handle = emb_handle->next;
                   15651:        
                   15652:        if(next_handle != NULL) {
                   15653:                emb_handle->size_kb += next_handle->size_kb;
                   15654:                
                   15655:                if(next_handle->next != NULL) {
                   15656:                        next_handle->next->prev = emb_handle;
                   15657:                }
                   15658:                emb_handle->next = next_handle->next;
                   15659:                free(next_handle);
                   15660:        }
                   15661: }
                   15662: 
                   15663: emb_handle_t *msdos_xms_alloc_emb_handle(int size_kb)
                   15664: {
                   15665:        emb_handle_t *target_handle = NULL;
                   15666:        
                   15667:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   15668:                if(emb_handle->handle == 0 && emb_handle->size_kb >= size_kb) {
                   15669:                        if(target_handle == NULL || target_handle->size_kb > emb_handle->size_kb) {
                   15670:                                target_handle = emb_handle;
                   15671:                        }
                   15672:                }
                   15673:        }
                   15674:        if(target_handle != NULL) {
                   15675:                if(target_handle->size_kb > size_kb) {
                   15676:                        msdos_xms_split_emb_handle(target_handle, size_kb);
                   15677:                }
                   15678: //             target_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   15679:                return(target_handle);
                   15680:        }
                   15681:        return(NULL);
                   15682: }
                   15683: 
                   15684: void msdos_xms_free_emb_handle(emb_handle_t *emb_handle)
                   15685: {
                   15686:        emb_handle_t *prev_handle = emb_handle->prev;
                   15687:        emb_handle_t *next_handle = emb_handle->next;
                   15688:        
                   15689:        if(prev_handle != NULL && prev_handle->handle == 0) {
                   15690:                msdos_xms_combine_emb_handles(prev_handle);
                   15691:                emb_handle = prev_handle;
                   15692:        }
                   15693:        if(next_handle != NULL && next_handle->handle == 0) {
                   15694:                msdos_xms_combine_emb_handles(emb_handle);
                   15695:        }
                   15696:        emb_handle->handle = 0;
                   15697: }
                   15698: 
1.1.1.19  root     15699: inline void msdos_call_xms_00h()
                   15700: {
1.1.1.29  root     15701: #if defined(HAS_I386)
                   15702:        REG16(AX) = 0x0300; // V3.00 (XMS Version)
1.1.1.45  root     15703: //     REG16(BX) = 0x0395; // V3.95 (Driver Revision in BCD)
1.1.1.29  root     15704:        REG16(BX) = 0x035f; // V3.95 (Driver Revision)
                   15705: #else
                   15706:        REG16(AX) = 0x0200; // V2.00 (XMS Version)
                   15707:        REG16(BX) = 0x0270; // V2.70 (Driver Revision)
                   15708: #endif
                   15709: //     REG16(DX) = 0x0000; // HMA does not exist
                   15710:        REG16(DX) = 0x0001; // HMA does exist
1.1.1.19  root     15711: }
                   15712: 
                   15713: inline void msdos_call_xms_01h()
                   15714: {
1.1.1.29  root     15715:        if(REG8(AL) == 0x40) {
                   15716:                // HIMEM.SYS will fail function 01h with error code 91h if AL=40h and
                   15717:                // DX=KB free extended memory returned by last call of function 08h
                   15718:                REG16(AX) = 0x0000;
                   15719:                REG8(BL) = 0x91;
                   15720:                REG16(DX) = xms_dx_after_call_08h;
                   15721:        } else if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   15722:                REG16(AX) = 0x0000;
                   15723:                REG8(BL) = 0x81; // Vdisk was detected
                   15724: #ifdef SUPPORT_HMA
                   15725:        } else if(is_hma_used_by_int_2fh) {
                   15726:                REG16(AX) = 0x0000;
                   15727:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   15728:        } else if(is_hma_used_by_xms) {
                   15729:                REG16(AX) = 0x0000;
                   15730:                REG8(BL) = 0x91; // HMA is already in use
                   15731:        } else {
                   15732:                REG16(AX) = 0x0001;
                   15733:                is_hma_used_by_xms = true;
                   15734: #else
                   15735:        } else {
                   15736:                REG16(AX) = 0x0000;
                   15737:                REG8(BL) = 0x91; // HMA is already in use
                   15738: #endif
                   15739:        }
1.1.1.19  root     15740: }
                   15741: 
                   15742: inline void msdos_call_xms_02h()
                   15743: {
1.1.1.29  root     15744:        if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   15745:                REG16(AX) = 0x0000;
                   15746:                REG8(BL) = 0x81; // Vdisk was detected
                   15747: #ifdef SUPPORT_HMA
                   15748:        } else if(is_hma_used_by_int_2fh) {
                   15749:                REG16(AX) = 0x0000;
                   15750:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   15751:        } else if(!is_hma_used_by_xms) {
                   15752:                REG16(AX) = 0x0000;
                   15753:                REG8(BL) = 0x93; // HMA is not allocated
                   15754:        } else {
                   15755:                REG16(AX) = 0x0001;
                   15756:                is_hma_used_by_xms = false;
                   15757:                // restore first free mcb in high memory area
                   15758:                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   15759: #else
                   15760:        } else {
                   15761:                REG16(AX) = 0x0000;
                   15762:                REG8(BL) = 0x91; // HMA is already in use
                   15763: #endif
                   15764:        }
1.1.1.19  root     15765: }
                   15766: 
                   15767: inline void msdos_call_xms_03h()
                   15768: {
                   15769:        i386_set_a20_line(1);
                   15770:        REG16(AX) = 0x0001;
                   15771:        REG8(BL) = 0x00;
                   15772: }
                   15773: 
                   15774: inline void msdos_call_xms_04h()
                   15775: {
1.1.1.21  root     15776:        i386_set_a20_line(0);
                   15777:        REG16(AX) = 0x0001;
                   15778:        REG8(BL) = 0x00;
1.1.1.19  root     15779: }
                   15780: 
                   15781: inline void msdos_call_xms_05h()
                   15782: {
                   15783:        i386_set_a20_line(1);
                   15784:        REG16(AX) = 0x0001;
                   15785:        REG8(BL) = 0x00;
1.1.1.21  root     15786:        xms_a20_local_enb_count++;
1.1.1.19  root     15787: }
                   15788: 
                   15789: void msdos_call_xms_06h()
                   15790: {
1.1.1.21  root     15791:        if(xms_a20_local_enb_count > 0) {
1.1.1.45  root     15792:                if(--xms_a20_local_enb_count == 0) {
                   15793:                        i386_set_a20_line(0);
                   15794:                        REG16(AX) = 0x0001;
                   15795:                        REG8(BL) = 0x00;
                   15796:                } else {
                   15797:                        REG16(AX) = 0x0000;
                   15798:                        REG8(BL) = 0x94;
                   15799:                }
1.1.1.21  root     15800:        } else {
1.1.1.45  root     15801:                i386_set_a20_line(0);
1.1.1.21  root     15802:                REG16(AX) = 0x0001;
                   15803:                REG8(BL) = 0x00;
1.1.1.19  root     15804:        }
                   15805: }
                   15806: 
                   15807: inline void msdos_call_xms_07h()
                   15808: {
                   15809:        REG16(AX) = (m_a20_mask >> 20) & 1;
                   15810:        REG8(BL) = 0x00;
                   15811: }
                   15812: 
                   15813: inline void msdos_call_xms_08h()
                   15814: {
1.1.1.45  root     15815:        UINT32 eax = 0, edx = 0;
1.1.1.19  root     15816:        
1.1.1.30  root     15817:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   15818:                if(emb_handle->handle == 0) {
1.1.1.45  root     15819:                        if(eax < emb_handle->size_kb) {
                   15820:                                eax = emb_handle->size_kb;
1.1.1.19  root     15821:                        }
1.1.1.45  root     15822:                        edx += emb_handle->size_kb;
1.1.1.19  root     15823:                }
                   15824:        }
1.1.1.45  root     15825:        if(eax > 65535) {
                   15826:                eax = 65535;
                   15827:        }
                   15828:        if(edx > 65535) {
                   15829:                edx = 65535;
                   15830:        }
                   15831:        if(eax == 0 && edx == 0) {
1.1.1.19  root     15832:                REG8(BL) = 0xa0;
                   15833:        } else {
                   15834:                REG8(BL) = 0x00;
                   15835:        }
1.1.1.45  root     15836: #if defined(HAS_I386)
                   15837:        REG32(EAX) = eax;
                   15838:        REG32(EDX) = edx;
                   15839: #else
                   15840:        REG16(AX) = (UINT16)eax;
                   15841:        REG16(DX) = (UINT16)edx;
                   15842: #endif
1.1.1.29  root     15843:        xms_dx_after_call_08h = REG16(DX);
1.1.1.19  root     15844: }
                   15845: 
1.1.1.30  root     15846: void msdos_call_xms_09h(int size_kb)
1.1.1.19  root     15847: {
1.1.1.30  root     15848:        emb_handle_t *emb_handle = msdos_xms_alloc_emb_handle(size_kb);
                   15849:        
                   15850:        if(emb_handle != NULL) {
                   15851:                emb_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   15852:                
                   15853:                REG16(AX) = 0x0001;
                   15854:                REG16(DX) = emb_handle->handle;
                   15855:                REG8(BL) = 0x00;
                   15856:        } else {
                   15857:                REG16(AX) = REG16(DX) = 0x0000;
                   15858:                REG8(BL) = 0xa0;
1.1.1.19  root     15859:        }
1.1.1.30  root     15860: }
                   15861: 
                   15862: inline void msdos_call_xms_09h()
                   15863: {
                   15864:        msdos_call_xms_09h(REG16(DX));
1.1.1.19  root     15865: }
                   15866: 
                   15867: inline void msdos_call_xms_0ah()
                   15868: {
1.1.1.30  root     15869:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   15870:        
                   15871:        if(emb_handle == NULL) {
1.1.1.19  root     15872:                REG16(AX) = 0x0000;
                   15873:                REG8(BL) = 0xa2;
1.1.1.45  root     15874: //     } else if(emb_handle->lock > 0) {
                   15875: //             REG16(AX) = 0x0000;
                   15876: //             REG8(BL) = 0xab;
1.1.1.19  root     15877:        } else {
1.1.1.30  root     15878:                msdos_xms_free_emb_handle(emb_handle);
1.1.1.19  root     15879:                
                   15880:                REG16(AX) = 0x0001;
                   15881:                REG8(BL) = 0x00;
                   15882:        }
                   15883: }
                   15884: 
                   15885: inline void msdos_call_xms_0bh()
                   15886: {
                   15887:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   15888:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   15889:        UINT32 src_addr    = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06);
                   15890:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0a);
                   15891:        UINT32 dest_addr   = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   15892:        
                   15893:        UINT8 *src_buffer, *dest_buffer;
                   15894:        UINT32 src_addr_max, dest_addr_max;
1.1.1.30  root     15895:        emb_handle_t *emb_handle;
1.1.1.19  root     15896:        
                   15897:        if(src_handle == 0) {
                   15898:                src_buffer = mem;
                   15899:                src_addr = (((src_addr >> 16) & 0xffff) << 4) + (src_addr & 0xffff);
                   15900:                src_addr_max = MAX_MEM;
                   15901:        } else {
1.1.1.30  root     15902:                if((emb_handle = msdos_xms_get_emb_handle(src_handle)) == NULL) {
1.1.1.19  root     15903:                        REG16(AX) = 0x0000;
                   15904:                        REG8(BL) = 0xa3;
                   15905:                        return;
                   15906:                }
1.1.1.30  root     15907:                src_buffer = mem + emb_handle->address;
                   15908:                src_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     15909:        }
                   15910:        if(dest_handle == 0) {
                   15911:                dest_buffer = mem;
                   15912:                dest_addr = (((dest_addr >> 16) & 0xffff) << 4) + (dest_addr & 0xffff);
                   15913:                dest_addr_max = MAX_MEM;
                   15914:        } else {
1.1.1.30  root     15915:                if((emb_handle = msdos_xms_get_emb_handle(dest_handle)) == NULL) {
1.1.1.19  root     15916:                        REG16(AX) = 0x0000;
                   15917:                        REG8(BL) = 0xa5;
                   15918:                        return;
                   15919:                }
1.1.1.30  root     15920:                dest_buffer = mem + emb_handle->address;
                   15921:                dest_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     15922:        }
                   15923:        for(int i = 0; i < copy_length; i++) {
                   15924:                if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   15925:                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   15926:                } else {
                   15927:                        break;
                   15928:                }
                   15929:        }
                   15930:        REG16(AX) = 0x0001;
                   15931:        REG8(BL) = 0x00;
                   15932: }
                   15933: 
                   15934: inline void msdos_call_xms_0ch()
                   15935: {
1.1.1.30  root     15936:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   15937:        
                   15938:        if(emb_handle == NULL) {
1.1.1.19  root     15939:                REG16(AX) = 0x0000;
                   15940:                REG8(BL) = 0xa2;
                   15941:        } else {
1.1.1.45  root     15942:                if(emb_handle->lock < 255) {
                   15943:                        emb_handle->lock++;
                   15944:                }
1.1.1.19  root     15945:                REG16(AX) = 0x0001;
1.1.1.30  root     15946:                REG16(DX) = (emb_handle->address >> 16) & 0xffff;
                   15947:                REG16(BX) = (emb_handle->address      ) & 0xffff;
1.1.1.19  root     15948:        }
                   15949: }
                   15950: 
                   15951: inline void msdos_call_xms_0dh()
                   15952: {
1.1.1.30  root     15953:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   15954:        
                   15955:        if(emb_handle == NULL) {
1.1.1.19  root     15956:                REG16(AX) = 0x0000;
                   15957:                REG8(BL) = 0xa2;
1.1.1.30  root     15958:        } else if(!(emb_handle->lock > 0)) {
1.1.1.19  root     15959:                REG16(AX) = 0x0000;
                   15960:                REG8(BL) = 0xaa;
                   15961:        } else {
1.1.1.30  root     15962:                emb_handle->lock--;
1.1.1.19  root     15963:                REG16(AX) = 0x0001;
                   15964:                REG8(BL) = 0x00;
                   15965:        }
                   15966: }
                   15967: 
                   15968: inline void msdos_call_xms_0eh()
                   15969: {
1.1.1.30  root     15970:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   15971:        
                   15972:        if(emb_handle == NULL) {
1.1.1.19  root     15973:                REG16(AX) = 0x0000;
                   15974:                REG8(BL) = 0xa2;
                   15975:        } else {
                   15976:                REG16(AX) = 0x0001;
1.1.1.30  root     15977:                REG8(BH) = emb_handle->lock;
                   15978:                REG8(BL) = msdos_xms_get_unused_emb_handle_count();
                   15979:                REG16(DX) = emb_handle->size_kb;
1.1.1.19  root     15980:        }
                   15981: }
                   15982: 
1.1.1.30  root     15983: void msdos_call_xms_0fh(int size_kb)
1.1.1.19  root     15984: {
1.1.1.30  root     15985:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   15986:        
                   15987:        if(emb_handle == NULL) {
1.1.1.19  root     15988:                REG16(AX) = 0x0000;
                   15989:                REG8(BL) = 0xa2;
1.1.1.30  root     15990:        } else if(emb_handle->lock > 0) {
1.1.1.19  root     15991:                REG16(AX) = 0x0000;
                   15992:                REG8(BL) = 0xab;
                   15993:        } else {
1.1.1.30  root     15994:                if(emb_handle->size_kb < size_kb) {
                   15995:                        if(emb_handle->next != NULL && emb_handle->next->handle == 0 && (emb_handle->size_kb + emb_handle->next->size_kb) >= size_kb) {
                   15996:                                msdos_xms_combine_emb_handles(emb_handle);
                   15997:                                if(emb_handle->size_kb > size_kb) {
                   15998:                                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   15999:                                }
                   16000:                        } else {
                   16001:                                int old_handle = emb_handle->handle;
                   16002:                                int old_size_kb = emb_handle->size_kb;
                   16003:                                UINT8 *buffer = (UINT8 *)malloc(old_size_kb * 1024);
                   16004:                                
                   16005:                                memcpy(buffer, mem + emb_handle->address, old_size_kb * 1024);
                   16006:                                msdos_xms_free_emb_handle(emb_handle);
                   16007:                                
                   16008:                                if((emb_handle = msdos_xms_alloc_emb_handle(size_kb)) == NULL) {
                   16009:                                        emb_handle = msdos_xms_alloc_emb_handle(old_size_kb); // should be always successed
                   16010:                                }
                   16011:                                emb_handle->handle = old_handle;
                   16012:                                memcpy(mem + emb_handle->address, buffer, old_size_kb * 1024);
                   16013:                                free(buffer);
                   16014:                        }
                   16015:                } else if(emb_handle->size_kb > size_kb) {
                   16016:                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   16017:                }
                   16018:                if(emb_handle->size_kb != size_kb) {
                   16019:                        REG16(AX) = 0x0000;
                   16020:                        REG8(BL) = 0xa0;
                   16021:                } else {
                   16022:                        REG16(AX) = 0x0001;
                   16023:                        REG8(BL) = 0x00;
                   16024:                }
1.1.1.19  root     16025:        }
                   16026: }
                   16027: 
1.1.1.30  root     16028: inline void msdos_call_xms_0fh()
                   16029: {
                   16030:        msdos_call_xms_0fh(REG16(BX));
                   16031: }
                   16032: 
1.1.1.19  root     16033: inline void msdos_call_xms_10h()
                   16034: {
                   16035:        int seg;
                   16036:        
                   16037:        if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(DX), 0)) != -1) {
                   16038:                REG16(AX) = 0x0001;
                   16039:                REG16(BX) = seg;
                   16040:        } else {
                   16041:                REG16(AX) = 0x0000;
                   16042:                REG8(BL) = 0xb0;
                   16043:                REG16(DX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   16044:        }
                   16045: }
                   16046: 
                   16047: inline void msdos_call_xms_11h()
                   16048: {
                   16049:        int mcb_seg = REG16(DX) - 1;
                   16050:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   16051:        
                   16052:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   16053:                msdos_mem_free(REG16(DX));
                   16054:                REG16(AX) = 0x0001;
                   16055:                REG8(BL) = 0x00;
                   16056:        } else {
                   16057:                REG16(AX) = 0x0000;
                   16058:                REG8(BL) = 0xb2;
                   16059:        }
                   16060: }
                   16061: 
                   16062: inline void msdos_call_xms_12h()
                   16063: {
                   16064:        int mcb_seg = REG16(DX) - 1;
                   16065:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   16066:        int max_paragraphs;
                   16067:        
                   16068:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   16069:                if(!msdos_mem_realloc(REG16(DX), REG16(BX), &max_paragraphs)) {
                   16070:                        REG16(AX) = 0x0001;
                   16071:                        REG8(BL) = 0x00;
                   16072:                } else {
                   16073:                        REG16(AX) = 0x0000;
                   16074:                        REG8(BL) = 0xb0;
                   16075:                        REG16(DX) = max_paragraphs;
                   16076:                }
                   16077:        } else {
                   16078:                REG16(AX) = 0x0000;
                   16079:                REG8(BL) = 0xb2;
                   16080:        }
                   16081: }
                   16082: 
1.1.1.29  root     16083: #if defined(HAS_I386)
                   16084: 
                   16085: inline void msdos_call_xms_88h()
                   16086: {
                   16087:        REG32(EAX) = REG32(EDX) = 0x0000;
                   16088:        
1.1.1.30  root     16089:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   16090:                if(emb_handle->handle == 0) {
                   16091:                        if(REG32(EAX) < emb_handle->size_kb) {
                   16092:                                REG32(EAX) = emb_handle->size_kb;
1.1.1.29  root     16093:                        }
1.1.1.30  root     16094:                        REG32(EDX) += emb_handle->size_kb;
1.1.1.29  root     16095:                }
                   16096:        }
                   16097:        if(REG32(EAX) == 0 && REG32(EDX) == 0) {
                   16098:                REG8(BL) = 0xa0;
                   16099:        } else {
                   16100:                REG8(BL) = 0x00;
                   16101:        }
                   16102:        REG32(ECX) = EMB_END - 1;
                   16103: }
                   16104: 
                   16105: inline void msdos_call_xms_89h()
                   16106: {
1.1.1.30  root     16107:        msdos_call_xms_09h(REG32(EDX));
1.1.1.29  root     16108: }
                   16109: 
                   16110: inline void msdos_call_xms_8eh()
                   16111: {
1.1.1.30  root     16112:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   16113:        
                   16114:        if(emb_handle == NULL) {
1.1.1.29  root     16115:                REG16(AX) = 0x0000;
                   16116:                REG8(BL) = 0xa2;
                   16117:        } else {
                   16118:                REG16(AX) = 0x0001;
1.1.1.30  root     16119:                REG8(BH) = emb_handle->lock;
                   16120:                REG16(CX) = msdos_xms_get_unused_emb_handle_count();
                   16121:                REG32(EDX) = emb_handle->size_kb;
1.1.1.29  root     16122:        }
                   16123: }
                   16124: 
                   16125: inline void msdos_call_xms_8fh()
                   16126: {
1.1.1.30  root     16127:        msdos_call_xms_0fh(REG32(EBX));
1.1.1.29  root     16128: }
                   16129: 
                   16130: #endif
1.1.1.19  root     16131: #endif
                   16132: 
1.1.1.26  root     16133: UINT16 msdos_get_equipment()
                   16134: {
                   16135:        static UINT16 equip = 0;
                   16136:        
                   16137:        if(equip == 0) {
                   16138: #ifdef SUPPORT_FPU
                   16139:                equip |= (1 << 1);      // 80x87 coprocessor installed
                   16140: #endif
                   16141:                equip |= (1 << 2);      // pointing device installed (PS/2)
                   16142:                equip |= (2 << 4);      // initial video mode (80x25 color)
                   16143: //             equip |= (1 << 8);      // 0 if DMA installed
                   16144:                equip |= (2 << 9);      // number of serial ports
                   16145:                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     16146:                
                   16147:                // check only A: and B: if it is floppy drive
                   16148:                int n = 0;
                   16149:                for(int i = 0; i < 2; i++) {
1.1.1.44  root     16150:                        if(msdos_is_valid_drive(i) && msdos_is_removable_drive(i)) {
                   16151:                                n++;
1.1.1.28  root     16152:                        }
                   16153:                }
                   16154:                if(n != 0) {
                   16155:                        equip |= (1 << 0);      // floppy disk(s) installed
                   16156:                        n--;
                   16157:                        equip |= (n << 6);      // number of floppies installed less 1
                   16158:                }
                   16159: //             if(joyGetNumDevs() != 0) {
                   16160: //                     equip |= (1 << 12);     // game port installed
                   16161: //             }
1.1.1.26  root     16162:        }
                   16163:        return(equip);
                   16164: }
                   16165: 
1.1       root     16166: void msdos_syscall(unsigned num)
                   16167: {
1.1.1.22  root     16168: #ifdef ENABLE_DEBUG_SYSCALL
1.1.1.43  root     16169:        if(num == 0x08 || num == 0x1c) {
                   16170:                // don't log the timer interrupts
1.1.1.45  root     16171: //             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.50  root     16172:        } else if(num == 0x30) {
                   16173:                // dummy interrupt for call 0005h (call near)
                   16174:                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.43  root     16175:        } else if(num == 0x68) {
1.1.1.22  root     16176:                // dummy interrupt for EMS (int 67h)
1.1.1.33  root     16177:                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     16178:        } else if(num == 0x69) {
                   16179:                // dummy interrupt for XMS (call far)
1.1.1.33  root     16180:                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.50  root     16181:        } else if(num >= 0x6a && num <= 0x6f) {
1.1.1.45  root     16182:                // dummy interrupt
1.1.1.22  root     16183:        } else {
1.1.1.33  root     16184:                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     16185:        }
                   16186: #endif
1.1.1.36  root     16187:        // update cursor position
                   16188:        if(cursor_moved) {
                   16189:                pcbios_update_cursor_position();
                   16190:                cursor_moved = false;
                   16191:        }
1.1.1.50  root     16192: #ifdef USE_SERVICE_THREAD
                   16193:        // this is called from dummy loop to wait until a serive that waits input is done
                   16194:        if(!in_service)
                   16195: #endif
1.1.1.33  root     16196:        ctrl_break_detected = ctrl_break_pressed = ctrl_c_pressed = false;
1.1.1.22  root     16197:        
1.1       root     16198:        switch(num) {
                   16199:        case 0x00:
1.1.1.28  root     16200:                try {
                   16201:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   16202:                        error("division by zero\n");
                   16203:                } catch(...) {
                   16204:                        fatalerror("division by zero detected, and failed to terminate current process\n");
                   16205:                }
1.1       root     16206:                break;
                   16207:        case 0x04:
1.1.1.28  root     16208:                try {
                   16209:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   16210:                        error("overflow\n");
                   16211:                } catch(...) {
                   16212:                        fatalerror("overflow detected, and failed to terminate current process\n");
                   16213:                }
1.1       root     16214:                break;
                   16215:        case 0x06:
                   16216:                // NOTE: ish.com has illegal instruction...
1.1.1.14  root     16217:                if(!ignore_illegal_insn) {
1.1.1.28  root     16218:                        try {
                   16219:                                msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   16220:                                error("illegal instruction\n");
                   16221:                        } catch(...) {
                   16222:                                fatalerror("illegal instruction detected, and failed to terminate current process\n");
                   16223:                        }
1.1.1.14  root     16224:                } else {
                   16225: #if defined(HAS_I386)
1.1.1.39  root     16226:                        m_eip = m_int6h_skip_eip;
                   16227: #elif defined(HAS_I286)
                   16228:                        m_pc = m_int6h_skip_pc;
1.1.1.14  root     16229: #else
1.1.1.39  root     16230:                        // 8086/80186 ignore an invalid opcode
1.1.1.14  root     16231: #endif
                   16232:                }
1.1       root     16233:                break;
1.1.1.33  root     16234:        case 0x09:
                   16235:                // ctrl-break is pressed
                   16236:                if(raise_int_1bh) {
                   16237: #if defined(HAS_I386)
                   16238:                        m_ext = 0; // not an external interrupt
                   16239:                        i386_trap(0x1b, 1, 0);
                   16240:                        m_ext = 1;
                   16241: #else
                   16242:                        PREFIX86(_interrupt)(0x1b);
                   16243: #endif
                   16244:                        raise_int_1bh = false;
                   16245:                }
1.1.1.8   root     16246:        case 0x08:
1.1.1.14  root     16247: //             pcbios_irq0(); // this causes too slow emulation...
1.1.1.8   root     16248:        case 0x0b:
                   16249:        case 0x0c:
                   16250:        case 0x0d:
                   16251:        case 0x0e:
                   16252:        case 0x0f:
                   16253:                // EOI
                   16254:                pic[0].isr &= ~(1 << (num - 0x08));
                   16255:                pic_update();
                   16256:                break;
1.1       root     16257:        case 0x10:
                   16258:                // PC BIOS - Video
1.1.1.14  root     16259:                if(!restore_console_on_exit) {
1.1.1.15  root     16260:                        change_console_size(scr_width, scr_height);
1.1       root     16261:                }
1.1.1.3   root     16262:                m_CF = 0;
1.1       root     16263:                switch(REG8(AH)) {
1.1.1.16  root     16264:                case 0x00: pcbios_int_10h_00h(); break;
1.1       root     16265:                case 0x01: pcbios_int_10h_01h(); break;
                   16266:                case 0x02: pcbios_int_10h_02h(); break;
                   16267:                case 0x03: pcbios_int_10h_03h(); break;
                   16268:                case 0x05: pcbios_int_10h_05h(); break;
                   16269:                case 0x06: pcbios_int_10h_06h(); break;
                   16270:                case 0x07: pcbios_int_10h_07h(); break;
                   16271:                case 0x08: pcbios_int_10h_08h(); break;
                   16272:                case 0x09: pcbios_int_10h_09h(); break;
                   16273:                case 0x0a: pcbios_int_10h_0ah(); break;
                   16274:                case 0x0b: break;
1.1.1.40  root     16275:                case 0x0c: pcbios_int_10h_0ch(); break;
                   16276:                case 0x0d: pcbios_int_10h_0dh(); break;
1.1       root     16277:                case 0x0e: pcbios_int_10h_0eh(); break;
                   16278:                case 0x0f: pcbios_int_10h_0fh(); break;
                   16279:                case 0x10: break;
1.1.1.14  root     16280:                case 0x11: pcbios_int_10h_11h(); break;
                   16281:                case 0x12: pcbios_int_10h_12h(); break;
1.1       root     16282:                case 0x13: pcbios_int_10h_13h(); break;
1.1.1.30  root     16283:                case 0x18: pcbios_int_10h_18h(); break;
1.1.1.14  root     16284:                case 0x1a: pcbios_int_10h_1ah(); break;
1.1.1.24  root     16285:                case 0x1b: REG8(AL) = 0x00; break; // functionality/state information is not supported
                   16286:                case 0x1c: REG8(AL) = 0x00; break; // save/restore video state is not supported
1.1       root     16287:                case 0x1d: pcbios_int_10h_1dh(); break;
1.1.1.24  root     16288:                case 0x1e: REG8(AL) = 0x00; break; // flat-panel functions are not supported
                   16289:                case 0x1f: REG8(AL) = 0x00; break; // xga functions are not supported
1.1.1.22  root     16290:                case 0x4f: pcbios_int_10h_4fh(); break;
1.1.1.30  root     16291:                case 0x6f: break;
1.1.1.22  root     16292:                case 0x80: m_CF = 1; break; // unknown
                   16293:                case 0x81: m_CF = 1; break; // unknown
1.1       root     16294:                case 0x82: pcbios_int_10h_82h(); break;
1.1.1.22  root     16295:                case 0x83: pcbios_int_10h_83h(); break;
                   16296:                case 0x8b: break;
                   16297:                case 0x8c: m_CF = 1; break; // unknown
                   16298:                case 0x8d: m_CF = 1; break; // unknown
                   16299:                case 0x8e: m_CF = 1; break; // unknown
                   16300:                case 0x90: pcbios_int_10h_90h(); break;
                   16301:                case 0x91: pcbios_int_10h_91h(); break;
                   16302:                case 0x92: break;
                   16303:                case 0x93: break;
                   16304:                case 0xef: pcbios_int_10h_efh(); break;
1.1.1.24  root     16305:                case 0xfa: break; // ega register interface library is not installed
1.1       root     16306:                case 0xfe: pcbios_int_10h_feh(); break;
                   16307:                case 0xff: pcbios_int_10h_ffh(); break;
                   16308:                default:
1.1.1.22  root     16309:                        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));
                   16310:                        m_CF = 1;
1.1       root     16311:                        break;
                   16312:                }
                   16313:                break;
                   16314:        case 0x11:
                   16315:                // PC BIOS - Get Equipment List
1.1.1.26  root     16316:                REG16(AX) = msdos_get_equipment();
1.1       root     16317:                break;
                   16318:        case 0x12:
                   16319:                // PC BIOS - Get Memory Size
1.1.1.33  root     16320:                REG16(AX) = *(UINT16 *)(mem + 0x413);
1.1       root     16321:                break;
                   16322:        case 0x13:
1.1.1.42  root     16323:                // PC BIOS - Disk I/O
                   16324:                {
                   16325:                        static UINT8 last = 0x00;
                   16326:                        switch(REG8(AH)) {
                   16327:                        case 0x00: pcbios_int_13h_00h(); break;
                   16328:                        case 0x01: // get last status
                   16329:                                REG8(AH) = last;
                   16330:                                break;
                   16331:                        case 0x02: pcbios_int_13h_02h(); break;
                   16332:                        case 0x03: pcbios_int_13h_03h(); break;
                   16333:                        case 0x04: pcbios_int_13h_04h(); break;
                   16334:                        case 0x08: pcbios_int_13h_08h(); break;
                   16335:                        case 0x0a: pcbios_int_13h_02h(); break;
                   16336:                        case 0x0b: pcbios_int_13h_03h(); break;
                   16337:                        case 0x0d: pcbios_int_13h_00h(); break;
                   16338:                        case 0x10: pcbios_int_13h_10h(); break;
                   16339:                        case 0x15: pcbios_int_13h_15h(); break;
1.1.1.43  root     16340:                        case 0x41: pcbios_int_13h_41h(); break;
1.1.1.42  root     16341:                        case 0x05: // format
                   16342:                        case 0x06:
                   16343:                        case 0x07:
                   16344:                                REG8(AH) = 0x0c; // unsupported track or invalid media
                   16345:                                m_CF = 1;
                   16346:                                break;
                   16347:                        case 0x09:
                   16348:                        case 0x0c: // seek
                   16349:                        case 0x11: // recalib
                   16350:                        case 0x14:
                   16351:                        case 0x17:
                   16352:                                REG8(AH) = 0x00; // successful completion
                   16353:                                break;
1.1.1.43  root     16354:                        case 0x21: // QUICKCACHE II v4.20 - Flush Cache
                   16355:                        case 0xa1: // Super PC-Kwik v3.20+ - Flush Cache
                   16356:                                REG8(AH) = 0x01; // invalid function
                   16357:                                m_CF = 1;
                   16358:                                break;
1.1.1.42  root     16359:                        default:
                   16360:                                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));
                   16361:                                REG8(AH) = 0x01; // invalid function
                   16362:                                m_CF = 1;
                   16363:                                break;
                   16364:                        }
                   16365:                        last = REG8(AH);
                   16366:                }
1.1       root     16367:                break;
                   16368:        case 0x14:
                   16369:                // PC BIOS - Serial I/O
1.1.1.25  root     16370:                switch(REG8(AH)) {
                   16371:                case 0x00: pcbios_int_14h_00h(); break;
                   16372:                case 0x01: pcbios_int_14h_01h(); break;
                   16373:                case 0x02: pcbios_int_14h_02h(); break;
                   16374:                case 0x03: pcbios_int_14h_03h(); break;
                   16375:                case 0x04: pcbios_int_14h_04h(); break;
                   16376:                case 0x05: pcbios_int_14h_05h(); break;
                   16377:                default:
                   16378:                        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));
                   16379:                        break;
                   16380:                }
1.1       root     16381:                break;
                   16382:        case 0x15:
                   16383:                // PC BIOS
1.1.1.3   root     16384:                m_CF = 0;
1.1       root     16385:                switch(REG8(AH)) {
1.1.1.14  root     16386:                case 0x10: pcbios_int_15h_10h(); break;
1.1       root     16387:                case 0x23: pcbios_int_15h_23h(); break;
                   16388:                case 0x24: pcbios_int_15h_24h(); break;
1.1.1.24  root     16389:                case 0x41: break;
1.1       root     16390:                case 0x49: pcbios_int_15h_49h(); break;
1.1.1.22  root     16391:                case 0x50: pcbios_int_15h_50h(); break;
1.1.1.30  root     16392:                case 0x53: pcbios_int_15h_53h(); break;
1.1.1.43  root     16393:                case 0x84: pcbios_int_15h_84h(); break;
1.1       root     16394:                case 0x86: pcbios_int_15h_86h(); break;
                   16395:                case 0x87: pcbios_int_15h_87h(); break;
                   16396:                case 0x88: pcbios_int_15h_88h(); break;
                   16397:                case 0x89: pcbios_int_15h_89h(); break;
1.1.1.21  root     16398:                case 0x8a: pcbios_int_15h_8ah(); break;
1.1.1.22  root     16399:                case 0xc0: // PS/2 ???
                   16400:                case 0xc1:
                   16401:                case 0xc2:
1.1.1.30  root     16402:                case 0xc3: // PS50+ ???
                   16403:                case 0xc4:
1.1.1.22  root     16404:                        REG8(AH) = 0x86;
                   16405:                        m_CF = 1;
                   16406:                        break;
1.1.1.3   root     16407: #if defined(HAS_I386)
1.1       root     16408:                case 0xc9: pcbios_int_15h_c9h(); break;
1.1.1.3   root     16409: #endif
1.1       root     16410:                case 0xca: pcbios_int_15h_cah(); break;
1.1.1.22  root     16411:                case 0xe8: pcbios_int_15h_e8h(); break;
1.1       root     16412:                default:
1.1.1.22  root     16413:                        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));
                   16414:                        REG8(AH) = 0x86;
1.1.1.3   root     16415:                        m_CF = 1;
1.1       root     16416:                        break;
                   16417:                }
                   16418:                break;
                   16419:        case 0x16:
                   16420:                // PC BIOS - Keyboard
1.1.1.3   root     16421:                m_CF = 0;
1.1       root     16422:                switch(REG8(AH)) {
                   16423:                case 0x00: pcbios_int_16h_00h(); break;
                   16424:                case 0x01: pcbios_int_16h_01h(); break;
                   16425:                case 0x02: pcbios_int_16h_02h(); break;
                   16426:                case 0x03: pcbios_int_16h_03h(); break;
                   16427:                case 0x05: pcbios_int_16h_05h(); break;
                   16428:                case 0x10: pcbios_int_16h_00h(); break;
                   16429:                case 0x11: pcbios_int_16h_01h(); break;
                   16430:                case 0x12: pcbios_int_16h_12h(); break;
                   16431:                case 0x13: pcbios_int_16h_13h(); break;
                   16432:                case 0x14: pcbios_int_16h_14h(); break;
1.1.1.24  root     16433:                case 0x55: pcbios_int_16h_55h(); break;
1.1.1.30  root     16434:                case 0x6f: pcbios_int_16h_6fh(); break;
1.1.1.22  root     16435:                case 0xda: break; // unknown
1.1.1.43  root     16436:                case 0xdb: break; // unknown
1.1.1.22  root     16437:                case 0xff: break; // unknown
1.1       root     16438:                default:
1.1.1.22  root     16439:                        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     16440:                        break;
                   16441:                }
                   16442:                break;
                   16443:        case 0x17:
                   16444:                // PC BIOS - Printer
1.1.1.37  root     16445:                m_CF = 0;
                   16446:                switch(REG8(AH)) {
                   16447:                case 0x00: pcbios_int_17h_00h(); break;
                   16448:                case 0x01: pcbios_int_17h_01h(); break;
                   16449:                case 0x02: pcbios_int_17h_02h(); break;
                   16450:                case 0x03: pcbios_int_17h_03h(); break;
                   16451:                case 0x50: pcbios_int_17h_50h(); break;
                   16452:                case 0x51: pcbios_int_17h_51h(); break;
                   16453:                case 0x52: pcbios_int_17h_52h(); break;
                   16454:                case 0x84: pcbios_int_17h_84h(); break;
                   16455:                case 0x85: pcbios_int_17h_85h(); break;
                   16456:                default:
                   16457:                        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));
                   16458:                        break;
                   16459:                }
1.1       root     16460:                break;
                   16461:        case 0x1a:
                   16462:                // PC BIOS - Timer
1.1.1.3   root     16463:                m_CF = 0;
1.1       root     16464:                switch(REG8(AH)) {
                   16465:                case 0x00: pcbios_int_1ah_00h(); break;
                   16466:                case 0x01: break;
                   16467:                case 0x02: pcbios_int_1ah_02h(); break;
                   16468:                case 0x03: break;
                   16469:                case 0x04: pcbios_int_1ah_04h(); break;
                   16470:                case 0x05: break;
                   16471:                case 0x0a: pcbios_int_1ah_0ah(); break;
                   16472:                case 0x0b: break;
1.1.1.14  root     16473:                case 0x35: break; // Word Perfect Third Party Interface?
                   16474:                case 0x36: break; // Word Perfect Third Party Interface
                   16475:                case 0x70: break; // SNAP? (Simple Network Application Protocol)
1.1.1.44  root     16476:                case 0xb0: break; // Microsoft Real-Time Compression Interface (MRCI)
1.1.1.43  root     16477:                case 0xb1: break; // PCI BIOS v2.0c+
                   16478:                case 0xb4: break; // Intel Plug-and-Play Auto-Configuration
1.1       root     16479:                default:
1.1.1.22  root     16480:                        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     16481:                        break;
                   16482:                }
                   16483:                break;
1.1.1.33  root     16484:        case 0x1b:
                   16485:                mem[0x471] = 0x00;
                   16486:                break;
1.1       root     16487:        case 0x20:
1.1.1.28  root     16488:                try {
                   16489:                        msdos_process_terminate(SREG(CS), retval, 1);
                   16490:                } catch(...) {
                   16491:                        fatalerror("failed to terminate the process (PSP=%04X) by int 20h\n", SREG(CS));
                   16492:                }
1.1       root     16493:                break;
1.1.1.49  root     16494:        case 0x30:
1.1.1.46  root     16495:                // dummy interrupt for case map routine pointed in the country info
                   16496: //             if(!(REG8(CL) >= 0x00 && REG8(CL) <= 0x24)) {
                   16497: //                     REG8(AL) = 0x00;
                   16498: //                     break;
                   16499: //             }
1.1       root     16500:        case 0x21:
                   16501:                // MS-DOS System Call
1.1.1.3   root     16502:                m_CF = 0;
1.1.1.28  root     16503:                try {
1.1.1.46  root     16504:                        switch(num == 0x21 ? REG8(AH) : REG8(CL)) {
1.1.1.28  root     16505:                        case 0x00: msdos_int_21h_00h(); break;
                   16506:                        case 0x01: msdos_int_21h_01h(); break;
                   16507:                        case 0x02: msdos_int_21h_02h(); break;
                   16508:                        case 0x03: msdos_int_21h_03h(); break;
                   16509:                        case 0x04: msdos_int_21h_04h(); break;
                   16510:                        case 0x05: msdos_int_21h_05h(); break;
                   16511:                        case 0x06: msdos_int_21h_06h(); break;
                   16512:                        case 0x07: msdos_int_21h_07h(); break;
                   16513:                        case 0x08: msdos_int_21h_08h(); break;
                   16514:                        case 0x09: msdos_int_21h_09h(); break;
                   16515:                        case 0x0a: msdos_int_21h_0ah(); break;
                   16516:                        case 0x0b: msdos_int_21h_0bh(); break;
                   16517:                        case 0x0c: msdos_int_21h_0ch(); break;
                   16518:                        case 0x0d: msdos_int_21h_0dh(); break;
                   16519:                        case 0x0e: msdos_int_21h_0eh(); break;
                   16520:                        case 0x0f: msdos_int_21h_0fh(); break;
                   16521:                        case 0x10: msdos_int_21h_10h(); break;
                   16522:                        case 0x11: msdos_int_21h_11h(); break;
                   16523:                        case 0x12: msdos_int_21h_12h(); break;
                   16524:                        case 0x13: msdos_int_21h_13h(); break;
                   16525:                        case 0x14: msdos_int_21h_14h(); break;
                   16526:                        case 0x15: msdos_int_21h_15h(); break;
                   16527:                        case 0x16: msdos_int_21h_16h(); break;
                   16528:                        case 0x17: msdos_int_21h_17h(); break;
                   16529:                        case 0x18: msdos_int_21h_18h(); break;
                   16530:                        case 0x19: msdos_int_21h_19h(); break;
                   16531:                        case 0x1a: msdos_int_21h_1ah(); break;
                   16532:                        case 0x1b: msdos_int_21h_1bh(); break;
                   16533:                        case 0x1c: msdos_int_21h_1ch(); break;
                   16534:                        case 0x1d: msdos_int_21h_1dh(); break;
                   16535:                        case 0x1e: msdos_int_21h_1eh(); break;
                   16536:                        case 0x1f: msdos_int_21h_1fh(); break;
                   16537:                        case 0x20: msdos_int_21h_20h(); break;
                   16538:                        case 0x21: msdos_int_21h_21h(); break;
                   16539:                        case 0x22: msdos_int_21h_22h(); break;
                   16540:                        case 0x23: msdos_int_21h_23h(); break;
                   16541:                        case 0x24: msdos_int_21h_24h(); break;
                   16542:                        case 0x25: msdos_int_21h_25h(); break;
                   16543:                        case 0x26: msdos_int_21h_26h(); break;
                   16544:                        case 0x27: msdos_int_21h_27h(); break;
                   16545:                        case 0x28: msdos_int_21h_28h(); break;
                   16546:                        case 0x29: msdos_int_21h_29h(); break;
                   16547:                        case 0x2a: msdos_int_21h_2ah(); break;
                   16548:                        case 0x2b: msdos_int_21h_2bh(); break;
                   16549:                        case 0x2c: msdos_int_21h_2ch(); break;
                   16550:                        case 0x2d: msdos_int_21h_2dh(); break;
                   16551:                        case 0x2e: msdos_int_21h_2eh(); break;
                   16552:                        case 0x2f: msdos_int_21h_2fh(); break;
                   16553:                        case 0x30: msdos_int_21h_30h(); break;
                   16554:                        case 0x31: msdos_int_21h_31h(); break;
                   16555:                        case 0x32: msdos_int_21h_32h(); break;
                   16556:                        case 0x33: msdos_int_21h_33h(); break;
                   16557:                        case 0x34: msdos_int_21h_34h(); break;
                   16558:                        case 0x35: msdos_int_21h_35h(); break;
                   16559:                        case 0x36: msdos_int_21h_36h(); break;
                   16560:                        case 0x37: msdos_int_21h_37h(); break;
                   16561:                        case 0x38: msdos_int_21h_38h(); break;
                   16562:                        case 0x39: msdos_int_21h_39h(0); break;
                   16563:                        case 0x3a: msdos_int_21h_3ah(0); break;
                   16564:                        case 0x3b: msdos_int_21h_3bh(0); break;
                   16565:                        case 0x3c: msdos_int_21h_3ch(); break;
                   16566:                        case 0x3d: msdos_int_21h_3dh(); break;
                   16567:                        case 0x3e: msdos_int_21h_3eh(); break;
                   16568:                        case 0x3f: msdos_int_21h_3fh(); break;
                   16569:                        case 0x40: msdos_int_21h_40h(); break;
                   16570:                        case 0x41: msdos_int_21h_41h(0); break;
                   16571:                        case 0x42: msdos_int_21h_42h(); break;
                   16572:                        case 0x43: msdos_int_21h_43h(0); break;
                   16573:                        case 0x44: msdos_int_21h_44h(); break;
                   16574:                        case 0x45: msdos_int_21h_45h(); break;
                   16575:                        case 0x46: msdos_int_21h_46h(); break;
                   16576:                        case 0x47: msdos_int_21h_47h(0); break;
                   16577:                        case 0x48: msdos_int_21h_48h(); break;
                   16578:                        case 0x49: msdos_int_21h_49h(); break;
                   16579:                        case 0x4a: msdos_int_21h_4ah(); break;
                   16580:                        case 0x4b: msdos_int_21h_4bh(); break;
                   16581:                        case 0x4c: msdos_int_21h_4ch(); break;
                   16582:                        case 0x4d: msdos_int_21h_4dh(); break;
                   16583:                        case 0x4e: msdos_int_21h_4eh(); break;
                   16584:                        case 0x4f: msdos_int_21h_4fh(); break;
                   16585:                        case 0x50: msdos_int_21h_50h(); break;
                   16586:                        case 0x51: msdos_int_21h_51h(); break;
                   16587:                        case 0x52: msdos_int_21h_52h(); break;
1.1.1.43  root     16588:                        case 0x53: msdos_int_21h_53h(); break;
1.1.1.28  root     16589:                        case 0x54: msdos_int_21h_54h(); break;
                   16590:                        case 0x55: msdos_int_21h_55h(); break;
                   16591:                        case 0x56: msdos_int_21h_56h(0); break;
                   16592:                        case 0x57: msdos_int_21h_57h(); break;
                   16593:                        case 0x58: msdos_int_21h_58h(); break;
                   16594:                        case 0x59: msdos_int_21h_59h(); break;
                   16595:                        case 0x5a: msdos_int_21h_5ah(); break;
                   16596:                        case 0x5b: msdos_int_21h_5bh(); break;
                   16597:                        case 0x5c: msdos_int_21h_5ch(); break;
                   16598:                        case 0x5d: msdos_int_21h_5dh(); break;
1.1.1.42  root     16599:                        case 0x5e: msdos_int_21h_5eh(); break;
1.1.1.30  root     16600:                        case 0x5f: msdos_int_21h_5fh(); break;
1.1.1.28  root     16601:                        case 0x60: msdos_int_21h_60h(0); break;
                   16602:                        case 0x61: msdos_int_21h_61h(); break;
                   16603:                        case 0x62: msdos_int_21h_62h(); break;
                   16604:                        case 0x63: msdos_int_21h_63h(); break;
1.1.1.33  root     16605:                        // 0x64: Set Device Driver Lockahead Flag
1.1.1.28  root     16606:                        case 0x65: msdos_int_21h_65h(); break;
                   16607:                        case 0x66: msdos_int_21h_66h(); break;
                   16608:                        case 0x67: msdos_int_21h_67h(); break;
                   16609:                        case 0x68: msdos_int_21h_68h(); break;
                   16610:                        case 0x69: msdos_int_21h_69h(); break;
                   16611:                        case 0x6a: msdos_int_21h_6ah(); break;
                   16612:                        case 0x6b: msdos_int_21h_6bh(); break;
                   16613:                        case 0x6c: msdos_int_21h_6ch(0); break;
1.1.1.48  root     16614:                        case 0x6d: // Find First ROM Program
                   16615:                        case 0x6e: // Find Next ROM Program
                   16616:                        case 0x6f: // Get/Set ROM Scan Start Address
                   16617:                                REG8(AL) = 0x00; // if not supported (DOS <5, MS-DOS 5+ non-ROM versions)
                   16618:                                break;
1.1.1.43  root     16619:                        case 0x70: msdos_int_21h_70h(); break;
1.1.1.48  root     16620:                        case 0x71: // Windows95 - Long Filename Functions
1.1.1.28  root     16621:                                switch(REG8(AL)) {
                   16622:                                case 0x0d: msdos_int_21h_710dh(); break;
                   16623:                                case 0x39: msdos_int_21h_39h(1); break;
                   16624:                                case 0x3a: msdos_int_21h_3ah(1); break;
                   16625:                                case 0x3b: msdos_int_21h_3bh(1); break;
1.1.1.48  root     16626:                                case 0x41: msdos_int_21h_7141h(); break;
1.1.1.28  root     16627:                                case 0x43: msdos_int_21h_43h(1); break;
                   16628:                                case 0x47: msdos_int_21h_47h(1); break;
                   16629:                                case 0x4e: msdos_int_21h_714eh(); break;
                   16630:                                case 0x4f: msdos_int_21h_714fh(); break;
                   16631:                                case 0x56: msdos_int_21h_56h(1); break;
                   16632:                                case 0x60: msdos_int_21h_60h(1); break;
                   16633:                                case 0x6c: msdos_int_21h_6ch(1); break;
                   16634:                                case 0xa0: msdos_int_21h_71a0h(); break;
                   16635:                                case 0xa1: msdos_int_21h_71a1h(); break;
                   16636:                                case 0xa6: msdos_int_21h_71a6h(); break;
                   16637:                                case 0xa7: msdos_int_21h_71a7h(); break;
                   16638:                                case 0xa8: msdos_int_21h_71a8h(); break;
1.1.1.45  root     16639:                                case 0xa9: msdos_int_21h_6ch(1); break;
1.1.1.28  root     16640:                                case 0xaa: msdos_int_21h_71aah(); break;
                   16641:                                default:
                   16642:                                        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));
                   16643:                                        REG16(AX) = 0x7100;
                   16644:                                        m_CF = 1;
                   16645:                                        break;
                   16646:                                }
                   16647:                                break;
1.1.1.48  root     16648:                        case 0x72: // Windows95 beta - LFN FindClose
                   16649: //                             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));
                   16650:                                REG16(AX) = 0x7200;
                   16651:                                m_CF = 1;
                   16652:                                break;
                   16653:                        case 0x73: // Windows95 - FAT32 Functions
1.1.1.28  root     16654:                                switch(REG8(AL)) {
                   16655:                                case 0x00: msdos_int_21h_7300h(); break;
1.1.1.33  root     16656:                                // 0x01: Set Drive Locking ???
1.1.1.28  root     16657:                                case 0x02: msdos_int_21h_7302h(); break;
                   16658:                                case 0x03: msdos_int_21h_7303h(); break;
1.1.1.33  root     16659:                                // 0x04: Set DPB to Use for Formatting
                   16660:                                // 0x05: Extended Absolute Disk Read/Write
1.1.1.28  root     16661:                                default:
                   16662:                                        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));
                   16663:                                        REG16(AX) = 0x7300;
                   16664:                                        m_CF = 1;
                   16665:                                        break;
                   16666:                                }
1.1       root     16667:                                break;
1.1.1.30  root     16668:                        case 0xdb: msdos_int_21h_dbh(); break;
                   16669:                        case 0xdc: msdos_int_21h_dch(); break;
1.1       root     16670:                        default:
1.1.1.22  root     16671:                                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     16672:                                REG16(AX) = 0x01;
1.1.1.3   root     16673:                                m_CF = 1;
1.1       root     16674:                                break;
                   16675:                        }
1.1.1.28  root     16676:                } catch(int error) {
                   16677:                        REG16(AX) = error;
                   16678:                        m_CF = 1;
                   16679:                } catch(...) {
                   16680:                        REG16(AX) = 0x1f; // general failure
1.1.1.3   root     16681:                        m_CF = 1;
1.1       root     16682:                }
1.1.1.3   root     16683:                if(m_CF) {
1.1.1.23  root     16684:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
1.1.1.47  root     16685:                        sda->int21h_5d0ah_called = 0;
1.1.1.23  root     16686:                        sda->extended_error_code = REG16(AX);
                   16687:                        switch(sda->extended_error_code) {
                   16688:                        case  4: // Too many open files
                   16689:                        case  8: // Insufficient memory
                   16690:                                sda->error_class = 1; // Out of resource
                   16691:                                break;
                   16692:                        case  5: // Access denied
                   16693:                                sda->error_class = 3; // Authorization
                   16694:                                break;
                   16695:                        case  7: // Memory control block destroyed
                   16696:                                sda->error_class = 4; // Internal
                   16697:                                break;
                   16698:                        case  2: // File not found
                   16699:                        case  3: // Path not found
                   16700:                        case 15: // Invaid drive specified
                   16701:                        case 18: // No more files
                   16702:                                sda->error_class = 8; // Not found
                   16703:                                break;
                   16704:                        case 32: // Sharing violation
                   16705:                        case 33: // Lock violation
                   16706:                                sda->error_class = 10; // Locked
                   16707:                                break;
                   16708: //                     case 16: // Removal of current directory attempted
                   16709:                        case 19: // Attempted write on protected disk
                   16710:                        case 21: // Drive not ready
                   16711: //                     case 29: // Write failure
                   16712: //                     case 30: // Read failure
                   16713: //                     case 82: // Cannot create subdirectory
                   16714:                                sda->error_class = 11; // Media
                   16715:                                break;
                   16716:                        case 80: // File already exists
                   16717:                                sda->error_class = 12; // Already exist
                   16718:                                break;
                   16719:                        default:
                   16720:                                sda->error_class = 13; // Unknown
                   16721:                                break;
                   16722:                        }
                   16723:                        sda->suggested_action = 1; // Retry
                   16724:                        sda->locus_of_last_error = 1; // Unknown
1.1       root     16725:                }
1.1.1.33  root     16726:                if(ctrl_break_checking && ctrl_break_detected) {
1.1.1.26  root     16727:                        // raise int 23h
                   16728: #if defined(HAS_I386)
                   16729:                        m_ext = 0; // not an external interrupt
                   16730:                        i386_trap(0x23, 1, 0);
                   16731:                        m_ext = 1;
                   16732: #else
                   16733:                        PREFIX86(_interrupt)(0x23);
                   16734: #endif
                   16735:                }
1.1       root     16736:                break;
                   16737:        case 0x22:
                   16738:                fatalerror("int 22h (terminate address)\n");
                   16739:        case 0x23:
1.1.1.28  root     16740:                try {
                   16741:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x100, 1);
                   16742:                } catch(...) {
                   16743:                        fatalerror("failed to terminate the current process by int 23h\n");
                   16744:                }
1.1       root     16745:                break;
                   16746:        case 0x24:
1.1.1.32  root     16747: /*
1.1.1.28  root     16748:                try {
                   16749:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   16750:                } catch(...) {
                   16751:                        fatalerror("failed to terminate the current process by int 24h\n");
                   16752:                }
1.1.1.32  root     16753: */
                   16754:                msdos_int_24h();
1.1       root     16755:                break;
                   16756:        case 0x25:
                   16757:                msdos_int_25h();
                   16758:                break;
                   16759:        case 0x26:
                   16760:                msdos_int_26h();
                   16761:                break;
                   16762:        case 0x27:
1.1.1.28  root     16763:                try {
                   16764:                        msdos_int_27h();
                   16765:                } catch(...) {
                   16766:                        fatalerror("failed to terminate the process (PSP=%04X) by int 27h\n", SREG(CS));
                   16767:                }
1.1       root     16768:                break;
                   16769:        case 0x28:
                   16770:                Sleep(10);
1.1.1.35  root     16771:                REQUEST_HARDWRE_UPDATE();
1.1       root     16772:                break;
                   16773:        case 0x29:
                   16774:                msdos_int_29h();
                   16775:                break;
                   16776:        case 0x2e:
                   16777:                msdos_int_2eh();
                   16778:                break;
                   16779:        case 0x2f:
                   16780:                // multiplex interrupt
                   16781:                switch(REG8(AH)) {
1.1.1.22  root     16782:                case 0x05: msdos_int_2fh_05h(); break;
1.1.1.44  root     16783:                case 0x06: msdos_int_2fh_06h(); break;
1.1.1.22  root     16784:                case 0x11: msdos_int_2fh_11h(); break;
1.1.1.21  root     16785:                case 0x12: msdos_int_2fh_12h(); break;
1.1.1.30  root     16786:                case 0x13: msdos_int_2fh_13h(); break;
1.1.1.22  root     16787:                case 0x14: msdos_int_2fh_14h(); break;
                   16788:                case 0x15: msdos_int_2fh_15h(); break;
1.1       root     16789:                case 0x16: msdos_int_2fh_16h(); break;
1.1.1.22  root     16790:                case 0x19: msdos_int_2fh_19h(); break;
1.1       root     16791:                case 0x1a: msdos_int_2fh_1ah(); break;
1.1.1.30  root     16792:                case 0x40: msdos_int_2fh_40h(); break;
1.1       root     16793:                case 0x43: msdos_int_2fh_43h(); break;
1.1.1.22  root     16794:                case 0x46: msdos_int_2fh_46h(); break;
                   16795:                case 0x48: msdos_int_2fh_48h(); break;
1.1       root     16796:                case 0x4a: msdos_int_2fh_4ah(); break;
1.1.1.22  root     16797:                case 0x4b: msdos_int_2fh_4bh(); break;
1.1.1.44  root     16798:                case 0x4d: msdos_int_2fh_4dh(); break;
1.1       root     16799:                case 0x4f: msdos_int_2fh_4fh(); break;
1.1.1.22  root     16800:                case 0x55: msdos_int_2fh_55h(); break;
1.1.1.44  root     16801:                case 0x56: msdos_int_2fh_56h(); break;
1.1.1.24  root     16802:                case 0xad: msdos_int_2fh_adh(); break;
1.1       root     16803:                case 0xae: msdos_int_2fh_aeh(); break;
1.1.1.34  root     16804:                case 0xb7: msdos_int_2fh_b7h(); break;
1.1.1.43  root     16805:                default:
1.1.1.30  root     16806:                        switch(REG8(AL)) {
                   16807:                        case 0x00:
                   16808:                                // This is not installed
                   16809: //                             REG8(AL) = 0x00;
                   16810:                                break;
1.1.1.33  root     16811:                        case 0x01:
1.1.1.42  root     16812:                                // Quarterdeck RPCI - QEMM/QRAM - PCL-838.EXE is not installed
                   16813:                                if(REG8(AH) == 0xd2 && REG16(BX) == 0x5145 && REG16(CX) == 0x4d4d && REG16(DX) == 0x3432) {
                   16814:                                        break;
                   16815:                                }
1.1.1.33  root     16816:                                // Banyan VINES v4+ is not installed
                   16817:                                if(REG8(AH) == 0xd7 && REG16(BX) == 0x0000) {
                   16818:                                        break;
                   16819:                                }
1.1.1.42  root     16820:                                // Quarterdeck QDPMI.SYS v1.0 is not installed
                   16821:                                if(REG8(AH) == 0xde && REG16(BX) == 0x4450 && REG16(CX) == 0x4d49 && REG16(DX) == 0x8f4f) {
                   16822:                                        break;
                   16823:                                }
1.1.1.30  root     16824:                        default:
1.1.1.42  root     16825:                                // NORTON UTILITIES 5.0+
                   16826:                                if(REG8(AH) == 0xfe && REG16(DI) == 0x4e55) {
                   16827:                                        break;
                   16828:                                }
1.1.1.30  root     16829:                                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     16830:                                REG16(AX) = 0x01; // invalid function
1.1.1.30  root     16831:                                m_CF = 1;
                   16832:                                break;
                   16833:                        }
                   16834:                        break;
1.1       root     16835:                }
                   16836:                break;
1.1.1.24  root     16837:        case 0x33:
                   16838:                switch(REG8(AH)) {
                   16839:                case 0x00:
                   16840:                        // Mouse
1.1.1.49  root     16841:                        switch(REG16(AX)) {
                   16842:                        case 0x0000: msdos_int_33h_0000h(); break;
                   16843:                        case 0x0001: msdos_int_33h_0001h(); break;
                   16844:                        case 0x0002: msdos_int_33h_0002h(); break;
                   16845:                        case 0x0003: msdos_int_33h_0003h(); break;
                   16846:                        case 0x0004: msdos_int_33h_0004h(); break;
                   16847:                        case 0x0005: msdos_int_33h_0005h(); break;
                   16848:                        case 0x0006: msdos_int_33h_0006h(); break;
                   16849:                        case 0x0007: msdos_int_33h_0007h(); break;
                   16850:                        case 0x0008: msdos_int_33h_0008h(); break;
                   16851:                        case 0x0009: msdos_int_33h_0009h(); break;
                   16852:                        case 0x000a: msdos_int_33h_000ah(); break;
                   16853:                        case 0x000b: msdos_int_33h_000bh(); break;
                   16854:                        case 0x000c: msdos_int_33h_000ch(); break;
                   16855:                        case 0x000d: break; // MS MOUSE v1.0+ - Light Pen Emulation On
                   16856:                        case 0x000e: break; // MS MOUSE v1.0+ - Light Pen Emulation Off
                   16857:                        case 0x000f: msdos_int_33h_000fh(); break;
                   16858:                        case 0x0010: break; // MS MOUSE v1.0+ - Define Screen Region for Updating
                   16859:                        case 0x0011: msdos_int_33h_0011h(); break;
                   16860:                        case 0x0012: REG16(AX) = 0xffff; break; // MS MOUSE - Set Large Graphics Cursor Block
                   16861:                        case 0x0013: break; // MS MOUSE v5.0+ - Define Double-Speed Threshold
                   16862:                        case 0x0014: msdos_int_33h_0014h(); break;
                   16863:                        case 0x0015: msdos_int_33h_0015h(); break;
                   16864:                        case 0x0016: msdos_int_33h_0016h(); break;
                   16865:                        case 0x0017: msdos_int_33h_0017h(); break;
                   16866:                        case 0x0018: msdos_int_33h_0018h(); break;
                   16867:                        case 0x0019: msdos_int_33h_0019h(); break;
                   16868:                        case 0x001a: msdos_int_33h_001ah(); break;
                   16869:                        case 0x001b: msdos_int_33h_001bh(); break;
                   16870:                        case 0x001c: break; // MS MOUSE v6.0+ - Set Interrupt Rate
                   16871:                        case 0x001d: msdos_int_33h_001dh(); break;
                   16872:                        case 0x001e: msdos_int_33h_001eh(); break;
                   16873:                        case 0x001f: msdos_int_33h_001fh(); break;
                   16874:                        case 0x0020: msdos_int_33h_0020h(); break;
                   16875:                        case 0x0021: msdos_int_33h_0021h(); break;
                   16876:                        case 0x0022: msdos_int_33h_0022h(); break;
                   16877:                        case 0x0023: msdos_int_33h_0023h(); break;
                   16878:                        case 0x0024: msdos_int_33h_0024h(); break;
                   16879:                        case 0x0025: msdos_int_33h_0025h(); break;
                   16880:                        case 0x0026: msdos_int_33h_0026h(); break;
                   16881:                        case 0x0027: msdos_int_33h_0027h(); break;
                   16882:                        case 0x0028: msdos_int_33h_0028h(); break;
                   16883:                        case 0x0029: msdos_int_33h_0029h(); break;
                   16884:                        case 0x002a: msdos_int_33h_002ah(); break;
                   16885:                        // 0x002b: MS MOUSE v7.0+ - Load Acceleration Profiles
                   16886:                        // 0x002c: MS MOUSE v7.0+ - Get Acceleration Profiles
                   16887:                        // 0x002d: MS MOUSE v7.0+ - Select Acceleration Profile
                   16888:                        // 0x002e: MS MOUSE v8.10+ - Set Acceleration Profile Names
                   16889:                        case 0x002f: break; // Mouse Hardware Reset
                   16890:                        // 0x0030: MS MOUSE v7.04+ - Get/Set BallPoint Information
                   16891:                        case 0x0031: msdos_int_33h_0031h(); break;
                   16892:                        case 0x0032: msdos_int_33h_0032h(); break;
                   16893:                        // 0x0033: MS MOUSE v7.05+ - Get Switch Settings And Acceleration Profile Data
                   16894:                        // 0x0034: MS MOUSE v8.0+ - Get Initialization File
                   16895:                        // 0x0035: MS MOUSE v8.10+ - LCD Screen Large Pointer Support
                   16896:                        case 0x004d: msdos_int_33h_004dh(); break;
                   16897:                        case 0x006d: msdos_int_33h_006dh(); break;
1.1.1.24  root     16898:                        default:
                   16899:                                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));
                   16900:                                break;
                   16901:                        }
                   16902:                        break;
                   16903:                default:
                   16904:                        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));
                   16905:                        break;
                   16906:                }
                   16907:                break;
1.1.1.50  root     16908: /*
                   16909:        case 0x67:
                   16910:                // int 67h handler is in EMS device driver (EMMXXXX0) and it calls int 68h
                   16911:                // NOTE: some softwares get address of int 67h handler and recognize the address is in EMS device driver
                   16912:                break;
                   16913: */
1.1.1.19  root     16914:        case 0x68:
                   16915:                // dummy interrupt for EMS (int 67h)
                   16916:                switch(REG8(AH)) {
                   16917:                case 0x40: msdos_int_67h_40h(); break;
                   16918:                case 0x41: msdos_int_67h_41h(); break;
                   16919:                case 0x42: msdos_int_67h_42h(); break;
                   16920:                case 0x43: msdos_int_67h_43h(); break;
                   16921:                case 0x44: msdos_int_67h_44h(); break;
                   16922:                case 0x45: msdos_int_67h_45h(); break;
                   16923:                case 0x46: msdos_int_67h_46h(); break;
                   16924:                case 0x47: msdos_int_67h_47h(); break;
                   16925:                case 0x48: msdos_int_67h_48h(); break;
1.1.1.20  root     16926:                // 0x49: LIM EMS - Reserved - Get I/O Port Address (Undocumented in EMS 3.2)
                   16927:                // 0x4a: LIM EMS - Reserved - Get Translation Array (Undocumented in EMS 3.2)
1.1.1.19  root     16928:                case 0x4b: msdos_int_67h_4bh(); break;
                   16929:                case 0x4c: msdos_int_67h_4ch(); break;
                   16930:                case 0x4d: msdos_int_67h_4dh(); break;
1.1.1.20  root     16931:                case 0x4e: msdos_int_67h_4eh(); break;
1.1.1.21  root     16932:                case 0x4f: msdos_int_67h_4fh(); break;
1.1.1.20  root     16933:                case 0x50: msdos_int_67h_50h(); break;
1.1.1.19  root     16934:                case 0x51: msdos_int_67h_51h(); break;
1.1.1.20  root     16935:                case 0x52: msdos_int_67h_52h(); break;
1.1.1.19  root     16936:                case 0x53: msdos_int_67h_53h(); break;
                   16937:                case 0x54: msdos_int_67h_54h(); break;
1.1.1.49  root     16938:                case 0x55: msdos_int_67h_55h(); break;
                   16939:                case 0x56: msdos_int_67h_56h(); break;
1.1.1.20  root     16940:                case 0x57: msdos_int_67h_57h(); break;
                   16941:                case 0x58: msdos_int_67h_58h(); break;
1.1.1.42  root     16942:                case 0x59: msdos_int_67h_59h(); break;
1.1.1.20  root     16943:                case 0x5a: msdos_int_67h_5ah(); break;
1.1.1.49  root     16944:                case 0x5b: msdos_int_67h_5bh(); break;
1.1.1.43  root     16945:                // 0x5c: LIM EMS 4.0 - Prepare Expanded Memory Hardware For Warm Boot
                   16946:                case 0x5d: msdos_int_67h_5dh(); break;
1.1.1.49  root     16947:                case 0x70: msdos_int_67h_70h(); break;
1.1.1.31  root     16948:                // 0xde: VCPI
1.1.1.30  root     16949:                case 0xde: msdos_int_67h_deh(); break;
1.1.1.19  root     16950:                default:
1.1.1.22  root     16951:                        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     16952:                        REG8(AH) = 0x84;
                   16953:                        break;
                   16954:                }
                   16955:                break;
                   16956: #ifdef SUPPORT_XMS
                   16957:        case 0x69:
                   16958:                // dummy interrupt for XMS (call far)
1.1.1.28  root     16959:                try {
                   16960:                        switch(REG8(AH)) {
                   16961:                        case 0x00: msdos_call_xms_00h(); break;
                   16962:                        case 0x01: msdos_call_xms_01h(); break;
                   16963:                        case 0x02: msdos_call_xms_02h(); break;
                   16964:                        case 0x03: msdos_call_xms_03h(); break;
                   16965:                        case 0x04: msdos_call_xms_04h(); break;
                   16966:                        case 0x05: msdos_call_xms_05h(); break;
                   16967:                        case 0x06: msdos_call_xms_06h(); break;
                   16968:                        case 0x07: msdos_call_xms_07h(); break;
                   16969:                        case 0x08: msdos_call_xms_08h(); break;
                   16970:                        case 0x09: msdos_call_xms_09h(); break;
                   16971:                        case 0x0a: msdos_call_xms_0ah(); break;
                   16972:                        case 0x0b: msdos_call_xms_0bh(); break;
                   16973:                        case 0x0c: msdos_call_xms_0ch(); break;
                   16974:                        case 0x0d: msdos_call_xms_0dh(); break;
                   16975:                        case 0x0e: msdos_call_xms_0eh(); break;
                   16976:                        case 0x0f: msdos_call_xms_0fh(); break;
                   16977:                        case 0x10: msdos_call_xms_10h(); break;
                   16978:                        case 0x11: msdos_call_xms_11h(); break;
                   16979:                        case 0x12: msdos_call_xms_12h(); break;
1.1.1.29  root     16980: #if defined(HAS_I386)
                   16981:                        case 0x88: msdos_call_xms_88h(); break;
                   16982:                        case 0x89: msdos_call_xms_89h(); break;
                   16983:                        case 0x8e: msdos_call_xms_8eh(); break;
                   16984:                        case 0x8f: msdos_call_xms_8fh(); break;
                   16985: #endif
1.1.1.28  root     16986:                        default:
                   16987:                                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));
                   16988:                                REG16(AX) = 0x0000;
                   16989:                                REG8(BL) = 0x80; // function not implemented
                   16990:                                break;
                   16991:                        }
                   16992:                } catch(...) {
1.1.1.19  root     16993:                        REG16(AX) = 0x0000;
1.1.1.28  root     16994:                        REG8(BL) = 0x8f; // unrecoverable driver error
1.1.1.19  root     16995:                }
                   16996:                break;
                   16997: #endif
                   16998:        case 0x6a:
1.1.1.24  root     16999:                // irq12 (mouse)
                   17000:                mouse_push_ax = REG16(AX);
                   17001:                mouse_push_bx = REG16(BX);
                   17002:                mouse_push_cx = REG16(CX);
                   17003:                mouse_push_dx = REG16(DX);
                   17004:                mouse_push_si = REG16(SI);
                   17005:                mouse_push_di = REG16(DI);
                   17006:                
1.1.1.43  root     17007:                if(mouse.status_irq && mouse.call_addr.dw) {
1.1.1.24  root     17008:                        REG16(AX) = mouse.status_irq;
                   17009:                        REG16(BX) = mouse.get_buttons();
1.1.1.34  root     17010:                        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   17011:                        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
1.1.1.24  root     17012:                        REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
                   17013:                        REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
                   17014:                        
1.1.1.49  root     17015:                        mem[DUMMY_TOP + 0x02] = 0x9a;   // call far
                   17016:                        mem[DUMMY_TOP + 0x03] = mouse.call_addr.w.l & 0xff;
                   17017:                        mem[DUMMY_TOP + 0x04] = mouse.call_addr.w.l >> 8;
                   17018:                        mem[DUMMY_TOP + 0x05] = mouse.call_addr.w.h & 0xff;
                   17019:                        mem[DUMMY_TOP + 0x06] = mouse.call_addr.w.h >> 8;
1.1.1.43  root     17020:                        break;
1.1.1.24  root     17021:                }
1.1.1.43  root     17022:                for(int i = 0; i < 8; i++) {
                   17023:                        if((mouse.status_irq_alt & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   17024:                                REG16(AX) = mouse.status_irq_alt;
                   17025:                                REG16(BX) = mouse.get_buttons();
                   17026:                                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   17027:                                REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
                   17028:                                REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
                   17029:                                REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
                   17030:                                
1.1.1.49  root     17031:                                mem[DUMMY_TOP + 0x02] = 0x9a;   // call far
                   17032:                                mem[DUMMY_TOP + 0x03] = mouse.call_addr_alt[i].w.l & 0xff;
                   17033:                                mem[DUMMY_TOP + 0x04] = mouse.call_addr_alt[i].w.l >> 8;
                   17034:                                mem[DUMMY_TOP + 0x05] = mouse.call_addr_alt[i].w.h & 0xff;
                   17035:                                mem[DUMMY_TOP + 0x06] = mouse.call_addr_alt[i].w.h >> 8;
1.1.1.43  root     17036:                                break;
                   17037:                        }
                   17038:                }
                   17039:                // invalid call addr :-(
1.1.1.49  root     17040:                mem[DUMMY_TOP + 0x02] = 0x90;   // nop
                   17041:                mem[DUMMY_TOP + 0x03] = 0x90;   // nop
                   17042:                mem[DUMMY_TOP + 0x04] = 0x90;   // nop
                   17043:                mem[DUMMY_TOP + 0x05] = 0x90;   // nop
                   17044:                mem[DUMMY_TOP + 0x06] = 0x90;   // nop
1.1.1.24  root     17045:                break;
                   17046:        case 0x6b:
                   17047:                // end of irq12 (mouse)
                   17048:                REG16(AX) = mouse_push_ax;
                   17049:                REG16(BX) = mouse_push_bx;
                   17050:                REG16(CX) = mouse_push_cx;
                   17051:                REG16(DX) = mouse_push_dx;
                   17052:                REG16(SI) = mouse_push_si;
                   17053:                REG16(DI) = mouse_push_di;
                   17054:                
                   17055:                // EOI
                   17056:                if((pic[1].isr &= ~(1 << 4)) == 0) {
                   17057:                        pic[0].isr &= ~(1 << 2); // master
                   17058:                }
                   17059:                pic_update();
                   17060:                break;
                   17061:        case 0x6c:
1.1.1.19  root     17062:                // dummy interrupt for case map routine pointed in the country info
                   17063:                if(REG8(AL) >= 0x80) {
                   17064:                        char tmp[2] = {0};
                   17065:                        tmp[0] = REG8(AL);
                   17066:                        my_strupr(tmp);
                   17067:                        REG8(AL) = tmp[0];
                   17068:                }
                   17069:                break;
1.1.1.27  root     17070:        case 0x6d:
                   17071:                // dummy interrupt for font read routine pointed by int 15h, ax=5000h
                   17072:                REG8(AL) = 0x86; // not supported
                   17073:                m_CF = 1;
                   17074:                break;
1.1.1.32  root     17075:        case 0x6e:
                   17076:                // dummy interrupt for parameter error message read routine pointed by int 2fh, ax=122eh, dl=08h
                   17077:                {
                   17078:                        UINT16 code = REG16(AX);
                   17079:                        if(code & 0xf0) {
                   17080:                                code = (code & 7) | ((code & 0x10) >> 1);
                   17081:                        }
                   17082:                        for(int i = 0; i < array_length(param_error_table); i++) {
                   17083:                                if(param_error_table[i].code == code || param_error_table[i].code == (UINT16)-1) {
                   17084:                                        const char *message = NULL;
                   17085:                                        if(active_code_page == 932) {
                   17086:                                                message = param_error_table[i].message_japanese;
                   17087:                                        }
                   17088:                                        if(message == NULL) {
                   17089:                                                message = param_error_table[i].message_english;
                   17090:                                        }
                   17091:                                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   17092:                                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   17093:                                        
                   17094:                                        SREG(ES) = WORK_TOP >> 4;
                   17095:                                        i386_load_segment_descriptor(ES);
                   17096:                                        REG16(DI) = 0x0000;
                   17097:                                        break;
                   17098:                                }
                   17099:                        }
                   17100:                }
                   17101:                break;
1.1.1.49  root     17102:        case 0x6f:
                   17103:                // dummy interrupt for end of alter page map and call
                   17104:                {
                   17105:                        UINT16 handles[4], pages[4];
                   17106:                        
                   17107:                        // pop old mapping data in new mapping
                   17108:                        for(int i = 0; i < 4; i++) {
                   17109:                                pages  [3 - i] = i386_pop16();
                   17110:                                handles[3 - i] = i386_pop16();
                   17111:                        }
                   17112:                        
                   17113:                        // restore old mapping
                   17114:                        for(int i = 0; i < 4; i++) {
                   17115:                                UINT16 handle = handles[i];
                   17116:                                UINT16 page   = pages  [i];
                   17117:                                
                   17118:                                if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
                   17119:                                        ems_map_page(i, handle, page);
                   17120:                                } else {
                   17121:                                        ems_unmap_page(i);
                   17122:                                }
                   17123:                        }
                   17124:                        // do ret_far (pop cs/ip) in old mapping
                   17125:                }
                   17126:                break;
1.1.1.8   root     17127:        case 0x70:
                   17128:        case 0x71:
                   17129:        case 0x72:
                   17130:        case 0x73:
                   17131:        case 0x74:
                   17132:        case 0x75:
                   17133:        case 0x76:
                   17134:        case 0x77:
                   17135:                // EOI
                   17136:                if((pic[1].isr &= ~(1 << (num - 0x70))) == 0) {
                   17137:                        pic[0].isr &= ~(1 << 2); // master
                   17138:                }
                   17139:                pic_update();
                   17140:                break;
1.1       root     17141:        default:
1.1.1.22  root     17142: //             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     17143:                break;
                   17144:        }
                   17145:        
                   17146:        // update cursor position
                   17147:        if(cursor_moved) {
1.1.1.36  root     17148:                pcbios_update_cursor_position();
1.1       root     17149:                cursor_moved = false;
                   17150:        }
                   17151: }
                   17152: 
                   17153: // init
                   17154: 
                   17155: int msdos_init(int argc, char *argv[], char *envp[], int standard_env)
                   17156: {
                   17157:        // init file handler
                   17158:        memset(file_handler, 0, sizeof(file_handler));
                   17159:        msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   17160:        msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   17161:        msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
1.1.1.21  root     17162: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     17163:        if(_open("stdaux.txt", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
1.1.1.21  root     17164: #else
                   17165:        if(_open("NUL", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
                   17166: #endif
                   17167:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
1.1       root     17168:        }
1.1.1.21  root     17169:        if(_open("NUL", _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 4) {
1.1.1.45  root     17170: //             msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0, 0, 1); // LPT1
                   17171:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0x80a0, 0, 0, 1); // LPT1
1.1.1.21  root     17172:        }
1.1       root     17173:        _dup2(0, DUP_STDIN);
                   17174:        _dup2(1, DUP_STDOUT);
                   17175:        _dup2(2, DUP_STDERR);
1.1.1.21  root     17176:        _dup2(3, DUP_STDAUX);
                   17177:        _dup2(4, DUP_STDPRN);
1.1       root     17178:        
1.1.1.24  root     17179:        // init mouse
                   17180:        memset(&mouse, 0, sizeof(mouse));
1.1.1.34  root     17181:        mouse.enabled = true;   // from DOSBox
                   17182:        mouse.hidden = 1;       // hidden in default ???
                   17183:        mouse.old_hidden = 1;   // from DOSBox
                   17184:        mouse.max_position.x = 8 * (scr_width  - 1);
                   17185:        mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24  root     17186:        mouse.mickey.x = 8;
                   17187:        mouse.mickey.y = 16;
                   17188:        
1.1.1.26  root     17189: #ifdef SUPPORT_XMS
                   17190:        // init xms
                   17191:        msdos_xms_init();
                   17192: #endif
                   17193:        
1.1       root     17194:        // init process
                   17195:        memset(process, 0, sizeof(process));
                   17196:        
1.1.1.13  root     17197:        // init dtainfo
                   17198:        msdos_dta_info_init();
                   17199:        
1.1       root     17200:        // init memory
                   17201:        memset(mem, 0, sizeof(mem));
                   17202:        
                   17203:        // bios data area
1.1.1.23  root     17204:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     17205:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   17206:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.14  root     17207:        CONSOLE_FONT_INFO cfi;
                   17208:        GetCurrentConsoleFont(hStdout, FALSE, &cfi);
                   17209:        
                   17210:        int regen = min(scr_width * scr_height * 2, 0x8000);
                   17211:        text_vram_top_address = TEXT_VRAM_TOP;
                   17212:        text_vram_end_address = text_vram_top_address + regen;
                   17213:        shadow_buffer_top_address = SHADOW_BUF_TOP;
                   17214:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
1.1.1.51  root     17215:        cursor_position_address = 0x450 + mem[0x462] * 2;
1.1.1.14  root     17216:        
                   17217:        if(regen > 0x4000) {
                   17218:                regen = 0x8000;
                   17219:                vram_pages = 1;
                   17220:        } else if(regen > 0x2000) {
                   17221:                regen = 0x4000;
                   17222:                vram_pages = 2;
                   17223:        } else if(regen > 0x1000) {
                   17224:                regen = 0x2000;
                   17225:                vram_pages = 4;
                   17226:        } else {
                   17227:                regen = 0x1000;
                   17228:                vram_pages = 8;
                   17229:        }
1.1       root     17230:        
1.1.1.25  root     17231:        *(UINT16 *)(mem + 0x400) = 0x3f8; // com1 port address
                   17232:        *(UINT16 *)(mem + 0x402) = 0x2f8; // com2 port address
1.1.1.29  root     17233:        *(UINT16 *)(mem + 0x404) = 0x3e8; // com3 port address
                   17234:        *(UINT16 *)(mem + 0x406) = 0x2e8; // com4 port address
1.1.1.25  root     17235:        *(UINT16 *)(mem + 0x408) = 0x378; // lpt1 port address
1.1.1.37  root     17236:        *(UINT16 *)(mem + 0x40a) = 0x278; // lpt2 port address
                   17237:        *(UINT16 *)(mem + 0x40c) = 0x3bc; // lpt3 port address
1.1.1.32  root     17238: #ifdef EXT_BIOS_TOP
1.1.1.25  root     17239:        *(UINT16 *)(mem + 0x40e) = EXT_BIOS_TOP >> 4;
1.1.1.32  root     17240: #endif
1.1.1.26  root     17241:        *(UINT16 *)(mem + 0x410) = msdos_get_equipment();
1.1.1.33  root     17242:        *(UINT16 *)(mem + 0x413) = MEMORY_END >> 10;
1.1.1.41  root     17243:        *(UINT16 *)(mem + 0x41a) = 0x1e;
                   17244:        *(UINT16 *)(mem + 0x41c) = 0x1e;
1.1       root     17245:        *(UINT8  *)(mem + 0x449) = 0x03;//0x73;
1.1.1.14  root     17246:        *(UINT16 *)(mem + 0x44a) = csbi.dwSize.X;
                   17247:        *(UINT16 *)(mem + 0x44c) = regen;
1.1       root     17248:        *(UINT16 *)(mem + 0x44e) = 0;
                   17249:        *(UINT8  *)(mem + 0x450) = csbi.dwCursorPosition.X;
1.1.1.14  root     17250:        *(UINT8  *)(mem + 0x451) = csbi.dwCursorPosition.Y - scr_top;
1.1       root     17251:        *(UINT8  *)(mem + 0x460) = 7;
                   17252:        *(UINT8  *)(mem + 0x461) = 7;
                   17253:        *(UINT8  *)(mem + 0x462) = 0;
                   17254:        *(UINT16 *)(mem + 0x463) = 0x3d4;
1.1.1.19  root     17255:        *(UINT8  *)(mem + 0x465) = 0x09;
                   17256:        *(UINT32 *)(mem + 0x46c) = get_ticks_since_midnight(timeGetTime());
1.1.1.40  root     17257:        *(UINT16 *)(mem + 0x472) = 0x4321; // preserve memory in cpu reset
1.1.1.41  root     17258:        *(UINT16 *)(mem + 0x480) = 0x1e;
                   17259:        *(UINT16 *)(mem + 0x482) = 0x3e;
1.1.1.14  root     17260:        *(UINT8  *)(mem + 0x484) = csbi.srWindow.Bottom - csbi.srWindow.Top;
                   17261:        *(UINT8  *)(mem + 0x485) = cfi.dwFontSize.Y;
                   17262:        *(UINT8  *)(mem + 0x487) = 0x60;
                   17263:        *(UINT8  *)(mem + 0x496) = 0x10; // enhanced keyboard installed
1.1.1.32  root     17264: #ifdef EXT_BIOS_TOP
1.1.1.25  root     17265:        *(UINT16 *)(mem + EXT_BIOS_TOP) = 1;
1.1.1.32  root     17266: #endif
1.1.1.14  root     17267:        
                   17268:        // initial screen
                   17269:        SMALL_RECT rect;
                   17270:        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   17271:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   17272:        for(int y = 0, ofs1 = TEXT_VRAM_TOP, ofs2 = SHADOW_BUF_TOP; y < scr_height; y++) {
                   17273:                for(int x = 0; x < scr_width; x++) {
                   17274:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Char.AsciiChar;
                   17275:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Attributes;
                   17276:                }
                   17277:        }
1.1       root     17278:        
1.1.1.19  root     17279:        // init mcb
1.1       root     17280:        int seg = MEMORY_TOP >> 4;
1.1.1.19  root     17281:        
                   17282:        // iret table
                   17283:        // note: int 2eh vector should address the routine in command.com,
                   17284:        // and some softwares invite (int 2eh vector segment) - 1 must address the mcb of command.com.
                   17285:        // so move iret table into allocated memory block
                   17286:        // http://www5c.biglobe.ne.jp/~ecb/assembler2/2_6.html
1.1.1.33  root     17287:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, IRET_SIZE >> 4);
1.1.1.19  root     17288:        IRET_TOP = seg << 4;
                   17289:        seg += IRET_SIZE >> 4;
1.1.1.25  root     17290:        memset(mem + IRET_TOP, 0xcf, IRET_SIZE); // iret
1.1.1.19  root     17291:        
                   17292:        // dummy xms/ems device
1.1.1.33  root     17293:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, XMS_SIZE >> 4);
1.1.1.19  root     17294:        XMS_TOP = seg << 4;
                   17295:        seg += XMS_SIZE >> 4;
                   17296:        
                   17297:        // environment
1.1.1.33  root     17298:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, ENV_SIZE >> 4);
1.1       root     17299:        int env_seg = seg;
                   17300:        int ofs = 0;
1.1.1.32  root     17301:        char env_append[ENV_SIZE] = {0}, append_added = 0;
                   17302:        char comspec_added = 0;
1.1.1.33  root     17303:        char lastdrive_added = 0;
1.1.1.32  root     17304:        char env_msdos_path[ENV_SIZE] = {0};
                   17305:        char env_path[ENV_SIZE] = {0}, path_added = 0;
1.1.1.33  root     17306:        char prompt_added = 0;
1.1.1.32  root     17307:        char env_temp[ENV_SIZE] = {0}, temp_added = 0, tmp_added = 0;
1.1.1.33  root     17308:        char tz_added = 0;
1.1.1.45  root     17309:        const char *path, *short_path;
1.1.1.32  root     17310:        
                   17311:        if((path = getenv("MSDOS_APPEND")) != NULL) {
                   17312:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17313:                        strcpy(env_append, short_path);
                   17314:                }
                   17315:        }
                   17316:        if((path = getenv("APPEND")) != NULL) {
                   17317:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17318:                        if(env_append[0] != '\0') {
                   17319:                                strcat(env_append, ";");
                   17320:                        }
                   17321:                        strcat(env_append, short_path);
                   17322:                }
                   17323:        }
                   17324:        
                   17325:        if((path = msdos_search_command_com(argv[0], env_path)) != NULL) {
                   17326:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17327:                        strcpy(comspec_path, short_path);
                   17328:                }
                   17329:        }
                   17330:        if((path = getenv("MSDOS_COMSPEC")) != NULL && _access(path, 0) == 0) {
                   17331:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17332:                        strcpy(comspec_path, short_path);
                   17333:                }
                   17334:        }
1.1       root     17335:        
1.1.1.28  root     17336:        if((path = getenv("MSDOS_PATH")) != NULL) {
1.1.1.32  root     17337:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17338:                        strcpy(env_msdos_path, short_path);
                   17339:                        strcpy(env_path, short_path);
1.1.1.14  root     17340:                }
                   17341:        }
1.1.1.28  root     17342:        if((path = getenv("PATH")) != NULL) {
1.1.1.32  root     17343:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17344:                        if(env_path[0] != '\0') {
                   17345:                                strcat(env_path, ";");
                   17346:                        }
                   17347:                        strcat(env_path, short_path);
1.1.1.9   root     17348:                }
                   17349:        }
1.1.1.32  root     17350:        
                   17351:        if(GetTempPath(ENV_SIZE, env_temp) != 0) {
                   17352:                strcpy(env_temp, msdos_get_multiple_short_path(env_temp));
1.1.1.15  root     17353:        }
1.1.1.32  root     17354:        for(int i = 0; i < 4; i++) {
                   17355:                static const char *name[4] = {"MSDOS_TEMP", "MSDOS_TMP", "TEMP", "TMP"};
                   17356:                if((path = getenv(name[i])) != NULL && _access(path, 0) == 0) {
                   17357:                        if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   17358:                                strcpy(env_temp, short_path);
                   17359:                                break;
                   17360:                        }
                   17361:                }
1.1.1.24  root     17362:        }
1.1.1.32  root     17363:        
1.1.1.9   root     17364:        for(char **p = envp; p != NULL && *p != NULL; p++) {
1.1       root     17365:                // lower to upper
1.1.1.28  root     17366:                char tmp[ENV_SIZE], name[ENV_SIZE];
1.1       root     17367:                strcpy(tmp, *p);
                   17368:                for(int i = 0;; i++) {
                   17369:                        if(tmp[i] == '=') {
                   17370:                                tmp[i] = '\0';
                   17371:                                sprintf(name, ";%s;", tmp);
1.1.1.25  root     17372:                                my_strupr(name);
1.1       root     17373:                                tmp[i] = '=';
                   17374:                                break;
                   17375:                        } else if(tmp[i] >= 'a' && tmp[i] <= 'z') {
1.1.1.28  root     17376:                                tmp[i] = (tmp[i] - 'a') + 'A';
1.1       root     17377:                        }
                   17378:                }
1.1.1.33  root     17379:                if(strstr(";MSDOS_APPEND;MSDOS_COMSPEC;MSDOS_LASTDRIVE;MSDOS_TEMP;MSDOS_TMP;MSDOS_TZ;", name) != NULL) {
                   17380:                        // ignore MSDOS_(APPEND/COMSPEC/LASTDRIVE/TEMP/TMP/TZ)
                   17381:                } else if(standard_env && strstr(";APPEND;COMSPEC;LASTDRIVE;MSDOS_PATH;PATH;PROMPT;TEMP;TMP;TZ;", name) == NULL) {
1.1.1.18  root     17382:                        // ignore non standard environments
                   17383:                } else {
1.1.1.33  root     17384:                        if(strncmp(tmp, "APPEND=", 7) == 0) {
1.1.1.32  root     17385:                                if(env_append[0] != '\0') {
                   17386:                                        sprintf(tmp, "APPEND=%s", env_append);
                   17387:                                } else {
                   17388:                                        sprintf(tmp, "APPEND=%s", msdos_get_multiple_short_path(tmp + 7));
                   17389:                                }
                   17390:                                append_added = 1;
                   17391:                        } else if(strncmp(tmp, "COMSPEC=", 8) == 0) {
1.1.1.14  root     17392:                                strcpy(tmp, "COMSPEC=C:\\COMMAND.COM");
1.1.1.32  root     17393:                                comspec_added = 1;
1.1.1.33  root     17394:                        } else if(strncmp(tmp, "LASTDRIVE=", 10) == 0) {
                   17395:                                char *env = getenv("MSDOS_LASTDRIVE");
                   17396:                                if(env != NULL) {
                   17397:                                        sprintf(tmp, "LASTDRIVE=%s", env);
                   17398:                                }
                   17399:                                lastdrive_added = 1;
                   17400:                        } else if(strncmp(tmp, "MSDOS_PATH=", 11) == 0) {
1.1.1.28  root     17401:                                if(env_msdos_path[0] != '\0') {
                   17402:                                        sprintf(tmp, "MSDOS_PATH=%s", env_msdos_path);
                   17403:                                } else {
                   17404:                                        sprintf(tmp, "MSDOS_PATH=%s", msdos_get_multiple_short_path(tmp + 11));
                   17405:                                }
1.1.1.33  root     17406:                        } else if(strncmp(tmp, "PATH=", 5) == 0) {
1.1.1.28  root     17407:                                if(env_path[0] != '\0') {
                   17408:                                        sprintf(tmp, "PATH=%s", env_path);
                   17409:                                } else {
                   17410:                                        sprintf(tmp, "PATH=%s", msdos_get_multiple_short_path(tmp + 5));
                   17411:                                }
1.1.1.32  root     17412:                                path_added = 1;
1.1.1.33  root     17413:                        } else if(strncmp(tmp, "PROMPT=", 7) == 0) {
                   17414:                                prompt_added = 1;
1.1.1.28  root     17415:                        } else if(strncmp(tmp, "TEMP=", 5) == 0) {
                   17416:                                if(env_temp[0] != '\0') {
                   17417:                                        sprintf(tmp, "TEMP=%s", env_temp);
                   17418:                                } else {
                   17419:                                        sprintf(tmp, "TEMP=%s", msdos_get_multiple_short_path(tmp + 5));
                   17420:                                }
1.1.1.32  root     17421:                                temp_added = 1;
1.1.1.33  root     17422:                        } else if(strncmp(tmp, "TMP=", 4) == 0) {
1.1.1.28  root     17423:                                if(env_temp[0] != '\0') {
                   17424:                                        sprintf(tmp, "TMP=%s", env_temp);
                   17425:                                } else {
                   17426:                                        sprintf(tmp, "TMP=%s", msdos_get_multiple_short_path(tmp + 4));
1.1       root     17427:                                }
1.1.1.32  root     17428:                                tmp_added = 1;
1.1.1.33  root     17429:                        } else if(strncmp(tmp, "TZ=", 3) == 0) {
                   17430:                                char *env = getenv("MSDOS_TZ");
                   17431:                                if(env != NULL) {
                   17432:                                        sprintf(tmp, "TZ=%s", env);
                   17433:                                }
                   17434:                                tz_added = 1;
1.1       root     17435:                        }
                   17436:                        int len = strlen(tmp);
1.1.1.14  root     17437:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) {
1.1       root     17438:                                fatalerror("too many environments\n");
                   17439:                        }
                   17440:                        memcpy(mem + (seg << 4) + ofs, tmp, len);
                   17441:                        ofs += len + 1;
                   17442:                }
                   17443:        }
1.1.1.32  root     17444:        if(!append_added && env_append[0] != '\0') {
                   17445:                #define SET_ENV(name, value) { \
                   17446:                        char tmp[ENV_SIZE]; \
                   17447:                        sprintf(tmp, "%s=%s", name, value); \
                   17448:                        int len = strlen(tmp); \
                   17449:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) { \
                   17450:                                fatalerror("too many environments\n"); \
                   17451:                        } \
                   17452:                        memcpy(mem + (seg << 4) + ofs, tmp, len); \
                   17453:                        ofs += len + 1; \
                   17454:                }
                   17455:                SET_ENV("APPEND", env_append);
                   17456:        }
                   17457:        if(!comspec_added) {
                   17458:                SET_ENV("COMSPEC", "C:\\COMMAND.COM");
                   17459:        }
1.1.1.33  root     17460:        if(!lastdrive_added) {
                   17461:                SET_ENV("LASTDRIVE", "Z");
                   17462:        }
1.1.1.32  root     17463:        if(!path_added) {
                   17464:                SET_ENV("PATH", env_path);
                   17465:        }
1.1.1.33  root     17466:        if(!prompt_added) {
                   17467:                SET_ENV("PROMPT", "$P$G");
                   17468:        }
1.1.1.32  root     17469:        if(!temp_added) {
                   17470:                SET_ENV("TEMP", env_temp);
                   17471:        }
                   17472:        if(!tmp_added) {
                   17473:                SET_ENV("TMP", env_temp);
                   17474:        }
1.1.1.33  root     17475:        if(!tz_added) {
                   17476:                TIME_ZONE_INFORMATION tzi;
                   17477:                HKEY hKey, hSubKey;
                   17478:                char tzi_std_name[64];
                   17479:                char tz_std[8] = "GMT";
                   17480:                char tz_dlt[8] = "GST";
                   17481:                char tz_value[32];
                   17482:                
                   17483:                // timezone name from GetTimeZoneInformation may not be english
                   17484:                bool daylight = (GetTimeZoneInformation(&tzi) != TIME_ZONE_ID_UNKNOWN);
                   17485:                setlocale(LC_CTYPE, "");
                   17486:                wcstombs(tzi_std_name, tzi.StandardName, sizeof(tzi_std_name));
                   17487:                
                   17488:                // get english timezone name from registry
                   17489:                if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones", 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
                   17490:                        for(DWORD i = 0; !tz_added; i++) {
                   17491:                                char reg_name[256], sub_key[1024], std_name[256];
                   17492:                                DWORD size;
                   17493:                                FILETIME ftTime;
                   17494:                                LONG result = RegEnumKeyEx(hKey, i, reg_name, &(size = array_length(reg_name)), NULL, NULL, NULL, &ftTime);
                   17495:                                
                   17496:                                if(result == ERROR_SUCCESS) {
                   17497:                                        sprintf(sub_key, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\%s", reg_name);
                   17498:                                        if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, sub_key, 0, KEY_QUERY_VALUE, &hSubKey) == ERROR_SUCCESS) {
                   17499:                                                if(RegQueryValueEx(hSubKey, "Std", NULL, NULL, (LPBYTE)std_name, &(size = array_length(std_name))) == ERROR_SUCCESS) {
                   17500:                                                        // search english timezone name from table
1.1.1.37  root     17501:                                                        if(strcmp(std_name, tzi_std_name) == 0) {
1.1.1.33  root     17502:                                                                for(int j = 0; j < array_length(tz_table); j++) {
                   17503:                                                                        if(strcmp(reg_name, tz_table[j].name) == 0 && (tz_table[j].lcid == 0 || tz_table[j].lcid == GetUserDefaultLCID())) {
                   17504:                                                                                if(tz_table[j].std != NULL) {
                   17505:                                                                                        strcpy(tz_std, tz_table[j].std);
                   17506:                                                                                }
                   17507:                                                                                if(tz_table[j].dlt != NULL) {
                   17508:                                                                                        strcpy(tz_dlt, tz_table[j].dlt);
                   17509:                                                                                }
                   17510:                                                                                tz_added = 1;
                   17511:                                                                                break;
                   17512:                                                                        }
                   17513:                                                                }
                   17514:                                                        }
                   17515:                                                }
                   17516:                                                RegCloseKey(hSubKey);
                   17517:                                        }
                   17518:                                } else if(result == ERROR_NO_MORE_ITEMS) {
                   17519:                                        break;
                   17520:                                }
                   17521:                        }
                   17522:                        RegCloseKey(hKey);
                   17523:                }
                   17524:                if((tzi.Bias % 60) != 0) {
                   17525:                        sprintf(tz_value, "%s%d:%2d", tz_std, tzi.Bias / 60, abs(tzi.Bias % 60));
                   17526:                } else {
                   17527:                        sprintf(tz_value, "%s%d", tz_std, tzi.Bias / 60);
                   17528:                }
                   17529:                if(daylight) {
                   17530:                        strcat(tz_value, tz_dlt);
                   17531:                }
                   17532:                SET_ENV("TZ", tz_value);
                   17533:        }
1.1       root     17534:        seg += (ENV_SIZE >> 4);
                   17535:        
                   17536:        // psp
1.1.1.33  root     17537:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, PSP_SIZE >> 4);
1.1       root     17538:        current_psp = seg;
1.1.1.35  root     17539:        psp_t *psp = msdos_psp_create(seg, seg + (PSP_SIZE >> 4), -1, env_seg);
                   17540:        psp->parent_psp = current_psp;
1.1       root     17541:        seg += (PSP_SIZE >> 4);
                   17542:        
1.1.1.19  root     17543:        // first free mcb in conventional memory
1.1.1.33  root     17544:        msdos_mcb_create(seg, 'M', 0, (MEMORY_END >> 4) - seg - 2);
1.1.1.8   root     17545:        first_mcb = seg;
                   17546:        
1.1.1.19  root     17547:        // dummy mcb to link to umb
1.1.1.33  root     17548: #if 0
1.1.1.39  root     17549:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC"); // link umb
1.1.1.33  root     17550: #else
1.1.1.39  root     17551:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC"); // unlink umb
1.1.1.33  root     17552: #endif
1.1.1.19  root     17553:        
                   17554:        // first free mcb in upper memory block
1.1.1.8   root     17555:        msdos_mcb_create(UMB_TOP >> 4, 'Z', 0, (UMB_END >> 4) - (UMB_TOP >> 4) - 1);
1.1       root     17556:        
1.1.1.29  root     17557: #ifdef SUPPORT_HMA
                   17558:        // first free mcb in high memory area
                   17559:        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   17560: #endif
                   17561:        
1.1.1.26  root     17562:        // interrupt vector
                   17563:        for(int i = 0; i < 0x80; i++) {
                   17564:                *(UINT16 *)(mem + 4 * i + 0) = i;
                   17565:                *(UINT16 *)(mem + 4 * i + 2) = (IRET_TOP >> 4);
                   17566:        }
1.1.1.49  root     17567:        *(UINT16 *)(mem + 4 * 0x08 + 0) = 0x0018;       // fffc:0018 irq0 (system timer)
                   17568:        *(UINT16 *)(mem + 4 * 0x08 + 2) = DUMMY_TOP >> 4;
1.1.1.26  root     17569:        *(UINT16 *)(mem + 4 * 0x22 + 0) = 0x0000;       // ffff:0000 boot
                   17570:        *(UINT16 *)(mem + 4 * 0x22 + 2) = 0xffff;
                   17571:        *(UINT16 *)(mem + 4 * 0x67 + 0) = 0x0012;       // xxxx:0012 ems
                   17572:        *(UINT16 *)(mem + 4 * 0x67 + 2) = XMS_TOP >> 4;
1.1.1.49  root     17573:        *(UINT16 *)(mem + 4 * 0x74 + 0) = 0x0000;       // fffc:0000 irq12 (mouse)
                   17574:        *(UINT16 *)(mem + 4 * 0x74 + 2) = DUMMY_TOP >> 4;
1.1.1.26  root     17575:        
1.1.1.29  root     17576:        // dummy devices (NUL -> CON -> ... -> CONFIG$ -> EMMXXXX0)
1.1.1.26  root     17577:        static const struct {
                   17578:                UINT16 attributes;
                   17579:                char *dev_name;
                   17580:        } dummy_devices[] = {
                   17581:                {0x8013, "CON     "},
                   17582:                {0x8000, "AUX     "},
                   17583:                {0xa0c0, "PRN     "},
                   17584:                {0x8008, "CLOCK$  "},
                   17585:                {0x8000, "COM1    "},
                   17586:                {0xa0c0, "LPT1    "},
                   17587:                {0xa0c0, "LPT2    "},
                   17588:                {0xa0c0, "LPT3    "},
                   17589:                {0x8000, "COM2    "},
                   17590:                {0x8000, "COM3    "},
                   17591:                {0x8000, "COM4    "},
1.1.1.30  root     17592: //             {0xc000, "CONFIG$ "},
                   17593:                {0xc000, "$IBMADSP"}, // for windows3.1 setup.exe
1.1.1.26  root     17594:        };
                   17595:        static const UINT8 dummy_device_routine[] = {
                   17596:                // from NUL device of Windows 98 SE
                   17597:                // or word ptr ES:[BX+03],0100
                   17598:                0x26, 0x81, 0x4f, 0x03, 0x00, 0x01,
                   17599:                // retf
                   17600:                0xcb,
                   17601:        };
1.1.1.29  root     17602:        device_t *last = NULL;
1.1.1.32  root     17603:        for(int i = 0; i < array_length(dummy_devices); i++) {
1.1.1.26  root     17604:                device_t *device = (device_t *)(mem + DEVICE_TOP + 22 + 18 * i);
1.1.1.29  root     17605:                device->next_driver.w.l = 22 + 18 * (i + 1);
                   17606:                device->next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     17607:                device->attributes = dummy_devices[i].attributes;
1.1.1.29  root     17608:                device->strategy = DEVICE_SIZE - sizeof(dummy_device_routine);
                   17609:                device->interrupt = DEVICE_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.26  root     17610:                memcpy(device->dev_name, dummy_devices[i].dev_name, 8);
1.1.1.29  root     17611:                last = device;
                   17612:        }
                   17613:        if(last != NULL) {
                   17614:                last->next_driver.w.l = 0;
                   17615:                last->next_driver.w.h = XMS_TOP >> 4;
1.1.1.26  root     17616:        }
1.1.1.29  root     17617:        memcpy(mem + DEVICE_TOP + DEVICE_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.26  root     17618:        
1.1.1.25  root     17619:        // dos info
                   17620:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   17621:        dos_info->magic_word = 1;
                   17622:        dos_info->first_mcb = MEMORY_TOP >> 4;
                   17623:        dos_info->first_dpb.w.l = 0;
                   17624:        dos_info->first_dpb.w.h = DPB_TOP >> 4;
                   17625:        dos_info->first_sft.w.l = 0;
                   17626:        dos_info->first_sft.w.h = SFT_TOP >> 4;
1.1.1.41  root     17627:        dos_info->clock_device.w.l = 22 + 18 * 3;       // CLOCK$ is the 4th device in IO.SYS
1.1.1.25  root     17628:        dos_info->clock_device.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     17629:        dos_info->con_device.w.l = 22 + 18 * 0;         // CON is the 1st device in IO.SYS
1.1.1.25  root     17630:        dos_info->con_device.w.h = DEVICE_TOP >> 4;
                   17631:        dos_info->max_sector_len = 512;
                   17632:        dos_info->disk_buf_info.w.l = offsetof(dos_info_t, disk_buf_heads);
                   17633:        dos_info->disk_buf_info.w.h = DOS_INFO_TOP >> 4;
                   17634:        dos_info->cds.w.l = 0;
                   17635:        dos_info->cds.w.h = CDS_TOP >> 4;
                   17636:        dos_info->fcb_table.w.l = 0;
                   17637:        dos_info->fcb_table.w.h = FCB_TABLE_TOP >> 4;
                   17638:        dos_info->last_drive = 'Z' - 'A' + 1;
                   17639:        dos_info->buffers_x = 20;
                   17640:        dos_info->buffers_y = 0;
                   17641:        dos_info->boot_drive = 'C' - 'A' + 1;
1.1.1.29  root     17642:        dos_info->nul_device.next_driver.w.l = 22;
                   17643:        dos_info->nul_device.next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.25  root     17644:        dos_info->nul_device.attributes = 0x8004;
1.1.1.29  root     17645:        dos_info->nul_device.strategy = DOS_INFO_SIZE - sizeof(dummy_device_routine);
                   17646:        dos_info->nul_device.interrupt = DOS_INFO_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     17647:        memcpy(dos_info->nul_device.dev_name, "NUL     ", 8);
                   17648:        dos_info->disk_buf_heads.w.l = 0;
                   17649:        dos_info->disk_buf_heads.w.h = DISK_BUF_TOP >> 4;
1.1.1.39  root     17650:        dos_info->umb_linked = (((mcb_t *)(mem + MEMORY_END - 16))->mz == 'M' ? 0x01 : 0x00);
1.1.1.25  root     17651:        dos_info->first_umb_fcb = UMB_TOP >> 4;
                   17652:        dos_info->first_mcb_2 = MEMORY_TOP >> 4;
1.1.1.29  root     17653:        memcpy(mem + DOS_INFO_TOP + DOS_INFO_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.25  root     17654:        
                   17655:        char *env;
                   17656:        if((env = getenv("LASTDRIVE")) != NULL) {
                   17657:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   17658:                        dos_info->last_drive = env[0] - 'A' + 1;
                   17659:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   17660:                        dos_info->last_drive = env[0] - 'a' + 1;
                   17661:                }
                   17662:        }
                   17663:        if((env = getenv("windir")) != NULL) {
                   17664:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   17665:                        dos_info->boot_drive = env[0] - 'A' + 1;
                   17666:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   17667:                        dos_info->boot_drive = env[0] - 'a' + 1;
                   17668:                }
                   17669:        }
                   17670: #if defined(HAS_I386)
                   17671:        dos_info->i386_or_later = 1;
                   17672: #else
                   17673:        dos_info->i386_or_later = 0;
                   17674: #endif
                   17675:        dos_info->ext_mem_size = (min(MAX_MEM, 0x4000000) - 0x100000) >> 10;
                   17676:        
1.1.1.27  root     17677:        // ems (int 67h) and xms
1.1.1.25  root     17678:        device_t *xms_device = (device_t *)(mem + XMS_TOP);
                   17679:        xms_device->next_driver.w.l = 0xffff;
                   17680:        xms_device->next_driver.w.h = 0xffff;
                   17681:        xms_device->attributes = 0xc000;
1.1.1.29  root     17682:        xms_device->strategy = XMS_SIZE - sizeof(dummy_device_routine);
                   17683:        xms_device->interrupt = XMS_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     17684:        memcpy(xms_device->dev_name, "EMMXXXX0", 8);
                   17685:        
1.1.1.26  root     17686:        mem[XMS_TOP + 0x12] = 0xcd;     // int 68h (dummy)
                   17687:        mem[XMS_TOP + 0x13] = 0x68;
                   17688:        mem[XMS_TOP + 0x14] = 0xcf;     // iret
1.1.1.19  root     17689: #ifdef SUPPORT_XMS
                   17690:        if(support_xms) {
1.1.1.26  root     17691:                mem[XMS_TOP + 0x15] = 0xcd;     // int 69h (dummy)
                   17692:                mem[XMS_TOP + 0x16] = 0x69;
                   17693:                mem[XMS_TOP + 0x17] = 0xcb;     // retf
1.1.1.19  root     17694:        } else
                   17695: #endif
1.1.1.26  root     17696:        mem[XMS_TOP + 0x15] = 0xcb;     // retf
1.1.1.29  root     17697:        memcpy(mem + XMS_TOP + XMS_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.19  root     17698:        
1.1.1.26  root     17699:        // irq12 routine (mouse)
1.1.1.49  root     17700:        mem[DUMMY_TOP + 0x00] = 0xcd;   // int 6ah (dummy)
                   17701:        mem[DUMMY_TOP + 0x01] = 0x6a;
                   17702:        mem[DUMMY_TOP + 0x02] = 0x9a;   // call far mouse
                   17703:        mem[DUMMY_TOP + 0x03] = 0xff;
                   17704:        mem[DUMMY_TOP + 0x04] = 0xff;
                   17705:        mem[DUMMY_TOP + 0x05] = 0xff;
                   17706:        mem[DUMMY_TOP + 0x06] = 0xff;
                   17707:        mem[DUMMY_TOP + 0x07] = 0xcd;   // int 6bh (dummy)
                   17708:        mem[DUMMY_TOP + 0x08] = 0x6b;
                   17709:        mem[DUMMY_TOP + 0x09] = 0xcf;   // iret
1.1.1.24  root     17710:        
1.1.1.27  root     17711:        // case map routine
1.1.1.49  root     17712:        mem[DUMMY_TOP + 0x0a] = 0xcd;   // int 6ch (dummy)
                   17713:        mem[DUMMY_TOP + 0x0b] = 0x6c;
                   17714:        mem[DUMMY_TOP + 0x0c] = 0xcb;   // retf
1.1.1.27  root     17715:        
                   17716:        // font read routine
1.1.1.49  root     17717:        mem[DUMMY_TOP + 0x0d] = 0xcd;   // int 6dh (dummy)
                   17718:        mem[DUMMY_TOP + 0x0e] = 0x6d;
                   17719:        mem[DUMMY_TOP + 0x0f] = 0xcb;   // retf
1.1.1.19  root     17720:        
1.1.1.32  root     17721:        // error message read routine
1.1.1.49  root     17722:        mem[DUMMY_TOP + 0x10] = 0xcd;   // int 6eh (dummy)
                   17723:        mem[DUMMY_TOP + 0x11] = 0x6e;
                   17724:        mem[DUMMY_TOP + 0x12] = 0xcb;   // retf
1.1.1.32  root     17725:        
1.1.1.35  root     17726:        // dummy loop to wait BIOS/DOS service is done
1.1.1.49  root     17727:        mem[DUMMY_TOP + 0x13] = 0xe6;   // out f7h, al
                   17728:        mem[DUMMY_TOP + 0x14] = 0xf7;
                   17729:        mem[DUMMY_TOP + 0x15] = 0x78;   // js/jns -4
                   17730:        mem[DUMMY_TOP + 0x16] = 0xfc;
                   17731:        mem[DUMMY_TOP + 0x17] = 0xcb;   // retf
1.1.1.35  root     17732:        
1.1.1.50  root     17733:        // irq0 routine (system timer)
1.1.1.49  root     17734:        mem[DUMMY_TOP + 0x18] = 0xcd;   // int 1ch
                   17735:        mem[DUMMY_TOP + 0x19] = 0x1c;
                   17736:        mem[DUMMY_TOP + 0x1a] = 0xea;   // jmp far (IRET_TOP >> 4):0008
                   17737:        mem[DUMMY_TOP + 0x1b] = 0x08;
                   17738:        mem[DUMMY_TOP + 0x1c] = 0x00;
                   17739:        mem[DUMMY_TOP + 0x1d] = ((IRET_TOP >> 4)     ) & 0xff;
                   17740:        mem[DUMMY_TOP + 0x1e] = ((IRET_TOP >> 4) >> 8) & 0xff;
                   17741:        
                   17742:        // alter page map and call routine
                   17743:        mem[DUMMY_TOP + 0x1f] = 0x9a;   // call far
                   17744:        mem[DUMMY_TOP + 0x20] = 0xff;
                   17745:        mem[DUMMY_TOP + 0x21] = 0xff;
                   17746:        mem[DUMMY_TOP + 0x22] = 0xff;
                   17747:        mem[DUMMY_TOP + 0x23] = 0xff;
                   17748:        mem[DUMMY_TOP + 0x24] = 0xcd;   // int 6fh (dummy)
                   17749:        mem[DUMMY_TOP + 0x25] = 0x6f;
                   17750:        mem[DUMMY_TOP + 0x26] = 0xcb;   // retf
1.1.1.14  root     17751:        
1.1.1.50  root     17752:        // call int 29h routine
                   17753:        mem[DUMMY_TOP + 0x27] = 0xcd;   // int 29h
                   17754:        mem[DUMMY_TOP + 0x28] = 0x29;
                   17755:        mem[DUMMY_TOP + 0x29] = 0xcb;   // retf
                   17756:        
1.1.1.26  root     17757:        // boot routine
1.1.1.49  root     17758:        mem[0xffff0 + 0x00] = 0xf4;     // halt
                   17759:        mem[0xffff0 + 0x05] = '0';      // rom date
                   17760:        mem[0xffff0 + 0x06] = '2';
                   17761:        mem[0xffff0 + 0x07] = '/';
                   17762:        mem[0xffff0 + 0x08] = '2';
                   17763:        mem[0xffff0 + 0x09] = '2';
                   17764:        mem[0xffff0 + 0x0a] = '/';
                   17765:        mem[0xffff0 + 0x0b] = '0';
                   17766:        mem[0xffff0 + 0x0c] = '6';
                   17767:        mem[0xffff0 + 0x0e] = 0xfc;     // machine id
                   17768:        mem[0xffff0 + 0x0f] = 0x00;
1.1.1.24  root     17769:        
1.1       root     17770:        // param block
                   17771:        // + 0: param block (22bytes)
                   17772:        // +24: fcb1/2 (20bytes)
                   17773:        // +44: command tail (128bytes)
                   17774:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   17775:        param->env_seg = 0;
                   17776:        param->cmd_line.w.l = 44;
                   17777:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   17778:        param->fcb1.w.l = 24;
                   17779:        param->fcb1.w.h = (WORK_TOP >> 4);
                   17780:        param->fcb2.w.l = 24;
                   17781:        param->fcb2.w.h = (WORK_TOP >> 4);
                   17782:        
                   17783:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   17784:        
                   17785:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   17786:        if(argc > 1) {
                   17787:                sprintf(cmd_line->cmd, " %s", argv[1]);
                   17788:                for(int i = 2; i < argc; i++) {
                   17789:                        char tmp[128];
                   17790:                        sprintf(tmp, "%s %s", cmd_line->cmd, argv[i]);
                   17791:                        strcpy(cmd_line->cmd, tmp);
                   17792:                }
                   17793:                cmd_line->len = (UINT8)strlen(cmd_line->cmd);
                   17794:        } else {
                   17795:                cmd_line->len = 0;
                   17796:        }
                   17797:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   17798:        
                   17799:        // system file table
1.1.1.21  root     17800:        *(UINT32 *)(mem + SFT_TOP + 0) = 0xffffffff;
                   17801:        *(UINT16 *)(mem + SFT_TOP + 4) = 20;
1.1       root     17802:        
1.1.1.19  root     17803:        // disk buffer header (from DOSBox)
                   17804:        *(UINT16 *)(mem + DISK_BUF_TOP +  0) = 0xffff;          // forward ptr
                   17805:        *(UINT16 *)(mem + DISK_BUF_TOP +  2) = 0xffff;          // backward ptr
                   17806:        *(UINT8  *)(mem + DISK_BUF_TOP +  4) = 0xff;            // not in use
                   17807:        *(UINT8  *)(mem + DISK_BUF_TOP + 10) = 0x01;            // number of FATs
                   17808:        *(UINT32 *)(mem + DISK_BUF_TOP + 13) = 0xffffffff;      // pointer to DPB
                   17809:        
1.1       root     17810:        // fcb table
                   17811:        *(UINT32 *)(mem + FCB_TABLE_TOP + 0) = 0xffffffff;
1.1.1.14  root     17812:        *(UINT16 *)(mem + FCB_TABLE_TOP + 4) = 0;
1.1       root     17813:        
1.1.1.41  root     17814:        // drive parameter block
1.1.1.42  root     17815:        for(int i = 0; i < 2; i++) {
1.1.1.43  root     17816:                // may be a floppy drive
1.1.1.44  root     17817:                cds_t *cds = (cds_t *)(mem + CDS_TOP + 88 * i);
                   17818:                sprintf(cds->path_name, "%c:\\", 'A' + i);
                   17819:                cds->drive_attrib = 0x4000;     // physical drive
                   17820:                cds->dpb_ptr.w.l = sizeof(dpb_t) * i;
                   17821:                cds->dpb_ptr.w.h = DPB_TOP >> 4;
                   17822:                cds->word_1 = cds->word_2 = cds->word_3 = 0xffff;
                   17823:                cds->bs_offset = 2;
                   17824:                
1.1.1.41  root     17825:                dpb_t *dpb = (dpb_t *)(mem + DPB_TOP + sizeof(dpb_t) * i);
                   17826:                dpb->drive_num = i;
                   17827:                dpb->unit_num = i;
1.1.1.43  root     17828:                dpb->next_dpb_ofs = /*(i == 25) ? 0xffff : */sizeof(dpb_t) * (i + 1);
                   17829:                dpb->next_dpb_seg = /*(i == 25) ? 0xffff : */DPB_TOP >> 4;
1.1.1.42  root     17830:        }
                   17831:        for(int i = 2; i < 26; i++) {
1.1.1.44  root     17832:                msdos_cds_update(i);
1.1.1.42  root     17833:                UINT16 seg, ofs;
                   17834:                msdos_drive_param_block_update(i, &seg, &ofs, 1);
1.1.1.41  root     17835:        }
                   17836:        
1.1.1.17  root     17837:        // nls stuff
                   17838:        msdos_nls_tables_init();
1.1       root     17839:        
                   17840:        // execute command
1.1.1.28  root     17841:        try {
1.1.1.52! root     17842:                if(msdos_process_exec(argv[0], param, 0, true)) {
1.1.1.28  root     17843:                        fatalerror("'%s' not found\n", argv[0]);
                   17844:                }
                   17845:        } catch(...) {
                   17846:                // we should not reach here :-(
                   17847:                fatalerror("failed to start '%s' because of unexpected exeption\n", argv[0]);
1.1       root     17848:        }
                   17849:        retval = 0;
                   17850:        return(0);
                   17851: }
                   17852: 
                   17853: #define remove_std_file(path) { \
                   17854:        int fd = _open(path, _O_RDONLY | _O_BINARY); \
                   17855:        if(fd != -1) { \
                   17856:                _lseek(fd, 0, SEEK_END); \
                   17857:                int size = _tell(fd); \
                   17858:                _close(fd); \
                   17859:                if(size == 0) { \
                   17860:                        remove(path); \
                   17861:                } \
                   17862:        } \
                   17863: }
                   17864: 
                   17865: void msdos_finish()
                   17866: {
                   17867:        for(int i = 0; i < MAX_FILES; i++) {
                   17868:                if(file_handler[i].valid) {
                   17869:                        _close(i);
                   17870:                }
                   17871:        }
1.1.1.21  root     17872: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     17873:        remove_std_file("stdaux.txt");
1.1.1.21  root     17874: #endif
1.1.1.30  root     17875: #ifdef SUPPORT_XMS
                   17876:        msdos_xms_finish();
                   17877: #endif
1.1       root     17878:        msdos_dbcs_table_finish();
                   17879: }
                   17880: 
                   17881: /* ----------------------------------------------------------------------------
                   17882:        PC/AT hardware emulation
                   17883: ---------------------------------------------------------------------------- */
                   17884: 
                   17885: void hardware_init()
                   17886: {
1.1.1.3   root     17887:        CPU_INIT_CALL(CPU_MODEL);
1.1       root     17888:        CPU_RESET_CALL(CPU_MODEL);
1.1.1.14  root     17889:        m_IF = 1;
1.1.1.3   root     17890: #if defined(HAS_I386)
1.1       root     17891:        cpu_type = (REG32(EDX) >> 8) & 0x0f;
                   17892:        cpu_step = (REG32(EDX) >> 0) & 0x0f;
1.1.1.3   root     17893: #endif
                   17894:        i386_set_a20_line(0);
1.1.1.14  root     17895:        
1.1.1.19  root     17896:        ems_init();
1.1.1.25  root     17897:        dma_init();
1.1       root     17898:        pic_init();
1.1.1.25  root     17899:        pio_init();
1.1.1.8   root     17900: #ifdef PIT_ALWAYS_RUNNING
                   17901:        pit_init();
                   17902: #else
1.1       root     17903:        pit_active = 0;
                   17904: #endif
1.1.1.25  root     17905:        sio_init();
1.1.1.8   root     17906:        cmos_init();
                   17907:        kbd_init();
1.1       root     17908: }
                   17909: 
1.1.1.10  root     17910: void hardware_finish()
                   17911: {
                   17912: #if defined(HAS_I386)
                   17913:        vtlb_free(m_vtlb);
                   17914: #endif
1.1.1.19  root     17915:        ems_finish();
1.1.1.37  root     17916:        pio_finish();
1.1.1.25  root     17917:        sio_finish();
1.1.1.10  root     17918: }
                   17919: 
1.1.1.28  root     17920: void hardware_release()
                   17921: {
                   17922:        // release hardware resources when this program will be terminated abnormally
                   17923: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     17924:        if(fp_debug_log != NULL) {
                   17925:                fclose(fp_debug_log);
                   17926:                fp_debug_log = NULL;
1.1.1.28  root     17927:        }
                   17928: #endif
                   17929: #if defined(HAS_I386)
                   17930:        vtlb_free(m_vtlb);
                   17931: #endif
                   17932:        ems_release();
1.1.1.37  root     17933:        pio_release();
1.1.1.28  root     17934:        sio_release();
                   17935: }
                   17936: 
1.1       root     17937: void hardware_run()
                   17938: {
1.1.1.22  root     17939: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.28  root     17940:        // open debug log file after msdos_init() is done not to use the standard file handlers
1.1.1.33  root     17941:        fp_debug_log = fopen("debug.log", "w");
1.1.1.22  root     17942: #endif
1.1.1.51  root     17943: #ifdef USE_DEBUGGER
                   17944:        m_int_num = -1;
                   17945: #endif
1.1.1.3   root     17946:        while(!m_halted) {
1.1.1.50  root     17947:                hardware_run_cpu();
1.1       root     17948:        }
1.1.1.22  root     17949: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     17950:        if(fp_debug_log != NULL) {
                   17951:                fclose(fp_debug_log);
                   17952:                fp_debug_log = NULL;
1.1.1.28  root     17953:        }
1.1.1.22  root     17954: #endif
1.1       root     17955: }
                   17956: 
1.1.1.50  root     17957: inline void hardware_run_cpu()
                   17958: {
                   17959: #if defined(HAS_I386)
                   17960:        CPU_EXECUTE_CALL(i386);
                   17961:        if(m_eip != m_prev_eip) {
                   17962:                idle_ops++;
                   17963:        }
                   17964: #else
                   17965:        CPU_EXECUTE_CALL(CPU_MODEL);
                   17966:        if(m_pc != m_prevpc) {
                   17967:                idle_ops++;
                   17968:        }
                   17969: #endif
                   17970: #ifdef USE_DEBUGGER
                   17971:        // Disallow reentering CPU_EXECUTE() in msdos_syscall()
                   17972:        if(m_int_num >= 0) {
                   17973:                unsigned num = (unsigned)m_int_num;
                   17974:                m_int_num = -1;
                   17975:                msdos_syscall(num);
                   17976:        }
                   17977: #endif
                   17978:        if(++update_ops == UPDATE_OPS) {
                   17979:                update_ops = 0;
                   17980:                hardware_update();
                   17981:        }
                   17982: }
                   17983: 
1.1       root     17984: void hardware_update()
                   17985: {
1.1.1.8   root     17986:        static UINT32 prev_time = 0;
                   17987:        UINT32 cur_time = timeGetTime();
                   17988:        
                   17989:        if(prev_time != cur_time) {
                   17990:                // update pit and raise irq0
                   17991: #ifndef PIT_ALWAYS_RUNNING
                   17992:                if(pit_active)
                   17993: #endif
                   17994:                {
                   17995:                        if(pit_run(0, cur_time)) {
                   17996:                                pic_req(0, 0, 1);
                   17997:                        }
                   17998:                        pit_run(1, cur_time);
                   17999:                        pit_run(2, cur_time);
                   18000:                }
1.1.1.24  root     18001:                
1.1.1.25  root     18002:                // update sio and raise irq4/3
1.1.1.29  root     18003:                for(int c = 0; c < 4; c++) {
1.1.1.25  root     18004:                        sio_update(c);
                   18005:                }
                   18006:                
1.1.1.24  root     18007:                // update keyboard and mouse
1.1.1.14  root     18008:                static UINT32 prev_tick = 0;
                   18009:                UINT32 cur_tick = cur_time / 32;
1.1.1.25  root     18010:                
1.1.1.14  root     18011:                if(prev_tick != cur_tick) {
                   18012:                        // update keyboard flags
                   18013:                        UINT8 state;
1.1.1.24  root     18014:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x0001) ? 0x80 : 0;
                   18015:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x0001) ? 0x40 : 0;
                   18016:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x0001) ? 0x20 : 0;
                   18017:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x0001) ? 0x10 : 0;
                   18018:                        state |= (GetAsyncKeyState(VK_MENU    ) & 0x8000) ? 0x08 : 0;
                   18019:                        state |= (GetAsyncKeyState(VK_CONTROL ) & 0x8000) ? 0x04 : 0;
                   18020:                        state |= (GetAsyncKeyState(VK_LSHIFT  ) & 0x8000) ? 0x02 : 0;
                   18021:                        state |= (GetAsyncKeyState(VK_RSHIFT  ) & 0x8000) ? 0x01 : 0;
1.1.1.14  root     18022:                        mem[0x417] = state;
                   18023:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x8000) ? 0x80 : 0;
                   18024:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   18025:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   18026:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
1.1.1.24  root     18027: //                     state |= (GetAsyncKeyState(VK_PAUSE   ) & 0x0001) ? 0x08 : 0;
                   18028: //                     state |= (GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x04 : 0;
1.1.1.14  root     18029:                        state |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   18030:                        state |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   18031:                        mem[0x418] = state;
                   18032:                        
1.1.1.24  root     18033:                        // update console input if needed
1.1.1.34  root     18034:                        if(!key_changed || mouse.hidden == 0) {
1.1.1.24  root     18035:                                update_console_input();
                   18036:                        }
                   18037:                        
                   18038:                        // raise irq1 if key is pressed/released
                   18039:                        if(key_changed) {
1.1.1.8   root     18040:                                pic_req(0, 1, 1);
1.1.1.24  root     18041:                                key_changed = false;
                   18042:                        }
                   18043:                        
                   18044:                        // raise irq12 if mouse status is changed
1.1.1.43  root     18045:                        if((mouse.status & mouse.call_mask) && mouse.call_addr.dw) {
                   18046:                                mouse.status_irq = mouse.status & mouse.call_mask;
                   18047:                                mouse.status_irq_alt = 0; // ???
1.1.1.24  root     18048:                                mouse.status &= ~mouse.call_mask;
1.1.1.43  root     18049:                                pic_req(1, 4, 1);
                   18050:                        } else {
                   18051:                                for(int i = 0; i < 8; i++) {
                   18052:                                        if((mouse.status_alt & (1 << i)) && mouse.call_addr_alt[i].dw) {
                   18053:                                                mouse.status_irq = 0; // ???
                   18054:                                                mouse.status_irq_alt = 0;
                   18055:                                                for(int j = 0; j < 8; j++) {
                   18056:                                                        if((mouse.status_alt & (1 << j)) && mouse.call_addr_alt[i].dw == mouse.call_addr_alt[j].dw) {
                   18057:                                                                mouse.status_irq_alt |=  (1 << j);
                   18058:                                                                mouse.status_alt     &= ~(1 << j);
                   18059:                                                        }
                   18060:                                                }
                   18061:                                                pic_req(1, 4, 1);
                   18062:                                                break;
                   18063:                                        }
                   18064:                                }
1.1.1.8   root     18065:                        }
1.1.1.24  root     18066:                        
1.1.1.14  root     18067:                        prev_tick = cur_tick;
1.1.1.8   root     18068:                }
1.1.1.24  root     18069:                
1.1.1.19  root     18070:                // update daily timer counter
                   18071:                pcbios_update_daily_timer_counter(cur_time);
1.1.1.25  root     18072:                
1.1.1.8   root     18073:                prev_time = cur_time;
1.1       root     18074:        }
                   18075: }
                   18076: 
1.1.1.19  root     18077: // ems
                   18078: 
                   18079: void ems_init()
                   18080: {
                   18081:        memset(ems_handles, 0, sizeof(ems_handles));
                   18082:        memset(ems_pages, 0, sizeof(ems_pages));
                   18083:        free_ems_pages = MAX_EMS_PAGES;
                   18084: }
                   18085: 
                   18086: void ems_finish()
                   18087: {
1.1.1.28  root     18088:        ems_release();
                   18089: }
                   18090: 
                   18091: void ems_release()
                   18092: {
1.1.1.31  root     18093:        for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.28  root     18094:                if(ems_handles[i].buffer != NULL) {
1.1.1.19  root     18095:                        free(ems_handles[i].buffer);
                   18096:                        ems_handles[i].buffer = NULL;
                   18097:                }
                   18098:        }
                   18099: }
                   18100: 
                   18101: void ems_allocate_pages(int handle, int pages)
                   18102: {
                   18103:        if(pages > 0) {
                   18104:                ems_handles[handle].buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   18105:        } else {
                   18106:                ems_handles[handle].buffer = NULL;
                   18107:        }
                   18108:        ems_handles[handle].pages = pages;
                   18109:        ems_handles[handle].allocated = true;
                   18110:        free_ems_pages -= pages;
                   18111: }
                   18112: 
                   18113: void ems_reallocate_pages(int handle, int pages)
                   18114: {
                   18115:        if(ems_handles[handle].allocated) {
                   18116:                if(ems_handles[handle].pages != pages) {
                   18117:                        UINT8 *new_buffer = NULL;
                   18118:                        
                   18119:                        if(pages > 0) {
                   18120:                                new_buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   18121:                        }
1.1.1.32  root     18122:                        if(ems_handles[handle].buffer != NULL) {
                   18123:                                if(new_buffer != NULL) {
1.1.1.19  root     18124:                                        if(pages > ems_handles[handle].pages) {
                   18125:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * ems_handles[handle].pages);
                   18126:                                        } else {
                   18127:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * pages);
                   18128:                                        }
                   18129:                                }
                   18130:                                free(ems_handles[handle].buffer);
                   18131:                                ems_handles[handle].buffer = NULL;
                   18132:                        }
                   18133:                        free_ems_pages += ems_handles[handle].pages;
                   18134:                        
                   18135:                        ems_handles[handle].buffer = new_buffer;
                   18136:                        ems_handles[handle].pages = pages;
                   18137:                        free_ems_pages -= pages;
                   18138:                }
                   18139:        } else {
                   18140:                ems_allocate_pages(handle, pages);
                   18141:        }
                   18142: }
                   18143: 
                   18144: void ems_release_pages(int handle)
                   18145: {
                   18146:        if(ems_handles[handle].allocated) {
1.1.1.32  root     18147:                if(ems_handles[handle].buffer != NULL) {
1.1.1.19  root     18148:                        free(ems_handles[handle].buffer);
                   18149:                        ems_handles[handle].buffer = NULL;
                   18150:                }
                   18151:                free_ems_pages += ems_handles[handle].pages;
                   18152:                ems_handles[handle].allocated = false;
                   18153:        }
                   18154: }
                   18155: 
                   18156: void ems_map_page(int physical, int handle, int logical)
                   18157: {
                   18158:        if(ems_pages[physical].mapped) {
                   18159:                if(ems_pages[physical].handle == handle && ems_pages[physical].page == logical) {
                   18160:                        return;
                   18161:                }
                   18162:                ems_unmap_page(physical);
                   18163:        }
1.1.1.32  root     18164:        if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     18165:                memcpy(mem + EMS_TOP + 0x4000 * physical, ems_handles[handle].buffer + 0x4000 * logical, 0x4000);
                   18166:        }
                   18167:        ems_pages[physical].handle = handle;
                   18168:        ems_pages[physical].page = logical;
                   18169:        ems_pages[physical].mapped = true;
                   18170: }
                   18171: 
                   18172: void ems_unmap_page(int physical)
                   18173: {
                   18174:        if(ems_pages[physical].mapped) {
                   18175:                int handle = ems_pages[physical].handle;
                   18176:                int logical = ems_pages[physical].page;
                   18177:                
1.1.1.32  root     18178:                if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     18179:                        memcpy(ems_handles[handle].buffer + 0x4000 * logical, mem + EMS_TOP + 0x4000 * physical, 0x4000);
                   18180:                }
                   18181:                ems_pages[physical].mapped = false;
                   18182:        }
                   18183: }
                   18184: 
1.1.1.25  root     18185: // dma
1.1       root     18186: 
1.1.1.25  root     18187: void dma_init()
1.1       root     18188: {
1.1.1.26  root     18189:        memset(dma, 0, sizeof(dma));
1.1.1.25  root     18190:        for(int c = 0; c < 2; c++) {
1.1.1.26  root     18191: //             for(int ch = 0; ch < 4; ch++) {
                   18192: //                     dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w = 0xffff;
                   18193: //             }
1.1.1.25  root     18194:                dma_reset(c);
                   18195:        }
1.1       root     18196: }
                   18197: 
1.1.1.25  root     18198: void dma_reset(int c)
1.1       root     18199: {
1.1.1.25  root     18200:        dma[c].low_high = false;
                   18201:        dma[c].cmd = dma[c].req = dma[c].tc = 0;
                   18202:        dma[c].mask = 0xff;
                   18203: }
                   18204: 
                   18205: void dma_write(int c, UINT32 addr, UINT8 data)
                   18206: {
                   18207:        int ch = (addr >> 1) & 3;
                   18208:        UINT8 bit = 1 << (data & 3);
                   18209:        
                   18210:        switch(addr & 0x0f) {
                   18211:        case 0x00: case 0x02: case 0x04: case 0x06:
                   18212:                if(dma[c].low_high) {
                   18213:                        dma[c].ch[ch].bareg.b.h = data;
1.1       root     18214:                } else {
1.1.1.25  root     18215:                        dma[c].ch[ch].bareg.b.l = data;
                   18216:                }
                   18217:                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   18218:                dma[c].low_high = !dma[c].low_high;
                   18219:                break;
                   18220:        case 0x01: case 0x03: case 0x05: case 0x07:
                   18221:                if(dma[c].low_high) {
                   18222:                        dma[c].ch[ch].bcreg.b.h = data;
                   18223:                } else {
                   18224:                        dma[c].ch[ch].bcreg.b.l = data;
                   18225:                }
                   18226:                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   18227:                dma[c].low_high = !dma[c].low_high;
                   18228:                break;
                   18229:        case 0x08:
                   18230:                // command register
                   18231:                dma[c].cmd = data;
                   18232:                break;
                   18233:        case 0x09:
                   18234:                // dma[c].request register
                   18235:                if(data & 4) {
                   18236:                        if(!(dma[c].req & bit)) {
                   18237:                                dma[c].req |= bit;
                   18238: //                             dma_run(c, ch);
                   18239:                        }
                   18240:                } else {
                   18241:                        dma[c].req &= ~bit;
                   18242:                }
                   18243:                break;
                   18244:        case 0x0a:
                   18245:                // single mask register
                   18246:                if(data & 4) {
                   18247:                        dma[c].mask |= bit;
                   18248:                } else {
                   18249:                        dma[c].mask &= ~bit;
                   18250:                }
                   18251:                break;
                   18252:        case 0x0b:
                   18253:                // mode register
                   18254:                dma[c].ch[data & 3].mode = data;
                   18255:                break;
                   18256:        case 0x0c:
                   18257:                dma[c].low_high = false;
                   18258:                break;
                   18259:        case 0x0d:
                   18260:                // clear master
                   18261:                dma_reset(c);
                   18262:                break;
                   18263:        case 0x0e:
                   18264:                // clear mask register
                   18265:                dma[c].mask = 0;
                   18266:                break;
                   18267:        case 0x0f:
                   18268:                // all mask register
                   18269:                dma[c].mask = data & 0x0f;
                   18270:                break;
                   18271:        }
                   18272: }
                   18273: 
                   18274: UINT8 dma_read(int c, UINT32 addr)
                   18275: {
                   18276:        int ch = (addr >> 1) & 3;
                   18277:        UINT8 val = 0xff;
                   18278:        
                   18279:        switch(addr & 0x0f) {
                   18280:        case 0x00: case 0x02: case 0x04: case 0x06:
                   18281:                if(dma[c].low_high) {
                   18282:                        val = dma[c].ch[ch].areg.b.h;
                   18283:                } else {
                   18284:                        val = dma[c].ch[ch].areg.b.l;
                   18285:                }
                   18286:                dma[c].low_high = !dma[c].low_high;
                   18287:                return(val);
                   18288:        case 0x01: case 0x03: case 0x05: case 0x07:
                   18289:                if(dma[c].low_high) {
                   18290:                        val = dma[c].ch[ch].creg.b.h;
                   18291:                } else {
                   18292:                        val = dma[c].ch[ch].creg.b.l;
                   18293:                }
                   18294:                dma[c].low_high = !dma[c].low_high;
                   18295:                return(val);
                   18296:        case 0x08:
                   18297:                // status register
                   18298:                val = (dma[c].req << 4) | dma[c].tc;
                   18299:                dma[c].tc = 0;
                   18300:                return(val);
                   18301:        case 0x0d:
1.1.1.26  root     18302:                // temporary register (intel 82374 does not support)
1.1.1.25  root     18303:                return(dma[c].tmp & 0xff);
1.1.1.26  root     18304:        case 0x0f:
                   18305:                // mask register (intel 82374 does support)
                   18306:                return(dma[c].mask);
1.1.1.25  root     18307:        }
                   18308:        return(0xff);
                   18309: }
                   18310: 
                   18311: void dma_page_write(int c, int ch, UINT8 data)
                   18312: {
                   18313:        dma[c].ch[ch].pagereg = data;
                   18314: }
                   18315: 
                   18316: UINT8 dma_page_read(int c, int ch)
                   18317: {
                   18318:        return(dma[c].ch[ch].pagereg);
                   18319: }
                   18320: 
                   18321: void dma_run(int c, int ch)
                   18322: {
                   18323:        UINT8 bit = 1 << ch;
                   18324:        
                   18325:        if((dma[c].req & bit) && !(dma[c].mask & bit)) {
                   18326:                // execute dma
                   18327:                while(dma[c].req & bit) {
                   18328:                        if(ch == 0 && (dma[c].cmd & 0x01)) {
                   18329:                                // memory -> memory
                   18330:                                UINT32 saddr = dma[c].ch[0].areg.w | (dma[c].ch[0].pagereg << 16);
                   18331:                                UINT32 daddr = dma[c].ch[1].areg.w | (dma[c].ch[1].pagereg << 16);
                   18332:                                
                   18333:                                if(c == 0) {
                   18334:                                        dma[c].tmp = read_byte(saddr);
                   18335:                                        write_byte(daddr, dma[c].tmp);
                   18336:                                } else {
                   18337:                                        dma[c].tmp = read_word(saddr << 1);
                   18338:                                        write_word(daddr << 1, dma[c].tmp);
                   18339:                                }
                   18340:                                if(!(dma[c].cmd & 0x02)) {
                   18341:                                        if(dma[c].ch[0].mode & 0x20) {
                   18342:                                                dma[c].ch[0].areg.w--;
                   18343:                                                if(dma[c].ch[0].areg.w == 0xffff) {
                   18344:                                                        dma[c].ch[0].pagereg--;
                   18345:                                                }
                   18346:                                        } else {
                   18347:                                                dma[c].ch[0].areg.w++;
                   18348:                                                if(dma[c].ch[0].areg.w == 0) {
                   18349:                                                        dma[c].ch[0].pagereg++;
                   18350:                                                }
                   18351:                                        }
                   18352:                                }
                   18353:                                if(dma[c].ch[1].mode & 0x20) {
                   18354:                                        dma[c].ch[1].areg.w--;
                   18355:                                        if(dma[c].ch[1].areg.w == 0xffff) {
                   18356:                                                dma[c].ch[1].pagereg--;
                   18357:                                        }
                   18358:                                } else {
                   18359:                                        dma[c].ch[1].areg.w++;
                   18360:                                        if(dma[c].ch[1].areg.w == 0) {
                   18361:                                                dma[c].ch[1].pagereg++;
                   18362:                                        }
                   18363:                                }
                   18364:                                
                   18365:                                // check dma condition
                   18366:                                if(dma[c].ch[0].creg.w-- == 0) {
                   18367:                                        if(dma[c].ch[0].mode & 0x10) {
                   18368:                                                // self initialize
                   18369:                                                dma[c].ch[0].areg.w = dma[c].ch[0].bareg.w;
                   18370:                                                dma[c].ch[0].creg.w = dma[c].ch[0].bcreg.w;
                   18371:                                        } else {
                   18372: //                                             dma[c].mask |= bit;
                   18373:                                        }
                   18374:                                }
                   18375:                                if(dma[c].ch[1].creg.w-- == 0) {
                   18376:                                        // terminal count
                   18377:                                        if(dma[c].ch[1].mode & 0x10) {
                   18378:                                                // self initialize
                   18379:                                                dma[c].ch[1].areg.w = dma[c].ch[1].bareg.w;
                   18380:                                                dma[c].ch[1].creg.w = dma[c].ch[1].bcreg.w;
                   18381:                                        } else {
                   18382:                                                dma[c].mask |= bit;
                   18383:                                        }
                   18384:                                        dma[c].req &= ~bit;
                   18385:                                        dma[c].tc |= bit;
                   18386:                                }
                   18387:                        } else {
                   18388:                                UINT32 addr = dma[c].ch[ch].areg.w | (dma[c].ch[ch].pagereg << 16);
                   18389:                                
                   18390:                                if((dma[c].ch[ch].mode & 0x0c) == 0x00) {
                   18391:                                        // verify
                   18392:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x04) {
                   18393:                                        // io -> memory
                   18394:                                        if(c == 0) {
                   18395:                                                dma[c].tmp = read_io_byte(dma[c].ch[ch].port);
                   18396:                                                write_byte(addr, dma[c].tmp);
                   18397:                                        } else {
                   18398:                                                dma[c].tmp = read_io_word(dma[c].ch[ch].port);
                   18399:                                                write_word(addr << 1, dma[c].tmp);
                   18400:                                        }
                   18401:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x08) {
                   18402:                                        // memory -> io
                   18403:                                        if(c == 0) {
                   18404:                                                dma[c].tmp = read_byte(addr);
                   18405:                                                write_io_byte(dma[c].ch[ch].port, dma[c].tmp);
                   18406:                                        } else {
                   18407:                                                dma[c].tmp = read_word(addr << 1);
                   18408:                                                write_io_word(dma[c].ch[ch].port, dma[c].tmp);
                   18409:                                        }
                   18410:                                }
                   18411:                                if(dma[c].ch[ch].mode & 0x20) {
                   18412:                                        dma[c].ch[ch].areg.w--;
                   18413:                                        if(dma[c].ch[ch].areg.w == 0xffff) {
                   18414:                                                dma[c].ch[ch].pagereg--;
                   18415:                                        }
                   18416:                                } else {
                   18417:                                        dma[c].ch[ch].areg.w++;
                   18418:                                        if(dma[c].ch[ch].areg.w == 0) {
                   18419:                                                dma[c].ch[ch].pagereg++;
                   18420:                                        }
                   18421:                                }
                   18422:                                
                   18423:                                // check dma condition
                   18424:                                if(dma[c].ch[ch].creg.w-- == 0) {
                   18425:                                        // terminal count
                   18426:                                        if(dma[c].ch[ch].mode & 0x10) {
                   18427:                                                // self initialize
                   18428:                                                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   18429:                                                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   18430:                                        } else {
                   18431:                                                dma[c].mask |= bit;
                   18432:                                        }
                   18433:                                        dma[c].req &= ~bit;
                   18434:                                        dma[c].tc |= bit;
                   18435:                                } else if((dma[c].ch[ch].mode & 0xc0) == 0x40) {
                   18436:                                        // single mode
                   18437:                                        break;
                   18438:                                }
                   18439:                        }
                   18440:                }
                   18441:        }
                   18442: }
                   18443: 
                   18444: // pic
                   18445: 
                   18446: void pic_init()
                   18447: {
                   18448:        memset(pic, 0, sizeof(pic));
                   18449:        pic[0].imr = pic[1].imr = 0xff;
                   18450:        
                   18451:        // from bochs bios
                   18452:        pic_write(0, 0, 0x11);  // icw1 = 11h
                   18453:        pic_write(0, 1, 0x08);  // icw2 = 08h
                   18454:        pic_write(0, 1, 0x04);  // icw3 = 04h
                   18455:        pic_write(0, 1, 0x01);  // icw4 = 01h
                   18456:        pic_write(0, 1, 0xb8);  // ocw1 = b8h
                   18457:        pic_write(1, 0, 0x11);  // icw1 = 11h
                   18458:        pic_write(1, 1, 0x70);  // icw2 = 70h
                   18459:        pic_write(1, 1, 0x02);  // icw3 = 02h
                   18460:        pic_write(1, 1, 0x01);  // icw4 = 01h
                   18461: }
                   18462: 
                   18463: void pic_write(int c, UINT32 addr, UINT8 data)
                   18464: {
                   18465:        if(addr & 1) {
                   18466:                if(pic[c].icw2_r) {
                   18467:                        // icw2
                   18468:                        pic[c].icw2 = data;
                   18469:                        pic[c].icw2_r = 0;
                   18470:                } else if(pic[c].icw3_r) {
                   18471:                        // icw3
                   18472:                        pic[c].icw3 = data;
                   18473:                        pic[c].icw3_r = 0;
                   18474:                } else if(pic[c].icw4_r) {
                   18475:                        // icw4
                   18476:                        pic[c].icw4 = data;
                   18477:                        pic[c].icw4_r = 0;
                   18478:                } else {
                   18479:                        // ocw1
1.1       root     18480:                        pic[c].imr = data;
                   18481:                }
                   18482:        } else {
                   18483:                if(data & 0x10) {
                   18484:                        // icw1
                   18485:                        pic[c].icw1 = data;
                   18486:                        pic[c].icw2_r = 1;
                   18487:                        pic[c].icw3_r = (data & 2) ? 0 : 1;
                   18488:                        pic[c].icw4_r = data & 1;
                   18489:                        pic[c].irr = 0;
                   18490:                        pic[c].isr = 0;
                   18491:                        pic[c].imr = 0;
                   18492:                        pic[c].prio = 0;
                   18493:                        if(!(pic[c].icw1 & 1)) {
                   18494:                                pic[c].icw4 = 0;
                   18495:                        }
                   18496:                        pic[c].ocw3 = 0;
                   18497:                } else if(data & 8) {
                   18498:                        // ocw3
                   18499:                        if(!(data & 2)) {
                   18500:                                data = (data & ~1) | (pic[c].ocw3 & 1);
                   18501:                        }
                   18502:                        if(!(data & 0x40)) {
                   18503:                                data = (data & ~0x20) | (pic[c].ocw3 & 0x20);
                   18504:                        }
                   18505:                        pic[c].ocw3 = data;
                   18506:                } else {
                   18507:                        // ocw2
                   18508:                        int level = 0;
                   18509:                        if(data & 0x40) {
                   18510:                                level = data & 7;
                   18511:                        } else {
                   18512:                                if(!pic[c].isr) {
                   18513:                                        return;
                   18514:                                }
                   18515:                                level = pic[c].prio;
                   18516:                                while(!(pic[c].isr & (1 << level))) {
                   18517:                                        level = (level + 1) & 7;
                   18518:                                }
                   18519:                        }
                   18520:                        if(data & 0x80) {
                   18521:                                pic[c].prio = (level + 1) & 7;
                   18522:                        }
                   18523:                        if(data & 0x20) {
                   18524:                                pic[c].isr &= ~(1 << level);
                   18525:                        }
                   18526:                }
                   18527:        }
                   18528:        pic_update();
                   18529: }
                   18530: 
                   18531: UINT8 pic_read(int c, UINT32 addr)
                   18532: {
                   18533:        if(addr & 1) {
                   18534:                return(pic[c].imr);
                   18535:        } else {
                   18536:                // polling mode is not supported...
                   18537:                //if(pic[c].ocw3 & 4) {
                   18538:                //      return ???;
                   18539:                //}
                   18540:                if(pic[c].ocw3 & 1) {
                   18541:                        return(pic[c].isr);
                   18542:                } else {
                   18543:                        return(pic[c].irr);
                   18544:                }
                   18545:        }
                   18546: }
                   18547: 
                   18548: void pic_req(int c, int level, int signal)
                   18549: {
                   18550:        if(signal) {
                   18551:                pic[c].irr |= (1 << level);
                   18552:        } else {
                   18553:                pic[c].irr &= ~(1 << level);
                   18554:        }
                   18555:        pic_update();
                   18556: }
                   18557: 
                   18558: int pic_ack()
                   18559: {
                   18560:        // ack (INTA=L)
                   18561:        pic[pic_req_chip].isr |= pic_req_bit;
                   18562:        pic[pic_req_chip].irr &= ~pic_req_bit;
                   18563:        if(pic_req_chip > 0) {
                   18564:                // update isr and irr of master
                   18565:                UINT8 slave = 1 << (pic[pic_req_chip].icw3 & 7);
                   18566:                pic[pic_req_chip - 1].isr |= slave;
                   18567:                pic[pic_req_chip - 1].irr &= ~slave;
                   18568:        }
                   18569:        //if(pic[pic_req_chip].icw4 & 1) {
                   18570:                // 8086 mode
                   18571:                int vector = (pic[pic_req_chip].icw2 & 0xf8) | pic_req_level;
                   18572:        //} else {
                   18573:        //      // 8080 mode
                   18574:        //      UINT16 addr = (UINT16)pic[pic_req_chip].icw2 << 8;
                   18575:        //      if(pic[pic_req_chip].icw1 & 4) {
                   18576:        //              addr |= (pic[pic_req_chip].icw1 & 0xe0) | (pic_req_level << 2);
                   18577:        //      } else {
                   18578:        //              addr |= (pic[pic_req_chip].icw1 & 0xc0) | (pic_req_level << 3);
                   18579:        //      }
                   18580:        //      vector = 0xcd | (addr << 8);
                   18581:        //}
                   18582:        if(pic[pic_req_chip].icw4 & 2) {
                   18583:                // auto eoi
                   18584:                pic[pic_req_chip].isr &= ~pic_req_bit;
                   18585:        }
                   18586:        return(vector);
                   18587: }
                   18588: 
                   18589: void pic_update()
                   18590: {
                   18591:        for(int c = 0; c < 2; c++) {
                   18592:                UINT8 irr = pic[c].irr;
                   18593:                if(c + 1 < 2) {
                   18594:                        // this is master
                   18595:                        if(pic[c + 1].irr & (~pic[c + 1].imr)) {
                   18596:                                // request from slave
                   18597:                                irr |= 1 << (pic[c + 1].icw3 & 7);
                   18598:                        }
                   18599:                }
                   18600:                irr &= (~pic[c].imr);
                   18601:                if(!irr) {
                   18602:                        break;
                   18603:                }
                   18604:                if(!(pic[c].ocw3 & 0x20)) {
                   18605:                        irr |= pic[c].isr;
                   18606:                }
                   18607:                int level = pic[c].prio;
                   18608:                UINT8 bit = 1 << level;
                   18609:                while(!(irr & bit)) {
                   18610:                        level = (level + 1) & 7;
                   18611:                        bit = 1 << level;
                   18612:                }
                   18613:                if((c + 1 < 2) && (pic[c].icw3 & bit)) {
                   18614:                        // check slave
                   18615:                        continue;
                   18616:                }
                   18617:                if(pic[c].isr & bit) {
                   18618:                        break;
                   18619:                }
                   18620:                // interrupt request
                   18621:                pic_req_chip = c;
                   18622:                pic_req_level = level;
                   18623:                pic_req_bit = bit;
1.1.1.3   root     18624:                i386_set_irq_line(INPUT_LINE_IRQ, HOLD_LINE);
1.1       root     18625:                return;
                   18626:        }
1.1.1.3   root     18627:        i386_set_irq_line(INPUT_LINE_IRQ, CLEAR_LINE);
1.1.1.2   root     18628: }
1.1       root     18629: 
1.1.1.25  root     18630: // pio
                   18631: 
                   18632: void pio_init()
                   18633: {
1.1.1.38  root     18634: //     bool conv_mode = (GetConsoleCP() == 932);
                   18635:        
1.1.1.26  root     18636:        memset(pio, 0, sizeof(pio));
1.1.1.37  root     18637:        
1.1.1.25  root     18638:        for(int c = 0; c < 2; c++) {
1.1.1.37  root     18639:                pio[c].stat = 0xdf;
1.1.1.25  root     18640:                pio[c].ctrl = 0x0c;
1.1.1.38  root     18641: //             pio[c].conv_mode = conv_mode;
1.1.1.25  root     18642:        }
                   18643: }
                   18644: 
1.1.1.37  root     18645: void pio_finish()
                   18646: {
                   18647:        pio_release();
                   18648: }
                   18649: 
                   18650: void pio_release()
                   18651: {
                   18652:        for(int c = 0; c < 2; c++) {
                   18653:                if(pio[c].fp != NULL) {
1.1.1.38  root     18654:                        if(pio[c].jis_mode) {
                   18655:                                fputc(0x1c, pio[c].fp);
                   18656:                                fputc(0x2e, pio[c].fp);
                   18657:                        }
1.1.1.37  root     18658:                        fclose(pio[c].fp);
                   18659:                        pio[c].fp = NULL;
                   18660:                }
                   18661:        }
                   18662: }
                   18663: 
1.1.1.25  root     18664: void pio_write(int c, UINT32 addr, UINT8 data)
                   18665: {
                   18666:        switch(addr & 3) {
                   18667:        case 0:
                   18668:                pio[c].data = data;
                   18669:                break;
                   18670:        case 2:
1.1.1.37  root     18671:                if((pio[c].ctrl & 0x01) && !(data & 0x01)) {
                   18672:                        // strobe H -> L
                   18673:                        if(pio[c].data == 0x0d && (data & 0x02)) {
                   18674:                                // auto feed
                   18675:                                printer_out(c, 0x0d);
                   18676:                                printer_out(c, 0x0a);
                   18677:                        } else {
                   18678:                                printer_out(c, pio[c].data);
                   18679:                        }
                   18680:                        pio[c].stat &= ~0x40; // set ack
                   18681:                }
1.1.1.25  root     18682:                pio[c].ctrl = data;
                   18683:                break;
                   18684:        }
                   18685: }
                   18686: 
                   18687: UINT8 pio_read(int c, UINT32 addr)
                   18688: {
                   18689:        switch(addr & 3) {
                   18690:        case 0:
1.1.1.37  root     18691:                if(pio[c].ctrl & 0x20) {
                   18692:                        // input mode
                   18693:                        return(0xff);
                   18694:                }
1.1.1.25  root     18695:                return(pio[c].data);
                   18696:        case 1:
1.1.1.37  root     18697:                {
                   18698:                        UINT8 stat = pio[c].stat;
                   18699:                        pio[c].stat |= 0x40; // clear ack
                   18700:                        return(stat);
                   18701:                }
1.1.1.25  root     18702:        case 2:
                   18703:                return(pio[c].ctrl);
                   18704:        }
                   18705:        return(0xff);
                   18706: }
                   18707: 
1.1.1.37  root     18708: void printer_out(int c, UINT8 data)
                   18709: {
                   18710:        SYSTEMTIME time;
1.1.1.38  root     18711:        bool jis_mode = false;
1.1.1.37  root     18712:        
                   18713:        GetLocalTime(&time);
                   18714:        
                   18715:        if(pio[c].fp != NULL) {
                   18716:                // if at least 1000ms passed from last written, close the current file
                   18717:                FILETIME ftime1;
                   18718:                FILETIME ftime2;
                   18719:                SystemTimeToFileTime(&pio[c].time, &ftime1);
                   18720:                SystemTimeToFileTime(&time, &ftime2);
                   18721:                INT64 *time1 = (INT64 *)&ftime1;
                   18722:                INT64 *time2 = (INT64 *)&ftime2;
                   18723:                INT64 msec = (*time2 - *time1) / 10000;
                   18724:                
                   18725:                if(msec >= 1000) {
1.1.1.38  root     18726:                        if(pio[c].jis_mode) {
                   18727:                                fputc(0x1c, pio[c].fp);
                   18728:                                fputc(0x2e, pio[c].fp);
                   18729:                                jis_mode = true;
                   18730:                        }
1.1.1.37  root     18731:                        fclose(pio[c].fp);
                   18732:                        pio[c].fp = NULL;
                   18733:                }
                   18734:        }
                   18735:        if(pio[c].fp == NULL) {
                   18736:                // create a new file in the temp folder
                   18737:                char file_name[MAX_PATH];
                   18738:                
                   18739:                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);
                   18740:                if(GetTempPath(MAX_PATH, pio[c].path)) {
                   18741:                        strcat(pio[c].path, file_name);
                   18742:                } else {
                   18743:                        strcpy(pio[c].path, file_name);
                   18744:                }
1.1.1.38  root     18745:                pio[c].fp = fopen(pio[c].path, "w+b");
1.1.1.37  root     18746:        }
                   18747:        if(pio[c].fp != NULL) {
1.1.1.38  root     18748:                if(jis_mode) {
                   18749:                        fputc(0x1c, pio[c].fp);
                   18750:                        fputc(0x26, pio[c].fp);
                   18751:                }
1.1.1.37  root     18752:                fputc(data, pio[c].fp);
1.1.1.38  root     18753:                
                   18754:                // reopen file if 1ch 26h 1ch 2eh (kanji-on  kanji-off) are written at the top
                   18755:                if(data == 0x2e && ftell(pio[c].fp) == 4) {
                   18756:                        UINT8 buffer[4];
                   18757:                        fseek(pio[c].fp, 0, SEEK_SET);
                   18758:                        fread(buffer, 4, 1, pio[c].fp);
                   18759:                        if(buffer[0] == 0x1c && buffer[1] == 0x26 && buffer[2] == 0x1c/* && buffer[3] == 0x2e*/) {
                   18760:                                fclose(pio[c].fp);
                   18761:                                pio[c].fp = fopen(pio[c].path, "w+b");
                   18762:                        }
                   18763:                }
1.1.1.37  root     18764:                pio[c].time = time;
                   18765:        }
                   18766: }
                   18767: 
1.1       root     18768: // pit
                   18769: 
1.1.1.22  root     18770: #define PIT_FREQ 1193182ULL
1.1       root     18771: #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)
                   18772: 
                   18773: void pit_init()
                   18774: {
1.1.1.8   root     18775:        memset(pit, 0, sizeof(pit));
1.1       root     18776:        for(int ch = 0; ch < 3; ch++) {
                   18777:                pit[ch].count = 0x10000;
                   18778:                pit[ch].ctrl_reg = 0x34;
                   18779:                pit[ch].mode = 3;
                   18780:        }
                   18781:        
                   18782:        // from bochs bios
                   18783:        pit_write(3, 0x34);
                   18784:        pit_write(0, 0x00);
                   18785:        pit_write(0, 0x00);
                   18786: }
                   18787: 
                   18788: void pit_write(int ch, UINT8 val)
                   18789: {
1.1.1.8   root     18790: #ifndef PIT_ALWAYS_RUNNING
1.1       root     18791:        if(!pit_active) {
                   18792:                pit_active = 1;
                   18793:                pit_init();
                   18794:        }
1.1.1.8   root     18795: #endif
1.1       root     18796:        switch(ch) {
                   18797:        case 0:
                   18798:        case 1:
                   18799:        case 2:
                   18800:                // write count register
                   18801:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   18802:                        if(pit[ch].ctrl_reg & 0x10) {
                   18803:                                pit[ch].low_write = 1;
                   18804:                        }
                   18805:                        if(pit[ch].ctrl_reg & 0x20) {
                   18806:                                pit[ch].high_write = 1;
                   18807:                        }
                   18808:                }
                   18809:                if(pit[ch].low_write) {
                   18810:                        pit[ch].count_reg = val;
                   18811:                        pit[ch].low_write = 0;
                   18812:                } else if(pit[ch].high_write) {
                   18813:                        if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   18814:                                pit[ch].count_reg = val << 8;
                   18815:                        } else {
                   18816:                                pit[ch].count_reg |= val << 8;
                   18817:                        }
                   18818:                        pit[ch].high_write = 0;
                   18819:                }
                   18820:                // start count
1.1.1.8   root     18821:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   18822:                        if(pit[ch].mode == 0 || pit[ch].mode == 4 || pit[ch].prev_time == 0) {
                   18823:                                pit[ch].count = PIT_COUNT_VALUE(ch);
                   18824:                                pit[ch].prev_time = timeGetTime();
                   18825:                                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     18826:                        }
                   18827:                }
                   18828:                break;
                   18829:        case 3: // ctrl reg
                   18830:                if((val & 0xc0) == 0xc0) {
                   18831:                        // i8254 read-back command
                   18832:                        for(ch = 0; ch < 3; ch++) {
                   18833:                                if(!(val & 0x10) && !pit[ch].status_latched) {
                   18834:                                        pit[ch].status = pit[ch].ctrl_reg & 0x3f;
                   18835:                                        pit[ch].status_latched = 1;
                   18836:                                }
                   18837:                                if(!(val & 0x20) && !pit[ch].count_latched) {
                   18838:                                        pit_latch_count(ch);
                   18839:                                }
                   18840:                        }
                   18841:                        break;
                   18842:                }
                   18843:                ch = (val >> 6) & 3;
                   18844:                if(val & 0x30) {
1.1.1.35  root     18845:                        static const int modes[8] = {0, 1, 2, 3, 4, 5, 2, 3};
1.1       root     18846:                        pit[ch].mode = modes[(val >> 1) & 7];
                   18847:                        pit[ch].count_latched = 0;
                   18848:                        pit[ch].low_read = pit[ch].high_read = 0;
                   18849:                        pit[ch].low_write = pit[ch].high_write = 0;
                   18850:                        pit[ch].ctrl_reg = val;
                   18851:                        // stop count
1.1.1.8   root     18852:                        pit[ch].prev_time = pit[ch].expired_time = 0;
1.1       root     18853:                        pit[ch].count_reg = 0;
                   18854:                } else if(!pit[ch].count_latched) {
                   18855:                        pit_latch_count(ch);
                   18856:                }
                   18857:                break;
                   18858:        }
                   18859: }
                   18860: 
                   18861: UINT8 pit_read(int ch)
                   18862: {
1.1.1.8   root     18863: #ifndef PIT_ALWAYS_RUNNING
1.1       root     18864:        if(!pit_active) {
                   18865:                pit_active = 1;
                   18866:                pit_init();
                   18867:        }
1.1.1.8   root     18868: #endif
1.1       root     18869:        switch(ch) {
                   18870:        case 0:
                   18871:        case 1:
                   18872:        case 2:
                   18873:                if(pit[ch].status_latched) {
                   18874:                        pit[ch].status_latched = 0;
                   18875:                        return(pit[ch].status);
                   18876:                }
                   18877:                // if not latched, through current count
                   18878:                if(!pit[ch].count_latched) {
                   18879:                        if(!pit[ch].low_read && !pit[ch].high_read) {
                   18880:                                pit_latch_count(ch);
                   18881:                        }
                   18882:                }
                   18883:                // return latched count
                   18884:                if(pit[ch].low_read) {
                   18885:                        pit[ch].low_read = 0;
                   18886:                        if(!pit[ch].high_read) {
                   18887:                                pit[ch].count_latched = 0;
                   18888:                        }
                   18889:                        return(pit[ch].latch & 0xff);
                   18890:                } else if(pit[ch].high_read) {
                   18891:                        pit[ch].high_read = 0;
                   18892:                        pit[ch].count_latched = 0;
                   18893:                        return((pit[ch].latch >> 8) & 0xff);
                   18894:                }
                   18895:        }
                   18896:        return(0xff);
                   18897: }
                   18898: 
1.1.1.8   root     18899: int pit_run(int ch, UINT32 cur_time)
1.1       root     18900: {
1.1.1.8   root     18901:        if(pit[ch].expired_time != 0 && cur_time >= pit[ch].expired_time) {
1.1       root     18902:                pit[ch].count = PIT_COUNT_VALUE(ch);
1.1.1.8   root     18903:                pit[ch].prev_time = pit[ch].expired_time;
                   18904:                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
                   18905:                if(cur_time >= pit[ch].expired_time) {
                   18906:                        pit[ch].prev_time = cur_time;
                   18907:                        pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     18908:                }
1.1.1.8   root     18909:                return(1);
1.1       root     18910:        }
1.1.1.8   root     18911:        return(0);
1.1       root     18912: }
                   18913: 
                   18914: void pit_latch_count(int ch)
                   18915: {
1.1.1.8   root     18916:        if(pit[ch].expired_time != 0) {
1.1.1.26  root     18917:                UINT32 cur_time = timeGetTime();
1.1.1.8   root     18918:                pit_run(ch, cur_time);
                   18919:                UINT32 tmp = (pit[ch].count * (pit[ch].expired_time - cur_time)) / (pit[ch].expired_time - pit[ch].prev_time);
1.1.1.26  root     18920:                UINT16 latch = (tmp != 0) ? (UINT16)tmp : 1;
                   18921:                
                   18922:                if(pit[ch].prev_latch == latch && pit[ch].expired_time > cur_time) {
                   18923:                        // decrement counter in 1msec period
                   18924:                        if(pit[ch].next_latch == 0) {
                   18925:                                tmp = (pit[ch].count * (pit[ch].expired_time - cur_time - 1)) / (pit[ch].expired_time - pit[ch].prev_time);
                   18926:                                pit[ch].next_latch = (tmp != 0) ? (UINT16)tmp : 1;
                   18927:                        }
                   18928:                        if(pit[ch].latch > pit[ch].next_latch) {
                   18929:                                pit[ch].latch--;
                   18930:                        }
                   18931:                } else {
                   18932:                        pit[ch].prev_latch = pit[ch].latch = latch;
                   18933:                        pit[ch].next_latch = 0;
                   18934:                }
1.1.1.8   root     18935:        } else {
                   18936:                pit[ch].latch = (UINT16)pit[ch].count;
1.1.1.26  root     18937:                pit[ch].prev_latch = pit[ch].next_latch = 0;
1.1       root     18938:        }
                   18939:        pit[ch].count_latched = 1;
                   18940:        if((pit[ch].ctrl_reg & 0x30) == 0x10) {
                   18941:                // lower byte
                   18942:                pit[ch].low_read = 1;
                   18943:                pit[ch].high_read = 0;
                   18944:        } else if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   18945:                // upper byte
                   18946:                pit[ch].low_read = 0;
                   18947:                pit[ch].high_read = 1;
                   18948:        } else {
                   18949:                // lower -> upper
1.1.1.14  root     18950:                pit[ch].low_read = pit[ch].high_read = 1;
1.1       root     18951:        }
                   18952: }
                   18953: 
1.1.1.8   root     18954: int pit_get_expired_time(int ch)
1.1       root     18955: {
1.1.1.22  root     18956:        pit[ch].accum += 1024ULL * 1000ULL * (UINT64)pit[ch].count / PIT_FREQ;
                   18957:        UINT64 val = pit[ch].accum >> 10;
                   18958:        pit[ch].accum -= val << 10;
                   18959:        return((val != 0) ? val : 1);
1.1.1.8   root     18960: }
                   18961: 
1.1.1.25  root     18962: // sio
                   18963: 
                   18964: void sio_init()
                   18965: {
1.1.1.26  root     18966:        memset(sio, 0, sizeof(sio));
                   18967:        memset(sio_mt, 0, sizeof(sio_mt));
                   18968:        
1.1.1.29  root     18969:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     18970:                sio[c].send_buffer = new FIFO(SIO_BUFFER_SIZE);
                   18971:                sio[c].recv_buffer = new FIFO(SIO_BUFFER_SIZE);
                   18972:                
                   18973:                sio[c].divisor.w = 12;          // 115200Hz / 9600Baud
                   18974:                sio[c].line_ctrl = 0x03;        // 8bit, stop 1bit, non parity
1.1.1.26  root     18975:                sio[c].modem_ctrl = 0x03;       // rts=on, dtr=on
                   18976:                sio[c].set_rts = sio[c].set_dtr = true;
1.1.1.25  root     18977:                sio[c].line_stat_buf = 0x60;    // send/recv buffers are empty
                   18978:                sio[c].irq_identify = 0x01;     // no pending irq
                   18979:                
                   18980:                InitializeCriticalSection(&sio_mt[c].csSendData);
                   18981:                InitializeCriticalSection(&sio_mt[c].csRecvData);
                   18982:                InitializeCriticalSection(&sio_mt[c].csLineCtrl);
                   18983:                InitializeCriticalSection(&sio_mt[c].csLineStat);
                   18984:                InitializeCriticalSection(&sio_mt[c].csModemCtrl);
                   18985:                InitializeCriticalSection(&sio_mt[c].csModemStat);
                   18986:                
1.1.1.26  root     18987:                if(sio_port_number[c] != 0) {
1.1.1.25  root     18988:                        sio[c].channel = c;
                   18989:                        sio_mt[c].hThread = CreateThread(NULL, 0, sio_thread, &sio[c], 0, NULL);
                   18990:                }
                   18991:        }
                   18992: }
                   18993: 
                   18994: void sio_finish()
                   18995: {
1.1.1.29  root     18996:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     18997:                if(sio_mt[c].hThread != NULL) {
                   18998:                        WaitForSingleObject(sio_mt[c].hThread, INFINITE);
                   18999:                        CloseHandle(sio_mt[c].hThread);
1.1.1.28  root     19000:                        sio_mt[c].hThread = NULL;
1.1.1.25  root     19001:                }
                   19002:                DeleteCriticalSection(&sio_mt[c].csSendData);
                   19003:                DeleteCriticalSection(&sio_mt[c].csRecvData);
                   19004:                DeleteCriticalSection(&sio_mt[c].csLineCtrl);
                   19005:                DeleteCriticalSection(&sio_mt[c].csLineStat);
                   19006:                DeleteCriticalSection(&sio_mt[c].csModemCtrl);
                   19007:                DeleteCriticalSection(&sio_mt[c].csModemStat);
1.1.1.28  root     19008:        }
                   19009:        sio_release();
                   19010: }
                   19011: 
                   19012: void sio_release()
                   19013: {
1.1.1.29  root     19014:        for(int c = 0; c < 4; c++) {
1.1.1.28  root     19015:                // sio_thread() may access the resources :-(
1.1.1.32  root     19016:                bool running = (sio_mt[c].hThread != NULL);
                   19017:                
                   19018:                if(running) {
                   19019:                        EnterCriticalSection(&sio_mt[c].csSendData);
                   19020:                }
                   19021:                if(sio[c].send_buffer != NULL) {
                   19022:                        sio[c].send_buffer->release();
                   19023:                        delete sio[c].send_buffer;
                   19024:                        sio[c].send_buffer = NULL;
                   19025:                }
                   19026:                if(running) {
                   19027:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   19028:                        EnterCriticalSection(&sio_mt[c].csRecvData);
                   19029:                }
                   19030:                if(sio[c].recv_buffer != NULL) {
                   19031:                        sio[c].recv_buffer->release();
                   19032:                        delete sio[c].recv_buffer;
                   19033:                        sio[c].recv_buffer = NULL;
                   19034:                }
                   19035:                if(running) {
                   19036:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
1.1.1.28  root     19037:                }
1.1.1.25  root     19038:        }
                   19039: }
                   19040: 
                   19041: void sio_write(int c, UINT32 addr, UINT8 data)
                   19042: {
                   19043:        switch(addr & 7) {
                   19044:        case 0:
                   19045:                if(sio[c].selector & 0x80) {
                   19046:                        if(sio[c].divisor.b.l != data) {
                   19047:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   19048:                                sio[c].divisor.b.l = data;
                   19049:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   19050:                        }
                   19051:                } else {
                   19052:                        EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     19053:                        if(sio[c].send_buffer != NULL) {
                   19054:                                sio[c].send_buffer->write(data);
                   19055:                        }
1.1.1.25  root     19056:                        // transmitter holding/shift registers are not empty
                   19057:                        sio[c].line_stat_buf &= ~0x60;
                   19058:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   19059:                        
                   19060:                        if(sio[c].irq_enable & 0x02) {
                   19061:                                sio_update_irq(c);
                   19062:                        }
                   19063:                }
                   19064:                break;
                   19065:        case 1:
                   19066:                if(sio[c].selector & 0x80) {
                   19067:                        if(sio[c].divisor.b.h != data) {
                   19068:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   19069:                                sio[c].divisor.b.h = data;
                   19070:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   19071:                        }
                   19072:                } else {
                   19073:                        if(sio[c].irq_enable != data) {
                   19074:                                sio[c].irq_enable = data;
                   19075:                                sio_update_irq(c);
                   19076:                        }
                   19077:                }
                   19078:                break;
                   19079:        case 3:
                   19080:                {
                   19081:                        UINT8 line_ctrl = data & 0x3f;
                   19082:                        bool set_brk = ((data & 0x40) != 0);
                   19083:                        
                   19084:                        if(sio[c].line_ctrl != line_ctrl) {
                   19085:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   19086:                                sio[c].line_ctrl = line_ctrl;
                   19087:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   19088:                        }
                   19089:                        if(sio[c].set_brk != set_brk) {
                   19090:                                EnterCriticalSection(&sio_mt[c].csModemCtrl);
                   19091:                                sio[c].set_brk = set_brk;
                   19092:                                LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   19093:                        }
                   19094:                }
                   19095:                sio[c].selector = data;
                   19096:                break;
                   19097:        case 4:
                   19098:                {
                   19099:                        bool set_dtr = ((data & 0x01) != 0);
                   19100:                        bool set_rts = ((data & 0x02) != 0);
                   19101:                        
                   19102:                        if(sio[c].set_dtr != set_dtr || sio[c].set_rts != set_rts) {
1.1.1.26  root     19103: //                             EnterCriticalSection(&sio_mt[c].csModemCtrl);
1.1.1.25  root     19104:                                sio[c].set_dtr = set_dtr;
                   19105:                                sio[c].set_rts = set_rts;
1.1.1.26  root     19106: //                             LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   19107:                                
                   19108:                                bool state_changed = false;
                   19109:                                
                   19110:                                EnterCriticalSection(&sio_mt[c].csModemStat);
                   19111:                                if(set_dtr) {
                   19112:                                        sio[c].modem_stat |= 0x20;      // dsr on
                   19113:                                } else {
                   19114:                                        sio[c].modem_stat &= ~0x20;     // dsr off
                   19115:                                }
                   19116:                                if(set_rts) {
                   19117:                                        sio[c].modem_stat |= 0x10;      // cts on
                   19118:                                } else {
                   19119:                                        sio[c].modem_stat &= ~0x10;     // cts off
                   19120:                                }
                   19121:                                if((sio[c].prev_modem_stat & 0x20) != (sio[c].modem_stat & 0x20)) {
                   19122:                                        if(!(sio[c].modem_stat & 0x02)) {
                   19123:                                                if(sio[c].irq_enable & 0x08) {
                   19124:                                                        state_changed = true;
                   19125:                                                }
                   19126:                                                sio[c].modem_stat |= 0x02;
                   19127:                                        }
                   19128:                                }
                   19129:                                if((sio[c].prev_modem_stat & 0x10) != (sio[c].modem_stat & 0x10)) {
                   19130:                                        if(!(sio[c].modem_stat & 0x01)) {
                   19131:                                                if(sio[c].irq_enable & 0x08) {
                   19132:                                                        state_changed = true;
                   19133:                                                }
                   19134:                                                sio[c].modem_stat |= 0x01;
                   19135:                                        }
                   19136:                                }
                   19137:                                LeaveCriticalSection(&sio_mt[c].csModemStat);
                   19138:                                
                   19139:                                if(state_changed) {
                   19140:                                        sio_update_irq(c);
                   19141:                                }
1.1.1.25  root     19142:                        }
                   19143:                }
                   19144:                sio[c].modem_ctrl = data;
                   19145:                break;
                   19146:        case 7:
                   19147:                sio[c].scratch = data;
                   19148:                break;
                   19149:        }
                   19150: }
                   19151: 
                   19152: UINT8 sio_read(int c, UINT32 addr)
                   19153: {
                   19154:        switch(addr & 7) {
                   19155:        case 0:
                   19156:                if(sio[c].selector & 0x80) {
                   19157:                        return(sio[c].divisor.b.l);
                   19158:                } else {
                   19159:                        EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     19160:                        UINT8 data = 0;
                   19161:                        if(sio[c].recv_buffer != NULL) {
                   19162:                                data = sio[c].recv_buffer->read();
                   19163:                        }
1.1.1.25  root     19164:                        // data is not ready
                   19165:                        sio[c].line_stat_buf &= ~0x01;
                   19166:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
                   19167:                        
                   19168:                        if(sio[c].irq_enable & 0x01) {
                   19169:                                sio_update_irq(c);
                   19170:                        }
                   19171:                        return(data);
                   19172:                }
                   19173:        case 1:
                   19174:                if(sio[c].selector & 0x80) {
                   19175:                        return(sio[c].divisor.b.h);
                   19176:                } else {
                   19177:                        return(sio[c].irq_enable);
                   19178:                }
                   19179:        case 2:
                   19180:                return(sio[c].irq_identify);
                   19181:        case 3:
                   19182:                return(sio[c].selector);
                   19183:        case 4:
                   19184:                return(sio[c].modem_ctrl);
                   19185:        case 5:
                   19186:                {
                   19187:                        EnterCriticalSection(&sio_mt[c].csLineStat);
                   19188:                        UINT8 val = sio[c].line_stat_err | sio[c].line_stat_buf;
                   19189:                        sio[c].line_stat_err = 0x00;
                   19190:                        LeaveCriticalSection(&sio_mt[c].csLineStat);
                   19191:                        
                   19192:                        bool state_changed = false;
                   19193:                        
                   19194:                        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   19195:                                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     19196:                                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     19197:                                        // transmitter holding register will be empty first
                   19198:                                        if(sio[c].irq_enable & 0x02) {
                   19199:                                                state_changed = true;
                   19200:                                        }
                   19201:                                        sio[c].line_stat_buf |= 0x20;
                   19202:                                }
                   19203:                                LeaveCriticalSection(&sio_mt[c].csSendData);
                   19204:                        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   19205:                                // transmitter shift register will be empty later
                   19206:                                sio[c].line_stat_buf |= 0x40;
                   19207:                        }
                   19208:                        if(!(sio[c].line_stat_buf & 0x01)) {
                   19209:                                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     19210:                                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     19211:                                        // data is ready
                   19212:                                        if(sio[c].irq_enable & 0x01) {
                   19213:                                                state_changed = true;
                   19214:                                        }
                   19215:                                        sio[c].line_stat_buf |= 0x01;
                   19216:                                }
                   19217:                                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   19218:                        }
                   19219:                        if(state_changed) {
                   19220:                                sio_update_irq(c);
                   19221:                        }
                   19222:                        return(val);
                   19223:                }
                   19224:        case 6:
                   19225:                {
                   19226:                        EnterCriticalSection(&sio_mt[c].csModemStat);
                   19227:                        UINT8 val = sio[c].modem_stat;
                   19228:                        sio[c].modem_stat &= 0xf0;
                   19229:                        sio[c].prev_modem_stat = sio[c].modem_stat;
                   19230:                        LeaveCriticalSection(&sio_mt[c].csModemStat);
                   19231:                        
                   19232:                        if(sio[c].modem_ctrl & 0x10) {
                   19233:                                // loop-back
                   19234:                                val &= 0x0f;
                   19235:                                val |= (sio[c].modem_ctrl & 0x0c) << 4;
                   19236:                                val |= (sio[c].modem_ctrl & 0x01) << 5;
                   19237:                                val |= (sio[c].modem_ctrl & 0x02) << 3;
                   19238:                        }
                   19239:                        return(val);
                   19240:                }
                   19241:        case 7:
                   19242:                return(sio[c].scratch);
                   19243:        }
                   19244:        return(0xff);
                   19245: }
                   19246: 
                   19247: void sio_update(int c)
                   19248: {
                   19249:        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   19250:                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     19251:                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     19252:                        // transmitter holding/shift registers will be empty
                   19253:                        sio[c].line_stat_buf |= 0x60;
                   19254:                }
                   19255:                LeaveCriticalSection(&sio_mt[c].csSendData);
                   19256:        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   19257:                // transmitter shift register will be empty
                   19258:                sio[c].line_stat_buf |= 0x40;
                   19259:        }
                   19260:        if(!(sio[c].line_stat_buf & 0x01)) {
                   19261:                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     19262:                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     19263:                        // data is ready
                   19264:                        sio[c].line_stat_buf |= 0x01;
                   19265:                }
                   19266:                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   19267:        }
                   19268:        sio_update_irq(c);
                   19269: }
                   19270: 
                   19271: void sio_update_irq(int c)
                   19272: {
                   19273:        int level = -1;
                   19274:        
                   19275:        if(sio[c].irq_enable & 0x08) {
                   19276:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   19277:                if((sio[c].modem_stat & 0x0f) != 0) {
                   19278:                        level = 0;
                   19279:                }
                   19280:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   19281:        }
                   19282:        if(sio[c].irq_enable & 0x02) {
                   19283:                if(sio[c].line_stat_buf & 0x20) {
                   19284:                        level = 1;
                   19285:                }
                   19286:        }
                   19287:        if(sio[c].irq_enable & 0x01) {
                   19288:                if(sio[c].line_stat_buf & 0x01) {
                   19289:                        level = 2;
                   19290:                }
                   19291:        }
                   19292:        if(sio[c].irq_enable & 0x04) {
                   19293:                EnterCriticalSection(&sio_mt[c].csLineStat);
                   19294:                if(sio[c].line_stat_err != 0) {
                   19295:                        level = 3;
                   19296:                }
                   19297:                LeaveCriticalSection(&sio_mt[c].csLineStat);
                   19298:        }
1.1.1.29  root     19299:        
                   19300:        // COM1 and COM3 shares IRQ4, COM2 and COM4 shares IRQ3
1.1.1.25  root     19301:        if(level != -1) {
                   19302:                sio[c].irq_identify = level << 1;
1.1.1.29  root     19303:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 1);
1.1.1.25  root     19304:        } else {
                   19305:                sio[c].irq_identify = 1;
1.1.1.29  root     19306:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 0);
1.1.1.25  root     19307:        }
                   19308: }
                   19309: 
                   19310: DWORD WINAPI sio_thread(void *lpx)
                   19311: {
                   19312:        volatile sio_t *p = (sio_t *)lpx;
                   19313:        sio_mt_t *q = &sio_mt[p->channel];
                   19314:        
                   19315:        char name[] = "COM1";
1.1.1.26  root     19316:        name[3] = '0' + sio_port_number[p->channel];
                   19317:        HANDLE hComm = NULL;
                   19318:        COMMPROP commProp;
                   19319:        DCB dcb;
                   19320:        DWORD dwSettableBaud = 0xffb; // 75, 110, 150, 300, 600, 1200, 1800, 2400, 4800, 7200, and 9600bps
                   19321:        BYTE bytBuffer[SIO_BUFFER_SIZE];
                   19322:        
                   19323:        if((hComm = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL)) != INVALID_HANDLE_VALUE) {
                   19324:                if(GetCommProperties(hComm, &commProp)) {
                   19325:                        dwSettableBaud = commProp.dwSettableBaud;
                   19326:                }
1.1.1.25  root     19327:                EscapeCommFunction(hComm, CLRBREAK);
1.1.1.26  root     19328: //             EscapeCommFunction(hComm, SETRTS);
                   19329: //             EscapeCommFunction(hComm, SETDTR);
1.1.1.25  root     19330:                
                   19331:                while(!m_halted) {
                   19332:                        // setup comm port
                   19333:                        bool comm_state_changed = false;
                   19334:                        
                   19335:                        EnterCriticalSection(&q->csLineCtrl);
                   19336:                        if((p->prev_divisor != p->divisor.w || p->prev_line_ctrl != p->line_ctrl) && p->divisor.w != 0) {
                   19337:                                p->prev_divisor = p->divisor.w;
                   19338:                                p->prev_line_ctrl = p->line_ctrl;
                   19339:                                comm_state_changed = true;
                   19340:                        }
                   19341:                        LeaveCriticalSection(&q->csLineCtrl);
                   19342:                        
                   19343:                        if(comm_state_changed) {
1.1.1.26  root     19344:                                if(GetCommState(hComm, &dcb)) {
                   19345: //                                     dcb.BaudRate = min(9600, 115200 / p->prev_divisor);
                   19346:                                        DWORD baud = 115200 / p->prev_divisor;
                   19347:                                        dcb.BaudRate = 9600; // default
                   19348:                                        
                   19349:                                        if((dwSettableBaud & BAUD_075  ) && baud >= 75   ) dcb.BaudRate = 75;
                   19350:                                        if((dwSettableBaud & BAUD_110  ) && baud >= 110  ) dcb.BaudRate = 110;
                   19351:                                        // 134.5bps is not supported ???
                   19352: //                                     if((dwSettableBaud & BAUD_134_5) && baud >= 134.5) dcb.BaudRate = 134.5;
                   19353:                                        if((dwSettableBaud & BAUD_150  ) && baud >= 150  ) dcb.BaudRate = 150;
                   19354:                                        if((dwSettableBaud & BAUD_300  ) && baud >= 300  ) dcb.BaudRate = 300;
                   19355:                                        if((dwSettableBaud & BAUD_600  ) && baud >= 600  ) dcb.BaudRate = 600;
                   19356:                                        if((dwSettableBaud & BAUD_1200 ) && baud >= 1200 ) dcb.BaudRate = 1200;
                   19357:                                        if((dwSettableBaud & BAUD_1800 ) && baud >= 1800 ) dcb.BaudRate = 1800;
                   19358:                                        if((dwSettableBaud & BAUD_2400 ) && baud >= 2400 ) dcb.BaudRate = 2400;
                   19359:                                        if((dwSettableBaud & BAUD_4800 ) && baud >= 4800 ) dcb.BaudRate = 4800;
                   19360:                                        if((dwSettableBaud & BAUD_7200 ) && baud >= 7200 ) dcb.BaudRate = 7200;
                   19361:                                        if((dwSettableBaud & BAUD_9600 ) && baud >= 9600 ) dcb.BaudRate = 9600;
                   19362: //                                     if((dwSettableBaud & BAUD_14400) && baud >= 14400) dcb.BaudRate = 14400;
                   19363: //                                     if((dwSettableBaud & BAUD_19200) && baud >= 19200) dcb.BaudRate = 19200;
                   19364: //                                     if((dwSettableBaud & BAUD_38400) && baud >= 38400) dcb.BaudRate = 38400;
                   19365:                                        
                   19366:                                        switch(p->prev_line_ctrl & 0x03) {
                   19367:                                        case 0x00: dcb.ByteSize = 5; break;
                   19368:                                        case 0x01: dcb.ByteSize = 6; break;
                   19369:                                        case 0x02: dcb.ByteSize = 7; break;
                   19370:                                        case 0x03: dcb.ByteSize = 8; break;
                   19371:                                        }
                   19372:                                        switch(p->prev_line_ctrl & 0x04) {
                   19373:                                        case 0x00: dcb.StopBits = ONESTOPBIT; break;
                   19374:                                        case 0x04: dcb.StopBits = (dcb.ByteSize == 5) ? ONE5STOPBITS : TWOSTOPBITS; break;
                   19375:                                        }
                   19376:                                        switch(p->prev_line_ctrl & 0x38) {
                   19377:                                        case 0x08: dcb.Parity = ODDPARITY;   break;
                   19378:                                        case 0x18: dcb.Parity = EVENPARITY;  break;
                   19379:                                        case 0x28: dcb.Parity = MARKPARITY;  break;
                   19380:                                        case 0x38: dcb.Parity = SPACEPARITY; break;
                   19381:                                        default:   dcb.Parity = NOPARITY;    break;
                   19382:                                        }
                   19383:                                        dcb.fBinary = TRUE;
                   19384:                                        dcb.fParity = (dcb.Parity != NOPARITY);
                   19385:                                        dcb.fOutxCtsFlow = dcb.fOutxDsrFlow = TRUE;
                   19386:                                        dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
                   19387:                                        dcb.fDsrSensitivity = FALSE;//TRUE;
                   19388:                                        dcb.fTXContinueOnXoff = TRUE;
                   19389:                                        dcb.fOutX = dcb.fInX = FALSE;
                   19390:                                        dcb.fErrorChar = FALSE;
                   19391:                                        dcb.fNull = FALSE;
                   19392:                                        dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
                   19393:                                        dcb.fAbortOnError = FALSE;
                   19394:                                        
                   19395:                                        SetCommState(hComm, &dcb);
1.1.1.25  root     19396:                                }
                   19397:                                
                   19398:                                // check again to apply all comm state changes
                   19399:                                Sleep(10);
                   19400:                                continue;
                   19401:                        }
                   19402:                        
                   19403:                        // set comm pins
                   19404:                        bool change_brk = false;
1.1.1.26  root     19405: //                     bool change_rts = false;
                   19406: //                     bool change_dtr = false;
1.1.1.25  root     19407:                        
                   19408:                        EnterCriticalSection(&q->csModemCtrl);
                   19409:                        if(p->prev_set_brk != p->set_brk) {
                   19410:                                p->prev_set_brk = p->set_brk;
                   19411:                                change_brk = true;
                   19412:                        }
1.1.1.26  root     19413: //                     if(p->prev_set_rts != p->set_rts) {
                   19414: //                             p->prev_set_rts = p->set_rts;
                   19415: //                             change_rts = true;
                   19416: //                     }
                   19417: //                     if(p->prev_set_dtr != p->set_dtr) {
                   19418: //                             p->prev_set_dtr = p->set_dtr;
                   19419: //                             change_dtr = true;
                   19420: //                     }
1.1.1.25  root     19421:                        LeaveCriticalSection(&q->csModemCtrl);
                   19422:                        
                   19423:                        if(change_brk) {
1.1.1.26  root     19424:                                static UINT32 clear_time = 0;
                   19425:                                if(p->prev_set_brk) {
                   19426:                                        EscapeCommFunction(hComm, SETBREAK);
                   19427:                                        clear_time = timeGetTime() + 200;
                   19428:                                } else {
                   19429:                                        // keep break for at least 200msec
                   19430:                                        UINT32 cur_time = timeGetTime();
                   19431:                                        if(clear_time > cur_time) {
                   19432:                                                Sleep(clear_time - cur_time);
                   19433:                                        }
                   19434:                                        EscapeCommFunction(hComm, CLRBREAK);
                   19435:                                }
1.1.1.25  root     19436:                        }
1.1.1.26  root     19437: //                     if(change_rts) {
                   19438: //                             if(p->prev_set_rts) {
                   19439: //                                     EscapeCommFunction(hComm, SETRTS);
                   19440: //                             } else {
                   19441: //                                     EscapeCommFunction(hComm, CLRRTS);
                   19442: //                             }
                   19443: //                     }
                   19444: //                     if(change_dtr) {
                   19445: //                             if(p->prev_set_dtr) {
                   19446: //                                     EscapeCommFunction(hComm, SETDTR);
                   19447: //                             } else {
                   19448: //                                     EscapeCommFunction(hComm, CLRDTR);
                   19449: //                             }
                   19450: //                     }
1.1.1.25  root     19451:                        
                   19452:                        // get comm pins
                   19453:                        DWORD dwModemStat = 0;
                   19454:                        
                   19455:                        if(GetCommModemStatus(hComm, &dwModemStat)) {
                   19456:                                EnterCriticalSection(&q->csModemStat);
                   19457:                                if(dwModemStat & MS_RLSD_ON) {
                   19458:                                        p->modem_stat |= 0x80;
                   19459:                                } else {
                   19460:                                        p->modem_stat &= ~0x80;
                   19461:                                }
                   19462:                                if(dwModemStat & MS_RING_ON) {
                   19463:                                        p->modem_stat |= 0x40;
                   19464:                                } else {
                   19465:                                        p->modem_stat &= ~0x40;
                   19466:                                }
1.1.1.26  root     19467: //                             if(dwModemStat & MS_DSR_ON) {
                   19468: //                                     p->modem_stat |= 0x20;
                   19469: //                             } else {
                   19470: //                                     p->modem_stat &= ~0x20;
                   19471: //                             }
                   19472: //                             if(dwModemStat & MS_CTS_ON) {
                   19473: //                                     p->modem_stat |= 0x10;
                   19474: //                             } else {
                   19475: //                                     p->modem_stat &= ~0x10;
                   19476: //                             }
1.1.1.25  root     19477:                                if((p->prev_modem_stat & 0x80) != (p->modem_stat & 0x80)) {
                   19478:                                        p->modem_stat |= 0x08;
                   19479:                                }
                   19480:                                if((p->prev_modem_stat & 0x40) && !(p->modem_stat & 0x40)) {
                   19481:                                        p->modem_stat |= 0x04;
                   19482:                                }
1.1.1.26  root     19483: //                             if((p->prev_modem_stat & 0x20) != (p->modem_stat & 0x20)) {
                   19484: //                                     p->modem_stat |= 0x02;
                   19485: //                             }
                   19486: //                             if((p->prev_modem_stat & 0x10) != (p->modem_stat & 0x10)) {
                   19487: //                                     p->modem_stat |= 0x01;
                   19488: //                             }
1.1.1.25  root     19489:                                LeaveCriticalSection(&q->csModemStat);
                   19490:                        }
                   19491:                        
                   19492:                        // send data
                   19493:                        DWORD dwSend = 0;
                   19494:                        
                   19495:                        EnterCriticalSection(&q->csSendData);
1.1.1.32  root     19496:                        while(p->send_buffer != NULL && !p->send_buffer->empty()) {
1.1.1.25  root     19497:                                bytBuffer[dwSend++] = p->send_buffer->read();
                   19498:                        }
                   19499:                        LeaveCriticalSection(&q->csSendData);
                   19500:                        
                   19501:                        if(dwSend != 0) {
                   19502:                                DWORD dwWritten = 0;
                   19503:                                WriteFile(hComm, bytBuffer, dwSend, &dwWritten, NULL);
                   19504:                        }
                   19505:                        
                   19506:                        // get line status and recv data
                   19507:                        DWORD dwLineStat = 0;
                   19508:                        COMSTAT comStat;
                   19509:                        
                   19510:                        if(ClearCommError(hComm, &dwLineStat, &comStat)) {
                   19511:                                EnterCriticalSection(&q->csLineStat);
                   19512:                                if(dwLineStat & CE_BREAK) {
                   19513:                                        p->line_stat_err |= 0x10;
                   19514:                                }
                   19515:                                if(dwLineStat & CE_FRAME) {
                   19516:                                        p->line_stat_err |= 0x08;
                   19517:                                }
                   19518:                                if(dwLineStat & CE_RXPARITY) {
                   19519:                                        p->line_stat_err |= 0x04;
                   19520:                                }
                   19521:                                if(dwLineStat & CE_OVERRUN) {
                   19522:                                        p->line_stat_err |= 0x02;
                   19523:                                }
                   19524:                                LeaveCriticalSection(&q->csLineStat);
                   19525:                                
                   19526:                                if(comStat.cbInQue != 0) {
                   19527:                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     19528:                                        DWORD dwRecv = 0;
                   19529:                                        if(p->recv_buffer != NULL) {
                   19530:                                                dwRecv = min(comStat.cbInQue, p->recv_buffer->remain());
                   19531:                                        }
1.1.1.25  root     19532:                                        LeaveCriticalSection(&q->csRecvData);
                   19533:                                        
                   19534:                                        if(dwRecv != 0) {
                   19535:                                                DWORD dwRead = 0;
                   19536:                                                if(ReadFile(hComm, bytBuffer, dwRecv, &dwRead, NULL) && dwRead != 0) {
                   19537:                                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     19538:                                                        if(p->recv_buffer != NULL) {
                   19539:                                                                for(int i = 0; i < dwRead; i++) {
                   19540:                                                                        p->recv_buffer->write(bytBuffer[i]);
                   19541:                                                                }
1.1.1.25  root     19542:                                                        }
                   19543:                                                        LeaveCriticalSection(&q->csRecvData);
                   19544:                                                }
                   19545:                                        }
                   19546:                                }
                   19547:                        }
                   19548:                        Sleep(10);
                   19549:                }
                   19550:                CloseHandle(hComm);
                   19551:        }
                   19552:        return 0;
                   19553: }
                   19554: 
1.1.1.8   root     19555: // cmos
                   19556: 
                   19557: void cmos_init()
                   19558: {
                   19559:        memset(cmos, 0, sizeof(cmos));
                   19560:        cmos_addr = 0;
1.1       root     19561:        
1.1.1.8   root     19562:        // from DOSBox
                   19563:        cmos_write(0x0a, 0x26);
                   19564:        cmos_write(0x0b, 0x02);
                   19565:        cmos_write(0x0d, 0x80);
1.1       root     19566: }
                   19567: 
1.1.1.8   root     19568: void cmos_write(int addr, UINT8 val)
1.1       root     19569: {
1.1.1.8   root     19570:        cmos[addr & 0x7f] = val;
                   19571: }
                   19572: 
                   19573: #define CMOS_GET_TIME() { \
                   19574:        UINT32 cur_sec = timeGetTime() / 1000 ; \
                   19575:        if(prev_sec != cur_sec) { \
                   19576:                GetLocalTime(&time); \
                   19577:                prev_sec = cur_sec; \
                   19578:        } \
1.1       root     19579: }
1.1.1.8   root     19580: #define CMOS_BCD(v) ((cmos[0x0b] & 4) ? (v) : to_bcd(v))
1.1       root     19581: 
1.1.1.8   root     19582: UINT8 cmos_read(int addr)
1.1       root     19583: {
1.1.1.8   root     19584:        static SYSTEMTIME time;
                   19585:        static UINT32 prev_sec = 0;
1.1       root     19586:        
1.1.1.8   root     19587:        switch(addr & 0x7f) {
                   19588:        case 0x00: CMOS_GET_TIME(); return(CMOS_BCD(time.wSecond));
                   19589:        case 0x02: CMOS_GET_TIME(); return(CMOS_BCD(time.wMinute));
                   19590:        case 0x04: CMOS_GET_TIME(); return(CMOS_BCD(time.wHour));
                   19591:        case 0x06: CMOS_GET_TIME(); return(time.wDayOfWeek + 1);
                   19592:        case 0x07: CMOS_GET_TIME(); return(CMOS_BCD(time.wDay));
                   19593:        case 0x08: CMOS_GET_TIME(); return(CMOS_BCD(time.wMonth));
                   19594:        case 0x09: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear));
                   19595: //     case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 2 ? 0x80 : 0));       // 2msec
                   19596:        case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 20 ? 0x80 : 0));      // precision of timeGetTime() may not be 1msec
                   19597:        case 0x15: return((MEMORY_END >> 10) & 0xff);
                   19598:        case 0x16: return((MEMORY_END >> 18) & 0xff);
                   19599:        case 0x17: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   19600:        case 0x18: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   19601:        case 0x30: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   19602:        case 0x31: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   19603:        case 0x32: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear / 100));
1.1       root     19604:        }
1.1.1.8   root     19605:        return(cmos[addr & 0x7f]);
1.1       root     19606: }
                   19607: 
1.1.1.7   root     19608: // kbd (a20)
                   19609: 
                   19610: void kbd_init()
                   19611: {
1.1.1.8   root     19612:        kbd_data = kbd_command = 0;
1.1.1.7   root     19613:        kbd_status = 0x18;
                   19614: }
                   19615: 
                   19616: UINT8 kbd_read_data()
                   19617: {
1.1.1.8   root     19618:        kbd_status &= ~1;
1.1.1.7   root     19619:        return(kbd_data);
                   19620: }
                   19621: 
                   19622: void kbd_write_data(UINT8 val)
                   19623: {
                   19624:        switch(kbd_command) {
                   19625:        case 0xd1:
                   19626:                i386_set_a20_line((val >> 1) & 1);
                   19627:                break;
                   19628:        }
                   19629:        kbd_command = 0;
1.1.1.8   root     19630:        kbd_status &= ~8;
1.1.1.7   root     19631: }
                   19632: 
                   19633: UINT8 kbd_read_status()
                   19634: {
                   19635:        return(kbd_status);
                   19636: }
                   19637: 
                   19638: void kbd_write_command(UINT8 val)
                   19639: {
                   19640:        switch(val) {
                   19641:        case 0xd0:
                   19642:                kbd_data = ((m_a20_mask >> 19) & 2) | 1;
1.1.1.8   root     19643:                kbd_status |= 1;
1.1.1.7   root     19644:                break;
                   19645:        case 0xdd:
                   19646:                i386_set_a20_line(0);
                   19647:                break;
                   19648:        case 0xdf:
                   19649:                i386_set_a20_line(1);
                   19650:                break;
1.1.1.26  root     19651:        case 0xf0: case 0xf1: case 0xf2: case 0xf3: case 0xf4: case 0xf5: case 0xf6: case 0xf7:
                   19652:        case 0xf8: case 0xf9: case 0xfa: case 0xfb: case 0xfc: case 0xfd: case 0xfe: case 0xff:
1.1.1.7   root     19653:                if(!(val & 1)) {
1.1.1.8   root     19654:                        if((cmos[0x0f] & 0x7f) == 5) {
1.1.1.7   root     19655:                                // reset pic
                   19656:                                pic_init();
                   19657:                                pic[0].irr = pic[1].irr = 0x00;
                   19658:                                pic[0].imr = pic[1].imr = 0xff;
                   19659:                        }
                   19660:                        CPU_RESET_CALL(CPU_MODEL);
1.1.1.40  root     19661:                        UINT16 address = *(UINT16 *)(mem + 0x467);
                   19662:                        UINT16 selector = *(UINT16 *)(mem + 0x469);
                   19663:                        i386_jmp_far(selector, address);
1.1.1.7   root     19664:                }
                   19665:                i386_set_a20_line((val >> 1) & 1);
                   19666:                break;
                   19667:        }
                   19668:        kbd_command = val;
1.1.1.8   root     19669:        kbd_status |= 8;
1.1.1.7   root     19670: }
                   19671: 
1.1.1.9   root     19672: // vga
                   19673: 
                   19674: UINT8 vga_read_status()
                   19675: {
                   19676:        // 60hz
                   19677:        static const int period[3] = {16, 17, 17};
                   19678:        static int index = 0;
                   19679:        UINT32 time = timeGetTime() % period[index];
                   19680:        
                   19681:        index = (index + 1) % 3;
1.1.1.14  root     19682:        return((time < 4 ? 0x08 : 0) | (time == 0 ? 0 : 0x01));
1.1.1.9   root     19683: }
                   19684: 
1.1       root     19685: // i/o bus
                   19686: 
1.1.1.29  root     19687: // this is ugly patch for SW1US.EXE, it sometimes mistakely read/write 01h-10h for serial I/O
                   19688: //#define SW1US_PATCH
                   19689: 
1.1.1.25  root     19690: UINT8 read_io_byte(offs_t addr)
1.1.1.33  root     19691: #ifdef USE_DEBUGGER
1.1.1.25  root     19692: {
1.1.1.33  root     19693:        if(now_debugging) {
                   19694:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   19695:                        if(in_break_point.table[i].status == 1) {
                   19696:                                if(addr == in_break_point.table[i].addr) {
                   19697:                                        in_break_point.hit = i + 1;
                   19698:                                        now_suspended = true;
                   19699:                                        break;
                   19700:                                }
                   19701:                        }
                   19702:                }
1.1.1.25  root     19703:        }
1.1.1.33  root     19704:        return(debugger_read_io_byte(addr));
1.1.1.25  root     19705: }
1.1.1.33  root     19706: UINT8 debugger_read_io_byte(offs_t addr)
1.1.1.25  root     19707: #endif
1.1       root     19708: {
1.1.1.33  root     19709:        UINT8 val = 0xff;
                   19710:        
1.1       root     19711:        switch(addr) {
1.1.1.29  root     19712: #ifdef SW1US_PATCH
                   19713:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   19714:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
1.1.1.33  root     19715:                val = sio_read(0, addr - 1);
                   19716:                break;
1.1.1.29  root     19717: #else
1.1.1.25  root     19718:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   19719:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
1.1.1.33  root     19720:                val = dma_read(0, addr);
                   19721:                break;
1.1.1.29  root     19722: #endif
1.1.1.25  root     19723:        case 0x20: case 0x21:
1.1.1.33  root     19724:                val = pic_read(0, addr);
                   19725:                break;
1.1.1.25  root     19726:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1.1.33  root     19727:                val = pit_read(addr & 0x03);
                   19728:                break;
1.1.1.7   root     19729:        case 0x60:
1.1.1.33  root     19730:                val = kbd_read_data();
                   19731:                break;
1.1.1.9   root     19732:        case 0x61:
1.1.1.33  root     19733:                val = system_port;
                   19734:                break;
1.1.1.7   root     19735:        case 0x64:
1.1.1.33  root     19736:                val = kbd_read_status();
                   19737:                break;
1.1       root     19738:        case 0x71:
1.1.1.33  root     19739:                val = cmos_read(cmos_addr);
                   19740:                break;
1.1.1.25  root     19741:        case 0x81:
1.1.1.33  root     19742:                val = dma_page_read(0, 2);
                   19743:                break;
1.1.1.25  root     19744:        case 0x82:
1.1.1.33  root     19745:                val = dma_page_read(0, 3);
                   19746:                break;
1.1.1.25  root     19747:        case 0x83:
1.1.1.33  root     19748:                val = dma_page_read(0, 1);
                   19749:                break;
1.1.1.25  root     19750:        case 0x87:
1.1.1.33  root     19751:                val = dma_page_read(0, 0);
                   19752:                break;
1.1.1.25  root     19753:        case 0x89:
1.1.1.33  root     19754:                val = dma_page_read(1, 2);
                   19755:                break;
1.1.1.25  root     19756:        case 0x8a:
1.1.1.33  root     19757:                val = dma_page_read(1, 3);
                   19758:                break;
1.1.1.25  root     19759:        case 0x8b:
1.1.1.33  root     19760:                val = dma_page_read(1, 1);
                   19761:                break;
1.1.1.25  root     19762:        case 0x8f:
1.1.1.33  root     19763:                val = dma_page_read(1, 0);
                   19764:                break;
1.1       root     19765:        case 0x92:
1.1.1.33  root     19766:                val = (m_a20_mask >> 19) & 2;
                   19767:                break;
1.1.1.25  root     19768:        case 0xa0: case 0xa1:
1.1.1.33  root     19769:                val = pic_read(1, addr);
                   19770:                break;
1.1.1.25  root     19771:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   19772:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.33  root     19773:                val = dma_read(1, (addr - 0xc0) >> 1);
                   19774:                break;
1.1.1.37  root     19775:        case 0x278: case 0x279: case 0x27a:
                   19776:                val = pio_read(1, addr);
                   19777:                break;
1.1.1.29  root     19778:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
1.1.1.33  root     19779:                val = sio_read(3, addr);
                   19780:                break;
1.1.1.25  root     19781:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
1.1.1.33  root     19782:                val = sio_read(1, addr);
                   19783:                break;
1.1.1.25  root     19784:        case 0x378: case 0x379: case 0x37a:
1.1.1.33  root     19785:                val = pio_read(0, addr);
                   19786:                break;
1.1.1.25  root     19787:        case 0x3ba: case 0x3da:
1.1.1.33  root     19788:                val = vga_read_status();
                   19789:                break;
1.1.1.37  root     19790:        case 0x3bc: case 0x3bd: case 0x3be:
                   19791:                val = pio_read(2, addr);
                   19792:                break;
1.1.1.29  root     19793:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
1.1.1.33  root     19794:                val = sio_read(2, addr);
                   19795:                break;
1.1.1.25  root     19796:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
1.1.1.33  root     19797:                val = sio_read(0, addr);
                   19798:                break;
1.1       root     19799:        default:
1.1.1.33  root     19800: //             fatalerror("unknown inb %4x\n", addr);
1.1       root     19801:                break;
                   19802:        }
1.1.1.33  root     19803: #ifdef ENABLE_DEBUG_IOPORT
                   19804:        if(fp_debug_log != NULL) {
                   19805:                fprintf(fp_debug_log, "inb %04X, %02X\n", addr, val);
                   19806:        }
                   19807: #endif
                   19808:        return(val);
1.1       root     19809: }
                   19810: 
                   19811: UINT16 read_io_word(offs_t addr)
                   19812: {
                   19813:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8));
                   19814: }
                   19815: 
1.1.1.33  root     19816: #ifdef USE_DEBUGGER
                   19817: UINT16 debugger_read_io_word(offs_t addr)
                   19818: {
                   19819:        return(debugger_read_io_byte(addr) | (debugger_read_io_byte(addr + 1) << 8));
                   19820: }
                   19821: #endif
                   19822: 
1.1       root     19823: UINT32 read_io_dword(offs_t addr)
                   19824: {
                   19825:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8) | (read_io_byte(addr + 2) << 16) | (read_io_byte(addr + 3) << 24));
                   19826: }
                   19827: 
1.1.1.33  root     19828: #ifdef USE_DEBUGGER
                   19829: UINT32 debugger_read_io_dword(offs_t addr)
                   19830: {
                   19831:        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));
                   19832: }
                   19833: #endif
                   19834: 
1.1       root     19835: void write_io_byte(offs_t addr, UINT8 val)
1.1.1.33  root     19836: #ifdef USE_DEBUGGER
                   19837: {
                   19838:        if(now_debugging) {
                   19839:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   19840:                        if(out_break_point.table[i].status == 1) {
                   19841:                                if(addr == out_break_point.table[i].addr) {
                   19842:                                        out_break_point.hit = i + 1;
                   19843:                                        now_suspended = true;
                   19844:                                        break;
                   19845:                                }
                   19846:                        }
                   19847:                }
                   19848:        }
                   19849:        debugger_write_io_byte(addr, val);
                   19850: }
                   19851: void debugger_write_io_byte(offs_t addr, UINT8 val)
                   19852: #endif
1.1       root     19853: {
1.1.1.25  root     19854: #ifdef ENABLE_DEBUG_IOPORT
1.1.1.33  root     19855:        if(fp_debug_log != NULL) {
1.1.1.43  root     19856: #ifdef USE_SERVICE_THREAD
                   19857:                if(addr != 0xf7)
                   19858: #endif
1.1.1.33  root     19859:                fprintf(fp_debug_log, "outb %04X, %02X\n", addr, val);
1.1.1.25  root     19860:        }
                   19861: #endif
1.1       root     19862:        switch(addr) {
1.1.1.29  root     19863: #ifdef SW1US_PATCH
                   19864:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   19865:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
                   19866:                sio_write(0, addr - 1, val);
                   19867:                break;
                   19868: #else
1.1.1.25  root     19869:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   19870:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
                   19871:                dma_write(0, addr, val);
                   19872:                break;
1.1.1.29  root     19873: #endif
1.1.1.25  root     19874:        case 0x20: case 0x21:
1.1       root     19875:                pic_write(0, addr, val);
                   19876:                break;
1.1.1.25  root     19877:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1       root     19878:                pit_write(addr & 0x03, val);
                   19879:                break;
1.1.1.7   root     19880:        case 0x60:
                   19881:                kbd_write_data(val);
                   19882:                break;
1.1.1.9   root     19883:        case 0x61:
                   19884:                if((system_port & 3) != 3 && (val & 3) == 3) {
                   19885:                        // beep on
                   19886: //                     MessageBeep(-1);
                   19887:                } else if((system_port & 3) == 3 && (val & 3) != 3) {
                   19888:                        // beep off
                   19889:                }
                   19890:                system_port = val;
                   19891:                break;
1.1       root     19892:        case 0x64:
1.1.1.7   root     19893:                kbd_write_command(val);
1.1       root     19894:                break;
                   19895:        case 0x70:
                   19896:                cmos_addr = val;
                   19897:                break;
                   19898:        case 0x71:
1.1.1.8   root     19899:                cmos_write(cmos_addr, val);
1.1       root     19900:                break;
1.1.1.25  root     19901:        case 0x81:
                   19902:                dma_page_write(0, 2, val);
                   19903:        case 0x82:
                   19904:                dma_page_write(0, 3, val);
                   19905:        case 0x83:
                   19906:                dma_page_write(0, 1, val);
                   19907:        case 0x87:
                   19908:                dma_page_write(0, 0, val);
                   19909:        case 0x89:
                   19910:                dma_page_write(1, 2, val);
                   19911:        case 0x8a:
                   19912:                dma_page_write(1, 3, val);
                   19913:        case 0x8b:
                   19914:                dma_page_write(1, 1, val);
                   19915:        case 0x8f:
                   19916:                dma_page_write(1, 0, val);
1.1       root     19917:        case 0x92:
1.1.1.7   root     19918:                i386_set_a20_line((val >> 1) & 1);
1.1       root     19919:                break;
1.1.1.25  root     19920:        case 0xa0: case 0xa1:
1.1       root     19921:                pic_write(1, addr, val);
                   19922:                break;
1.1.1.25  root     19923:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   19924:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.26  root     19925:                dma_write(1, (addr - 0xc0) >> 1, val);
1.1.1.25  root     19926:                break;
1.1.1.35  root     19927: #ifdef USE_SERVICE_THREAD
                   19928:        case 0xf7:
                   19929:                // dummy i/o for BIOS/DOS service
1.1.1.36  root     19930:                if(in_service && cursor_moved) {
                   19931:                        // update cursor position before service is done
                   19932:                        pcbios_update_cursor_position();
                   19933:                        cursor_moved = false;
                   19934:                }
1.1.1.35  root     19935:                finish_service_loop();
                   19936:                break;
                   19937: #endif
1.1.1.37  root     19938:        case 0x278: case 0x279: case 0x27a:
                   19939:                pio_write(1, addr, val);
                   19940:                break;
1.1.1.29  root     19941:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
                   19942:                sio_write(3, addr, val);
                   19943:                break;
1.1.1.25  root     19944:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
                   19945:                sio_write(1, addr, val);
                   19946:                break;
                   19947:        case 0x378: case 0x379: case 0x37a:
                   19948:                pio_write(0, addr, val);
                   19949:                break;
1.1.1.37  root     19950:        case 0x3bc: case 0x3bd: case 0x3be:
                   19951:                pio_write(2, addr, val);
                   19952:                break;
1.1.1.29  root     19953:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
                   19954:                sio_write(2, addr, val);
                   19955:                break;
1.1.1.25  root     19956:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
                   19957:                sio_write(0, addr, val);
                   19958:                break;
1.1       root     19959:        default:
1.1.1.33  root     19960: //             fatalerror("unknown outb %4x,%2x\n", addr, val);
1.1       root     19961:                break;
                   19962:        }
                   19963: }
                   19964: 
                   19965: void write_io_word(offs_t addr, UINT16 val)
                   19966: {
                   19967:        write_io_byte(addr + 0, (val >> 0) & 0xff);
                   19968:        write_io_byte(addr + 1, (val >> 8) & 0xff);
                   19969: }
                   19970: 
1.1.1.33  root     19971: #ifdef USE_DEBUGGER
                   19972: void debugger_write_io_word(offs_t addr, UINT16 val)
                   19973: {
                   19974:        debugger_write_io_byte(addr + 0, (val >> 0) & 0xff);
                   19975:        debugger_write_io_byte(addr + 1, (val >> 8) & 0xff);
                   19976: }
                   19977: #endif
                   19978: 
1.1       root     19979: void write_io_dword(offs_t addr, UINT32 val)
                   19980: {
                   19981:        write_io_byte(addr + 0, (val >>  0) & 0xff);
                   19982:        write_io_byte(addr + 1, (val >>  8) & 0xff);
                   19983:        write_io_byte(addr + 2, (val >> 16) & 0xff);
                   19984:        write_io_byte(addr + 3, (val >> 24) & 0xff);
                   19985: }
1.1.1.33  root     19986: 
                   19987: #ifdef USE_DEBUGGER
                   19988: void debugger_write_io_dword(offs_t addr, UINT32 val)
                   19989: {
                   19990:        debugger_write_io_byte(addr + 0, (val >>  0) & 0xff);
                   19991:        debugger_write_io_byte(addr + 1, (val >>  8) & 0xff);
                   19992:        debugger_write_io_byte(addr + 2, (val >> 16) & 0xff);
                   19993:        debugger_write_io_byte(addr + 3, (val >> 24) & 0xff);
                   19994: }
                   19995: #endif

unix.superglobalmegacorp.com

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