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

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.25  root       34:                #define unimplemented_14h fatalerror
1.1.1.22  root       35:                #define unimplemented_15h fatalerror
                     36:                #define unimplemented_16h fatalerror
1.1.1.37! root       37:                #define unimplemented_17h fatalerror
1.1.1.22  root       38:                #define unimplemented_1ah fatalerror
                     39:                #define unimplemented_21h fatalerror
                     40:                #define unimplemented_2fh fatalerror
1.1.1.24  root       41:                #define unimplemented_33h fatalerror
1.1.1.22  root       42:                #define unimplemented_67h fatalerror
                     43:                #define unimplemented_xms fatalerror
                     44:        #endif
                     45: #endif
                     46: #ifndef unimplemented_10h
                     47:        #define unimplemented_10h nolog
                     48: #endif
1.1.1.25  root       49: #ifndef unimplemented_14h
                     50:        #define unimplemented_14h nolog
                     51: #endif
1.1.1.22  root       52: #ifndef unimplemented_15h
                     53:        #define unimplemented_15h nolog
                     54: #endif
                     55: #ifndef unimplemented_16h
                     56:        #define unimplemented_16h nolog
                     57: #endif
1.1.1.37! root       58: #ifndef unimplemented_17h
        !            59:        #define unimplemented_17h nolog
        !            60: #endif
1.1.1.22  root       61: #ifndef unimplemented_1ah
                     62:        #define unimplemented_1ah nolog
                     63: #endif
                     64: #ifndef unimplemented_21h
                     65:        #define unimplemented_21h nolog
                     66: #endif
                     67: #ifndef unimplemented_2fh
                     68:        #define unimplemented_2fh nolog
                     69: #endif
1.1.1.24  root       70: #ifndef unimplemented_33h
                     71:        #define unimplemented_33h nolog
                     72: #endif
1.1.1.22  root       73: #ifndef unimplemented_67h
                     74:        #define unimplemented_67h nolog
                     75: #endif
                     76: #ifndef unimplemented_xms
                     77:        #define unimplemented_xms nolog
                     78: #endif
                     79: 
1.1.1.32  root       80: #define array_length(array) (sizeof(array) / sizeof(array[0]))
1.1.1.22  root       81: #define my_strchr(str, chr) (char *)_mbschr((unsigned char *)(str), (unsigned int)(chr))
                     82: #define my_strtok(tok, del) (char *)_mbstok((unsigned char *)(tok), (const unsigned char *)(del))
                     83: #define my_strupr(str) (char *)_mbsupr((unsigned char *)(str))
1.1       root       84: 
1.1.1.12  root       85: #if defined(__MINGW32__)
                     86: extern "C" int _CRT_glob = 0;
                     87: #endif
                     88: 
                     89: /*
                     90:        kludge for "more-standardized" C++
                     91: */
                     92: #if !defined(_MSC_VER)
                     93: inline int kludge_min(int a, int b) { return (a<b ? a:b); }
                     94: inline int kludge_max(int a, int b) { return (a>b ? a:b); }
                     95: #define min(a,b) kludge_min(a,b)
                     96: #define max(a,b) kludge_max(a,b)
1.1.1.14  root       97: #elif _MSC_VER >= 1400
                     98: void ignore_invalid_parameters(const wchar_t *, const wchar_t *, const wchar_t *, unsigned int, uintptr_t)
                     99: {
                    100: }
                    101: #endif
                    102: 
1.1.1.35  root      103: #define USE_VRAM_THREAD
1.1.1.14  root      104: 
1.1.1.35  root      105: #ifdef USE_VRAM_THREAD
1.1.1.14  root      106: static CRITICAL_SECTION vram_crit_sect;
                    107: #else
                    108: #define vram_flush()
1.1.1.12  root      109: #endif
                    110: 
1.1.1.14  root      111: #define VIDEO_REGEN *(UINT16 *)(mem + 0x44c)
                    112: #define SCR_BUF(y,x) scr_buf[(y) * scr_buf_size.X + (x)]
                    113: 
                    114: void change_console_size(int width, int height);
                    115: void clear_scr_buffer(WORD attr);
                    116: 
                    117: static UINT32 vram_length_char = 0, vram_length_attr = 0;
                    118: static UINT32 vram_last_length_char = 0, vram_last_length_attr = 0;
                    119: static COORD vram_coord_char, vram_coord_attr;
                    120: 
1.1.1.28  root      121: char temp_file_path[MAX_PATH];
                    122: bool temp_file_created = false;
                    123: 
1.1.1.14  root      124: bool ignore_illegal_insn = false;
                    125: bool limit_max_memory = false;
                    126: bool no_windows = false;
                    127: bool stay_busy = false;
1.1.1.19  root      128: bool support_ems = false;
                    129: #ifdef SUPPORT_XMS
                    130: bool support_xms = false;
                    131: #endif
1.1.1.29  root      132: int sio_port_number[4] = {0, 0, 0, 0};
1.1.1.14  root      133: 
                    134: BOOL is_vista_or_later;
                    135: 
1.1.1.35  root      136: #define UPDATE_OPS 16384
                    137: #define REQUEST_HARDWRE_UPDATE() { \
                    138:        update_ops = UPDATE_OPS - 1; \
                    139: }
                    140: UINT32 update_ops = 0;
                    141: UINT32 idle_ops = 0;
                    142: 
1.1.1.14  root      143: inline void maybe_idle()
                    144: {
                    145:        // if it appears to be in a tight loop, assume waiting for input
                    146:        // allow for one updated video character, for a spinning cursor
1.1.1.35  root      147:        if(!stay_busy && idle_ops < 1024 && vram_length_char <= 1 && vram_length_attr <= 1) {
1.1.1.14  root      148:                Sleep(10);
1.1.1.35  root      149:                REQUEST_HARDWRE_UPDATE();
1.1.1.14  root      150:        }
1.1.1.35  root      151:        idle_ops = 0;
1.1.1.14  root      152: }
1.1.1.12  root      153: 
1.1       root      154: /* ----------------------------------------------------------------------------
1.1.1.3   root      155:        MAME i86/i386
1.1       root      156: ---------------------------------------------------------------------------- */
                    157: 
1.1.1.10  root      158: #ifndef __BIG_ENDIAN__
1.1       root      159: #define LSB_FIRST
1.1.1.10  root      160: #endif
1.1       root      161: 
                    162: #ifndef INLINE
                    163: #define INLINE inline
                    164: #endif
                    165: #define U64(v) UINT64(v)
                    166: 
                    167: //#define logerror(...) fprintf(stderr, __VA_ARGS__)
                    168: #define logerror(...)
                    169: //#define popmessage(...) fprintf(stderr, __VA_ARGS__)
                    170: #define popmessage(...)
                    171: 
                    172: /*****************************************************************************/
1.1.1.10  root      173: /* src/emu/devcpu.h */
                    174: 
                    175: // CPU interface functions
                    176: #define CPU_INIT_NAME(name)                    cpu_init_##name
                    177: #define CPU_INIT(name)                         void CPU_INIT_NAME(name)()
                    178: #define CPU_INIT_CALL(name)                    CPU_INIT_NAME(name)()
                    179: 
                    180: #define CPU_RESET_NAME(name)                   cpu_reset_##name
                    181: #define CPU_RESET(name)                                void CPU_RESET_NAME(name)()
                    182: #define CPU_RESET_CALL(name)                   CPU_RESET_NAME(name)()
                    183: 
                    184: #define CPU_EXECUTE_NAME(name)                 cpu_execute_##name
                    185: #define CPU_EXECUTE(name)                      void CPU_EXECUTE_NAME(name)()
                    186: #define CPU_EXECUTE_CALL(name)                 CPU_EXECUTE_NAME(name)()
                    187: 
                    188: #define CPU_TRANSLATE_NAME(name)               cpu_translate_##name
                    189: #define CPU_TRANSLATE(name)                    int CPU_TRANSLATE_NAME(name)(address_spacenum space, int intention, offs_t *address)
                    190: #define CPU_TRANSLATE_CALL(name)               CPU_TRANSLATE_NAME(name)(space, intention, address)
                    191: 
                    192: #define CPU_DISASSEMBLE_NAME(name)             cpu_disassemble_##name
                    193: #define CPU_DISASSEMBLE(name)                  int CPU_DISASSEMBLE_NAME(name)(char *buffer, offs_t eip, const UINT8 *oprom)
                    194: #define CPU_DISASSEMBLE_CALL(name)             CPU_DISASSEMBLE_NAME(name)(buffer, eip, oprom)
                    195: 
1.1.1.14  root      196: #define CPU_MODEL_STR(name)                    #name
                    197: #define CPU_MODEL_NAME(name)                   CPU_MODEL_STR(name)
                    198: 
1.1.1.10  root      199: /*****************************************************************************/
                    200: /* src/emu/didisasm.h */
                    201: 
                    202: // Disassembler constants
                    203: const UINT32 DASMFLAG_SUPPORTED     = 0x80000000;   // are disassembly flags supported?
                    204: const UINT32 DASMFLAG_STEP_OUT      = 0x40000000;   // this instruction should be the end of a step out sequence
                    205: const UINT32 DASMFLAG_STEP_OVER     = 0x20000000;   // this instruction should be stepped over by setting a breakpoint afterwards
                    206: const UINT32 DASMFLAG_OVERINSTMASK  = 0x18000000;   // number of extra instructions to skip when stepping over
                    207: const UINT32 DASMFLAG_OVERINSTSHIFT = 27;           // bits to shift after masking to get the value
                    208: const UINT32 DASMFLAG_LENGTHMASK    = 0x0000ffff;   // the low 16-bits contain the actual length
                    209: 
                    210: /*****************************************************************************/
1.1       root      211: /* src/emu/diexec.h */
                    212: 
                    213: // I/O line states
                    214: enum line_state
                    215: {
                    216:        CLEAR_LINE = 0,                         // clear (a fired or held) line
                    217:        ASSERT_LINE,                            // assert an interrupt immediately
                    218:        HOLD_LINE,                              // hold interrupt line until acknowledged
                    219:        PULSE_LINE                              // pulse interrupt line instantaneously (only for NMI, RESET)
                    220: };
                    221: 
                    222: // I/O line definitions
                    223: enum
                    224: {
                    225:        INPUT_LINE_IRQ = 0,
                    226:        INPUT_LINE_NMI
                    227: };
                    228: 
                    229: /*****************************************************************************/
1.1.1.10  root      230: /* src/emu/dimemory.h */
1.1       root      231: 
1.1.1.10  root      232: // Translation intentions
                    233: const int TRANSLATE_TYPE_MASK       = 0x03;     // read write or fetch
                    234: const int TRANSLATE_USER_MASK       = 0x04;     // user mode or fully privileged
                    235: const int TRANSLATE_DEBUG_MASK      = 0x08;     // debug mode (no side effects)
                    236: 
                    237: const int TRANSLATE_READ            = 0;        // translate for read
                    238: const int TRANSLATE_WRITE           = 1;        // translate for write
                    239: const int TRANSLATE_FETCH           = 2;        // translate for instruction fetch
                    240: const int TRANSLATE_READ_USER       = (TRANSLATE_READ | TRANSLATE_USER_MASK);
                    241: const int TRANSLATE_WRITE_USER      = (TRANSLATE_WRITE | TRANSLATE_USER_MASK);
                    242: const int TRANSLATE_FETCH_USER      = (TRANSLATE_FETCH | TRANSLATE_USER_MASK);
                    243: const int TRANSLATE_READ_DEBUG      = (TRANSLATE_READ | TRANSLATE_DEBUG_MASK);
                    244: const int TRANSLATE_WRITE_DEBUG     = (TRANSLATE_WRITE | TRANSLATE_DEBUG_MASK);
                    245: const int TRANSLATE_FETCH_DEBUG     = (TRANSLATE_FETCH | TRANSLATE_DEBUG_MASK);
1.1       root      246: 
1.1.1.10  root      247: /*****************************************************************************/
                    248: /* src/emu/emucore.h */
1.1       root      249: 
1.1.1.10  root      250: // constants for expression endianness
                    251: enum endianness_t
                    252: {
                    253:        ENDIANNESS_LITTLE,
                    254:        ENDIANNESS_BIG
                    255: };
1.1       root      256: 
1.1.1.10  root      257: // declare native endianness to be one or the other
                    258: #ifdef LSB_FIRST
                    259: const endianness_t ENDIANNESS_NATIVE = ENDIANNESS_LITTLE;
                    260: #else
                    261: const endianness_t ENDIANNESS_NATIVE = ENDIANNESS_BIG;
                    262: #endif
                    263: 
                    264: // endian-based value: first value is if 'endian' is little-endian, second is if 'endian' is big-endian
                    265: #define ENDIAN_VALUE_LE_BE(endian,leval,beval) (((endian) == ENDIANNESS_LITTLE) ? (leval) : (beval))
                    266: 
                    267: // endian-based value: first value is if native endianness is little-endian, second is if native is big-endian
                    268: #define NATIVE_ENDIAN_VALUE_LE_BE(leval,beval) ENDIAN_VALUE_LE_BE(ENDIANNESS_NATIVE, leval, beval)
                    269: 
                    270: // endian-based value: first value is if 'endian' matches native, second is if 'endian' doesn't match native
                    271: #define ENDIAN_VALUE_NE_NNE(endian,leval,beval)        (((endian) == ENDIANNESS_NATIVE) ? (neval) : (nneval))
1.1       root      272: 
                    273: /*****************************************************************************/
                    274: /* src/emu/memory.h */
                    275: 
1.1.1.10  root      276: // address spaces
                    277: enum address_spacenum
                    278: {
                    279:        AS_0,                           // first address space
                    280:        AS_1,                           // second address space
                    281:        AS_2,                           // third address space
                    282:        AS_3,                           // fourth address space
                    283:        ADDRESS_SPACES,                 // maximum number of address spaces
                    284: 
                    285:        // alternate address space names for common use
                    286:        AS_PROGRAM = AS_0,              // program address space
                    287:        AS_DATA = AS_1,                 // data address space
                    288:        AS_IO = AS_2                    // I/O address space
                    289: };
                    290: 
1.1       root      291: // offsets and addresses are 32-bit (for now...)
1.1.1.30  root      292: //typedef UINT32       offs_t;
1.1       root      293: 
                    294: // read accessors
                    295: UINT8 read_byte(offs_t byteaddress)
1.1.1.33  root      296: #ifdef USE_DEBUGGER
                    297: {
                    298:        if(now_debugging) {
                    299:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    300:                        if(rd_break_point.table[i].status == 1) {
                    301:                                if(byteaddress == rd_break_point.table[i].addr) {
                    302:                                        rd_break_point.hit = i + 1;
                    303:                                        now_suspended = true;
                    304:                                        break;
                    305:                                }
                    306:                        }
                    307:                }
                    308:        }
                    309:        return(debugger_read_byte(byteaddress));
                    310: }
                    311: UINT8 debugger_read_byte(offs_t byteaddress)
                    312: #endif
1.1       root      313: {
1.1.1.4   root      314: #if defined(HAS_I386)
1.1       root      315:        if(byteaddress < MAX_MEM) {
                    316:                return mem[byteaddress];
1.1.1.3   root      317: //     } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
                    318: //             return read_byte(byteaddress & 0xfffff);
1.1       root      319:        }
                    320:        return 0;
1.1.1.4   root      321: #else
                    322:        return mem[byteaddress];
                    323: #endif
1.1       root      324: }
                    325: 
                    326: UINT16 read_word(offs_t byteaddress)
1.1.1.33  root      327: #ifdef USE_DEBUGGER
                    328: {
                    329:        if(now_debugging) {
                    330:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    331:                        if(rd_break_point.table[i].status == 1) {
                    332:                                if(byteaddress >= rd_break_point.table[i].addr && byteaddress < rd_break_point.table[i].addr + 2) {
                    333:                                        rd_break_point.hit = i + 1;
                    334:                                        now_suspended = true;
                    335:                                        break;
                    336:                                }
                    337:                        }
                    338:                }
                    339:        }
                    340:        return(debugger_read_word(byteaddress));
                    341: }
                    342: UINT16 debugger_read_word(offs_t byteaddress)
                    343: #endif
1.1       root      344: {
1.1.1.14  root      345:        if(byteaddress == 0x41c) {
                    346:                // pointer to first free slot in keyboard buffer
                    347:                // XXX: the buffer itself doesn't actually exist in DOS memory
1.1.1.35  root      348:                if(key_buf_char != NULL && key_buf_scan != NULL) {
                    349: #ifdef USE_SERVICE_THREAD
                    350:                        EnterCriticalSection(&key_buf_crit_sect);
                    351: #endif
                    352:                        int count = key_buf_char->count();
                    353: #ifdef USE_SERVICE_THREAD
                    354:                        LeaveCriticalSection(&key_buf_crit_sect);
                    355: #endif
                    356:                        if(count == 0) {
1.1.1.32  root      357:                                maybe_idle();
                    358:                        }
1.1.1.35  root      359:                        return (UINT16)count;
1.1.1.14  root      360:                }
1.1.1.32  root      361:                return 0;
1.1.1.14  root      362:        }
1.1.1.4   root      363: #if defined(HAS_I386)
1.1       root      364:        if(byteaddress < MAX_MEM - 1) {
                    365:                return *(UINT16 *)(mem + byteaddress);
1.1.1.3   root      366: //     } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
                    367: //             return read_word(byteaddress & 0xfffff);
1.1       root      368:        }
                    369:        return 0;
1.1.1.4   root      370: #else
                    371:        return *(UINT16 *)(mem + byteaddress);
                    372: #endif
1.1       root      373: }
                    374: 
                    375: UINT32 read_dword(offs_t byteaddress)
1.1.1.33  root      376: #ifdef USE_DEBUGGER
                    377: {
                    378:        if(now_debugging) {
                    379:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    380:                        if(rd_break_point.table[i].status == 1) {
                    381:                                if(byteaddress >= rd_break_point.table[i].addr && byteaddress < rd_break_point.table[i].addr + 4) {
                    382:                                        rd_break_point.hit = i + 1;
                    383:                                        now_suspended = true;
                    384:                                        break;
                    385:                                }
                    386:                        }
                    387:                }
                    388:        }
                    389:        return(debugger_read_dword(byteaddress));
                    390: }
                    391: UINT32 debugger_read_dword(offs_t byteaddress)
                    392: #endif
1.1       root      393: {
1.1.1.4   root      394: #if defined(HAS_I386)
1.1       root      395:        if(byteaddress < MAX_MEM - 3) {
                    396:                return *(UINT32 *)(mem + byteaddress);
1.1.1.3   root      397: //     } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
                    398: //             return read_dword(byteaddress & 0xfffff);
1.1       root      399:        }
                    400:        return 0;
1.1.1.4   root      401: #else
                    402:        return *(UINT32 *)(mem + byteaddress);
                    403: #endif
1.1       root      404: }
                    405: 
                    406: // write accessors
1.1.1.35  root      407: #ifdef USE_VRAM_THREAD
1.1.1.14  root      408: void vram_flush_char()
                    409: {
                    410:        if(vram_length_char != 0) {
                    411:                DWORD num;
1.1.1.23  root      412:                WriteConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), scr_char, vram_length_char, vram_coord_char, &num);
1.1.1.14  root      413:                vram_length_char = vram_last_length_char = 0;
                    414:        }
                    415: }
                    416: 
                    417: void vram_flush_attr()
                    418: {
                    419:        if(vram_length_attr != 0) {
                    420:                DWORD num;
1.1.1.23  root      421:                WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), scr_attr, vram_length_attr, vram_coord_attr, &num);
1.1.1.14  root      422:                vram_length_attr = vram_last_length_attr = 0;
                    423:        }
                    424: }
                    425: 
                    426: void vram_flush()
                    427: {
                    428:        if(vram_length_char != 0 || vram_length_attr != 0) {
                    429:                EnterCriticalSection(&vram_crit_sect);
                    430:                vram_flush_char();
                    431:                vram_flush_attr();
                    432:                LeaveCriticalSection(&vram_crit_sect);
                    433:        }
                    434: }
                    435: #endif
                    436: 
                    437: void write_text_vram_char(offs_t offset, UINT8 data)
1.1.1.8   root      438: {
1.1.1.35  root      439: #ifdef USE_VRAM_THREAD
1.1.1.14  root      440:        static offs_t first_offset_char, last_offset_char;
                    441:        
                    442:        if(vram_length_char != 0) {
                    443:                if(offset <= last_offset_char && offset >= first_offset_char) {
                    444:                        scr_char[(offset - first_offset_char) >> 1] = data;
                    445:                        return;
                    446:                }
                    447:                if(offset != last_offset_char + 2) {
                    448:                        vram_flush_char();
                    449:                }
                    450:        }
                    451:        if(vram_length_char == 0) {
                    452:                first_offset_char = offset;
                    453:                vram_coord_char.X = (offset >> 1) % scr_width;
                    454:                vram_coord_char.Y = (offset >> 1) / scr_width + scr_top;
                    455:        }
                    456:        scr_char[vram_length_char++] = data;
                    457:        last_offset_char = offset;
                    458: #else
1.1.1.8   root      459:        COORD co;
                    460:        DWORD num;
                    461:        
1.1.1.14  root      462:        co.X = (offset >> 1) % scr_width;
                    463:        co.Y = (offset >> 1) / scr_width;
                    464:        scr_char[0] = data;
1.1.1.23  root      465:        WriteConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), scr_char, 1, co, &num);
1.1.1.14  root      466: #endif
                    467: }
                    468: 
                    469: void write_text_vram_attr(offs_t offset, UINT8 data)
                    470: {
1.1.1.35  root      471: #ifdef USE_VRAM_THREAD
1.1.1.14  root      472:        static offs_t first_offset_attr, last_offset_attr;
                    473:        
                    474:        if(vram_length_attr != 0) {
                    475:                if(offset <= last_offset_attr && offset >= first_offset_attr) {
                    476:                        scr_attr[(offset - first_offset_attr) >> 1] = data;
                    477:                        return;
                    478:                }
                    479:                if(offset != last_offset_attr + 2) {
                    480:                        vram_flush_attr();
                    481:                }
                    482:        }
                    483:        if(vram_length_attr == 0) {
                    484:                first_offset_attr = offset;
                    485:                vram_coord_attr.X = (offset >> 1) % scr_width;
                    486:                vram_coord_attr.Y = (offset >> 1) / scr_width + scr_top;
                    487:        }
                    488:        scr_attr[vram_length_attr++] = data;
                    489:        last_offset_attr = offset;
                    490: #else
                    491:        COORD co;
                    492:        DWORD num;
1.1.1.8   root      493:        
1.1.1.14  root      494:        co.X = (offset >> 1) % scr_width;
                    495:        co.Y = (offset >> 1) / scr_width;
                    496:        scr_attr[0] = data;
1.1.1.23  root      497:        WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), scr_attr, 1, co, &num);
1.1.1.14  root      498: #endif
                    499: }
                    500: 
                    501: void write_text_vram_byte(offs_t offset, UINT8 data)
                    502: {
1.1.1.35  root      503: #ifdef USE_VRAM_THREAD
1.1.1.14  root      504:        EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root      505: #endif
1.1.1.8   root      506:        if(offset & 1) {
1.1.1.14  root      507:                write_text_vram_attr(offset, data);
1.1.1.8   root      508:        } else {
1.1.1.14  root      509:                write_text_vram_char(offset, data);
1.1.1.8   root      510:        }
1.1.1.35  root      511: #ifdef USE_VRAM_THREAD
1.1.1.14  root      512:        LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root      513: #endif
1.1.1.8   root      514: }
                    515: 
                    516: void write_text_vram_word(offs_t offset, UINT16 data)
                    517: {
1.1.1.35  root      518: #ifdef USE_VRAM_THREAD
1.1.1.14  root      519:        EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root      520: #endif
1.1.1.8   root      521:        if(offset & 1) {
1.1.1.14  root      522:                write_text_vram_attr(offset    , (data     ) & 0xff);
                    523:                write_text_vram_char(offset + 1, (data >> 8) & 0xff);
1.1.1.8   root      524:        } else {
1.1.1.14  root      525:                write_text_vram_char(offset    , (data     ) & 0xff);
                    526:                write_text_vram_attr(offset + 1, (data >> 8) & 0xff);
1.1.1.8   root      527:        }
1.1.1.35  root      528: #ifdef USE_VRAM_THREAD
1.1.1.14  root      529:        LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root      530: #endif
1.1.1.8   root      531: }
                    532: 
                    533: void write_text_vram_dword(offs_t offset, UINT32 data)
                    534: {
1.1.1.35  root      535: #ifdef USE_VRAM_THREAD
1.1.1.14  root      536:        EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root      537: #endif
1.1.1.8   root      538:        if(offset & 1) {
1.1.1.14  root      539:                write_text_vram_attr(offset    , (data      ) & 0xff);
                    540:                write_text_vram_char(offset + 1, (data >>  8) & 0xff);
                    541:                write_text_vram_attr(offset + 2, (data >> 16) & 0xff);
                    542:                write_text_vram_char(offset + 3, (data >> 24) & 0xff);
                    543:        } else {
                    544:                write_text_vram_char(offset    , (data      ) & 0xff);
                    545:                write_text_vram_attr(offset + 1, (data >>  8) & 0xff);
                    546:                write_text_vram_char(offset + 2, (data >> 16) & 0xff);
                    547:                write_text_vram_attr(offset + 3, (data >> 24) & 0xff);
1.1.1.8   root      548:        }
1.1.1.35  root      549: #ifdef USE_VRAM_THREAD
1.1.1.14  root      550:        LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root      551: #endif
1.1.1.8   root      552: }
                    553: 
1.1       root      554: void write_byte(offs_t byteaddress, UINT8 data)
1.1.1.33  root      555: #ifdef USE_DEBUGGER
                    556: {
                    557:        if(now_debugging) {
                    558:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    559:                        if(wr_break_point.table[i].status == 1) {
                    560:                                if(byteaddress == wr_break_point.table[i].addr) {
                    561:                                        wr_break_point.hit = i + 1;
                    562:                                        now_suspended = true;
                    563:                                        break;
                    564:                                }
                    565:                        }
                    566:                }
                    567:        }
                    568:        debugger_write_byte(byteaddress, data);
                    569: }
                    570: void debugger_write_byte(offs_t byteaddress, UINT8 data)
                    571: #endif
1.1       root      572: {
1.1.1.8   root      573:        if(byteaddress < MEMORY_END) {
1.1.1.3   root      574:                mem[byteaddress] = data;
1.1.1.8   root      575:        } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14  root      576:                if(!restore_console_on_exit) {
                    577:                        change_console_size(scr_width, scr_height);
1.1.1.12  root      578:                }
1.1.1.8   root      579:                write_text_vram_byte(byteaddress - text_vram_top_address, data);
                    580:                mem[byteaddress] = data;
                    581:        } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
                    582:                if(int_10h_feh_called && !int_10h_ffh_called) {
                    583:                        write_text_vram_byte(byteaddress - shadow_buffer_top_address, data);
1.1       root      584:                }
                    585:                mem[byteaddress] = data;
1.1.1.4   root      586: #if defined(HAS_I386)
1.1.1.3   root      587:        } else if(byteaddress < MAX_MEM) {
1.1.1.4   root      588: #else
                    589:        } else {
                    590: #endif
1.1.1.3   root      591:                mem[byteaddress] = data;
1.1       root      592:        }
                    593: }
                    594: 
                    595: void write_word(offs_t byteaddress, UINT16 data)
1.1.1.33  root      596: #ifdef USE_DEBUGGER
                    597: {
                    598:        if(now_debugging) {
                    599:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    600:                        if(wr_break_point.table[i].status == 1) {
                    601:                                if(byteaddress >= wr_break_point.table[i].addr && byteaddress < wr_break_point.table[i].addr + 2) {
                    602:                                        wr_break_point.hit = i + 1;
                    603:                                        now_suspended = true;
                    604:                                        break;
                    605:                                }
                    606:                        }
                    607:                }
                    608:        }
                    609:        debugger_write_word(byteaddress, data);
                    610: }
                    611: void debugger_write_word(offs_t byteaddress, UINT16 data)
                    612: #endif
1.1       root      613: {
1.1.1.8   root      614:        if(byteaddress < MEMORY_END) {
1.1.1.14  root      615:                if(byteaddress == 0x450 + mem[0x462] * 2) {
                    616:                        COORD co;
                    617:                        co.X = data & 0xff;
                    618:                        co.Y = (data >> 8) + scr_top;
1.1.1.23  root      619:                        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), co);
1.1.1.14  root      620:                }
1.1.1.3   root      621:                *(UINT16 *)(mem + byteaddress) = data;
1.1.1.8   root      622:        } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14  root      623:                if(!restore_console_on_exit) {
                    624:                        change_console_size(scr_width, scr_height);
1.1.1.12  root      625:                }
1.1.1.8   root      626:                write_text_vram_word(byteaddress - text_vram_top_address, data);
                    627:                *(UINT16 *)(mem + byteaddress) = data;
                    628:        } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
                    629:                if(int_10h_feh_called && !int_10h_ffh_called) {
                    630:                        write_text_vram_word(byteaddress - shadow_buffer_top_address, data);
1.1       root      631:                }
                    632:                *(UINT16 *)(mem + byteaddress) = data;
1.1.1.4   root      633: #if defined(HAS_I386)
1.1.1.3   root      634:        } else if(byteaddress < MAX_MEM - 1) {
1.1.1.4   root      635: #else
                    636:        } else {
                    637: #endif
1.1.1.3   root      638:                *(UINT16 *)(mem + byteaddress) = data;
1.1       root      639:        }
                    640: }
                    641: 
                    642: void write_dword(offs_t byteaddress, UINT32 data)
1.1.1.33  root      643: #ifdef USE_DEBUGGER
                    644: {
                    645:        if(now_debugging) {
                    646:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    647:                        if(wr_break_point.table[i].status == 1) {
                    648:                                if(byteaddress >= wr_break_point.table[i].addr && byteaddress < wr_break_point.table[i].addr + 4) {
                    649:                                        wr_break_point.hit = i + 1;
                    650:                                        now_suspended = true;
                    651:                                        break;
                    652:                                }
                    653:                        }
                    654:                }
                    655:        }
                    656:        debugger_write_dword(byteaddress, data);
                    657: }
                    658: void debugger_write_dword(offs_t byteaddress, UINT32 data)
                    659: #endif
1.1       root      660: {
1.1.1.8   root      661:        if(byteaddress < MEMORY_END) {
1.1.1.3   root      662:                *(UINT32 *)(mem + byteaddress) = data;
1.1.1.8   root      663:        } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14  root      664:                if(!restore_console_on_exit) {
                    665:                        change_console_size(scr_width, scr_height);
1.1.1.12  root      666:                }
1.1.1.8   root      667:                write_text_vram_dword(byteaddress - text_vram_top_address, data);
                    668:                *(UINT32 *)(mem + byteaddress) = data;
                    669:        } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
                    670:                if(int_10h_feh_called && !int_10h_ffh_called) {
                    671:                        write_text_vram_dword(byteaddress - shadow_buffer_top_address, data);
1.1       root      672:                }
                    673:                *(UINT32 *)(mem + byteaddress) = data;
1.1.1.4   root      674: #if defined(HAS_I386)
1.1.1.3   root      675:        } else if(byteaddress < MAX_MEM - 3) {
1.1.1.4   root      676: #else
                    677:        } else {
                    678: #endif
1.1.1.3   root      679:                *(UINT32 *)(mem + byteaddress) = data;
1.1       root      680:        }
                    681: }
                    682: 
                    683: #define read_decrypted_byte read_byte
                    684: #define read_decrypted_word read_word
                    685: #define read_decrypted_dword read_dword
                    686: 
1.1.1.3   root      687: #define read_raw_byte read_byte
                    688: #define write_raw_byte write_byte
                    689: 
                    690: #define read_word_unaligned read_word
                    691: #define write_word_unaligned write_word
                    692: 
                    693: #define read_io_word_unaligned read_io_word
                    694: #define write_io_word_unaligned write_io_word
                    695: 
1.1       root      696: UINT8 read_io_byte(offs_t byteaddress);
                    697: UINT16 read_io_word(offs_t byteaddress);
                    698: UINT32 read_io_dword(offs_t byteaddress);
                    699: 
                    700: void write_io_byte(offs_t byteaddress, UINT8 data);
                    701: void write_io_word(offs_t byteaddress, UINT16 data);
                    702: void write_io_dword(offs_t byteaddress, UINT32 data);
                    703: 
                    704: /*****************************************************************************/
                    705: /* src/osd/osdcomm.h */
                    706: 
                    707: /* Highly useful macro for compile-time knowledge of an array size */
                    708: #define ARRAY_LENGTH(x)     (sizeof(x) / sizeof(x[0]))
                    709: 
1.1.1.3   root      710: #if defined(HAS_I386)
1.1.1.10  root      711:        static CPU_TRANSLATE(i386);
                    712:        #include "mame/lib/softfloat/softfloat.c"
                    713:        #include "mame/emu/cpu/i386/i386.c"
1.1.1.12  root      714:        #include "mame/emu/cpu/vtlb.c"
1.1.1.3   root      715: #elif defined(HAS_I286)
1.1.1.10  root      716:        #include "mame/emu/cpu/i86/i286.c"
1.1.1.3   root      717: #else
1.1.1.10  root      718:        #include "mame/emu/cpu/i86/i86.c"
1.1.1.3   root      719: #endif
1.1.1.33  root      720: #ifdef USE_DEBUGGER
1.1.1.10  root      721:        #include "mame/emu/cpu/i386/i386dasm.c"
1.1       root      722: #endif
                    723: 
1.1.1.3   root      724: #if defined(HAS_I386)
                    725:        #define SREG(x)                         m_sreg[x].selector
                    726:        #define SREG_BASE(x)                    m_sreg[x].base
                    727:        int cpu_type, cpu_step;
                    728: #else
                    729:        #define REG8(x)                         m_regs.b[x]
                    730:        #define REG16(x)                        m_regs.w[x]
                    731:        #define SREG(x)                         m_sregs[x]
                    732:        #define SREG_BASE(x)                    m_base[x]
1.1.1.33  root      733:        #define m_eip                           (m_pc - m_base[CS])
1.1.1.3   root      734:        #define m_CF                            m_CarryVal
                    735:        #define m_a20_mask                      AMASK
                    736:        #define i386_load_segment_descriptor(x) m_base[x] = SegBase(x)
                    737:        #if defined(HAS_I286)
                    738:                #define i386_set_a20_line(x)    i80286_set_a20_line(x)
                    739:        #else
                    740:                #define i386_set_a20_line(x)
                    741:        #endif
                    742:        #define i386_set_irq_line(x, y)         set_irq_line(x, y)
                    743: #endif
1.1       root      744: 
                    745: void i386_jmp_far(UINT16 selector, UINT32 address)
                    746: {
1.1.1.3   root      747: #if defined(HAS_I386)
1.1       root      748:        if(PROTECTED_MODE && !V8086_MODE) {
1.1.1.3   root      749:                i386_protected_mode_jump(selector, address, 1, m_operand_size);
1.1       root      750:        } else {
1.1.1.3   root      751:                SREG(CS) = selector;
                    752:                m_performed_intersegment_jump = 1;
                    753:                i386_load_segment_descriptor(CS);
                    754:                m_eip = address;
                    755:                CHANGE_PC(m_eip);
1.1       root      756:        }
1.1.1.3   root      757: #elif defined(HAS_I286)
                    758:        i80286_code_descriptor(selector, address, 1);
                    759: #else
                    760:        SREG(CS) = selector;
                    761:        i386_load_segment_descriptor(CS);
                    762:        m_pc = (SREG_BASE(CS) + address) & m_a20_mask;
                    763: #endif
1.1       root      764: }
                    765: 
1.1.1.35  root      766: #ifdef USE_SERVICE_THREAD
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.35  root      796: #endif
1.1.1.24  root      797: 
1.1.1.29  root      798: UINT16 i386_read_stack()
                    799: {
                    800: #if defined(HAS_I386)
                    801:        UINT32 ea, new_esp;
                    802:        if( STACK_32BIT ) {
                    803:                new_esp = REG32(ESP) + 2;
                    804:                ea = i386_translate(SS, new_esp - 2, 0);
                    805:        } else {
                    806:                new_esp = REG16(SP) + 2;
                    807:                ea = i386_translate(SS, (new_esp - 2) & 0xffff, 0);
                    808:        }
                    809:        return READ16(ea);
                    810: #else
                    811:        UINT16 sp = m_regs.w[SP] + 2;
                    812:        return ReadWord(((m_base[SS] + ((sp - 2) & 0xffff)) & AMASK));
                    813: #endif
                    814: }
                    815: 
1.1       root      816: /* ----------------------------------------------------------------------------
1.1.1.33  root      817:        debugger
                    818: ---------------------------------------------------------------------------- */
                    819: 
                    820: #ifdef USE_DEBUGGER
                    821: #define TELNET_BLUE      0x0004 // text color contains blue.
                    822: #define TELNET_GREEN     0x0002 // text color contains green.
                    823: #define TELNET_RED       0x0001 // text color contains red.
                    824: #define TELNET_INTENSITY 0x0008 // text color is intensified.
                    825: 
                    826: int svr_socket = 0;
                    827: int cli_socket = 0;
                    828: 
                    829: process_t *msdos_process_info_get(UINT16 psp_seg, bool show_error);
                    830: 
                    831: void debugger_init()
                    832: {
                    833:        now_debugging = false;
                    834:        now_going = false;
                    835:        now_suspended = false;
                    836:        force_suspend = false;
                    837:        
                    838:        memset(&break_point, 0, sizeof(break_point_t));
                    839:        memset(&rd_break_point, 0, sizeof(break_point_t));
                    840:        memset(&wr_break_point, 0, sizeof(break_point_t));
                    841:        memset(&in_break_point, 0, sizeof(break_point_t));
                    842:        memset(&out_break_point, 0, sizeof(break_point_t));
                    843:        memset(&int_break_point, 0, sizeof(int_break_point_t));
                    844: }
                    845: 
                    846: void telnet_send(char *string)
                    847: {
                    848:        char buffer[8192], *ptr;
                    849:        strcpy(buffer, string);
                    850:        while((ptr = strstr(buffer, "\n")) != NULL) {
                    851:                char tmp[8192];
                    852:                *ptr = '\0';
                    853:                sprintf(tmp, "%s\033E%s", buffer, ptr + 1);
                    854:                strcpy(buffer, tmp);
                    855:        }
                    856:        
                    857:        int len = strlen(buffer), res;
                    858:        ptr = buffer;
                    859:        while(len > 0) {
                    860:                if((res = send(cli_socket, ptr, len, 0)) > 0) {
                    861:                        len -= res;
                    862:                        ptr += res;
                    863:                }
                    864:        }
                    865: }
                    866: 
                    867: void telnet_command(const char *format, ...)
                    868: {
                    869:        char buffer[1024];
                    870:        va_list ap;
                    871:        va_start(ap, format);
                    872:        vsprintf(buffer, format, ap);
                    873:        va_end(ap);
                    874:        
                    875:        telnet_send(buffer);
                    876: }
                    877: 
                    878: void telnet_printf(const char *format, ...)
                    879: {
                    880:        char buffer[1024];
                    881:        va_list ap;
                    882:        va_start(ap, format);
                    883:        vsprintf(buffer, format, ap);
                    884:        va_end(ap);
                    885:        
                    886:        if(fp_debugger != NULL) {
                    887:                fprintf(fp_debugger, "%s", buffer);
                    888:        }
                    889:        telnet_send(buffer);
                    890: }
                    891: 
                    892: bool telnet_gets(char *str, int n)
                    893: {
                    894:        char buffer[1024];
                    895:        int ptr = 0;
                    896:        
                    897:        telnet_command("\033[12l"); // local echo on
                    898:        telnet_command("\033[2l");  // key unlock
                    899:        
                    900:        while(!m_halted) {
                    901:                int len = recv(cli_socket, buffer, sizeof(buffer), 0);
                    902:                
                    903:                if(len > 0 && buffer[0] != 0xff) {
                    904:                        for(int i = 0; i < len; i++) {
                    905:                                if(buffer[i] == 0x0d || buffer[i] == 0x0a) {
                    906:                                        str[ptr] = 0;
                    907:                                        telnet_command("\033[2h");  // key lock
                    908:                                        telnet_command("\033[12h"); // local echo off
                    909:                                        return(!m_halted);
                    910:                                } else if(buffer[i] == 0x08) {
                    911:                                        if(ptr > 0) {
                    912:                                                telnet_command("\033[0K"); // erase from cursor position
                    913:                                                ptr--;
                    914:                                        } else {
                    915:                                                telnet_command("\033[1C"); // move cursor forward
                    916:                                        }
                    917:                                } else if(ptr < n - 1) {
1.1.1.37! root      918:                                        if(buffer[i] >= 0x20 && buffer[i] <= 0x7e) {
1.1.1.33  root      919:                                                str[ptr++] = buffer[i];
                    920:                                        }
                    921:                                } else {
                    922:                                        telnet_command("\033[1D\033[0K"); // move cursor backward and erase from cursor position
                    923:                                }
                    924:                        }
                    925:                } else if(len == -1) {
                    926:                        if(WSAGetLastError() != WSAEWOULDBLOCK) {
                    927:                                return(false);
                    928:                        }
                    929:                } else if(len == 0) {
                    930:                        return(false);
                    931:                }
                    932:                Sleep(10);
                    933:        }
                    934:        return(!m_halted);
                    935: }
                    936: 
                    937: bool telnet_kbhit()
                    938: {
                    939:        char buffer[1024];
                    940:        
                    941:        if(!m_halted) {
                    942:                int len = recv(cli_socket, buffer, sizeof(buffer), 0);
                    943:                
                    944:                if(len > 0) {
                    945:                        for(int i = 0; i < len; i++) {
                    946:                                if(buffer[i] == 0x0d || buffer[i] == 0x0a) {
                    947:                                        return(true);
                    948:                                }
                    949:                        }
                    950:                } else if(len == 0) {
                    951:                        return(true); // disconnected
                    952:                }
                    953:        }
                    954:        return(false);
                    955: }
                    956: 
                    957: bool telnet_disconnected()
                    958: {
                    959:        char buffer[1024];
                    960:        int len = recv(cli_socket, buffer, sizeof(buffer), 0);
                    961:        
                    962:        if(len == 0) {
                    963:                return(true);
                    964:        } else if(len == -1) {
                    965:                if(WSAGetLastError() != WSAEWOULDBLOCK) {
                    966:                        return(true);
                    967:                }
                    968:        }
                    969:        return(false);
                    970: }
                    971: 
                    972: void telnet_set_color(int color)
                    973: {
                    974:        telnet_command("\033[%dm\033[3%dm", (color >> 3) & 1, (color & 7));
                    975: }
                    976: 
                    977: int debugger_dasm(char *buffer, UINT32 cs, UINT32 eip)
                    978: {
                    979: //     UINT8 *oprom = mem + (((cs << 4) + eip) & (MAX_MEM - 1));
                    980:        UINT8 ops[16];
                    981:        for(int i = 0; i < 16; i++) {
                    982:                ops[i] = debugger_read_byte(((cs << 4) + (eip + i)) & ADDR_MASK);
                    983:        }
                    984:        UINT8 *oprom = ops;
                    985:        
                    986: #if defined(HAS_I386)
                    987:        if(m_operand_size) {
                    988:                return(CPU_DISASSEMBLE_CALL(x86_32) & DASMFLAG_LENGTHMASK);
                    989:        } else
                    990: #endif
                    991:        return(CPU_DISASSEMBLE_CALL(x86_16) & DASMFLAG_LENGTHMASK);
                    992: }
                    993: 
                    994: void debugger_regs_info(char *buffer)
                    995: {
                    996: #if defined(HAS_I386)
                    997:        UINT32 flags = get_flags();
                    998: #else
                    999:        UINT32 flags = CompressFlags();
                   1000: #endif
                   1001: #if defined(HAS_I386)
                   1002:        if(m_operand_size) {
                   1003:                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",
                   1004:                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),
                   1005:                PROTECTED_MODE ? "PE" : "--",
                   1006:                (flags & 0x40000) ? 'A' : '-',
                   1007:                (flags & 0x20000) ? 'V' : '-',
                   1008:                (flags & 0x10000) ? 'R' : '-',
                   1009:                (flags & 0x04000) ? 'N' : '-',
                   1010:                (flags & 0x02000) ? '1' : '0',
                   1011:                (flags & 0x01000) ? '1' : '0',
                   1012:                (flags & 0x00800) ? 'O' : '-',
                   1013:                (flags & 0x00400) ? 'D' : '-',
                   1014:                (flags & 0x00200) ? 'I' : '-',
                   1015:                (flags & 0x00100) ? 'T' : '-',
                   1016:                (flags & 0x00080) ? 'S' : '-',
                   1017:                (flags & 0x00040) ? 'Z' : '-',
                   1018:                (flags & 0x00010) ? 'A' : '-',
                   1019:                (flags & 0x00004) ? 'P' : '-',
                   1020:                (flags & 0x00001) ? 'C' : '-');
                   1021:        } else {
                   1022: #endif
                   1023:                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",
                   1024:                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),
                   1025: #if defined(HAS_I386)
                   1026:                PROTECTED_MODE ? "PE" : "--",
                   1027: #else
                   1028:                "--",
                   1029: #endif
                   1030:                (flags & 0x40000) ? 'A' : '-',
                   1031:                (flags & 0x20000) ? 'V' : '-',
                   1032:                (flags & 0x10000) ? 'R' : '-',
                   1033:                (flags & 0x04000) ? 'N' : '-',
                   1034:                (flags & 0x02000) ? '1' : '0',
                   1035:                (flags & 0x01000) ? '1' : '0',
                   1036:                (flags & 0x00800) ? 'O' : '-',
                   1037:                (flags & 0x00400) ? 'D' : '-',
                   1038:                (flags & 0x00200) ? 'I' : '-',
                   1039:                (flags & 0x00100) ? 'T' : '-',
                   1040:                (flags & 0x00080) ? 'S' : '-',
                   1041:                (flags & 0x00040) ? 'Z' : '-',
                   1042:                (flags & 0x00010) ? 'A' : '-',
                   1043:                (flags & 0x00004) ? 'P' : '-',
                   1044:                (flags & 0x00001) ? 'C' : '-');
                   1045: #if defined(HAS_I386)
                   1046:        }
                   1047: #endif
                   1048: }
                   1049: 
                   1050: void debugger_process_info(char *buffer)
                   1051: {
                   1052:        UINT16 psp_seg = current_psp;
                   1053:        process_t *process;
                   1054:        bool check[0x10000] = {0};
                   1055:        
                   1056:        buffer[0] = '\0';
                   1057:        
                   1058:        while(!check[psp_seg] && (process  = msdos_process_info_get(psp_seg, false)) != NULL) {
                   1059:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   1060:                char *file = process->module_path, *s;
                   1061:                char tmp[8192];
                   1062:                
                   1063:                while((s = strstr(file, "\\")) != NULL) {
                   1064:                        file = s + 1;
                   1065:                }
                   1066:                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));
                   1067:                strcat(tmp, buffer);
                   1068:                strcpy(buffer, tmp);
                   1069:                
                   1070:                check[psp_seg] = true;
                   1071:                psp_seg = psp->parent_psp;
                   1072:        }
                   1073: }
                   1074: 
                   1075: UINT32 debugger_get_val(const char *str)
                   1076: {
                   1077:        char tmp[1024];
                   1078:        
                   1079:        if(str == NULL || strlen(str) == 0) {
                   1080:                return(0);
                   1081:        }
                   1082:        strcpy(tmp, str);
                   1083:        
                   1084:        if(strlen(tmp) == 3 && tmp[0] == '\'' && tmp[2] == '\'') {
                   1085:                // ank
                   1086:                return(tmp[1] & 0xff);
                   1087:        } else if(tmp[0] == '%') {
                   1088:                // decimal
                   1089:                return(strtoul(tmp + 1, NULL, 10));
                   1090:        }
                   1091:        return(strtoul(tmp, NULL, 16));
                   1092: }
                   1093: 
                   1094: UINT32 debugger_get_seg(const char *str, UINT32 val)
                   1095: {
                   1096:        char tmp[1024], *s;
                   1097:        
                   1098:        if(str == NULL || strlen(str) == 0) {
                   1099:                return(val);
                   1100:        }
                   1101:        strcpy(tmp, str);
                   1102:        
                   1103:        if((s = strstr(tmp, ":")) != NULL) {
                   1104:                // 0000:0000
                   1105:                *s = '\0';
                   1106:                return(debugger_get_val(tmp));
                   1107:        }
                   1108:        return(val);
                   1109: }
                   1110: 
                   1111: UINT32 debugger_get_ofs(const char *str)
                   1112: {
                   1113:        char tmp[1024], *s;
                   1114:        
                   1115:        if(str == NULL || strlen(str) == 0) {
                   1116:                return(0);
                   1117:        }
                   1118:        strcpy(tmp, str);
                   1119:        
                   1120:        if((s = strstr(tmp, ":")) != NULL) {
                   1121:                // 0000:0000
                   1122:                return(debugger_get_val(s + 1));
                   1123:        }
                   1124:        return(debugger_get_val(tmp));
                   1125: }
                   1126: 
                   1127: void debugger_main()
                   1128: {
                   1129:        telnet_command("\033[20h"); // cr-lf
                   1130:        
                   1131:        force_suspend = true;
                   1132:        now_going = false;
                   1133:        now_debugging = true;
                   1134:        Sleep(100);
                   1135:        
                   1136:        if(!m_halted && !now_suspended) {
                   1137:                telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1138:                telnet_printf("waiting until cpu is suspended...\n");
                   1139:        }
                   1140:        while(!m_halted && !now_suspended) {
                   1141:                if(telnet_disconnected()) {
                   1142:                        break;
                   1143:                }
                   1144:                Sleep(10);
                   1145:        }
                   1146:        
                   1147:        char buffer[8192];
                   1148:        
                   1149:        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1150:        debugger_process_info(buffer);
                   1151:        telnet_printf("%s", buffer);
                   1152:        debugger_regs_info(buffer);
                   1153:        telnet_printf("%s", buffer);
                   1154:        telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1155:        telnet_printf("breaked at %04X:%04X\n", SREG(CS), m_eip);
                   1156:        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1157:        debugger_dasm(buffer, SREG(CS), m_eip);
                   1158:        telnet_printf("next\t%04X:%04X  %s\n", SREG(CS), m_eip, buffer);
                   1159:        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1160:        
                   1161:        #define MAX_COMMAND_LEN 64
                   1162:        
                   1163:        char command[MAX_COMMAND_LEN + 1];
                   1164:        char prev_command[MAX_COMMAND_LEN + 1] = {0};
                   1165:        
                   1166:        UINT32 data_seg = SREG(DS);
                   1167:        UINT32 data_ofs = 0;
                   1168:        UINT32 dasm_seg = SREG(CS);
                   1169:        UINT32 dasm_ofs = m_eip;
                   1170:        
                   1171:        while(!m_halted) {
                   1172:                telnet_printf("- ");
                   1173:                command[0] = '\0';
                   1174:                
                   1175:                if(fi_debugger != NULL) {
                   1176:                        while(command[0] == '\0') {
                   1177:                                if(fgets(command, sizeof(command), fi_debugger) == NULL) {
                   1178:                                        break;
                   1179:                                }
                   1180:                                while(strlen(command) > 0 && (command[strlen(command) - 1] == 0x0d || command[strlen(command) - 1] == 0x0a)) {
                   1181:                                        command[strlen(command) - 1] = '\0';
                   1182:                                }
                   1183:                        }
                   1184:                        if(command[0] != '\0') {
                   1185:                                telnet_command("%s\n", command);
                   1186:                        }
                   1187:                }
                   1188:                if(command[0] == '\0') {
                   1189:                        if(!telnet_gets(command, sizeof(command))) {
                   1190:                                break;
                   1191:                        }
                   1192:                }
                   1193:                if(command[0] == '\0') {
                   1194:                        strcpy(command, prev_command);
                   1195:                } else {
                   1196:                        strcpy(prev_command, command);
                   1197:                }
                   1198:                if(fp_debugger != NULL) {
                   1199:                        fprintf(fp_debugger, "%s\n", command);
                   1200:                }
                   1201:                
                   1202:                if(!m_halted && command[0] != 0) {
                   1203:                        char *params[32], *token = NULL;
                   1204:                        int num = 0;
                   1205:                        
                   1206:                        if((token = strtok(command, " ")) != NULL) {
                   1207:                                params[num++] = token;
                   1208:                                while(num < 32 && (token = strtok(NULL, " ")) != NULL) {
                   1209:                                        params[num++] = token;
                   1210:                                }
                   1211:                        }
                   1212:                        if(stricmp(params[0], "D") == 0) {
                   1213:                                if(num <= 3) {
                   1214:                                        if(num >= 2) {
                   1215:                                                data_seg = debugger_get_seg(params[1], data_seg);
                   1216:                                                data_ofs = debugger_get_ofs(params[1]);
                   1217:                                        }
                   1218:                                        UINT32 end_seg = data_seg;
                   1219:                                        UINT32 end_ofs = data_ofs + 8 * 16 - 1;
                   1220:                                        if(num == 3) {
                   1221:                                                end_seg = debugger_get_seg(params[2], data_seg);
                   1222:                                                end_ofs = debugger_get_ofs(params[2]);
                   1223:                                        }
                   1224:                                        UINT64 start_addr = (data_seg << 4) + data_ofs;
                   1225:                                        UINT64 end_addr = (end_seg << 4) + end_ofs;
1.1.1.37! root     1226: //                                     bool is_sjis = false;
1.1.1.33  root     1227:                                        
                   1228:                                        for(UINT64 addr = (start_addr & ~0x0f); addr <= (end_addr | 0x0f); addr++) {
                   1229:                                                if((addr & 0x0f) == 0) {
                   1230:                                                        if((data_ofs = addr - (data_seg << 4)) > 0xffff) {
                   1231:                                                                data_seg += 0x1000;
                   1232:                                                                data_ofs -= 0x10000;
                   1233:                                                        }
                   1234:                                                        telnet_printf("%06X:%04X ", data_seg, data_ofs);
                   1235:                                                        memset(buffer, 0, sizeof(buffer));
                   1236:                                                }
                   1237:                                                if(addr < start_addr || addr > end_addr) {
                   1238:                                                        telnet_printf("   ");
                   1239:                                                        buffer[addr & 0x0f] = ' ';
                   1240:                                                } else {
                   1241:                                                        UINT8 data = debugger_read_byte(addr & ADDR_MASK);
                   1242:                                                        telnet_printf(" %02X", data);
1.1.1.37! root     1243: //                                                     if(is_sjis) {
1.1.1.33  root     1244: //                                                             buffer[addr & 0x0f] = data;
1.1.1.37! root     1245: //                                                             is_sjis = false;
1.1.1.33  root     1246: //                                                     } else if(((data >= 0x81 && data <= 0x9f) || (data >= 0xe0 && data <= 0xef)) && (addr & 0x0f) < 0x0f) {
                   1247: //                                                             buffer[addr & 0x0f] = data;
1.1.1.37! root     1248: //                                                             is_sjis = true;
1.1.1.33  root     1249: //                                                     } else
                   1250:                                                        if((data >= 0x20 && data <= 0x7e)/* || (data >= 0xa1 && data <= 0xdf)*/) {
                   1251:                                                                buffer[addr & 0x0f] = data;
                   1252:                                                        } else {
                   1253:                                                                buffer[addr & 0x0f] = '.';
                   1254:                                                        }
                   1255:                                                }
                   1256:                                                if((addr & 0x0f) == 0x0f) {
                   1257:                                                        telnet_printf("  %s\n", buffer);
                   1258:                                                }
                   1259:                                        }
                   1260:                                        if((data_ofs = (end_addr + 1) - (data_seg << 4)) > 0xffff) {
                   1261:                                                data_seg += 0x1000;
                   1262:                                                data_ofs -= 0x10000;
                   1263:                                        }
                   1264:                                        prev_command[1] = '\0'; // remove parameters to dump continuously
                   1265:                                } else {
                   1266:                                        telnet_printf("invalid parameter number\n");
                   1267:                                }
                   1268:                        } else if(stricmp(params[0], "E") == 0 || stricmp(params[0], "EB") == 0) {
                   1269:                                if(num >= 3) {
                   1270:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1271:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1272:                                        for(int i = 2, j = 0; i < num; i++, j++) {
                   1273:                                                debugger_write_byte(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]) & 0xff);
                   1274:                                        }
                   1275:                                } else {
                   1276:                                        telnet_printf("invalid parameter number\n");
                   1277:                                }
                   1278:                        } else if(stricmp(params[0], "EW") == 0) {
                   1279:                                if(num >= 3) {
                   1280:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1281:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1282:                                        for(int i = 2, j = 0; i < num; i++, j += 2) {
                   1283:                                                debugger_write_word(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]) & 0xffff);
                   1284:                                        }
                   1285:                                } else {
                   1286:                                        telnet_printf("invalid parameter number\n");
                   1287:                                }
                   1288:                        } else if(stricmp(params[0], "ED") == 0) {
                   1289:                                if(num >= 3) {
                   1290:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1291:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1292:                                        for(int i = 2, j = 0; i < num; i++, j += 4) {
                   1293:                                                debugger_write_dword(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]));
                   1294:                                        }
                   1295:                                } else {
                   1296:                                        telnet_printf("invalid parameter number\n");
                   1297:                                }
                   1298:                        } else if(stricmp(params[0], "EA") == 0) {
                   1299:                                if(num >= 3) {
                   1300:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1301:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1302:                                        strcpy(buffer, prev_command);
                   1303:                                        if((token = strtok(buffer, "\"")) != NULL && (token = strtok(NULL, "\"")) != NULL) {
                   1304:                                                int len = strlen(token);
                   1305:                                                for(int i = 0; i < len; i++) {
                   1306:                                                        debugger_write_byte(((seg << 4) + (ofs + i)) & ADDR_MASK, token[i] & 0xff);
                   1307:                                                }
                   1308:                                        } else {
                   1309:                                                telnet_printf("invalid parameter\n");
                   1310:                                        }
                   1311:                                } else {
                   1312:                                        telnet_printf("invalid parameter number\n");
                   1313:                                }
                   1314:                        } else if(stricmp(params[0], "I") == 0 || stricmp(params[0], "IB") == 0) {
                   1315:                                if(num == 2) {
                   1316:                                        telnet_printf("%02X\n", debugger_read_io_byte(debugger_get_val(params[1])) & 0xff);
                   1317:                                } else {
                   1318:                                        telnet_printf("invalid parameter number\n");
                   1319:                                }
                   1320:                        } else if(stricmp(params[0], "IW") == 0) {
                   1321:                                if(num == 2) {
                   1322:                                        telnet_printf("%04X\n", debugger_read_io_word(debugger_get_val(params[1])) & 0xffff);
                   1323:                                } else {
                   1324:                                        telnet_printf("invalid parameter number\n");
                   1325:                                }
                   1326:                        } else if(stricmp(params[0], "ID") == 0) {
                   1327:                                if(num == 2) {
                   1328:                                        telnet_printf("%08X\n", debugger_read_io_dword(debugger_get_val(params[1])));
                   1329:                                } else {
                   1330:                                        telnet_printf("invalid parameter number\n");
                   1331:                                }
                   1332:                        } else if(stricmp(params[0], "O") == 0 || stricmp(params[0], "OB") == 0) {
                   1333:                                if(num == 3) {
                   1334:                                        debugger_write_io_byte(debugger_get_val(params[1]), debugger_get_val(params[2]) & 0xff);
                   1335:                                } else {
                   1336:                                        telnet_printf("invalid parameter number\n");
                   1337:                                }
                   1338:                        } else if(stricmp(params[0], "OW") == 0) {
                   1339:                                if(num == 3) {
                   1340:                                        debugger_write_io_word(debugger_get_val(params[1]), debugger_get_val(params[2]) & 0xffff);
                   1341:                                } else {
                   1342:                                        telnet_printf("invalid parameter number\n");
                   1343:                                }
                   1344:                        } else if(stricmp(params[0], "OD") == 0) {
                   1345:                                if(num == 3) {
                   1346:                                        debugger_write_io_dword(debugger_get_val(params[1]), debugger_get_val(params[2]));
                   1347:                                } else {
                   1348:                                        telnet_printf("invalid parameter number\n");
                   1349:                                }
                   1350:                        } else if(stricmp(params[0], "R") == 0) {
                   1351:                                if(num == 1) {
                   1352:                                        debugger_regs_info(buffer);
                   1353:                                        telnet_printf("%s", buffer);
                   1354:                                } else if(num == 3) {
                   1355: #if defined(HAS_I386)
                   1356:                                        if(stricmp(params[1], "EAX") == 0) {
                   1357:                                                REG32(EAX) = debugger_get_val(params[2]);
                   1358:                                        } else if(stricmp(params[1], "EBX") == 0) {
                   1359:                                                REG32(EBX) = debugger_get_val(params[2]);
                   1360:                                        } else if(stricmp(params[1], "ECX") == 0) {
                   1361:                                                REG32(ECX) = debugger_get_val(params[2]);
                   1362:                                        } else if(stricmp(params[1], "EDX") == 0) {
                   1363:                                                REG32(EDX) = debugger_get_val(params[2]);
                   1364:                                        } else if(stricmp(params[1], "ESP") == 0) {
                   1365:                                                REG32(ESP) = debugger_get_val(params[2]);
                   1366:                                        } else if(stricmp(params[1], "EBP") == 0) {
                   1367:                                                REG32(EBP) = debugger_get_val(params[2]);
                   1368:                                        } else if(stricmp(params[1], "ESI") == 0) {
                   1369:                                                REG32(ESI) = debugger_get_val(params[2]);
                   1370:                                        } else if(stricmp(params[1], "EDI") == 0) {
                   1371:                                                REG32(EDI) = debugger_get_val(params[2]);
                   1372:                                        } else
                   1373: #endif
                   1374:                                        if(stricmp(params[1], "AX") == 0) {
                   1375:                                                REG16(AX) = debugger_get_val(params[2]);
                   1376:                                        } else if(stricmp(params[1], "BX") == 0) {
                   1377:                                                REG16(BX) = debugger_get_val(params[2]);
                   1378:                                        } else if(stricmp(params[1], "CX") == 0) {
                   1379:                                                REG16(CX) = debugger_get_val(params[2]);
                   1380:                                        } else if(stricmp(params[1], "DX") == 0) {
                   1381:                                                REG16(DX) = debugger_get_val(params[2]);
                   1382:                                        } else if(stricmp(params[1], "SP") == 0) {
                   1383:                                                REG16(SP) = debugger_get_val(params[2]);
                   1384:                                        } else if(stricmp(params[1], "BP") == 0) {
                   1385:                                                REG16(BP) = debugger_get_val(params[2]);
                   1386:                                        } else if(stricmp(params[1], "SI") == 0) {
                   1387:                                                REG16(SI) = debugger_get_val(params[2]);
                   1388:                                        } else if(stricmp(params[1], "DI") == 0) {
                   1389:                                                REG16(DI) = debugger_get_val(params[2]);
                   1390:                                        } else if(stricmp(params[1], "IP") == 0 || stricmp(params[1], "EIP") == 0) {
                   1391: #if defined(HAS_I386)
                   1392:                                                if(m_operand_size) {
                   1393:                                                        m_eip = debugger_get_val(params[2]);
                   1394:                                                } else {
                   1395:                                                        m_eip = debugger_get_val(params[2]) & 0xffff;
                   1396:                                                }
                   1397:                                                CHANGE_PC(m_eip);
                   1398: #else
                   1399:                                                m_pc = (SREG_BASE(CS) + (debugger_get_val(params[2]) & 0xffff)) & ADDR_MASK;
                   1400:                                                CHANGE_PC(m_pc);
                   1401: #endif
                   1402:                                        } else if(stricmp(params[1], "AL") == 0) {
                   1403:                                                REG8(AL) = debugger_get_val(params[2]);
                   1404:                                        } else if(stricmp(params[1], "AH") == 0) {
                   1405:                                                REG8(AH) = debugger_get_val(params[2]);
                   1406:                                        } else if(stricmp(params[1], "BL") == 0) {
                   1407:                                                REG8(BL) = debugger_get_val(params[2]);
                   1408:                                        } else if(stricmp(params[1], "BH") == 0) {
                   1409:                                                REG8(BH) = debugger_get_val(params[2]);
                   1410:                                        } else if(stricmp(params[1], "CL") == 0) {
                   1411:                                                REG8(CL) = debugger_get_val(params[2]);
                   1412:                                        } else if(stricmp(params[1], "CH") == 0) {
                   1413:                                                REG8(CH) = debugger_get_val(params[2]);
                   1414:                                        } else if(stricmp(params[1], "DL") == 0) {
                   1415:                                                REG8(DL) = debugger_get_val(params[2]);
                   1416:                                        } else if(stricmp(params[1], "DH") == 0) {
                   1417:                                                REG8(DH) = debugger_get_val(params[2]);
                   1418:                                        } else {
                   1419:                                                telnet_printf("unknown register %s\n", params[1]);
                   1420:                                        }
                   1421:                                } else {
                   1422:                                        telnet_printf("invalid parameter number\n");
                   1423:                                }
                   1424:                        } else if(_tcsicmp(params[0], "S") == 0) {
                   1425:                                if(num >= 4) {
                   1426:                                        UINT32 cur_seg = debugger_get_seg(params[1], data_seg);
                   1427:                                        UINT32 cur_ofs = debugger_get_ofs(params[1]);
                   1428:                                        UINT32 end_seg = debugger_get_seg(params[2], cur_seg);
                   1429:                                        UINT32 end_ofs = debugger_get_ofs(params[2]);
                   1430:                                        UINT8 list[32];
                   1431:                                        
                   1432:                                        for(int i = 3, j = 0; i < num && j < 32; i++, j++) {
                   1433:                                                list[j] = debugger_get_val(params[i]);
                   1434:                                        }
                   1435:                                        while((cur_seg << 4) + cur_ofs <= (end_seg << 4) + end_ofs) {
                   1436:                                                bool found = true;
                   1437:                                                for(int i = 3, j = 0; i < num && j < 32; i++, j++) {
                   1438:                                                        if(debugger_read_byte(((cur_seg << 4) + (cur_ofs + j)) & ADDR_MASK) != list[j]) {
                   1439:                                                                found = false;
                   1440:                                                                break;
                   1441:                                                        }
                   1442:                                                }
                   1443:                                                if(found) {
                   1444:                                                        telnet_printf("%04X:%04X\n", cur_seg, cur_ofs);
                   1445:                                                }
                   1446:                                                if((cur_ofs += 1) > 0xffff) {
                   1447:                                                        cur_seg += 0x1000;
                   1448:                                                        cur_ofs -= 0x10000;
                   1449:                                                }
                   1450:                                        }
                   1451:                                } else {
                   1452:                                        telnet_printf("invalid parameter number\n");
                   1453:                                }
                   1454:                        } else if(stricmp(params[0], "U") == 0) {
                   1455:                                if(num <= 3) {
                   1456:                                        if(num >= 2) {
                   1457:                                                dasm_seg = debugger_get_seg(params[1], dasm_seg);
                   1458:                                                dasm_ofs = debugger_get_ofs(params[1]);
                   1459:                                        }
                   1460:                                        if(num == 3) {
                   1461:                                                UINT32 end_seg = debugger_get_seg(params[2], dasm_seg);
                   1462:                                                UINT32 end_ofs = debugger_get_ofs(params[2]);
                   1463:                                                
                   1464:                                                while((dasm_seg << 4) + dasm_ofs <= (end_seg << 4) + end_ofs) {
                   1465:                                                        int len = debugger_dasm(buffer, dasm_seg, dasm_ofs);
                   1466:                                                        telnet_printf("%04X:%04X  ", dasm_seg, dasm_ofs);
                   1467:                                                        for(int i = 0; i < len; i++) {
                   1468:                                                                telnet_printf("%02X", debugger_read_byte(((dasm_seg << 4) + (dasm_ofs + i)) & ADDR_MASK));
                   1469:                                                        }
                   1470:                                                        for(int i = len; i < 8; i++) {
                   1471:                                                                telnet_printf("  ");
                   1472:                                                        }
                   1473:                                                        telnet_printf("  %s\n", buffer);
                   1474:                                                        if((dasm_ofs += len) > 0xffff) {
                   1475:                                                                dasm_seg += 0x1000;
                   1476:                                                                dasm_ofs -= 0x10000;
                   1477:                                                        }
                   1478:                                                }
                   1479:                                        } else {
                   1480:                                                for(int i = 0; i < 16; i++) {
                   1481:                                                        int len = debugger_dasm(buffer, dasm_seg, dasm_ofs);
                   1482:                                                        telnet_printf("%04X:%04X  ", dasm_seg, dasm_ofs);
                   1483:                                                        for(int i = 0; i < len; i++) {
                   1484:                                                                telnet_printf("%02X", debugger_read_byte(((dasm_seg << 4) + (dasm_ofs + i)) & ADDR_MASK));
                   1485:                                                        }
                   1486:                                                        for(int i = len; i < 8; i++) {
                   1487:                                                                telnet_printf("  ");
                   1488:                                                        }
                   1489:                                                        telnet_printf("  %s\n", buffer);
                   1490:                                                        if((dasm_ofs += len) > 0xffff) {
                   1491:                                                                dasm_seg += 0x1000;
                   1492:                                                                dasm_ofs -= 0x10000;
                   1493:                                                        }
                   1494:                                                }
                   1495:                                        }
                   1496:                                        prev_command[1] = '\0'; // remove parameters to disassemble continuously
                   1497:                                } else {
                   1498:                                        telnet_printf("invalid parameter number\n");
                   1499:                                }
                   1500:                        } else if(stricmp(params[0], "H") == 0) {
                   1501:                                if(num == 3) {
                   1502:                                        UINT32 l = debugger_get_val(params[1]);
                   1503:                                        UINT32 r = debugger_get_val(params[2]);
                   1504:                                        telnet_printf("%08X  %08X\n", l + r, l - r);
                   1505:                                } else {
                   1506:                                        telnet_printf("invalid parameter number\n");
                   1507:                                }
                   1508:                        } else if(stricmp(params[0], "BP") == 0 || stricmp(params[0], "RBP") == 0 || stricmp(params[0], "WBP") == 0) {
                   1509:                                break_point_t *break_point_ptr;
                   1510:                                #define GET_BREAK_POINT_PTR() { \
                   1511:                                        if(params[0][0] == 'R') { \
                   1512:                                                break_point_ptr = &rd_break_point; \
                   1513:                                        } else if(params[0][0] == 'W') { \
                   1514:                                                break_point_ptr = &wr_break_point; \
                   1515:                                        } else if(params[0][0] == 'I') { \
                   1516:                                                break_point_ptr = &in_break_point; \
                   1517:                                        } else if(params[0][0] == 'O') { \
                   1518:                                                break_point_ptr = &out_break_point; \
                   1519:                                        } else { \
                   1520:                                                break_point_ptr = &break_point; \
                   1521:                                        } \
                   1522:                                }
                   1523:                                GET_BREAK_POINT_PTR();
                   1524:                                if(num == 2) {
                   1525:                                        UINT32 seg = debugger_get_seg(params[1], SREG(CS));
                   1526:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1527:                                        bool found = false;
                   1528:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1529:                                                if(break_point_ptr->table[i].status == 0 || break_point_ptr->table[i].addr == ((seg << 4) + ofs)) {
                   1530:                                                        break_point_ptr->table[i].addr = (seg << 4) + ofs;
                   1531:                                                        break_point_ptr->table[i].seg = seg;
                   1532:                                                        break_point_ptr->table[i].ofs = ofs;
                   1533:                                                        break_point_ptr->table[i].status = 1;
                   1534:                                                        found = true;
                   1535:                                                }
                   1536:                                        }
                   1537:                                        if(!found) {
                   1538:                                                telnet_printf("too many break points\n");
                   1539:                                        }
                   1540:                                } else {
                   1541:                                        telnet_printf("invalid parameter number\n");
                   1542:                                }
                   1543:                        } else if(stricmp(params[0], "IBP") == 0 || stricmp(params[0], "OBP") == 0) {
                   1544:                                break_point_t *break_point_ptr;
                   1545:                                GET_BREAK_POINT_PTR();
                   1546:                                if(num == 2) {
                   1547:                                        UINT32 addr = debugger_get_val(params[1]);
                   1548:                                        bool found = false;
                   1549:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1550:                                                if(break_point_ptr->table[i].status == 0 || break_point_ptr->table[i].addr == addr) {
                   1551:                                                        break_point_ptr->table[i].addr = addr;
                   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], "BC") == 0 || stricmp(params[0], "RBC") == 0 || stricmp(params[0], "WBC") == 0 || stricmp(params[0], "IBC") == 0 || stricmp(params[0], "OBC") == 0) {
                   1563:                                break_point_t *break_point_ptr;
                   1564:                                GET_BREAK_POINT_PTR();
                   1565:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1566:                                        memset(break_point_ptr, 0, sizeof(break_point_t));
                   1567:                                } else if(num >= 2) {
                   1568:                                        for(int i = 1; i < num; i++) {
                   1569:                                                int index = debugger_get_val(params[i]);
                   1570:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1571:                                                        telnet_printf("invalid index %x\n", index);
                   1572:                                                } else {
                   1573:                                                        break_point_ptr->table[index - 1].addr = 0;
                   1574:                                                        break_point_ptr->table[index - 1].seg = 0;
                   1575:                                                        break_point_ptr->table[index - 1].ofs = 0;
                   1576:                                                        break_point_ptr->table[index - 1].status = 0;
                   1577:                                                }
                   1578:                                        }
                   1579:                                } else {
                   1580:                                        telnet_printf("invalid parameter number\n");
                   1581:                                }
                   1582:                        } 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 ||
                   1583:                                  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) {
                   1584:                                break_point_t *break_point_ptr;
                   1585:                                GET_BREAK_POINT_PTR();
                   1586:                                bool enabled = (params[0][strlen(params[0]) - 1] == 'E' || params[0][strlen(params[0]) - 1] == 'e');
                   1587:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1588:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1589:                                                if(break_point_ptr->table[i].status != 0) {
                   1590:                                                        break_point_ptr->table[i].status = enabled ? 1 : -1;
                   1591:                                                }
                   1592:                                        }
                   1593:                                } else if(num >= 2) {
                   1594:                                        for(int i = 1; i < num; i++) {
                   1595:                                                int index = debugger_get_val(params[i]);
                   1596:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1597:                                                        telnet_printf("invalid index %x\n", index);
                   1598:                                                } else if(break_point_ptr->table[index - 1].status == 0) {
                   1599:                                                        telnet_printf("break point %x is null\n", index);
                   1600:                                                } else {
                   1601:                                                        break_point_ptr->table[index - 1].status = enabled ? 1 : -1;
                   1602:                                                }
                   1603:                                        }
                   1604:                                } else {
                   1605:                                        telnet_printf("invalid parameter number\n");
                   1606:                                }
                   1607:                        } else if(stricmp(params[0], "BL") == 0 || stricmp(params[0], "RBL") == 0 || stricmp(params[0], "WBL") == 0) {
                   1608:                                break_point_t *break_point_ptr;
                   1609:                                GET_BREAK_POINT_PTR();
                   1610:                                if(num == 1) {
                   1611:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1612:                                                if(break_point_ptr->table[i].status) {
                   1613:                                                        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);
                   1614:                                                }
                   1615:                                        }
                   1616:                                } else {
                   1617:                                        telnet_printf("invalid parameter number\n");
                   1618:                                }
                   1619:                        } else if(stricmp(params[0], "IBL") == 0 || stricmp(params[0], "OBL") == 0) {
                   1620:                                break_point_t *break_point_ptr;
                   1621:                                GET_BREAK_POINT_PTR();
                   1622:                                if(num == 1) {
                   1623:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1624:                                                if(break_point_ptr->table[i].status) {
                   1625:                                                        telnet_printf("%d %c %04X\n", i + 1, break_point_ptr->table[i].status == 1 ? 'e' : 'd', break_point_ptr->table[i].addr);
                   1626:                                                }
                   1627:                                        }
                   1628:                                } else {
                   1629:                                        telnet_printf("invalid parameter number\n");
                   1630:                                }
                   1631:                        } else if(stricmp(params[0], "INTBP") == 0) {
                   1632:                                if(num >= 2 && num <= 4) {
                   1633:                                        int int_num = debugger_get_val(params[1]);
                   1634:                                        UINT8 ah = 0, ah_registered = 0;
                   1635:                                        UINT8 al = 0, al_registered = 0;
                   1636:                                        if(num >= 3) {
                   1637:                                                ah = debugger_get_val(params[2]);
                   1638:                                                ah_registered = 1;
                   1639:                                        }
                   1640:                                        if(num == 4) {
                   1641:                                                al = debugger_get_val(params[3]);
                   1642:                                                al_registered = 1;
                   1643:                                        }
                   1644:                                        bool found = false;
                   1645:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1646:                                                if(int_break_point.table[i].status == 0 || (
                   1647:                                                   int_break_point.table[i].int_num == int_num &&
                   1648:                                                   int_break_point.table[i].ah == ah && int_break_point.table[i].ah_registered == ah_registered &&
                   1649:                                                   int_break_point.table[i].al == al && int_break_point.table[i].al_registered == al_registered)) {
                   1650:                                                        int_break_point.table[i].int_num = int_num;
                   1651:                                                        int_break_point.table[i].ah = ah;
                   1652:                                                        int_break_point.table[i].ah_registered = ah_registered;
                   1653:                                                        int_break_point.table[i].al = al;
                   1654:                                                        int_break_point.table[i].al_registered = al_registered;
                   1655:                                                        int_break_point.table[i].status = 1;
                   1656:                                                        found = true;
                   1657:                                                }
                   1658:                                        }
                   1659:                                        if(!found) {
                   1660:                                                telnet_printf("too many break points\n");
                   1661:                                        }
                   1662:                                } else {
                   1663:                                        telnet_printf("invalid parameter number\n");
                   1664:                                }
                   1665:                        } else if(stricmp(params[0], "INTBC") == 0) {
                   1666:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1667:                                        memset(&int_break_point, 0, sizeof(int_break_point_t));
                   1668:                                } else if(num >= 2) {
                   1669:                                        for(int i = 1; i < num; i++) {
                   1670:                                                int index = debugger_get_val(params[i]);
                   1671:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1672:                                                        telnet_printf("invalid index %x\n", index);
                   1673:                                                } else {
                   1674:                                                        int_break_point.table[index - 1].int_num = 0;
                   1675:                                                        int_break_point.table[index - 1].ah = 0;
                   1676:                                                        int_break_point.table[index - 1].ah_registered = 0;
                   1677:                                                        int_break_point.table[index - 1].al = 0;
                   1678:                                                        int_break_point.table[index - 1].al_registered = 0;
                   1679:                                                        int_break_point.table[index - 1].status = 0;
                   1680:                                                }
                   1681:                                        }
                   1682:                                } else {
                   1683:                                        telnet_printf("invalid parameter number\n");
                   1684:                                }
                   1685:                        } else if(stricmp(params[0], "INTBD") == 0 || stricmp(params[0], "INTBE") == 0) {
                   1686:                                bool enabled = (params[0][strlen(params[0]) - 1] == 'E' || params[0][strlen(params[0]) - 1] == 'e');
                   1687:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1688:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1689:                                                if(int_break_point.table[i].status != 0) {
                   1690:                                                        int_break_point.table[i].status = enabled ? 1 : -1;
                   1691:                                                }
                   1692:                                        }
                   1693:                                } else if(num >= 2) {
                   1694:                                        for(int i = 1; i < num; i++) {
                   1695:                                                int index = debugger_get_val(params[i]);
                   1696:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1697:                                                        telnet_printf("invalid index %x\n", index);
                   1698:                                                } else if(int_break_point.table[index - 1].status == 0) {
                   1699:                                                        telnet_printf("break point %x is null\n", index);
                   1700:                                                } else {
                   1701:                                                        int_break_point.table[index - 1].status = enabled ? 1 : -1;
                   1702:                                                }
                   1703:                                        }
                   1704:                                } else {
                   1705:                                        telnet_printf("invalid parameter number\n");
                   1706:                                }
                   1707:                        } else if(stricmp(params[0], "INTBL") == 0) {
                   1708:                                if(num == 1) {
                   1709:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1710:                                                if(int_break_point.table[i].status) {
                   1711:                                                        telnet_printf("%d %c %02X", i + 1, int_break_point.table[i].status == 1 ? 'e' : 'd', int_break_point.table[i].int_num);
                   1712:                                                        if(int_break_point.table[i].ah_registered) {
                   1713:                                                                telnet_printf(" %02X", int_break_point.table[i].ah);
                   1714:                                                        }
                   1715:                                                        if(int_break_point.table[i].al_registered) {
                   1716:                                                                telnet_printf(" %02X", int_break_point.table[i].al);
                   1717:                                                        }
                   1718:                                                        telnet_printf("\n");
                   1719:                                                }
                   1720:                                        }
                   1721:                                } else {
                   1722:                                        telnet_printf("invalid parameter number\n");
                   1723:                                }
                   1724:                        } else if(stricmp(params[0], "G") == 0 || stricmp(params[0], "P") == 0) {
                   1725:                                if(num == 1 || num == 2) {
                   1726:                                        break_point_t break_point_stored;
                   1727:                                        bool break_points_stored = false;
                   1728:                                        
                   1729:                                        if(stricmp(params[0], "P") == 0) {
                   1730:                                                memcpy(&break_point_stored, &break_point, sizeof(break_point_t));
                   1731:                                                memset(&break_point, 0, sizeof(break_point_t));
                   1732:                                                break_points_stored = true;
                   1733:                                                
                   1734:                                                break_point.table[0].addr = (SREG(CS) << 4) + m_eip + debugger_dasm(buffer, SREG(CS), m_eip);
                   1735:                                                break_point.table[0].status = 1;
                   1736:                                        } else if(num >= 2) {
                   1737:                                                memcpy(&break_point_stored, &break_point, sizeof(break_point_t));
                   1738:                                                memset(&break_point, 0, sizeof(break_point_t));
                   1739:                                                break_points_stored = true;
                   1740:                                                
                   1741:                                                UINT32 seg = debugger_get_seg(params[1], SREG(CS));
                   1742:                                                UINT32 ofs = debugger_get_ofs(params[1]);
                   1743:                                                break_point.table[0].addr = (seg << 4) + ofs;
                   1744:                                                break_point.table[0].seg = seg;
                   1745:                                                break_point.table[0].ofs = ofs;
                   1746:                                                break_point.table[0].status = 1;
                   1747:                                        }
                   1748:                                        break_point.hit = rd_break_point.hit = wr_break_point.hit = in_break_point.hit = out_break_point.hit = int_break_point.hit = 0;
                   1749:                                        now_going = true;
                   1750:                                        now_suspended = false;
                   1751:                                        
                   1752:                                        telnet_command("\033[2l"); // key unlock
                   1753:                                        while(!m_halted && !now_suspended) {
                   1754:                                                if(telnet_kbhit()) {
                   1755:                                                        break;
                   1756:                                                }
                   1757:                                                Sleep(10);
                   1758:                                        }
                   1759:                                        now_going = false;
                   1760:                                        telnet_command("\033[2h"); // key lock
                   1761:                                        
                   1762:                                        if(!(break_point.hit || rd_break_point.hit || wr_break_point.hit || in_break_point.hit || out_break_point.hit || int_break_point.hit)) {
                   1763:                                                Sleep(100);
                   1764:                                                if(!m_halted && !now_suspended) {
                   1765:                                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1766:                                                        telnet_printf("waiting until cpu is suspended...\n");
                   1767:                                                }
                   1768:                                        }
                   1769:                                        while(!m_halted && !now_suspended) {
                   1770:                                                if(telnet_disconnected()) {
                   1771:                                                        break;
                   1772:                                                }
                   1773:                                                Sleep(10);
                   1774:                                        }
                   1775:                                        dasm_seg = SREG(CS);
                   1776:                                        dasm_ofs = m_eip;
                   1777:                                        
                   1778:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1779:                                        debugger_dasm(buffer, m_prev_cs, m_prev_eip);
                   1780:                                        telnet_printf("done\t%04X:%04X  %s\n", m_prev_cs, m_prev_eip, buffer);
                   1781:                                        
                   1782:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1783:                                        debugger_regs_info(buffer);
                   1784:                                        telnet_printf("%s", buffer);
                   1785:                                        
                   1786:                                        if(break_point.hit) {
                   1787:                                                if(stricmp(params[0], "G") == 0 && num == 1) {
                   1788:                                                        telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1789:                                                        telnet_printf("breaked at %04X:%04X: break point is hit\n", SREG(CS), m_eip);
                   1790:                                                }
                   1791:                                        } else if(rd_break_point.hit) {
                   1792:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1793:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1794:                                                rd_break_point.table[rd_break_point.hit - 1].seg, rd_break_point.table[rd_break_point.hit - 1].ofs,
                   1795:                                                m_prev_cs, m_prev_eip);
                   1796:                                        } else if(wr_break_point.hit) {
                   1797:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1798:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1799:                                                wr_break_point.table[wr_break_point.hit - 1].seg, wr_break_point.table[wr_break_point.hit - 1].ofs,
                   1800:                                                m_prev_cs, m_prev_eip);
                   1801:                                        } else if(in_break_point.hit) {
                   1802:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1803:                                                telnet_printf("breaked at %04X:%04X: port %04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1804:                                                in_break_point.table[in_break_point.hit - 1].addr,
                   1805:                                                m_prev_cs, m_prev_eip);
                   1806:                                        } else if(out_break_point.hit) {
                   1807:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1808:                                                telnet_printf("breaked at %04X:%04X: port %04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1809:                                                out_break_point.table[out_break_point.hit - 1].addr,
                   1810:                                                m_prev_cs, m_prev_eip);
                   1811:                                        } else if(int_break_point.hit) {
                   1812:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1813:                                                telnet_printf("breaked at %04X:%04X: INT %02x", SREG(CS), m_eip, int_break_point.table[int_break_point.hit - 1].int_num);
                   1814:                                                if(int_break_point.table[int_break_point.hit - 1].ah_registered) {
                   1815:                                                        telnet_printf(" AH=%02x", int_break_point.table[int_break_point.hit - 1].ah);
                   1816:                                                }
                   1817:                                                if(int_break_point.table[int_break_point.hit - 1].al_registered) {
                   1818:                                                        telnet_printf(" AL=%02x", int_break_point.table[int_break_point.hit - 1].al);
                   1819:                                                }
                   1820:                                                telnet_printf(" is raised at %04X:%04X\n", m_prev_cs, m_prev_eip);
                   1821:                                        } else {
                   1822:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1823:                                                telnet_printf("breaked at %04X:%04X: enter key was pressed\n", SREG(CS), m_eip);
                   1824:                                        }
                   1825:                                        if(break_points_stored) {
                   1826:                                                memcpy(&break_point, &break_point_stored, sizeof(break_point_t));
                   1827:                                        }
                   1828:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1829:                                        debugger_dasm(buffer, SREG(CS), m_eip);
                   1830:                                        telnet_printf("next\t%04X:%04X  %s\n", SREG(CS), m_eip, buffer);
                   1831:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1832:                                } else {
                   1833:                                        telnet_printf("invalid parameter number\n");
                   1834:                                }
                   1835:                        } else if(stricmp(params[0], "T") == 0) {
                   1836:                                if(num == 1 || num == 2) {
                   1837:                                        int steps = 1;
                   1838:                                        if(num >= 2) {
                   1839:                                                steps = debugger_get_val(params[1]);
                   1840:                                        }
                   1841:                                        
                   1842:                                        telnet_command("\033[2l"); // key unlock
                   1843:                                        while(steps-- > 0) {
                   1844:                                                break_point.hit = rd_break_point.hit = wr_break_point.hit = in_break_point.hit = out_break_point.hit = int_break_point.hit = 0;
                   1845:                                                now_going = false;
                   1846:                                                now_suspended = false;
                   1847:                                                
                   1848:                                                while(!m_halted && !now_suspended) {
                   1849:                                                        if(telnet_disconnected()) {
                   1850:                                                                break;
                   1851:                                                        }
                   1852:                                                        Sleep(10);
                   1853:                                                }
                   1854:                                                dasm_seg = SREG(CS);
                   1855:                                                dasm_ofs = m_eip;
                   1856:                                                
                   1857:                                                telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1858:                                                debugger_dasm(buffer, m_prev_cs, m_prev_eip);
                   1859:                                                telnet_printf("done\t%04X:%04X  %s\n", m_prev_cs, m_prev_eip, buffer);
                   1860:                                                
                   1861:                                                telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1862:                                                debugger_regs_info(buffer);
                   1863:                                                telnet_printf("%s", buffer);
                   1864:                                                
                   1865:                                                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()) {
                   1866:                                                        break;
                   1867:                                                }
                   1868:                                        }
                   1869:                                        telnet_command("\033[2h"); // key lock
                   1870:                                        
                   1871:                                        if(!(break_point.hit || rd_break_point.hit || wr_break_point.hit || in_break_point.hit || out_break_point.hit || int_break_point.hit)) {
                   1872:                                                Sleep(100);
                   1873:                                                if(!m_halted && !now_suspended) {
                   1874:                                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1875:                                                        telnet_printf("waiting until cpu is suspended...\n");
                   1876:                                                }
                   1877:                                        }
                   1878:                                        while(!m_halted && !now_suspended) {
                   1879:                                                if(telnet_disconnected()) {
                   1880:                                                        break;
                   1881:                                                }
                   1882:                                                Sleep(10);
                   1883:                                        }
                   1884:                                        if(break_point.hit) {
                   1885:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1886:                                                telnet_printf("breaked at %04X:%04X: break point is hit\n", SREG(CS), m_eip);
                   1887:                                        } else if(rd_break_point.hit) {
                   1888:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1889:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1890:                                                rd_break_point.table[rd_break_point.hit - 1].seg, rd_break_point.table[rd_break_point.hit - 1].ofs,
                   1891:                                                m_prev_cs, m_prev_eip);
                   1892:                                        } else if(wr_break_point.hit) {
                   1893:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1894:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1895:                                                wr_break_point.table[wr_break_point.hit - 1].seg, wr_break_point.table[wr_break_point.hit - 1].ofs,
                   1896:                                                m_prev_cs, m_prev_eip);
                   1897:                                        } else if(in_break_point.hit) {
                   1898:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1899:                                                telnet_printf("breaked at %04X:%04X: port %04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1900:                                                in_break_point.table[in_break_point.hit - 1].addr,
                   1901:                                                m_prev_cs, m_prev_eip);
                   1902:                                        } else if(out_break_point.hit) {
                   1903:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1904:                                                telnet_printf("breaked at %04X:%04X: port %04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1905:                                                out_break_point.table[out_break_point.hit - 1].addr,
                   1906:                                                m_prev_cs, m_prev_eip);
                   1907:                                        } else if(int_break_point.hit) {
                   1908:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1909:                                                telnet_printf("breaked at %04X:%04X: INT %02x", SREG(CS), m_eip, int_break_point.table[int_break_point.hit - 1].int_num);
                   1910:                                                if(int_break_point.table[int_break_point.hit - 1].ah_registered) {
                   1911:                                                        telnet_printf(" AH=%02x", int_break_point.table[int_break_point.hit - 1].ah);
                   1912:                                                }
                   1913:                                                if(int_break_point.table[int_break_point.hit - 1].al_registered) {
                   1914:                                                        telnet_printf(" AL=%02x", int_break_point.table[int_break_point.hit - 1].al);
                   1915:                                                }
                   1916:                                                telnet_printf(" is raised at %04X:%04X\n", m_prev_cs, m_prev_eip);
                   1917:                                        } else if(steps > 0) {
                   1918:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1919:                                                telnet_printf("breaked at %04X:%04X: enter key was pressed\n", SREG(CS), m_eip);
                   1920:                                        }
                   1921:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1922:                                        debugger_dasm(buffer, SREG(CS), m_eip);
                   1923:                                        telnet_printf("next\t%04X:%04X  %s\n", SREG(CS), m_eip, buffer);
                   1924:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1925:                                } else {
                   1926:                                        telnet_printf("invalid parameter number\n");
                   1927:                                }
                   1928:                        } else if(stricmp(params[0], "Q") == 0) {
                   1929:                                break;
                   1930:                        } else if(stricmp(params[0], "X") == 0) {
                   1931:                                debugger_process_info(buffer);
                   1932:                                telnet_printf("%s", buffer);
                   1933:                        } else if(stricmp(params[0], ">") == 0) {
                   1934:                                if(num == 2) {
                   1935:                                        if(fp_debugger != NULL) {
                   1936:                                                fclose(fp_debugger);
                   1937:                                                fp_debugger = NULL;
                   1938:                                        }
                   1939:                                        fp_debugger = fopen(params[1], "w");
                   1940:                                } else {
                   1941:                                        telnet_printf("invalid parameter number\n");
                   1942:                                }
                   1943:                        } else if(stricmp(params[0], "<") == 0) {
                   1944:                                if(num == 2) {
                   1945:                                        if(fi_debugger != NULL) {
                   1946:                                                fclose(fi_debugger);
                   1947:                                                fi_debugger = NULL;
                   1948:                                        }
                   1949:                                        fi_debugger = fopen(params[1], "r");
                   1950:                                } else {
                   1951:                                        telnet_printf("invalid parameter number\n");
                   1952:                                }
                   1953:                        } else if(stricmp(params[0], "?") == 0) {
                   1954:                                telnet_printf("D [<start> [<end>]] - dump memory\n");
                   1955:                                telnet_printf("E[{B,W,D}] <address> <list> - edit memory (byte,word,dword)\n");
                   1956:                                telnet_printf("EA <address> \"<value>\" - edit memory (ascii)\n");
                   1957:                                telnet_printf("I[{B,W,D}] <port> - input port (byte,word,dword)\n");
                   1958:                                telnet_printf("O[{B,W,D}] <port> <value> - output port (byte,word,dword)\n");
                   1959:                                
                   1960:                                telnet_printf("R - show registers\n");
                   1961:                                telnet_printf("R <reg> <value> - edit register\n");
                   1962:                                telnet_printf("S <start> <end> <list> - search\n");
                   1963:                                telnet_printf("U [<start> [<end>]] - unassemble\n");
                   1964:                                
                   1965:                                telnet_printf("H <value> <value> - hexadd\n");
                   1966:                                
                   1967:                                telnet_printf("BP <address> - set breakpoint\n");
                   1968:                                telnet_printf("{R,W}BP <address> - set breakpoint (break at memory access)\n");
                   1969:                                telnet_printf("{I,O}BP <port> - set breakpoint (break at i/o access)\n");
                   1970:                                telnet_printf("INTBP <num> [<ah> [<al>]]- set breakpoint (break at interrupt)\n");
                   1971:                                telnet_printf("[{R,W,I,O,INT}]B{C,D,E} {*,<list>} - clear/disable/enable breakpoint(s)\n");
                   1972:                                telnet_printf("[{R,W,I,O,INT}]BL - list breakpoint(s)\n");
                   1973:                                
                   1974:                                telnet_printf("G - go (press enter key to break)\n");
                   1975:                                telnet_printf("G <address> - go and break at address\n");
                   1976:                                telnet_printf("P - trace one opcode (step over)\n");
                   1977:                                telnet_printf("T [<count>] - trace (step in)\n");
                   1978:                                telnet_printf("Q - quit\n");
                   1979:                                telnet_printf("X - show dos process info\n");
                   1980:                                
                   1981:                                telnet_printf("> <filename> - output logfile\n");
                   1982:                                telnet_printf("< <filename> - input commands from file\n");
                   1983:                                
                   1984:                                telnet_printf("<value> - hexa, decimal(%%d), ascii('a')\n");
                   1985:                                telnet_printf("<list> - <value> [<value> [<value> [...]]]\n");
                   1986:                        } else {
                   1987:                                telnet_printf("unknown command %s\n", params[0]);
                   1988:                        }
                   1989:                }
                   1990:        }
                   1991:        if(fp_debugger != NULL) {
                   1992:                fclose(fp_debugger);
                   1993:                fp_debugger = NULL;
                   1994:        }
                   1995:        if(fi_debugger != NULL) {
                   1996:                fclose(fi_debugger);
                   1997:                fi_debugger = NULL;
                   1998:        }
                   1999:        now_debugging = now_going = now_suspended = force_suspend = false;
                   2000:        closesocket(cli_socket);
                   2001: }
                   2002: 
                   2003: const char *debugger_get_ttermpro_path()
                   2004: {
                   2005:        static char path[MAX_PATH] = {0};
                   2006:        
                   2007:        if(getenv("ProgramFiles")) {
                   2008:                sprintf(path, "%s\\teraterm\\ttermpro.exe", getenv("ProgramFiles"));
                   2009:        }
                   2010:        return(path);
                   2011: }
                   2012: 
                   2013: const char *debugger_get_ttermpro_x86_path()
                   2014: {
                   2015:        static char path[MAX_PATH] = {0};
                   2016:        
                   2017:        if(getenv("ProgramFiles(x86)")) {
                   2018:                sprintf(path, "%s\\teraterm\\ttermpro.exe", getenv("ProgramFiles(x86)"));
                   2019:        }
                   2020:        return(path);
                   2021: }
                   2022: 
                   2023: const char *debugger_get_putty_path()
                   2024: {
                   2025:        static char path[MAX_PATH] = {0};
                   2026:        
                   2027:        if(getenv("ProgramFiles")) {
                   2028:                sprintf(path, "%s\\PuTTY\\putty.exe", getenv("ProgramFiles"));
                   2029:        }
                   2030:        return(path);
                   2031: }
                   2032: 
                   2033: const char *debugger_get_putty_x86_path()
                   2034: {
                   2035:        static char path[MAX_PATH] = {0};
                   2036:        
                   2037:        if(getenv("ProgramFiles(x86)")) {
                   2038:                sprintf(path, "%s\\PuTTY\\putty.exe", getenv("ProgramFiles(x86)"));
                   2039:        }
                   2040:        return(path);
                   2041: }
                   2042: 
                   2043: const char *debugger_get_telnet_path()
                   2044: {
                   2045:        // NOTE: When you run 32bit version of msdos.exe on Windows x64,
                   2046:        // C:\Windows\System32\telnet.exe is redirected to telnet.exe in SysWOW64.
                   2047:        // But 32bit version of telnet.exe will not be installed in SysWOW64
                   2048:        // and 64bit version of telnet.exe will be installed in System32.
                   2049:        static char path[MAX_PATH] = {0};
                   2050:        
                   2051:        if(getenv("windir") != NULL) {
                   2052:                sprintf(path, "%s\\System32\\telnet.exe", getenv("windir"));
                   2053:        }
                   2054:        return(path);
                   2055: }
                   2056: 
                   2057: DWORD WINAPI debugger_thread(LPVOID)
                   2058: {
                   2059:        WSADATA was_data;
                   2060:        struct sockaddr_in svr_addr;
                   2061:        struct sockaddr_in cli_addr;
                   2062:        int cli_addr_len = sizeof(cli_addr);
                   2063:        int port = 23;
                   2064:        int bind_stat = SOCKET_ERROR;
                   2065:        struct timeval timeout;
                   2066:        
                   2067:        WSAStartup(MAKEWORD(2,0), &was_data);
                   2068:        
                   2069:        if((svr_socket = socket(AF_INET, SOCK_STREAM, 0)) != INVALID_SOCKET) {
                   2070:                memset(&svr_addr, 0, sizeof(svr_addr));
                   2071:                svr_addr.sin_family = AF_INET;
                   2072:                svr_addr.sin_addr.s_addr = htonl(INADDR_ANY);
                   2073:                
                   2074:                while(!m_halted && port < 10000) {
                   2075:                        svr_addr.sin_port = htons(port);
                   2076:                        if((bind_stat = bind(svr_socket, (struct sockaddr *)&svr_addr, sizeof(svr_addr))) == 0) {
                   2077:                                break;
                   2078:                        } else {
                   2079:                                port = (port == 23) ? 9000 : (port + 1);
                   2080:                        }
                   2081:                }
                   2082:                if(bind_stat == 0) {
                   2083:                        timeout.tv_sec = 1;
                   2084:                        timeout.tv_usec = 0;
                   2085:                        setsockopt(svr_socket, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(timeout));
                   2086:                        
                   2087:                        listen(svr_socket, 1);
                   2088:                        
                   2089:                        char command[MAX_PATH] = {0};
                   2090:                        STARTUPINFO si;
                   2091:                        PROCESS_INFORMATION pi;
                   2092:                        
                   2093:                        if(_access(debugger_get_ttermpro_path(), 0) == 0) {
                   2094:                                sprintf(command, "%s localhost:%d /T=1", debugger_get_ttermpro_path(), port);
                   2095:                        } else if(_access(debugger_get_ttermpro_x86_path(), 0) == 0) {
                   2096:                                sprintf(command, "%s localhost:%d /T=1", debugger_get_ttermpro_x86_path(), port);
                   2097:                        } else if(_access(debugger_get_putty_path(), 0) == 0) {
                   2098:                                sprintf(command, "%s -telnet localhost %d", debugger_get_putty_path(), port);
                   2099:                        } else if(_access(debugger_get_putty_x86_path(), 0) == 0) {
                   2100:                                sprintf(command, "%s -telnet localhost %d", debugger_get_putty_x86_path(), port);
                   2101:                        } else if(_access(debugger_get_telnet_path(), 0) == 0) {
                   2102:                                sprintf(command, "%s -t vt100 localhost %d", debugger_get_telnet_path(), port);
                   2103:                        }
                   2104:                        if(command[0] != '\0') {
                   2105:                                memset(&si, 0, sizeof(STARTUPINFO));
                   2106:                                memset(&pi, 0, sizeof(PROCESS_INFORMATION));
                   2107:                                CreateProcess(NULL, command, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
                   2108:                        }
                   2109:                        
                   2110:                        while(!m_halted) {
                   2111:                                if((cli_socket = accept(svr_socket, (struct sockaddr *) &cli_addr, &cli_addr_len)) != INVALID_SOCKET) {
                   2112:                                        u_long val = 1;
                   2113:                                        ioctlsocket(cli_socket, FIONBIO, &val);
                   2114:                                        debugger_main();
                   2115:                                }
                   2116:                        }
                   2117:                }
                   2118:        }
                   2119:        WSACleanup();
                   2120:        return(0);
                   2121: }
                   2122: #endif
                   2123: 
                   2124: /* ----------------------------------------------------------------------------
1.1       root     2125:        main
                   2126: ---------------------------------------------------------------------------- */
                   2127: 
1.1.1.28  root     2128: BOOL WINAPI ctrl_handler(DWORD dwCtrlType)
                   2129: {
                   2130:        if(dwCtrlType == CTRL_BREAK_EVENT) {
1.1.1.33  root     2131:                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     2132: #ifdef USE_SERVICE_THREAD
                   2133:                        EnterCriticalSection(&key_buf_crit_sect);
                   2134: #endif
1.1.1.33  root     2135:                        key_buf_char->clear();
                   2136:                        key_buf_scan->clear();
1.1.1.35  root     2137: #ifdef USE_SERVICE_THREAD
                   2138:                        LeaveCriticalSection(&key_buf_crit_sect);
                   2139: #endif
1.1.1.33  root     2140:                }
                   2141: //             key_code = key_recv = 0;
1.1.1.28  root     2142:                return TRUE;
                   2143:        } else if(dwCtrlType == CTRL_C_EVENT) {
                   2144:                return TRUE;
                   2145:        } else if(dwCtrlType == CTRL_CLOSE_EVENT) {
                   2146:                // this program will be terminated abnormally, do minimum end process
                   2147:                exit_handler();
                   2148:                exit(1);
                   2149:        }
                   2150:        return FALSE;
                   2151: }
                   2152: 
                   2153: void exit_handler()
                   2154: {
                   2155:        if(temp_file_created) {
                   2156:                DeleteFile(temp_file_path);
                   2157:                temp_file_created = false;
                   2158:        }
                   2159:        if(key_buf_char != NULL) {
                   2160:                key_buf_char->release();
                   2161:                delete key_buf_char;
                   2162:                key_buf_char = NULL;
                   2163:        }
                   2164:        if(key_buf_scan != NULL) {
                   2165:                key_buf_scan->release();
                   2166:                delete key_buf_scan;
                   2167:                key_buf_scan = NULL;
                   2168:        }
1.1.1.32  root     2169: #ifdef SUPPORT_XMS
                   2170:        msdos_xms_release();
                   2171: #endif
1.1.1.28  root     2172:        hardware_release();
                   2173: }
                   2174: 
1.1.1.35  root     2175: #ifdef USE_VRAM_THREAD
1.1.1.28  root     2176: DWORD WINAPI vram_thread(LPVOID)
                   2177: {
                   2178:        while(!m_halted) {
                   2179:                EnterCriticalSection(&vram_crit_sect);
                   2180:                if(vram_length_char != 0 && vram_length_char == vram_last_length_char) {
                   2181:                        vram_flush_char();
                   2182:                }
                   2183:                if(vram_length_attr != 0 && vram_length_attr == vram_last_length_attr) {
                   2184:                        vram_flush_attr();
                   2185:                }
                   2186:                vram_last_length_char = vram_length_char;
                   2187:                vram_last_length_attr = vram_length_attr;
                   2188:                LeaveCriticalSection(&vram_crit_sect);
                   2189:                // this is about half the maximum keyboard repeat rate - any
                   2190:                // lower tends to be jerky, any higher misses updates
                   2191:                Sleep(15);
                   2192:        }
                   2193:        return 0;
                   2194: }
                   2195: #endif
                   2196: 
                   2197: long get_section_in_exec_file(FILE *fp, char *name)
                   2198: {
                   2199:        UINT8 header[0x400];
                   2200:        
                   2201:        long position = ftell(fp);
                   2202:        fseek(fp, 0, SEEK_SET);
                   2203:        fread(header, sizeof(header), 1, fp);
                   2204:        fseek(fp, position, SEEK_SET);
                   2205:        
                   2206:        try {
                   2207:                _IMAGE_DOS_HEADER *dosHeader = (_IMAGE_DOS_HEADER *)(header + 0);
                   2208:                DWORD dwTopOfSignature = dosHeader->e_lfanew;
                   2209:                DWORD dwTopOfCoffHeader = dwTopOfSignature + 4;
                   2210:                _IMAGE_FILE_HEADER *coffHeader = (_IMAGE_FILE_HEADER *)(header + dwTopOfCoffHeader);
                   2211:                DWORD dwTopOfOptionalHeader = dwTopOfCoffHeader + sizeof(_IMAGE_FILE_HEADER);
                   2212:                DWORD dwTopOfFirstSectionHeader = dwTopOfOptionalHeader + coffHeader->SizeOfOptionalHeader;
                   2213:                
                   2214:                for(int i = 0; i < coffHeader->NumberOfSections; i++) {
                   2215:                        _IMAGE_SECTION_HEADER *sectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * i);
                   2216:                        if(memcmp(sectionHeader->Name, name, strlen(name)) == 0) {
                   2217:                                return(sectionHeader->PointerToRawData);
                   2218:                        }
                   2219:                }
                   2220:        } catch(...) {
                   2221:        }
                   2222:        return(0);
                   2223: }
                   2224: 
1.1.1.10  root     2225: bool is_started_from_command_prompt()
                   2226: {
1.1.1.18  root     2227:        bool ret = false;
                   2228:        
                   2229:        HMODULE hLibrary = LoadLibrary(_T("Kernel32.dll"));
                   2230:        if(hLibrary) {
                   2231:                typedef DWORD (WINAPI *GetConsoleProcessListFunction)(__out LPDWORD lpdwProcessList, __in DWORD dwProcessCount);
                   2232:                GetConsoleProcessListFunction lpfnGetConsoleProcessList;
                   2233:                lpfnGetConsoleProcessList = reinterpret_cast<GetConsoleProcessListFunction>(::GetProcAddress(hLibrary, "GetConsoleProcessList"));
                   2234:                if(lpfnGetConsoleProcessList) {
                   2235:                        DWORD pl;
                   2236:                        ret = (lpfnGetConsoleProcessList(&pl, 1) > 1);
                   2237:                        FreeLibrary(hLibrary);
                   2238:                        return(ret);
                   2239:                }
                   2240:                FreeLibrary(hLibrary);
                   2241:        }
                   2242:        
                   2243:        HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
                   2244:        if(hSnapshot != INVALID_HANDLE_VALUE) {
                   2245:                DWORD dwParentProcessID = 0;
                   2246:                PROCESSENTRY32 pe32;
                   2247:                pe32.dwSize = sizeof(PROCESSENTRY32);
                   2248:                if(Process32First(hSnapshot, &pe32)) {
                   2249:                        do {
                   2250:                                if(pe32.th32ProcessID == GetCurrentProcessId()) {
                   2251:                                        dwParentProcessID = pe32.th32ParentProcessID;
                   2252:                                        break;
                   2253:                                }
                   2254:                        } while(Process32Next(hSnapshot, &pe32));
                   2255:                }
                   2256:                CloseHandle(hSnapshot);
                   2257:                if(dwParentProcessID != 0) {
                   2258:                        HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwParentProcessID);
                   2259:                        if(hProcess != NULL) {
                   2260:                                HMODULE hMod;
                   2261:                                DWORD cbNeeded;
                   2262:                                if(EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded)) {
                   2263:                                        char module_name[MAX_PATH];
                   2264:                                        if(GetModuleBaseName(hProcess, hMod, module_name, sizeof(module_name))) {
                   2265:                                                ret = (_strnicmp(module_name, "cmd.exe", 7) == 0);
                   2266:                                        }
                   2267:                                }
                   2268:                                CloseHandle(hProcess);
                   2269:                        }
                   2270:                }
                   2271:        }
                   2272:        return(ret);
1.1.1.14  root     2273: }
                   2274: 
                   2275: BOOL is_greater_windows_version(DWORD dwMajorVersion, DWORD dwMinorVersion, WORD wServicePackMajor, WORD wServicePackMinor)
                   2276: {
1.1.1.24  root     2277:        // https://msdn.microsoft.com/en-us/library/windows/desktop/ms725491(v=vs.85).aspx
1.1.1.14  root     2278:        OSVERSIONINFOEX osvi;
                   2279:        DWORDLONG dwlConditionMask = 0;
                   2280:        int op = VER_GREATER_EQUAL;
                   2281:        
                   2282:        // Initialize the OSVERSIONINFOEX structure.
                   2283:        ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
                   2284:        osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
                   2285:        osvi.dwMajorVersion = dwMajorVersion;
                   2286:        osvi.dwMinorVersion = dwMinorVersion;
                   2287:        osvi.wServicePackMajor = wServicePackMajor;
                   2288:        osvi.wServicePackMinor = wServicePackMinor;
                   2289:        
                   2290:         // Initialize the condition mask.
                   2291:        VER_SET_CONDITION( dwlConditionMask, VER_MAJORVERSION, op );
                   2292:        VER_SET_CONDITION( dwlConditionMask, VER_MINORVERSION, op );
                   2293:        VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMAJOR, op );
                   2294:        VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMINOR, op );
                   2295:        
                   2296:        // Perform the test.
                   2297:        return VerifyVersionInfo(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR, dwlConditionMask);
                   2298: }
                   2299: 
1.1.1.27  root     2300: void get_sio_port_numbers()
                   2301: {
                   2302:        SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)};
                   2303:        HDEVINFO hDevInfo = 0;
                   2304:        HKEY hKey = 0;
                   2305:        if((hDevInfo = SetupDiGetClassDevs(&GUID_DEVINTERFACE_COMPORT, NULL, NULL, (DIGCF_PRESENT | DIGCF_DEVICEINTERFACE))) != 0) {
                   2306:                for(int i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++) {
                   2307:                        if((hKey = SetupDiOpenDevRegKey(hDevInfo, &DeviceInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_QUERY_VALUE)) != INVALID_HANDLE_VALUE) {
                   2308:                                char chData[256];
                   2309:                                DWORD dwType = 0;
                   2310:                                DWORD dwSize = sizeof(chData);
                   2311:                                int port_number = 0;
                   2312:                                
                   2313:                                if(RegQueryValueEx(hKey, _T("PortName"), NULL, &dwType, (BYTE *)chData, &dwSize) == ERROR_SUCCESS) {
                   2314:                                        if(_strnicmp(chData, "COM", 3) == 0) {
                   2315:                                                port_number = atoi(chData + 3);
                   2316:                                        }
                   2317:                                }
                   2318:                                RegCloseKey(hKey);
                   2319:                                
1.1.1.29  root     2320:                                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     2321:                                        continue;
                   2322:                                }
                   2323:                                if(sio_port_number[0] == 0) {
                   2324:                                        sio_port_number[0] = port_number;
                   2325:                                } else if(sio_port_number[1] == 0) {
                   2326:                                        sio_port_number[1] = port_number;
1.1.1.29  root     2327:                                } else if(sio_port_number[2] == 0) {
                   2328:                                        sio_port_number[2] = port_number;
                   2329:                                } else if(sio_port_number[3] == 0) {
                   2330:                                        sio_port_number[3] = port_number;
1.1.1.27  root     2331:                                }
1.1.1.29  root     2332:                                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     2333:                                        break;
                   2334:                                }
                   2335:                        }
                   2336:                }
                   2337:        }
                   2338: }
                   2339: 
1.1.1.28  root     2340: #define IS_NUMERIC(c) ((c) >= '0' && (c) <= '9')
                   2341: 
1.1       root     2342: int main(int argc, char *argv[], char *envp[])
                   2343: {
1.1.1.9   root     2344:        int arg_offset = 0;
                   2345:        int standard_env = 0;
1.1.1.14  root     2346:        int buf_width = 0, buf_height = 0;
1.1.1.28  root     2347:        bool get_console_info_success = false;
                   2348:        bool screen_size_changed = false;
                   2349:        
                   2350:        _TCHAR path[MAX_PATH], full[MAX_PATH], *name = NULL;
                   2351:        GetModuleFileName(NULL, path, MAX_PATH);
                   2352:        GetFullPathName(path, MAX_PATH, full, &name);
1.1       root     2353:        
1.1.1.27  root     2354:        char dummy_argv_0[] = "msdos.exe";
                   2355:        char dummy_argv_1[MAX_PATH];
                   2356:        char *dummy_argv[256] = {dummy_argv_0, dummy_argv_1, 0};
                   2357:        char new_exec_file[MAX_PATH];
                   2358:        bool convert_cmd_file = false;
1.1.1.28  root     2359:        unsigned int code_page = 0;
1.1.1.27  root     2360:        
                   2361:        if(name != NULL && stricmp(name, "msdos.exe") != 0) {
1.1.1.28  root     2362:                // check if command file is embedded to this execution file
                   2363:                // if this execution file name is msdos.exe, don't check
1.1.1.27  root     2364:                FILE* fp = fopen(full, "rb");
1.1.1.28  root     2365:                long offset = get_section_in_exec_file(fp, ".msdos");
                   2366:                if(offset != 0) {
1.1.1.30  root     2367:                        UINT8 buffer[16];
1.1.1.28  root     2368:                        fseek(fp, offset, SEEK_SET);
                   2369:                        fread(buffer, sizeof(buffer), 1, fp);
                   2370:                        
                   2371:                        // restore flags
                   2372:                        stay_busy           = ((buffer[0] & 0x01) != 0);
                   2373:                        no_windows          = ((buffer[0] & 0x02) != 0);
                   2374:                        standard_env        = ((buffer[0] & 0x04) != 0);
                   2375:                        ignore_illegal_insn = ((buffer[0] & 0x08) != 0);
                   2376:                        limit_max_memory    = ((buffer[0] & 0x10) != 0);
                   2377:                        if((buffer[0] & 0x20) != 0) {
                   2378:                                get_sio_port_numbers();
                   2379:                        }
                   2380:                        if((buffer[0] & 0x40) != 0) {
                   2381:                                UMB_TOP = EMS_TOP + EMS_SIZE;
                   2382:                                support_ems = true;
1.1.1.30  root     2383:                        }
1.1.1.27  root     2384: #ifdef SUPPORT_XMS
1.1.1.30  root     2385:                        if((buffer[0] & 0x80) != 0) {
1.1.1.28  root     2386:                                support_xms = true;
                   2387:                        }
1.1.1.30  root     2388: #endif
1.1.1.28  root     2389:                        if((buffer[1] != 0 || buffer[2] != 0) && (buffer[3] != 0 || buffer[4] != 0)) {
                   2390:                                buf_width  = buffer[1] | (buffer[2] << 8);
                   2391:                                buf_height = buffer[3] | (buffer[4] << 8);
                   2392:                        }
                   2393:                        if(buffer[5] != 0) {
1.1.1.30  root     2394:                                dos_major_version = buffer[5];
                   2395:                                dos_minor_version = buffer[6];
                   2396:                        }
                   2397:                        if(buffer[7] != 0) {
                   2398:                                win_major_version = buffer[7];
                   2399:                                win_minor_version = buffer[8];
1.1.1.28  root     2400:                        }
1.1.1.30  root     2401:                        if((code_page = buffer[9] | (buffer[10] << 8)) != 0) {
1.1.1.28  root     2402:                                SetConsoleCP(code_page);
                   2403:                                SetConsoleOutputCP(code_page);
                   2404:                        }
1.1.1.30  root     2405:                        int name_len = buffer[11];
                   2406:                        int file_len = buffer[12] | (buffer[13] << 8) | (buffer[14] << 16) | (buffer[15] << 24);
1.1.1.28  root     2407:                        
                   2408:                        // restore command file name
                   2409:                        memset(dummy_argv_1, 0, sizeof(dummy_argv_1));
                   2410:                        fread(dummy_argv_1, name_len, 1, fp);
                   2411:                        
                   2412:                        if(name_len != 0 && _access(dummy_argv_1, 0) == 0) {
                   2413:                                // if original command file exists, create a temporary file name
                   2414:                                if(GetTempFileName(_T("."), _T("DOS"), 0, dummy_argv_1) != 0) {
                   2415:                                        // create a temporary command file in the current director
                   2416:                                        DeleteFile(dummy_argv_1);
1.1.1.27  root     2417:                                } else {
1.1.1.28  root     2418:                                        // create a temporary command file in the temporary folder
                   2419:                                        GetTempPath(MAX_PATH, path);
                   2420:                                        if(GetTempFileName(path, _T("DOS"), 0, dummy_argv_1) != 0) {
                   2421:                                                DeleteFile(dummy_argv_1);
                   2422:                                        } else {
                   2423:                                                sprintf(dummy_argv_1, "%s$DOSPRG$.TMP", path);
                   2424:                                        }
1.1.1.27  root     2425:                                }
1.1.1.28  root     2426:                                // check the command file type
                   2427:                                fread(buffer, 2, 1, fp);
                   2428:                                fseek(fp, -2, SEEK_CUR);
                   2429:                                if(memcmp(buffer, "MZ", 2) != 0) {
                   2430:                                        memcpy(dummy_argv_1 + strlen(dummy_argv_1) - 4, ".COM", 4);
                   2431:                                } else {
                   2432:                                        memcpy(dummy_argv_1 + strlen(dummy_argv_1) - 4, ".EXE", 4);
1.1.1.27  root     2433:                                }
                   2434:                        }
1.1.1.28  root     2435:                        
                   2436:                        // restore command file
                   2437:                        FILE* fo = fopen(dummy_argv_1, "wb");
                   2438:                        for(int i = 0; i < file_len; i++) {
                   2439:                                fputc(fgetc(fp), fo);
                   2440:                        }
                   2441:                        fclose(fo);
                   2442:                        
                   2443:                        GetFullPathName(dummy_argv_1, MAX_PATH, temp_file_path, NULL);
                   2444:                        temp_file_created = true;
                   2445:                        SetFileAttributes(temp_file_path, FILE_ATTRIBUTE_HIDDEN);
                   2446:                        
                   2447:                        // adjust argc/argv
                   2448:                        for(int i = 1; i < argc && (i + 1) < 256; i++) {
                   2449:                                dummy_argv[i + 1] = argv[i];
                   2450:                        }
                   2451:                        argc++;
                   2452:                        argv = dummy_argv;
1.1.1.27  root     2453:                }
                   2454:                fclose(fp);
                   2455:        }
1.1.1.9   root     2456:        for(int i = 1; i < argc; i++) {
1.1.1.25  root     2457:                if(_strnicmp(argv[i], "-b", 2) == 0) {
                   2458:                        stay_busy = true;
                   2459:                        arg_offset++;
1.1.1.27  root     2460:                } else if(_strnicmp(argv[i], "-c", 2) == 0) {
                   2461:                        if(argv[i][2] != '\0') {
                   2462:                                strcpy(new_exec_file, &argv[i][2]);
                   2463:                        } else {
                   2464:                                strcpy(new_exec_file, "new_exec_file.exe");
                   2465:                        }
                   2466:                        convert_cmd_file = true;
                   2467:                        arg_offset++;
1.1.1.28  root     2468:                } else if(_strnicmp(argv[i], "-p", 2) == 0) {
                   2469:                        if(IS_NUMERIC(argv[i][2])) {
                   2470:                                code_page = atoi(&argv[i][2]);
                   2471:                        } else {
                   2472:                                code_page = GetConsoleCP();
                   2473:                        }
                   2474:                        arg_offset++;
1.1.1.25  root     2475:                } else if(_strnicmp(argv[i], "-d", 2) == 0) {
                   2476:                        no_windows = true;
                   2477:                        arg_offset++;
                   2478:                } else if(_strnicmp(argv[i], "-e", 2) == 0) {
1.1.1.9   root     2479:                        standard_env = 1;
                   2480:                        arg_offset++;
1.1.1.14  root     2481:                } else if(_strnicmp(argv[i], "-i", 2) == 0) {
                   2482:                        ignore_illegal_insn = true;
                   2483:                        arg_offset++;
                   2484:                } else if(_strnicmp(argv[i], "-m", 2) == 0) {
                   2485:                        limit_max_memory = true;
                   2486:                        arg_offset++;
                   2487:                } else if(_strnicmp(argv[i], "-n", 2) == 0) {
1.1.1.17  root     2488:                        if(sscanf(argv[1] + 2, "%d,%d", &buf_height, &buf_width) != 2) {
                   2489:                                buf_width = buf_height = 0;
                   2490:                        }
                   2491:                        if(buf_width <= 0 || buf_width > 0x7fff) {
                   2492:                                buf_width = 80;
                   2493:                        }
                   2494:                        if(buf_height <= 0 || buf_height > 0x7fff) {
                   2495:                                buf_height = 25;
                   2496:                        }
1.1.1.14  root     2497:                        arg_offset++;
1.1.1.25  root     2498:                } else if(_strnicmp(argv[i], "-s", 2) == 0) {
1.1.1.28  root     2499:                        if(IS_NUMERIC(argv[i][2])) {
1.1.1.29  root     2500:                                char *p0 = &argv[i][2], *p1, *p2, *p3;
                   2501:                                if((p1 = strchr(p0, ',')) != NULL && IS_NUMERIC(p1[1])) {
                   2502:                                        sio_port_number[1] = atoi(p1 + 1);
                   2503:                                        if((p2 = strchr(p1, ',')) != NULL && IS_NUMERIC(p2[1])) {
                   2504:                                                sio_port_number[2] = atoi(p2 + 1);
                   2505:                                                if((p3 = strchr(p2, ',')) != NULL && IS_NUMERIC(p3[1])) {
                   2506:                                                        sio_port_number[3] = atoi(p3 + 1);
                   2507:                                                }
                   2508:                                        }
1.1.1.25  root     2509:                                }
1.1.1.29  root     2510:                                sio_port_number[0] = atoi(p0);
1.1.1.25  root     2511:                        }
1.1.1.29  root     2512:                        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     2513:                                get_sio_port_numbers();
1.1.1.25  root     2514:                        }
                   2515:                        arg_offset++;
1.1.1.9   root     2516:                } else if(_strnicmp(argv[i], "-v", 2) == 0) {
1.1.1.17  root     2517:                        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     2518:                                dos_major_version = argv[i][2] - '0';
                   2519:                                dos_minor_version = (argv[i][4] - '0') * 10 + (argv[i][5] ? (argv[i][5] - '0') : 0);
                   2520:                        }
                   2521:                        arg_offset++;
                   2522:                } else if(_strnicmp(argv[i], "-w", 2) == 0) {
                   2523:                        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]))) {
                   2524:                                win_major_version = argv[i][2] - '0';
                   2525:                                win_minor_version = (argv[i][4] - '0') * 10 + (argv[i][5] ? (argv[i][5] - '0') : 0);
1.1.1.9   root     2526:                        }
                   2527:                        arg_offset++;
1.1.1.25  root     2528:                } else if(_strnicmp(argv[i], "-x", 2) == 0) {
                   2529:                        UMB_TOP = EMS_TOP + EMS_SIZE;
                   2530:                        support_ems = true;
                   2531: #ifdef SUPPORT_XMS
                   2532:                        support_xms = true;
                   2533: #endif
                   2534:                        arg_offset++;
1.1.1.9   root     2535:                } else {
                   2536:                        break;
                   2537:                }
                   2538:        }
                   2539:        if(argc < 2 + arg_offset) {
1.1       root     2540: #ifdef _WIN64
1.1.1.14  root     2541:                fprintf(stderr, "MS-DOS Player (" CPU_MODEL_NAME(CPU_MODEL) ") for Win32-x64 console\n\n");
1.1       root     2542: #else
1.1.1.14  root     2543:                fprintf(stderr, "MS-DOS Player (" CPU_MODEL_NAME(CPU_MODEL) ") for Win32 console\n\n");
1.1       root     2544: #endif
1.1.1.25  root     2545:                fprintf(stderr,
1.1.1.28  root     2546:                        "Usage: MSDOS [-b] [-c[(new exec file)] [-p[P]]] [-d] [-e] [-i] [-m] [-n[L[,C]]]\n"
1.1.1.30  root     2547:                        "             [-s[P1[,P2[,P3[,P4]]]]] [-vX.XX] [-wX.XX] [-x] (command) [options]\n"
1.1.1.25  root     2548:                        "\n"
                   2549:                        "\t-b\tstay busy during keyboard polling\n"
1.1.1.28  root     2550: #ifdef _WIN64
1.1.1.27  root     2551:                        "\t-c\tconvert command file to 64bit execution file\n"
1.1.1.28  root     2552: #else
1.1.1.27  root     2553:                        "\t-c\tconvert command file to 32bit execution file\n"
                   2554: #endif
1.1.1.28  root     2555:                        "\t-p\trecord current code page when convert command file\n"
1.1.1.25  root     2556:                        "\t-d\tpretend running under straight DOS, not Windows\n"
                   2557:                        "\t-e\tuse a reduced environment block\n"
                   2558:                        "\t-i\tignore invalid instructions\n"
                   2559:                        "\t-m\trestrict free memory to 0x7FFF paragraphs\n"
                   2560:                        "\t-n\tcreate a new buffer (25 lines, 80 columns by default)\n"
                   2561:                        "\t-s\tenable serial I/O and set host's COM port numbers\n"
                   2562:                        "\t-v\tset the DOS version\n"
1.1.1.30  root     2563:                        "\t-w\tset the Windows version\n"
1.1.1.19  root     2564: #ifdef SUPPORT_XMS
1.1.1.28  root     2565:                        "\t-x\tenable XMS and LIM EMS\n"
1.1.1.19  root     2566: #else
1.1.1.28  root     2567:                        "\t-x\tenable LIM EMS\n"
1.1.1.19  root     2568: #endif
                   2569:                );
1.1.1.10  root     2570:                
                   2571:                if(!is_started_from_command_prompt()) {
                   2572:                        fprintf(stderr, "\nStart this program from a command prompt!\n\nHit any key to quit...");
                   2573:                        while(!_kbhit()) {
                   2574:                                Sleep(10);
                   2575:                        }
                   2576:                }
1.1.1.20  root     2577: #ifdef _DEBUG
                   2578:                _CrtDumpMemoryLeaks();
                   2579: #endif
1.1       root     2580:                return(EXIT_FAILURE);
                   2581:        }
1.1.1.27  root     2582:        if(convert_cmd_file) {
                   2583:                retval = EXIT_FAILURE;
1.1.1.28  root     2584:                if(name != NULL/* && stricmp(name, "msdos.exe") == 0*/) {
1.1.1.27  root     2585:                        FILE *fp = NULL, *fs = NULL, *fo = NULL;
1.1.1.28  root     2586:                        int len = strlen(argv[arg_offset + 1]), data;
1.1.1.27  root     2587:                        
1.1.1.28  root     2588:                        if(!(len > 4 && (stricmp(argv[arg_offset + 1] + len - 4, ".COM") == 0 || stricmp(argv[arg_offset + 1] + len - 4, ".EXE") == 0))) {
                   2589:                                fprintf(stderr, "Specify command file with extenstion (.COM or .EXE)\n");
                   2590:                        } else if((fp = fopen(full, "rb")) == NULL) {
                   2591:                                fprintf(stderr, "Can't open '%s'\n", name);
1.1.1.27  root     2592:                        } else {
1.1.1.28  root     2593:                                long offset = get_section_in_exec_file(fp, ".msdos");
                   2594:                                if(offset != 0) {
                   2595:                                        UINT8 buffer[14];
                   2596:                                        fseek(fp, offset, SEEK_SET);
                   2597:                                        fread(buffer, sizeof(buffer), 1, fp);
                   2598:                                        memset(path, 0, sizeof(path));
                   2599:                                        fread(path, buffer[9], 1, fp);
                   2600:                                        fprintf(stderr, "Command file '%s' was already embedded to '%s'\n", path, name);
                   2601:                                } else if((fs = fopen(argv[arg_offset + 1], "rb")) == NULL) {
                   2602:                                        fprintf(stderr, "Can't open '%s'\n", argv[arg_offset + 1]);
                   2603:                                } else if((fo = fopen(new_exec_file, "wb")) == NULL) {
                   2604:                                        fprintf(stderr, "Can't open '%s'\n", new_exec_file);
                   2605:                                } else {
                   2606:                                        // read pe header of msdos.exe
                   2607:                                        UINT8 header[0x400];
                   2608:                                        fseek(fp, 0, SEEK_SET);
                   2609:                                        fread(header, sizeof(header), 1, fp);
                   2610:                                        
                   2611:                                        _IMAGE_DOS_HEADER *dosHeader = (_IMAGE_DOS_HEADER *)(header + 0);
                   2612:                                        DWORD dwTopOfSignature = dosHeader->e_lfanew;
                   2613:                                        DWORD dwTopOfCoffHeader = dwTopOfSignature + 4;
                   2614:                                        _IMAGE_FILE_HEADER *coffHeader = (_IMAGE_FILE_HEADER *)(header + dwTopOfCoffHeader);
                   2615:                                        DWORD dwTopOfOptionalHeader = dwTopOfCoffHeader + sizeof(_IMAGE_FILE_HEADER);
                   2616:                                        _IMAGE_OPTIONAL_HEADER *optionalHeader = (_IMAGE_OPTIONAL_HEADER *)(header + dwTopOfOptionalHeader);
                   2617:                                        DWORD dwTopOfFirstSectionHeader = dwTopOfOptionalHeader + coffHeader->SizeOfOptionalHeader;
                   2618:                                        
                   2619:                                        _IMAGE_SECTION_HEADER *lastSectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * (coffHeader->NumberOfSections - 1));
                   2620:                                        DWORD dwEndOfFile = lastSectionHeader->PointerToRawData + lastSectionHeader->SizeOfRawData;
                   2621:                                        DWORD dwLastSectionSize = lastSectionHeader->SizeOfRawData;
                   2622:                                        DWORD dwExtraLastSectionBytes = dwLastSectionSize % optionalHeader->SectionAlignment;
                   2623:                                        if(dwExtraLastSectionBytes != 0) {
                   2624:                                                DWORD dwRemain = optionalHeader->SectionAlignment - dwExtraLastSectionBytes;
                   2625:                                                dwLastSectionSize += dwRemain;
                   2626:                                        }
                   2627:                                        DWORD dwVirtualAddress = lastSectionHeader->VirtualAddress + dwLastSectionSize;
                   2628:                                        
                   2629:                                        // store msdos.exe
                   2630:                                        fseek(fp, 0, SEEK_SET);
                   2631:                                        for(int i = 0; i < dwEndOfFile; i++) {
                   2632:                                                if((data = fgetc(fp)) != EOF) {
                   2633:                                                        fputc(data, fo);
                   2634:                                                } else {
                   2635:                                                        // we should not reach here :-(
                   2636:                                                        fputc(0, fo);
                   2637:                                                }
                   2638:                                        }
                   2639:                                        
                   2640:                                        // store options
                   2641:                                        UINT8 flags = 0;
                   2642:                                        if(stay_busy) {
                   2643:                                                flags |= 0x01;
                   2644:                                        }
                   2645:                                        if(no_windows) {
                   2646:                                                flags |= 0x02;
                   2647:                                        }
                   2648:                                        if(standard_env) {
                   2649:                                                flags |= 0x04;
                   2650:                                        }
                   2651:                                        if(ignore_illegal_insn) {
                   2652:                                                flags |= 0x08;
                   2653:                                        }
                   2654:                                        if(limit_max_memory) {
                   2655:                                                flags |= 0x10;
                   2656:                                        }
1.1.1.29  root     2657:                                        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     2658:                                                flags |= 0x20;
                   2659:                                        }
                   2660:                                        if(support_ems) {
                   2661:                                                flags |= 0x40;
                   2662:                                        }
1.1.1.30  root     2663: #ifdef SUPPORT_XMS
                   2664:                                        if(support_xms) {
                   2665:                                                flags |= 0x80;
                   2666:                                        }
                   2667: #endif
1.1.1.28  root     2668:                                        
                   2669:                                        fputc(flags, fo);
                   2670:                                        fputc((buf_width  >> 0) & 0xff, fo);
                   2671:                                        fputc((buf_width  >> 8) & 0xff, fo);
                   2672:                                        fputc((buf_height >> 0) & 0xff, fo);
                   2673:                                        fputc((buf_height >> 8) & 0xff, fo);
1.1.1.30  root     2674:                                        fputc(dos_major_version, fo);
                   2675:                                        fputc(dos_minor_version, fo);
                   2676:                                        fputc(win_major_version, fo);
                   2677:                                        fputc(win_minor_version, fo);
1.1.1.28  root     2678:                                        fputc((code_page >> 0) & 0xff, fo);
                   2679:                                        fputc((code_page >> 8) & 0xff, fo);
                   2680:                                        
                   2681:                                        // store command file info
                   2682:                                        GetFullPathName(argv[arg_offset + 1], MAX_PATH, full, &name);
                   2683:                                        int name_len = strlen(name);
                   2684:                                        fseek(fs, 0, SEEK_END);
                   2685:                                        long file_size = ftell(fs);
                   2686:                                        
                   2687:                                        fputc(name_len, fo);
                   2688:                                        fputc((file_size >>  0) & 0xff, fo);
                   2689:                                        fputc((file_size >>  8) & 0xff, fo);
                   2690:                                        fputc((file_size >> 16) & 0xff, fo);
                   2691:                                        fputc((file_size >> 24) & 0xff, fo);
                   2692:                                        fwrite(name, name_len, 1, fo);
                   2693:                                        
                   2694:                                        // store command file
                   2695:                                        fseek(fs, 0, SEEK_SET);
                   2696:                                        for(int i = 0; i < file_size; i++) {
                   2697:                                                if((data = fgetc(fs)) != EOF) {
                   2698:                                                        fputc(data, fo);
                   2699:                                                } else {
                   2700:                                                        // we should not reach here :-(
                   2701:                                                        fputc(0, fo);
                   2702:                                                }
                   2703:                                        }
                   2704:                                        
                   2705:                                        // store padding data and update pe header
1.1.1.29  root     2706:                                        _IMAGE_SECTION_HEADER *newSectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * coffHeader->NumberOfSections);
                   2707:                                        coffHeader->NumberOfSections++;
                   2708:                                        memset(newSectionHeader, 0, IMAGE_SIZEOF_SECTION_HEADER);
                   2709:                                        memcpy(newSectionHeader->Name, ".msdos", 6);
                   2710:                                        newSectionHeader->VirtualAddress = dwVirtualAddress;
                   2711:                                        newSectionHeader->PointerToRawData = dwEndOfFile;
                   2712:                                        newSectionHeader->Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_DISCARDABLE;
1.1.1.28  root     2713:                                        newSectionHeader->SizeOfRawData = 14 + name_len + file_size;
                   2714:                                        DWORD dwExtraRawBytes = newSectionHeader->SizeOfRawData % optionalHeader->FileAlignment;
                   2715:                                        if(dwExtraRawBytes != 0) {
1.1.1.29  root     2716:                                                static const char padding[] = "PADDINGXXPADDING";
1.1.1.28  root     2717:                                                DWORD dwRemain = optionalHeader->FileAlignment - dwExtraRawBytes;
                   2718:                                                for(int i = 0; i < dwRemain; i++) {
1.1.1.29  root     2719:                                                        if(i < 2) {
                   2720:                                                                fputc(padding[i & 15], fo);
                   2721:                                                        } else {
                   2722:                                                                fputc(padding[(i - 2) & 15], fo);
                   2723:                                                        }
1.1.1.28  root     2724:                                                }
                   2725:                                                newSectionHeader->SizeOfRawData += dwRemain;
                   2726:                                        }
                   2727:                                        newSectionHeader->Misc.VirtualSize = newSectionHeader->SizeOfRawData;
                   2728:                                        
                   2729:                                        DWORD dwNewSectionSize = newSectionHeader->SizeOfRawData;
                   2730:                                        DWORD dwExtraNewSectionBytes = dwNewSectionSize % optionalHeader->SectionAlignment;
                   2731:                                        if(dwExtraNewSectionBytes != 0) {
                   2732:                                                DWORD dwRemain = optionalHeader->SectionAlignment - dwExtraNewSectionBytes;
                   2733:                                                dwNewSectionSize += dwRemain;
                   2734:                                        }
                   2735:                                        optionalHeader->SizeOfImage += dwNewSectionSize;
                   2736:                                        
                   2737:                                        fseek(fo, 0, SEEK_SET);
                   2738:                                        fwrite(header, sizeof(header), 1, fo);
                   2739:                                        
                   2740:                                        fprintf(stderr, "'%s' is successfully created\n", new_exec_file);
                   2741:                                        retval = EXIT_SUCCESS;
1.1.1.27  root     2742:                                }
                   2743:                        }
                   2744:                        if(fp != NULL) {
                   2745:                                fclose(fp);
                   2746:                        }
                   2747:                        if(fs != NULL) {
                   2748:                                fclose(fs);
                   2749:                        }
                   2750:                        if(fo != NULL) {
                   2751:                                fclose(fo);
                   2752:                        }
                   2753:                }
                   2754: #ifdef _DEBUG
                   2755:                _CrtDumpMemoryLeaks();
                   2756: #endif
                   2757:                return(retval);
                   2758:        }
1.1       root     2759:        
1.1.1.14  root     2760:        is_vista_or_later = is_greater_windows_version(6, 0, 0, 0);
                   2761:        
1.1.1.23  root     2762:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     2763:        CONSOLE_SCREEN_BUFFER_INFO csbi;
1.1.1.14  root     2764:        CONSOLE_CURSOR_INFO ci;
1.1.1.23  root     2765:        
1.1.1.28  root     2766:        get_console_info_success = (GetConsoleScreenBufferInfo(hStdout, &csbi) != 0);
1.1.1.14  root     2767:        GetConsoleCursorInfo(hStdout, &ci);
1.1.1.24  root     2768:        GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &dwConsoleMode);
1.1       root     2769:        
1.1.1.14  root     2770:        for(int y = 0; y < SCR_BUF_WIDTH; y++) {
                   2771:                for(int x = 0; x < SCR_BUF_HEIGHT; x++) {
                   2772:                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   2773:                        SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1       root     2774:                }
                   2775:        }
1.1.1.28  root     2776:        if(get_console_info_success) {
1.1.1.12  root     2777:                scr_width = csbi.dwSize.X;
1.1.1.14  root     2778:                scr_height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
                   2779:                
1.1.1.28  root     2780:                // v-text shadow buffer size must be lesser than 0x7fd0
                   2781:                if((scr_width > SCR_BUF_WIDTH) || (scr_height > SCR_BUF_HEIGHT) || (scr_width * scr_height * 2 > 0x7fd0) ||
1.1.1.14  root     2782:                   (buf_width != 0 && buf_width != scr_width) || (buf_height != 0 && buf_height != scr_height)) {
                   2783:                        scr_width = min(buf_width != 0 ? buf_width : scr_width, SCR_BUF_WIDTH);
                   2784:                        scr_height = min(buf_height != 0 ? buf_height : scr_height, SCR_BUF_HEIGHT);
1.1.1.28  root     2785:                        if(scr_width * scr_height * 2 > 0x7fd0) {
1.1.1.14  root     2786:                                scr_width = 80;
                   2787:                                scr_height = 25;
                   2788:                        }
1.1.1.28  root     2789:                        screen_size_changed = true;
1.1.1.14  root     2790:                }
1.1.1.12  root     2791:        } else {
                   2792:                // for a proof (not a console)
                   2793:                scr_width = 80;
                   2794:                scr_height = 25;
                   2795:        }
1.1.1.14  root     2796:        scr_buf_size.X = scr_width;
                   2797:        scr_buf_size.Y = scr_height;
                   2798:        scr_buf_pos.X = scr_buf_pos.Y = 0;
                   2799:        scr_top = csbi.srWindow.Top;
1.1       root     2800:        cursor_moved = false;
                   2801:        
1.1.1.35  root     2802: #ifdef USE_SERVICE_THREAD
                   2803:        InitializeCriticalSection(&input_crit_sect);
                   2804:        InitializeCriticalSection(&key_buf_crit_sect);
                   2805:        InitializeCriticalSection(&putch_crit_sect);
                   2806: #endif
1.1.1.25  root     2807:        key_buf_char = new FIFO(256);
                   2808:        key_buf_scan = new FIFO(256);
1.1       root     2809:        
                   2810:        hardware_init();
                   2811:        
1.1.1.33  root     2812: #ifdef USE_DEBUGGER
                   2813:        debugger_init();
                   2814: #endif
                   2815:        
1.1.1.9   root     2816:        if(msdos_init(argc - (arg_offset + 1), argv + (arg_offset + 1), envp, standard_env)) {
1.1       root     2817:                retval = EXIT_FAILURE;
                   2818:        } else {
1.1.1.27  root     2819: #if defined(_MSC_VER) && (_MSC_VER >= 1400)
1.1.1.14  root     2820:                _set_invalid_parameter_handler((_invalid_parameter_handler)ignore_invalid_parameters);
                   2821: #endif
                   2822:                SetConsoleCtrlHandler(ctrl_handler, TRUE);
                   2823:                
1.1.1.28  root     2824:                if(screen_size_changed) {
1.1.1.24  root     2825:                        change_console_size(scr_width, scr_height);
                   2826:                }
1.1.1.8   root     2827:                TIMECAPS caps;
                   2828:                timeGetDevCaps(&caps, sizeof(TIMECAPS));
                   2829:                timeBeginPeriod(caps.wPeriodMin);
1.1.1.35  root     2830: #ifdef USE_VRAM_THREAD
1.1.1.14  root     2831:                InitializeCriticalSection(&vram_crit_sect);
                   2832:                CloseHandle(CreateThread(NULL, 4096, vram_thread, NULL, 0, NULL));
                   2833: #endif
1.1.1.33  root     2834: #ifdef USE_DEBUGGER
                   2835:                CloseHandle(CreateThread(NULL, 0, debugger_thread, NULL, 0, NULL));
                   2836:                // wait until telnet client starts and connects to me
                   2837:                if(_access(debugger_get_ttermpro_path(), 0) == 0 ||
                   2838:                   _access(debugger_get_ttermpro_x86_path(), 0) == 0 ||
                   2839:                   _access(debugger_get_putty_path(), 0) == 0 ||
                   2840:                   _access(debugger_get_putty_x86_path(), 0) == 0 ||
                   2841:                   _access(debugger_get_telnet_path(), 0) == 0) {
                   2842:                        for(int i = 0; i < 100 && cli_socket == 0; i++) {
                   2843:                                Sleep(100);
                   2844:                        }
                   2845:                }
                   2846: #endif
1.1       root     2847:                hardware_run();
1.1.1.35  root     2848: #ifdef USE_VRAM_THREAD
1.1.1.14  root     2849:                vram_flush();
                   2850:                DeleteCriticalSection(&vram_crit_sect);
                   2851: #endif
1.1.1.24  root     2852:                timeEndPeriod(caps.wPeriodMin);
1.1.1.14  root     2853:                
1.1.1.24  root     2854:                // hStdin/hStdout (and all handles) will be closed in msdos_finish()...
1.1.1.28  root     2855:                if(get_console_info_success) {
1.1.1.23  root     2856:                        hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.12  root     2857:                        if(restore_console_on_exit) {
1.1.1.14  root     2858:                                // window can't be bigger than buffer,
                   2859:                                // buffer can't be smaller than window,
                   2860:                                // so make a tiny window,
                   2861:                                // set the required buffer,
                   2862:                                // then set the required window
                   2863:                                SMALL_RECT rect;
                   2864:                                SET_RECT(rect, 0, csbi.srWindow.Top, 0, csbi.srWindow.Top);
                   2865:                                SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.12  root     2866:                                SetConsoleScreenBufferSize(hStdout, csbi.dwSize);
1.1.1.14  root     2867:                                SET_RECT(rect, 0, 0, csbi.srWindow.Right - csbi.srWindow.Left, csbi.srWindow.Bottom - csbi.srWindow.Top);
1.1.1.12  root     2868:                                SetConsoleWindowInfo(hStdout, TRUE, &rect);
                   2869:                        }
1.1.1.14  root     2870:                        SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   2871:                        SetConsoleCursorInfo(hStdout, &ci);
1.1.1.12  root     2872:                }
1.1.1.24  root     2873:                SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode);
                   2874:                
1.1       root     2875:                msdos_finish();
1.1.1.14  root     2876:                
                   2877:                SetConsoleCtrlHandler(ctrl_handler, FALSE);
1.1       root     2878:        }
1.1.1.35  root     2879:        if(temp_file_created) {
                   2880:                DeleteFile(temp_file_path);
                   2881:                temp_file_created = false;
                   2882:        }
1.1.1.10  root     2883:        hardware_finish();
                   2884:        
1.1.1.28  root     2885:        if(key_buf_char != NULL) {
                   2886:                key_buf_char->release();
                   2887:                delete key_buf_char;
                   2888:                key_buf_char = NULL;
                   2889:        }
                   2890:        if(key_buf_scan != NULL) {
                   2891:                key_buf_scan->release();
                   2892:                delete key_buf_scan;
                   2893:                key_buf_scan = NULL;
                   2894:        }
1.1.1.35  root     2895: #ifdef USE_SERVICE_THREAD
                   2896:        DeleteCriticalSection(&input_crit_sect);
                   2897:        DeleteCriticalSection(&key_buf_crit_sect);
                   2898:        DeleteCriticalSection(&putch_crit_sect);
                   2899: #endif
1.1.1.20  root     2900: #ifdef _DEBUG
                   2901:        _CrtDumpMemoryLeaks();
                   2902: #endif
1.1       root     2903:        return(retval);
                   2904: }
                   2905: 
1.1.1.20  root     2906: /* ----------------------------------------------------------------------------
                   2907:        console
                   2908: ---------------------------------------------------------------------------- */
                   2909: 
1.1.1.14  root     2910: void change_console_size(int width, int height)
1.1.1.12  root     2911: {
1.1.1.23  root     2912:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.12  root     2913:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   2914:        SMALL_RECT rect;
                   2915:        COORD co;
                   2916:        
                   2917:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.14  root     2918:        if(csbi.srWindow.Top != 0 || csbi.dwCursorPosition.Y > height - 1) {
                   2919:                if(csbi.srWindow.Right - csbi.srWindow.Left + 1 == width && csbi.srWindow.Bottom - csbi.srWindow.Top + 1 == height) {
                   2920:                        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &csbi.srWindow);
                   2921:                        SET_RECT(rect, 0, 0, width - 1, height - 1);
                   2922:                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   2923:                } else if(csbi.dwCursorPosition.Y > height - 1) {
                   2924:                        SET_RECT(rect, 0, csbi.dwCursorPosition.Y - (height - 1), width - 1, csbi.dwCursorPosition.Y);
                   2925:                        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   2926:                        SET_RECT(rect, 0, 0, width - 1, height - 1);
                   2927:                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.12  root     2928:                }
                   2929:        }
1.1.1.14  root     2930:        if(csbi.dwCursorPosition.Y > height - 1) {
1.1.1.12  root     2931:                co.X = csbi.dwCursorPosition.X;
1.1.1.14  root     2932:                co.Y = min(height - 1, csbi.dwCursorPosition.Y - csbi.srWindow.Top);
1.1.1.12  root     2933:                SetConsoleCursorPosition(hStdout, co);
                   2934:                cursor_moved = true;
                   2935:        }
1.1.1.14  root     2936:        
                   2937:        // window can't be bigger than buffer,
                   2938:        // buffer can't be smaller than window,
                   2939:        // so make a tiny window,
                   2940:        // set the required buffer,
                   2941:        // then set the required window
                   2942:        SET_RECT(rect, 0, csbi.srWindow.Top, 0, csbi.srWindow.Top);
1.1.1.12  root     2943:        SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.14  root     2944:        co.X = width;
                   2945:        co.Y = height;
1.1.1.12  root     2946:        SetConsoleScreenBufferSize(hStdout, co);
1.1.1.14  root     2947:        SET_RECT(rect, 0, 0, width - 1, height - 1);
                   2948:        SetConsoleWindowInfo(hStdout, TRUE, &rect);
                   2949:        
                   2950:        scr_width = scr_buf_size.X = width;
                   2951:        scr_height = scr_buf_size.Y = height;
                   2952:        scr_top = 0;
                   2953:        
                   2954:        clear_scr_buffer(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
                   2955:        
                   2956:        int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.15  root     2957:        text_vram_end_address = text_vram_top_address + regen;
                   2958:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
                   2959:        
1.1.1.14  root     2960:        if(regen > 0x4000) {
                   2961:                regen = 0x8000;
                   2962:                vram_pages = 1;
                   2963:        } else if(regen > 0x2000) {
                   2964:                regen = 0x4000;
                   2965:                vram_pages = 2;
                   2966:        } else if(regen > 0x1000) {
                   2967:                regen = 0x2000;
                   2968:                vram_pages = 4;
                   2969:        } else {
                   2970:                regen = 0x1000;
                   2971:                vram_pages = 8;
                   2972:        }
1.1.1.15  root     2973:        *(UINT16 *)(mem + 0x44a) = scr_width;
                   2974:        *(UINT16 *)(mem + 0x44c) = regen;
                   2975:        *(UINT8  *)(mem + 0x484) = scr_height - 1;
                   2976:        
1.1.1.24  root     2977:        mouse.min_position.x = 0;
                   2978:        mouse.min_position.y = 0;
1.1.1.34  root     2979:        mouse.max_position.x = 8 * (scr_width  - 1);
                   2980:        mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24  root     2981:        
1.1.1.15  root     2982:        restore_console_on_exit = true;
1.1.1.14  root     2983: }
                   2984: 
                   2985: void clear_scr_buffer(WORD attr)
                   2986: {
                   2987:        for(int y = 0; y < scr_height; y++) {
                   2988:                for(int x = 0; x < scr_width; x++) {
                   2989:                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   2990:                        SCR_BUF(y,x).Attributes = attr;
                   2991:                }
                   2992:        }
1.1.1.12  root     2993: }
                   2994: 
1.1.1.24  root     2995: bool update_console_input()
1.1       root     2996: {
1.1.1.35  root     2997: #ifdef USE_SERVICE_THREAD
                   2998:        EnterCriticalSection(&input_crit_sect);
                   2999: #endif
1.1.1.23  root     3000:        HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
1.1.1.8   root     3001:        DWORD dwNumberOfEvents = 0;
1.1       root     3002:        DWORD dwRead;
                   3003:        INPUT_RECORD ir[16];
1.1.1.24  root     3004:        CONSOLE_SCREEN_BUFFER_INFO csbi = {0};
                   3005:        bool result = false;
1.1       root     3006:        
1.1.1.8   root     3007:        if(GetNumberOfConsoleInputEvents(hStdin, &dwNumberOfEvents) && dwNumberOfEvents != 0) {
                   3008:                if(ReadConsoleInputA(hStdin, ir, 16, &dwRead)) {
                   3009:                        for(int i = 0; i < dwRead; i++) {
1.1.1.24  root     3010:                                if(ir[i].EventType & MOUSE_EVENT) {
1.1.1.34  root     3011:                                        if(mouse.hidden == 0) {
                   3012:                                                // NOTE: if restore_console_on_exit, console is not scrolled
                   3013:                                                if(!restore_console_on_exit && csbi.srWindow.Bottom == 0) {
                   3014:                                                        GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
                   3015:                                                }
                   3016:                                                // FIXME: character size is always 8x8 ???
                   3017:                                                int x = 8 * (ir[i].Event.MouseEvent.dwMousePosition.X);
                   3018:                                                int y = 8 * (ir[i].Event.MouseEvent.dwMousePosition.Y - csbi.srWindow.Top);
                   3019:                                                
                   3020:                                                if(mouse.position.x != x || mouse.position.y != y) {
                   3021:                                                        mouse.position.x = x;
                   3022:                                                        mouse.position.y = y;
                   3023:                                                        mouse.status |= 1;
                   3024:                                                }
                   3025:                                        }
                   3026:                                        if(ir[i].Event.MouseEvent.dwEventFlags == 0) {
                   3027:                                                for(int i = 0; i < MAX_MOUSE_BUTTONS; i++) {
                   3028:                                                        static const DWORD bits[] = {
                   3029:                                                                FROM_LEFT_1ST_BUTTON_PRESSED,   // left
                   3030:                                                                RIGHTMOST_BUTTON_PRESSED,       // right
                   3031:                                                                FROM_LEFT_2ND_BUTTON_PRESSED,   // middle
                   3032:                                                        };
                   3033:                                                        bool prev_status = mouse.buttons[i].status;
                   3034:                                                        mouse.buttons[i].status = ((ir[i].Event.MouseEvent.dwButtonState & bits[i]) != 0);
                   3035:                                                        
                   3036:                                                        if(!prev_status && mouse.buttons[i].status) {
                   3037:                                                                mouse.buttons[i].pressed_times++;
                   3038:                                                                mouse.buttons[i].pressed_position.x = mouse.position.x;
                   3039:                                                                mouse.buttons[i].pressed_position.y = mouse.position.x;
                   3040:                                                                mouse.status |= 2 << (i * 2);
                   3041:                                                        } else if(prev_status && !mouse.buttons[i].status) {
                   3042:                                                                mouse.buttons[i].released_times++;
                   3043:                                                                mouse.buttons[i].released_position.x = mouse.position.x;
                   3044:                                                                mouse.buttons[i].released_position.y = mouse.position.x;
                   3045:                                                                mouse.status |= 4 << (i * 2);
1.1.1.14  root     3046:                                                        }
                   3047:                                                }
                   3048:                                        }
1.1.1.24  root     3049:                                } else if(ir[i].EventType & KEY_EVENT) {
1.1.1.33  root     3050:                                        // update keyboard flags in bios data area
1.1.1.35  root     3051:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & CAPSLOCK_ON) {
                   3052:                                                mem[0x417] |= 0x40;
1.1.1.33  root     3053:                                        } else {
1.1.1.35  root     3054:                                                mem[0x417] &= ~0x40;
1.1.1.33  root     3055:                                        }
1.1.1.35  root     3056:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & NUMLOCK_ON) {
                   3057:                                                mem[0x417] |= 0x20;
1.1.1.33  root     3058:                                        } else {
1.1.1.35  root     3059:                                                mem[0x417] &= ~0x20;
                   3060:                                        }
                   3061:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & SCROLLLOCK_ON) {
                   3062:                                                mem[0x417] |= 0x10;
                   3063:                                        } else {
                   3064:                                                mem[0x417] &= ~0x10;
1.1.1.33  root     3065:                                        }
                   3066:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
                   3067:                                                mem[0x417] |= 0x08;
                   3068:                                        } else {
                   3069:                                                mem[0x417] &= ~0x08;
                   3070:                                        }
1.1.1.35  root     3071:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
                   3072:                                                mem[0x417] |= 0x04;
1.1.1.33  root     3073:                                        } else {
1.1.1.35  root     3074:                                                mem[0x417] &= ~0x04;
1.1.1.33  root     3075:                                        }
                   3076:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) {
                   3077:                                                if(!(mem[0x417] & 0x03)) {
                   3078:                                                        mem[0x417] |= 0x02; // left shift
                   3079:                                                }
                   3080:                                        } else {
                   3081:                                                mem[0x417] &= ~0x03;
                   3082:                                        }
1.1.1.35  root     3083:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & LEFT_ALT_PRESSED) {
                   3084:                                                mem[0x418] |= 0x02;
                   3085:                                        } else {
                   3086:                                                mem[0x418] &= ~0x02;
                   3087:                                        }
                   3088:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & LEFT_CTRL_PRESSED) {
                   3089:                                                mem[0x418] |= 0x01;
                   3090:                                        } else {
                   3091:                                                mem[0x418] &= ~0x01;
                   3092:                                        }
1.1.1.33  root     3093:                                        
1.1.1.28  root     3094:                                        // set scan code of last pressed/release key to kbd_data (in-port 60h)
1.1.1.24  root     3095:                                        kbd_data = ir[i].Event.KeyEvent.wVirtualScanCode;
1.1.1.33  root     3096:                                        kbd_status |= 1;
                   3097:                                        
                   3098:                                        // update dos key buffer
                   3099:                                        UINT8 chr = ir[i].Event.KeyEvent.uChar.AsciiChar;
                   3100:                                        UINT8 scn = ir[i].Event.KeyEvent.wVirtualScanCode & 0xff;
                   3101:                                        
                   3102:                                        if(ir[i].Event.KeyEvent.bKeyDown) {
1.1.1.28  root     3103:                                                // make
1.1.1.24  root     3104:                                                kbd_data &= 0x7f;
                   3105:                                                
1.1.1.33  root     3106:                                                if(chr == 0x00) {
1.1.1.24  root     3107:                                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
                   3108:                                                                if(scn >= 0x3b && scn <= 0x44) {
                   3109:                                                                        scn += 0x68 - 0x3b;     // F1 to F10
                   3110:                                                                } else if(scn == 0x57 || scn == 0x58) {
                   3111:                                                                        scn += 0x8b - 0x57;     // F11 & F12
                   3112:                                                                } else if(scn >= 0x47 && scn <= 0x53) {
                   3113:                                                                        scn += 0x97 - 0x47;     // edit/arrow clusters
                   3114:                                                                } else if(scn == 0x35) {
                   3115:                                                                        scn = 0xa4;             // keypad /
                   3116:                                                                }
                   3117:                                                        } else if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
                   3118:                                                                if(scn == 0x07) {
                   3119:                                                                        chr = 0x1e;     // Ctrl+^
                   3120:                                                                } else if(scn == 0x0c) {
                   3121:                                                                        chr = 0x1f;     // Ctrl+_
                   3122:                                                                } else if(scn >= 0x35 && scn <= 0x58) {
                   3123:                                                                        static const UINT8 ctrl_map[] = {
                   3124:                                                                                0x95,   // keypad /
                   3125:                                                                                0,
                   3126:                                                                                0x96,   // keypad *
                   3127:                                                                                0, 0, 0,
                   3128:                                                                                0x5e,   // F1
                   3129:                                                                                0x5f,   // F2
                   3130:                                                                                0x60,   // F3
                   3131:                                                                                0x61,   // F4
                   3132:                                                                                0x62,   // F5
                   3133:                                                                                0x63,   // F6
                   3134:                                                                                0x64,   // F7
                   3135:                                                                                0x65,   // F8
                   3136:                                                                                0x66,   // F9
                   3137:                                                                                0x67,   // F10
                   3138:                                                                                0,
                   3139:                                                                                0,
                   3140:                                                                                0x77,   // Home
                   3141:                                                                                0x8d,   // Up
                   3142:                                                                                0x84,   // PgUp
                   3143:                                                                                0x8e,   // keypad -
                   3144:                                                                                0x73,   // Left
                   3145:                                                                                0x8f,   // keypad center
                   3146:                                                                                0x74,   // Right
                   3147:                                                                                0x90,   // keyapd +
                   3148:                                                                                0x75,   // End
                   3149:                                                                                0x91,   // Down
                   3150:                                                                                0x76,   // PgDn
                   3151:                                                                                0x92,   // Insert
                   3152:                                                                                0x93,   // Delete
                   3153:                                                                                0, 0, 0,
                   3154:                                                                                0x89,   // F11
                   3155:                                                                                0x8a,   // F12
                   3156:                                                                        };
                   3157:                                                                        scn = ctrl_map[scn - 0x35];
                   3158:                                                                }
                   3159:                                                        } else if(ir[i].Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) {
                   3160:                                                                if(scn >= 0x3b && scn <= 0x44) {
                   3161:                                                                        scn += 0x54 - 0x3b;     // F1 to F10
                   3162:                                                                } else if(scn == 0x57 || scn == 0x58) {
                   3163:                                                                        scn += 0x87 - 0x57;     // F11 & F12
                   3164:                                                                }
                   3165:                                                        } else if(scn == 0x57 || scn == 0x58) {
                   3166:                                                                scn += 0x85 - 0x57;
                   3167:                                                        }
                   3168:                                                        // ignore shift, ctrl, alt, win and menu keys
                   3169:                                                        if(scn != 0x1d && scn != 0x2a && scn != 0x36 && scn != 0x38 && (scn < 0x5b || scn > 0x5e)) {
1.1.1.32  root     3170:                                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3171: #ifdef USE_SERVICE_THREAD
                   3172:                                                                        EnterCriticalSection(&key_buf_crit_sect);
                   3173: #endif
1.1.1.32  root     3174:                                                                        if(chr == 0) {
                   3175:                                                                                key_buf_char->write(0x00);
                   3176:                                                                                key_buf_scan->write(ir[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY ? 0xe0 : 0x00);
                   3177:                                                                        }
                   3178:                                                                        key_buf_char->write(chr);
                   3179:                                                                        key_buf_scan->write(scn);
1.1.1.35  root     3180: #ifdef USE_SERVICE_THREAD
                   3181:                                                                        LeaveCriticalSection(&key_buf_crit_sect);
                   3182: #endif
1.1.1.24  root     3183:                                                                }
                   3184:                                                        }
                   3185:                                                } else {
                   3186:                                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
                   3187:                                                                chr = 0;
                   3188:                                                                if(scn >= 0x02 && scn <= 0x0e) {
                   3189:                                                                        scn += 0x78 - 0x02;     // 1 to 0 - =
                   3190:                                                                }
                   3191:                                                        }
1.1.1.32  root     3192:                                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3193: #ifdef USE_SERVICE_THREAD
                   3194:                                                                EnterCriticalSection(&key_buf_crit_sect);
                   3195: #endif
1.1.1.32  root     3196:                                                                key_buf_char->write(chr);
                   3197:                                                                key_buf_scan->write(scn);
1.1.1.35  root     3198: #ifdef USE_SERVICE_THREAD
                   3199:                                                                LeaveCriticalSection(&key_buf_crit_sect);
                   3200: #endif
1.1.1.32  root     3201:                                                        }
1.1.1.24  root     3202:                                                }
1.1.1.33  root     3203:                                        } else if(chr == 0x03 && (ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))) {
                   3204:                                                // ctrl-break, ctrl-c
                   3205:                                                if(scn == 0x46) {
                   3206:                                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3207: #ifdef USE_SERVICE_THREAD
                   3208:                                                                EnterCriticalSection(&key_buf_crit_sect);
                   3209: #endif
1.1.1.33  root     3210:                                                                key_buf_char->write(0x00);
                   3211:                                                                key_buf_scan->write(0x00);
1.1.1.35  root     3212: #ifdef USE_SERVICE_THREAD
                   3213:                                                                LeaveCriticalSection(&key_buf_crit_sect);
                   3214: #endif
1.1.1.33  root     3215:                                                        }
                   3216:                                                        ctrl_break_pressed = true;
                   3217:                                                        mem[0x471] = 0x80;
                   3218:                                                        raise_int_1bh = true;
                   3219:                                                } else {
                   3220:                                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     3221: #ifdef USE_SERVICE_THREAD
                   3222:                                                                EnterCriticalSection(&key_buf_crit_sect);
                   3223: #endif
1.1.1.33  root     3224:                                                                key_buf_char->write(chr);
                   3225:                                                                key_buf_scan->write(scn);
1.1.1.35  root     3226: #ifdef USE_SERVICE_THREAD
                   3227:                                                                LeaveCriticalSection(&key_buf_crit_sect);
                   3228: #endif
1.1.1.33  root     3229:                                                        }
                   3230:                                                        ctrl_c_pressed = (scn == 0x2e);
                   3231:                                                }
                   3232:                                        } else {
                   3233:                                                // break
                   3234:                                                kbd_data |= 0x80;
1.1       root     3235:                                        }
1.1.1.24  root     3236:                                        result = key_changed = true;
1.1.1.36  root     3237:                                        // IME may be on and it may causes screen scroll up and cursor position change
                   3238:                                        cursor_moved = true;
1.1       root     3239:                                }
                   3240:                        }
                   3241:                }
                   3242:        }
1.1.1.35  root     3243: #ifdef USE_SERVICE_THREAD
                   3244:        LeaveCriticalSection(&input_crit_sect);
                   3245: #endif
1.1.1.24  root     3246:        return(result);
1.1.1.8   root     3247: }
                   3248: 
1.1.1.14  root     3249: bool update_key_buffer()
1.1.1.8   root     3250: {
1.1.1.35  root     3251:        if(update_console_input()) {
                   3252:                return(true);
                   3253:        }
                   3254:        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   3255: #ifdef USE_SERVICE_THREAD
                   3256:                EnterCriticalSection(&key_buf_crit_sect);
                   3257: #endif
                   3258:                bool empty = key_buf_char->empty();
                   3259: #ifdef USE_SERVICE_THREAD
                   3260:                LeaveCriticalSection(&key_buf_crit_sect);
                   3261: #endif
                   3262:                if(!empty) return(true);
                   3263:        }
                   3264:        return(false);
1.1.1.8   root     3265: }
                   3266: 
1.1.1.20  root     3267: /* ----------------------------------------------------------------------------
                   3268:        MS-DOS virtual machine
                   3269: ---------------------------------------------------------------------------- */
                   3270: 
1.1.1.32  root     3271: static const struct {
1.1.1.33  root     3272:        char *name;
                   3273:        DWORD lcid;
                   3274:        char *std;
                   3275:        char *dlt;
                   3276: } tz_table[] = {
                   3277:        // https://science.ksc.nasa.gov/software/winvn/userguide/3_1_4.htm
                   3278: //     0       GMT             Greenwich Mean Time             GMT0
                   3279:        {"GMT Standard Time",                   0x0809, "GMT", "BST"},          // (UTC+00:00) GB London (en-gb)
                   3280:        {"GMT Standard Time",                   0x1809, "GMT", "IST"},          // (UTC+00:00) IE Dublin (en-ie)
                   3281:        {"GMT Standard Time",                   0x0000, "WET", "WES"},          // (UTC+00:00) PT Lisbon
                   3282:        {"Greenwich Standard Time",             0x0000, "GMT", "GST"},          // (UTC+00:00) IS Reykjavik
                   3283: //     2       FST     FDT     Fernando De Noronha Std         FST2FDT
                   3284:        {"Mid-Atlantic Standard Time",          0x0416, "FST", "FDT"},          // (UTC-02:00) BR Noronha (pt-br)
                   3285:        {"UTC-02",                              0x0416, "FST", "FDT"},          // (UTC-02:00) BR Noronha (pt-br)
                   3286: //     3       BST             Brazil Standard Time            BST3
                   3287:        {"Bahia Standard Time",                 0x0000, "BST", "BDT"},          // (UTC-03:00) BR Bahia
                   3288:        {"SA Eastern Standard Time",            0x0000, "BST", "BDT"},          // (UTC-03:00) BR Fortaleza
                   3289:        {"Tocantins Standard Time",             0x0000, "BST", "BDT"},          // (UTC-03:00) BR Palmas
                   3290: //     3       EST     EDT     Eastern Standard (Brazil)       EST3EDT
                   3291:        {"E. South America Standard Time",      0x0000, "EST", "EDT"},          // (UTC-03:00) BR Sao Paulo
                   3292: //     3       GST             Greenland Standard Time         GST3
                   3293:        {"Greenland Standard Time",             0x0000, "GST", "GDT"},          // (UTC-03:00) GL Godthab
                   3294: //     3:30    NST     NDT     Newfoundland Standard Time      NST3:30NDT
                   3295:        {"Newfoundland Standard Time",          0x0000, "NST", "NDT"},          // (UTC-03:30) CA St.Johns
                   3296: //     4       AST     ADT     Atlantic Standard Time          AST4ADT
                   3297:        {"Atlantic Standard Time",              0x0000, "AST", "ADT"},          // (UTC-04:00) CA Halifax
                   3298: //     4       WST     WDT     Western Standard (Brazil)       WST4WDT
                   3299:        {"Central Brazilian Standard Time",     0x0000, "WST", "WDT"},          // (UTC-04:00) BR Cuiaba
                   3300:        {"SA Western Standard Time",            0x0000, "WST", "WDT"},          // (UTC-04:00) BR Manaus
                   3301: //     5       EST     EDT     Eastern Standard Time           EST5EDT
                   3302:        {"Eastern Standard Time",               0x0000, "EST", "EDT"},          // (UTC-05:00) US New York
                   3303:        {"Eastern Standard Time (Mexico)",      0x0000, "EST", "EDT"},          // (UTC-05:00) MX Cancun
                   3304:        {"US Eastern Standard Time",            0x0000, "EST", "EDT"},          // (UTC-05:00) US Indianapolis
                   3305: //     5       CST     CDT     Chile Standard Time             CST5CDT
                   3306:        {"Pacific SA Standard Time",            0x0000, "CST", "CDT"},          // (UTC-04:00) CL Santiago
                   3307: //     5       AST     ADT     Acre Standard Time              AST5ADT
                   3308:        {"SA Pacific Standard Time",            0x0000, "AST", "ADT"},          // (UTC-05:00) BR Rio Branco
                   3309: //     5       CST     CDT     Cuba Standard Time              CST5CDT
                   3310:        {"Cuba Standard Time",                  0x0000, "CST", "CDT"},          // (UTC-05:00) CU Havana
                   3311: //     6       CST     CDT     Central Standard Time           CST6CDT
                   3312:        {"Canada Central Standard Time",        0x0000, "CST", "CDT"},          // (UTC-06:00) CA Regina
                   3313:        {"Central Standard Time",               0x0000, "CST", "CDT"},          // (UTC-06:00) US Chicago
                   3314:        {"Central Standard Time (Mexico)",      0x0000, "CST", "CDT"},          // (UTC-06:00) MX Mexico City
                   3315: //     6       EST     EDT     Easter Island Standard          EST6EDT
                   3316:        {"Easter Island Standard Time",         0x0000, "EST", "EDT"},          // (UTC-06:00) CL Easter
                   3317: //     7       MST     MDT     Mountain Standard Time          MST7MDT
                   3318:        {"Mountain Standard Time",              0x0000, "MST", "MDT"},          // (UTC-07:00) US Denver
                   3319:        {"Mountain Standard Time (Mexico)",     0x0000, "MST", "MDT"},          // (UTC-07:00) MX Chihuahua
                   3320:        {"US Mountain Standard Time",           0x0000, "MST", "MDT"},          // (UTC-07:00) US Phoenix
                   3321: //     8       PST     PDT     Pacific Standard Time           PST8PDT
                   3322:        {"Pacific Standard Time",               0x0000, "PST", "PDT"},          // (UTC-08:00) US Los Angeles
                   3323:        {"Pacific Standard Time (Mexico)",      0x0000, "PST", "PDT"},          // (UTC-08:00) MX Tijuana
                   3324: //     9       AKS     AKD     Alaska Standard Time            AKS9AKD
                   3325: //     9       YST     YDT     Yukon Standard Time             YST9YST
                   3326:        {"Alaskan Standard Time",               0x0000, "AKS", "AKD"},          // (UTC-09:00) US Anchorage
                   3327: //     10      HST     HDT     Hawaii Standard Time            HST10HDT
                   3328:        {"Aleutian Standard Time",              0x0000, "HST", "HDT"},          // (UTC-10:00) US Aleutian
                   3329:        {"Hawaiian Standard Time",              0x0000, "HST", "HDT"},          // (UTC-10:00) US Honolulu
                   3330: //     11      SST             Samoa Standard Time             SST11
                   3331:        {"Samoa Standard Time",                 0x0000, "SST", "SDT"},          // (UTC-11:00) US Samoa
                   3332: //     -12     NZS     NZD     New Zealand Standard Time       NZS-12NZD
                   3333:        {"New Zealand Standard Time",           0x0000, "NZS", "NZD"},          // (UTC+12:00) NZ Auckland
                   3334: //     -10     GST             Guam Standard Time              GST-10
                   3335:        {"West Pacific Standard Time",          0x0000, "GST", "GDT"},          // (UTC+10:00) GU Guam
                   3336: //     -10     EAS     EAD     Eastern Australian Standard     EAS-10EAD
                   3337:        {"AUS Eastern Standard Time",           0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Sydney
                   3338:        {"E. Australia Standard Time",          0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Brisbane
                   3339:        {"Tasmania Standard Time",              0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Hobart
                   3340: //     -9:30   CAS     CAD     Central Australian Standard     CAS-9:30CAD
                   3341:        {"AUS Central Standard Time",           0x0000, "CAS", "CAD"},          // (UTC+09:30) AU Darwin
                   3342:        {"Cen. Australia Standard Time",        0x0000, "CAS", "CAD"},          // (UTC+09:30) AU Adelaide
                   3343: //     -9      JST             Japan Standard Time             JST-9
                   3344:        {"Tokyo Standard Time",                 0x0000, "JST", "JDT"},          // (UTC+09:00) JP Tokyo
                   3345: //     -9      KST     KDT     Korean Standard Time            KST-9KDT
                   3346:        {"Korea Standard Time",                 0x0000, "KST", "KDT"},          // (UTC+09:00) KR Seoul
                   3347:        {"North Korea Standard Time",           0x0000, "KST", "KDT"},          // (UTC+08:30) KP Pyongyang
                   3348: //     -8      HKT             Hong Kong Time                  HKT-8
                   3349:        {"China Standard Time",                 0x0C04, "HKT", "HKS"},          // (UTC+08:00) HK Hong Kong (zh-hk)
                   3350: //     -8      CCT             China Coast Time                CCT-8
                   3351:        {"China Standard Time",                 0x0000, "CCT", "CDT"},          // (UTC+08:00) CN Shanghai
                   3352:        {"Taipei Standard Time",                0x0000, "CCT", "CDT"},          // (UTC+08:00) TW Taipei
                   3353: //     -8      SST             Singapore Standard Time         SST-8
                   3354:        {"Singapore Standard Time",             0x0000, "SST", "SDT"},          // (UTC+08:00) SG Singapore
                   3355: //     -8      WAS     WAD     Western Australian Standard     WAS-8WAD
                   3356:        {"Aus Central W. Standard Time",        0x0000, "WAS", "WAD"},          // (UTC+08:45) AU Eucla
                   3357:        {"W. Australia Standard Time",          0x0000, "WAS", "WAD"},          // (UTC+08:00) AU Perth
                   3358: //     -7:30   JT              Java Standard Time              JST-7:30
                   3359: //     -7      NST             North Sumatra Time              NST-7
                   3360:        {"SE Asia Standard Time",               0x0000, "NST", "NDT"},          // (UTC+07:00) ID Jakarta
                   3361: //     -5:30   IST             Indian Standard Time            IST-5:30
                   3362:        {"India Standard Time",                 0x0000, "IST", "IDT"},          // (UTC+05:30) IN Calcutta
                   3363: //     -3:30   IST     IDT     Iran Standard Time              IST-3:30IDT
                   3364:        {"Iran Standard Time",                  0x0000, "IST", "IDT"},          // (UTC+03:30) IR Tehran
                   3365: //     -3      MSK     MSD     Moscow Winter Time              MSK-3MSD
                   3366:        {"Belarus Standard Time",               0x0000, "MSK", "MSD"},          // (UTC+03:00) BY Minsk
                   3367:        {"Russian Standard Time",               0x0000, "MSK", "MSD"},          // (UTC+03:00) RU Moscow
                   3368: //     -2      EET             Eastern Europe Time             EET-2
                   3369:        {"E. Europe Standard Time",             0x0000, "EET", "EES"},          // (UTC+02:00) MD Chisinau
                   3370:        {"FLE Standard Time",                   0x0000, "EET", "EES"},          // (UTC+02:00) UA Kiev
                   3371:        {"GTB Standard Time",                   0x0000, "EET", "EES"},          // (UTC+02:00) RO Bucharest
                   3372:        {"Kaliningrad Standard Time",           0x0000, "EET", "EES"},          // (UTC+02:00) RU Kaliningrad
                   3373: //     -2      IST     IDT     Israel Standard Time            IST-2IDT
                   3374:        {"Israel Standard Time",                0x0000, "IST", "IDT"},          // (UTC+02:00) IL Jerusalem
                   3375: //     -1      MEZ     MES     Middle European Time            MEZ-1MES
                   3376: //     -1      SWT     SST     Swedish Winter Time             SWT-1SST
                   3377: //     -1      FWT     FST     French Winter Time              FWT-1FST
                   3378: //     -1      CET     CES     Central European Time           CET-1CES
                   3379:        {"Central Europe Standard Time",        0x0000, "CET", "CES"},          // (UTC+01:00) HU Budapest
                   3380:        {"Central European Standard Time",      0x0000, "CET", "CES"},          // (UTC+01:00) PL Warsaw
                   3381:        {"Romance Standard Time",               0x0000, "CET", "CES"},          // (UTC+01:00) FR Paris
                   3382:        {"W. Europe Standard Time",             0x0000, "CET", "CES"},          // (UTC+01:00) DE Berlin
                   3383: //     -1      WAT             West African Time               WAT-1
                   3384:        {"Namibia Standard Time",               0x0000, "WAT", "WAS"},          // (UTC+01:00) NA Windhoek
                   3385:        {"W. Central Africa Standard Time",     0x0000, "WAT", "WAS"},          // (UTC+01:00) NG Lagos
                   3386: //     0       UTC             Universal Coordinated Time      UTC0
                   3387:        {"UTC",                                 0x0000, "UTC", ""   },          // (UTC+00:00) GMT+0
                   3388:        {"UTC-02",                              0x0000, "UTC", ""   },          // (UTC-02:00) GMT+2
                   3389:        {"UTC-08",                              0x0000, "UTC", ""   },          // (UTC-08:00) GMT+8
                   3390:        {"UTC-09",                              0x0000, "UTC", ""   },          // (UTC-09:00) GMT+9
                   3391:        {"UTC-11",                              0x0000, "UTC", ""   },          // (UTC-11:00) GMT+11
                   3392:        {"UTC+12",                              0x0000, "UTC", ""   },          // (UTC+12:00) GMT-12
                   3393: };
                   3394: 
                   3395: static const struct {
1.1.1.32  root     3396:        UINT16 code;
                   3397:        char *message_english;
                   3398:        char *message_japanese;
                   3399: } standard_error_table[] = {
                   3400:        {0x01,  "Invalid function", "�����ȃt�@���N�V�����ł�."},
                   3401:        {0x02,  "File not found", "�t�@�C�������‚���܂���."},
                   3402:        {0x03,  "Path not found", "�p�X�����‚���܂���."},
                   3403:        {0x04,  "Too many open files", "�J����Ă���t�@�C�����������܂�."},
                   3404:        {0x05,  "Access denied", "�A�N�Z�X�͋��ۂ���܂���."},
                   3405:        {0x06,  "Invalid handle", "�����ȃn���h���ł�."},
                   3406:        {0x07,  "Memory control blocks destroyed", "����������u���b�N���j������܂���."},
                   3407:        {0x08,  "Insufficient memory", "������������܂���."},
                   3408:        {0x09,  "Invalid memory block address", "�����ȃ������u���b�N�̃A�h���X�ł�."},
                   3409:        {0x0A,  "Invalid Environment", "�����Ȋ‹��ł�."},
                   3410:        {0x0B,  "Invalid format", "�����ȃt�H�[�}�b�g�ł�."},
                   3411:        {0x0C,  "Invalid function parameter", "�����ȃt�@���N�V�����p�����[�^�ł�."},
                   3412:        {0x0D,  "Invalid data", "�����ȃf�[�^�ł�."},
                   3413:        {0x0F,  "Invalid drive specification", "�����ȃh���C�u�̎w��ł�."},
                   3414:        {0x10,  "Attempt to remove current directory", "���݂̃f�B���N�g�����폜���悤�Ƃ��܂���."},
                   3415:        {0x11,  "Not same device", "�����f�o�C�X�ł͂���܂���."},
                   3416:        {0x12,  "No more files", "�t�@�C���͂���ȏ゠��܂���."},
                   3417:        {0x13,  "Write protect error", "�������ݕی�G���[�ł�."},
                   3418:        {0x14,  "Invalid unit", "�����ȃ��j�b�g�ł�."},
                   3419:        {0x15,  "Not ready", "�������ł��Ă��܂���."},
                   3420:        {0x16,  "Invalid device request", "�����ȃf�o�C�X�v���ł�."},
                   3421:        {0x17,  "Data error", "�f�[�^�G���[�ł�."},
                   3422:        {0x18,  "Invalid device request parameters", "�����ȃf�o�C�X�v���̃p�����[�^�ł�."},
                   3423:        {0x19,  "Seek error", "�V�[�N�G���[�ł�."},
                   3424:        {0x1A,  "Invalid media type", "�����ȃ��f�B�A�̎�ނł�."},
                   3425:        {0x1B,  "Sector not found", "�Z�N�^�����‚���܂���."},
                   3426:        {0x1C,  "Printer out of paper error", "�v�����^�̗p��������܂���."},
                   3427:        {0x1D,  "Write fault error", "�������݃G���[�ł�."},
                   3428:        {0x1E,  "Read fault error", "�ǂݎ��G���[�ł�."},
                   3429:        {0x1F,  "General failure", "�G���[�ł�."},
                   3430:        {0x20,  "Sharing violation", "���L�ᔽ�ł�."},
                   3431:        {0x21,  "Lock violation", "���b�N�ᔽ�ł�."},
                   3432:        {0x22,  "Invalid disk change", "�����ȃf�B�X�N�̌����ł�."},
                   3433:        {0x23,  "FCB unavailable", "FCB �͎g���܂���."},
                   3434:        {0x24,  "System resource exhausted", "�V�X�e�����\�[�X�͂����g���܂���."},
                   3435:        {0x25,  "Code page mismatch", "�R�[�h �y�[�W����v���܂���."},
                   3436:        {0x26,  "Out of input", "���͂��I���܂���."},
                   3437:        {0x27,  "Insufficient disk space", "�f�B�X�N�̋󂫗̈悪����܂���."},
                   3438: /*
                   3439:        {0x32,  "Network request not supported", NULL},
                   3440:        {0x33,  "Remote computer not listening", NULL},
                   3441:        {0x34,  "Duplicate name on network", NULL},
                   3442:        {0x35,  "Network name not found", NULL},
                   3443:        {0x36,  "Network busy", NULL},
                   3444:        {0x37,  "Network device no longer exists", NULL},
                   3445:        {0x38,  "Network BIOS command limit exceeded", NULL},
                   3446:        {0x39,  "Network adapter hardware error", NULL},
                   3447:        {0x3A,  "Incorrect response from network", NULL},
                   3448:        {0x3B,  "Unexpected network error", NULL},
                   3449:        {0x3C,  "Incompatible remote adapter", NULL},
                   3450:        {0x3D,  "Print queue full", NULL},
                   3451:        {0x3E,  "Queue not full", NULL},
                   3452:        {0x3F,  "Not enough space to print file", NULL},
                   3453:        {0x40,  "Network name was deleted", NULL},
                   3454:        {0x41,  "Network: Access denied", NULL},
                   3455:        {0x42,  "Network device type incorrect", NULL},
                   3456:        {0x43,  "Network name not found", NULL},
                   3457:        {0x44,  "Network name limit exceeded", NULL},
                   3458:        {0x45,  "Network BIOS session limit exceeded", NULL},
                   3459:        {0x46,  "Temporarily paused", NULL},
                   3460:        {0x47,  "Network request not accepted", NULL},
                   3461:        {0x48,  "Network print/disk redirection paused", NULL},
                   3462:        {0x49,  "Network software not installed", NULL},
                   3463:        {0x4A,  "Unexpected adapter close", NULL},
                   3464: */
                   3465:        {0x50,  "File exists", "�t�@�C���͑��݂��܂�."},
1.1.1.33  root     3466:        {0x52,  "Cannot make directory entry", "�f�B���N�g���G���g�����쐬�ł��܂���."},
1.1.1.32  root     3467:        {0x53,  "Fail on INT 24", "INT 24 �Ŏ��s���܂���."},
                   3468:        {0x54,  "Too many redirections", "���_�C���N�g���������܂�."},
                   3469:        {0x55,  "Duplicate redirection", "���_�C���N�g���d�����Ă��܂�."},
                   3470:        {0x56,  "Invalid password", "�p�X���[�h���Ⴂ�܂�."},
                   3471:        {0x57,  "Invalid parameter", "�p�����[�^�̎w�肪�Ⴂ�܂�."},
                   3472:        {0x58,  "Network data fault", "�l�b�g���[�N�f�[�^�̃G���[�ł�."},
                   3473:        {0x59,  "Function not supported by network", "�t�@���N�V�����̓l�b�g���[�N�ŃT�|�[�g����Ă��܂���."},
                   3474:        {0x5A,  "Required system component not installe", "�K�v�ȃV�X�e�� �R���|�[�l���g���g�ݍ��܂�Ă��܂���."},
                   3475: /*
                   3476:        {0x64,  "Unknown error", "�s���ȃG���[�ł�."},
                   3477:        {0x65,  "Not ready", "�������ł��Ă��܂���."},
                   3478:        {0x66,  "EMS memory no longer valid", "EMS �������͂����L���ł͂���܂���."},
                   3479:        {0x67,  "CDROM not High Sierra or ISO-9660 format", "CDROM �� High Sierra �܂��� ISO-9660 �t�H�[�}�b�g�ł͂���܂���."},
                   3480:        {0x68,  "Door open", "���o�[���‚܂��Ă��܂���."
                   3481: */
                   3482:        {0xB0,  "Volume is not locked", "�{�����[�������b�N����Ă��܂���."},
                   3483:        {0xB1,  "Volume is locked in drive", "�{�����[�������b�N����Ă��܂�."},
                   3484:        {0xB2,  "Volume is not removable", "�{�����[���͎��O���ł��܂���."},
                   3485:        {0xB4,  "Lock count has been exceeded", "�{�����[��������ȏネ�b�N�ł��܂���."},
                   3486:        {0xB5,  "A valid eject request failed", "���o���Ɏ��s���܂���."},
                   3487:        {-1  ,  "Unknown error", "�s���ȃG���[�ł�."},
                   3488: };
                   3489: 
                   3490: static const struct {
                   3491:        UINT16 code;
                   3492:        char *message_english;
                   3493:        char *message_japanese;
                   3494: } param_error_table[] = {
                   3495:        {0x01,  "Too many parameters", "�p�����[�^���������܂�."},
                   3496:        {0x02,  "Required parameter missing", "�p�����[�^������܂���."},
                   3497:        {0x03,  "Invalid switch", "�����ȃX�C�b�`�ł�."},
                   3498:        {0x04,  "Invalid keyword", "�����ȃL�[���[�h�ł�."},
                   3499:        {0x06,  "Parameter value not in allowed range", "�p�����[�^�̒l���͈͂𒴂��Ă��܂�."},
                   3500:        {0x07,  "Parameter value not allowed", "���̃p�����[�^�̒l�͎g���܂���."},
                   3501:        {0x08,  "Parameter value not allowed", "���̃p�����[�^�̒l�͎g���܂���."},
                   3502:        {0x09,  "Parameter format not correct", "�p�����[�^�̏������Ⴂ�܂�."},
                   3503:        {0x0A,  "Invalid parameter", "�����ȃp�����[�^�ł�."},
                   3504:        {0x0B,  "Invalid parameter combination", "�����ȃp�����[�^�̑g�ݍ��킹�ł�."},
                   3505:        {-1  ,  "Unknown error", "�s���ȃG���[�ł�."},
                   3506: };
                   3507: 
                   3508: static const struct {
                   3509:        UINT16 code;
                   3510:        char *message_english;
                   3511:        char *message_japanese;
                   3512: } critical_error_table[] = {
                   3513:        {0x00,  "Write protect error", "�������ݕی�G���[�ł�."},
                   3514:        {0x01,  "Invalid unit", "�����ȃ��j�b�g�ł�."},
                   3515:        {0x02,  "Not ready", "�������ł��Ă��܂���."},
                   3516:        {0x03,  "Invalid device request", "�����ȃf�o�C�X�v���ł�."},
                   3517:        {0x04,  "Data error", "�f�[�^�G���[�ł�."},
                   3518:        {0x05,  "Invalid device request parameters", "�����ȃf�o�C�X�v���̃p�����[�^�ł�."},
                   3519:        {0x06,  "Seek error", "�V�[�N�G���[�ł�."},
                   3520:        {0x07,  "Invalid media type", "�����ȃ��f�B�A�̎�ނł�."},
                   3521:        {0x08,  "Sector not found", "�Z�N�^�����‚���܂���."},
                   3522:        {0x09,  "Printer out of paper error", "�v�����^�̗p��������܂���."},
                   3523:        {0x0A,  "Write fault error", "�������݃G���[�ł�."},
                   3524:        {0x0B,  "Read fault error", "�ǂݎ��G���[�ł�."},
                   3525:        {0x0C,  "General failure", "�G���[�ł�."},
                   3526:        {0x0D,  "Sharing violation", "���L�ᔽ�ł�."},
                   3527:        {0x0E,  "Lock violation", "���b�N�ᔽ�ł�."},
                   3528:        {0x0F,  "Invalid disk change", "�����ȃf�B�X�N�̌����ł�."},
                   3529:        {0x10,  "FCB unavailable", "FCB �͎g���܂���."},
                   3530:        {0x11,  "System resource exhausted", "�V�X�e�����\�[�X�͂����g���܂���."},
                   3531:        {0x12,  "Code page mismatch", "�R�[�h �y�[�W����v���܂���."},
                   3532:        {0x13,  "Out of input", "���͂��I���܂���."},
                   3533:        {0x14,  "Insufficient disk space", "�f�B�X�N�̋󂫗̈悪����܂���."},
                   3534:        {-1  ,  "Critical error", "�v���I�ȃG���[�ł�."},
                   3535: };
                   3536: 
1.1.1.20  root     3537: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg);
                   3538: int msdos_psp_get_file_table(int fd, int psp_seg);
                   3539: void msdos_putch(UINT8 data);
1.1.1.35  root     3540: #ifdef USE_SERVICE_THREAD
                   3541: void msdos_putch_tmp(UINT8 data);
                   3542: #endif
1.1.1.20  root     3543: 
1.1       root     3544: // process info
                   3545: 
                   3546: process_t *msdos_process_info_create(UINT16 psp_seg)
                   3547: {
                   3548:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3549:                if(process[i].psp == 0 || process[i].psp == psp_seg) {
                   3550:                        memset(&process[i], 0, sizeof(process_t));
                   3551:                        process[i].psp = psp_seg;
                   3552:                        return(&process[i]);
                   3553:                }
                   3554:        }
                   3555:        fatalerror("too many processes\n");
                   3556:        return(NULL);
                   3557: }
                   3558: 
1.1.1.33  root     3559: process_t *msdos_process_info_get(UINT16 psp_seg, bool show_error)
1.1       root     3560: {
                   3561:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3562:                if(process[i].psp == psp_seg) {
                   3563:                        return(&process[i]);
                   3564:                }
                   3565:        }
1.1.1.33  root     3566:        if(show_error) {
                   3567:                fatalerror("invalid psp address\n");
                   3568:        }
1.1       root     3569:        return(NULL);
                   3570: }
                   3571: 
1.1.1.33  root     3572: process_t *msdos_process_info_get(UINT16 psp_seg)
                   3573: {
                   3574:        return(msdos_process_info_get(psp_seg, true));
                   3575: }
                   3576: 
1.1.1.23  root     3577: void msdos_sda_update(int psp_seg)
                   3578: {
                   3579:        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   3580:        
                   3581:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3582:                if(process[i].psp == psp_seg) {
                   3583:                        sda->switchar = process[i].switchar;
                   3584:                        sda->current_dta.w.l = process[i].dta.w.l;
                   3585:                        sda->current_dta.w.h = process[i].dta.w.h;
                   3586:                        sda->current_psp = process[i].psp;
                   3587:                        break;
                   3588:                }
                   3589:        }
                   3590:        sda->malloc_strategy = malloc_strategy;
                   3591:        sda->return_code = retval;
                   3592:        sda->current_drive = _getdrive();
                   3593: }
                   3594: 
1.1.1.13  root     3595: // dta info
                   3596: 
                   3597: void msdos_dta_info_init()
                   3598: {
1.1.1.14  root     3599:        for(int i = 0; i < MAX_DTAINFO; i++) {
1.1.1.13  root     3600:                dtalist[i].find_handle = INVALID_HANDLE_VALUE;
                   3601:        }
                   3602: }
                   3603: 
                   3604: dtainfo_t *msdos_dta_info_get(UINT16 psp_seg, UINT32 dta_laddr)
                   3605: {
                   3606:        dtainfo_t *free_dta = NULL;
1.1.1.14  root     3607:        for(int i = 0; i < MAX_DTAINFO; i++) {
                   3608:                if(dtalist[i].find_handle == INVALID_HANDLE_VALUE) {
                   3609:                        if(free_dta == NULL) {
1.1.1.13  root     3610:                                free_dta = &dtalist[i];
                   3611:                        }
1.1.1.14  root     3612:                } else if(dta_laddr < LFN_DTA_LADDR && dtalist[i].dta == dta_laddr) {
1.1.1.13  root     3613:                        return(&dtalist[i]);
                   3614:                }
                   3615:        }
1.1.1.14  root     3616:        if(free_dta) {
1.1.1.13  root     3617:                free_dta->psp = psp_seg;
                   3618:                free_dta->dta = dta_laddr;
                   3619:                return(free_dta);
                   3620:        }
                   3621:        fatalerror("too many dta\n");
                   3622:        return(NULL);
                   3623: }
                   3624: 
                   3625: void msdos_dta_info_free(UINT16 psp_seg)
                   3626: {
1.1.1.14  root     3627:        for(int i = 0; i < MAX_DTAINFO; i++) {
                   3628:                if(dtalist[i].psp == psp_seg && dtalist[i].find_handle != INVALID_HANDLE_VALUE) {
1.1.1.13  root     3629:                        FindClose(dtalist[i].find_handle);
                   3630:                        dtalist[i].find_handle = INVALID_HANDLE_VALUE;
                   3631:                }
                   3632:        }
                   3633: }
                   3634: 
1.1       root     3635: void msdos_cds_update(int drv)
                   3636: {
                   3637:        cds_t *cds = (cds_t *)(mem + CDS_TOP);
                   3638:        
                   3639:        memset(mem + CDS_TOP, 0, CDS_SIZE);
                   3640:        sprintf(cds->path_name, "%c:\\", 'A' + drv);
                   3641:        cds->drive_attrib = 0x4000;     // physical drive
                   3642:        cds->physical_drive_number = drv;
                   3643: }
                   3644: 
1.1.1.17  root     3645: // nls information tables
                   3646: 
                   3647: // uppercase table (func 6502h)
                   3648: void msdos_upper_table_update()
                   3649: {
                   3650:        *(UINT16 *)(mem + UPPERTABLE_TOP) = 0x80;
1.1.1.22  root     3651:        for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17  root     3652:                UINT8 c[4];
1.1.1.33  root     3653:                *(UINT32 *)c = 0;               // reset internal conversion state
                   3654:                CharUpperBuffA((LPSTR)c, 4);    // (workaround for MBCS codepage)
1.1.1.17  root     3655:                c[0] = 0x80 + i;
                   3656:                DWORD rc = CharUpperBuffA((LPSTR)c, 1);
                   3657:                mem[UPPERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
                   3658:        }
                   3659: }
                   3660: 
1.1.1.23  root     3661: // lowercase table (func 6503h)
                   3662: void msdos_lower_table_update()
                   3663: {
                   3664:        *(UINT16 *)(mem + LOWERTABLE_TOP) = 0x80;
                   3665:        for(unsigned i = 0; i < 0x80; ++i) {
                   3666:                UINT8 c[4];
1.1.1.33  root     3667:                *(UINT32 *)c = 0;               // reset internal conversion state
                   3668:                CharLowerBuffA((LPSTR)c, 4);    // (workaround for MBCS codepage)
1.1.1.23  root     3669:                c[0] = 0x80 + i;
                   3670:                DWORD rc = CharLowerBuffA((LPSTR)c, 1);
                   3671:                mem[LOWERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
                   3672:        }
                   3673: }
                   3674: 
1.1.1.17  root     3675: // filename uppercase table (func 6504h)
                   3676: void msdos_filename_upper_table_init()
                   3677: {
                   3678:        // depended on (file)system, not on active codepage
                   3679:        // temporary solution: just filling data
                   3680:        *(UINT16 *)(mem + FILENAME_UPPERTABLE_TOP) = 0x80;
1.1.1.22  root     3681:        for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17  root     3682:                mem[FILENAME_UPPERTABLE_TOP + 2 + i] = 0x80 + i;
                   3683:        }
                   3684: }
                   3685: 
                   3686: // filaname terminator table (func 6505h)
                   3687: void msdos_filename_terminator_table_init()
                   3688: {
                   3689:        const char illegal_chars[] = ".\"/\\[]:|<>+=;,";        // for standard MS-DOS fs.
                   3690:        UINT8 *data = mem + FILENAME_TERMINATOR_TOP;
                   3691:        
                   3692:        data[2] = 1;            // marker? (permissible character value)
                   3693:        data[3] = 0x00;         // 00h...FFh
                   3694:        data[4] = 0xff;
                   3695:        data[5] = 0;            // marker? (excluded character)
                   3696:        data[6] = 0x00;         // 00h...20h
                   3697:        data[7] = 0x20;
                   3698:        data[8] = 2;            // marker? (illegal characters for filename)
                   3699:        data[9] = (UINT8)strlen(illegal_chars);
                   3700:        memcpy(data + 10, illegal_chars, data[9]);
                   3701:        
                   3702:        // total length
                   3703:        *(UINT16 *)data = (10 - 2) + data[9];
                   3704: }
                   3705: 
                   3706: // collating table (func 6506h)
                   3707: void msdos_collating_table_update()
                   3708: {
                   3709:        // temporary solution: just filling data
                   3710:        *(UINT16 *)(mem + COLLATING_TABLE_TOP) = 0x100;
1.1.1.22  root     3711:        for(unsigned i = 0; i < 256; ++i) {
1.1.1.17  root     3712:                mem[COLLATING_TABLE_TOP + 2 + i] = i;
                   3713:        }
                   3714: }
                   3715: 
1.1       root     3716: // dbcs
                   3717: 
                   3718: void msdos_dbcs_table_update()
                   3719: {
                   3720:        UINT8 dbcs_data[DBCS_SIZE];
                   3721:        memset(dbcs_data, 0, sizeof(dbcs_data));
                   3722:        
                   3723:        CPINFO info;
                   3724:        GetCPInfo(active_code_page, &info);
                   3725:        
                   3726:        if(info.MaxCharSize != 1) {
                   3727:                for(int i = 0;; i += 2) {
                   3728:                        UINT8 lo = info.LeadByte[i + 0];
                   3729:                        UINT8 hi = info.LeadByte[i + 1];
                   3730:                        dbcs_data[2 + i + 0] = lo;
                   3731:                        dbcs_data[2 + i + 1] = hi;
                   3732:                        if(lo == 0 && hi == 0) {
                   3733:                                dbcs_data[0] = i + 2;
                   3734:                                break;
                   3735:                        }
                   3736:                }
                   3737:        } else {
                   3738:                dbcs_data[0] = 2;       // ???
                   3739:        }
                   3740:        memcpy(mem + DBCS_TOP, dbcs_data, sizeof(dbcs_data));
                   3741: }
                   3742: 
1.1.1.17  root     3743: void msdos_dbcs_table_finish()
                   3744: {
1.1.1.32  root     3745:        if(system_code_page != _getmbcp()) {
1.1.1.17  root     3746:                _setmbcp(system_code_page);
                   3747:        }
1.1.1.32  root     3748:        if(console_code_page != GetConsoleCP()) {
                   3749:                SetConsoleCP(console_code_page);
                   3750:                SetConsoleOutputCP(console_code_page);
                   3751:        }
1.1.1.17  root     3752: }
                   3753: 
                   3754: void msdos_nls_tables_init()
1.1       root     3755: {
1.1.1.32  root     3756:        active_code_page = console_code_page = GetConsoleCP();
                   3757:        system_code_page = _getmbcp();
                   3758:        
                   3759:        if(active_code_page != system_code_page) {
                   3760:                if(_setmbcp(active_code_page) != 0) {
                   3761:                        active_code_page = system_code_page;
                   3762:                }
                   3763:        }
                   3764:        
1.1.1.17  root     3765:        msdos_upper_table_update();
1.1.1.23  root     3766:        msdos_lower_table_update();
1.1.1.17  root     3767:        msdos_filename_terminator_table_init();
                   3768:        msdos_filename_upper_table_init();
                   3769:        msdos_collating_table_update();
1.1       root     3770:        msdos_dbcs_table_update();
                   3771: }
                   3772: 
1.1.1.17  root     3773: void msdos_nls_tables_update()
1.1       root     3774: {
1.1.1.17  root     3775:        msdos_dbcs_table_update();
                   3776:        msdos_upper_table_update();
1.1.1.23  root     3777:        msdos_lower_table_update();
                   3778: //     msdos_collating_table_update();
1.1       root     3779: }
                   3780: 
                   3781: int msdos_lead_byte_check(UINT8 code)
                   3782: {
                   3783:        UINT8 *dbcs_table = mem + DBCS_TABLE;
                   3784:        
                   3785:        for(int i = 0;; i += 2) {
                   3786:                UINT8 lo = dbcs_table[i + 0];
                   3787:                UINT8 hi = dbcs_table[i + 1];
                   3788:                if(lo == 0 && hi == 0) {
                   3789:                        break;
                   3790:                }
                   3791:                if(lo <= code && code <= hi) {
                   3792:                        return(1);
                   3793:                }
                   3794:        }
                   3795:        return(0);
                   3796: }
                   3797: 
1.1.1.20  root     3798: int msdos_ctrl_code_check(UINT8 code)
                   3799: {
1.1.1.22  root     3800:        return (code >= 0x01 && code <= 0x1a && code != 0x07 && code != 0x08 && code != 0x09 && code != 0x0a && code != 0x0d);
1.1.1.20  root     3801: }
                   3802: 
1.1.1.36  root     3803: int msdos_kanji_2nd_byte_check(UINT8 *buf, int n)
                   3804: {
                   3805:        int is_kanji_1st = 0;
                   3806:        int is_kanji_2nd = 0;
                   3807:        
                   3808:        for(int p = 0;; p++) {
                   3809:                if(is_kanji_1st) {
                   3810:                        is_kanji_1st = 0;
                   3811:                        is_kanji_2nd = 1;
                   3812:                } else if(msdos_lead_byte_check(buf[p])) {
                   3813:                        is_kanji_1st = 1;
                   3814:                }
                   3815:                if(p == n) {
                   3816:                        return(is_kanji_2nd);
                   3817:                }
                   3818:                is_kanji_2nd = 0;
                   3819:        }
                   3820: }
                   3821: 
1.1       root     3822: // file control
                   3823: 
1.1.1.14  root     3824: char *msdos_remove_double_quote(char *path)
                   3825: {
                   3826:        static char tmp[MAX_PATH];
                   3827:        
                   3828:        memset(tmp, 0, sizeof(tmp));
                   3829:        if(strlen(path) >= 2 && path[0] == '"' && path[strlen(path) - 1] == '"') {
                   3830:                memcpy(tmp, path + 1, strlen(path) - 2);
                   3831:        } else {
                   3832:                strcpy(tmp, path);
                   3833:        }
                   3834:        return(tmp);
                   3835: }
                   3836: 
1.1.1.32  root     3837: char *msdos_remove_end_separator(char *path)
                   3838: {
                   3839:        static char tmp[MAX_PATH];
                   3840:        
                   3841:        strcpy(tmp, path);
                   3842:        int len = strlen(tmp);
                   3843:        if(len > 3 && tmp[len - 1] == '\\') {
                   3844:                tmp[len - 1] = '\0';
                   3845:        }
                   3846:        return(tmp);
                   3847: }
                   3848: 
1.1.1.14  root     3849: char *msdos_combine_path(char *dir, const char *file)
                   3850: {
                   3851:        static char tmp[MAX_PATH];
                   3852:        char *tmp_dir = msdos_remove_double_quote(dir);
                   3853:        
                   3854:        if(strlen(tmp_dir) == 0) {
                   3855:                strcpy(tmp, file);
                   3856:        } else if(tmp_dir[strlen(tmp_dir) - 1] == '\\') {
                   3857:                sprintf(tmp, "%s%s", tmp_dir, file);
                   3858:        } else {
                   3859:                sprintf(tmp, "%s\\%s", tmp_dir, file);
                   3860:        }
                   3861:        return(tmp);
                   3862: }
                   3863: 
1.1       root     3864: char *msdos_trimmed_path(char *path, int lfn)
                   3865: {
                   3866:        static char tmp[MAX_PATH];
                   3867:        
                   3868:        if(lfn) {
                   3869:                strcpy(tmp, path);
                   3870:        } else {
                   3871:                // remove space in the path
                   3872:                char *src = path, *dst = tmp;
                   3873:                
                   3874:                while(*src != '\0') {
                   3875:                        if(msdos_lead_byte_check(*src)) {
                   3876:                                *dst++ = *src++;
                   3877:                                *dst++ = *src++;
                   3878:                        } else if(*src != ' ') {
                   3879:                                *dst++ = *src++;
                   3880:                        } else {
                   3881:                                src++;  // skip space
                   3882:                        }
                   3883:                }
                   3884:                *dst = '\0';
                   3885:        }
1.1.1.14  root     3886:        if(_stricmp(tmp, "C:\\COMMAND.COM") == 0) {
                   3887:                // redirect C:\COMMAND.COM to comspec_path
                   3888:                strcpy(tmp, comspec_path);
                   3889:        } else if(is_vista_or_later && _access(tmp, 0) != 0) {
                   3890:                // redirect new files (without wildcards) in C:\ to %TEMP%, since C:\ is not usually writable
                   3891:                static int root_drive_protected = -1;
                   3892:                char temp[MAX_PATH], name[MAX_PATH], *name_temp = NULL;
                   3893:                dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   3894:                
                   3895:                if(GetFullPathName(tmp, MAX_PATH, temp, &name_temp) != 0 &&
                   3896:                   name_temp != NULL && strstr(name_temp, "?") == NULL && strstr(name_temp, "*") == NULL) {
                   3897:                        strcpy(name, name_temp);
                   3898:                        name_temp[0] = '\0';
                   3899:                        
                   3900:                        if((temp[0] == 'A' + dos_info->boot_drive - 1 || temp[0] == 'a' + dos_info->boot_drive - 1) &&
                   3901:                           (temp[1] == ':') && (temp[2] == '\\' || temp[2] == '/') && (temp[3] == '\0')) {
                   3902:                                if(root_drive_protected == -1) {
                   3903:                                        FILE *fp = NULL;
                   3904:                                        
                   3905:                                        sprintf(temp, "%c:\\MS-DOS_Player.$$$", 'A' + dos_info->boot_drive - 1);
                   3906:                                        root_drive_protected = 1;
                   3907:                                        try {
                   3908:                                                if((fp = fopen(temp, "w")) != NULL) {
                   3909:                                                        if(fprintf(fp, "TEST") == 4) {
                   3910:                                                                root_drive_protected = 0;
                   3911:                                                        }
                   3912:                                                }
                   3913:                                        } catch(...) {
                   3914:                                        }
                   3915:                                        if(fp != NULL) {
                   3916:                                                fclose(fp);
                   3917:                                        }
                   3918:                                        if(_access(temp, 0) == 0) {
                   3919:                                                remove(temp);
                   3920:                                        }
                   3921:                                }
                   3922:                                if(root_drive_protected == 1) {
                   3923:                                        if(GetEnvironmentVariable("TEMP", temp, MAX_PATH) != 0 ||
                   3924:                                           GetEnvironmentVariable("TMP",  temp, MAX_PATH) != 0) {
                   3925:                                                strcpy(tmp, msdos_combine_path(temp, name));
                   3926:                                        }
                   3927:                                }
                   3928:                        }
                   3929:                }
                   3930:        }
1.1       root     3931:        return(tmp);
                   3932: }
                   3933: 
1.1.1.28  root     3934: char *msdos_get_multiple_short_path(char *src)
                   3935: {
1.1.1.32  root     3936:        // "LONGPATH\";"LONGPATH\";"LONGPATH\" to SHORTPATH;SHORTPATH;SHORTPATH
1.1.1.28  root     3937:        static char env_path[ENV_SIZE];
                   3938:        char tmp[ENV_SIZE], *token;
                   3939:        
                   3940:        memset(env_path, 0, sizeof(env_path));
                   3941:        strcpy(tmp, src);
                   3942:        token = my_strtok(tmp, ";");
                   3943:        
                   3944:        while(token != NULL) {
                   3945:                if(token[0] != '\0') {
                   3946:                        char *path = msdos_remove_double_quote(token), short_path[MAX_PATH];
1.1.1.32  root     3947:                        if(path != NULL && strlen(path) != 0) {
                   3948:                                if(env_path[0] != '\0') {
                   3949:                                        strcat(env_path, ";");
                   3950:                                }
1.1.1.28  root     3951:                                if(GetShortPathName(path, short_path, MAX_PATH) == 0) {
1.1.1.32  root     3952:                                        strcat(env_path, msdos_remove_end_separator(path));
1.1.1.28  root     3953:                                } else {
                   3954:                                        my_strupr(short_path);
1.1.1.32  root     3955:                                        strcat(env_path, msdos_remove_end_separator(short_path));
1.1.1.28  root     3956:                                }
                   3957:                        }
                   3958:                }
                   3959:                token = my_strtok(NULL, ";");
                   3960:        }
                   3961:        return(env_path);
                   3962: }
                   3963: 
1.1       root     3964: bool match(char *text, char *pattern)
                   3965: {
1.1.1.24  root     3966:        // http://www.prefield.com/algorithm/string/wildcard.html
1.1.1.14  root     3967:        switch(*pattern) {
1.1       root     3968:        case '\0':
                   3969:                return !*text;
                   3970:        case '*':
1.1.1.14  root     3971:                return match(text, pattern + 1) || (*text && match(text + 1, pattern));
1.1       root     3972:        case '?':
                   3973:                return *text && match(text + 1, pattern + 1);
                   3974:        default:
                   3975:                return (*text == *pattern) && match(text + 1, pattern + 1);
                   3976:        }
                   3977: }
                   3978: 
                   3979: bool msdos_match_volume_label(char *path, char *volume)
                   3980: {
                   3981:        char *p;
                   3982:        
1.1.1.14  root     3983:        if(!*volume) {
                   3984:                return false;
                   3985:        } else if((p = my_strchr(path, ':')) != NULL) {
1.1       root     3986:                return msdos_match_volume_label(p + 1, volume);
                   3987:        } else if((p = my_strchr(path, '\\')) != NULL) {
                   3988:                return msdos_match_volume_label(p + 1, volume);
                   3989:        } else if((p = my_strchr(path, '.')) != NULL) {
1.1.1.14  root     3990:                char tmp[MAX_PATH];
                   3991:                sprintf(tmp, "%.*s%s", (int)(p - path), path, p + 1);
                   3992:                return match(volume, tmp);
1.1       root     3993:        } else {
                   3994:                return match(volume, path);
                   3995:        }
                   3996: }
                   3997: 
                   3998: char *msdos_fcb_path(fcb_t *fcb)
                   3999: {
                   4000:        static char tmp[MAX_PATH];
                   4001:        char name[9], ext[4];
                   4002:        
                   4003:        memset(name, 0, sizeof(name));
                   4004:        memcpy(name, fcb->file_name, 8);
                   4005:        strcpy(name, msdos_trimmed_path(name, 0));
                   4006:        
                   4007:        memset(ext, 0, sizeof(ext));
                   4008:        memcpy(ext, fcb->file_name + 8, 3);
                   4009:        strcpy(ext, msdos_trimmed_path(ext, 0));
                   4010:        
                   4011:        if(name[0] == '\0' || strcmp(name, "????????") == 0) {
                   4012:                strcpy(name, "*");
                   4013:        }
                   4014:        if(ext[0] == '\0') {
                   4015:                strcpy(tmp, name);
                   4016:        } else {
                   4017:                if(strcmp(ext, "???") == 0) {
                   4018:                        strcpy(ext, "*");
                   4019:                }
                   4020:                sprintf(tmp, "%s.%s", name, ext);
                   4021:        }
                   4022:        return(tmp);
                   4023: }
                   4024: 
                   4025: void msdos_set_fcb_path(fcb_t *fcb, char *path)
                   4026: {
                   4027:        char *ext = my_strchr(path, '.');
                   4028:        
                   4029:        memset(fcb->file_name, 0x20, 8 + 3);
                   4030:        if(ext != NULL && path[0] != '.') {
                   4031:                *ext = '\0';
                   4032:                memcpy(fcb->file_name + 8, ext + 1, strlen(ext + 1));
                   4033:        }
                   4034:        memcpy(fcb->file_name, path, strlen(path));
                   4035: }
                   4036: 
                   4037: char *msdos_short_path(char *path)
                   4038: {
                   4039:        static char tmp[MAX_PATH];
                   4040:        
1.1.1.24  root     4041:        if(GetShortPathName(path, tmp, MAX_PATH) == 0) {
                   4042:                strcpy(tmp, path);
                   4043:        }
1.1       root     4044:        my_strupr(tmp);
                   4045:        return(tmp);
                   4046: }
                   4047: 
1.1.1.13  root     4048: char *msdos_short_name(WIN32_FIND_DATA *fd)
                   4049: {
                   4050:        static char tmp[MAX_PATH];
                   4051: 
1.1.1.14  root     4052:        if(fd->cAlternateFileName[0]) {
1.1.1.13  root     4053:                strcpy(tmp, fd->cAlternateFileName);
                   4054:        } else {
                   4055:                strcpy(tmp, fd->cFileName);
                   4056:        }
                   4057:        my_strupr(tmp);
                   4058:        return(tmp);
                   4059: }
                   4060: 
1.1       root     4061: char *msdos_short_full_path(char *path)
                   4062: {
                   4063:        static char tmp[MAX_PATH];
                   4064:        char full[MAX_PATH], *name;
                   4065:        
1.1.1.14  root     4066:        // Full works with non-existent files, but Short does not
1.1       root     4067:        GetFullPathName(path, MAX_PATH, full, &name);
1.1.1.14  root     4068:        *tmp = '\0';
                   4069:        if(GetShortPathName(full, tmp, MAX_PATH) == 0 && name > path) {
                   4070:                name[-1] = '\0';
                   4071:                DWORD len = GetShortPathName(full, tmp, MAX_PATH);
                   4072:                if(len == 0) {
                   4073:                        strcpy(tmp, full);
                   4074:                } else {
                   4075:                        tmp[len++] = '\\';
                   4076:                        strcpy(tmp + len, name);
                   4077:                }
                   4078:        }
1.1       root     4079:        my_strupr(tmp);
                   4080:        return(tmp);
                   4081: }
                   4082: 
                   4083: char *msdos_short_full_dir(char *path)
                   4084: {
                   4085:        static char tmp[MAX_PATH];
                   4086:        char full[MAX_PATH], *name;
                   4087:        
                   4088:        GetFullPathName(path, MAX_PATH, full, &name);
                   4089:        name[-1] = '\0';
1.1.1.24  root     4090:        if(GetShortPathName(full, tmp, MAX_PATH) == 0) {
                   4091:                strcpy(tmp, full);
                   4092:        }
1.1       root     4093:        my_strupr(tmp);
                   4094:        return(tmp);
                   4095: }
                   4096: 
                   4097: char *msdos_local_file_path(char *path, int lfn)
                   4098: {
                   4099:        char *trimmed = msdos_trimmed_path(path, lfn);
1.1.1.14  root     4100: #if 0
                   4101:        // I have forgotten the reason of this routine... :-(
1.1       root     4102:        if(_access(trimmed, 0) != 0) {
                   4103:                process_t *process = msdos_process_info_get(current_psp);
                   4104:                static char tmp[MAX_PATH];
                   4105:                
                   4106:                sprintf(tmp, "%s\\%s", process->module_dir, trimmed);
                   4107:                if(_access(tmp, 0) == 0) {
                   4108:                        return(tmp);
                   4109:                }
                   4110:        }
1.1.1.14  root     4111: #endif
1.1       root     4112:        return(trimmed);
                   4113: }
                   4114: 
1.1.1.29  root     4115: bool msdos_is_device_path(char *path)
1.1.1.11  root     4116: {
                   4117:        char full[MAX_PATH], *name;
                   4118:        
1.1.1.24  root     4119:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4120:                if(_stricmp(full, "\\\\.\\AUX" ) == 0 ||
                   4121:                   _stricmp(full, "\\\\.\\CON" ) == 0 ||
                   4122:                   _stricmp(full, "\\\\.\\NUL" ) == 0 ||
                   4123:                   _stricmp(full, "\\\\.\\PRN" ) == 0 ||
                   4124:                   _stricmp(full, "\\\\.\\COM1") == 0 ||
                   4125:                   _stricmp(full, "\\\\.\\COM2") == 0 ||
                   4126:                   _stricmp(full, "\\\\.\\COM3") == 0 ||
                   4127:                   _stricmp(full, "\\\\.\\COM4") == 0 ||
                   4128:                   _stricmp(full, "\\\\.\\COM5") == 0 ||
                   4129:                   _stricmp(full, "\\\\.\\COM6") == 0 ||
                   4130:                   _stricmp(full, "\\\\.\\COM7") == 0 ||
                   4131:                   _stricmp(full, "\\\\.\\COM8") == 0 ||
                   4132:                   _stricmp(full, "\\\\.\\COM9") == 0 ||
                   4133:                   _stricmp(full, "\\\\.\\LPT1") == 0 ||
                   4134:                   _stricmp(full, "\\\\.\\LPT2") == 0 ||
                   4135:                   _stricmp(full, "\\\\.\\LPT3") == 0 ||
                   4136:                   _stricmp(full, "\\\\.\\LPT4") == 0 ||
                   4137:                   _stricmp(full, "\\\\.\\LPT5") == 0 ||
                   4138:                   _stricmp(full, "\\\\.\\LPT6") == 0 ||
                   4139:                   _stricmp(full, "\\\\.\\LPT7") == 0 ||
                   4140:                   _stricmp(full, "\\\\.\\LPT8") == 0 ||
                   4141:                   _stricmp(full, "\\\\.\\LPT9") == 0) {
                   4142:                        return(true);
                   4143:                } else if(name != NULL) {
                   4144:                        if(_stricmp(name, "CLOCK$"  ) == 0 ||
                   4145:                           _stricmp(name, "CONFIG$" ) == 0 ||
1.1.1.30  root     4146:                           _stricmp(name, "EMMXXXX0") == 0 ||
                   4147:                           _stricmp(name, "$IBMAIAS") == 0) {
1.1.1.29  root     4148:                                return(true);
                   4149:                        }
                   4150:                }
1.1.1.24  root     4151:        }
                   4152:        return(false);
1.1.1.11  root     4153: }
                   4154: 
1.1.1.29  root     4155: bool msdos_is_con_path(char *path)
1.1.1.8   root     4156: {
1.1.1.14  root     4157:        char full[MAX_PATH], *name;
1.1.1.8   root     4158:        
1.1.1.24  root     4159:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4160:                return(_stricmp(full, "\\\\.\\CON") == 0);
1.1.1.24  root     4161:        }
                   4162:        return(false);
                   4163: }
                   4164: 
1.1.1.29  root     4165: int msdos_is_comm_path(char *path)
1.1.1.24  root     4166: {
                   4167:        char full[MAX_PATH], *name;
                   4168:        
                   4169:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4170:                if(_stricmp(full, "\\\\.\\COM1") == 0) {
                   4171:                        return(1);
                   4172:                } else if(_stricmp(full, "\\\\.\\COM2") == 0) {
                   4173:                        return(2);
                   4174:                } else if(_stricmp(full, "\\\\.\\COM3") == 0) {
                   4175:                        return(3);
                   4176:                } else if(_stricmp(full, "\\\\.\\COM4") == 0) {
                   4177:                        return(4);
1.1.1.24  root     4178:                }
                   4179:        }
1.1.1.29  root     4180:        return(0);
                   4181: }
                   4182: 
1.1.1.37! root     4183: void msdos_set_comm_params(int sio_port, char *path)
        !          4184: {
        !          4185:        // COM1:{110,150,300,600,1200,2400,4800,9600},{N,O,E,M,S},{8,7,6,5},{1,1.5,2}
        !          4186:        char *p = NULL;
        !          4187:        
        !          4188:        if((p = strstr(path, ":")) != NULL) {
        !          4189:                UINT8 selector = sio_read(sio_port - 1, 3);
        !          4190:                
        !          4191:                // baud rate
        !          4192:                int baud = max(110, min(9600, atoi(p + 1)));
        !          4193:                UINT16 divisor = 115200 / baud;
        !          4194:                
        !          4195:                if((p = strstr(p + 1, ",")) != NULL) {
        !          4196:                        // parity
        !          4197:                        if(p[1] == 'N' || p[1] == 'n') {
        !          4198:                                selector = (selector & ~0x38) | 0x00;
        !          4199:                        } else if(p[1] == 'O' || p[1] == 'o') {
        !          4200:                                selector = (selector & ~0x38) | 0x08;
        !          4201:                        } else if(p[1] == 'E' || p[1] == 'e') {
        !          4202:                                selector = (selector & ~0x38) | 0x18;
        !          4203:                        } else if(p[1] == 'M' || p[1] == 'm') {
        !          4204:                                selector = (selector & ~0x38) | 0x28;
        !          4205:                        } else if(p[1] == 'S' || p[1] == 's') {
        !          4206:                                selector = (selector & ~0x38) | 0x38;
        !          4207:                        }
        !          4208:                        if((p = strstr(p + 1, ",")) != NULL) {
        !          4209:                                // word length
        !          4210:                                if(p[1] == '8') {
        !          4211:                                        selector = (selector & ~0x03) | 0x03;
        !          4212:                                } else if(p[1] == '7') {
        !          4213:                                        selector = (selector & ~0x03) | 0x02;
        !          4214:                                } else if(p[1] == '6') {
        !          4215:                                        selector = (selector & ~0x03) | 0x01;
        !          4216:                                } else if(p[1] == '5') {
        !          4217:                                        selector = (selector & ~0x03) | 0x00;
        !          4218:                                }
        !          4219:                                if((p = strstr(p + 1, ",")) != NULL) {
        !          4220:                                        // stop bits
        !          4221:                                        float bits = atof(p + 1);
        !          4222:                                        if(bits > 1.0F) {
        !          4223:                                                selector |= 0x04;
        !          4224:                                        } else {
        !          4225:                                                selector &= ~0x04;
        !          4226:                                        }
        !          4227:                                }
        !          4228:                        }
        !          4229:                }
        !          4230:                sio_write(sio_port - 1, 3, selector | 0x80);
        !          4231:                sio_write(sio_port - 1, 0, divisor & 0xff);
        !          4232:                sio_write(sio_port - 1, 1, divisor >> 8);
        !          4233:                sio_write(sio_port - 1, 3, selector);
        !          4234:        }
        !          4235: }
        !          4236: 
1.1.1.30  root     4237: int msdos_is_prn_path(char *path)
                   4238: {
                   4239:        char full[MAX_PATH], *name;
                   4240:        
                   4241:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
                   4242:                if(_stricmp(full, "\\\\.\\PRN") == 0) {
                   4243:                        return(1);
                   4244:                } else if(_stricmp(full, "\\\\.\\LPT1") == 0) {
                   4245:                        return(1);
                   4246:                } else if(_stricmp(full, "\\\\.\\LPT2") == 0) {
                   4247:                        return(2);
                   4248:                } else if(_stricmp(full, "\\\\.\\LPT3") == 0) {
                   4249:                        return(3);
                   4250:                }
                   4251:        }
                   4252:        return(0);
                   4253: }
                   4254: 
1.1.1.24  root     4255: bool msdos_is_existing_file(char *path)
                   4256: {
                   4257:        // http://d.hatena.ne.jp/yu-hr/20100317/1268826458
                   4258:        WIN32_FIND_DATA FindData;
                   4259:        HANDLE hFind;
                   4260:        
                   4261:        if((hFind = FindFirstFile(path, &FindData)) != INVALID_HANDLE_VALUE) {
                   4262:                FindClose(hFind);
                   4263:                return(!(FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY));
                   4264:        }
                   4265:        return(false);
1.1.1.8   root     4266: }
                   4267: 
1.1.1.9   root     4268: char *msdos_search_command_com(char *command_path, char *env_path)
                   4269: {
                   4270:        static char tmp[MAX_PATH];
1.1.1.28  root     4271:        char path[ENV_SIZE], *file_name;
1.1.1.9   root     4272:        
1.1.1.28  root     4273:        // check if COMMAND.COM is in the same directory as the target program file
1.1.1.9   root     4274:        if(GetFullPathName(command_path, MAX_PATH, tmp, &file_name) != 0) {
                   4275:                sprintf(file_name, "COMMAND.COM");
                   4276:                if(_access(tmp, 0) == 0) {
                   4277:                        return(tmp);
                   4278:                }
                   4279:        }
1.1.1.28  root     4280:        
                   4281:        // check if COMMAND.COM is in the same directory as the running msdos.exe
1.1.1.9   root     4282:        if(GetModuleFileName(NULL, path, MAX_PATH) != 0 && GetFullPathName(path, MAX_PATH, tmp, &file_name) != 0) {
                   4283:                sprintf(file_name, "COMMAND.COM");
                   4284:                if(_access(tmp, 0) == 0) {
                   4285:                        return(tmp);
                   4286:                }
                   4287:        }
1.1.1.28  root     4288:        
                   4289:        // check if COMMAND.COM is in the current directory
1.1.1.9   root     4290:        if(GetFullPathName("COMMAND.COM", MAX_PATH, tmp, &file_name) != 0) {
                   4291:                if(_access(tmp, 0) == 0) {
                   4292:                        return(tmp);
                   4293:                }
                   4294:        }
1.1.1.28  root     4295:        
                   4296:        // cehck if COMMAND.COM is in the directory that is in MSDOS_PATH and PATH environment variables
                   4297:        strcpy(path, env_path);
                   4298:        char *token = my_strtok(path, ";");
1.1.1.9   root     4299:        while(token != NULL) {
1.1.1.14  root     4300:                if(strlen(token) != 0 && token[0] != '%') {
1.1.1.9   root     4301:                        strcpy(tmp, msdos_combine_path(token, "COMMAND.COM"));
                   4302:                        if(_access(tmp, 0) == 0) {
                   4303:                                return(tmp);
                   4304:                        }
                   4305:                }
                   4306:                token = my_strtok(NULL, ";");
                   4307:        }
                   4308:        return(NULL);
                   4309: }
                   4310: 
1.1.1.14  root     4311: int msdos_drive_number(const char *path)
1.1       root     4312: {
                   4313:        char tmp[MAX_PATH], *name;
                   4314:        
                   4315:        GetFullPathName(path, MAX_PATH, tmp, &name);
                   4316:        if(tmp[0] >= 'a' && tmp[0] <= 'z') {
                   4317:                return(tmp[0] - 'a');
                   4318:        } else {
                   4319:                return(tmp[0] - 'A');
                   4320:        }
                   4321: }
                   4322: 
                   4323: char *msdos_volume_label(char *path)
                   4324: {
                   4325:        static char tmp[MAX_PATH];
                   4326:        char volume[] = "A:\\";
                   4327:        
                   4328:        if(path[1] == ':') {
                   4329:                volume[0] = path[0];
                   4330:        } else {
                   4331:                volume[0] = 'A' + _getdrive() - 1;
                   4332:        }
                   4333:        if(!GetVolumeInformation(volume, tmp, MAX_PATH, NULL, NULL, NULL, NULL, 0)) {
                   4334:                memset(tmp, 0, sizeof(tmp));
                   4335:        }
                   4336:        return(tmp);
                   4337: }
                   4338: 
                   4339: char *msdos_short_volume_label(char *label)
                   4340: {
                   4341:        static char tmp[(8 + 1 + 3) + 1];
                   4342:        char *src = label;
                   4343:        int remain = strlen(label);
                   4344:        char *dst_n = tmp;
                   4345:        char *dst_e = tmp + 9;
                   4346:        
                   4347:        strcpy(tmp, "        .   ");
                   4348:        for(int i = 0; i < 8 && remain > 0; i++) {
                   4349:                if(msdos_lead_byte_check(*src)) {
                   4350:                        if(++i == 8) {
                   4351:                                break;
                   4352:                        }
                   4353:                        *dst_n++ = *src++;
                   4354:                        remain--;
                   4355:                }
                   4356:                *dst_n++ = *src++;
                   4357:                remain--;
                   4358:        }
                   4359:        if(remain > 0) {
                   4360:                for(int i = 0; i < 3 && remain > 0; i++) {
                   4361:                        if(msdos_lead_byte_check(*src)) {
                   4362:                                if(++i == 3) {
                   4363:                                        break;
                   4364:                                }
                   4365:                                *dst_e++ = *src++;
                   4366:                                remain--;
                   4367:                        }
                   4368:                        *dst_e++ = *src++;
                   4369:                        remain--;
                   4370:                }
                   4371:                *dst_e = '\0';
                   4372:        } else {
                   4373:                *dst_n = '\0';
                   4374:        }
                   4375:        my_strupr(tmp);
                   4376:        return(tmp);
                   4377: }
                   4378: 
1.1.1.13  root     4379: errno_t msdos_maperr(unsigned long oserrno)
                   4380: {
                   4381:        _doserrno = oserrno;
1.1.1.14  root     4382:        switch(oserrno) {
1.1.1.13  root     4383:        case ERROR_FILE_NOT_FOUND:         // 2
                   4384:        case ERROR_PATH_NOT_FOUND:         // 3
                   4385:        case ERROR_INVALID_DRIVE:          // 15
                   4386:        case ERROR_NO_MORE_FILES:          // 18
                   4387:        case ERROR_BAD_NETPATH:            // 53
                   4388:        case ERROR_BAD_NET_NAME:           // 67
                   4389:        case ERROR_BAD_PATHNAME:           // 161
                   4390:        case ERROR_FILENAME_EXCED_RANGE:   // 206
                   4391:                return ENOENT;
                   4392:        case ERROR_TOO_MANY_OPEN_FILES:    // 4
                   4393:                return EMFILE;
                   4394:        case ERROR_ACCESS_DENIED:          // 5
                   4395:        case ERROR_CURRENT_DIRECTORY:      // 16
                   4396:        case ERROR_NETWORK_ACCESS_DENIED:  // 65
                   4397:        case ERROR_CANNOT_MAKE:            // 82
                   4398:        case ERROR_FAIL_I24:               // 83
                   4399:        case ERROR_DRIVE_LOCKED:           // 108
                   4400:        case ERROR_SEEK_ON_DEVICE:         // 132
                   4401:        case ERROR_NOT_LOCKED:             // 158
                   4402:        case ERROR_LOCK_FAILED:            // 167
                   4403:                return EACCES;
                   4404:        case ERROR_INVALID_HANDLE:         // 6
                   4405:        case ERROR_INVALID_TARGET_HANDLE:  // 114
                   4406:        case ERROR_DIRECT_ACCESS_HANDLE:   // 130
                   4407:                return EBADF;
                   4408:        case ERROR_ARENA_TRASHED:          // 7
                   4409:        case ERROR_NOT_ENOUGH_MEMORY:      // 8
                   4410:        case ERROR_INVALID_BLOCK:          // 9
                   4411:        case ERROR_NOT_ENOUGH_QUOTA:       // 1816
                   4412:                return ENOMEM;
                   4413:        case ERROR_BAD_ENVIRONMENT:        // 10
                   4414:                return E2BIG;
                   4415:        case ERROR_BAD_FORMAT:             // 11
                   4416:                return ENOEXEC;
                   4417:        case ERROR_NOT_SAME_DEVICE:        // 17
                   4418:                return EXDEV;
                   4419:        case ERROR_FILE_EXISTS:            // 80
                   4420:        case ERROR_ALREADY_EXISTS:         // 183
                   4421:                return EEXIST;
                   4422:        case ERROR_NO_PROC_SLOTS:          // 89
                   4423:        case ERROR_MAX_THRDS_REACHED:      // 164
                   4424:        case ERROR_NESTING_NOT_ALLOWED:    // 215
                   4425:                return EAGAIN;
                   4426:        case ERROR_BROKEN_PIPE:            // 109
                   4427:                return EPIPE;
                   4428:        case ERROR_DISK_FULL:              // 112
                   4429:                return ENOSPC;
                   4430:        case ERROR_WAIT_NO_CHILDREN:       // 128
                   4431:        case ERROR_CHILD_NOT_COMPLETE:     // 129
                   4432:                return ECHILD;
                   4433:        case ERROR_DIR_NOT_EMPTY:          // 145
                   4434:                return ENOTEMPTY;
                   4435:        }
1.1.1.14  root     4436:        if(oserrno >= ERROR_WRITE_PROTECT /* 19 */ &&
1.1.1.13  root     4437:                oserrno <= ERROR_SHARING_BUFFER_EXCEEDED /* 36 */) {
                   4438:                return EACCES;
                   4439:        }
1.1.1.14  root     4440:        if(oserrno >= ERROR_INVALID_STARTING_CODESEG /* 188 */ &&
1.1.1.13  root     4441:                oserrno <= ERROR_INFLOOP_IN_RELOC_CHAIN /* 202 */) {
                   4442:                return ENOEXEC;
                   4443:        }
                   4444:        return EINVAL;
                   4445: }
                   4446: 
                   4447: int msdos_open(const char *filename, int oflag)
                   4448: {
1.1.1.14  root     4449:        if((oflag & (_O_RDONLY | _O_WRONLY | _O_RDWR)) != _O_RDONLY) {
1.1.1.13  root     4450:                return _open(filename, oflag);
                   4451:        }
1.1.1.14  root     4452:        
                   4453:        SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, !(oflag & _O_NOINHERIT) };
1.1.1.13  root     4454:        DWORD disposition;
1.1.1.14  root     4455:        switch(oflag & (_O_CREAT | _O_EXCL | _O_TRUNC)) {
                   4456:        default:
1.1.1.13  root     4457:        case _O_EXCL:
                   4458:                disposition = OPEN_EXISTING;
                   4459:                break;
                   4460:        case _O_CREAT:
                   4461:                disposition = OPEN_ALWAYS;
                   4462:                break;
                   4463:        case _O_CREAT | _O_EXCL:
                   4464:        case _O_CREAT | _O_TRUNC | _O_EXCL:
                   4465:                disposition = CREATE_NEW;
                   4466:                break;
                   4467:        case _O_TRUNC:
                   4468:        case _O_TRUNC | _O_EXCL:
                   4469:                disposition = TRUNCATE_EXISTING;
                   4470:                break;
                   4471:        case _O_CREAT | _O_TRUNC:
                   4472:                disposition = CREATE_ALWAYS;
                   4473:                break;
                   4474:        }
1.1.1.14  root     4475:        
1.1.1.13  root     4476:        HANDLE h = CreateFile(filename, GENERIC_READ | FILE_WRITE_ATTRIBUTES,
                   4477:                FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
                   4478:                FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14  root     4479:        if(h == INVALID_HANDLE_VALUE) {
1.1.1.13  root     4480:                // FILE_WRITE_ATTRIBUTES may not be granted for standard users.
                   4481:                // Retry without FILE_WRITE_ATTRIBUTES.
                   4482:                h = CreateFile(filename, GENERIC_READ,
                   4483:                        FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
                   4484:                        FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14  root     4485:                if(h == INVALID_HANDLE_VALUE) {
1.1.1.13  root     4486:                        errno = msdos_maperr(GetLastError());
                   4487:                        return -1;
                   4488:                }
                   4489:        }
1.1.1.14  root     4490:        
1.1.1.13  root     4491:        int fd = _open_osfhandle((intptr_t) h, oflag);
1.1.1.14  root     4492:        if(fd == -1) {
1.1.1.13  root     4493:                CloseHandle(h);
                   4494:        }
                   4495:        return fd;
                   4496: }
                   4497: 
1.1.1.37! root     4498: void msdos_file_handler_open(int fd, const char *path, int atty, int mode, UINT16 info, UINT16 psp_seg, int sio_port, int lpt_port)
1.1       root     4499: {
                   4500:        static int id = 0;
                   4501:        char full[MAX_PATH], *name;
                   4502:        
                   4503:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
                   4504:                strcpy(file_handler[fd].path, full);
                   4505:        } else {
                   4506:                strcpy(file_handler[fd].path, path);
                   4507:        }
1.1.1.14  root     4508:        // isatty makes no distinction between CON & NUL
                   4509:        // GetFileSize fails on CON, succeeds on NUL
                   4510:        if(atty && (info != 0x80d3 || GetFileSize((HANDLE)_get_osfhandle(fd), NULL) == 0)) {
                   4511:                info = 0x8084;
                   4512:                atty = 0;
                   4513:        } else if(!atty && info == 0x80d3) {
                   4514:                info = msdos_drive_number(".");
                   4515:        }
1.1       root     4516:        file_handler[fd].valid = 1;
                   4517:        file_handler[fd].id = id++;     // dummy id for int 21h ax=71a6h
1.1.1.37! root     4518:        file_handler[fd].atty = (sio_port == 0 && lpt_port == 0) ? atty : 0;
1.1       root     4519:        file_handler[fd].mode = mode;
                   4520:        file_handler[fd].info = info;
                   4521:        file_handler[fd].psp = psp_seg;
1.1.1.37! root     4522:        file_handler[fd].sio_port = sio_port;
        !          4523:        file_handler[fd].lpt_port = lpt_port;
1.1.1.21  root     4524:        
                   4525:        // init system file table
                   4526:        if(fd < 20) {
                   4527:                UINT8 *sft = mem + SFT_TOP + 6 + 0x3b * fd;
                   4528:                
                   4529:                memset(sft, 0, 0x3b);
                   4530:                
                   4531:                *(UINT16 *)(sft + 0x00) = 1;
                   4532:                *(UINT16 *)(sft + 0x02) = file_handler[fd].mode;
                   4533:                *(UINT8  *)(sft + 0x04) = GetFileAttributes(file_handler[fd].path) & 0xff;
                   4534:                *(UINT16 *)(sft + 0x05) = file_handler[fd].info & 0xff;
                   4535:                
                   4536:                if(!(file_handler[fd].info & 0x80)) {
                   4537:                        *(UINT16 *)(sft + 0x07) = sizeof(dpb_t) * (file_handler[fd].info & 0x1f);
                   4538:                        *(UINT16 *)(sft + 0x09) = DPB_TOP >> 4;
                   4539:                        
                   4540:                        FILETIME time, local;
                   4541:                        HANDLE hHandle;
                   4542:                        WORD dos_date = 0, dos_time = 0;
                   4543:                        DWORD file_size = 0;
                   4544:                        if((hHandle = (HANDLE)_get_osfhandle(fd)) != INVALID_HANDLE_VALUE) {
                   4545:                                if(GetFileTime(hHandle, NULL, NULL, &time)) {
                   4546:                                        FileTimeToLocalFileTime(&time, &local);
                   4547:                                        FileTimeToDosDateTime(&local, &dos_date, &dos_time);
                   4548:                                }
                   4549:                                file_size = GetFileSize(hHandle, NULL);
                   4550:                        }
                   4551:                        *(UINT16 *)(sft + 0x0d) = dos_time;
                   4552:                        *(UINT16 *)(sft + 0x0f) = dos_date;
                   4553:                        *(UINT32 *)(sft + 0x11) = file_size;
                   4554:                }
                   4555:                
                   4556:                char fname[MAX_PATH] = {0}, ext[MAX_PATH] = {0};
                   4557:                _splitpath(file_handler[fd].path, NULL, NULL, fname, ext);
                   4558:                my_strupr(fname);
                   4559:                my_strupr(ext);
                   4560:                memset(sft + 0x20, 0x20, 11);
                   4561:                memcpy(sft + 0x20, fname, min(strlen(fname), 8));
                   4562:                memcpy(sft + 0x28, ext + 1, min(strlen(ext + 1), 3));
                   4563:                
                   4564:                *(UINT16 *)(sft + 0x31) = psp_seg;
                   4565:        }
1.1       root     4566: }
                   4567: 
1.1.1.37! root     4568: void msdos_file_handler_open(int fd, const char *path, int atty, int mode, UINT16 info, UINT16 psp_seg)
        !          4569: {
        !          4570:         msdos_file_handler_open(fd, path, atty, mode, info, psp_seg, 0, 0);
        !          4571: }
        !          4572: 
1.1       root     4573: void msdos_file_handler_dup(int dst, int src, UINT16 psp_seg)
                   4574: {
                   4575:        strcpy(file_handler[dst].path, file_handler[src].path);
                   4576:        file_handler[dst].valid = 1;
                   4577:        file_handler[dst].id = file_handler[src].id;
                   4578:        file_handler[dst].atty = file_handler[src].atty;
                   4579:        file_handler[dst].mode = file_handler[src].mode;
                   4580:        file_handler[dst].info = file_handler[src].info;
                   4581:        file_handler[dst].psp = psp_seg;
1.1.1.37! root     4582:        file_handler[dst].sio_port = file_handler[src].sio_port;
        !          4583:        file_handler[dst].lpt_port = file_handler[src].lpt_port;
1.1       root     4584: }
                   4585: 
1.1.1.20  root     4586: void msdos_file_handler_close(int fd)
1.1       root     4587: {
                   4588:        file_handler[fd].valid = 0;
1.1.1.21  root     4589:        
                   4590:        if(fd < 20) {
                   4591:                memset(mem + SFT_TOP + 6 + 0x3b * fd, 0, 0x3b);
                   4592:        }
1.1       root     4593: }
                   4594: 
1.1.1.14  root     4595: inline int msdos_file_attribute_create(UINT16 new_attr)
1.1       root     4596: {
1.1.1.14  root     4597:        return(new_attr & (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN |
                   4598:                           FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE |
                   4599:                           FILE_ATTRIBUTE_DIRECTORY));
1.1       root     4600: }
                   4601: 
                   4602: // find file
                   4603: 
                   4604: int msdos_find_file_check_attribute(int attribute, int allowed_mask, int required_mask)
                   4605: {
                   4606:        if((allowed_mask & 0x08) && !(attribute & FILE_ATTRIBUTE_DIRECTORY)) {
                   4607:                return(0);      // search directory only !!!
                   4608:        } else if(!(allowed_mask & 0x02) && (attribute & FILE_ATTRIBUTE_HIDDEN)) {
                   4609:                return(0);
                   4610:        } else if(!(allowed_mask & 0x04) && (attribute & FILE_ATTRIBUTE_SYSTEM)) {
                   4611:                return(0);
                   4612:        } else if(!(allowed_mask & 0x10) && (attribute & FILE_ATTRIBUTE_DIRECTORY)) {
                   4613:                return(0);
                   4614:        } else if((attribute & required_mask) != required_mask) {
                   4615:                return(0);
                   4616:        } else {
                   4617:                return(1);
                   4618:        }
                   4619: }
                   4620: 
1.1.1.13  root     4621: int msdos_find_file_has_8dot3name(WIN32_FIND_DATA *fd)
                   4622: {
1.1.1.14  root     4623:        if(fd->cAlternateFileName[0]) {
1.1.1.13  root     4624:                return 1;
                   4625:        }
                   4626:        size_t len = strlen(fd->cFileName);
1.1.1.14  root     4627:        if(len > 12) {
1.1.1.13  root     4628:                return 0;
                   4629:        }
                   4630:        const char *ext = strrchr(fd->cFileName, '.');
1.1.1.14  root     4631:        if((ext ? ext - fd->cFileName : len) > 8) {
1.1.1.13  root     4632:                return 0;
                   4633:        }
                   4634:        return 1;
                   4635: }
                   4636: 
1.1       root     4637: void msdos_find_file_conv_local_time(WIN32_FIND_DATA *fd)
                   4638: {
                   4639:        FILETIME local;
                   4640:        
                   4641:        FileTimeToLocalFileTime(&fd->ftCreationTime, &local);
                   4642:        fd->ftCreationTime.dwLowDateTime = local.dwLowDateTime;
                   4643:        fd->ftCreationTime.dwHighDateTime = local.dwHighDateTime;
                   4644:        
                   4645:        FileTimeToLocalFileTime(&fd->ftLastAccessTime, &local);
                   4646:        fd->ftLastAccessTime.dwLowDateTime = local.dwLowDateTime;
                   4647:        fd->ftLastAccessTime.dwHighDateTime = local.dwHighDateTime;
                   4648:        
                   4649:        FileTimeToLocalFileTime(&fd->ftLastWriteTime, &local);
                   4650:        fd->ftLastWriteTime.dwLowDateTime = local.dwLowDateTime;
                   4651:        fd->ftLastWriteTime.dwHighDateTime = local.dwHighDateTime;
                   4652: }
                   4653: 
                   4654: // i/o
                   4655: 
                   4656: void msdos_stdio_reopen()
                   4657: {
                   4658:        if(!file_handler[0].valid) {
                   4659:                _dup2(DUP_STDIN, 0);
                   4660:                msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   4661:        }
                   4662:        if(!file_handler[1].valid) {
                   4663:                _dup2(DUP_STDOUT, 1);
                   4664:                msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   4665:        }
                   4666:        if(!file_handler[2].valid) {
                   4667:                _dup2(DUP_STDERR, 2);
                   4668:                msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
                   4669:        }
1.1.1.21  root     4670:        if(!file_handler[3].valid) {
                   4671:                _dup2(DUP_STDAUX, 3);
                   4672:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
                   4673:        }
                   4674:        if(!file_handler[4].valid) {
                   4675:                _dup2(DUP_STDPRN, 4);
1.1.1.37! root     4676:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0, 0, 1); // LPT1
1.1.1.21  root     4677:        }
                   4678:        for(int i = 0; i < 5; i++) {
                   4679:                if(msdos_psp_get_file_table(i, current_psp) == 0xff) {
                   4680:                        msdos_psp_set_file_table(i, i, current_psp);
                   4681:                }
                   4682:        }
1.1       root     4683: }
                   4684: 
1.1.1.37! root     4685: int msdos_read(int fd, void *buffer, unsigned int count)
        !          4686: {
        !          4687:        if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].sio_port >= 1 && file_handler[fd].sio_port <= 4) {
        !          4688:                // read from serial port
        !          4689:                int read = 0;
        !          4690:                if(sio_port_number[file_handler[fd].sio_port - 1] != 0) {
        !          4691:                        UINT8 *buf = (UINT8 *)buffer;
        !          4692:                        UINT8 selector = sio_read(file_handler[fd].sio_port - 1, 3);
        !          4693:                        sio_write(file_handler[fd].sio_port - 1, 3, selector & ~0x80);
        !          4694:                        for(unsigned int i = 0; i < count; i++) {
        !          4695:                                if(!(sio_read(file_handler[fd].sio_port - 1, 5) & 0x01)) {
        !          4696:                                        // data is nto ready
        !          4697:                                        break;
        !          4698:                                }
        !          4699:                                buf[i] = sio_read(file_handler[fd].sio_port - 1, 0);
        !          4700:                                read++;
        !          4701:                        }
        !          4702:                        sio_write(file_handler[fd].sio_port - 1, 3, selector);
        !          4703:                }
        !          4704:                return(read);
        !          4705:        }
        !          4706:        return(_read(fd, buffer, count));
        !          4707: }
        !          4708: 
1.1       root     4709: int msdos_kbhit()
                   4710: {
                   4711:        msdos_stdio_reopen();
                   4712:        
1.1.1.20  root     4713:        process_t *process = msdos_process_info_get(current_psp);
                   4714:        int fd = msdos_psp_get_file_table(0, current_psp);
                   4715:        
                   4716:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     4717:                // stdin is redirected to file
1.1.1.20  root     4718:                return(eof(fd) == 0);
1.1       root     4719:        }
                   4720:        
                   4721:        // check keyboard status
1.1.1.35  root     4722:        if(key_recv != 0) {
1.1       root     4723:                return(1);
                   4724:        }
1.1.1.35  root     4725:        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   4726: #ifdef USE_SERVICE_THREAD
                   4727:                EnterCriticalSection(&key_buf_crit_sect);
                   4728: #endif
                   4729:                bool empty = key_buf_char->empty();
                   4730: #ifdef USE_SERVICE_THREAD
                   4731:                LeaveCriticalSection(&key_buf_crit_sect);
                   4732: #endif
                   4733:                if(!empty) return(1);
                   4734:        }
                   4735:        return(_kbhit());
1.1       root     4736: }
                   4737: 
                   4738: int msdos_getch_ex(int echo)
                   4739: {
                   4740:        static char prev = 0;
                   4741:        
                   4742:        msdos_stdio_reopen();
                   4743:        
1.1.1.20  root     4744:        process_t *process = msdos_process_info_get(current_psp);
                   4745:        int fd = msdos_psp_get_file_table(0, current_psp);
                   4746:        
                   4747:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     4748:                // stdin is redirected to file
                   4749: retry:
                   4750:                char data;
1.1.1.37! root     4751:                if(msdos_read(fd, &data, 1) == 1) {
1.1       root     4752:                        char tmp = data;
                   4753:                        if(data == 0x0a) {
                   4754:                                if(prev == 0x0d) {
                   4755:                                        goto retry; // CRLF -> skip LF
                   4756:                                } else {
                   4757:                                        data = 0x0d; // LF only -> CR
                   4758:                                }
                   4759:                        }
                   4760:                        prev = tmp;
                   4761:                        return(data);
                   4762:                }
                   4763:                return(EOF);
                   4764:        }
                   4765:        
                   4766:        // input from console
1.1.1.5   root     4767:        int key_char, key_scan;
1.1.1.33  root     4768:        if(key_recv != 0) {
1.1.1.5   root     4769:                key_char = (key_code >> 0) & 0xff;
                   4770:                key_scan = (key_code >> 8) & 0xff;
                   4771:                key_code >>= 16;
1.1.1.33  root     4772:                key_recv >>= 16;
1.1.1.5   root     4773:        } else {
1.1.1.35  root     4774:                while(key_buf_char != NULL && key_buf_scan != NULL && !m_halted) {
                   4775:                        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   4776: #ifdef USE_SERVICE_THREAD
                   4777:                                EnterCriticalSection(&key_buf_crit_sect);
                   4778: #endif
                   4779:                                bool empty = key_buf_char->empty();
                   4780: #ifdef USE_SERVICE_THREAD
                   4781:                                LeaveCriticalSection(&key_buf_crit_sect);
                   4782: #endif
                   4783:                                if(!empty) break;
                   4784:                        }
1.1.1.23  root     4785:                        if(!(fd < process->max_files && file_handler[fd].valid && file_handler[fd].atty && file_mode[file_handler[fd].mode].in)) {
                   4786:                                // NOTE: stdin is redirected to stderr when we do "type (file) | more" on freedos's command.com
                   4787:                                if(_kbhit()) {
1.1.1.32  root     4788:                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     4789: #ifdef USE_SERVICE_THREAD
                   4790:                                                EnterCriticalSection(&key_buf_crit_sect);
                   4791: #endif
1.1.1.32  root     4792:                                                key_buf_char->write(_getch());
1.1.1.35  root     4793:                                                key_buf_scan->write(0x00);
                   4794: #ifdef USE_SERVICE_THREAD
                   4795:                                                LeaveCriticalSection(&key_buf_crit_sect);
                   4796: #endif
1.1.1.32  root     4797:                                        }
1.1.1.23  root     4798:                                } else {
                   4799:                                        Sleep(10);
                   4800:                                }
                   4801:                        } else {
                   4802:                                if(!update_key_buffer()) {
                   4803:                                        Sleep(10);
                   4804:                                }
1.1.1.14  root     4805:                        }
                   4806:                }
                   4807:                if(m_halted) {
1.1.1.33  root     4808:                        // insert CR to terminate input loops
1.1.1.14  root     4809:                        key_char = 0x0d;
                   4810:                        key_scan = 0;
1.1.1.32  root     4811:                } else if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     4812: #ifdef USE_SERVICE_THREAD
                   4813:                        EnterCriticalSection(&key_buf_crit_sect);
                   4814: #endif
1.1.1.14  root     4815:                        key_char = key_buf_char->read();
                   4816:                        key_scan = key_buf_scan->read();
1.1.1.35  root     4817: #ifdef USE_SERVICE_THREAD
                   4818:                        LeaveCriticalSection(&key_buf_crit_sect);
                   4819: #endif
1.1.1.5   root     4820:                }
1.1       root     4821:        }
                   4822:        if(echo && key_char) {
                   4823:                msdos_putch(key_char);
                   4824:        }
                   4825:        return key_char ? key_char : (key_scan != 0xe0) ? key_scan : 0;
                   4826: }
                   4827: 
                   4828: inline int msdos_getch()
                   4829: {
                   4830:        return(msdos_getch_ex(0));
                   4831: }
                   4832: 
                   4833: inline int msdos_getche()
                   4834: {
                   4835:        return(msdos_getch_ex(1));
                   4836: }
                   4837: 
                   4838: int msdos_write(int fd, const void *buffer, unsigned int count)
                   4839: {
1.1.1.37! root     4840:        if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].sio_port >= 1 && file_handler[fd].sio_port <= 4) {
        !          4841:                // write to serial port
        !          4842:                if(sio_port_number[file_handler[fd].sio_port - 1] != 0) {
        !          4843:                        UINT8 *buf = (UINT8 *)buffer;
        !          4844:                        UINT8 selector = sio_read(file_handler[fd].sio_port - 1, 3);
        !          4845:                        sio_write(file_handler[fd].sio_port - 1, 3, selector & ~0x80);
        !          4846:                        for(unsigned int i = 0; i < count; i++) {
        !          4847:                                sio_write(file_handler[fd].sio_port - 1, 0, buf[i]);
        !          4848:                        }
        !          4849:                        sio_write(file_handler[fd].sio_port - 1, 3, selector);
        !          4850:                }
        !          4851:                return(count);
        !          4852:        } else if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].lpt_port >= 1 && file_handler[fd].lpt_port <= 3) {
        !          4853:                // write to printer port
        !          4854:                UINT8 *buf = (UINT8 *)buffer;
        !          4855:                for(unsigned int i = 0; i < count; i++) {
        !          4856: //                     printer_out(file_handler[fd].lpt_port - 1, buf[i]);
        !          4857:                        pcbios_printer_out(file_handler[fd].lpt_port - 1, buf[i]);
        !          4858:                }
        !          4859:                return(count);
        !          4860:        } else if(fd == 1 && file_handler[1].valid && !file_handler[1].atty) {
1.1       root     4861:                // CR+LF -> LF
1.1.1.37! root     4862:                static int is_cr = 0;
1.1       root     4863:                UINT8 *buf = (UINT8 *)buffer;
                   4864:                for(unsigned int i = 0; i < count; i++) {
                   4865:                        UINT8 data = buf[i];
                   4866:                        if(is_cr) {
                   4867:                                if(data != 0x0a) {
                   4868:                                        UINT8 tmp = 0x0d;
                   4869:                                        _write(1, &tmp, 1);
                   4870:                                }
                   4871:                                _write(1, &data, 1);
                   4872:                                is_cr = 0;
                   4873:                        } else if(data == 0x0d) {
                   4874:                                is_cr = 1;
                   4875:                        } else {
                   4876:                                _write(1, &data, 1);
                   4877:                        }
                   4878:                }
                   4879:                return(count);
                   4880:        }
1.1.1.14  root     4881:        vram_flush();
1.1       root     4882:        return(_write(fd, buffer, count));
                   4883: }
                   4884: 
                   4885: void msdos_putch(UINT8 data)
1.1.1.35  root     4886: #ifdef USE_SERVICE_THREAD
                   4887: {
                   4888:        EnterCriticalSection(&putch_crit_sect);
                   4889:        msdos_putch_tmp(data);
                   4890:        LeaveCriticalSection(&putch_crit_sect);
                   4891: }
                   4892: void msdos_putch_tmp(UINT8 data)
                   4893: #endif
1.1       root     4894: {
1.1.1.34  root     4895:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   4896:        SMALL_RECT rect;
                   4897:        COORD co;
1.1       root     4898:        static int p = 0;
                   4899:        static int is_kanji = 0;
                   4900:        static int is_esc = 0;
                   4901:        static int stored_x;
                   4902:        static int stored_y;
                   4903:        static WORD stored_a;
1.1.1.20  root     4904:        static char tmp[64], out[64];
1.1       root     4905:        
                   4906:        msdos_stdio_reopen();
                   4907:        
1.1.1.20  root     4908:        process_t *process = msdos_process_info_get(current_psp);
                   4909:        int fd = msdos_psp_get_file_table(1, current_psp);
                   4910:        
                   4911:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     4912:                // stdout is redirected to file
1.1.1.20  root     4913:                msdos_write(fd, &data, 1);
1.1       root     4914:                return;
                   4915:        }
1.1.1.23  root     4916:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     4917:        
                   4918:        // output to console
                   4919:        tmp[p++] = data;
                   4920:        
1.1.1.14  root     4921:        vram_flush();
                   4922:        
1.1       root     4923:        if(is_kanji) {
                   4924:                // kanji character
                   4925:                is_kanji = 0;
                   4926:        } else if(is_esc) {
                   4927:                // escape sequense
                   4928:                if((tmp[1] == ')' || tmp[1] == '(') && p == 3) {
                   4929:                        p = is_esc = 0;
                   4930:                } else if(tmp[1] == '=' && p == 4) {
                   4931:                        co.X = tmp[3] - 0x20;
1.1.1.14  root     4932:                        co.Y = tmp[2] - 0x20 + scr_top;
1.1       root     4933:                        SetConsoleCursorPosition(hStdout, co);
                   4934:                        mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14  root     4935:                        mem[0x451 + mem[0x462] * 2] = co.Y - scr_top;
1.1       root     4936:                        cursor_moved = false;
                   4937:                        p = is_esc = 0;
                   4938:                } else if((data >= 'a' && data <= 'z') || (data >= 'A' && data <= 'Z') || data == '*') {
                   4939:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   4940:                        co.X = csbi.dwCursorPosition.X;
                   4941:                        co.Y = csbi.dwCursorPosition.Y;
                   4942:                        WORD wAttributes = csbi.wAttributes;
                   4943:                        
                   4944:                        if(tmp[1] == 'D') {
                   4945:                                co.Y++;
                   4946:                        } else if(tmp[1] == 'E') {
                   4947:                                co.X = 0;
                   4948:                                co.Y++;
                   4949:                        } else if(tmp[1] == 'M') {
                   4950:                                co.Y--;
                   4951:                        } else if(tmp[1] == '*') {
1.1.1.14  root     4952:                                SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   4953:                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   4954:                                co.X = 0;
                   4955:                                co.Y = csbi.srWindow.Top;
1.1       root     4956:                        } else if(tmp[1] == '[') {
                   4957:                                int param[256], params = 0;
                   4958:                                memset(param, 0, sizeof(param));
                   4959:                                for(int i = 2; i < p; i++) {
                   4960:                                        if(tmp[i] >= '0' && tmp[i] <= '9') {
                   4961:                                                param[params] *= 10;
                   4962:                                                param[params] += tmp[i] - '0';
                   4963:                                        } else {
                   4964:                                                params++;
                   4965:                                        }
                   4966:                                }
                   4967:                                if(data == 'A') {
1.1.1.14  root     4968:                                        co.Y -= (params == 0) ? 1 : param[0];
1.1       root     4969:                                } else if(data == 'B') {
1.1.1.14  root     4970:                                        co.Y += (params == 0) ? 1 : param[0];
1.1       root     4971:                                } else if(data == 'C') {
1.1.1.14  root     4972:                                        co.X += (params == 0) ? 1 : param[0];
1.1       root     4973:                                } else if(data == 'D') {
1.1.1.14  root     4974:                                        co.X -= (params == 0) ? 1 : param[0];
1.1       root     4975:                                } else if(data == 'H' || data == 'f') {
1.1.1.14  root     4976:                                        co.X = (param[1] == 0 ? 1 : param[1]) - 1;
                   4977:                                        co.Y = (param[0] == 0 ? 1 : param[0]) - 1 + csbi.srWindow.Top;
1.1       root     4978:                                } else if(data == 'J') {
1.1.1.14  root     4979:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     4980:                                        if(param[0] == 0) {
                   4981:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     4982:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   4983:                                                if(co.Y < csbi.srWindow.Bottom) {
                   4984:                                                        SET_RECT(rect, 0, co.Y + 1, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   4985:                                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     4986:                                                }
                   4987:                                        } else if(param[0] == 1) {
1.1.1.14  root     4988:                                                if(co.Y > csbi.srWindow.Top) {
                   4989:                                                        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, co.Y - 1);
                   4990:                                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     4991:                                                }
                   4992:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.14  root     4993:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     4994:                                        } else if(param[0] == 2) {
1.1.1.14  root     4995:                                                SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   4996:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     4997:                                                co.X = co.Y = 0;
                   4998:                                        }
                   4999:                                } else if(data == 'K') {
1.1.1.14  root     5000:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5001:                                        if(param[0] == 0) {
                   5002:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     5003:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5004:                                        } else if(param[0] == 1) {
                   5005:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.14  root     5006:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5007:                                        } else if(param[0] == 2) {
                   5008:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     5009:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5010:                                        }
                   5011:                                } else if(data == 'L') {
1.1.1.14  root     5012:                                        if(params == 0) {
                   5013:                                                param[0] = 1;
1.1       root     5014:                                        }
1.1.1.14  root     5015:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5016:                                        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5017:                                        SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5018:                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5019:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5020:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y + param[0] - 1);
1.1.1.14  root     5021:                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5022:                                        co.X = 0;
                   5023:                                } else if(data == 'M') {
1.1.1.14  root     5024:                                        if(params == 0) {
                   5025:                                                param[0] = 1;
                   5026:                                        }
                   5027:                                        if(co.Y + param[0] > csbi.srWindow.Bottom) {
                   5028:                                                clear_scr_buffer(csbi.wAttributes);
                   5029:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5030:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5031:                                        } else {
1.1.1.14  root     5032:                                                SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5033:                                                ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5034:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5035:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5036:                                                clear_scr_buffer(csbi.wAttributes);
1.1       root     5037:                                        }
                   5038:                                        co.X = 0;
                   5039:                                } else if(data == 'h') {
                   5040:                                        if(tmp[2] == '>' && tmp[3] == '5') {
                   5041:                                                CONSOLE_CURSOR_INFO cur;
                   5042:                                                GetConsoleCursorInfo(hStdout, &cur);
                   5043:                                                if(cur.bVisible) {
                   5044:                                                        cur.bVisible = FALSE;
1.1.1.14  root     5045: //                                                     SetConsoleCursorInfo(hStdout, &cur);
1.1       root     5046:                                                }
                   5047:                                        }
                   5048:                                } else if(data == 'l') {
                   5049:                                        if(tmp[2] == '>' && tmp[3] == '5') {
                   5050:                                                CONSOLE_CURSOR_INFO cur;
                   5051:                                                GetConsoleCursorInfo(hStdout, &cur);
                   5052:                                                if(!cur.bVisible) {
                   5053:                                                        cur.bVisible = TRUE;
1.1.1.14  root     5054: //                                                     SetConsoleCursorInfo(hStdout, &cur);
1.1       root     5055:                                                }
                   5056:                                        }
                   5057:                                } else if(data == 'm') {
                   5058:                                        wAttributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
                   5059:                                        int reverse = 0, hidden = 0;
                   5060:                                        for(int i = 0; i < params; i++) {
                   5061:                                                if(param[i] == 1) {
                   5062:                                                        wAttributes |= FOREGROUND_INTENSITY;
                   5063:                                                } else if(param[i] == 4) {
                   5064:                                                        wAttributes |= COMMON_LVB_UNDERSCORE;
                   5065:                                                } else if(param[i] == 7) {
                   5066:                                                        reverse = 1;
                   5067:                                                } else if(param[i] == 8 || param[i] == 16) {
                   5068:                                                        hidden = 1;
                   5069:                                                } else if((param[i] >= 17 && param[i] <= 23) || (param[i] >= 30 && param[i] <= 37)) {
                   5070:                                                        wAttributes &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
                   5071:                                                        if(param[i] >= 17 && param[i] <= 23) {
                   5072:                                                                param[i] -= 16;
                   5073:                                                        } else {
                   5074:                                                                param[i] -= 30;
                   5075:                                                        }
                   5076:                                                        if(param[i] & 1) {
                   5077:                                                                wAttributes |= FOREGROUND_RED;
                   5078:                                                        }
                   5079:                                                        if(param[i] & 2) {
                   5080:                                                                wAttributes |= FOREGROUND_GREEN;
                   5081:                                                        }
                   5082:                                                        if(param[i] & 4) {
                   5083:                                                                wAttributes |= FOREGROUND_BLUE;
                   5084:                                                        }
                   5085:                                                } else if(param[i] >= 40 && param[i] <= 47) {
                   5086:                                                        wAttributes &= ~(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
                   5087:                                                        if((param[i] - 40) & 1) {
                   5088:                                                                wAttributes |= BACKGROUND_RED;
                   5089:                                                        }
                   5090:                                                        if((param[i] - 40) & 2) {
                   5091:                                                                wAttributes |= BACKGROUND_GREEN;
                   5092:                                                        }
                   5093:                                                        if((param[i] - 40) & 4) {
                   5094:                                                                wAttributes |= BACKGROUND_BLUE;
                   5095:                                                        }
                   5096:                                                }
                   5097:                                        }
                   5098:                                        if(reverse) {
                   5099:                                                wAttributes &= ~0xff;
                   5100:                                                wAttributes |= BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
                   5101:                                        }
                   5102:                                        if(hidden) {
                   5103:                                                wAttributes &= ~0x0f;
                   5104:                                                wAttributes |= (wAttributes >> 4) & 0x0f;
                   5105:                                        }
                   5106:                                } else if(data == 'n') {
                   5107:                                        if(param[0] == 6) {
                   5108:                                                char tmp[16];
                   5109:                                                sprintf(tmp, "\x1b[%d;%dR", co.Y + 1, co.X + 1);
                   5110:                                                int len = strlen(tmp);
1.1.1.32  root     5111:                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     5112: #ifdef USE_SERVICE_THREAD
                   5113:                                                        EnterCriticalSection(&key_buf_crit_sect);
                   5114: #endif
1.1.1.32  root     5115:                                                        for(int i = 0; i < len; i++) {
                   5116:                                                                key_buf_char->write(tmp[i]);
                   5117:                                                                key_buf_scan->write(0x00);
                   5118:                                                        }
1.1.1.35  root     5119: #ifdef USE_SERVICE_THREAD
                   5120:                                                        LeaveCriticalSection(&key_buf_crit_sect);
                   5121: #endif
1.1       root     5122:                                                }
                   5123:                                        }
                   5124:                                } else if(data == 's') {
                   5125:                                        stored_x = co.X;
                   5126:                                        stored_y = co.Y;
                   5127:                                        stored_a = wAttributes;
                   5128:                                } else if(data == 'u') {
                   5129:                                        co.X = stored_x;
                   5130:                                        co.Y = stored_y;
                   5131:                                        wAttributes = stored_a;
                   5132:                                }
                   5133:                        }
                   5134:                        if(co.X < 0) {
                   5135:                                co.X = 0;
                   5136:                        } else if(co.X >= csbi.dwSize.X) {
                   5137:                                co.X = csbi.dwSize.X - 1;
                   5138:                        }
1.1.1.14  root     5139:                        if(co.Y < csbi.srWindow.Top) {
                   5140:                                co.Y = csbi.srWindow.Top;
                   5141:                        } else if(co.Y > csbi.srWindow.Bottom) {
                   5142:                                co.Y = csbi.srWindow.Bottom;
1.1       root     5143:                        }
                   5144:                        if(co.X != csbi.dwCursorPosition.X || co.Y != csbi.dwCursorPosition.Y) {
                   5145:                                SetConsoleCursorPosition(hStdout, co);
                   5146:                                mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14  root     5147:                                mem[0x451 + mem[0x462] * 2] = co.Y - csbi.srWindow.Top;
1.1       root     5148:                                cursor_moved = false;
                   5149:                        }
                   5150:                        if(wAttributes != csbi.wAttributes) {
                   5151:                                SetConsoleTextAttribute(hStdout, wAttributes);
                   5152:                        }
                   5153:                        p = is_esc = 0;
                   5154:                }
                   5155:                return;
                   5156:        } else {
                   5157:                if(msdos_lead_byte_check(data)) {
                   5158:                        is_kanji = 1;
                   5159:                        return;
                   5160:                } else if(data == 0x1b) {
                   5161:                        is_esc = 1;
                   5162:                        return;
                   5163:                }
                   5164:        }
1.1.1.20  root     5165:        
                   5166:        DWORD q = 0, num;
                   5167:        is_kanji = 0;
                   5168:        for(int i = 0; i < p; i++) {
                   5169:                UINT8 c = tmp[i];
                   5170:                if(is_kanji) {
                   5171:                        is_kanji = 0;
                   5172:                } else if(msdos_lead_byte_check(data)) {
                   5173:                        is_kanji = 1;
                   5174:                } else if(msdos_ctrl_code_check(data)) {
                   5175:                        out[q++] = '^';
                   5176:                        c += 'A' - 1;
                   5177:                }
                   5178:                out[q++] = c;
                   5179:        }
1.1.1.34  root     5180:        if(q == 1 && out[0] == 0x08) {
                   5181:                // back space
                   5182:                GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5183:                if(csbi.dwCursorPosition.X > 0) {
                   5184:                        co.X = csbi.dwCursorPosition.X - 1;
                   5185:                        co.Y = csbi.dwCursorPosition.Y;
                   5186:                        SetConsoleCursorPosition(hStdout, co);
                   5187:                } else if(csbi.dwCursorPosition.Y > 0) {
                   5188:                        co.X = csbi.dwSize.X - 1;
                   5189:                        co.Y = csbi.dwCursorPosition.Y - 1;
                   5190:                        SetConsoleCursorPosition(hStdout, co);
                   5191:                } else {
                   5192:                        WriteConsole(hStdout, out, q, &num, NULL); // to make sure
                   5193:                }
                   5194:        } else {
                   5195:                WriteConsole(hStdout, out, q, &num, NULL);
                   5196:        }
1.1       root     5197:        p = 0;
1.1.1.14  root     5198:        
1.1.1.15  root     5199:        if(!restore_console_on_exit) {
                   5200:                GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5201:                scr_top = csbi.srWindow.Top;
                   5202:        }
1.1       root     5203:        cursor_moved = true;
                   5204: }
                   5205: 
                   5206: int msdos_aux_in()
                   5207: {
1.1.1.21  root     5208:        msdos_stdio_reopen();
                   5209:        
1.1.1.20  root     5210:        process_t *process = msdos_process_info_get(current_psp);
                   5211:        int fd = msdos_psp_get_file_table(3, current_psp);
                   5212:        
                   5213:        if(fd < process->max_files && file_handler[fd].valid && !eof(fd)) {
1.1       root     5214:                char data = 0;
1.1.1.37! root     5215:                msdos_read(fd, &data, 1);
1.1       root     5216:                return(data);
                   5217:        } else {
                   5218:                return(EOF);
                   5219:        }
                   5220: }
                   5221: 
                   5222: void msdos_aux_out(char data)
                   5223: {
1.1.1.21  root     5224:        msdos_stdio_reopen();
                   5225:        
1.1.1.20  root     5226:        process_t *process = msdos_process_info_get(current_psp);
                   5227:        int fd = msdos_psp_get_file_table(3, current_psp);
                   5228:        
                   5229:        if(fd < process->max_files && file_handler[fd].valid) {
                   5230:                msdos_write(fd, &data, 1);
1.1       root     5231:        }
                   5232: }
                   5233: 
                   5234: void msdos_prn_out(char data)
                   5235: {
1.1.1.21  root     5236:        msdos_stdio_reopen();
                   5237:        
1.1.1.20  root     5238:        process_t *process = msdos_process_info_get(current_psp);
                   5239:        int fd = msdos_psp_get_file_table(4, current_psp);
                   5240:        
                   5241:        if(fd < process->max_files && file_handler[fd].valid) {
                   5242:                msdos_write(fd, &data, 1);
1.1       root     5243:        }
                   5244: }
                   5245: 
                   5246: // memory control
                   5247: 
1.1.1.19  root     5248: mcb_t *msdos_mcb_create(int mcb_seg, UINT8 mz, UINT16 psp, int paragraphs)
1.1       root     5249: {
                   5250:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5251:        
                   5252:        mcb->mz = mz;
                   5253:        mcb->psp = psp;
1.1.1.30  root     5254:        mcb->paragraphs = paragraphs;
1.1       root     5255:        return(mcb);
                   5256: }
                   5257: 
                   5258: void msdos_mcb_check(mcb_t *mcb)
                   5259: {
                   5260:        if(!(mcb->mz == 'M' || mcb->mz == 'Z')) {
1.1.1.28  root     5261:                #if 0
                   5262:                        // shutdown now !!!
                   5263:                        fatalerror("broken memory control block\n");
                   5264:                #else
                   5265:                        // return error code and continue
                   5266:                        throw(0x07); // broken memory control block
                   5267:                #endif
1.1       root     5268:        }
                   5269: }
                   5270: 
                   5271: int msdos_mem_split(int seg, int paragraphs)
                   5272: {
                   5273:        int mcb_seg = seg - 1;
                   5274:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5275:        msdos_mcb_check(mcb);
                   5276:        
1.1.1.30  root     5277:        if(mcb->paragraphs > paragraphs) {
1.1       root     5278:                int new_seg = mcb_seg + 1 + paragraphs;
1.1.1.30  root     5279:                int new_paragraphs = mcb->paragraphs - paragraphs - 1;
1.1       root     5280:                
                   5281:                msdos_mcb_create(new_seg, mcb->mz, 0, new_paragraphs);
                   5282:                mcb->mz = 'M';
1.1.1.30  root     5283:                mcb->paragraphs = paragraphs;
1.1       root     5284:                return(0);
                   5285:        }
                   5286:        return(-1);
                   5287: }
                   5288: 
                   5289: void msdos_mem_merge(int seg)
                   5290: {
                   5291:        int mcb_seg = seg - 1;
                   5292:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5293:        msdos_mcb_check(mcb);
                   5294:        
                   5295:        while(1) {
                   5296:                if(mcb->mz == 'Z') {
                   5297:                        break;
                   5298:                }
1.1.1.30  root     5299:                int next_seg = mcb_seg + 1 + mcb->paragraphs;
1.1       root     5300:                mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5301:                msdos_mcb_check(next_mcb);
                   5302:                
                   5303:                if(next_mcb->psp != 0) {
                   5304:                        break;
                   5305:                }
                   5306:                mcb->mz = next_mcb->mz;
1.1.1.30  root     5307:                mcb->paragraphs = mcb->paragraphs + 1 + next_mcb->paragraphs;
1.1       root     5308:        }
                   5309: }
                   5310: 
1.1.1.8   root     5311: int msdos_mem_alloc(int mcb_seg, int paragraphs, int new_process)
1.1       root     5312: {
                   5313:        while(1) {
                   5314:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     5315:                bool last_block;
1.1       root     5316:                
1.1.1.14  root     5317:                if(mcb->psp == 0) {
                   5318:                        msdos_mem_merge(mcb_seg + 1);
                   5319:                } else {
                   5320:                        msdos_mcb_check(mcb);
                   5321:                }
1.1.1.33  root     5322:                if(!(last_block = (mcb->mz == 'Z'))) {
                   5323:                        // check if the next is dummy mcb to link to umb
                   5324:                        int next_seg = mcb_seg + 1 + mcb->paragraphs;
                   5325:                        mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5326:                        last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
                   5327:                }
                   5328:                if(!(new_process && !last_block)) {
1.1.1.30  root     5329:                        if(mcb->psp == 0 && mcb->paragraphs >= paragraphs) {
1.1       root     5330:                                msdos_mem_split(mcb_seg + 1, paragraphs);
                   5331:                                mcb->psp = current_psp;
                   5332:                                return(mcb_seg + 1);
                   5333:                        }
                   5334:                }
                   5335:                if(mcb->mz == 'Z') {
                   5336:                        break;
                   5337:                }
1.1.1.30  root     5338:                mcb_seg += 1 + mcb->paragraphs;
1.1       root     5339:        }
                   5340:        return(-1);
                   5341: }
                   5342: 
                   5343: int msdos_mem_realloc(int seg, int paragraphs, int *max_paragraphs)
                   5344: {
                   5345:        int mcb_seg = seg - 1;
                   5346:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5347:        msdos_mcb_check(mcb);
1.1.1.30  root     5348:        int current_paragraphs = mcb->paragraphs;
1.1       root     5349:        
                   5350:        msdos_mem_merge(seg);
1.1.1.30  root     5351:        if(paragraphs > mcb->paragraphs) {
1.1.1.14  root     5352:                if(max_paragraphs) {
1.1.1.30  root     5353:                        *max_paragraphs = mcb->paragraphs;
1.1.1.14  root     5354:                }
1.1       root     5355:                msdos_mem_split(seg, current_paragraphs);
                   5356:                return(-1);
                   5357:        }
                   5358:        msdos_mem_split(seg, paragraphs);
                   5359:        return(0);
                   5360: }
                   5361: 
                   5362: void msdos_mem_free(int seg)
                   5363: {
                   5364:        int mcb_seg = seg - 1;
                   5365:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5366:        msdos_mcb_check(mcb);
                   5367:        
                   5368:        mcb->psp = 0;
                   5369:        msdos_mem_merge(seg);
                   5370: }
                   5371: 
1.1.1.8   root     5372: int msdos_mem_get_free(int mcb_seg, int new_process)
1.1       root     5373: {
                   5374:        int max_paragraphs = 0;
                   5375:        
                   5376:        while(1) {
                   5377:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     5378:                bool last_block;
                   5379:                
1.1       root     5380:                msdos_mcb_check(mcb);
                   5381:                
1.1.1.33  root     5382:                if(!(last_block = (mcb->mz == 'Z'))) {
                   5383:                        // check if the next is dummy mcb to link to umb
                   5384:                        int next_seg = mcb_seg + 1 + mcb->paragraphs;
                   5385:                        mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5386:                        last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
                   5387:                }
                   5388:                if(!(new_process && !last_block)) {
1.1.1.30  root     5389:                        if(mcb->psp == 0 && mcb->paragraphs > max_paragraphs) {
                   5390:                                max_paragraphs = mcb->paragraphs;
1.1       root     5391:                        }
                   5392:                }
                   5393:                if(mcb->mz == 'Z') {
                   5394:                        break;
                   5395:                }
1.1.1.30  root     5396:                mcb_seg += 1 + mcb->paragraphs;
1.1       root     5397:        }
1.1.1.14  root     5398:        return(max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs);
1.1       root     5399: }
                   5400: 
1.1.1.8   root     5401: int msdos_mem_get_last_mcb(int mcb_seg, UINT16 psp)
                   5402: {
                   5403:        int last_seg = -1;
                   5404:        
                   5405:        while(1) {
                   5406:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5407:                msdos_mcb_check(mcb);
                   5408:                
1.1.1.14  root     5409:                if(mcb->psp == psp) {
1.1.1.8   root     5410:                        last_seg = mcb_seg;
                   5411:                }
1.1.1.14  root     5412:                if(mcb->mz == 'Z') {
                   5413:                        break;
                   5414:                }
1.1.1.30  root     5415:                mcb_seg += 1 + mcb->paragraphs;
1.1.1.8   root     5416:        }
                   5417:        return(last_seg);
                   5418: }
                   5419: 
1.1.1.19  root     5420: int msdos_mem_get_umb_linked()
                   5421: {
1.1.1.33  root     5422:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   5423:        msdos_mcb_check(mcb);
1.1.1.19  root     5424:        
1.1.1.33  root     5425:        if(mcb->mz == 'M') {
                   5426:                return(-1);
1.1.1.19  root     5427:        }
                   5428:        return(0);
                   5429: }
                   5430: 
1.1.1.33  root     5431: void msdos_mem_link_umb()
1.1.1.19  root     5432: {
1.1.1.33  root     5433:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   5434:        msdos_mcb_check(mcb);
1.1.1.19  root     5435:        
1.1.1.33  root     5436:        mcb->mz = 'M';
                   5437:        mcb->paragraphs = (UMB_TOP >> 4) - (MEMORY_END >> 4);
1.1.1.19  root     5438: }
                   5439: 
1.1.1.33  root     5440: void msdos_mem_unlink_umb()
1.1.1.19  root     5441: {
1.1.1.33  root     5442:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   5443:        msdos_mcb_check(mcb);
1.1.1.19  root     5444:        
1.1.1.33  root     5445:        mcb->mz = 'Z';
                   5446:        mcb->paragraphs = 0;
1.1.1.19  root     5447: }
                   5448: 
1.1.1.29  root     5449: #ifdef SUPPORT_HMA
                   5450: 
                   5451: hma_mcb_t *msdos_hma_mcb_create(int offset, int owner, int size, int next)
                   5452: {
                   5453:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5454:        
                   5455:        mcb->ms[0] = 'M';
                   5456:        mcb->ms[1] = 'S';
                   5457:        mcb->owner = owner;
                   5458:        mcb->size = size;
                   5459:        mcb->next = next;
                   5460:        return(mcb);
                   5461: }
                   5462: 
                   5463: bool msdos_is_hma_mcb_valid(hma_mcb_t *mcb)
                   5464: {
                   5465:        return(mcb->ms[0] == 'M' && mcb->ms[1] == 'S');
                   5466: }
                   5467: 
                   5468: int msdos_hma_mem_split(int offset, int size)
                   5469: {
                   5470:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5471:        
                   5472:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5473:                return(-1);
                   5474:        }
                   5475:        if(mcb->size >= size + 0x10) {
                   5476:                int new_offset = offset + 0x10 + size;
                   5477:                int new_size = mcb->size - 0x10 - size;
                   5478:                
                   5479:                msdos_hma_mcb_create(new_offset, 0, new_size, mcb->next);
                   5480:                mcb->size = size;
                   5481:                mcb->next = new_offset;
                   5482:                return(0);
                   5483:        }
                   5484:        return(-1);
                   5485: }
                   5486: 
                   5487: void msdos_hma_mem_merge(int offset)
                   5488: {
                   5489:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5490:        
                   5491:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5492:                return;
                   5493:        }
                   5494:        while(1) {
                   5495:                if(mcb->next == 0) {
                   5496:                        break;
                   5497:                }
                   5498:                hma_mcb_t *next_mcb = (hma_mcb_t *)(mem + 0xffff0 + mcb->next);
                   5499:                
                   5500:                if(!msdos_is_hma_mcb_valid(next_mcb)) {
                   5501:                        return;
                   5502:                }
                   5503:                if(next_mcb->owner != 0) {
                   5504:                        break;
                   5505:                }
                   5506:                mcb->size += 0x10 + next_mcb->size;
                   5507:                mcb->next = next_mcb->next;
                   5508:        }
                   5509: }
                   5510: 
                   5511: int msdos_hma_mem_alloc(int size, UINT16 owner)
                   5512: {
                   5513:        int offset = 0x10; // first mcb in HMA
                   5514:        
                   5515:        while(1) {
                   5516:                hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5517:                
                   5518:                if(!msdos_is_hma_mcb_valid(mcb)) {
                   5519:                        return(-1);
                   5520:                }
                   5521:                if(mcb->owner == 0) {
                   5522:                        msdos_hma_mem_merge(offset);
                   5523:                }
                   5524:                if(mcb->owner == 0 && mcb->size >= size) {
                   5525:                        msdos_hma_mem_split(offset, size);
                   5526:                        mcb->owner = owner;
                   5527:                        return(offset);
                   5528:                }
                   5529:                if(mcb->next == 0) {
                   5530:                        break;
                   5531:                }
                   5532:                offset = mcb->next;
                   5533:        }
                   5534:        return(-1);
                   5535: }
                   5536: 
                   5537: int msdos_hma_mem_realloc(int offset, int size)
                   5538: {
                   5539:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5540:        
                   5541:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5542:                return(-1);
                   5543:        }
                   5544:        if(mcb->size < size) {
                   5545:                return(-1);
                   5546:        }
                   5547:        msdos_hma_mem_split(offset, size);
                   5548:        return(0);
                   5549: }
                   5550: 
                   5551: void msdos_hma_mem_free(int offset)
                   5552: {
                   5553:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5554:        
                   5555:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5556:                return;
                   5557:        }
                   5558:        mcb->owner = 0;
                   5559:        msdos_hma_mem_merge(offset);
                   5560: }
                   5561: 
                   5562: int msdos_hma_mem_get_free(int *available_offset)
                   5563: {
                   5564:        int offset = 0x10; // first mcb in HMA
                   5565:        int size = 0;
                   5566:        
                   5567:        while(1) {
                   5568:                hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5569:                
                   5570:                if(!msdos_is_hma_mcb_valid(mcb)) {
                   5571:                        return(0);
                   5572:                }
                   5573:                if(mcb->owner == 0 && size < mcb->size) {
                   5574:                        if(available_offset != NULL) {
                   5575:                                *available_offset = offset;
                   5576:                        }
                   5577:                        size = mcb->size;
                   5578:                }
                   5579:                if(mcb->next == 0) {
                   5580:                        break;
                   5581:                }
                   5582:                offset = mcb->next;
                   5583:        }
                   5584:        return(size);
                   5585: }
                   5586: 
                   5587: #endif
                   5588: 
1.1       root     5589: // environment
                   5590: 
                   5591: void msdos_env_set_argv(int env_seg, char *argv)
                   5592: {
                   5593:        char *dst = (char *)(mem + (env_seg << 4));
                   5594:        
                   5595:        while(1) {
                   5596:                if(dst[0] == 0) {
                   5597:                        break;
                   5598:                }
                   5599:                dst += strlen(dst) + 1;
                   5600:        }
                   5601:        *dst++ = 0; // end of environment
                   5602:        *dst++ = 1; // top of argv[0]
                   5603:        *dst++ = 0;
                   5604:        memcpy(dst, argv, strlen(argv));
                   5605:        dst += strlen(argv);
                   5606:        *dst++ = 0;
                   5607:        *dst++ = 0;
                   5608: }
                   5609: 
                   5610: char *msdos_env_get_argv(int env_seg)
                   5611: {
                   5612:        static char env[ENV_SIZE];
                   5613:        char *src = env;
                   5614:        
                   5615:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   5616:        while(1) {
                   5617:                if(src[0] == 0) {
                   5618:                        if(src[1] == 1) {
                   5619:                                return(src + 3);
                   5620:                        }
                   5621:                        break;
                   5622:                }
                   5623:                src += strlen(src) + 1;
                   5624:        }
                   5625:        return(NULL);
                   5626: }
                   5627: 
                   5628: char *msdos_env_get(int env_seg, const char *name)
                   5629: {
                   5630:        static char env[ENV_SIZE];
                   5631:        char *src = env;
                   5632:        
                   5633:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   5634:        while(1) {
                   5635:                if(src[0] == 0) {
                   5636:                        break;
                   5637:                }
                   5638:                int len = strlen(src);
                   5639:                char *n = my_strtok(src, "=");
                   5640:                char *v = src + strlen(n) + 1;
                   5641:                
                   5642:                if(_stricmp(name, n) == 0) {
                   5643:                        return(v);
                   5644:                }
                   5645:                src += len + 1;
                   5646:        }
                   5647:        return(NULL);
                   5648: }
                   5649: 
                   5650: void msdos_env_set(int env_seg, char *name, char *value)
                   5651: {
                   5652:        char env[ENV_SIZE];
                   5653:        char *src = env;
                   5654:        char *dst = (char *)(mem + (env_seg << 4));
                   5655:        char *argv = msdos_env_get_argv(env_seg);
                   5656:        int done = 0;
                   5657:        
                   5658:        memcpy(src, dst, ENV_SIZE);
                   5659:        memset(dst, 0, ENV_SIZE);
                   5660:        while(1) {
                   5661:                if(src[0] == 0) {
                   5662:                        break;
                   5663:                }
                   5664:                int len = strlen(src);
                   5665:                char *n = my_strtok(src, "=");
                   5666:                char *v = src + strlen(n) + 1;
                   5667:                char tmp[1024];
                   5668:                
                   5669:                if(_stricmp(name, n) == 0) {
                   5670:                        sprintf(tmp, "%s=%s", n, value);
                   5671:                        done = 1;
                   5672:                } else {
                   5673:                        sprintf(tmp, "%s=%s", n, v);
                   5674:                }
                   5675:                memcpy(dst, tmp, strlen(tmp));
                   5676:                dst += strlen(tmp) + 1;
                   5677:                src += len + 1;
                   5678:        }
                   5679:        if(!done) {
                   5680:                char tmp[1024];
                   5681:                
                   5682:                sprintf(tmp, "%s=%s", name, value);
                   5683:                memcpy(dst, tmp, strlen(tmp));
                   5684:                dst += strlen(tmp) + 1;
                   5685:        }
                   5686:        if(argv) {
                   5687:                *dst++ = 0; // end of environment
                   5688:                *dst++ = 1; // top of argv[0]
                   5689:                *dst++ = 0;
                   5690:                memcpy(dst, argv, strlen(argv));
                   5691:                dst += strlen(argv);
                   5692:                *dst++ = 0;
                   5693:                *dst++ = 0;
                   5694:        }
                   5695: }
                   5696: 
                   5697: // process
                   5698: 
1.1.1.8   root     5699: psp_t *msdos_psp_create(int psp_seg, UINT16 mcb_seg, UINT16 parent_psp, UINT16 env_seg)
1.1       root     5700: {
                   5701:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   5702:        
                   5703:        memset(psp, 0, PSP_SIZE);
                   5704:        psp->exit[0] = 0xcd;
                   5705:        psp->exit[1] = 0x20;
1.1.1.8   root     5706:        psp->first_mcb = mcb_seg;
1.1       root     5707:        psp->far_call = 0xea;
                   5708:        psp->cpm_entry.w.l = 0xfff1;    // int 21h, retf
                   5709:        psp->cpm_entry.w.h = 0xf000;
                   5710:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   5711:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   5712:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   5713:        psp->parent_psp = parent_psp;
1.1.1.20  root     5714:        if(parent_psp == (UINT16)-1) {
                   5715:                for(int i = 0; i < 20; i++) {
                   5716:                        if(file_handler[i].valid) {
                   5717:                                psp->file_table[i] = i;
                   5718:                        } else {
                   5719:                                psp->file_table[i] = 0xff;
                   5720:                        }
1.1       root     5721:                }
1.1.1.20  root     5722:        } else {
                   5723:                memcpy(psp->file_table, ((psp_t *)(mem + (parent_psp << 4)))->file_table, 20);
1.1       root     5724:        }
                   5725:        psp->env_seg = env_seg;
                   5726:        psp->stack.w.l = REG16(SP);
1.1.1.3   root     5727:        psp->stack.w.h = SREG(SS);
1.1.1.14  root     5728:        psp->file_table_size = 20;
                   5729:        psp->file_table_ptr.w.l = 0x18;
                   5730:        psp->file_table_ptr.w.h = psp_seg;
1.1       root     5731:        psp->service[0] = 0xcd;
                   5732:        psp->service[1] = 0x21;
                   5733:        psp->service[2] = 0xcb;
                   5734:        return(psp);
                   5735: }
                   5736: 
1.1.1.20  root     5737: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg)
                   5738: {
                   5739:        if(psp_seg && fd < 20) {
                   5740:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   5741:                psp->file_table[fd] = value;
                   5742:        }
                   5743: }
                   5744: 
                   5745: int msdos_psp_get_file_table(int fd, int psp_seg)
                   5746: {
                   5747:        if(psp_seg && fd < 20) {
                   5748:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   5749:                fd = psp->file_table[fd];
                   5750:        }
                   5751:        return fd;
                   5752: }
                   5753: 
1.1       root     5754: int msdos_process_exec(char *cmd, param_block_t *param, UINT8 al)
                   5755: {
                   5756:        // load command file
                   5757:        int fd = -1;
                   5758:        int dos_command = 0;
1.1.1.24  root     5759:        char command[MAX_PATH], path[MAX_PATH], opt[MAX_PATH], *name = NULL, name_tmp[MAX_PATH];
1.1       root     5760:        
                   5761:        int opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   5762:        int opt_len = mem[opt_ofs];
                   5763:        memset(opt, 0, sizeof(opt));
                   5764:        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   5765:        
1.1.1.14  root     5766:        if(strlen(cmd) >= 5 && _stricmp(&cmd[strlen(cmd) - 4], ".BAT") == 0) {
                   5767:                // this is a batch file, run command.com
                   5768:                char tmp[MAX_PATH];
                   5769:                if(opt_len != 0) {
                   5770:                        sprintf(tmp, "/C %s %s", cmd, opt);
                   5771:                } else {
                   5772:                        sprintf(tmp, "/C %s", cmd);
                   5773:                }
                   5774:                strcpy(opt, tmp);
                   5775:                opt_len = strlen(opt);
                   5776:                mem[opt_ofs] = opt_len;
                   5777:                sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   5778:                strcpy(command, comspec_path);
                   5779:                strcpy(name_tmp, "COMMAND.COM");
                   5780:        } else {
                   5781:                if(_stricmp(cmd, "C:\\COMMAND.COM") == 0) {
                   5782:                        // redirect C:\COMMAND.COM to comspec_path
                   5783:                        strcpy(command, comspec_path);
                   5784:                } else {
                   5785:                        strcpy(command, cmd);
                   5786:                }
1.1.1.24  root     5787:                if(GetFullPathName(command, MAX_PATH, path, &name) == 0) {
                   5788:                        return(-1);
                   5789:                }
1.1.1.14  root     5790:                memset(name_tmp, 0, sizeof(name_tmp));
                   5791:                strcpy(name_tmp, name);
                   5792:                
                   5793:                // check command.com
                   5794:                if(_stricmp(name, "COMMAND.COM") == 0 || _stricmp(name, "COMMAND") == 0) {
                   5795:                        if(opt_len == 0) {
                   5796: //                             process_t *current_process = msdos_process_info_get(current_psp);
                   5797:                                process_t *current_process = NULL;
                   5798:                                for(int i = 0; i < MAX_PROCESS; i++) {
                   5799:                                        if(process[i].psp == current_psp) {
                   5800:                                                current_process = &process[i];
                   5801:                                                break;
                   5802:                                        }
                   5803:                                }
                   5804:                                if(current_process != NULL) {
                   5805:                                        param->cmd_line.dw = current_process->dta.dw;
                   5806:                                        opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   5807:                                        opt_len = mem[opt_ofs];
                   5808:                                        memset(opt, 0, sizeof(opt));
                   5809:                                        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   5810:                                }
                   5811:                        }
                   5812:                        for(int i = 0; i < opt_len; i++) {
                   5813:                                if(opt[i] == ' ') {
                   5814:                                        continue;
                   5815:                                }
                   5816:                                if(opt[i] == '/' && (opt[i + 1] == 'c' || opt[i + 1] == 'C') && opt[i + 2] == ' ') {
                   5817:                                        for(int j = i + 3; j < opt_len; j++) {
                   5818:                                                if(opt[j] == ' ') {
                   5819:                                                        continue;
                   5820:                                                }
                   5821:                                                char *token = my_strtok(opt + j, " ");
                   5822:                                                
                   5823:                                                if(strlen(token) >= 5 && _stricmp(&token[strlen(token) - 4], ".BAT") == 0) {
                   5824:                                                        // this is a batch file, okay to run command.com
                   5825:                                                } else {
                   5826:                                                        // run program directly without command.com
                   5827:                                                        strcpy(command, token);
                   5828:                                                        char tmp[MAX_PATH];
                   5829:                                                        strcpy(tmp, token + strlen(token) + 1);
                   5830:                                                        strcpy(opt, tmp);
                   5831:                                                        opt_len = strlen(opt);
                   5832:                                                        mem[opt_ofs] = opt_len;
                   5833:                                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   5834:                                                        dos_command = 1;
                   5835:                                                }
                   5836:                                                break;
1.1       root     5837:                                        }
                   5838:                                }
1.1.1.14  root     5839:                                break;
1.1       root     5840:                        }
                   5841:                }
                   5842:        }
                   5843:        
                   5844:        // load command file
                   5845:        strcpy(path, command);
                   5846:        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   5847:                sprintf(path, "%s.COM", command);
                   5848:                if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   5849:                        sprintf(path, "%s.EXE", command);
                   5850:                        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
1.1.1.14  root     5851:                                sprintf(path, "%s.BAT", command);
                   5852:                                if(_access(path, 0) == 0) {
                   5853:                                        // this is a batch file, run command.com
                   5854:                                        char tmp[MAX_PATH];
                   5855:                                        if(opt_len != 0) {
                   5856:                                                sprintf(tmp, "/C %s %s", path, opt);
                   5857:                                        } else {
                   5858:                                                sprintf(tmp, "/C %s", path);
                   5859:                                        }
                   5860:                                        strcpy(opt, tmp);
                   5861:                                        opt_len = strlen(opt);
                   5862:                                        mem[opt_ofs] = opt_len;
                   5863:                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   5864:                                        strcpy(path, comspec_path);
                   5865:                                        strcpy(name_tmp, "COMMAND.COM");
                   5866:                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   5867:                                } else {
                   5868:                                        // search path in parent environments
                   5869:                                        psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
                   5870:                                        char *env = msdos_env_get(parent_psp->env_seg, "PATH");
                   5871:                                        if(env != NULL) {
                   5872:                                                char env_path[4096];
                   5873:                                                strcpy(env_path, env);
                   5874:                                                char *token = my_strtok(env_path, ";");
                   5875:                                                
                   5876:                                                while(token != NULL) {
                   5877:                                                        if(strlen(token) != 0) {
                   5878:                                                                sprintf(path, "%s", msdos_combine_path(token, command));
                   5879:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   5880:                                                                        break;
                   5881:                                                                }
                   5882:                                                                sprintf(path, "%s.COM", msdos_combine_path(token, command));
                   5883:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   5884:                                                                        break;
                   5885:                                                                }
                   5886:                                                                sprintf(path, "%s.EXE", msdos_combine_path(token, command));
                   5887:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   5888:                                                                        break;
                   5889:                                                                }
                   5890:                                                                sprintf(path, "%s.BAT", msdos_combine_path(token, command));
                   5891:                                                                if(_access(path, 0) == 0) {
                   5892:                                                                        // this is a batch file, run command.com
                   5893:                                                                        char tmp[MAX_PATH];
                   5894:                                                                        if(opt_len != 0) {
                   5895:                                                                                sprintf(tmp, "/C %s %s", path, opt);
                   5896:                                                                        } else {
                   5897:                                                                                sprintf(tmp, "/C %s", path);
                   5898:                                                                        }
                   5899:                                                                        strcpy(opt, tmp);
                   5900:                                                                        opt_len = strlen(opt);
                   5901:                                                                        mem[opt_ofs] = opt_len;
                   5902:                                                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   5903:                                                                        strcpy(path, comspec_path);
                   5904:                                                                        strcpy(name_tmp, "COMMAND.COM");
                   5905:                                                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   5906:                                                                        break;
                   5907:                                                                }
1.1.1.8   root     5908:                                                        }
1.1.1.14  root     5909:                                                        token = my_strtok(NULL, ";");
1.1       root     5910:                                                }
                   5911:                                        }
                   5912:                                }
                   5913:                        }
                   5914:                }
                   5915:        }
                   5916:        if(fd == -1) {
                   5917:                if(dos_command) {
                   5918:                        // may be dos command
                   5919:                        char tmp[MAX_PATH];
                   5920:                        sprintf(tmp, "%s %s", command, opt);
                   5921:                        system(tmp);
                   5922:                        return(0);
                   5923:                } else {
                   5924:                        return(-1);
                   5925:                }
                   5926:        }
                   5927:        _read(fd, file_buffer, sizeof(file_buffer));
                   5928:        _close(fd);
                   5929:        
                   5930:        // copy environment
1.1.1.29  root     5931:        int umb_linked, env_seg, psp_seg;
1.1       root     5932:        
1.1.1.29  root     5933:        if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   5934:                msdos_mem_unlink_umb();
                   5935:        }
1.1.1.8   root     5936:        if((env_seg = msdos_mem_alloc(first_mcb, ENV_SIZE >> 4, 1)) == -1) {
1.1.1.29  root     5937:                if((env_seg = msdos_mem_alloc(UMB_TOP >> 4, ENV_SIZE >> 4, 1)) == -1) {
                   5938:                        if(umb_linked != 0) {
                   5939:                                msdos_mem_link_umb();
                   5940:                        }
                   5941:                        return(-1);
                   5942:                }
1.1       root     5943:        }
                   5944:        if(param->env_seg == 0) {
                   5945:                psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
                   5946:                memcpy(mem + (env_seg << 4), mem + (parent_psp->env_seg << 4), ENV_SIZE);
                   5947:        } else {
                   5948:                memcpy(mem + (env_seg << 4), mem + (param->env_seg << 4), ENV_SIZE);
                   5949:        }
                   5950:        msdos_env_set_argv(env_seg, msdos_short_full_path(path));
                   5951:        
                   5952:        // check exe header
                   5953:        exe_header_t *header = (exe_header_t *)file_buffer;
1.1.1.8   root     5954:        int paragraphs, free_paragraphs = msdos_mem_get_free(first_mcb, 1);
1.1       root     5955:        UINT16 cs, ss, ip, sp;
                   5956:        
                   5957:        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   5958:                // memory allocation
                   5959:                int header_size = header->header_size * 16;
                   5960:                int load_size = header->pages * 512 - header_size;
                   5961:                if(header_size + load_size < 512) {
                   5962:                        load_size = 512 - header_size;
                   5963:                }
                   5964:                paragraphs = (PSP_SIZE + load_size) >> 4;
                   5965:                if(paragraphs + header->min_alloc > free_paragraphs) {
                   5966:                        msdos_mem_free(env_seg);
                   5967:                        return(-1);
                   5968:                }
                   5969:                paragraphs += header->max_alloc ? header->max_alloc : header->min_alloc;
                   5970:                if(paragraphs > free_paragraphs) {
                   5971:                        paragraphs = free_paragraphs;
                   5972:                }
1.1.1.8   root     5973:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29  root     5974:                        if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
                   5975:                                if(umb_linked != 0) {
                   5976:                                        msdos_mem_link_umb();
                   5977:                                }
                   5978:                                msdos_mem_free(env_seg);
                   5979:                                return(-1);
                   5980:                        }
1.1       root     5981:                }
                   5982:                // relocation
                   5983:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   5984:                for(int i = 0; i < header->relocations; i++) {
                   5985:                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   5986:                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   5987:                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   5988:                }
                   5989:                memcpy(mem + (start_seg << 4), file_buffer + header_size, load_size);
                   5990:                // segments
                   5991:                cs = header->init_cs + start_seg;
                   5992:                ss = header->init_ss + start_seg;
                   5993:                ip = header->init_ip;
                   5994:                sp = header->init_sp - 2; // for symdeb
                   5995:        } else {
                   5996:                // memory allocation
                   5997:                paragraphs = free_paragraphs;
1.1.1.8   root     5998:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29  root     5999:                        if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
                   6000:                                if(umb_linked != 0) {
                   6001:                                        msdos_mem_link_umb();
                   6002:                                }
                   6003:                                msdos_mem_free(env_seg);
                   6004:                                return(-1);
                   6005:                        }
1.1       root     6006:                }
                   6007:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   6008:                memcpy(mem + (start_seg << 4), file_buffer, 0x10000 - PSP_SIZE);
                   6009:                // segments
                   6010:                cs = ss = psp_seg;
                   6011:                ip = 0x100;
                   6012:                sp = 0xfffe;
                   6013:        }
1.1.1.29  root     6014:        if(umb_linked != 0) {
                   6015:                msdos_mem_link_umb();
                   6016:        }
1.1       root     6017:        
                   6018:        // create psp
1.1.1.3   root     6019:        *(UINT16 *)(mem + 4 * 0x22 + 0) = m_eip;
                   6020:        *(UINT16 *)(mem + 4 * 0x22 + 2) = SREG(CS);
1.1       root     6021:        psp_t *psp = msdos_psp_create(psp_seg, psp_seg + paragraphs, current_psp, env_seg);
                   6022:        memcpy(psp->fcb1, mem + (param->fcb1.w.h << 4) + param->fcb1.w.l, sizeof(psp->fcb1));
                   6023:        memcpy(psp->fcb2, mem + (param->fcb2.w.h << 4) + param->fcb2.w.l, sizeof(psp->fcb2));
                   6024:        memcpy(psp->buffer, mem + (param->cmd_line.w.h << 4) + param->cmd_line.w.l, sizeof(psp->buffer));
                   6025:        
                   6026:        mcb_t *mcb_env = (mcb_t *)(mem + ((env_seg - 1) << 4));
                   6027:        mcb_t *mcb_psp = (mcb_t *)(mem + ((psp_seg - 1) << 4));
                   6028:        mcb_psp->psp = mcb_env->psp = psp_seg;
                   6029:        
1.1.1.4   root     6030:        for(int i = 0; i < 8; i++) {
                   6031:                if(name_tmp[i] == '.') {
                   6032:                        mcb_psp->prog_name[i] = '\0';
                   6033:                        break;
                   6034:                } else if(i < 7 && msdos_lead_byte_check(name_tmp[i])) {
                   6035:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6036:                        i++;
                   6037:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6038:                } else if(name_tmp[i] >= 'a' && name_tmp[i] <= 'z') {
                   6039:                        mcb_psp->prog_name[i] = name_tmp[i] - 'a' + 'A';
                   6040:                } else {
                   6041:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6042:                }
                   6043:        }
                   6044:        
1.1       root     6045:        // process info
                   6046:        process_t *process = msdos_process_info_create(psp_seg);
                   6047:        strcpy(process->module_dir, msdos_short_full_dir(path));
1.1.1.33  root     6048: #ifdef USE_DEBUGGER
                   6049:        strcpy(process->module_path, path);
                   6050: #endif
1.1       root     6051:        process->dta.w.l = 0x80;
                   6052:        process->dta.w.h = psp_seg;
                   6053:        process->switchar = '/';
                   6054:        process->max_files = 20;
                   6055:        process->parent_int_10h_feh_called = int_10h_feh_called;
                   6056:        process->parent_int_10h_ffh_called = int_10h_ffh_called;
1.1.1.14  root     6057:        process->parent_ds = SREG(DS);
1.1.1.31  root     6058:        process->parent_es = SREG(ES);
1.1       root     6059:        
                   6060:        current_psp = psp_seg;
1.1.1.23  root     6061:        msdos_sda_update(current_psp);
1.1       root     6062:        
                   6063:        if(al == 0x00) {
                   6064:                int_10h_feh_called = int_10h_ffh_called = false;
                   6065:                
                   6066:                // registers and segments
                   6067:                REG16(AX) = REG16(BX) = 0x00;
                   6068:                REG16(CX) = 0xff;
                   6069:                REG16(DX) = psp_seg;
                   6070:                REG16(SI) = ip;
                   6071:                REG16(DI) = sp;
                   6072:                REG16(SP) = sp;
1.1.1.3   root     6073:                SREG(DS) = SREG(ES) = psp_seg;
                   6074:                SREG(SS) = ss;
                   6075:                i386_load_segment_descriptor(DS);
                   6076:                i386_load_segment_descriptor(ES);
                   6077:                i386_load_segment_descriptor(SS);
1.1       root     6078:                
                   6079:                *(UINT16 *)(mem + (ss << 4) + sp) = 0;
                   6080:                i386_jmp_far(cs, ip);
                   6081:        } else if(al == 0x01) {
                   6082:                // copy ss:sp and cs:ip to param block
                   6083:                param->sp = sp;
                   6084:                param->ss = ss;
                   6085:                param->ip = ip;
                   6086:                param->cs = cs;
1.1.1.31  root     6087:                
                   6088:                // the AX value to be passed to the child program is put on top of the child's stack
                   6089:                *(UINT16 *)(mem + (ss << 4) + sp) = REG16(AX);
1.1       root     6090:        }
                   6091:        return(0);
                   6092: }
                   6093: 
                   6094: void msdos_process_terminate(int psp_seg, int ret, int mem_free)
                   6095: {
                   6096:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6097:        
                   6098:        *(UINT32 *)(mem + 4 * 0x22) = psp->int_22h.dw;
                   6099:        *(UINT32 *)(mem + 4 * 0x23) = psp->int_23h.dw;
                   6100:        *(UINT32 *)(mem + 4 * 0x24) = psp->int_24h.dw;
                   6101:        
1.1.1.3   root     6102:        SREG(SS) = psp->stack.w.h;
                   6103:        i386_load_segment_descriptor(SS);
1.1       root     6104:        REG16(SP) = psp->stack.w.l;
                   6105:        i386_jmp_far(psp->int_22h.w.h, psp->int_22h.w.l);
                   6106:        
1.1.1.28  root     6107: //     process_t *current_process = msdos_process_info_get(psp_seg);
                   6108:        process_t *current_process = NULL;
                   6109:        for(int i = 0; i < MAX_PROCESS; i++) {
                   6110:                if(process[i].psp == psp_seg) {
                   6111:                        current_process = &process[i];
                   6112:                        break;
                   6113:                }
                   6114:        }
                   6115:        if(current_process == NULL) {
                   6116:                throw(0x1f); // general failure
                   6117:        }
                   6118:        int_10h_feh_called = current_process->parent_int_10h_feh_called;
                   6119:        int_10h_ffh_called = current_process->parent_int_10h_ffh_called;
                   6120:        if(current_process->called_by_int2eh) {
                   6121:                REG16(AX) = ret;
                   6122:        }
                   6123:        SREG(DS) = current_process->parent_ds;
1.1.1.31  root     6124:        SREG(ES) = current_process->parent_es;
1.1.1.14  root     6125:        i386_load_segment_descriptor(DS);
1.1.1.31  root     6126:        i386_load_segment_descriptor(ES);
1.1       root     6127:        
                   6128:        if(mem_free) {
1.1.1.8   root     6129:                int mcb_seg;
                   6130:                while((mcb_seg = msdos_mem_get_last_mcb(first_mcb, psp_seg)) != -1) {
                   6131:                        msdos_mem_free(mcb_seg + 1);
                   6132:                }
                   6133:                while((mcb_seg = msdos_mem_get_last_mcb(UMB_TOP >> 4, psp_seg)) != -1) {
                   6134:                        msdos_mem_free(mcb_seg + 1);
                   6135:                }
1.1       root     6136:                
                   6137:                for(int i = 0; i < MAX_FILES; i++) {
                   6138:                        if(file_handler[i].valid && file_handler[i].psp == psp_seg) {
                   6139:                                _close(i);
1.1.1.20  root     6140:                                msdos_file_handler_close(i);
                   6141:                                msdos_psp_set_file_table(i, 0x0ff, psp_seg); // FIXME: consider duplicated file handles
1.1       root     6142:                        }
                   6143:                }
1.1.1.13  root     6144:                msdos_dta_info_free(psp_seg);
1.1       root     6145:        }
1.1.1.14  root     6146:        msdos_stdio_reopen();
1.1       root     6147:        
1.1.1.28  root     6148:        memset(current_process, 0, sizeof(process_t));
1.1       root     6149:        
                   6150:        current_psp = psp->parent_psp;
                   6151:        retval = ret;
1.1.1.23  root     6152:        msdos_sda_update(current_psp);
1.1       root     6153: }
                   6154: 
                   6155: // drive
                   6156: 
                   6157: int msdos_drive_param_block_update(int drive_num, UINT16 *seg, UINT16 *ofs, int force_update)
                   6158: {
                   6159:        *seg = DPB_TOP >> 4;
                   6160:        *ofs = sizeof(dpb_t) * drive_num;
                   6161:        dpb_t *dpb = (dpb_t *)(mem + (*seg << 4) + *ofs);
                   6162:        
                   6163:        if(!force_update && dpb->free_clusters != 0) {
                   6164:                return(dpb->bytes_per_sector ? 1 : 0);
                   6165:        }
                   6166:        memset(dpb, 0, sizeof(dpb_t));
                   6167:        
                   6168:        int res = 0;
                   6169:        char dev[64];
                   6170:        sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   6171:        
1.1.1.17  root     6172:        HANDLE hFile = CreateFile(dev, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1.1       root     6173:        if(hFile != INVALID_HANDLE_VALUE) {
                   6174:                DISK_GEOMETRY geo;
                   6175:                DWORD dwSize;
                   6176:                if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &geo, sizeof(geo), &dwSize, NULL)) {
                   6177:                        dpb->bytes_per_sector = (UINT16)geo.BytesPerSector;
                   6178:                        dpb->highest_sector_num = (UINT8)(geo.SectorsPerTrack - 1);
                   6179:                        dpb->highest_cluster_num = (UINT16)(geo.TracksPerCylinder * geo.Cylinders.QuadPart + 1);
1.1.1.14  root     6180:                        dpb->maximum_cluster_num = (UINT32)(geo.TracksPerCylinder * geo.Cylinders.QuadPart + 1);
1.1       root     6181:                        switch(geo.MediaType) {
                   6182:                        case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   6183:                                dpb->media_type = 0xff;
                   6184:                                break;
                   6185:                        case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   6186:                                dpb->media_type = 0xfe;
                   6187:                                break;
                   6188:                        case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   6189:                                dpb->media_type = 0xfd;
                   6190:                                break;
                   6191:                        case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   6192:                                dpb->media_type = 0xfc;
                   6193:                                break;
                   6194:                        case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   6195:                        case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   6196:                                dpb->media_type = 0xf9;
                   6197:                                break;
                   6198:                        case FixedMedia:        // hard disk
                   6199:                        case RemovableMedia:
1.1.1.19  root     6200:                        case Unknown:
1.1       root     6201:                                dpb->media_type = 0xf8;
                   6202:                                break;
                   6203:                        default:
                   6204:                                dpb->media_type = 0xf0;
                   6205:                                break;
                   6206:                        }
                   6207:                        res = 1;
                   6208:                }
                   6209:                dpb->drive_num = drive_num;
                   6210:                dpb->unit_num = drive_num;
                   6211:                dpb->next_dpb_ofs = *ofs + sizeof(dpb_t);
                   6212:                dpb->next_dpb_seg = *seg;
1.1.1.14  root     6213:                dpb->info_sector = 0xffff;
                   6214:                dpb->backup_boot_sector = 0xffff;
1.1       root     6215:                dpb->free_clusters = 0xffff;
1.1.1.14  root     6216:                dpb->free_search_cluster = 0xffffffff;
1.1       root     6217:                CloseHandle(hFile);
                   6218:        }
                   6219:        return(res);
                   6220: }
                   6221: 
                   6222: // pc bios
                   6223: 
1.1.1.35  root     6224: #ifdef USE_SERVICE_THREAD
                   6225: void start_service_loop(LPTHREAD_START_ROUTINE lpStartAddress)
                   6226: {
                   6227: #if defined(HAS_I386)
                   6228:        if(m_SF != 0) {
                   6229:                m_SF = 0;
                   6230:                mem[0xfffd0 + 0x15] = 0x79;     // jns -4
                   6231:        } else {
                   6232:                m_SF = 1;
                   6233:                mem[0xfffd0 + 0x15] = 0x78;     // js -4
                   6234:        }
                   6235: #else
                   6236:        if(m_SignVal < 0) {
                   6237:                m_SignVal = 0;
                   6238:                mem[0xfffd0 + 0x15] = 0x79;     // jns -4
                   6239:        } else {
                   6240:                m_SignVal = -1;
                   6241:                mem[0xfffd0 + 0x15] = 0x78;     // js -4
                   6242:        }
                   6243: #endif
                   6244:        i386_call_far(0xfffd, 0x0013);
                   6245:        in_service = true;
                   6246:        service_exit = false;
                   6247:        CloseHandle(CreateThread(NULL, 0, lpStartAddress, NULL, 0, NULL));
                   6248: }
                   6249: 
                   6250: void finish_service_loop()
                   6251: {
                   6252:        if(in_service && service_exit) {
                   6253: #if defined(HAS_I386)
                   6254:                if(m_SF != 0) {
                   6255:                        m_SF = 0;
                   6256:                } else {
                   6257:                        m_SF = 1;
                   6258:                }
                   6259: #else
                   6260:                if(m_SignVal < 0) {
                   6261:                        m_SignVal = 0;
                   6262:                } else {
                   6263:                        m_SignVal = -1;
                   6264:                }
                   6265: #endif
                   6266:                in_service = false;
                   6267:        }
                   6268: }
                   6269: #endif
                   6270: 
1.1.1.19  root     6271: UINT32 get_ticks_since_midnight(UINT32 cur_msec)
                   6272: {
                   6273:        static unsigned __int64 start_msec_since_midnight = 0;
                   6274:        static unsigned __int64 start_msec_since_hostboot = 0;
                   6275:        
                   6276:        if(start_msec_since_midnight == 0) {
                   6277:                SYSTEMTIME time;
                   6278:                GetLocalTime(&time);
                   6279:                start_msec_since_midnight = ((time.wHour * 60 + time.wMinute) * 60 + time.wSecond) * 1000 + time.wMilliseconds;
                   6280:                start_msec_since_hostboot = cur_msec;
                   6281:        }
                   6282:        unsigned __int64 msec = (start_msec_since_midnight + cur_msec - start_msec_since_hostboot) % (24 * 60 * 60 * 1000);
                   6283:        unsigned __int64 tick = msec * 0x1800b0 / (24 * 60 * 60 * 1000);
                   6284:        return (UINT32)tick;
                   6285: }
                   6286: 
                   6287: void pcbios_update_daily_timer_counter(UINT32 cur_msec)
                   6288: {
                   6289:        UINT32 prev_tick = *(UINT32 *)(mem + 0x46c);
                   6290:        UINT32 next_tick = get_ticks_since_midnight(cur_msec);
                   6291:        
                   6292:        if(prev_tick > next_tick) {
                   6293:                mem[0x470] = 1;
                   6294:        }
                   6295:        *(UINT32 *)(mem + 0x46c) = next_tick;
                   6296: }
                   6297: 
1.1.1.14  root     6298: inline void pcbios_irq0()
                   6299: {
                   6300:        //++*(UINT32 *)(mem + 0x46c);
1.1.1.19  root     6301:        pcbios_update_daily_timer_counter(timeGetTime());
1.1.1.14  root     6302: }
                   6303: 
1.1.1.16  root     6304: int pcbios_get_text_vram_address(int page)
1.1       root     6305: {
                   6306:        if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     6307:                return TEXT_VRAM_TOP;
1.1       root     6308:        } else {
1.1.1.14  root     6309:                return TEXT_VRAM_TOP + VIDEO_REGEN * (page % vram_pages);
1.1       root     6310:        }
                   6311: }
                   6312: 
1.1.1.16  root     6313: int pcbios_get_shadow_buffer_address(int page)
1.1       root     6314: {
1.1.1.14  root     6315:        if(!int_10h_feh_called) {
1.1.1.16  root     6316:                return pcbios_get_text_vram_address(page);
1.1.1.14  root     6317:        } else if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     6318:                return SHADOW_BUF_TOP;
                   6319:        } else {
1.1.1.14  root     6320:                return SHADOW_BUF_TOP + VIDEO_REGEN * (page % vram_pages);
1.1.1.8   root     6321:        }
                   6322: }
                   6323: 
1.1.1.16  root     6324: int pcbios_get_shadow_buffer_address(int page, int x, int y)
1.1.1.8   root     6325: {
1.1.1.16  root     6326:        return pcbios_get_shadow_buffer_address(page) + (x + y * scr_width) * 2;
1.1       root     6327: }
                   6328: 
1.1.1.16  root     6329: void pcbios_set_console_size(int width, int height, bool clr_screen)
1.1       root     6330: {
1.1.1.14  root     6331:        // clear the existing screen, not just the new one
                   6332:        int clr_height = max(height, scr_height);
                   6333:        
1.1.1.16  root     6334:        if(scr_width != width || scr_height != height) {
                   6335:                change_console_size(width, height);
1.1.1.14  root     6336:        }
                   6337:        mem[0x462] = 0;
                   6338:        *(UINT16 *)(mem + 0x44e) = 0;
                   6339:        
1.1.1.16  root     6340:        text_vram_top_address = pcbios_get_text_vram_address(0);
                   6341:        text_vram_end_address = text_vram_top_address + width * height * 2;
                   6342:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(0);
                   6343:        shadow_buffer_end_address = shadow_buffer_top_address + width * height * 2;
1.1       root     6344:        
1.1.1.23  root     6345:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.16  root     6346:        if(clr_screen) {
1.1.1.14  root     6347:                for(int ofs = shadow_buffer_top_address; ofs < shadow_buffer_end_address;) {
                   6348:                        mem[ofs++] = 0x20;
                   6349:                        mem[ofs++] = 0x07;
                   6350:                }
                   6351:                
1.1.1.35  root     6352: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6353:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     6354: #endif
1.1.1.14  root     6355:                for(int y = 0; y < clr_height; y++) {
                   6356:                        for(int x = 0; x < scr_width; x++) {
                   6357:                                SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6358:                                SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1       root     6359:                        }
                   6360:                }
                   6361:                SMALL_RECT rect;
1.1.1.14  root     6362:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + clr_height - 1);
                   6363:                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   6364:                vram_length_char = vram_last_length_char = 0;
                   6365:                vram_length_attr = vram_last_length_attr = 0;
1.1.1.35  root     6366: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6367:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     6368: #endif
1.1       root     6369:        }
1.1.1.14  root     6370:        COORD co;
                   6371:        co.X = 0;
                   6372:        co.Y = scr_top;
                   6373:        SetConsoleCursorPosition(hStdout, co);
                   6374:        cursor_moved = true;
                   6375:        SetConsoleTextAttribute(hStdout, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
1.1       root     6376: }
                   6377: 
1.1.1.36  root     6378: void pcbios_update_cursor_position()
                   6379: {
                   6380:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   6381:        GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
                   6382:        if(!restore_console_on_exit) {
                   6383:                scr_top = csbi.srWindow.Top;
                   6384:        }
                   6385:        mem[0x450 + mem[0x462] * 2] = csbi.dwCursorPosition.X;
                   6386:        mem[0x451 + mem[0x462] * 2] = csbi.dwCursorPosition.Y - scr_top;
                   6387: }
                   6388: 
1.1.1.16  root     6389: inline void pcbios_int_10h_00h()
                   6390: {
                   6391:        switch(REG8(AL) & 0x7f) {
                   6392:        case 0x70: // v-text mode
                   6393:        case 0x71: // extended cga v-text mode
                   6394:                pcbios_set_console_size(scr_width, scr_height, !(REG8(AL) & 0x80));
                   6395:                break;
                   6396:        default:
                   6397:                pcbios_set_console_size(80, 25, !(REG8(AL) & 0x80));
                   6398:                break;
                   6399:        }
                   6400:        if(REG8(AL) & 0x80) {
                   6401:                mem[0x487] |= 0x80;
                   6402:        } else {
                   6403:                mem[0x487] &= ~0x80;
                   6404:        }
                   6405:        mem[0x449] = REG8(AL) & 0x7f;
                   6406: }
                   6407: 
1.1       root     6408: inline void pcbios_int_10h_01h()
                   6409: {
1.1.1.13  root     6410:        mem[0x460] = REG8(CL);
                   6411:        mem[0x461] = REG8(CH);
1.1.1.14  root     6412:        
1.1.1.23  root     6413:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6414:        CONSOLE_CURSOR_INFO ci;
                   6415:        GetConsoleCursorInfo(hStdout, &ci);
                   6416: //     ci.bVisible = !(REG8(CH) & 0x60) && REG8(CH) <= REG8(CL);
                   6417: //     if(ci.bVisible) {
                   6418:                int lines = max(8, REG8(CL) + 1);
                   6419:                ci.dwSize = (REG8(CL) - REG8(CH) + 1) * 100 / lines;
                   6420: //     }
                   6421:        SetConsoleCursorInfo(hStdout, &ci);
1.1       root     6422: }
                   6423: 
                   6424: inline void pcbios_int_10h_02h()
                   6425: {
1.1.1.14  root     6426:        // continuously setting the cursor effectively stops it blinking
                   6427:        if(mem[0x462] == REG8(BH) && (REG8(DL) != mem[0x450 + REG8(BH) * 2] || REG8(DH) != mem[0x451 + REG8(BH) * 2])) {
1.1       root     6428:                COORD co;
                   6429:                co.X = REG8(DL);
1.1.1.14  root     6430:                co.Y = REG8(DH) + scr_top;
                   6431:                
                   6432:                // some programs hide the cursor by moving it off screen
                   6433:                static bool hidden = false;
1.1.1.23  root     6434:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6435:                CONSOLE_CURSOR_INFO ci;
                   6436:                GetConsoleCursorInfo(hStdout, &ci);
                   6437:                
                   6438:                if(REG8(DH) >= scr_height || !SetConsoleCursorPosition(hStdout, co)) {
                   6439:                        if(ci.bVisible) {
                   6440:                                ci.bVisible = FALSE;
                   6441: //                             SetConsoleCursorInfo(hStdout, &ci);
                   6442:                                hidden = true;
                   6443:                        }
                   6444:                } else if(hidden) {
                   6445:                        if(!ci.bVisible) {
                   6446:                                ci.bVisible = TRUE;
                   6447: //                             SetConsoleCursorInfo(hStdout, &ci);
                   6448:                        }
                   6449:                        hidden = false;
                   6450:                }
1.1       root     6451:        }
1.1.1.14  root     6452:        mem[0x450 + (REG8(BH) % vram_pages) * 2] = REG8(DL);
                   6453:        mem[0x451 + (REG8(BH) % vram_pages) * 2] = REG8(DH);
1.1       root     6454: }
                   6455: 
                   6456: inline void pcbios_int_10h_03h()
                   6457: {
1.1.1.14  root     6458:        REG8(DL) = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   6459:        REG8(DH) = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     6460:        REG8(CL) = mem[0x460];
                   6461:        REG8(CH) = mem[0x461];
                   6462: }
                   6463: 
                   6464: inline void pcbios_int_10h_05h()
                   6465: {
1.1.1.14  root     6466:        if(REG8(AL) >= vram_pages) {
                   6467:                return;
                   6468:        }
                   6469:        if(mem[0x462] != REG8(AL)) {
                   6470:                vram_flush();
                   6471:                
1.1.1.23  root     6472:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6473:                SMALL_RECT rect;
1.1.1.14  root     6474:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   6475:                ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     6476:                
1.1.1.16  root     6477:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(mem[0x462]); y < scr_height; y++) {
1.1.1.14  root     6478:                        for(int x = 0; x < scr_width; x++) {
                   6479:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   6480:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     6481:                        }
                   6482:                }
1.1.1.16  root     6483:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(REG8(AL)); y < scr_height; y++) {
1.1.1.14  root     6484:                        for(int x = 0; x < scr_width; x++) {
                   6485:                                SCR_BUF(y,x).Char.AsciiChar = mem[ofs++];
                   6486:                                SCR_BUF(y,x).Attributes = mem[ofs++];
1.1       root     6487:                        }
                   6488:                }
1.1.1.14  root     6489:                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     6490:                
                   6491:                COORD co;
1.1.1.14  root     6492:                co.X = mem[0x450 + REG8(AL) * 2];
                   6493:                co.Y = mem[0x451 + REG8(AL) * 2] + scr_top;
                   6494:                if(co.Y < scr_top + scr_height) {
                   6495:                        SetConsoleCursorPosition(hStdout, co);
                   6496:                }
1.1       root     6497:        }
1.1.1.14  root     6498:        mem[0x462] = REG8(AL);
                   6499:        *(UINT16 *)(mem + 0x44e) = REG8(AL) * VIDEO_REGEN;
                   6500:        int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.16  root     6501:        text_vram_top_address = pcbios_get_text_vram_address(mem[0x462]);
1.1.1.14  root     6502:        text_vram_end_address = text_vram_top_address + regen;
1.1.1.16  root     6503:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(mem[0x462]);
1.1.1.14  root     6504:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
1.1       root     6505: }
                   6506: 
                   6507: inline void pcbios_int_10h_06h()
                   6508: {
1.1.1.14  root     6509:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   6510:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   6511:                return;
                   6512:        }
                   6513:        vram_flush();
                   6514:        
1.1.1.23  root     6515:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6516:        SMALL_RECT rect;
1.1.1.14  root     6517:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   6518:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   6519:        
                   6520:        int right = min(REG8(DL), scr_width - 1);
                   6521:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     6522:        
                   6523:        if(REG8(AL) == 0) {
1.1.1.14  root     6524:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     6525:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     6526:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6527:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     6528:                        }
                   6529:                }
                   6530:        } else {
1.1.1.14  root     6531:                for(int y = REG8(CH), y2 = min(REG8(CH) + REG8(AL), bottom + 1); y <= bottom; y++, y2++) {
1.1.1.16  root     6532:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     6533:                                if(y2 <= bottom) {
                   6534:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     6535:                                } else {
1.1.1.14  root     6536:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6537:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     6538:                                }
1.1.1.14  root     6539:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   6540:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     6541:                        }
                   6542:                }
                   6543:        }
1.1.1.14  root     6544:        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     6545: }
                   6546: 
                   6547: inline void pcbios_int_10h_07h()
                   6548: {
1.1.1.14  root     6549:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   6550:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   6551:                return;
                   6552:        }
                   6553:        vram_flush();
                   6554:        
1.1.1.23  root     6555:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6556:        SMALL_RECT rect;
1.1.1.14  root     6557:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   6558:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   6559:        
                   6560:        int right = min(REG8(DL), scr_width - 1);
                   6561:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     6562:        
                   6563:        if(REG8(AL) == 0) {
1.1.1.14  root     6564:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     6565:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     6566:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6567:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     6568:                        }
                   6569:                }
                   6570:        } else {
1.1.1.14  root     6571:                for(int y = bottom, y2 = max(REG8(CH) - 1, bottom - REG8(AL)); y >= REG8(CH); y--, y2--) {
1.1.1.16  root     6572:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     6573:                                if(y2 >= REG8(CH)) {
                   6574:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     6575:                                } else {
1.1.1.14  root     6576:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6577:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     6578:                                }
1.1.1.14  root     6579:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   6580:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     6581:                        }
                   6582:                }
                   6583:        }
1.1.1.14  root     6584:        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     6585: }
                   6586: 
                   6587: inline void pcbios_int_10h_08h()
                   6588: {
                   6589:        COORD co;
                   6590:        DWORD num;
                   6591:        
1.1.1.14  root     6592:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   6593:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     6594:        
                   6595:        if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     6596:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6597:                co.Y += scr_top;
                   6598:                vram_flush();
1.1       root     6599:                ReadConsoleOutputCharacter(hStdout, scr_char, 1, co, &num);
                   6600:                ReadConsoleOutputAttribute(hStdout, scr_attr, 1, co, &num);
                   6601:                REG8(AL) = scr_char[0];
                   6602:                REG8(AH) = scr_attr[0];
                   6603:        } else {
1.1.1.16  root     6604:                REG16(AX) = *(UINT16 *)(mem + pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y));
1.1       root     6605:        }
                   6606: }
                   6607: 
                   6608: inline void pcbios_int_10h_09h()
                   6609: {
                   6610:        COORD co;
                   6611:        
1.1.1.14  root     6612:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   6613:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   6614:        
1.1.1.16  root     6615:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   6616:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     6617:        
                   6618:        if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     6619: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6620:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     6621: #endif
1.1.1.16  root     6622:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     6623:                while(dest < end) {
                   6624:                        write_text_vram_char(dest - vram, REG8(AL));
                   6625:                        mem[dest++] = REG8(AL);
                   6626:                        write_text_vram_attr(dest - vram, REG8(BL));
                   6627:                        mem[dest++] = REG8(BL);
1.1       root     6628:                }
1.1.1.35  root     6629: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6630:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     6631: #endif
1.1       root     6632:        } else {
1.1.1.14  root     6633:                while(dest < end) {
1.1       root     6634:                        mem[dest++] = REG8(AL);
                   6635:                        mem[dest++] = REG8(BL);
                   6636:                }
                   6637:        }
                   6638: }
                   6639: 
                   6640: inline void pcbios_int_10h_0ah()
                   6641: {
                   6642:        COORD co;
                   6643:        
1.1.1.14  root     6644:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   6645:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   6646:        
1.1.1.16  root     6647:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   6648:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     6649:        
                   6650:        if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     6651: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6652:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     6653: #endif
1.1.1.16  root     6654:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     6655:                while(dest < end) {
                   6656:                        write_text_vram_char(dest - vram, REG8(AL));
                   6657:                        mem[dest++] = REG8(AL);
                   6658:                        dest++;
1.1       root     6659:                }
1.1.1.35  root     6660: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6661:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     6662: #endif
1.1       root     6663:        } else {
1.1.1.14  root     6664:                while(dest < end) {
1.1       root     6665:                        mem[dest++] = REG8(AL);
                   6666:                        dest++;
                   6667:                }
                   6668:        }
                   6669: }
                   6670: 
                   6671: inline void pcbios_int_10h_0eh()
                   6672: {
1.1.1.14  root     6673:        DWORD num;
                   6674:        COORD co;
                   6675:        
                   6676:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   6677:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   6678:        
                   6679:        if(REG8(AL) == 7) {
                   6680:                //MessageBeep(-1);
                   6681:        } else if(REG8(AL) == 8 || REG8(AL) == 10 || REG8(AL) == 13) {
                   6682:                if(REG8(AL) == 10) {
                   6683:                        vram_flush();
                   6684:                }
1.1.1.23  root     6685:                WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), &REG8(AL), 1, &num, NULL);
1.1.1.14  root     6686:                cursor_moved = true;
                   6687:        } else {
1.1.1.16  root     6688:                int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
1.1.1.14  root     6689:                if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     6690: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6691:                        EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     6692: #endif
1.1.1.16  root     6693:                        int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     6694:                        write_text_vram_char(dest - vram, REG8(AL));
1.1.1.35  root     6695: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6696:                        LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     6697: #endif
1.1.1.14  root     6698:                        
1.1.1.23  root     6699:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6700:                        if(++co.X == scr_width) {
                   6701:                                co.X = 0;
                   6702:                                if(++co.Y == scr_height) {
                   6703:                                        vram_flush();
                   6704:                                        WriteConsole(hStdout, "\n", 1, &num, NULL);
                   6705:                                        cursor_moved = true;
                   6706:                                }
                   6707:                        }
                   6708:                        if(!cursor_moved) {
                   6709:                                co.Y += scr_top;
                   6710:                                SetConsoleCursorPosition(hStdout, co);
                   6711:                                cursor_moved = true;
                   6712:                        }
                   6713:                }
                   6714:                mem[dest] = REG8(AL);
                   6715:        }
1.1       root     6716: }
                   6717: 
                   6718: inline void pcbios_int_10h_0fh()
                   6719: {
                   6720:        REG8(AL) = mem[0x449];
                   6721:        REG8(AH) = mem[0x44a];
                   6722:        REG8(BH) = mem[0x462];
                   6723: }
                   6724: 
1.1.1.14  root     6725: inline void pcbios_int_10h_11h()
                   6726: {
                   6727:        switch(REG8(AL)) {
1.1.1.16  root     6728:        case 0x01:
1.1.1.14  root     6729:        case 0x11:
1.1.1.16  root     6730:                pcbios_set_console_size(80, 28, true);
1.1.1.14  root     6731:                break;
1.1.1.16  root     6732:        case 0x02:
1.1.1.14  root     6733:        case 0x12:
1.1.1.16  root     6734:                pcbios_set_console_size(80, 50, true);
1.1.1.14  root     6735:                break;
1.1.1.16  root     6736:        case 0x04:
1.1.1.14  root     6737:        case 0x14:
1.1.1.16  root     6738:                pcbios_set_console_size(80, 25, true);
                   6739:                break;
                   6740:        case 0x18:
                   6741:                pcbios_set_console_size(80, 50, true);
1.1.1.14  root     6742:                break;
                   6743:        case 0x30:
                   6744:                SREG(ES) = 0;
                   6745:                i386_load_segment_descriptor(ES);
                   6746:                REG16(BP) = 0;
                   6747:                REG16(CX) = mem[0x485];
                   6748:                REG8(DL) = mem[0x484];
                   6749:                break;
                   6750:        }
                   6751: }
                   6752: 
                   6753: inline void pcbios_int_10h_12h()
                   6754: {
1.1.1.16  root     6755:        switch(REG8(BL)) {
                   6756:        case 0x10:
1.1.1.14  root     6757:                REG16(BX) = 0x0003;
                   6758:                REG16(CX) = 0x0009;
1.1.1.16  root     6759:                break;
1.1.1.14  root     6760:        }
                   6761: }
                   6762: 
1.1       root     6763: inline void pcbios_int_10h_13h()
                   6764: {
1.1.1.3   root     6765:        int ofs = SREG_BASE(ES) + REG16(BP);
1.1       root     6766:        COORD co;
                   6767:        DWORD num;
                   6768:        
                   6769:        co.X = REG8(DL);
1.1.1.14  root     6770:        co.Y = REG8(DH) + scr_top;
                   6771:        
                   6772:        vram_flush();
1.1       root     6773:        
                   6774:        switch(REG8(AL)) {
                   6775:        case 0x00:
                   6776:        case 0x01:
                   6777:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     6778:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6779:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   6780:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   6781:                        SetConsoleCursorPosition(hStdout, co);
                   6782:                        
                   6783:                        if(csbi.wAttributes != REG8(BL)) {
                   6784:                                SetConsoleTextAttribute(hStdout, REG8(BL));
                   6785:                        }
1.1.1.14  root     6786:                        WriteConsole(hStdout, &mem[ofs], REG16(CX), &num, NULL);
                   6787:                        
1.1       root     6788:                        if(csbi.wAttributes != REG8(BL)) {
                   6789:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   6790:                        }
                   6791:                        if(REG8(AL) == 0x00) {
1.1.1.15  root     6792:                                if(!restore_console_on_exit) {
                   6793:                                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   6794:                                        scr_top = csbi.srWindow.Top;
                   6795:                                }
1.1.1.14  root     6796:                                co.X = mem[0x450 + REG8(BH) * 2];
                   6797:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     6798:                                SetConsoleCursorPosition(hStdout, co);
                   6799:                        } else {
                   6800:                                cursor_moved = true;
                   6801:                        }
                   6802:                } else {
1.1.1.3   root     6803:                        m_CF = 1;
1.1       root     6804:                }
                   6805:                break;
                   6806:        case 0x02:
                   6807:        case 0x03:
                   6808:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     6809:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6810:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   6811:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   6812:                        SetConsoleCursorPosition(hStdout, co);
                   6813:                        
                   6814:                        WORD wAttributes = csbi.wAttributes;
                   6815:                        for(int i = 0; i < REG16(CX); i++, ofs += 2) {
                   6816:                                if(wAttributes != mem[ofs + 1]) {
                   6817:                                        SetConsoleTextAttribute(hStdout, mem[ofs + 1]);
                   6818:                                        wAttributes = mem[ofs + 1];
                   6819:                                }
1.1.1.14  root     6820:                                WriteConsole(hStdout, &mem[ofs], 1, &num, NULL);
1.1       root     6821:                        }
                   6822:                        if(csbi.wAttributes != wAttributes) {
                   6823:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   6824:                        }
                   6825:                        if(REG8(AL) == 0x02) {
1.1.1.14  root     6826:                                co.X = mem[0x450 + REG8(BH) * 2];
                   6827:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     6828:                                SetConsoleCursorPosition(hStdout, co);
                   6829:                        } else {
                   6830:                                cursor_moved = true;
                   6831:                        }
                   6832:                } else {
1.1.1.3   root     6833:                        m_CF = 1;
1.1       root     6834:                }
                   6835:                break;
                   6836:        case 0x10:
                   6837:        case 0x11:
                   6838:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     6839:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6840:                        ReadConsoleOutputCharacter(hStdout, scr_char, REG16(CX), co, &num);
                   6841:                        ReadConsoleOutputAttribute(hStdout, scr_attr, REG16(CX), co, &num);
                   6842:                        for(int i = 0; i < num; i++) {
                   6843:                                mem[ofs++] = scr_char[i];
                   6844:                                mem[ofs++] = scr_attr[i];
                   6845:                                if(REG8(AL) == 0x11) {
                   6846:                                        mem[ofs++] = 0;
                   6847:                                        mem[ofs++] = 0;
                   6848:                                }
                   6849:                        }
                   6850:                } else {
1.1.1.16  root     6851:                        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     6852:                                mem[ofs++] = mem[src++];
                   6853:                                mem[ofs++] = mem[src++];
                   6854:                                if(REG8(AL) == 0x11) {
                   6855:                                        mem[ofs++] = 0;
                   6856:                                        mem[ofs++] = 0;
                   6857:                                }
1.1.1.14  root     6858:                                if(++co.X == scr_width) {
                   6859:                                        if(++co.Y == scr_height) {
1.1       root     6860:                                                break;
                   6861:                                        }
                   6862:                                        co.X = 0;
                   6863:                                }
                   6864:                        }
                   6865:                }
                   6866:                break;
                   6867:        case 0x20:
                   6868:        case 0x21:
                   6869:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     6870:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6871:                        int len = min(REG16(CX), scr_width * scr_height);
                   6872:                        for(int i = 0; i < len; i++) {
1.1       root     6873:                                scr_char[i] = mem[ofs++];
                   6874:                                scr_attr[i] = mem[ofs++];
                   6875:                                if(REG8(AL) == 0x21) {
                   6876:                                        ofs += 2;
                   6877:                                }
                   6878:                        }
1.1.1.14  root     6879:                        WriteConsoleOutputCharacter(hStdout, scr_char, len, co, &num);
                   6880:                        WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     6881:                } else {
1.1.1.16  root     6882:                        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     6883:                                mem[dest++] = mem[ofs++];
                   6884:                                mem[dest++] = mem[ofs++];
                   6885:                                if(REG8(AL) == 0x21) {
                   6886:                                        ofs += 2;
                   6887:                                }
1.1.1.14  root     6888:                                if(++co.X == scr_width) {
                   6889:                                        if(++co.Y == scr_height) {
1.1       root     6890:                                                break;
                   6891:                                        }
                   6892:                                        co.X = 0;
                   6893:                                }
                   6894:                        }
                   6895:                }
                   6896:                break;
                   6897:        default:
1.1.1.22  root     6898:                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     6899:                m_CF = 1;
1.1       root     6900:                break;
                   6901:        }
                   6902: }
                   6903: 
1.1.1.30  root     6904: inline void pcbios_int_10h_18h()
                   6905: {
                   6906:        switch(REG8(AL)) {
                   6907:        case 0x00:
                   6908:        case 0x01:
                   6909: //             REG8(AL) = 0x86;
                   6910:                REG8(AL) = 0x00;
                   6911:                break;
                   6912:        default:
                   6913:                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));
                   6914:                m_CF = 1;
                   6915:                break;
                   6916:        }
                   6917: }
                   6918: 
1.1.1.14  root     6919: inline void pcbios_int_10h_1ah()
                   6920: {
                   6921:        switch(REG8(AL)) {
                   6922:        case 0x00:
                   6923:                REG8(AL) = 0x1a;
                   6924:                REG8(BL) = 0x08;
                   6925:                REG8(BH) = 0x00;
                   6926:                break;
                   6927:        default:
1.1.1.22  root     6928:                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     6929:                m_CF = 1;
                   6930:                break;
                   6931:        }
                   6932: }
                   6933: 
1.1       root     6934: inline void pcbios_int_10h_1dh()
                   6935: {
                   6936:        switch(REG8(AL)) {
                   6937:        case 0x01:
                   6938:                break;
                   6939:        case 0x02:
                   6940:                REG16(BX) = 0;
                   6941:                break;
                   6942:        default:
1.1.1.22  root     6943:                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));
                   6944:                m_CF = 1;
                   6945:                break;
                   6946:        }
                   6947: }
                   6948: 
                   6949: inline void pcbios_int_10h_4fh()
                   6950: {
                   6951:        switch(REG8(AL)) {
                   6952:        case 0x00:
                   6953:                REG8(AH) = 0x02; // not supported
                   6954:                break;
                   6955:        case 0x01:
                   6956:        case 0x02:
                   6957:        case 0x03:
                   6958:        case 0x04:
                   6959:        case 0x05:
                   6960:        case 0x06:
                   6961:        case 0x07:
                   6962:        case 0x08:
                   6963:        case 0x09:
                   6964:        case 0x0a:
                   6965:        case 0x0b:
                   6966:        case 0x0c:
                   6967:                REG8(AH) = 0x01; // failed
                   6968:                break;
                   6969:        default:
                   6970:                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     6971:                m_CF = 1;
1.1       root     6972:                break;
                   6973:        }
                   6974: }
                   6975: 
                   6976: inline void pcbios_int_10h_82h()
                   6977: {
                   6978:        static UINT8 mode = 0;
                   6979:        
                   6980:        switch(REG8(AL)) {
1.1.1.22  root     6981:        case 0x00:
1.1       root     6982:                if(REG8(BL) != 0xff) {
                   6983:                        mode = REG8(BL);
                   6984:                }
                   6985:                REG8(AL) = mode;
                   6986:                break;
                   6987:        default:
1.1.1.22  root     6988:                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     6989:                m_CF = 1;
1.1       root     6990:                break;
                   6991:        }
                   6992: }
                   6993: 
1.1.1.22  root     6994: inline void pcbios_int_10h_83h()
                   6995: {
                   6996:        static UINT8 mode = 0;
                   6997:        
                   6998:        switch(REG8(AL)) {
                   6999:        case 0x00:
                   7000:                REG16(AX) = 0; // offset???
                   7001:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
                   7002:                i386_load_segment_descriptor(ES);
                   7003:                REG16(BX) = (SHADOW_BUF_TOP & 0x0f);
                   7004:                break;
                   7005:        default:
                   7006:                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));
                   7007:                m_CF = 1;
                   7008:                break;
                   7009:        }
                   7010: }
                   7011: 
                   7012: inline void pcbios_int_10h_90h()
                   7013: {
                   7014:        REG8(AL) = mem[0x449];
                   7015: }
                   7016: 
                   7017: inline void pcbios_int_10h_91h()
                   7018: {
                   7019:        REG8(AL) = 0x04; // VGA
                   7020: }
                   7021: 
                   7022: inline void pcbios_int_10h_efh()
                   7023: {
                   7024:        REG16(DX) = 0xffff;
                   7025: }
                   7026: 
1.1       root     7027: inline void pcbios_int_10h_feh()
                   7028: {
                   7029:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     7030:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
1.1.1.3   root     7031:                i386_load_segment_descriptor(ES);
1.1.1.8   root     7032:                REG16(DI) = (SHADOW_BUF_TOP & 0x0f);
1.1       root     7033:        }
                   7034:        int_10h_feh_called = true;
                   7035: }
                   7036: 
                   7037: inline void pcbios_int_10h_ffh()
                   7038: {
                   7039:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.23  root     7040:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7041:                COORD co;
                   7042:                DWORD num;
                   7043:                
1.1.1.14  root     7044:                vram_flush();
                   7045:                
                   7046:                co.X = (REG16(DI) >> 1) % scr_width;
                   7047:                co.Y = (REG16(DI) >> 1) / scr_width;
1.1.1.16  root     7048:                int ofs = pcbios_get_shadow_buffer_address(0, co.X, co.Y);
                   7049:                int end = min(ofs + REG16(CX) * 2, pcbios_get_shadow_buffer_address(0, 0, scr_height));
1.1.1.14  root     7050:                int len;
                   7051:                for(len = 0; ofs < end; len++) {
                   7052:                        scr_char[len] = mem[ofs++];
                   7053:                        scr_attr[len] = mem[ofs++];
                   7054:                }
                   7055:                co.Y += scr_top;
                   7056:                WriteConsoleOutputCharacter(hStdout, scr_char, len, co, &num);
                   7057:                WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     7058:        }
                   7059:        int_10h_ffh_called = true;
                   7060: }
                   7061: 
1.1.1.25  root     7062: inline void pcbios_int_14h_00h()
                   7063: {
1.1.1.29  root     7064:        if(REG16(DX) < 4) {
1.1.1.25  root     7065:                static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600};
                   7066:                UINT8 selector = sio_read(REG16(DX), 3);
                   7067:                selector &= ~0x3f;
                   7068:                selector |= REG8(AL) & 0x1f;
                   7069:                UINT16 divisor = 115200 / rate[REG8(AL) >> 5];
                   7070:                sio_write(REG16(DX), 3, selector | 0x80);
                   7071:                sio_write(REG16(DX), 0, divisor & 0xff);
                   7072:                sio_write(REG16(DX), 1, divisor >> 8);
                   7073:                sio_write(REG16(DX), 3, selector);
                   7074:                REG8(AH) = sio_read(REG16(DX), 5);
                   7075:                REG8(AL) = sio_read(REG16(DX), 6);
                   7076:        } else {
                   7077:                REG8(AH) = 0x80;
                   7078:        }
                   7079: }
                   7080: 
                   7081: inline void pcbios_int_14h_01h()
                   7082: {
1.1.1.29  root     7083:        if(REG16(DX) < 4) {
1.1.1.25  root     7084:                UINT8 selector = sio_read(REG16(DX), 3);
                   7085:                sio_write(REG16(DX), 3, selector & ~0x80);
                   7086:                sio_write(REG16(DX), 0, REG8(AL));
                   7087:                sio_write(REG16(DX), 3, selector);
                   7088:                REG8(AH) = sio_read(REG16(DX), 5);
                   7089:        } else {
                   7090:                REG8(AH) = 0x80;
                   7091:        }
                   7092: }
                   7093: 
                   7094: inline void pcbios_int_14h_02h()
                   7095: {
1.1.1.29  root     7096:        if(REG16(DX) < 4) {
1.1.1.25  root     7097:                UINT8 selector = sio_read(REG16(DX), 3);
                   7098:                sio_write(REG16(DX), 3, selector & ~0x80);
                   7099:                REG8(AL) = sio_read(REG16(DX), 0);
                   7100:                sio_write(REG16(DX), 3, selector);
                   7101:                REG8(AH) = sio_read(REG16(DX), 5);
                   7102:        } else {
                   7103:                REG8(AH) = 0x80;
                   7104:        }
                   7105: }
                   7106: 
                   7107: inline void pcbios_int_14h_03h()
                   7108: {
1.1.1.29  root     7109:        if(REG16(DX) < 4) {
1.1.1.25  root     7110:                REG8(AH) = sio_read(REG16(DX), 5);
                   7111:                REG8(AL) = sio_read(REG16(DX), 6);
                   7112:        } else {
                   7113:                REG8(AH) = 0x80;
                   7114:        }
                   7115: }
                   7116: 
                   7117: inline void pcbios_int_14h_04h()
                   7118: {
1.1.1.29  root     7119:        if(REG16(DX) < 4) {
1.1.1.25  root     7120:                UINT8 selector = sio_read(REG16(DX), 3);
                   7121:                if(REG8(CH) <= 0x03) {
                   7122:                        selector = (selector & ~0x03) | REG8(CH);
                   7123:                }
                   7124:                if(REG8(BL) == 0x00) {
                   7125:                        selector &= ~0x04;
                   7126:                } else if(REG8(BL) == 0x01) {
                   7127:                        selector |= 0x04;
                   7128:                }
                   7129:                if(REG8(BH) == 0x00) {
                   7130:                        selector = (selector & ~0x38) | 0x00;
                   7131:                } else if(REG8(BH) == 0x01) {
                   7132:                        selector = (selector & ~0x38) | 0x08;
                   7133:                } else if(REG8(BH) == 0x02) {
                   7134:                        selector = (selector & ~0x38) | 0x18;
                   7135:                } else if(REG8(BH) == 0x03) {
                   7136:                        selector = (selector & ~0x38) | 0x28;
                   7137:                } else if(REG8(BH) == 0x04) {
                   7138:                        selector = (selector & ~0x38) | 0x38;
                   7139:                }
                   7140:                if(REG8(AL) == 0x00) {
                   7141:                        selector |= 0x40;
                   7142:                } else if(REG8(AL) == 0x01) {
                   7143:                        selector &= ~0x40;
                   7144:                }
                   7145:                if(REG8(CL) <= 0x0b) {
                   7146:                        static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200};
                   7147:                        UINT16 divisor = 115200 / rate[REG8(CL)];
                   7148:                        sio_write(REG16(DX), 3, selector | 0x80);
                   7149:                        sio_write(REG16(DX), 0, divisor & 0xff);
                   7150:                        sio_write(REG16(DX), 1, divisor >> 8);
                   7151:                }
                   7152:                sio_write(REG16(DX), 3, selector);
                   7153:                REG8(AH) = sio_read(REG16(DX), 5);
                   7154:                REG8(AL) = sio_read(REG16(DX), 6);
                   7155:        } else {
                   7156:                REG8(AH) = 0x80;
                   7157:        }
                   7158: }
                   7159: 
                   7160: inline void pcbios_int_14h_05h()
                   7161: {
1.1.1.29  root     7162:        if(REG16(DX) < 4) {
1.1.1.25  root     7163:                if(REG8(AL) == 0x00) {
                   7164:                        REG8(BL) = sio_read(REG16(DX), 4);
                   7165:                        REG8(AH) = sio_read(REG16(DX), 5);
                   7166:                        REG8(AL) = sio_read(REG16(DX), 6);
                   7167:                } else if(REG8(AL) == 0x01) {
                   7168:                        sio_write(REG16(DX), 4, REG8(BL));
                   7169:                        REG8(AH) = sio_read(REG16(DX), 5);
                   7170:                        REG8(AL) = sio_read(REG16(DX), 6);
                   7171:                } else {
                   7172:                        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));
                   7173:                }
                   7174:        } else {
                   7175:                REG8(AH) = 0x80;
                   7176:        }
                   7177: }
                   7178: 
1.1.1.14  root     7179: inline void pcbios_int_15h_10h()
                   7180: {
1.1.1.22  root     7181:        switch(REG8(AL)) {
                   7182:        case 0x00:
1.1.1.14  root     7183:                Sleep(10);
1.1.1.35  root     7184:                REQUEST_HARDWRE_UPDATE();
1.1.1.22  root     7185:                break;
                   7186:        default:
                   7187:                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     7188:                REG8(AH) = 0x86;
                   7189:                m_CF = 1;
                   7190:        }
                   7191: }
                   7192: 
1.1       root     7193: inline void pcbios_int_15h_23h()
                   7194: {
                   7195:        switch(REG8(AL)) {
1.1.1.22  root     7196:        case 0x00:
1.1.1.8   root     7197:                REG8(CL) = cmos_read(0x2d);
                   7198:                REG8(CH) = cmos_read(0x2e);
1.1       root     7199:                break;
1.1.1.22  root     7200:        case 0x01:
1.1.1.8   root     7201:                cmos_write(0x2d, REG8(CL));
                   7202:                cmos_write(0x2e, REG8(CH));
1.1       root     7203:                break;
                   7204:        default:
1.1.1.22  root     7205:                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     7206:                REG8(AH) = 0x86;
1.1.1.3   root     7207:                m_CF = 1;
1.1       root     7208:                break;
                   7209:        }
                   7210: }
                   7211: 
                   7212: inline void pcbios_int_15h_24h()
                   7213: {
                   7214:        switch(REG8(AL)) {
1.1.1.22  root     7215:        case 0x00:
1.1.1.3   root     7216:                i386_set_a20_line(0);
1.1       root     7217:                REG8(AH) = 0;
                   7218:                break;
1.1.1.22  root     7219:        case 0x01:
1.1.1.3   root     7220:                i386_set_a20_line(1);
1.1       root     7221:                REG8(AH) = 0;
                   7222:                break;
1.1.1.22  root     7223:        case 0x02:
1.1       root     7224:                REG8(AH) = 0;
1.1.1.3   root     7225:                REG8(AL) = (m_a20_mask >> 20) & 1;
1.1       root     7226:                REG16(CX) = 0;
                   7227:                break;
1.1.1.22  root     7228:        case 0x03:
1.1       root     7229:                REG16(AX) = 0;
                   7230:                REG16(BX) = 0;
                   7231:                break;
1.1.1.22  root     7232:        default:
                   7233:                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));
                   7234:                REG8(AH) = 0x86;
                   7235:                m_CF = 1;
                   7236:                break;
1.1       root     7237:        }
                   7238: }
                   7239: 
                   7240: inline void pcbios_int_15h_49h()
                   7241: {
1.1.1.27  root     7242:        REG8(AH) = 0x00;
                   7243:        REG8(BL) = 0x00; // DOS/V
1.1       root     7244: }
                   7245: 
1.1.1.22  root     7246: inline void pcbios_int_15h_50h()
                   7247: {
                   7248:        switch(REG8(AL)) {
                   7249:        case 0x00:
                   7250:        case 0x01:
                   7251:                if(REG8(BH) != 0x00 && REG8(BH) != 0x01) {
                   7252:                        REG8(AH) = 0x01; // invalid font type in bh
                   7253:                        m_CF = 1;
1.1.1.27  root     7254:                } else if(REG8(BL) != 0x00) {
1.1.1.22  root     7255:                        REG8(AH) = 0x02; // bl not zero
                   7256:                        m_CF = 1;
                   7257:                } else if(REG16(BP) != 0 && REG16(BP) != 437 && REG16(BP) != 932 && REG16(BP) != 934 && REG16(BP) != 936 && REG16(BP) != 938) {
                   7258:                        REG8(AH) = 0x04; // invalid code page
                   7259:                        m_CF = 1;
1.1.1.27  root     7260:                } else if(REG8(AL) == 0x01) {
                   7261:                        REG8(AH) = 0x06; // font is read only
1.1.1.22  root     7262:                        m_CF = 1;
1.1.1.27  root     7263:                } else {
                   7264:                        // dummy font read routine is at fffd:000d
                   7265:                        SREG(ES) = 0xfffd;
                   7266:                        i386_load_segment_descriptor(ES);
1.1.1.32  root     7267:                        REG16(BX) = 0x000d;
1.1.1.27  root     7268:                        REG8(AH) = 0x00; // success
1.1.1.22  root     7269:                }
                   7270:                break;
                   7271:        default:
                   7272:                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));
                   7273:                REG8(AH) = 0x86;
                   7274:                m_CF = 1;
                   7275:                break;
                   7276:        }
                   7277: }
                   7278: 
1.1.1.30  root     7279: inline void pcbios_int_15h_53h()
                   7280: {
                   7281:        switch(REG8(AL)) {
                   7282:        case 0x00:
                   7283:                // APM is not installed
                   7284:                REG8(AH) = 0x86;
                   7285:                m_CF = 1;
                   7286:                break;
                   7287:        default:
                   7288:                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));
                   7289:                REG8(AH) = 0x86;
                   7290:                m_CF = 1;
                   7291:                break;
                   7292:        }
                   7293: }
                   7294: 
1.1.1.35  root     7295: 
                   7296: DWORD WINAPI pcbios_int_15h_86h_thread(LPVOID)
1.1       root     7297: {
                   7298:        UINT32 usec = (REG16(CX) << 16) | REG16(DX);
1.1.1.14  root     7299:        UINT32 msec = usec / 1000;
                   7300:        
1.1.1.35  root     7301:        while(msec && !m_halted) {
1.1.1.14  root     7302:                UINT32 tmp = min(msec, 100);
                   7303:                if(msec - tmp < 10) {
                   7304:                        tmp = msec;
                   7305:                }
                   7306:                Sleep(tmp);
                   7307:                msec -= tmp;
                   7308:        }
1.1.1.35  root     7309:        
                   7310: #ifdef USE_SERVICE_THREAD
                   7311:        service_exit = true;
                   7312: #endif
                   7313:        return(0);
                   7314: }
                   7315: 
                   7316: inline void pcbios_int_15h_86h()
                   7317: {
                   7318:        if(!(REG16(CX) == 0 && REG16(DX) == 0)) {
                   7319: #ifdef USE_SERVICE_THREAD
                   7320:                start_service_loop(pcbios_int_15h_86h_thread);
                   7321: #else
                   7322:                pcbios_int_15h_86h_thread(NULL);
                   7323:                REQUEST_HARDWRE_UPDATE();
                   7324: #endif
                   7325:        }
1.1       root     7326: }
                   7327: 
                   7328: inline void pcbios_int_15h_87h()
                   7329: {
                   7330:        // copy extended memory (from DOSBox)
                   7331:        int len = REG16(CX) * 2;
1.1.1.3   root     7332:        int ofs = SREG_BASE(ES) + REG16(SI);
1.1       root     7333:        int src = (*(UINT32 *)(mem + ofs + 0x12) & 0xffffff); // + (mem[ofs + 0x16] << 24);
                   7334:        int dst = (*(UINT32 *)(mem + ofs + 0x1a) & 0xffffff); // + (mem[ofs + 0x1e] << 24);
                   7335:        memcpy(mem + dst, mem + src, len);
                   7336:        REG16(AX) = 0x00;
                   7337: }
                   7338: 
                   7339: inline void pcbios_int_15h_88h()
                   7340: {
1.1.1.17  root     7341:        REG16(AX) = ((min(MAX_MEM, 0x4000000) - 0x100000) >> 10);
1.1       root     7342: }
                   7343: 
                   7344: inline void pcbios_int_15h_89h()
                   7345: {
1.1.1.21  root     7346: #if defined(HAS_I286) || defined(HAS_I386)
1.1       root     7347:        // switch to protected mode (from DOSBox)
                   7348:        write_io_byte(0x20, 0x10);
                   7349:        write_io_byte(0x21, REG8(BH));
                   7350:        write_io_byte(0x21, 0x00);
                   7351:        write_io_byte(0xa0, 0x10);
                   7352:        write_io_byte(0xa1, REG8(BL));
                   7353:        write_io_byte(0xa1, 0x00);
1.1.1.3   root     7354:        i386_set_a20_line(1);
                   7355:        int ofs = SREG_BASE(ES) + REG16(SI);
                   7356:        m_gdtr.limit = *(UINT16 *)(mem + ofs + 0x08);
                   7357:        m_gdtr.base = *(UINT32 *)(mem + ofs + 0x08 + 0x02) & 0xffffff;
                   7358:        m_idtr.limit = *(UINT16 *)(mem + ofs + 0x10);
                   7359:        m_idtr.base = *(UINT32 *)(mem + ofs + 0x10 + 0x02) & 0xffffff;
                   7360: #if defined(HAS_I386)
                   7361:        m_cr[0] |= 1;
                   7362: #else
                   7363:        m_msw |= 1;
                   7364: #endif
                   7365:        SREG(DS) = 0x18;
                   7366:        SREG(ES) = 0x20;
                   7367:        SREG(SS) = 0x28;
                   7368:        i386_load_segment_descriptor(DS);
                   7369:        i386_load_segment_descriptor(ES);
                   7370:        i386_load_segment_descriptor(SS);
1.1.1.21  root     7371:        UINT16 offset = *(UINT16 *)(mem + SREG_BASE(SS) + REG16(SP));
1.1       root     7372:        REG16(SP) += 6;
1.1.1.3   root     7373: #if defined(HAS_I386)
1.1.1.21  root     7374:        UINT32 flags = get_flags();
                   7375:        flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
                   7376:        set_flags(flags);
1.1.1.3   root     7377: #else
1.1.1.21  root     7378:        UINT32 flags = CompressFlags();
                   7379:        flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
                   7380:        ExpandFlags(flags);
1.1.1.3   root     7381: #endif
1.1       root     7382:        REG16(AX) = 0x00;
1.1.1.21  root     7383:        i386_jmp_far(0x30, /*REG16(CX)*/offset);
1.1       root     7384: #else
1.1.1.21  root     7385:        // i86/i186/v30: protected mode is not supported
1.1       root     7386:        REG8(AH) = 0x86;
1.1.1.3   root     7387:        m_CF = 1;
1.1       root     7388: #endif
                   7389: }
                   7390: 
1.1.1.21  root     7391: inline void pcbios_int_15h_8ah()
                   7392: {
                   7393:        UINT32 size = MAX_MEM - 0x100000;
                   7394:        REG16(AX) = size & 0xffff;
                   7395:        REG16(DX) = size >> 16;
                   7396: }
                   7397: 
1.1.1.3   root     7398: #if defined(HAS_I386)
1.1       root     7399: inline void pcbios_int_15h_c9h()
                   7400: {
                   7401:        REG8(AH) = 0x00;
                   7402:        REG8(CH) = cpu_type;
                   7403:        REG8(CL) = cpu_step;
                   7404: }
1.1.1.3   root     7405: #endif
1.1       root     7406: 
                   7407: inline void pcbios_int_15h_cah()
                   7408: {
                   7409:        switch(REG8(AL)) {
1.1.1.22  root     7410:        case 0x00:
1.1       root     7411:                if(REG8(BL) > 0x3f) {
                   7412:                        REG8(AH) = 0x03;
1.1.1.3   root     7413:                        m_CF = 1;
1.1       root     7414:                } else if(REG8(BL) < 0x0e) {
                   7415:                        REG8(AH) = 0x04;
1.1.1.3   root     7416:                        m_CF = 1;
1.1       root     7417:                } else {
1.1.1.8   root     7418:                        REG8(CL) = cmos_read(REG8(BL));
1.1       root     7419:                }
                   7420:                break;
1.1.1.22  root     7421:        case 0x01:
1.1       root     7422:                if(REG8(BL) > 0x3f) {
                   7423:                        REG8(AH) = 0x03;
1.1.1.3   root     7424:                        m_CF = 1;
1.1       root     7425:                } else if(REG8(BL) < 0x0e) {
                   7426:                        REG8(AH) = 0x04;
1.1.1.3   root     7427:                        m_CF = 1;
1.1       root     7428:                } else {
1.1.1.8   root     7429:                        cmos_write(REG8(BL), REG8(CL));
1.1       root     7430:                }
                   7431:                break;
                   7432:        default:
1.1.1.22  root     7433:                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     7434:                REG8(AH) = 0x86;
1.1.1.3   root     7435:                m_CF = 1;
1.1       root     7436:                break;
                   7437:        }
                   7438: }
                   7439: 
1.1.1.22  root     7440: inline void pcbios_int_15h_e8h()
1.1.1.17  root     7441: {
1.1.1.22  root     7442:        switch(REG8(AL)) {
                   7443: #if defined(HAS_I386)
                   7444:        case 0x01:
                   7445:                REG16(AX) = REG16(CX) = ((min(MAX_MEM, 0x1000000) - 0x100000) >> 10);
                   7446:                REG16(BX) = REG16(DX) = ((max(MAX_MEM, 0x1000000) - 0x1000000) >> 16);
                   7447:                break;
1.1.1.17  root     7448: #endif
1.1.1.22  root     7449:        default:
                   7450:                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));
                   7451:                REG8(AH) = 0x86;
                   7452:                m_CF = 1;
                   7453:                break;
                   7454:        }
                   7455: }
1.1.1.17  root     7456: 
1.1.1.33  root     7457: void pcbios_update_key_code(bool wait)
1.1       root     7458: {
1.1.1.32  root     7459:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     7460: #ifdef USE_SERVICE_THREAD
                   7461:                EnterCriticalSection(&key_buf_crit_sect);
                   7462: #endif
                   7463:                bool empty = key_buf_char->empty();
                   7464: #ifdef USE_SERVICE_THREAD
                   7465:                LeaveCriticalSection(&key_buf_crit_sect);
                   7466: #endif
                   7467:                if(empty) {
1.1.1.32  root     7468:                        if(!update_key_buffer()) {
1.1.1.33  root     7469:                                if(wait) {
1.1.1.32  root     7470:                                        Sleep(10);
                   7471:                                } else {
                   7472:                                        maybe_idle();
                   7473:                                }
1.1.1.14  root     7474:                        }
                   7475:                }
1.1.1.34  root     7476:        }
                   7477:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     7478: #ifdef USE_SERVICE_THREAD
                   7479:                EnterCriticalSection(&key_buf_crit_sect);
                   7480: #endif
1.1.1.32  root     7481:                if(key_buf_char->count() != 0) {
1.1.1.35  root     7482:                        key_code  = key_buf_char->read() << 0;
                   7483:                        key_code |= key_buf_scan->read() << 8;
                   7484:                        key_recv  = 0x0000ffff;
1.1.1.32  root     7485:                }
                   7486:                if(key_buf_char->count() != 0) {
1.1.1.35  root     7487:                        key_code |= key_buf_char->read() << 16;
                   7488:                        key_code |= key_buf_scan->read() << 24;
1.1.1.33  root     7489:                        key_recv |= 0xffff0000;
1.1.1.32  root     7490:                }
1.1.1.35  root     7491: #ifdef USE_SERVICE_THREAD
                   7492:                LeaveCriticalSection(&key_buf_crit_sect);
                   7493: #endif
1.1       root     7494:        }
                   7495: }
                   7496: 
1.1.1.35  root     7497: DWORD WINAPI pcbios_int_16h_00h_thread(LPVOID)
1.1       root     7498: {
1.1.1.33  root     7499:        while(key_recv == 0 && !m_halted) {
                   7500:                pcbios_update_key_code(true);
1.1       root     7501:        }
1.1.1.33  root     7502:        if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
                   7503:                if((key_code & 0xffff) == 0x0000 || (key_code & 0xffff) == 0xe000) {
                   7504:                        if(REG8(AH) == 0x10) {
                   7505:                                key_code = ((key_code >> 8) & 0xff) | ((key_code >> 16) & 0xff00);
                   7506:                        } else {
                   7507:                                key_code = ((key_code >> 16) & 0xff00);
                   7508:                        }
                   7509:                        key_recv >>= 16;
1.1       root     7510:                }
                   7511:        }
                   7512:        REG16(AX) = key_code & 0xffff;
                   7513:        key_code >>= 16;
1.1.1.33  root     7514:        key_recv >>= 16;
1.1.1.35  root     7515:        
                   7516: #ifdef USE_SERVICE_THREAD
                   7517:        service_exit = true;
                   7518: #endif
                   7519:        return(0);
                   7520: }
                   7521: 
                   7522: inline void pcbios_int_16h_00h()
                   7523: {
                   7524: #ifdef USE_SERVICE_THREAD
                   7525:        start_service_loop(pcbios_int_16h_00h_thread);
                   7526: #else
                   7527:        pcbios_int_16h_00h_thread(NULL);
                   7528:        REQUEST_HARDWRE_UPDATE();
                   7529: #endif
1.1       root     7530: }
                   7531: 
                   7532: inline void pcbios_int_16h_01h()
                   7533: {
1.1.1.33  root     7534:        if(key_recv == 0) {
                   7535:                pcbios_update_key_code(false);
1.1.1.5   root     7536:        }
1.1.1.33  root     7537:        if(key_recv != 0) {
                   7538:                UINT32 key_code_tmp = key_code;
                   7539:                if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
                   7540:                        if((key_code_tmp & 0xffff) == 0x0000 || (key_code_tmp & 0xffff) == 0xe000) {
                   7541:                                if(REG8(AH) == 0x11) {
                   7542:                                        key_code_tmp = ((key_code_tmp >> 8) & 0xff) | ((key_code_tmp >> 16) & 0xff00);
                   7543:                                } else {
                   7544:                                        key_code_tmp = ((key_code_tmp >> 16) & 0xff00);
                   7545:                                }
                   7546:                        }
1.1       root     7547:                }
1.1.1.5   root     7548:                REG16(AX) = key_code_tmp & 0xffff;
1.1.1.3   root     7549: #if defined(HAS_I386)
1.1.1.33  root     7550:                m_ZF = 0;
                   7551: #else
                   7552:                m_ZeroVal = 1;
                   7553: #endif
                   7554:        } else {
                   7555: #if defined(HAS_I386)
                   7556:                m_ZF = 1;
1.1.1.3   root     7557: #else
1.1.1.33  root     7558:                m_ZeroVal = 0;
1.1.1.3   root     7559: #endif
1.1.1.33  root     7560:        }
1.1       root     7561: }
                   7562: 
                   7563: inline void pcbios_int_16h_02h()
                   7564: {
                   7565:        REG8(AL)  = (GetAsyncKeyState(VK_INSERT ) & 0x0001) ? 0x80 : 0;
                   7566:        REG8(AL) |= (GetAsyncKeyState(VK_CAPITAL) & 0x0001) ? 0x40 : 0;
                   7567:        REG8(AL) |= (GetAsyncKeyState(VK_NUMLOCK) & 0x0001) ? 0x20 : 0;
                   7568:        REG8(AL) |= (GetAsyncKeyState(VK_SCROLL ) & 0x0001) ? 0x10 : 0;
                   7569:        REG8(AL) |= (GetAsyncKeyState(VK_MENU   ) & 0x8000) ? 0x08 : 0;
                   7570:        REG8(AL) |= (GetAsyncKeyState(VK_CONTROL) & 0x8000) ? 0x04 : 0;
                   7571:        REG8(AL) |= (GetAsyncKeyState(VK_LSHIFT ) & 0x8000) ? 0x02 : 0;
                   7572:        REG8(AL) |= (GetAsyncKeyState(VK_RSHIFT ) & 0x8000) ? 0x01 : 0;
                   7573: }
                   7574: 
                   7575: inline void pcbios_int_16h_03h()
                   7576: {
                   7577:        static UINT16 status = 0;
                   7578:        
                   7579:        switch(REG8(AL)) {
                   7580:        case 0x05:
                   7581:                status = REG16(BX);
                   7582:                break;
                   7583:        case 0x06:
                   7584:                REG16(BX) = status;
                   7585:                break;
                   7586:        default:
1.1.1.3   root     7587:                m_CF = 1;
1.1       root     7588:                break;
                   7589:        }
                   7590: }
                   7591: 
                   7592: inline void pcbios_int_16h_05h()
                   7593: {
1.1.1.32  root     7594:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     7595: #ifdef USE_SERVICE_THREAD
                   7596:                EnterCriticalSection(&key_buf_crit_sect);
                   7597: #endif
1.1.1.32  root     7598:                key_buf_char->write(REG8(CL));
                   7599:                key_buf_scan->write(REG8(CH));
1.1.1.35  root     7600: #ifdef USE_SERVICE_THREAD
                   7601:                LeaveCriticalSection(&key_buf_crit_sect);
                   7602: #endif
1.1.1.32  root     7603:        }
1.1       root     7604:        REG8(AL) = 0x00;
                   7605: }
                   7606: 
                   7607: inline void pcbios_int_16h_12h()
                   7608: {
                   7609:        pcbios_int_16h_02h();
                   7610:        
                   7611:        REG8(AH)  = 0;//(GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x80 : 0;
                   7612:        REG8(AH) |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   7613:        REG8(AH) |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   7614:        REG8(AH) |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
                   7615:        REG8(AH) |= (GetAsyncKeyState(VK_RMENU   ) & 0x8000) ? 0x08 : 0;
                   7616:        REG8(AH) |= (GetAsyncKeyState(VK_RCONTROL) & 0x8000) ? 0x04 : 0;
                   7617:        REG8(AH) |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   7618:        REG8(AH) |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   7619: }
                   7620: 
                   7621: inline void pcbios_int_16h_13h()
                   7622: {
                   7623:        static UINT16 status = 0;
                   7624:        
                   7625:        switch(REG8(AL)) {
                   7626:        case 0x00:
                   7627:                status = REG16(DX);
                   7628:                break;
                   7629:        case 0x01:
                   7630:                REG16(DX) = status;
                   7631:                break;
                   7632:        default:
1.1.1.22  root     7633:                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     7634:                m_CF = 1;
1.1       root     7635:                break;
                   7636:        }
                   7637: }
                   7638: 
                   7639: inline void pcbios_int_16h_14h()
                   7640: {
                   7641:        static UINT8 status = 0;
                   7642:        
                   7643:        switch(REG8(AL)) {
                   7644:        case 0x00:
                   7645:        case 0x01:
                   7646:                status = REG8(AL);
                   7647:                break;
                   7648:        case 0x02:
                   7649:                REG8(AL) = status;
                   7650:                break;
                   7651:        default:
1.1.1.22  root     7652:                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     7653:                m_CF = 1;
1.1       root     7654:                break;
                   7655:        }
                   7656: }
                   7657: 
1.1.1.24  root     7658: inline void pcbios_int_16h_55h()
                   7659: {
                   7660:        switch(REG8(AL)) {
                   7661:        case 0x00:
                   7662:                // keyboard tsr is not present
                   7663:                break;
                   7664:        case 0xfe:
                   7665:                // handlers for INT 08, INT 09, INT 16, INT 1B, and INT 1C are installed
                   7666:                break;
                   7667:        case 0xff:
                   7668:                break;
                   7669:        default:
                   7670:                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));
                   7671:                m_CF = 1;
                   7672:                break;
                   7673:        }
                   7674: }
                   7675: 
1.1.1.30  root     7676: inline void pcbios_int_16h_6fh()
                   7677: {
                   7678:        switch(REG8(AL)) {
                   7679:        case 0x00:
                   7680:                // HP Vectra EX-BIOS - "F16_INQUIRE" - Extended BIOS is not installed
                   7681:                break;
                   7682:        default:
                   7683:                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));
                   7684:                m_CF = 1;
                   7685:                break;
                   7686:        }
                   7687: }
                   7688: 
1.1.1.37! root     7689: UINT16 pcbios_printer_jis2sjis(UINT16 jis)
        !          7690: {
        !          7691:        UINT8 hi = jis >> 8;
        !          7692:        UINT8 lo = jis & 0xff;
        !          7693:        
        !          7694:        lo = (hi & 0x01) ? lo - 0x1f : lo + 0x7d;
        !          7695:        hi = (hi - 0x21) / 2 + 0x81;
        !          7696:        hi = (hi >= 0xa0) ? hi + 0x40 : hi;
        !          7697:        lo = (lo >= 0x7f) ? lo + 0x01 : lo;
        !          7698:        
        !          7699:        return((hi << 8) + lo);
        !          7700: }
        !          7701: 
        !          7702: UINT16 pcbios_printer_sjis2jis(UINT16 sjis)
        !          7703: {
        !          7704:        UINT8 hi = sjis >> 8;
        !          7705:        UINT8 lo = sjis & 0xff;
        !          7706:        
        !          7707:        if(hi == 0x80 || (hi >= 0xeb && hi <= 0xef) || (hi >= 0xf4 && hi <= 0xff)) {
        !          7708:                return(0x2121);
        !          7709:        }
        !          7710:        if((lo >= 0x00 && lo <= 0x3f) || lo == 0x7f || (lo >= 0xfd && lo <= 0xff)) {
        !          7711:                return(0x2121);
        !          7712:        }
        !          7713:        if(hi >= 0xf0 && hi <= 0xf3) {
        !          7714:                // gaiji
        !          7715:                if(lo >= 0x40 && lo <= 0x7e) {
        !          7716:                        return(0x7721 + lo - 0x40 + (hi - 0xf0) * 0x200);
        !          7717:                }
        !          7718:                if(lo >= 0x80 && lo <= 0x9e) {
        !          7719:                        return(0x7760 + lo - 0x80 + (hi - 0xf0) * 0x200);
        !          7720:                }
        !          7721:                if(lo >= 0x9f && lo <= 0xfc) {
        !          7722:                        return(0x7821 + lo - 0x9f + (hi - 0xf0) * 0x200);
        !          7723:                }
        !          7724:        }
        !          7725:        hi = (hi >= 0xe0) ? hi - 0x40 : hi;
        !          7726:        lo = (lo >= 0x80) ? lo - 0x01 : lo;
        !          7727:        hi = ((lo >= 0x9e) ? 1 : 0) + (hi - 0x81) * 2 + 0x21;
        !          7728:        lo = ((lo >= 0x9e) ? lo - 0x9e : lo - 0x40) + 0x21;
        !          7729:        
        !          7730:        return((hi << 8) + lo);
        !          7731: }
        !          7732: 
        !          7733: // ESC/P Users Guide:
        !          7734: // http://cweb.canon.jp/manual/satera-bj/pdf/bij1350-2350-escp.pdf
        !          7735: 
        !          7736: void pcbios_printer_out(int c, UINT8 data)
        !          7737: {
        !          7738:        if(pio[c].conv_mode) {
        !          7739:                if(pio[c].sjis_hi != 0) {
        !          7740:                        if(!pio[c].jis_mode) {
        !          7741:                                printer_out(c, 0x1c);
        !          7742:                                printer_out(c, 0x26);
        !          7743:                                pio[c].jis_mode = true;
        !          7744:                        }
        !          7745:                        UINT16 jis = pcbios_printer_sjis2jis((pio[c].sjis_hi << 8) | data);
        !          7746:                        printer_out(c, jis >> 8);
        !          7747:                        printer_out(c, jis & 0xff);
        !          7748:                        pio[c].sjis_hi = 0;
        !          7749:                } else if(pio[c].esc_buf[0] == 0x1b) {
        !          7750:                        printer_out(c, data);
        !          7751:                        if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
        !          7752:                                pio[c].esc_buf[pio[c].esc_len] = data;
        !          7753:                        }
        !          7754:                        pio[c].esc_len++;
        !          7755:                        
        !          7756:                        switch(pio[c].esc_buf[1]) {
        !          7757:                        case 0x20: case 0x21: case 0x2b: case 0x2d:
        !          7758:                        case 0x33:
        !          7759:                        case 0x4a: case 0x4e:
        !          7760:                        case 0x51: case 0x52: case 0x53: case 0x55: case 0x57:
        !          7761:                        case 0x6b: case 0x6c:
        !          7762:                        case 0x70: case 0x71: case 0x72: case 0x74: case 0x77: case 0x78:
        !          7763:                                if(pio[c].esc_len == 3) {
        !          7764:                                        pio[c].esc_buf[0] = 0x00;
        !          7765:                                }
        !          7766:                                break;
        !          7767:                        case 0x24: case 0x25:
        !          7768:                        case 0x3a:
        !          7769:                        case 0x5c:
        !          7770:                                if(pio[c].esc_len == 4) {
        !          7771:                                        pio[c].esc_buf[0] = 0x00;
        !          7772:                                }
        !          7773:                                break;
        !          7774:                        case 0x2a:
        !          7775:                                if(pio[c].esc_len >= 3) {
        !          7776:                                        switch(pio[c].esc_buf[2]) {
        !          7777:                                        case 0: case 1: case 2: case 3: case 4: case 6:
        !          7778:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 1) {
        !          7779:                                                        pio[c].esc_buf[0] = 0x00;
        !          7780:                                                }
        !          7781:                                                break;
        !          7782:                                        case 32: case 33: case 38: case 39: case 40:
        !          7783:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 3) {
        !          7784:                                                        pio[c].esc_buf[0] = 0x00;
        !          7785:                                                }
        !          7786:                                                break;
        !          7787:                                        default:
        !          7788:                                                pio[c].esc_buf[0] = 0x00;
        !          7789:                                                break;
        !          7790:                                        }
        !          7791:                                }
        !          7792:                                break;
        !          7793:                        case 0x40:
        !          7794:                                if(pio[c].jis_mode) {
        !          7795:                                        printer_out(c, 0x1c);
        !          7796:                                        printer_out(c, 0x2e);
        !          7797:                                        pio[c].jis_mode = false;
        !          7798:                                }
        !          7799:                                pio[c].esc_buf[0] = 0x00;
        !          7800:                                break;
        !          7801:                        case 0x42: case 0x44:
        !          7802:                                if(pio[c].esc_len >= 3 && data == 0) {
        !          7803:                                        pio[c].esc_buf[0] = 0x00;
        !          7804:                                }
        !          7805:                                break;
        !          7806:                        case 0x43:
        !          7807:                                if(pio[c].esc_len >= 3 && data != 0) {
        !          7808:                                        pio[c].esc_buf[0] = 0x00;
        !          7809:                                }
        !          7810:                                break;
        !          7811:                        default:
        !          7812:                                pio[c].esc_buf[0] = 0x00;
        !          7813:                                break;
        !          7814:                        }
        !          7815:                } else if(pio[c].esc_buf[0] == 0x1c) {
        !          7816:                        printer_out(c, data);
        !          7817:                        if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
        !          7818:                                pio[c].esc_buf[pio[c].esc_len] = data;
        !          7819:                        }
        !          7820:                        pio[c].esc_len++;
        !          7821:                        
        !          7822:                        switch(pio[c].esc_buf[1]) {
        !          7823:                        case 0x21: case 0x2d:
        !          7824:                        case 0x57:
        !          7825:                        case 0x6b:
        !          7826:                        case 0x72: case 0x78:
        !          7827:                                if(pio[c].esc_len == 3) {
        !          7828:                                        pio[c].esc_buf[0] = 0x00;
        !          7829:                                }
        !          7830:                                break;
        !          7831:                        case 0x26:
        !          7832:                                pio[c].jis_mode = true;
        !          7833:                                pio[c].esc_buf[0] = 0x00;
        !          7834:                                break;
        !          7835:                        case 0x2e:
        !          7836:                                pio[c].jis_mode = false;
        !          7837:                                pio[c].esc_buf[0] = 0x00;
        !          7838:                                break;
        !          7839:                        case 0x32:
        !          7840:                                if(pio[c].esc_len == 76) {
        !          7841:                                        pio[c].esc_buf[0] = 0x00;
        !          7842:                                }
        !          7843:                                break;
        !          7844:                        case 0x44:
        !          7845:                                if(pio[c].esc_len == 6) {
        !          7846:                                        pio[c].esc_buf[0] = 0x00;
        !          7847:                                }
        !          7848:                                break;
        !          7849:                        case 0x53: case 0x54:
        !          7850:                                if(pio[c].esc_len == 4) {
        !          7851:                                        pio[c].esc_buf[0] = 0x00;
        !          7852:                                }
        !          7853:                                break;
        !          7854:                        default:
        !          7855:                                pio[c].esc_buf[0] = 0x00;
        !          7856:                                break;
        !          7857:                        }
        !          7858:                } else if(data == 0x1b || data == 0x1c) {
        !          7859:                        printer_out(c, data);
        !          7860:                        pio[c].esc_buf[0] = data;
        !          7861:                        pio[c].esc_len = 1;
        !          7862:                } else if((data >= 0x80 && data <= 0x9f) || (data >= 0xe0 && data <= 0xff)) {
        !          7863:                        pio[c].sjis_hi = data;
        !          7864:                } else {
        !          7865:                        if(pio[c].jis_mode) {
        !          7866:                                printer_out(c, 0x1c);
        !          7867:                                printer_out(c, 0x2e);
        !          7868:                                pio[c].jis_mode = false;
        !          7869:                        }
        !          7870:                        printer_out(c, data);
        !          7871:                }
        !          7872:        } else {
        !          7873:                if(pio[c].jis_mode) {
        !          7874:                        printer_out(c, 0x1c);
        !          7875:                        printer_out(c, 0x2e);
        !          7876:                        pio[c].jis_mode = false;
        !          7877:                }
        !          7878:                printer_out(c, data);
        !          7879:        }
        !          7880: }
        !          7881: 
        !          7882: inline void pcbios_int_17h_00h()
        !          7883: {
        !          7884:        if(REG16(DX) < 3) {
        !          7885:                pcbios_printer_out(REG16(DX), REG8(AL));
        !          7886:                REG8(AH) = 0xd0;
        !          7887:        }
        !          7888: }
        !          7889: 
        !          7890: inline void pcbios_int_17h_01h()
        !          7891: {
        !          7892:        if(REG16(DX) < 3) {
        !          7893:                REG8(AH) = 0xd0;
        !          7894:        }
        !          7895: }
        !          7896: 
        !          7897: inline void pcbios_int_17h_02h()
        !          7898: {
        !          7899:        if(REG16(DX) < 3) {
        !          7900:                REG8(AH) = 0xd0;
        !          7901:        }
        !          7902: }
        !          7903: 
        !          7904: inline void pcbios_int_17h_03h()
        !          7905: {
        !          7906:        switch(REG8(AL)) {
        !          7907:        case 0x00:
        !          7908:                if(REG16(DX) < 3) {
        !          7909:                        if(pio[REG16(DX)].jis_mode) {
        !          7910:                                printer_out(REG16(DX), 0x1c);
        !          7911:                                printer_out(REG16(DX), 0x2e);
        !          7912:                                pio[REG16(DX)].jis_mode = false;
        !          7913:                        }
        !          7914:                        for(UINT16 i = 0; i < REG16(CX); i++) {
        !          7915:                                printer_out(REG16(DX), mem[SREG_BASE(ES) + REG16(BX) + i]);
        !          7916:                        }
        !          7917:                        REG16(CX) = 0x0000;
        !          7918:                        REG8(AH) = 0xd0;
        !          7919:                }
        !          7920:                break;
        !          7921:        default:
        !          7922:                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));
        !          7923:                break;
        !          7924:        }
        !          7925: }
        !          7926: 
        !          7927: inline void pcbios_int_17h_50h()
        !          7928: {
        !          7929:        switch(REG8(AL)) {
        !          7930:        case 0x00:
        !          7931:                if(REG16(DX) < 3) {
        !          7932:                        if(REG16(BX) = 0x0001) {
        !          7933:                                pio[REG16(DX)].conv_mode = false;
        !          7934:                                REG8(AL) = 0x00;
        !          7935:                        } else if(REG16(BX) = 0x0051) {
        !          7936:                                pio[REG16(DX)].conv_mode = true;
        !          7937:                                REG8(AL) = 0x00;
        !          7938:                        } else {
        !          7939:                                REG8(AL) = 0x01;
        !          7940:                        }
        !          7941:                } else {
        !          7942:                        REG8(AL) = 0x02;
        !          7943:                }
        !          7944:                break;
        !          7945:        case 0x01:
        !          7946:                if(REG16(DX) < 3) {
        !          7947:                        if(pio[REG16(DX)].conv_mode) {
        !          7948:                                REG16(BX) = 0x0051;
        !          7949:                        } else {
        !          7950:                                REG16(BX) = 0x0001;
        !          7951:                        }
        !          7952:                        REG8(AL) = 0x00;
        !          7953:                } else {
        !          7954:                        REG8(AL) = 0x02;
        !          7955:                }
        !          7956:                break;
        !          7957:        default:
        !          7958:                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));
        !          7959:                break;
        !          7960:        }
        !          7961: }
        !          7962: 
        !          7963: inline void pcbios_int_17h_51h()
        !          7964: {
        !          7965:        if(REG8(DH) >= 0x21 && REG8(DH) <= 0x7e && REG8(DL) >= 0x21 && REG8(DL) <= 0x7e) {
        !          7966:                REG16(DX) = pcbios_printer_jis2sjis(REG16(DX));
        !          7967:        } else {
        !          7968:                REG16(DX) = 0x0000;
        !          7969:        }
        !          7970: }
        !          7971: 
        !          7972: inline void pcbios_int_17h_52h()
        !          7973: {
        !          7974:        if(((REG8(DH) >= 0x81 && REG8(DH) <= 0x9f) || (REG8(DH) >= 0xe0 && REG8(DH) <= 0xfc)) && (REG8(DL) >= 0x40 && REG8(DL) <= 0xfc && REG8(DL) != 0x7f)) {
        !          7975:                REG16(DX) = pcbios_printer_sjis2jis(REG16(DX));
        !          7976:        } else {
        !          7977:                REG16(DX) = 0x0000;
        !          7978:        }
        !          7979: }
        !          7980: 
        !          7981: inline void pcbios_int_17h_84h()
        !          7982: {
        !          7983:        if(REG16(DX) < 3) {
        !          7984:                if(pio[REG16(DX)].jis_mode) {
        !          7985:                        printer_out(REG16(DX), 0x1c);
        !          7986:                        printer_out(REG16(DX), 0x2e);
        !          7987:                        pio[REG16(DX)].jis_mode = false;
        !          7988:                }
        !          7989:                printer_out(REG16(DX), REG8(AL));
        !          7990:                REG8(AH) = 0xd0;
        !          7991:        }
        !          7992: }
        !          7993: 
        !          7994: inline void pcbios_int_17h_85h()
        !          7995: {
        !          7996:        pio[0].conv_mode = (REG8(AL) == 0x00);
        !          7997: }
        !          7998: 
1.1       root     7999: inline void pcbios_int_1ah_00h()
                   8000: {
1.1.1.19  root     8001:        pcbios_update_daily_timer_counter(timeGetTime());
                   8002:        REG16(CX) = *(UINT16 *)(mem + 0x46e);
                   8003:        REG16(DX) = *(UINT16 *)(mem + 0x46c);
                   8004:        REG8(AL) = mem[0x470];
                   8005:        mem[0x470] = 0;
1.1       root     8006: }
                   8007: 
                   8008: inline int to_bcd(int t)
                   8009: {
                   8010:        int u = (t % 100) / 10;
                   8011:        return (u << 4) | (t % 10);
                   8012: }
                   8013: 
                   8014: inline void pcbios_int_1ah_02h()
                   8015: {
                   8016:        SYSTEMTIME time;
                   8017:        
                   8018:        GetLocalTime(&time);
                   8019:        REG8(CH) = to_bcd(time.wHour);
                   8020:        REG8(CL) = to_bcd(time.wMinute);
                   8021:        REG8(DH) = to_bcd(time.wSecond);
                   8022:        REG8(DL) = 0x00;
                   8023: }
                   8024: 
                   8025: inline void pcbios_int_1ah_04h()
                   8026: {
                   8027:        SYSTEMTIME time;
                   8028:        
                   8029:        GetLocalTime(&time);
                   8030:        REG8(CH) = to_bcd(time.wYear / 100);
                   8031:        REG8(CL) = to_bcd(time.wYear);
                   8032:        REG8(DH) = to_bcd(time.wMonth);
                   8033:        REG8(DL) = to_bcd(time.wDay);
                   8034: }
                   8035: 
                   8036: inline void pcbios_int_1ah_0ah()
                   8037: {
                   8038:        SYSTEMTIME time;
                   8039:        FILETIME file_time;
                   8040:        WORD dos_date, dos_time;
                   8041:        
                   8042:        GetLocalTime(&time);
                   8043:        SystemTimeToFileTime(&time, &file_time);
                   8044:        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   8045:        REG16(CX) = dos_date;
                   8046: }
                   8047: 
                   8048: // msdos system call
                   8049: 
                   8050: inline void msdos_int_21h_00h()
                   8051: {
1.1.1.3   root     8052:        msdos_process_terminate(SREG(CS), retval, 1);
1.1       root     8053: }
                   8054: 
1.1.1.35  root     8055: DWORD WINAPI msdos_int_21h_01h_thread(LPVOID)
1.1       root     8056: {
                   8057:        REG8(AL) = msdos_getche();
1.1.1.33  root     8058:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     8059:        
1.1.1.35  root     8060: #ifdef USE_SERVICE_THREAD
                   8061:        service_exit = true;
                   8062: #endif
                   8063:        return(0);
                   8064: }
                   8065: 
                   8066: inline void msdos_int_21h_01h()
                   8067: {
                   8068: #ifdef USE_SERVICE_THREAD
                   8069:        start_service_loop(msdos_int_21h_01h_thread);
                   8070: #else
                   8071:        msdos_int_21h_01h_thread(NULL);
                   8072:        REQUEST_HARDWRE_UPDATE();
                   8073: #endif
1.1       root     8074: }
                   8075: 
                   8076: inline void msdos_int_21h_02h()
                   8077: {
1.1.1.33  root     8078:        UINT8 data = REG8(DL);
                   8079:        msdos_putch(data);
                   8080:        REG8(AL) = data;
                   8081:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     8082: }
                   8083: 
                   8084: inline void msdos_int_21h_03h()
                   8085: {
                   8086:        REG8(AL) = msdos_aux_in();
                   8087: }
                   8088: 
                   8089: inline void msdos_int_21h_04h()
                   8090: {
                   8091:        msdos_aux_out(REG8(DL));
                   8092: }
                   8093: 
                   8094: inline void msdos_int_21h_05h()
                   8095: {
                   8096:        msdos_prn_out(REG8(DL));
                   8097: }
                   8098: 
                   8099: inline void msdos_int_21h_06h()
                   8100: {
                   8101:        if(REG8(DL) == 0xff) {
                   8102:                if(msdos_kbhit()) {
                   8103:                        REG8(AL) = msdos_getch();
1.1.1.3   root     8104: #if defined(HAS_I386)
                   8105:                        m_ZF = 0;
                   8106: #else
                   8107:                        m_ZeroVal = 1;
                   8108: #endif
1.1       root     8109:                } else {
                   8110:                        REG8(AL) = 0;
1.1.1.3   root     8111: #if defined(HAS_I386)
                   8112:                        m_ZF = 1;
                   8113: #else
                   8114:                        m_ZeroVal = 0;
                   8115: #endif
1.1.1.14  root     8116:                        maybe_idle();
1.1       root     8117:                }
                   8118:        } else {
1.1.1.33  root     8119:                UINT8 data = REG8(DL);
                   8120:                msdos_putch(data);
                   8121:                REG8(AL) = data;
1.1       root     8122:        }
                   8123: }
                   8124: 
1.1.1.35  root     8125: DWORD WINAPI msdos_int_21h_07h_thread(LPVOID)
1.1       root     8126: {
                   8127:        REG8(AL) = msdos_getch();
1.1.1.26  root     8128:        
1.1.1.35  root     8129: #ifdef USE_SERVICE_THREAD
                   8130:        service_exit = true;
                   8131: #endif
                   8132:        return(0);
1.1       root     8133: }
                   8134: 
1.1.1.35  root     8135: inline void msdos_int_21h_07h()
                   8136: {
                   8137: #ifdef USE_SERVICE_THREAD
                   8138:        start_service_loop(msdos_int_21h_07h_thread);
                   8139: #else
                   8140:        msdos_int_21h_07h_thread(NULL);
                   8141:        REQUEST_HARDWRE_UPDATE();
                   8142: #endif
                   8143: }
                   8144: 
                   8145: DWORD WINAPI msdos_int_21h_08h_thread(LPVOID)
1.1       root     8146: {
                   8147:        REG8(AL) = msdos_getch();
1.1.1.33  root     8148:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     8149:        
1.1.1.35  root     8150: #ifdef USE_SERVICE_THREAD
                   8151:        service_exit = true;
                   8152: #endif
                   8153:        return(0);
                   8154: }
                   8155: 
                   8156: inline void msdos_int_21h_08h()
                   8157: {
                   8158: #ifdef USE_SERVICE_THREAD
                   8159:        start_service_loop(msdos_int_21h_08h_thread);
                   8160: #else
                   8161:        msdos_int_21h_08h_thread(NULL);
                   8162:        REQUEST_HARDWRE_UPDATE();
                   8163: #endif
1.1       root     8164: }
                   8165: 
                   8166: inline void msdos_int_21h_09h()
                   8167: {
1.1.1.21  root     8168:        msdos_stdio_reopen();
                   8169:        
1.1.1.20  root     8170:        process_t *process = msdos_process_info_get(current_psp);
                   8171:        int fd = msdos_psp_get_file_table(1, current_psp);
                   8172:        
1.1.1.14  root     8173:        char *str = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   8174:        int len = 0;
1.1       root     8175:        
1.1.1.14  root     8176:        while(str[len] != '$' && len < 0x10000) {
                   8177:                len++;
                   8178:        }
1.1.1.20  root     8179:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     8180:                // stdout is redirected to file
1.1.1.20  root     8181:                msdos_write(fd, str, len);
1.1       root     8182:        } else {
                   8183:                for(int i = 0; i < len; i++) {
1.1.1.14  root     8184:                        msdos_putch(str[i]);
1.1       root     8185:                }
                   8186:        }
1.1.1.33  root     8187:        REG8(AL) = '$';
                   8188:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     8189: }
                   8190: 
1.1.1.35  root     8191: DWORD WINAPI msdos_int_21h_0ah_thread(LPVOID)
1.1       root     8192: {
1.1.1.3   root     8193:        int ofs = SREG_BASE(DS) + REG16(DX);
1.1       root     8194:        int max = mem[ofs] - 1;
                   8195:        UINT8 *buf = mem + ofs + 2;
                   8196:        int chr, p = 0;
                   8197:        
                   8198:        while((chr = msdos_getch()) != 0x0d) {
1.1.1.33  root     8199:                if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
1.1.1.26  root     8200:                        p = 0;
1.1.1.33  root     8201:                        msdos_putch(0x03);
                   8202:                        msdos_putch(0x0d);
                   8203:                        msdos_putch(0x0a);
1.1.1.26  root     8204:                        break;
1.1.1.33  root     8205:                } else if(ctrl_break_pressed) {
                   8206:                        // skip this byte
1.1.1.26  root     8207:                } else if(chr == 0x00) {
1.1       root     8208:                        // skip 2nd byte
                   8209:                        msdos_getch();
                   8210:                } else if(chr == 0x08) {
                   8211:                        // back space
                   8212:                        if(p > 0) {
                   8213:                                p--;
1.1.1.20  root     8214:                                if(msdos_ctrl_code_check(buf[p])) {
1.1.1.34  root     8215:                                        msdos_putch(0x08);
                   8216:                                        msdos_putch(0x08);
                   8217:                                        msdos_putch(0x20);
                   8218:                                        msdos_putch(0x20);
                   8219:                                        msdos_putch(0x08);
                   8220:                                        msdos_putch(0x08);
1.1.1.36  root     8221:                                } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
                   8222:                                        p--;
                   8223:                                        msdos_putch(0x08);
                   8224:                                        msdos_putch(0x08);
                   8225:                                        msdos_putch(0x20);
                   8226:                                        msdos_putch(0x20);
                   8227:                                        msdos_putch(0x08);
                   8228:                                        msdos_putch(0x08);
1.1.1.34  root     8229:                                } else {
                   8230:                                        msdos_putch(0x08);
                   8231:                                        msdos_putch(0x20);
                   8232:                                        msdos_putch(0x08);
                   8233:                                }
                   8234:                        }
                   8235:                } else if(chr == 0x1b) {
                   8236:                        // escape
                   8237:                        while(p > 0) {
                   8238:                                p--;
                   8239:                                if(msdos_ctrl_code_check(buf[p])) {
                   8240:                                        msdos_putch(0x08);
                   8241:                                        msdos_putch(0x08);
                   8242:                                        msdos_putch(0x20);
                   8243:                                        msdos_putch(0x20);
                   8244:                                        msdos_putch(0x08);
                   8245:                                        msdos_putch(0x08);
1.1.1.20  root     8246:                                } else {
1.1.1.34  root     8247:                                        msdos_putch(0x08);
                   8248:                                        msdos_putch(0x20);
                   8249:                                        msdos_putch(0x08);
1.1.1.20  root     8250:                                }
1.1       root     8251:                        }
                   8252:                } else if(p < max) {
                   8253:                        buf[p++] = chr;
                   8254:                        msdos_putch(chr);
                   8255:                }
                   8256:        }
                   8257:        buf[p] = 0x0d;
                   8258:        mem[ofs + 1] = p;
1.1.1.33  root     8259:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     8260:        
1.1.1.35  root     8261: #ifdef USE_SERVICE_THREAD
                   8262:        service_exit = true;
                   8263: #endif
                   8264:        return(0);
                   8265: }
                   8266: 
                   8267: inline void msdos_int_21h_0ah()
                   8268: {
                   8269:        if(mem[SREG_BASE(DS) + REG16(DX)] != 0x00) {
                   8270: #ifdef USE_SERVICE_THREAD
                   8271:                start_service_loop(msdos_int_21h_0ah_thread);
                   8272: #else
                   8273:                msdos_int_21h_0ah_thread(NULL);
                   8274:                REQUEST_HARDWRE_UPDATE();
                   8275: #endif
                   8276:        }
1.1       root     8277: }
                   8278: 
                   8279: inline void msdos_int_21h_0bh()
                   8280: {
                   8281:        if(msdos_kbhit()) {
                   8282:                REG8(AL) = 0xff;
                   8283:        } else {
                   8284:                REG8(AL) = 0x00;
1.1.1.14  root     8285:                maybe_idle();
1.1       root     8286:        }
1.1.1.33  root     8287:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     8288: }
                   8289: 
                   8290: inline void msdos_int_21h_0ch()
                   8291: {
                   8292:        // clear key buffer
1.1.1.21  root     8293:        msdos_stdio_reopen();
                   8294:        
1.1.1.20  root     8295:        process_t *process = msdos_process_info_get(current_psp);
                   8296:        int fd = msdos_psp_get_file_table(0, current_psp);
                   8297:        
                   8298:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     8299:                // stdin is redirected to file
                   8300:        } else {
                   8301:                while(msdos_kbhit()) {
                   8302:                        msdos_getch();
                   8303:                }
                   8304:        }
                   8305:        
                   8306:        switch(REG8(AL)) {
                   8307:        case 0x01:
                   8308:                msdos_int_21h_01h();
                   8309:                break;
                   8310:        case 0x06:
                   8311:                msdos_int_21h_06h();
                   8312:                break;
                   8313:        case 0x07:
                   8314:                msdos_int_21h_07h();
                   8315:                break;
                   8316:        case 0x08:
                   8317:                msdos_int_21h_08h();
                   8318:                break;
                   8319:        case 0x0a:
                   8320:                msdos_int_21h_0ah();
                   8321:                break;
                   8322:        default:
1.1.1.22  root     8323: //             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));
                   8324: //             REG16(AX) = 0x01;
                   8325: //             m_CF = 1;
1.1       root     8326:                break;
                   8327:        }
                   8328: }
                   8329: 
                   8330: inline void msdos_int_21h_0dh()
                   8331: {
                   8332: }
                   8333: 
                   8334: inline void msdos_int_21h_0eh()
                   8335: {
                   8336:        if(REG8(DL) < 26) {
                   8337:                _chdrive(REG8(DL) + 1);
                   8338:                msdos_cds_update(REG8(DL));
1.1.1.23  root     8339:                msdos_sda_update(current_psp);
1.1       root     8340:        }
                   8341:        REG8(AL) = 26; // zdrive
                   8342: }
                   8343: 
1.1.1.14  root     8344: inline void msdos_int_21h_0fh()
                   8345: {
                   8346:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8347:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   8348:        char *path = msdos_fcb_path(fcb);
                   8349:        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     8350:        
1.1.1.14  root     8351:        if(hFile == INVALID_HANDLE_VALUE) {
                   8352:                REG8(AL) = 0xff;
                   8353:        } else {
                   8354:                REG8(AL) = 0;
                   8355:                fcb->current_block = 0;
                   8356:                fcb->record_size = 128;
                   8357:                fcb->file_size = GetFileSize(hFile, NULL);
                   8358:                fcb->handle = hFile;
                   8359:                fcb->cur_record = 0;
                   8360:        }
                   8361: }
                   8362: 
                   8363: inline void msdos_int_21h_10h()
                   8364: {
                   8365:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8366:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   8367:        
                   8368:        REG8(AL) = CloseHandle(fcb->handle) ? 0 : 0xff;
                   8369: }
                   8370: 
1.1       root     8371: inline void msdos_int_21h_11h()
                   8372: {
1.1.1.3   root     8373:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8374:        fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     8375:        
                   8376:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     8377:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   8378:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   8379:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     8380:        char *path = msdos_fcb_path(fcb);
                   8381:        WIN32_FIND_DATA fd;
                   8382:        
1.1.1.13  root     8383:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   8384:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   8385:                FindClose(dtainfo->find_handle);
                   8386:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     8387:        }
                   8388:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     8389:        dtainfo->allowable_mask = (ext_fcb->flag == 0xff) ? ext_fcb->attribute : 0x20;
                   8390:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     8391:        
1.1.1.14  root     8392:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   8393:                dtainfo->allowable_mask &= ~8;
1.1       root     8394:        }
1.1.1.14  root     8395:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   8396:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     8397:                      !msdos_find_file_has_8dot3name(&fd)) {
                   8398:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   8399:                                FindClose(dtainfo->find_handle);
                   8400:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     8401:                                break;
                   8402:                        }
                   8403:                }
                   8404:        }
1.1.1.13  root     8405:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     8406:                if(ext_fcb->flag == 0xff) {
                   8407:                        ext_find->flag = 0xff;
                   8408:                        memset(ext_find->reserved, 0, 5);
                   8409:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   8410:                }
                   8411:                find->drive = _getdrive();
1.1.1.13  root     8412:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     8413:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   8414:                find->nt_res = 0;
                   8415:                msdos_find_file_conv_local_time(&fd);
                   8416:                find->create_time_ms = 0;
                   8417:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   8418:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   8419:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   8420:                find->cluster_hi = find->cluster_lo = 0;
                   8421:                find->file_size = fd.nFileSizeLow;
                   8422:                REG8(AL) = 0x00;
1.1.1.14  root     8423:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     8424:                if(ext_fcb->flag == 0xff) {
                   8425:                        ext_find->flag = 0xff;
                   8426:                        memset(ext_find->reserved, 0, 5);
                   8427:                        ext_find->attribute = 8;
                   8428:                }
                   8429:                find->drive = _getdrive();
                   8430:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   8431:                find->attribute = 8;
                   8432:                find->nt_res = 0;
                   8433:                msdos_find_file_conv_local_time(&fd);
                   8434:                find->create_time_ms = 0;
                   8435:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   8436:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   8437:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   8438:                find->cluster_hi = find->cluster_lo = 0;
                   8439:                find->file_size = 0;
1.1.1.14  root     8440:                dtainfo->allowable_mask &= ~8;
1.1       root     8441:                REG8(AL) = 0x00;
                   8442:        } else {
                   8443:                REG8(AL) = 0xff;
                   8444:        }
                   8445: }
                   8446: 
                   8447: inline void msdos_int_21h_12h()
                   8448: {
1.1.1.3   root     8449:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1.1.14  root     8450: //     fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     8451:        
                   8452:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     8453:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   8454:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   8455:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     8456:        WIN32_FIND_DATA fd;
                   8457:        
1.1.1.13  root     8458:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   8459:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   8460:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     8461:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     8462:                              !msdos_find_file_has_8dot3name(&fd)) {
                   8463:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   8464:                                        FindClose(dtainfo->find_handle);
                   8465:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     8466:                                        break;
                   8467:                                }
                   8468:                        }
                   8469:                } else {
1.1.1.13  root     8470:                        FindClose(dtainfo->find_handle);
                   8471:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     8472:                }
                   8473:        }
1.1.1.13  root     8474:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     8475:                if(ext_fcb->flag == 0xff) {
                   8476:                        ext_find->flag = 0xff;
                   8477:                        memset(ext_find->reserved, 0, 5);
                   8478:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   8479:                }
                   8480:                find->drive = _getdrive();
1.1.1.13  root     8481:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     8482:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   8483:                find->nt_res = 0;
                   8484:                msdos_find_file_conv_local_time(&fd);
                   8485:                find->create_time_ms = 0;
                   8486:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   8487:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   8488:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   8489:                find->cluster_hi = find->cluster_lo = 0;
                   8490:                find->file_size = fd.nFileSizeLow;
                   8491:                REG8(AL) = 0x00;
1.1.1.14  root     8492:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     8493:                if(ext_fcb->flag == 0xff) {
                   8494:                        ext_find->flag = 0xff;
                   8495:                        memset(ext_find->reserved, 0, 5);
                   8496:                        ext_find->attribute = 8;
                   8497:                }
                   8498:                find->drive = _getdrive();
                   8499:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   8500:                find->attribute = 8;
                   8501:                find->nt_res = 0;
                   8502:                msdos_find_file_conv_local_time(&fd);
                   8503:                find->create_time_ms = 0;
                   8504:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   8505:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   8506:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   8507:                find->cluster_hi = find->cluster_lo = 0;
                   8508:                find->file_size = 0;
1.1.1.14  root     8509:                dtainfo->allowable_mask &= ~8;
1.1       root     8510:                REG8(AL) = 0x00;
                   8511:        } else {
                   8512:                REG8(AL) = 0xff;
                   8513:        }
                   8514: }
                   8515: 
                   8516: inline void msdos_int_21h_13h()
                   8517: {
1.1.1.3   root     8518:        if(remove(msdos_fcb_path((fcb_t *)(mem + SREG_BASE(DS) + REG16(DX))))) {
1.1       root     8519:                REG8(AL) = 0xff;
                   8520:        } else {
                   8521:                REG8(AL) = 0x00;
                   8522:        }
                   8523: }
                   8524: 
1.1.1.16  root     8525: inline void msdos_int_21h_14h()
                   8526: {
                   8527:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8528:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   8529:        process_t *process = msdos_process_info_get(current_psp);
                   8530:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   8531:        DWORD num = 0;
                   8532:        
                   8533:        memset(mem + dta_laddr, 0, fcb->record_size);
                   8534:        if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   8535:                REG8(AL) = 1;
                   8536:        } else {
                   8537:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   8538:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   8539:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   8540:                REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   8541:        }
                   8542: }
                   8543: 
                   8544: inline void msdos_int_21h_15h()
1.1.1.14  root     8545: {
                   8546:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8547:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.16  root     8548:        process_t *process = msdos_process_info_get(current_psp);
                   8549:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   8550:        DWORD num = 0;
1.1.1.14  root     8551:        
1.1.1.16  root     8552:        if(!WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   8553:                REG8(AL) = 1;
                   8554:        } else {
                   8555:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   8556:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   8557:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   8558:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   8559:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   8560:        }
                   8561: }
                   8562: 
                   8563: inline void msdos_int_21h_16h()
                   8564: {
                   8565:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8566:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.14  root     8567:        char *path = msdos_fcb_path(fcb);
                   8568:        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     8569:        
1.1.1.14  root     8570:        if(hFile == INVALID_HANDLE_VALUE) {
                   8571:                REG8(AL) = 0xff;
                   8572:        } else {
                   8573:                REG8(AL) = 0;
                   8574:                fcb->current_block = 0;
                   8575:                fcb->record_size = 128;
                   8576:                fcb->file_size = 0;
                   8577:                fcb->handle = hFile;
                   8578:                fcb->cur_record = 0;
                   8579:        }
                   8580: }
                   8581: 
1.1.1.16  root     8582: inline void msdos_int_21h_17h()
                   8583: {
                   8584:        ext_fcb_t *ext_fcb_src = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8585:        fcb_t *fcb_src = (fcb_t *)(ext_fcb_src + (ext_fcb_src->flag == 0xff ? 1 : 0));
                   8586:        char *path_src = msdos_fcb_path(fcb_src);
                   8587:        ext_fcb_t *ext_fcb_dst = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + 16);
                   8588:        fcb_t *fcb_dst = (fcb_t *)(ext_fcb_dst + (ext_fcb_dst->flag == 0xff ? 1 : 0));
                   8589:        char *path_dst = msdos_fcb_path(fcb_dst);
                   8590:        
                   8591:        if(rename(path_src, path_dst)) {
                   8592:                REG8(AL) = 0xff;
                   8593:        } else {
                   8594:                REG8(AL) = 0;
                   8595:        }
                   8596: }
                   8597: 
1.1       root     8598: inline void msdos_int_21h_18h()
                   8599: {
                   8600:        REG8(AL) = 0x00;
                   8601: }
                   8602: 
                   8603: inline void msdos_int_21h_19h()
                   8604: {
                   8605:        REG8(AL) = _getdrive() - 1;
                   8606: }
                   8607: 
                   8608: inline void msdos_int_21h_1ah()
                   8609: {
                   8610:        process_t *process = msdos_process_info_get(current_psp);
                   8611:        
                   8612:        process->dta.w.l = REG16(DX);
1.1.1.3   root     8613:        process->dta.w.h = SREG(DS);
1.1.1.23  root     8614:        msdos_sda_update(current_psp);
1.1       root     8615: }
                   8616: 
                   8617: inline void msdos_int_21h_1bh()
                   8618: {
                   8619:        int drive_num = _getdrive() - 1;
                   8620:        UINT16 seg, ofs;
                   8621:        
                   8622:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   8623:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   8624:                REG8(AL) = dpb->highest_sector_num + 1;
                   8625:                REG16(CX) = dpb->bytes_per_sector;
                   8626:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     8627:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     8628:        } else {
                   8629:                REG8(AL) = 0xff;
1.1.1.3   root     8630:                m_CF = 1;
1.1       root     8631:        }
                   8632: 
                   8633: }
                   8634: 
                   8635: inline void msdos_int_21h_1ch()
                   8636: {
                   8637:        int drive_num = REG8(DL) ? (REG8(DL) - 1) : (_getdrive() - 1);
                   8638:        UINT16 seg, ofs;
                   8639:        
                   8640:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   8641:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   8642:                REG8(AL) = dpb->highest_sector_num + 1;
                   8643:                REG16(CX) = dpb->bytes_per_sector;
                   8644:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     8645:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     8646:        } else {
                   8647:                REG8(AL) = 0xff;
1.1.1.3   root     8648:                m_CF = 1;
1.1       root     8649:        }
                   8650: 
                   8651: }
                   8652: 
                   8653: inline void msdos_int_21h_1dh()
                   8654: {
                   8655:        REG8(AL) = 0;
                   8656: }
                   8657: 
                   8658: inline void msdos_int_21h_1eh()
                   8659: {
                   8660:        REG8(AL) = 0;
                   8661: }
                   8662: 
                   8663: inline void msdos_int_21h_1fh()
                   8664: {
                   8665:        int drive_num = _getdrive() - 1;
                   8666:        UINT16 seg, ofs;
                   8667:        
                   8668:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   8669:                REG8(AL) = 0;
1.1.1.3   root     8670:                SREG(DS) = seg;
                   8671:                i386_load_segment_descriptor(DS);
1.1       root     8672:                REG16(BX) = ofs;
                   8673:        } else {
                   8674:                REG8(AL) = 0xff;
1.1.1.3   root     8675:                m_CF = 1;
1.1       root     8676:        }
                   8677: }
                   8678: 
                   8679: inline void msdos_int_21h_20h()
                   8680: {
                   8681:        REG8(AL) = 0;
                   8682: }
                   8683: 
1.1.1.14  root     8684: inline void msdos_int_21h_21h()
                   8685: {
                   8686:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8687:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   8688:        
                   8689:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   8690:                REG8(AL) = 1;
                   8691:        } else {
                   8692:                process_t *process = msdos_process_info_get(current_psp);
                   8693:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   8694:                memset(mem + dta_laddr, 0, fcb->record_size);
1.1.1.16  root     8695:                DWORD num = 0;
1.1.1.14  root     8696:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   8697:                        REG8(AL) = 1;
                   8698:                } else {
                   8699:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   8700:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     8701:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
1.1.1.14  root     8702:                }
                   8703:        }
                   8704: }
                   8705: 
                   8706: inline void msdos_int_21h_22h()
                   8707: {
                   8708:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8709:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   8710:        
                   8711:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   8712:                REG8(AL) = 0xff;
                   8713:        } else {
                   8714:                process_t *process = msdos_process_info_get(current_psp);
                   8715:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
1.1.1.16  root     8716:                DWORD num = 0;
1.1.1.14  root     8717:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL);
                   8718:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   8719:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   8720:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     8721:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
1.1.1.14  root     8722:        }
                   8723: }
                   8724: 
1.1.1.16  root     8725: inline void msdos_int_21h_23h()
                   8726: {
                   8727:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8728:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   8729:        char *path = msdos_fcb_path(fcb);
                   8730:        HANDLE hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   8731:        
                   8732:        if(hFile == INVALID_HANDLE_VALUE) {
                   8733:                REG8(AL) = 0xff;
                   8734:        } else {
                   8735:                UINT32 size = GetFileSize(hFile, NULL);
                   8736:                fcb->rand_record = size / fcb->record_size + ((size % fcb->record_size) != 0);
                   8737:                REG8(AL) = 0;
                   8738:        }
                   8739: }
                   8740: 
                   8741: inline void msdos_int_21h_24h()
                   8742: {
                   8743:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8744:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   8745:        
                   8746:        fcb->rand_record = fcb->current_block * fcb->record_size + fcb->cur_record;
                   8747: }
                   8748: 
1.1       root     8749: inline void msdos_int_21h_25h()
                   8750: {
                   8751:        *(UINT16 *)(mem + 4 * REG8(AL) + 0) = REG16(DX);
1.1.1.3   root     8752:        *(UINT16 *)(mem + 4 * REG8(AL) + 2) = SREG(DS);
1.1       root     8753: }
                   8754: 
                   8755: inline void msdos_int_21h_26h()
                   8756: {
                   8757:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   8758:        
                   8759:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
                   8760:        psp->first_mcb = REG16(DX) + 16;
                   8761:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   8762:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   8763:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   8764:        psp->parent_psp = 0;
                   8765: }
                   8766: 
1.1.1.16  root     8767: inline void msdos_int_21h_27h()
                   8768: {
                   8769:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8770:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   8771:        
                   8772:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   8773:                REG8(AL) = 1;
                   8774:        } else {
                   8775:                process_t *process = msdos_process_info_get(current_psp);
                   8776:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   8777:                memset(mem + dta_laddr, 0, fcb->record_size * REG16(CX));
                   8778:                DWORD num = 0;
                   8779:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL) || num == 0) {
                   8780:                        REG8(AL) = 1;
                   8781:                } else {
                   8782:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   8783:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   8784:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   8785:                        REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   8786:                }
                   8787:        }
                   8788: }
                   8789: 
                   8790: inline void msdos_int_21h_28h()
                   8791: {
                   8792:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8793:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   8794:        
                   8795:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   8796:                REG8(AL) = 0xff;
                   8797:        } else {
                   8798:                process_t *process = msdos_process_info_get(current_psp);
                   8799:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   8800:                DWORD num = 0;
                   8801:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL);
                   8802:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   8803:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   8804:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   8805:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   8806:                REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   8807:        }
                   8808: }
                   8809: 
1.1       root     8810: inline void msdos_int_21h_29h()
                   8811: {
1.1.1.20  root     8812:        int ofs = 0;//SREG_BASE(DS) + REG16(SI);
                   8813:        char buffer[1024], name[MAX_PATH], ext[MAX_PATH];
1.1       root     8814:        UINT8 drv = 0;
                   8815:        char sep_chars[] = ":.;,=+";
                   8816:        char end_chars[] = "\\<>|/\"[]";
                   8817:        char spc_chars[] = " \t";
                   8818:        
1.1.1.20  root     8819:        memcpy(buffer, mem + SREG_BASE(DS) + REG16(SI), 1023);
                   8820:        buffer[1023] = 0;
                   8821:        memset(name, 0x20, sizeof(name));
                   8822:        memset(ext, 0x20, sizeof(ext));
                   8823:        
1.1       root     8824:        if(REG8(AL) & 1) {
1.1.1.20  root     8825:                ofs += strspn((char *)(buffer + ofs), spc_chars);
                   8826:                if(my_strchr(sep_chars, buffer[ofs]) && buffer[ofs] != '\0') {
1.1       root     8827:                        ofs++;
                   8828:                }
                   8829:        }
1.1.1.20  root     8830:        ofs += strspn((char *)(buffer + ofs), spc_chars);
1.1       root     8831:        
1.1.1.24  root     8832:        if(buffer[ofs + 1] == ':') {
                   8833:                if(buffer[ofs] >= 'a' && buffer[ofs] <= 'z') {
                   8834:                        drv = buffer[ofs] - 'a' + 1;
1.1.1.20  root     8835:                        ofs += 2;
1.1.1.24  root     8836:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   8837:                                ofs++;
                   8838:                        }
                   8839:                } else if(buffer[ofs] >= 'A' && buffer[ofs] <= 'Z') {
                   8840:                        drv = buffer[ofs] - 'A' + 1;
1.1       root     8841:                        ofs += 2;
1.1.1.24  root     8842:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   8843:                                ofs++;
                   8844:                        }
1.1       root     8845:                }
                   8846:        }
1.1.1.20  root     8847:        for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   8848:                UINT8 c = buffer[ofs];
                   8849:                if(is_kanji) {
                   8850:                        is_kanji = 0;
                   8851:                } else if(msdos_lead_byte_check(c)) {
                   8852:                        is_kanji = 1;
                   8853:                } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     8854:                        break;
                   8855:                } else if(c >= 'a' && c <= 'z') {
                   8856:                        c -= 0x20;
                   8857:                }
                   8858:                ofs++;
                   8859:                name[i] = c;
                   8860:        }
1.1.1.20  root     8861:        if(buffer[ofs] == '.') {
1.1       root     8862:                ofs++;
1.1.1.20  root     8863:                for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   8864:                        UINT8 c = buffer[ofs];
                   8865:                        if(is_kanji) {
                   8866:                                is_kanji = 0;
                   8867:                        } else if(msdos_lead_byte_check(c)) {
                   8868:                                is_kanji = 1;
                   8869:                        } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     8870:                                break;
                   8871:                        } else if(c >= 'a' && c <= 'z') {
                   8872:                                c -= 0x20;
                   8873:                        }
                   8874:                        ofs++;
                   8875:                        ext[i] = c;
                   8876:                }
                   8877:        }
1.1.1.20  root     8878:        int si = REG16(SI) + ofs;
1.1.1.3   root     8879:        int ds = SREG(DS);
1.1       root     8880:        while(si > 0xffff) {
                   8881:                si -= 0x10;
                   8882:                ds++;
                   8883:        }
                   8884:        REG16(SI) = si;
1.1.1.3   root     8885:        SREG(DS) = ds;
                   8886:        i386_load_segment_descriptor(DS);
1.1       root     8887:        
1.1.1.3   root     8888:        UINT8 *fcb = mem + SREG_BASE(ES) + REG16(DI);
1.1.1.20  root     8889:        if(!(REG8(AL) & 2) || drv != 0) {
                   8890:                fcb[0] = drv;
                   8891:        }
                   8892:        if(!(REG8(AL) & 4) || name[0] != 0x20) {
                   8893:                memcpy(fcb + 1, name, 8);
                   8894:        }
                   8895:        if(!(REG8(AL) & 8) || ext[0] != 0x20) {
                   8896:                memcpy(fcb + 9, ext, 3);
                   8897:        }
                   8898:        for(int i = 1, found_star = 0; i < 1 + 8; i++) {
1.1       root     8899:                if(fcb[i] == '*') {
                   8900:                        found_star = 1;
                   8901:                }
                   8902:                if(found_star) {
                   8903:                        fcb[i] = '?';
                   8904:                }
                   8905:        }
1.1.1.20  root     8906:        for(int i = 9, found_star = 0; i < 9 + 3; i++) {
1.1       root     8907:                if(fcb[i] == '*') {
                   8908:                        found_star = 1;
                   8909:                }
                   8910:                if(found_star) {
                   8911:                        fcb[i] = '?';
                   8912:                }
                   8913:        }
                   8914:        
                   8915:        if(drv == 0 || (drv > 0 && drv <= 26 && (GetLogicalDrives() & ( 1 << (drv - 1) )))) {
                   8916:                if(memchr(fcb + 1, '?', 8 + 3)) {
                   8917:                        REG8(AL) = 0x01;
1.1.1.20  root     8918:                } else {
                   8919:                        REG8(AL) = 0x00;
1.1       root     8920:                }
                   8921:        } else {
                   8922:                REG8(AL) = 0xff;
                   8923:        }
                   8924: }
                   8925: 
                   8926: inline void msdos_int_21h_2ah()
                   8927: {
                   8928:        SYSTEMTIME sTime;
                   8929:        
                   8930:        GetLocalTime(&sTime);
                   8931:        REG16(CX) = sTime.wYear;
                   8932:        REG8(DH) = (UINT8)sTime.wMonth;
                   8933:        REG8(DL) = (UINT8)sTime.wDay;
                   8934:        REG8(AL) = (UINT8)sTime.wDayOfWeek;
                   8935: }
                   8936: 
                   8937: inline void msdos_int_21h_2bh()
                   8938: {
1.1.1.14  root     8939:        REG8(AL) = 0xff;
1.1       root     8940: }
                   8941: 
                   8942: inline void msdos_int_21h_2ch()
                   8943: {
                   8944:        SYSTEMTIME sTime;
                   8945:        
                   8946:        GetLocalTime(&sTime);
                   8947:        REG8(CH) = (UINT8)sTime.wHour;
                   8948:        REG8(CL) = (UINT8)sTime.wMinute;
                   8949:        REG8(DH) = (UINT8)sTime.wSecond;
1.1.1.14  root     8950:        REG8(DL) = (UINT8)sTime.wMilliseconds / 10;
1.1       root     8951: }
                   8952: 
                   8953: inline void msdos_int_21h_2dh()
                   8954: {
                   8955:        REG8(AL) = 0x00;
                   8956: }
                   8957: 
                   8958: inline void msdos_int_21h_2eh()
                   8959: {
                   8960:        process_t *process = msdos_process_info_get(current_psp);
                   8961:        
                   8962:        process->verify = REG8(AL);
                   8963: }
                   8964: 
                   8965: inline void msdos_int_21h_2fh()
                   8966: {
                   8967:        process_t *process = msdos_process_info_get(current_psp);
                   8968:        
                   8969:        REG16(BX) = process->dta.w.l;
1.1.1.3   root     8970:        SREG(ES) = process->dta.w.h;
                   8971:        i386_load_segment_descriptor(ES);
1.1       root     8972: }
                   8973: 
                   8974: inline void msdos_int_21h_30h()
                   8975: {
                   8976:        // Version Flag / OEM
1.1.1.27  root     8977:        if(REG8(AL) == 0x01) {
1.1.1.29  root     8978: #ifdef SUPPORT_HMA
                   8979:                REG16(BX) = 0x0000;
                   8980: #else
                   8981:                REG16(BX) = 0x1000; // DOS is in HMA
                   8982: #endif
1.1       root     8983:        } else {
1.1.1.27  root     8984:                // NOTE: EXDEB invites BL shows the machine type (0=PC-98, 1=PC/AT, 2=FMR),
                   8985:                // but this is not correct on Windows 98 SE
                   8986: //             REG16(BX) = 0xff01;     // OEM = Microsoft, PC/AT
                   8987:                REG16(BX) = 0xff00;     // OEM = Microsoft
1.1       root     8988:        }
1.1.1.27  root     8989:        REG16(CX) = 0x0000;
1.1.1.30  root     8990:        REG8(AL) = dos_major_version;   // 7
                   8991:        REG8(AH) = dos_minor_version;   // 10
1.1       root     8992: }
                   8993: 
                   8994: inline void msdos_int_21h_31h()
                   8995: {
1.1.1.29  root     8996:        try {
                   8997:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   8998:        } catch(...) {
                   8999:                // recover the broken mcb
                   9000:                int mcb_seg = current_psp - 1;
                   9001:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     9002:                
1.1.1.29  root     9003:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     9004:                        mcb->mz = 'M';
                   9005:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   9006:                        
1.1.1.29  root     9007:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.33  root     9008:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4));
1.1.1.29  root     9009:                        } else {
1.1.1.33  root     9010:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0);
1.1.1.29  root     9011:                        }
                   9012:                } else {
                   9013:                        mcb->mz = 'Z';
1.1.1.30  root     9014:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     9015:                }
                   9016:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   9017:        }
1.1       root     9018:        msdos_process_terminate(current_psp, REG8(AL) | 0x300, 0);
                   9019: }
                   9020: 
                   9021: inline void msdos_int_21h_32h()
                   9022: {
                   9023:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   9024:        UINT16 seg, ofs;
                   9025:        
                   9026:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9027:                REG8(AL) = 0;
1.1.1.3   root     9028:                SREG(DS) = seg;
                   9029:                i386_load_segment_descriptor(DS);
1.1       root     9030:                REG16(BX) = ofs;
                   9031:        } else {
                   9032:                REG8(AL) = 0xff;
1.1.1.3   root     9033:                m_CF = 1;
1.1       root     9034:        }
                   9035: }
                   9036: 
                   9037: inline void msdos_int_21h_33h()
                   9038: {
                   9039:        char path[MAX_PATH];
                   9040:        
                   9041:        switch(REG8(AL)) {
                   9042:        case 0x00:
1.1.1.33  root     9043:                REG8(DL) = ctrl_break_checking;
1.1       root     9044:                break;
                   9045:        case 0x01:
1.1.1.33  root     9046:                ctrl_break_checking = REG8(DL);
                   9047:                break;
                   9048:        case 0x02:
                   9049:                {
                   9050:                        UINT8 old = ctrl_break_checking;
                   9051:                        ctrl_break_checking = REG8(DL);
                   9052:                        REG8(DL) = old;
                   9053:                }
                   9054:                break;
                   9055:        case 0x03:
                   9056:        case 0x04:
                   9057:                // DOS 4.0+ - Unused
1.1       root     9058:                break;
                   9059:        case 0x05:
                   9060:                GetSystemDirectory(path, MAX_PATH);
                   9061:                if(path[0] >= 'a' && path[0] <= 'z') {
                   9062:                        REG8(DL) = path[0] - 'a' + 1;
                   9063:                } else {
                   9064:                        REG8(DL) = path[0] - 'A' + 1;
                   9065:                }
                   9066:                break;
                   9067:        case 0x06:
1.1.1.2   root     9068:                // MS-DOS version (7.10)
1.1       root     9069:                REG8(BL) = 7;
1.1.1.2   root     9070:                REG8(BH) = 10;
1.1       root     9071:                REG8(DL) = 0;
1.1.1.29  root     9072: #ifdef SUPPORT_HMA
                   9073:                REG8(DH) = 0x00;
                   9074: #else
                   9075:                REG8(DH) = 0x10; // DOS is in HMA
                   9076: #endif
1.1       root     9077:                break;
1.1.1.6   root     9078:        case 0x07:
                   9079:                if(REG8(DL) == 0) {
                   9080:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag &= ~0x20;
                   9081:                } else if(REG8(DL) == 1) {
                   9082:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag |= 0x20;
                   9083:                }
                   9084:                break;
1.1       root     9085:        default:
1.1.1.22  root     9086:                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     9087:                REG16(AX) = 0x01;
1.1.1.3   root     9088:                m_CF = 1;
1.1       root     9089:                break;
                   9090:        }
                   9091: }
                   9092: 
1.1.1.23  root     9093: inline void msdos_int_21h_34h()
                   9094: {
                   9095:        SREG(ES) = SDA_TOP >> 4;
                   9096:        i386_load_segment_descriptor(ES);
                   9097:        REG16(BX) = offsetof(sda_t, indos_flag);;
                   9098: }
                   9099: 
1.1       root     9100: inline void msdos_int_21h_35h()
                   9101: {
                   9102:        REG16(BX) = *(UINT16 *)(mem + 4 * REG8(AL) + 0);
1.1.1.3   root     9103:        SREG(ES) = *(UINT16 *)(mem + 4 * REG8(AL) + 2);
                   9104:        i386_load_segment_descriptor(ES);
1.1       root     9105: }
                   9106: 
                   9107: inline void msdos_int_21h_36h()
                   9108: {
                   9109:        struct _diskfree_t df = {0};
                   9110:        
                   9111:        if(_getdiskfree(REG8(DL), &df) == 0) {
                   9112:                REG16(AX) = (UINT16)df.sectors_per_cluster;
                   9113:                REG16(CX) = (UINT16)df.bytes_per_sector;
1.1.1.13  root     9114:                REG16(BX) = df.avail_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.avail_clusters;
                   9115:                REG16(DX) = df.total_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.total_clusters;
1.1       root     9116:        } else {
                   9117:                REG16(AX) = 0xffff;
                   9118:        }
                   9119: }
                   9120: 
                   9121: inline void msdos_int_21h_37h()
                   9122: {
1.1.1.22  root     9123:        static UINT8 dev_flag = 0xff;
1.1       root     9124:        
                   9125:        switch(REG8(AL)) {
                   9126:        case 0x00:
1.1.1.22  root     9127:                {
                   9128:                        process_t *process = msdos_process_info_get(current_psp);
                   9129:                        REG8(AL) = 0x00;
                   9130:                        REG8(DL) = process->switchar;
                   9131:                }
1.1       root     9132:                break;
                   9133:        case 0x01:
1.1.1.22  root     9134:                {
                   9135:                        process_t *process = msdos_process_info_get(current_psp);
                   9136:                        REG8(AL) = 0x00;
                   9137:                        process->switchar = REG8(DL);
1.1.1.23  root     9138:                        msdos_sda_update(current_psp);
1.1.1.22  root     9139:                }
                   9140:                break;
                   9141:        case 0x02:
                   9142:                REG8(DL) = dev_flag;
                   9143:                break;
                   9144:        case 0x03:
                   9145:                dev_flag = REG8(DL);
                   9146:                break;
                   9147:        case 0xd0:
                   9148:        case 0xd1:
                   9149:        case 0xd2:
                   9150:        case 0xd3:
                   9151:        case 0xd4:
                   9152:        case 0xd5:
                   9153:        case 0xd6:
                   9154:        case 0xd7:
                   9155:        case 0xdc:
                   9156:        case 0xdd:
                   9157:        case 0xde:
                   9158:        case 0xdf:
                   9159:                // diet ???
                   9160:                REG16(AX) = 1;
1.1       root     9161:                break;
                   9162:        default:
1.1.1.22  root     9163:                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     9164:                REG16(AX) = 1;
                   9165:                break;
                   9166:        }
                   9167: }
                   9168: 
1.1.1.19  root     9169: int get_country_info(country_info_t *ci)
1.1.1.17  root     9170: {
                   9171:        char LCdata[80];
                   9172:        
1.1.1.19  root     9173:        ZeroMemory(ci, offsetof(country_info_t, reserved));
1.1.1.17  root     9174:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRDIGITS, LCdata, sizeof(LCdata));
                   9175:        ci->currency_dec_digits = atoi(LCdata);
                   9176:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRENCY, LCdata, sizeof(LCdata));
                   9177:        ci->currency_format = *LCdata - '0';
                   9178:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IDATE, LCdata, sizeof(LCdata));
                   9179:        ci->date_format = *LCdata - '0';
                   9180:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SCURRENCY, LCdata, sizeof(LCdata));
                   9181:        memcpy(&ci->currency_symbol, LCdata, 4);
                   9182:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDATE, LCdata, sizeof(LCdata));
                   9183:        *ci->date_sep = *LCdata;
                   9184:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, LCdata, sizeof(LCdata));
                   9185:        *ci->dec_sep = *LCdata;
                   9186:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SLIST, LCdata, sizeof(LCdata));
                   9187:        *ci->list_sep = *LCdata;
                   9188:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, LCdata, sizeof(LCdata));
                   9189:        *ci->thou_sep = *LCdata;
                   9190:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIME, LCdata, sizeof(LCdata));
                   9191:        *ci->time_sep = *LCdata;
                   9192:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, LCdata, sizeof(LCdata));
                   9193:        if(strchr(LCdata, 'H') != NULL) {
                   9194:                ci->time_format = 1;
                   9195:        }
1.1.1.27  root     9196:        ci->case_map.w.l = 0x000a; // dummy case map routine is at fffd:000a
1.1.1.24  root     9197:        ci->case_map.w.h = 0xfffd;
1.1.1.17  root     9198:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICOUNTRY, LCdata, sizeof(LCdata));
                   9199:        return atoi(LCdata);
                   9200: }
                   9201: 
1.1.1.14  root     9202: inline void msdos_int_21h_38h()
                   9203: {
                   9204:        switch(REG8(AL)) {
                   9205:        case 0x00:
1.1.1.19  root     9206:                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1.1.14  root     9207:                break;
                   9208:        default:
1.1.1.22  root     9209:                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     9210:                REG16(AX) = 2;
                   9211:                m_CF = 1;
                   9212:                break;
                   9213:        }
                   9214: }
                   9215: 
1.1       root     9216: inline void msdos_int_21h_39h(int lfn)
                   9217: {
1.1.1.3   root     9218:        if(_mkdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     9219:                REG16(AX) = errno;
1.1.1.3   root     9220:                m_CF = 1;
1.1       root     9221:        }
                   9222: }
                   9223: 
                   9224: inline void msdos_int_21h_3ah(int lfn)
                   9225: {
1.1.1.3   root     9226:        if(_rmdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     9227:                REG16(AX) = errno;
1.1.1.3   root     9228:                m_CF = 1;
1.1       root     9229:        }
                   9230: }
                   9231: 
                   9232: inline void msdos_int_21h_3bh(int lfn)
                   9233: {
1.1.1.3   root     9234:        if(_chdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1.1.17  root     9235:                REG16(AX) = 3;  // must be 3 (path not found)
1.1.1.3   root     9236:                m_CF = 1;
1.1       root     9237:        }
                   9238: }
                   9239: 
                   9240: inline void msdos_int_21h_3ch()
                   9241: {
1.1.1.3   root     9242:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     9243:        int attr = GetFileAttributes(path);
1.1.1.37! root     9244:        int fd = -1;
1.1.1.11  root     9245:        UINT16 info;
1.1.1.37! root     9246:        int sio_port = 0;
        !          9247:        int lpt_port = 0;
1.1       root     9248:        
1.1.1.11  root     9249:        if(msdos_is_con_path(path)) {
                   9250:                fd = _open("CON", _O_WRONLY | _O_BINARY);
                   9251:                info = 0x80d3;
1.1.1.37! root     9252:        } else if((sio_port = msdos_is_comm_path(path)) != 0) {
        !          9253:                fd = _open("NUL", _O_WRONLY | _O_BINARY);
1.1.1.14  root     9254:                info = 0x80d3;
1.1.1.37! root     9255:                msdos_set_comm_params(sio_port, path);
        !          9256:        } else if((lpt_port = msdos_is_prn_path(path)) != 0) {
        !          9257:                fd = _open("NUL", _O_WRONLY | _O_BINARY);
        !          9258:                info = 0xa8c0;
1.1.1.29  root     9259:        } else if(msdos_is_device_path(path)) {
1.1.1.20  root     9260:                fd = _open("NUL", _O_WRONLY | _O_BINARY);
                   9261:                info = 0x80d3;
1.1       root     9262:        } else {
                   9263:                fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
1.1.1.11  root     9264:                info = msdos_drive_number(path);
1.1       root     9265:        }
                   9266:        if(fd != -1) {
                   9267:                if(attr == -1) {
                   9268:                        attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   9269:                }
                   9270:                SetFileAttributes(path, attr);
                   9271:                REG16(AX) = fd;
1.1.1.37! root     9272:                msdos_file_handler_open(fd, path, _isatty(fd), 2, info, current_psp, sio_port, lpt_port);
1.1.1.20  root     9273:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     9274:        } else {
                   9275:                REG16(AX) = errno;
1.1.1.3   root     9276:                m_CF = 1;
1.1       root     9277:        }
                   9278: }
                   9279: 
                   9280: inline void msdos_int_21h_3dh()
                   9281: {
1.1.1.3   root     9282:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     9283:        int mode = REG8(AL) & 0x03;
1.1.1.37! root     9284:        int fd = -1;
1.1.1.11  root     9285:        UINT16 info;
1.1.1.37! root     9286:        int sio_port = 0;
        !          9287:        int lpt_port = 0;
1.1       root     9288:        
                   9289:        if(mode < 0x03) {
1.1.1.11  root     9290:                if(msdos_is_con_path(path)) {
1.1.1.13  root     9291:                        fd = msdos_open("CON", file_mode[mode].mode);
1.1.1.11  root     9292:                        info = 0x80d3;
1.1.1.37! root     9293:                } else if((sio_port = msdos_is_comm_path(path)) != 0) {
        !          9294:                        fd = msdos_open("NUL", file_mode[mode].mode);
1.1.1.14  root     9295:                        info = 0x80d3;
1.1.1.37! root     9296:                        msdos_set_comm_params(sio_port, path);
        !          9297:                } else if((lpt_port = msdos_is_prn_path(path)) != 0) {
        !          9298:                        fd = _open("NUL", file_mode[mode].mode);
        !          9299:                        info = 0xa8c0;
1.1.1.29  root     9300:                } else if(msdos_is_device_path(path)) {
1.1.1.20  root     9301:                        fd = msdos_open("NUL", file_mode[mode].mode);
                   9302:                        info = 0x80d3;
1.1.1.11  root     9303:                } else {
1.1.1.13  root     9304:                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     9305:                        info = msdos_drive_number(path);
                   9306:                }
1.1       root     9307:                if(fd != -1) {
                   9308:                        REG16(AX) = fd;
1.1.1.37! root     9309:                        msdos_file_handler_open(fd, path, _isatty(fd), mode, info, current_psp, sio_port, lpt_port);
1.1.1.20  root     9310:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     9311:                } else {
                   9312:                        REG16(AX) = errno;
1.1.1.3   root     9313:                        m_CF = 1;
1.1       root     9314:                }
                   9315:        } else {
                   9316:                REG16(AX) = 0x0c;
1.1.1.3   root     9317:                m_CF = 1;
1.1       root     9318:        }
                   9319: }
                   9320: 
                   9321: inline void msdos_int_21h_3eh()
                   9322: {
                   9323:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     9324:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     9325:        
1.1.1.20  root     9326:        if(fd < process->max_files && file_handler[fd].valid) {
                   9327:                _close(fd);
                   9328:                msdos_file_handler_close(fd);
                   9329:                msdos_psp_set_file_table(REG16(BX), 0x0ff, current_psp);
1.1       root     9330:        } else {
                   9331:                REG16(AX) = 0x06;
1.1.1.3   root     9332:                m_CF = 1;
1.1       root     9333:        }
                   9334: }
                   9335: 
1.1.1.35  root     9336: DWORD WINAPI msdos_int_21h_3fh_thread(LPVOID)
                   9337: {
                   9338:        UINT8 *buf = mem + SREG_BASE(DS) + REG16(DX);
                   9339:        int max = REG16(CX);
                   9340:        int p = 0;
                   9341:        
                   9342:        while(max > p) {
                   9343:                int chr = msdos_getch();
                   9344:                
                   9345:                if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
                   9346:                        p = 0;
                   9347:                        buf[p++] = 0x0d;
                   9348:                        if(max > p) {
                   9349:                                buf[p++] = 0x0a;
                   9350:                        }
                   9351:                        msdos_putch(0x03);
                   9352:                        msdos_putch(0x0d);
                   9353:                        msdos_putch(0x0a);
                   9354:                        break;
                   9355:                } else if(ctrl_break_pressed) {
                   9356:                        // skip this byte
                   9357:                } else if(chr == 0x00) {
                   9358:                        // skip 2nd byte
                   9359:                        msdos_getch();
                   9360:                } else if(chr == 0x0d) {
                   9361:                        // carriage return
                   9362:                        buf[p++] = 0x0d;
                   9363:                        if(max > p) {
                   9364:                                buf[p++] = 0x0a;
                   9365:                        }
                   9366:                        msdos_putch('\n');
                   9367:                        break;
                   9368:                } else if(chr == 0x08) {
                   9369:                        // back space
                   9370:                        if(p > 0) {
                   9371:                                p--;
                   9372:                                if(msdos_ctrl_code_check(buf[p])) {
                   9373:                                        msdos_putch(0x08);
                   9374:                                        msdos_putch(0x08);
                   9375:                                        msdos_putch(0x20);
                   9376:                                        msdos_putch(0x20);
                   9377:                                        msdos_putch(0x08);
                   9378:                                        msdos_putch(0x08);
1.1.1.36  root     9379:                                } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
                   9380:                                        p--;
                   9381:                                        msdos_putch(0x08);
                   9382:                                        msdos_putch(0x08);
                   9383:                                        msdos_putch(0x20);
                   9384:                                        msdos_putch(0x20);
                   9385:                                        msdos_putch(0x08);
                   9386:                                        msdos_putch(0x08);
1.1.1.35  root     9387:                                } else {
                   9388:                                        msdos_putch(0x08);
                   9389:                                        msdos_putch(0x20);
                   9390:                                        msdos_putch(0x08);
                   9391:                                }
                   9392:                        }
                   9393:                } else if(chr == 0x1b) {
                   9394:                        // escape
                   9395:                        while(p > 0) {
                   9396:                                p--;
                   9397:                                if(msdos_ctrl_code_check(buf[p])) {
                   9398:                                        msdos_putch(0x08);
                   9399:                                        msdos_putch(0x08);
                   9400:                                        msdos_putch(0x20);
                   9401:                                        msdos_putch(0x20);
                   9402:                                        msdos_putch(0x08);
                   9403:                                        msdos_putch(0x08);
                   9404:                                } else {
                   9405:                                        msdos_putch(0x08);
                   9406:                                        msdos_putch(0x20);
                   9407:                                        msdos_putch(0x08);
                   9408:                                }
                   9409:                        }
                   9410:                } else {
                   9411:                        buf[p++] = chr;
                   9412:                        msdos_putch(chr);
                   9413:                }
                   9414:        }
                   9415:        REG16(AX) = p;
                   9416:        
                   9417: #ifdef USE_SERVICE_THREAD
                   9418:        service_exit = true;
                   9419: #endif
                   9420:        return(0);
                   9421: }
                   9422: 
1.1       root     9423: inline void msdos_int_21h_3fh()
                   9424: {
                   9425:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     9426:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     9427:        
1.1.1.20  root     9428:        if(fd < process->max_files && file_handler[fd].valid) {
                   9429:                if(file_mode[file_handler[fd].mode].in) {
                   9430:                        if(file_handler[fd].atty) {
1.1       root     9431:                                // BX is stdin or is redirected to stdin
1.1.1.35  root     9432:                                if(REG16(CX) != 0) {
                   9433: #ifdef USE_SERVICE_THREAD
                   9434:                                        start_service_loop(msdos_int_21h_3fh_thread);
                   9435: #else
                   9436:                                        msdos_int_21h_3fh_thread(NULL);
                   9437:                                        REQUEST_HARDWRE_UPDATE();
                   9438: #endif
                   9439:                                } else {
                   9440:                                        REG16(AX) = 0;
1.1       root     9441:                                }
                   9442:                        } else {
1.1.1.37! root     9443:                                REG16(AX) = msdos_read(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     9444:                        }
                   9445:                } else {
                   9446:                        REG16(AX) = 0x05;
1.1.1.3   root     9447:                        m_CF = 1;
1.1       root     9448:                }
                   9449:        } else {
                   9450:                REG16(AX) = 0x06;
1.1.1.3   root     9451:                m_CF = 1;
1.1       root     9452:        }
                   9453: }
                   9454: 
                   9455: inline void msdos_int_21h_40h()
                   9456: {
                   9457:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     9458:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     9459:        
1.1.1.20  root     9460:        if(fd < process->max_files && file_handler[fd].valid) {
                   9461:                if(file_mode[file_handler[fd].mode].out) {
1.1       root     9462:                        if(REG16(CX)) {
1.1.1.20  root     9463:                                if(file_handler[fd].atty) {
1.1       root     9464:                                        // BX is stdout/stderr or is redirected to stdout
                   9465:                                        for(int i = 0; i < REG16(CX); i++) {
1.1.1.3   root     9466:                                                msdos_putch(mem[SREG_BASE(DS) + REG16(DX) + i]);
1.1       root     9467:                                        }
                   9468:                                        REG16(AX) = REG16(CX);
                   9469:                                } else {
1.1.1.20  root     9470:                                        REG16(AX) = msdos_write(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     9471:                                }
                   9472:                        } else {
1.1.1.20  root     9473:                                UINT32 pos = _tell(fd);
                   9474:                                _lseek(fd, 0, SEEK_END);
                   9475:                                UINT32 size = _tell(fd);
1.1.1.12  root     9476:                                if(pos < size) {
1.1.1.20  root     9477:                                        _lseek(fd, pos, SEEK_SET);
                   9478:                                        SetEndOfFile((HANDLE)_get_osfhandle(fd));
1.1.1.12  root     9479:                                } else {
                   9480:                                        for(UINT32 i = size; i < pos; i++) {
                   9481:                                                UINT8 tmp = 0;
1.1.1.23  root     9482:                                                msdos_write(fd, &tmp, 1);
1.1.1.12  root     9483:                                        }
1.1.1.20  root     9484:                                        _lseek(fd, pos, SEEK_SET);
1.1       root     9485:                                }
1.1.1.23  root     9486:                                REG16(AX) = 0;
1.1       root     9487:                        }
                   9488:                } else {
                   9489:                        REG16(AX) = 0x05;
1.1.1.3   root     9490:                        m_CF = 1;
1.1       root     9491:                }
                   9492:        } else {
                   9493:                REG16(AX) = 0x06;
1.1.1.3   root     9494:                m_CF = 1;
1.1       root     9495:        }
                   9496: }
                   9497: 
                   9498: inline void msdos_int_21h_41h(int lfn)
                   9499: {
1.1.1.3   root     9500:        if(remove(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     9501:                REG16(AX) = errno;
1.1.1.3   root     9502:                m_CF = 1;
1.1       root     9503:        }
                   9504: }
                   9505: 
                   9506: inline void msdos_int_21h_42h()
                   9507: {
                   9508:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     9509:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     9510:        
1.1.1.20  root     9511:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     9512:                if(REG8(AL) < 0x03) {
1.1.1.35  root     9513:                        static const int ptrname[] = { SEEK_SET, SEEK_CUR, SEEK_END };
1.1.1.20  root     9514:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), ptrname[REG8(AL)]);
                   9515:                        UINT32 pos = _tell(fd);
1.1       root     9516:                        REG16(AX) = pos & 0xffff;
                   9517:                        REG16(DX) = (pos >> 16);
                   9518:                } else {
                   9519:                        REG16(AX) = 0x01;
1.1.1.3   root     9520:                        m_CF = 1;
1.1       root     9521:                }
                   9522:        } else {
                   9523:                REG16(AX) = 0x06;
1.1.1.3   root     9524:                m_CF = 1;
1.1       root     9525:        }
                   9526: }
                   9527: 
                   9528: inline void msdos_int_21h_43h(int lfn)
                   9529: {
1.1.1.3   root     9530:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1       root     9531:        int attr;
                   9532:        
1.1.1.14  root     9533:        if(!lfn && REG8(AL) > 2) {
                   9534:                REG16(AX) = 0x01;
                   9535:                m_CF = 1;
                   9536:                return;
                   9537:        }
                   9538:        switch(REG8(lfn ? BL : AL)) {
1.1       root     9539:        case 0x00:
                   9540:                if((attr = GetFileAttributes(path)) != -1) {
1.1.1.14  root     9541:                        REG16(CX) = (UINT16)msdos_file_attribute_create((UINT16)attr);
                   9542:                } else {
                   9543:                        REG16(AX) = (UINT16)GetLastError();
                   9544:                        m_CF = 1;
                   9545:                }
                   9546:                break;
                   9547:        case 0x01:
                   9548:                if(!SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)))) {
                   9549:                        REG16(AX) = (UINT16)GetLastError();
                   9550:                        m_CF = 1;
                   9551:                }
                   9552:                break;
                   9553:        case 0x02:
                   9554:                {
                   9555:                        DWORD size = GetCompressedFileSize(path, NULL);
                   9556:                        if(size != INVALID_FILE_SIZE) {
                   9557:                                if(size != 0 && size == GetFileSize(path, NULL)) {
                   9558:                                        DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   9559:                                        // this isn't correct if the file is in the NTFS MFT
                   9560:                                        if(GetDiskFreeSpace(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
                   9561:                                                size = ((size - 1) | (sectors_per_cluster * bytes_per_sector - 1)) + 1;
                   9562:                                        }
                   9563:                                }
                   9564:                                REG16(AX) = LOWORD(size);
                   9565:                                REG16(DX) = HIWORD(size);
                   9566:                        } else {
                   9567:                                REG16(AX) = (UINT16)GetLastError();
                   9568:                                m_CF = 1;
1.1       root     9569:                        }
1.1.1.14  root     9570:                }
                   9571:                break;
                   9572:        case 0x03:
                   9573:        case 0x05:
                   9574:        case 0x07:
                   9575:                {
                   9576:                        HANDLE hFile = CreateFile(path, FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   9577:                        if(hFile != INVALID_HANDLE_VALUE) {
                   9578:                                FILETIME local, time;
                   9579:                                DosDateTimeToFileTime(REG16(DI), /*REG8(BL) == 5 ? 0 : */REG16(CX), &local);
                   9580:                                if(REG8(BL) == 7) {
                   9581:                                        ULARGE_INTEGER hund;
                   9582:                                        hund.LowPart = local.dwLowDateTime;
                   9583:                                        hund.HighPart = local.dwHighDateTime;
                   9584:                                        hund.QuadPart += REG16(SI) * 100000;
                   9585:                                        local.dwLowDateTime = hund.LowPart;
                   9586:                                        local.dwHighDateTime = hund.HighPart;
                   9587:                                }
                   9588:                                LocalFileTimeToFileTime(&local, &time);
                   9589:                                if(!SetFileTime(hFile, REG8(BL) == 0x07 ? &time : NULL,
                   9590:                                                       REG8(BL) == 0x05 ? &time : NULL,
                   9591:                                                       REG8(BL) == 0x03 ? &time : NULL)) {
                   9592:                                        REG16(AX) = (UINT16)GetLastError();
                   9593:                                        m_CF = 1;
                   9594:                                }
                   9595:                                CloseHandle(hFile);
                   9596:                        } else {
                   9597:                                REG16(AX) = (UINT16)GetLastError();
                   9598:                                m_CF = 1;
1.1       root     9599:                        }
1.1.1.14  root     9600:                }
                   9601:                break;
                   9602:        case 0x04:
                   9603:        case 0x06:
                   9604:        case 0x08:
                   9605:                {
                   9606:                        WIN32_FILE_ATTRIBUTE_DATA fad;
                   9607:                        if(GetFileAttributesEx(path, GetFileExInfoStandard, (LPVOID)&fad)) {
                   9608:                                FILETIME *time, local;
                   9609:                                time = REG8(BL) == 0x04 ? &fad.ftLastWriteTime :
                   9610:                                                   0x06 ? &fad.ftLastAccessTime :
                   9611:                                                          &fad.ftCreationTime;
                   9612:                                FileTimeToLocalFileTime(time, &local);
                   9613:                                FileTimeToDosDateTime(&local, &REG16(DI), &REG16(CX));
                   9614:                                if(REG8(BL) == 0x08) {
                   9615:                                        ULARGE_INTEGER hund;
                   9616:                                        hund.LowPart = local.dwLowDateTime;
                   9617:                                        hund.HighPart = local.dwHighDateTime;
                   9618:                                        hund.QuadPart /= 100000;
                   9619:                                        REG16(SI) = (UINT16)(hund.QuadPart % 200);
                   9620:                                }
                   9621:                        } else {
                   9622:                                REG16(AX) = (UINT16)GetLastError();
                   9623:                                m_CF = 1;
1.1       root     9624:                        }
1.1.1.14  root     9625:                }
                   9626:                break;
                   9627:        default:
1.1.1.22  root     9628:                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     9629:                REG16(AX) = 0x01;
                   9630:                m_CF = 1;
                   9631:                break;
                   9632:        }
                   9633: }
                   9634: 
                   9635: inline void msdos_int_21h_44h()
                   9636: {
1.1.1.22  root     9637:        static UINT16 iteration_count = 0;
                   9638:        
1.1.1.20  root     9639:        process_t *process = msdos_process_info_get(current_psp);
                   9640:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   9641:        
1.1.1.14  root     9642:        UINT32 val = DRIVE_NO_ROOT_DIR;
                   9643:        
                   9644:        switch(REG8(AL)) {
                   9645:        case 0x00:
                   9646:        case 0x01:
                   9647:        case 0x02:
                   9648:        case 0x03:
                   9649:        case 0x04:
                   9650:        case 0x05:
                   9651:        case 0x06:
                   9652:        case 0x07:
1.1.1.20  root     9653:                if(fd >= process->max_files || !file_handler[fd].valid) {
                   9654:                        REG16(AX) = 0x06;
                   9655:                        m_CF = 1;
                   9656:                        return;
1.1.1.14  root     9657:                }
                   9658:                break;
                   9659:        case 0x08:
                   9660:        case 0x09:
                   9661:                if(REG8(BL) >= ('Z' - 'A' + 1)) {
                   9662:                        // invalid drive number
                   9663:                        REG16(AX) = 0x0f;
                   9664:                        m_CF = 1;
                   9665:                        return;
                   9666:                } else {
                   9667:                        if(REG8(BL) == 0) {
                   9668:                                val = GetDriveType(NULL);
                   9669:                        } else {
                   9670:                                char tmp[8];
                   9671:                                sprintf(tmp, "%c:\\", 'A' + REG8(BL) - 1);
                   9672:                                val = GetDriveType(tmp);
                   9673:                        }
                   9674:                        if(val == DRIVE_NO_ROOT_DIR) {
                   9675:                                // no drive
                   9676:                                REG16(AX) = 0x0f;
                   9677:                                m_CF = 1;
                   9678:                                return;
1.1       root     9679:                        }
                   9680:                }
                   9681:                break;
                   9682:        }
                   9683:        switch(REG8(AL)) {
                   9684:        case 0x00: // get ioctrl data
1.1.1.20  root     9685:                REG16(DX) = file_handler[fd].info;
1.1       root     9686:                break;
                   9687:        case 0x01: // set ioctrl data
1.1.1.20  root     9688:                file_handler[fd].info |= REG8(DL);
1.1       root     9689:                break;
                   9690:        case 0x02: // recv from character device
                   9691:        case 0x03: // send to character device
                   9692:        case 0x04: // recv from block device
                   9693:        case 0x05: // send to block device
                   9694:                REG16(AX) = 0x05;
1.1.1.3   root     9695:                m_CF = 1;
1.1       root     9696:                break;
                   9697:        case 0x06: // get read status
1.1.1.20  root     9698:                if(file_mode[file_handler[fd].mode].in) {
                   9699:                        if(file_handler[fd].atty) {
1.1.1.14  root     9700:                                REG8(AL) = msdos_kbhit() ? 0xff : 0x00;
1.1       root     9701:                        } else {
1.1.1.20  root     9702:                                REG8(AL) = eof(fd) ? 0x00 : 0xff;
1.1       root     9703:                        }
1.1.1.14  root     9704:                } else {
                   9705:                        REG8(AL) = 0x00;
1.1       root     9706:                }
                   9707:                break;
                   9708:        case 0x07: // get write status
1.1.1.20  root     9709:                if(file_mode[file_handler[fd].mode].out) {
1.1.1.14  root     9710:                        REG8(AL) = 0xff;
                   9711:                } else {
                   9712:                        REG8(AL) = 0x00;
1.1       root     9713:                }
                   9714:                break;
                   9715:        case 0x08: // check removable drive
1.1.1.14  root     9716:                if(val == DRIVE_REMOVABLE || val == DRIVE_CDROM) {
                   9717:                        // removable drive
                   9718:                        REG16(AX) = 0x00;
1.1       root     9719:                } else {
1.1.1.14  root     9720:                        // fixed drive
                   9721:                        REG16(AX) = 0x01;
1.1       root     9722:                }
                   9723:                break;
                   9724:        case 0x09: // check remote drive
1.1.1.14  root     9725:                if(val == DRIVE_REMOTE) {
                   9726:                        // remote drive
                   9727:                        REG16(DX) = 0x1000;
1.1       root     9728:                } else {
1.1.1.14  root     9729:                        // local drive
                   9730:                        REG16(DX) = 0x00;
1.1       root     9731:                }
                   9732:                break;
1.1.1.21  root     9733:        case 0x0a: // check remote handle
                   9734:                REG16(DX) = 0x00; // FIXME
                   9735:                break;
1.1       root     9736:        case 0x0b: // set retry count
                   9737:                break;
1.1.1.22  root     9738:        case 0x0c: // generic character device request
                   9739:                if(REG8(CL) == 0x45) {
                   9740:                        // set iteration (retry) count
                   9741:                        iteration_count = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
                   9742:                } else if(REG8(CL) == 0x4a) {
                   9743:                        // select code page
                   9744:                        active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2);
                   9745:                        msdos_nls_tables_update();
                   9746:                } else if(REG8(CL) == 0x65) {
                   9747:                        // get iteration (retry) count
                   9748:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX)) = iteration_count;
                   9749:                } else if(REG8(CL) == 0x6a) {
                   9750:                        // query selected code page
                   9751:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2; // FIXME
                   9752:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
                   9753:                        
                   9754:                        CPINFO info;
                   9755:                        GetCPInfo(active_code_page, &info);
                   9756:                        
                   9757:                        if(info.MaxCharSize != 1) {
                   9758:                                for(int i = 0;; i++) {
                   9759:                                        UINT8 lo = info.LeadByte[2 * i + 0];
                   9760:                                        UINT8 hi = info.LeadByte[2 * i + 1];
                   9761:                                        
                   9762:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 0) = lo;
                   9763:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 2) = hi;
                   9764:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) + 4;
                   9765:                                        
                   9766:                                        if(lo == 0 && hi == 0) {
                   9767:                                                break;
                   9768:                                        }
                   9769:                                }
                   9770:                        }
                   9771:                } else if(REG8(CL) == 0x7f) {
                   9772:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00) = 0;
                   9773:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x01) = 0;
                   9774:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) = 14;
                   9775:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04) = 1;
                   9776:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06) = 1;
                   9777:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x07) = 0;
                   9778:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08) = 4;
                   9779:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a) =  8 * (*(UINT16 *)(mem + 0x44a));
                   9780:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c) = 16 * (*(UINT8  *)(mem + 0x484) + 1);
                   9781:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e) = *(UINT16 *)(mem + 0x44a);
                   9782:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10) = *(UINT8  *)(mem + 0x484) + 1;
                   9783:                } else {
                   9784:                        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));
                   9785:                        REG16(AX) = 0x01; // invalid function
                   9786:                        m_CF = 1;
                   9787:                }
                   9788:                break;
                   9789:        case 0x0d: // generic block device request
                   9790:                if(REG8(CL) == 0x40) {
                   9791:                        // set device parameters
                   9792:                } else if(REG8(CL) == 0x46) {
                   9793:                        // set volume serial number
                   9794:                } else if(REG8(CL) == 0x4a) {
                   9795:                        // lock logical volume
                   9796:                } else if(REG8(CL) == 0x4b) {
                   9797:                        // lock physical volume
                   9798:                } else if(REG8(CL) == 0x60) {
                   9799:                        // get device parameters
                   9800:                        char dev[] = "\\\\.\\A:";
                   9801:                        dev[4] = 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   9802:                        
                   9803:                        HANDLE hFile = CreateFile(dev, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   9804:                        if(hFile != INVALID_HANDLE_VALUE) {
                   9805:                                DISK_GEOMETRY geo;
                   9806:                                DWORD dwSize;
                   9807:                                if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &geo, sizeof(geo), &dwSize, NULL)) {
                   9808:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x07; // ???
                   9809:                                        switch(geo.MediaType) {
                   9810:                                        case F5_360_512:
                   9811:                                        case F5_320_512:
                   9812:                                        case F5_320_1024:
                   9813:                                        case F5_180_512:
                   9814:                                        case F5_160_512:
                   9815:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // 320K/360K disk
                   9816:                                                break;
                   9817:                                        case F5_1Pt2_512:
                   9818:                                        case F3_1Pt2_512:
                   9819:                                        case F3_1Pt23_1024:
                   9820:                                        case F5_1Pt23_1024:
                   9821:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x01; // 1.2M disk
                   9822:                                                break;
                   9823:                                        case F3_720_512:
                   9824:                                        case F3_640_512:
                   9825:                                        case F5_640_512:
                   9826:                                        case F5_720_512:
                   9827:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02; // 720K disk
                   9828:                                                break;
                   9829:                                        case F8_256_128:
                   9830:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x03; // single-density 8-inch disk
                   9831:                                                break;
                   9832:                                        case FixedMedia:
                   9833:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   9834:                                                break;
                   9835:                                        case F3_1Pt44_512:
                   9836:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   9837:                                                break;
                   9838:                                        case F3_2Pt88_512:
                   9839:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09; // (DOS 5+) 2.88M floppy
                   9840:                                                break;
                   9841:                                        default:
                   9842:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   9843: //                                             *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   9844:                                                break;
                   9845:                                        }
                   9846:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = (geo.MediaType == FixedMedia) ? 0x01 : 0x00;
                   9847:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04) = (geo.Cylinders.QuadPart > 0xffff) ? 0xffff : geo.Cylinders.QuadPart;
                   9848:                                        switch(geo.MediaType) {
                   9849:                                        case F5_360_512:
                   9850:                                        case F5_320_512:
                   9851:                                        case F5_320_1024:
                   9852:                                        case F5_180_512:
                   9853:                                        case F5_160_512:
                   9854:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x01; // 320K/360K disk
                   9855:                                                break;
                   9856:                                        default:
                   9857:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x00; // other drive types
                   9858:                                                break;
                   9859:                                        }
                   9860:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x00) = geo.BytesPerSector;
                   9861:                                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x02) = geo.SectorsPerTrack * geo.TracksPerCylinder;
                   9862:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x03) = 0;
                   9863:                                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x05) = 0;
                   9864:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x06) = 0;
                   9865:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x08) = 0;
                   9866:                                        switch(geo.MediaType) {
                   9867:                                        case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   9868:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xff;
                   9869:                                                break;
                   9870:                                        case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   9871:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfe;
                   9872:                                                break;
                   9873:                                        case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   9874:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfd;
                   9875:                                                break;
                   9876:                                        case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   9877:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfc;
                   9878:                                                break;
                   9879:                                        case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   9880:                                        case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   9881:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf9;
                   9882:                                                break;
                   9883:                                        case FixedMedia:        // hard disk
                   9884:                                        case RemovableMedia:
                   9885:                                        case Unknown:
                   9886:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf8;
                   9887:                                                break;
                   9888:                                        default:
                   9889:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf0;
                   9890:                                                break;
                   9891:                                        }
                   9892:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0d) = geo.SectorsPerTrack;
                   9893:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0f) = 1;
                   9894:                                        *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x11) = 0;
                   9895:                                        *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x15) = geo.TracksPerCylinder * geo.Cylinders.QuadPart;
                   9896:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x1f) = geo.Cylinders.QuadPart;
                   9897:                                        // 21h  BYTE    device type
                   9898:                                        // 22h  WORD    device attributes (removable or not, etc)
                   9899:                                } else {
                   9900:                                        REG16(AX) = 0x0f; // invalid drive
                   9901:                                        m_CF = 1;
                   9902:                                }
                   9903:                                CloseHandle(hFile);
                   9904:                        } else {
                   9905:                                REG16(AX) = 0x0f; // invalid drive
                   9906:                                m_CF = 1;
                   9907:                        }
                   9908:                } else if(REG8(CL) == 0x66) {
                   9909:                        // get volume serial number
                   9910:                        char path[] = "A:\\";
                   9911:                        char volume_label[MAX_PATH];
                   9912:                        DWORD serial_number = 0;
                   9913:                        char file_system[MAX_PATH];
                   9914:                        
                   9915:                        path[0] = 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   9916:                        
                   9917:                        if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
                   9918:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   9919:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = serial_number;
                   9920:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x06, 0x20, 11);
                   9921:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x06, volume_label, min(strlen(volume_label), 11));
                   9922:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x11, 0x20,  8);
                   9923:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x11, file_system, min(strlen(file_system), 8));
                   9924:                        } else {
                   9925:                                REG16(AX) = 0x0f; // invalid drive
                   9926:                                m_CF = 1;
                   9927:                        }
                   9928:                } else if(REG8(CL) == 0x67) {
                   9929:                        // get access flag
                   9930:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   9931:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 1;
                   9932:                } else if(REG8(CL) == 0x68) {
                   9933:                        // sense media type
                   9934:                        char dev[64];
                   9935:                        sprintf(dev, "\\\\.\\%c:", 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1);
                   9936:                        
                   9937:                        HANDLE hFile = CreateFile(dev, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   9938:                        if(hFile != INVALID_HANDLE_VALUE) {
                   9939:                                DISK_GEOMETRY geo;
                   9940:                                DWORD dwSize;
                   9941:                                if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &geo, sizeof(geo), &dwSize, NULL)) {
                   9942:                                        switch(geo.MediaType) {
                   9943:                                        case F3_720_512:
                   9944:                                        case F5_720_512:
                   9945:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   9946:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02;
                   9947:                                                break;
                   9948:                                        case F3_1Pt44_512:
                   9949:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   9950:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07;
                   9951:                                                break;
                   9952:                                        case F3_2Pt88_512:
                   9953:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   9954:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09;
                   9955:                                                break;
                   9956:                                        default:
                   9957:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x00;
                   9958:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // ???
                   9959:                                                break;
                   9960:                                        }
                   9961:                                } else {
                   9962:                                        REG16(AX) = 0x0f; // invalid drive
                   9963:                                        m_CF = 1;
                   9964:                                }
                   9965:                                CloseHandle(hFile);
                   9966:                        } else {
                   9967:                                REG16(AX) = 0x0f; // invalid drive
                   9968:                                m_CF = 1;
                   9969:                        }
                   9970:                } else if(REG8(CL) == 0x6a) {
                   9971:                        // unlock logical volume
                   9972:                } else if(REG8(CL) == 0x6b) {
                   9973:                        // unlock physical volume
                   9974:                } else {
                   9975:                        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));
                   9976:                        REG16(AX) = 0x01; // invalid function
                   9977:                        m_CF = 1;
                   9978:                }
                   9979:                break;
                   9980:        case 0x0e: // get logical drive map
                   9981:                {
                   9982:                        DWORD bits = 1 << ((REG8(BL) ? REG8(BL) : _getdrive()) - 1);
                   9983:                        if(!(GetLogicalDrives() & bits)) {
                   9984:                                REG16(AX) = 0x0f; // invalid drive
                   9985:                                m_CF = 1;
                   9986:                        } else {
                   9987:                                REG8(AL) = 0;
                   9988:                        }
                   9989:                }
                   9990:                break;
                   9991:        case 0x0f: // set logical drive map
                   9992:                {
                   9993:                        DWORD bits = 1 << ((REG8(BL) ? REG8(BL) : _getdrive()) - 1);
                   9994:                        if(!(GetLogicalDrives() & bits)) {
                   9995:                                REG16(AX) = 0x0f; // invalid drive
                   9996:                                m_CF = 1;
                   9997:                        }
                   9998:                }
                   9999:                break;
                   10000:        case 0x10: // query generic ioctrl capability (handle)
                   10001:                switch(REG8(CL)) {
                   10002:                case 0x45:
                   10003:                case 0x4a:
                   10004:                case 0x65:
                   10005:                case 0x6a:
                   10006:                case 0x7f:
                   10007:                        REG16(AX) = 0x0000; // supported
                   10008:                        break;
                   10009:                default:
                   10010:                        REG8(AL) = 0x01; // ioctl capability not available
                   10011:                        m_CF = 1;
                   10012:                        break;
                   10013:                }
                   10014:                break;
                   10015:        case 0x11: // query generic ioctrl capability (drive)
                   10016:                switch(REG8(CL)) {
                   10017:                case 0x40:
                   10018:                case 0x46:
                   10019:                case 0x4a:
                   10020:                case 0x4b:
                   10021:                case 0x60:
                   10022:                case 0x66:
                   10023:                case 0x67:
                   10024:                case 0x68:
                   10025:                case 0x6a:
                   10026:                case 0x6b:
                   10027:                        REG16(AX) = 0x0000; // supported
                   10028:                        break;
                   10029:                default:
                   10030:                        REG8(AL) = 0x01; // ioctl capability not available
                   10031:                        m_CF = 1;
                   10032:                        break;
                   10033:                }
                   10034:                break;
                   10035:        case 0x12: // determine dos type
                   10036:        case 0x51: // concurrent dos v3.2+ - installation check
                   10037:        case 0x52: // determine dos type/get dr dos versuin
                   10038:                REG16(AX) = 0x01; // this  is not DR-DOS
                   10039:                m_CF = 1;
                   10040:                break;
1.1       root     10041:        default:
1.1.1.22  root     10042:                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     10043:                REG16(AX) = 0x01;
1.1.1.3   root     10044:                m_CF = 1;
1.1       root     10045:                break;
                   10046:        }
                   10047: }
                   10048: 
                   10049: inline void msdos_int_21h_45h()
                   10050: {
                   10051:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10052:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10053:        
1.1.1.20  root     10054:        if(fd < process->max_files && file_handler[fd].valid) {
                   10055:                int dup_fd = _dup(fd);
                   10056:                if(dup_fd != -1) {
                   10057:                        REG16(AX) = dup_fd;
                   10058:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   10059: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   10060:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     10061:                } else {
                   10062:                        REG16(AX) = errno;
1.1.1.3   root     10063:                        m_CF = 1;
1.1       root     10064:                }
                   10065:        } else {
                   10066:                REG16(AX) = 0x06;
1.1.1.3   root     10067:                m_CF = 1;
1.1       root     10068:        }
                   10069: }
                   10070: 
                   10071: inline void msdos_int_21h_46h()
                   10072: {
                   10073:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10074:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   10075:        int dup_fd = REG16(CX);
                   10076:        int tmp_fd = msdos_psp_get_file_table(REG16(CX), current_psp);
1.1       root     10077:        
1.1.1.20  root     10078:        if(REG16(BX) == REG16(CX)) {
                   10079:                REG16(AX) = 0x06;
                   10080:                m_CF = 1;
                   10081:        } else if(fd < process->max_files && file_handler[fd].valid && dup_fd < process->max_files) {
                   10082:                if(tmp_fd < process->max_files && file_handler[tmp_fd].valid) {
                   10083:                        _close(tmp_fd);
                   10084:                        msdos_file_handler_close(tmp_fd);
                   10085:                        msdos_psp_set_file_table(dup_fd, 0x0ff, current_psp);
                   10086:                }
                   10087:                if(_dup2(fd, dup_fd) != -1) {
                   10088:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   10089: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   10090:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     10091:                } else {
                   10092:                        REG16(AX) = errno;
1.1.1.3   root     10093:                        m_CF = 1;
1.1       root     10094:                }
                   10095:        } else {
                   10096:                REG16(AX) = 0x06;
1.1.1.3   root     10097:                m_CF = 1;
1.1       root     10098:        }
                   10099: }
                   10100: 
                   10101: inline void msdos_int_21h_47h(int lfn)
                   10102: {
                   10103:        char path[MAX_PATH];
                   10104:        
                   10105:        if(_getdcwd(REG8(DL), path, MAX_PATH) != NULL) {
                   10106:                if(path[1] == ':') {
                   10107:                        // the returned path does not include a drive or the initial backslash
1.1.1.3   root     10108:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), (lfn ? path : msdos_short_path(path)) + 3);
1.1       root     10109:                } else {
1.1.1.3   root     10110:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), lfn ? path : msdos_short_path(path));
1.1       root     10111:                }
                   10112:        } else {
                   10113:                REG16(AX) = errno;
1.1.1.3   root     10114:                m_CF = 1;
1.1       root     10115:        }
                   10116: }
                   10117: 
                   10118: inline void msdos_int_21h_48h()
                   10119: {
1.1.1.19  root     10120:        int seg, umb_linked;
1.1       root     10121:        
1.1.1.8   root     10122:        if((malloc_strategy & 0xf0) == 0x00) {
1.1.1.19  root     10123:                // unlink umb not to allocate memory in umb
                   10124:                if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   10125:                        msdos_mem_unlink_umb();
                   10126:                }
1.1.1.8   root     10127:                if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   10128:                        REG16(AX) = seg;
                   10129:                } else {
                   10130:                        REG16(AX) = 0x08;
                   10131:                        REG16(BX) = msdos_mem_get_free(first_mcb, 0);
                   10132:                        m_CF = 1;
                   10133:                }
1.1.1.19  root     10134:                if(umb_linked != 0) {
                   10135:                        msdos_mem_link_umb();
                   10136:                }
1.1.1.8   root     10137:        } else if((malloc_strategy & 0xf0) == 0x40) {
                   10138:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   10139:                        REG16(AX) = seg;
                   10140:                } else {
                   10141:                        REG16(AX) = 0x08;
                   10142:                        REG16(BX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   10143:                        m_CF = 1;
                   10144:                }
                   10145:        } else if((malloc_strategy & 0xf0) == 0x80) {
                   10146:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   10147:                        REG16(AX) = seg;
                   10148:                } else if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   10149:                        REG16(AX) = seg;
                   10150:                } else {
                   10151:                        REG16(AX) = 0x08;
                   10152:                        REG16(BX) = max(msdos_mem_get_free(UMB_TOP >> 4, 0), msdos_mem_get_free(first_mcb, 0));
                   10153:                        m_CF = 1;
                   10154:                }
1.1       root     10155:        }
                   10156: }
                   10157: 
                   10158: inline void msdos_int_21h_49h()
                   10159: {
1.1.1.14  root     10160:        int mcb_seg = SREG(ES) - 1;
                   10161:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   10162:        
                   10163:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   10164:                msdos_mem_free(SREG(ES));
                   10165:        } else {
1.1.1.33  root     10166:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.14  root     10167:                m_CF = 1;
                   10168:        }
1.1       root     10169: }
                   10170: 
                   10171: inline void msdos_int_21h_4ah()
                   10172: {
1.1.1.14  root     10173:        int mcb_seg = SREG(ES) - 1;
                   10174:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1       root     10175:        int max_paragraphs;
                   10176:        
1.1.1.14  root     10177:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   10178:                if(msdos_mem_realloc(SREG(ES), REG16(BX), &max_paragraphs)) {
                   10179:                        REG16(AX) = 0x08;
                   10180:                        REG16(BX) = max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs;
                   10181:                        m_CF = 1;
                   10182:                }
                   10183:        } else {
1.1.1.33  root     10184:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.3   root     10185:                m_CF = 1;
1.1       root     10186:        }
                   10187: }
                   10188: 
                   10189: inline void msdos_int_21h_4bh()
                   10190: {
1.1.1.3   root     10191:        char *command = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   10192:        param_block_t *param = (param_block_t *)(mem + SREG_BASE(ES) + REG16(BX));
1.1       root     10193:        
                   10194:        switch(REG8(AL)) {
                   10195:        case 0x00:
                   10196:        case 0x01:
                   10197:                if(msdos_process_exec(command, param, REG8(AL))) {
                   10198:                        REG16(AX) = 0x02;
1.1.1.3   root     10199:                        m_CF = 1;
1.1       root     10200:                }
                   10201:                break;
1.1.1.14  root     10202:        case 0x03:
                   10203:                {
                   10204:                        int fd;
                   10205:                        if((fd = _open(command, _O_RDONLY | _O_BINARY)) == -1) {
                   10206:                                REG16(AX) = 0x02;
                   10207:                                m_CF = 1;
                   10208:                                break;
                   10209:                        }
                   10210:                        int size = _read(fd, file_buffer, sizeof(file_buffer));
                   10211:                        _close(fd);
                   10212:                        
                   10213:                        UINT16 *overlay = (UINT16 *)param;
                   10214:                        
                   10215:                        // check exe header
                   10216:                        exe_header_t *header = (exe_header_t *)file_buffer;
                   10217:                        int header_size = 0;
                   10218:                        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   10219:                                header_size = header->header_size * 16;
                   10220:                                // relocation
                   10221:                                int start_seg = overlay[1];
                   10222:                                for(int i = 0; i < header->relocations; i++) {
                   10223:                                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   10224:                                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   10225:                                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   10226:                                }
                   10227:                        }
                   10228:                        memcpy(mem + (overlay[0] << 4), file_buffer + header_size, size - header_size);
                   10229:                }
                   10230:                break;
1.1       root     10231:        default:
1.1.1.22  root     10232:                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     10233:                REG16(AX) = 0x01;
1.1.1.3   root     10234:                m_CF = 1;
1.1       root     10235:                break;
                   10236:        }
                   10237: }
                   10238: 
                   10239: inline void msdos_int_21h_4ch()
                   10240: {
                   10241:        msdos_process_terminate(current_psp, REG8(AL), 1);
                   10242: }
                   10243: 
                   10244: inline void msdos_int_21h_4dh()
                   10245: {
                   10246:        REG16(AX) = retval;
                   10247: }
                   10248: 
                   10249: inline void msdos_int_21h_4eh()
                   10250: {
                   10251:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     10252:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   10253:        find_t *find = (find_t *)(mem + dta_laddr);
1.1.1.3   root     10254:        char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     10255:        WIN32_FIND_DATA fd;
                   10256:        
1.1.1.14  root     10257:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   10258:        find->find_magic = FIND_MAGIC;
                   10259:        find->dta_index = dtainfo - dtalist;
1.1       root     10260:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     10261:        dtainfo->allowable_mask = REG8(CL);
                   10262:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     10263:        
1.1.1.14  root     10264:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   10265:                dtainfo->allowable_mask &= ~8;
1.1       root     10266:        }
1.1.1.14  root     10267:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   10268:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     10269:                      !msdos_find_file_has_8dot3name(&fd)) {
                   10270:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   10271:                                FindClose(dtainfo->find_handle);
                   10272:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     10273:                                break;
                   10274:                        }
                   10275:                }
                   10276:        }
1.1.1.13  root     10277:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     10278:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   10279:                msdos_find_file_conv_local_time(&fd);
                   10280:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   10281:                find->size = fd.nFileSizeLow;
1.1.1.13  root     10282:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     10283:                REG16(AX) = 0;
1.1.1.14  root     10284:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     10285:                find->attrib = 8;
                   10286:                find->size = 0;
                   10287:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     10288:                dtainfo->allowable_mask &= ~8;
1.1       root     10289:                REG16(AX) = 0;
                   10290:        } else {
                   10291:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     10292:                m_CF = 1;
1.1       root     10293:        }
                   10294: }
                   10295: 
                   10296: inline void msdos_int_21h_4fh()
                   10297: {
                   10298:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     10299:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   10300:        find_t *find = (find_t *)(mem + dta_laddr);
1.1       root     10301:        WIN32_FIND_DATA fd;
                   10302:        
1.1.1.14  root     10303:        if(find->find_magic != FIND_MAGIC || find->dta_index >= MAX_DTAINFO) {
                   10304:                REG16(AX) = 0x12;
                   10305:                m_CF = 1;
                   10306:                return;
                   10307:        }
                   10308:        dtainfo_t *dtainfo = &dtalist[find->dta_index];
1.1.1.13  root     10309:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   10310:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     10311:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     10312:                              !msdos_find_file_has_8dot3name(&fd)) {
                   10313:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   10314:                                        FindClose(dtainfo->find_handle);
                   10315:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     10316:                                        break;
                   10317:                                }
                   10318:                        }
                   10319:                } else {
1.1.1.13  root     10320:                        FindClose(dtainfo->find_handle);
                   10321:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     10322:                }
                   10323:        }
1.1.1.13  root     10324:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     10325:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   10326:                msdos_find_file_conv_local_time(&fd);
                   10327:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   10328:                find->size = fd.nFileSizeLow;
1.1.1.13  root     10329:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     10330:                REG16(AX) = 0;
1.1.1.14  root     10331:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     10332:                find->attrib = 8;
                   10333:                find->size = 0;
                   10334:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     10335:                dtainfo->allowable_mask &= ~8;
1.1       root     10336:                REG16(AX) = 0;
                   10337:        } else {
                   10338:                REG16(AX) = 0x12;
1.1.1.3   root     10339:                m_CF = 1;
1.1       root     10340:        }
                   10341: }
                   10342: 
                   10343: inline void msdos_int_21h_50h()
                   10344: {
1.1.1.8   root     10345:        if(current_psp != REG16(BX)) {
                   10346:                process_t *process = msdos_process_info_get(current_psp);
                   10347:                if(process != NULL) {
                   10348:                        process->psp = REG16(BX);
                   10349:                }
                   10350:                current_psp = REG16(BX);
1.1.1.23  root     10351:                msdos_sda_update(current_psp);
1.1.1.8   root     10352:        }
1.1       root     10353: }
                   10354: 
                   10355: inline void msdos_int_21h_51h()
                   10356: {
                   10357:        REG16(BX) = current_psp;
                   10358: }
                   10359: 
                   10360: inline void msdos_int_21h_52h()
                   10361: {
1.1.1.25  root     10362:        SREG(ES) = DOS_INFO_TOP >> 4;
1.1.1.3   root     10363:        i386_load_segment_descriptor(ES);
1.1.1.25  root     10364:        REG16(BX) = offsetof(dos_info_t, first_dpb);
1.1       root     10365: }
                   10366: 
                   10367: inline void msdos_int_21h_54h()
                   10368: {
                   10369:        process_t *process = msdos_process_info_get(current_psp);
                   10370:        
                   10371:        REG8(AL) = process->verify;
                   10372: }
                   10373: 
                   10374: inline void msdos_int_21h_55h()
                   10375: {
                   10376:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   10377:        
                   10378:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
                   10379:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   10380:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   10381:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   10382:        psp->parent_psp = current_psp;
                   10383: }
                   10384: 
                   10385: inline void msdos_int_21h_56h(int lfn)
                   10386: {
                   10387:        char src[MAX_PATH], dst[MAX_PATH];
1.1.1.3   root     10388:        strcpy(src, msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn));
                   10389:        strcpy(dst, msdos_trimmed_path((char *)(mem + SREG_BASE(ES) + REG16(DI)), lfn));
1.1       root     10390:        
                   10391:        if(rename(src, dst)) {
                   10392:                REG16(AX) = errno;
1.1.1.3   root     10393:                m_CF = 1;
1.1       root     10394:        }
                   10395: }
                   10396: 
                   10397: inline void msdos_int_21h_57h()
                   10398: {
                   10399:        FILETIME time, local;
1.1.1.14  root     10400:        FILETIME *ctime, *atime, *mtime;
1.1.1.21  root     10401:        HANDLE hHandle;
1.1       root     10402:        
1.1.1.21  root     10403:        if((hHandle = (HANDLE)_get_osfhandle(REG16(BX))) == INVALID_HANDLE_VALUE) {
1.1.1.14  root     10404:                REG16(AX) = (UINT16)GetLastError();
                   10405:                m_CF = 1;
                   10406:                return;
                   10407:        }
                   10408:        ctime = atime = mtime = NULL;
                   10409:        
1.1       root     10410:        switch(REG8(AL)) {
                   10411:        case 0x00:
1.1.1.6   root     10412:        case 0x01:
1.1.1.14  root     10413:                mtime = &time;
1.1.1.6   root     10414:                break;
                   10415:        case 0x04:
                   10416:        case 0x05:
1.1.1.14  root     10417:                atime = &time;
1.1       root     10418:                break;
1.1.1.6   root     10419:        case 0x06:
                   10420:        case 0x07:
1.1.1.14  root     10421:                ctime = &time;
                   10422:                break;
                   10423:        default:
1.1.1.22  root     10424:                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     10425:                REG16(AX) = 0x01;
                   10426:                m_CF = 1;
                   10427:                return;
                   10428:        }
                   10429:        if(REG8(AL) & 1) {
1.1       root     10430:                DosDateTimeToFileTime(REG16(DX), REG16(CX), &local);
                   10431:                LocalFileTimeToFileTime(&local, &time);
1.1.1.21  root     10432:                if(!SetFileTime(hHandle, ctime, atime, mtime)) {
1.1       root     10433:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     10434:                        m_CF = 1;
1.1       root     10435:                }
1.1.1.14  root     10436:        } else {
1.1.1.21  root     10437:                if(!GetFileTime(hHandle, ctime, atime, mtime)) {
1.1.1.14  root     10438:                        // assume a device and use the current time
                   10439:                        GetSystemTimeAsFileTime(&time);
                   10440:                }
                   10441:                FileTimeToLocalFileTime(&time, &local);
                   10442:                FileTimeToDosDateTime(&local, &REG16(DX), &REG16(CX));
1.1       root     10443:        }
                   10444: }
                   10445: 
                   10446: inline void msdos_int_21h_58h()
                   10447: {
                   10448:        switch(REG8(AL)) {
                   10449:        case 0x00:
1.1.1.7   root     10450:                REG16(AX) = malloc_strategy;
                   10451:                break;
                   10452:        case 0x01:
1.1.1.24  root     10453: //             switch(REG16(BX)) {
                   10454:                switch(REG8(BL)) {
1.1.1.7   root     10455:                case 0x0000:
                   10456:                case 0x0001:
                   10457:                case 0x0002:
                   10458:                case 0x0040:
                   10459:                case 0x0041:
                   10460:                case 0x0042:
                   10461:                case 0x0080:
                   10462:                case 0x0081:
                   10463:                case 0x0082:
                   10464:                        malloc_strategy = REG16(BX);
1.1.1.23  root     10465:                        msdos_sda_update(current_psp);
1.1.1.7   root     10466:                        break;
                   10467:                default:
1.1.1.22  root     10468:                        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     10469:                        REG16(AX) = 0x01;
                   10470:                        m_CF = 1;
                   10471:                        break;
                   10472:                }
                   10473:                break;
                   10474:        case 0x02:
1.1.1.19  root     10475:                REG8(AL) = msdos_mem_get_umb_linked() ? 1 : 0;
1.1.1.7   root     10476:                break;
                   10477:        case 0x03:
1.1.1.24  root     10478: //             switch(REG16(BX)) {
                   10479:                switch(REG8(BL)) {
1.1.1.7   root     10480:                case 0x0000:
1.1.1.19  root     10481:                        msdos_mem_unlink_umb();
                   10482:                        break;
1.1.1.7   root     10483:                case 0x0001:
1.1.1.19  root     10484:                        msdos_mem_link_umb();
1.1.1.7   root     10485:                        break;
                   10486:                default:
1.1.1.22  root     10487:                        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     10488:                        REG16(AX) = 0x01;
                   10489:                        m_CF = 1;
                   10490:                        break;
                   10491:                }
1.1       root     10492:                break;
                   10493:        default:
1.1.1.22  root     10494:                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     10495:                REG16(AX) = 0x01;
1.1.1.3   root     10496:                m_CF = 1;
1.1       root     10497:                break;
                   10498:        }
                   10499: }
                   10500: 
                   10501: inline void msdos_int_21h_59h()
                   10502: {
1.1.1.23  root     10503:        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   10504:        
                   10505:        REG16(AX) = sda->extended_error_code;
                   10506:        REG8(BH) = sda->error_class;
                   10507:        REG8(BL) = sda->suggested_action;
                   10508:        REG8(CH) = sda->locus_of_last_error;
1.1       root     10509: }
                   10510: 
                   10511: inline void msdos_int_21h_5ah()
                   10512: {
1.1.1.3   root     10513:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     10514:        int len = strlen(path);
                   10515:        char tmp[MAX_PATH];
                   10516:        
                   10517:        if(GetTempFileName(path, "TMP", 0, tmp)) {
                   10518:                int fd = _open(tmp, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   10519:                
                   10520:                SetFileAttributes(tmp, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   10521:                REG16(AX) = fd;
                   10522:                msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     10523:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     10524:                
                   10525:                strcpy(path, tmp);
                   10526:                int dx = REG16(DX) + len;
1.1.1.3   root     10527:                int ds = SREG(DS);
1.1       root     10528:                while(dx > 0xffff) {
                   10529:                        dx -= 0x10;
                   10530:                        ds++;
                   10531:                }
                   10532:                REG16(DX) = dx;
1.1.1.3   root     10533:                SREG(DS) = ds;
                   10534:                i386_load_segment_descriptor(DS);
1.1       root     10535:        } else {
                   10536:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     10537:                m_CF = 1;
1.1       root     10538:        }
                   10539: }
                   10540: 
                   10541: inline void msdos_int_21h_5bh()
                   10542: {
1.1.1.3   root     10543:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     10544:        
1.1.1.24  root     10545:        if(msdos_is_existing_file(path)) {
1.1       root     10546:                // already exists
                   10547:                REG16(AX) = 0x50;
1.1.1.3   root     10548:                m_CF = 1;
1.1       root     10549:        } else {
                   10550:                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   10551:                
                   10552:                if(fd != -1) {
                   10553:                        SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   10554:                        REG16(AX) = fd;
                   10555:                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     10556:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     10557:                } else {
                   10558:                        REG16(AX) = errno;
1.1.1.3   root     10559:                        m_CF = 1;
1.1       root     10560:                }
                   10561:        }
                   10562: }
                   10563: 
                   10564: inline void msdos_int_21h_5ch()
                   10565: {
                   10566:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10567:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10568:        
1.1.1.20  root     10569:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     10570:                if(REG8(AL) == 0 || REG8(AL) == 1) {
1.1.1.35  root     10571:                        static const int modes[2] = {_LK_LOCK, _LK_UNLCK};
1.1.1.20  root     10572:                        UINT32 pos = _tell(fd);
                   10573:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), SEEK_SET);
                   10574:                        if(_locking(fd, modes[REG8(AL)], (REG16(SI) << 16) | REG16(DI))) {
1.1       root     10575:                                REG16(AX) = errno;
1.1.1.3   root     10576:                                m_CF = 1;
1.1       root     10577:                        }
1.1.1.20  root     10578:                        _lseek(fd, pos, SEEK_SET);
1.1.1.26  root     10579:                        
1.1       root     10580:                        // some seconds may be passed in _locking()
1.1.1.35  root     10581:                        REQUEST_HARDWRE_UPDATE();
1.1       root     10582:                } else {
                   10583:                        REG16(AX) = 0x01;
1.1.1.3   root     10584:                        m_CF = 1;
1.1       root     10585:                }
                   10586:        } else {
                   10587:                REG16(AX) = 0x06;
1.1.1.3   root     10588:                m_CF = 1;
1.1       root     10589:        }
                   10590: }
                   10591: 
1.1.1.22  root     10592: inline void msdos_int_21h_5dh()
                   10593: {
                   10594:        switch(REG8(AL)) {
                   10595:        case 0x06: // get address of dos swappable data area
1.1.1.23  root     10596:                SREG(DS) = (SDA_TOP >> 4);
                   10597:                i386_load_segment_descriptor(DS);
                   10598:                REG16(SI) = offsetof(sda_t, crit_error_flag);
                   10599:                REG16(CX) = 0x80;
                   10600:                REG16(DX) = 0x1a;
                   10601:                break;
                   10602:        case 0x0b: // get dos swappable data areas
1.1.1.22  root     10603:                REG16(AX) = 0x01;
                   10604:                m_CF = 1;
                   10605:                break;
                   10606:        case 0x08: // set redirected printer mode
                   10607:        case 0x09: // flush redirected printer output
                   10608:        case 0x0a: // set extended error information
                   10609:                break;
                   10610:        default:
                   10611:                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));
                   10612:                REG16(AX) = 0x01;
                   10613:                m_CF = 1;
                   10614:                break;
                   10615:        }
                   10616: }
                   10617: 
1.1.1.30  root     10618: inline void msdos_int_21h_5fh()
                   10619: {
                   10620:        switch(REG8(AL)) {
                   10621:        case 0x02:
                   10622:                {
                   10623:                        DWORD drives = GetLogicalDrives();
                   10624:                        for(int i = 0, index = 0; i < 26; i++) {
                   10625:                                if(drives & (1 << i)) {
                   10626:                                        char volume[] = "A:\\";
                   10627:                                        volume[0] = 'A' + i;
                   10628:                                        if(GetDriveType(volume) == DRIVE_REMOTE) {
                   10629:                                                if(index == REG16(BX)) {
                   10630:                                                        DWORD dwSize = 128;
                   10631:                                                        volume[2] = '\0';
                   10632:                                                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), volume);
                   10633:                                                        WNetGetConnection(volume, (char *)(mem + SREG_BASE(ES) + REG16(DI)), &dwSize);
                   10634:                                                        REG8(BH) = 0x00; // valid
                   10635:                                                        REG8(BL) = 0x04; // disk drive
                   10636:                                                        REG16(CX) = 0x00;
                   10637:                                                        return;
                   10638:                                                }
                   10639:                                                index++;
                   10640:                                        }
                   10641:                                }
                   10642:                        }
                   10643:                }
                   10644:                REG16(AX) = 0x12; // no more files
                   10645:                m_CF = 1;
                   10646:                break;
                   10647:        default:
                   10648:                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));
                   10649:                REG16(AX) = 0x01;
                   10650:                m_CF = 1;
                   10651:                break;
                   10652:        }
                   10653: }
                   10654: 
1.1       root     10655: inline void msdos_int_21h_60h(int lfn)
                   10656: {
1.1.1.14  root     10657:        char full[MAX_PATH], *path;
                   10658:        
1.1       root     10659:        if(lfn) {
1.1.1.14  root     10660:                char *name;
                   10661:                *full = '\0';
1.1.1.3   root     10662:                GetFullPathName((char *)(mem + SREG_BASE(DS) + REG16(SI)), MAX_PATH, full, &name);
1.1.1.14  root     10663:                switch(REG8(CL)) {
                   10664:                case 1:
                   10665:                        GetShortPathName(full, full, MAX_PATH);
                   10666:                        my_strupr(full);
                   10667:                        break;
                   10668:                case 2:
                   10669:                        GetLongPathName(full, full, MAX_PATH);
                   10670:                        break;
                   10671:                }
                   10672:                path = full;
                   10673:        } else {
                   10674:                path = msdos_short_full_path((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   10675:        }
                   10676:        if(*path != '\0') {
                   10677:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
1.1       root     10678:        } else {
1.1.1.14  root     10679:                REG16(AX) = (UINT16)GetLastError();
                   10680:                m_CF = 1;
1.1       root     10681:        }
                   10682: }
                   10683: 
                   10684: inline void msdos_int_21h_61h()
                   10685: {
                   10686:        REG8(AL) = 0;
                   10687: }
                   10688: 
                   10689: inline void msdos_int_21h_62h()
                   10690: {
                   10691:        REG16(BX) = current_psp;
                   10692: }
                   10693: 
                   10694: inline void msdos_int_21h_63h()
                   10695: {
                   10696:        switch(REG8(AL)) {
                   10697:        case 0x00:
1.1.1.3   root     10698:                SREG(DS) = (DBCS_TABLE >> 4);
                   10699:                i386_load_segment_descriptor(DS);
1.1       root     10700:                REG16(SI) = (DBCS_TABLE & 0x0f);
                   10701:                REG8(AL) = 0x00;
                   10702:                break;
1.1.1.22  root     10703:        case 0x01: // set korean input mode
                   10704:        case 0x02: // get korean input mode
                   10705:                REG8(AL) = 0xff; // not supported
                   10706:                break;
1.1       root     10707:        default:
1.1.1.22  root     10708:                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     10709:                REG16(AX) = 0x01;
1.1.1.3   root     10710:                m_CF = 1;
1.1       root     10711:                break;
                   10712:        }
                   10713: }
                   10714: 
1.1.1.25  root     10715: UINT16 get_extended_country_info(UINT8 func)
1.1       root     10716: {
1.1.1.25  root     10717:        switch(func) {
1.1.1.17  root     10718:        case 0x01:
                   10719:                if(REG16(CX) >= 5) {
1.1.1.19  root     10720:                        UINT8 data[1 + 2 + 2 + 2 + sizeof(country_info_t)];
1.1.1.17  root     10721:                        if(REG16(CX) > sizeof(data))            // cx = actual transfer size
                   10722:                                REG16(CX) = sizeof(data);
                   10723:                        ZeroMemory(data, sizeof(data));
                   10724:                        data[0] = 0x01;
                   10725:                        *(UINT16 *)(data + 1) = REG16(CX) - 3;
1.1.1.19  root     10726:                        *(UINT16 *)(data + 3) = get_country_info((country_info_t*)(data + 7));
1.1.1.17  root     10727:                        *(UINT16 *)(data + 5) = active_code_page;
                   10728:                        memcpy(mem + SREG_BASE(ES) + REG16(DI), data, REG16(CX));
1.1.1.25  root     10729: //                     REG16(AX) = active_code_page;
1.1.1.17  root     10730:                } else {
1.1.1.25  root     10731:                        return(0x08); // insufficient memory
1.1.1.17  root     10732:                }
                   10733:                break;
                   10734:        case 0x02:
                   10735:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   10736:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (UPPERTABLE_TOP & 0x0f);
                   10737:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (UPPERTABLE_TOP >> 4);
1.1.1.25  root     10738: //             REG16(AX) = active_code_page;
1.1.1.17  root     10739:                REG16(CX) = 0x05;
                   10740:                break;
1.1.1.23  root     10741:        case 0x03:
                   10742:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   10743:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (LOWERTABLE_TOP & 0x0f);
                   10744:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (LOWERTABLE_TOP >> 4);
1.1.1.25  root     10745: //             REG16(AX) = active_code_page;
1.1.1.23  root     10746:                REG16(CX) = 0x05;
                   10747:                break;
1.1.1.17  root     10748:        case 0x04:
                   10749:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x04;
                   10750:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_UPPERTABLE_TOP & 0x0f);
                   10751:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_UPPERTABLE_TOP >> 4);
1.1.1.25  root     10752: //             REG16(AX) = active_code_page;
1.1.1.17  root     10753:                REG16(CX) = 0x05;
                   10754:                break;
                   10755:        case 0x05:
                   10756:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x05;
                   10757:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_TERMINATOR_TOP & 0x0f);
                   10758:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_TERMINATOR_TOP >> 4);
1.1.1.25  root     10759: //             REG16(AX) = active_code_page;
1.1.1.17  root     10760:                REG16(CX) = 0x05;
                   10761:                break;
                   10762:        case 0x06:
                   10763:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x06;
                   10764:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (COLLATING_TABLE_TOP & 0x0f);
                   10765:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (COLLATING_TABLE_TOP >> 4);
1.1.1.25  root     10766: //             REG16(AX) = active_code_page;
1.1.1.17  root     10767:                REG16(CX) = 0x05;
                   10768:                break;
1.1       root     10769:        case 0x07:
1.1.1.3   root     10770:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x07;
                   10771:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (DBCS_TOP & 0x0f);
                   10772:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (DBCS_TOP >> 4);
1.1.1.25  root     10773: //             REG16(AX) = active_code_page;
1.1       root     10774:                REG16(CX) = 0x05;
                   10775:                break;
1.1.1.25  root     10776:        default:
                   10777:                return(0x01); // function number invalid
                   10778:        }
                   10779:        return(0x00);
                   10780: }
                   10781: 
                   10782: inline void msdos_int_21h_65h()
                   10783: {
                   10784:        char tmp[0x10000];
                   10785:        
                   10786:        switch(REG8(AL)) {
                   10787:        case 0x01:
                   10788:        case 0x02:
                   10789:        case 0x03:
                   10790:        case 0x04:
                   10791:        case 0x05:
                   10792:        case 0x06:
                   10793:        case 0x07:
                   10794:                {
                   10795:                        UINT16 result = get_extended_country_info(REG8(AL));
                   10796:                        if(result) {
                   10797:                                REG16(AX) = result;
                   10798:                                m_CF = 1;
                   10799:                        } else {
                   10800:                                REG16(AX) = active_code_page; // FIXME: is this correct???
                   10801:                        }
                   10802:                }
                   10803:                break;
1.1       root     10804:        case 0x20:
1.1.1.25  root     10805:        case 0xa0:
1.1.1.19  root     10806:                memset(tmp, 0, sizeof(tmp));
                   10807:                tmp[0] = REG8(DL);
1.1       root     10808:                my_strupr(tmp);
                   10809:                REG8(DL) = tmp[0];
                   10810:                break;
                   10811:        case 0x21:
1.1.1.25  root     10812:        case 0xa1:
1.1       root     10813:                memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     10814:                memcpy(tmp, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     10815:                my_strupr(tmp);
1.1.1.3   root     10816:                memcpy(mem + SREG_BASE(DS) + REG16(DX), tmp, REG16(CX));
1.1       root     10817:                break;
                   10818:        case 0x22:
1.1.1.25  root     10819:        case 0xa2:
1.1.1.3   root     10820:                my_strupr((char *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1       root     10821:                break;
1.1.1.25  root     10822:        case 0x23:
                   10823:                // FIXME: need to check multi-byte (kanji) charactre?
                   10824:                if(REG8(DL) == 'N' || REG8(DL) == 'n' || (REG8(DL) == 0x82 && REG8(DH) == 0x78) || (REG8(DL) == 0x82 && REG8(DH) == 0x99)) {
                   10825:                        // 8278h/8299h: multi-byte (kanji) Y and y
                   10826:                        REG16(AX) = 0x00;
                   10827:                } else if(REG8(DL) == 'Y' || REG8(DL) == 'y' || (REG8(DL) == 0x82 && REG8(DH) == 0x6d) || (REG8(DL) == 0x82 && REG8(DH) == 0x8e)) {
                   10828:                        // 826dh/828eh: multi-byte (kanji) N and n
                   10829:                        REG16(AX) = 0x01;
                   10830:                } else {
                   10831:                        REG16(AX) = 0x02;
                   10832:                }
                   10833:                break;
1.1       root     10834:        default:
1.1.1.22  root     10835:                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     10836:                REG16(AX) = 0x01;
1.1.1.3   root     10837:                m_CF = 1;
1.1       root     10838:                break;
                   10839:        }
                   10840: }
                   10841: 
                   10842: inline void msdos_int_21h_66h()
                   10843: {
                   10844:        switch(REG8(AL)) {
                   10845:        case 0x01:
                   10846:                REG16(BX) = active_code_page;
                   10847:                REG16(DX) = system_code_page;
                   10848:                break;
                   10849:        case 0x02:
                   10850:                if(active_code_page == REG16(BX)) {
                   10851:                        REG16(AX) = 0xeb41;
                   10852:                } else if(_setmbcp(REG16(BX)) == 0) {
                   10853:                        active_code_page = REG16(BX);
1.1.1.17  root     10854:                        msdos_nls_tables_update();
1.1       root     10855:                        REG16(AX) = 0xeb41;
1.1.1.32  root     10856:                        SetConsoleCP(active_code_page);
                   10857:                        SetConsoleOutputCP(active_code_page);
1.1       root     10858:                } else {
                   10859:                        REG16(AX) = 0x25;
1.1.1.3   root     10860:                        m_CF = 1;
1.1       root     10861:                }
                   10862:                break;
                   10863:        default:
1.1.1.22  root     10864:                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     10865:                REG16(AX) = 0x01;
1.1.1.3   root     10866:                m_CF = 1;
1.1       root     10867:                break;
                   10868:        }
                   10869: }
                   10870: 
                   10871: inline void msdos_int_21h_67h()
                   10872: {
                   10873:        process_t *process = msdos_process_info_get(current_psp);
                   10874:        
                   10875:        if(REG16(BX) <= MAX_FILES) {
                   10876:                process->max_files = max(REG16(BX), 20);
                   10877:        } else {
                   10878:                REG16(AX) = 0x08;
1.1.1.3   root     10879:                m_CF = 1;
1.1       root     10880:        }
                   10881: }
                   10882: 
                   10883: inline void msdos_int_21h_68h()
                   10884: {
                   10885:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10886:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10887:        
1.1.1.20  root     10888:        if(fd < process->max_files && file_handler[fd].valid) {
                   10889:                // fflush(_fdopen(fd, ""));
1.1       root     10890:        } else {
                   10891:                REG16(AX) = 0x06;
1.1.1.3   root     10892:                m_CF = 1;
1.1       root     10893:        }
                   10894: }
                   10895: 
                   10896: inline void msdos_int_21h_69h()
                   10897: {
1.1.1.3   root     10898:        drive_info_t *info = (drive_info_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     10899:        char path[] = "A:\\";
                   10900:        char volume_label[MAX_PATH];
                   10901:        DWORD serial_number = 0;
                   10902:        char file_system[MAX_PATH];
                   10903:        
                   10904:        if(REG8(BL) == 0) {
                   10905:                path[0] = 'A' + _getdrive() - 1;
                   10906:        } else {
                   10907:                path[0] = 'A' + REG8(BL) - 1;
                   10908:        }
                   10909:        
                   10910:        switch(REG8(AL)) {
                   10911:        case 0x00:
                   10912:                if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
                   10913:                        info->info_level = 0;
                   10914:                        info->serial_number = serial_number;
                   10915:                        memset(info->volume_label, 0x20, 11);
                   10916:                        memcpy(info->volume_label, volume_label, min(strlen(volume_label), 11));
                   10917:                        memset(info->file_system, 0x20, 8);
                   10918:                        memcpy(info->file_system, file_system, min(strlen(file_system), 8));
                   10919:                } else {
                   10920:                        REG16(AX) = errno;
1.1.1.3   root     10921:                        m_CF = 1;
1.1       root     10922:                }
                   10923:                break;
                   10924:        case 0x01:
                   10925:                REG16(AX) = 0x03;
1.1.1.3   root     10926:                m_CF = 1;
1.1       root     10927:        }
                   10928: }
                   10929: 
                   10930: inline void msdos_int_21h_6ah()
                   10931: {
                   10932:        REG8(AH) = 0x68;
                   10933:        msdos_int_21h_68h();
                   10934: }
                   10935: 
                   10936: inline void msdos_int_21h_6bh()
                   10937: {
                   10938:        REG8(AL) = 0;
                   10939: }
                   10940: 
                   10941: inline void msdos_int_21h_6ch(int lfn)
                   10942: {
1.1.1.3   root     10943:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(SI)), lfn);
1.1       root     10944:        int mode = REG8(BL) & 0x03;
                   10945:        
                   10946:        if(mode < 0x03) {
1.1.1.29  root     10947:                if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1       root     10948:                        // file exists
                   10949:                        if(REG8(DL) & 1) {
1.1.1.37! root     10950:                                int fd = -1;
1.1.1.11  root     10951:                                UINT16 info;
1.1.1.37! root     10952:                                int sio_port = 0;
        !          10953:                                int lpt_port = 0;
1.1       root     10954:                                
1.1.1.11  root     10955:                                if(msdos_is_con_path(path)) {
1.1.1.13  root     10956:                                        fd = msdos_open("CON", file_mode[mode].mode);
1.1.1.11  root     10957:                                        info = 0x80d3;
1.1.1.37! root     10958:                                } else if((sio_port = msdos_is_comm_path(path)) != 0) {
        !          10959:                                        fd = msdos_open("NUL", file_mode[mode].mode);
1.1.1.14  root     10960:                                        info = 0x80d3;
1.1.1.37! root     10961:                                        msdos_set_comm_params(sio_port, path);
        !          10962:                                } else if((lpt_port = msdos_is_prn_path(path)) != 0) {
        !          10963:                                        fd = msdos_open("NUL", file_mode[mode].mode);
        !          10964:                                        info = 0xa8c0;
1.1.1.29  root     10965:                                } else if(msdos_is_device_path(path)) {
1.1.1.20  root     10966:                                        fd = msdos_open("NUL", file_mode[mode].mode);
                   10967:                                        info = 0x80d3;
1.1.1.11  root     10968:                                } else {
1.1.1.13  root     10969:                                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     10970:                                        info = msdos_drive_number(path);
                   10971:                                }
1.1       root     10972:                                if(fd != -1) {
                   10973:                                        REG16(AX) = fd;
                   10974:                                        REG16(CX) = 1;
1.1.1.37! root     10975:                                        msdos_file_handler_open(fd, path, _isatty(fd), mode, info, current_psp, sio_port, lpt_port);
1.1.1.20  root     10976:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     10977:                                } else {
                   10978:                                        REG16(AX) = errno;
1.1.1.3   root     10979:                                        m_CF = 1;
1.1       root     10980:                                }
                   10981:                        } else if(REG8(DL) & 2) {
                   10982:                                int attr = GetFileAttributes(path);
1.1.1.37! root     10983:                                int fd = -1;
1.1.1.11  root     10984:                                UINT16 info;
1.1.1.37! root     10985:                                int sio_port = 0;
        !          10986:                                int lpt_port = 0;
1.1       root     10987:                                
1.1.1.11  root     10988:                                if(msdos_is_con_path(path)) {
1.1.1.13  root     10989:                                        fd = msdos_open("CON", file_mode[mode].mode);
1.1.1.11  root     10990:                                        info = 0x80d3;
1.1.1.37! root     10991:                                } else if((sio_port = msdos_is_comm_path(path)) != 0) {
        !          10992:                                        fd = msdos_open("NUL", file_mode[mode].mode);
1.1.1.14  root     10993:                                        info = 0x80d3;
1.1.1.37! root     10994:                                        msdos_set_comm_params(sio_port, path);
        !          10995:                                } else if((lpt_port = msdos_is_prn_path(path)) != 0) {
        !          10996:                                        fd = msdos_open("NUL", file_mode[mode].mode);
        !          10997:                                        info = 0xa8c0;
1.1.1.29  root     10998:                                } else if(msdos_is_device_path(path)) {
1.1.1.20  root     10999:                                        fd = msdos_open("NUL", file_mode[mode].mode);
                   11000:                                        info = 0x80d3;
1.1       root     11001:                                } else {
                   11002:                                        fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
1.1.1.11  root     11003:                                        info = msdos_drive_number(path);
1.1       root     11004:                                }
                   11005:                                if(fd != -1) {
                   11006:                                        if(attr == -1) {
                   11007:                                                attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   11008:                                        }
                   11009:                                        SetFileAttributes(path, attr);
                   11010:                                        REG16(AX) = fd;
                   11011:                                        REG16(CX) = 3;
1.1.1.37! root     11012:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, info, current_psp, sio_port, lpt_port);
1.1.1.20  root     11013:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     11014:                                } else {
                   11015:                                        REG16(AX) = errno;
1.1.1.3   root     11016:                                        m_CF = 1;
1.1       root     11017:                                }
                   11018:                        } else {
                   11019:                                REG16(AX) = 0x50;
1.1.1.3   root     11020:                                m_CF = 1;
1.1       root     11021:                        }
                   11022:                } else {
                   11023:                        // file not exists
                   11024:                        if(REG8(DL) & 0x10) {
                   11025:                                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   11026:                                
                   11027:                                if(fd != -1) {
                   11028:                                        SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   11029:                                        REG16(AX) = fd;
                   11030:                                        REG16(CX) = 2;
                   11031:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     11032:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     11033:                                } else {
                   11034:                                        REG16(AX) = errno;
1.1.1.3   root     11035:                                        m_CF = 1;
1.1       root     11036:                                }
                   11037:                        } else {
                   11038:                                REG16(AX) = 0x02;
1.1.1.3   root     11039:                                m_CF = 1;
1.1       root     11040:                        }
                   11041:                }
                   11042:        } else {
                   11043:                REG16(AX) = 0x0c;
1.1.1.3   root     11044:                m_CF = 1;
1.1       root     11045:        }
                   11046: }
                   11047: 
                   11048: inline void msdos_int_21h_710dh()
                   11049: {
                   11050:        // reset drive
                   11051: }
                   11052: 
1.1.1.17  root     11053: inline void msdos_int_21h_7141h(int lfn)
                   11054: {
                   11055:        if(REG16(SI) == 0) {
                   11056:                msdos_int_21h_41h(lfn);
                   11057:                return;
                   11058:        }
                   11059:        if(REG16(SI) != 1) {
                   11060:                REG16(AX) = 5;
                   11061:                m_CF = 1;
                   11062:        }
                   11063:        /* wild card and matching attributes... */
                   11064:        char tmp[MAX_PATH * 2];
                   11065:        // copy search pathname (and quick check overrun)
                   11066:        ZeroMemory(tmp, sizeof(tmp));
                   11067:        tmp[MAX_PATH - 1] = '\0';
                   11068:        tmp[MAX_PATH] = 1;
                   11069:        strncpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(DX)), MAX_PATH);
                   11070:        
                   11071:        if(tmp[MAX_PATH - 1] != '\0' || tmp[MAX_PATH] != 1) {
                   11072:                REG16(AX) = 1;
                   11073:                m_CF = 1;
                   11074:                return;
                   11075:        }
                   11076:        for(char *s = tmp; *s; ++s) {
                   11077:                if(*s == '/') {
                   11078:                        *s = '\\';
                   11079:                }
                   11080:        }
                   11081:        char *tmp_name = (char *)_mbsrchr((unsigned char *)tmp, '\\');
                   11082:        if(tmp_name) {
                   11083:                ++tmp_name;
                   11084:        } else {
                   11085:                tmp_name = strchr(tmp, ':');
                   11086:                tmp_name = tmp_name ? tmp_name + 1 : tmp;
                   11087:        }
                   11088:        
                   11089:        WIN32_FIND_DATAA fd;
                   11090:        HANDLE fh = FindFirstFileA(tmp, &fd);
                   11091:        if(fh == INVALID_HANDLE_VALUE) {
                   11092:                REG16(AX) = 2;
                   11093:                m_CF = 1;
                   11094:                return;
                   11095:        }
                   11096:        do {
                   11097:                if(msdos_find_file_check_attribute(fd.dwFileAttributes, REG8(CL), REG8(CH)) && msdos_find_file_has_8dot3name(&fd)) {
                   11098:                        strcpy(tmp_name, fd.cFileName);
                   11099:                        if(remove(msdos_trimmed_path(tmp, lfn))) {
                   11100:                                REG16(AX) = 5;
                   11101:                                m_CF = 1;
                   11102:                                break;
                   11103:                        }
                   11104:                }
                   11105:        } while(FindNextFileA(fh, &fd));
                   11106:        if(!m_CF) {
                   11107:                if(GetLastError() != ERROR_NO_MORE_FILES) {
                   11108:                        m_CF = 1;
                   11109:                        REG16(AX) = 2;
                   11110:                }
                   11111:        }
                   11112:        FindClose(fh);
                   11113: }
                   11114: 
1.1       root     11115: inline void msdos_int_21h_714eh()
                   11116: {
                   11117:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     11118:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
                   11119:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     11120:        WIN32_FIND_DATA fd;
                   11121:        
1.1.1.13  root     11122:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   11123:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   11124:                FindClose(dtainfo->find_handle);
                   11125:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11126:        }
                   11127:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     11128:        dtainfo->allowable_mask = REG8(CL);
                   11129:        dtainfo->required_mask = REG8(CH);
                   11130:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     11131:        
1.1.1.14  root     11132:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   11133:                dtainfo->allowable_mask &= ~8;
1.1       root     11134:        }
1.1.1.14  root     11135:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   11136:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13  root     11137:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   11138:                                FindClose(dtainfo->find_handle);
                   11139:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11140:                                break;
                   11141:                        }
                   11142:                }
                   11143:        }
1.1.1.13  root     11144:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     11145:                find->attrib = fd.dwFileAttributes;
                   11146:                msdos_find_file_conv_local_time(&fd);
                   11147:                if(REG16(SI) == 0) {
                   11148:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   11149:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   11150:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   11151:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   11152:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   11153:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   11154:                } else {
                   11155:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   11156:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   11157:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   11158:                }
                   11159:                find->size_hi = fd.nFileSizeHigh;
                   11160:                find->size_lo = fd.nFileSizeLow;
                   11161:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     11162:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     11163:                REG16(AX) = dtainfo - dtalist + 1;
                   11164:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     11165:                // volume label
                   11166:                find->attrib = 8;
                   11167:                find->size_hi = find->size_lo = 0;
                   11168:                strcpy(find->full_name, process->volume_label);
                   11169:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     11170:                dtainfo->allowable_mask &= ~8;
                   11171:                REG16(AX) = dtainfo - dtalist + 1;
1.1       root     11172:        } else {
                   11173:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     11174:                m_CF = 1;
1.1       root     11175:        }
                   11176: }
                   11177: 
                   11178: inline void msdos_int_21h_714fh()
                   11179: {
                   11180:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     11181:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     11182:        WIN32_FIND_DATA fd;
                   11183:        
1.1.1.14  root     11184:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   11185:                REG16(AX) = 6;
1.1.1.13  root     11186:                m_CF = 1;
                   11187:                return;
                   11188:        }
1.1.1.14  root     11189:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     11190:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   11191:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     11192:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13  root     11193:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   11194:                                        FindClose(dtainfo->find_handle);
                   11195:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11196:                                        break;
                   11197:                                }
                   11198:                        }
                   11199:                } else {
1.1.1.13  root     11200:                        FindClose(dtainfo->find_handle);
                   11201:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11202:                }
                   11203:        }
1.1.1.13  root     11204:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     11205:                find->attrib = fd.dwFileAttributes;
                   11206:                msdos_find_file_conv_local_time(&fd);
                   11207:                if(REG16(SI) == 0) {
                   11208:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   11209:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   11210:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   11211:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   11212:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   11213:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   11214:                } else {
                   11215:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   11216:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   11217:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   11218:                }
                   11219:                find->size_hi = fd.nFileSizeHigh;
                   11220:                find->size_lo = fd.nFileSizeLow;
                   11221:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     11222:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     11223:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     11224:                // volume label
                   11225:                find->attrib = 8;
                   11226:                find->size_hi = find->size_lo = 0;
                   11227:                strcpy(find->full_name, process->volume_label);
                   11228:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     11229:                dtainfo->allowable_mask &= ~8;
1.1       root     11230:        } else {
                   11231:                REG16(AX) = 0x12;
1.1.1.3   root     11232:                m_CF = 1;
1.1       root     11233:        }
                   11234: }
                   11235: 
                   11236: inline void msdos_int_21h_71a0h()
                   11237: {
                   11238:        DWORD max_component_len, file_sys_flag;
                   11239:        
1.1.1.14  root     11240:        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))) {
                   11241:                REG16(BX) = (UINT16)file_sys_flag & (FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES | FILE_UNICODE_ON_DISK | FILE_VOLUME_IS_COMPRESSED);
                   11242:                REG16(BX) |= 0x4000;                            // supports LFN functions
1.1       root     11243:                REG16(CX) = (UINT16)max_component_len;          // 255
                   11244:                REG16(DX) = (UINT16)max_component_len + 5;      // 260
                   11245:        } else {
                   11246:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     11247:                m_CF = 1;
1.1       root     11248:        }
                   11249: }
                   11250: 
                   11251: inline void msdos_int_21h_71a1h()
                   11252: {
1.1.1.14  root     11253:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   11254:                REG16(AX) = 6;
1.1.1.13  root     11255:                m_CF = 1;
                   11256:                return;
                   11257:        }
1.1.1.14  root     11258:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     11259:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   11260:                FindClose(dtainfo->find_handle);
                   11261:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11262:        }
                   11263: }
                   11264: 
                   11265: inline void msdos_int_21h_71a6h()
                   11266: {
                   11267:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11268:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   11269:        
1.1.1.3   root     11270:        UINT8 *buffer = (UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     11271:        struct _stat64 status;
                   11272:        DWORD serial_number = 0;
                   11273:        
1.1.1.20  root     11274:        if(fd < process->max_files && file_handler[fd].valid) {
                   11275:                if(_fstat64(fd, &status) == 0) {
                   11276:                        if(file_handler[fd].path[1] == ':') {
1.1       root     11277:                                // NOTE: we need to consider the network file path "\\host\share\"
                   11278:                                char volume[] = "A:\\";
1.1.1.20  root     11279:                                volume[0] = file_handler[fd].path[1];
1.1       root     11280:                                GetVolumeInformation(volume, NULL, 0, &serial_number, NULL, NULL, NULL, 0);
                   11281:                        }
1.1.1.20  root     11282:                        *(UINT32 *)(buffer + 0x00) = GetFileAttributes(file_handler[fd].path);
1.1       root     11283:                        *(UINT32 *)(buffer + 0x04) = (UINT32)(status.st_ctime & 0xffffffff);
                   11284:                        *(UINT32 *)(buffer + 0x08) = (UINT32)((status.st_ctime >> 32) & 0xffffffff);
                   11285:                        *(UINT32 *)(buffer + 0x0c) = (UINT32)(status.st_atime & 0xffffffff);
                   11286:                        *(UINT32 *)(buffer + 0x10) = (UINT32)((status.st_atime >> 32) & 0xffffffff);
                   11287:                        *(UINT32 *)(buffer + 0x14) = (UINT32)(status.st_mtime & 0xffffffff);
                   11288:                        *(UINT32 *)(buffer + 0x18) = (UINT32)((status.st_mtime >> 32) & 0xffffffff);
                   11289:                        *(UINT32 *)(buffer + 0x1c) = serial_number;
                   11290:                        *(UINT32 *)(buffer + 0x20) = (UINT32)((status.st_size >> 32) & 0xffffffff);
                   11291:                        *(UINT32 *)(buffer + 0x24) = (UINT32)(status.st_size & 0xffffffff);
                   11292:                        *(UINT32 *)(buffer + 0x28) = status.st_nlink;
1.1.1.14  root     11293:                        // this is dummy id and it will be changed when it is reopened...
1.1       root     11294:                        *(UINT32 *)(buffer + 0x2c) = 0;
1.1.1.20  root     11295:                        *(UINT32 *)(buffer + 0x30) = file_handler[fd].id;
1.1       root     11296:                } else {
                   11297:                        REG16(AX) = errno;
1.1.1.3   root     11298:                        m_CF = 1;
1.1       root     11299:                }
                   11300:        } else {
                   11301:                REG16(AX) = 0x06;
1.1.1.3   root     11302:                m_CF = 1;
1.1       root     11303:        }
                   11304: }
                   11305: 
                   11306: inline void msdos_int_21h_71a7h()
                   11307: {
                   11308:        switch(REG8(BL)) {
                   11309:        case 0x00:
1.1.1.3   root     11310:                if(!FileTimeToDosDateTime((FILETIME *)(mem + SREG_BASE(DS) + REG16(SI)), &REG16(DX), &REG16(CX))) {
1.1       root     11311:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     11312:                        m_CF = 1;
1.1       root     11313:                }
                   11314:                break;
                   11315:        case 0x01:
                   11316:                // NOTE: we need to check BH that shows 10-millisecond untils past time in CX
1.1.1.3   root     11317:                if(!DosDateTimeToFileTime(REG16(DX), REG16(CX), (FILETIME *)(mem + SREG_BASE(ES) + REG16(DI)))) {
1.1       root     11318:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     11319:                        m_CF = 1;
1.1       root     11320:                }
                   11321:                break;
                   11322:        default:
1.1.1.22  root     11323:                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     11324:                REG16(AX) = 0x01;
1.1.1.3   root     11325:                m_CF = 1;
1.1       root     11326:                break;
                   11327:        }
                   11328: }
                   11329: 
                   11330: inline void msdos_int_21h_71a8h()
                   11331: {
                   11332:        if(REG8(DH) == 0) {
                   11333:                char tmp[MAX_PATH], fcb[MAX_PATH];
1.1.1.3   root     11334:                strcpy(tmp, msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     11335:                memset(fcb, 0x20, sizeof(fcb));
                   11336:                int len = strlen(tmp);
1.1.1.21  root     11337:                for(int i = 0, pos = 0; i < len; i++) {
1.1       root     11338:                        if(tmp[i] == '.') {
                   11339:                                pos = 8;
                   11340:                        } else {
                   11341:                                if(msdos_lead_byte_check(tmp[i])) {
                   11342:                                        fcb[pos++] = tmp[i++];
                   11343:                                }
                   11344:                                fcb[pos++] = tmp[i];
                   11345:                        }
                   11346:                }
1.1.1.3   root     11347:                memcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), fcb, 11);
1.1       root     11348:        } else {
1.1.1.3   root     11349:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     11350:        }
                   11351: }
                   11352: 
1.1.1.22  root     11353: inline void msdos_int_21h_71aah()
                   11354: {
                   11355:        char drv[] = "A:", path[MAX_PATH];
                   11356:        char *hoge=(char *)(mem + SREG_BASE(DS) + REG16(DX));
                   11357:        
                   11358:        if(REG8(BL) == 0) {
                   11359:                drv[0] = 'A' + _getdrive() - 1;
                   11360:        } else {
                   11361:                drv[0] = 'A' + REG8(BL) - 1;
                   11362:        }
                   11363:        switch(REG8(BH)) {
                   11364:        case 0x00:
                   11365:                if(DefineDosDevice(0, drv, (char *)(mem + SREG_BASE(DS) + REG16(DX))) == 0) {
                   11366:                        DWORD bits = 1 << ((REG8(BL) ? REG8(BL) : _getdrive()) - 1);
                   11367:                        if(GetLogicalDrives() & bits) {
                   11368:                                REG16(AX) = 0x0f; // invalid drive
                   11369:                        } else {
                   11370:                                REG16(AX) = 0x03; // path not found
                   11371:                        }
                   11372:                        m_CF = 1;
                   11373:                }
                   11374:                break;
                   11375:        case 0x01:
                   11376:                if(DefineDosDevice(DDD_REMOVE_DEFINITION, drv, NULL) == 0) {
                   11377:                        REG16(AX) = 0x0f; // invalid drive
                   11378:                        m_CF = 1;
                   11379:                }
                   11380:                break;
                   11381:        case 0x02:
                   11382:                if(QueryDosDevice(drv, path, MAX_PATH) == 0) {
                   11383:                        REG16(AX) = 0x0f; // invalid drive
                   11384:                        m_CF = 1;
                   11385:                } else if(strncmp(path, "\\??\\", 4) != 0) {
                   11386:                        REG16(AX) = 0x0f; // invalid drive
                   11387:                        m_CF = 1;
                   11388:                } else {
                   11389:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(DX)), path + 4);
                   11390:                }
                   11391:                break;
                   11392:        default:
                   11393:                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));
                   11394:                REG16(AX) = 0x01;
                   11395:                m_CF = 1;
                   11396:                break;
                   11397:        }
                   11398: }
                   11399: 
1.1.1.14  root     11400: inline void msdos_int_21h_7300h()
                   11401: {
                   11402:        if(REG8(AL) == 0) {
                   11403:                REG8(AL) = REG8(CL);
                   11404:                REG8(AH) = 0;
                   11405:        } else {
                   11406:                REG16(AX) = 0x01;
                   11407:                m_CF = 1;
                   11408:        }
                   11409: }
                   11410: 
                   11411: inline void msdos_int_21h_7302h()
                   11412: {
                   11413:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   11414:        UINT16 seg, ofs;
                   11415:        
                   11416:        if(REG16(CX) < 0x3f) {
                   11417:                REG8(AL) = 0x18;
                   11418:                m_CF = 1;
                   11419:        } else if(!msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   11420:                REG8(AL) = 0xff;
                   11421:                m_CF = 1;
                   11422:        } else {
                   11423:                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 2, mem + (seg << 4) + ofs, sizeof(dpb_t));
                   11424:        }
                   11425: }
                   11426: 
1.1       root     11427: inline void msdos_int_21h_7303h()
                   11428: {
1.1.1.3   root     11429:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   11430:        ext_space_info_t *info = (ext_space_info_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     11431:        DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   11432:        
                   11433:        if(GetDiskFreeSpace(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
                   11434:                info->size_of_structure = sizeof(ext_space_info_t);
                   11435:                info->structure_version = 0;
                   11436:                info->sectors_per_cluster = sectors_per_cluster;
                   11437:                info->bytes_per_sector = bytes_per_sector;
                   11438:                info->available_clusters_on_drive = free_clusters;
                   11439:                info->total_clusters_on_drive = total_clusters;
                   11440:                info->available_sectors_on_drive = sectors_per_cluster * free_clusters;
                   11441:                info->total_sectors_on_drive = sectors_per_cluster * total_clusters;
                   11442:                info->available_allocation_units = free_clusters;       // ???
                   11443:                info->total_allocation_units = total_clusters;          // ???
                   11444:        } else {
                   11445:                REG16(AX) = errno;
1.1.1.3   root     11446:                m_CF = 1;
1.1       root     11447:        }
                   11448: }
                   11449: 
1.1.1.30  root     11450: inline void msdos_int_21h_dbh()
                   11451: {
                   11452:        // Novell NetWare - Workstation - Get Number of Local Drives
                   11453:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   11454:        REG8(AL) = dos_info->last_drive;
                   11455: }
                   11456: 
                   11457: inline void msdos_int_21h_dch()
                   11458: {
                   11459:        // Novell NetWare - Connection Services - Get Connection Number
                   11460:        REG8(AL) = 0x00;
                   11461: }
                   11462: 
1.1.1.32  root     11463: inline void msdos_int_24h()
                   11464: {
                   11465:        const char *message = NULL;
                   11466:        int key = 0;
                   11467:        
                   11468:        for(int i = 0; i < array_length(critical_error_table); i++) {
                   11469:                if(critical_error_table[i].code == (REG16(DI) & 0xff) || critical_error_table[i].code == (UINT16)-1) {
                   11470:                        if(active_code_page == 932) {
                   11471:                                message = critical_error_table[i].message_japanese;
                   11472:                        }
                   11473:                        if(message == NULL) {
                   11474:                                message = critical_error_table[i].message_english;
                   11475:                        }
                   11476:                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   11477:                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   11478:                        
                   11479:                        SREG(ES) = WORK_TOP >> 4;
                   11480:                        i386_load_segment_descriptor(ES);
                   11481:                        REG16(DI) = 0x0000;
                   11482:                        break;
                   11483:                }
                   11484:        }
                   11485:        fprintf(stderr, "\n%s", message);
                   11486:        if(!(REG8(AH) & 0x80)) {
                   11487:                if(REG8(AH) & 0x01) {
                   11488:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�������ݒ� �h���C�u" : "writing drive", 'A' + REG8(AL));
                   11489:                } else {
                   11490:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�ǂݎ�蒆 �h���C�u" : "reading drive", 'A' + REG8(AL));
                   11491:                }
                   11492:        }
                   11493:        fprintf(stderr, "\n");
                   11494:        
1.1.1.33  root     11495:        {
1.1.1.32  root     11496:                fprintf(stderr, "%s",   (active_code_page == 932) ? "���~ (A)" : "Abort");
1.1.1.33  root     11497:        }
1.1.1.32  root     11498:        if(REG8(AH) & 0x10) {
                   11499:                fprintf(stderr, ", %s", (active_code_page == 932) ? "�Ď��s (R)" : "Retry");
                   11500:        }
                   11501:        if(REG8(AH) & 0x20) {
                   11502:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���� (I)" : "Ignore");
                   11503:        }
                   11504:        if(REG8(AH) & 0x08) {
                   11505:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���s (F)" : "Fail");
                   11506:        }
                   11507:        fprintf(stderr, "? ");
                   11508:        
                   11509:        while(1) {
                   11510:                while(!_kbhit()) {
                   11511:                        Sleep(10);
                   11512:                }
                   11513:                key = _getch();
                   11514:                
                   11515:                if(key == 'I' || key == 'i') {
                   11516:                        if(REG8(AH) & 0x20) {
                   11517:                                REG8(AL) = 0;
                   11518:                                break;
                   11519:                        }
                   11520:                } else if(key == 'R' || key == 'r') {
                   11521:                        if(REG8(AH) & 0x10) {
                   11522:                                REG8(AL) = 1;
                   11523:                                break;
                   11524:                        }
                   11525:                } else if(key == 'A' || key == 'a') {
                   11526:                        REG8(AL) = 2;
                   11527:                        break;
                   11528:                } else if(key == 'F' || key == 'f') {
                   11529:                        if(REG8(AH) & 0x08) {
                   11530:                                REG8(AL) = 3;
                   11531:                                break;
                   11532:                        }
                   11533:                }
                   11534:        }
                   11535:        fprintf(stderr, "%c\n", key);
                   11536: }
                   11537: 
1.1       root     11538: inline void msdos_int_25h()
                   11539: {
                   11540:        UINT16 seg, ofs;
                   11541:        DWORD dwSize;
                   11542:        
1.1.1.3   root     11543: #if defined(HAS_I386)
                   11544:        I386OP(pushf)();
                   11545: #else
                   11546:        PREFIX86(_pushf());
                   11547: #endif
1.1       root     11548:        
                   11549:        if(!(REG8(AL) < 26)) {
                   11550:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     11551:                m_CF = 1;
1.1       root     11552:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   11553:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     11554:                m_CF = 1;
1.1       root     11555:        } else {
                   11556:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   11557:                char dev[64];
                   11558:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   11559:                
                   11560:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   11561:                if(hFile == INVALID_HANDLE_VALUE) {
                   11562:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     11563:                        m_CF = 1;
1.1       root     11564:                } else {
1.1.1.19  root     11565:                        UINT32 top_sector  = REG16(DX);
                   11566:                        UINT16 sector_num  = REG16(CX);
                   11567:                        UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   11568:                        
                   11569:                        if(sector_num == 0xffff) {
                   11570:                                top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   11571:                                sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   11572:                                UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   11573:                                UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   11574:                                buffer_addr = (seg << 4) + ofs;
                   11575:                        }
                   11576: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   11577: //                             REG8(AL) = 0x02; // drive not ready
                   11578: //                             m_CF = 1;
                   11579: //                     } else 
                   11580:                        if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     11581:                                REG8(AL) = 0x08; // sector not found
1.1.1.3   root     11582:                                m_CF = 1;
1.1.1.19  root     11583:                        } else if(ReadFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     11584:                                REG8(AL) = 0x0b; // read error
1.1.1.3   root     11585:                                m_CF = 1;
1.1       root     11586:                        }
                   11587:                        CloseHandle(hFile);
                   11588:                }
                   11589:        }
                   11590: }
                   11591: 
                   11592: inline void msdos_int_26h()
                   11593: {
                   11594:        // this operation may cause serious damage for drives, so always returns error...
                   11595:        UINT16 seg, ofs;
                   11596:        DWORD dwSize;
                   11597:        
1.1.1.3   root     11598: #if defined(HAS_I386)
                   11599:        I386OP(pushf)();
                   11600: #else
                   11601:        PREFIX86(_pushf());
                   11602: #endif
1.1       root     11603:        
                   11604:        if(!(REG8(AL) < 26)) {
                   11605:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     11606:                m_CF = 1;
1.1       root     11607:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   11608:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     11609:                m_CF = 1;
1.1       root     11610:        } else {
                   11611:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   11612:                char dev[64];
                   11613:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   11614:                
                   11615:                if(dpb->media_type == 0xf8) {
                   11616:                        // this drive is not a floppy
1.1.1.6   root     11617: //                     if(!(((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag & 0x40)) {
                   11618: //                             fatalerror("This application tried the absolute disk write to drive %c:\n", 'A' + REG8(AL));
                   11619: //                     }
1.1       root     11620:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     11621:                        m_CF = 1;
1.1       root     11622:                } else {
                   11623:                        HANDLE hFile = CreateFile(dev, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   11624:                        if(hFile == INVALID_HANDLE_VALUE) {
                   11625:                                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     11626:                                m_CF = 1;
1.1       root     11627:                        } else {
1.1.1.19  root     11628:                                UINT32 top_sector  = REG16(DX);
                   11629:                                UINT16 sector_num  = REG16(CX);
                   11630:                                UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   11631:                                
                   11632:                                if(sector_num == 0xffff) {
                   11633:                                        top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   11634:                                        sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   11635:                                        UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   11636:                                        UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   11637:                                        buffer_addr = (seg << 4) + ofs;
                   11638:                                }
1.1       root     11639:                                if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   11640:                                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     11641:                                        m_CF = 1;
1.1.1.19  root     11642:                                } else if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     11643:                                        REG8(AL) = 0x08; // sector not found
1.1.1.3   root     11644:                                        m_CF = 1;
1.1.1.19  root     11645:                                } else if(WriteFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     11646:                                        REG8(AL) = 0x0a; // write error
1.1.1.3   root     11647:                                        m_CF = 1;
1.1       root     11648:                                }
                   11649:                                CloseHandle(hFile);
                   11650:                        }
                   11651:                }
                   11652:        }
                   11653: }
                   11654: 
                   11655: inline void msdos_int_27h()
                   11656: {
1.1.1.29  root     11657:        int paragraphs = (min(REG16(DX), 0xfff0) + 15) >> 4;
                   11658:        try {
                   11659:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   11660:        } catch(...) {
                   11661:                // recover the broken mcb
                   11662:                int mcb_seg = SREG(CS) - 1;
                   11663:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     11664:                
1.1.1.29  root     11665:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     11666:                        mcb->mz = 'M';
                   11667:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   11668:                        
1.1.1.29  root     11669:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.33  root     11670:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4));
1.1.1.29  root     11671:                        } else {
1.1.1.33  root     11672:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0);
1.1.1.29  root     11673:                        }
                   11674:                } else {
                   11675:                        mcb->mz = 'Z';
1.1.1.30  root     11676:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     11677:                }
                   11678:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   11679:        }
1.1.1.3   root     11680:        msdos_process_terminate(SREG(CS), retval | 0x300, 0);
1.1       root     11681: }
                   11682: 
                   11683: inline void msdos_int_29h()
                   11684: {
1.1.1.14  root     11685: #if 1
                   11686:        // need to check escape sequences
1.1       root     11687:        msdos_putch(REG8(AL));
1.1.1.14  root     11688: #else
                   11689:        DWORD num;
                   11690:        vram_flush();
1.1.1.23  root     11691:        WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), &REG8(AL), 1, &num, NULL);
1.1.1.14  root     11692:        cursor_moved = true;
                   11693: #endif
1.1       root     11694: }
                   11695: 
                   11696: inline void msdos_int_2eh()
                   11697: {
                   11698:        char tmp[MAX_PATH], command[MAX_PATH], opt[MAX_PATH];
                   11699:        memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     11700:        strcpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
1.1       root     11701:        char *token = my_strtok(tmp, " ");
                   11702:        strcpy(command, token);
                   11703:        strcpy(opt, token + strlen(token) + 1);
                   11704:        
                   11705:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   11706:        param->env_seg = 0;
                   11707:        param->cmd_line.w.l = 44;
                   11708:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   11709:        param->fcb1.w.l = 24;
                   11710:        param->fcb1.w.h = (WORK_TOP >> 4);
                   11711:        param->fcb2.w.l = 24;
                   11712:        param->fcb2.w.h = (WORK_TOP >> 4);
                   11713:        
                   11714:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   11715:        
                   11716:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   11717:        cmd_line->len = strlen(opt);
                   11718:        strcpy(cmd_line->cmd, opt);
                   11719:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   11720:        
1.1.1.28  root     11721:        try {
                   11722:                if(msdos_process_exec(command, param, 0)) {
                   11723:                        REG16(AX) = 0xffff; // error before processing command
                   11724:                } else {
                   11725:                        // set flag to set retval to ax when the started process is terminated
                   11726:                        process_t *process = msdos_process_info_get(current_psp);
                   11727:                        process->called_by_int2eh = true;
                   11728:                }
                   11729:        } catch(...) {
                   11730:                REG16(AX) = 0xffff; // error before processing command
                   11731:        }
1.1       root     11732: }
                   11733: 
1.1.1.29  root     11734: inline void msdos_int_2fh_05h()
                   11735: {
                   11736:        switch(REG8(AL)) {
                   11737:        case 0x00:
1.1.1.32  root     11738:                REG8(AL) = 0xff;
                   11739:                break;
                   11740:        case 0x01:
                   11741:        case 0x02:
                   11742:                for(int i = 0; i < array_length(standard_error_table); i++) {
                   11743:                        if(standard_error_table[i].code == REG16(BX) || standard_error_table[i].code == (UINT16)-1) {
                   11744:                                const char *message = NULL;
                   11745:                                if(active_code_page == 932) {
                   11746:                                        message = standard_error_table[i].message_japanese;
                   11747:                                }
                   11748:                                if(message == NULL) {
                   11749:                                        message = standard_error_table[i].message_english;
                   11750:                                }
                   11751:                                strcpy((char *)(mem + WORK_TOP), message);
                   11752:                                
                   11753:                                SREG(ES) = WORK_TOP >> 4;
                   11754:                                i386_load_segment_descriptor(ES);
                   11755:                                REG16(DI) = 0x0000;
                   11756:                                REG8(AL) = 0x01;
                   11757:                                break;
                   11758:                        }
                   11759:                }
1.1.1.29  root     11760:                break;
                   11761:        default:
                   11762:                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));
                   11763:                m_CF = 1;
                   11764:        }
                   11765: }
                   11766: 
1.1.1.22  root     11767: inline void msdos_int_2fh_11h()
                   11768: {
                   11769:        switch(REG8(AL)) {
                   11770:        case 0x00:
1.1.1.29  root     11771:                if(i386_read_stack() == 0xdada) {
                   11772:                        // MSCDEX is not installed
                   11773: //                     REG8(AL) = 0x00;
                   11774:                } else {
                   11775:                        // Network Redirector is not installed
                   11776: //                     REG8(AL) = 0x00;
                   11777:                }
1.1.1.22  root     11778:                break;
                   11779:        default:
                   11780:                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     11781:                REG16(AX) = 0x49; //  network software not installed
1.1.1.22  root     11782:                m_CF = 1;
                   11783:                break;
                   11784:        }
                   11785: }
                   11786: 
1.1.1.21  root     11787: inline void msdos_int_2fh_12h()
                   11788: {
                   11789:        switch(REG8(AL)) {
1.1.1.22  root     11790:        case 0x00:
1.1.1.29  root     11791:                // DOS 3.0+ internal functions are installed
1.1.1.22  root     11792:                REG8(AL) = 0xff;
                   11793:                break;
1.1.1.29  root     11794: //     case 0x01: // DOS 3.0+ internal - Close Current File
                   11795:        case 0x02:
                   11796:                {
                   11797:                        UINT16 stack = i386_read_stack();
                   11798:                        REG16(BX) = *(UINT16 *)(mem + 4 * stack + 0);
                   11799:                        SREG(ES) = *(UINT16 *)(mem + 4 * stack + 2);
                   11800:                        i386_load_segment_descriptor(ES);
                   11801:                }
                   11802:                break;
1.1.1.30  root     11803:        case 0x03:
                   11804:                SREG(DS) = (DEVICE_TOP >> 4);
                   11805:                i386_load_segment_descriptor(DS);
                   11806:                break;
1.1.1.29  root     11807:        case 0x04:
                   11808:                {
                   11809:                        UINT16 stack = i386_read_stack();
                   11810:                        REG8(AL) = (stack == '/') ? '\\' : stack;
                   11811: #if defined(HAS_I386)
                   11812:                        m_ZF = (REG8(AL) == '\\');
                   11813: #else
                   11814:                        m_ZeroVal = (REG8(AL) != '\\');
                   11815: #endif
                   11816:                }
                   11817:                break;
                   11818:        case 0x05:
                   11819:                msdos_putch(i386_read_stack());
                   11820:                break;
                   11821: //     case 0x06: // DOS 3.0+ internal - Invole Critical Error
                   11822: //     case 0x07: // DOS 3.0+ internal - Make Disk Buffer Most Recentry Used
                   11823: //     case 0x08: // DOS 3.0+ internal - Decrement SFT Reference Count
                   11824: //     case 0x09: // DOS 3.0+ internal - Flush and FREE Disk Buffer
                   11825: //     case 0x0a: // DOS 3.0+ internal - Perform Critical Error Interrupt
                   11826: //     case 0x0b: // DOS 3.0+ internal - Signal Sharing Violation to User
                   11827: //     case 0x0c: // DOS 3.0+ internal - Open Device and Set SFT Owner/Mode
                   11828:        case 0x0d:
                   11829:                {
                   11830:                        SYSTEMTIME time;
                   11831:                        FILETIME file_time;
                   11832:                        WORD dos_date, dos_time;
                   11833:                        GetLocalTime(&time);
                   11834:                        SystemTimeToFileTime(&time, &file_time);
                   11835:                        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   11836:                        REG16(AX) = dos_date;
                   11837:                        REG16(DX) = dos_time;
                   11838:                }
                   11839:                break;
                   11840: //     case 0x0e: // DOS 3.0+ internal - Mark All Disk Buffers Unreferenced
                   11841: //     case 0x0f: // DOS 3.0+ internal - Make Buffer Most Recentry Used
                   11842: //     case 0x10: // DOS 3.0+ internal - Find Unreferenced Disk Buffer
                   11843:        case 0x11:
                   11844:                {
                   11845:                        char path[MAX_PATH], *p;
                   11846:                        strcpy(path, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   11847:                        my_strupr(path);
                   11848:                        while((p = my_strchr(path, '/')) != NULL) {
                   11849:                                *p = '\\';
                   11850:                        }
                   11851:                        strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
                   11852:                }
                   11853:                break;
                   11854:        case 0x12:
                   11855:                REG16(CX) = strlen((char *)(mem + SREG_BASE(ES) + REG16(DI)));
                   11856:                break;
                   11857:        case 0x13:
                   11858:                {
                   11859:                        char tmp[2] = {0};
                   11860:                        tmp[0] = i386_read_stack();
                   11861:                        my_strupr(tmp);
                   11862:                        REG8(AL) = tmp[0];
                   11863:                }
                   11864:                break;
                   11865:        case 0x14:
                   11866: #if defined(HAS_I386)
                   11867:                m_ZF = (SREG_BASE(DS) + REG16(SI) == SREG_BASE(ES) + REG16(DI));
                   11868: #else
                   11869:                m_ZeroVal = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   11870: #endif
                   11871:                m_CF = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   11872:                break;
                   11873: //     case 0x15: // DOS 3.0+ internal - Flush Buffer
1.1.1.21  root     11874:        case 0x16:
                   11875:                if(REG16(BX) < 20) {
                   11876:                        SREG(ES) = SFT_TOP >> 4;
                   11877:                        i386_load_segment_descriptor(ES);
                   11878:                        REG16(DI) = 6 + 0x3b * REG16(BX);
                   11879:                        
                   11880:                        // update system file table
                   11881:                        UINT8* sft = mem + SFT_TOP + 6 + 0x3b * REG16(BX);
                   11882:                        if(file_handler[REG16(BX)].valid) {
                   11883:                                int count = 0;
                   11884:                                for(int i = 0; i < 20; i++) {
                   11885:                                        if(msdos_psp_get_file_table(i, current_psp) == REG16(BX)) {
                   11886:                                                count++;
                   11887:                                        }
                   11888:                                }
                   11889:                                *(UINT16 *)(sft + 0x00) = count ? count : 0xffff;
                   11890:                                *(UINT32 *)(sft + 0x15) = _tell(REG16(BX));
                   11891:                                _lseek(REG16(BX), 0, SEEK_END);
                   11892:                                *(UINT32 *)(sft + 0x11) = _tell(REG16(BX));
                   11893:                                _lseek(REG16(BX), *(UINT32 *)(sft + 0x15), SEEK_SET);
                   11894:                        } else {
                   11895:                                memset(sft, 0, 0x3b);
                   11896:                        }
                   11897:                } else {
                   11898:                        REG16(AX) = 0x06;
                   11899:                        m_CF = 1;
                   11900:                }
                   11901:                break;
1.1.1.29  root     11902: //     case 0x17: // DOS 3.0+ internal - Get Current Directory Structure for Drive
                   11903: //     case 0x18: // DOS 3.0+ internal - Get Caller's Registers
                   11904: //     case 0x19: // DOS 3.0+ internal - Set Drive???
                   11905:        case 0x1a:
                   11906:                {
                   11907:                        char *path = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full[MAX_PATH];
                   11908:                        if(path[1] == ':') {
                   11909:                                if(path[0] >= 'a' && path[0] <= 'z') {
                   11910:                                        REG8(AL) = path[0] - 'a' + 1;
                   11911:                                } else if(path[0] >= 'A' && path[0] <= 'Z') {
                   11912:                                        REG8(AL) = path[0] - 'A' + 1;
                   11913:                                } else {
                   11914:                                        REG8(AL) = 0xff; // invalid
                   11915:                                }
                   11916:                                strcpy(full, path);
                   11917:                                strcpy(path, full + 2);
                   11918:                        } else if(GetFullPathName(path, MAX_PATH, full, NULL) != 0 && full[1] == ':') {
                   11919:                                if(full[0] >= 'a' && full[0] <= 'z') {
                   11920:                                        REG8(AL) = full[0] - 'a' + 1;
                   11921:                                } else if(full[0] >= 'A' && full[0] <= 'Z') {
                   11922:                                        REG8(AL) = full[0] - 'A' + 1;
                   11923:                                } else {
                   11924:                                        REG8(AL) = 0xff; // invalid
                   11925:                                }
                   11926:                        } else {
                   11927:                                REG8(AL) = 0x00; // default
                   11928:                        }
                   11929:                }
                   11930:                break;
                   11931:        case 0x1b:
                   11932:                {
                   11933:                        int year = REG16(CX) + 1980;
                   11934:                        REG8(AL) = ((year % 4) == 0 && (year % 100) != 0 && (year % 400) == 0) ? 29 : 28;
                   11935:                }
                   11936:                break;
                   11937: //     case 0x1c: // DOS 3.0+ internal - Check Sum Memory
                   11938: //     case 0x1d: // DOS 3.0+ internal - Sum Memory
                   11939:        case 0x1e:
                   11940:                {
                   11941:                        char *path_1st = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full_1st[MAX_PATH];
                   11942:                        char *path_2nd = (char *)(mem + SREG_BASE(ES) + REG16(DI)), full_2nd[MAX_PATH];
                   11943:                        if(GetFullPathName(path_1st, MAX_PATH, full_1st, NULL) != 0 && GetFullPathName(path_2nd, MAX_PATH, full_2nd, NULL) != 0) {
                   11944: #if defined(HAS_I386)
                   11945:                                m_ZF = (strcmp(full_1st, full_2nd) == 0);
                   11946: #else
                   11947:                                m_ZeroVal = (strcmp(full_1st, full_2nd) != 0);
                   11948: #endif
                   11949:                        } else {
                   11950: #if defined(HAS_I386)
                   11951:                                m_ZF = (strcmp(path_1st, path_2nd) == 0);
                   11952: #else
                   11953:                                m_ZeroVal = (strcmp(path_1st, path_2nd) != 0);
                   11954: #endif
                   11955:                        }
                   11956:                }
                   11957:                break;
                   11958: //     case 0x1f: // DOS 3.0+ internal - Build Current Directory Structure
1.1.1.21  root     11959:        case 0x20:
                   11960:                {
                   11961:                        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   11962:                        
                   11963:                        if(fd < 20) {
                   11964:                                SREG(ES) = current_psp;
                   11965:                                i386_load_segment_descriptor(ES);
                   11966:                                REG16(DI) = offsetof(psp_t, file_table) + fd;
                   11967:                        } else {
                   11968:                                REG16(AX) = 0x06;
                   11969:                                m_CF = 1;
                   11970:                        }
                   11971:                }
                   11972:                break;
1.1.1.29  root     11973:        case 0x21:
                   11974:                msdos_int_21h_60h(0);
                   11975:                break;
                   11976: //     case 0x22: // DOS 3.0+ internal - Set Extended Error Info
                   11977: //     case 0x23: // DOS 3.0+ internal - Check If Character Device
                   11978: //     case 0x24: // DOS 3.0+ internal - Sharing Retry Delay
                   11979:        case 0x25:
                   11980:                REG16(CX) = strlen((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   11981:                break;
                   11982:        case 0x26:
                   11983:                REG8(AL) = REG8(CL);
                   11984:                msdos_int_21h_3dh();
                   11985:                break;
                   11986:        case 0x27:
                   11987:                msdos_int_21h_3eh();
                   11988:                break;
                   11989:        case 0x28:
                   11990:                REG16(AX) = REG16(BP);
                   11991:                msdos_int_21h_42h();
                   11992:                break;
                   11993:        case 0x29:
                   11994:                msdos_int_21h_3fh();
                   11995:                break;
                   11996: //     case 0x2a: // DOS 3.0+ internal - Set Fast Open Entry Point
                   11997:        case 0x2b:
                   11998:                REG16(AX) = REG16(BP);
                   11999:                msdos_int_21h_44h();
                   12000:                break;
                   12001:        case 0x2c:
                   12002:                REG16(BX) = DEVICE_TOP >> 4;
                   12003:                REG16(AX) = 22;
                   12004:                break;
                   12005:        case 0x2d:
                   12006:                {
                   12007:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   12008:                        REG16(AX) = sda->extended_error_code;
                   12009:                }
                   12010:                break;
                   12011:        case 0x2e:
                   12012:                if(REG8(DL) == 0x00 || REG8(DL) == 0x02 || REG8(DL) == 0x04 || REG8(DL) == 0x06) {
1.1.1.32  root     12013:                        SREG(ES) = 0x0001;
                   12014:                        i386_load_segment_descriptor(ES);
                   12015:                        REG16(DI) = 0x00;
                   12016:                } else if(REG8(DL) == 0x08) {
                   12017:                        // dummy parameter error message read routine is at fffd:0010
                   12018:                        SREG(ES) = 0xfffd;
1.1.1.22  root     12019:                        i386_load_segment_descriptor(ES);
1.1.1.32  root     12020:                        REG16(DI) = 0x0010;
1.1.1.22  root     12021:                }
                   12022:                break;
1.1.1.29  root     12023:        case 0x2f:
                   12024:                if(REG16(DX) != 0) {
1.1.1.30  root     12025:                        dos_major_version = REG8(DL);
                   12026:                        dos_minor_version = REG8(DH);
1.1.1.29  root     12027:                } else {
                   12028:                        REG8(DL) = 7;
                   12029:                        REG8(DH) = 10;
                   12030:                }
                   12031:                break;
                   12032: //     case 0x30: // Windows95 - Find SFT Entry in Internal File Tables
                   12033: //     case 0x31: // Windows95 - Set/Clear "Report Windows to DOS Programs" Flag
1.1.1.22  root     12034:        default:
                   12035:                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));
                   12036:                REG16(AX) = 0x01;
                   12037:                m_CF = 1;
                   12038:                break;
                   12039:        }
                   12040: }
                   12041: 
1.1.1.30  root     12042: inline void msdos_int_2fh_13h()
                   12043: {
                   12044:        static UINT16 prevDS = 0, prevDX = 0;
                   12045:        static UINT16 prevES = 0, prevBX = 0;
                   12046:        UINT16 tmp;
                   12047:        
                   12048:        tmp = SREG(DS); SREG(DS) = prevDS; prevDS = tmp;
                   12049:        i386_load_segment_descriptor(DS);
                   12050:        tmp = REG16(DX); REG16(DX) = prevDX; prevDX = tmp;
                   12051:        
                   12052:        tmp = SREG(ES); SREG(ES) = prevES; prevES = tmp;
                   12053:        i386_load_segment_descriptor(ES);
                   12054:        tmp = REG16(BX); REG16(BX) = prevBX; prevBX = tmp;
                   12055: }
                   12056: 
1.1.1.22  root     12057: inline void msdos_int_2fh_14h()
                   12058: {
                   12059:        switch(REG8(AL)) {
                   12060:        case 0x00:
1.1.1.29  root     12061:                // NLSFUNC.COM is installed
                   12062:                REG8(AL) = 0xff;
1.1.1.25  root     12063:                break;
                   12064:        case 0x01:
                   12065:        case 0x03:
                   12066:                REG8(AL) = 0x00;
                   12067:                active_code_page = REG16(BX);
                   12068:                msdos_nls_tables_update();
                   12069:                break;
                   12070:        case 0x02:
                   12071:                REG8(AL) = get_extended_country_info(REG16(BP));
                   12072:                break;
                   12073:        case 0x04:
                   12074:                REG8(AL) = 0x00;
                   12075:                get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)));
1.1.1.22  root     12076:                break;
                   12077:        default:
                   12078:                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));
                   12079:                REG16(AX) = 0x01;
                   12080:                m_CF = 1;
                   12081:                break;
                   12082:        }
                   12083: }
                   12084: 
                   12085: inline void msdos_int_2fh_15h()
                   12086: {
                   12087:        switch(REG8(AL)) {
1.1.1.29  root     12088:        case 0x00: // CD-ROM - Installation Check
                   12089:                if(REG16(BX) == 0x0000) {
                   12090:                        // MSCDEX is not installed
                   12091: //                     REG8(AL) = 0x00;
                   12092:                } else {
                   12093:                        // GRAPHICS.COM is not installed
                   12094: //                     REG8(AL) = 0x00;
                   12095:                }
1.1.1.22  root     12096:                break;
                   12097:        case 0xff:
1.1.1.29  root     12098:                if(REG16(BX) == 0x0000) {
                   12099:                        // CORELCDX is not installed
                   12100:                } else {
                   12101:                        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));
                   12102:                        REG16(AX) = 0x01;
                   12103:                        m_CF = 1;
                   12104:                }
1.1.1.22  root     12105:                break;
1.1.1.21  root     12106:        default:
1.1.1.22  root     12107:                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     12108:                REG16(AX) = 0x01;
                   12109:                m_CF = 1;
                   12110:                break;
                   12111:        }
                   12112: }
                   12113: 
1.1       root     12114: inline void msdos_int_2fh_16h()
                   12115: {
                   12116:        switch(REG8(AL)) {
                   12117:        case 0x00:
1.1.1.14  root     12118:                if(no_windows) {
1.1.1.29  root     12119:                        // neither Windows 3.x enhanced mode nor Windows/386 2.x running
                   12120: //                     REG8(AL) = 0x00;
1.1.1.14  root     12121:                } else {
1.1.1.30  root     12122:                        REG8(AL) = win_major_version;
                   12123:                        REG8(AH) = win_minor_version;
1.1       root     12124:                }
                   12125:                break;
1.1.1.30  root     12126:        case 0x05:
                   12127:                // from DOSBox
                   12128:                i386_set_a20_line(1);
                   12129:                break;
1.1.1.22  root     12130:        case 0x0a:
                   12131:                if(!no_windows) {
                   12132:                        REG16(AX) = 0x0000;
1.1.1.30  root     12133:                        REG8(BH) = win_major_version;
                   12134:                        REG8(BL) = win_minor_version;
1.1.1.22  root     12135:                        REG16(CX) = 0x0003; // enhanced
                   12136:                }
                   12137:                break;
1.1.1.30  root     12138:        case 0x0b:
                   12139:                // no TRS, keep ES:DI = 0000h:0000h
1.1.1.22  root     12140:        case 0x0e:
                   12141:        case 0x0f:
1.1.1.30  root     12142:        case 0x10:
1.1.1.22  root     12143:        case 0x11:
                   12144:        case 0x12:
                   12145:        case 0x13:
                   12146:        case 0x14:
1.1.1.30  root     12147:        case 0x15:
1.1.1.33  root     12148:        case 0x86:
1.1.1.22  root     12149:        case 0x87:
1.1.1.30  root     12150:        case 0x89:
1.1.1.33  root     12151:        case 0x8a:
1.1.1.22  root     12152:                // function not supported, do not clear AX
                   12153:                break;
1.1.1.14  root     12154:        case 0x80:
                   12155:                Sleep(10);
1.1.1.35  root     12156:                REQUEST_HARDWRE_UPDATE();
1.1.1.29  root     12157:                REG8(AL) = 0x00;
1.1.1.14  root     12158:                break;
1.1.1.33  root     12159:        case 0x83:
                   12160:                REG16(BX) = 0x01; // system vm id
                   12161:                break;
1.1.1.22  root     12162:        case 0x8e:
                   12163:                REG16(AX) = 0x00; // failed
                   12164:                break;
1.1.1.20  root     12165:        case 0x8f:
                   12166:                switch(REG8(DH)) {
                   12167:                case 0x00:
                   12168:                case 0x02:
                   12169:                case 0x03:
                   12170:                        REG16(AX) = 0x00;
                   12171:                        break;
                   12172:                case 0x01:
                   12173:                        REG16(AX) = 0x168f;
                   12174:                        break;
                   12175:                }
                   12176:                break;
1.1       root     12177:        default:
1.1.1.22  root     12178:                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));
                   12179:                REG16(AX) = 0x01;
                   12180:                m_CF = 1;
                   12181:                break;
                   12182:        }
                   12183: }
                   12184: 
                   12185: inline void msdos_int_2fh_19h()
                   12186: {
                   12187:        switch(REG8(AL)) {
                   12188:        case 0x00:
1.1.1.29  root     12189:                // SHELLB.COM is not installed
                   12190: //             REG8(AL) = 0x00;
1.1.1.22  root     12191:                break;
                   12192:        case 0x01:
                   12193:        case 0x02:
                   12194:        case 0x03:
                   12195:        case 0x04:
                   12196:                REG16(AX) = 0x01;
                   12197:                m_CF = 1;
                   12198:                break;
1.1.1.29  root     12199:        case 0x80:
                   12200:                // IBM ROM-DOS v4.0 is not installed
                   12201: //             REG8(AL) = 0x00;
                   12202:                break;
1.1.1.22  root     12203:        default:
                   12204:                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     12205:                REG16(AX) = 0x01;
1.1.1.3   root     12206:                m_CF = 1;
1.1       root     12207:                break;
                   12208:        }
                   12209: }
                   12210: 
                   12211: inline void msdos_int_2fh_1ah()
                   12212: {
                   12213:        switch(REG8(AL)) {
                   12214:        case 0x00:
1.1.1.29  root     12215:                // ANSI.SYS is installed
1.1       root     12216:                REG8(AL) = 0xff;
                   12217:                break;
                   12218:        default:
1.1.1.22  root     12219:                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));
                   12220:                REG16(AX) = 0x01;
                   12221:                m_CF = 1;
                   12222:                break;
                   12223:        }
                   12224: }
                   12225: 
1.1.1.30  root     12226: inline void msdos_int_2fh_40h()
1.1.1.22  root     12227: {
                   12228:        switch(REG8(AL)) {
                   12229:        case 0x00:
1.1.1.30  root     12230:                // Windows 3+ - Get Virtual Device Driver (VDD) Capabilities
                   12231:                REG8(AL) = 0x01; // does not virtualize video access
1.1.1.22  root     12232:                break;
                   12233:        default:
                   12234:                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     12235:                REG16(AX) = 0x01;
1.1.1.3   root     12236:                m_CF = 1;
1.1       root     12237:                break;
                   12238:        }
                   12239: }
                   12240: 
                   12241: inline void msdos_int_2fh_43h()
                   12242: {
                   12243:        switch(REG8(AL)) {
                   12244:        case 0x00:
1.1.1.29  root     12245:                // XMS is installed ?
1.1.1.19  root     12246: #ifdef SUPPORT_XMS
                   12247:                if(support_xms) {
                   12248:                        REG8(AL) = 0x80;
                   12249:                } else
                   12250: #endif
                   12251:                REG8(AL) = 0x00;
                   12252:                break;
                   12253:        case 0x10:
                   12254:                SREG(ES) = XMS_TOP >> 4;
                   12255:                i386_load_segment_descriptor(ES);
1.1.1.26  root     12256:                REG16(BX) = 0x15;
1.1       root     12257:                break;
                   12258:        default:
1.1.1.22  root     12259:                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));
                   12260:                REG16(AX) = 0x01;
                   12261:                m_CF = 1;
                   12262:                break;
                   12263:        }
                   12264: }
                   12265: 
                   12266: inline void msdos_int_2fh_46h()
                   12267: {
                   12268:        switch(REG8(AL)) {
                   12269:        case 0x80:
1.1.1.29  root     12270:                // Windows v3.0 is not installed
                   12271: //             REG8(AL) = 0x00;
1.1.1.22  root     12272:                break;
                   12273:        default:
                   12274:                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));
                   12275:                REG16(AX) = 0x01;
                   12276:                m_CF = 1;
                   12277:                break;
                   12278:        }
                   12279: }
                   12280: 
                   12281: inline void msdos_int_2fh_48h()
                   12282: {
                   12283:        switch(REG8(AL)) {
                   12284:        case 0x00:
1.1.1.29  root     12285:                // DOSKEY is not installed
                   12286: //             REG8(AL) = 0x00;
1.1.1.22  root     12287:                break;
                   12288:        case 0x10:
                   12289:                msdos_int_21h_0ah();
                   12290:                REG16(AX) = 0x00;
                   12291:                break;
                   12292:        default:
                   12293:                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     12294:                REG16(AX) = 0x01;
1.1.1.3   root     12295:                m_CF = 1;
1.1       root     12296:                break;
                   12297:        }
                   12298: }
                   12299: 
                   12300: inline void msdos_int_2fh_4ah()
                   12301: {
                   12302:        switch(REG8(AL)) {
1.1.1.29  root     12303: #ifdef SUPPORT_HMA
                   12304:        case 0x01: // DOS 5.0+ - Query Free HMA Space
                   12305:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   12306:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   12307:                                // restore first free mcb in high memory area
                   12308:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   12309:                        }
                   12310:                        int offset = 0xffff;
                   12311:                        if((REG16(BX) = msdos_hma_mem_get_free(&offset)) != 0) {
                   12312:                                REG16(DI) = offset + 0x10;
                   12313:                        } else {
                   12314:                                REG16(DI) = 0xffff;
                   12315:                        }
                   12316:                } else {
                   12317:                        // HMA is already used
                   12318:                        REG16(BX) = 0;
                   12319:                        REG16(DI) = 0xffff;
                   12320:                }
                   12321:                SREG(ES) = 0xffff;
                   12322:                i386_load_segment_descriptor(ES);
                   12323:                break;
                   12324:        case 0x02: // DOS 5.0+ - Allocate HMA Space
                   12325:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   12326:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   12327:                                // restore first free mcb in high memory area
                   12328:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   12329:                        }
                   12330:                        int size = REG16(BX), offset;
                   12331:                        if((size % 16) != 0) {
                   12332:                                size &= ~15;
                   12333:                                size += 16;
                   12334:                        }
                   12335:                        if((offset = msdos_hma_mem_alloc(size, current_psp)) != -1) {
                   12336:                                REG16(BX) = size;
                   12337:                                REG16(DI) = offset + 0x10;
                   12338:                                is_hma_used_by_int_2fh = true;
                   12339:                        } else {
                   12340:                                REG16(BX) = 0;
                   12341:                                REG16(DI) = 0xffff;
                   12342:                        }
                   12343:                } else {
                   12344:                        // HMA is already used
                   12345:                        REG16(BX) = 0;
                   12346:                        REG16(DI) = 0xffff;
                   12347:                }
                   12348:                SREG(ES) = 0xffff;
                   12349:                i386_load_segment_descriptor(ES);
                   12350:                break;
                   12351:        case 0x03: // Windows95 - (De)Allocate HMA Memory Block
                   12352:                if(REG8(DL) == 0x00) {
                   12353:                        if(!is_hma_used_by_xms) {
                   12354:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   12355:                                        // restore first free mcb in high memory area
                   12356:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   12357:                                        is_hma_used_by_int_2fh = false;
                   12358:                                }
                   12359:                                int size = REG16(BX), offset;
                   12360:                                if((size % 16) != 0) {
                   12361:                                        size &= ~15;
                   12362:                                        size += 16;
                   12363:                                }
                   12364:                                if((offset = msdos_hma_mem_alloc(size, REG16(CX))) != -1) {
                   12365: //                                     REG16(BX) = size;
                   12366:                                        SREG(ES) = 0xffff;
                   12367:                                        i386_load_segment_descriptor(ES);
                   12368:                                        REG16(DI) = offset + 0x10;
                   12369:                                        is_hma_used_by_int_2fh = true;
                   12370:                                } else {
                   12371:                                        REG16(DI) = 0xffff;
                   12372:                                }
                   12373:                        } else {
                   12374:                                REG16(DI) = 0xffff;
                   12375:                        }
                   12376:                } else if(REG8(DL) == 0x01) {
                   12377:                        if(!is_hma_used_by_xms) {
                   12378:                                int size = REG16(BX);
                   12379:                                if((size % 16) != 0) {
                   12380:                                        size &= ~15;
                   12381:                                        size += 16;
                   12382:                                }
                   12383:                                if(msdos_hma_mem_realloc(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10, size) != -1) {
                   12384:                                        // memory block address is not changed
                   12385:                                } else {
                   12386:                                        REG16(DI) = 0xffff;
                   12387:                                }
                   12388:                        } else {
                   12389:                                REG16(DI) = 0xffff;
                   12390:                        }
                   12391:                } else if(REG8(DL) == 0x02) {
                   12392:                        if(!is_hma_used_by_xms) {
                   12393:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   12394:                                        // restore first free mcb in high memory area
                   12395:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   12396:                                        is_hma_used_by_int_2fh = false;
                   12397:                                } else {
                   12398:                                        msdos_hma_mem_free(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10);
                   12399:                                        if(msdos_hma_mem_get_free(NULL) == 0xffe0) {
                   12400:                                                is_hma_used_by_int_2fh = false;
                   12401:                                        }
                   12402:                                }
                   12403:                        }
                   12404:                } else {
                   12405:                        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));
                   12406:                        REG16(AX) = 0x01;
                   12407:                        m_CF = 1;
                   12408:                }
                   12409:                break;
                   12410:        case 0x04: // Windows95 - Get Start of HMA Memory Chain
                   12411:                if(!is_hma_used_by_xms) {
                   12412:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   12413:                                // restore first free mcb in high memory area
                   12414:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   12415:                                is_hma_used_by_int_2fh = false;
                   12416:                        }
                   12417:                        REG16(AX) = 0x0000;
                   12418:                        SREG(ES) = 0xffff;
                   12419:                        i386_load_segment_descriptor(ES);
                   12420:                        REG16(DI) = 0x10;
                   12421:                }
                   12422:                break;
                   12423: #else
1.1       root     12424:        case 0x01:
                   12425:        case 0x02:
1.1.1.29  root     12426:                // HMA is already used
1.1.1.27  root     12427:                REG16(BX) = 0x0000;
1.1.1.3   root     12428:                SREG(ES) = 0xffff;
                   12429:                i386_load_segment_descriptor(ES);
1.1       root     12430:                REG16(DI) = 0xffff;
                   12431:                break;
1.1.1.19  root     12432:        case 0x03:
                   12433:                // unable to allocate
                   12434:                REG16(DI) = 0xffff;
                   12435:                break;
                   12436:        case 0x04:
                   12437:                // function not supported, do not clear AX
                   12438:                break;
1.1.1.29  root     12439: #endif
                   12440:        case 0x10:
                   12441:                if(REG16(BX) == 0x0000) {
                   12442:                        // SMARTDRV is not installed
                   12443:                } else {
                   12444:                        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));
                   12445:                        REG16(AX) = 0x01;
                   12446:                        m_CF = 1;
                   12447:                }
                   12448:                break;
                   12449:        case 0x11:
                   12450:                if(REG16(BX) == 0x0000) {
                   12451:                        // DBLSPACE.BIN is not installed
                   12452:                } else {
                   12453:                        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));
                   12454:                        REG16(AX) = 0x01;
                   12455:                        m_CF = 1;
                   12456:                }
1.1.1.22  root     12457:                break;
                   12458:        default:
                   12459:                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));
                   12460:                REG16(AX) = 0x01;
                   12461:                m_CF = 1;
                   12462:                break;
                   12463:        }
                   12464: }
                   12465: 
                   12466: inline void msdos_int_2fh_4bh()
                   12467: {
                   12468:        switch(REG8(AL)) {
1.1.1.24  root     12469:        case 0x01:
1.1.1.22  root     12470:        case 0x02:
1.1.1.29  root     12471:                // Task Switcher is not installed
1.1.1.24  root     12472:                break;
                   12473:        case 0x03:
                   12474:                // this call is available from within DOSSHELL even if the task switcher is not installed
                   12475:                REG16(AX) = REG16(BX) = 0x0000; // no more avaiable switcher id
1.1.1.22  root     12476:                break;
1.1.1.30  root     12477:        case 0x04:
                   12478:                REG16(BX) = 0x0000; // free switcher id successfully
                   12479:                break;
1.1       root     12480:        default:
1.1.1.22  root     12481:                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     12482:                REG16(AX) = 0x01;
1.1.1.3   root     12483:                m_CF = 1;
1.1       root     12484:                break;
                   12485:        }
                   12486: }
                   12487: 
                   12488: inline void msdos_int_2fh_4fh()
                   12489: {
                   12490:        switch(REG8(AL)) {
                   12491:        case 0x00:
1.1.1.29  root     12492:                // BILING is installed
1.1.1.27  root     12493:                REG16(AX) = 0x0000;
                   12494:                REG8(DL) = 0x01;        // major version
                   12495:                REG8(DH) = 0x00;        // minor version
1.1       root     12496:                break;
                   12497:        case 0x01:
1.1.1.27  root     12498:                REG16(AX) = 0x0000;
1.1       root     12499:                REG16(BX) = active_code_page;
                   12500:                break;
                   12501:        default:
1.1.1.22  root     12502:                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));
                   12503:                REG16(AX) = 0x01;
                   12504:                m_CF = 1;
                   12505:                break;
                   12506:        }
                   12507: }
                   12508: 
                   12509: inline void msdos_int_2fh_55h()
                   12510: {
                   12511:        switch(REG8(AL)) {
                   12512:        case 0x00:
                   12513:        case 0x01:
                   12514: //             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));
                   12515:                break;
                   12516:        default:
                   12517:                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     12518:                REG16(AX) = 0x01;
1.1.1.3   root     12519:                m_CF = 1;
1.1       root     12520:                break;
                   12521:        }
                   12522: }
                   12523: 
1.1.1.24  root     12524: inline void msdos_int_2fh_adh()
                   12525: {
                   12526:        switch(REG8(AL)) {
                   12527:        case 0x00:
1.1.1.29  root     12528:                // DISPLAY.SYS is installed
1.1.1.24  root     12529:                REG8(AL) = 0xff;
                   12530:                REG16(BX) = 0x100; // ???
                   12531:                break;
                   12532:        case 0x01:
                   12533:                active_code_page = REG16(BX);
                   12534:                msdos_nls_tables_update();
                   12535:                REG16(AX) = 0x01;
                   12536:                break;
                   12537:        case 0x02:
                   12538:                REG16(BX) = active_code_page;
                   12539:                break;
                   12540:        case 0x03:
                   12541:                // FIXME
                   12542:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 1;
                   12543:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2) = 3;
                   12544:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4) = 1;
                   12545:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 6) = active_code_page;
                   12546:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 8) = active_code_page;
                   12547:                break;
                   12548:        case 0x80:
                   12549:                break; // keyb.com is not installed
                   12550:        default:
                   12551:                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));
                   12552:                REG16(AX) = 0x01;
                   12553:                m_CF = 1;
                   12554:                break;
                   12555:        }
                   12556: }
                   12557: 
1.1       root     12558: inline void msdos_int_2fh_aeh()
                   12559: {
                   12560:        switch(REG8(AL)) {
                   12561:        case 0x00:
1.1.1.28  root     12562:                // FIXME: we need to check the given command line
                   12563:                REG8(AL) = 0x00; // the command should be executed as usual
                   12564: //             REG8(AL) = 0xff; // this command is a TSR extension to COMMAND.COM
1.1       root     12565:                break;
                   12566:        case 0x01:
                   12567:                {
                   12568:                        char command[MAX_PATH];
                   12569:                        memset(command, 0, sizeof(command));
1.1.1.3   root     12570:                        memcpy(command, mem + SREG_BASE(DS) + REG16(SI) + 1, mem[SREG_BASE(DS) + REG16(SI)]);
1.1       root     12571:                        
                   12572:                        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   12573:                        param->env_seg = 0;
                   12574:                        param->cmd_line.w.l = 44;
                   12575:                        param->cmd_line.w.h = (WORK_TOP >> 4);
                   12576:                        param->fcb1.w.l = 24;
                   12577:                        param->fcb1.w.h = (WORK_TOP >> 4);
                   12578:                        param->fcb2.w.l = 24;
                   12579:                        param->fcb2.w.h = (WORK_TOP >> 4);
                   12580:                        
                   12581:                        memset(mem + WORK_TOP + 24, 0x20, 20);
                   12582:                        
                   12583:                        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
1.1.1.3   root     12584:                        cmd_line->len = mem[SREG_BASE(DS) + REG16(BX) + 1];
                   12585:                        memcpy(cmd_line->cmd, mem + SREG_BASE(DS) + REG16(BX) + 2, cmd_line->len);
1.1       root     12586:                        cmd_line->cmd[cmd_line->len] = 0x0d;
                   12587:                        
1.1.1.28  root     12588:                        try {
                   12589:                                msdos_process_exec(command, param, 0);
                   12590:                        } catch(...) {
                   12591:                                fatalerror("failed to start '%s' by int 2Fh, AX=AE01h\n", command);
1.1       root     12592:                        }
                   12593:                }
                   12594:                break;
                   12595:        default:
1.1.1.22  root     12596:                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     12597:                REG16(AX) = 0x01;
1.1.1.3   root     12598:                m_CF = 1;
1.1       root     12599:                break;
                   12600:        }
                   12601: }
                   12602: 
1.1.1.34  root     12603: inline void msdos_int_2fh_b7h()
                   12604: {
                   12605:        switch(REG8(AL)) {
                   12606:        case 0x00:
                   12607:                // APPEND is not installed
                   12608: //             REG8(AL) = 0x00;
                   12609:                break;
                   12610:        case 0x07:
                   12611:                // COMMAND.COM calls this service without checking APPEND is installed
                   12612:                break;
                   12613:        default:
                   12614:                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));
                   12615:                REG16(AX) = 0x01;
                   12616:                m_CF = 1;
                   12617:                break;
                   12618:        }
                   12619: }
                   12620: 
1.1.1.24  root     12621: inline void msdos_int_33h_0000h()
                   12622: {
                   12623:        REG16(AX) = 0xffff; // hardware/driver installed
                   12624:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   12625: }
                   12626: 
                   12627: inline void msdos_int_33h_0001h()
                   12628: {
1.1.1.34  root     12629:        if(mouse.hidden > 0) {
                   12630:                mouse.hidden--;
                   12631:        }
                   12632:        if(mouse.hidden == 0) {
1.1.1.24  root     12633:                if(!(dwConsoleMode & ENABLE_MOUSE_INPUT)) {
                   12634:                        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode | ENABLE_MOUSE_INPUT);
                   12635:                }
                   12636:                pic[1].imr &= ~0x10;    // enable irq12
                   12637:        }
                   12638: }
                   12639: 
                   12640: inline void msdos_int_33h_0002h()
                   12641: {
1.1.1.34  root     12642:        mouse.hidden++;
                   12643: //     SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode & ~ENABLE_MOUSE_INPUT);
                   12644:        pic[1].imr |= 0x10;     // enable irq12
1.1.1.24  root     12645: }
                   12646: 
                   12647: inline void msdos_int_33h_0003h()
                   12648: {
1.1.1.34  root     12649: //     if(mouse.hidden > 0) {
                   12650:                update_console_input();
                   12651: //     }
1.1.1.24  root     12652:        REG16(BX) = mouse.get_buttons();
1.1.1.34  root     12653:        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   12654:        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
                   12655: }
                   12656: 
                   12657: inline void msdos_int_33h_0004h()
                   12658: {
                   12659:        mouse.position.x = REG16(CX);
                   12660:        mouse.position.x = REG16(DX);
1.1.1.24  root     12661: }
                   12662: 
                   12663: inline void msdos_int_33h_0005h()
                   12664: {
1.1.1.34  root     12665: //     if(mouse.hidden > 0) {
                   12666:                update_console_input();
                   12667: //     }
1.1.1.24  root     12668:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   12669:                int idx = REG16(BX);
1.1.1.34  root     12670:                REG16(BX) = min(mouse.buttons[idx].pressed_times, 0x7fff);
                   12671:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].pressed_position.x));
                   12672:                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     12673:                mouse.buttons[idx].pressed_times = 0;
                   12674:        } else {
                   12675:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   12676:        }
                   12677:        REG16(AX) = mouse.get_buttons();
                   12678: }
                   12679: 
                   12680: inline void msdos_int_33h_0006h()
                   12681: {
1.1.1.34  root     12682: //     if(mouse.hidden > 0) {
                   12683:                update_console_input();
                   12684: //     }
1.1.1.24  root     12685:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   12686:                int idx = REG16(BX);
1.1.1.34  root     12687:                REG16(BX) = min(mouse.buttons[idx].released_times, 0x7fff);
                   12688:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].released_position.x));
                   12689:                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     12690:                mouse.buttons[idx].released_times = 0;
                   12691:        } else {
                   12692:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   12693:        }
                   12694:        REG16(AX) = mouse.get_buttons();
                   12695: }
                   12696: 
                   12697: inline void msdos_int_33h_0007h()
                   12698: {
                   12699:        mouse.min_position.x = min(REG16(CX), REG16(DX));
                   12700:        mouse.max_position.x = max(REG16(CX), REG16(DX));
                   12701: }
                   12702: 
                   12703: inline void msdos_int_33h_0008h()
                   12704: {
                   12705:        mouse.min_position.y = min(REG16(CX), REG16(DX));
                   12706:        mouse.max_position.y = max(REG16(CX), REG16(DX));
                   12707: }
                   12708: 
                   12709: inline void msdos_int_33h_0009h()
                   12710: {
                   12711:        mouse.hot_spot[0] = REG16(BX);
                   12712:        mouse.hot_spot[1] = REG16(CX);
                   12713: }
                   12714: 
                   12715: inline void msdos_int_33h_000bh()
                   12716: {
1.1.1.34  root     12717: //     if(mouse.hidden > 0) {
                   12718:                update_console_input();
                   12719: //     }
1.1.1.24  root     12720:        int dx = (mouse.position.x - mouse.prev_position.x) * mouse.mickey.x / 8;
                   12721:        int dy = (mouse.position.y - mouse.prev_position.y) * mouse.mickey.y / 8;
                   12722:        mouse.prev_position.x = mouse.position.x;
                   12723:        mouse.prev_position.y = mouse.position.y;
                   12724:        REG16(CX) = dx;
                   12725:        REG16(DX) = dy;
                   12726: }
                   12727: 
                   12728: inline void msdos_int_33h_000ch()
                   12729: {
                   12730:        mouse.call_mask = REG16(CX);
                   12731:        mouse.call_addr.w.l = REG16(DX);
                   12732:        mouse.call_addr.w.h = SREG(ES);
                   12733: }
                   12734: 
                   12735: inline void msdos_int_33h_000fh()
                   12736: {
                   12737:        mouse.mickey.x = REG16(CX);
                   12738:        mouse.mickey.y = REG16(DX);
                   12739: }
                   12740: 
                   12741: inline void msdos_int_33h_0011h()
                   12742: {
                   12743:        REG16(AX) = 0xffff;
                   12744:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   12745: }
                   12746: 
                   12747: inline void msdos_int_33h_0014h()
                   12748: {
                   12749:        UINT16 old_mask = mouse.call_mask;
                   12750:        UINT16 old_ofs = mouse.call_addr.w.l;
                   12751:        UINT16 old_seg = mouse.call_addr.w.h;
                   12752:        
                   12753:        mouse.call_mask = REG16(CX);
                   12754:        mouse.call_addr.w.l = REG16(DX);
                   12755:        mouse.call_addr.w.h = SREG(ES);
                   12756:        
                   12757:        REG16(CX) = old_mask;
                   12758:        REG16(DX) = old_ofs;
                   12759:        SREG(ES) = old_seg;
                   12760:        i386_load_segment_descriptor(ES);
                   12761: }
                   12762: 
                   12763: inline void msdos_int_33h_0015h()
                   12764: {
                   12765:        REG16(BX) = sizeof(mouse);
                   12766: }
                   12767: 
                   12768: inline void msdos_int_33h_0016h()
                   12769: {
                   12770:        memcpy(mem + SREG_BASE(ES) + REG16(DX), &mouse, sizeof(mouse));
                   12771: }
                   12772: 
                   12773: inline void msdos_int_33h_0017h()
                   12774: {
                   12775:        memcpy(&mouse, mem + SREG_BASE(ES) + REG16(DX), sizeof(mouse));
                   12776: }
                   12777: 
                   12778: inline void msdos_int_33h_001ah()
                   12779: {
                   12780:        mouse.sensitivity[0] = REG16(BX);
                   12781:        mouse.sensitivity[1] = REG16(CX);
                   12782:        mouse.sensitivity[2] = REG16(DX);
                   12783: }
                   12784: 
                   12785: inline void msdos_int_33h_001bh()
                   12786: {
                   12787:        REG16(BX) = mouse.sensitivity[0];
                   12788:        REG16(CX) = mouse.sensitivity[1];
                   12789:        REG16(DX) = mouse.sensitivity[2];
                   12790: }
                   12791: 
                   12792: inline void msdos_int_33h_001dh()
                   12793: {
                   12794:        mouse.display_page = REG16(BX);
                   12795: }
                   12796: 
                   12797: inline void msdos_int_33h_001eh()
                   12798: {
                   12799:        REG16(BX) = mouse.display_page;
                   12800: }
                   12801: 
1.1.1.34  root     12802: inline void msdos_int_33h_001fh()
                   12803: {
                   12804:        // from DOSBox
                   12805:        REG16(BX) = 0x0000;
                   12806:        SREG(ES) = 0x0000;
                   12807:        i386_load_segment_descriptor(ES);
                   12808:        mouse.enabled = false;
                   12809:        mouse.old_hidden = mouse.hidden;
                   12810:        mouse.hidden = 1;
                   12811: }
                   12812: 
                   12813: inline void msdos_int_33h_0020h()
                   12814: {
                   12815:        // from DOSBox
                   12816:        mouse.enabled = true;
                   12817:        mouse.hidden = mouse.old_hidden;
                   12818: }
                   12819: 
1.1.1.24  root     12820: inline void msdos_int_33h_0021h()
                   12821: {
                   12822:        REG16(AX) = 0xffff;
                   12823:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   12824: }
                   12825: 
                   12826: inline void msdos_int_33h_0022h()
                   12827: {
                   12828:        mouse.language = REG16(BX);
                   12829: }
                   12830: 
                   12831: inline void msdos_int_33h_0023h()
                   12832: {
                   12833:        REG16(BX) = mouse.language;
                   12834: }
                   12835: 
                   12836: inline void msdos_int_33h_0024h()
                   12837: {
                   12838:        REG16(BX) = 0x0805; // V8.05
                   12839:        REG16(CX) = 0x0400; // PS/2
                   12840: }
                   12841: 
                   12842: inline void msdos_int_33h_0026h()
                   12843: {
                   12844:        REG16(BX) = 0x0000;
                   12845:        REG16(CX) = mouse.max_position.x;
                   12846:        REG16(DX) = mouse.max_position.y;
                   12847: }
                   12848: 
                   12849: inline void msdos_int_33h_002ah()
                   12850: {
1.1.1.34  root     12851:        REG16(AX) = -mouse.hidden;
1.1.1.24  root     12852:        REG16(BX) = mouse.hot_spot[0];
                   12853:        REG16(CX) = mouse.hot_spot[1];
                   12854:        REG16(DX) = 4; // PS/2
                   12855: }
                   12856: 
                   12857: inline void msdos_int_33h_0031h()
                   12858: {
                   12859:        REG16(AX) = mouse.min_position.x;
                   12860:        REG16(BX) = mouse.min_position.y;
                   12861:        REG16(CX) = mouse.max_position.x;
                   12862:        REG16(DX) = mouse.max_position.y;
                   12863: }
                   12864: 
                   12865: inline void msdos_int_33h_0032h()
                   12866: {
                   12867:        REG16(AX) = 0;
                   12868: //     REG16(AX) |= 0x8000; // 0025h
                   12869:        REG16(AX) |= 0x4000; // 0026h
                   12870: //     REG16(AX) |= 0x2000; // 0027h
                   12871: //     REG16(AX) |= 0x1000; // 0028h
                   12872: //     REG16(AX) |= 0x0800; // 0029h
                   12873:        REG16(AX) |= 0x0400; // 002ah
                   12874: //     REG16(AX) |= 0x0200; // 002bh
                   12875: //     REG16(AX) |= 0x0100; // 002ch
                   12876: //     REG16(AX) |= 0x0080; // 002dh
                   12877: //     REG16(AX) |= 0x0040; // 002eh
                   12878:        REG16(AX) |= 0x0020; // 002fh
                   12879: //     REG16(AX) |= 0x0010; // 0030h
                   12880:        REG16(AX) |= 0x0008; // 0031h
                   12881:        REG16(AX) |= 0x0004; // 0032h
                   12882: //     REG16(AX) |= 0x0002; // 0033h
                   12883: //     REG16(AX) |= 0x0001; // 0034h
                   12884: }
                   12885: 
1.1.1.19  root     12886: inline void msdos_int_67h_40h()
                   12887: {
                   12888:        if(!support_ems) {
                   12889:                REG8(AH) = 0x84;
                   12890:        } else {
                   12891:                REG8(AH) = 0x00;
                   12892:        }
                   12893: }
                   12894: 
                   12895: inline void msdos_int_67h_41h()
                   12896: {
                   12897:        if(!support_ems) {
                   12898:                REG8(AH) = 0x84;
                   12899:        } else {
                   12900:                REG8(AH) = 0x00;
                   12901:                REG16(BX) = EMS_TOP >> 4;
                   12902:        }
                   12903: }
                   12904: 
                   12905: inline void msdos_int_67h_42h()
                   12906: {
                   12907:        if(!support_ems) {
                   12908:                REG8(AH) = 0x84;
                   12909:        } else {
                   12910:                REG8(AH) = 0x00;
                   12911:                REG16(BX) = free_ems_pages;
                   12912:                REG16(DX) = MAX_EMS_PAGES;
                   12913:        }
                   12914: }
                   12915: 
                   12916: inline void msdos_int_67h_43h()
                   12917: {
                   12918:        if(!support_ems) {
                   12919:                REG8(AH) = 0x84;
                   12920:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   12921:                REG8(AH) = 0x87;
                   12922:        } else if(REG16(BX) > free_ems_pages) {
                   12923:                REG8(AH) = 0x88;
                   12924:        } else if(REG16(BX) == 0) {
                   12925:                REG8(AH) = 0x89;
                   12926:        } else {
1.1.1.31  root     12927:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     12928:                        if(!ems_handles[i].allocated) {
                   12929:                                ems_allocate_pages(i, REG16(BX));
                   12930:                                REG8(AH) = 0x00;
                   12931:                                REG16(DX) = i;
                   12932:                                return;
                   12933:                        }
                   12934:                }
                   12935:                REG8(AH) = 0x85;
                   12936:        }
                   12937: }
                   12938: 
                   12939: inline void msdos_int_67h_44h()
                   12940: {
                   12941:        if(!support_ems) {
                   12942:                REG8(AH) = 0x84;
1.1.1.31  root     12943:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     12944:                REG8(AH) = 0x83;
                   12945:        } else if(!(REG16(BX) == 0xffff || REG16(BX) < ems_handles[REG16(DX)].pages)) {
                   12946:                REG8(AH) = 0x8a;
                   12947: //     } else if(!(REG8(AL) < 4)) {
                   12948: //             REG8(AH) = 0x8b;
                   12949:        } else if(REG16(BX) == 0xffff) {
                   12950:                ems_unmap_page(REG8(AL) & 3);
                   12951:                REG8(AH) = 0x00;
                   12952:        } else {
                   12953:                ems_map_page(REG8(AL) & 3, REG16(DX), REG16(BX));
                   12954:                REG8(AH) = 0x00;
                   12955:        }
                   12956: }
                   12957: 
                   12958: inline void msdos_int_67h_45h()
                   12959: {
                   12960:        if(!support_ems) {
                   12961:                REG8(AH) = 0x84;
1.1.1.31  root     12962:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     12963:                REG8(AH) = 0x83;
                   12964:        } else {
                   12965:                ems_release_pages(REG16(DX));
                   12966:                REG8(AH) = 0x00;
                   12967:        }
                   12968: }
                   12969: 
                   12970: inline void msdos_int_67h_46h()
                   12971: {
                   12972:        if(!support_ems) {
                   12973:                REG8(AH) = 0x84;
                   12974:        } else {
1.1.1.29  root     12975: //             REG16(AX) = 0x0032; // EMS 3.2
                   12976:                REG16(AX) = 0x0040; // EMS 4.0
1.1.1.19  root     12977:        }
                   12978: }
                   12979: 
                   12980: inline void msdos_int_67h_47h()
                   12981: {
                   12982:        // NOTE: the map data should be stored in the specified ems page, not process data
                   12983:        process_t *process = msdos_process_info_get(current_psp);
                   12984:        
                   12985:        if(!support_ems) {
                   12986:                REG8(AH) = 0x84;
1.1.1.31  root     12987: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     12988: //             REG8(AH) = 0x83;
                   12989:        } else if(process->ems_pages_stored) {
                   12990:                REG8(AH) = 0x8d;
                   12991:        } else {
                   12992:                for(int i = 0; i < 4; i++) {
                   12993:                        process->ems_pages[i].handle = ems_pages[i].handle;
                   12994:                        process->ems_pages[i].page   = ems_pages[i].page;
                   12995:                        process->ems_pages[i].mapped = ems_pages[i].mapped;
                   12996:                }
                   12997:                process->ems_pages_stored = true;
                   12998:                REG8(AH) = 0x00;
                   12999:        }
                   13000: }
                   13001: 
                   13002: inline void msdos_int_67h_48h()
                   13003: {
                   13004:        // NOTE: the map data should be restored from the specified ems page, not process data
                   13005:        process_t *process = msdos_process_info_get(current_psp);
                   13006:        
                   13007:        if(!support_ems) {
                   13008:                REG8(AH) = 0x84;
1.1.1.31  root     13009: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     13010: //             REG8(AH) = 0x83;
                   13011:        } else if(!process->ems_pages_stored) {
                   13012:                REG8(AH) = 0x8e;
                   13013:        } else {
                   13014:                for(int i = 0; i < 4; i++) {
                   13015:                        if(process->ems_pages[i].mapped) {
                   13016:                                ems_map_page(i, process->ems_pages[i].handle, process->ems_pages[i].page);
                   13017:                        } else {
                   13018:                                ems_unmap_page(i);
                   13019:                        }
                   13020:                }
                   13021:                process->ems_pages_stored = false;
                   13022:                REG8(AH) = 0x00;
                   13023:        }
                   13024: }
                   13025: 
                   13026: inline void msdos_int_67h_4bh()
                   13027: {
                   13028:        if(!support_ems) {
                   13029:                REG8(AH) = 0x84;
                   13030:        } else {
                   13031:                REG8(AH) = 0x00;
                   13032:                REG16(BX) = 0;
1.1.1.31  root     13033:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     13034:                        if(ems_handles[i].allocated) {
                   13035:                                REG16(BX)++;
                   13036:                        }
                   13037:                }
                   13038:        }
                   13039: }
                   13040: 
                   13041: inline void msdos_int_67h_4ch()
                   13042: {
                   13043:        if(!support_ems) {
                   13044:                REG8(AH) = 0x84;
1.1.1.31  root     13045:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     13046:                REG8(AH) = 0x83;
                   13047:        } else {
                   13048:                REG8(AH) = 0x00;
                   13049:                REG16(BX) = ems_handles[REG16(DX)].pages;
                   13050:        }
                   13051: }
                   13052: 
                   13053: inline void msdos_int_67h_4dh()
                   13054: {
                   13055:        if(!support_ems) {
                   13056:                REG8(AH) = 0x84;
                   13057:        } else {
                   13058:                REG8(AH) = 0x00;
                   13059:                REG16(BX) = 0;
1.1.1.31  root     13060:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     13061:                        if(ems_handles[i].allocated) {
                   13062:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 0) = i;
                   13063:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 2) = ems_handles[i].pages;
                   13064:                                REG16(BX)++;
                   13065:                        }
                   13066:                }
                   13067:        }
                   13068: }
                   13069: 
1.1.1.20  root     13070: inline void msdos_int_67h_4eh()
                   13071: {
                   13072:        if(!support_ems) {
                   13073:                REG8(AH) = 0x84;
                   13074:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   13075:                if(REG8(AL) == 0x00 || REG8(AL) == 0x02) {
                   13076:                        // save page map
                   13077:                        for(int i = 0; i < 4; i++) {
                   13078:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   13079:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   13080:                        }
                   13081:                }
                   13082:                if(REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   13083:                        // restore page map
                   13084:                        for(int i = 0; i < 4; i++) {
                   13085:                                UINT16 handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   13086:                                UINT16 page   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   13087:                                
1.1.1.31  root     13088:                                if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
1.1.1.20  root     13089:                                        ems_map_page(i, handle, page);
                   13090:                                } else {
                   13091:                                        ems_unmap_page(i);
                   13092:                                }
                   13093:                        }
                   13094:                }
                   13095:                REG8(AH) = 0x00;
                   13096:        } else if(REG8(AL) == 0x03) {
                   13097:                REG8(AH) = 0x00;
1.1.1.21  root     13098:                REG8(AL) = 4 * 4;
                   13099:        } else {
1.1.1.22  root     13100:                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     13101:                REG8(AH) = 0x8f;
                   13102:        }
                   13103: }
                   13104: 
                   13105: inline void msdos_int_67h_4fh()
                   13106: {
                   13107:        if(!support_ems) {
                   13108:                REG8(AH) = 0x84;
                   13109:        } else if(REG8(AL) == 0x00) {
                   13110:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   13111:                
                   13112:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI)) = count;
                   13113:                for(int i = 0; i < count; i++) {
                   13114:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 2 * i);
                   13115:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   13116:                        
                   13117: //                     if(!(physical < 4)) {
                   13118: //                             REG8(AH) = 0x8b;
                   13119: //                             return;
                   13120: //                     }
                   13121:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 0) = segment;
                   13122:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 2) = ems_pages[physical & 3].handle;
                   13123:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 4) = ems_pages[physical & 3].mapped ? ems_pages[physical & 3].page : 0xffff;
                   13124:                }
                   13125:                REG8(AH) = 0x00;
                   13126:        } else if(REG8(AL) == 0x01) {
                   13127:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   13128:                
                   13129:                for(int i = 0; i < count; i++) {
                   13130:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 0);
                   13131:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   13132:                        UINT16 handle   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 2);
                   13133:                        UINT16 logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 4);
                   13134:                        
                   13135: //                     if(!(physical < 4)) {
                   13136: //                             REG8(AH) = 0x8b;
                   13137: //                             return;
                   13138: //                     } else
1.1.1.31  root     13139:                        if(!(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated)) {
1.1.1.21  root     13140:                                REG8(AH) = 0x83;
                   13141:                                return;
                   13142:                        } else if(logical == 0xffff) {
                   13143:                                ems_unmap_page(physical & 3);
                   13144:                        } else if(logical < ems_handles[handle].pages) {
                   13145:                                ems_map_page(physical & 3, handle, logical);
                   13146:                        } else {
                   13147:                                REG8(AH) = 0x8a;
                   13148:                                return;
                   13149:                        }
                   13150:                }
                   13151:                REG8(AH) = 0x00;
                   13152:        } else if(REG8(AL) == 0x02) {
                   13153:                REG8(AH) = 0x00;
                   13154:                REG8(AL) = 2 + REG16(BX) * 6;
1.1.1.20  root     13155:        } else {
1.1.1.22  root     13156:                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     13157:                REG8(AH) = 0x8f;
                   13158:        }
                   13159: }
                   13160: 
                   13161: inline void msdos_int_67h_50h()
                   13162: {
                   13163:        if(!support_ems) {
                   13164:                REG8(AH) = 0x84;
1.1.1.31  root     13165:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.20  root     13166:                REG8(AH) = 0x83;
                   13167:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   13168:                for(int i = 0; i < REG16(CX); i++) {
                   13169:                        int logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   13170:                        int physical = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   13171:                        
                   13172:                        if(REG8(AL) == 0x01) {
                   13173:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   13174:                        }
                   13175: //                     if(!(physical < 4)) {
                   13176: //                             REG8(AH) = 0x8b;
                   13177: //                             return;
                   13178: //                     } else
                   13179:                        if(logical == 0xffff) {
                   13180:                                ems_unmap_page(physical & 3);
                   13181:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   13182:                                ems_map_page(physical & 3, REG16(DX), logical);
                   13183:                        } else {
                   13184:                                REG8(AH) = 0x8a;
                   13185:                                return;
                   13186:                        }
                   13187:                }
                   13188:                REG8(AH) = 0x00;
                   13189:        } else {
1.1.1.22  root     13190:                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     13191:                REG8(AH) = 0x8f;
                   13192:        }
                   13193: }
                   13194: 
1.1.1.19  root     13195: inline void msdos_int_67h_51h()
                   13196: {
                   13197:        if(!support_ems) {
                   13198:                REG8(AH) = 0x84;
1.1.1.31  root     13199:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     13200:                REG8(AH) = 0x83;
                   13201:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   13202:                REG8(AH) = 0x87;
                   13203:        } else if(REG16(BX) > free_ems_pages + ems_handles[REG16(DX)].pages) {
                   13204:                REG8(AH) = 0x88;
                   13205:        } else {
                   13206:                ems_reallocate_pages(REG16(DX), REG16(BX));
                   13207:                REG8(AH) = 0x00;
                   13208:        }
                   13209: }
                   13210: 
1.1.1.20  root     13211: inline void msdos_int_67h_52h()
                   13212: {
                   13213:        if(!support_ems) {
                   13214:                REG8(AH) = 0x84;
1.1.1.31  root     13215: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   13216: //             REG8(AH) = 0x83;
1.1.1.20  root     13217:        } else if(REG8(AL) == 0x00) {
                   13218:                REG8(AL) = 0x00; // handle is volatile
                   13219:                REG8(AH) = 0x00;
                   13220:        } else if(REG8(AL) == 0x01) {
                   13221:                if(REG8(BL) == 0x00) {
                   13222:                        REG8(AH) = 0x00;
                   13223:                } else {
                   13224:                        REG8(AH) = 0x90; // undefined attribute type
                   13225:                }
                   13226:        } else if(REG8(AL) == 0x02) {
                   13227:                REG8(AL) = 0x00; // only volatile handles supported
                   13228:                REG8(AH) = 0x00;
                   13229:        } else {
1.1.1.22  root     13230:                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     13231:                REG8(AH) = 0x8f;
                   13232:        }
                   13233: }
                   13234: 
1.1.1.19  root     13235: inline void msdos_int_67h_53h()
                   13236: {
                   13237:        if(!support_ems) {
                   13238:                REG8(AH) = 0x84;
1.1.1.31  root     13239:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     13240:                REG8(AH) = 0x83;
                   13241:        } else if(REG8(AL) == 0x00) {
                   13242:                memcpy(mem + SREG_BASE(ES) + REG16(DI), ems_handles[REG16(DX)].name, 8);
                   13243:                REG8(AH) = 0x00;
                   13244:        } else if(REG8(AL) == 0x01) {
1.1.1.31  root     13245:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     13246:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   13247:                                REG8(AH) = 0xa1;
                   13248:                                return;
                   13249:                        }
                   13250:                }
                   13251:                REG8(AH) = 0x00;
                   13252:                memcpy(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8);
                   13253:        } else {
1.1.1.22  root     13254:                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     13255:                REG8(AH) = 0x8f;
1.1.1.19  root     13256:        }
                   13257: }
                   13258: 
                   13259: inline void msdos_int_67h_54h()
                   13260: {
                   13261:        if(!support_ems) {
                   13262:                REG8(AH) = 0x84;
                   13263:        } else if(REG8(AL) == 0x00) {
1.1.1.31  root     13264:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     13265:                        if(ems_handles[i].allocated) {
                   13266:                                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, ems_handles[i].name, 10);
                   13267:                        } else {
                   13268:                                memset(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, 0, 10);
                   13269:                        }
                   13270:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 0) = i;
                   13271:                }
                   13272:                REG8(AH) = 0x00;
                   13273:                REG8(AL) = MAX_EMS_HANDLES;
                   13274:        } else if(REG8(AL) == 0x01) {
                   13275:                REG8(AH) = 0xa0; // not found
1.1.1.31  root     13276:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     13277:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   13278:                                REG8(AH) = 0x00;
                   13279:                                REG16(DX) = i;
                   13280:                                break;
                   13281:                        }
                   13282:                }
                   13283:        } else if(REG8(AL) == 0x02) {
                   13284:                REG8(AH) = 0x00;
                   13285:                REG16(BX) = MAX_EMS_HANDLES;
                   13286:        } else {
1.1.1.22  root     13287:                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     13288:                REG8(AH) = 0x8f;
                   13289:        }
                   13290: }
                   13291: 
                   13292: inline void msdos_int_67h_57h_tmp()
                   13293: {
                   13294:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   13295:        UINT8  src_type    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   13296:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x05);
                   13297:        UINT16 src_ofs     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07);
                   13298:        UINT16 src_seg     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x09);
                   13299:        UINT8  dest_type   = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0b);
                   13300:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   13301:        UINT16 dest_ofs    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0e);
                   13302:        UINT16 dest_seg    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x10);
                   13303:        
1.1.1.32  root     13304:        UINT8 *src_buffer = NULL, *dest_buffer = NULL;
1.1.1.20  root     13305:        UINT32 src_addr, dest_addr;
                   13306:        UINT32 src_addr_max, dest_addr_max;
                   13307:        
                   13308:        if(src_type == 0) {
                   13309:                src_buffer = mem;
                   13310:                src_addr = (src_seg << 4) + src_ofs;
                   13311:                src_addr_max = MAX_MEM;
                   13312:        } else {
1.1.1.31  root     13313:                if(!(src_handle >= 1 && src_handle <= MAX_EMS_HANDLES && ems_handles[src_handle].allocated)) {
1.1.1.20  root     13314:                        REG8(AH) = 0x83;
                   13315:                        return;
                   13316:                } else if(!(src_seg < ems_handles[src_handle].pages)) {
                   13317:                        REG8(AH) = 0x8a;
                   13318:                        return;
                   13319:                }
1.1.1.32  root     13320:                if(ems_handles[src_handle].buffer != NULL) {
                   13321:                        src_buffer = ems_handles[src_handle].buffer + 0x4000 * src_seg;
                   13322:                }
1.1.1.20  root     13323:                src_addr = src_ofs;
1.1.1.32  root     13324:                src_addr_max = 0x4000 * (ems_handles[src_handle].pages - src_seg);
1.1.1.20  root     13325:        }
                   13326:        if(dest_type == 0) {
                   13327:                dest_buffer = mem;
                   13328:                dest_addr = (dest_seg << 4) + dest_ofs;
                   13329:                dest_addr_max = MAX_MEM;
                   13330:        } else {
1.1.1.31  root     13331:                if(!(dest_handle >= 1 && dest_handle <= MAX_EMS_HANDLES && ems_handles[dest_handle].allocated)) {
1.1.1.20  root     13332:                        REG8(AH) = 0x83;
                   13333:                        return;
                   13334:                } else if(!(dest_seg < ems_handles[dest_handle].pages)) {
                   13335:                        REG8(AH) = 0x8a;
                   13336:                        return;
                   13337:                }
1.1.1.32  root     13338:                if(ems_handles[dest_handle].buffer != NULL) {
                   13339:                        dest_buffer = ems_handles[dest_handle].buffer + 0x4000 * dest_seg;
                   13340:                }
1.1.1.20  root     13341:                dest_addr = dest_ofs;
1.1.1.32  root     13342:                dest_addr_max = 0x4000 * (ems_handles[dest_handle].pages - dest_seg);
1.1.1.20  root     13343:        }
1.1.1.32  root     13344:        if(src_buffer != NULL && dest_buffer != NULL) {
                   13345:                for(int i = 0; i < copy_length; i++) {
                   13346:                        if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   13347:                                if(REG8(AL) == 0x00) {
                   13348:                                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   13349:                                } else if(REG8(AL) == 0x01) {
                   13350:                                        UINT8 tmp = dest_buffer[dest_addr];
                   13351:                                        dest_buffer[dest_addr++] = src_buffer[src_addr];
                   13352:                                        src_buffer[src_addr++] = tmp;
                   13353:                                }
                   13354:                        } else {
                   13355:                                REG8(AH) = 0x93;
                   13356:                                return;
1.1.1.20  root     13357:                        }
                   13358:                }
1.1.1.32  root     13359:                REG8(AH) = 0x00;
                   13360:        } else {
                   13361:                REG8(AH) = 0x80;
1.1.1.20  root     13362:        }
                   13363: }
                   13364: 
                   13365: inline void msdos_int_67h_57h()
                   13366: {
                   13367:        if(!support_ems) {
                   13368:                REG8(AH) = 0x84;
                   13369:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   13370:                struct {
                   13371:                        UINT16 handle;
                   13372:                        UINT16 page;
                   13373:                        bool mapped;
                   13374:                } tmp_pages[4];
                   13375:                
                   13376:                // unmap pages to copy memory data to ems buffer
                   13377:                for(int i = 0; i < 4; i++) {
                   13378:                        tmp_pages[i].handle = ems_pages[i].handle;
                   13379:                        tmp_pages[i].page   = ems_pages[i].page;
                   13380:                        tmp_pages[i].mapped = ems_pages[i].mapped;
                   13381:                        ems_unmap_page(i);
                   13382:                }
                   13383:                
                   13384:                // run move/exchange operation
                   13385:                msdos_int_67h_57h_tmp();
                   13386:                
                   13387:                // restore unmapped pages
                   13388:                for(int i = 0; i < 4; i++) {
                   13389:                        if(tmp_pages[i].mapped) {
                   13390:                                ems_map_page(i, tmp_pages[i].handle, tmp_pages[i].page);
                   13391:                        }
                   13392:                }
                   13393:        } else {
1.1.1.22  root     13394:                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     13395:                REG8(AH) = 0x8f;
                   13396:        }
                   13397: }
                   13398: 
                   13399: inline void msdos_int_67h_58h()
                   13400: {
                   13401:        if(!support_ems) {
                   13402:                REG8(AH) = 0x84;
                   13403:        } else if(REG8(AL) == 0x00) {
                   13404:                for(int i = 0; i < 4; i++) {
1.1.1.30  root     13405:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = (EMS_TOP + 0x4000 * i) >> 4;
                   13406:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = i;
1.1.1.20  root     13407:                }
                   13408:                REG8(AH) = 0x00;
                   13409:                REG16(CX) = 4;
                   13410:        } else if(REG8(AL) == 0x01) {
                   13411:                REG8(AH) = 0x00;
                   13412:                REG16(CX) = 4;
                   13413:        } else {
1.1.1.22  root     13414:                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     13415:                REG8(AH) = 0x8f;
                   13416:        }
                   13417: }
                   13418: 
                   13419: inline void msdos_int_67h_5ah()
                   13420: {
                   13421:        if(!support_ems) {
1.1.1.19  root     13422:                REG8(AH) = 0x84;
1.1.1.20  root     13423:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   13424:                REG8(AH) = 0x87;
                   13425:        } else if(REG16(BX) > free_ems_pages) {
                   13426:                REG8(AH) = 0x88;
                   13427: //     } else if(REG16(BX) == 0) {
                   13428: //             REG8(AH) = 0x89;
                   13429:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
1.1.1.31  root     13430:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.20  root     13431:                        if(!ems_handles[i].allocated) {
                   13432:                                ems_allocate_pages(i, REG16(BX));
                   13433:                                REG8(AH) = 0x00;
                   13434:                                REG16(DX) = i;
                   13435:                                return;
                   13436:                        }
                   13437:                }
                   13438:                REG8(AH) = 0x85;
                   13439:        } else {
1.1.1.22  root     13440:                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     13441:                REG8(AH) = 0x8f;
1.1.1.19  root     13442:        }
                   13443: }
                   13444: 
1.1.1.30  root     13445: inline void msdos_int_67h_deh()
                   13446: {
                   13447:        REG8(AH) = 0x84;
                   13448: }
                   13449: 
1.1.1.19  root     13450: #ifdef SUPPORT_XMS
                   13451: 
1.1.1.32  root     13452: void msdos_xms_init()
1.1.1.26  root     13453: {
1.1.1.30  root     13454:        emb_handle_top = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   13455:        emb_handle_top->address = EMB_TOP;
                   13456:        emb_handle_top->size_kb = (EMB_END - EMB_TOP) >> 10;
1.1.1.26  root     13457:        xms_a20_local_enb_count = 0;
                   13458: }
                   13459: 
1.1.1.32  root     13460: void msdos_xms_finish()
                   13461: {
                   13462:        msdos_xms_release();
                   13463: }
                   13464: 
                   13465: void msdos_xms_release()
1.1.1.30  root     13466: {
                   13467:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL;) {
                   13468:                emb_handle_t *next_handle = emb_handle->next;
                   13469:                free(emb_handle);
                   13470:                emb_handle = next_handle;
                   13471:        }
                   13472: }
                   13473: 
                   13474: emb_handle_t *msdos_xms_get_emb_handle(int handle)
                   13475: {
                   13476:        if(handle != 0) {
                   13477:                for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   13478:                        if(emb_handle->handle == handle) {
                   13479:                                return(emb_handle);
                   13480:                        }
                   13481:                }
                   13482:        }
                   13483:        return(NULL);
                   13484: }
                   13485: 
                   13486: int msdos_xms_get_unused_emb_handle_id()
                   13487: {
                   13488:        for(int handle = 1;; handle++) {
                   13489:                if(msdos_xms_get_emb_handle(handle) == NULL) {
                   13490:                        return(handle);
                   13491:                }
                   13492:        }
                   13493:        return(0);
                   13494: }
                   13495: 
                   13496: int msdos_xms_get_unused_emb_handle_count()
                   13497: {
                   13498:        int count = 64; //255;
                   13499:        
                   13500:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   13501:                if(emb_handle->handle != 0) {
                   13502:                        if(--count == 1) {
                   13503:                                break;
                   13504:                        }
                   13505:                }
                   13506:        }
                   13507:        return(count);
                   13508: }
                   13509: 
                   13510: void msdos_xms_split_emb_handle(emb_handle_t *emb_handle, int size_kb)
                   13511: {
                   13512:        if(emb_handle->size_kb > size_kb) {
                   13513:                emb_handle_t *new_handle = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   13514:                
                   13515:                new_handle->address = emb_handle->address + size_kb * 1024;
                   13516:                new_handle->size_kb = emb_handle->size_kb - size_kb;
                   13517:                emb_handle->size_kb = size_kb;
                   13518:                
                   13519:                new_handle->prev = emb_handle;
                   13520:                new_handle->next = emb_handle->next;
                   13521:                if(emb_handle->next != NULL) {
                   13522:                        emb_handle->next->prev = new_handle;
                   13523:                }
                   13524:                emb_handle->next = new_handle;
                   13525:        }
                   13526: }
                   13527: 
                   13528: void msdos_xms_combine_emb_handles(emb_handle_t *emb_handle)
                   13529: {
                   13530:        emb_handle_t *next_handle = emb_handle->next;
                   13531:        
                   13532:        if(next_handle != NULL) {
                   13533:                emb_handle->size_kb += next_handle->size_kb;
                   13534:                
                   13535:                if(next_handle->next != NULL) {
                   13536:                        next_handle->next->prev = emb_handle;
                   13537:                }
                   13538:                emb_handle->next = next_handle->next;
                   13539:                free(next_handle);
                   13540:        }
                   13541: }
                   13542: 
                   13543: emb_handle_t *msdos_xms_alloc_emb_handle(int size_kb)
                   13544: {
                   13545:        emb_handle_t *target_handle = NULL;
                   13546:        
                   13547:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   13548:                if(emb_handle->handle == 0 && emb_handle->size_kb >= size_kb) {
                   13549:                        if(target_handle == NULL || target_handle->size_kb > emb_handle->size_kb) {
                   13550:                                target_handle = emb_handle;
                   13551:                        }
                   13552:                }
                   13553:        }
                   13554:        if(target_handle != NULL) {
                   13555:                if(target_handle->size_kb > size_kb) {
                   13556:                        msdos_xms_split_emb_handle(target_handle, size_kb);
                   13557:                }
                   13558: //             target_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   13559:                return(target_handle);
                   13560:        }
                   13561:        return(NULL);
                   13562: }
                   13563: 
                   13564: void msdos_xms_free_emb_handle(emb_handle_t *emb_handle)
                   13565: {
                   13566:        emb_handle_t *prev_handle = emb_handle->prev;
                   13567:        emb_handle_t *next_handle = emb_handle->next;
                   13568:        
                   13569:        if(prev_handle != NULL && prev_handle->handle == 0) {
                   13570:                msdos_xms_combine_emb_handles(prev_handle);
                   13571:                emb_handle = prev_handle;
                   13572:        }
                   13573:        if(next_handle != NULL && next_handle->handle == 0) {
                   13574:                msdos_xms_combine_emb_handles(emb_handle);
                   13575:        }
                   13576:        emb_handle->handle = 0;
                   13577: }
                   13578: 
1.1.1.19  root     13579: inline void msdos_call_xms_00h()
                   13580: {
1.1.1.29  root     13581: #if defined(HAS_I386)
                   13582:        REG16(AX) = 0x0300; // V3.00 (XMS Version)
                   13583:        REG16(BX) = 0x035f; // V3.95 (Driver Revision)
                   13584: #else
                   13585:        REG16(AX) = 0x0200; // V2.00 (XMS Version)
                   13586:        REG16(BX) = 0x0270; // V2.70 (Driver Revision)
                   13587: #endif
                   13588: //     REG16(DX) = 0x0000; // HMA does not exist
                   13589:        REG16(DX) = 0x0001; // HMA does exist
1.1.1.19  root     13590: }
                   13591: 
                   13592: inline void msdos_call_xms_01h()
                   13593: {
1.1.1.29  root     13594:        if(REG8(AL) == 0x40) {
                   13595:                // HIMEM.SYS will fail function 01h with error code 91h if AL=40h and
                   13596:                // DX=KB free extended memory returned by last call of function 08h
                   13597:                REG16(AX) = 0x0000;
                   13598:                REG8(BL) = 0x91;
                   13599:                REG16(DX) = xms_dx_after_call_08h;
                   13600:        } else if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   13601:                REG16(AX) = 0x0000;
                   13602:                REG8(BL) = 0x81; // Vdisk was detected
                   13603: #ifdef SUPPORT_HMA
                   13604:        } else if(is_hma_used_by_int_2fh) {
                   13605:                REG16(AX) = 0x0000;
                   13606:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   13607:        } else if(is_hma_used_by_xms) {
                   13608:                REG16(AX) = 0x0000;
                   13609:                REG8(BL) = 0x91; // HMA is already in use
                   13610:        } else {
                   13611:                REG16(AX) = 0x0001;
                   13612:                is_hma_used_by_xms = true;
                   13613: #else
                   13614:        } else {
                   13615:                REG16(AX) = 0x0000;
                   13616:                REG8(BL) = 0x91; // HMA is already in use
                   13617: #endif
                   13618:        }
1.1.1.19  root     13619: }
                   13620: 
                   13621: inline void msdos_call_xms_02h()
                   13622: {
1.1.1.29  root     13623:        if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   13624:                REG16(AX) = 0x0000;
                   13625:                REG8(BL) = 0x81; // Vdisk was detected
                   13626: #ifdef SUPPORT_HMA
                   13627:        } else if(is_hma_used_by_int_2fh) {
                   13628:                REG16(AX) = 0x0000;
                   13629:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   13630:        } else if(!is_hma_used_by_xms) {
                   13631:                REG16(AX) = 0x0000;
                   13632:                REG8(BL) = 0x93; // HMA is not allocated
                   13633:        } else {
                   13634:                REG16(AX) = 0x0001;
                   13635:                is_hma_used_by_xms = false;
                   13636:                // restore first free mcb in high memory area
                   13637:                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   13638: #else
                   13639:        } else {
                   13640:                REG16(AX) = 0x0000;
                   13641:                REG8(BL) = 0x91; // HMA is already in use
                   13642: #endif
                   13643:        }
1.1.1.19  root     13644: }
                   13645: 
                   13646: inline void msdos_call_xms_03h()
                   13647: {
                   13648:        i386_set_a20_line(1);
                   13649:        REG16(AX) = 0x0001;
                   13650:        REG8(BL) = 0x00;
                   13651: }
                   13652: 
                   13653: inline void msdos_call_xms_04h()
                   13654: {
1.1.1.21  root     13655:        i386_set_a20_line(0);
                   13656:        REG16(AX) = 0x0001;
                   13657:        REG8(BL) = 0x00;
1.1.1.19  root     13658: }
                   13659: 
                   13660: inline void msdos_call_xms_05h()
                   13661: {
                   13662:        i386_set_a20_line(1);
                   13663:        REG16(AX) = 0x0001;
                   13664:        REG8(BL) = 0x00;
1.1.1.21  root     13665:        xms_a20_local_enb_count++;
1.1.1.19  root     13666: }
                   13667: 
                   13668: void msdos_call_xms_06h()
                   13669: {
1.1.1.21  root     13670:        if(xms_a20_local_enb_count > 0) {
                   13671:                xms_a20_local_enb_count--;
                   13672:        }
                   13673:        if(xms_a20_local_enb_count == 0) {
1.1.1.19  root     13674:                i386_set_a20_line(0);
1.1.1.21  root     13675:        }
                   13676:        if((m_a20_mask >> 20) & 1) {
1.1.1.19  root     13677:                REG16(AX) = 0x0000;
                   13678:                REG8(BL) = 0x94;
1.1.1.21  root     13679:        } else {
                   13680:                REG16(AX) = 0x0001;
                   13681:                REG8(BL) = 0x00;
1.1.1.19  root     13682:        }
                   13683: }
                   13684: 
                   13685: inline void msdos_call_xms_07h()
                   13686: {
                   13687:        REG16(AX) = (m_a20_mask >> 20) & 1;
                   13688:        REG8(BL) = 0x00;
                   13689: }
                   13690: 
                   13691: inline void msdos_call_xms_08h()
                   13692: {
                   13693:        REG16(AX) = REG16(DX) = 0x0000;
                   13694:        
1.1.1.30  root     13695:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   13696:                if(emb_handle->handle == 0) {
                   13697:                        if(REG16(AX) < emb_handle->size_kb) {
                   13698:                                REG16(AX) = emb_handle->size_kb;
1.1.1.19  root     13699:                        }
1.1.1.30  root     13700:                        REG16(DX) += emb_handle->size_kb;
1.1.1.19  root     13701:                }
                   13702:        }
                   13703:        
                   13704:        if(REG16(AX) == 0 && REG16(DX) == 0) {
                   13705:                REG8(BL) = 0xa0;
                   13706:        } else {
                   13707:                REG8(BL) = 0x00;
                   13708:        }
1.1.1.29  root     13709:        xms_dx_after_call_08h = REG16(DX);
1.1.1.19  root     13710: }
                   13711: 
1.1.1.30  root     13712: void msdos_call_xms_09h(int size_kb)
1.1.1.19  root     13713: {
1.1.1.30  root     13714:        emb_handle_t *emb_handle = msdos_xms_alloc_emb_handle(size_kb);
                   13715:        
                   13716:        if(emb_handle != NULL) {
                   13717:                emb_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   13718:                
                   13719:                REG16(AX) = 0x0001;
                   13720:                REG16(DX) = emb_handle->handle;
                   13721:                REG8(BL) = 0x00;
                   13722:        } else {
                   13723:                REG16(AX) = REG16(DX) = 0x0000;
                   13724:                REG8(BL) = 0xa0;
1.1.1.19  root     13725:        }
1.1.1.30  root     13726: }
                   13727: 
                   13728: inline void msdos_call_xms_09h()
                   13729: {
                   13730:        msdos_call_xms_09h(REG16(DX));
1.1.1.19  root     13731: }
                   13732: 
                   13733: inline void msdos_call_xms_0ah()
                   13734: {
1.1.1.30  root     13735:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   13736:        
                   13737:        if(emb_handle == NULL) {
1.1.1.19  root     13738:                REG16(AX) = 0x0000;
                   13739:                REG8(BL) = 0xa2;
1.1.1.30  root     13740:        } else if(emb_handle->lock > 0) {
1.1.1.19  root     13741:                REG16(AX) = 0x0000;
                   13742:                REG8(BL) = 0xab;
                   13743:        } else {
1.1.1.30  root     13744:                msdos_xms_free_emb_handle(emb_handle);
1.1.1.19  root     13745:                
                   13746:                REG16(AX) = 0x0001;
                   13747:                REG8(BL) = 0x00;
                   13748:        }
                   13749: }
                   13750: 
                   13751: inline void msdos_call_xms_0bh()
                   13752: {
                   13753:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   13754:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   13755:        UINT32 src_addr    = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06);
                   13756:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0a);
                   13757:        UINT32 dest_addr   = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   13758:        
                   13759:        UINT8 *src_buffer, *dest_buffer;
                   13760:        UINT32 src_addr_max, dest_addr_max;
1.1.1.30  root     13761:        emb_handle_t *emb_handle;
1.1.1.19  root     13762:        
                   13763:        if(src_handle == 0) {
                   13764:                src_buffer = mem;
                   13765:                src_addr = (((src_addr >> 16) & 0xffff) << 4) + (src_addr & 0xffff);
                   13766:                src_addr_max = MAX_MEM;
1.1.1.30  root     13767: 
1.1.1.19  root     13768:        } else {
1.1.1.30  root     13769:                if((emb_handle = msdos_xms_get_emb_handle(src_handle)) == NULL) {
1.1.1.19  root     13770:                        REG16(AX) = 0x0000;
                   13771:                        REG8(BL) = 0xa3;
                   13772:                        return;
                   13773:                }
1.1.1.30  root     13774:                src_buffer = mem + emb_handle->address;
                   13775:                src_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     13776:        }
                   13777:        if(dest_handle == 0) {
                   13778:                dest_buffer = mem;
                   13779:                dest_addr = (((dest_addr >> 16) & 0xffff) << 4) + (dest_addr & 0xffff);
                   13780:                dest_addr_max = MAX_MEM;
                   13781:        } else {
1.1.1.30  root     13782:                if((emb_handle = msdos_xms_get_emb_handle(dest_handle)) == NULL) {
1.1.1.19  root     13783:                        REG16(AX) = 0x0000;
                   13784:                        REG8(BL) = 0xa5;
                   13785:                        return;
                   13786:                }
1.1.1.30  root     13787:                dest_buffer = mem + emb_handle->address;
                   13788:                dest_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     13789:        }
                   13790:        for(int i = 0; i < copy_length; i++) {
                   13791:                if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   13792:                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   13793:                } else {
                   13794:                        break;
                   13795:                }
                   13796:        }
                   13797:        REG16(AX) = 0x0001;
                   13798:        REG8(BL) = 0x00;
                   13799: }
                   13800: 
                   13801: inline void msdos_call_xms_0ch()
                   13802: {
1.1.1.30  root     13803:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   13804:        
                   13805:        if(emb_handle == NULL) {
1.1.1.19  root     13806:                REG16(AX) = 0x0000;
                   13807:                REG8(BL) = 0xa2;
                   13808:        } else {
1.1.1.30  root     13809:                emb_handle->lock++;
1.1.1.19  root     13810:                REG16(AX) = 0x0001;
                   13811:                REG8(BL) = 0x00;
1.1.1.30  root     13812:                REG16(DX) = (emb_handle->address >> 16) & 0xffff;
                   13813:                REG16(BX) = (emb_handle->address      ) & 0xffff;
1.1.1.19  root     13814:        }
                   13815: }
                   13816: 
                   13817: inline void msdos_call_xms_0dh()
                   13818: {
1.1.1.30  root     13819:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   13820:        
                   13821:        if(emb_handle == NULL) {
1.1.1.19  root     13822:                REG16(AX) = 0x0000;
                   13823:                REG8(BL) = 0xa2;
1.1.1.30  root     13824:        } else if(!(emb_handle->lock > 0)) {
1.1.1.19  root     13825:                REG16(AX) = 0x0000;
                   13826:                REG8(BL) = 0xaa;
                   13827:        } else {
1.1.1.30  root     13828:                emb_handle->lock--;
1.1.1.19  root     13829:                REG16(AX) = 0x0001;
                   13830:                REG8(BL) = 0x00;
                   13831:        }
                   13832: }
                   13833: 
                   13834: inline void msdos_call_xms_0eh()
                   13835: {
1.1.1.30  root     13836:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   13837:        
                   13838:        if(emb_handle == NULL) {
1.1.1.19  root     13839:                REG16(AX) = 0x0000;
                   13840:                REG8(BL) = 0xa2;
                   13841:        } else {
                   13842:                REG16(AX) = 0x0001;
1.1.1.30  root     13843:                REG8(BH) = emb_handle->lock;
                   13844:                REG8(BL) = msdos_xms_get_unused_emb_handle_count();
                   13845:                REG16(DX) = emb_handle->size_kb;
1.1.1.19  root     13846:        }
                   13847: }
                   13848: 
1.1.1.30  root     13849: void msdos_call_xms_0fh(int size_kb)
1.1.1.19  root     13850: {
1.1.1.30  root     13851:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   13852:        
                   13853:        if(emb_handle == NULL) {
1.1.1.19  root     13854:                REG16(AX) = 0x0000;
                   13855:                REG8(BL) = 0xa2;
1.1.1.30  root     13856:        } else if(emb_handle->lock > 0) {
1.1.1.19  root     13857:                REG16(AX) = 0x0000;
                   13858:                REG8(BL) = 0xab;
                   13859:        } else {
1.1.1.30  root     13860:                if(emb_handle->size_kb < size_kb) {
                   13861:                        if(emb_handle->next != NULL && emb_handle->next->handle == 0 && (emb_handle->size_kb + emb_handle->next->size_kb) >= size_kb) {
                   13862:                                msdos_xms_combine_emb_handles(emb_handle);
                   13863:                                if(emb_handle->size_kb > size_kb) {
                   13864:                                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   13865:                                }
                   13866:                        } else {
                   13867:                                int old_handle = emb_handle->handle;
                   13868:                                int old_size_kb = emb_handle->size_kb;
                   13869:                                UINT8 *buffer = (UINT8 *)malloc(old_size_kb * 1024);
                   13870:                                
                   13871:                                memcpy(buffer, mem + emb_handle->address, old_size_kb * 1024);
                   13872:                                msdos_xms_free_emb_handle(emb_handle);
                   13873:                                
                   13874:                                if((emb_handle = msdos_xms_alloc_emb_handle(size_kb)) == NULL) {
                   13875:                                        emb_handle = msdos_xms_alloc_emb_handle(old_size_kb); // should be always successed
                   13876:                                }
                   13877:                                emb_handle->handle = old_handle;
                   13878:                                memcpy(mem + emb_handle->address, buffer, old_size_kb * 1024);
                   13879:                                free(buffer);
                   13880:                        }
                   13881:                } else if(emb_handle->size_kb > size_kb) {
                   13882:                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   13883:                }
                   13884:                if(emb_handle->size_kb != size_kb) {
                   13885:                        REG16(AX) = 0x0000;
                   13886:                        REG8(BL) = 0xa0;
                   13887:                } else {
                   13888:                        REG16(AX) = 0x0001;
                   13889:                        REG8(BL) = 0x00;
                   13890:                }
1.1.1.19  root     13891:        }
                   13892: }
                   13893: 
1.1.1.30  root     13894: inline void msdos_call_xms_0fh()
                   13895: {
                   13896:        msdos_call_xms_0fh(REG16(BX));
                   13897: }
                   13898: 
1.1.1.19  root     13899: inline void msdos_call_xms_10h()
                   13900: {
                   13901:        int seg;
                   13902:        
                   13903:        if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(DX), 0)) != -1) {
                   13904:                REG16(AX) = 0x0001;
                   13905:                REG16(BX) = seg;
                   13906:        } else {
                   13907:                REG16(AX) = 0x0000;
                   13908:                REG8(BL) = 0xb0;
                   13909:                REG16(DX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   13910:        }
                   13911: }
                   13912: 
                   13913: inline void msdos_call_xms_11h()
                   13914: {
                   13915:        int mcb_seg = REG16(DX) - 1;
                   13916:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   13917:        
                   13918:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   13919:                msdos_mem_free(REG16(DX));
                   13920:                REG16(AX) = 0x0001;
                   13921:                REG8(BL) = 0x00;
                   13922:        } else {
                   13923:                REG16(AX) = 0x0000;
                   13924:                REG8(BL) = 0xb2;
                   13925:        }
                   13926: }
                   13927: 
                   13928: inline void msdos_call_xms_12h()
                   13929: {
                   13930:        int mcb_seg = REG16(DX) - 1;
                   13931:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   13932:        int max_paragraphs;
                   13933:        
                   13934:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   13935:                if(!msdos_mem_realloc(REG16(DX), REG16(BX), &max_paragraphs)) {
                   13936:                        REG16(AX) = 0x0001;
                   13937:                        REG8(BL) = 0x00;
                   13938:                } else {
                   13939:                        REG16(AX) = 0x0000;
                   13940:                        REG8(BL) = 0xb0;
                   13941:                        REG16(DX) = max_paragraphs;
                   13942:                }
                   13943:        } else {
                   13944:                REG16(AX) = 0x0000;
                   13945:                REG8(BL) = 0xb2;
                   13946:        }
                   13947: }
                   13948: 
1.1.1.29  root     13949: #if defined(HAS_I386)
                   13950: 
                   13951: inline void msdos_call_xms_88h()
                   13952: {
                   13953:        REG32(EAX) = REG32(EDX) = 0x0000;
                   13954:        
1.1.1.30  root     13955:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   13956:                if(emb_handle->handle == 0) {
                   13957:                        if(REG32(EAX) < emb_handle->size_kb) {
                   13958:                                REG32(EAX) = emb_handle->size_kb;
1.1.1.29  root     13959:                        }
1.1.1.30  root     13960:                        REG32(EDX) += emb_handle->size_kb;
1.1.1.29  root     13961:                }
                   13962:        }
                   13963:        
                   13964:        if(REG32(EAX) == 0 && REG32(EDX) == 0) {
                   13965:                REG8(BL) = 0xa0;
                   13966:        } else {
                   13967:                REG8(BL) = 0x00;
                   13968:        }
                   13969:        REG32(ECX) = EMB_END - 1;
                   13970: }
                   13971: 
                   13972: inline void msdos_call_xms_89h()
                   13973: {
1.1.1.30  root     13974:        msdos_call_xms_09h(REG32(EDX));
1.1.1.29  root     13975: }
                   13976: 
                   13977: inline void msdos_call_xms_8eh()
                   13978: {
1.1.1.30  root     13979:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   13980:        
                   13981:        if(emb_handle == NULL) {
1.1.1.29  root     13982:                REG16(AX) = 0x0000;
                   13983:                REG8(BL) = 0xa2;
                   13984:        } else {
                   13985:                REG16(AX) = 0x0001;
1.1.1.30  root     13986:                REG8(BH) = emb_handle->lock;
                   13987:                REG16(CX) = msdos_xms_get_unused_emb_handle_count();
                   13988:                REG32(EDX) = emb_handle->size_kb;
1.1.1.29  root     13989:        }
                   13990: }
                   13991: 
                   13992: inline void msdos_call_xms_8fh()
                   13993: {
1.1.1.30  root     13994:        msdos_call_xms_0fh(REG32(EBX));
1.1.1.29  root     13995: }
                   13996: 
                   13997: #endif
1.1.1.19  root     13998: #endif
                   13999: 
1.1.1.26  root     14000: UINT16 msdos_get_equipment()
                   14001: {
                   14002:        static UINT16 equip = 0;
                   14003:        
                   14004:        if(equip == 0) {
                   14005: #ifdef SUPPORT_FPU
                   14006:                equip |= (1 << 1);      // 80x87 coprocessor installed
                   14007: #endif
                   14008:                equip |= (1 << 2);      // pointing device installed (PS/2)
                   14009:                equip |= (2 << 4);      // initial video mode (80x25 color)
                   14010: //             equip |= (1 << 8);      // 0 if DMA installed
                   14011:                equip |= (2 << 9);      // number of serial ports
                   14012:                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     14013:                
                   14014:                // check only A: and B: if it is floppy drive
                   14015:                DWORD dwDrives = GetLogicalDrives();
                   14016:                int n = 0;
                   14017:                for(int i = 0; i < 2; i++) {
                   14018:                        if(dwDrives & (1 << i)) {
                   14019:                                char volume[] = "A:\\";
                   14020:                                volume[0] = 'A' + i;
                   14021:                                if(GetDriveType(volume) == DRIVE_REMOVABLE) {
                   14022:                                        n++;
                   14023:                                }
                   14024:                        }
                   14025:                }
                   14026:                if(n != 0) {
                   14027:                        equip |= (1 << 0);      // floppy disk(s) installed
                   14028:                        n--;
                   14029:                        equip |= (n << 6);      // number of floppies installed less 1
                   14030:                }
                   14031: //             if(joyGetNumDevs() != 0) {
                   14032: //                     equip |= (1 << 12);     // game port installed
                   14033: //             }
1.1.1.26  root     14034:        }
                   14035:        return(equip);
                   14036: }
                   14037: 
1.1       root     14038: void msdos_syscall(unsigned num)
                   14039: {
1.1.1.22  root     14040: #ifdef ENABLE_DEBUG_SYSCALL
                   14041:        if(num == 0x68) {
                   14042:                // dummy interrupt for EMS (int 67h)
1.1.1.33  root     14043:                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     14044:        } else if(num == 0x69) {
                   14045:                // dummy interrupt for XMS (call far)
1.1.1.33  root     14046:                fprintf(fp_debug_log, "call XMS (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.22  root     14047:        } else if(num == 0x6a) {
                   14048:                // dummy interrupt for case map routine pointed in the country info
                   14049:        } else {
1.1.1.33  root     14050:                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     14051:        }
                   14052: #endif
1.1.1.36  root     14053:        // update cursor position
                   14054:        if(cursor_moved) {
                   14055:                pcbios_update_cursor_position();
                   14056:                cursor_moved = false;
                   14057:        }
1.1.1.33  root     14058:        ctrl_break_detected = ctrl_break_pressed = ctrl_c_pressed = false;
1.1.1.22  root     14059:        
1.1       root     14060:        switch(num) {
                   14061:        case 0x00:
1.1.1.28  root     14062:                try {
                   14063:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   14064:                        error("division by zero\n");
                   14065:                } catch(...) {
                   14066:                        fatalerror("division by zero detected, and failed to terminate current process\n");
                   14067:                }
1.1       root     14068:                break;
                   14069:        case 0x04:
1.1.1.28  root     14070:                try {
                   14071:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   14072:                        error("overflow\n");
                   14073:                } catch(...) {
                   14074:                        fatalerror("overflow detected, and failed to terminate current process\n");
                   14075:                }
1.1       root     14076:                break;
                   14077:        case 0x06:
                   14078:                // NOTE: ish.com has illegal instruction...
1.1.1.14  root     14079:                if(!ignore_illegal_insn) {
1.1.1.28  root     14080:                        try {
                   14081:                                msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   14082:                                error("illegal instruction\n");
                   14083:                        } catch(...) {
                   14084:                                fatalerror("illegal instruction detected, and failed to terminate current process\n");
                   14085:                        }
1.1.1.14  root     14086:                } else {
                   14087: #if defined(HAS_I386)
                   14088:                        m_eip++;
                   14089: #else
                   14090:                        m_pc++;
                   14091: #endif
                   14092:                }
1.1       root     14093:                break;
1.1.1.33  root     14094:        case 0x09:
                   14095:                // ctrl-break is pressed
                   14096:                if(raise_int_1bh) {
                   14097: #if defined(HAS_I386)
                   14098:                        m_ext = 0; // not an external interrupt
                   14099:                        i386_trap(0x1b, 1, 0);
                   14100:                        m_ext = 1;
                   14101: #else
                   14102:                        PREFIX86(_interrupt)(0x1b);
                   14103: #endif
                   14104:                        raise_int_1bh = false;
                   14105:                }
1.1.1.8   root     14106:        case 0x08:
1.1.1.14  root     14107: //             pcbios_irq0(); // this causes too slow emulation...
1.1.1.8   root     14108:        case 0x0b:
                   14109:        case 0x0c:
                   14110:        case 0x0d:
                   14111:        case 0x0e:
                   14112:        case 0x0f:
                   14113:                // EOI
                   14114:                pic[0].isr &= ~(1 << (num - 0x08));
                   14115:                pic_update();
                   14116:                break;
1.1       root     14117:        case 0x10:
                   14118:                // PC BIOS - Video
1.1.1.14  root     14119:                if(!restore_console_on_exit) {
1.1.1.15  root     14120:                        change_console_size(scr_width, scr_height);
1.1       root     14121:                }
1.1.1.3   root     14122:                m_CF = 0;
1.1       root     14123:                switch(REG8(AH)) {
1.1.1.16  root     14124:                case 0x00: pcbios_int_10h_00h(); break;
1.1       root     14125:                case 0x01: pcbios_int_10h_01h(); break;
                   14126:                case 0x02: pcbios_int_10h_02h(); break;
                   14127:                case 0x03: pcbios_int_10h_03h(); break;
                   14128:                case 0x05: pcbios_int_10h_05h(); break;
                   14129:                case 0x06: pcbios_int_10h_06h(); break;
                   14130:                case 0x07: pcbios_int_10h_07h(); break;
                   14131:                case 0x08: pcbios_int_10h_08h(); break;
                   14132:                case 0x09: pcbios_int_10h_09h(); break;
                   14133:                case 0x0a: pcbios_int_10h_0ah(); break;
                   14134:                case 0x0b: break;
                   14135:                case 0x0c: break;
                   14136:                case 0x0d: break;
                   14137:                case 0x0e: pcbios_int_10h_0eh(); break;
                   14138:                case 0x0f: pcbios_int_10h_0fh(); break;
                   14139:                case 0x10: break;
1.1.1.14  root     14140:                case 0x11: pcbios_int_10h_11h(); break;
                   14141:                case 0x12: pcbios_int_10h_12h(); break;
1.1       root     14142:                case 0x13: pcbios_int_10h_13h(); break;
1.1.1.30  root     14143:                case 0x18: pcbios_int_10h_18h(); break;
1.1.1.14  root     14144:                case 0x1a: pcbios_int_10h_1ah(); break;
1.1.1.24  root     14145:                case 0x1b: REG8(AL) = 0x00; break; // functionality/state information is not supported
                   14146:                case 0x1c: REG8(AL) = 0x00; break; // save/restore video state is not supported
1.1       root     14147:                case 0x1d: pcbios_int_10h_1dh(); break;
1.1.1.24  root     14148:                case 0x1e: REG8(AL) = 0x00; break; // flat-panel functions are not supported
                   14149:                case 0x1f: REG8(AL) = 0x00; break; // xga functions are not supported
1.1.1.22  root     14150:                case 0x4f: pcbios_int_10h_4fh(); break;
1.1.1.30  root     14151:                case 0x6f: break;
1.1.1.22  root     14152:                case 0x80: m_CF = 1; break; // unknown
                   14153:                case 0x81: m_CF = 1; break; // unknown
1.1       root     14154:                case 0x82: pcbios_int_10h_82h(); break;
1.1.1.22  root     14155:                case 0x83: pcbios_int_10h_83h(); break;
                   14156:                case 0x8b: break;
                   14157:                case 0x8c: m_CF = 1; break; // unknown
                   14158:                case 0x8d: m_CF = 1; break; // unknown
                   14159:                case 0x8e: m_CF = 1; break; // unknown
                   14160:                case 0x90: pcbios_int_10h_90h(); break;
                   14161:                case 0x91: pcbios_int_10h_91h(); break;
                   14162:                case 0x92: break;
                   14163:                case 0x93: break;
                   14164:                case 0xef: pcbios_int_10h_efh(); break;
1.1.1.24  root     14165:                case 0xfa: break; // ega register interface library is not installed
1.1       root     14166:                case 0xfe: pcbios_int_10h_feh(); break;
                   14167:                case 0xff: pcbios_int_10h_ffh(); break;
                   14168:                default:
1.1.1.22  root     14169:                        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));
                   14170:                        m_CF = 1;
1.1       root     14171:                        break;
                   14172:                }
                   14173:                break;
                   14174:        case 0x11:
                   14175:                // PC BIOS - Get Equipment List
1.1.1.26  root     14176:                REG16(AX) = msdos_get_equipment();
1.1       root     14177:                break;
                   14178:        case 0x12:
                   14179:                // PC BIOS - Get Memory Size
1.1.1.33  root     14180:                REG16(AX) = *(UINT16 *)(mem + 0x413);
1.1       root     14181:                break;
                   14182:        case 0x13:
                   14183:                // PC BIOS - Disk
1.1.1.22  root     14184: //             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     14185:                REG8(AH) = 0xff;
1.1.1.3   root     14186:                m_CF = 1;
1.1       root     14187:                break;
                   14188:        case 0x14:
                   14189:                // PC BIOS - Serial I/O
1.1.1.25  root     14190:                switch(REG8(AH)) {
                   14191:                case 0x00: pcbios_int_14h_00h(); break;
                   14192:                case 0x01: pcbios_int_14h_01h(); break;
                   14193:                case 0x02: pcbios_int_14h_02h(); break;
                   14194:                case 0x03: pcbios_int_14h_03h(); break;
                   14195:                case 0x04: pcbios_int_14h_04h(); break;
                   14196:                case 0x05: pcbios_int_14h_05h(); break;
                   14197:                default:
                   14198:                        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));
                   14199:                        break;
                   14200:                }
1.1       root     14201:                break;
                   14202:        case 0x15:
                   14203:                // PC BIOS
1.1.1.3   root     14204:                m_CF = 0;
1.1       root     14205:                switch(REG8(AH)) {
1.1.1.14  root     14206:                case 0x10: pcbios_int_15h_10h(); break;
1.1       root     14207:                case 0x23: pcbios_int_15h_23h(); break;
                   14208:                case 0x24: pcbios_int_15h_24h(); break;
1.1.1.24  root     14209:                case 0x41: break;
1.1       root     14210:                case 0x49: pcbios_int_15h_49h(); break;
1.1.1.22  root     14211:                case 0x50: pcbios_int_15h_50h(); break;
1.1.1.30  root     14212:                case 0x53: pcbios_int_15h_53h(); break;
1.1       root     14213:                case 0x86: pcbios_int_15h_86h(); break;
                   14214:                case 0x87: pcbios_int_15h_87h(); break;
                   14215:                case 0x88: pcbios_int_15h_88h(); break;
                   14216:                case 0x89: pcbios_int_15h_89h(); break;
1.1.1.21  root     14217:                case 0x8a: pcbios_int_15h_8ah(); break;
1.1.1.22  root     14218:                case 0xc0: // PS/2 ???
                   14219:                case 0xc1:
                   14220:                case 0xc2:
1.1.1.30  root     14221:                case 0xc3: // PS50+ ???
                   14222:                case 0xc4:
1.1.1.22  root     14223:                        REG8(AH) = 0x86;
                   14224:                        m_CF = 1;
                   14225:                        break;
1.1.1.3   root     14226: #if defined(HAS_I386)
1.1       root     14227:                case 0xc9: pcbios_int_15h_c9h(); break;
1.1.1.3   root     14228: #endif
1.1       root     14229:                case 0xca: pcbios_int_15h_cah(); break;
1.1.1.22  root     14230:                case 0xe8: pcbios_int_15h_e8h(); break;
1.1       root     14231:                default:
1.1.1.22  root     14232:                        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));
                   14233:                        REG8(AH) = 0x86;
1.1.1.3   root     14234:                        m_CF = 1;
1.1       root     14235:                        break;
                   14236:                }
                   14237:                break;
                   14238:        case 0x16:
                   14239:                // PC BIOS - Keyboard
1.1.1.3   root     14240:                m_CF = 0;
1.1       root     14241:                switch(REG8(AH)) {
                   14242:                case 0x00: pcbios_int_16h_00h(); break;
                   14243:                case 0x01: pcbios_int_16h_01h(); break;
                   14244:                case 0x02: pcbios_int_16h_02h(); break;
                   14245:                case 0x03: pcbios_int_16h_03h(); break;
                   14246:                case 0x05: pcbios_int_16h_05h(); break;
                   14247:                case 0x10: pcbios_int_16h_00h(); break;
                   14248:                case 0x11: pcbios_int_16h_01h(); break;
                   14249:                case 0x12: pcbios_int_16h_12h(); break;
                   14250:                case 0x13: pcbios_int_16h_13h(); break;
                   14251:                case 0x14: pcbios_int_16h_14h(); break;
1.1.1.24  root     14252:                case 0x55: pcbios_int_16h_55h(); break;
1.1.1.30  root     14253:                case 0x6f: pcbios_int_16h_6fh(); break;
1.1.1.22  root     14254:                case 0xda: break; // unknown
                   14255:                case 0xff: break; // unknown
1.1       root     14256:                default:
1.1.1.22  root     14257:                        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     14258:                        break;
                   14259:                }
                   14260:                break;
                   14261:        case 0x17:
                   14262:                // PC BIOS - Printer
1.1.1.37! root     14263:                m_CF = 0;
        !          14264:                switch(REG8(AH)) {
        !          14265:                case 0x00: pcbios_int_17h_00h(); break;
        !          14266:                case 0x01: pcbios_int_17h_01h(); break;
        !          14267:                case 0x02: pcbios_int_17h_02h(); break;
        !          14268:                case 0x03: pcbios_int_17h_03h(); break;
        !          14269:                case 0x50: pcbios_int_17h_50h(); break;
        !          14270:                case 0x51: pcbios_int_17h_51h(); break;
        !          14271:                case 0x52: pcbios_int_17h_52h(); break;
        !          14272:                case 0x84: pcbios_int_17h_84h(); break;
        !          14273:                case 0x85: pcbios_int_17h_85h(); break;
        !          14274:                default:
        !          14275:                        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));
        !          14276:                        break;
        !          14277:                }
1.1       root     14278:                break;
                   14279:        case 0x1a:
                   14280:                // PC BIOS - Timer
1.1.1.3   root     14281:                m_CF = 0;
1.1       root     14282:                switch(REG8(AH)) {
                   14283:                case 0x00: pcbios_int_1ah_00h(); break;
                   14284:                case 0x01: break;
                   14285:                case 0x02: pcbios_int_1ah_02h(); break;
                   14286:                case 0x03: break;
                   14287:                case 0x04: pcbios_int_1ah_04h(); break;
                   14288:                case 0x05: break;
                   14289:                case 0x0a: pcbios_int_1ah_0ah(); break;
                   14290:                case 0x0b: break;
1.1.1.14  root     14291:                case 0x35: break; // Word Perfect Third Party Interface?
                   14292:                case 0x36: break; // Word Perfect Third Party Interface
                   14293:                case 0x70: break; // SNAP? (Simple Network Application Protocol)
1.1       root     14294:                default:
1.1.1.22  root     14295:                        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     14296:                        break;
                   14297:                }
                   14298:                break;
1.1.1.33  root     14299:        case 0x1b:
                   14300:                mem[0x471] = 0x00;
                   14301:                break;
1.1       root     14302:        case 0x20:
1.1.1.28  root     14303:                try {
                   14304:                        msdos_process_terminate(SREG(CS), retval, 1);
                   14305:                } catch(...) {
                   14306:                        fatalerror("failed to terminate the process (PSP=%04X) by int 20h\n", SREG(CS));
                   14307:                }
1.1       root     14308:                break;
                   14309:        case 0x21:
                   14310:                // MS-DOS System Call
1.1.1.3   root     14311:                m_CF = 0;
1.1.1.28  root     14312:                try {
                   14313:                        switch(REG8(AH)) {
                   14314:                        case 0x00: msdos_int_21h_00h(); break;
                   14315:                        case 0x01: msdos_int_21h_01h(); break;
                   14316:                        case 0x02: msdos_int_21h_02h(); break;
                   14317:                        case 0x03: msdos_int_21h_03h(); break;
                   14318:                        case 0x04: msdos_int_21h_04h(); break;
                   14319:                        case 0x05: msdos_int_21h_05h(); break;
                   14320:                        case 0x06: msdos_int_21h_06h(); break;
                   14321:                        case 0x07: msdos_int_21h_07h(); break;
                   14322:                        case 0x08: msdos_int_21h_08h(); break;
                   14323:                        case 0x09: msdos_int_21h_09h(); break;
                   14324:                        case 0x0a: msdos_int_21h_0ah(); break;
                   14325:                        case 0x0b: msdos_int_21h_0bh(); break;
                   14326:                        case 0x0c: msdos_int_21h_0ch(); break;
                   14327:                        case 0x0d: msdos_int_21h_0dh(); break;
                   14328:                        case 0x0e: msdos_int_21h_0eh(); break;
                   14329:                        case 0x0f: msdos_int_21h_0fh(); break;
                   14330:                        case 0x10: msdos_int_21h_10h(); break;
                   14331:                        case 0x11: msdos_int_21h_11h(); break;
                   14332:                        case 0x12: msdos_int_21h_12h(); break;
                   14333:                        case 0x13: msdos_int_21h_13h(); break;
                   14334:                        case 0x14: msdos_int_21h_14h(); break;
                   14335:                        case 0x15: msdos_int_21h_15h(); break;
                   14336:                        case 0x16: msdos_int_21h_16h(); break;
                   14337:                        case 0x17: msdos_int_21h_17h(); break;
                   14338:                        case 0x18: msdos_int_21h_18h(); break;
                   14339:                        case 0x19: msdos_int_21h_19h(); break;
                   14340:                        case 0x1a: msdos_int_21h_1ah(); break;
                   14341:                        case 0x1b: msdos_int_21h_1bh(); break;
                   14342:                        case 0x1c: msdos_int_21h_1ch(); break;
                   14343:                        case 0x1d: msdos_int_21h_1dh(); break;
                   14344:                        case 0x1e: msdos_int_21h_1eh(); break;
                   14345:                        case 0x1f: msdos_int_21h_1fh(); break;
                   14346:                        case 0x20: msdos_int_21h_20h(); break;
                   14347:                        case 0x21: msdos_int_21h_21h(); break;
                   14348:                        case 0x22: msdos_int_21h_22h(); break;
                   14349:                        case 0x23: msdos_int_21h_23h(); break;
                   14350:                        case 0x24: msdos_int_21h_24h(); break;
                   14351:                        case 0x25: msdos_int_21h_25h(); break;
                   14352:                        case 0x26: msdos_int_21h_26h(); break;
                   14353:                        case 0x27: msdos_int_21h_27h(); break;
                   14354:                        case 0x28: msdos_int_21h_28h(); break;
                   14355:                        case 0x29: msdos_int_21h_29h(); break;
                   14356:                        case 0x2a: msdos_int_21h_2ah(); break;
                   14357:                        case 0x2b: msdos_int_21h_2bh(); break;
                   14358:                        case 0x2c: msdos_int_21h_2ch(); break;
                   14359:                        case 0x2d: msdos_int_21h_2dh(); break;
                   14360:                        case 0x2e: msdos_int_21h_2eh(); break;
                   14361:                        case 0x2f: msdos_int_21h_2fh(); break;
                   14362:                        case 0x30: msdos_int_21h_30h(); break;
                   14363:                        case 0x31: msdos_int_21h_31h(); break;
                   14364:                        case 0x32: msdos_int_21h_32h(); break;
                   14365:                        case 0x33: msdos_int_21h_33h(); break;
                   14366:                        case 0x34: msdos_int_21h_34h(); break;
                   14367:                        case 0x35: msdos_int_21h_35h(); break;
                   14368:                        case 0x36: msdos_int_21h_36h(); break;
                   14369:                        case 0x37: msdos_int_21h_37h(); break;
                   14370:                        case 0x38: msdos_int_21h_38h(); break;
                   14371:                        case 0x39: msdos_int_21h_39h(0); break;
                   14372:                        case 0x3a: msdos_int_21h_3ah(0); break;
                   14373:                        case 0x3b: msdos_int_21h_3bh(0); break;
                   14374:                        case 0x3c: msdos_int_21h_3ch(); break;
                   14375:                        case 0x3d: msdos_int_21h_3dh(); break;
                   14376:                        case 0x3e: msdos_int_21h_3eh(); break;
                   14377:                        case 0x3f: msdos_int_21h_3fh(); break;
                   14378:                        case 0x40: msdos_int_21h_40h(); break;
                   14379:                        case 0x41: msdos_int_21h_41h(0); break;
                   14380:                        case 0x42: msdos_int_21h_42h(); break;
                   14381:                        case 0x43: msdos_int_21h_43h(0); break;
                   14382:                        case 0x44: msdos_int_21h_44h(); break;
                   14383:                        case 0x45: msdos_int_21h_45h(); break;
                   14384:                        case 0x46: msdos_int_21h_46h(); break;
                   14385:                        case 0x47: msdos_int_21h_47h(0); break;
                   14386:                        case 0x48: msdos_int_21h_48h(); break;
                   14387:                        case 0x49: msdos_int_21h_49h(); break;
                   14388:                        case 0x4a: msdos_int_21h_4ah(); break;
                   14389:                        case 0x4b: msdos_int_21h_4bh(); break;
                   14390:                        case 0x4c: msdos_int_21h_4ch(); break;
                   14391:                        case 0x4d: msdos_int_21h_4dh(); break;
                   14392:                        case 0x4e: msdos_int_21h_4eh(); break;
                   14393:                        case 0x4f: msdos_int_21h_4fh(); break;
                   14394:                        case 0x50: msdos_int_21h_50h(); break;
                   14395:                        case 0x51: msdos_int_21h_51h(); break;
                   14396:                        case 0x52: msdos_int_21h_52h(); break;
1.1.1.33  root     14397:                        // 0x53: Translate BIOS Parameter Block to Drive Param Bock
1.1.1.28  root     14398:                        case 0x54: msdos_int_21h_54h(); break;
                   14399:                        case 0x55: msdos_int_21h_55h(); break;
                   14400:                        case 0x56: msdos_int_21h_56h(0); break;
                   14401:                        case 0x57: msdos_int_21h_57h(); break;
                   14402:                        case 0x58: msdos_int_21h_58h(); break;
                   14403:                        case 0x59: msdos_int_21h_59h(); break;
                   14404:                        case 0x5a: msdos_int_21h_5ah(); break;
                   14405:                        case 0x5b: msdos_int_21h_5bh(); break;
                   14406:                        case 0x5c: msdos_int_21h_5ch(); break;
                   14407:                        case 0x5d: msdos_int_21h_5dh(); break;
1.1.1.33  root     14408:                        // 0x5e: MS-Network
1.1.1.30  root     14409:                        case 0x5f: msdos_int_21h_5fh(); break;
1.1.1.28  root     14410:                        case 0x60: msdos_int_21h_60h(0); break;
                   14411:                        case 0x61: msdos_int_21h_61h(); break;
                   14412:                        case 0x62: msdos_int_21h_62h(); break;
                   14413:                        case 0x63: msdos_int_21h_63h(); break;
1.1.1.33  root     14414:                        // 0x64: Set Device Driver Lockahead Flag
1.1.1.28  root     14415:                        case 0x65: msdos_int_21h_65h(); break;
                   14416:                        case 0x66: msdos_int_21h_66h(); break;
                   14417:                        case 0x67: msdos_int_21h_67h(); break;
                   14418:                        case 0x68: msdos_int_21h_68h(); break;
                   14419:                        case 0x69: msdos_int_21h_69h(); break;
                   14420:                        case 0x6a: msdos_int_21h_6ah(); break;
                   14421:                        case 0x6b: msdos_int_21h_6bh(); break;
                   14422:                        case 0x6c: msdos_int_21h_6ch(0); break;
1.1.1.33  root     14423:                        // 0x6d: Find First ROM Program
                   14424:                        // 0x6e: Find Next ROM Program
                   14425:                        // 0x6f: Get/Set ROM Scan Start Address
                   14426:                        // 0x70: Windows95 - Get/Set Internationalization Information
1.1.1.28  root     14427:                        case 0x71:
1.1.1.33  root     14428:                                // Windows95 - Long Filename Functions
1.1.1.28  root     14429:                                switch(REG8(AL)) {
                   14430:                                case 0x0d: msdos_int_21h_710dh(); break;
                   14431:                                case 0x39: msdos_int_21h_39h(1); break;
                   14432:                                case 0x3a: msdos_int_21h_3ah(1); break;
                   14433:                                case 0x3b: msdos_int_21h_3bh(1); break;
                   14434:                                case 0x41: msdos_int_21h_7141h(1); break;
                   14435:                                case 0x43: msdos_int_21h_43h(1); break;
                   14436:                                case 0x47: msdos_int_21h_47h(1); break;
                   14437:                                case 0x4e: msdos_int_21h_714eh(); break;
                   14438:                                case 0x4f: msdos_int_21h_714fh(); break;
                   14439:                                case 0x56: msdos_int_21h_56h(1); break;
                   14440:                                case 0x60: msdos_int_21h_60h(1); break;
                   14441:                                case 0x6c: msdos_int_21h_6ch(1); break;
                   14442:                                case 0xa0: msdos_int_21h_71a0h(); break;
                   14443:                                case 0xa1: msdos_int_21h_71a1h(); break;
                   14444:                                case 0xa6: msdos_int_21h_71a6h(); break;
                   14445:                                case 0xa7: msdos_int_21h_71a7h(); break;
                   14446:                                case 0xa8: msdos_int_21h_71a8h(); break;
1.1.1.33  root     14447:                                // 0xa9: Server Create/Open File
1.1.1.28  root     14448:                                case 0xaa: msdos_int_21h_71aah(); break;
                   14449:                                default:
                   14450:                                        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));
                   14451:                                        REG16(AX) = 0x7100;
                   14452:                                        m_CF = 1;
                   14453:                                        break;
                   14454:                                }
                   14455:                                break;
                   14456:                        // 0x72: Windows95 beta - LFN FindClose
                   14457:                        case 0x73:
1.1.1.33  root     14458:                                // Windows95 - FAT32 Functions
1.1.1.28  root     14459:                                switch(REG8(AL)) {
                   14460:                                case 0x00: msdos_int_21h_7300h(); break;
1.1.1.33  root     14461:                                // 0x01: Set Drive Locking ???
1.1.1.28  root     14462:                                case 0x02: msdos_int_21h_7302h(); break;
                   14463:                                case 0x03: msdos_int_21h_7303h(); break;
1.1.1.33  root     14464:                                // 0x04: Set DPB to Use for Formatting
                   14465:                                // 0x05: Extended Absolute Disk Read/Write
1.1.1.28  root     14466:                                default:
                   14467:                                        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));
                   14468:                                        REG16(AX) = 0x7300;
                   14469:                                        m_CF = 1;
                   14470:                                        break;
                   14471:                                }
1.1       root     14472:                                break;
1.1.1.30  root     14473:                        case 0xdb: msdos_int_21h_dbh(); break;
                   14474:                        case 0xdc: msdos_int_21h_dch(); break;
1.1       root     14475:                        default:
1.1.1.22  root     14476:                                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     14477:                                REG16(AX) = 0x01;
1.1.1.3   root     14478:                                m_CF = 1;
1.1       root     14479:                                break;
                   14480:                        }
1.1.1.28  root     14481:                } catch(int error) {
                   14482:                        REG16(AX) = error;
                   14483:                        m_CF = 1;
                   14484:                } catch(...) {
                   14485:                        REG16(AX) = 0x1f; // general failure
1.1.1.3   root     14486:                        m_CF = 1;
1.1       root     14487:                }
1.1.1.3   root     14488:                if(m_CF) {
1.1.1.23  root     14489:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   14490:                        sda->extended_error_code = REG16(AX);
                   14491:                        switch(sda->extended_error_code) {
                   14492:                        case  4: // Too many open files
                   14493:                        case  8: // Insufficient memory
                   14494:                                sda->error_class = 1; // Out of resource
                   14495:                                break;
                   14496:                        case  5: // Access denied
                   14497:                                sda->error_class = 3; // Authorization
                   14498:                                break;
                   14499:                        case  7: // Memory control block destroyed
                   14500:                                sda->error_class = 4; // Internal
                   14501:                                break;
                   14502:                        case  2: // File not found
                   14503:                        case  3: // Path not found
                   14504:                        case 15: // Invaid drive specified
                   14505:                        case 18: // No more files
                   14506:                                sda->error_class = 8; // Not found
                   14507:                                break;
                   14508:                        case 32: // Sharing violation
                   14509:                        case 33: // Lock violation
                   14510:                                sda->error_class = 10; // Locked
                   14511:                                break;
                   14512: //                     case 16: // Removal of current directory attempted
                   14513:                        case 19: // Attempted write on protected disk
                   14514:                        case 21: // Drive not ready
                   14515: //                     case 29: // Write failure
                   14516: //                     case 30: // Read failure
                   14517: //                     case 82: // Cannot create subdirectory
                   14518:                                sda->error_class = 11; // Media
                   14519:                                break;
                   14520:                        case 80: // File already exists
                   14521:                                sda->error_class = 12; // Already exist
                   14522:                                break;
                   14523:                        default:
                   14524:                                sda->error_class = 13; // Unknown
                   14525:                                break;
                   14526:                        }
                   14527:                        sda->suggested_action = 1; // Retry
                   14528:                        sda->locus_of_last_error = 1; // Unknown
1.1       root     14529:                }
1.1.1.33  root     14530:                if(ctrl_break_checking && ctrl_break_detected) {
1.1.1.26  root     14531:                        // raise int 23h
                   14532: #if defined(HAS_I386)
                   14533:                        m_ext = 0; // not an external interrupt
                   14534:                        i386_trap(0x23, 1, 0);
                   14535:                        m_ext = 1;
                   14536: #else
                   14537:                        PREFIX86(_interrupt)(0x23);
                   14538: #endif
                   14539:                }
1.1       root     14540:                break;
                   14541:        case 0x22:
                   14542:                fatalerror("int 22h (terminate address)\n");
                   14543:        case 0x23:
1.1.1.28  root     14544:                try {
                   14545:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x100, 1);
                   14546:                } catch(...) {
                   14547:                        fatalerror("failed to terminate the current process by int 23h\n");
                   14548:                }
1.1       root     14549:                break;
                   14550:        case 0x24:
1.1.1.32  root     14551: /*
1.1.1.28  root     14552:                try {
                   14553:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   14554:                } catch(...) {
                   14555:                        fatalerror("failed to terminate the current process by int 24h\n");
                   14556:                }
1.1.1.32  root     14557: */
                   14558:                msdos_int_24h();
1.1       root     14559:                break;
                   14560:        case 0x25:
                   14561:                msdos_int_25h();
                   14562:                break;
                   14563:        case 0x26:
                   14564:                msdos_int_26h();
                   14565:                break;
                   14566:        case 0x27:
1.1.1.28  root     14567:                try {
                   14568:                        msdos_int_27h();
                   14569:                } catch(...) {
                   14570:                        fatalerror("failed to terminate the process (PSP=%04X) by int 27h\n", SREG(CS));
                   14571:                }
1.1       root     14572:                break;
                   14573:        case 0x28:
                   14574:                Sleep(10);
1.1.1.35  root     14575:                REQUEST_HARDWRE_UPDATE();
1.1       root     14576:                break;
                   14577:        case 0x29:
                   14578:                msdos_int_29h();
                   14579:                break;
                   14580:        case 0x2e:
                   14581:                msdos_int_2eh();
                   14582:                break;
                   14583:        case 0x2f:
                   14584:                // multiplex interrupt
                   14585:                switch(REG8(AH)) {
1.1.1.22  root     14586:                case 0x05: msdos_int_2fh_05h(); break;
                   14587:                case 0x11: msdos_int_2fh_11h(); break;
1.1.1.21  root     14588:                case 0x12: msdos_int_2fh_12h(); break;
1.1.1.30  root     14589:                case 0x13: msdos_int_2fh_13h(); break;
1.1.1.22  root     14590:                case 0x14: msdos_int_2fh_14h(); break;
                   14591:                case 0x15: msdos_int_2fh_15h(); break;
1.1       root     14592:                case 0x16: msdos_int_2fh_16h(); break;
1.1.1.22  root     14593:                case 0x19: msdos_int_2fh_19h(); break;
1.1       root     14594:                case 0x1a: msdos_int_2fh_1ah(); break;
1.1.1.30  root     14595:                case 0x40: msdos_int_2fh_40h(); break;
1.1       root     14596:                case 0x43: msdos_int_2fh_43h(); break;
1.1.1.22  root     14597:                case 0x46: msdos_int_2fh_46h(); break;
                   14598:                case 0x48: msdos_int_2fh_48h(); break;
1.1       root     14599:                case 0x4a: msdos_int_2fh_4ah(); break;
1.1.1.22  root     14600:                case 0x4b: msdos_int_2fh_4bh(); break;
1.1       root     14601:                case 0x4f: msdos_int_2fh_4fh(); break;
1.1.1.22  root     14602:                case 0x55: msdos_int_2fh_55h(); break;
1.1.1.24  root     14603:                case 0xad: msdos_int_2fh_adh(); break;
1.1       root     14604:                case 0xae: msdos_int_2fh_aeh(); break;
1.1.1.34  root     14605:                case 0xb7: msdos_int_2fh_b7h(); break;
1.1.1.30  root     14606:                // Installation Check
                   14607:                case 0x01: // PRINT.COM
                   14608:                case 0x02: // PC LAN Program Redirector
                   14609:                case 0x06: // ASSIGN
                   14610:                case 0x08: // DRIVER.SYS
                   14611:                case 0x10: // SHARE
                   14612:                case 0x17: // Clibboard functions
                   14613:                case 0x1b: // XMA2EMS.SYS
                   14614:                case 0x23: // DR DOS 5.0 GRAFTABL
                   14615:                case 0x27: // DR-DOR 6.0 TaskMAX
                   14616:                case 0x2e: // Novell DOS 7 GRAFTABL
1.1.1.33  root     14617:                case 0x39: // Kingswood TSR INTERFACE
                   14618:                case 0x41: // DOS Enhanced LAN Manager 2.0+ MINIPOP/NETPOPUP
1.1.1.30  root     14619:                case 0x45: // PROF.COM
                   14620:                case 0x51: // ODIHELP.EXE
1.1.1.33  root     14621:                case 0x52: // JAM.SYS v1.10+
1.1.1.30  root     14622:                case 0x54: // POWER.EXE
                   14623:                case 0x56: // INTERLNK
1.1.1.33  root     14624:                case 0x57: // IOMEGA DRIVERS
1.1.1.30  root     14625:                case 0x70: // License Service API
1.1.1.33  root     14626:                case 0x72: // SRDISK v1.30+
1.1.1.30  root     14627:                case 0x7a: // Novell NetWare
1.1.1.33  root     14628:                case 0x7f: // PRINDIR v9.0
                   14629:                case 0x80: // FAX BIOS
                   14630:                case 0x81: // Nanosoft, Inc. TurboNET redirector
                   14631:                case 0x82: // Nanosoft, Inc. CAPDOS
                   14632:                case 0x89: // WHOA!.COM
                   14633:                case 0x90: // Resident AID
1.1.1.30  root     14634:                case 0x94: // MICRO.EXE
1.1.1.33  root     14635:                case 0x97: // Micro Focus COBOL v3.1.31
                   14636:                case 0x98: // Micro Focus COBOL v3.1.31
                   14637:                case 0x99: // DOS Navigator II
                   14638:                case 0x9e: // INTMON v2.1
                   14639:                case 0x9f: // INTCFG v2.1
                   14640:                case 0xa9: // METZTSR.COM
                   14641:                case 0xab: // SRSoft MODAL PC v2
1.1.1.30  root     14642:                case 0xac: // GRAPHICS.COM
1.1.1.33  root     14643:                case 0xaf: // WinDOS v2.11
1.1.1.30  root     14644:                case 0xb0: // GRAFTABLE.COM
1.1.1.33  root     14645:                case 0xb4: // IBM PC3270 EMULATION PROG v3
1.1.1.30  root     14646:                case 0xb8: // NETWORK
                   14647:                case 0xb9: // RECEIVER.COM
                   14648:                case 0xbc: // EGA.SYS
1.1.1.33  root     14649:                case 0xbe: // REDVIEW
1.1.1.30  root     14650:                case 0xbf: // PC LAN Program - REDIRIFS.EXE
                   14651:                case 0xc0: // Novell LSL.COM
1.1.1.33  root     14652:                case 0xc1: // Personal NetWare - STPIPX v1.00
                   14653:                case 0xc3: // SETWPR.COM
                   14654:                case 0xc5: // PC-DOS Econet v1.05
                   14655:                case 0xc7: // COLAP.COM
                   14656:                case 0xc9: // ThunderByte???
                   14657:                case 0xca: // TBSCANX
                   14658:                case 0xcb: // Communicating Applications Specification
                   14659:                case 0xcc: // Tsoft NFSDRVR
                   14660:                case 0xcd: // SWELL.EXE
                   14661:                case 0xcf: // TEMPLEXX 1.0
                   14662:                case 0xd0: // Lotus CD/Networker
1.1.1.30  root     14663:                case 0xd2: // PCL-838.EXE
1.1.1.33  root     14664:                case 0xd3: // TeleReplica
                   14665:                case 0xd6: // VEDIT VSWAP
                   14666:                case 0xd7: // Banyan VINES v4+
1.1.1.30  root     14667:                case 0xd8: // Novell NetWare Lite - CLIENT.EXE
1.1.1.33  root     14668:                case 0xda: // ZyXEL ZFAX v1.x
                   14669:                case 0xdb: // ZyXEL ZFAX v2+
                   14670:                case 0xdc: // GOLD.COM
                   14671:                case 0xdd: // MIXFIX.EXE
                   14672:                case 0xde: // Novell Netware - RPRINTER, NPRINTER
                   14673:                case 0xdf: // HyperWare programs
                   14674:                case 0xe0: // SETDRVER.COM v2.10+
                   14675:                case 0xe1: // Phantom2 v1.1+
                   14676:                case 0xe3: // ANARKEY.COM
                   14677:                case 0xed: // Phar Lap DOS EXTENDERS
                   14678:                case 0xee: // XVIEW
                   14679:                case 0xf0: // 4MAP
                   14680:                case 0xf1: // DOS EXTENDER
                   14681:                case 0xf2: // WINX
                   14682:                case 0xf4: // FINDIRQ.COM
                   14683:                case 0xf7: // AUTOPARK.COM
                   14684:                case 0xf8: // SuperStor PRO 2XON.COM
                   14685:                case 0xfb: // AutoBraille v1.1A
                   14686:                case 0xfe: // PC-NFS ???
                   14687:                case 0xff: // Topware Network Operating System
1.1.1.30  root     14688:                        switch(REG8(AL)) {
                   14689:                        case 0x00:
                   14690:                                // This is not installed
                   14691: //                             REG8(AL) = 0x00;
                   14692:                                break;
1.1.1.33  root     14693:                        case 0x01:
                   14694:                                // Banyan VINES v4+ is not installed
                   14695:                                if(REG8(AH) == 0xd7 && REG16(BX) == 0x0000) {
                   14696:                                        break;
                   14697:                                }
1.1.1.30  root     14698:                        default:
                   14699:                                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));
                   14700:                                REG16(AX) = 0x01;
                   14701:                                m_CF = 1;
                   14702:                                break;
                   14703:                        }
                   14704:                        break;
1.1.1.22  root     14705:                default:
                   14706:                        unimplemented_2fh("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));
                   14707:                        break;
1.1       root     14708:                }
                   14709:                break;
1.1.1.24  root     14710:        case 0x33:
                   14711:                switch(REG8(AH)) {
                   14712:                case 0x00:
                   14713:                        // Mouse
                   14714:                        switch(REG8(AL)) {
                   14715:                        case 0x00: msdos_int_33h_0000h(); break;
                   14716:                        case 0x01: msdos_int_33h_0001h(); break;
                   14717:                        case 0x02: msdos_int_33h_0002h(); break;
                   14718:                        case 0x03: msdos_int_33h_0003h(); break;
1.1.1.34  root     14719:                        case 0x04: msdos_int_33h_0004h(); break;
1.1.1.24  root     14720:                        case 0x05: msdos_int_33h_0005h(); break;
                   14721:                        case 0x06: msdos_int_33h_0006h(); break;
                   14722:                        case 0x07: msdos_int_33h_0007h(); break;
                   14723:                        case 0x08: msdos_int_33h_0008h(); break;
                   14724:                        case 0x09: msdos_int_33h_0009h(); break;
1.1.1.34  root     14725:                        case 0x0a: break; // Define Text Cursor
1.1.1.24  root     14726:                        case 0x0b: msdos_int_33h_000bh(); break;
                   14727:                        case 0x0c: msdos_int_33h_000ch(); break;
1.1.1.34  root     14728:                        case 0x0d: break; // Light Pen Emulation On
                   14729:                        case 0x0e: break; // Light Pen Emulation Off
1.1.1.24  root     14730:                        case 0x0f: msdos_int_33h_000fh(); break;
1.1.1.34  root     14731:                        case 0x10: break; // Define Screen Region for Updating
1.1.1.24  root     14732:                        case 0x11: msdos_int_33h_0011h(); break;
1.1.1.34  root     14733:                        case 0x12: REG16(AX) = 0xffff; break; // Set Large Graphics Cursor Block
                   14734:                        case 0x13: break; // Define Double-Speed Threshold
1.1.1.24  root     14735:                        case 0x14: msdos_int_33h_0014h(); break;
                   14736:                        case 0x15: msdos_int_33h_0015h(); break;
                   14737:                        case 0x16: msdos_int_33h_0016h(); break;
                   14738:                        case 0x17: msdos_int_33h_0017h(); break;
                   14739:                        case 0x1a: msdos_int_33h_001ah(); break;
                   14740:                        case 0x1b: msdos_int_33h_001bh(); break;
                   14741:                        case 0x1d: msdos_int_33h_001dh(); break;
                   14742:                        case 0x1e: msdos_int_33h_001eh(); break;
1.1.1.34  root     14743:                        case 0x1f: msdos_int_33h_001fh(); break;
                   14744:                        case 0x20: msdos_int_33h_0020h(); break;
1.1.1.24  root     14745:                        case 0x21: msdos_int_33h_0021h(); break;
                   14746:                        case 0x22: msdos_int_33h_0022h(); break;
                   14747:                        case 0x23: msdos_int_33h_0023h(); break;
                   14748:                        case 0x24: msdos_int_33h_0024h(); break;
                   14749:                        case 0x26: msdos_int_33h_0026h(); break;
                   14750:                        case 0x2a: msdos_int_33h_002ah(); break;
1.1.1.34  root     14751:                        case 0x2f: break; // Mouse Hardware Reset
1.1.1.24  root     14752:                        case 0x31: msdos_int_33h_0031h(); break;
                   14753:                        case 0x32: msdos_int_33h_0032h(); break;
                   14754:                        default:
                   14755:                                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));
                   14756:                                break;
                   14757:                        }
                   14758:                        break;
                   14759:                default:
                   14760:                        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));
                   14761:                        break;
                   14762:                }
                   14763:                break;
1.1.1.19  root     14764:        case 0x68:
                   14765:                // dummy interrupt for EMS (int 67h)
                   14766:                switch(REG8(AH)) {
                   14767:                case 0x40: msdos_int_67h_40h(); break;
                   14768:                case 0x41: msdos_int_67h_41h(); break;
                   14769:                case 0x42: msdos_int_67h_42h(); break;
                   14770:                case 0x43: msdos_int_67h_43h(); break;
                   14771:                case 0x44: msdos_int_67h_44h(); break;
                   14772:                case 0x45: msdos_int_67h_45h(); break;
                   14773:                case 0x46: msdos_int_67h_46h(); break;
                   14774:                case 0x47: msdos_int_67h_47h(); break;
                   14775:                case 0x48: msdos_int_67h_48h(); break;
1.1.1.20  root     14776:                // 0x49: LIM EMS - Reserved - Get I/O Port Address (Undocumented in EMS 3.2)
                   14777:                // 0x4a: LIM EMS - Reserved - Get Translation Array (Undocumented in EMS 3.2)
1.1.1.19  root     14778:                case 0x4b: msdos_int_67h_4bh(); break;
                   14779:                case 0x4c: msdos_int_67h_4ch(); break;
                   14780:                case 0x4d: msdos_int_67h_4dh(); break;
1.1.1.20  root     14781:                case 0x4e: msdos_int_67h_4eh(); break;
1.1.1.21  root     14782:                case 0x4f: msdos_int_67h_4fh(); break;
1.1.1.20  root     14783:                case 0x50: msdos_int_67h_50h(); break;
1.1.1.19  root     14784:                case 0x51: msdos_int_67h_51h(); break;
1.1.1.20  root     14785:                case 0x52: msdos_int_67h_52h(); break;
1.1.1.19  root     14786:                case 0x53: msdos_int_67h_53h(); break;
                   14787:                case 0x54: msdos_int_67h_54h(); break;
1.1.1.20  root     14788:                // 0x55: LIM EMS 4.0 - Alter Page Map And JUMP
                   14789:                // 0x56: LIM EMS 4.0 - Alter Page Map And CALL
                   14790:                case 0x57: msdos_int_67h_57h(); break;
                   14791:                case 0x58: msdos_int_67h_58h(); break;
                   14792:                // 0x59: LIM EMS 4.0 - Get Expanded Memory Hardware Information (for DOS Kernel)
                   14793:                case 0x5a: msdos_int_67h_5ah(); break;
                   14794:                // 0x5b: LIM EMS 4.0 - Alternate Map Register Set (for DOS Kernel)
                   14795:                // 0x5c: LIM EMS 4.0 - Prepate Expanded Memory Hardware For Warm Boot
                   14796:                // 0x5d: LIM EMS 4.0 - Enable/Disable OS Function Set Functions (for DOS Kernel)
1.1.1.31  root     14797:                // 0x60: EEMS - Get Physical Window Array
                   14798:                // 0x61: EEMS - Generic Accelerator Card Support
                   14799:                // 0x68: EEMS - Get Address of All Pge Frames om System
                   14800:                // 0x69: EEMS - Map Page into Frame
                   14801:                // 0x6a: EEMS - Page Mapping
                   14802:                // 0xde: VCPI
1.1.1.30  root     14803:                case 0xde: msdos_int_67h_deh(); break;
1.1.1.19  root     14804:                default:
1.1.1.22  root     14805:                        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     14806:                        REG8(AH) = 0x84;
                   14807:                        break;
                   14808:                }
                   14809:                break;
                   14810: #ifdef SUPPORT_XMS
                   14811:        case 0x69:
                   14812:                // dummy interrupt for XMS (call far)
1.1.1.28  root     14813:                try {
                   14814:                        switch(REG8(AH)) {
                   14815:                        case 0x00: msdos_call_xms_00h(); break;
                   14816:                        case 0x01: msdos_call_xms_01h(); break;
                   14817:                        case 0x02: msdos_call_xms_02h(); break;
                   14818:                        case 0x03: msdos_call_xms_03h(); break;
                   14819:                        case 0x04: msdos_call_xms_04h(); break;
                   14820:                        case 0x05: msdos_call_xms_05h(); break;
                   14821:                        case 0x06: msdos_call_xms_06h(); break;
                   14822:                        case 0x07: msdos_call_xms_07h(); break;
                   14823:                        case 0x08: msdos_call_xms_08h(); break;
                   14824:                        case 0x09: msdos_call_xms_09h(); break;
                   14825:                        case 0x0a: msdos_call_xms_0ah(); break;
                   14826:                        case 0x0b: msdos_call_xms_0bh(); break;
                   14827:                        case 0x0c: msdos_call_xms_0ch(); break;
                   14828:                        case 0x0d: msdos_call_xms_0dh(); break;
                   14829:                        case 0x0e: msdos_call_xms_0eh(); break;
                   14830:                        case 0x0f: msdos_call_xms_0fh(); break;
                   14831:                        case 0x10: msdos_call_xms_10h(); break;
                   14832:                        case 0x11: msdos_call_xms_11h(); break;
                   14833:                        case 0x12: msdos_call_xms_12h(); break;
1.1.1.29  root     14834: #if defined(HAS_I386)
                   14835:                        case 0x88: msdos_call_xms_88h(); break;
                   14836:                        case 0x89: msdos_call_xms_89h(); break;
                   14837:                        case 0x8e: msdos_call_xms_8eh(); break;
                   14838:                        case 0x8f: msdos_call_xms_8fh(); break;
                   14839: #endif
1.1.1.28  root     14840:                        default:
                   14841:                                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));
                   14842:                                REG16(AX) = 0x0000;
                   14843:                                REG8(BL) = 0x80; // function not implemented
                   14844:                                break;
                   14845:                        }
                   14846:                } catch(...) {
1.1.1.19  root     14847:                        REG16(AX) = 0x0000;
1.1.1.28  root     14848:                        REG8(BL) = 0x8f; // unrecoverable driver error
1.1.1.19  root     14849:                }
                   14850:                break;
                   14851: #endif
                   14852:        case 0x6a:
1.1.1.24  root     14853:                // irq12 (mouse)
                   14854:                mouse_push_ax = REG16(AX);
                   14855:                mouse_push_bx = REG16(BX);
                   14856:                mouse_push_cx = REG16(CX);
                   14857:                mouse_push_dx = REG16(DX);
                   14858:                mouse_push_si = REG16(SI);
                   14859:                mouse_push_di = REG16(DI);
                   14860:                
1.1.1.34  root     14861:                if(/*mouse.hidden == 0 && */mouse.call_addr.dw != 0) {
1.1.1.24  root     14862:                        REG16(AX) = mouse.status_irq;
                   14863:                        REG16(BX) = mouse.get_buttons();
1.1.1.34  root     14864:                        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   14865:                        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
1.1.1.24  root     14866:                        REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
                   14867:                        REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
                   14868:                        
                   14869:                        mem[0xfffd0 + 0x02] = 0x9a;     // call far
                   14870:                        mem[0xfffd0 + 0x03] = mouse.call_addr.w.l & 0xff;
                   14871:                        mem[0xfffd0 + 0x04] = mouse.call_addr.w.l >> 8;
                   14872:                        mem[0xfffd0 + 0x05] = mouse.call_addr.w.h & 0xff;
                   14873:                        mem[0xfffd0 + 0x06] = mouse.call_addr.w.h >> 8;
                   14874:                } else {
                   14875:                        mem[0xfffd0 + 0x02] = 0x90;     // nop
                   14876:                        mem[0xfffd0 + 0x03] = 0x90;     // nop
                   14877:                        mem[0xfffd0 + 0x04] = 0x90;     // nop
                   14878:                        mem[0xfffd0 + 0x05] = 0x90;     // nop
                   14879:                        mem[0xfffd0 + 0x06] = 0x90;     // nop
                   14880:                }
                   14881:                break;
                   14882:        case 0x6b:
                   14883:                // end of irq12 (mouse)
                   14884:                REG16(AX) = mouse_push_ax;
                   14885:                REG16(BX) = mouse_push_bx;
                   14886:                REG16(CX) = mouse_push_cx;
                   14887:                REG16(DX) = mouse_push_dx;
                   14888:                REG16(SI) = mouse_push_si;
                   14889:                REG16(DI) = mouse_push_di;
                   14890:                
                   14891:                // EOI
                   14892:                if((pic[1].isr &= ~(1 << 4)) == 0) {
                   14893:                        pic[0].isr &= ~(1 << 2); // master
                   14894:                }
                   14895:                pic_update();
                   14896:                break;
                   14897:        case 0x6c:
1.1.1.19  root     14898:                // dummy interrupt for case map routine pointed in the country info
                   14899:                if(REG8(AL) >= 0x80) {
                   14900:                        char tmp[2] = {0};
                   14901:                        tmp[0] = REG8(AL);
                   14902:                        my_strupr(tmp);
                   14903:                        REG8(AL) = tmp[0];
                   14904:                }
                   14905:                break;
1.1.1.27  root     14906:        case 0x6d:
                   14907:                // dummy interrupt for font read routine pointed by int 15h, ax=5000h
                   14908:                REG8(AL) = 0x86; // not supported
                   14909:                m_CF = 1;
                   14910:                break;
1.1.1.32  root     14911:        case 0x6e:
                   14912:                // dummy interrupt for parameter error message read routine pointed by int 2fh, ax=122eh, dl=08h
                   14913:                {
                   14914:                        UINT16 code = REG16(AX);
                   14915:                        if(code & 0xf0) {
                   14916:                                code = (code & 7) | ((code & 0x10) >> 1);
                   14917:                        }
                   14918:                        for(int i = 0; i < array_length(param_error_table); i++) {
                   14919:                                if(param_error_table[i].code == code || param_error_table[i].code == (UINT16)-1) {
                   14920:                                        const char *message = NULL;
                   14921:                                        if(active_code_page == 932) {
                   14922:                                                message = param_error_table[i].message_japanese;
                   14923:                                        }
                   14924:                                        if(message == NULL) {
                   14925:                                                message = param_error_table[i].message_english;
                   14926:                                        }
                   14927:                                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   14928:                                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   14929:                                        
                   14930:                                        SREG(ES) = WORK_TOP >> 4;
                   14931:                                        i386_load_segment_descriptor(ES);
                   14932:                                        REG16(DI) = 0x0000;
                   14933:                                        break;
                   14934:                                }
                   14935:                        }
                   14936:                }
                   14937:                break;
1.1.1.8   root     14938:        case 0x70:
                   14939:        case 0x71:
                   14940:        case 0x72:
                   14941:        case 0x73:
                   14942:        case 0x74:
                   14943:        case 0x75:
                   14944:        case 0x76:
                   14945:        case 0x77:
                   14946:                // EOI
                   14947:                if((pic[1].isr &= ~(1 << (num - 0x70))) == 0) {
                   14948:                        pic[0].isr &= ~(1 << 2); // master
                   14949:                }
                   14950:                pic_update();
                   14951:                break;
1.1       root     14952:        default:
1.1.1.22  root     14953: //             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     14954:                break;
                   14955:        }
                   14956:        
                   14957:        // update cursor position
                   14958:        if(cursor_moved) {
1.1.1.36  root     14959:                pcbios_update_cursor_position();
1.1       root     14960:                cursor_moved = false;
                   14961:        }
                   14962: }
                   14963: 
                   14964: // init
                   14965: 
                   14966: int msdos_init(int argc, char *argv[], char *envp[], int standard_env)
                   14967: {
                   14968:        // init file handler
                   14969:        memset(file_handler, 0, sizeof(file_handler));
                   14970:        msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   14971:        msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   14972:        msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
1.1.1.21  root     14973: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     14974:        if(_open("stdaux.txt", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
1.1.1.21  root     14975: #else
                   14976:        if(_open("NUL", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
                   14977: #endif
                   14978:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
1.1       root     14979:        }
1.1.1.21  root     14980:        if(_open("NUL", _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 4) {
1.1.1.37! root     14981:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0, 0, 1); // LPT1
1.1.1.21  root     14982:        }
1.1       root     14983:        _dup2(0, DUP_STDIN);
                   14984:        _dup2(1, DUP_STDOUT);
                   14985:        _dup2(2, DUP_STDERR);
1.1.1.21  root     14986:        _dup2(3, DUP_STDAUX);
                   14987:        _dup2(4, DUP_STDPRN);
1.1       root     14988:        
1.1.1.24  root     14989:        // init mouse
                   14990:        memset(&mouse, 0, sizeof(mouse));
1.1.1.34  root     14991:        mouse.enabled = true;   // from DOSBox
                   14992:        mouse.hidden = 1;       // hidden in default ???
                   14993:        mouse.old_hidden = 1;   // from DOSBox
                   14994:        mouse.max_position.x = 8 * (scr_width  - 1);
                   14995:        mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24  root     14996:        mouse.mickey.x = 8;
                   14997:        mouse.mickey.y = 16;
                   14998:        
1.1.1.26  root     14999: #ifdef SUPPORT_XMS
                   15000:        // init xms
                   15001:        msdos_xms_init();
                   15002: #endif
                   15003:        
1.1       root     15004:        // init process
                   15005:        memset(process, 0, sizeof(process));
                   15006:        
1.1.1.13  root     15007:        // init dtainfo
                   15008:        msdos_dta_info_init();
                   15009:        
1.1       root     15010:        // init memory
                   15011:        memset(mem, 0, sizeof(mem));
                   15012:        
                   15013:        // bios data area
1.1.1.23  root     15014:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     15015:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   15016:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.14  root     15017:        CONSOLE_FONT_INFO cfi;
                   15018:        GetCurrentConsoleFont(hStdout, FALSE, &cfi);
                   15019:        
                   15020:        int regen = min(scr_width * scr_height * 2, 0x8000);
                   15021:        text_vram_top_address = TEXT_VRAM_TOP;
                   15022:        text_vram_end_address = text_vram_top_address + regen;
                   15023:        shadow_buffer_top_address = SHADOW_BUF_TOP;
                   15024:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
                   15025:        
                   15026:        if(regen > 0x4000) {
                   15027:                regen = 0x8000;
                   15028:                vram_pages = 1;
                   15029:        } else if(regen > 0x2000) {
                   15030:                regen = 0x4000;
                   15031:                vram_pages = 2;
                   15032:        } else if(regen > 0x1000) {
                   15033:                regen = 0x2000;
                   15034:                vram_pages = 4;
                   15035:        } else {
                   15036:                regen = 0x1000;
                   15037:                vram_pages = 8;
                   15038:        }
1.1       root     15039:        
1.1.1.25  root     15040:        *(UINT16 *)(mem + 0x400) = 0x3f8; // com1 port address
                   15041:        *(UINT16 *)(mem + 0x402) = 0x2f8; // com2 port address
1.1.1.29  root     15042:        *(UINT16 *)(mem + 0x404) = 0x3e8; // com3 port address
                   15043:        *(UINT16 *)(mem + 0x406) = 0x2e8; // com4 port address
1.1.1.25  root     15044:        *(UINT16 *)(mem + 0x408) = 0x378; // lpt1 port address
1.1.1.37! root     15045:        *(UINT16 *)(mem + 0x40a) = 0x278; // lpt2 port address
        !          15046:        *(UINT16 *)(mem + 0x40c) = 0x3bc; // lpt3 port address
1.1.1.32  root     15047: #ifdef EXT_BIOS_TOP
1.1.1.25  root     15048:        *(UINT16 *)(mem + 0x40e) = EXT_BIOS_TOP >> 4;
1.1.1.32  root     15049: #endif
1.1.1.26  root     15050:        *(UINT16 *)(mem + 0x410) = msdos_get_equipment();
1.1.1.33  root     15051:        *(UINT16 *)(mem + 0x413) = MEMORY_END >> 10;
1.1       root     15052:        *(UINT8  *)(mem + 0x449) = 0x03;//0x73;
1.1.1.14  root     15053:        *(UINT16 *)(mem + 0x44a) = csbi.dwSize.X;
                   15054:        *(UINT16 *)(mem + 0x44c) = regen;
1.1       root     15055:        *(UINT16 *)(mem + 0x44e) = 0;
                   15056:        *(UINT8  *)(mem + 0x450) = csbi.dwCursorPosition.X;
1.1.1.14  root     15057:        *(UINT8  *)(mem + 0x451) = csbi.dwCursorPosition.Y - scr_top;
1.1       root     15058:        *(UINT8  *)(mem + 0x460) = 7;
                   15059:        *(UINT8  *)(mem + 0x461) = 7;
                   15060:        *(UINT8  *)(mem + 0x462) = 0;
                   15061:        *(UINT16 *)(mem + 0x463) = 0x3d4;
1.1.1.19  root     15062:        *(UINT8  *)(mem + 0x465) = 0x09;
                   15063:        *(UINT32 *)(mem + 0x46c) = get_ticks_since_midnight(timeGetTime());
1.1.1.14  root     15064:        *(UINT8  *)(mem + 0x484) = csbi.srWindow.Bottom - csbi.srWindow.Top;
                   15065:        *(UINT8  *)(mem + 0x485) = cfi.dwFontSize.Y;
                   15066:        *(UINT8  *)(mem + 0x487) = 0x60;
                   15067:        *(UINT8  *)(mem + 0x496) = 0x10; // enhanced keyboard installed
1.1.1.32  root     15068: #ifdef EXT_BIOS_TOP
1.1.1.25  root     15069:        *(UINT16 *)(mem + EXT_BIOS_TOP) = 1;
1.1.1.32  root     15070: #endif
1.1.1.14  root     15071:        
                   15072:        // initial screen
                   15073:        SMALL_RECT rect;
                   15074:        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   15075:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   15076:        for(int y = 0, ofs1 = TEXT_VRAM_TOP, ofs2 = SHADOW_BUF_TOP; y < scr_height; y++) {
                   15077:                for(int x = 0; x < scr_width; x++) {
                   15078:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Char.AsciiChar;
                   15079:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Attributes;
                   15080:                }
                   15081:        }
1.1       root     15082:        
1.1.1.19  root     15083:        // init mcb
1.1       root     15084:        int seg = MEMORY_TOP >> 4;
1.1.1.19  root     15085:        
                   15086:        // iret table
                   15087:        // note: int 2eh vector should address the routine in command.com,
                   15088:        // and some softwares invite (int 2eh vector segment) - 1 must address the mcb of command.com.
                   15089:        // so move iret table into allocated memory block
                   15090:        // http://www5c.biglobe.ne.jp/~ecb/assembler2/2_6.html
1.1.1.33  root     15091:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, IRET_SIZE >> 4);
1.1.1.19  root     15092:        IRET_TOP = seg << 4;
                   15093:        seg += IRET_SIZE >> 4;
1.1.1.25  root     15094:        memset(mem + IRET_TOP, 0xcf, IRET_SIZE); // iret
1.1.1.19  root     15095:        
                   15096:        // dummy xms/ems device
1.1.1.33  root     15097:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, XMS_SIZE >> 4);
1.1.1.19  root     15098:        XMS_TOP = seg << 4;
                   15099:        seg += XMS_SIZE >> 4;
                   15100:        
                   15101:        // environment
1.1.1.33  root     15102:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, ENV_SIZE >> 4);
1.1       root     15103:        int env_seg = seg;
                   15104:        int ofs = 0;
1.1.1.32  root     15105:        char env_append[ENV_SIZE] = {0}, append_added = 0;
                   15106:        char comspec_added = 0;
1.1.1.33  root     15107:        char lastdrive_added = 0;
1.1.1.32  root     15108:        char env_msdos_path[ENV_SIZE] = {0};
                   15109:        char env_path[ENV_SIZE] = {0}, path_added = 0;
1.1.1.33  root     15110:        char prompt_added = 0;
1.1.1.32  root     15111:        char env_temp[ENV_SIZE] = {0}, temp_added = 0, tmp_added = 0;
1.1.1.33  root     15112:        char tz_added = 0;
1.1.1.32  root     15113:        char *path, *short_path;
                   15114:        
                   15115:        if((path = getenv("MSDOS_APPEND")) != NULL) {
                   15116:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   15117:                        strcpy(env_append, short_path);
                   15118:                }
                   15119:        }
                   15120:        if((path = getenv("APPEND")) != NULL) {
                   15121:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   15122:                        if(env_append[0] != '\0') {
                   15123:                                strcat(env_append, ";");
                   15124:                        }
                   15125:                        strcat(env_append, short_path);
                   15126:                }
                   15127:        }
                   15128:        
                   15129:        if((path = msdos_search_command_com(argv[0], env_path)) != NULL) {
                   15130:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   15131:                        strcpy(comspec_path, short_path);
                   15132:                }
                   15133:        }
                   15134:        if((path = getenv("MSDOS_COMSPEC")) != NULL && _access(path, 0) == 0) {
                   15135:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   15136:                        strcpy(comspec_path, short_path);
                   15137:                }
                   15138:        }
1.1       root     15139:        
1.1.1.28  root     15140:        if((path = getenv("MSDOS_PATH")) != NULL) {
1.1.1.32  root     15141:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   15142:                        strcpy(env_msdos_path, short_path);
                   15143:                        strcpy(env_path, short_path);
1.1.1.14  root     15144:                }
                   15145:        }
1.1.1.28  root     15146:        if((path = getenv("PATH")) != NULL) {
1.1.1.32  root     15147:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   15148:                        if(env_path[0] != '\0') {
                   15149:                                strcat(env_path, ";");
                   15150:                        }
                   15151:                        strcat(env_path, short_path);
1.1.1.9   root     15152:                }
                   15153:        }
1.1.1.32  root     15154:        
                   15155:        if(GetTempPath(ENV_SIZE, env_temp) != 0) {
                   15156:                strcpy(env_temp, msdos_get_multiple_short_path(env_temp));
1.1.1.15  root     15157:        }
1.1.1.32  root     15158:        for(int i = 0; i < 4; i++) {
                   15159:                static const char *name[4] = {"MSDOS_TEMP", "MSDOS_TMP", "TEMP", "TMP"};
                   15160:                if((path = getenv(name[i])) != NULL && _access(path, 0) == 0) {
                   15161:                        if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   15162:                                strcpy(env_temp, short_path);
                   15163:                                break;
                   15164:                        }
                   15165:                }
1.1.1.24  root     15166:        }
1.1.1.32  root     15167:        
1.1.1.9   root     15168:        for(char **p = envp; p != NULL && *p != NULL; p++) {
1.1       root     15169:                // lower to upper
1.1.1.28  root     15170:                char tmp[ENV_SIZE], name[ENV_SIZE];
1.1       root     15171:                strcpy(tmp, *p);
                   15172:                for(int i = 0;; i++) {
                   15173:                        if(tmp[i] == '=') {
                   15174:                                tmp[i] = '\0';
                   15175:                                sprintf(name, ";%s;", tmp);
1.1.1.25  root     15176:                                my_strupr(name);
1.1       root     15177:                                tmp[i] = '=';
                   15178:                                break;
                   15179:                        } else if(tmp[i] >= 'a' && tmp[i] <= 'z') {
1.1.1.28  root     15180:                                tmp[i] = (tmp[i] - 'a') + 'A';
1.1       root     15181:                        }
                   15182:                }
1.1.1.33  root     15183:                if(strstr(";MSDOS_APPEND;MSDOS_COMSPEC;MSDOS_LASTDRIVE;MSDOS_TEMP;MSDOS_TMP;MSDOS_TZ;", name) != NULL) {
                   15184:                        // ignore MSDOS_(APPEND/COMSPEC/LASTDRIVE/TEMP/TMP/TZ)
                   15185:                } else if(standard_env && strstr(";APPEND;COMSPEC;LASTDRIVE;MSDOS_PATH;PATH;PROMPT;TEMP;TMP;TZ;", name) == NULL) {
1.1.1.18  root     15186:                        // ignore non standard environments
                   15187:                } else {
1.1.1.33  root     15188:                        if(strncmp(tmp, "APPEND=", 7) == 0) {
1.1.1.32  root     15189:                                if(env_append[0] != '\0') {
                   15190:                                        sprintf(tmp, "APPEND=%s", env_append);
                   15191:                                } else {
                   15192:                                        sprintf(tmp, "APPEND=%s", msdos_get_multiple_short_path(tmp + 7));
                   15193:                                }
                   15194:                                append_added = 1;
                   15195:                        } else if(strncmp(tmp, "COMSPEC=", 8) == 0) {
1.1.1.14  root     15196:                                strcpy(tmp, "COMSPEC=C:\\COMMAND.COM");
1.1.1.32  root     15197:                                comspec_added = 1;
1.1.1.33  root     15198:                        } else if(strncmp(tmp, "LASTDRIVE=", 10) == 0) {
                   15199:                                char *env = getenv("MSDOS_LASTDRIVE");
                   15200:                                if(env != NULL) {
                   15201:                                        sprintf(tmp, "LASTDRIVE=%s", env);
                   15202:                                }
                   15203:                                lastdrive_added = 1;
                   15204:                        } else if(strncmp(tmp, "MSDOS_PATH=", 11) == 0) {
1.1.1.28  root     15205:                                if(env_msdos_path[0] != '\0') {
                   15206:                                        sprintf(tmp, "MSDOS_PATH=%s", env_msdos_path);
                   15207:                                } else {
                   15208:                                        sprintf(tmp, "MSDOS_PATH=%s", msdos_get_multiple_short_path(tmp + 11));
                   15209:                                }
1.1.1.33  root     15210:                        } else if(strncmp(tmp, "PATH=", 5) == 0) {
1.1.1.28  root     15211:                                if(env_path[0] != '\0') {
                   15212:                                        sprintf(tmp, "PATH=%s", env_path);
                   15213:                                } else {
                   15214:                                        sprintf(tmp, "PATH=%s", msdos_get_multiple_short_path(tmp + 5));
                   15215:                                }
1.1.1.32  root     15216:                                path_added = 1;
1.1.1.33  root     15217:                        } else if(strncmp(tmp, "PROMPT=", 7) == 0) {
                   15218:                                prompt_added = 1;
1.1.1.28  root     15219:                        } else if(strncmp(tmp, "TEMP=", 5) == 0) {
                   15220:                                if(env_temp[0] != '\0') {
                   15221:                                        sprintf(tmp, "TEMP=%s", env_temp);
                   15222:                                } else {
                   15223:                                        sprintf(tmp, "TEMP=%s", msdos_get_multiple_short_path(tmp + 5));
                   15224:                                }
1.1.1.32  root     15225:                                temp_added = 1;
1.1.1.33  root     15226:                        } else if(strncmp(tmp, "TMP=", 4) == 0) {
1.1.1.28  root     15227:                                if(env_temp[0] != '\0') {
                   15228:                                        sprintf(tmp, "TMP=%s", env_temp);
                   15229:                                } else {
                   15230:                                        sprintf(tmp, "TMP=%s", msdos_get_multiple_short_path(tmp + 4));
1.1       root     15231:                                }
1.1.1.32  root     15232:                                tmp_added = 1;
1.1.1.33  root     15233:                        } else if(strncmp(tmp, "TZ=", 3) == 0) {
                   15234:                                char *env = getenv("MSDOS_TZ");
                   15235:                                if(env != NULL) {
                   15236:                                        sprintf(tmp, "TZ=%s", env);
                   15237:                                }
                   15238:                                tz_added = 1;
1.1       root     15239:                        }
                   15240:                        int len = strlen(tmp);
1.1.1.14  root     15241:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) {
1.1       root     15242:                                fatalerror("too many environments\n");
                   15243:                        }
                   15244:                        memcpy(mem + (seg << 4) + ofs, tmp, len);
                   15245:                        ofs += len + 1;
                   15246:                }
                   15247:        }
1.1.1.32  root     15248:        if(!append_added && env_append[0] != '\0') {
                   15249:                #define SET_ENV(name, value) { \
                   15250:                        char tmp[ENV_SIZE]; \
                   15251:                        sprintf(tmp, "%s=%s", name, value); \
                   15252:                        int len = strlen(tmp); \
                   15253:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) { \
                   15254:                                fatalerror("too many environments\n"); \
                   15255:                        } \
                   15256:                        memcpy(mem + (seg << 4) + ofs, tmp, len); \
                   15257:                        ofs += len + 1; \
                   15258:                }
                   15259:                SET_ENV("APPEND", env_append);
                   15260:        }
                   15261:        if(!comspec_added) {
                   15262:                SET_ENV("COMSPEC", "C:\\COMMAND.COM");
                   15263:        }
1.1.1.33  root     15264:        if(!lastdrive_added) {
                   15265:                SET_ENV("LASTDRIVE", "Z");
                   15266:        }
1.1.1.32  root     15267:        if(!path_added) {
                   15268:                SET_ENV("PATH", env_path);
                   15269:        }
1.1.1.33  root     15270:        if(!prompt_added) {
                   15271:                SET_ENV("PROMPT", "$P$G");
                   15272:        }
1.1.1.32  root     15273:        if(!temp_added) {
                   15274:                SET_ENV("TEMP", env_temp);
                   15275:        }
                   15276:        if(!tmp_added) {
                   15277:                SET_ENV("TMP", env_temp);
                   15278:        }
1.1.1.33  root     15279:        if(!tz_added) {
                   15280:                TIME_ZONE_INFORMATION tzi;
                   15281:                HKEY hKey, hSubKey;
                   15282:                char tzi_std_name[64];
                   15283:                char tz_std[8] = "GMT";
                   15284:                char tz_dlt[8] = "GST";
                   15285:                char tz_value[32];
                   15286:                
                   15287:                // timezone name from GetTimeZoneInformation may not be english
                   15288:                bool daylight = (GetTimeZoneInformation(&tzi) != TIME_ZONE_ID_UNKNOWN);
                   15289:                setlocale(LC_CTYPE, "");
                   15290:                wcstombs(tzi_std_name, tzi.StandardName, sizeof(tzi_std_name));
                   15291:                
                   15292:                // get english timezone name from registry
                   15293:                if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones", 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
                   15294:                        for(DWORD i = 0; !tz_added; i++) {
                   15295:                                char reg_name[256], sub_key[1024], std_name[256];
                   15296:                                DWORD size;
                   15297:                                FILETIME ftTime;
                   15298:                                LONG result = RegEnumKeyEx(hKey, i, reg_name, &(size = array_length(reg_name)), NULL, NULL, NULL, &ftTime);
                   15299:                                
                   15300:                                if(result == ERROR_SUCCESS) {
                   15301:                                        sprintf(sub_key, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\%s", reg_name);
                   15302:                                        if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, sub_key, 0, KEY_QUERY_VALUE, &hSubKey) == ERROR_SUCCESS) {
                   15303:                                                if(RegQueryValueEx(hSubKey, "Std", NULL, NULL, (LPBYTE)std_name, &(size = array_length(std_name))) == ERROR_SUCCESS) {
                   15304:                                                        // search english timezone name from table
1.1.1.37! root     15305:                                                        if(strcmp(std_name, tzi_std_name) == 0) {
1.1.1.33  root     15306:                                                                for(int j = 0; j < array_length(tz_table); j++) {
                   15307:                                                                        if(strcmp(reg_name, tz_table[j].name) == 0 && (tz_table[j].lcid == 0 || tz_table[j].lcid == GetUserDefaultLCID())) {
                   15308:                                                                                if(tz_table[j].std != NULL) {
                   15309:                                                                                        strcpy(tz_std, tz_table[j].std);
                   15310:                                                                                }
                   15311:                                                                                if(tz_table[j].dlt != NULL) {
                   15312:                                                                                        strcpy(tz_dlt, tz_table[j].dlt);
                   15313:                                                                                }
                   15314:                                                                                tz_added = 1;
                   15315:                                                                                break;
                   15316:                                                                        }
                   15317:                                                                }
                   15318:                                                        }
                   15319:                                                }
                   15320:                                                RegCloseKey(hSubKey);
                   15321:                                        }
                   15322:                                } else if(result == ERROR_NO_MORE_ITEMS) {
                   15323:                                        break;
                   15324:                                }
                   15325:                        }
                   15326:                        RegCloseKey(hKey);
                   15327:                }
                   15328:                if((tzi.Bias % 60) != 0) {
                   15329:                        sprintf(tz_value, "%s%d:%2d", tz_std, tzi.Bias / 60, abs(tzi.Bias % 60));
                   15330:                } else {
                   15331:                        sprintf(tz_value, "%s%d", tz_std, tzi.Bias / 60);
                   15332:                }
                   15333:                if(daylight) {
                   15334:                        strcat(tz_value, tz_dlt);
                   15335:                }
                   15336:                SET_ENV("TZ", tz_value);
                   15337:        }
1.1       root     15338:        seg += (ENV_SIZE >> 4);
                   15339:        
                   15340:        // psp
1.1.1.33  root     15341:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, PSP_SIZE >> 4);
1.1       root     15342:        current_psp = seg;
1.1.1.35  root     15343:        psp_t *psp = msdos_psp_create(seg, seg + (PSP_SIZE >> 4), -1, env_seg);
                   15344:        psp->parent_psp = current_psp;
1.1       root     15345:        seg += (PSP_SIZE >> 4);
                   15346:        
1.1.1.19  root     15347:        // first free mcb in conventional memory
1.1.1.33  root     15348:        msdos_mcb_create(seg, 'M', 0, (MEMORY_END >> 4) - seg - 2);
1.1.1.8   root     15349:        first_mcb = seg;
                   15350:        
1.1.1.19  root     15351:        // dummy mcb to link to umb
1.1.1.33  root     15352: #if 0
                   15353:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4)); // link umb
                   15354: #else
                   15355:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0); // unlink umb
                   15356: #endif
1.1.1.19  root     15357:        
                   15358:        // first free mcb in upper memory block
1.1.1.8   root     15359:        msdos_mcb_create(UMB_TOP >> 4, 'Z', 0, (UMB_END >> 4) - (UMB_TOP >> 4) - 1);
1.1       root     15360:        
1.1.1.29  root     15361: #ifdef SUPPORT_HMA
                   15362:        // first free mcb in high memory area
                   15363:        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   15364: #endif
                   15365:        
1.1.1.26  root     15366:        // interrupt vector
                   15367:        for(int i = 0; i < 0x80; i++) {
                   15368:                *(UINT16 *)(mem + 4 * i + 0) = i;
                   15369:                *(UINT16 *)(mem + 4 * i + 2) = (IRET_TOP >> 4);
                   15370:        }
1.1.1.35  root     15371:        *(UINT16 *)(mem + 4 * 0x08 + 0) = 0x0018;       // fffd:0018 irq0 (system timer)
1.1.1.26  root     15372:        *(UINT16 *)(mem + 4 * 0x08 + 2) = 0xfffd;
                   15373:        *(UINT16 *)(mem + 4 * 0x22 + 0) = 0x0000;       // ffff:0000 boot
                   15374:        *(UINT16 *)(mem + 4 * 0x22 + 2) = 0xffff;
                   15375:        *(UINT16 *)(mem + 4 * 0x67 + 0) = 0x0012;       // xxxx:0012 ems
                   15376:        *(UINT16 *)(mem + 4 * 0x67 + 2) = XMS_TOP >> 4;
                   15377:        *(UINT16 *)(mem + 4 * 0x74 + 0) = 0x0000;       // fffd:0000 irq12 (mouse)
                   15378:        *(UINT16 *)(mem + 4 * 0x74 + 2) = 0xfffd;
                   15379:        
1.1.1.29  root     15380:        // dummy devices (NUL -> CON -> ... -> CONFIG$ -> EMMXXXX0)
1.1.1.26  root     15381:        static const struct {
                   15382:                UINT16 attributes;
                   15383:                char *dev_name;
                   15384:        } dummy_devices[] = {
                   15385:                {0x8013, "CON     "},
                   15386:                {0x8000, "AUX     "},
                   15387:                {0xa0c0, "PRN     "},
                   15388:                {0x8008, "CLOCK$  "},
                   15389:                {0x8000, "COM1    "},
                   15390:                {0xa0c0, "LPT1    "},
                   15391:                {0xa0c0, "LPT2    "},
                   15392:                {0xa0c0, "LPT3    "},
                   15393:                {0x8000, "COM2    "},
                   15394:                {0x8000, "COM3    "},
                   15395:                {0x8000, "COM4    "},
1.1.1.30  root     15396: //             {0xc000, "CONFIG$ "},
                   15397:                {0xc000, "$IBMADSP"}, // for windows3.1 setup.exe
1.1.1.26  root     15398:        };
                   15399:        static const UINT8 dummy_device_routine[] = {
                   15400:                // from NUL device of Windows 98 SE
                   15401:                // or word ptr ES:[BX+03],0100
                   15402:                0x26, 0x81, 0x4f, 0x03, 0x00, 0x01,
                   15403:                // retf
                   15404:                0xcb,
                   15405:        };
1.1.1.29  root     15406:        device_t *last = NULL;
1.1.1.32  root     15407:        for(int i = 0; i < array_length(dummy_devices); i++) {
1.1.1.26  root     15408:                device_t *device = (device_t *)(mem + DEVICE_TOP + 22 + 18 * i);
1.1.1.29  root     15409:                device->next_driver.w.l = 22 + 18 * (i + 1);
                   15410:                device->next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     15411:                device->attributes = dummy_devices[i].attributes;
1.1.1.29  root     15412:                device->strategy = DEVICE_SIZE - sizeof(dummy_device_routine);
                   15413:                device->interrupt = DEVICE_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.26  root     15414:                memcpy(device->dev_name, dummy_devices[i].dev_name, 8);
1.1.1.29  root     15415:                last = device;
                   15416:        }
                   15417:        if(last != NULL) {
                   15418:                last->next_driver.w.l = 0;
                   15419:                last->next_driver.w.h = XMS_TOP >> 4;
1.1.1.26  root     15420:        }
1.1.1.29  root     15421:        memcpy(mem + DEVICE_TOP + DEVICE_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.26  root     15422:        
1.1.1.25  root     15423:        // dos info
                   15424:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   15425:        dos_info->magic_word = 1;
                   15426:        dos_info->first_mcb = MEMORY_TOP >> 4;
                   15427:        dos_info->first_dpb.w.l = 0;
                   15428:        dos_info->first_dpb.w.h = DPB_TOP >> 4;
                   15429:        dos_info->first_sft.w.l = 0;
                   15430:        dos_info->first_sft.w.h = SFT_TOP >> 4;
1.1.1.26  root     15431:        dos_info->clock_device.w.l = 22 + 18 * 3;       // CLOCK$ is the 3rd device in IO.SYS
1.1.1.25  root     15432:        dos_info->clock_device.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     15433:        dos_info->con_device.w.l = 22 + 18 * 0;         // CON is the 1st device in IO.SYS
1.1.1.25  root     15434:        dos_info->con_device.w.h = DEVICE_TOP >> 4;
                   15435:        dos_info->max_sector_len = 512;
                   15436:        dos_info->disk_buf_info.w.l = offsetof(dos_info_t, disk_buf_heads);
                   15437:        dos_info->disk_buf_info.w.h = DOS_INFO_TOP >> 4;
                   15438:        dos_info->cds.w.l = 0;
                   15439:        dos_info->cds.w.h = CDS_TOP >> 4;
                   15440:        dos_info->fcb_table.w.l = 0;
                   15441:        dos_info->fcb_table.w.h = FCB_TABLE_TOP >> 4;
                   15442:        dos_info->last_drive = 'Z' - 'A' + 1;
                   15443:        dos_info->buffers_x = 20;
                   15444:        dos_info->buffers_y = 0;
                   15445:        dos_info->boot_drive = 'C' - 'A' + 1;
1.1.1.29  root     15446:        dos_info->nul_device.next_driver.w.l = 22;
                   15447:        dos_info->nul_device.next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.25  root     15448:        dos_info->nul_device.attributes = 0x8004;
1.1.1.29  root     15449:        dos_info->nul_device.strategy = DOS_INFO_SIZE - sizeof(dummy_device_routine);
                   15450:        dos_info->nul_device.interrupt = DOS_INFO_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     15451:        memcpy(dos_info->nul_device.dev_name, "NUL     ", 8);
                   15452:        dos_info->disk_buf_heads.w.l = 0;
                   15453:        dos_info->disk_buf_heads.w.h = DISK_BUF_TOP >> 4;
                   15454:        dos_info->first_umb_fcb = UMB_TOP >> 4;
                   15455:        dos_info->first_mcb_2 = MEMORY_TOP >> 4;
1.1.1.29  root     15456:        memcpy(mem + DOS_INFO_TOP + DOS_INFO_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.25  root     15457:        
                   15458:        char *env;
                   15459:        if((env = getenv("LASTDRIVE")) != NULL) {
                   15460:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   15461:                        dos_info->last_drive = env[0] - 'A' + 1;
                   15462:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   15463:                        dos_info->last_drive = env[0] - 'a' + 1;
                   15464:                }
                   15465:        }
                   15466:        if((env = getenv("windir")) != NULL) {
                   15467:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   15468:                        dos_info->boot_drive = env[0] - 'A' + 1;
                   15469:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   15470:                        dos_info->boot_drive = env[0] - 'a' + 1;
                   15471:                }
                   15472:        }
                   15473: #if defined(HAS_I386)
                   15474:        dos_info->i386_or_later = 1;
                   15475: #else
                   15476:        dos_info->i386_or_later = 0;
                   15477: #endif
                   15478:        dos_info->ext_mem_size = (min(MAX_MEM, 0x4000000) - 0x100000) >> 10;
                   15479:        
1.1.1.27  root     15480:        // ems (int 67h) and xms
1.1.1.25  root     15481:        device_t *xms_device = (device_t *)(mem + XMS_TOP);
                   15482:        xms_device->next_driver.w.l = 0xffff;
                   15483:        xms_device->next_driver.w.h = 0xffff;
                   15484:        xms_device->attributes = 0xc000;
1.1.1.29  root     15485:        xms_device->strategy = XMS_SIZE - sizeof(dummy_device_routine);
                   15486:        xms_device->interrupt = XMS_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     15487:        memcpy(xms_device->dev_name, "EMMXXXX0", 8);
                   15488:        
1.1.1.26  root     15489:        mem[XMS_TOP + 0x12] = 0xcd;     // int 68h (dummy)
                   15490:        mem[XMS_TOP + 0x13] = 0x68;
                   15491:        mem[XMS_TOP + 0x14] = 0xcf;     // iret
1.1.1.19  root     15492: #ifdef SUPPORT_XMS
                   15493:        if(support_xms) {
1.1.1.26  root     15494:                mem[XMS_TOP + 0x15] = 0xcd;     // int 69h (dummy)
                   15495:                mem[XMS_TOP + 0x16] = 0x69;
                   15496:                mem[XMS_TOP + 0x17] = 0xcb;     // retf
1.1.1.19  root     15497:        } else
                   15498: #endif
1.1.1.26  root     15499:        mem[XMS_TOP + 0x15] = 0xcb;     // retf
1.1.1.29  root     15500:        memcpy(mem + XMS_TOP + XMS_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.19  root     15501:        
1.1.1.26  root     15502:        // irq12 routine (mouse)
1.1.1.24  root     15503:        mem[0xfffd0 + 0x00] = 0xcd;     // int 6ah (dummy)
                   15504:        mem[0xfffd0 + 0x01] = 0x6a;
                   15505:        mem[0xfffd0 + 0x02] = 0x9a;     // call far mouse
                   15506:        mem[0xfffd0 + 0x03] = 0xff;
                   15507:        mem[0xfffd0 + 0x04] = 0xff;
                   15508:        mem[0xfffd0 + 0x05] = 0xff;
                   15509:        mem[0xfffd0 + 0x06] = 0xff;
                   15510:        mem[0xfffd0 + 0x07] = 0xcd;     // int 6bh (dummy)
                   15511:        mem[0xfffd0 + 0x08] = 0x6b;
                   15512:        mem[0xfffd0 + 0x09] = 0xcf;     // iret
                   15513:        
1.1.1.27  root     15514:        // case map routine
                   15515:        mem[0xfffd0 + 0x0a] = 0xcd;     // int 6ch (dummy)
                   15516:        mem[0xfffd0 + 0x0b] = 0x6c;
                   15517:        mem[0xfffd0 + 0x0c] = 0xcb;     // retf
                   15518:        
                   15519:        // font read routine
                   15520:        mem[0xfffd0 + 0x0d] = 0xcd;     // int 6dh (dummy)
                   15521:        mem[0xfffd0 + 0x0e] = 0x6d;
                   15522:        mem[0xfffd0 + 0x0f] = 0xcb;     // retf
1.1.1.19  root     15523:        
1.1.1.32  root     15524:        // error message read routine
                   15525:        mem[0xfffd0 + 0x10] = 0xcd;     // int 6eh (dummy)
                   15526:        mem[0xfffd0 + 0x11] = 0x6e;
                   15527:        mem[0xfffd0 + 0x12] = 0xcb;     // retf
                   15528:        
1.1.1.35  root     15529:        // dummy loop to wait BIOS/DOS service is done
                   15530:        mem[0xfffd0 + 0x13] = 0xe6;     // out f7h, al
                   15531:        mem[0xfffd0 + 0x14] = 0xf7;
                   15532:        mem[0xfffd0 + 0x15] = 0x78;     // js/jns -4
                   15533:        mem[0xfffd0 + 0x16] = 0xfc;
                   15534:        mem[0xfffd0 + 0x17] = 0xcb;     // retf
                   15535:        
1.1.1.26  root     15536:        // irq0 routine (system time)
1.1.1.35  root     15537:        mem[0xfffd0 + 0x18] = 0xcd;     // int 1ch
                   15538:        mem[0xfffd0 + 0x19] = 0x1c;
                   15539:        mem[0xfffd0 + 0x1a] = 0xea;     // jmp far (IRET_TOP >> 4):0008
                   15540:        mem[0xfffd0 + 0x1b] = 0x08;
                   15541:        mem[0xfffd0 + 0x1c] = 0x00;
                   15542:        mem[0xfffd0 + 0x1d] = ((IRET_TOP >> 4)     ) & 0xff;
                   15543:        mem[0xfffd0 + 0x1e] = ((IRET_TOP >> 4) >> 8) & 0xff;
1.1.1.14  root     15544:        
1.1.1.26  root     15545:        // boot routine
1.1       root     15546:        mem[0xffff0] = 0xf4;    // halt
                   15547:        mem[0xffff1] = 0xcd;    // int 21h
                   15548:        mem[0xffff2] = 0x21;
                   15549:        mem[0xffff3] = 0xcb;    // retf
                   15550:        
1.1.1.24  root     15551:        mem[0xffff5] = '0';     // rom date
                   15552:        mem[0xffff6] = '2';
                   15553:        mem[0xffff7] = '/';
                   15554:        mem[0xffff8] = '2';
                   15555:        mem[0xffff9] = '2';
                   15556:        mem[0xffffa] = '/';
                   15557:        mem[0xffffb] = '0';
                   15558:        mem[0xffffc] = '6';
                   15559:        mem[0xffffe] = 0xfc;    // machine id
                   15560:        mem[0xfffff] = 0x00;
                   15561:        
1.1       root     15562:        // param block
                   15563:        // + 0: param block (22bytes)
                   15564:        // +24: fcb1/2 (20bytes)
                   15565:        // +44: command tail (128bytes)
                   15566:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   15567:        param->env_seg = 0;
                   15568:        param->cmd_line.w.l = 44;
                   15569:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   15570:        param->fcb1.w.l = 24;
                   15571:        param->fcb1.w.h = (WORK_TOP >> 4);
                   15572:        param->fcb2.w.l = 24;
                   15573:        param->fcb2.w.h = (WORK_TOP >> 4);
                   15574:        
                   15575:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   15576:        
                   15577:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   15578:        if(argc > 1) {
                   15579:                sprintf(cmd_line->cmd, " %s", argv[1]);
                   15580:                for(int i = 2; i < argc; i++) {
                   15581:                        char tmp[128];
                   15582:                        sprintf(tmp, "%s %s", cmd_line->cmd, argv[i]);
                   15583:                        strcpy(cmd_line->cmd, tmp);
                   15584:                }
                   15585:                cmd_line->len = (UINT8)strlen(cmd_line->cmd);
                   15586:        } else {
                   15587:                cmd_line->len = 0;
                   15588:        }
                   15589:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   15590:        
                   15591:        // system file table
1.1.1.21  root     15592:        *(UINT32 *)(mem + SFT_TOP + 0) = 0xffffffff;
                   15593:        *(UINT16 *)(mem + SFT_TOP + 4) = 20;
1.1       root     15594:        
1.1.1.19  root     15595:        // disk buffer header (from DOSBox)
                   15596:        *(UINT16 *)(mem + DISK_BUF_TOP +  0) = 0xffff;          // forward ptr
                   15597:        *(UINT16 *)(mem + DISK_BUF_TOP +  2) = 0xffff;          // backward ptr
                   15598:        *(UINT8  *)(mem + DISK_BUF_TOP +  4) = 0xff;            // not in use
                   15599:        *(UINT8  *)(mem + DISK_BUF_TOP + 10) = 0x01;            // number of FATs
                   15600:        *(UINT32 *)(mem + DISK_BUF_TOP + 13) = 0xffffffff;      // pointer to DPB
                   15601:        
1.1       root     15602:        // current directory structure
                   15603:        msdos_cds_update(_getdrive() - 1);
                   15604:        
                   15605:        // fcb table
                   15606:        *(UINT32 *)(mem + FCB_TABLE_TOP + 0) = 0xffffffff;
1.1.1.14  root     15607:        *(UINT16 *)(mem + FCB_TABLE_TOP + 4) = 0;
1.1       root     15608:        
1.1.1.17  root     15609:        // nls stuff
                   15610:        msdos_nls_tables_init();
1.1       root     15611:        
                   15612:        // execute command
1.1.1.28  root     15613:        try {
                   15614:                if(msdos_process_exec(argv[0], param, 0)) {
                   15615:                        fatalerror("'%s' not found\n", argv[0]);
                   15616:                }
                   15617:        } catch(...) {
                   15618:                // we should not reach here :-(
                   15619:                fatalerror("failed to start '%s' because of unexpected exeption\n", argv[0]);
1.1       root     15620:        }
                   15621:        retval = 0;
                   15622:        return(0);
                   15623: }
                   15624: 
                   15625: #define remove_std_file(path) { \
                   15626:        int fd = _open(path, _O_RDONLY | _O_BINARY); \
                   15627:        if(fd != -1) { \
                   15628:                _lseek(fd, 0, SEEK_END); \
                   15629:                int size = _tell(fd); \
                   15630:                _close(fd); \
                   15631:                if(size == 0) { \
                   15632:                        remove(path); \
                   15633:                } \
                   15634:        } \
                   15635: }
                   15636: 
                   15637: void msdos_finish()
                   15638: {
                   15639:        for(int i = 0; i < MAX_FILES; i++) {
                   15640:                if(file_handler[i].valid) {
                   15641:                        _close(i);
                   15642:                }
                   15643:        }
1.1.1.21  root     15644: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     15645:        remove_std_file("stdaux.txt");
1.1.1.21  root     15646: #endif
1.1.1.30  root     15647: #ifdef SUPPORT_XMS
                   15648:        msdos_xms_finish();
                   15649: #endif
1.1       root     15650:        msdos_dbcs_table_finish();
                   15651: }
                   15652: 
                   15653: /* ----------------------------------------------------------------------------
                   15654:        PC/AT hardware emulation
                   15655: ---------------------------------------------------------------------------- */
                   15656: 
                   15657: void hardware_init()
                   15658: {
1.1.1.3   root     15659:        CPU_INIT_CALL(CPU_MODEL);
1.1       root     15660:        CPU_RESET_CALL(CPU_MODEL);
1.1.1.14  root     15661:        m_IF = 1;
1.1.1.3   root     15662: #if defined(HAS_I386)
1.1       root     15663:        cpu_type = (REG32(EDX) >> 8) & 0x0f;
                   15664:        cpu_step = (REG32(EDX) >> 0) & 0x0f;
1.1.1.3   root     15665: #endif
                   15666:        i386_set_a20_line(0);
1.1.1.14  root     15667:        
1.1.1.19  root     15668:        ems_init();
1.1.1.25  root     15669:        dma_init();
1.1       root     15670:        pic_init();
1.1.1.25  root     15671:        pio_init();
1.1.1.8   root     15672: #ifdef PIT_ALWAYS_RUNNING
                   15673:        pit_init();
                   15674: #else
1.1       root     15675:        pit_active = 0;
                   15676: #endif
1.1.1.25  root     15677:        sio_init();
1.1.1.8   root     15678:        cmos_init();
                   15679:        kbd_init();
1.1       root     15680: }
                   15681: 
1.1.1.10  root     15682: void hardware_finish()
                   15683: {
                   15684: #if defined(HAS_I386)
                   15685:        vtlb_free(m_vtlb);
                   15686: #endif
1.1.1.19  root     15687:        ems_finish();
1.1.1.37! root     15688:        pio_finish();
1.1.1.25  root     15689:        sio_finish();
1.1.1.10  root     15690: }
                   15691: 
1.1.1.28  root     15692: void hardware_release()
                   15693: {
                   15694:        // release hardware resources when this program will be terminated abnormally
                   15695: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     15696:        if(fp_debug_log != NULL) {
                   15697:                fclose(fp_debug_log);
                   15698:                fp_debug_log = NULL;
1.1.1.28  root     15699:        }
                   15700: #endif
                   15701: #if defined(HAS_I386)
                   15702:        vtlb_free(m_vtlb);
                   15703: #endif
                   15704:        ems_release();
1.1.1.37! root     15705:        pio_release();
1.1.1.28  root     15706:        sio_release();
                   15707: }
                   15708: 
1.1       root     15709: void hardware_run()
                   15710: {
1.1.1.22  root     15711: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.28  root     15712:        // open debug log file after msdos_init() is done not to use the standard file handlers
1.1.1.33  root     15713:        fp_debug_log = fopen("debug.log", "w");
1.1.1.22  root     15714: #endif
1.1.1.3   root     15715:        while(!m_halted) {
                   15716: #if defined(HAS_I386)
1.1       root     15717:                CPU_EXECUTE_CALL(i386);
1.1.1.14  root     15718:                if(m_eip != m_prev_eip) {
1.1.1.35  root     15719:                        idle_ops++;
                   15720:                }
1.1.1.14  root     15721: #else
1.1.1.35  root     15722:                CPU_EXECUTE_CALL(CPU_MODEL);
1.1.1.14  root     15723:                if(m_pc != m_prevpc) {
1.1.1.35  root     15724:                        idle_ops++;
1.1.1.14  root     15725:                }
1.1.1.35  root     15726: #endif
                   15727:                if(++update_ops == UPDATE_OPS) {
1.1       root     15728:                        hardware_update();
1.1.1.35  root     15729:                        update_ops = 0;
1.1       root     15730:                }
                   15731:        }
1.1.1.22  root     15732: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     15733:        if(fp_debug_log != NULL) {
                   15734:                fclose(fp_debug_log);
                   15735:                fp_debug_log = NULL;
1.1.1.28  root     15736:        }
1.1.1.22  root     15737: #endif
1.1       root     15738: }
                   15739: 
                   15740: void hardware_update()
                   15741: {
1.1.1.8   root     15742:        static UINT32 prev_time = 0;
                   15743:        UINT32 cur_time = timeGetTime();
                   15744:        
                   15745:        if(prev_time != cur_time) {
                   15746:                // update pit and raise irq0
                   15747: #ifndef PIT_ALWAYS_RUNNING
                   15748:                if(pit_active)
                   15749: #endif
                   15750:                {
                   15751:                        if(pit_run(0, cur_time)) {
                   15752:                                pic_req(0, 0, 1);
                   15753:                        }
                   15754:                        pit_run(1, cur_time);
                   15755:                        pit_run(2, cur_time);
                   15756:                }
1.1.1.24  root     15757:                
1.1.1.25  root     15758:                // update sio and raise irq4/3
1.1.1.29  root     15759:                for(int c = 0; c < 4; c++) {
1.1.1.25  root     15760:                        sio_update(c);
                   15761:                }
                   15762:                
1.1.1.24  root     15763:                // update keyboard and mouse
1.1.1.14  root     15764:                static UINT32 prev_tick = 0;
                   15765:                UINT32 cur_tick = cur_time / 32;
1.1.1.25  root     15766:                
1.1.1.14  root     15767:                if(prev_tick != cur_tick) {
                   15768:                        // update keyboard flags
                   15769:                        UINT8 state;
1.1.1.24  root     15770:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x0001) ? 0x80 : 0;
                   15771:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x0001) ? 0x40 : 0;
                   15772:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x0001) ? 0x20 : 0;
                   15773:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x0001) ? 0x10 : 0;
                   15774:                        state |= (GetAsyncKeyState(VK_MENU    ) & 0x8000) ? 0x08 : 0;
                   15775:                        state |= (GetAsyncKeyState(VK_CONTROL ) & 0x8000) ? 0x04 : 0;
                   15776:                        state |= (GetAsyncKeyState(VK_LSHIFT  ) & 0x8000) ? 0x02 : 0;
                   15777:                        state |= (GetAsyncKeyState(VK_RSHIFT  ) & 0x8000) ? 0x01 : 0;
1.1.1.14  root     15778:                        mem[0x417] = state;
                   15779:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x8000) ? 0x80 : 0;
                   15780:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   15781:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   15782:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
1.1.1.24  root     15783: //                     state |= (GetAsyncKeyState(VK_PAUSE   ) & 0x0001) ? 0x08 : 0;
                   15784: //                     state |= (GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x04 : 0;
1.1.1.14  root     15785:                        state |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   15786:                        state |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   15787:                        mem[0x418] = state;
                   15788:                        
1.1.1.24  root     15789:                        // update console input if needed
1.1.1.34  root     15790:                        if(!key_changed || mouse.hidden == 0) {
1.1.1.24  root     15791:                                update_console_input();
                   15792:                        }
                   15793:                        
                   15794:                        // raise irq1 if key is pressed/released
                   15795:                        if(key_changed) {
1.1.1.8   root     15796:                                pic_req(0, 1, 1);
1.1.1.24  root     15797:                                key_changed = false;
                   15798:                        }
                   15799:                        
                   15800:                        // raise irq12 if mouse status is changed
                   15801:                        if(mouse.status & mouse.call_mask) {
1.1.1.34  root     15802: //                             if(mouse.hidden == 0) {
1.1.1.24  root     15803:                                        pic_req(1, 4, 1);
                   15804:                                        mouse.status_irq = mouse.status & mouse.call_mask;
1.1.1.34  root     15805: //                             }
1.1.1.24  root     15806:                                mouse.status &= ~mouse.call_mask;
1.1.1.8   root     15807:                        }
1.1.1.24  root     15808:                        
1.1.1.14  root     15809:                        prev_tick = cur_tick;
1.1.1.8   root     15810:                }
1.1.1.24  root     15811:                
1.1.1.19  root     15812:                // update daily timer counter
                   15813:                pcbios_update_daily_timer_counter(cur_time);
1.1.1.25  root     15814:                
1.1.1.8   root     15815:                prev_time = cur_time;
1.1       root     15816:        }
                   15817: }
                   15818: 
1.1.1.19  root     15819: // ems
                   15820: 
                   15821: void ems_init()
                   15822: {
                   15823:        memset(ems_handles, 0, sizeof(ems_handles));
                   15824:        memset(ems_pages, 0, sizeof(ems_pages));
                   15825:        free_ems_pages = MAX_EMS_PAGES;
                   15826: }
                   15827: 
                   15828: void ems_finish()
                   15829: {
1.1.1.28  root     15830:        ems_release();
                   15831: }
                   15832: 
                   15833: void ems_release()
                   15834: {
1.1.1.31  root     15835:        for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.28  root     15836:                if(ems_handles[i].buffer != NULL) {
1.1.1.19  root     15837:                        free(ems_handles[i].buffer);
                   15838:                        ems_handles[i].buffer = NULL;
                   15839:                }
                   15840:        }
                   15841: }
                   15842: 
                   15843: void ems_allocate_pages(int handle, int pages)
                   15844: {
                   15845:        if(pages > 0) {
                   15846:                ems_handles[handle].buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   15847:        } else {
                   15848:                ems_handles[handle].buffer = NULL;
                   15849:        }
                   15850:        ems_handles[handle].pages = pages;
                   15851:        ems_handles[handle].allocated = true;
                   15852:        free_ems_pages -= pages;
                   15853: }
                   15854: 
                   15855: void ems_reallocate_pages(int handle, int pages)
                   15856: {
                   15857:        if(ems_handles[handle].allocated) {
                   15858:                if(ems_handles[handle].pages != pages) {
                   15859:                        UINT8 *new_buffer = NULL;
                   15860:                        
                   15861:                        if(pages > 0) {
                   15862:                                new_buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   15863:                        }
1.1.1.32  root     15864:                        if(ems_handles[handle].buffer != NULL) {
                   15865:                                if(new_buffer != NULL) {
1.1.1.19  root     15866:                                        if(pages > ems_handles[handle].pages) {
                   15867:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * ems_handles[handle].pages);
                   15868:                                        } else {
                   15869:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * pages);
                   15870:                                        }
                   15871:                                }
                   15872:                                free(ems_handles[handle].buffer);
                   15873:                                ems_handles[handle].buffer = NULL;
                   15874:                        }
                   15875:                        free_ems_pages += ems_handles[handle].pages;
                   15876:                        
                   15877:                        ems_handles[handle].buffer = new_buffer;
                   15878:                        ems_handles[handle].pages = pages;
                   15879:                        free_ems_pages -= pages;
                   15880:                }
                   15881:        } else {
                   15882:                ems_allocate_pages(handle, pages);
                   15883:        }
                   15884: }
                   15885: 
                   15886: void ems_release_pages(int handle)
                   15887: {
                   15888:        if(ems_handles[handle].allocated) {
1.1.1.32  root     15889:                if(ems_handles[handle].buffer != NULL) {
1.1.1.19  root     15890:                        free(ems_handles[handle].buffer);
                   15891:                        ems_handles[handle].buffer = NULL;
                   15892:                }
                   15893:                free_ems_pages += ems_handles[handle].pages;
                   15894:                ems_handles[handle].allocated = false;
                   15895:        }
                   15896: }
                   15897: 
                   15898: void ems_map_page(int physical, int handle, int logical)
                   15899: {
                   15900:        if(ems_pages[physical].mapped) {
                   15901:                if(ems_pages[physical].handle == handle && ems_pages[physical].page == logical) {
                   15902:                        return;
                   15903:                }
                   15904:                ems_unmap_page(physical);
                   15905:        }
1.1.1.32  root     15906:        if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     15907:                memcpy(mem + EMS_TOP + 0x4000 * physical, ems_handles[handle].buffer + 0x4000 * logical, 0x4000);
                   15908:        }
                   15909:        ems_pages[physical].handle = handle;
                   15910:        ems_pages[physical].page = logical;
                   15911:        ems_pages[physical].mapped = true;
                   15912: }
                   15913: 
                   15914: void ems_unmap_page(int physical)
                   15915: {
                   15916:        if(ems_pages[physical].mapped) {
                   15917:                int handle = ems_pages[physical].handle;
                   15918:                int logical = ems_pages[physical].page;
                   15919:                
1.1.1.32  root     15920:                if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     15921:                        memcpy(ems_handles[handle].buffer + 0x4000 * logical, mem + EMS_TOP + 0x4000 * physical, 0x4000);
                   15922:                }
                   15923:                ems_pages[physical].mapped = false;
                   15924:        }
                   15925: }
                   15926: 
1.1.1.25  root     15927: // dma
1.1       root     15928: 
1.1.1.25  root     15929: void dma_init()
1.1       root     15930: {
1.1.1.26  root     15931:        memset(dma, 0, sizeof(dma));
1.1.1.25  root     15932:        for(int c = 0; c < 2; c++) {
1.1.1.26  root     15933: //             for(int ch = 0; ch < 4; ch++) {
                   15934: //                     dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w = 0xffff;
                   15935: //             }
1.1.1.25  root     15936:                dma_reset(c);
                   15937:        }
1.1       root     15938: }
                   15939: 
1.1.1.25  root     15940: void dma_reset(int c)
1.1       root     15941: {
1.1.1.25  root     15942:        dma[c].low_high = false;
                   15943:        dma[c].cmd = dma[c].req = dma[c].tc = 0;
                   15944:        dma[c].mask = 0xff;
                   15945: }
                   15946: 
                   15947: void dma_write(int c, UINT32 addr, UINT8 data)
                   15948: {
                   15949:        int ch = (addr >> 1) & 3;
                   15950:        UINT8 bit = 1 << (data & 3);
                   15951:        
                   15952:        switch(addr & 0x0f) {
                   15953:        case 0x00: case 0x02: case 0x04: case 0x06:
                   15954:                if(dma[c].low_high) {
                   15955:                        dma[c].ch[ch].bareg.b.h = data;
1.1       root     15956:                } else {
1.1.1.25  root     15957:                        dma[c].ch[ch].bareg.b.l = data;
                   15958:                }
                   15959:                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   15960:                dma[c].low_high = !dma[c].low_high;
                   15961:                break;
                   15962:        case 0x01: case 0x03: case 0x05: case 0x07:
                   15963:                if(dma[c].low_high) {
                   15964:                        dma[c].ch[ch].bcreg.b.h = data;
                   15965:                } else {
                   15966:                        dma[c].ch[ch].bcreg.b.l = data;
                   15967:                }
                   15968:                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   15969:                dma[c].low_high = !dma[c].low_high;
                   15970:                break;
                   15971:        case 0x08:
                   15972:                // command register
                   15973:                dma[c].cmd = data;
                   15974:                break;
                   15975:        case 0x09:
                   15976:                // dma[c].request register
                   15977:                if(data & 4) {
                   15978:                        if(!(dma[c].req & bit)) {
                   15979:                                dma[c].req |= bit;
                   15980: //                             dma_run(c, ch);
                   15981:                        }
                   15982:                } else {
                   15983:                        dma[c].req &= ~bit;
                   15984:                }
                   15985:                break;
                   15986:        case 0x0a:
                   15987:                // single mask register
                   15988:                if(data & 4) {
                   15989:                        dma[c].mask |= bit;
                   15990:                } else {
                   15991:                        dma[c].mask &= ~bit;
                   15992:                }
                   15993:                break;
                   15994:        case 0x0b:
                   15995:                // mode register
                   15996:                dma[c].ch[data & 3].mode = data;
                   15997:                break;
                   15998:        case 0x0c:
                   15999:                dma[c].low_high = false;
                   16000:                break;
                   16001:        case 0x0d:
                   16002:                // clear master
                   16003:                dma_reset(c);
                   16004:                break;
                   16005:        case 0x0e:
                   16006:                // clear mask register
                   16007:                dma[c].mask = 0;
                   16008:                break;
                   16009:        case 0x0f:
                   16010:                // all mask register
                   16011:                dma[c].mask = data & 0x0f;
                   16012:                break;
                   16013:        }
                   16014: }
                   16015: 
                   16016: UINT8 dma_read(int c, UINT32 addr)
                   16017: {
                   16018:        int ch = (addr >> 1) & 3;
                   16019:        UINT8 val = 0xff;
                   16020:        
                   16021:        switch(addr & 0x0f) {
                   16022:        case 0x00: case 0x02: case 0x04: case 0x06:
                   16023:                if(dma[c].low_high) {
                   16024:                        val = dma[c].ch[ch].areg.b.h;
                   16025:                } else {
                   16026:                        val = dma[c].ch[ch].areg.b.l;
                   16027:                }
                   16028:                dma[c].low_high = !dma[c].low_high;
                   16029:                return(val);
                   16030:        case 0x01: case 0x03: case 0x05: case 0x07:
                   16031:                if(dma[c].low_high) {
                   16032:                        val = dma[c].ch[ch].creg.b.h;
                   16033:                } else {
                   16034:                        val = dma[c].ch[ch].creg.b.l;
                   16035:                }
                   16036:                dma[c].low_high = !dma[c].low_high;
                   16037:                return(val);
                   16038:        case 0x08:
                   16039:                // status register
                   16040:                val = (dma[c].req << 4) | dma[c].tc;
                   16041:                dma[c].tc = 0;
                   16042:                return(val);
                   16043:        case 0x0d:
1.1.1.26  root     16044:                // temporary register (intel 82374 does not support)
1.1.1.25  root     16045:                return(dma[c].tmp & 0xff);
1.1.1.26  root     16046:        case 0x0f:
                   16047:                // mask register (intel 82374 does support)
                   16048:                return(dma[c].mask);
1.1.1.25  root     16049:        }
                   16050:        return(0xff);
                   16051: }
                   16052: 
                   16053: void dma_page_write(int c, int ch, UINT8 data)
                   16054: {
                   16055:        dma[c].ch[ch].pagereg = data;
                   16056: }
                   16057: 
                   16058: UINT8 dma_page_read(int c, int ch)
                   16059: {
                   16060:        return(dma[c].ch[ch].pagereg);
                   16061: }
                   16062: 
                   16063: void dma_run(int c, int ch)
                   16064: {
                   16065:        UINT8 bit = 1 << ch;
                   16066:        
                   16067:        if((dma[c].req & bit) && !(dma[c].mask & bit)) {
                   16068:                // execute dma
                   16069:                while(dma[c].req & bit) {
                   16070:                        if(ch == 0 && (dma[c].cmd & 0x01)) {
                   16071:                                // memory -> memory
                   16072:                                UINT32 saddr = dma[c].ch[0].areg.w | (dma[c].ch[0].pagereg << 16);
                   16073:                                UINT32 daddr = dma[c].ch[1].areg.w | (dma[c].ch[1].pagereg << 16);
                   16074:                                
                   16075:                                if(c == 0) {
                   16076:                                        dma[c].tmp = read_byte(saddr);
                   16077:                                        write_byte(daddr, dma[c].tmp);
                   16078:                                } else {
                   16079:                                        dma[c].tmp = read_word(saddr << 1);
                   16080:                                        write_word(daddr << 1, dma[c].tmp);
                   16081:                                }
                   16082:                                if(!(dma[c].cmd & 0x02)) {
                   16083:                                        if(dma[c].ch[0].mode & 0x20) {
                   16084:                                                dma[c].ch[0].areg.w--;
                   16085:                                                if(dma[c].ch[0].areg.w == 0xffff) {
                   16086:                                                        dma[c].ch[0].pagereg--;
                   16087:                                                }
                   16088:                                        } else {
                   16089:                                                dma[c].ch[0].areg.w++;
                   16090:                                                if(dma[c].ch[0].areg.w == 0) {
                   16091:                                                        dma[c].ch[0].pagereg++;
                   16092:                                                }
                   16093:                                        }
                   16094:                                }
                   16095:                                if(dma[c].ch[1].mode & 0x20) {
                   16096:                                        dma[c].ch[1].areg.w--;
                   16097:                                        if(dma[c].ch[1].areg.w == 0xffff) {
                   16098:                                                dma[c].ch[1].pagereg--;
                   16099:                                        }
                   16100:                                } else {
                   16101:                                        dma[c].ch[1].areg.w++;
                   16102:                                        if(dma[c].ch[1].areg.w == 0) {
                   16103:                                                dma[c].ch[1].pagereg++;
                   16104:                                        }
                   16105:                                }
                   16106:                                
                   16107:                                // check dma condition
                   16108:                                if(dma[c].ch[0].creg.w-- == 0) {
                   16109:                                        if(dma[c].ch[0].mode & 0x10) {
                   16110:                                                // self initialize
                   16111:                                                dma[c].ch[0].areg.w = dma[c].ch[0].bareg.w;
                   16112:                                                dma[c].ch[0].creg.w = dma[c].ch[0].bcreg.w;
                   16113:                                        } else {
                   16114: //                                             dma[c].mask |= bit;
                   16115:                                        }
                   16116:                                }
                   16117:                                if(dma[c].ch[1].creg.w-- == 0) {
                   16118:                                        // terminal count
                   16119:                                        if(dma[c].ch[1].mode & 0x10) {
                   16120:                                                // self initialize
                   16121:                                                dma[c].ch[1].areg.w = dma[c].ch[1].bareg.w;
                   16122:                                                dma[c].ch[1].creg.w = dma[c].ch[1].bcreg.w;
                   16123:                                        } else {
                   16124:                                                dma[c].mask |= bit;
                   16125:                                        }
                   16126:                                        dma[c].req &= ~bit;
                   16127:                                        dma[c].tc |= bit;
                   16128:                                }
                   16129:                        } else {
                   16130:                                UINT32 addr = dma[c].ch[ch].areg.w | (dma[c].ch[ch].pagereg << 16);
                   16131:                                
                   16132:                                if((dma[c].ch[ch].mode & 0x0c) == 0x00) {
                   16133:                                        // verify
                   16134:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x04) {
                   16135:                                        // io -> memory
                   16136:                                        if(c == 0) {
                   16137:                                                dma[c].tmp = read_io_byte(dma[c].ch[ch].port);
                   16138:                                                write_byte(addr, dma[c].tmp);
                   16139:                                        } else {
                   16140:                                                dma[c].tmp = read_io_word(dma[c].ch[ch].port);
                   16141:                                                write_word(addr << 1, dma[c].tmp);
                   16142:                                        }
                   16143:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x08) {
                   16144:                                        // memory -> io
                   16145:                                        if(c == 0) {
                   16146:                                                dma[c].tmp = read_byte(addr);
                   16147:                                                write_io_byte(dma[c].ch[ch].port, dma[c].tmp);
                   16148:                                        } else {
                   16149:                                                dma[c].tmp = read_word(addr << 1);
                   16150:                                                write_io_word(dma[c].ch[ch].port, dma[c].tmp);
                   16151:                                        }
                   16152:                                }
                   16153:                                if(dma[c].ch[ch].mode & 0x20) {
                   16154:                                        dma[c].ch[ch].areg.w--;
                   16155:                                        if(dma[c].ch[ch].areg.w == 0xffff) {
                   16156:                                                dma[c].ch[ch].pagereg--;
                   16157:                                        }
                   16158:                                } else {
                   16159:                                        dma[c].ch[ch].areg.w++;
                   16160:                                        if(dma[c].ch[ch].areg.w == 0) {
                   16161:                                                dma[c].ch[ch].pagereg++;
                   16162:                                        }
                   16163:                                }
                   16164:                                
                   16165:                                // check dma condition
                   16166:                                if(dma[c].ch[ch].creg.w-- == 0) {
                   16167:                                        // terminal count
                   16168:                                        if(dma[c].ch[ch].mode & 0x10) {
                   16169:                                                // self initialize
                   16170:                                                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   16171:                                                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   16172:                                        } else {
                   16173:                                                dma[c].mask |= bit;
                   16174:                                        }
                   16175:                                        dma[c].req &= ~bit;
                   16176:                                        dma[c].tc |= bit;
                   16177:                                } else if((dma[c].ch[ch].mode & 0xc0) == 0x40) {
                   16178:                                        // single mode
                   16179:                                        break;
                   16180:                                }
                   16181:                        }
                   16182:                }
                   16183:        }
                   16184: }
                   16185: 
                   16186: // pic
                   16187: 
                   16188: void pic_init()
                   16189: {
                   16190:        memset(pic, 0, sizeof(pic));
                   16191:        pic[0].imr = pic[1].imr = 0xff;
                   16192:        
                   16193:        // from bochs bios
                   16194:        pic_write(0, 0, 0x11);  // icw1 = 11h
                   16195:        pic_write(0, 1, 0x08);  // icw2 = 08h
                   16196:        pic_write(0, 1, 0x04);  // icw3 = 04h
                   16197:        pic_write(0, 1, 0x01);  // icw4 = 01h
                   16198:        pic_write(0, 1, 0xb8);  // ocw1 = b8h
                   16199:        pic_write(1, 0, 0x11);  // icw1 = 11h
                   16200:        pic_write(1, 1, 0x70);  // icw2 = 70h
                   16201:        pic_write(1, 1, 0x02);  // icw3 = 02h
                   16202:        pic_write(1, 1, 0x01);  // icw4 = 01h
                   16203: }
                   16204: 
                   16205: void pic_write(int c, UINT32 addr, UINT8 data)
                   16206: {
                   16207:        if(addr & 1) {
                   16208:                if(pic[c].icw2_r) {
                   16209:                        // icw2
                   16210:                        pic[c].icw2 = data;
                   16211:                        pic[c].icw2_r = 0;
                   16212:                } else if(pic[c].icw3_r) {
                   16213:                        // icw3
                   16214:                        pic[c].icw3 = data;
                   16215:                        pic[c].icw3_r = 0;
                   16216:                } else if(pic[c].icw4_r) {
                   16217:                        // icw4
                   16218:                        pic[c].icw4 = data;
                   16219:                        pic[c].icw4_r = 0;
                   16220:                } else {
                   16221:                        // ocw1
1.1       root     16222:                        pic[c].imr = data;
                   16223:                }
                   16224:        } else {
                   16225:                if(data & 0x10) {
                   16226:                        // icw1
                   16227:                        pic[c].icw1 = data;
                   16228:                        pic[c].icw2_r = 1;
                   16229:                        pic[c].icw3_r = (data & 2) ? 0 : 1;
                   16230:                        pic[c].icw4_r = data & 1;
                   16231:                        pic[c].irr = 0;
                   16232:                        pic[c].isr = 0;
                   16233:                        pic[c].imr = 0;
                   16234:                        pic[c].prio = 0;
                   16235:                        if(!(pic[c].icw1 & 1)) {
                   16236:                                pic[c].icw4 = 0;
                   16237:                        }
                   16238:                        pic[c].ocw3 = 0;
                   16239:                } else if(data & 8) {
                   16240:                        // ocw3
                   16241:                        if(!(data & 2)) {
                   16242:                                data = (data & ~1) | (pic[c].ocw3 & 1);
                   16243:                        }
                   16244:                        if(!(data & 0x40)) {
                   16245:                                data = (data & ~0x20) | (pic[c].ocw3 & 0x20);
                   16246:                        }
                   16247:                        pic[c].ocw3 = data;
                   16248:                } else {
                   16249:                        // ocw2
                   16250:                        int level = 0;
                   16251:                        if(data & 0x40) {
                   16252:                                level = data & 7;
                   16253:                        } else {
                   16254:                                if(!pic[c].isr) {
                   16255:                                        return;
                   16256:                                }
                   16257:                                level = pic[c].prio;
                   16258:                                while(!(pic[c].isr & (1 << level))) {
                   16259:                                        level = (level + 1) & 7;
                   16260:                                }
                   16261:                        }
                   16262:                        if(data & 0x80) {
                   16263:                                pic[c].prio = (level + 1) & 7;
                   16264:                        }
                   16265:                        if(data & 0x20) {
                   16266:                                pic[c].isr &= ~(1 << level);
                   16267:                        }
                   16268:                }
                   16269:        }
                   16270:        pic_update();
                   16271: }
                   16272: 
                   16273: UINT8 pic_read(int c, UINT32 addr)
                   16274: {
                   16275:        if(addr & 1) {
                   16276:                return(pic[c].imr);
                   16277:        } else {
                   16278:                // polling mode is not supported...
                   16279:                //if(pic[c].ocw3 & 4) {
                   16280:                //      return ???;
                   16281:                //}
                   16282:                if(pic[c].ocw3 & 1) {
                   16283:                        return(pic[c].isr);
                   16284:                } else {
                   16285:                        return(pic[c].irr);
                   16286:                }
                   16287:        }
                   16288: }
                   16289: 
                   16290: void pic_req(int c, int level, int signal)
                   16291: {
                   16292:        if(signal) {
                   16293:                pic[c].irr |= (1 << level);
                   16294:        } else {
                   16295:                pic[c].irr &= ~(1 << level);
                   16296:        }
                   16297:        pic_update();
                   16298: }
                   16299: 
                   16300: int pic_ack()
                   16301: {
                   16302:        // ack (INTA=L)
                   16303:        pic[pic_req_chip].isr |= pic_req_bit;
                   16304:        pic[pic_req_chip].irr &= ~pic_req_bit;
                   16305:        if(pic_req_chip > 0) {
                   16306:                // update isr and irr of master
                   16307:                UINT8 slave = 1 << (pic[pic_req_chip].icw3 & 7);
                   16308:                pic[pic_req_chip - 1].isr |= slave;
                   16309:                pic[pic_req_chip - 1].irr &= ~slave;
                   16310:        }
                   16311:        //if(pic[pic_req_chip].icw4 & 1) {
                   16312:                // 8086 mode
                   16313:                int vector = (pic[pic_req_chip].icw2 & 0xf8) | pic_req_level;
                   16314:        //} else {
                   16315:        //      // 8080 mode
                   16316:        //      UINT16 addr = (UINT16)pic[pic_req_chip].icw2 << 8;
                   16317:        //      if(pic[pic_req_chip].icw1 & 4) {
                   16318:        //              addr |= (pic[pic_req_chip].icw1 & 0xe0) | (pic_req_level << 2);
                   16319:        //      } else {
                   16320:        //              addr |= (pic[pic_req_chip].icw1 & 0xc0) | (pic_req_level << 3);
                   16321:        //      }
                   16322:        //      vector = 0xcd | (addr << 8);
                   16323:        //}
                   16324:        if(pic[pic_req_chip].icw4 & 2) {
                   16325:                // auto eoi
                   16326:                pic[pic_req_chip].isr &= ~pic_req_bit;
                   16327:        }
                   16328:        return(vector);
                   16329: }
                   16330: 
                   16331: void pic_update()
                   16332: {
                   16333:        for(int c = 0; c < 2; c++) {
                   16334:                UINT8 irr = pic[c].irr;
                   16335:                if(c + 1 < 2) {
                   16336:                        // this is master
                   16337:                        if(pic[c + 1].irr & (~pic[c + 1].imr)) {
                   16338:                                // request from slave
                   16339:                                irr |= 1 << (pic[c + 1].icw3 & 7);
                   16340:                        }
                   16341:                }
                   16342:                irr &= (~pic[c].imr);
                   16343:                if(!irr) {
                   16344:                        break;
                   16345:                }
                   16346:                if(!(pic[c].ocw3 & 0x20)) {
                   16347:                        irr |= pic[c].isr;
                   16348:                }
                   16349:                int level = pic[c].prio;
                   16350:                UINT8 bit = 1 << level;
                   16351:                while(!(irr & bit)) {
                   16352:                        level = (level + 1) & 7;
                   16353:                        bit = 1 << level;
                   16354:                }
                   16355:                if((c + 1 < 2) && (pic[c].icw3 & bit)) {
                   16356:                        // check slave
                   16357:                        continue;
                   16358:                }
                   16359:                if(pic[c].isr & bit) {
                   16360:                        break;
                   16361:                }
                   16362:                // interrupt request
                   16363:                pic_req_chip = c;
                   16364:                pic_req_level = level;
                   16365:                pic_req_bit = bit;
1.1.1.3   root     16366:                i386_set_irq_line(INPUT_LINE_IRQ, HOLD_LINE);
1.1       root     16367:                return;
                   16368:        }
1.1.1.3   root     16369:        i386_set_irq_line(INPUT_LINE_IRQ, CLEAR_LINE);
1.1.1.2   root     16370: }
1.1       root     16371: 
1.1.1.25  root     16372: // pio
                   16373: 
                   16374: void pio_init()
                   16375: {
1.1.1.26  root     16376:        memset(pio, 0, sizeof(pio));
1.1.1.37! root     16377:        
1.1.1.25  root     16378:        for(int c = 0; c < 2; c++) {
1.1.1.37! root     16379:                pio[c].stat = 0xdf;
1.1.1.25  root     16380:                pio[c].ctrl = 0x0c;
                   16381:        }
                   16382: }
                   16383: 
1.1.1.37! root     16384: void pio_finish()
        !          16385: {
        !          16386:        pio_release();
        !          16387: }
        !          16388: 
        !          16389: void pio_release()
        !          16390: {
        !          16391:        for(int c = 0; c < 2; c++) {
        !          16392:                if(pio[c].fp != NULL) {
        !          16393:                        fclose(pio[c].fp);
        !          16394:                        pio[c].fp = NULL;
        !          16395:                }
        !          16396:        }
        !          16397: }
        !          16398: 
1.1.1.25  root     16399: void pio_write(int c, UINT32 addr, UINT8 data)
                   16400: {
                   16401:        switch(addr & 3) {
                   16402:        case 0:
                   16403:                pio[c].data = data;
                   16404:                break;
                   16405:        case 2:
1.1.1.37! root     16406:                if((pio[c].ctrl & 0x01) && !(data & 0x01)) {
        !          16407:                        // strobe H -> L
        !          16408:                        if(pio[c].data == 0x0d && (data & 0x02)) {
        !          16409:                                // auto feed
        !          16410:                                printer_out(c, 0x0d);
        !          16411:                                printer_out(c, 0x0a);
        !          16412:                        } else {
        !          16413:                                printer_out(c, pio[c].data);
        !          16414:                        }
        !          16415:                        pio[c].stat &= ~0x40; // set ack
        !          16416:                }
1.1.1.25  root     16417:                pio[c].ctrl = data;
                   16418:                break;
                   16419:        }
                   16420: }
                   16421: 
                   16422: UINT8 pio_read(int c, UINT32 addr)
                   16423: {
                   16424:        switch(addr & 3) {
                   16425:        case 0:
1.1.1.37! root     16426:                if(pio[c].ctrl & 0x20) {
        !          16427:                        // input mode
        !          16428:                        return(0xff);
        !          16429:                }
1.1.1.25  root     16430:                return(pio[c].data);
                   16431:        case 1:
1.1.1.37! root     16432:                {
        !          16433:                        UINT8 stat = pio[c].stat;
        !          16434:                        pio[c].stat |= 0x40; // clear ack
        !          16435:                        return(stat);
        !          16436:                }
1.1.1.25  root     16437:        case 2:
                   16438:                return(pio[c].ctrl);
                   16439:        }
                   16440:        return(0xff);
                   16441: }
                   16442: 
1.1.1.37! root     16443: void printer_out(int c, UINT8 data)
        !          16444: {
        !          16445:        SYSTEMTIME time;
        !          16446:        
        !          16447:        GetLocalTime(&time);
        !          16448:        
        !          16449:        if(pio[c].fp != NULL) {
        !          16450:                // if at least 1000ms passed from last written, close the current file
        !          16451:                FILETIME ftime1;
        !          16452:                FILETIME ftime2;
        !          16453:                SystemTimeToFileTime(&pio[c].time, &ftime1);
        !          16454:                SystemTimeToFileTime(&time, &ftime2);
        !          16455:                INT64 *time1 = (INT64 *)&ftime1;
        !          16456:                INT64 *time2 = (INT64 *)&ftime2;
        !          16457:                INT64 msec = (*time2 - *time1) / 10000;
        !          16458:                
        !          16459:                if(msec >= 1000) {
        !          16460:                        fclose(pio[c].fp);
        !          16461:                        pio[c].fp = NULL;
        !          16462:                }
        !          16463:        }
        !          16464:        if(pio[c].fp == NULL) {
        !          16465:                // create a new file in the temp folder
        !          16466:                char file_name[MAX_PATH];
        !          16467:                
        !          16468:                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);
        !          16469:                if(GetTempPath(MAX_PATH, pio[c].path)) {
        !          16470:                        strcat(pio[c].path, file_name);
        !          16471:                } else {
        !          16472:                        strcpy(pio[c].path, file_name);
        !          16473:                }
        !          16474:                pio[c].fp = fopen(pio[c].path, "wb");
        !          16475:        }
        !          16476:        if(pio[c].fp != NULL) {
        !          16477:                fputc(data, pio[c].fp);
        !          16478:                pio[c].time = time;
        !          16479:        }
        !          16480: }
        !          16481: 
1.1       root     16482: // pit
                   16483: 
1.1.1.22  root     16484: #define PIT_FREQ 1193182ULL
1.1       root     16485: #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)
                   16486: 
                   16487: void pit_init()
                   16488: {
1.1.1.8   root     16489:        memset(pit, 0, sizeof(pit));
1.1       root     16490:        for(int ch = 0; ch < 3; ch++) {
                   16491:                pit[ch].count = 0x10000;
                   16492:                pit[ch].ctrl_reg = 0x34;
                   16493:                pit[ch].mode = 3;
                   16494:        }
                   16495:        
                   16496:        // from bochs bios
                   16497:        pit_write(3, 0x34);
                   16498:        pit_write(0, 0x00);
                   16499:        pit_write(0, 0x00);
                   16500: }
                   16501: 
                   16502: void pit_write(int ch, UINT8 val)
                   16503: {
1.1.1.8   root     16504: #ifndef PIT_ALWAYS_RUNNING
1.1       root     16505:        if(!pit_active) {
                   16506:                pit_active = 1;
                   16507:                pit_init();
                   16508:        }
1.1.1.8   root     16509: #endif
1.1       root     16510:        switch(ch) {
                   16511:        case 0:
                   16512:        case 1:
                   16513:        case 2:
                   16514:                // write count register
                   16515:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   16516:                        if(pit[ch].ctrl_reg & 0x10) {
                   16517:                                pit[ch].low_write = 1;
                   16518:                        }
                   16519:                        if(pit[ch].ctrl_reg & 0x20) {
                   16520:                                pit[ch].high_write = 1;
                   16521:                        }
                   16522:                }
                   16523:                if(pit[ch].low_write) {
                   16524:                        pit[ch].count_reg = val;
                   16525:                        pit[ch].low_write = 0;
                   16526:                } else if(pit[ch].high_write) {
                   16527:                        if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   16528:                                pit[ch].count_reg = val << 8;
                   16529:                        } else {
                   16530:                                pit[ch].count_reg |= val << 8;
                   16531:                        }
                   16532:                        pit[ch].high_write = 0;
                   16533:                }
                   16534:                // start count
1.1.1.8   root     16535:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   16536:                        if(pit[ch].mode == 0 || pit[ch].mode == 4 || pit[ch].prev_time == 0) {
                   16537:                                pit[ch].count = PIT_COUNT_VALUE(ch);
                   16538:                                pit[ch].prev_time = timeGetTime();
                   16539:                                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     16540:                        }
                   16541:                }
                   16542:                break;
                   16543:        case 3: // ctrl reg
                   16544:                if((val & 0xc0) == 0xc0) {
                   16545:                        // i8254 read-back command
                   16546:                        for(ch = 0; ch < 3; ch++) {
                   16547:                                if(!(val & 0x10) && !pit[ch].status_latched) {
                   16548:                                        pit[ch].status = pit[ch].ctrl_reg & 0x3f;
                   16549:                                        pit[ch].status_latched = 1;
                   16550:                                }
                   16551:                                if(!(val & 0x20) && !pit[ch].count_latched) {
                   16552:                                        pit_latch_count(ch);
                   16553:                                }
                   16554:                        }
                   16555:                        break;
                   16556:                }
                   16557:                ch = (val >> 6) & 3;
                   16558:                if(val & 0x30) {
1.1.1.35  root     16559:                        static const int modes[8] = {0, 1, 2, 3, 4, 5, 2, 3};
1.1       root     16560:                        pit[ch].mode = modes[(val >> 1) & 7];
                   16561:                        pit[ch].count_latched = 0;
                   16562:                        pit[ch].low_read = pit[ch].high_read = 0;
                   16563:                        pit[ch].low_write = pit[ch].high_write = 0;
                   16564:                        pit[ch].ctrl_reg = val;
                   16565:                        // stop count
1.1.1.8   root     16566:                        pit[ch].prev_time = pit[ch].expired_time = 0;
1.1       root     16567:                        pit[ch].count_reg = 0;
                   16568:                } else if(!pit[ch].count_latched) {
                   16569:                        pit_latch_count(ch);
                   16570:                }
                   16571:                break;
                   16572:        }
                   16573: }
                   16574: 
                   16575: UINT8 pit_read(int ch)
                   16576: {
1.1.1.8   root     16577: #ifndef PIT_ALWAYS_RUNNING
1.1       root     16578:        if(!pit_active) {
                   16579:                pit_active = 1;
                   16580:                pit_init();
                   16581:        }
1.1.1.8   root     16582: #endif
1.1       root     16583:        switch(ch) {
                   16584:        case 0:
                   16585:        case 1:
                   16586:        case 2:
                   16587:                if(pit[ch].status_latched) {
                   16588:                        pit[ch].status_latched = 0;
                   16589:                        return(pit[ch].status);
                   16590:                }
                   16591:                // if not latched, through current count
                   16592:                if(!pit[ch].count_latched) {
                   16593:                        if(!pit[ch].low_read && !pit[ch].high_read) {
                   16594:                                pit_latch_count(ch);
                   16595:                        }
                   16596:                }
                   16597:                // return latched count
                   16598:                if(pit[ch].low_read) {
                   16599:                        pit[ch].low_read = 0;
                   16600:                        if(!pit[ch].high_read) {
                   16601:                                pit[ch].count_latched = 0;
                   16602:                        }
                   16603:                        return(pit[ch].latch & 0xff);
                   16604:                } else if(pit[ch].high_read) {
                   16605:                        pit[ch].high_read = 0;
                   16606:                        pit[ch].count_latched = 0;
                   16607:                        return((pit[ch].latch >> 8) & 0xff);
                   16608:                }
                   16609:        }
                   16610:        return(0xff);
                   16611: }
                   16612: 
1.1.1.8   root     16613: int pit_run(int ch, UINT32 cur_time)
1.1       root     16614: {
1.1.1.8   root     16615:        if(pit[ch].expired_time != 0 && cur_time >= pit[ch].expired_time) {
1.1       root     16616:                pit[ch].count = PIT_COUNT_VALUE(ch);
1.1.1.8   root     16617:                pit[ch].prev_time = pit[ch].expired_time;
                   16618:                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
                   16619:                if(cur_time >= pit[ch].expired_time) {
                   16620:                        pit[ch].prev_time = cur_time;
                   16621:                        pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     16622:                }
1.1.1.8   root     16623:                return(1);
1.1       root     16624:        }
1.1.1.8   root     16625:        return(0);
1.1       root     16626: }
                   16627: 
                   16628: void pit_latch_count(int ch)
                   16629: {
1.1.1.8   root     16630:        if(pit[ch].expired_time != 0) {
1.1.1.26  root     16631:                UINT32 cur_time = timeGetTime();
1.1.1.8   root     16632:                pit_run(ch, cur_time);
                   16633:                UINT32 tmp = (pit[ch].count * (pit[ch].expired_time - cur_time)) / (pit[ch].expired_time - pit[ch].prev_time);
1.1.1.26  root     16634:                UINT16 latch = (tmp != 0) ? (UINT16)tmp : 1;
                   16635:                
                   16636:                if(pit[ch].prev_latch == latch && pit[ch].expired_time > cur_time) {
                   16637:                        // decrement counter in 1msec period
                   16638:                        if(pit[ch].next_latch == 0) {
                   16639:                                tmp = (pit[ch].count * (pit[ch].expired_time - cur_time - 1)) / (pit[ch].expired_time - pit[ch].prev_time);
                   16640:                                pit[ch].next_latch = (tmp != 0) ? (UINT16)tmp : 1;
                   16641:                        }
                   16642:                        if(pit[ch].latch > pit[ch].next_latch) {
                   16643:                                pit[ch].latch--;
                   16644:                        }
                   16645:                } else {
                   16646:                        pit[ch].prev_latch = pit[ch].latch = latch;
                   16647:                        pit[ch].next_latch = 0;
                   16648:                }
1.1.1.8   root     16649:        } else {
                   16650:                pit[ch].latch = (UINT16)pit[ch].count;
1.1.1.26  root     16651:                pit[ch].prev_latch = pit[ch].next_latch = 0;
1.1       root     16652:        }
                   16653:        pit[ch].count_latched = 1;
                   16654:        if((pit[ch].ctrl_reg & 0x30) == 0x10) {
                   16655:                // lower byte
                   16656:                pit[ch].low_read = 1;
                   16657:                pit[ch].high_read = 0;
                   16658:        } else if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   16659:                // upper byte
                   16660:                pit[ch].low_read = 0;
                   16661:                pit[ch].high_read = 1;
                   16662:        } else {
                   16663:                // lower -> upper
1.1.1.14  root     16664:                pit[ch].low_read = pit[ch].high_read = 1;
1.1       root     16665:        }
                   16666: }
                   16667: 
1.1.1.8   root     16668: int pit_get_expired_time(int ch)
1.1       root     16669: {
1.1.1.22  root     16670:        pit[ch].accum += 1024ULL * 1000ULL * (UINT64)pit[ch].count / PIT_FREQ;
                   16671:        UINT64 val = pit[ch].accum >> 10;
                   16672:        pit[ch].accum -= val << 10;
                   16673:        return((val != 0) ? val : 1);
1.1.1.8   root     16674: }
                   16675: 
1.1.1.25  root     16676: // sio
                   16677: 
                   16678: void sio_init()
                   16679: {
1.1.1.26  root     16680:        memset(sio, 0, sizeof(sio));
                   16681:        memset(sio_mt, 0, sizeof(sio_mt));
                   16682:        
1.1.1.29  root     16683:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     16684:                sio[c].send_buffer = new FIFO(SIO_BUFFER_SIZE);
                   16685:                sio[c].recv_buffer = new FIFO(SIO_BUFFER_SIZE);
                   16686:                
                   16687:                sio[c].divisor.w = 12;          // 115200Hz / 9600Baud
                   16688:                sio[c].line_ctrl = 0x03;        // 8bit, stop 1bit, non parity
1.1.1.26  root     16689:                sio[c].modem_ctrl = 0x03;       // rts=on, dtr=on
                   16690:                sio[c].set_rts = sio[c].set_dtr = true;
1.1.1.25  root     16691:                sio[c].line_stat_buf = 0x60;    // send/recv buffers are empty
                   16692:                sio[c].irq_identify = 0x01;     // no pending irq
                   16693:                
                   16694:                InitializeCriticalSection(&sio_mt[c].csSendData);
                   16695:                InitializeCriticalSection(&sio_mt[c].csRecvData);
                   16696:                InitializeCriticalSection(&sio_mt[c].csLineCtrl);
                   16697:                InitializeCriticalSection(&sio_mt[c].csLineStat);
                   16698:                InitializeCriticalSection(&sio_mt[c].csModemCtrl);
                   16699:                InitializeCriticalSection(&sio_mt[c].csModemStat);
                   16700:                
1.1.1.26  root     16701:                if(sio_port_number[c] != 0) {
1.1.1.25  root     16702:                        sio[c].channel = c;
                   16703:                        sio_mt[c].hThread = CreateThread(NULL, 0, sio_thread, &sio[c], 0, NULL);
                   16704:                }
                   16705:        }
                   16706: }
                   16707: 
                   16708: void sio_finish()
                   16709: {
1.1.1.29  root     16710:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     16711:                if(sio_mt[c].hThread != NULL) {
                   16712:                        WaitForSingleObject(sio_mt[c].hThread, INFINITE);
                   16713:                        CloseHandle(sio_mt[c].hThread);
1.1.1.28  root     16714:                        sio_mt[c].hThread = NULL;
1.1.1.25  root     16715:                }
                   16716:                DeleteCriticalSection(&sio_mt[c].csSendData);
                   16717:                DeleteCriticalSection(&sio_mt[c].csRecvData);
                   16718:                DeleteCriticalSection(&sio_mt[c].csLineCtrl);
                   16719:                DeleteCriticalSection(&sio_mt[c].csLineStat);
                   16720:                DeleteCriticalSection(&sio_mt[c].csModemCtrl);
                   16721:                DeleteCriticalSection(&sio_mt[c].csModemStat);
1.1.1.28  root     16722:        }
                   16723:        sio_release();
                   16724: }
                   16725: 
                   16726: void sio_release()
                   16727: {
1.1.1.29  root     16728:        for(int c = 0; c < 4; c++) {
1.1.1.28  root     16729:                // sio_thread() may access the resources :-(
1.1.1.32  root     16730:                bool running = (sio_mt[c].hThread != NULL);
                   16731:                
                   16732:                if(running) {
                   16733:                        EnterCriticalSection(&sio_mt[c].csSendData);
                   16734:                }
                   16735:                if(sio[c].send_buffer != NULL) {
                   16736:                        sio[c].send_buffer->release();
                   16737:                        delete sio[c].send_buffer;
                   16738:                        sio[c].send_buffer = NULL;
                   16739:                }
                   16740:                if(running) {
                   16741:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   16742:                        EnterCriticalSection(&sio_mt[c].csRecvData);
                   16743:                }
                   16744:                if(sio[c].recv_buffer != NULL) {
                   16745:                        sio[c].recv_buffer->release();
                   16746:                        delete sio[c].recv_buffer;
                   16747:                        sio[c].recv_buffer = NULL;
                   16748:                }
                   16749:                if(running) {
                   16750:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
1.1.1.28  root     16751:                }
1.1.1.25  root     16752:        }
                   16753: }
                   16754: 
                   16755: void sio_write(int c, UINT32 addr, UINT8 data)
                   16756: {
                   16757:        switch(addr & 7) {
                   16758:        case 0:
                   16759:                if(sio[c].selector & 0x80) {
                   16760:                        if(sio[c].divisor.b.l != data) {
                   16761:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   16762:                                sio[c].divisor.b.l = data;
                   16763:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   16764:                        }
                   16765:                } else {
                   16766:                        EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     16767:                        if(sio[c].send_buffer != NULL) {
                   16768:                                sio[c].send_buffer->write(data);
                   16769:                        }
1.1.1.25  root     16770:                        // transmitter holding/shift registers are not empty
                   16771:                        sio[c].line_stat_buf &= ~0x60;
                   16772:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   16773:                        
                   16774:                        if(sio[c].irq_enable & 0x02) {
                   16775:                                sio_update_irq(c);
                   16776:                        }
                   16777:                }
                   16778:                break;
                   16779:        case 1:
                   16780:                if(sio[c].selector & 0x80) {
                   16781:                        if(sio[c].divisor.b.h != data) {
                   16782:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   16783:                                sio[c].divisor.b.h = data;
                   16784:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   16785:                        }
                   16786:                } else {
                   16787:                        if(sio[c].irq_enable != data) {
                   16788:                                sio[c].irq_enable = data;
                   16789:                                sio_update_irq(c);
                   16790:                        }
                   16791:                }
                   16792:                break;
                   16793:        case 3:
                   16794:                {
                   16795:                        UINT8 line_ctrl = data & 0x3f;
                   16796:                        bool set_brk = ((data & 0x40) != 0);
                   16797:                        
                   16798:                        if(sio[c].line_ctrl != line_ctrl) {
                   16799:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   16800:                                sio[c].line_ctrl = line_ctrl;
                   16801:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   16802:                        }
                   16803:                        if(sio[c].set_brk != set_brk) {
                   16804:                                EnterCriticalSection(&sio_mt[c].csModemCtrl);
                   16805:                                sio[c].set_brk = set_brk;
                   16806:                                LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   16807:                        }
                   16808:                }
                   16809:                sio[c].selector = data;
                   16810:                break;
                   16811:        case 4:
                   16812:                {
                   16813:                        bool set_dtr = ((data & 0x01) != 0);
                   16814:                        bool set_rts = ((data & 0x02) != 0);
                   16815:                        
                   16816:                        if(sio[c].set_dtr != set_dtr || sio[c].set_rts != set_rts) {
1.1.1.26  root     16817: //                             EnterCriticalSection(&sio_mt[c].csModemCtrl);
1.1.1.25  root     16818:                                sio[c].set_dtr = set_dtr;
                   16819:                                sio[c].set_rts = set_rts;
1.1.1.26  root     16820: //                             LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   16821:                                
                   16822:                                bool state_changed = false;
                   16823:                                
                   16824:                                EnterCriticalSection(&sio_mt[c].csModemStat);
                   16825:                                if(set_dtr) {
                   16826:                                        sio[c].modem_stat |= 0x20;      // dsr on
                   16827:                                } else {
                   16828:                                        sio[c].modem_stat &= ~0x20;     // dsr off
                   16829:                                }
                   16830:                                if(set_rts) {
                   16831:                                        sio[c].modem_stat |= 0x10;      // cts on
                   16832:                                } else {
                   16833:                                        sio[c].modem_stat &= ~0x10;     // cts off
                   16834:                                }
                   16835:                                if((sio[c].prev_modem_stat & 0x20) != (sio[c].modem_stat & 0x20)) {
                   16836:                                        if(!(sio[c].modem_stat & 0x02)) {
                   16837:                                                if(sio[c].irq_enable & 0x08) {
                   16838:                                                        state_changed = true;
                   16839:                                                }
                   16840:                                                sio[c].modem_stat |= 0x02;
                   16841:                                        }
                   16842:                                }
                   16843:                                if((sio[c].prev_modem_stat & 0x10) != (sio[c].modem_stat & 0x10)) {
                   16844:                                        if(!(sio[c].modem_stat & 0x01)) {
                   16845:                                                if(sio[c].irq_enable & 0x08) {
                   16846:                                                        state_changed = true;
                   16847:                                                }
                   16848:                                                sio[c].modem_stat |= 0x01;
                   16849:                                        }
                   16850:                                }
                   16851:                                LeaveCriticalSection(&sio_mt[c].csModemStat);
                   16852:                                
                   16853:                                if(state_changed) {
                   16854:                                        sio_update_irq(c);
                   16855:                                }
1.1.1.25  root     16856:                        }
                   16857:                }
                   16858:                sio[c].modem_ctrl = data;
                   16859:                break;
                   16860:        case 7:
                   16861:                sio[c].scratch = data;
                   16862:                break;
                   16863:        }
                   16864: }
                   16865: 
                   16866: UINT8 sio_read(int c, UINT32 addr)
                   16867: {
                   16868:        switch(addr & 7) {
                   16869:        case 0:
                   16870:                if(sio[c].selector & 0x80) {
                   16871:                        return(sio[c].divisor.b.l);
                   16872:                } else {
                   16873:                        EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     16874:                        UINT8 data = 0;
                   16875:                        if(sio[c].recv_buffer != NULL) {
                   16876:                                data = sio[c].recv_buffer->read();
                   16877:                        }
1.1.1.25  root     16878:                        // data is not ready
                   16879:                        sio[c].line_stat_buf &= ~0x01;
                   16880:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
                   16881:                        
                   16882:                        if(sio[c].irq_enable & 0x01) {
                   16883:                                sio_update_irq(c);
                   16884:                        }
                   16885:                        return(data);
                   16886:                }
                   16887:        case 1:
                   16888:                if(sio[c].selector & 0x80) {
                   16889:                        return(sio[c].divisor.b.h);
                   16890:                } else {
                   16891:                        return(sio[c].irq_enable);
                   16892:                }
                   16893:        case 2:
                   16894:                return(sio[c].irq_identify);
                   16895:        case 3:
                   16896:                return(sio[c].selector);
                   16897:        case 4:
                   16898:                return(sio[c].modem_ctrl);
                   16899:        case 5:
                   16900:                {
                   16901:                        EnterCriticalSection(&sio_mt[c].csLineStat);
                   16902:                        UINT8 val = sio[c].line_stat_err | sio[c].line_stat_buf;
                   16903:                        sio[c].line_stat_err = 0x00;
                   16904:                        LeaveCriticalSection(&sio_mt[c].csLineStat);
                   16905:                        
                   16906:                        bool state_changed = false;
                   16907:                        
                   16908:                        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   16909:                                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     16910:                                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     16911:                                        // transmitter holding register will be empty first
                   16912:                                        if(sio[c].irq_enable & 0x02) {
                   16913:                                                state_changed = true;
                   16914:                                        }
                   16915:                                        sio[c].line_stat_buf |= 0x20;
                   16916:                                }
                   16917:                                LeaveCriticalSection(&sio_mt[c].csSendData);
                   16918:                        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   16919:                                // transmitter shift register will be empty later
                   16920:                                sio[c].line_stat_buf |= 0x40;
                   16921:                        }
                   16922:                        if(!(sio[c].line_stat_buf & 0x01)) {
                   16923:                                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     16924:                                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     16925:                                        // data is ready
                   16926:                                        if(sio[c].irq_enable & 0x01) {
                   16927:                                                state_changed = true;
                   16928:                                        }
                   16929:                                        sio[c].line_stat_buf |= 0x01;
                   16930:                                }
                   16931:                                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   16932:                        }
                   16933:                        if(state_changed) {
                   16934:                                sio_update_irq(c);
                   16935:                        }
                   16936:                        return(val);
                   16937:                }
                   16938:        case 6:
                   16939:                {
                   16940:                        EnterCriticalSection(&sio_mt[c].csModemStat);
                   16941:                        UINT8 val = sio[c].modem_stat;
                   16942:                        sio[c].modem_stat &= 0xf0;
                   16943:                        sio[c].prev_modem_stat = sio[c].modem_stat;
                   16944:                        LeaveCriticalSection(&sio_mt[c].csModemStat);
                   16945:                        
                   16946:                        if(sio[c].modem_ctrl & 0x10) {
                   16947:                                // loop-back
                   16948:                                val &= 0x0f;
                   16949:                                val |= (sio[c].modem_ctrl & 0x0c) << 4;
                   16950:                                val |= (sio[c].modem_ctrl & 0x01) << 5;
                   16951:                                val |= (sio[c].modem_ctrl & 0x02) << 3;
                   16952:                        }
                   16953:                        return(val);
                   16954:                }
                   16955:        case 7:
                   16956:                return(sio[c].scratch);
                   16957:        }
                   16958:        return(0xff);
                   16959: }
                   16960: 
                   16961: void sio_update(int c)
                   16962: {
                   16963:        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   16964:                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     16965:                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     16966:                        // transmitter holding/shift registers will be empty
                   16967:                        sio[c].line_stat_buf |= 0x60;
                   16968:                }
                   16969:                LeaveCriticalSection(&sio_mt[c].csSendData);
                   16970:        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   16971:                // transmitter shift register will be empty
                   16972:                sio[c].line_stat_buf |= 0x40;
                   16973:        }
                   16974:        if(!(sio[c].line_stat_buf & 0x01)) {
                   16975:                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     16976:                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     16977:                        // data is ready
                   16978:                        sio[c].line_stat_buf |= 0x01;
                   16979:                }
                   16980:                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   16981:        }
                   16982:        sio_update_irq(c);
                   16983: }
                   16984: 
                   16985: void sio_update_irq(int c)
                   16986: {
                   16987:        int level = -1;
                   16988:        
                   16989:        if(sio[c].irq_enable & 0x08) {
                   16990:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   16991:                if((sio[c].modem_stat & 0x0f) != 0) {
                   16992:                        level = 0;
                   16993:                }
                   16994:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   16995:        }
                   16996:        if(sio[c].irq_enable & 0x02) {
                   16997:                if(sio[c].line_stat_buf & 0x20) {
                   16998:                        level = 1;
                   16999:                }
                   17000:        }
                   17001:        if(sio[c].irq_enable & 0x01) {
                   17002:                if(sio[c].line_stat_buf & 0x01) {
                   17003:                        level = 2;
                   17004:                }
                   17005:        }
                   17006:        if(sio[c].irq_enable & 0x04) {
                   17007:                EnterCriticalSection(&sio_mt[c].csLineStat);
                   17008:                if(sio[c].line_stat_err != 0) {
                   17009:                        level = 3;
                   17010:                }
                   17011:                LeaveCriticalSection(&sio_mt[c].csLineStat);
                   17012:        }
1.1.1.29  root     17013:        
                   17014:        // COM1 and COM3 shares IRQ4, COM2 and COM4 shares IRQ3
1.1.1.25  root     17015:        if(level != -1) {
                   17016:                sio[c].irq_identify = level << 1;
1.1.1.29  root     17017:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 1);
1.1.1.25  root     17018:        } else {
                   17019:                sio[c].irq_identify = 1;
1.1.1.29  root     17020:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 0);
1.1.1.25  root     17021:        }
                   17022: }
                   17023: 
                   17024: DWORD WINAPI sio_thread(void *lpx)
                   17025: {
                   17026:        volatile sio_t *p = (sio_t *)lpx;
                   17027:        sio_mt_t *q = &sio_mt[p->channel];
                   17028:        
                   17029:        char name[] = "COM1";
1.1.1.26  root     17030:        name[3] = '0' + sio_port_number[p->channel];
                   17031:        HANDLE hComm = NULL;
                   17032:        COMMPROP commProp;
                   17033:        DCB dcb;
                   17034:        DWORD dwSettableBaud = 0xffb; // 75, 110, 150, 300, 600, 1200, 1800, 2400, 4800, 7200, and 9600bps
                   17035:        BYTE bytBuffer[SIO_BUFFER_SIZE];
                   17036:        
                   17037:        if((hComm = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL)) != INVALID_HANDLE_VALUE) {
                   17038:                if(GetCommProperties(hComm, &commProp)) {
                   17039:                        dwSettableBaud = commProp.dwSettableBaud;
                   17040:                }
1.1.1.25  root     17041:                EscapeCommFunction(hComm, CLRBREAK);
1.1.1.26  root     17042: //             EscapeCommFunction(hComm, SETRTS);
                   17043: //             EscapeCommFunction(hComm, SETDTR);
1.1.1.25  root     17044:                
                   17045:                while(!m_halted) {
                   17046:                        // setup comm port
                   17047:                        bool comm_state_changed = false;
                   17048:                        
                   17049:                        EnterCriticalSection(&q->csLineCtrl);
                   17050:                        if((p->prev_divisor != p->divisor.w || p->prev_line_ctrl != p->line_ctrl) && p->divisor.w != 0) {
                   17051:                                p->prev_divisor = p->divisor.w;
                   17052:                                p->prev_line_ctrl = p->line_ctrl;
                   17053:                                comm_state_changed = true;
                   17054:                        }
                   17055:                        LeaveCriticalSection(&q->csLineCtrl);
                   17056:                        
                   17057:                        if(comm_state_changed) {
1.1.1.26  root     17058:                                if(GetCommState(hComm, &dcb)) {
                   17059: //                                     dcb.BaudRate = min(9600, 115200 / p->prev_divisor);
                   17060:                                        DWORD baud = 115200 / p->prev_divisor;
                   17061:                                        dcb.BaudRate = 9600; // default
                   17062:                                        
                   17063:                                        if((dwSettableBaud & BAUD_075  ) && baud >= 75   ) dcb.BaudRate = 75;
                   17064:                                        if((dwSettableBaud & BAUD_110  ) && baud >= 110  ) dcb.BaudRate = 110;
                   17065:                                        // 134.5bps is not supported ???
                   17066: //                                     if((dwSettableBaud & BAUD_134_5) && baud >= 134.5) dcb.BaudRate = 134.5;
                   17067:                                        if((dwSettableBaud & BAUD_150  ) && baud >= 150  ) dcb.BaudRate = 150;
                   17068:                                        if((dwSettableBaud & BAUD_300  ) && baud >= 300  ) dcb.BaudRate = 300;
                   17069:                                        if((dwSettableBaud & BAUD_600  ) && baud >= 600  ) dcb.BaudRate = 600;
                   17070:                                        if((dwSettableBaud & BAUD_1200 ) && baud >= 1200 ) dcb.BaudRate = 1200;
                   17071:                                        if((dwSettableBaud & BAUD_1800 ) && baud >= 1800 ) dcb.BaudRate = 1800;
                   17072:                                        if((dwSettableBaud & BAUD_2400 ) && baud >= 2400 ) dcb.BaudRate = 2400;
                   17073:                                        if((dwSettableBaud & BAUD_4800 ) && baud >= 4800 ) dcb.BaudRate = 4800;
                   17074:                                        if((dwSettableBaud & BAUD_7200 ) && baud >= 7200 ) dcb.BaudRate = 7200;
                   17075:                                        if((dwSettableBaud & BAUD_9600 ) && baud >= 9600 ) dcb.BaudRate = 9600;
                   17076: //                                     if((dwSettableBaud & BAUD_14400) && baud >= 14400) dcb.BaudRate = 14400;
                   17077: //                                     if((dwSettableBaud & BAUD_19200) && baud >= 19200) dcb.BaudRate = 19200;
                   17078: //                                     if((dwSettableBaud & BAUD_38400) && baud >= 38400) dcb.BaudRate = 38400;
                   17079:                                        
                   17080:                                        switch(p->prev_line_ctrl & 0x03) {
                   17081:                                        case 0x00: dcb.ByteSize = 5; break;
                   17082:                                        case 0x01: dcb.ByteSize = 6; break;
                   17083:                                        case 0x02: dcb.ByteSize = 7; break;
                   17084:                                        case 0x03: dcb.ByteSize = 8; break;
                   17085:                                        }
                   17086:                                        switch(p->prev_line_ctrl & 0x04) {
                   17087:                                        case 0x00: dcb.StopBits = ONESTOPBIT; break;
                   17088:                                        case 0x04: dcb.StopBits = (dcb.ByteSize == 5) ? ONE5STOPBITS : TWOSTOPBITS; break;
                   17089:                                        }
                   17090:                                        switch(p->prev_line_ctrl & 0x38) {
                   17091:                                        case 0x08: dcb.Parity = ODDPARITY;   break;
                   17092:                                        case 0x18: dcb.Parity = EVENPARITY;  break;
                   17093:                                        case 0x28: dcb.Parity = MARKPARITY;  break;
                   17094:                                        case 0x38: dcb.Parity = SPACEPARITY; break;
                   17095:                                        default:   dcb.Parity = NOPARITY;    break;
                   17096:                                        }
                   17097:                                        dcb.fBinary = TRUE;
                   17098:                                        dcb.fParity = (dcb.Parity != NOPARITY);
                   17099:                                        dcb.fOutxCtsFlow = dcb.fOutxDsrFlow = TRUE;
                   17100:                                        dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
                   17101:                                        dcb.fDsrSensitivity = FALSE;//TRUE;
                   17102:                                        dcb.fTXContinueOnXoff = TRUE;
                   17103:                                        dcb.fOutX = dcb.fInX = FALSE;
                   17104:                                        dcb.fErrorChar = FALSE;
                   17105:                                        dcb.fNull = FALSE;
                   17106:                                        dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
                   17107:                                        dcb.fAbortOnError = FALSE;
                   17108:                                        
                   17109:                                        SetCommState(hComm, &dcb);
1.1.1.25  root     17110:                                }
                   17111:                                
                   17112:                                // check again to apply all comm state changes
                   17113:                                Sleep(10);
                   17114:                                continue;
                   17115:                        }
                   17116:                        
                   17117:                        // set comm pins
                   17118:                        bool change_brk = false;
1.1.1.26  root     17119: //                     bool change_rts = false;
                   17120: //                     bool change_dtr = false;
1.1.1.25  root     17121:                        
                   17122:                        EnterCriticalSection(&q->csModemCtrl);
                   17123:                        if(p->prev_set_brk != p->set_brk) {
                   17124:                                p->prev_set_brk = p->set_brk;
                   17125:                                change_brk = true;
                   17126:                        }
1.1.1.26  root     17127: //                     if(p->prev_set_rts != p->set_rts) {
                   17128: //                             p->prev_set_rts = p->set_rts;
                   17129: //                             change_rts = true;
                   17130: //                     }
                   17131: //                     if(p->prev_set_dtr != p->set_dtr) {
                   17132: //                             p->prev_set_dtr = p->set_dtr;
                   17133: //                             change_dtr = true;
                   17134: //                     }
1.1.1.25  root     17135:                        LeaveCriticalSection(&q->csModemCtrl);
                   17136:                        
                   17137:                        if(change_brk) {
1.1.1.26  root     17138:                                static UINT32 clear_time = 0;
                   17139:                                if(p->prev_set_brk) {
                   17140:                                        EscapeCommFunction(hComm, SETBREAK);
                   17141:                                        clear_time = timeGetTime() + 200;
                   17142:                                } else {
                   17143:                                        // keep break for at least 200msec
                   17144:                                        UINT32 cur_time = timeGetTime();
                   17145:                                        if(clear_time > cur_time) {
                   17146:                                                Sleep(clear_time - cur_time);
                   17147:                                        }
                   17148:                                        EscapeCommFunction(hComm, CLRBREAK);
                   17149:                                }
1.1.1.25  root     17150:                        }
1.1.1.26  root     17151: //                     if(change_rts) {
                   17152: //                             if(p->prev_set_rts) {
                   17153: //                                     EscapeCommFunction(hComm, SETRTS);
                   17154: //                             } else {
                   17155: //                                     EscapeCommFunction(hComm, CLRRTS);
                   17156: //                             }
                   17157: //                     }
                   17158: //                     if(change_dtr) {
                   17159: //                             if(p->prev_set_dtr) {
                   17160: //                                     EscapeCommFunction(hComm, SETDTR);
                   17161: //                             } else {
                   17162: //                                     EscapeCommFunction(hComm, CLRDTR);
                   17163: //                             }
                   17164: //                     }
1.1.1.25  root     17165:                        
                   17166:                        // get comm pins
                   17167:                        DWORD dwModemStat = 0;
                   17168:                        
                   17169:                        if(GetCommModemStatus(hComm, &dwModemStat)) {
                   17170:                                EnterCriticalSection(&q->csModemStat);
                   17171:                                if(dwModemStat & MS_RLSD_ON) {
                   17172:                                        p->modem_stat |= 0x80;
                   17173:                                } else {
                   17174:                                        p->modem_stat &= ~0x80;
                   17175:                                }
                   17176:                                if(dwModemStat & MS_RING_ON) {
                   17177:                                        p->modem_stat |= 0x40;
                   17178:                                } else {
                   17179:                                        p->modem_stat &= ~0x40;
                   17180:                                }
1.1.1.26  root     17181: //                             if(dwModemStat & MS_DSR_ON) {
                   17182: //                                     p->modem_stat |= 0x20;
                   17183: //                             } else {
                   17184: //                                     p->modem_stat &= ~0x20;
                   17185: //                             }
                   17186: //                             if(dwModemStat & MS_CTS_ON) {
                   17187: //                                     p->modem_stat |= 0x10;
                   17188: //                             } else {
                   17189: //                                     p->modem_stat &= ~0x10;
                   17190: //                             }
1.1.1.25  root     17191:                                if((p->prev_modem_stat & 0x80) != (p->modem_stat & 0x80)) {
                   17192:                                        p->modem_stat |= 0x08;
                   17193:                                }
                   17194:                                if((p->prev_modem_stat & 0x40) && !(p->modem_stat & 0x40)) {
                   17195:                                        p->modem_stat |= 0x04;
                   17196:                                }
1.1.1.26  root     17197: //                             if((p->prev_modem_stat & 0x20) != (p->modem_stat & 0x20)) {
                   17198: //                                     p->modem_stat |= 0x02;
                   17199: //                             }
                   17200: //                             if((p->prev_modem_stat & 0x10) != (p->modem_stat & 0x10)) {
                   17201: //                                     p->modem_stat |= 0x01;
                   17202: //                             }
1.1.1.25  root     17203:                                LeaveCriticalSection(&q->csModemStat);
                   17204:                        }
                   17205:                        
                   17206:                        // send data
                   17207:                        DWORD dwSend = 0;
                   17208:                        
                   17209:                        EnterCriticalSection(&q->csSendData);
1.1.1.32  root     17210:                        while(p->send_buffer != NULL && !p->send_buffer->empty()) {
1.1.1.25  root     17211:                                bytBuffer[dwSend++] = p->send_buffer->read();
                   17212:                        }
                   17213:                        LeaveCriticalSection(&q->csSendData);
                   17214:                        
                   17215:                        if(dwSend != 0) {
                   17216:                                DWORD dwWritten = 0;
                   17217:                                WriteFile(hComm, bytBuffer, dwSend, &dwWritten, NULL);
                   17218:                        }
                   17219:                        
                   17220:                        // get line status and recv data
                   17221:                        DWORD dwLineStat = 0;
                   17222:                        COMSTAT comStat;
                   17223:                        
                   17224:                        if(ClearCommError(hComm, &dwLineStat, &comStat)) {
                   17225:                                EnterCriticalSection(&q->csLineStat);
                   17226:                                if(dwLineStat & CE_BREAK) {
                   17227:                                        p->line_stat_err |= 0x10;
                   17228:                                }
                   17229:                                if(dwLineStat & CE_FRAME) {
                   17230:                                        p->line_stat_err |= 0x08;
                   17231:                                }
                   17232:                                if(dwLineStat & CE_RXPARITY) {
                   17233:                                        p->line_stat_err |= 0x04;
                   17234:                                }
                   17235:                                if(dwLineStat & CE_OVERRUN) {
                   17236:                                        p->line_stat_err |= 0x02;
                   17237:                                }
                   17238:                                LeaveCriticalSection(&q->csLineStat);
                   17239:                                
                   17240:                                if(comStat.cbInQue != 0) {
                   17241:                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     17242:                                        DWORD dwRecv = 0;
                   17243:                                        if(p->recv_buffer != NULL) {
                   17244:                                                dwRecv = min(comStat.cbInQue, p->recv_buffer->remain());
                   17245:                                        }
1.1.1.25  root     17246:                                        LeaveCriticalSection(&q->csRecvData);
                   17247:                                        
                   17248:                                        if(dwRecv != 0) {
                   17249:                                                DWORD dwRead = 0;
                   17250:                                                if(ReadFile(hComm, bytBuffer, dwRecv, &dwRead, NULL) && dwRead != 0) {
                   17251:                                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     17252:                                                        if(p->recv_buffer != NULL) {
                   17253:                                                                for(int i = 0; i < dwRead; i++) {
                   17254:                                                                        p->recv_buffer->write(bytBuffer[i]);
                   17255:                                                                }
1.1.1.25  root     17256:                                                        }
                   17257:                                                        LeaveCriticalSection(&q->csRecvData);
                   17258:                                                }
                   17259:                                        }
                   17260:                                }
                   17261:                        }
                   17262:                        Sleep(10);
                   17263:                }
                   17264:                CloseHandle(hComm);
                   17265:        }
                   17266:        return 0;
                   17267: }
                   17268: 
1.1.1.8   root     17269: // cmos
                   17270: 
                   17271: void cmos_init()
                   17272: {
                   17273:        memset(cmos, 0, sizeof(cmos));
                   17274:        cmos_addr = 0;
1.1       root     17275:        
1.1.1.8   root     17276:        // from DOSBox
                   17277:        cmos_write(0x0a, 0x26);
                   17278:        cmos_write(0x0b, 0x02);
                   17279:        cmos_write(0x0d, 0x80);
1.1       root     17280: }
                   17281: 
1.1.1.8   root     17282: void cmos_write(int addr, UINT8 val)
1.1       root     17283: {
1.1.1.8   root     17284:        cmos[addr & 0x7f] = val;
                   17285: }
                   17286: 
                   17287: #define CMOS_GET_TIME() { \
                   17288:        UINT32 cur_sec = timeGetTime() / 1000 ; \
                   17289:        if(prev_sec != cur_sec) { \
                   17290:                GetLocalTime(&time); \
                   17291:                prev_sec = cur_sec; \
                   17292:        } \
1.1       root     17293: }
1.1.1.8   root     17294: #define CMOS_BCD(v) ((cmos[0x0b] & 4) ? (v) : to_bcd(v))
1.1       root     17295: 
1.1.1.8   root     17296: UINT8 cmos_read(int addr)
1.1       root     17297: {
1.1.1.8   root     17298:        static SYSTEMTIME time;
                   17299:        static UINT32 prev_sec = 0;
1.1       root     17300:        
1.1.1.8   root     17301:        switch(addr & 0x7f) {
                   17302:        case 0x00: CMOS_GET_TIME(); return(CMOS_BCD(time.wSecond));
                   17303:        case 0x02: CMOS_GET_TIME(); return(CMOS_BCD(time.wMinute));
                   17304:        case 0x04: CMOS_GET_TIME(); return(CMOS_BCD(time.wHour));
                   17305:        case 0x06: CMOS_GET_TIME(); return(time.wDayOfWeek + 1);
                   17306:        case 0x07: CMOS_GET_TIME(); return(CMOS_BCD(time.wDay));
                   17307:        case 0x08: CMOS_GET_TIME(); return(CMOS_BCD(time.wMonth));
                   17308:        case 0x09: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear));
                   17309: //     case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 2 ? 0x80 : 0));       // 2msec
                   17310:        case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 20 ? 0x80 : 0));      // precision of timeGetTime() may not be 1msec
                   17311:        case 0x15: return((MEMORY_END >> 10) & 0xff);
                   17312:        case 0x16: return((MEMORY_END >> 18) & 0xff);
                   17313:        case 0x17: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   17314:        case 0x18: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   17315:        case 0x30: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   17316:        case 0x31: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   17317:        case 0x32: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear / 100));
1.1       root     17318:        }
1.1.1.8   root     17319:        return(cmos[addr & 0x7f]);
1.1       root     17320: }
                   17321: 
1.1.1.7   root     17322: // kbd (a20)
                   17323: 
                   17324: void kbd_init()
                   17325: {
1.1.1.8   root     17326:        kbd_data = kbd_command = 0;
1.1.1.7   root     17327:        kbd_status = 0x18;
                   17328: }
                   17329: 
                   17330: UINT8 kbd_read_data()
                   17331: {
1.1.1.8   root     17332:        kbd_status &= ~1;
1.1.1.7   root     17333:        return(kbd_data);
                   17334: }
                   17335: 
                   17336: void kbd_write_data(UINT8 val)
                   17337: {
                   17338:        switch(kbd_command) {
                   17339:        case 0xd1:
                   17340:                i386_set_a20_line((val >> 1) & 1);
                   17341:                break;
                   17342:        }
                   17343:        kbd_command = 0;
1.1.1.8   root     17344:        kbd_status &= ~8;
1.1.1.7   root     17345: }
                   17346: 
                   17347: UINT8 kbd_read_status()
                   17348: {
                   17349:        return(kbd_status);
                   17350: }
                   17351: 
                   17352: void kbd_write_command(UINT8 val)
                   17353: {
                   17354:        switch(val) {
                   17355:        case 0xd0:
                   17356:                kbd_data = ((m_a20_mask >> 19) & 2) | 1;
1.1.1.8   root     17357:                kbd_status |= 1;
1.1.1.7   root     17358:                break;
                   17359:        case 0xdd:
                   17360:                i386_set_a20_line(0);
                   17361:                break;
                   17362:        case 0xdf:
                   17363:                i386_set_a20_line(1);
                   17364:                break;
1.1.1.26  root     17365:        case 0xf0: case 0xf1: case 0xf2: case 0xf3: case 0xf4: case 0xf5: case 0xf6: case 0xf7:
                   17366:        case 0xf8: case 0xf9: case 0xfa: case 0xfb: case 0xfc: case 0xfd: case 0xfe: case 0xff:
1.1.1.7   root     17367:                if(!(val & 1)) {
1.1.1.8   root     17368:                        if((cmos[0x0f] & 0x7f) == 5) {
1.1.1.7   root     17369:                                // reset pic
                   17370:                                pic_init();
                   17371:                                pic[0].irr = pic[1].irr = 0x00;
                   17372:                                pic[0].imr = pic[1].imr = 0xff;
                   17373:                        }
                   17374:                        CPU_RESET_CALL(CPU_MODEL);
                   17375:                        i386_jmp_far(0x40, 0x67);
                   17376:                }
                   17377:                i386_set_a20_line((val >> 1) & 1);
                   17378:                break;
                   17379:        }
                   17380:        kbd_command = val;
1.1.1.8   root     17381:        kbd_status |= 8;
1.1.1.7   root     17382: }
                   17383: 
1.1.1.9   root     17384: // vga
                   17385: 
                   17386: UINT8 vga_read_status()
                   17387: {
                   17388:        // 60hz
                   17389:        static const int period[3] = {16, 17, 17};
                   17390:        static int index = 0;
                   17391:        UINT32 time = timeGetTime() % period[index];
                   17392:        
                   17393:        index = (index + 1) % 3;
1.1.1.14  root     17394:        return((time < 4 ? 0x08 : 0) | (time == 0 ? 0 : 0x01));
1.1.1.9   root     17395: }
                   17396: 
1.1       root     17397: // i/o bus
                   17398: 
1.1.1.29  root     17399: // this is ugly patch for SW1US.EXE, it sometimes mistakely read/write 01h-10h for serial I/O
                   17400: //#define SW1US_PATCH
                   17401: 
1.1.1.25  root     17402: UINT8 read_io_byte(offs_t addr)
1.1.1.33  root     17403: #ifdef USE_DEBUGGER
1.1.1.25  root     17404: {
1.1.1.33  root     17405:        if(now_debugging) {
                   17406:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   17407:                        if(in_break_point.table[i].status == 1) {
                   17408:                                if(addr == in_break_point.table[i].addr) {
                   17409:                                        in_break_point.hit = i + 1;
                   17410:                                        now_suspended = true;
                   17411:                                        break;
                   17412:                                }
                   17413:                        }
                   17414:                }
1.1.1.25  root     17415:        }
1.1.1.33  root     17416:        return(debugger_read_io_byte(addr));
1.1.1.25  root     17417: }
1.1.1.33  root     17418: UINT8 debugger_read_io_byte(offs_t addr)
1.1.1.25  root     17419: #endif
1.1       root     17420: {
1.1.1.33  root     17421:        UINT8 val = 0xff;
                   17422:        
1.1       root     17423:        switch(addr) {
1.1.1.29  root     17424: #ifdef SW1US_PATCH
                   17425:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   17426:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
1.1.1.33  root     17427:                val = sio_read(0, addr - 1);
                   17428:                break;
1.1.1.29  root     17429: #else
1.1.1.25  root     17430:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   17431:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
1.1.1.33  root     17432:                val = dma_read(0, addr);
                   17433:                break;
1.1.1.29  root     17434: #endif
1.1.1.25  root     17435:        case 0x20: case 0x21:
1.1.1.33  root     17436:                val = pic_read(0, addr);
                   17437:                break;
1.1.1.25  root     17438:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1.1.33  root     17439:                val = pit_read(addr & 0x03);
                   17440:                break;
1.1.1.7   root     17441:        case 0x60:
1.1.1.33  root     17442:                val = kbd_read_data();
                   17443:                break;
1.1.1.9   root     17444:        case 0x61:
1.1.1.33  root     17445:                val = system_port;
                   17446:                break;
1.1.1.7   root     17447:        case 0x64:
1.1.1.33  root     17448:                val = kbd_read_status();
                   17449:                break;
1.1       root     17450:        case 0x71:
1.1.1.33  root     17451:                val = cmos_read(cmos_addr);
                   17452:                break;
1.1.1.25  root     17453:        case 0x81:
1.1.1.33  root     17454:                val = dma_page_read(0, 2);
                   17455:                break;
1.1.1.25  root     17456:        case 0x82:
1.1.1.33  root     17457:                val = dma_page_read(0, 3);
                   17458:                break;
1.1.1.25  root     17459:        case 0x83:
1.1.1.33  root     17460:                val = dma_page_read(0, 1);
                   17461:                break;
1.1.1.25  root     17462:        case 0x87:
1.1.1.33  root     17463:                val = dma_page_read(0, 0);
                   17464:                break;
1.1.1.25  root     17465:        case 0x89:
1.1.1.33  root     17466:                val = dma_page_read(1, 2);
                   17467:                break;
1.1.1.25  root     17468:        case 0x8a:
1.1.1.33  root     17469:                val = dma_page_read(1, 3);
                   17470:                break;
1.1.1.25  root     17471:        case 0x8b:
1.1.1.33  root     17472:                val = dma_page_read(1, 1);
                   17473:                break;
1.1.1.25  root     17474:        case 0x8f:
1.1.1.33  root     17475:                val = dma_page_read(1, 0);
                   17476:                break;
1.1       root     17477:        case 0x92:
1.1.1.33  root     17478:                val = (m_a20_mask >> 19) & 2;
                   17479:                break;
1.1.1.25  root     17480:        case 0xa0: case 0xa1:
1.1.1.33  root     17481:                val = pic_read(1, addr);
                   17482:                break;
1.1.1.25  root     17483:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   17484:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.33  root     17485:                val = dma_read(1, (addr - 0xc0) >> 1);
                   17486:                break;
1.1.1.37! root     17487:        case 0x278: case 0x279: case 0x27a:
        !          17488:                val = pio_read(1, addr);
        !          17489:                break;
1.1.1.29  root     17490:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
1.1.1.33  root     17491:                val = sio_read(3, addr);
                   17492:                break;
1.1.1.25  root     17493:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
1.1.1.33  root     17494:                val = sio_read(1, addr);
                   17495:                break;
1.1.1.25  root     17496:        case 0x378: case 0x379: case 0x37a:
1.1.1.33  root     17497:                val = pio_read(0, addr);
                   17498:                break;
1.1.1.25  root     17499:        case 0x3ba: case 0x3da:
1.1.1.33  root     17500:                val = vga_read_status();
                   17501:                break;
1.1.1.37! root     17502:        case 0x3bc: case 0x3bd: case 0x3be:
        !          17503:                val = pio_read(2, addr);
        !          17504:                break;
1.1.1.29  root     17505:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
1.1.1.33  root     17506:                val = sio_read(2, addr);
                   17507:                break;
1.1.1.25  root     17508:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
1.1.1.33  root     17509:                val = sio_read(0, addr);
                   17510:                break;
1.1       root     17511:        default:
1.1.1.33  root     17512: //             fatalerror("unknown inb %4x\n", addr);
1.1       root     17513:                break;
                   17514:        }
1.1.1.33  root     17515: #ifdef ENABLE_DEBUG_IOPORT
                   17516:        if(fp_debug_log != NULL) {
                   17517:                fprintf(fp_debug_log, "inb %04X, %02X\n", addr, val);
                   17518:        }
                   17519: #endif
                   17520:        return(val);
1.1       root     17521: }
                   17522: 
                   17523: UINT16 read_io_word(offs_t addr)
                   17524: {
                   17525:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8));
                   17526: }
                   17527: 
1.1.1.33  root     17528: #ifdef USE_DEBUGGER
                   17529: UINT16 debugger_read_io_word(offs_t addr)
                   17530: {
                   17531:        return(debugger_read_io_byte(addr) | (debugger_read_io_byte(addr + 1) << 8));
                   17532: }
                   17533: #endif
                   17534: 
1.1       root     17535: UINT32 read_io_dword(offs_t addr)
                   17536: {
                   17537:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8) | (read_io_byte(addr + 2) << 16) | (read_io_byte(addr + 3) << 24));
                   17538: }
                   17539: 
1.1.1.33  root     17540: #ifdef USE_DEBUGGER
                   17541: UINT32 debugger_read_io_dword(offs_t addr)
                   17542: {
                   17543:        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));
                   17544: }
                   17545: #endif
                   17546: 
1.1       root     17547: void write_io_byte(offs_t addr, UINT8 val)
1.1.1.33  root     17548: #ifdef USE_DEBUGGER
                   17549: {
                   17550:        if(now_debugging) {
                   17551:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   17552:                        if(out_break_point.table[i].status == 1) {
                   17553:                                if(addr == out_break_point.table[i].addr) {
                   17554:                                        out_break_point.hit = i + 1;
                   17555:                                        now_suspended = true;
                   17556:                                        break;
                   17557:                                }
                   17558:                        }
                   17559:                }
                   17560:        }
                   17561:        debugger_write_io_byte(addr, val);
                   17562: }
                   17563: void debugger_write_io_byte(offs_t addr, UINT8 val)
                   17564: #endif
1.1       root     17565: {
1.1.1.25  root     17566: #ifdef ENABLE_DEBUG_IOPORT
1.1.1.33  root     17567:        if(fp_debug_log != NULL) {
                   17568:                fprintf(fp_debug_log, "outb %04X, %02X\n", addr, val);
1.1.1.25  root     17569:        }
                   17570: #endif
1.1       root     17571:        switch(addr) {
1.1.1.29  root     17572: #ifdef SW1US_PATCH
                   17573:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   17574:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
                   17575:                sio_write(0, addr - 1, val);
                   17576:                break;
                   17577: #else
1.1.1.25  root     17578:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   17579:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
                   17580:                dma_write(0, addr, val);
                   17581:                break;
1.1.1.29  root     17582: #endif
1.1.1.25  root     17583:        case 0x20: case 0x21:
1.1       root     17584:                pic_write(0, addr, val);
                   17585:                break;
1.1.1.25  root     17586:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1       root     17587:                pit_write(addr & 0x03, val);
                   17588:                break;
1.1.1.7   root     17589:        case 0x60:
                   17590:                kbd_write_data(val);
                   17591:                break;
1.1.1.9   root     17592:        case 0x61:
                   17593:                if((system_port & 3) != 3 && (val & 3) == 3) {
                   17594:                        // beep on
                   17595: //                     MessageBeep(-1);
                   17596:                } else if((system_port & 3) == 3 && (val & 3) != 3) {
                   17597:                        // beep off
                   17598:                }
                   17599:                system_port = val;
                   17600:                break;
1.1       root     17601:        case 0x64:
1.1.1.7   root     17602:                kbd_write_command(val);
1.1       root     17603:                break;
                   17604:        case 0x70:
                   17605:                cmos_addr = val;
                   17606:                break;
                   17607:        case 0x71:
1.1.1.8   root     17608:                cmos_write(cmos_addr, val);
1.1       root     17609:                break;
1.1.1.25  root     17610:        case 0x81:
                   17611:                dma_page_write(0, 2, val);
                   17612:        case 0x82:
                   17613:                dma_page_write(0, 3, val);
                   17614:        case 0x83:
                   17615:                dma_page_write(0, 1, val);
                   17616:        case 0x87:
                   17617:                dma_page_write(0, 0, val);
                   17618:        case 0x89:
                   17619:                dma_page_write(1, 2, val);
                   17620:        case 0x8a:
                   17621:                dma_page_write(1, 3, val);
                   17622:        case 0x8b:
                   17623:                dma_page_write(1, 1, val);
                   17624:        case 0x8f:
                   17625:                dma_page_write(1, 0, val);
1.1       root     17626:        case 0x92:
1.1.1.7   root     17627:                i386_set_a20_line((val >> 1) & 1);
1.1       root     17628:                break;
1.1.1.25  root     17629:        case 0xa0: case 0xa1:
1.1       root     17630:                pic_write(1, addr, val);
                   17631:                break;
1.1.1.25  root     17632:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   17633:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.26  root     17634:                dma_write(1, (addr - 0xc0) >> 1, val);
1.1.1.25  root     17635:                break;
1.1.1.35  root     17636: #ifdef USE_SERVICE_THREAD
                   17637:        case 0xf7:
                   17638:                // dummy i/o for BIOS/DOS service
1.1.1.36  root     17639:                if(in_service && cursor_moved) {
                   17640:                        // update cursor position before service is done
                   17641:                        pcbios_update_cursor_position();
                   17642:                        cursor_moved = false;
                   17643:                }
1.1.1.35  root     17644:                finish_service_loop();
                   17645:                break;
                   17646: #endif
1.1.1.37! root     17647:        case 0x278: case 0x279: case 0x27a:
        !          17648:                pio_write(1, addr, val);
        !          17649:                break;
1.1.1.29  root     17650:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
                   17651:                sio_write(3, addr, val);
                   17652:                break;
1.1.1.25  root     17653:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
                   17654:                sio_write(1, addr, val);
                   17655:                break;
                   17656:        case 0x378: case 0x379: case 0x37a:
                   17657:                pio_write(0, addr, val);
                   17658:                break;
1.1.1.37! root     17659:        case 0x3bc: case 0x3bd: case 0x3be:
        !          17660:                pio_write(2, addr, val);
        !          17661:                break;
1.1.1.29  root     17662:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
                   17663:                sio_write(2, addr, val);
                   17664:                break;
1.1.1.25  root     17665:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
                   17666:                sio_write(0, addr, val);
                   17667:                break;
1.1       root     17668:        default:
1.1.1.33  root     17669: //             fatalerror("unknown outb %4x,%2x\n", addr, val);
1.1       root     17670:                break;
                   17671:        }
                   17672: }
                   17673: 
                   17674: void write_io_word(offs_t addr, UINT16 val)
                   17675: {
                   17676:        write_io_byte(addr + 0, (val >> 0) & 0xff);
                   17677:        write_io_byte(addr + 1, (val >> 8) & 0xff);
                   17678: }
                   17679: 
1.1.1.33  root     17680: #ifdef USE_DEBUGGER
                   17681: void debugger_write_io_word(offs_t addr, UINT16 val)
                   17682: {
                   17683:        debugger_write_io_byte(addr + 0, (val >> 0) & 0xff);
                   17684:        debugger_write_io_byte(addr + 1, (val >> 8) & 0xff);
                   17685: }
                   17686: #endif
                   17687: 
1.1       root     17688: void write_io_dword(offs_t addr, UINT32 val)
                   17689: {
                   17690:        write_io_byte(addr + 0, (val >>  0) & 0xff);
                   17691:        write_io_byte(addr + 1, (val >>  8) & 0xff);
                   17692:        write_io_byte(addr + 2, (val >> 16) & 0xff);
                   17693:        write_io_byte(addr + 3, (val >> 24) & 0xff);
                   17694: }
1.1.1.33  root     17695: 
                   17696: #ifdef USE_DEBUGGER
                   17697: void debugger_write_io_dword(offs_t addr, UINT32 val)
                   17698: {
                   17699:        debugger_write_io_byte(addr + 0, (val >>  0) & 0xff);
                   17700:        debugger_write_io_byte(addr + 1, (val >>  8) & 0xff);
                   17701:        debugger_write_io_byte(addr + 2, (val >> 16) & 0xff);
                   17702:        debugger_write_io_byte(addr + 3, (val >> 24) & 0xff);
                   17703: }
                   17704: #endif

unix.superglobalmegacorp.com

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