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

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);
1.1.1.38! root     4694:                        DWORD timeout = timeGetTime() + 1000;
        !          4695:                        while(read < count) {
        !          4696:                                if(sio_read(file_handler[fd].sio_port - 1, 5) & 0x01) {
        !          4697:                                        buf[read++] = sio_read(file_handler[fd].sio_port - 1, 0);
        !          4698:                                        timeout = timeGetTime() + 1000;
        !          4699:                                } else {
        !          4700:                                        if(timeGetTime() > timeout) {
        !          4701:                                                break;
        !          4702:                                        }
        !          4703:                                        Sleep(10);
1.1.1.37  root     4704:                                }
                   4705:                        }
                   4706:                        sio_write(file_handler[fd].sio_port - 1, 3, selector);
                   4707:                }
                   4708:                return(read);
                   4709:        }
                   4710:        return(_read(fd, buffer, count));
                   4711: }
                   4712: 
1.1       root     4713: int msdos_kbhit()
                   4714: {
                   4715:        msdos_stdio_reopen();
                   4716:        
1.1.1.20  root     4717:        process_t *process = msdos_process_info_get(current_psp);
                   4718:        int fd = msdos_psp_get_file_table(0, current_psp);
                   4719:        
                   4720:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     4721:                // stdin is redirected to file
1.1.1.20  root     4722:                return(eof(fd) == 0);
1.1       root     4723:        }
                   4724:        
                   4725:        // check keyboard status
1.1.1.35  root     4726:        if(key_recv != 0) {
1.1       root     4727:                return(1);
                   4728:        }
1.1.1.35  root     4729:        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   4730: #ifdef USE_SERVICE_THREAD
                   4731:                EnterCriticalSection(&key_buf_crit_sect);
                   4732: #endif
                   4733:                bool empty = key_buf_char->empty();
                   4734: #ifdef USE_SERVICE_THREAD
                   4735:                LeaveCriticalSection(&key_buf_crit_sect);
                   4736: #endif
                   4737:                if(!empty) return(1);
                   4738:        }
                   4739:        return(_kbhit());
1.1       root     4740: }
                   4741: 
                   4742: int msdos_getch_ex(int echo)
                   4743: {
                   4744:        static char prev = 0;
                   4745:        
                   4746:        msdos_stdio_reopen();
                   4747:        
1.1.1.20  root     4748:        process_t *process = msdos_process_info_get(current_psp);
                   4749:        int fd = msdos_psp_get_file_table(0, current_psp);
                   4750:        
                   4751:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     4752:                // stdin is redirected to file
                   4753: retry:
                   4754:                char data;
1.1.1.37  root     4755:                if(msdos_read(fd, &data, 1) == 1) {
1.1       root     4756:                        char tmp = data;
                   4757:                        if(data == 0x0a) {
                   4758:                                if(prev == 0x0d) {
                   4759:                                        goto retry; // CRLF -> skip LF
                   4760:                                } else {
                   4761:                                        data = 0x0d; // LF only -> CR
                   4762:                                }
                   4763:                        }
                   4764:                        prev = tmp;
                   4765:                        return(data);
                   4766:                }
                   4767:                return(EOF);
                   4768:        }
                   4769:        
                   4770:        // input from console
1.1.1.5   root     4771:        int key_char, key_scan;
1.1.1.33  root     4772:        if(key_recv != 0) {
1.1.1.5   root     4773:                key_char = (key_code >> 0) & 0xff;
                   4774:                key_scan = (key_code >> 8) & 0xff;
                   4775:                key_code >>= 16;
1.1.1.33  root     4776:                key_recv >>= 16;
1.1.1.5   root     4777:        } else {
1.1.1.35  root     4778:                while(key_buf_char != NULL && key_buf_scan != NULL && !m_halted) {
                   4779:                        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   4780: #ifdef USE_SERVICE_THREAD
                   4781:                                EnterCriticalSection(&key_buf_crit_sect);
                   4782: #endif
                   4783:                                bool empty = key_buf_char->empty();
                   4784: #ifdef USE_SERVICE_THREAD
                   4785:                                LeaveCriticalSection(&key_buf_crit_sect);
                   4786: #endif
                   4787:                                if(!empty) break;
                   4788:                        }
1.1.1.23  root     4789:                        if(!(fd < process->max_files && file_handler[fd].valid && file_handler[fd].atty && file_mode[file_handler[fd].mode].in)) {
                   4790:                                // NOTE: stdin is redirected to stderr when we do "type (file) | more" on freedos's command.com
                   4791:                                if(_kbhit()) {
1.1.1.32  root     4792:                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     4793: #ifdef USE_SERVICE_THREAD
                   4794:                                                EnterCriticalSection(&key_buf_crit_sect);
                   4795: #endif
1.1.1.32  root     4796:                                                key_buf_char->write(_getch());
1.1.1.35  root     4797:                                                key_buf_scan->write(0x00);
                   4798: #ifdef USE_SERVICE_THREAD
                   4799:                                                LeaveCriticalSection(&key_buf_crit_sect);
                   4800: #endif
1.1.1.32  root     4801:                                        }
1.1.1.23  root     4802:                                } else {
                   4803:                                        Sleep(10);
                   4804:                                }
                   4805:                        } else {
                   4806:                                if(!update_key_buffer()) {
                   4807:                                        Sleep(10);
                   4808:                                }
1.1.1.14  root     4809:                        }
                   4810:                }
                   4811:                if(m_halted) {
1.1.1.33  root     4812:                        // insert CR to terminate input loops
1.1.1.14  root     4813:                        key_char = 0x0d;
                   4814:                        key_scan = 0;
1.1.1.32  root     4815:                } else if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     4816: #ifdef USE_SERVICE_THREAD
                   4817:                        EnterCriticalSection(&key_buf_crit_sect);
                   4818: #endif
1.1.1.14  root     4819:                        key_char = key_buf_char->read();
                   4820:                        key_scan = key_buf_scan->read();
1.1.1.35  root     4821: #ifdef USE_SERVICE_THREAD
                   4822:                        LeaveCriticalSection(&key_buf_crit_sect);
                   4823: #endif
1.1.1.5   root     4824:                }
1.1       root     4825:        }
                   4826:        if(echo && key_char) {
                   4827:                msdos_putch(key_char);
                   4828:        }
                   4829:        return key_char ? key_char : (key_scan != 0xe0) ? key_scan : 0;
                   4830: }
                   4831: 
                   4832: inline int msdos_getch()
                   4833: {
                   4834:        return(msdos_getch_ex(0));
                   4835: }
                   4836: 
                   4837: inline int msdos_getche()
                   4838: {
                   4839:        return(msdos_getch_ex(1));
                   4840: }
                   4841: 
                   4842: int msdos_write(int fd, const void *buffer, unsigned int count)
                   4843: {
1.1.1.37  root     4844:        if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].sio_port >= 1 && file_handler[fd].sio_port <= 4) {
                   4845:                // write to serial port
1.1.1.38! root     4846:                int written = 0;
1.1.1.37  root     4847:                if(sio_port_number[file_handler[fd].sio_port - 1] != 0) {
                   4848:                        UINT8 *buf = (UINT8 *)buffer;
                   4849:                        UINT8 selector = sio_read(file_handler[fd].sio_port - 1, 3);
                   4850:                        sio_write(file_handler[fd].sio_port - 1, 3, selector & ~0x80);
1.1.1.38! root     4851:                        DWORD timeout = timeGetTime() + 1000;
        !          4852:                        while(written < count) {
        !          4853:                                if(sio_read(file_handler[fd].sio_port - 1, 5) & 0x20) {
        !          4854:                                        sio_write(file_handler[fd].sio_port - 1, 0, buf[written++]);
        !          4855:                                        timeout = timeGetTime() + 1000;
        !          4856:                                } else {
        !          4857:                                        if(timeGetTime() > timeout) {
        !          4858:                                                break;
        !          4859:                                        }
        !          4860:                                        Sleep(10);
        !          4861:                                }
1.1.1.37  root     4862:                        }
                   4863:                        sio_write(file_handler[fd].sio_port - 1, 3, selector);
                   4864:                }
1.1.1.38! root     4865:                return(written);
1.1.1.37  root     4866:        } else if(fd < process->max_files && file_handler[fd].valid && file_handler[fd].lpt_port >= 1 && file_handler[fd].lpt_port <= 3) {
                   4867:                // write to printer port
                   4868:                UINT8 *buf = (UINT8 *)buffer;
                   4869:                for(unsigned int i = 0; i < count; i++) {
                   4870: //                     printer_out(file_handler[fd].lpt_port - 1, buf[i]);
                   4871:                        pcbios_printer_out(file_handler[fd].lpt_port - 1, buf[i]);
                   4872:                }
                   4873:                return(count);
                   4874:        } else if(fd == 1 && file_handler[1].valid && !file_handler[1].atty) {
1.1       root     4875:                // CR+LF -> LF
1.1.1.37  root     4876:                static int is_cr = 0;
1.1       root     4877:                UINT8 *buf = (UINT8 *)buffer;
                   4878:                for(unsigned int i = 0; i < count; i++) {
                   4879:                        UINT8 data = buf[i];
                   4880:                        if(is_cr) {
                   4881:                                if(data != 0x0a) {
                   4882:                                        UINT8 tmp = 0x0d;
                   4883:                                        _write(1, &tmp, 1);
                   4884:                                }
                   4885:                                _write(1, &data, 1);
                   4886:                                is_cr = 0;
                   4887:                        } else if(data == 0x0d) {
                   4888:                                is_cr = 1;
                   4889:                        } else {
                   4890:                                _write(1, &data, 1);
                   4891:                        }
                   4892:                }
                   4893:                return(count);
                   4894:        }
1.1.1.14  root     4895:        vram_flush();
1.1       root     4896:        return(_write(fd, buffer, count));
                   4897: }
                   4898: 
                   4899: void msdos_putch(UINT8 data)
1.1.1.35  root     4900: #ifdef USE_SERVICE_THREAD
                   4901: {
                   4902:        EnterCriticalSection(&putch_crit_sect);
                   4903:        msdos_putch_tmp(data);
                   4904:        LeaveCriticalSection(&putch_crit_sect);
                   4905: }
                   4906: void msdos_putch_tmp(UINT8 data)
                   4907: #endif
1.1       root     4908: {
1.1.1.34  root     4909:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   4910:        SMALL_RECT rect;
                   4911:        COORD co;
1.1       root     4912:        static int p = 0;
                   4913:        static int is_kanji = 0;
                   4914:        static int is_esc = 0;
                   4915:        static int stored_x;
                   4916:        static int stored_y;
                   4917:        static WORD stored_a;
1.1.1.20  root     4918:        static char tmp[64], out[64];
1.1       root     4919:        
                   4920:        msdos_stdio_reopen();
                   4921:        
1.1.1.20  root     4922:        process_t *process = msdos_process_info_get(current_psp);
                   4923:        int fd = msdos_psp_get_file_table(1, current_psp);
                   4924:        
                   4925:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     4926:                // stdout is redirected to file
1.1.1.20  root     4927:                msdos_write(fd, &data, 1);
1.1       root     4928:                return;
                   4929:        }
1.1.1.23  root     4930:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     4931:        
                   4932:        // output to console
                   4933:        tmp[p++] = data;
                   4934:        
1.1.1.14  root     4935:        vram_flush();
                   4936:        
1.1       root     4937:        if(is_kanji) {
                   4938:                // kanji character
                   4939:                is_kanji = 0;
                   4940:        } else if(is_esc) {
                   4941:                // escape sequense
                   4942:                if((tmp[1] == ')' || tmp[1] == '(') && p == 3) {
                   4943:                        p = is_esc = 0;
                   4944:                } else if(tmp[1] == '=' && p == 4) {
                   4945:                        co.X = tmp[3] - 0x20;
1.1.1.14  root     4946:                        co.Y = tmp[2] - 0x20 + scr_top;
1.1       root     4947:                        SetConsoleCursorPosition(hStdout, co);
                   4948:                        mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14  root     4949:                        mem[0x451 + mem[0x462] * 2] = co.Y - scr_top;
1.1       root     4950:                        cursor_moved = false;
                   4951:                        p = is_esc = 0;
                   4952:                } else if((data >= 'a' && data <= 'z') || (data >= 'A' && data <= 'Z') || data == '*') {
                   4953:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   4954:                        co.X = csbi.dwCursorPosition.X;
                   4955:                        co.Y = csbi.dwCursorPosition.Y;
                   4956:                        WORD wAttributes = csbi.wAttributes;
                   4957:                        
                   4958:                        if(tmp[1] == 'D') {
                   4959:                                co.Y++;
                   4960:                        } else if(tmp[1] == 'E') {
                   4961:                                co.X = 0;
                   4962:                                co.Y++;
                   4963:                        } else if(tmp[1] == 'M') {
                   4964:                                co.Y--;
                   4965:                        } else if(tmp[1] == '*') {
1.1.1.14  root     4966:                                SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   4967:                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   4968:                                co.X = 0;
                   4969:                                co.Y = csbi.srWindow.Top;
1.1       root     4970:                        } else if(tmp[1] == '[') {
                   4971:                                int param[256], params = 0;
                   4972:                                memset(param, 0, sizeof(param));
                   4973:                                for(int i = 2; i < p; i++) {
                   4974:                                        if(tmp[i] >= '0' && tmp[i] <= '9') {
                   4975:                                                param[params] *= 10;
                   4976:                                                param[params] += tmp[i] - '0';
                   4977:                                        } else {
                   4978:                                                params++;
                   4979:                                        }
                   4980:                                }
                   4981:                                if(data == 'A') {
1.1.1.14  root     4982:                                        co.Y -= (params == 0) ? 1 : param[0];
1.1       root     4983:                                } else if(data == 'B') {
1.1.1.14  root     4984:                                        co.Y += (params == 0) ? 1 : param[0];
1.1       root     4985:                                } else if(data == 'C') {
1.1.1.14  root     4986:                                        co.X += (params == 0) ? 1 : param[0];
1.1       root     4987:                                } else if(data == 'D') {
1.1.1.14  root     4988:                                        co.X -= (params == 0) ? 1 : param[0];
1.1       root     4989:                                } else if(data == 'H' || data == 'f') {
1.1.1.14  root     4990:                                        co.X = (param[1] == 0 ? 1 : param[1]) - 1;
                   4991:                                        co.Y = (param[0] == 0 ? 1 : param[0]) - 1 + csbi.srWindow.Top;
1.1       root     4992:                                } else if(data == 'J') {
1.1.1.14  root     4993:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     4994:                                        if(param[0] == 0) {
                   4995:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     4996:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   4997:                                                if(co.Y < csbi.srWindow.Bottom) {
                   4998:                                                        SET_RECT(rect, 0, co.Y + 1, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   4999:                                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5000:                                                }
                   5001:                                        } else if(param[0] == 1) {
1.1.1.14  root     5002:                                                if(co.Y > csbi.srWindow.Top) {
                   5003:                                                        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, co.Y - 1);
                   5004:                                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5005:                                                }
                   5006:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.14  root     5007:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5008:                                        } else if(param[0] == 2) {
1.1.1.14  root     5009:                                                SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5010:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5011:                                                co.X = co.Y = 0;
                   5012:                                        }
                   5013:                                } else if(data == 'K') {
1.1.1.14  root     5014:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5015:                                        if(param[0] == 0) {
                   5016:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     5017:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5018:                                        } else if(param[0] == 1) {
                   5019:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.14  root     5020:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5021:                                        } else if(param[0] == 2) {
                   5022:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     5023:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5024:                                        }
                   5025:                                } else if(data == 'L') {
1.1.1.14  root     5026:                                        if(params == 0) {
                   5027:                                                param[0] = 1;
1.1       root     5028:                                        }
1.1.1.14  root     5029:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5030:                                        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5031:                                        SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5032:                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5033:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     5034:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y + param[0] - 1);
1.1.1.14  root     5035:                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5036:                                        co.X = 0;
                   5037:                                } else if(data == 'M') {
1.1.1.14  root     5038:                                        if(params == 0) {
                   5039:                                                param[0] = 1;
                   5040:                                        }
                   5041:                                        if(co.Y + param[0] > csbi.srWindow.Bottom) {
                   5042:                                                clear_scr_buffer(csbi.wAttributes);
                   5043:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5044:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     5045:                                        } else {
1.1.1.14  root     5046:                                                SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5047:                                                ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5048:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   5049:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   5050:                                                clear_scr_buffer(csbi.wAttributes);
1.1       root     5051:                                        }
                   5052:                                        co.X = 0;
                   5053:                                } else if(data == 'h') {
                   5054:                                        if(tmp[2] == '>' && tmp[3] == '5') {
                   5055:                                                CONSOLE_CURSOR_INFO cur;
                   5056:                                                GetConsoleCursorInfo(hStdout, &cur);
                   5057:                                                if(cur.bVisible) {
                   5058:                                                        cur.bVisible = FALSE;
1.1.1.14  root     5059: //                                                     SetConsoleCursorInfo(hStdout, &cur);
1.1       root     5060:                                                }
                   5061:                                        }
                   5062:                                } else if(data == 'l') {
                   5063:                                        if(tmp[2] == '>' && tmp[3] == '5') {
                   5064:                                                CONSOLE_CURSOR_INFO cur;
                   5065:                                                GetConsoleCursorInfo(hStdout, &cur);
                   5066:                                                if(!cur.bVisible) {
                   5067:                                                        cur.bVisible = TRUE;
1.1.1.14  root     5068: //                                                     SetConsoleCursorInfo(hStdout, &cur);
1.1       root     5069:                                                }
                   5070:                                        }
                   5071:                                } else if(data == 'm') {
                   5072:                                        wAttributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
                   5073:                                        int reverse = 0, hidden = 0;
                   5074:                                        for(int i = 0; i < params; i++) {
                   5075:                                                if(param[i] == 1) {
                   5076:                                                        wAttributes |= FOREGROUND_INTENSITY;
                   5077:                                                } else if(param[i] == 4) {
                   5078:                                                        wAttributes |= COMMON_LVB_UNDERSCORE;
                   5079:                                                } else if(param[i] == 7) {
                   5080:                                                        reverse = 1;
                   5081:                                                } else if(param[i] == 8 || param[i] == 16) {
                   5082:                                                        hidden = 1;
                   5083:                                                } else if((param[i] >= 17 && param[i] <= 23) || (param[i] >= 30 && param[i] <= 37)) {
                   5084:                                                        wAttributes &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
                   5085:                                                        if(param[i] >= 17 && param[i] <= 23) {
                   5086:                                                                param[i] -= 16;
                   5087:                                                        } else {
                   5088:                                                                param[i] -= 30;
                   5089:                                                        }
                   5090:                                                        if(param[i] & 1) {
                   5091:                                                                wAttributes |= FOREGROUND_RED;
                   5092:                                                        }
                   5093:                                                        if(param[i] & 2) {
                   5094:                                                                wAttributes |= FOREGROUND_GREEN;
                   5095:                                                        }
                   5096:                                                        if(param[i] & 4) {
                   5097:                                                                wAttributes |= FOREGROUND_BLUE;
                   5098:                                                        }
                   5099:                                                } else if(param[i] >= 40 && param[i] <= 47) {
                   5100:                                                        wAttributes &= ~(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
                   5101:                                                        if((param[i] - 40) & 1) {
                   5102:                                                                wAttributes |= BACKGROUND_RED;
                   5103:                                                        }
                   5104:                                                        if((param[i] - 40) & 2) {
                   5105:                                                                wAttributes |= BACKGROUND_GREEN;
                   5106:                                                        }
                   5107:                                                        if((param[i] - 40) & 4) {
                   5108:                                                                wAttributes |= BACKGROUND_BLUE;
                   5109:                                                        }
                   5110:                                                }
                   5111:                                        }
                   5112:                                        if(reverse) {
                   5113:                                                wAttributes &= ~0xff;
                   5114:                                                wAttributes |= BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
                   5115:                                        }
                   5116:                                        if(hidden) {
                   5117:                                                wAttributes &= ~0x0f;
                   5118:                                                wAttributes |= (wAttributes >> 4) & 0x0f;
                   5119:                                        }
                   5120:                                } else if(data == 'n') {
                   5121:                                        if(param[0] == 6) {
                   5122:                                                char tmp[16];
                   5123:                                                sprintf(tmp, "\x1b[%d;%dR", co.Y + 1, co.X + 1);
                   5124:                                                int len = strlen(tmp);
1.1.1.32  root     5125:                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     5126: #ifdef USE_SERVICE_THREAD
                   5127:                                                        EnterCriticalSection(&key_buf_crit_sect);
                   5128: #endif
1.1.1.32  root     5129:                                                        for(int i = 0; i < len; i++) {
                   5130:                                                                key_buf_char->write(tmp[i]);
                   5131:                                                                key_buf_scan->write(0x00);
                   5132:                                                        }
1.1.1.35  root     5133: #ifdef USE_SERVICE_THREAD
                   5134:                                                        LeaveCriticalSection(&key_buf_crit_sect);
                   5135: #endif
1.1       root     5136:                                                }
                   5137:                                        }
                   5138:                                } else if(data == 's') {
                   5139:                                        stored_x = co.X;
                   5140:                                        stored_y = co.Y;
                   5141:                                        stored_a = wAttributes;
                   5142:                                } else if(data == 'u') {
                   5143:                                        co.X = stored_x;
                   5144:                                        co.Y = stored_y;
                   5145:                                        wAttributes = stored_a;
                   5146:                                }
                   5147:                        }
                   5148:                        if(co.X < 0) {
                   5149:                                co.X = 0;
                   5150:                        } else if(co.X >= csbi.dwSize.X) {
                   5151:                                co.X = csbi.dwSize.X - 1;
                   5152:                        }
1.1.1.14  root     5153:                        if(co.Y < csbi.srWindow.Top) {
                   5154:                                co.Y = csbi.srWindow.Top;
                   5155:                        } else if(co.Y > csbi.srWindow.Bottom) {
                   5156:                                co.Y = csbi.srWindow.Bottom;
1.1       root     5157:                        }
                   5158:                        if(co.X != csbi.dwCursorPosition.X || co.Y != csbi.dwCursorPosition.Y) {
                   5159:                                SetConsoleCursorPosition(hStdout, co);
                   5160:                                mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14  root     5161:                                mem[0x451 + mem[0x462] * 2] = co.Y - csbi.srWindow.Top;
1.1       root     5162:                                cursor_moved = false;
                   5163:                        }
                   5164:                        if(wAttributes != csbi.wAttributes) {
                   5165:                                SetConsoleTextAttribute(hStdout, wAttributes);
                   5166:                        }
                   5167:                        p = is_esc = 0;
                   5168:                }
                   5169:                return;
                   5170:        } else {
                   5171:                if(msdos_lead_byte_check(data)) {
                   5172:                        is_kanji = 1;
                   5173:                        return;
                   5174:                } else if(data == 0x1b) {
                   5175:                        is_esc = 1;
                   5176:                        return;
                   5177:                }
                   5178:        }
1.1.1.20  root     5179:        
                   5180:        DWORD q = 0, num;
                   5181:        is_kanji = 0;
                   5182:        for(int i = 0; i < p; i++) {
                   5183:                UINT8 c = tmp[i];
                   5184:                if(is_kanji) {
                   5185:                        is_kanji = 0;
                   5186:                } else if(msdos_lead_byte_check(data)) {
                   5187:                        is_kanji = 1;
                   5188:                } else if(msdos_ctrl_code_check(data)) {
                   5189:                        out[q++] = '^';
                   5190:                        c += 'A' - 1;
                   5191:                }
                   5192:                out[q++] = c;
                   5193:        }
1.1.1.34  root     5194:        if(q == 1 && out[0] == 0x08) {
                   5195:                // back space
                   5196:                GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5197:                if(csbi.dwCursorPosition.X > 0) {
                   5198:                        co.X = csbi.dwCursorPosition.X - 1;
                   5199:                        co.Y = csbi.dwCursorPosition.Y;
                   5200:                        SetConsoleCursorPosition(hStdout, co);
                   5201:                } else if(csbi.dwCursorPosition.Y > 0) {
                   5202:                        co.X = csbi.dwSize.X - 1;
                   5203:                        co.Y = csbi.dwCursorPosition.Y - 1;
                   5204:                        SetConsoleCursorPosition(hStdout, co);
                   5205:                } else {
                   5206:                        WriteConsole(hStdout, out, q, &num, NULL); // to make sure
                   5207:                }
                   5208:        } else {
                   5209:                WriteConsole(hStdout, out, q, &num, NULL);
                   5210:        }
1.1       root     5211:        p = 0;
1.1.1.14  root     5212:        
1.1.1.15  root     5213:        if(!restore_console_on_exit) {
                   5214:                GetConsoleScreenBufferInfo(hStdout, &csbi);
                   5215:                scr_top = csbi.srWindow.Top;
                   5216:        }
1.1       root     5217:        cursor_moved = true;
                   5218: }
                   5219: 
                   5220: int msdos_aux_in()
                   5221: {
1.1.1.21  root     5222:        msdos_stdio_reopen();
                   5223:        
1.1.1.20  root     5224:        process_t *process = msdos_process_info_get(current_psp);
                   5225:        int fd = msdos_psp_get_file_table(3, current_psp);
                   5226:        
                   5227:        if(fd < process->max_files && file_handler[fd].valid && !eof(fd)) {
1.1       root     5228:                char data = 0;
1.1.1.37  root     5229:                msdos_read(fd, &data, 1);
1.1       root     5230:                return(data);
                   5231:        } else {
                   5232:                return(EOF);
                   5233:        }
                   5234: }
                   5235: 
                   5236: void msdos_aux_out(char data)
                   5237: {
1.1.1.21  root     5238:        msdos_stdio_reopen();
                   5239:        
1.1.1.20  root     5240:        process_t *process = msdos_process_info_get(current_psp);
                   5241:        int fd = msdos_psp_get_file_table(3, current_psp);
                   5242:        
                   5243:        if(fd < process->max_files && file_handler[fd].valid) {
                   5244:                msdos_write(fd, &data, 1);
1.1       root     5245:        }
                   5246: }
                   5247: 
                   5248: void msdos_prn_out(char data)
                   5249: {
1.1.1.21  root     5250:        msdos_stdio_reopen();
                   5251:        
1.1.1.20  root     5252:        process_t *process = msdos_process_info_get(current_psp);
                   5253:        int fd = msdos_psp_get_file_table(4, current_psp);
                   5254:        
                   5255:        if(fd < process->max_files && file_handler[fd].valid) {
                   5256:                msdos_write(fd, &data, 1);
1.1       root     5257:        }
                   5258: }
                   5259: 
                   5260: // memory control
                   5261: 
1.1.1.19  root     5262: mcb_t *msdos_mcb_create(int mcb_seg, UINT8 mz, UINT16 psp, int paragraphs)
1.1       root     5263: {
                   5264:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5265:        
                   5266:        mcb->mz = mz;
                   5267:        mcb->psp = psp;
1.1.1.30  root     5268:        mcb->paragraphs = paragraphs;
1.1       root     5269:        return(mcb);
                   5270: }
                   5271: 
                   5272: void msdos_mcb_check(mcb_t *mcb)
                   5273: {
                   5274:        if(!(mcb->mz == 'M' || mcb->mz == 'Z')) {
1.1.1.28  root     5275:                #if 0
                   5276:                        // shutdown now !!!
                   5277:                        fatalerror("broken memory control block\n");
                   5278:                #else
                   5279:                        // return error code and continue
                   5280:                        throw(0x07); // broken memory control block
                   5281:                #endif
1.1       root     5282:        }
                   5283: }
                   5284: 
                   5285: int msdos_mem_split(int seg, int paragraphs)
                   5286: {
                   5287:        int mcb_seg = seg - 1;
                   5288:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5289:        msdos_mcb_check(mcb);
                   5290:        
1.1.1.30  root     5291:        if(mcb->paragraphs > paragraphs) {
1.1       root     5292:                int new_seg = mcb_seg + 1 + paragraphs;
1.1.1.30  root     5293:                int new_paragraphs = mcb->paragraphs - paragraphs - 1;
1.1       root     5294:                
                   5295:                msdos_mcb_create(new_seg, mcb->mz, 0, new_paragraphs);
                   5296:                mcb->mz = 'M';
1.1.1.30  root     5297:                mcb->paragraphs = paragraphs;
1.1       root     5298:                return(0);
                   5299:        }
                   5300:        return(-1);
                   5301: }
                   5302: 
                   5303: void msdos_mem_merge(int seg)
                   5304: {
                   5305:        int mcb_seg = seg - 1;
                   5306:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5307:        msdos_mcb_check(mcb);
                   5308:        
                   5309:        while(1) {
                   5310:                if(mcb->mz == 'Z') {
                   5311:                        break;
                   5312:                }
1.1.1.30  root     5313:                int next_seg = mcb_seg + 1 + mcb->paragraphs;
1.1       root     5314:                mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5315:                msdos_mcb_check(next_mcb);
                   5316:                
                   5317:                if(next_mcb->psp != 0) {
                   5318:                        break;
                   5319:                }
                   5320:                mcb->mz = next_mcb->mz;
1.1.1.30  root     5321:                mcb->paragraphs = mcb->paragraphs + 1 + next_mcb->paragraphs;
1.1       root     5322:        }
                   5323: }
                   5324: 
1.1.1.8   root     5325: int msdos_mem_alloc(int mcb_seg, int paragraphs, int new_process)
1.1       root     5326: {
                   5327:        while(1) {
                   5328:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     5329:                bool last_block;
1.1       root     5330:                
1.1.1.14  root     5331:                if(mcb->psp == 0) {
                   5332:                        msdos_mem_merge(mcb_seg + 1);
                   5333:                } else {
                   5334:                        msdos_mcb_check(mcb);
                   5335:                }
1.1.1.33  root     5336:                if(!(last_block = (mcb->mz == 'Z'))) {
                   5337:                        // check if the next is dummy mcb to link to umb
                   5338:                        int next_seg = mcb_seg + 1 + mcb->paragraphs;
                   5339:                        mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5340:                        last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
                   5341:                }
                   5342:                if(!(new_process && !last_block)) {
1.1.1.30  root     5343:                        if(mcb->psp == 0 && mcb->paragraphs >= paragraphs) {
1.1       root     5344:                                msdos_mem_split(mcb_seg + 1, paragraphs);
                   5345:                                mcb->psp = current_psp;
                   5346:                                return(mcb_seg + 1);
                   5347:                        }
                   5348:                }
                   5349:                if(mcb->mz == 'Z') {
                   5350:                        break;
                   5351:                }
1.1.1.30  root     5352:                mcb_seg += 1 + mcb->paragraphs;
1.1       root     5353:        }
                   5354:        return(-1);
                   5355: }
                   5356: 
                   5357: int msdos_mem_realloc(int seg, int paragraphs, int *max_paragraphs)
                   5358: {
                   5359:        int mcb_seg = seg - 1;
                   5360:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5361:        msdos_mcb_check(mcb);
1.1.1.30  root     5362:        int current_paragraphs = mcb->paragraphs;
1.1       root     5363:        
                   5364:        msdos_mem_merge(seg);
1.1.1.30  root     5365:        if(paragraphs > mcb->paragraphs) {
1.1.1.14  root     5366:                if(max_paragraphs) {
1.1.1.30  root     5367:                        *max_paragraphs = mcb->paragraphs;
1.1.1.14  root     5368:                }
1.1       root     5369:                msdos_mem_split(seg, current_paragraphs);
                   5370:                return(-1);
                   5371:        }
                   5372:        msdos_mem_split(seg, paragraphs);
                   5373:        return(0);
                   5374: }
                   5375: 
                   5376: void msdos_mem_free(int seg)
                   5377: {
                   5378:        int mcb_seg = seg - 1;
                   5379:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5380:        msdos_mcb_check(mcb);
                   5381:        
                   5382:        mcb->psp = 0;
                   5383:        msdos_mem_merge(seg);
                   5384: }
                   5385: 
1.1.1.8   root     5386: int msdos_mem_get_free(int mcb_seg, int new_process)
1.1       root     5387: {
                   5388:        int max_paragraphs = 0;
                   5389:        
                   5390:        while(1) {
                   5391:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     5392:                bool last_block;
                   5393:                
1.1       root     5394:                msdos_mcb_check(mcb);
                   5395:                
1.1.1.33  root     5396:                if(!(last_block = (mcb->mz == 'Z'))) {
                   5397:                        // check if the next is dummy mcb to link to umb
                   5398:                        int next_seg = mcb_seg + 1 + mcb->paragraphs;
                   5399:                        mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5400:                        last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
                   5401:                }
                   5402:                if(!(new_process && !last_block)) {
1.1.1.30  root     5403:                        if(mcb->psp == 0 && mcb->paragraphs > max_paragraphs) {
                   5404:                                max_paragraphs = mcb->paragraphs;
1.1       root     5405:                        }
                   5406:                }
                   5407:                if(mcb->mz == 'Z') {
                   5408:                        break;
                   5409:                }
1.1.1.30  root     5410:                mcb_seg += 1 + mcb->paragraphs;
1.1       root     5411:        }
1.1.1.14  root     5412:        return(max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs);
1.1       root     5413: }
                   5414: 
1.1.1.8   root     5415: int msdos_mem_get_last_mcb(int mcb_seg, UINT16 psp)
                   5416: {
                   5417:        int last_seg = -1;
                   5418:        
                   5419:        while(1) {
                   5420:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5421:                msdos_mcb_check(mcb);
                   5422:                
1.1.1.14  root     5423:                if(mcb->psp == psp) {
1.1.1.8   root     5424:                        last_seg = mcb_seg;
                   5425:                }
1.1.1.14  root     5426:                if(mcb->mz == 'Z') {
                   5427:                        break;
                   5428:                }
1.1.1.30  root     5429:                mcb_seg += 1 + mcb->paragraphs;
1.1.1.8   root     5430:        }
                   5431:        return(last_seg);
                   5432: }
                   5433: 
1.1.1.19  root     5434: int msdos_mem_get_umb_linked()
                   5435: {
1.1.1.33  root     5436:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   5437:        msdos_mcb_check(mcb);
1.1.1.19  root     5438:        
1.1.1.33  root     5439:        if(mcb->mz == 'M') {
                   5440:                return(-1);
1.1.1.19  root     5441:        }
                   5442:        return(0);
                   5443: }
                   5444: 
1.1.1.33  root     5445: void msdos_mem_link_umb()
1.1.1.19  root     5446: {
1.1.1.33  root     5447:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   5448:        msdos_mcb_check(mcb);
1.1.1.19  root     5449:        
1.1.1.33  root     5450:        mcb->mz = 'M';
                   5451:        mcb->paragraphs = (UMB_TOP >> 4) - (MEMORY_END >> 4);
1.1.1.19  root     5452: }
                   5453: 
1.1.1.33  root     5454: void msdos_mem_unlink_umb()
1.1.1.19  root     5455: {
1.1.1.33  root     5456:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   5457:        msdos_mcb_check(mcb);
1.1.1.19  root     5458:        
1.1.1.33  root     5459:        mcb->mz = 'Z';
                   5460:        mcb->paragraphs = 0;
1.1.1.19  root     5461: }
                   5462: 
1.1.1.29  root     5463: #ifdef SUPPORT_HMA
                   5464: 
                   5465: hma_mcb_t *msdos_hma_mcb_create(int offset, int owner, int size, int next)
                   5466: {
                   5467:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5468:        
                   5469:        mcb->ms[0] = 'M';
                   5470:        mcb->ms[1] = 'S';
                   5471:        mcb->owner = owner;
                   5472:        mcb->size = size;
                   5473:        mcb->next = next;
                   5474:        return(mcb);
                   5475: }
                   5476: 
                   5477: bool msdos_is_hma_mcb_valid(hma_mcb_t *mcb)
                   5478: {
                   5479:        return(mcb->ms[0] == 'M' && mcb->ms[1] == 'S');
                   5480: }
                   5481: 
                   5482: int msdos_hma_mem_split(int offset, int size)
                   5483: {
                   5484:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5485:        
                   5486:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5487:                return(-1);
                   5488:        }
                   5489:        if(mcb->size >= size + 0x10) {
                   5490:                int new_offset = offset + 0x10 + size;
                   5491:                int new_size = mcb->size - 0x10 - size;
                   5492:                
                   5493:                msdos_hma_mcb_create(new_offset, 0, new_size, mcb->next);
                   5494:                mcb->size = size;
                   5495:                mcb->next = new_offset;
                   5496:                return(0);
                   5497:        }
                   5498:        return(-1);
                   5499: }
                   5500: 
                   5501: void msdos_hma_mem_merge(int offset)
                   5502: {
                   5503:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5504:        
                   5505:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5506:                return;
                   5507:        }
                   5508:        while(1) {
                   5509:                if(mcb->next == 0) {
                   5510:                        break;
                   5511:                }
                   5512:                hma_mcb_t *next_mcb = (hma_mcb_t *)(mem + 0xffff0 + mcb->next);
                   5513:                
                   5514:                if(!msdos_is_hma_mcb_valid(next_mcb)) {
                   5515:                        return;
                   5516:                }
                   5517:                if(next_mcb->owner != 0) {
                   5518:                        break;
                   5519:                }
                   5520:                mcb->size += 0x10 + next_mcb->size;
                   5521:                mcb->next = next_mcb->next;
                   5522:        }
                   5523: }
                   5524: 
                   5525: int msdos_hma_mem_alloc(int size, UINT16 owner)
                   5526: {
                   5527:        int offset = 0x10; // first mcb in HMA
                   5528:        
                   5529:        while(1) {
                   5530:                hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5531:                
                   5532:                if(!msdos_is_hma_mcb_valid(mcb)) {
                   5533:                        return(-1);
                   5534:                }
                   5535:                if(mcb->owner == 0) {
                   5536:                        msdos_hma_mem_merge(offset);
                   5537:                }
                   5538:                if(mcb->owner == 0 && mcb->size >= size) {
                   5539:                        msdos_hma_mem_split(offset, size);
                   5540:                        mcb->owner = owner;
                   5541:                        return(offset);
                   5542:                }
                   5543:                if(mcb->next == 0) {
                   5544:                        break;
                   5545:                }
                   5546:                offset = mcb->next;
                   5547:        }
                   5548:        return(-1);
                   5549: }
                   5550: 
                   5551: int msdos_hma_mem_realloc(int offset, int size)
                   5552: {
                   5553:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5554:        
                   5555:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5556:                return(-1);
                   5557:        }
                   5558:        if(mcb->size < size) {
                   5559:                return(-1);
                   5560:        }
                   5561:        msdos_hma_mem_split(offset, size);
                   5562:        return(0);
                   5563: }
                   5564: 
                   5565: void msdos_hma_mem_free(int offset)
                   5566: {
                   5567:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5568:        
                   5569:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5570:                return;
                   5571:        }
                   5572:        mcb->owner = 0;
                   5573:        msdos_hma_mem_merge(offset);
                   5574: }
                   5575: 
                   5576: int msdos_hma_mem_get_free(int *available_offset)
                   5577: {
                   5578:        int offset = 0x10; // first mcb in HMA
                   5579:        int size = 0;
                   5580:        
                   5581:        while(1) {
                   5582:                hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5583:                
                   5584:                if(!msdos_is_hma_mcb_valid(mcb)) {
                   5585:                        return(0);
                   5586:                }
                   5587:                if(mcb->owner == 0 && size < mcb->size) {
                   5588:                        if(available_offset != NULL) {
                   5589:                                *available_offset = offset;
                   5590:                        }
                   5591:                        size = mcb->size;
                   5592:                }
                   5593:                if(mcb->next == 0) {
                   5594:                        break;
                   5595:                }
                   5596:                offset = mcb->next;
                   5597:        }
                   5598:        return(size);
                   5599: }
                   5600: 
                   5601: #endif
                   5602: 
1.1       root     5603: // environment
                   5604: 
                   5605: void msdos_env_set_argv(int env_seg, char *argv)
                   5606: {
                   5607:        char *dst = (char *)(mem + (env_seg << 4));
                   5608:        
                   5609:        while(1) {
                   5610:                if(dst[0] == 0) {
                   5611:                        break;
                   5612:                }
                   5613:                dst += strlen(dst) + 1;
                   5614:        }
                   5615:        *dst++ = 0; // end of environment
                   5616:        *dst++ = 1; // top of argv[0]
                   5617:        *dst++ = 0;
                   5618:        memcpy(dst, argv, strlen(argv));
                   5619:        dst += strlen(argv);
                   5620:        *dst++ = 0;
                   5621:        *dst++ = 0;
                   5622: }
                   5623: 
                   5624: char *msdos_env_get_argv(int env_seg)
                   5625: {
                   5626:        static char env[ENV_SIZE];
                   5627:        char *src = env;
                   5628:        
                   5629:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   5630:        while(1) {
                   5631:                if(src[0] == 0) {
                   5632:                        if(src[1] == 1) {
                   5633:                                return(src + 3);
                   5634:                        }
                   5635:                        break;
                   5636:                }
                   5637:                src += strlen(src) + 1;
                   5638:        }
                   5639:        return(NULL);
                   5640: }
                   5641: 
                   5642: char *msdos_env_get(int env_seg, const char *name)
                   5643: {
                   5644:        static char env[ENV_SIZE];
                   5645:        char *src = env;
                   5646:        
                   5647:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   5648:        while(1) {
                   5649:                if(src[0] == 0) {
                   5650:                        break;
                   5651:                }
                   5652:                int len = strlen(src);
                   5653:                char *n = my_strtok(src, "=");
                   5654:                char *v = src + strlen(n) + 1;
                   5655:                
                   5656:                if(_stricmp(name, n) == 0) {
                   5657:                        return(v);
                   5658:                }
                   5659:                src += len + 1;
                   5660:        }
                   5661:        return(NULL);
                   5662: }
                   5663: 
                   5664: void msdos_env_set(int env_seg, char *name, char *value)
                   5665: {
                   5666:        char env[ENV_SIZE];
                   5667:        char *src = env;
                   5668:        char *dst = (char *)(mem + (env_seg << 4));
                   5669:        char *argv = msdos_env_get_argv(env_seg);
                   5670:        int done = 0;
                   5671:        
                   5672:        memcpy(src, dst, ENV_SIZE);
                   5673:        memset(dst, 0, ENV_SIZE);
                   5674:        while(1) {
                   5675:                if(src[0] == 0) {
                   5676:                        break;
                   5677:                }
                   5678:                int len = strlen(src);
                   5679:                char *n = my_strtok(src, "=");
                   5680:                char *v = src + strlen(n) + 1;
                   5681:                char tmp[1024];
                   5682:                
                   5683:                if(_stricmp(name, n) == 0) {
                   5684:                        sprintf(tmp, "%s=%s", n, value);
                   5685:                        done = 1;
                   5686:                } else {
                   5687:                        sprintf(tmp, "%s=%s", n, v);
                   5688:                }
                   5689:                memcpy(dst, tmp, strlen(tmp));
                   5690:                dst += strlen(tmp) + 1;
                   5691:                src += len + 1;
                   5692:        }
                   5693:        if(!done) {
                   5694:                char tmp[1024];
                   5695:                
                   5696:                sprintf(tmp, "%s=%s", name, value);
                   5697:                memcpy(dst, tmp, strlen(tmp));
                   5698:                dst += strlen(tmp) + 1;
                   5699:        }
                   5700:        if(argv) {
                   5701:                *dst++ = 0; // end of environment
                   5702:                *dst++ = 1; // top of argv[0]
                   5703:                *dst++ = 0;
                   5704:                memcpy(dst, argv, strlen(argv));
                   5705:                dst += strlen(argv);
                   5706:                *dst++ = 0;
                   5707:                *dst++ = 0;
                   5708:        }
                   5709: }
                   5710: 
                   5711: // process
                   5712: 
1.1.1.8   root     5713: psp_t *msdos_psp_create(int psp_seg, UINT16 mcb_seg, UINT16 parent_psp, UINT16 env_seg)
1.1       root     5714: {
                   5715:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   5716:        
                   5717:        memset(psp, 0, PSP_SIZE);
                   5718:        psp->exit[0] = 0xcd;
                   5719:        psp->exit[1] = 0x20;
1.1.1.8   root     5720:        psp->first_mcb = mcb_seg;
1.1       root     5721:        psp->far_call = 0xea;
                   5722:        psp->cpm_entry.w.l = 0xfff1;    // int 21h, retf
                   5723:        psp->cpm_entry.w.h = 0xf000;
                   5724:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   5725:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   5726:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   5727:        psp->parent_psp = parent_psp;
1.1.1.20  root     5728:        if(parent_psp == (UINT16)-1) {
                   5729:                for(int i = 0; i < 20; i++) {
                   5730:                        if(file_handler[i].valid) {
                   5731:                                psp->file_table[i] = i;
                   5732:                        } else {
                   5733:                                psp->file_table[i] = 0xff;
                   5734:                        }
1.1       root     5735:                }
1.1.1.20  root     5736:        } else {
                   5737:                memcpy(psp->file_table, ((psp_t *)(mem + (parent_psp << 4)))->file_table, 20);
1.1       root     5738:        }
                   5739:        psp->env_seg = env_seg;
                   5740:        psp->stack.w.l = REG16(SP);
1.1.1.3   root     5741:        psp->stack.w.h = SREG(SS);
1.1.1.14  root     5742:        psp->file_table_size = 20;
                   5743:        psp->file_table_ptr.w.l = 0x18;
                   5744:        psp->file_table_ptr.w.h = psp_seg;
1.1       root     5745:        psp->service[0] = 0xcd;
                   5746:        psp->service[1] = 0x21;
                   5747:        psp->service[2] = 0xcb;
                   5748:        return(psp);
                   5749: }
                   5750: 
1.1.1.20  root     5751: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg)
                   5752: {
                   5753:        if(psp_seg && fd < 20) {
                   5754:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   5755:                psp->file_table[fd] = value;
                   5756:        }
                   5757: }
                   5758: 
                   5759: int msdos_psp_get_file_table(int fd, int psp_seg)
                   5760: {
                   5761:        if(psp_seg && fd < 20) {
                   5762:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   5763:                fd = psp->file_table[fd];
                   5764:        }
                   5765:        return fd;
                   5766: }
                   5767: 
1.1       root     5768: int msdos_process_exec(char *cmd, param_block_t *param, UINT8 al)
                   5769: {
                   5770:        // load command file
                   5771:        int fd = -1;
                   5772:        int dos_command = 0;
1.1.1.24  root     5773:        char command[MAX_PATH], path[MAX_PATH], opt[MAX_PATH], *name = NULL, name_tmp[MAX_PATH];
1.1.1.38! root     5774:        char pipe_stdin_path[MAX_PATH] = {0};
        !          5775:        char pipe_stdout_path[MAX_PATH] = {0};
1.1       root     5776:        
                   5777:        int opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   5778:        int opt_len = mem[opt_ofs];
                   5779:        memset(opt, 0, sizeof(opt));
                   5780:        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   5781:        
1.1.1.14  root     5782:        if(strlen(cmd) >= 5 && _stricmp(&cmd[strlen(cmd) - 4], ".BAT") == 0) {
                   5783:                // this is a batch file, run command.com
                   5784:                char tmp[MAX_PATH];
                   5785:                if(opt_len != 0) {
                   5786:                        sprintf(tmp, "/C %s %s", cmd, opt);
                   5787:                } else {
                   5788:                        sprintf(tmp, "/C %s", cmd);
                   5789:                }
                   5790:                strcpy(opt, tmp);
                   5791:                opt_len = strlen(opt);
                   5792:                mem[opt_ofs] = opt_len;
                   5793:                sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   5794:                strcpy(command, comspec_path);
                   5795:                strcpy(name_tmp, "COMMAND.COM");
                   5796:        } else {
                   5797:                if(_stricmp(cmd, "C:\\COMMAND.COM") == 0) {
                   5798:                        // redirect C:\COMMAND.COM to comspec_path
                   5799:                        strcpy(command, comspec_path);
                   5800:                } else {
                   5801:                        strcpy(command, cmd);
                   5802:                }
1.1.1.24  root     5803:                if(GetFullPathName(command, MAX_PATH, path, &name) == 0) {
                   5804:                        return(-1);
                   5805:                }
1.1.1.14  root     5806:                memset(name_tmp, 0, sizeof(name_tmp));
                   5807:                strcpy(name_tmp, name);
                   5808:                
                   5809:                // check command.com
1.1.1.38! root     5810:                if((_stricmp(name, "COMMAND.COM") == 0 || _stricmp(name, "COMMAND") == 0) && _access(comspec_path, 0) != 0) {
        !          5811:                        // we can not load command.com, so run program directly if "command /c (program)" is specified
1.1.1.14  root     5812:                        if(opt_len == 0) {
                   5813: //                             process_t *current_process = msdos_process_info_get(current_psp);
                   5814:                                process_t *current_process = NULL;
                   5815:                                for(int i = 0; i < MAX_PROCESS; i++) {
                   5816:                                        if(process[i].psp == current_psp) {
                   5817:                                                current_process = &process[i];
                   5818:                                                break;
                   5819:                                        }
                   5820:                                }
                   5821:                                if(current_process != NULL) {
                   5822:                                        param->cmd_line.dw = current_process->dta.dw;
                   5823:                                        opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   5824:                                        opt_len = mem[opt_ofs];
                   5825:                                        memset(opt, 0, sizeof(opt));
                   5826:                                        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   5827:                                }
                   5828:                        }
                   5829:                        for(int i = 0; i < opt_len; i++) {
                   5830:                                if(opt[i] == ' ') {
                   5831:                                        continue;
                   5832:                                }
                   5833:                                if(opt[i] == '/' && (opt[i + 1] == 'c' || opt[i + 1] == 'C') && opt[i + 2] == ' ') {
                   5834:                                        for(int j = i + 3; j < opt_len; j++) {
                   5835:                                                if(opt[j] == ' ') {
                   5836:                                                        continue;
                   5837:                                                }
                   5838:                                                char *token = my_strtok(opt + j, " ");
                   5839:                                                
1.1.1.38! root     5840:                                                strcpy(command, token);
        !          5841:                                                char tmp[MAX_PATH];
        !          5842:                                                strcpy(tmp, token + strlen(token) + 1);
        !          5843:                                                strcpy(opt, tmp);
        !          5844:                                                
        !          5845:                                                if(al == 0x00) {
        !          5846:                                                        if((token = strstr(opt, "<")) != NULL) {
        !          5847:                                                                token++;
        !          5848:                                                                while(*token == ' ') {
        !          5849:                                                                        token++;
        !          5850:                                                                }
        !          5851:                                                                char *ptr = token;
        !          5852:                                                                while(*ptr != ' ' && *ptr != '\0') {
        !          5853:                                                                        ptr++;
        !          5854:                                                                }
        !          5855:                                                                *ptr = '\0';
        !          5856:                                                                strcpy(pipe_stdin_path, token);
        !          5857:                                                                strcpy(opt, tmp);
        !          5858:                                                        }
        !          5859:                                                        if((token = strstr(opt, ">")) != NULL) {
        !          5860:                                                                token++;
        !          5861:                                                                while(*token == ' ') {
        !          5862:                                                                        token++;
        !          5863:                                                                }
        !          5864:                                                                char *ptr = token;
        !          5865:                                                                while(*ptr != ' ' && *ptr != '\0') {
        !          5866:                                                                        ptr++;
        !          5867:                                                                }
        !          5868:                                                                *ptr = '\0';
        !          5869:                                                                strcpy(pipe_stdout_path, token);
        !          5870:                                                                strcpy(opt, tmp);
        !          5871:                                                        }
        !          5872:                                                        if((token = strstr(opt, "<")) != NULL) {
        !          5873:                                                                *token = '\0';
        !          5874:                                                        }
        !          5875:                                                        if((token = strstr(opt, ">")) != NULL) {
        !          5876:                                                                *token = '\0';
        !          5877:                                                        }
1.1.1.14  root     5878:                                                }
1.1.1.38! root     5879:                                                opt_len = strlen(opt);
        !          5880:                                                mem[opt_ofs] = opt_len;
        !          5881:                                                sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
        !          5882:                                                dos_command = 1;
1.1.1.14  root     5883:                                                break;
1.1       root     5884:                                        }
                   5885:                                }
1.1.1.14  root     5886:                                break;
1.1       root     5887:                        }
                   5888:                }
                   5889:        }
                   5890:        
                   5891:        // load command file
                   5892:        strcpy(path, command);
                   5893:        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   5894:                sprintf(path, "%s.COM", command);
                   5895:                if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   5896:                        sprintf(path, "%s.EXE", command);
                   5897:                        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
1.1.1.14  root     5898:                                sprintf(path, "%s.BAT", command);
                   5899:                                if(_access(path, 0) == 0) {
                   5900:                                        // this is a batch file, run command.com
                   5901:                                        char tmp[MAX_PATH];
                   5902:                                        if(opt_len != 0) {
                   5903:                                                sprintf(tmp, "/C %s %s", path, opt);
                   5904:                                        } else {
                   5905:                                                sprintf(tmp, "/C %s", path);
                   5906:                                        }
                   5907:                                        strcpy(opt, tmp);
                   5908:                                        opt_len = strlen(opt);
                   5909:                                        mem[opt_ofs] = opt_len;
                   5910:                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   5911:                                        strcpy(path, comspec_path);
                   5912:                                        strcpy(name_tmp, "COMMAND.COM");
                   5913:                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   5914:                                } else {
                   5915:                                        // search path in parent environments
                   5916:                                        psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
                   5917:                                        char *env = msdos_env_get(parent_psp->env_seg, "PATH");
                   5918:                                        if(env != NULL) {
                   5919:                                                char env_path[4096];
                   5920:                                                strcpy(env_path, env);
                   5921:                                                char *token = my_strtok(env_path, ";");
                   5922:                                                
                   5923:                                                while(token != NULL) {
                   5924:                                                        if(strlen(token) != 0) {
                   5925:                                                                sprintf(path, "%s", msdos_combine_path(token, command));
                   5926:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   5927:                                                                        break;
                   5928:                                                                }
                   5929:                                                                sprintf(path, "%s.COM", msdos_combine_path(token, command));
                   5930:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   5931:                                                                        break;
                   5932:                                                                }
                   5933:                                                                sprintf(path, "%s.EXE", msdos_combine_path(token, command));
                   5934:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   5935:                                                                        break;
                   5936:                                                                }
                   5937:                                                                sprintf(path, "%s.BAT", msdos_combine_path(token, command));
                   5938:                                                                if(_access(path, 0) == 0) {
                   5939:                                                                        // this is a batch file, run command.com
                   5940:                                                                        char tmp[MAX_PATH];
                   5941:                                                                        if(opt_len != 0) {
                   5942:                                                                                sprintf(tmp, "/C %s %s", path, opt);
                   5943:                                                                        } else {
                   5944:                                                                                sprintf(tmp, "/C %s", path);
                   5945:                                                                        }
                   5946:                                                                        strcpy(opt, tmp);
                   5947:                                                                        opt_len = strlen(opt);
                   5948:                                                                        mem[opt_ofs] = opt_len;
                   5949:                                                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   5950:                                                                        strcpy(path, comspec_path);
                   5951:                                                                        strcpy(name_tmp, "COMMAND.COM");
                   5952:                                                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   5953:                                                                        break;
                   5954:                                                                }
1.1.1.8   root     5955:                                                        }
1.1.1.14  root     5956:                                                        token = my_strtok(NULL, ";");
1.1       root     5957:                                                }
                   5958:                                        }
                   5959:                                }
                   5960:                        }
                   5961:                }
                   5962:        }
                   5963:        if(fd == -1) {
1.1.1.38! root     5964:                // we can not find command.com in the path, so open comspec_path
        !          5965:                if(_stricmp(command, "COMMAND.COM") == 0 || _stricmp(command, "COMMAND") == 0) {
        !          5966:                        strcpy(command, comspec_path);
        !          5967:                        strcpy(path, command);
        !          5968:                        fd = _open(path, _O_RDONLY | _O_BINARY);
        !          5969:                }
        !          5970:        }
        !          5971:        if(fd == -1) {
1.1       root     5972:                if(dos_command) {
                   5973:                        // may be dos command
                   5974:                        char tmp[MAX_PATH];
                   5975:                        sprintf(tmp, "%s %s", command, opt);
                   5976:                        system(tmp);
                   5977:                        return(0);
                   5978:                } else {
                   5979:                        return(-1);
                   5980:                }
                   5981:        }
                   5982:        _read(fd, file_buffer, sizeof(file_buffer));
                   5983:        _close(fd);
                   5984:        
                   5985:        // copy environment
1.1.1.29  root     5986:        int umb_linked, env_seg, psp_seg;
1.1       root     5987:        
1.1.1.29  root     5988:        if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   5989:                msdos_mem_unlink_umb();
                   5990:        }
1.1.1.8   root     5991:        if((env_seg = msdos_mem_alloc(first_mcb, ENV_SIZE >> 4, 1)) == -1) {
1.1.1.29  root     5992:                if((env_seg = msdos_mem_alloc(UMB_TOP >> 4, ENV_SIZE >> 4, 1)) == -1) {
                   5993:                        if(umb_linked != 0) {
                   5994:                                msdos_mem_link_umb();
                   5995:                        }
                   5996:                        return(-1);
                   5997:                }
1.1       root     5998:        }
                   5999:        if(param->env_seg == 0) {
                   6000:                psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
                   6001:                memcpy(mem + (env_seg << 4), mem + (parent_psp->env_seg << 4), ENV_SIZE);
                   6002:        } else {
                   6003:                memcpy(mem + (env_seg << 4), mem + (param->env_seg << 4), ENV_SIZE);
                   6004:        }
                   6005:        msdos_env_set_argv(env_seg, msdos_short_full_path(path));
                   6006:        
                   6007:        // check exe header
                   6008:        exe_header_t *header = (exe_header_t *)file_buffer;
1.1.1.8   root     6009:        int paragraphs, free_paragraphs = msdos_mem_get_free(first_mcb, 1);
1.1       root     6010:        UINT16 cs, ss, ip, sp;
                   6011:        
                   6012:        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   6013:                // memory allocation
                   6014:                int header_size = header->header_size * 16;
                   6015:                int load_size = header->pages * 512 - header_size;
                   6016:                if(header_size + load_size < 512) {
                   6017:                        load_size = 512 - header_size;
                   6018:                }
                   6019:                paragraphs = (PSP_SIZE + load_size) >> 4;
                   6020:                if(paragraphs + header->min_alloc > free_paragraphs) {
                   6021:                        msdos_mem_free(env_seg);
                   6022:                        return(-1);
                   6023:                }
                   6024:                paragraphs += header->max_alloc ? header->max_alloc : header->min_alloc;
                   6025:                if(paragraphs > free_paragraphs) {
                   6026:                        paragraphs = free_paragraphs;
                   6027:                }
1.1.1.8   root     6028:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29  root     6029:                        if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
                   6030:                                if(umb_linked != 0) {
                   6031:                                        msdos_mem_link_umb();
                   6032:                                }
                   6033:                                msdos_mem_free(env_seg);
                   6034:                                return(-1);
                   6035:                        }
1.1       root     6036:                }
                   6037:                // relocation
                   6038:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   6039:                for(int i = 0; i < header->relocations; i++) {
                   6040:                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   6041:                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   6042:                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   6043:                }
                   6044:                memcpy(mem + (start_seg << 4), file_buffer + header_size, load_size);
                   6045:                // segments
                   6046:                cs = header->init_cs + start_seg;
                   6047:                ss = header->init_ss + start_seg;
                   6048:                ip = header->init_ip;
                   6049:                sp = header->init_sp - 2; // for symdeb
                   6050:        } else {
                   6051:                // memory allocation
                   6052:                paragraphs = free_paragraphs;
1.1.1.8   root     6053:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29  root     6054:                        if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
                   6055:                                if(umb_linked != 0) {
                   6056:                                        msdos_mem_link_umb();
                   6057:                                }
                   6058:                                msdos_mem_free(env_seg);
                   6059:                                return(-1);
                   6060:                        }
1.1       root     6061:                }
                   6062:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   6063:                memcpy(mem + (start_seg << 4), file_buffer, 0x10000 - PSP_SIZE);
                   6064:                // segments
                   6065:                cs = ss = psp_seg;
                   6066:                ip = 0x100;
                   6067:                sp = 0xfffe;
                   6068:        }
1.1.1.29  root     6069:        if(umb_linked != 0) {
                   6070:                msdos_mem_link_umb();
                   6071:        }
1.1       root     6072:        
                   6073:        // create psp
1.1.1.3   root     6074:        *(UINT16 *)(mem + 4 * 0x22 + 0) = m_eip;
                   6075:        *(UINT16 *)(mem + 4 * 0x22 + 2) = SREG(CS);
1.1       root     6076:        psp_t *psp = msdos_psp_create(psp_seg, psp_seg + paragraphs, current_psp, env_seg);
                   6077:        memcpy(psp->fcb1, mem + (param->fcb1.w.h << 4) + param->fcb1.w.l, sizeof(psp->fcb1));
                   6078:        memcpy(psp->fcb2, mem + (param->fcb2.w.h << 4) + param->fcb2.w.l, sizeof(psp->fcb2));
                   6079:        memcpy(psp->buffer, mem + (param->cmd_line.w.h << 4) + param->cmd_line.w.l, sizeof(psp->buffer));
                   6080:        
                   6081:        mcb_t *mcb_env = (mcb_t *)(mem + ((env_seg - 1) << 4));
                   6082:        mcb_t *mcb_psp = (mcb_t *)(mem + ((psp_seg - 1) << 4));
                   6083:        mcb_psp->psp = mcb_env->psp = psp_seg;
                   6084:        
1.1.1.4   root     6085:        for(int i = 0; i < 8; i++) {
                   6086:                if(name_tmp[i] == '.') {
                   6087:                        mcb_psp->prog_name[i] = '\0';
                   6088:                        break;
                   6089:                } else if(i < 7 && msdos_lead_byte_check(name_tmp[i])) {
                   6090:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6091:                        i++;
                   6092:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6093:                } else if(name_tmp[i] >= 'a' && name_tmp[i] <= 'z') {
                   6094:                        mcb_psp->prog_name[i] = name_tmp[i] - 'a' + 'A';
                   6095:                } else {
                   6096:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6097:                }
                   6098:        }
                   6099:        
1.1       root     6100:        // process info
                   6101:        process_t *process = msdos_process_info_create(psp_seg);
                   6102:        strcpy(process->module_dir, msdos_short_full_dir(path));
1.1.1.33  root     6103: #ifdef USE_DEBUGGER
                   6104:        strcpy(process->module_path, path);
                   6105: #endif
1.1       root     6106:        process->dta.w.l = 0x80;
                   6107:        process->dta.w.h = psp_seg;
                   6108:        process->switchar = '/';
                   6109:        process->max_files = 20;
                   6110:        process->parent_int_10h_feh_called = int_10h_feh_called;
                   6111:        process->parent_int_10h_ffh_called = int_10h_ffh_called;
1.1.1.14  root     6112:        process->parent_ds = SREG(DS);
1.1.1.31  root     6113:        process->parent_es = SREG(ES);
1.1       root     6114:        
                   6115:        current_psp = psp_seg;
1.1.1.23  root     6116:        msdos_sda_update(current_psp);
1.1       root     6117:        
                   6118:        if(al == 0x00) {
                   6119:                int_10h_feh_called = int_10h_ffh_called = false;
                   6120:                
1.1.1.38! root     6121:                // pipe
        !          6122:                if(pipe_stdin_path[0] != '\0') {
        !          6123:                        if(pipe_stdin_path[strlen(pipe_stdin_path) - 1] == 0x0d) {
        !          6124:                                pipe_stdin_path[strlen(pipe_stdin_path) - 1] = '\0';
        !          6125:                        }
        !          6126:                        if((fd = _open(pipe_stdin_path, _O_RDONLY | _O_BINARY)) != -1) {
        !          6127:                                UINT16 info = msdos_drive_number(pipe_stdin_path);
        !          6128:                                msdos_file_handler_open(fd, pipe_stdin_path, _isatty(fd), 0, info, current_psp);
        !          6129:                                psp->file_table[0] = fd;
        !          6130:                                msdos_psp_set_file_table(fd, fd, current_psp);
        !          6131:                        }
        !          6132:                }
        !          6133:                if(pipe_stdout_path[0] != '\0') {
        !          6134:                        // LIST.COM specifies the file path with '\r'
        !          6135:                        if(pipe_stdout_path[strlen(pipe_stdout_path) - 1] == 0x0d) {
        !          6136:                                pipe_stdout_path[strlen(pipe_stdout_path) - 1] = '\0';
        !          6137:                        }
        !          6138:                        if(_access(pipe_stdout_path, 0) == 0) {
        !          6139:                                SetFileAttributes(pipe_stdout_path, FILE_ATTRIBUTE_NORMAL);
        !          6140:                                DeleteFile(pipe_stdout_path);
        !          6141:                        }
        !          6142:                        if((fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT)) != -1) {
        !          6143:                                UINT16 info = msdos_drive_number(pipe_stdout_path);
        !          6144:                                msdos_file_handler_open(fd, pipe_stdout_path, _isatty(fd), 1, info, current_psp);
        !          6145:                                psp->file_table[1] = fd;
        !          6146:                                msdos_psp_set_file_table(fd, fd, current_psp);
        !          6147:                        }
        !          6148:                }
        !          6149:                
1.1       root     6150:                // registers and segments
                   6151:                REG16(AX) = REG16(BX) = 0x00;
                   6152:                REG16(CX) = 0xff;
                   6153:                REG16(DX) = psp_seg;
                   6154:                REG16(SI) = ip;
                   6155:                REG16(DI) = sp;
                   6156:                REG16(SP) = sp;
1.1.1.3   root     6157:                SREG(DS) = SREG(ES) = psp_seg;
                   6158:                SREG(SS) = ss;
                   6159:                i386_load_segment_descriptor(DS);
                   6160:                i386_load_segment_descriptor(ES);
                   6161:                i386_load_segment_descriptor(SS);
1.1       root     6162:                
                   6163:                *(UINT16 *)(mem + (ss << 4) + sp) = 0;
                   6164:                i386_jmp_far(cs, ip);
                   6165:        } else if(al == 0x01) {
                   6166:                // copy ss:sp and cs:ip to param block
                   6167:                param->sp = sp;
                   6168:                param->ss = ss;
                   6169:                param->ip = ip;
                   6170:                param->cs = cs;
1.1.1.31  root     6171:                
                   6172:                // the AX value to be passed to the child program is put on top of the child's stack
                   6173:                *(UINT16 *)(mem + (ss << 4) + sp) = REG16(AX);
1.1       root     6174:        }
                   6175:        return(0);
                   6176: }
                   6177: 
                   6178: void msdos_process_terminate(int psp_seg, int ret, int mem_free)
                   6179: {
                   6180:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6181:        
                   6182:        *(UINT32 *)(mem + 4 * 0x22) = psp->int_22h.dw;
                   6183:        *(UINT32 *)(mem + 4 * 0x23) = psp->int_23h.dw;
                   6184:        *(UINT32 *)(mem + 4 * 0x24) = psp->int_24h.dw;
                   6185:        
1.1.1.3   root     6186:        SREG(SS) = psp->stack.w.h;
                   6187:        i386_load_segment_descriptor(SS);
1.1       root     6188:        REG16(SP) = psp->stack.w.l;
                   6189:        i386_jmp_far(psp->int_22h.w.h, psp->int_22h.w.l);
                   6190:        
1.1.1.28  root     6191: //     process_t *current_process = msdos_process_info_get(psp_seg);
                   6192:        process_t *current_process = NULL;
                   6193:        for(int i = 0; i < MAX_PROCESS; i++) {
                   6194:                if(process[i].psp == psp_seg) {
                   6195:                        current_process = &process[i];
                   6196:                        break;
                   6197:                }
                   6198:        }
                   6199:        if(current_process == NULL) {
                   6200:                throw(0x1f); // general failure
                   6201:        }
                   6202:        int_10h_feh_called = current_process->parent_int_10h_feh_called;
                   6203:        int_10h_ffh_called = current_process->parent_int_10h_ffh_called;
                   6204:        if(current_process->called_by_int2eh) {
                   6205:                REG16(AX) = ret;
                   6206:        }
                   6207:        SREG(DS) = current_process->parent_ds;
1.1.1.31  root     6208:        SREG(ES) = current_process->parent_es;
1.1.1.14  root     6209:        i386_load_segment_descriptor(DS);
1.1.1.31  root     6210:        i386_load_segment_descriptor(ES);
1.1       root     6211:        
                   6212:        if(mem_free) {
1.1.1.8   root     6213:                int mcb_seg;
                   6214:                while((mcb_seg = msdos_mem_get_last_mcb(first_mcb, psp_seg)) != -1) {
                   6215:                        msdos_mem_free(mcb_seg + 1);
                   6216:                }
                   6217:                while((mcb_seg = msdos_mem_get_last_mcb(UMB_TOP >> 4, psp_seg)) != -1) {
                   6218:                        msdos_mem_free(mcb_seg + 1);
                   6219:                }
1.1       root     6220:                
                   6221:                for(int i = 0; i < MAX_FILES; i++) {
                   6222:                        if(file_handler[i].valid && file_handler[i].psp == psp_seg) {
                   6223:                                _close(i);
1.1.1.20  root     6224:                                msdos_file_handler_close(i);
                   6225:                                msdos_psp_set_file_table(i, 0x0ff, psp_seg); // FIXME: consider duplicated file handles
1.1       root     6226:                        }
                   6227:                }
1.1.1.13  root     6228:                msdos_dta_info_free(psp_seg);
1.1       root     6229:        }
1.1.1.14  root     6230:        msdos_stdio_reopen();
1.1       root     6231:        
1.1.1.28  root     6232:        memset(current_process, 0, sizeof(process_t));
1.1       root     6233:        
                   6234:        current_psp = psp->parent_psp;
                   6235:        retval = ret;
1.1.1.23  root     6236:        msdos_sda_update(current_psp);
1.1       root     6237: }
                   6238: 
                   6239: // drive
                   6240: 
                   6241: int msdos_drive_param_block_update(int drive_num, UINT16 *seg, UINT16 *ofs, int force_update)
                   6242: {
                   6243:        *seg = DPB_TOP >> 4;
                   6244:        *ofs = sizeof(dpb_t) * drive_num;
                   6245:        dpb_t *dpb = (dpb_t *)(mem + (*seg << 4) + *ofs);
                   6246:        
                   6247:        if(!force_update && dpb->free_clusters != 0) {
                   6248:                return(dpb->bytes_per_sector ? 1 : 0);
                   6249:        }
                   6250:        memset(dpb, 0, sizeof(dpb_t));
                   6251:        
                   6252:        int res = 0;
                   6253:        char dev[64];
                   6254:        sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   6255:        
1.1.1.17  root     6256:        HANDLE hFile = CreateFile(dev, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1.1       root     6257:        if(hFile != INVALID_HANDLE_VALUE) {
                   6258:                DISK_GEOMETRY geo;
                   6259:                DWORD dwSize;
                   6260:                if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &geo, sizeof(geo), &dwSize, NULL)) {
                   6261:                        dpb->bytes_per_sector = (UINT16)geo.BytesPerSector;
                   6262:                        dpb->highest_sector_num = (UINT8)(geo.SectorsPerTrack - 1);
                   6263:                        dpb->highest_cluster_num = (UINT16)(geo.TracksPerCylinder * geo.Cylinders.QuadPart + 1);
1.1.1.14  root     6264:                        dpb->maximum_cluster_num = (UINT32)(geo.TracksPerCylinder * geo.Cylinders.QuadPart + 1);
1.1       root     6265:                        switch(geo.MediaType) {
                   6266:                        case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   6267:                                dpb->media_type = 0xff;
                   6268:                                break;
                   6269:                        case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   6270:                                dpb->media_type = 0xfe;
                   6271:                                break;
                   6272:                        case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   6273:                                dpb->media_type = 0xfd;
                   6274:                                break;
                   6275:                        case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   6276:                                dpb->media_type = 0xfc;
                   6277:                                break;
                   6278:                        case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   6279:                        case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   6280:                                dpb->media_type = 0xf9;
                   6281:                                break;
                   6282:                        case FixedMedia:        // hard disk
                   6283:                        case RemovableMedia:
1.1.1.19  root     6284:                        case Unknown:
1.1       root     6285:                                dpb->media_type = 0xf8;
                   6286:                                break;
                   6287:                        default:
                   6288:                                dpb->media_type = 0xf0;
                   6289:                                break;
                   6290:                        }
                   6291:                        res = 1;
                   6292:                }
                   6293:                dpb->drive_num = drive_num;
                   6294:                dpb->unit_num = drive_num;
                   6295:                dpb->next_dpb_ofs = *ofs + sizeof(dpb_t);
                   6296:                dpb->next_dpb_seg = *seg;
1.1.1.14  root     6297:                dpb->info_sector = 0xffff;
                   6298:                dpb->backup_boot_sector = 0xffff;
1.1       root     6299:                dpb->free_clusters = 0xffff;
1.1.1.14  root     6300:                dpb->free_search_cluster = 0xffffffff;
1.1       root     6301:                CloseHandle(hFile);
                   6302:        }
                   6303:        return(res);
                   6304: }
                   6305: 
                   6306: // pc bios
                   6307: 
1.1.1.35  root     6308: #ifdef USE_SERVICE_THREAD
                   6309: void start_service_loop(LPTHREAD_START_ROUTINE lpStartAddress)
                   6310: {
                   6311: #if defined(HAS_I386)
                   6312:        if(m_SF != 0) {
                   6313:                m_SF = 0;
                   6314:                mem[0xfffd0 + 0x15] = 0x79;     // jns -4
                   6315:        } else {
                   6316:                m_SF = 1;
                   6317:                mem[0xfffd0 + 0x15] = 0x78;     // js -4
                   6318:        }
                   6319: #else
                   6320:        if(m_SignVal < 0) {
                   6321:                m_SignVal = 0;
                   6322:                mem[0xfffd0 + 0x15] = 0x79;     // jns -4
                   6323:        } else {
                   6324:                m_SignVal = -1;
                   6325:                mem[0xfffd0 + 0x15] = 0x78;     // js -4
                   6326:        }
                   6327: #endif
                   6328:        i386_call_far(0xfffd, 0x0013);
                   6329:        in_service = true;
                   6330:        service_exit = false;
                   6331:        CloseHandle(CreateThread(NULL, 0, lpStartAddress, NULL, 0, NULL));
                   6332: }
                   6333: 
                   6334: void finish_service_loop()
                   6335: {
                   6336:        if(in_service && service_exit) {
                   6337: #if defined(HAS_I386)
                   6338:                if(m_SF != 0) {
                   6339:                        m_SF = 0;
                   6340:                } else {
                   6341:                        m_SF = 1;
                   6342:                }
                   6343: #else
                   6344:                if(m_SignVal < 0) {
                   6345:                        m_SignVal = 0;
                   6346:                } else {
                   6347:                        m_SignVal = -1;
                   6348:                }
                   6349: #endif
                   6350:                in_service = false;
                   6351:        }
                   6352: }
                   6353: #endif
                   6354: 
1.1.1.19  root     6355: UINT32 get_ticks_since_midnight(UINT32 cur_msec)
                   6356: {
                   6357:        static unsigned __int64 start_msec_since_midnight = 0;
                   6358:        static unsigned __int64 start_msec_since_hostboot = 0;
                   6359:        
                   6360:        if(start_msec_since_midnight == 0) {
                   6361:                SYSTEMTIME time;
                   6362:                GetLocalTime(&time);
                   6363:                start_msec_since_midnight = ((time.wHour * 60 + time.wMinute) * 60 + time.wSecond) * 1000 + time.wMilliseconds;
                   6364:                start_msec_since_hostboot = cur_msec;
                   6365:        }
                   6366:        unsigned __int64 msec = (start_msec_since_midnight + cur_msec - start_msec_since_hostboot) % (24 * 60 * 60 * 1000);
                   6367:        unsigned __int64 tick = msec * 0x1800b0 / (24 * 60 * 60 * 1000);
                   6368:        return (UINT32)tick;
                   6369: }
                   6370: 
                   6371: void pcbios_update_daily_timer_counter(UINT32 cur_msec)
                   6372: {
                   6373:        UINT32 prev_tick = *(UINT32 *)(mem + 0x46c);
                   6374:        UINT32 next_tick = get_ticks_since_midnight(cur_msec);
                   6375:        
                   6376:        if(prev_tick > next_tick) {
                   6377:                mem[0x470] = 1;
                   6378:        }
                   6379:        *(UINT32 *)(mem + 0x46c) = next_tick;
                   6380: }
                   6381: 
1.1.1.14  root     6382: inline void pcbios_irq0()
                   6383: {
                   6384:        //++*(UINT32 *)(mem + 0x46c);
1.1.1.19  root     6385:        pcbios_update_daily_timer_counter(timeGetTime());
1.1.1.14  root     6386: }
                   6387: 
1.1.1.16  root     6388: int pcbios_get_text_vram_address(int page)
1.1       root     6389: {
                   6390:        if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     6391:                return TEXT_VRAM_TOP;
1.1       root     6392:        } else {
1.1.1.14  root     6393:                return TEXT_VRAM_TOP + VIDEO_REGEN * (page % vram_pages);
1.1       root     6394:        }
                   6395: }
                   6396: 
1.1.1.16  root     6397: int pcbios_get_shadow_buffer_address(int page)
1.1       root     6398: {
1.1.1.14  root     6399:        if(!int_10h_feh_called) {
1.1.1.16  root     6400:                return pcbios_get_text_vram_address(page);
1.1.1.14  root     6401:        } else if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     6402:                return SHADOW_BUF_TOP;
                   6403:        } else {
1.1.1.14  root     6404:                return SHADOW_BUF_TOP + VIDEO_REGEN * (page % vram_pages);
1.1.1.8   root     6405:        }
                   6406: }
                   6407: 
1.1.1.16  root     6408: int pcbios_get_shadow_buffer_address(int page, int x, int y)
1.1.1.8   root     6409: {
1.1.1.16  root     6410:        return pcbios_get_shadow_buffer_address(page) + (x + y * scr_width) * 2;
1.1       root     6411: }
                   6412: 
1.1.1.16  root     6413: void pcbios_set_console_size(int width, int height, bool clr_screen)
1.1       root     6414: {
1.1.1.14  root     6415:        // clear the existing screen, not just the new one
                   6416:        int clr_height = max(height, scr_height);
                   6417:        
1.1.1.16  root     6418:        if(scr_width != width || scr_height != height) {
                   6419:                change_console_size(width, height);
1.1.1.14  root     6420:        }
                   6421:        mem[0x462] = 0;
                   6422:        *(UINT16 *)(mem + 0x44e) = 0;
                   6423:        
1.1.1.16  root     6424:        text_vram_top_address = pcbios_get_text_vram_address(0);
                   6425:        text_vram_end_address = text_vram_top_address + width * height * 2;
                   6426:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(0);
                   6427:        shadow_buffer_end_address = shadow_buffer_top_address + width * height * 2;
1.1       root     6428:        
1.1.1.23  root     6429:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.16  root     6430:        if(clr_screen) {
1.1.1.14  root     6431:                for(int ofs = shadow_buffer_top_address; ofs < shadow_buffer_end_address;) {
                   6432:                        mem[ofs++] = 0x20;
                   6433:                        mem[ofs++] = 0x07;
                   6434:                }
                   6435:                
1.1.1.35  root     6436: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6437:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     6438: #endif
1.1.1.14  root     6439:                for(int y = 0; y < clr_height; y++) {
                   6440:                        for(int x = 0; x < scr_width; x++) {
                   6441:                                SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6442:                                SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1       root     6443:                        }
                   6444:                }
                   6445:                SMALL_RECT rect;
1.1.1.14  root     6446:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + clr_height - 1);
                   6447:                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   6448:                vram_length_char = vram_last_length_char = 0;
                   6449:                vram_length_attr = vram_last_length_attr = 0;
1.1.1.35  root     6450: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6451:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     6452: #endif
1.1       root     6453:        }
1.1.1.14  root     6454:        COORD co;
                   6455:        co.X = 0;
                   6456:        co.Y = scr_top;
                   6457:        SetConsoleCursorPosition(hStdout, co);
                   6458:        cursor_moved = true;
                   6459:        SetConsoleTextAttribute(hStdout, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
1.1       root     6460: }
                   6461: 
1.1.1.36  root     6462: void pcbios_update_cursor_position()
                   6463: {
                   6464:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   6465:        GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
                   6466:        if(!restore_console_on_exit) {
                   6467:                scr_top = csbi.srWindow.Top;
                   6468:        }
                   6469:        mem[0x450 + mem[0x462] * 2] = csbi.dwCursorPosition.X;
                   6470:        mem[0x451 + mem[0x462] * 2] = csbi.dwCursorPosition.Y - scr_top;
                   6471: }
                   6472: 
1.1.1.16  root     6473: inline void pcbios_int_10h_00h()
                   6474: {
                   6475:        switch(REG8(AL) & 0x7f) {
                   6476:        case 0x70: // v-text mode
                   6477:        case 0x71: // extended cga v-text mode
                   6478:                pcbios_set_console_size(scr_width, scr_height, !(REG8(AL) & 0x80));
                   6479:                break;
                   6480:        default:
                   6481:                pcbios_set_console_size(80, 25, !(REG8(AL) & 0x80));
                   6482:                break;
                   6483:        }
                   6484:        if(REG8(AL) & 0x80) {
                   6485:                mem[0x487] |= 0x80;
                   6486:        } else {
                   6487:                mem[0x487] &= ~0x80;
                   6488:        }
                   6489:        mem[0x449] = REG8(AL) & 0x7f;
                   6490: }
                   6491: 
1.1       root     6492: inline void pcbios_int_10h_01h()
                   6493: {
1.1.1.13  root     6494:        mem[0x460] = REG8(CL);
                   6495:        mem[0x461] = REG8(CH);
1.1.1.14  root     6496:        
1.1.1.23  root     6497:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6498:        CONSOLE_CURSOR_INFO ci;
                   6499:        GetConsoleCursorInfo(hStdout, &ci);
                   6500: //     ci.bVisible = !(REG8(CH) & 0x60) && REG8(CH) <= REG8(CL);
                   6501: //     if(ci.bVisible) {
                   6502:                int lines = max(8, REG8(CL) + 1);
                   6503:                ci.dwSize = (REG8(CL) - REG8(CH) + 1) * 100 / lines;
                   6504: //     }
                   6505:        SetConsoleCursorInfo(hStdout, &ci);
1.1       root     6506: }
                   6507: 
                   6508: inline void pcbios_int_10h_02h()
                   6509: {
1.1.1.14  root     6510:        // continuously setting the cursor effectively stops it blinking
                   6511:        if(mem[0x462] == REG8(BH) && (REG8(DL) != mem[0x450 + REG8(BH) * 2] || REG8(DH) != mem[0x451 + REG8(BH) * 2])) {
1.1       root     6512:                COORD co;
                   6513:                co.X = REG8(DL);
1.1.1.14  root     6514:                co.Y = REG8(DH) + scr_top;
                   6515:                
                   6516:                // some programs hide the cursor by moving it off screen
                   6517:                static bool hidden = false;
1.1.1.23  root     6518:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6519:                CONSOLE_CURSOR_INFO ci;
                   6520:                GetConsoleCursorInfo(hStdout, &ci);
                   6521:                
                   6522:                if(REG8(DH) >= scr_height || !SetConsoleCursorPosition(hStdout, co)) {
                   6523:                        if(ci.bVisible) {
                   6524:                                ci.bVisible = FALSE;
                   6525: //                             SetConsoleCursorInfo(hStdout, &ci);
                   6526:                                hidden = true;
                   6527:                        }
                   6528:                } else if(hidden) {
                   6529:                        if(!ci.bVisible) {
                   6530:                                ci.bVisible = TRUE;
                   6531: //                             SetConsoleCursorInfo(hStdout, &ci);
                   6532:                        }
                   6533:                        hidden = false;
                   6534:                }
1.1       root     6535:        }
1.1.1.14  root     6536:        mem[0x450 + (REG8(BH) % vram_pages) * 2] = REG8(DL);
                   6537:        mem[0x451 + (REG8(BH) % vram_pages) * 2] = REG8(DH);
1.1       root     6538: }
                   6539: 
                   6540: inline void pcbios_int_10h_03h()
                   6541: {
1.1.1.14  root     6542:        REG8(DL) = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   6543:        REG8(DH) = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     6544:        REG8(CL) = mem[0x460];
                   6545:        REG8(CH) = mem[0x461];
                   6546: }
                   6547: 
                   6548: inline void pcbios_int_10h_05h()
                   6549: {
1.1.1.14  root     6550:        if(REG8(AL) >= vram_pages) {
                   6551:                return;
                   6552:        }
                   6553:        if(mem[0x462] != REG8(AL)) {
                   6554:                vram_flush();
                   6555:                
1.1.1.23  root     6556:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6557:                SMALL_RECT rect;
1.1.1.14  root     6558:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   6559:                ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     6560:                
1.1.1.16  root     6561:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(mem[0x462]); y < scr_height; y++) {
1.1.1.14  root     6562:                        for(int x = 0; x < scr_width; x++) {
                   6563:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   6564:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     6565:                        }
                   6566:                }
1.1.1.16  root     6567:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(REG8(AL)); y < scr_height; y++) {
1.1.1.14  root     6568:                        for(int x = 0; x < scr_width; x++) {
                   6569:                                SCR_BUF(y,x).Char.AsciiChar = mem[ofs++];
                   6570:                                SCR_BUF(y,x).Attributes = mem[ofs++];
1.1       root     6571:                        }
                   6572:                }
1.1.1.14  root     6573:                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     6574:                
                   6575:                COORD co;
1.1.1.14  root     6576:                co.X = mem[0x450 + REG8(AL) * 2];
                   6577:                co.Y = mem[0x451 + REG8(AL) * 2] + scr_top;
                   6578:                if(co.Y < scr_top + scr_height) {
                   6579:                        SetConsoleCursorPosition(hStdout, co);
                   6580:                }
1.1       root     6581:        }
1.1.1.14  root     6582:        mem[0x462] = REG8(AL);
                   6583:        *(UINT16 *)(mem + 0x44e) = REG8(AL) * VIDEO_REGEN;
                   6584:        int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.16  root     6585:        text_vram_top_address = pcbios_get_text_vram_address(mem[0x462]);
1.1.1.14  root     6586:        text_vram_end_address = text_vram_top_address + regen;
1.1.1.16  root     6587:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(mem[0x462]);
1.1.1.14  root     6588:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
1.1       root     6589: }
                   6590: 
                   6591: inline void pcbios_int_10h_06h()
                   6592: {
1.1.1.14  root     6593:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   6594:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   6595:                return;
                   6596:        }
                   6597:        vram_flush();
                   6598:        
1.1.1.23  root     6599:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6600:        SMALL_RECT rect;
1.1.1.14  root     6601:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   6602:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   6603:        
                   6604:        int right = min(REG8(DL), scr_width - 1);
                   6605:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     6606:        
                   6607:        if(REG8(AL) == 0) {
1.1.1.14  root     6608:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     6609:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     6610:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6611:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     6612:                        }
                   6613:                }
                   6614:        } else {
1.1.1.14  root     6615:                for(int y = REG8(CH), y2 = min(REG8(CH) + REG8(AL), bottom + 1); y <= bottom; y++, y2++) {
1.1.1.16  root     6616:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     6617:                                if(y2 <= bottom) {
                   6618:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     6619:                                } else {
1.1.1.14  root     6620:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6621:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     6622:                                }
1.1.1.14  root     6623:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   6624:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     6625:                        }
                   6626:                }
                   6627:        }
1.1.1.14  root     6628:        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     6629: }
                   6630: 
                   6631: inline void pcbios_int_10h_07h()
                   6632: {
1.1.1.14  root     6633:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   6634:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   6635:                return;
                   6636:        }
                   6637:        vram_flush();
                   6638:        
1.1.1.23  root     6639:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6640:        SMALL_RECT rect;
1.1.1.14  root     6641:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   6642:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   6643:        
                   6644:        int right = min(REG8(DL), scr_width - 1);
                   6645:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     6646:        
                   6647:        if(REG8(AL) == 0) {
1.1.1.14  root     6648:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     6649:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     6650:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6651:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     6652:                        }
                   6653:                }
                   6654:        } else {
1.1.1.14  root     6655:                for(int y = bottom, y2 = max(REG8(CH) - 1, bottom - REG8(AL)); y >= REG8(CH); y--, y2--) {
1.1.1.16  root     6656:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     6657:                                if(y2 >= REG8(CH)) {
                   6658:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     6659:                                } else {
1.1.1.14  root     6660:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6661:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     6662:                                }
1.1.1.14  root     6663:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   6664:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     6665:                        }
                   6666:                }
                   6667:        }
1.1.1.14  root     6668:        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     6669: }
                   6670: 
                   6671: inline void pcbios_int_10h_08h()
                   6672: {
                   6673:        COORD co;
                   6674:        DWORD num;
                   6675:        
1.1.1.14  root     6676:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   6677:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     6678:        
                   6679:        if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     6680:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6681:                co.Y += scr_top;
                   6682:                vram_flush();
1.1       root     6683:                ReadConsoleOutputCharacter(hStdout, scr_char, 1, co, &num);
                   6684:                ReadConsoleOutputAttribute(hStdout, scr_attr, 1, co, &num);
                   6685:                REG8(AL) = scr_char[0];
                   6686:                REG8(AH) = scr_attr[0];
                   6687:        } else {
1.1.1.16  root     6688:                REG16(AX) = *(UINT16 *)(mem + pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y));
1.1       root     6689:        }
                   6690: }
                   6691: 
                   6692: inline void pcbios_int_10h_09h()
                   6693: {
                   6694:        COORD co;
                   6695:        
1.1.1.14  root     6696:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   6697:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   6698:        
1.1.1.16  root     6699:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   6700:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     6701:        
                   6702:        if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     6703: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6704:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     6705: #endif
1.1.1.16  root     6706:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     6707:                while(dest < end) {
                   6708:                        write_text_vram_char(dest - vram, REG8(AL));
                   6709:                        mem[dest++] = REG8(AL);
                   6710:                        write_text_vram_attr(dest - vram, REG8(BL));
                   6711:                        mem[dest++] = REG8(BL);
1.1       root     6712:                }
1.1.1.35  root     6713: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6714:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     6715: #endif
1.1       root     6716:        } else {
1.1.1.14  root     6717:                while(dest < end) {
1.1       root     6718:                        mem[dest++] = REG8(AL);
                   6719:                        mem[dest++] = REG8(BL);
                   6720:                }
                   6721:        }
                   6722: }
                   6723: 
                   6724: inline void pcbios_int_10h_0ah()
                   6725: {
                   6726:        COORD co;
                   6727:        
1.1.1.14  root     6728:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   6729:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   6730:        
1.1.1.16  root     6731:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   6732:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     6733:        
                   6734:        if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     6735: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6736:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     6737: #endif
1.1.1.16  root     6738:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     6739:                while(dest < end) {
                   6740:                        write_text_vram_char(dest - vram, REG8(AL));
                   6741:                        mem[dest++] = REG8(AL);
                   6742:                        dest++;
1.1       root     6743:                }
1.1.1.35  root     6744: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6745:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     6746: #endif
1.1       root     6747:        } else {
1.1.1.14  root     6748:                while(dest < end) {
1.1       root     6749:                        mem[dest++] = REG8(AL);
                   6750:                        dest++;
                   6751:                }
                   6752:        }
                   6753: }
                   6754: 
                   6755: inline void pcbios_int_10h_0eh()
                   6756: {
1.1.1.14  root     6757:        DWORD num;
                   6758:        COORD co;
                   6759:        
                   6760:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   6761:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   6762:        
                   6763:        if(REG8(AL) == 7) {
                   6764:                //MessageBeep(-1);
                   6765:        } else if(REG8(AL) == 8 || REG8(AL) == 10 || REG8(AL) == 13) {
                   6766:                if(REG8(AL) == 10) {
                   6767:                        vram_flush();
                   6768:                }
1.1.1.23  root     6769:                WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), &REG8(AL), 1, &num, NULL);
1.1.1.14  root     6770:                cursor_moved = true;
                   6771:        } else {
1.1.1.16  root     6772:                int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
1.1.1.14  root     6773:                if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     6774: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6775:                        EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     6776: #endif
1.1.1.16  root     6777:                        int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     6778:                        write_text_vram_char(dest - vram, REG8(AL));
1.1.1.35  root     6779: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6780:                        LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     6781: #endif
1.1.1.14  root     6782:                        
1.1.1.23  root     6783:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6784:                        if(++co.X == scr_width) {
                   6785:                                co.X = 0;
                   6786:                                if(++co.Y == scr_height) {
                   6787:                                        vram_flush();
                   6788:                                        WriteConsole(hStdout, "\n", 1, &num, NULL);
                   6789:                                        cursor_moved = true;
                   6790:                                }
                   6791:                        }
                   6792:                        if(!cursor_moved) {
                   6793:                                co.Y += scr_top;
                   6794:                                SetConsoleCursorPosition(hStdout, co);
                   6795:                                cursor_moved = true;
                   6796:                        }
                   6797:                }
                   6798:                mem[dest] = REG8(AL);
                   6799:        }
1.1       root     6800: }
                   6801: 
                   6802: inline void pcbios_int_10h_0fh()
                   6803: {
                   6804:        REG8(AL) = mem[0x449];
                   6805:        REG8(AH) = mem[0x44a];
                   6806:        REG8(BH) = mem[0x462];
                   6807: }
                   6808: 
1.1.1.14  root     6809: inline void pcbios_int_10h_11h()
                   6810: {
                   6811:        switch(REG8(AL)) {
1.1.1.16  root     6812:        case 0x01:
1.1.1.14  root     6813:        case 0x11:
1.1.1.16  root     6814:                pcbios_set_console_size(80, 28, true);
1.1.1.14  root     6815:                break;
1.1.1.16  root     6816:        case 0x02:
1.1.1.14  root     6817:        case 0x12:
1.1.1.16  root     6818:                pcbios_set_console_size(80, 50, true);
1.1.1.14  root     6819:                break;
1.1.1.16  root     6820:        case 0x04:
1.1.1.14  root     6821:        case 0x14:
1.1.1.16  root     6822:                pcbios_set_console_size(80, 25, true);
                   6823:                break;
                   6824:        case 0x18:
                   6825:                pcbios_set_console_size(80, 50, true);
1.1.1.14  root     6826:                break;
                   6827:        case 0x30:
                   6828:                SREG(ES) = 0;
                   6829:                i386_load_segment_descriptor(ES);
                   6830:                REG16(BP) = 0;
                   6831:                REG16(CX) = mem[0x485];
                   6832:                REG8(DL) = mem[0x484];
                   6833:                break;
                   6834:        }
                   6835: }
                   6836: 
                   6837: inline void pcbios_int_10h_12h()
                   6838: {
1.1.1.16  root     6839:        switch(REG8(BL)) {
                   6840:        case 0x10:
1.1.1.14  root     6841:                REG16(BX) = 0x0003;
                   6842:                REG16(CX) = 0x0009;
1.1.1.16  root     6843:                break;
1.1.1.14  root     6844:        }
                   6845: }
                   6846: 
1.1       root     6847: inline void pcbios_int_10h_13h()
                   6848: {
1.1.1.3   root     6849:        int ofs = SREG_BASE(ES) + REG16(BP);
1.1       root     6850:        COORD co;
                   6851:        DWORD num;
                   6852:        
                   6853:        co.X = REG8(DL);
1.1.1.14  root     6854:        co.Y = REG8(DH) + scr_top;
                   6855:        
                   6856:        vram_flush();
1.1       root     6857:        
                   6858:        switch(REG8(AL)) {
                   6859:        case 0x00:
                   6860:        case 0x01:
                   6861:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     6862:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6863:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   6864:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   6865:                        SetConsoleCursorPosition(hStdout, co);
                   6866:                        
                   6867:                        if(csbi.wAttributes != REG8(BL)) {
                   6868:                                SetConsoleTextAttribute(hStdout, REG8(BL));
                   6869:                        }
1.1.1.14  root     6870:                        WriteConsole(hStdout, &mem[ofs], REG16(CX), &num, NULL);
                   6871:                        
1.1       root     6872:                        if(csbi.wAttributes != REG8(BL)) {
                   6873:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   6874:                        }
                   6875:                        if(REG8(AL) == 0x00) {
1.1.1.15  root     6876:                                if(!restore_console_on_exit) {
                   6877:                                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   6878:                                        scr_top = csbi.srWindow.Top;
                   6879:                                }
1.1.1.14  root     6880:                                co.X = mem[0x450 + REG8(BH) * 2];
                   6881:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     6882:                                SetConsoleCursorPosition(hStdout, co);
                   6883:                        } else {
                   6884:                                cursor_moved = true;
                   6885:                        }
                   6886:                } else {
1.1.1.3   root     6887:                        m_CF = 1;
1.1       root     6888:                }
                   6889:                break;
                   6890:        case 0x02:
                   6891:        case 0x03:
                   6892:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     6893:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6894:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   6895:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   6896:                        SetConsoleCursorPosition(hStdout, co);
                   6897:                        
                   6898:                        WORD wAttributes = csbi.wAttributes;
                   6899:                        for(int i = 0; i < REG16(CX); i++, ofs += 2) {
                   6900:                                if(wAttributes != mem[ofs + 1]) {
                   6901:                                        SetConsoleTextAttribute(hStdout, mem[ofs + 1]);
                   6902:                                        wAttributes = mem[ofs + 1];
                   6903:                                }
1.1.1.14  root     6904:                                WriteConsole(hStdout, &mem[ofs], 1, &num, NULL);
1.1       root     6905:                        }
                   6906:                        if(csbi.wAttributes != wAttributes) {
                   6907:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   6908:                        }
                   6909:                        if(REG8(AL) == 0x02) {
1.1.1.14  root     6910:                                co.X = mem[0x450 + REG8(BH) * 2];
                   6911:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     6912:                                SetConsoleCursorPosition(hStdout, co);
                   6913:                        } else {
                   6914:                                cursor_moved = true;
                   6915:                        }
                   6916:                } else {
1.1.1.3   root     6917:                        m_CF = 1;
1.1       root     6918:                }
                   6919:                break;
                   6920:        case 0x10:
                   6921:        case 0x11:
                   6922:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     6923:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6924:                        ReadConsoleOutputCharacter(hStdout, scr_char, REG16(CX), co, &num);
                   6925:                        ReadConsoleOutputAttribute(hStdout, scr_attr, REG16(CX), co, &num);
                   6926:                        for(int i = 0; i < num; i++) {
                   6927:                                mem[ofs++] = scr_char[i];
                   6928:                                mem[ofs++] = scr_attr[i];
                   6929:                                if(REG8(AL) == 0x11) {
                   6930:                                        mem[ofs++] = 0;
                   6931:                                        mem[ofs++] = 0;
                   6932:                                }
                   6933:                        }
                   6934:                } else {
1.1.1.16  root     6935:                        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     6936:                                mem[ofs++] = mem[src++];
                   6937:                                mem[ofs++] = mem[src++];
                   6938:                                if(REG8(AL) == 0x11) {
                   6939:                                        mem[ofs++] = 0;
                   6940:                                        mem[ofs++] = 0;
                   6941:                                }
1.1.1.14  root     6942:                                if(++co.X == scr_width) {
                   6943:                                        if(++co.Y == scr_height) {
1.1       root     6944:                                                break;
                   6945:                                        }
                   6946:                                        co.X = 0;
                   6947:                                }
                   6948:                        }
                   6949:                }
                   6950:                break;
                   6951:        case 0x20:
                   6952:        case 0x21:
                   6953:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     6954:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6955:                        int len = min(REG16(CX), scr_width * scr_height);
                   6956:                        for(int i = 0; i < len; i++) {
1.1       root     6957:                                scr_char[i] = mem[ofs++];
                   6958:                                scr_attr[i] = mem[ofs++];
                   6959:                                if(REG8(AL) == 0x21) {
                   6960:                                        ofs += 2;
                   6961:                                }
                   6962:                        }
1.1.1.14  root     6963:                        WriteConsoleOutputCharacter(hStdout, scr_char, len, co, &num);
                   6964:                        WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     6965:                } else {
1.1.1.16  root     6966:                        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     6967:                                mem[dest++] = mem[ofs++];
                   6968:                                mem[dest++] = mem[ofs++];
                   6969:                                if(REG8(AL) == 0x21) {
                   6970:                                        ofs += 2;
                   6971:                                }
1.1.1.14  root     6972:                                if(++co.X == scr_width) {
                   6973:                                        if(++co.Y == scr_height) {
1.1       root     6974:                                                break;
                   6975:                                        }
                   6976:                                        co.X = 0;
                   6977:                                }
                   6978:                        }
                   6979:                }
                   6980:                break;
                   6981:        default:
1.1.1.22  root     6982:                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     6983:                m_CF = 1;
1.1       root     6984:                break;
                   6985:        }
                   6986: }
                   6987: 
1.1.1.30  root     6988: inline void pcbios_int_10h_18h()
                   6989: {
                   6990:        switch(REG8(AL)) {
                   6991:        case 0x00:
                   6992:        case 0x01:
                   6993: //             REG8(AL) = 0x86;
                   6994:                REG8(AL) = 0x00;
                   6995:                break;
                   6996:        default:
                   6997:                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));
                   6998:                m_CF = 1;
                   6999:                break;
                   7000:        }
                   7001: }
                   7002: 
1.1.1.14  root     7003: inline void pcbios_int_10h_1ah()
                   7004: {
                   7005:        switch(REG8(AL)) {
                   7006:        case 0x00:
                   7007:                REG8(AL) = 0x1a;
                   7008:                REG8(BL) = 0x08;
                   7009:                REG8(BH) = 0x00;
                   7010:                break;
                   7011:        default:
1.1.1.22  root     7012:                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     7013:                m_CF = 1;
                   7014:                break;
                   7015:        }
                   7016: }
                   7017: 
1.1       root     7018: inline void pcbios_int_10h_1dh()
                   7019: {
                   7020:        switch(REG8(AL)) {
                   7021:        case 0x01:
                   7022:                break;
                   7023:        case 0x02:
                   7024:                REG16(BX) = 0;
                   7025:                break;
                   7026:        default:
1.1.1.22  root     7027:                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));
                   7028:                m_CF = 1;
                   7029:                break;
                   7030:        }
                   7031: }
                   7032: 
                   7033: inline void pcbios_int_10h_4fh()
                   7034: {
                   7035:        switch(REG8(AL)) {
                   7036:        case 0x00:
                   7037:                REG8(AH) = 0x02; // not supported
                   7038:                break;
                   7039:        case 0x01:
                   7040:        case 0x02:
                   7041:        case 0x03:
                   7042:        case 0x04:
                   7043:        case 0x05:
                   7044:        case 0x06:
                   7045:        case 0x07:
                   7046:        case 0x08:
                   7047:        case 0x09:
                   7048:        case 0x0a:
                   7049:        case 0x0b:
                   7050:        case 0x0c:
                   7051:                REG8(AH) = 0x01; // failed
                   7052:                break;
                   7053:        default:
                   7054:                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     7055:                m_CF = 1;
1.1       root     7056:                break;
                   7057:        }
                   7058: }
                   7059: 
                   7060: inline void pcbios_int_10h_82h()
                   7061: {
                   7062:        static UINT8 mode = 0;
                   7063:        
                   7064:        switch(REG8(AL)) {
1.1.1.22  root     7065:        case 0x00:
1.1       root     7066:                if(REG8(BL) != 0xff) {
                   7067:                        mode = REG8(BL);
                   7068:                }
                   7069:                REG8(AL) = mode;
                   7070:                break;
                   7071:        default:
1.1.1.22  root     7072:                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     7073:                m_CF = 1;
1.1       root     7074:                break;
                   7075:        }
                   7076: }
                   7077: 
1.1.1.22  root     7078: inline void pcbios_int_10h_83h()
                   7079: {
                   7080:        static UINT8 mode = 0;
                   7081:        
                   7082:        switch(REG8(AL)) {
                   7083:        case 0x00:
                   7084:                REG16(AX) = 0; // offset???
                   7085:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
                   7086:                i386_load_segment_descriptor(ES);
                   7087:                REG16(BX) = (SHADOW_BUF_TOP & 0x0f);
                   7088:                break;
                   7089:        default:
                   7090:                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));
                   7091:                m_CF = 1;
                   7092:                break;
                   7093:        }
                   7094: }
                   7095: 
                   7096: inline void pcbios_int_10h_90h()
                   7097: {
                   7098:        REG8(AL) = mem[0x449];
                   7099: }
                   7100: 
                   7101: inline void pcbios_int_10h_91h()
                   7102: {
                   7103:        REG8(AL) = 0x04; // VGA
                   7104: }
                   7105: 
                   7106: inline void pcbios_int_10h_efh()
                   7107: {
                   7108:        REG16(DX) = 0xffff;
                   7109: }
                   7110: 
1.1       root     7111: inline void pcbios_int_10h_feh()
                   7112: {
                   7113:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     7114:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
1.1.1.3   root     7115:                i386_load_segment_descriptor(ES);
1.1.1.8   root     7116:                REG16(DI) = (SHADOW_BUF_TOP & 0x0f);
1.1       root     7117:        }
                   7118:        int_10h_feh_called = true;
                   7119: }
                   7120: 
                   7121: inline void pcbios_int_10h_ffh()
                   7122: {
                   7123:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.23  root     7124:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7125:                COORD co;
                   7126:                DWORD num;
                   7127:                
1.1.1.14  root     7128:                vram_flush();
                   7129:                
                   7130:                co.X = (REG16(DI) >> 1) % scr_width;
                   7131:                co.Y = (REG16(DI) >> 1) / scr_width;
1.1.1.16  root     7132:                int ofs = pcbios_get_shadow_buffer_address(0, co.X, co.Y);
                   7133:                int end = min(ofs + REG16(CX) * 2, pcbios_get_shadow_buffer_address(0, 0, scr_height));
1.1.1.14  root     7134:                int len;
                   7135:                for(len = 0; ofs < end; len++) {
                   7136:                        scr_char[len] = mem[ofs++];
                   7137:                        scr_attr[len] = mem[ofs++];
                   7138:                }
                   7139:                co.Y += scr_top;
                   7140:                WriteConsoleOutputCharacter(hStdout, scr_char, len, co, &num);
                   7141:                WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     7142:        }
                   7143:        int_10h_ffh_called = true;
                   7144: }
                   7145: 
1.1.1.25  root     7146: inline void pcbios_int_14h_00h()
                   7147: {
1.1.1.29  root     7148:        if(REG16(DX) < 4) {
1.1.1.25  root     7149:                static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600};
                   7150:                UINT8 selector = sio_read(REG16(DX), 3);
                   7151:                selector &= ~0x3f;
                   7152:                selector |= REG8(AL) & 0x1f;
                   7153:                UINT16 divisor = 115200 / rate[REG8(AL) >> 5];
                   7154:                sio_write(REG16(DX), 3, selector | 0x80);
                   7155:                sio_write(REG16(DX), 0, divisor & 0xff);
                   7156:                sio_write(REG16(DX), 1, divisor >> 8);
                   7157:                sio_write(REG16(DX), 3, selector);
                   7158:                REG8(AH) = sio_read(REG16(DX), 5);
                   7159:                REG8(AL) = sio_read(REG16(DX), 6);
                   7160:        } else {
                   7161:                REG8(AH) = 0x80;
                   7162:        }
                   7163: }
                   7164: 
                   7165: inline void pcbios_int_14h_01h()
                   7166: {
1.1.1.29  root     7167:        if(REG16(DX) < 4) {
1.1.1.25  root     7168:                UINT8 selector = sio_read(REG16(DX), 3);
                   7169:                sio_write(REG16(DX), 3, selector & ~0x80);
                   7170:                sio_write(REG16(DX), 0, REG8(AL));
                   7171:                sio_write(REG16(DX), 3, selector);
                   7172:                REG8(AH) = sio_read(REG16(DX), 5);
                   7173:        } else {
                   7174:                REG8(AH) = 0x80;
                   7175:        }
                   7176: }
                   7177: 
                   7178: inline void pcbios_int_14h_02h()
                   7179: {
1.1.1.29  root     7180:        if(REG16(DX) < 4) {
1.1.1.25  root     7181:                UINT8 selector = sio_read(REG16(DX), 3);
                   7182:                sio_write(REG16(DX), 3, selector & ~0x80);
                   7183:                REG8(AL) = sio_read(REG16(DX), 0);
                   7184:                sio_write(REG16(DX), 3, selector);
                   7185:                REG8(AH) = sio_read(REG16(DX), 5);
                   7186:        } else {
                   7187:                REG8(AH) = 0x80;
                   7188:        }
                   7189: }
                   7190: 
                   7191: inline void pcbios_int_14h_03h()
                   7192: {
1.1.1.29  root     7193:        if(REG16(DX) < 4) {
1.1.1.25  root     7194:                REG8(AH) = sio_read(REG16(DX), 5);
                   7195:                REG8(AL) = sio_read(REG16(DX), 6);
                   7196:        } else {
                   7197:                REG8(AH) = 0x80;
                   7198:        }
                   7199: }
                   7200: 
                   7201: inline void pcbios_int_14h_04h()
                   7202: {
1.1.1.29  root     7203:        if(REG16(DX) < 4) {
1.1.1.25  root     7204:                UINT8 selector = sio_read(REG16(DX), 3);
                   7205:                if(REG8(CH) <= 0x03) {
                   7206:                        selector = (selector & ~0x03) | REG8(CH);
                   7207:                }
                   7208:                if(REG8(BL) == 0x00) {
                   7209:                        selector &= ~0x04;
                   7210:                } else if(REG8(BL) == 0x01) {
                   7211:                        selector |= 0x04;
                   7212:                }
                   7213:                if(REG8(BH) == 0x00) {
                   7214:                        selector = (selector & ~0x38) | 0x00;
                   7215:                } else if(REG8(BH) == 0x01) {
                   7216:                        selector = (selector & ~0x38) | 0x08;
                   7217:                } else if(REG8(BH) == 0x02) {
                   7218:                        selector = (selector & ~0x38) | 0x18;
                   7219:                } else if(REG8(BH) == 0x03) {
                   7220:                        selector = (selector & ~0x38) | 0x28;
                   7221:                } else if(REG8(BH) == 0x04) {
                   7222:                        selector = (selector & ~0x38) | 0x38;
                   7223:                }
                   7224:                if(REG8(AL) == 0x00) {
                   7225:                        selector |= 0x40;
                   7226:                } else if(REG8(AL) == 0x01) {
                   7227:                        selector &= ~0x40;
                   7228:                }
                   7229:                if(REG8(CL) <= 0x0b) {
                   7230:                        static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200};
                   7231:                        UINT16 divisor = 115200 / rate[REG8(CL)];
                   7232:                        sio_write(REG16(DX), 3, selector | 0x80);
                   7233:                        sio_write(REG16(DX), 0, divisor & 0xff);
                   7234:                        sio_write(REG16(DX), 1, divisor >> 8);
                   7235:                }
                   7236:                sio_write(REG16(DX), 3, selector);
                   7237:                REG8(AH) = sio_read(REG16(DX), 5);
                   7238:                REG8(AL) = sio_read(REG16(DX), 6);
                   7239:        } else {
                   7240:                REG8(AH) = 0x80;
                   7241:        }
                   7242: }
                   7243: 
                   7244: inline void pcbios_int_14h_05h()
                   7245: {
1.1.1.29  root     7246:        if(REG16(DX) < 4) {
1.1.1.25  root     7247:                if(REG8(AL) == 0x00) {
                   7248:                        REG8(BL) = sio_read(REG16(DX), 4);
                   7249:                        REG8(AH) = sio_read(REG16(DX), 5);
                   7250:                        REG8(AL) = sio_read(REG16(DX), 6);
                   7251:                } else if(REG8(AL) == 0x01) {
                   7252:                        sio_write(REG16(DX), 4, REG8(BL));
                   7253:                        REG8(AH) = sio_read(REG16(DX), 5);
                   7254:                        REG8(AL) = sio_read(REG16(DX), 6);
                   7255:                } else {
                   7256:                        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));
                   7257:                }
                   7258:        } else {
                   7259:                REG8(AH) = 0x80;
                   7260:        }
                   7261: }
                   7262: 
1.1.1.14  root     7263: inline void pcbios_int_15h_10h()
                   7264: {
1.1.1.22  root     7265:        switch(REG8(AL)) {
                   7266:        case 0x00:
1.1.1.14  root     7267:                Sleep(10);
1.1.1.35  root     7268:                REQUEST_HARDWRE_UPDATE();
1.1.1.22  root     7269:                break;
                   7270:        default:
                   7271:                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     7272:                REG8(AH) = 0x86;
                   7273:                m_CF = 1;
                   7274:        }
                   7275: }
                   7276: 
1.1       root     7277: inline void pcbios_int_15h_23h()
                   7278: {
                   7279:        switch(REG8(AL)) {
1.1.1.22  root     7280:        case 0x00:
1.1.1.8   root     7281:                REG8(CL) = cmos_read(0x2d);
                   7282:                REG8(CH) = cmos_read(0x2e);
1.1       root     7283:                break;
1.1.1.22  root     7284:        case 0x01:
1.1.1.8   root     7285:                cmos_write(0x2d, REG8(CL));
                   7286:                cmos_write(0x2e, REG8(CH));
1.1       root     7287:                break;
                   7288:        default:
1.1.1.22  root     7289:                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     7290:                REG8(AH) = 0x86;
1.1.1.3   root     7291:                m_CF = 1;
1.1       root     7292:                break;
                   7293:        }
                   7294: }
                   7295: 
                   7296: inline void pcbios_int_15h_24h()
                   7297: {
                   7298:        switch(REG8(AL)) {
1.1.1.22  root     7299:        case 0x00:
1.1.1.3   root     7300:                i386_set_a20_line(0);
1.1       root     7301:                REG8(AH) = 0;
                   7302:                break;
1.1.1.22  root     7303:        case 0x01:
1.1.1.3   root     7304:                i386_set_a20_line(1);
1.1       root     7305:                REG8(AH) = 0;
                   7306:                break;
1.1.1.22  root     7307:        case 0x02:
1.1       root     7308:                REG8(AH) = 0;
1.1.1.3   root     7309:                REG8(AL) = (m_a20_mask >> 20) & 1;
1.1       root     7310:                REG16(CX) = 0;
                   7311:                break;
1.1.1.22  root     7312:        case 0x03:
1.1       root     7313:                REG16(AX) = 0;
                   7314:                REG16(BX) = 0;
                   7315:                break;
1.1.1.22  root     7316:        default:
                   7317:                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));
                   7318:                REG8(AH) = 0x86;
                   7319:                m_CF = 1;
                   7320:                break;
1.1       root     7321:        }
                   7322: }
                   7323: 
                   7324: inline void pcbios_int_15h_49h()
                   7325: {
1.1.1.27  root     7326:        REG8(AH) = 0x00;
                   7327:        REG8(BL) = 0x00; // DOS/V
1.1       root     7328: }
                   7329: 
1.1.1.22  root     7330: inline void pcbios_int_15h_50h()
                   7331: {
                   7332:        switch(REG8(AL)) {
                   7333:        case 0x00:
                   7334:        case 0x01:
                   7335:                if(REG8(BH) != 0x00 && REG8(BH) != 0x01) {
                   7336:                        REG8(AH) = 0x01; // invalid font type in bh
                   7337:                        m_CF = 1;
1.1.1.27  root     7338:                } else if(REG8(BL) != 0x00) {
1.1.1.22  root     7339:                        REG8(AH) = 0x02; // bl not zero
                   7340:                        m_CF = 1;
                   7341:                } else if(REG16(BP) != 0 && REG16(BP) != 437 && REG16(BP) != 932 && REG16(BP) != 934 && REG16(BP) != 936 && REG16(BP) != 938) {
                   7342:                        REG8(AH) = 0x04; // invalid code page
                   7343:                        m_CF = 1;
1.1.1.27  root     7344:                } else if(REG8(AL) == 0x01) {
                   7345:                        REG8(AH) = 0x06; // font is read only
1.1.1.22  root     7346:                        m_CF = 1;
1.1.1.27  root     7347:                } else {
                   7348:                        // dummy font read routine is at fffd:000d
                   7349:                        SREG(ES) = 0xfffd;
                   7350:                        i386_load_segment_descriptor(ES);
1.1.1.32  root     7351:                        REG16(BX) = 0x000d;
1.1.1.27  root     7352:                        REG8(AH) = 0x00; // success
1.1.1.22  root     7353:                }
                   7354:                break;
                   7355:        default:
                   7356:                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));
                   7357:                REG8(AH) = 0x86;
                   7358:                m_CF = 1;
                   7359:                break;
                   7360:        }
                   7361: }
                   7362: 
1.1.1.30  root     7363: inline void pcbios_int_15h_53h()
                   7364: {
                   7365:        switch(REG8(AL)) {
                   7366:        case 0x00:
                   7367:                // APM is not installed
                   7368:                REG8(AH) = 0x86;
                   7369:                m_CF = 1;
                   7370:                break;
                   7371:        default:
                   7372:                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));
                   7373:                REG8(AH) = 0x86;
                   7374:                m_CF = 1;
                   7375:                break;
                   7376:        }
                   7377: }
                   7378: 
1.1.1.35  root     7379: 
                   7380: DWORD WINAPI pcbios_int_15h_86h_thread(LPVOID)
1.1       root     7381: {
                   7382:        UINT32 usec = (REG16(CX) << 16) | REG16(DX);
1.1.1.14  root     7383:        UINT32 msec = usec / 1000;
                   7384:        
1.1.1.35  root     7385:        while(msec && !m_halted) {
1.1.1.14  root     7386:                UINT32 tmp = min(msec, 100);
                   7387:                if(msec - tmp < 10) {
                   7388:                        tmp = msec;
                   7389:                }
                   7390:                Sleep(tmp);
                   7391:                msec -= tmp;
                   7392:        }
1.1.1.35  root     7393:        
                   7394: #ifdef USE_SERVICE_THREAD
                   7395:        service_exit = true;
                   7396: #endif
                   7397:        return(0);
                   7398: }
                   7399: 
                   7400: inline void pcbios_int_15h_86h()
                   7401: {
                   7402:        if(!(REG16(CX) == 0 && REG16(DX) == 0)) {
                   7403: #ifdef USE_SERVICE_THREAD
                   7404:                start_service_loop(pcbios_int_15h_86h_thread);
                   7405: #else
                   7406:                pcbios_int_15h_86h_thread(NULL);
                   7407:                REQUEST_HARDWRE_UPDATE();
                   7408: #endif
                   7409:        }
1.1       root     7410: }
                   7411: 
                   7412: inline void pcbios_int_15h_87h()
                   7413: {
                   7414:        // copy extended memory (from DOSBox)
                   7415:        int len = REG16(CX) * 2;
1.1.1.3   root     7416:        int ofs = SREG_BASE(ES) + REG16(SI);
1.1       root     7417:        int src = (*(UINT32 *)(mem + ofs + 0x12) & 0xffffff); // + (mem[ofs + 0x16] << 24);
                   7418:        int dst = (*(UINT32 *)(mem + ofs + 0x1a) & 0xffffff); // + (mem[ofs + 0x1e] << 24);
                   7419:        memcpy(mem + dst, mem + src, len);
                   7420:        REG16(AX) = 0x00;
                   7421: }
                   7422: 
                   7423: inline void pcbios_int_15h_88h()
                   7424: {
1.1.1.17  root     7425:        REG16(AX) = ((min(MAX_MEM, 0x4000000) - 0x100000) >> 10);
1.1       root     7426: }
                   7427: 
                   7428: inline void pcbios_int_15h_89h()
                   7429: {
1.1.1.21  root     7430: #if defined(HAS_I286) || defined(HAS_I386)
1.1       root     7431:        // switch to protected mode (from DOSBox)
                   7432:        write_io_byte(0x20, 0x10);
                   7433:        write_io_byte(0x21, REG8(BH));
                   7434:        write_io_byte(0x21, 0x00);
                   7435:        write_io_byte(0xa0, 0x10);
                   7436:        write_io_byte(0xa1, REG8(BL));
                   7437:        write_io_byte(0xa1, 0x00);
1.1.1.3   root     7438:        i386_set_a20_line(1);
                   7439:        int ofs = SREG_BASE(ES) + REG16(SI);
                   7440:        m_gdtr.limit = *(UINT16 *)(mem + ofs + 0x08);
                   7441:        m_gdtr.base = *(UINT32 *)(mem + ofs + 0x08 + 0x02) & 0xffffff;
                   7442:        m_idtr.limit = *(UINT16 *)(mem + ofs + 0x10);
                   7443:        m_idtr.base = *(UINT32 *)(mem + ofs + 0x10 + 0x02) & 0xffffff;
                   7444: #if defined(HAS_I386)
                   7445:        m_cr[0] |= 1;
                   7446: #else
                   7447:        m_msw |= 1;
                   7448: #endif
                   7449:        SREG(DS) = 0x18;
                   7450:        SREG(ES) = 0x20;
                   7451:        SREG(SS) = 0x28;
                   7452:        i386_load_segment_descriptor(DS);
                   7453:        i386_load_segment_descriptor(ES);
                   7454:        i386_load_segment_descriptor(SS);
1.1.1.21  root     7455:        UINT16 offset = *(UINT16 *)(mem + SREG_BASE(SS) + REG16(SP));
1.1       root     7456:        REG16(SP) += 6;
1.1.1.3   root     7457: #if defined(HAS_I386)
1.1.1.21  root     7458:        UINT32 flags = get_flags();
                   7459:        flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
                   7460:        set_flags(flags);
1.1.1.3   root     7461: #else
1.1.1.21  root     7462:        UINT32 flags = CompressFlags();
                   7463:        flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
                   7464:        ExpandFlags(flags);
1.1.1.3   root     7465: #endif
1.1       root     7466:        REG16(AX) = 0x00;
1.1.1.21  root     7467:        i386_jmp_far(0x30, /*REG16(CX)*/offset);
1.1       root     7468: #else
1.1.1.21  root     7469:        // i86/i186/v30: protected mode is not supported
1.1       root     7470:        REG8(AH) = 0x86;
1.1.1.3   root     7471:        m_CF = 1;
1.1       root     7472: #endif
                   7473: }
                   7474: 
1.1.1.21  root     7475: inline void pcbios_int_15h_8ah()
                   7476: {
                   7477:        UINT32 size = MAX_MEM - 0x100000;
                   7478:        REG16(AX) = size & 0xffff;
                   7479:        REG16(DX) = size >> 16;
                   7480: }
                   7481: 
1.1.1.3   root     7482: #if defined(HAS_I386)
1.1       root     7483: inline void pcbios_int_15h_c9h()
                   7484: {
                   7485:        REG8(AH) = 0x00;
                   7486:        REG8(CH) = cpu_type;
                   7487:        REG8(CL) = cpu_step;
                   7488: }
1.1.1.3   root     7489: #endif
1.1       root     7490: 
                   7491: inline void pcbios_int_15h_cah()
                   7492: {
                   7493:        switch(REG8(AL)) {
1.1.1.22  root     7494:        case 0x00:
1.1       root     7495:                if(REG8(BL) > 0x3f) {
                   7496:                        REG8(AH) = 0x03;
1.1.1.3   root     7497:                        m_CF = 1;
1.1       root     7498:                } else if(REG8(BL) < 0x0e) {
                   7499:                        REG8(AH) = 0x04;
1.1.1.3   root     7500:                        m_CF = 1;
1.1       root     7501:                } else {
1.1.1.8   root     7502:                        REG8(CL) = cmos_read(REG8(BL));
1.1       root     7503:                }
                   7504:                break;
1.1.1.22  root     7505:        case 0x01:
1.1       root     7506:                if(REG8(BL) > 0x3f) {
                   7507:                        REG8(AH) = 0x03;
1.1.1.3   root     7508:                        m_CF = 1;
1.1       root     7509:                } else if(REG8(BL) < 0x0e) {
                   7510:                        REG8(AH) = 0x04;
1.1.1.3   root     7511:                        m_CF = 1;
1.1       root     7512:                } else {
1.1.1.8   root     7513:                        cmos_write(REG8(BL), REG8(CL));
1.1       root     7514:                }
                   7515:                break;
                   7516:        default:
1.1.1.22  root     7517:                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     7518:                REG8(AH) = 0x86;
1.1.1.3   root     7519:                m_CF = 1;
1.1       root     7520:                break;
                   7521:        }
                   7522: }
                   7523: 
1.1.1.22  root     7524: inline void pcbios_int_15h_e8h()
1.1.1.17  root     7525: {
1.1.1.22  root     7526:        switch(REG8(AL)) {
                   7527: #if defined(HAS_I386)
                   7528:        case 0x01:
                   7529:                REG16(AX) = REG16(CX) = ((min(MAX_MEM, 0x1000000) - 0x100000) >> 10);
                   7530:                REG16(BX) = REG16(DX) = ((max(MAX_MEM, 0x1000000) - 0x1000000) >> 16);
                   7531:                break;
1.1.1.17  root     7532: #endif
1.1.1.22  root     7533:        default:
                   7534:                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));
                   7535:                REG8(AH) = 0x86;
                   7536:                m_CF = 1;
                   7537:                break;
                   7538:        }
                   7539: }
1.1.1.17  root     7540: 
1.1.1.33  root     7541: void pcbios_update_key_code(bool wait)
1.1       root     7542: {
1.1.1.32  root     7543:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     7544: #ifdef USE_SERVICE_THREAD
                   7545:                EnterCriticalSection(&key_buf_crit_sect);
                   7546: #endif
                   7547:                bool empty = key_buf_char->empty();
                   7548: #ifdef USE_SERVICE_THREAD
                   7549:                LeaveCriticalSection(&key_buf_crit_sect);
                   7550: #endif
                   7551:                if(empty) {
1.1.1.32  root     7552:                        if(!update_key_buffer()) {
1.1.1.33  root     7553:                                if(wait) {
1.1.1.32  root     7554:                                        Sleep(10);
                   7555:                                } else {
                   7556:                                        maybe_idle();
                   7557:                                }
1.1.1.14  root     7558:                        }
                   7559:                }
1.1.1.34  root     7560:        }
                   7561:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     7562: #ifdef USE_SERVICE_THREAD
                   7563:                EnterCriticalSection(&key_buf_crit_sect);
                   7564: #endif
1.1.1.32  root     7565:                if(key_buf_char->count() != 0) {
1.1.1.35  root     7566:                        key_code  = key_buf_char->read() << 0;
                   7567:                        key_code |= key_buf_scan->read() << 8;
                   7568:                        key_recv  = 0x0000ffff;
1.1.1.32  root     7569:                }
                   7570:                if(key_buf_char->count() != 0) {
1.1.1.35  root     7571:                        key_code |= key_buf_char->read() << 16;
                   7572:                        key_code |= key_buf_scan->read() << 24;
1.1.1.33  root     7573:                        key_recv |= 0xffff0000;
1.1.1.32  root     7574:                }
1.1.1.35  root     7575: #ifdef USE_SERVICE_THREAD
                   7576:                LeaveCriticalSection(&key_buf_crit_sect);
                   7577: #endif
1.1       root     7578:        }
                   7579: }
                   7580: 
1.1.1.35  root     7581: DWORD WINAPI pcbios_int_16h_00h_thread(LPVOID)
1.1       root     7582: {
1.1.1.33  root     7583:        while(key_recv == 0 && !m_halted) {
                   7584:                pcbios_update_key_code(true);
1.1       root     7585:        }
1.1.1.33  root     7586:        if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
                   7587:                if((key_code & 0xffff) == 0x0000 || (key_code & 0xffff) == 0xe000) {
                   7588:                        if(REG8(AH) == 0x10) {
                   7589:                                key_code = ((key_code >> 8) & 0xff) | ((key_code >> 16) & 0xff00);
                   7590:                        } else {
                   7591:                                key_code = ((key_code >> 16) & 0xff00);
                   7592:                        }
                   7593:                        key_recv >>= 16;
1.1       root     7594:                }
                   7595:        }
                   7596:        REG16(AX) = key_code & 0xffff;
                   7597:        key_code >>= 16;
1.1.1.33  root     7598:        key_recv >>= 16;
1.1.1.35  root     7599:        
                   7600: #ifdef USE_SERVICE_THREAD
                   7601:        service_exit = true;
                   7602: #endif
                   7603:        return(0);
                   7604: }
                   7605: 
                   7606: inline void pcbios_int_16h_00h()
                   7607: {
                   7608: #ifdef USE_SERVICE_THREAD
                   7609:        start_service_loop(pcbios_int_16h_00h_thread);
                   7610: #else
                   7611:        pcbios_int_16h_00h_thread(NULL);
                   7612:        REQUEST_HARDWRE_UPDATE();
                   7613: #endif
1.1       root     7614: }
                   7615: 
                   7616: inline void pcbios_int_16h_01h()
                   7617: {
1.1.1.33  root     7618:        if(key_recv == 0) {
                   7619:                pcbios_update_key_code(false);
1.1.1.5   root     7620:        }
1.1.1.33  root     7621:        if(key_recv != 0) {
                   7622:                UINT32 key_code_tmp = key_code;
                   7623:                if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
                   7624:                        if((key_code_tmp & 0xffff) == 0x0000 || (key_code_tmp & 0xffff) == 0xe000) {
                   7625:                                if(REG8(AH) == 0x11) {
                   7626:                                        key_code_tmp = ((key_code_tmp >> 8) & 0xff) | ((key_code_tmp >> 16) & 0xff00);
                   7627:                                } else {
                   7628:                                        key_code_tmp = ((key_code_tmp >> 16) & 0xff00);
                   7629:                                }
                   7630:                        }
1.1       root     7631:                }
1.1.1.5   root     7632:                REG16(AX) = key_code_tmp & 0xffff;
1.1.1.3   root     7633: #if defined(HAS_I386)
1.1.1.33  root     7634:                m_ZF = 0;
                   7635: #else
                   7636:                m_ZeroVal = 1;
                   7637: #endif
                   7638:        } else {
                   7639: #if defined(HAS_I386)
                   7640:                m_ZF = 1;
1.1.1.3   root     7641: #else
1.1.1.33  root     7642:                m_ZeroVal = 0;
1.1.1.3   root     7643: #endif
1.1.1.33  root     7644:        }
1.1       root     7645: }
                   7646: 
                   7647: inline void pcbios_int_16h_02h()
                   7648: {
                   7649:        REG8(AL)  = (GetAsyncKeyState(VK_INSERT ) & 0x0001) ? 0x80 : 0;
                   7650:        REG8(AL) |= (GetAsyncKeyState(VK_CAPITAL) & 0x0001) ? 0x40 : 0;
                   7651:        REG8(AL) |= (GetAsyncKeyState(VK_NUMLOCK) & 0x0001) ? 0x20 : 0;
                   7652:        REG8(AL) |= (GetAsyncKeyState(VK_SCROLL ) & 0x0001) ? 0x10 : 0;
                   7653:        REG8(AL) |= (GetAsyncKeyState(VK_MENU   ) & 0x8000) ? 0x08 : 0;
                   7654:        REG8(AL) |= (GetAsyncKeyState(VK_CONTROL) & 0x8000) ? 0x04 : 0;
                   7655:        REG8(AL) |= (GetAsyncKeyState(VK_LSHIFT ) & 0x8000) ? 0x02 : 0;
                   7656:        REG8(AL) |= (GetAsyncKeyState(VK_RSHIFT ) & 0x8000) ? 0x01 : 0;
                   7657: }
                   7658: 
                   7659: inline void pcbios_int_16h_03h()
                   7660: {
                   7661:        static UINT16 status = 0;
                   7662:        
                   7663:        switch(REG8(AL)) {
                   7664:        case 0x05:
                   7665:                status = REG16(BX);
                   7666:                break;
                   7667:        case 0x06:
                   7668:                REG16(BX) = status;
                   7669:                break;
                   7670:        default:
1.1.1.3   root     7671:                m_CF = 1;
1.1       root     7672:                break;
                   7673:        }
                   7674: }
                   7675: 
                   7676: inline void pcbios_int_16h_05h()
                   7677: {
1.1.1.32  root     7678:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     7679: #ifdef USE_SERVICE_THREAD
                   7680:                EnterCriticalSection(&key_buf_crit_sect);
                   7681: #endif
1.1.1.32  root     7682:                key_buf_char->write(REG8(CL));
                   7683:                key_buf_scan->write(REG8(CH));
1.1.1.35  root     7684: #ifdef USE_SERVICE_THREAD
                   7685:                LeaveCriticalSection(&key_buf_crit_sect);
                   7686: #endif
1.1.1.32  root     7687:        }
1.1       root     7688:        REG8(AL) = 0x00;
                   7689: }
                   7690: 
                   7691: inline void pcbios_int_16h_12h()
                   7692: {
                   7693:        pcbios_int_16h_02h();
                   7694:        
                   7695:        REG8(AH)  = 0;//(GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x80 : 0;
                   7696:        REG8(AH) |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   7697:        REG8(AH) |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   7698:        REG8(AH) |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
                   7699:        REG8(AH) |= (GetAsyncKeyState(VK_RMENU   ) & 0x8000) ? 0x08 : 0;
                   7700:        REG8(AH) |= (GetAsyncKeyState(VK_RCONTROL) & 0x8000) ? 0x04 : 0;
                   7701:        REG8(AH) |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   7702:        REG8(AH) |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   7703: }
                   7704: 
                   7705: inline void pcbios_int_16h_13h()
                   7706: {
                   7707:        static UINT16 status = 0;
                   7708:        
                   7709:        switch(REG8(AL)) {
                   7710:        case 0x00:
                   7711:                status = REG16(DX);
                   7712:                break;
                   7713:        case 0x01:
                   7714:                REG16(DX) = status;
                   7715:                break;
                   7716:        default:
1.1.1.22  root     7717:                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     7718:                m_CF = 1;
1.1       root     7719:                break;
                   7720:        }
                   7721: }
                   7722: 
                   7723: inline void pcbios_int_16h_14h()
                   7724: {
                   7725:        static UINT8 status = 0;
                   7726:        
                   7727:        switch(REG8(AL)) {
                   7728:        case 0x00:
                   7729:        case 0x01:
                   7730:                status = REG8(AL);
                   7731:                break;
                   7732:        case 0x02:
                   7733:                REG8(AL) = status;
                   7734:                break;
                   7735:        default:
1.1.1.22  root     7736:                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     7737:                m_CF = 1;
1.1       root     7738:                break;
                   7739:        }
                   7740: }
                   7741: 
1.1.1.24  root     7742: inline void pcbios_int_16h_55h()
                   7743: {
                   7744:        switch(REG8(AL)) {
                   7745:        case 0x00:
                   7746:                // keyboard tsr is not present
                   7747:                break;
                   7748:        case 0xfe:
                   7749:                // handlers for INT 08, INT 09, INT 16, INT 1B, and INT 1C are installed
                   7750:                break;
                   7751:        case 0xff:
                   7752:                break;
                   7753:        default:
                   7754:                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));
                   7755:                m_CF = 1;
                   7756:                break;
                   7757:        }
                   7758: }
                   7759: 
1.1.1.30  root     7760: inline void pcbios_int_16h_6fh()
                   7761: {
                   7762:        switch(REG8(AL)) {
                   7763:        case 0x00:
                   7764:                // HP Vectra EX-BIOS - "F16_INQUIRE" - Extended BIOS is not installed
                   7765:                break;
                   7766:        default:
                   7767:                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));
                   7768:                m_CF = 1;
                   7769:                break;
                   7770:        }
                   7771: }
                   7772: 
1.1.1.37  root     7773: UINT16 pcbios_printer_jis2sjis(UINT16 jis)
                   7774: {
                   7775:        UINT8 hi = jis >> 8;
                   7776:        UINT8 lo = jis & 0xff;
                   7777:        
                   7778:        lo = (hi & 0x01) ? lo - 0x1f : lo + 0x7d;
                   7779:        hi = (hi - 0x21) / 2 + 0x81;
                   7780:        hi = (hi >= 0xa0) ? hi + 0x40 : hi;
                   7781:        lo = (lo >= 0x7f) ? lo + 0x01 : lo;
                   7782:        
                   7783:        return((hi << 8) + lo);
                   7784: }
                   7785: 
                   7786: UINT16 pcbios_printer_sjis2jis(UINT16 sjis)
                   7787: {
                   7788:        UINT8 hi = sjis >> 8;
                   7789:        UINT8 lo = sjis & 0xff;
                   7790:        
                   7791:        if(hi == 0x80 || (hi >= 0xeb && hi <= 0xef) || (hi >= 0xf4 && hi <= 0xff)) {
                   7792:                return(0x2121);
                   7793:        }
                   7794:        if((lo >= 0x00 && lo <= 0x3f) || lo == 0x7f || (lo >= 0xfd && lo <= 0xff)) {
                   7795:                return(0x2121);
                   7796:        }
                   7797:        if(hi >= 0xf0 && hi <= 0xf3) {
                   7798:                // gaiji
                   7799:                if(lo >= 0x40 && lo <= 0x7e) {
                   7800:                        return(0x7721 + lo - 0x40 + (hi - 0xf0) * 0x200);
                   7801:                }
                   7802:                if(lo >= 0x80 && lo <= 0x9e) {
                   7803:                        return(0x7760 + lo - 0x80 + (hi - 0xf0) * 0x200);
                   7804:                }
                   7805:                if(lo >= 0x9f && lo <= 0xfc) {
                   7806:                        return(0x7821 + lo - 0x9f + (hi - 0xf0) * 0x200);
                   7807:                }
                   7808:        }
                   7809:        hi = (hi >= 0xe0) ? hi - 0x40 : hi;
                   7810:        lo = (lo >= 0x80) ? lo - 0x01 : lo;
                   7811:        hi = ((lo >= 0x9e) ? 1 : 0) + (hi - 0x81) * 2 + 0x21;
                   7812:        lo = ((lo >= 0x9e) ? lo - 0x9e : lo - 0x40) + 0x21;
                   7813:        
                   7814:        return((hi << 8) + lo);
                   7815: }
                   7816: 
1.1.1.38! root     7817: // AX�e�N�j�J�����t�@�����X�K�C�h 1989
        !          7818: // �t�^10 ���{��g��PRINTER DRIVER NIOS�T�d�l
        !          7819: // 6. �R���g���[���R�[�h�̉��
1.1.1.37  root     7820: 
                   7821: void pcbios_printer_out(int c, UINT8 data)
                   7822: {
                   7823:        if(pio[c].conv_mode) {
                   7824:                if(pio[c].sjis_hi != 0) {
                   7825:                        if(!pio[c].jis_mode) {
                   7826:                                printer_out(c, 0x1c);
                   7827:                                printer_out(c, 0x26);
                   7828:                                pio[c].jis_mode = true;
                   7829:                        }
                   7830:                        UINT16 jis = pcbios_printer_sjis2jis((pio[c].sjis_hi << 8) | data);
                   7831:                        printer_out(c, jis >> 8);
                   7832:                        printer_out(c, jis & 0xff);
                   7833:                        pio[c].sjis_hi = 0;
                   7834:                } else if(pio[c].esc_buf[0] == 0x1b) {
                   7835:                        printer_out(c, data);
                   7836:                        if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
                   7837:                                pio[c].esc_buf[pio[c].esc_len] = data;
                   7838:                        }
                   7839:                        pio[c].esc_len++;
                   7840:                        
                   7841:                        switch(pio[c].esc_buf[1]) {
1.1.1.38! root     7842:                        case 0x33: // 1Bh 33h XX
        !          7843:                        case 0x4a: // 1Bh 4Ah XX
        !          7844:                        case 0x4e: // 1Bh 4Eh XX
        !          7845:                        case 0x51: // 1Bh 51h XX
        !          7846:                        case 0x55: // 1Bh 55h XX
        !          7847:                        case 0x6c: // 1Bh 6Ch XX
        !          7848:                        case 0x71: // 1Bh 71h XX
        !          7849:                        case 0x72: // 1Bh 72h XX
1.1.1.37  root     7850:                                if(pio[c].esc_len == 3) {
                   7851:                                        pio[c].esc_buf[0] = 0x00;
                   7852:                                }
                   7853:                                break;
1.1.1.38! root     7854:                        case 0x24: // 1Bh 24h XX XX
        !          7855:                        case 0x5c: // 1Bh 5Ch XX XX
1.1.1.37  root     7856:                                if(pio[c].esc_len == 4) {
                   7857:                                        pio[c].esc_buf[0] = 0x00;
                   7858:                                }
                   7859:                                break;
1.1.1.38! root     7860:                        case 0x2a: // 1Bh 2Ah XX XX XX data
1.1.1.37  root     7861:                                if(pio[c].esc_len >= 3) {
                   7862:                                        switch(pio[c].esc_buf[2]) {
                   7863:                                        case 0: case 1: case 2: case 3: case 4: case 6:
                   7864:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 1) {
                   7865:                                                        pio[c].esc_buf[0] = 0x00;
                   7866:                                                }
                   7867:                                                break;
                   7868:                                        case 32: case 33: case 38: case 39: case 40:
                   7869:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 3) {
                   7870:                                                        pio[c].esc_buf[0] = 0x00;
                   7871:                                                }
                   7872:                                                break;
1.1.1.38! root     7873:                                        case 71: case 72: case 73:
        !          7874:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 6) {
        !          7875:                                                        pio[c].esc_buf[0] = 0x00;
        !          7876:                                                }
        !          7877:                                                break;
1.1.1.37  root     7878:                                        default:
                   7879:                                                pio[c].esc_buf[0] = 0x00;
                   7880:                                                break;
                   7881:                                        }
                   7882:                                }
                   7883:                                break;
1.1.1.38! root     7884:                        case 0x40: // 1Bh 40h
1.1.1.37  root     7885:                                if(pio[c].jis_mode) {
                   7886:                                        printer_out(c, 0x1c);
                   7887:                                        printer_out(c, 0x2e);
                   7888:                                        pio[c].jis_mode = false;
                   7889:                                }
                   7890:                                pio[c].esc_buf[0] = 0x00;
                   7891:                                break;
1.1.1.38! root     7892:                        case 0x42: // 1Bh 42h data 00h
        !          7893:                        case 0x44: // 1Bh 44h data 00h
1.1.1.37  root     7894:                                if(pio[c].esc_len >= 3 && data == 0) {
                   7895:                                        pio[c].esc_buf[0] = 0x00;
                   7896:                                }
                   7897:                                break;
1.1.1.38! root     7898:                        case 0x43: // 1Bh 43h (00h) XX
1.1.1.37  root     7899:                                if(pio[c].esc_len >= 3 && data != 0) {
                   7900:                                        pio[c].esc_buf[0] = 0x00;
                   7901:                                }
                   7902:                                break;
1.1.1.38! root     7903:                        default: // 1Bh XX
1.1.1.37  root     7904:                                pio[c].esc_buf[0] = 0x00;
                   7905:                                break;
                   7906:                        }
                   7907:                } else if(pio[c].esc_buf[0] == 0x1c) {
                   7908:                        printer_out(c, data);
                   7909:                        if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
                   7910:                                pio[c].esc_buf[pio[c].esc_len] = data;
                   7911:                        }
                   7912:                        pio[c].esc_len++;
                   7913:                        
                   7914:                        switch(pio[c].esc_buf[1]) {
1.1.1.38! root     7915:                        case 0x21: // 1Ch 21h XX
        !          7916:                        case 0x2d: // 1Ch 2Dh XX
        !          7917:                        case 0x57: // 1Ch 57h XX
        !          7918:                        case 0x6b: // 1Ch 6Bh XX
        !          7919:                        case 0x72: // 1Ch 72h XX
        !          7920:                        case 0x78: // 1Ch 78h XX
1.1.1.37  root     7921:                                if(pio[c].esc_len == 3) {
                   7922:                                        pio[c].esc_buf[0] = 0x00;
                   7923:                                }
                   7924:                                break;
1.1.1.38! root     7925:                        case 0x26: // 1Ch 26h
1.1.1.37  root     7926:                                pio[c].jis_mode = true;
                   7927:                                pio[c].esc_buf[0] = 0x00;
                   7928:                                break;
1.1.1.38! root     7929:                        case 0x2e: // 1Ch 2Eh
1.1.1.37  root     7930:                                pio[c].jis_mode = false;
                   7931:                                pio[c].esc_buf[0] = 0x00;
                   7932:                                break;
1.1.1.38! root     7933:                        case 0x32: // 1Ch 32h XX XX data
1.1.1.37  root     7934:                                if(pio[c].esc_len == 76) {
                   7935:                                        pio[c].esc_buf[0] = 0x00;
                   7936:                                }
                   7937:                                break;
1.1.1.38! root     7938:                        case 0x44: // 1Bh 44h data 00h
1.1.1.37  root     7939:                                if(pio[c].esc_len == 6) {
                   7940:                                        pio[c].esc_buf[0] = 0x00;
                   7941:                                }
                   7942:                                break;
1.1.1.38! root     7943:                        case 0x53: // 1Ch 53h XX XX
        !          7944:                        case 0x54: // 1Ch 54h XX XX
1.1.1.37  root     7945:                                if(pio[c].esc_len == 4) {
                   7946:                                        pio[c].esc_buf[0] = 0x00;
                   7947:                                }
                   7948:                                break;
1.1.1.38! root     7949:                        default: // 1Ch XX
1.1.1.37  root     7950:                                pio[c].esc_buf[0] = 0x00;
                   7951:                                break;
                   7952:                        }
                   7953:                } else if(data == 0x1b || data == 0x1c) {
                   7954:                        printer_out(c, data);
                   7955:                        pio[c].esc_buf[0] = data;
                   7956:                        pio[c].esc_len = 1;
                   7957:                } else if((data >= 0x80 && data <= 0x9f) || (data >= 0xe0 && data <= 0xff)) {
                   7958:                        pio[c].sjis_hi = data;
                   7959:                } else {
                   7960:                        if(pio[c].jis_mode) {
                   7961:                                printer_out(c, 0x1c);
                   7962:                                printer_out(c, 0x2e);
                   7963:                                pio[c].jis_mode = false;
                   7964:                        }
                   7965:                        printer_out(c, data);
                   7966:                }
                   7967:        } else {
                   7968:                if(pio[c].jis_mode) {
                   7969:                        printer_out(c, 0x1c);
                   7970:                        printer_out(c, 0x2e);
                   7971:                        pio[c].jis_mode = false;
                   7972:                }
                   7973:                printer_out(c, data);
                   7974:        }
                   7975: }
                   7976: 
                   7977: inline void pcbios_int_17h_00h()
                   7978: {
                   7979:        if(REG16(DX) < 3) {
                   7980:                pcbios_printer_out(REG16(DX), REG8(AL));
                   7981:                REG8(AH) = 0xd0;
                   7982:        }
                   7983: }
                   7984: 
                   7985: inline void pcbios_int_17h_01h()
                   7986: {
                   7987:        if(REG16(DX) < 3) {
                   7988:                REG8(AH) = 0xd0;
                   7989:        }
                   7990: }
                   7991: 
                   7992: inline void pcbios_int_17h_02h()
                   7993: {
                   7994:        if(REG16(DX) < 3) {
                   7995:                REG8(AH) = 0xd0;
                   7996:        }
                   7997: }
                   7998: 
                   7999: inline void pcbios_int_17h_03h()
                   8000: {
                   8001:        switch(REG8(AL)) {
                   8002:        case 0x00:
                   8003:                if(REG16(DX) < 3) {
                   8004:                        if(pio[REG16(DX)].jis_mode) {
                   8005:                                printer_out(REG16(DX), 0x1c);
                   8006:                                printer_out(REG16(DX), 0x2e);
                   8007:                                pio[REG16(DX)].jis_mode = false;
                   8008:                        }
                   8009:                        for(UINT16 i = 0; i < REG16(CX); i++) {
                   8010:                                printer_out(REG16(DX), mem[SREG_BASE(ES) + REG16(BX) + i]);
                   8011:                        }
                   8012:                        REG16(CX) = 0x0000;
                   8013:                        REG8(AH) = 0xd0;
                   8014:                }
                   8015:                break;
                   8016:        default:
                   8017:                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));
                   8018:                break;
                   8019:        }
                   8020: }
                   8021: 
                   8022: inline void pcbios_int_17h_50h()
                   8023: {
                   8024:        switch(REG8(AL)) {
                   8025:        case 0x00:
                   8026:                if(REG16(DX) < 3) {
                   8027:                        if(REG16(BX) = 0x0001) {
                   8028:                                pio[REG16(DX)].conv_mode = false;
                   8029:                                REG8(AL) = 0x00;
                   8030:                        } else if(REG16(BX) = 0x0051) {
                   8031:                                pio[REG16(DX)].conv_mode = true;
                   8032:                                REG8(AL) = 0x00;
                   8033:                        } else {
                   8034:                                REG8(AL) = 0x01;
                   8035:                        }
                   8036:                } else {
                   8037:                        REG8(AL) = 0x02;
                   8038:                }
                   8039:                break;
                   8040:        case 0x01:
                   8041:                if(REG16(DX) < 3) {
                   8042:                        if(pio[REG16(DX)].conv_mode) {
                   8043:                                REG16(BX) = 0x0051;
                   8044:                        } else {
                   8045:                                REG16(BX) = 0x0001;
                   8046:                        }
                   8047:                        REG8(AL) = 0x00;
                   8048:                } else {
                   8049:                        REG8(AL) = 0x02;
                   8050:                }
                   8051:                break;
                   8052:        default:
                   8053:                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));
                   8054:                break;
                   8055:        }
                   8056: }
                   8057: 
                   8058: inline void pcbios_int_17h_51h()
                   8059: {
                   8060:        if(REG8(DH) >= 0x21 && REG8(DH) <= 0x7e && REG8(DL) >= 0x21 && REG8(DL) <= 0x7e) {
                   8061:                REG16(DX) = pcbios_printer_jis2sjis(REG16(DX));
                   8062:        } else {
                   8063:                REG16(DX) = 0x0000;
                   8064:        }
                   8065: }
                   8066: 
                   8067: inline void pcbios_int_17h_52h()
                   8068: {
                   8069:        if(((REG8(DH) >= 0x81 && REG8(DH) <= 0x9f) || (REG8(DH) >= 0xe0 && REG8(DH) <= 0xfc)) && (REG8(DL) >= 0x40 && REG8(DL) <= 0xfc && REG8(DL) != 0x7f)) {
                   8070:                REG16(DX) = pcbios_printer_sjis2jis(REG16(DX));
                   8071:        } else {
                   8072:                REG16(DX) = 0x0000;
                   8073:        }
                   8074: }
                   8075: 
                   8076: inline void pcbios_int_17h_84h()
                   8077: {
                   8078:        if(REG16(DX) < 3) {
                   8079:                if(pio[REG16(DX)].jis_mode) {
                   8080:                        printer_out(REG16(DX), 0x1c);
                   8081:                        printer_out(REG16(DX), 0x2e);
                   8082:                        pio[REG16(DX)].jis_mode = false;
                   8083:                }
                   8084:                printer_out(REG16(DX), REG8(AL));
                   8085:                REG8(AH) = 0xd0;
                   8086:        }
                   8087: }
                   8088: 
                   8089: inline void pcbios_int_17h_85h()
                   8090: {
                   8091:        pio[0].conv_mode = (REG8(AL) == 0x00);
                   8092: }
                   8093: 
1.1       root     8094: inline void pcbios_int_1ah_00h()
                   8095: {
1.1.1.19  root     8096:        pcbios_update_daily_timer_counter(timeGetTime());
                   8097:        REG16(CX) = *(UINT16 *)(mem + 0x46e);
                   8098:        REG16(DX) = *(UINT16 *)(mem + 0x46c);
                   8099:        REG8(AL) = mem[0x470];
                   8100:        mem[0x470] = 0;
1.1       root     8101: }
                   8102: 
                   8103: inline int to_bcd(int t)
                   8104: {
                   8105:        int u = (t % 100) / 10;
                   8106:        return (u << 4) | (t % 10);
                   8107: }
                   8108: 
                   8109: inline void pcbios_int_1ah_02h()
                   8110: {
                   8111:        SYSTEMTIME time;
                   8112:        
                   8113:        GetLocalTime(&time);
                   8114:        REG8(CH) = to_bcd(time.wHour);
                   8115:        REG8(CL) = to_bcd(time.wMinute);
                   8116:        REG8(DH) = to_bcd(time.wSecond);
                   8117:        REG8(DL) = 0x00;
                   8118: }
                   8119: 
                   8120: inline void pcbios_int_1ah_04h()
                   8121: {
                   8122:        SYSTEMTIME time;
                   8123:        
                   8124:        GetLocalTime(&time);
                   8125:        REG8(CH) = to_bcd(time.wYear / 100);
                   8126:        REG8(CL) = to_bcd(time.wYear);
                   8127:        REG8(DH) = to_bcd(time.wMonth);
                   8128:        REG8(DL) = to_bcd(time.wDay);
                   8129: }
                   8130: 
                   8131: inline void pcbios_int_1ah_0ah()
                   8132: {
                   8133:        SYSTEMTIME time;
                   8134:        FILETIME file_time;
                   8135:        WORD dos_date, dos_time;
                   8136:        
                   8137:        GetLocalTime(&time);
                   8138:        SystemTimeToFileTime(&time, &file_time);
                   8139:        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   8140:        REG16(CX) = dos_date;
                   8141: }
                   8142: 
                   8143: // msdos system call
                   8144: 
                   8145: inline void msdos_int_21h_00h()
                   8146: {
1.1.1.3   root     8147:        msdos_process_terminate(SREG(CS), retval, 1);
1.1       root     8148: }
                   8149: 
1.1.1.35  root     8150: DWORD WINAPI msdos_int_21h_01h_thread(LPVOID)
1.1       root     8151: {
                   8152:        REG8(AL) = msdos_getche();
1.1.1.33  root     8153:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     8154:        
1.1.1.35  root     8155: #ifdef USE_SERVICE_THREAD
                   8156:        service_exit = true;
                   8157: #endif
                   8158:        return(0);
                   8159: }
                   8160: 
                   8161: inline void msdos_int_21h_01h()
                   8162: {
                   8163: #ifdef USE_SERVICE_THREAD
                   8164:        start_service_loop(msdos_int_21h_01h_thread);
                   8165: #else
                   8166:        msdos_int_21h_01h_thread(NULL);
                   8167:        REQUEST_HARDWRE_UPDATE();
                   8168: #endif
1.1       root     8169: }
                   8170: 
                   8171: inline void msdos_int_21h_02h()
                   8172: {
1.1.1.33  root     8173:        UINT8 data = REG8(DL);
                   8174:        msdos_putch(data);
                   8175:        REG8(AL) = data;
                   8176:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     8177: }
                   8178: 
                   8179: inline void msdos_int_21h_03h()
                   8180: {
                   8181:        REG8(AL) = msdos_aux_in();
                   8182: }
                   8183: 
                   8184: inline void msdos_int_21h_04h()
                   8185: {
                   8186:        msdos_aux_out(REG8(DL));
                   8187: }
                   8188: 
                   8189: inline void msdos_int_21h_05h()
                   8190: {
                   8191:        msdos_prn_out(REG8(DL));
                   8192: }
                   8193: 
                   8194: inline void msdos_int_21h_06h()
                   8195: {
                   8196:        if(REG8(DL) == 0xff) {
                   8197:                if(msdos_kbhit()) {
                   8198:                        REG8(AL) = msdos_getch();
1.1.1.3   root     8199: #if defined(HAS_I386)
                   8200:                        m_ZF = 0;
                   8201: #else
                   8202:                        m_ZeroVal = 1;
                   8203: #endif
1.1       root     8204:                } else {
                   8205:                        REG8(AL) = 0;
1.1.1.3   root     8206: #if defined(HAS_I386)
                   8207:                        m_ZF = 1;
                   8208: #else
                   8209:                        m_ZeroVal = 0;
                   8210: #endif
1.1.1.14  root     8211:                        maybe_idle();
1.1       root     8212:                }
                   8213:        } else {
1.1.1.33  root     8214:                UINT8 data = REG8(DL);
                   8215:                msdos_putch(data);
                   8216:                REG8(AL) = data;
1.1       root     8217:        }
                   8218: }
                   8219: 
1.1.1.35  root     8220: DWORD WINAPI msdos_int_21h_07h_thread(LPVOID)
1.1       root     8221: {
                   8222:        REG8(AL) = msdos_getch();
1.1.1.26  root     8223:        
1.1.1.35  root     8224: #ifdef USE_SERVICE_THREAD
                   8225:        service_exit = true;
                   8226: #endif
                   8227:        return(0);
1.1       root     8228: }
                   8229: 
1.1.1.35  root     8230: inline void msdos_int_21h_07h()
                   8231: {
                   8232: #ifdef USE_SERVICE_THREAD
                   8233:        start_service_loop(msdos_int_21h_07h_thread);
                   8234: #else
                   8235:        msdos_int_21h_07h_thread(NULL);
                   8236:        REQUEST_HARDWRE_UPDATE();
                   8237: #endif
                   8238: }
                   8239: 
                   8240: DWORD WINAPI msdos_int_21h_08h_thread(LPVOID)
1.1       root     8241: {
                   8242:        REG8(AL) = msdos_getch();
1.1.1.33  root     8243:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     8244:        
1.1.1.35  root     8245: #ifdef USE_SERVICE_THREAD
                   8246:        service_exit = true;
                   8247: #endif
                   8248:        return(0);
                   8249: }
                   8250: 
                   8251: inline void msdos_int_21h_08h()
                   8252: {
                   8253: #ifdef USE_SERVICE_THREAD
                   8254:        start_service_loop(msdos_int_21h_08h_thread);
                   8255: #else
                   8256:        msdos_int_21h_08h_thread(NULL);
                   8257:        REQUEST_HARDWRE_UPDATE();
                   8258: #endif
1.1       root     8259: }
                   8260: 
                   8261: inline void msdos_int_21h_09h()
                   8262: {
1.1.1.21  root     8263:        msdos_stdio_reopen();
                   8264:        
1.1.1.20  root     8265:        process_t *process = msdos_process_info_get(current_psp);
                   8266:        int fd = msdos_psp_get_file_table(1, current_psp);
                   8267:        
1.1.1.14  root     8268:        char *str = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   8269:        int len = 0;
1.1       root     8270:        
1.1.1.14  root     8271:        while(str[len] != '$' && len < 0x10000) {
                   8272:                len++;
                   8273:        }
1.1.1.20  root     8274:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     8275:                // stdout is redirected to file
1.1.1.20  root     8276:                msdos_write(fd, str, len);
1.1       root     8277:        } else {
                   8278:                for(int i = 0; i < len; i++) {
1.1.1.14  root     8279:                        msdos_putch(str[i]);
1.1       root     8280:                }
                   8281:        }
1.1.1.33  root     8282:        REG8(AL) = '$';
                   8283:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     8284: }
                   8285: 
1.1.1.35  root     8286: DWORD WINAPI msdos_int_21h_0ah_thread(LPVOID)
1.1       root     8287: {
1.1.1.3   root     8288:        int ofs = SREG_BASE(DS) + REG16(DX);
1.1       root     8289:        int max = mem[ofs] - 1;
                   8290:        UINT8 *buf = mem + ofs + 2;
                   8291:        int chr, p = 0;
                   8292:        
                   8293:        while((chr = msdos_getch()) != 0x0d) {
1.1.1.33  root     8294:                if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
1.1.1.26  root     8295:                        p = 0;
1.1.1.33  root     8296:                        msdos_putch(0x03);
                   8297:                        msdos_putch(0x0d);
                   8298:                        msdos_putch(0x0a);
1.1.1.26  root     8299:                        break;
1.1.1.33  root     8300:                } else if(ctrl_break_pressed) {
                   8301:                        // skip this byte
1.1.1.26  root     8302:                } else if(chr == 0x00) {
1.1       root     8303:                        // skip 2nd byte
                   8304:                        msdos_getch();
                   8305:                } else if(chr == 0x08) {
                   8306:                        // back space
                   8307:                        if(p > 0) {
                   8308:                                p--;
1.1.1.20  root     8309:                                if(msdos_ctrl_code_check(buf[p])) {
1.1.1.34  root     8310:                                        msdos_putch(0x08);
                   8311:                                        msdos_putch(0x08);
                   8312:                                        msdos_putch(0x20);
                   8313:                                        msdos_putch(0x20);
                   8314:                                        msdos_putch(0x08);
                   8315:                                        msdos_putch(0x08);
1.1.1.36  root     8316:                                } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
                   8317:                                        p--;
                   8318:                                        msdos_putch(0x08);
                   8319:                                        msdos_putch(0x08);
                   8320:                                        msdos_putch(0x20);
                   8321:                                        msdos_putch(0x20);
                   8322:                                        msdos_putch(0x08);
                   8323:                                        msdos_putch(0x08);
1.1.1.34  root     8324:                                } else {
                   8325:                                        msdos_putch(0x08);
                   8326:                                        msdos_putch(0x20);
                   8327:                                        msdos_putch(0x08);
                   8328:                                }
                   8329:                        }
                   8330:                } else if(chr == 0x1b) {
                   8331:                        // escape
                   8332:                        while(p > 0) {
                   8333:                                p--;
                   8334:                                if(msdos_ctrl_code_check(buf[p])) {
                   8335:                                        msdos_putch(0x08);
                   8336:                                        msdos_putch(0x08);
                   8337:                                        msdos_putch(0x20);
                   8338:                                        msdos_putch(0x20);
                   8339:                                        msdos_putch(0x08);
                   8340:                                        msdos_putch(0x08);
1.1.1.20  root     8341:                                } else {
1.1.1.34  root     8342:                                        msdos_putch(0x08);
                   8343:                                        msdos_putch(0x20);
                   8344:                                        msdos_putch(0x08);
1.1.1.20  root     8345:                                }
1.1       root     8346:                        }
                   8347:                } else if(p < max) {
                   8348:                        buf[p++] = chr;
                   8349:                        msdos_putch(chr);
                   8350:                }
                   8351:        }
                   8352:        buf[p] = 0x0d;
                   8353:        mem[ofs + 1] = p;
1.1.1.33  root     8354:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     8355:        
1.1.1.35  root     8356: #ifdef USE_SERVICE_THREAD
                   8357:        service_exit = true;
                   8358: #endif
                   8359:        return(0);
                   8360: }
                   8361: 
                   8362: inline void msdos_int_21h_0ah()
                   8363: {
                   8364:        if(mem[SREG_BASE(DS) + REG16(DX)] != 0x00) {
                   8365: #ifdef USE_SERVICE_THREAD
                   8366:                start_service_loop(msdos_int_21h_0ah_thread);
                   8367: #else
                   8368:                msdos_int_21h_0ah_thread(NULL);
                   8369:                REQUEST_HARDWRE_UPDATE();
                   8370: #endif
                   8371:        }
1.1       root     8372: }
                   8373: 
                   8374: inline void msdos_int_21h_0bh()
                   8375: {
                   8376:        if(msdos_kbhit()) {
                   8377:                REG8(AL) = 0xff;
                   8378:        } else {
                   8379:                REG8(AL) = 0x00;
1.1.1.14  root     8380:                maybe_idle();
1.1       root     8381:        }
1.1.1.33  root     8382:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     8383: }
                   8384: 
                   8385: inline void msdos_int_21h_0ch()
                   8386: {
                   8387:        // clear key buffer
1.1.1.21  root     8388:        msdos_stdio_reopen();
                   8389:        
1.1.1.20  root     8390:        process_t *process = msdos_process_info_get(current_psp);
                   8391:        int fd = msdos_psp_get_file_table(0, current_psp);
                   8392:        
                   8393:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     8394:                // stdin is redirected to file
                   8395:        } else {
                   8396:                while(msdos_kbhit()) {
                   8397:                        msdos_getch();
                   8398:                }
                   8399:        }
                   8400:        
                   8401:        switch(REG8(AL)) {
                   8402:        case 0x01:
                   8403:                msdos_int_21h_01h();
                   8404:                break;
                   8405:        case 0x06:
                   8406:                msdos_int_21h_06h();
                   8407:                break;
                   8408:        case 0x07:
                   8409:                msdos_int_21h_07h();
                   8410:                break;
                   8411:        case 0x08:
                   8412:                msdos_int_21h_08h();
                   8413:                break;
                   8414:        case 0x0a:
                   8415:                msdos_int_21h_0ah();
                   8416:                break;
                   8417:        default:
1.1.1.22  root     8418: //             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));
                   8419: //             REG16(AX) = 0x01;
                   8420: //             m_CF = 1;
1.1       root     8421:                break;
                   8422:        }
                   8423: }
                   8424: 
                   8425: inline void msdos_int_21h_0dh()
                   8426: {
                   8427: }
                   8428: 
                   8429: inline void msdos_int_21h_0eh()
                   8430: {
                   8431:        if(REG8(DL) < 26) {
                   8432:                _chdrive(REG8(DL) + 1);
                   8433:                msdos_cds_update(REG8(DL));
1.1.1.23  root     8434:                msdos_sda_update(current_psp);
1.1       root     8435:        }
                   8436:        REG8(AL) = 26; // zdrive
                   8437: }
                   8438: 
1.1.1.14  root     8439: inline void msdos_int_21h_0fh()
                   8440: {
                   8441:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8442:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   8443:        char *path = msdos_fcb_path(fcb);
                   8444:        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     8445:        
1.1.1.14  root     8446:        if(hFile == INVALID_HANDLE_VALUE) {
                   8447:                REG8(AL) = 0xff;
                   8448:        } else {
                   8449:                REG8(AL) = 0;
                   8450:                fcb->current_block = 0;
                   8451:                fcb->record_size = 128;
                   8452:                fcb->file_size = GetFileSize(hFile, NULL);
                   8453:                fcb->handle = hFile;
                   8454:                fcb->cur_record = 0;
                   8455:        }
                   8456: }
                   8457: 
                   8458: inline void msdos_int_21h_10h()
                   8459: {
                   8460:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8461:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   8462:        
                   8463:        REG8(AL) = CloseHandle(fcb->handle) ? 0 : 0xff;
                   8464: }
                   8465: 
1.1       root     8466: inline void msdos_int_21h_11h()
                   8467: {
1.1.1.3   root     8468:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8469:        fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     8470:        
                   8471:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     8472:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   8473:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   8474:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     8475:        char *path = msdos_fcb_path(fcb);
                   8476:        WIN32_FIND_DATA fd;
                   8477:        
1.1.1.13  root     8478:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   8479:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   8480:                FindClose(dtainfo->find_handle);
                   8481:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     8482:        }
                   8483:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     8484:        dtainfo->allowable_mask = (ext_fcb->flag == 0xff) ? ext_fcb->attribute : 0x20;
                   8485:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     8486:        
1.1.1.14  root     8487:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   8488:                dtainfo->allowable_mask &= ~8;
1.1       root     8489:        }
1.1.1.14  root     8490:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   8491:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     8492:                      !msdos_find_file_has_8dot3name(&fd)) {
                   8493:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   8494:                                FindClose(dtainfo->find_handle);
                   8495:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     8496:                                break;
                   8497:                        }
                   8498:                }
                   8499:        }
1.1.1.13  root     8500:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     8501:                if(ext_fcb->flag == 0xff) {
                   8502:                        ext_find->flag = 0xff;
                   8503:                        memset(ext_find->reserved, 0, 5);
                   8504:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   8505:                }
                   8506:                find->drive = _getdrive();
1.1.1.13  root     8507:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     8508:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   8509:                find->nt_res = 0;
                   8510:                msdos_find_file_conv_local_time(&fd);
                   8511:                find->create_time_ms = 0;
                   8512:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   8513:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   8514:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   8515:                find->cluster_hi = find->cluster_lo = 0;
                   8516:                find->file_size = fd.nFileSizeLow;
                   8517:                REG8(AL) = 0x00;
1.1.1.14  root     8518:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     8519:                if(ext_fcb->flag == 0xff) {
                   8520:                        ext_find->flag = 0xff;
                   8521:                        memset(ext_find->reserved, 0, 5);
                   8522:                        ext_find->attribute = 8;
                   8523:                }
                   8524:                find->drive = _getdrive();
                   8525:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   8526:                find->attribute = 8;
                   8527:                find->nt_res = 0;
                   8528:                msdos_find_file_conv_local_time(&fd);
                   8529:                find->create_time_ms = 0;
                   8530:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   8531:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   8532:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   8533:                find->cluster_hi = find->cluster_lo = 0;
                   8534:                find->file_size = 0;
1.1.1.14  root     8535:                dtainfo->allowable_mask &= ~8;
1.1       root     8536:                REG8(AL) = 0x00;
                   8537:        } else {
                   8538:                REG8(AL) = 0xff;
                   8539:        }
                   8540: }
                   8541: 
                   8542: inline void msdos_int_21h_12h()
                   8543: {
1.1.1.3   root     8544:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1.1.14  root     8545: //     fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     8546:        
                   8547:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     8548:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   8549:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   8550:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     8551:        WIN32_FIND_DATA fd;
                   8552:        
1.1.1.13  root     8553:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   8554:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   8555:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     8556:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     8557:                              !msdos_find_file_has_8dot3name(&fd)) {
                   8558:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   8559:                                        FindClose(dtainfo->find_handle);
                   8560:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     8561:                                        break;
                   8562:                                }
                   8563:                        }
                   8564:                } else {
1.1.1.13  root     8565:                        FindClose(dtainfo->find_handle);
                   8566:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     8567:                }
                   8568:        }
1.1.1.13  root     8569:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     8570:                if(ext_fcb->flag == 0xff) {
                   8571:                        ext_find->flag = 0xff;
                   8572:                        memset(ext_find->reserved, 0, 5);
                   8573:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   8574:                }
                   8575:                find->drive = _getdrive();
1.1.1.13  root     8576:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     8577:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   8578:                find->nt_res = 0;
                   8579:                msdos_find_file_conv_local_time(&fd);
                   8580:                find->create_time_ms = 0;
                   8581:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   8582:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   8583:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   8584:                find->cluster_hi = find->cluster_lo = 0;
                   8585:                find->file_size = fd.nFileSizeLow;
                   8586:                REG8(AL) = 0x00;
1.1.1.14  root     8587:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     8588:                if(ext_fcb->flag == 0xff) {
                   8589:                        ext_find->flag = 0xff;
                   8590:                        memset(ext_find->reserved, 0, 5);
                   8591:                        ext_find->attribute = 8;
                   8592:                }
                   8593:                find->drive = _getdrive();
                   8594:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   8595:                find->attribute = 8;
                   8596:                find->nt_res = 0;
                   8597:                msdos_find_file_conv_local_time(&fd);
                   8598:                find->create_time_ms = 0;
                   8599:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   8600:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   8601:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   8602:                find->cluster_hi = find->cluster_lo = 0;
                   8603:                find->file_size = 0;
1.1.1.14  root     8604:                dtainfo->allowable_mask &= ~8;
1.1       root     8605:                REG8(AL) = 0x00;
                   8606:        } else {
                   8607:                REG8(AL) = 0xff;
                   8608:        }
                   8609: }
                   8610: 
                   8611: inline void msdos_int_21h_13h()
                   8612: {
1.1.1.3   root     8613:        if(remove(msdos_fcb_path((fcb_t *)(mem + SREG_BASE(DS) + REG16(DX))))) {
1.1       root     8614:                REG8(AL) = 0xff;
                   8615:        } else {
                   8616:                REG8(AL) = 0x00;
                   8617:        }
                   8618: }
                   8619: 
1.1.1.16  root     8620: inline void msdos_int_21h_14h()
                   8621: {
                   8622:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8623:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   8624:        process_t *process = msdos_process_info_get(current_psp);
                   8625:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   8626:        DWORD num = 0;
                   8627:        
                   8628:        memset(mem + dta_laddr, 0, fcb->record_size);
                   8629:        if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   8630:                REG8(AL) = 1;
                   8631:        } else {
                   8632:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   8633:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   8634:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   8635:                REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   8636:        }
                   8637: }
                   8638: 
                   8639: inline void msdos_int_21h_15h()
1.1.1.14  root     8640: {
                   8641:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8642:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.16  root     8643:        process_t *process = msdos_process_info_get(current_psp);
                   8644:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   8645:        DWORD num = 0;
1.1.1.14  root     8646:        
1.1.1.16  root     8647:        if(!WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   8648:                REG8(AL) = 1;
                   8649:        } else {
                   8650:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   8651:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   8652:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   8653:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   8654:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   8655:        }
                   8656: }
                   8657: 
                   8658: inline void msdos_int_21h_16h()
                   8659: {
                   8660:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8661:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.14  root     8662:        char *path = msdos_fcb_path(fcb);
                   8663:        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     8664:        
1.1.1.14  root     8665:        if(hFile == INVALID_HANDLE_VALUE) {
                   8666:                REG8(AL) = 0xff;
                   8667:        } else {
                   8668:                REG8(AL) = 0;
                   8669:                fcb->current_block = 0;
                   8670:                fcb->record_size = 128;
                   8671:                fcb->file_size = 0;
                   8672:                fcb->handle = hFile;
                   8673:                fcb->cur_record = 0;
                   8674:        }
                   8675: }
                   8676: 
1.1.1.16  root     8677: inline void msdos_int_21h_17h()
                   8678: {
                   8679:        ext_fcb_t *ext_fcb_src = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8680:        fcb_t *fcb_src = (fcb_t *)(ext_fcb_src + (ext_fcb_src->flag == 0xff ? 1 : 0));
                   8681:        char *path_src = msdos_fcb_path(fcb_src);
                   8682:        ext_fcb_t *ext_fcb_dst = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + 16);
                   8683:        fcb_t *fcb_dst = (fcb_t *)(ext_fcb_dst + (ext_fcb_dst->flag == 0xff ? 1 : 0));
                   8684:        char *path_dst = msdos_fcb_path(fcb_dst);
                   8685:        
                   8686:        if(rename(path_src, path_dst)) {
                   8687:                REG8(AL) = 0xff;
                   8688:        } else {
                   8689:                REG8(AL) = 0;
                   8690:        }
                   8691: }
                   8692: 
1.1       root     8693: inline void msdos_int_21h_18h()
                   8694: {
                   8695:        REG8(AL) = 0x00;
                   8696: }
                   8697: 
                   8698: inline void msdos_int_21h_19h()
                   8699: {
                   8700:        REG8(AL) = _getdrive() - 1;
                   8701: }
                   8702: 
                   8703: inline void msdos_int_21h_1ah()
                   8704: {
                   8705:        process_t *process = msdos_process_info_get(current_psp);
                   8706:        
                   8707:        process->dta.w.l = REG16(DX);
1.1.1.3   root     8708:        process->dta.w.h = SREG(DS);
1.1.1.23  root     8709:        msdos_sda_update(current_psp);
1.1       root     8710: }
                   8711: 
                   8712: inline void msdos_int_21h_1bh()
                   8713: {
                   8714:        int drive_num = _getdrive() - 1;
                   8715:        UINT16 seg, ofs;
                   8716:        
                   8717:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   8718:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   8719:                REG8(AL) = dpb->highest_sector_num + 1;
                   8720:                REG16(CX) = dpb->bytes_per_sector;
                   8721:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     8722:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     8723:        } else {
                   8724:                REG8(AL) = 0xff;
1.1.1.3   root     8725:                m_CF = 1;
1.1       root     8726:        }
                   8727: 
                   8728: }
                   8729: 
                   8730: inline void msdos_int_21h_1ch()
                   8731: {
                   8732:        int drive_num = REG8(DL) ? (REG8(DL) - 1) : (_getdrive() - 1);
                   8733:        UINT16 seg, ofs;
                   8734:        
                   8735:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   8736:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   8737:                REG8(AL) = dpb->highest_sector_num + 1;
                   8738:                REG16(CX) = dpb->bytes_per_sector;
                   8739:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     8740:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     8741:        } else {
                   8742:                REG8(AL) = 0xff;
1.1.1.3   root     8743:                m_CF = 1;
1.1       root     8744:        }
                   8745: 
                   8746: }
                   8747: 
                   8748: inline void msdos_int_21h_1dh()
                   8749: {
                   8750:        REG8(AL) = 0;
                   8751: }
                   8752: 
                   8753: inline void msdos_int_21h_1eh()
                   8754: {
                   8755:        REG8(AL) = 0;
                   8756: }
                   8757: 
                   8758: inline void msdos_int_21h_1fh()
                   8759: {
                   8760:        int drive_num = _getdrive() - 1;
                   8761:        UINT16 seg, ofs;
                   8762:        
                   8763:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   8764:                REG8(AL) = 0;
1.1.1.3   root     8765:                SREG(DS) = seg;
                   8766:                i386_load_segment_descriptor(DS);
1.1       root     8767:                REG16(BX) = ofs;
                   8768:        } else {
                   8769:                REG8(AL) = 0xff;
1.1.1.3   root     8770:                m_CF = 1;
1.1       root     8771:        }
                   8772: }
                   8773: 
                   8774: inline void msdos_int_21h_20h()
                   8775: {
                   8776:        REG8(AL) = 0;
                   8777: }
                   8778: 
1.1.1.14  root     8779: inline void msdos_int_21h_21h()
                   8780: {
                   8781:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8782:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   8783:        
                   8784:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   8785:                REG8(AL) = 1;
                   8786:        } else {
                   8787:                process_t *process = msdos_process_info_get(current_psp);
                   8788:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   8789:                memset(mem + dta_laddr, 0, fcb->record_size);
1.1.1.16  root     8790:                DWORD num = 0;
1.1.1.14  root     8791:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   8792:                        REG8(AL) = 1;
                   8793:                } else {
                   8794:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   8795:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     8796:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
1.1.1.14  root     8797:                }
                   8798:        }
                   8799: }
                   8800: 
                   8801: inline void msdos_int_21h_22h()
                   8802: {
                   8803:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8804:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   8805:        
                   8806:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   8807:                REG8(AL) = 0xff;
                   8808:        } else {
                   8809:                process_t *process = msdos_process_info_get(current_psp);
                   8810:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
1.1.1.16  root     8811:                DWORD num = 0;
1.1.1.14  root     8812:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL);
                   8813:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   8814:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   8815:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     8816:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
1.1.1.14  root     8817:        }
                   8818: }
                   8819: 
1.1.1.16  root     8820: inline void msdos_int_21h_23h()
                   8821: {
                   8822:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8823:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   8824:        char *path = msdos_fcb_path(fcb);
                   8825:        HANDLE hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   8826:        
                   8827:        if(hFile == INVALID_HANDLE_VALUE) {
                   8828:                REG8(AL) = 0xff;
                   8829:        } else {
                   8830:                UINT32 size = GetFileSize(hFile, NULL);
                   8831:                fcb->rand_record = size / fcb->record_size + ((size % fcb->record_size) != 0);
                   8832:                REG8(AL) = 0;
                   8833:        }
                   8834: }
                   8835: 
                   8836: inline void msdos_int_21h_24h()
                   8837: {
                   8838:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8839:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   8840:        
                   8841:        fcb->rand_record = fcb->current_block * fcb->record_size + fcb->cur_record;
                   8842: }
                   8843: 
1.1       root     8844: inline void msdos_int_21h_25h()
                   8845: {
                   8846:        *(UINT16 *)(mem + 4 * REG8(AL) + 0) = REG16(DX);
1.1.1.3   root     8847:        *(UINT16 *)(mem + 4 * REG8(AL) + 2) = SREG(DS);
1.1       root     8848: }
                   8849: 
                   8850: inline void msdos_int_21h_26h()
                   8851: {
                   8852:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   8853:        
                   8854:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
                   8855:        psp->first_mcb = REG16(DX) + 16;
                   8856:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   8857:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   8858:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   8859:        psp->parent_psp = 0;
                   8860: }
                   8861: 
1.1.1.16  root     8862: inline void msdos_int_21h_27h()
                   8863: {
                   8864:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8865:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   8866:        
                   8867:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   8868:                REG8(AL) = 1;
                   8869:        } else {
                   8870:                process_t *process = msdos_process_info_get(current_psp);
                   8871:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   8872:                memset(mem + dta_laddr, 0, fcb->record_size * REG16(CX));
                   8873:                DWORD num = 0;
                   8874:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL) || num == 0) {
                   8875:                        REG8(AL) = 1;
                   8876:                } else {
                   8877:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   8878:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   8879:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   8880:                        REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   8881:                }
                   8882:        }
                   8883: }
                   8884: 
                   8885: inline void msdos_int_21h_28h()
                   8886: {
                   8887:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8888:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   8889:        
                   8890:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   8891:                REG8(AL) = 0xff;
                   8892:        } else {
                   8893:                process_t *process = msdos_process_info_get(current_psp);
                   8894:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   8895:                DWORD num = 0;
                   8896:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL);
                   8897:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   8898:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   8899:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   8900:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   8901:                REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   8902:        }
                   8903: }
                   8904: 
1.1       root     8905: inline void msdos_int_21h_29h()
                   8906: {
1.1.1.20  root     8907:        int ofs = 0;//SREG_BASE(DS) + REG16(SI);
                   8908:        char buffer[1024], name[MAX_PATH], ext[MAX_PATH];
1.1       root     8909:        UINT8 drv = 0;
                   8910:        char sep_chars[] = ":.;,=+";
                   8911:        char end_chars[] = "\\<>|/\"[]";
                   8912:        char spc_chars[] = " \t";
                   8913:        
1.1.1.20  root     8914:        memcpy(buffer, mem + SREG_BASE(DS) + REG16(SI), 1023);
                   8915:        buffer[1023] = 0;
                   8916:        memset(name, 0x20, sizeof(name));
                   8917:        memset(ext, 0x20, sizeof(ext));
                   8918:        
1.1       root     8919:        if(REG8(AL) & 1) {
1.1.1.20  root     8920:                ofs += strspn((char *)(buffer + ofs), spc_chars);
                   8921:                if(my_strchr(sep_chars, buffer[ofs]) && buffer[ofs] != '\0') {
1.1       root     8922:                        ofs++;
                   8923:                }
                   8924:        }
1.1.1.20  root     8925:        ofs += strspn((char *)(buffer + ofs), spc_chars);
1.1       root     8926:        
1.1.1.24  root     8927:        if(buffer[ofs + 1] == ':') {
                   8928:                if(buffer[ofs] >= 'a' && buffer[ofs] <= 'z') {
                   8929:                        drv = buffer[ofs] - 'a' + 1;
1.1.1.20  root     8930:                        ofs += 2;
1.1.1.24  root     8931:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   8932:                                ofs++;
                   8933:                        }
                   8934:                } else if(buffer[ofs] >= 'A' && buffer[ofs] <= 'Z') {
                   8935:                        drv = buffer[ofs] - 'A' + 1;
1.1       root     8936:                        ofs += 2;
1.1.1.24  root     8937:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   8938:                                ofs++;
                   8939:                        }
1.1       root     8940:                }
                   8941:        }
1.1.1.20  root     8942:        for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   8943:                UINT8 c = buffer[ofs];
                   8944:                if(is_kanji) {
                   8945:                        is_kanji = 0;
                   8946:                } else if(msdos_lead_byte_check(c)) {
                   8947:                        is_kanji = 1;
                   8948:                } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     8949:                        break;
                   8950:                } else if(c >= 'a' && c <= 'z') {
                   8951:                        c -= 0x20;
                   8952:                }
                   8953:                ofs++;
                   8954:                name[i] = c;
                   8955:        }
1.1.1.20  root     8956:        if(buffer[ofs] == '.') {
1.1       root     8957:                ofs++;
1.1.1.20  root     8958:                for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   8959:                        UINT8 c = buffer[ofs];
                   8960:                        if(is_kanji) {
                   8961:                                is_kanji = 0;
                   8962:                        } else if(msdos_lead_byte_check(c)) {
                   8963:                                is_kanji = 1;
                   8964:                        } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     8965:                                break;
                   8966:                        } else if(c >= 'a' && c <= 'z') {
                   8967:                                c -= 0x20;
                   8968:                        }
                   8969:                        ofs++;
                   8970:                        ext[i] = c;
                   8971:                }
                   8972:        }
1.1.1.20  root     8973:        int si = REG16(SI) + ofs;
1.1.1.3   root     8974:        int ds = SREG(DS);
1.1       root     8975:        while(si > 0xffff) {
                   8976:                si -= 0x10;
                   8977:                ds++;
                   8978:        }
                   8979:        REG16(SI) = si;
1.1.1.3   root     8980:        SREG(DS) = ds;
                   8981:        i386_load_segment_descriptor(DS);
1.1       root     8982:        
1.1.1.3   root     8983:        UINT8 *fcb = mem + SREG_BASE(ES) + REG16(DI);
1.1.1.20  root     8984:        if(!(REG8(AL) & 2) || drv != 0) {
                   8985:                fcb[0] = drv;
                   8986:        }
                   8987:        if(!(REG8(AL) & 4) || name[0] != 0x20) {
                   8988:                memcpy(fcb + 1, name, 8);
                   8989:        }
                   8990:        if(!(REG8(AL) & 8) || ext[0] != 0x20) {
                   8991:                memcpy(fcb + 9, ext, 3);
                   8992:        }
                   8993:        for(int i = 1, found_star = 0; i < 1 + 8; i++) {
1.1       root     8994:                if(fcb[i] == '*') {
                   8995:                        found_star = 1;
                   8996:                }
                   8997:                if(found_star) {
                   8998:                        fcb[i] = '?';
                   8999:                }
                   9000:        }
1.1.1.20  root     9001:        for(int i = 9, found_star = 0; i < 9 + 3; i++) {
1.1       root     9002:                if(fcb[i] == '*') {
                   9003:                        found_star = 1;
                   9004:                }
                   9005:                if(found_star) {
                   9006:                        fcb[i] = '?';
                   9007:                }
                   9008:        }
                   9009:        
                   9010:        if(drv == 0 || (drv > 0 && drv <= 26 && (GetLogicalDrives() & ( 1 << (drv - 1) )))) {
                   9011:                if(memchr(fcb + 1, '?', 8 + 3)) {
                   9012:                        REG8(AL) = 0x01;
1.1.1.20  root     9013:                } else {
                   9014:                        REG8(AL) = 0x00;
1.1       root     9015:                }
                   9016:        } else {
                   9017:                REG8(AL) = 0xff;
                   9018:        }
                   9019: }
                   9020: 
                   9021: inline void msdos_int_21h_2ah()
                   9022: {
                   9023:        SYSTEMTIME sTime;
                   9024:        
                   9025:        GetLocalTime(&sTime);
                   9026:        REG16(CX) = sTime.wYear;
                   9027:        REG8(DH) = (UINT8)sTime.wMonth;
                   9028:        REG8(DL) = (UINT8)sTime.wDay;
                   9029:        REG8(AL) = (UINT8)sTime.wDayOfWeek;
                   9030: }
                   9031: 
                   9032: inline void msdos_int_21h_2bh()
                   9033: {
1.1.1.14  root     9034:        REG8(AL) = 0xff;
1.1       root     9035: }
                   9036: 
                   9037: inline void msdos_int_21h_2ch()
                   9038: {
                   9039:        SYSTEMTIME sTime;
                   9040:        
                   9041:        GetLocalTime(&sTime);
                   9042:        REG8(CH) = (UINT8)sTime.wHour;
                   9043:        REG8(CL) = (UINT8)sTime.wMinute;
                   9044:        REG8(DH) = (UINT8)sTime.wSecond;
1.1.1.14  root     9045:        REG8(DL) = (UINT8)sTime.wMilliseconds / 10;
1.1       root     9046: }
                   9047: 
                   9048: inline void msdos_int_21h_2dh()
                   9049: {
                   9050:        REG8(AL) = 0x00;
                   9051: }
                   9052: 
                   9053: inline void msdos_int_21h_2eh()
                   9054: {
                   9055:        process_t *process = msdos_process_info_get(current_psp);
                   9056:        
                   9057:        process->verify = REG8(AL);
                   9058: }
                   9059: 
                   9060: inline void msdos_int_21h_2fh()
                   9061: {
                   9062:        process_t *process = msdos_process_info_get(current_psp);
                   9063:        
                   9064:        REG16(BX) = process->dta.w.l;
1.1.1.3   root     9065:        SREG(ES) = process->dta.w.h;
                   9066:        i386_load_segment_descriptor(ES);
1.1       root     9067: }
                   9068: 
                   9069: inline void msdos_int_21h_30h()
                   9070: {
                   9071:        // Version Flag / OEM
1.1.1.27  root     9072:        if(REG8(AL) == 0x01) {
1.1.1.29  root     9073: #ifdef SUPPORT_HMA
                   9074:                REG16(BX) = 0x0000;
                   9075: #else
                   9076:                REG16(BX) = 0x1000; // DOS is in HMA
                   9077: #endif
1.1       root     9078:        } else {
1.1.1.27  root     9079:                // NOTE: EXDEB invites BL shows the machine type (0=PC-98, 1=PC/AT, 2=FMR),
                   9080:                // but this is not correct on Windows 98 SE
                   9081: //             REG16(BX) = 0xff01;     // OEM = Microsoft, PC/AT
                   9082:                REG16(BX) = 0xff00;     // OEM = Microsoft
1.1       root     9083:        }
1.1.1.27  root     9084:        REG16(CX) = 0x0000;
1.1.1.30  root     9085:        REG8(AL) = dos_major_version;   // 7
                   9086:        REG8(AH) = dos_minor_version;   // 10
1.1       root     9087: }
                   9088: 
                   9089: inline void msdos_int_21h_31h()
                   9090: {
1.1.1.29  root     9091:        try {
                   9092:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   9093:        } catch(...) {
                   9094:                // recover the broken mcb
                   9095:                int mcb_seg = current_psp - 1;
                   9096:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     9097:                
1.1.1.29  root     9098:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     9099:                        mcb->mz = 'M';
                   9100:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   9101:                        
1.1.1.29  root     9102:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.33  root     9103:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4));
1.1.1.29  root     9104:                        } else {
1.1.1.33  root     9105:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0);
1.1.1.29  root     9106:                        }
                   9107:                } else {
                   9108:                        mcb->mz = 'Z';
1.1.1.30  root     9109:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     9110:                }
                   9111:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   9112:        }
1.1       root     9113:        msdos_process_terminate(current_psp, REG8(AL) | 0x300, 0);
                   9114: }
                   9115: 
                   9116: inline void msdos_int_21h_32h()
                   9117: {
                   9118:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   9119:        UINT16 seg, ofs;
                   9120:        
                   9121:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9122:                REG8(AL) = 0;
1.1.1.3   root     9123:                SREG(DS) = seg;
                   9124:                i386_load_segment_descriptor(DS);
1.1       root     9125:                REG16(BX) = ofs;
                   9126:        } else {
                   9127:                REG8(AL) = 0xff;
1.1.1.3   root     9128:                m_CF = 1;
1.1       root     9129:        }
                   9130: }
                   9131: 
                   9132: inline void msdos_int_21h_33h()
                   9133: {
                   9134:        char path[MAX_PATH];
                   9135:        
                   9136:        switch(REG8(AL)) {
                   9137:        case 0x00:
1.1.1.33  root     9138:                REG8(DL) = ctrl_break_checking;
1.1       root     9139:                break;
                   9140:        case 0x01:
1.1.1.33  root     9141:                ctrl_break_checking = REG8(DL);
                   9142:                break;
                   9143:        case 0x02:
                   9144:                {
                   9145:                        UINT8 old = ctrl_break_checking;
                   9146:                        ctrl_break_checking = REG8(DL);
                   9147:                        REG8(DL) = old;
                   9148:                }
                   9149:                break;
                   9150:        case 0x03:
                   9151:        case 0x04:
                   9152:                // DOS 4.0+ - Unused
1.1       root     9153:                break;
                   9154:        case 0x05:
                   9155:                GetSystemDirectory(path, MAX_PATH);
                   9156:                if(path[0] >= 'a' && path[0] <= 'z') {
                   9157:                        REG8(DL) = path[0] - 'a' + 1;
                   9158:                } else {
                   9159:                        REG8(DL) = path[0] - 'A' + 1;
                   9160:                }
                   9161:                break;
                   9162:        case 0x06:
1.1.1.2   root     9163:                // MS-DOS version (7.10)
1.1       root     9164:                REG8(BL) = 7;
1.1.1.2   root     9165:                REG8(BH) = 10;
1.1       root     9166:                REG8(DL) = 0;
1.1.1.29  root     9167: #ifdef SUPPORT_HMA
                   9168:                REG8(DH) = 0x00;
                   9169: #else
                   9170:                REG8(DH) = 0x10; // DOS is in HMA
                   9171: #endif
1.1       root     9172:                break;
1.1.1.6   root     9173:        case 0x07:
                   9174:                if(REG8(DL) == 0) {
                   9175:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag &= ~0x20;
                   9176:                } else if(REG8(DL) == 1) {
                   9177:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag |= 0x20;
                   9178:                }
                   9179:                break;
1.1       root     9180:        default:
1.1.1.22  root     9181:                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     9182:                REG16(AX) = 0x01;
1.1.1.3   root     9183:                m_CF = 1;
1.1       root     9184:                break;
                   9185:        }
                   9186: }
                   9187: 
1.1.1.23  root     9188: inline void msdos_int_21h_34h()
                   9189: {
                   9190:        SREG(ES) = SDA_TOP >> 4;
                   9191:        i386_load_segment_descriptor(ES);
                   9192:        REG16(BX) = offsetof(sda_t, indos_flag);;
                   9193: }
                   9194: 
1.1       root     9195: inline void msdos_int_21h_35h()
                   9196: {
                   9197:        REG16(BX) = *(UINT16 *)(mem + 4 * REG8(AL) + 0);
1.1.1.3   root     9198:        SREG(ES) = *(UINT16 *)(mem + 4 * REG8(AL) + 2);
                   9199:        i386_load_segment_descriptor(ES);
1.1       root     9200: }
                   9201: 
                   9202: inline void msdos_int_21h_36h()
                   9203: {
                   9204:        struct _diskfree_t df = {0};
                   9205:        
                   9206:        if(_getdiskfree(REG8(DL), &df) == 0) {
                   9207:                REG16(AX) = (UINT16)df.sectors_per_cluster;
                   9208:                REG16(CX) = (UINT16)df.bytes_per_sector;
1.1.1.13  root     9209:                REG16(BX) = df.avail_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.avail_clusters;
                   9210:                REG16(DX) = df.total_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.total_clusters;
1.1       root     9211:        } else {
                   9212:                REG16(AX) = 0xffff;
                   9213:        }
                   9214: }
                   9215: 
                   9216: inline void msdos_int_21h_37h()
                   9217: {
1.1.1.22  root     9218:        static UINT8 dev_flag = 0xff;
1.1       root     9219:        
                   9220:        switch(REG8(AL)) {
                   9221:        case 0x00:
1.1.1.22  root     9222:                {
                   9223:                        process_t *process = msdos_process_info_get(current_psp);
                   9224:                        REG8(AL) = 0x00;
                   9225:                        REG8(DL) = process->switchar;
                   9226:                }
1.1       root     9227:                break;
                   9228:        case 0x01:
1.1.1.22  root     9229:                {
                   9230:                        process_t *process = msdos_process_info_get(current_psp);
                   9231:                        REG8(AL) = 0x00;
                   9232:                        process->switchar = REG8(DL);
1.1.1.23  root     9233:                        msdos_sda_update(current_psp);
1.1.1.22  root     9234:                }
                   9235:                break;
                   9236:        case 0x02:
                   9237:                REG8(DL) = dev_flag;
                   9238:                break;
                   9239:        case 0x03:
                   9240:                dev_flag = REG8(DL);
                   9241:                break;
                   9242:        case 0xd0:
                   9243:        case 0xd1:
                   9244:        case 0xd2:
                   9245:        case 0xd3:
                   9246:        case 0xd4:
                   9247:        case 0xd5:
                   9248:        case 0xd6:
                   9249:        case 0xd7:
                   9250:        case 0xdc:
                   9251:        case 0xdd:
                   9252:        case 0xde:
                   9253:        case 0xdf:
                   9254:                // diet ???
                   9255:                REG16(AX) = 1;
1.1       root     9256:                break;
                   9257:        default:
1.1.1.22  root     9258:                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     9259:                REG16(AX) = 1;
                   9260:                break;
                   9261:        }
                   9262: }
                   9263: 
1.1.1.19  root     9264: int get_country_info(country_info_t *ci)
1.1.1.17  root     9265: {
                   9266:        char LCdata[80];
                   9267:        
1.1.1.19  root     9268:        ZeroMemory(ci, offsetof(country_info_t, reserved));
1.1.1.17  root     9269:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRDIGITS, LCdata, sizeof(LCdata));
                   9270:        ci->currency_dec_digits = atoi(LCdata);
                   9271:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRENCY, LCdata, sizeof(LCdata));
                   9272:        ci->currency_format = *LCdata - '0';
                   9273:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IDATE, LCdata, sizeof(LCdata));
                   9274:        ci->date_format = *LCdata - '0';
                   9275:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SCURRENCY, LCdata, sizeof(LCdata));
                   9276:        memcpy(&ci->currency_symbol, LCdata, 4);
                   9277:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDATE, LCdata, sizeof(LCdata));
                   9278:        *ci->date_sep = *LCdata;
                   9279:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, LCdata, sizeof(LCdata));
                   9280:        *ci->dec_sep = *LCdata;
                   9281:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SLIST, LCdata, sizeof(LCdata));
                   9282:        *ci->list_sep = *LCdata;
                   9283:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, LCdata, sizeof(LCdata));
                   9284:        *ci->thou_sep = *LCdata;
                   9285:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIME, LCdata, sizeof(LCdata));
                   9286:        *ci->time_sep = *LCdata;
                   9287:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, LCdata, sizeof(LCdata));
                   9288:        if(strchr(LCdata, 'H') != NULL) {
                   9289:                ci->time_format = 1;
                   9290:        }
1.1.1.27  root     9291:        ci->case_map.w.l = 0x000a; // dummy case map routine is at fffd:000a
1.1.1.24  root     9292:        ci->case_map.w.h = 0xfffd;
1.1.1.17  root     9293:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICOUNTRY, LCdata, sizeof(LCdata));
                   9294:        return atoi(LCdata);
                   9295: }
                   9296: 
1.1.1.14  root     9297: inline void msdos_int_21h_38h()
                   9298: {
                   9299:        switch(REG8(AL)) {
                   9300:        case 0x00:
1.1.1.19  root     9301:                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1.1.14  root     9302:                break;
                   9303:        default:
1.1.1.22  root     9304:                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     9305:                REG16(AX) = 2;
                   9306:                m_CF = 1;
                   9307:                break;
                   9308:        }
                   9309: }
                   9310: 
1.1       root     9311: inline void msdos_int_21h_39h(int lfn)
                   9312: {
1.1.1.3   root     9313:        if(_mkdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     9314:                REG16(AX) = errno;
1.1.1.3   root     9315:                m_CF = 1;
1.1       root     9316:        }
                   9317: }
                   9318: 
                   9319: inline void msdos_int_21h_3ah(int lfn)
                   9320: {
1.1.1.3   root     9321:        if(_rmdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     9322:                REG16(AX) = errno;
1.1.1.3   root     9323:                m_CF = 1;
1.1       root     9324:        }
                   9325: }
                   9326: 
                   9327: inline void msdos_int_21h_3bh(int lfn)
                   9328: {
1.1.1.3   root     9329:        if(_chdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1.1.17  root     9330:                REG16(AX) = 3;  // must be 3 (path not found)
1.1.1.3   root     9331:                m_CF = 1;
1.1       root     9332:        }
                   9333: }
                   9334: 
                   9335: inline void msdos_int_21h_3ch()
                   9336: {
1.1.1.3   root     9337:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     9338:        int attr = GetFileAttributes(path);
1.1.1.37  root     9339:        int fd = -1;
1.1.1.11  root     9340:        UINT16 info;
1.1.1.37  root     9341:        int sio_port = 0;
                   9342:        int lpt_port = 0;
1.1       root     9343:        
1.1.1.11  root     9344:        if(msdos_is_con_path(path)) {
                   9345:                fd = _open("CON", _O_WRONLY | _O_BINARY);
                   9346:                info = 0x80d3;
1.1.1.37  root     9347:        } else if((sio_port = msdos_is_comm_path(path)) != 0) {
1.1.1.38! root     9348:                fd = _open("NUL", _O_RDWR | _O_BINARY);
1.1.1.14  root     9349:                info = 0x80d3;
1.1.1.37  root     9350:                msdos_set_comm_params(sio_port, path);
                   9351:        } else if((lpt_port = msdos_is_prn_path(path)) != 0) {
                   9352:                fd = _open("NUL", _O_WRONLY | _O_BINARY);
                   9353:                info = 0xa8c0;
1.1.1.29  root     9354:        } else if(msdos_is_device_path(path)) {
1.1.1.20  root     9355:                fd = _open("NUL", _O_WRONLY | _O_BINARY);
                   9356:                info = 0x80d3;
1.1       root     9357:        } else {
                   9358:                fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
1.1.1.11  root     9359:                info = msdos_drive_number(path);
1.1       root     9360:        }
                   9361:        if(fd != -1) {
                   9362:                if(attr == -1) {
                   9363:                        attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   9364:                }
                   9365:                SetFileAttributes(path, attr);
                   9366:                REG16(AX) = fd;
1.1.1.37  root     9367:                msdos_file_handler_open(fd, path, _isatty(fd), 2, info, current_psp, sio_port, lpt_port);
1.1.1.20  root     9368:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     9369:        } else {
                   9370:                REG16(AX) = errno;
1.1.1.3   root     9371:                m_CF = 1;
1.1       root     9372:        }
                   9373: }
                   9374: 
                   9375: inline void msdos_int_21h_3dh()
                   9376: {
1.1.1.3   root     9377:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     9378:        int mode = REG8(AL) & 0x03;
1.1.1.37  root     9379:        int fd = -1;
1.1.1.11  root     9380:        UINT16 info;
1.1.1.37  root     9381:        int sio_port = 0;
                   9382:        int lpt_port = 0;
1.1       root     9383:        
                   9384:        if(mode < 0x03) {
1.1.1.11  root     9385:                if(msdos_is_con_path(path)) {
1.1.1.13  root     9386:                        fd = msdos_open("CON", file_mode[mode].mode);
1.1.1.11  root     9387:                        info = 0x80d3;
1.1.1.37  root     9388:                } else if((sio_port = msdos_is_comm_path(path)) != 0) {
                   9389:                        fd = msdos_open("NUL", file_mode[mode].mode);
1.1.1.14  root     9390:                        info = 0x80d3;
1.1.1.37  root     9391:                        msdos_set_comm_params(sio_port, path);
                   9392:                } else if((lpt_port = msdos_is_prn_path(path)) != 0) {
                   9393:                        fd = _open("NUL", file_mode[mode].mode);
                   9394:                        info = 0xa8c0;
1.1.1.29  root     9395:                } else if(msdos_is_device_path(path)) {
1.1.1.20  root     9396:                        fd = msdos_open("NUL", file_mode[mode].mode);
                   9397:                        info = 0x80d3;
1.1.1.11  root     9398:                } else {
1.1.1.13  root     9399:                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     9400:                        info = msdos_drive_number(path);
                   9401:                }
1.1       root     9402:                if(fd != -1) {
                   9403:                        REG16(AX) = fd;
1.1.1.37  root     9404:                        msdos_file_handler_open(fd, path, _isatty(fd), mode, info, current_psp, sio_port, lpt_port);
1.1.1.20  root     9405:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     9406:                } else {
                   9407:                        REG16(AX) = errno;
1.1.1.3   root     9408:                        m_CF = 1;
1.1       root     9409:                }
                   9410:        } else {
                   9411:                REG16(AX) = 0x0c;
1.1.1.3   root     9412:                m_CF = 1;
1.1       root     9413:        }
                   9414: }
                   9415: 
                   9416: inline void msdos_int_21h_3eh()
                   9417: {
                   9418:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     9419:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     9420:        
1.1.1.20  root     9421:        if(fd < process->max_files && file_handler[fd].valid) {
                   9422:                _close(fd);
                   9423:                msdos_file_handler_close(fd);
                   9424:                msdos_psp_set_file_table(REG16(BX), 0x0ff, current_psp);
1.1       root     9425:        } else {
                   9426:                REG16(AX) = 0x06;
1.1.1.3   root     9427:                m_CF = 1;
1.1       root     9428:        }
                   9429: }
                   9430: 
1.1.1.35  root     9431: DWORD WINAPI msdos_int_21h_3fh_thread(LPVOID)
                   9432: {
                   9433:        UINT8 *buf = mem + SREG_BASE(DS) + REG16(DX);
                   9434:        int max = REG16(CX);
                   9435:        int p = 0;
                   9436:        
                   9437:        while(max > p) {
                   9438:                int chr = msdos_getch();
                   9439:                
                   9440:                if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
                   9441:                        p = 0;
                   9442:                        buf[p++] = 0x0d;
                   9443:                        if(max > p) {
                   9444:                                buf[p++] = 0x0a;
                   9445:                        }
                   9446:                        msdos_putch(0x03);
                   9447:                        msdos_putch(0x0d);
                   9448:                        msdos_putch(0x0a);
                   9449:                        break;
                   9450:                } else if(ctrl_break_pressed) {
                   9451:                        // skip this byte
                   9452:                } else if(chr == 0x00) {
                   9453:                        // skip 2nd byte
                   9454:                        msdos_getch();
                   9455:                } else if(chr == 0x0d) {
                   9456:                        // carriage return
                   9457:                        buf[p++] = 0x0d;
                   9458:                        if(max > p) {
                   9459:                                buf[p++] = 0x0a;
                   9460:                        }
                   9461:                        msdos_putch('\n');
                   9462:                        break;
                   9463:                } else if(chr == 0x08) {
                   9464:                        // back space
                   9465:                        if(p > 0) {
                   9466:                                p--;
                   9467:                                if(msdos_ctrl_code_check(buf[p])) {
                   9468:                                        msdos_putch(0x08);
                   9469:                                        msdos_putch(0x08);
                   9470:                                        msdos_putch(0x20);
                   9471:                                        msdos_putch(0x20);
                   9472:                                        msdos_putch(0x08);
                   9473:                                        msdos_putch(0x08);
1.1.1.36  root     9474:                                } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
                   9475:                                        p--;
                   9476:                                        msdos_putch(0x08);
                   9477:                                        msdos_putch(0x08);
                   9478:                                        msdos_putch(0x20);
                   9479:                                        msdos_putch(0x20);
                   9480:                                        msdos_putch(0x08);
                   9481:                                        msdos_putch(0x08);
1.1.1.35  root     9482:                                } else {
                   9483:                                        msdos_putch(0x08);
                   9484:                                        msdos_putch(0x20);
                   9485:                                        msdos_putch(0x08);
                   9486:                                }
                   9487:                        }
                   9488:                } else if(chr == 0x1b) {
                   9489:                        // escape
                   9490:                        while(p > 0) {
                   9491:                                p--;
                   9492:                                if(msdos_ctrl_code_check(buf[p])) {
                   9493:                                        msdos_putch(0x08);
                   9494:                                        msdos_putch(0x08);
                   9495:                                        msdos_putch(0x20);
                   9496:                                        msdos_putch(0x20);
                   9497:                                        msdos_putch(0x08);
                   9498:                                        msdos_putch(0x08);
                   9499:                                } else {
                   9500:                                        msdos_putch(0x08);
                   9501:                                        msdos_putch(0x20);
                   9502:                                        msdos_putch(0x08);
                   9503:                                }
                   9504:                        }
                   9505:                } else {
                   9506:                        buf[p++] = chr;
                   9507:                        msdos_putch(chr);
                   9508:                }
                   9509:        }
                   9510:        REG16(AX) = p;
                   9511:        
                   9512: #ifdef USE_SERVICE_THREAD
                   9513:        service_exit = true;
                   9514: #endif
                   9515:        return(0);
                   9516: }
                   9517: 
1.1       root     9518: inline void msdos_int_21h_3fh()
                   9519: {
                   9520:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     9521:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     9522:        
1.1.1.20  root     9523:        if(fd < process->max_files && file_handler[fd].valid) {
                   9524:                if(file_mode[file_handler[fd].mode].in) {
                   9525:                        if(file_handler[fd].atty) {
1.1       root     9526:                                // BX is stdin or is redirected to stdin
1.1.1.35  root     9527:                                if(REG16(CX) != 0) {
                   9528: #ifdef USE_SERVICE_THREAD
                   9529:                                        start_service_loop(msdos_int_21h_3fh_thread);
                   9530: #else
                   9531:                                        msdos_int_21h_3fh_thread(NULL);
                   9532:                                        REQUEST_HARDWRE_UPDATE();
                   9533: #endif
                   9534:                                } else {
                   9535:                                        REG16(AX) = 0;
1.1       root     9536:                                }
                   9537:                        } else {
1.1.1.37  root     9538:                                REG16(AX) = msdos_read(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     9539:                        }
                   9540:                } else {
                   9541:                        REG16(AX) = 0x05;
1.1.1.3   root     9542:                        m_CF = 1;
1.1       root     9543:                }
                   9544:        } else {
                   9545:                REG16(AX) = 0x06;
1.1.1.3   root     9546:                m_CF = 1;
1.1       root     9547:        }
                   9548: }
                   9549: 
                   9550: inline void msdos_int_21h_40h()
                   9551: {
                   9552:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     9553:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     9554:        
1.1.1.20  root     9555:        if(fd < process->max_files && file_handler[fd].valid) {
                   9556:                if(file_mode[file_handler[fd].mode].out) {
1.1       root     9557:                        if(REG16(CX)) {
1.1.1.20  root     9558:                                if(file_handler[fd].atty) {
1.1       root     9559:                                        // BX is stdout/stderr or is redirected to stdout
                   9560:                                        for(int i = 0; i < REG16(CX); i++) {
1.1.1.3   root     9561:                                                msdos_putch(mem[SREG_BASE(DS) + REG16(DX) + i]);
1.1       root     9562:                                        }
                   9563:                                        REG16(AX) = REG16(CX);
                   9564:                                } else {
1.1.1.20  root     9565:                                        REG16(AX) = msdos_write(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     9566:                                }
                   9567:                        } else {
1.1.1.20  root     9568:                                UINT32 pos = _tell(fd);
                   9569:                                _lseek(fd, 0, SEEK_END);
                   9570:                                UINT32 size = _tell(fd);
1.1.1.12  root     9571:                                if(pos < size) {
1.1.1.20  root     9572:                                        _lseek(fd, pos, SEEK_SET);
                   9573:                                        SetEndOfFile((HANDLE)_get_osfhandle(fd));
1.1.1.12  root     9574:                                } else {
                   9575:                                        for(UINT32 i = size; i < pos; i++) {
                   9576:                                                UINT8 tmp = 0;
1.1.1.23  root     9577:                                                msdos_write(fd, &tmp, 1);
1.1.1.12  root     9578:                                        }
1.1.1.20  root     9579:                                        _lseek(fd, pos, SEEK_SET);
1.1       root     9580:                                }
1.1.1.23  root     9581:                                REG16(AX) = 0;
1.1       root     9582:                        }
                   9583:                } else {
                   9584:                        REG16(AX) = 0x05;
1.1.1.3   root     9585:                        m_CF = 1;
1.1       root     9586:                }
                   9587:        } else {
                   9588:                REG16(AX) = 0x06;
1.1.1.3   root     9589:                m_CF = 1;
1.1       root     9590:        }
                   9591: }
                   9592: 
                   9593: inline void msdos_int_21h_41h(int lfn)
                   9594: {
1.1.1.3   root     9595:        if(remove(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     9596:                REG16(AX) = errno;
1.1.1.3   root     9597:                m_CF = 1;
1.1       root     9598:        }
                   9599: }
                   9600: 
                   9601: inline void msdos_int_21h_42h()
                   9602: {
                   9603:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     9604:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     9605:        
1.1.1.20  root     9606:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     9607:                if(REG8(AL) < 0x03) {
1.1.1.35  root     9608:                        static const int ptrname[] = { SEEK_SET, SEEK_CUR, SEEK_END };
1.1.1.20  root     9609:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), ptrname[REG8(AL)]);
                   9610:                        UINT32 pos = _tell(fd);
1.1       root     9611:                        REG16(AX) = pos & 0xffff;
                   9612:                        REG16(DX) = (pos >> 16);
                   9613:                } else {
                   9614:                        REG16(AX) = 0x01;
1.1.1.3   root     9615:                        m_CF = 1;
1.1       root     9616:                }
                   9617:        } else {
                   9618:                REG16(AX) = 0x06;
1.1.1.3   root     9619:                m_CF = 1;
1.1       root     9620:        }
                   9621: }
                   9622: 
                   9623: inline void msdos_int_21h_43h(int lfn)
                   9624: {
1.1.1.3   root     9625:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1       root     9626:        int attr;
                   9627:        
1.1.1.14  root     9628:        if(!lfn && REG8(AL) > 2) {
                   9629:                REG16(AX) = 0x01;
                   9630:                m_CF = 1;
                   9631:                return;
                   9632:        }
                   9633:        switch(REG8(lfn ? BL : AL)) {
1.1       root     9634:        case 0x00:
                   9635:                if((attr = GetFileAttributes(path)) != -1) {
1.1.1.14  root     9636:                        REG16(CX) = (UINT16)msdos_file_attribute_create((UINT16)attr);
                   9637:                } else {
                   9638:                        REG16(AX) = (UINT16)GetLastError();
                   9639:                        m_CF = 1;
                   9640:                }
                   9641:                break;
                   9642:        case 0x01:
                   9643:                if(!SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)))) {
                   9644:                        REG16(AX) = (UINT16)GetLastError();
                   9645:                        m_CF = 1;
                   9646:                }
                   9647:                break;
                   9648:        case 0x02:
                   9649:                {
                   9650:                        DWORD size = GetCompressedFileSize(path, NULL);
                   9651:                        if(size != INVALID_FILE_SIZE) {
                   9652:                                if(size != 0 && size == GetFileSize(path, NULL)) {
                   9653:                                        DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   9654:                                        // this isn't correct if the file is in the NTFS MFT
                   9655:                                        if(GetDiskFreeSpace(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
                   9656:                                                size = ((size - 1) | (sectors_per_cluster * bytes_per_sector - 1)) + 1;
                   9657:                                        }
                   9658:                                }
                   9659:                                REG16(AX) = LOWORD(size);
                   9660:                                REG16(DX) = HIWORD(size);
                   9661:                        } else {
                   9662:                                REG16(AX) = (UINT16)GetLastError();
                   9663:                                m_CF = 1;
1.1       root     9664:                        }
1.1.1.14  root     9665:                }
                   9666:                break;
                   9667:        case 0x03:
                   9668:        case 0x05:
                   9669:        case 0x07:
                   9670:                {
                   9671:                        HANDLE hFile = CreateFile(path, FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   9672:                        if(hFile != INVALID_HANDLE_VALUE) {
                   9673:                                FILETIME local, time;
                   9674:                                DosDateTimeToFileTime(REG16(DI), /*REG8(BL) == 5 ? 0 : */REG16(CX), &local);
                   9675:                                if(REG8(BL) == 7) {
                   9676:                                        ULARGE_INTEGER hund;
                   9677:                                        hund.LowPart = local.dwLowDateTime;
                   9678:                                        hund.HighPart = local.dwHighDateTime;
                   9679:                                        hund.QuadPart += REG16(SI) * 100000;
                   9680:                                        local.dwLowDateTime = hund.LowPart;
                   9681:                                        local.dwHighDateTime = hund.HighPart;
                   9682:                                }
                   9683:                                LocalFileTimeToFileTime(&local, &time);
                   9684:                                if(!SetFileTime(hFile, REG8(BL) == 0x07 ? &time : NULL,
                   9685:                                                       REG8(BL) == 0x05 ? &time : NULL,
                   9686:                                                       REG8(BL) == 0x03 ? &time : NULL)) {
                   9687:                                        REG16(AX) = (UINT16)GetLastError();
                   9688:                                        m_CF = 1;
                   9689:                                }
                   9690:                                CloseHandle(hFile);
                   9691:                        } else {
                   9692:                                REG16(AX) = (UINT16)GetLastError();
                   9693:                                m_CF = 1;
1.1       root     9694:                        }
1.1.1.14  root     9695:                }
                   9696:                break;
                   9697:        case 0x04:
                   9698:        case 0x06:
                   9699:        case 0x08:
                   9700:                {
                   9701:                        WIN32_FILE_ATTRIBUTE_DATA fad;
                   9702:                        if(GetFileAttributesEx(path, GetFileExInfoStandard, (LPVOID)&fad)) {
                   9703:                                FILETIME *time, local;
                   9704:                                time = REG8(BL) == 0x04 ? &fad.ftLastWriteTime :
                   9705:                                                   0x06 ? &fad.ftLastAccessTime :
                   9706:                                                          &fad.ftCreationTime;
                   9707:                                FileTimeToLocalFileTime(time, &local);
                   9708:                                FileTimeToDosDateTime(&local, &REG16(DI), &REG16(CX));
                   9709:                                if(REG8(BL) == 0x08) {
                   9710:                                        ULARGE_INTEGER hund;
                   9711:                                        hund.LowPart = local.dwLowDateTime;
                   9712:                                        hund.HighPart = local.dwHighDateTime;
                   9713:                                        hund.QuadPart /= 100000;
                   9714:                                        REG16(SI) = (UINT16)(hund.QuadPart % 200);
                   9715:                                }
                   9716:                        } else {
                   9717:                                REG16(AX) = (UINT16)GetLastError();
                   9718:                                m_CF = 1;
1.1       root     9719:                        }
1.1.1.14  root     9720:                }
                   9721:                break;
                   9722:        default:
1.1.1.22  root     9723:                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     9724:                REG16(AX) = 0x01;
                   9725:                m_CF = 1;
                   9726:                break;
                   9727:        }
                   9728: }
                   9729: 
                   9730: inline void msdos_int_21h_44h()
                   9731: {
1.1.1.22  root     9732:        static UINT16 iteration_count = 0;
                   9733:        
1.1.1.20  root     9734:        process_t *process = msdos_process_info_get(current_psp);
                   9735:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   9736:        
1.1.1.14  root     9737:        UINT32 val = DRIVE_NO_ROOT_DIR;
                   9738:        
                   9739:        switch(REG8(AL)) {
                   9740:        case 0x00:
                   9741:        case 0x01:
                   9742:        case 0x02:
                   9743:        case 0x03:
                   9744:        case 0x04:
                   9745:        case 0x05:
                   9746:        case 0x06:
                   9747:        case 0x07:
1.1.1.20  root     9748:                if(fd >= process->max_files || !file_handler[fd].valid) {
                   9749:                        REG16(AX) = 0x06;
                   9750:                        m_CF = 1;
                   9751:                        return;
1.1.1.14  root     9752:                }
                   9753:                break;
                   9754:        case 0x08:
                   9755:        case 0x09:
                   9756:                if(REG8(BL) >= ('Z' - 'A' + 1)) {
                   9757:                        // invalid drive number
                   9758:                        REG16(AX) = 0x0f;
                   9759:                        m_CF = 1;
                   9760:                        return;
                   9761:                } else {
                   9762:                        if(REG8(BL) == 0) {
                   9763:                                val = GetDriveType(NULL);
                   9764:                        } else {
                   9765:                                char tmp[8];
                   9766:                                sprintf(tmp, "%c:\\", 'A' + REG8(BL) - 1);
                   9767:                                val = GetDriveType(tmp);
                   9768:                        }
                   9769:                        if(val == DRIVE_NO_ROOT_DIR) {
                   9770:                                // no drive
                   9771:                                REG16(AX) = 0x0f;
                   9772:                                m_CF = 1;
                   9773:                                return;
1.1       root     9774:                        }
                   9775:                }
                   9776:                break;
                   9777:        }
                   9778:        switch(REG8(AL)) {
                   9779:        case 0x00: // get ioctrl data
1.1.1.20  root     9780:                REG16(DX) = file_handler[fd].info;
1.1       root     9781:                break;
                   9782:        case 0x01: // set ioctrl data
1.1.1.20  root     9783:                file_handler[fd].info |= REG8(DL);
1.1       root     9784:                break;
                   9785:        case 0x02: // recv from character device
                   9786:        case 0x03: // send to character device
                   9787:        case 0x04: // recv from block device
                   9788:        case 0x05: // send to block device
                   9789:                REG16(AX) = 0x05;
1.1.1.3   root     9790:                m_CF = 1;
1.1       root     9791:                break;
                   9792:        case 0x06: // get read status
1.1.1.20  root     9793:                if(file_mode[file_handler[fd].mode].in) {
                   9794:                        if(file_handler[fd].atty) {
1.1.1.14  root     9795:                                REG8(AL) = msdos_kbhit() ? 0xff : 0x00;
1.1       root     9796:                        } else {
1.1.1.20  root     9797:                                REG8(AL) = eof(fd) ? 0x00 : 0xff;
1.1       root     9798:                        }
1.1.1.14  root     9799:                } else {
                   9800:                        REG8(AL) = 0x00;
1.1       root     9801:                }
                   9802:                break;
                   9803:        case 0x07: // get write status
1.1.1.20  root     9804:                if(file_mode[file_handler[fd].mode].out) {
1.1.1.14  root     9805:                        REG8(AL) = 0xff;
                   9806:                } else {
                   9807:                        REG8(AL) = 0x00;
1.1       root     9808:                }
                   9809:                break;
                   9810:        case 0x08: // check removable drive
1.1.1.14  root     9811:                if(val == DRIVE_REMOVABLE || val == DRIVE_CDROM) {
                   9812:                        // removable drive
                   9813:                        REG16(AX) = 0x00;
1.1       root     9814:                } else {
1.1.1.14  root     9815:                        // fixed drive
                   9816:                        REG16(AX) = 0x01;
1.1       root     9817:                }
                   9818:                break;
                   9819:        case 0x09: // check remote drive
1.1.1.14  root     9820:                if(val == DRIVE_REMOTE) {
                   9821:                        // remote drive
                   9822:                        REG16(DX) = 0x1000;
1.1       root     9823:                } else {
1.1.1.14  root     9824:                        // local drive
                   9825:                        REG16(DX) = 0x00;
1.1       root     9826:                }
                   9827:                break;
1.1.1.21  root     9828:        case 0x0a: // check remote handle
                   9829:                REG16(DX) = 0x00; // FIXME
                   9830:                break;
1.1       root     9831:        case 0x0b: // set retry count
                   9832:                break;
1.1.1.22  root     9833:        case 0x0c: // generic character device request
                   9834:                if(REG8(CL) == 0x45) {
                   9835:                        // set iteration (retry) count
                   9836:                        iteration_count = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
                   9837:                } else if(REG8(CL) == 0x4a) {
                   9838:                        // select code page
                   9839:                        active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2);
                   9840:                        msdos_nls_tables_update();
                   9841:                } else if(REG8(CL) == 0x65) {
                   9842:                        // get iteration (retry) count
                   9843:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX)) = iteration_count;
                   9844:                } else if(REG8(CL) == 0x6a) {
                   9845:                        // query selected code page
                   9846:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2; // FIXME
                   9847:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
                   9848:                        
                   9849:                        CPINFO info;
                   9850:                        GetCPInfo(active_code_page, &info);
                   9851:                        
                   9852:                        if(info.MaxCharSize != 1) {
                   9853:                                for(int i = 0;; i++) {
                   9854:                                        UINT8 lo = info.LeadByte[2 * i + 0];
                   9855:                                        UINT8 hi = info.LeadByte[2 * i + 1];
                   9856:                                        
                   9857:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 0) = lo;
                   9858:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 2) = hi;
                   9859:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) + 4;
                   9860:                                        
                   9861:                                        if(lo == 0 && hi == 0) {
                   9862:                                                break;
                   9863:                                        }
                   9864:                                }
                   9865:                        }
                   9866:                } else if(REG8(CL) == 0x7f) {
                   9867:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00) = 0;
                   9868:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x01) = 0;
                   9869:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) = 14;
                   9870:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04) = 1;
                   9871:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06) = 1;
                   9872:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x07) = 0;
                   9873:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08) = 4;
                   9874:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a) =  8 * (*(UINT16 *)(mem + 0x44a));
                   9875:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c) = 16 * (*(UINT8  *)(mem + 0x484) + 1);
                   9876:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e) = *(UINT16 *)(mem + 0x44a);
                   9877:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10) = *(UINT8  *)(mem + 0x484) + 1;
                   9878:                } else {
                   9879:                        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));
                   9880:                        REG16(AX) = 0x01; // invalid function
                   9881:                        m_CF = 1;
                   9882:                }
                   9883:                break;
                   9884:        case 0x0d: // generic block device request
                   9885:                if(REG8(CL) == 0x40) {
                   9886:                        // set device parameters
                   9887:                } else if(REG8(CL) == 0x46) {
                   9888:                        // set volume serial number
                   9889:                } else if(REG8(CL) == 0x4a) {
                   9890:                        // lock logical volume
                   9891:                } else if(REG8(CL) == 0x4b) {
                   9892:                        // lock physical volume
                   9893:                } else if(REG8(CL) == 0x60) {
                   9894:                        // get device parameters
                   9895:                        char dev[] = "\\\\.\\A:";
                   9896:                        dev[4] = 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   9897:                        
                   9898:                        HANDLE hFile = CreateFile(dev, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   9899:                        if(hFile != INVALID_HANDLE_VALUE) {
                   9900:                                DISK_GEOMETRY geo;
                   9901:                                DWORD dwSize;
                   9902:                                if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &geo, sizeof(geo), &dwSize, NULL)) {
                   9903:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x07; // ???
                   9904:                                        switch(geo.MediaType) {
                   9905:                                        case F5_360_512:
                   9906:                                        case F5_320_512:
                   9907:                                        case F5_320_1024:
                   9908:                                        case F5_180_512:
                   9909:                                        case F5_160_512:
                   9910:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // 320K/360K disk
                   9911:                                                break;
                   9912:                                        case F5_1Pt2_512:
                   9913:                                        case F3_1Pt2_512:
                   9914:                                        case F3_1Pt23_1024:
                   9915:                                        case F5_1Pt23_1024:
                   9916:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x01; // 1.2M disk
                   9917:                                                break;
                   9918:                                        case F3_720_512:
                   9919:                                        case F3_640_512:
                   9920:                                        case F5_640_512:
                   9921:                                        case F5_720_512:
                   9922:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02; // 720K disk
                   9923:                                                break;
                   9924:                                        case F8_256_128:
                   9925:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x03; // single-density 8-inch disk
                   9926:                                                break;
                   9927:                                        case FixedMedia:
                   9928:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   9929:                                                break;
                   9930:                                        case F3_1Pt44_512:
                   9931:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   9932:                                                break;
                   9933:                                        case F3_2Pt88_512:
                   9934:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09; // (DOS 5+) 2.88M floppy
                   9935:                                                break;
                   9936:                                        default:
                   9937:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   9938: //                                             *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   9939:                                                break;
                   9940:                                        }
                   9941:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = (geo.MediaType == FixedMedia) ? 0x01 : 0x00;
                   9942:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04) = (geo.Cylinders.QuadPart > 0xffff) ? 0xffff : geo.Cylinders.QuadPart;
                   9943:                                        switch(geo.MediaType) {
                   9944:                                        case F5_360_512:
                   9945:                                        case F5_320_512:
                   9946:                                        case F5_320_1024:
                   9947:                                        case F5_180_512:
                   9948:                                        case F5_160_512:
                   9949:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x01; // 320K/360K disk
                   9950:                                                break;
                   9951:                                        default:
                   9952:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x00; // other drive types
                   9953:                                                break;
                   9954:                                        }
                   9955:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x00) = geo.BytesPerSector;
                   9956:                                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x02) = geo.SectorsPerTrack * geo.TracksPerCylinder;
                   9957:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x03) = 0;
                   9958:                                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x05) = 0;
                   9959:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x06) = 0;
                   9960:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x08) = 0;
                   9961:                                        switch(geo.MediaType) {
                   9962:                                        case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   9963:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xff;
                   9964:                                                break;
                   9965:                                        case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   9966:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfe;
                   9967:                                                break;
                   9968:                                        case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   9969:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfd;
                   9970:                                                break;
                   9971:                                        case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   9972:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfc;
                   9973:                                                break;
                   9974:                                        case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   9975:                                        case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   9976:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf9;
                   9977:                                                break;
                   9978:                                        case FixedMedia:        // hard disk
                   9979:                                        case RemovableMedia:
                   9980:                                        case Unknown:
                   9981:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf8;
                   9982:                                                break;
                   9983:                                        default:
                   9984:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf0;
                   9985:                                                break;
                   9986:                                        }
                   9987:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0d) = geo.SectorsPerTrack;
                   9988:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0f) = 1;
                   9989:                                        *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x11) = 0;
                   9990:                                        *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x15) = geo.TracksPerCylinder * geo.Cylinders.QuadPart;
                   9991:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x1f) = geo.Cylinders.QuadPart;
                   9992:                                        // 21h  BYTE    device type
                   9993:                                        // 22h  WORD    device attributes (removable or not, etc)
                   9994:                                } else {
                   9995:                                        REG16(AX) = 0x0f; // invalid drive
                   9996:                                        m_CF = 1;
                   9997:                                }
                   9998:                                CloseHandle(hFile);
                   9999:                        } else {
                   10000:                                REG16(AX) = 0x0f; // invalid drive
                   10001:                                m_CF = 1;
                   10002:                        }
                   10003:                } else if(REG8(CL) == 0x66) {
                   10004:                        // get volume serial number
                   10005:                        char path[] = "A:\\";
                   10006:                        char volume_label[MAX_PATH];
                   10007:                        DWORD serial_number = 0;
                   10008:                        char file_system[MAX_PATH];
                   10009:                        
                   10010:                        path[0] = 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   10011:                        
                   10012:                        if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
                   10013:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   10014:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = serial_number;
                   10015:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x06, 0x20, 11);
                   10016:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x06, volume_label, min(strlen(volume_label), 11));
                   10017:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x11, 0x20,  8);
                   10018:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x11, file_system, min(strlen(file_system), 8));
                   10019:                        } else {
                   10020:                                REG16(AX) = 0x0f; // invalid drive
                   10021:                                m_CF = 1;
                   10022:                        }
                   10023:                } else if(REG8(CL) == 0x67) {
                   10024:                        // get access flag
                   10025:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   10026:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 1;
                   10027:                } else if(REG8(CL) == 0x68) {
                   10028:                        // sense media type
                   10029:                        char dev[64];
                   10030:                        sprintf(dev, "\\\\.\\%c:", 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1);
                   10031:                        
                   10032:                        HANDLE hFile = CreateFile(dev, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   10033:                        if(hFile != INVALID_HANDLE_VALUE) {
                   10034:                                DISK_GEOMETRY geo;
                   10035:                                DWORD dwSize;
                   10036:                                if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &geo, sizeof(geo), &dwSize, NULL)) {
                   10037:                                        switch(geo.MediaType) {
                   10038:                                        case F3_720_512:
                   10039:                                        case F5_720_512:
                   10040:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   10041:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02;
                   10042:                                                break;
                   10043:                                        case F3_1Pt44_512:
                   10044:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   10045:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07;
                   10046:                                                break;
                   10047:                                        case F3_2Pt88_512:
                   10048:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   10049:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09;
                   10050:                                                break;
                   10051:                                        default:
                   10052:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x00;
                   10053:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // ???
                   10054:                                                break;
                   10055:                                        }
                   10056:                                } else {
                   10057:                                        REG16(AX) = 0x0f; // invalid drive
                   10058:                                        m_CF = 1;
                   10059:                                }
                   10060:                                CloseHandle(hFile);
                   10061:                        } else {
                   10062:                                REG16(AX) = 0x0f; // invalid drive
                   10063:                                m_CF = 1;
                   10064:                        }
                   10065:                } else if(REG8(CL) == 0x6a) {
                   10066:                        // unlock logical volume
                   10067:                } else if(REG8(CL) == 0x6b) {
                   10068:                        // unlock physical volume
                   10069:                } else {
                   10070:                        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));
                   10071:                        REG16(AX) = 0x01; // invalid function
                   10072:                        m_CF = 1;
                   10073:                }
                   10074:                break;
                   10075:        case 0x0e: // get logical drive map
                   10076:                {
                   10077:                        DWORD bits = 1 << ((REG8(BL) ? REG8(BL) : _getdrive()) - 1);
                   10078:                        if(!(GetLogicalDrives() & bits)) {
                   10079:                                REG16(AX) = 0x0f; // invalid drive
                   10080:                                m_CF = 1;
                   10081:                        } else {
                   10082:                                REG8(AL) = 0;
                   10083:                        }
                   10084:                }
                   10085:                break;
                   10086:        case 0x0f: // set logical drive map
                   10087:                {
                   10088:                        DWORD bits = 1 << ((REG8(BL) ? REG8(BL) : _getdrive()) - 1);
                   10089:                        if(!(GetLogicalDrives() & bits)) {
                   10090:                                REG16(AX) = 0x0f; // invalid drive
                   10091:                                m_CF = 1;
                   10092:                        }
                   10093:                }
                   10094:                break;
                   10095:        case 0x10: // query generic ioctrl capability (handle)
                   10096:                switch(REG8(CL)) {
                   10097:                case 0x45:
                   10098:                case 0x4a:
                   10099:                case 0x65:
                   10100:                case 0x6a:
                   10101:                case 0x7f:
                   10102:                        REG16(AX) = 0x0000; // supported
                   10103:                        break;
                   10104:                default:
                   10105:                        REG8(AL) = 0x01; // ioctl capability not available
                   10106:                        m_CF = 1;
                   10107:                        break;
                   10108:                }
                   10109:                break;
                   10110:        case 0x11: // query generic ioctrl capability (drive)
                   10111:                switch(REG8(CL)) {
                   10112:                case 0x40:
                   10113:                case 0x46:
                   10114:                case 0x4a:
                   10115:                case 0x4b:
                   10116:                case 0x60:
                   10117:                case 0x66:
                   10118:                case 0x67:
                   10119:                case 0x68:
                   10120:                case 0x6a:
                   10121:                case 0x6b:
                   10122:                        REG16(AX) = 0x0000; // supported
                   10123:                        break;
                   10124:                default:
                   10125:                        REG8(AL) = 0x01; // ioctl capability not available
                   10126:                        m_CF = 1;
                   10127:                        break;
                   10128:                }
                   10129:                break;
                   10130:        case 0x12: // determine dos type
                   10131:        case 0x51: // concurrent dos v3.2+ - installation check
                   10132:        case 0x52: // determine dos type/get dr dos versuin
                   10133:                REG16(AX) = 0x01; // this  is not DR-DOS
                   10134:                m_CF = 1;
                   10135:                break;
1.1       root     10136:        default:
1.1.1.22  root     10137:                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     10138:                REG16(AX) = 0x01;
1.1.1.3   root     10139:                m_CF = 1;
1.1       root     10140:                break;
                   10141:        }
                   10142: }
                   10143: 
                   10144: inline void msdos_int_21h_45h()
                   10145: {
                   10146:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10147:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10148:        
1.1.1.20  root     10149:        if(fd < process->max_files && file_handler[fd].valid) {
                   10150:                int dup_fd = _dup(fd);
                   10151:                if(dup_fd != -1) {
                   10152:                        REG16(AX) = dup_fd;
                   10153:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   10154: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   10155:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     10156:                } else {
                   10157:                        REG16(AX) = errno;
1.1.1.3   root     10158:                        m_CF = 1;
1.1       root     10159:                }
                   10160:        } else {
                   10161:                REG16(AX) = 0x06;
1.1.1.3   root     10162:                m_CF = 1;
1.1       root     10163:        }
                   10164: }
                   10165: 
                   10166: inline void msdos_int_21h_46h()
                   10167: {
                   10168:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10169:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   10170:        int dup_fd = REG16(CX);
                   10171:        int tmp_fd = msdos_psp_get_file_table(REG16(CX), current_psp);
1.1       root     10172:        
1.1.1.20  root     10173:        if(REG16(BX) == REG16(CX)) {
                   10174:                REG16(AX) = 0x06;
                   10175:                m_CF = 1;
                   10176:        } else if(fd < process->max_files && file_handler[fd].valid && dup_fd < process->max_files) {
                   10177:                if(tmp_fd < process->max_files && file_handler[tmp_fd].valid) {
                   10178:                        _close(tmp_fd);
                   10179:                        msdos_file_handler_close(tmp_fd);
                   10180:                        msdos_psp_set_file_table(dup_fd, 0x0ff, current_psp);
                   10181:                }
                   10182:                if(_dup2(fd, dup_fd) != -1) {
                   10183:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   10184: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   10185:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     10186:                } else {
                   10187:                        REG16(AX) = errno;
1.1.1.3   root     10188:                        m_CF = 1;
1.1       root     10189:                }
                   10190:        } else {
                   10191:                REG16(AX) = 0x06;
1.1.1.3   root     10192:                m_CF = 1;
1.1       root     10193:        }
                   10194: }
                   10195: 
                   10196: inline void msdos_int_21h_47h(int lfn)
                   10197: {
                   10198:        char path[MAX_PATH];
                   10199:        
                   10200:        if(_getdcwd(REG8(DL), path, MAX_PATH) != NULL) {
                   10201:                if(path[1] == ':') {
                   10202:                        // the returned path does not include a drive or the initial backslash
1.1.1.3   root     10203:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), (lfn ? path : msdos_short_path(path)) + 3);
1.1       root     10204:                } else {
1.1.1.3   root     10205:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), lfn ? path : msdos_short_path(path));
1.1       root     10206:                }
                   10207:        } else {
                   10208:                REG16(AX) = errno;
1.1.1.3   root     10209:                m_CF = 1;
1.1       root     10210:        }
                   10211: }
                   10212: 
                   10213: inline void msdos_int_21h_48h()
                   10214: {
1.1.1.19  root     10215:        int seg, umb_linked;
1.1       root     10216:        
1.1.1.8   root     10217:        if((malloc_strategy & 0xf0) == 0x00) {
1.1.1.19  root     10218:                // unlink umb not to allocate memory in umb
                   10219:                if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   10220:                        msdos_mem_unlink_umb();
                   10221:                }
1.1.1.8   root     10222:                if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   10223:                        REG16(AX) = seg;
                   10224:                } else {
                   10225:                        REG16(AX) = 0x08;
                   10226:                        REG16(BX) = msdos_mem_get_free(first_mcb, 0);
                   10227:                        m_CF = 1;
                   10228:                }
1.1.1.19  root     10229:                if(umb_linked != 0) {
                   10230:                        msdos_mem_link_umb();
                   10231:                }
1.1.1.8   root     10232:        } else if((malloc_strategy & 0xf0) == 0x40) {
                   10233:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   10234:                        REG16(AX) = seg;
                   10235:                } else {
                   10236:                        REG16(AX) = 0x08;
                   10237:                        REG16(BX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   10238:                        m_CF = 1;
                   10239:                }
                   10240:        } else if((malloc_strategy & 0xf0) == 0x80) {
                   10241:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   10242:                        REG16(AX) = seg;
                   10243:                } else if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   10244:                        REG16(AX) = seg;
                   10245:                } else {
                   10246:                        REG16(AX) = 0x08;
                   10247:                        REG16(BX) = max(msdos_mem_get_free(UMB_TOP >> 4, 0), msdos_mem_get_free(first_mcb, 0));
                   10248:                        m_CF = 1;
                   10249:                }
1.1       root     10250:        }
                   10251: }
                   10252: 
                   10253: inline void msdos_int_21h_49h()
                   10254: {
1.1.1.14  root     10255:        int mcb_seg = SREG(ES) - 1;
                   10256:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   10257:        
                   10258:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   10259:                msdos_mem_free(SREG(ES));
                   10260:        } else {
1.1.1.33  root     10261:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.14  root     10262:                m_CF = 1;
                   10263:        }
1.1       root     10264: }
                   10265: 
                   10266: inline void msdos_int_21h_4ah()
                   10267: {
1.1.1.14  root     10268:        int mcb_seg = SREG(ES) - 1;
                   10269:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1       root     10270:        int max_paragraphs;
                   10271:        
1.1.1.14  root     10272:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   10273:                if(msdos_mem_realloc(SREG(ES), REG16(BX), &max_paragraphs)) {
                   10274:                        REG16(AX) = 0x08;
                   10275:                        REG16(BX) = max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs;
                   10276:                        m_CF = 1;
                   10277:                }
                   10278:        } else {
1.1.1.33  root     10279:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.3   root     10280:                m_CF = 1;
1.1       root     10281:        }
                   10282: }
                   10283: 
                   10284: inline void msdos_int_21h_4bh()
                   10285: {
1.1.1.3   root     10286:        char *command = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   10287:        param_block_t *param = (param_block_t *)(mem + SREG_BASE(ES) + REG16(BX));
1.1       root     10288:        
                   10289:        switch(REG8(AL)) {
                   10290:        case 0x00:
                   10291:        case 0x01:
                   10292:                if(msdos_process_exec(command, param, REG8(AL))) {
                   10293:                        REG16(AX) = 0x02;
1.1.1.3   root     10294:                        m_CF = 1;
1.1       root     10295:                }
                   10296:                break;
1.1.1.14  root     10297:        case 0x03:
                   10298:                {
                   10299:                        int fd;
                   10300:                        if((fd = _open(command, _O_RDONLY | _O_BINARY)) == -1) {
                   10301:                                REG16(AX) = 0x02;
                   10302:                                m_CF = 1;
                   10303:                                break;
                   10304:                        }
                   10305:                        int size = _read(fd, file_buffer, sizeof(file_buffer));
                   10306:                        _close(fd);
                   10307:                        
                   10308:                        UINT16 *overlay = (UINT16 *)param;
                   10309:                        
                   10310:                        // check exe header
                   10311:                        exe_header_t *header = (exe_header_t *)file_buffer;
                   10312:                        int header_size = 0;
                   10313:                        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   10314:                                header_size = header->header_size * 16;
                   10315:                                // relocation
                   10316:                                int start_seg = overlay[1];
                   10317:                                for(int i = 0; i < header->relocations; i++) {
                   10318:                                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   10319:                                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   10320:                                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   10321:                                }
                   10322:                        }
                   10323:                        memcpy(mem + (overlay[0] << 4), file_buffer + header_size, size - header_size);
                   10324:                }
                   10325:                break;
1.1       root     10326:        default:
1.1.1.22  root     10327:                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     10328:                REG16(AX) = 0x01;
1.1.1.3   root     10329:                m_CF = 1;
1.1       root     10330:                break;
                   10331:        }
                   10332: }
                   10333: 
                   10334: inline void msdos_int_21h_4ch()
                   10335: {
                   10336:        msdos_process_terminate(current_psp, REG8(AL), 1);
                   10337: }
                   10338: 
                   10339: inline void msdos_int_21h_4dh()
                   10340: {
                   10341:        REG16(AX) = retval;
                   10342: }
                   10343: 
                   10344: inline void msdos_int_21h_4eh()
                   10345: {
                   10346:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     10347:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   10348:        find_t *find = (find_t *)(mem + dta_laddr);
1.1.1.3   root     10349:        char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     10350:        WIN32_FIND_DATA fd;
                   10351:        
1.1.1.14  root     10352:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   10353:        find->find_magic = FIND_MAGIC;
                   10354:        find->dta_index = dtainfo - dtalist;
1.1       root     10355:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     10356:        dtainfo->allowable_mask = REG8(CL);
                   10357:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     10358:        
1.1.1.14  root     10359:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   10360:                dtainfo->allowable_mask &= ~8;
1.1       root     10361:        }
1.1.1.14  root     10362:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   10363:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     10364:                      !msdos_find_file_has_8dot3name(&fd)) {
                   10365:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   10366:                                FindClose(dtainfo->find_handle);
                   10367:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     10368:                                break;
                   10369:                        }
                   10370:                }
                   10371:        }
1.1.1.13  root     10372:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     10373:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   10374:                msdos_find_file_conv_local_time(&fd);
                   10375:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   10376:                find->size = fd.nFileSizeLow;
1.1.1.13  root     10377:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     10378:                REG16(AX) = 0;
1.1.1.14  root     10379:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     10380:                find->attrib = 8;
                   10381:                find->size = 0;
                   10382:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     10383:                dtainfo->allowable_mask &= ~8;
1.1       root     10384:                REG16(AX) = 0;
                   10385:        } else {
                   10386:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     10387:                m_CF = 1;
1.1       root     10388:        }
                   10389: }
                   10390: 
                   10391: inline void msdos_int_21h_4fh()
                   10392: {
                   10393:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     10394:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   10395:        find_t *find = (find_t *)(mem + dta_laddr);
1.1       root     10396:        WIN32_FIND_DATA fd;
                   10397:        
1.1.1.14  root     10398:        if(find->find_magic != FIND_MAGIC || find->dta_index >= MAX_DTAINFO) {
                   10399:                REG16(AX) = 0x12;
                   10400:                m_CF = 1;
                   10401:                return;
                   10402:        }
                   10403:        dtainfo_t *dtainfo = &dtalist[find->dta_index];
1.1.1.13  root     10404:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   10405:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     10406:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     10407:                              !msdos_find_file_has_8dot3name(&fd)) {
                   10408:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   10409:                                        FindClose(dtainfo->find_handle);
                   10410:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     10411:                                        break;
                   10412:                                }
                   10413:                        }
                   10414:                } else {
1.1.1.13  root     10415:                        FindClose(dtainfo->find_handle);
                   10416:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     10417:                }
                   10418:        }
1.1.1.13  root     10419:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     10420:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   10421:                msdos_find_file_conv_local_time(&fd);
                   10422:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   10423:                find->size = fd.nFileSizeLow;
1.1.1.13  root     10424:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     10425:                REG16(AX) = 0;
1.1.1.14  root     10426:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     10427:                find->attrib = 8;
                   10428:                find->size = 0;
                   10429:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     10430:                dtainfo->allowable_mask &= ~8;
1.1       root     10431:                REG16(AX) = 0;
                   10432:        } else {
                   10433:                REG16(AX) = 0x12;
1.1.1.3   root     10434:                m_CF = 1;
1.1       root     10435:        }
                   10436: }
                   10437: 
                   10438: inline void msdos_int_21h_50h()
                   10439: {
1.1.1.8   root     10440:        if(current_psp != REG16(BX)) {
                   10441:                process_t *process = msdos_process_info_get(current_psp);
                   10442:                if(process != NULL) {
                   10443:                        process->psp = REG16(BX);
                   10444:                }
                   10445:                current_psp = REG16(BX);
1.1.1.23  root     10446:                msdos_sda_update(current_psp);
1.1.1.8   root     10447:        }
1.1       root     10448: }
                   10449: 
                   10450: inline void msdos_int_21h_51h()
                   10451: {
                   10452:        REG16(BX) = current_psp;
                   10453: }
                   10454: 
                   10455: inline void msdos_int_21h_52h()
                   10456: {
1.1.1.25  root     10457:        SREG(ES) = DOS_INFO_TOP >> 4;
1.1.1.3   root     10458:        i386_load_segment_descriptor(ES);
1.1.1.25  root     10459:        REG16(BX) = offsetof(dos_info_t, first_dpb);
1.1       root     10460: }
                   10461: 
                   10462: inline void msdos_int_21h_54h()
                   10463: {
                   10464:        process_t *process = msdos_process_info_get(current_psp);
                   10465:        
                   10466:        REG8(AL) = process->verify;
                   10467: }
                   10468: 
                   10469: inline void msdos_int_21h_55h()
                   10470: {
                   10471:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   10472:        
                   10473:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
                   10474:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   10475:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   10476:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   10477:        psp->parent_psp = current_psp;
                   10478: }
                   10479: 
                   10480: inline void msdos_int_21h_56h(int lfn)
                   10481: {
                   10482:        char src[MAX_PATH], dst[MAX_PATH];
1.1.1.3   root     10483:        strcpy(src, msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn));
                   10484:        strcpy(dst, msdos_trimmed_path((char *)(mem + SREG_BASE(ES) + REG16(DI)), lfn));
1.1       root     10485:        
                   10486:        if(rename(src, dst)) {
                   10487:                REG16(AX) = errno;
1.1.1.3   root     10488:                m_CF = 1;
1.1       root     10489:        }
                   10490: }
                   10491: 
                   10492: inline void msdos_int_21h_57h()
                   10493: {
                   10494:        FILETIME time, local;
1.1.1.14  root     10495:        FILETIME *ctime, *atime, *mtime;
1.1.1.21  root     10496:        HANDLE hHandle;
1.1       root     10497:        
1.1.1.21  root     10498:        if((hHandle = (HANDLE)_get_osfhandle(REG16(BX))) == INVALID_HANDLE_VALUE) {
1.1.1.14  root     10499:                REG16(AX) = (UINT16)GetLastError();
                   10500:                m_CF = 1;
                   10501:                return;
                   10502:        }
                   10503:        ctime = atime = mtime = NULL;
                   10504:        
1.1       root     10505:        switch(REG8(AL)) {
                   10506:        case 0x00:
1.1.1.6   root     10507:        case 0x01:
1.1.1.14  root     10508:                mtime = &time;
1.1.1.6   root     10509:                break;
                   10510:        case 0x04:
                   10511:        case 0x05:
1.1.1.14  root     10512:                atime = &time;
1.1       root     10513:                break;
1.1.1.6   root     10514:        case 0x06:
                   10515:        case 0x07:
1.1.1.14  root     10516:                ctime = &time;
                   10517:                break;
                   10518:        default:
1.1.1.22  root     10519:                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     10520:                REG16(AX) = 0x01;
                   10521:                m_CF = 1;
                   10522:                return;
                   10523:        }
                   10524:        if(REG8(AL) & 1) {
1.1       root     10525:                DosDateTimeToFileTime(REG16(DX), REG16(CX), &local);
                   10526:                LocalFileTimeToFileTime(&local, &time);
1.1.1.21  root     10527:                if(!SetFileTime(hHandle, ctime, atime, mtime)) {
1.1       root     10528:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     10529:                        m_CF = 1;
1.1       root     10530:                }
1.1.1.14  root     10531:        } else {
1.1.1.21  root     10532:                if(!GetFileTime(hHandle, ctime, atime, mtime)) {
1.1.1.14  root     10533:                        // assume a device and use the current time
                   10534:                        GetSystemTimeAsFileTime(&time);
                   10535:                }
                   10536:                FileTimeToLocalFileTime(&time, &local);
                   10537:                FileTimeToDosDateTime(&local, &REG16(DX), &REG16(CX));
1.1       root     10538:        }
                   10539: }
                   10540: 
                   10541: inline void msdos_int_21h_58h()
                   10542: {
                   10543:        switch(REG8(AL)) {
                   10544:        case 0x00:
1.1.1.7   root     10545:                REG16(AX) = malloc_strategy;
                   10546:                break;
                   10547:        case 0x01:
1.1.1.24  root     10548: //             switch(REG16(BX)) {
                   10549:                switch(REG8(BL)) {
1.1.1.7   root     10550:                case 0x0000:
                   10551:                case 0x0001:
                   10552:                case 0x0002:
                   10553:                case 0x0040:
                   10554:                case 0x0041:
                   10555:                case 0x0042:
                   10556:                case 0x0080:
                   10557:                case 0x0081:
                   10558:                case 0x0082:
                   10559:                        malloc_strategy = REG16(BX);
1.1.1.23  root     10560:                        msdos_sda_update(current_psp);
1.1.1.7   root     10561:                        break;
                   10562:                default:
1.1.1.22  root     10563:                        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     10564:                        REG16(AX) = 0x01;
                   10565:                        m_CF = 1;
                   10566:                        break;
                   10567:                }
                   10568:                break;
                   10569:        case 0x02:
1.1.1.19  root     10570:                REG8(AL) = msdos_mem_get_umb_linked() ? 1 : 0;
1.1.1.7   root     10571:                break;
                   10572:        case 0x03:
1.1.1.24  root     10573: //             switch(REG16(BX)) {
                   10574:                switch(REG8(BL)) {
1.1.1.7   root     10575:                case 0x0000:
1.1.1.19  root     10576:                        msdos_mem_unlink_umb();
                   10577:                        break;
1.1.1.7   root     10578:                case 0x0001:
1.1.1.19  root     10579:                        msdos_mem_link_umb();
1.1.1.7   root     10580:                        break;
                   10581:                default:
1.1.1.22  root     10582:                        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     10583:                        REG16(AX) = 0x01;
                   10584:                        m_CF = 1;
                   10585:                        break;
                   10586:                }
1.1       root     10587:                break;
                   10588:        default:
1.1.1.22  root     10589:                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     10590:                REG16(AX) = 0x01;
1.1.1.3   root     10591:                m_CF = 1;
1.1       root     10592:                break;
                   10593:        }
                   10594: }
                   10595: 
                   10596: inline void msdos_int_21h_59h()
                   10597: {
1.1.1.23  root     10598:        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   10599:        
                   10600:        REG16(AX) = sda->extended_error_code;
                   10601:        REG8(BH) = sda->error_class;
                   10602:        REG8(BL) = sda->suggested_action;
                   10603:        REG8(CH) = sda->locus_of_last_error;
1.1       root     10604: }
                   10605: 
                   10606: inline void msdos_int_21h_5ah()
                   10607: {
1.1.1.3   root     10608:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     10609:        int len = strlen(path);
                   10610:        char tmp[MAX_PATH];
                   10611:        
                   10612:        if(GetTempFileName(path, "TMP", 0, tmp)) {
                   10613:                int fd = _open(tmp, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   10614:                
                   10615:                SetFileAttributes(tmp, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   10616:                REG16(AX) = fd;
                   10617:                msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     10618:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     10619:                
                   10620:                strcpy(path, tmp);
                   10621:                int dx = REG16(DX) + len;
1.1.1.3   root     10622:                int ds = SREG(DS);
1.1       root     10623:                while(dx > 0xffff) {
                   10624:                        dx -= 0x10;
                   10625:                        ds++;
                   10626:                }
                   10627:                REG16(DX) = dx;
1.1.1.3   root     10628:                SREG(DS) = ds;
                   10629:                i386_load_segment_descriptor(DS);
1.1       root     10630:        } else {
                   10631:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     10632:                m_CF = 1;
1.1       root     10633:        }
                   10634: }
                   10635: 
                   10636: inline void msdos_int_21h_5bh()
                   10637: {
1.1.1.3   root     10638:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     10639:        
1.1.1.24  root     10640:        if(msdos_is_existing_file(path)) {
1.1       root     10641:                // already exists
                   10642:                REG16(AX) = 0x50;
1.1.1.3   root     10643:                m_CF = 1;
1.1       root     10644:        } else {
                   10645:                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   10646:                
                   10647:                if(fd != -1) {
                   10648:                        SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   10649:                        REG16(AX) = fd;
                   10650:                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     10651:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     10652:                } else {
                   10653:                        REG16(AX) = errno;
1.1.1.3   root     10654:                        m_CF = 1;
1.1       root     10655:                }
                   10656:        }
                   10657: }
                   10658: 
                   10659: inline void msdos_int_21h_5ch()
                   10660: {
                   10661:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10662:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10663:        
1.1.1.20  root     10664:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     10665:                if(REG8(AL) == 0 || REG8(AL) == 1) {
1.1.1.35  root     10666:                        static const int modes[2] = {_LK_LOCK, _LK_UNLCK};
1.1.1.20  root     10667:                        UINT32 pos = _tell(fd);
                   10668:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), SEEK_SET);
                   10669:                        if(_locking(fd, modes[REG8(AL)], (REG16(SI) << 16) | REG16(DI))) {
1.1       root     10670:                                REG16(AX) = errno;
1.1.1.3   root     10671:                                m_CF = 1;
1.1       root     10672:                        }
1.1.1.20  root     10673:                        _lseek(fd, pos, SEEK_SET);
1.1.1.26  root     10674:                        
1.1       root     10675:                        // some seconds may be passed in _locking()
1.1.1.35  root     10676:                        REQUEST_HARDWRE_UPDATE();
1.1       root     10677:                } else {
                   10678:                        REG16(AX) = 0x01;
1.1.1.3   root     10679:                        m_CF = 1;
1.1       root     10680:                }
                   10681:        } else {
                   10682:                REG16(AX) = 0x06;
1.1.1.3   root     10683:                m_CF = 1;
1.1       root     10684:        }
                   10685: }
                   10686: 
1.1.1.22  root     10687: inline void msdos_int_21h_5dh()
                   10688: {
                   10689:        switch(REG8(AL)) {
                   10690:        case 0x06: // get address of dos swappable data area
1.1.1.23  root     10691:                SREG(DS) = (SDA_TOP >> 4);
                   10692:                i386_load_segment_descriptor(DS);
                   10693:                REG16(SI) = offsetof(sda_t, crit_error_flag);
                   10694:                REG16(CX) = 0x80;
                   10695:                REG16(DX) = 0x1a;
                   10696:                break;
                   10697:        case 0x0b: // get dos swappable data areas
1.1.1.22  root     10698:                REG16(AX) = 0x01;
                   10699:                m_CF = 1;
                   10700:                break;
                   10701:        case 0x08: // set redirected printer mode
                   10702:        case 0x09: // flush redirected printer output
                   10703:        case 0x0a: // set extended error information
                   10704:                break;
                   10705:        default:
                   10706:                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));
                   10707:                REG16(AX) = 0x01;
                   10708:                m_CF = 1;
                   10709:                break;
                   10710:        }
                   10711: }
                   10712: 
1.1.1.30  root     10713: inline void msdos_int_21h_5fh()
                   10714: {
                   10715:        switch(REG8(AL)) {
                   10716:        case 0x02:
                   10717:                {
                   10718:                        DWORD drives = GetLogicalDrives();
                   10719:                        for(int i = 0, index = 0; i < 26; i++) {
                   10720:                                if(drives & (1 << i)) {
                   10721:                                        char volume[] = "A:\\";
                   10722:                                        volume[0] = 'A' + i;
                   10723:                                        if(GetDriveType(volume) == DRIVE_REMOTE) {
                   10724:                                                if(index == REG16(BX)) {
                   10725:                                                        DWORD dwSize = 128;
                   10726:                                                        volume[2] = '\0';
                   10727:                                                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), volume);
                   10728:                                                        WNetGetConnection(volume, (char *)(mem + SREG_BASE(ES) + REG16(DI)), &dwSize);
                   10729:                                                        REG8(BH) = 0x00; // valid
                   10730:                                                        REG8(BL) = 0x04; // disk drive
                   10731:                                                        REG16(CX) = 0x00;
                   10732:                                                        return;
                   10733:                                                }
                   10734:                                                index++;
                   10735:                                        }
                   10736:                                }
                   10737:                        }
                   10738:                }
                   10739:                REG16(AX) = 0x12; // no more files
                   10740:                m_CF = 1;
                   10741:                break;
                   10742:        default:
                   10743:                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));
                   10744:                REG16(AX) = 0x01;
                   10745:                m_CF = 1;
                   10746:                break;
                   10747:        }
                   10748: }
                   10749: 
1.1       root     10750: inline void msdos_int_21h_60h(int lfn)
                   10751: {
1.1.1.14  root     10752:        char full[MAX_PATH], *path;
                   10753:        
1.1       root     10754:        if(lfn) {
1.1.1.14  root     10755:                char *name;
                   10756:                *full = '\0';
1.1.1.3   root     10757:                GetFullPathName((char *)(mem + SREG_BASE(DS) + REG16(SI)), MAX_PATH, full, &name);
1.1.1.14  root     10758:                switch(REG8(CL)) {
                   10759:                case 1:
                   10760:                        GetShortPathName(full, full, MAX_PATH);
                   10761:                        my_strupr(full);
                   10762:                        break;
                   10763:                case 2:
                   10764:                        GetLongPathName(full, full, MAX_PATH);
                   10765:                        break;
                   10766:                }
                   10767:                path = full;
                   10768:        } else {
                   10769:                path = msdos_short_full_path((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   10770:        }
                   10771:        if(*path != '\0') {
                   10772:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
1.1       root     10773:        } else {
1.1.1.14  root     10774:                REG16(AX) = (UINT16)GetLastError();
                   10775:                m_CF = 1;
1.1       root     10776:        }
                   10777: }
                   10778: 
                   10779: inline void msdos_int_21h_61h()
                   10780: {
                   10781:        REG8(AL) = 0;
                   10782: }
                   10783: 
                   10784: inline void msdos_int_21h_62h()
                   10785: {
                   10786:        REG16(BX) = current_psp;
                   10787: }
                   10788: 
                   10789: inline void msdos_int_21h_63h()
                   10790: {
                   10791:        switch(REG8(AL)) {
                   10792:        case 0x00:
1.1.1.3   root     10793:                SREG(DS) = (DBCS_TABLE >> 4);
                   10794:                i386_load_segment_descriptor(DS);
1.1       root     10795:                REG16(SI) = (DBCS_TABLE & 0x0f);
                   10796:                REG8(AL) = 0x00;
                   10797:                break;
1.1.1.22  root     10798:        case 0x01: // set korean input mode
                   10799:        case 0x02: // get korean input mode
                   10800:                REG8(AL) = 0xff; // not supported
                   10801:                break;
1.1       root     10802:        default:
1.1.1.22  root     10803:                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     10804:                REG16(AX) = 0x01;
1.1.1.3   root     10805:                m_CF = 1;
1.1       root     10806:                break;
                   10807:        }
                   10808: }
                   10809: 
1.1.1.25  root     10810: UINT16 get_extended_country_info(UINT8 func)
1.1       root     10811: {
1.1.1.25  root     10812:        switch(func) {
1.1.1.17  root     10813:        case 0x01:
                   10814:                if(REG16(CX) >= 5) {
1.1.1.19  root     10815:                        UINT8 data[1 + 2 + 2 + 2 + sizeof(country_info_t)];
1.1.1.17  root     10816:                        if(REG16(CX) > sizeof(data))            // cx = actual transfer size
                   10817:                                REG16(CX) = sizeof(data);
                   10818:                        ZeroMemory(data, sizeof(data));
                   10819:                        data[0] = 0x01;
                   10820:                        *(UINT16 *)(data + 1) = REG16(CX) - 3;
1.1.1.19  root     10821:                        *(UINT16 *)(data + 3) = get_country_info((country_info_t*)(data + 7));
1.1.1.17  root     10822:                        *(UINT16 *)(data + 5) = active_code_page;
                   10823:                        memcpy(mem + SREG_BASE(ES) + REG16(DI), data, REG16(CX));
1.1.1.25  root     10824: //                     REG16(AX) = active_code_page;
1.1.1.17  root     10825:                } else {
1.1.1.25  root     10826:                        return(0x08); // insufficient memory
1.1.1.17  root     10827:                }
                   10828:                break;
                   10829:        case 0x02:
                   10830:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   10831:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (UPPERTABLE_TOP & 0x0f);
                   10832:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (UPPERTABLE_TOP >> 4);
1.1.1.25  root     10833: //             REG16(AX) = active_code_page;
1.1.1.17  root     10834:                REG16(CX) = 0x05;
                   10835:                break;
1.1.1.23  root     10836:        case 0x03:
                   10837:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   10838:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (LOWERTABLE_TOP & 0x0f);
                   10839:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (LOWERTABLE_TOP >> 4);
1.1.1.25  root     10840: //             REG16(AX) = active_code_page;
1.1.1.23  root     10841:                REG16(CX) = 0x05;
                   10842:                break;
1.1.1.17  root     10843:        case 0x04:
                   10844:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x04;
                   10845:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_UPPERTABLE_TOP & 0x0f);
                   10846:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_UPPERTABLE_TOP >> 4);
1.1.1.25  root     10847: //             REG16(AX) = active_code_page;
1.1.1.17  root     10848:                REG16(CX) = 0x05;
                   10849:                break;
                   10850:        case 0x05:
                   10851:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x05;
                   10852:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_TERMINATOR_TOP & 0x0f);
                   10853:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_TERMINATOR_TOP >> 4);
1.1.1.25  root     10854: //             REG16(AX) = active_code_page;
1.1.1.17  root     10855:                REG16(CX) = 0x05;
                   10856:                break;
                   10857:        case 0x06:
                   10858:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x06;
                   10859:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (COLLATING_TABLE_TOP & 0x0f);
                   10860:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (COLLATING_TABLE_TOP >> 4);
1.1.1.25  root     10861: //             REG16(AX) = active_code_page;
1.1.1.17  root     10862:                REG16(CX) = 0x05;
                   10863:                break;
1.1       root     10864:        case 0x07:
1.1.1.3   root     10865:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x07;
                   10866:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (DBCS_TOP & 0x0f);
                   10867:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (DBCS_TOP >> 4);
1.1.1.25  root     10868: //             REG16(AX) = active_code_page;
1.1       root     10869:                REG16(CX) = 0x05;
                   10870:                break;
1.1.1.25  root     10871:        default:
                   10872:                return(0x01); // function number invalid
                   10873:        }
                   10874:        return(0x00);
                   10875: }
                   10876: 
                   10877: inline void msdos_int_21h_65h()
                   10878: {
                   10879:        char tmp[0x10000];
                   10880:        
                   10881:        switch(REG8(AL)) {
                   10882:        case 0x01:
                   10883:        case 0x02:
                   10884:        case 0x03:
                   10885:        case 0x04:
                   10886:        case 0x05:
                   10887:        case 0x06:
                   10888:        case 0x07:
                   10889:                {
                   10890:                        UINT16 result = get_extended_country_info(REG8(AL));
                   10891:                        if(result) {
                   10892:                                REG16(AX) = result;
                   10893:                                m_CF = 1;
                   10894:                        } else {
                   10895:                                REG16(AX) = active_code_page; // FIXME: is this correct???
                   10896:                        }
                   10897:                }
                   10898:                break;
1.1       root     10899:        case 0x20:
1.1.1.25  root     10900:        case 0xa0:
1.1.1.19  root     10901:                memset(tmp, 0, sizeof(tmp));
                   10902:                tmp[0] = REG8(DL);
1.1       root     10903:                my_strupr(tmp);
                   10904:                REG8(DL) = tmp[0];
                   10905:                break;
                   10906:        case 0x21:
1.1.1.25  root     10907:        case 0xa1:
1.1       root     10908:                memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     10909:                memcpy(tmp, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     10910:                my_strupr(tmp);
1.1.1.3   root     10911:                memcpy(mem + SREG_BASE(DS) + REG16(DX), tmp, REG16(CX));
1.1       root     10912:                break;
                   10913:        case 0x22:
1.1.1.25  root     10914:        case 0xa2:
1.1.1.3   root     10915:                my_strupr((char *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1       root     10916:                break;
1.1.1.25  root     10917:        case 0x23:
                   10918:                // FIXME: need to check multi-byte (kanji) charactre?
                   10919:                if(REG8(DL) == 'N' || REG8(DL) == 'n' || (REG8(DL) == 0x82 && REG8(DH) == 0x78) || (REG8(DL) == 0x82 && REG8(DH) == 0x99)) {
                   10920:                        // 8278h/8299h: multi-byte (kanji) Y and y
                   10921:                        REG16(AX) = 0x00;
                   10922:                } else if(REG8(DL) == 'Y' || REG8(DL) == 'y' || (REG8(DL) == 0x82 && REG8(DH) == 0x6d) || (REG8(DL) == 0x82 && REG8(DH) == 0x8e)) {
                   10923:                        // 826dh/828eh: multi-byte (kanji) N and n
                   10924:                        REG16(AX) = 0x01;
                   10925:                } else {
                   10926:                        REG16(AX) = 0x02;
                   10927:                }
                   10928:                break;
1.1       root     10929:        default:
1.1.1.22  root     10930:                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     10931:                REG16(AX) = 0x01;
1.1.1.3   root     10932:                m_CF = 1;
1.1       root     10933:                break;
                   10934:        }
                   10935: }
                   10936: 
                   10937: inline void msdos_int_21h_66h()
                   10938: {
                   10939:        switch(REG8(AL)) {
                   10940:        case 0x01:
                   10941:                REG16(BX) = active_code_page;
                   10942:                REG16(DX) = system_code_page;
                   10943:                break;
                   10944:        case 0x02:
                   10945:                if(active_code_page == REG16(BX)) {
                   10946:                        REG16(AX) = 0xeb41;
                   10947:                } else if(_setmbcp(REG16(BX)) == 0) {
                   10948:                        active_code_page = REG16(BX);
1.1.1.17  root     10949:                        msdos_nls_tables_update();
1.1       root     10950:                        REG16(AX) = 0xeb41;
1.1.1.32  root     10951:                        SetConsoleCP(active_code_page);
                   10952:                        SetConsoleOutputCP(active_code_page);
1.1       root     10953:                } else {
                   10954:                        REG16(AX) = 0x25;
1.1.1.3   root     10955:                        m_CF = 1;
1.1       root     10956:                }
                   10957:                break;
                   10958:        default:
1.1.1.22  root     10959:                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     10960:                REG16(AX) = 0x01;
1.1.1.3   root     10961:                m_CF = 1;
1.1       root     10962:                break;
                   10963:        }
                   10964: }
                   10965: 
                   10966: inline void msdos_int_21h_67h()
                   10967: {
                   10968:        process_t *process = msdos_process_info_get(current_psp);
                   10969:        
                   10970:        if(REG16(BX) <= MAX_FILES) {
                   10971:                process->max_files = max(REG16(BX), 20);
                   10972:        } else {
                   10973:                REG16(AX) = 0x08;
1.1.1.3   root     10974:                m_CF = 1;
1.1       root     10975:        }
                   10976: }
                   10977: 
                   10978: inline void msdos_int_21h_68h()
                   10979: {
                   10980:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10981:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10982:        
1.1.1.20  root     10983:        if(fd < process->max_files && file_handler[fd].valid) {
                   10984:                // fflush(_fdopen(fd, ""));
1.1       root     10985:        } else {
                   10986:                REG16(AX) = 0x06;
1.1.1.3   root     10987:                m_CF = 1;
1.1       root     10988:        }
                   10989: }
                   10990: 
                   10991: inline void msdos_int_21h_69h()
                   10992: {
1.1.1.3   root     10993:        drive_info_t *info = (drive_info_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     10994:        char path[] = "A:\\";
                   10995:        char volume_label[MAX_PATH];
                   10996:        DWORD serial_number = 0;
                   10997:        char file_system[MAX_PATH];
                   10998:        
                   10999:        if(REG8(BL) == 0) {
                   11000:                path[0] = 'A' + _getdrive() - 1;
                   11001:        } else {
                   11002:                path[0] = 'A' + REG8(BL) - 1;
                   11003:        }
                   11004:        
                   11005:        switch(REG8(AL)) {
                   11006:        case 0x00:
                   11007:                if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
                   11008:                        info->info_level = 0;
                   11009:                        info->serial_number = serial_number;
                   11010:                        memset(info->volume_label, 0x20, 11);
                   11011:                        memcpy(info->volume_label, volume_label, min(strlen(volume_label), 11));
                   11012:                        memset(info->file_system, 0x20, 8);
                   11013:                        memcpy(info->file_system, file_system, min(strlen(file_system), 8));
                   11014:                } else {
                   11015:                        REG16(AX) = errno;
1.1.1.3   root     11016:                        m_CF = 1;
1.1       root     11017:                }
                   11018:                break;
                   11019:        case 0x01:
                   11020:                REG16(AX) = 0x03;
1.1.1.3   root     11021:                m_CF = 1;
1.1       root     11022:        }
                   11023: }
                   11024: 
                   11025: inline void msdos_int_21h_6ah()
                   11026: {
                   11027:        REG8(AH) = 0x68;
                   11028:        msdos_int_21h_68h();
                   11029: }
                   11030: 
                   11031: inline void msdos_int_21h_6bh()
                   11032: {
                   11033:        REG8(AL) = 0;
                   11034: }
                   11035: 
                   11036: inline void msdos_int_21h_6ch(int lfn)
                   11037: {
1.1.1.3   root     11038:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(SI)), lfn);
1.1       root     11039:        int mode = REG8(BL) & 0x03;
                   11040:        
                   11041:        if(mode < 0x03) {
1.1.1.29  root     11042:                if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1       root     11043:                        // file exists
                   11044:                        if(REG8(DL) & 1) {
1.1.1.37  root     11045:                                int fd = -1;
1.1.1.11  root     11046:                                UINT16 info;
1.1.1.37  root     11047:                                int sio_port = 0;
                   11048:                                int lpt_port = 0;
1.1       root     11049:                                
1.1.1.11  root     11050:                                if(msdos_is_con_path(path)) {
1.1.1.13  root     11051:                                        fd = msdos_open("CON", file_mode[mode].mode);
1.1.1.11  root     11052:                                        info = 0x80d3;
1.1.1.37  root     11053:                                } else if((sio_port = msdos_is_comm_path(path)) != 0) {
                   11054:                                        fd = msdos_open("NUL", file_mode[mode].mode);
1.1.1.14  root     11055:                                        info = 0x80d3;
1.1.1.37  root     11056:                                        msdos_set_comm_params(sio_port, path);
                   11057:                                } else if((lpt_port = msdos_is_prn_path(path)) != 0) {
                   11058:                                        fd = msdos_open("NUL", file_mode[mode].mode);
                   11059:                                        info = 0xa8c0;
1.1.1.29  root     11060:                                } else if(msdos_is_device_path(path)) {
1.1.1.20  root     11061:                                        fd = msdos_open("NUL", file_mode[mode].mode);
                   11062:                                        info = 0x80d3;
1.1.1.11  root     11063:                                } else {
1.1.1.13  root     11064:                                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     11065:                                        info = msdos_drive_number(path);
                   11066:                                }
1.1       root     11067:                                if(fd != -1) {
                   11068:                                        REG16(AX) = fd;
                   11069:                                        REG16(CX) = 1;
1.1.1.37  root     11070:                                        msdos_file_handler_open(fd, path, _isatty(fd), mode, info, current_psp, sio_port, lpt_port);
1.1.1.20  root     11071:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     11072:                                } else {
                   11073:                                        REG16(AX) = errno;
1.1.1.3   root     11074:                                        m_CF = 1;
1.1       root     11075:                                }
                   11076:                        } else if(REG8(DL) & 2) {
                   11077:                                int attr = GetFileAttributes(path);
1.1.1.37  root     11078:                                int fd = -1;
1.1.1.11  root     11079:                                UINT16 info;
1.1.1.37  root     11080:                                int sio_port = 0;
                   11081:                                int lpt_port = 0;
1.1       root     11082:                                
1.1.1.11  root     11083:                                if(msdos_is_con_path(path)) {
1.1.1.13  root     11084:                                        fd = msdos_open("CON", file_mode[mode].mode);
1.1.1.11  root     11085:                                        info = 0x80d3;
1.1.1.37  root     11086:                                } else if((sio_port = msdos_is_comm_path(path)) != 0) {
                   11087:                                        fd = msdos_open("NUL", file_mode[mode].mode);
1.1.1.14  root     11088:                                        info = 0x80d3;
1.1.1.37  root     11089:                                        msdos_set_comm_params(sio_port, path);
                   11090:                                } else if((lpt_port = msdos_is_prn_path(path)) != 0) {
                   11091:                                        fd = msdos_open("NUL", file_mode[mode].mode);
                   11092:                                        info = 0xa8c0;
1.1.1.29  root     11093:                                } else if(msdos_is_device_path(path)) {
1.1.1.20  root     11094:                                        fd = msdos_open("NUL", file_mode[mode].mode);
                   11095:                                        info = 0x80d3;
1.1       root     11096:                                } else {
                   11097:                                        fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
1.1.1.11  root     11098:                                        info = msdos_drive_number(path);
1.1       root     11099:                                }
                   11100:                                if(fd != -1) {
                   11101:                                        if(attr == -1) {
                   11102:                                                attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   11103:                                        }
                   11104:                                        SetFileAttributes(path, attr);
                   11105:                                        REG16(AX) = fd;
                   11106:                                        REG16(CX) = 3;
1.1.1.37  root     11107:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, info, current_psp, sio_port, lpt_port);
1.1.1.20  root     11108:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     11109:                                } else {
                   11110:                                        REG16(AX) = errno;
1.1.1.3   root     11111:                                        m_CF = 1;
1.1       root     11112:                                }
                   11113:                        } else {
                   11114:                                REG16(AX) = 0x50;
1.1.1.3   root     11115:                                m_CF = 1;
1.1       root     11116:                        }
                   11117:                } else {
                   11118:                        // file not exists
                   11119:                        if(REG8(DL) & 0x10) {
                   11120:                                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   11121:                                
                   11122:                                if(fd != -1) {
                   11123:                                        SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   11124:                                        REG16(AX) = fd;
                   11125:                                        REG16(CX) = 2;
                   11126:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     11127:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     11128:                                } else {
                   11129:                                        REG16(AX) = errno;
1.1.1.3   root     11130:                                        m_CF = 1;
1.1       root     11131:                                }
                   11132:                        } else {
                   11133:                                REG16(AX) = 0x02;
1.1.1.3   root     11134:                                m_CF = 1;
1.1       root     11135:                        }
                   11136:                }
                   11137:        } else {
                   11138:                REG16(AX) = 0x0c;
1.1.1.3   root     11139:                m_CF = 1;
1.1       root     11140:        }
                   11141: }
                   11142: 
                   11143: inline void msdos_int_21h_710dh()
                   11144: {
                   11145:        // reset drive
                   11146: }
                   11147: 
1.1.1.17  root     11148: inline void msdos_int_21h_7141h(int lfn)
                   11149: {
                   11150:        if(REG16(SI) == 0) {
                   11151:                msdos_int_21h_41h(lfn);
                   11152:                return;
                   11153:        }
                   11154:        if(REG16(SI) != 1) {
                   11155:                REG16(AX) = 5;
                   11156:                m_CF = 1;
                   11157:        }
                   11158:        /* wild card and matching attributes... */
                   11159:        char tmp[MAX_PATH * 2];
                   11160:        // copy search pathname (and quick check overrun)
                   11161:        ZeroMemory(tmp, sizeof(tmp));
                   11162:        tmp[MAX_PATH - 1] = '\0';
                   11163:        tmp[MAX_PATH] = 1;
                   11164:        strncpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(DX)), MAX_PATH);
                   11165:        
                   11166:        if(tmp[MAX_PATH - 1] != '\0' || tmp[MAX_PATH] != 1) {
                   11167:                REG16(AX) = 1;
                   11168:                m_CF = 1;
                   11169:                return;
                   11170:        }
                   11171:        for(char *s = tmp; *s; ++s) {
                   11172:                if(*s == '/') {
                   11173:                        *s = '\\';
                   11174:                }
                   11175:        }
                   11176:        char *tmp_name = (char *)_mbsrchr((unsigned char *)tmp, '\\');
                   11177:        if(tmp_name) {
                   11178:                ++tmp_name;
                   11179:        } else {
                   11180:                tmp_name = strchr(tmp, ':');
                   11181:                tmp_name = tmp_name ? tmp_name + 1 : tmp;
                   11182:        }
                   11183:        
                   11184:        WIN32_FIND_DATAA fd;
                   11185:        HANDLE fh = FindFirstFileA(tmp, &fd);
                   11186:        if(fh == INVALID_HANDLE_VALUE) {
                   11187:                REG16(AX) = 2;
                   11188:                m_CF = 1;
                   11189:                return;
                   11190:        }
                   11191:        do {
                   11192:                if(msdos_find_file_check_attribute(fd.dwFileAttributes, REG8(CL), REG8(CH)) && msdos_find_file_has_8dot3name(&fd)) {
                   11193:                        strcpy(tmp_name, fd.cFileName);
                   11194:                        if(remove(msdos_trimmed_path(tmp, lfn))) {
                   11195:                                REG16(AX) = 5;
                   11196:                                m_CF = 1;
                   11197:                                break;
                   11198:                        }
                   11199:                }
                   11200:        } while(FindNextFileA(fh, &fd));
                   11201:        if(!m_CF) {
                   11202:                if(GetLastError() != ERROR_NO_MORE_FILES) {
                   11203:                        m_CF = 1;
                   11204:                        REG16(AX) = 2;
                   11205:                }
                   11206:        }
                   11207:        FindClose(fh);
                   11208: }
                   11209: 
1.1       root     11210: inline void msdos_int_21h_714eh()
                   11211: {
                   11212:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     11213:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
                   11214:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     11215:        WIN32_FIND_DATA fd;
                   11216:        
1.1.1.13  root     11217:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   11218:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   11219:                FindClose(dtainfo->find_handle);
                   11220:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11221:        }
                   11222:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     11223:        dtainfo->allowable_mask = REG8(CL);
                   11224:        dtainfo->required_mask = REG8(CH);
                   11225:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     11226:        
1.1.1.14  root     11227:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   11228:                dtainfo->allowable_mask &= ~8;
1.1       root     11229:        }
1.1.1.14  root     11230:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   11231:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13  root     11232:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   11233:                                FindClose(dtainfo->find_handle);
                   11234:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11235:                                break;
                   11236:                        }
                   11237:                }
                   11238:        }
1.1.1.13  root     11239:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     11240:                find->attrib = fd.dwFileAttributes;
                   11241:                msdos_find_file_conv_local_time(&fd);
                   11242:                if(REG16(SI) == 0) {
                   11243:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   11244:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   11245:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   11246:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   11247:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   11248:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   11249:                } else {
                   11250:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   11251:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   11252:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   11253:                }
                   11254:                find->size_hi = fd.nFileSizeHigh;
                   11255:                find->size_lo = fd.nFileSizeLow;
                   11256:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     11257:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     11258:                REG16(AX) = dtainfo - dtalist + 1;
                   11259:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     11260:                // volume label
                   11261:                find->attrib = 8;
                   11262:                find->size_hi = find->size_lo = 0;
                   11263:                strcpy(find->full_name, process->volume_label);
                   11264:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     11265:                dtainfo->allowable_mask &= ~8;
                   11266:                REG16(AX) = dtainfo - dtalist + 1;
1.1       root     11267:        } else {
                   11268:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     11269:                m_CF = 1;
1.1       root     11270:        }
                   11271: }
                   11272: 
                   11273: inline void msdos_int_21h_714fh()
                   11274: {
                   11275:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     11276:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     11277:        WIN32_FIND_DATA fd;
                   11278:        
1.1.1.14  root     11279:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   11280:                REG16(AX) = 6;
1.1.1.13  root     11281:                m_CF = 1;
                   11282:                return;
                   11283:        }
1.1.1.14  root     11284:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     11285:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   11286:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     11287:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13  root     11288:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   11289:                                        FindClose(dtainfo->find_handle);
                   11290:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11291:                                        break;
                   11292:                                }
                   11293:                        }
                   11294:                } else {
1.1.1.13  root     11295:                        FindClose(dtainfo->find_handle);
                   11296:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11297:                }
                   11298:        }
1.1.1.13  root     11299:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     11300:                find->attrib = fd.dwFileAttributes;
                   11301:                msdos_find_file_conv_local_time(&fd);
                   11302:                if(REG16(SI) == 0) {
                   11303:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   11304:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   11305:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   11306:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   11307:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   11308:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   11309:                } else {
                   11310:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   11311:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   11312:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   11313:                }
                   11314:                find->size_hi = fd.nFileSizeHigh;
                   11315:                find->size_lo = fd.nFileSizeLow;
                   11316:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     11317:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     11318:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     11319:                // volume label
                   11320:                find->attrib = 8;
                   11321:                find->size_hi = find->size_lo = 0;
                   11322:                strcpy(find->full_name, process->volume_label);
                   11323:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     11324:                dtainfo->allowable_mask &= ~8;
1.1       root     11325:        } else {
                   11326:                REG16(AX) = 0x12;
1.1.1.3   root     11327:                m_CF = 1;
1.1       root     11328:        }
                   11329: }
                   11330: 
                   11331: inline void msdos_int_21h_71a0h()
                   11332: {
                   11333:        DWORD max_component_len, file_sys_flag;
                   11334:        
1.1.1.14  root     11335:        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))) {
                   11336:                REG16(BX) = (UINT16)file_sys_flag & (FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES | FILE_UNICODE_ON_DISK | FILE_VOLUME_IS_COMPRESSED);
                   11337:                REG16(BX) |= 0x4000;                            // supports LFN functions
1.1       root     11338:                REG16(CX) = (UINT16)max_component_len;          // 255
                   11339:                REG16(DX) = (UINT16)max_component_len + 5;      // 260
                   11340:        } else {
                   11341:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     11342:                m_CF = 1;
1.1       root     11343:        }
                   11344: }
                   11345: 
                   11346: inline void msdos_int_21h_71a1h()
                   11347: {
1.1.1.14  root     11348:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   11349:                REG16(AX) = 6;
1.1.1.13  root     11350:                m_CF = 1;
                   11351:                return;
                   11352:        }
1.1.1.14  root     11353:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     11354:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   11355:                FindClose(dtainfo->find_handle);
                   11356:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11357:        }
                   11358: }
                   11359: 
                   11360: inline void msdos_int_21h_71a6h()
                   11361: {
                   11362:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11363:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   11364:        
1.1.1.3   root     11365:        UINT8 *buffer = (UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     11366:        struct _stat64 status;
                   11367:        DWORD serial_number = 0;
                   11368:        
1.1.1.20  root     11369:        if(fd < process->max_files && file_handler[fd].valid) {
                   11370:                if(_fstat64(fd, &status) == 0) {
                   11371:                        if(file_handler[fd].path[1] == ':') {
1.1       root     11372:                                // NOTE: we need to consider the network file path "\\host\share\"
                   11373:                                char volume[] = "A:\\";
1.1.1.20  root     11374:                                volume[0] = file_handler[fd].path[1];
1.1       root     11375:                                GetVolumeInformation(volume, NULL, 0, &serial_number, NULL, NULL, NULL, 0);
                   11376:                        }
1.1.1.20  root     11377:                        *(UINT32 *)(buffer + 0x00) = GetFileAttributes(file_handler[fd].path);
1.1       root     11378:                        *(UINT32 *)(buffer + 0x04) = (UINT32)(status.st_ctime & 0xffffffff);
                   11379:                        *(UINT32 *)(buffer + 0x08) = (UINT32)((status.st_ctime >> 32) & 0xffffffff);
                   11380:                        *(UINT32 *)(buffer + 0x0c) = (UINT32)(status.st_atime & 0xffffffff);
                   11381:                        *(UINT32 *)(buffer + 0x10) = (UINT32)((status.st_atime >> 32) & 0xffffffff);
                   11382:                        *(UINT32 *)(buffer + 0x14) = (UINT32)(status.st_mtime & 0xffffffff);
                   11383:                        *(UINT32 *)(buffer + 0x18) = (UINT32)((status.st_mtime >> 32) & 0xffffffff);
                   11384:                        *(UINT32 *)(buffer + 0x1c) = serial_number;
                   11385:                        *(UINT32 *)(buffer + 0x20) = (UINT32)((status.st_size >> 32) & 0xffffffff);
                   11386:                        *(UINT32 *)(buffer + 0x24) = (UINT32)(status.st_size & 0xffffffff);
                   11387:                        *(UINT32 *)(buffer + 0x28) = status.st_nlink;
1.1.1.14  root     11388:                        // this is dummy id and it will be changed when it is reopened...
1.1       root     11389:                        *(UINT32 *)(buffer + 0x2c) = 0;
1.1.1.20  root     11390:                        *(UINT32 *)(buffer + 0x30) = file_handler[fd].id;
1.1       root     11391:                } else {
                   11392:                        REG16(AX) = errno;
1.1.1.3   root     11393:                        m_CF = 1;
1.1       root     11394:                }
                   11395:        } else {
                   11396:                REG16(AX) = 0x06;
1.1.1.3   root     11397:                m_CF = 1;
1.1       root     11398:        }
                   11399: }
                   11400: 
                   11401: inline void msdos_int_21h_71a7h()
                   11402: {
                   11403:        switch(REG8(BL)) {
                   11404:        case 0x00:
1.1.1.3   root     11405:                if(!FileTimeToDosDateTime((FILETIME *)(mem + SREG_BASE(DS) + REG16(SI)), &REG16(DX), &REG16(CX))) {
1.1       root     11406:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     11407:                        m_CF = 1;
1.1       root     11408:                }
                   11409:                break;
                   11410:        case 0x01:
                   11411:                // NOTE: we need to check BH that shows 10-millisecond untils past time in CX
1.1.1.3   root     11412:                if(!DosDateTimeToFileTime(REG16(DX), REG16(CX), (FILETIME *)(mem + SREG_BASE(ES) + REG16(DI)))) {
1.1       root     11413:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     11414:                        m_CF = 1;
1.1       root     11415:                }
                   11416:                break;
                   11417:        default:
1.1.1.22  root     11418:                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     11419:                REG16(AX) = 0x01;
1.1.1.3   root     11420:                m_CF = 1;
1.1       root     11421:                break;
                   11422:        }
                   11423: }
                   11424: 
                   11425: inline void msdos_int_21h_71a8h()
                   11426: {
                   11427:        if(REG8(DH) == 0) {
                   11428:                char tmp[MAX_PATH], fcb[MAX_PATH];
1.1.1.3   root     11429:                strcpy(tmp, msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     11430:                memset(fcb, 0x20, sizeof(fcb));
                   11431:                int len = strlen(tmp);
1.1.1.21  root     11432:                for(int i = 0, pos = 0; i < len; i++) {
1.1       root     11433:                        if(tmp[i] == '.') {
                   11434:                                pos = 8;
                   11435:                        } else {
                   11436:                                if(msdos_lead_byte_check(tmp[i])) {
                   11437:                                        fcb[pos++] = tmp[i++];
                   11438:                                }
                   11439:                                fcb[pos++] = tmp[i];
                   11440:                        }
                   11441:                }
1.1.1.3   root     11442:                memcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), fcb, 11);
1.1       root     11443:        } else {
1.1.1.3   root     11444:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     11445:        }
                   11446: }
                   11447: 
1.1.1.22  root     11448: inline void msdos_int_21h_71aah()
                   11449: {
                   11450:        char drv[] = "A:", path[MAX_PATH];
                   11451:        char *hoge=(char *)(mem + SREG_BASE(DS) + REG16(DX));
                   11452:        
                   11453:        if(REG8(BL) == 0) {
                   11454:                drv[0] = 'A' + _getdrive() - 1;
                   11455:        } else {
                   11456:                drv[0] = 'A' + REG8(BL) - 1;
                   11457:        }
                   11458:        switch(REG8(BH)) {
                   11459:        case 0x00:
                   11460:                if(DefineDosDevice(0, drv, (char *)(mem + SREG_BASE(DS) + REG16(DX))) == 0) {
                   11461:                        DWORD bits = 1 << ((REG8(BL) ? REG8(BL) : _getdrive()) - 1);
                   11462:                        if(GetLogicalDrives() & bits) {
                   11463:                                REG16(AX) = 0x0f; // invalid drive
                   11464:                        } else {
                   11465:                                REG16(AX) = 0x03; // path not found
                   11466:                        }
                   11467:                        m_CF = 1;
                   11468:                }
                   11469:                break;
                   11470:        case 0x01:
                   11471:                if(DefineDosDevice(DDD_REMOVE_DEFINITION, drv, NULL) == 0) {
                   11472:                        REG16(AX) = 0x0f; // invalid drive
                   11473:                        m_CF = 1;
                   11474:                }
                   11475:                break;
                   11476:        case 0x02:
                   11477:                if(QueryDosDevice(drv, path, MAX_PATH) == 0) {
                   11478:                        REG16(AX) = 0x0f; // invalid drive
                   11479:                        m_CF = 1;
                   11480:                } else if(strncmp(path, "\\??\\", 4) != 0) {
                   11481:                        REG16(AX) = 0x0f; // invalid drive
                   11482:                        m_CF = 1;
                   11483:                } else {
                   11484:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(DX)), path + 4);
                   11485:                }
                   11486:                break;
                   11487:        default:
                   11488:                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));
                   11489:                REG16(AX) = 0x01;
                   11490:                m_CF = 1;
                   11491:                break;
                   11492:        }
                   11493: }
                   11494: 
1.1.1.14  root     11495: inline void msdos_int_21h_7300h()
                   11496: {
                   11497:        if(REG8(AL) == 0) {
                   11498:                REG8(AL) = REG8(CL);
                   11499:                REG8(AH) = 0;
                   11500:        } else {
                   11501:                REG16(AX) = 0x01;
                   11502:                m_CF = 1;
                   11503:        }
                   11504: }
                   11505: 
                   11506: inline void msdos_int_21h_7302h()
                   11507: {
                   11508:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   11509:        UINT16 seg, ofs;
                   11510:        
                   11511:        if(REG16(CX) < 0x3f) {
                   11512:                REG8(AL) = 0x18;
                   11513:                m_CF = 1;
                   11514:        } else if(!msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   11515:                REG8(AL) = 0xff;
                   11516:                m_CF = 1;
                   11517:        } else {
                   11518:                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 2, mem + (seg << 4) + ofs, sizeof(dpb_t));
                   11519:        }
                   11520: }
                   11521: 
1.1       root     11522: inline void msdos_int_21h_7303h()
                   11523: {
1.1.1.3   root     11524:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   11525:        ext_space_info_t *info = (ext_space_info_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     11526:        DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   11527:        
                   11528:        if(GetDiskFreeSpace(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
                   11529:                info->size_of_structure = sizeof(ext_space_info_t);
                   11530:                info->structure_version = 0;
                   11531:                info->sectors_per_cluster = sectors_per_cluster;
                   11532:                info->bytes_per_sector = bytes_per_sector;
                   11533:                info->available_clusters_on_drive = free_clusters;
                   11534:                info->total_clusters_on_drive = total_clusters;
                   11535:                info->available_sectors_on_drive = sectors_per_cluster * free_clusters;
                   11536:                info->total_sectors_on_drive = sectors_per_cluster * total_clusters;
                   11537:                info->available_allocation_units = free_clusters;       // ???
                   11538:                info->total_allocation_units = total_clusters;          // ???
                   11539:        } else {
                   11540:                REG16(AX) = errno;
1.1.1.3   root     11541:                m_CF = 1;
1.1       root     11542:        }
                   11543: }
                   11544: 
1.1.1.30  root     11545: inline void msdos_int_21h_dbh()
                   11546: {
                   11547:        // Novell NetWare - Workstation - Get Number of Local Drives
                   11548:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   11549:        REG8(AL) = dos_info->last_drive;
                   11550: }
                   11551: 
                   11552: inline void msdos_int_21h_dch()
                   11553: {
                   11554:        // Novell NetWare - Connection Services - Get Connection Number
                   11555:        REG8(AL) = 0x00;
                   11556: }
                   11557: 
1.1.1.32  root     11558: inline void msdos_int_24h()
                   11559: {
                   11560:        const char *message = NULL;
                   11561:        int key = 0;
                   11562:        
                   11563:        for(int i = 0; i < array_length(critical_error_table); i++) {
                   11564:                if(critical_error_table[i].code == (REG16(DI) & 0xff) || critical_error_table[i].code == (UINT16)-1) {
                   11565:                        if(active_code_page == 932) {
                   11566:                                message = critical_error_table[i].message_japanese;
                   11567:                        }
                   11568:                        if(message == NULL) {
                   11569:                                message = critical_error_table[i].message_english;
                   11570:                        }
                   11571:                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   11572:                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   11573:                        
                   11574:                        SREG(ES) = WORK_TOP >> 4;
                   11575:                        i386_load_segment_descriptor(ES);
                   11576:                        REG16(DI) = 0x0000;
                   11577:                        break;
                   11578:                }
                   11579:        }
                   11580:        fprintf(stderr, "\n%s", message);
                   11581:        if(!(REG8(AH) & 0x80)) {
                   11582:                if(REG8(AH) & 0x01) {
                   11583:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�������ݒ� �h���C�u" : "writing drive", 'A' + REG8(AL));
                   11584:                } else {
                   11585:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�ǂݎ�蒆 �h���C�u" : "reading drive", 'A' + REG8(AL));
                   11586:                }
                   11587:        }
                   11588:        fprintf(stderr, "\n");
                   11589:        
1.1.1.33  root     11590:        {
1.1.1.32  root     11591:                fprintf(stderr, "%s",   (active_code_page == 932) ? "���~ (A)" : "Abort");
1.1.1.33  root     11592:        }
1.1.1.32  root     11593:        if(REG8(AH) & 0x10) {
                   11594:                fprintf(stderr, ", %s", (active_code_page == 932) ? "�Ď��s (R)" : "Retry");
                   11595:        }
                   11596:        if(REG8(AH) & 0x20) {
                   11597:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���� (I)" : "Ignore");
                   11598:        }
                   11599:        if(REG8(AH) & 0x08) {
                   11600:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���s (F)" : "Fail");
                   11601:        }
                   11602:        fprintf(stderr, "? ");
                   11603:        
                   11604:        while(1) {
                   11605:                while(!_kbhit()) {
                   11606:                        Sleep(10);
                   11607:                }
                   11608:                key = _getch();
                   11609:                
                   11610:                if(key == 'I' || key == 'i') {
                   11611:                        if(REG8(AH) & 0x20) {
                   11612:                                REG8(AL) = 0;
                   11613:                                break;
                   11614:                        }
                   11615:                } else if(key == 'R' || key == 'r') {
                   11616:                        if(REG8(AH) & 0x10) {
                   11617:                                REG8(AL) = 1;
                   11618:                                break;
                   11619:                        }
                   11620:                } else if(key == 'A' || key == 'a') {
                   11621:                        REG8(AL) = 2;
                   11622:                        break;
                   11623:                } else if(key == 'F' || key == 'f') {
                   11624:                        if(REG8(AH) & 0x08) {
                   11625:                                REG8(AL) = 3;
                   11626:                                break;
                   11627:                        }
                   11628:                }
                   11629:        }
                   11630:        fprintf(stderr, "%c\n", key);
                   11631: }
                   11632: 
1.1       root     11633: inline void msdos_int_25h()
                   11634: {
                   11635:        UINT16 seg, ofs;
                   11636:        DWORD dwSize;
                   11637:        
1.1.1.3   root     11638: #if defined(HAS_I386)
                   11639:        I386OP(pushf)();
                   11640: #else
                   11641:        PREFIX86(_pushf());
                   11642: #endif
1.1       root     11643:        
                   11644:        if(!(REG8(AL) < 26)) {
                   11645:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     11646:                m_CF = 1;
1.1       root     11647:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   11648:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     11649:                m_CF = 1;
1.1       root     11650:        } else {
                   11651:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   11652:                char dev[64];
                   11653:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   11654:                
                   11655:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   11656:                if(hFile == INVALID_HANDLE_VALUE) {
                   11657:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     11658:                        m_CF = 1;
1.1       root     11659:                } else {
1.1.1.19  root     11660:                        UINT32 top_sector  = REG16(DX);
                   11661:                        UINT16 sector_num  = REG16(CX);
                   11662:                        UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   11663:                        
                   11664:                        if(sector_num == 0xffff) {
                   11665:                                top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   11666:                                sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   11667:                                UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   11668:                                UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   11669:                                buffer_addr = (seg << 4) + ofs;
                   11670:                        }
                   11671: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   11672: //                             REG8(AL) = 0x02; // drive not ready
                   11673: //                             m_CF = 1;
                   11674: //                     } else 
                   11675:                        if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     11676:                                REG8(AL) = 0x08; // sector not found
1.1.1.3   root     11677:                                m_CF = 1;
1.1.1.19  root     11678:                        } else if(ReadFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     11679:                                REG8(AL) = 0x0b; // read error
1.1.1.3   root     11680:                                m_CF = 1;
1.1       root     11681:                        }
                   11682:                        CloseHandle(hFile);
                   11683:                }
                   11684:        }
                   11685: }
                   11686: 
                   11687: inline void msdos_int_26h()
                   11688: {
                   11689:        // this operation may cause serious damage for drives, so always returns error...
                   11690:        UINT16 seg, ofs;
                   11691:        DWORD dwSize;
                   11692:        
1.1.1.3   root     11693: #if defined(HAS_I386)
                   11694:        I386OP(pushf)();
                   11695: #else
                   11696:        PREFIX86(_pushf());
                   11697: #endif
1.1       root     11698:        
                   11699:        if(!(REG8(AL) < 26)) {
                   11700:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     11701:                m_CF = 1;
1.1       root     11702:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   11703:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     11704:                m_CF = 1;
1.1       root     11705:        } else {
                   11706:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   11707:                char dev[64];
                   11708:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   11709:                
                   11710:                if(dpb->media_type == 0xf8) {
                   11711:                        // this drive is not a floppy
1.1.1.6   root     11712: //                     if(!(((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag & 0x40)) {
                   11713: //                             fatalerror("This application tried the absolute disk write to drive %c:\n", 'A' + REG8(AL));
                   11714: //                     }
1.1       root     11715:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     11716:                        m_CF = 1;
1.1       root     11717:                } else {
                   11718:                        HANDLE hFile = CreateFile(dev, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   11719:                        if(hFile == INVALID_HANDLE_VALUE) {
                   11720:                                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     11721:                                m_CF = 1;
1.1       root     11722:                        } else {
1.1.1.19  root     11723:                                UINT32 top_sector  = REG16(DX);
                   11724:                                UINT16 sector_num  = REG16(CX);
                   11725:                                UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   11726:                                
                   11727:                                if(sector_num == 0xffff) {
                   11728:                                        top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   11729:                                        sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   11730:                                        UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   11731:                                        UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   11732:                                        buffer_addr = (seg << 4) + ofs;
                   11733:                                }
1.1       root     11734:                                if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   11735:                                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     11736:                                        m_CF = 1;
1.1.1.19  root     11737:                                } else if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     11738:                                        REG8(AL) = 0x08; // sector not found
1.1.1.3   root     11739:                                        m_CF = 1;
1.1.1.19  root     11740:                                } else if(WriteFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     11741:                                        REG8(AL) = 0x0a; // write error
1.1.1.3   root     11742:                                        m_CF = 1;
1.1       root     11743:                                }
                   11744:                                CloseHandle(hFile);
                   11745:                        }
                   11746:                }
                   11747:        }
                   11748: }
                   11749: 
                   11750: inline void msdos_int_27h()
                   11751: {
1.1.1.29  root     11752:        int paragraphs = (min(REG16(DX), 0xfff0) + 15) >> 4;
                   11753:        try {
                   11754:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   11755:        } catch(...) {
                   11756:                // recover the broken mcb
                   11757:                int mcb_seg = SREG(CS) - 1;
                   11758:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     11759:                
1.1.1.29  root     11760:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     11761:                        mcb->mz = 'M';
                   11762:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   11763:                        
1.1.1.29  root     11764:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.33  root     11765:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4));
1.1.1.29  root     11766:                        } else {
1.1.1.33  root     11767:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0);
1.1.1.29  root     11768:                        }
                   11769:                } else {
                   11770:                        mcb->mz = 'Z';
1.1.1.30  root     11771:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     11772:                }
                   11773:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   11774:        }
1.1.1.3   root     11775:        msdos_process_terminate(SREG(CS), retval | 0x300, 0);
1.1       root     11776: }
                   11777: 
                   11778: inline void msdos_int_29h()
                   11779: {
1.1.1.14  root     11780: #if 1
                   11781:        // need to check escape sequences
1.1       root     11782:        msdos_putch(REG8(AL));
1.1.1.14  root     11783: #else
                   11784:        DWORD num;
                   11785:        vram_flush();
1.1.1.23  root     11786:        WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), &REG8(AL), 1, &num, NULL);
1.1.1.14  root     11787:        cursor_moved = true;
                   11788: #endif
1.1       root     11789: }
                   11790: 
                   11791: inline void msdos_int_2eh()
                   11792: {
                   11793:        char tmp[MAX_PATH], command[MAX_PATH], opt[MAX_PATH];
                   11794:        memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     11795:        strcpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
1.1       root     11796:        char *token = my_strtok(tmp, " ");
                   11797:        strcpy(command, token);
                   11798:        strcpy(opt, token + strlen(token) + 1);
                   11799:        
                   11800:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   11801:        param->env_seg = 0;
                   11802:        param->cmd_line.w.l = 44;
                   11803:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   11804:        param->fcb1.w.l = 24;
                   11805:        param->fcb1.w.h = (WORK_TOP >> 4);
                   11806:        param->fcb2.w.l = 24;
                   11807:        param->fcb2.w.h = (WORK_TOP >> 4);
                   11808:        
                   11809:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   11810:        
                   11811:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   11812:        cmd_line->len = strlen(opt);
                   11813:        strcpy(cmd_line->cmd, opt);
                   11814:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   11815:        
1.1.1.28  root     11816:        try {
                   11817:                if(msdos_process_exec(command, param, 0)) {
                   11818:                        REG16(AX) = 0xffff; // error before processing command
                   11819:                } else {
                   11820:                        // set flag to set retval to ax when the started process is terminated
                   11821:                        process_t *process = msdos_process_info_get(current_psp);
                   11822:                        process->called_by_int2eh = true;
                   11823:                }
                   11824:        } catch(...) {
                   11825:                REG16(AX) = 0xffff; // error before processing command
                   11826:        }
1.1       root     11827: }
                   11828: 
1.1.1.29  root     11829: inline void msdos_int_2fh_05h()
                   11830: {
                   11831:        switch(REG8(AL)) {
                   11832:        case 0x00:
1.1.1.32  root     11833:                REG8(AL) = 0xff;
                   11834:                break;
                   11835:        case 0x01:
                   11836:        case 0x02:
                   11837:                for(int i = 0; i < array_length(standard_error_table); i++) {
                   11838:                        if(standard_error_table[i].code == REG16(BX) || standard_error_table[i].code == (UINT16)-1) {
                   11839:                                const char *message = NULL;
                   11840:                                if(active_code_page == 932) {
                   11841:                                        message = standard_error_table[i].message_japanese;
                   11842:                                }
                   11843:                                if(message == NULL) {
                   11844:                                        message = standard_error_table[i].message_english;
                   11845:                                }
                   11846:                                strcpy((char *)(mem + WORK_TOP), message);
                   11847:                                
                   11848:                                SREG(ES) = WORK_TOP >> 4;
                   11849:                                i386_load_segment_descriptor(ES);
                   11850:                                REG16(DI) = 0x0000;
                   11851:                                REG8(AL) = 0x01;
                   11852:                                break;
                   11853:                        }
                   11854:                }
1.1.1.29  root     11855:                break;
                   11856:        default:
                   11857:                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));
                   11858:                m_CF = 1;
                   11859:        }
                   11860: }
                   11861: 
1.1.1.22  root     11862: inline void msdos_int_2fh_11h()
                   11863: {
                   11864:        switch(REG8(AL)) {
                   11865:        case 0x00:
1.1.1.29  root     11866:                if(i386_read_stack() == 0xdada) {
                   11867:                        // MSCDEX is not installed
                   11868: //                     REG8(AL) = 0x00;
                   11869:                } else {
                   11870:                        // Network Redirector is not installed
                   11871: //                     REG8(AL) = 0x00;
                   11872:                }
1.1.1.22  root     11873:                break;
                   11874:        default:
                   11875:                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     11876:                REG16(AX) = 0x49; //  network software not installed
1.1.1.22  root     11877:                m_CF = 1;
                   11878:                break;
                   11879:        }
                   11880: }
                   11881: 
1.1.1.21  root     11882: inline void msdos_int_2fh_12h()
                   11883: {
                   11884:        switch(REG8(AL)) {
1.1.1.22  root     11885:        case 0x00:
1.1.1.29  root     11886:                // DOS 3.0+ internal functions are installed
1.1.1.22  root     11887:                REG8(AL) = 0xff;
                   11888:                break;
1.1.1.29  root     11889: //     case 0x01: // DOS 3.0+ internal - Close Current File
                   11890:        case 0x02:
                   11891:                {
                   11892:                        UINT16 stack = i386_read_stack();
                   11893:                        REG16(BX) = *(UINT16 *)(mem + 4 * stack + 0);
                   11894:                        SREG(ES) = *(UINT16 *)(mem + 4 * stack + 2);
                   11895:                        i386_load_segment_descriptor(ES);
                   11896:                }
                   11897:                break;
1.1.1.30  root     11898:        case 0x03:
                   11899:                SREG(DS) = (DEVICE_TOP >> 4);
                   11900:                i386_load_segment_descriptor(DS);
                   11901:                break;
1.1.1.29  root     11902:        case 0x04:
                   11903:                {
                   11904:                        UINT16 stack = i386_read_stack();
                   11905:                        REG8(AL) = (stack == '/') ? '\\' : stack;
                   11906: #if defined(HAS_I386)
                   11907:                        m_ZF = (REG8(AL) == '\\');
                   11908: #else
                   11909:                        m_ZeroVal = (REG8(AL) != '\\');
                   11910: #endif
                   11911:                }
                   11912:                break;
                   11913:        case 0x05:
                   11914:                msdos_putch(i386_read_stack());
                   11915:                break;
                   11916: //     case 0x06: // DOS 3.0+ internal - Invole Critical Error
                   11917: //     case 0x07: // DOS 3.0+ internal - Make Disk Buffer Most Recentry Used
                   11918: //     case 0x08: // DOS 3.0+ internal - Decrement SFT Reference Count
                   11919: //     case 0x09: // DOS 3.0+ internal - Flush and FREE Disk Buffer
                   11920: //     case 0x0a: // DOS 3.0+ internal - Perform Critical Error Interrupt
                   11921: //     case 0x0b: // DOS 3.0+ internal - Signal Sharing Violation to User
                   11922: //     case 0x0c: // DOS 3.0+ internal - Open Device and Set SFT Owner/Mode
                   11923:        case 0x0d:
                   11924:                {
                   11925:                        SYSTEMTIME time;
                   11926:                        FILETIME file_time;
                   11927:                        WORD dos_date, dos_time;
                   11928:                        GetLocalTime(&time);
                   11929:                        SystemTimeToFileTime(&time, &file_time);
                   11930:                        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   11931:                        REG16(AX) = dos_date;
                   11932:                        REG16(DX) = dos_time;
                   11933:                }
                   11934:                break;
                   11935: //     case 0x0e: // DOS 3.0+ internal - Mark All Disk Buffers Unreferenced
                   11936: //     case 0x0f: // DOS 3.0+ internal - Make Buffer Most Recentry Used
                   11937: //     case 0x10: // DOS 3.0+ internal - Find Unreferenced Disk Buffer
                   11938:        case 0x11:
                   11939:                {
                   11940:                        char path[MAX_PATH], *p;
                   11941:                        strcpy(path, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   11942:                        my_strupr(path);
                   11943:                        while((p = my_strchr(path, '/')) != NULL) {
                   11944:                                *p = '\\';
                   11945:                        }
                   11946:                        strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
                   11947:                }
                   11948:                break;
                   11949:        case 0x12:
                   11950:                REG16(CX) = strlen((char *)(mem + SREG_BASE(ES) + REG16(DI)));
                   11951:                break;
                   11952:        case 0x13:
                   11953:                {
                   11954:                        char tmp[2] = {0};
                   11955:                        tmp[0] = i386_read_stack();
                   11956:                        my_strupr(tmp);
                   11957:                        REG8(AL) = tmp[0];
                   11958:                }
                   11959:                break;
                   11960:        case 0x14:
                   11961: #if defined(HAS_I386)
                   11962:                m_ZF = (SREG_BASE(DS) + REG16(SI) == SREG_BASE(ES) + REG16(DI));
                   11963: #else
                   11964:                m_ZeroVal = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   11965: #endif
                   11966:                m_CF = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   11967:                break;
                   11968: //     case 0x15: // DOS 3.0+ internal - Flush Buffer
1.1.1.21  root     11969:        case 0x16:
                   11970:                if(REG16(BX) < 20) {
                   11971:                        SREG(ES) = SFT_TOP >> 4;
                   11972:                        i386_load_segment_descriptor(ES);
                   11973:                        REG16(DI) = 6 + 0x3b * REG16(BX);
                   11974:                        
                   11975:                        // update system file table
                   11976:                        UINT8* sft = mem + SFT_TOP + 6 + 0x3b * REG16(BX);
                   11977:                        if(file_handler[REG16(BX)].valid) {
                   11978:                                int count = 0;
                   11979:                                for(int i = 0; i < 20; i++) {
                   11980:                                        if(msdos_psp_get_file_table(i, current_psp) == REG16(BX)) {
                   11981:                                                count++;
                   11982:                                        }
                   11983:                                }
                   11984:                                *(UINT16 *)(sft + 0x00) = count ? count : 0xffff;
                   11985:                                *(UINT32 *)(sft + 0x15) = _tell(REG16(BX));
                   11986:                                _lseek(REG16(BX), 0, SEEK_END);
                   11987:                                *(UINT32 *)(sft + 0x11) = _tell(REG16(BX));
                   11988:                                _lseek(REG16(BX), *(UINT32 *)(sft + 0x15), SEEK_SET);
                   11989:                        } else {
                   11990:                                memset(sft, 0, 0x3b);
                   11991:                        }
                   11992:                } else {
                   11993:                        REG16(AX) = 0x06;
                   11994:                        m_CF = 1;
                   11995:                }
                   11996:                break;
1.1.1.29  root     11997: //     case 0x17: // DOS 3.0+ internal - Get Current Directory Structure for Drive
                   11998: //     case 0x18: // DOS 3.0+ internal - Get Caller's Registers
                   11999: //     case 0x19: // DOS 3.0+ internal - Set Drive???
                   12000:        case 0x1a:
                   12001:                {
                   12002:                        char *path = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full[MAX_PATH];
                   12003:                        if(path[1] == ':') {
                   12004:                                if(path[0] >= 'a' && path[0] <= 'z') {
                   12005:                                        REG8(AL) = path[0] - 'a' + 1;
                   12006:                                } else if(path[0] >= 'A' && path[0] <= 'Z') {
                   12007:                                        REG8(AL) = path[0] - 'A' + 1;
                   12008:                                } else {
                   12009:                                        REG8(AL) = 0xff; // invalid
                   12010:                                }
                   12011:                                strcpy(full, path);
                   12012:                                strcpy(path, full + 2);
                   12013:                        } else if(GetFullPathName(path, MAX_PATH, full, NULL) != 0 && full[1] == ':') {
                   12014:                                if(full[0] >= 'a' && full[0] <= 'z') {
                   12015:                                        REG8(AL) = full[0] - 'a' + 1;
                   12016:                                } else if(full[0] >= 'A' && full[0] <= 'Z') {
                   12017:                                        REG8(AL) = full[0] - 'A' + 1;
                   12018:                                } else {
                   12019:                                        REG8(AL) = 0xff; // invalid
                   12020:                                }
                   12021:                        } else {
                   12022:                                REG8(AL) = 0x00; // default
                   12023:                        }
                   12024:                }
                   12025:                break;
                   12026:        case 0x1b:
                   12027:                {
                   12028:                        int year = REG16(CX) + 1980;
                   12029:                        REG8(AL) = ((year % 4) == 0 && (year % 100) != 0 && (year % 400) == 0) ? 29 : 28;
                   12030:                }
                   12031:                break;
                   12032: //     case 0x1c: // DOS 3.0+ internal - Check Sum Memory
                   12033: //     case 0x1d: // DOS 3.0+ internal - Sum Memory
                   12034:        case 0x1e:
                   12035:                {
                   12036:                        char *path_1st = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full_1st[MAX_PATH];
                   12037:                        char *path_2nd = (char *)(mem + SREG_BASE(ES) + REG16(DI)), full_2nd[MAX_PATH];
                   12038:                        if(GetFullPathName(path_1st, MAX_PATH, full_1st, NULL) != 0 && GetFullPathName(path_2nd, MAX_PATH, full_2nd, NULL) != 0) {
                   12039: #if defined(HAS_I386)
                   12040:                                m_ZF = (strcmp(full_1st, full_2nd) == 0);
                   12041: #else
                   12042:                                m_ZeroVal = (strcmp(full_1st, full_2nd) != 0);
                   12043: #endif
                   12044:                        } else {
                   12045: #if defined(HAS_I386)
                   12046:                                m_ZF = (strcmp(path_1st, path_2nd) == 0);
                   12047: #else
                   12048:                                m_ZeroVal = (strcmp(path_1st, path_2nd) != 0);
                   12049: #endif
                   12050:                        }
                   12051:                }
                   12052:                break;
                   12053: //     case 0x1f: // DOS 3.0+ internal - Build Current Directory Structure
1.1.1.21  root     12054:        case 0x20:
                   12055:                {
                   12056:                        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   12057:                        
                   12058:                        if(fd < 20) {
                   12059:                                SREG(ES) = current_psp;
                   12060:                                i386_load_segment_descriptor(ES);
                   12061:                                REG16(DI) = offsetof(psp_t, file_table) + fd;
                   12062:                        } else {
                   12063:                                REG16(AX) = 0x06;
                   12064:                                m_CF = 1;
                   12065:                        }
                   12066:                }
                   12067:                break;
1.1.1.29  root     12068:        case 0x21:
                   12069:                msdos_int_21h_60h(0);
                   12070:                break;
                   12071: //     case 0x22: // DOS 3.0+ internal - Set Extended Error Info
                   12072: //     case 0x23: // DOS 3.0+ internal - Check If Character Device
                   12073: //     case 0x24: // DOS 3.0+ internal - Sharing Retry Delay
                   12074:        case 0x25:
                   12075:                REG16(CX) = strlen((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   12076:                break;
                   12077:        case 0x26:
                   12078:                REG8(AL) = REG8(CL);
                   12079:                msdos_int_21h_3dh();
                   12080:                break;
                   12081:        case 0x27:
                   12082:                msdos_int_21h_3eh();
                   12083:                break;
                   12084:        case 0x28:
                   12085:                REG16(AX) = REG16(BP);
                   12086:                msdos_int_21h_42h();
                   12087:                break;
                   12088:        case 0x29:
                   12089:                msdos_int_21h_3fh();
                   12090:                break;
                   12091: //     case 0x2a: // DOS 3.0+ internal - Set Fast Open Entry Point
                   12092:        case 0x2b:
                   12093:                REG16(AX) = REG16(BP);
                   12094:                msdos_int_21h_44h();
                   12095:                break;
                   12096:        case 0x2c:
                   12097:                REG16(BX) = DEVICE_TOP >> 4;
                   12098:                REG16(AX) = 22;
                   12099:                break;
                   12100:        case 0x2d:
                   12101:                {
                   12102:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   12103:                        REG16(AX) = sda->extended_error_code;
                   12104:                }
                   12105:                break;
                   12106:        case 0x2e:
                   12107:                if(REG8(DL) == 0x00 || REG8(DL) == 0x02 || REG8(DL) == 0x04 || REG8(DL) == 0x06) {
1.1.1.32  root     12108:                        SREG(ES) = 0x0001;
                   12109:                        i386_load_segment_descriptor(ES);
                   12110:                        REG16(DI) = 0x00;
                   12111:                } else if(REG8(DL) == 0x08) {
                   12112:                        // dummy parameter error message read routine is at fffd:0010
                   12113:                        SREG(ES) = 0xfffd;
1.1.1.22  root     12114:                        i386_load_segment_descriptor(ES);
1.1.1.32  root     12115:                        REG16(DI) = 0x0010;
1.1.1.22  root     12116:                }
                   12117:                break;
1.1.1.29  root     12118:        case 0x2f:
                   12119:                if(REG16(DX) != 0) {
1.1.1.30  root     12120:                        dos_major_version = REG8(DL);
                   12121:                        dos_minor_version = REG8(DH);
1.1.1.29  root     12122:                } else {
                   12123:                        REG8(DL) = 7;
                   12124:                        REG8(DH) = 10;
                   12125:                }
                   12126:                break;
                   12127: //     case 0x30: // Windows95 - Find SFT Entry in Internal File Tables
                   12128: //     case 0x31: // Windows95 - Set/Clear "Report Windows to DOS Programs" Flag
1.1.1.22  root     12129:        default:
                   12130:                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));
                   12131:                REG16(AX) = 0x01;
                   12132:                m_CF = 1;
                   12133:                break;
                   12134:        }
                   12135: }
                   12136: 
1.1.1.30  root     12137: inline void msdos_int_2fh_13h()
                   12138: {
                   12139:        static UINT16 prevDS = 0, prevDX = 0;
                   12140:        static UINT16 prevES = 0, prevBX = 0;
                   12141:        UINT16 tmp;
                   12142:        
                   12143:        tmp = SREG(DS); SREG(DS) = prevDS; prevDS = tmp;
                   12144:        i386_load_segment_descriptor(DS);
                   12145:        tmp = REG16(DX); REG16(DX) = prevDX; prevDX = tmp;
                   12146:        
                   12147:        tmp = SREG(ES); SREG(ES) = prevES; prevES = tmp;
                   12148:        i386_load_segment_descriptor(ES);
                   12149:        tmp = REG16(BX); REG16(BX) = prevBX; prevBX = tmp;
                   12150: }
                   12151: 
1.1.1.22  root     12152: inline void msdos_int_2fh_14h()
                   12153: {
                   12154:        switch(REG8(AL)) {
                   12155:        case 0x00:
1.1.1.29  root     12156:                // NLSFUNC.COM is installed
                   12157:                REG8(AL) = 0xff;
1.1.1.25  root     12158:                break;
                   12159:        case 0x01:
                   12160:        case 0x03:
                   12161:                REG8(AL) = 0x00;
                   12162:                active_code_page = REG16(BX);
                   12163:                msdos_nls_tables_update();
                   12164:                break;
                   12165:        case 0x02:
                   12166:                REG8(AL) = get_extended_country_info(REG16(BP));
                   12167:                break;
                   12168:        case 0x04:
                   12169:                REG8(AL) = 0x00;
                   12170:                get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)));
1.1.1.22  root     12171:                break;
                   12172:        default:
                   12173:                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));
                   12174:                REG16(AX) = 0x01;
                   12175:                m_CF = 1;
                   12176:                break;
                   12177:        }
                   12178: }
                   12179: 
                   12180: inline void msdos_int_2fh_15h()
                   12181: {
                   12182:        switch(REG8(AL)) {
1.1.1.29  root     12183:        case 0x00: // CD-ROM - Installation Check
                   12184:                if(REG16(BX) == 0x0000) {
                   12185:                        // MSCDEX is not installed
                   12186: //                     REG8(AL) = 0x00;
                   12187:                } else {
                   12188:                        // GRAPHICS.COM is not installed
                   12189: //                     REG8(AL) = 0x00;
                   12190:                }
1.1.1.22  root     12191:                break;
                   12192:        case 0xff:
1.1.1.29  root     12193:                if(REG16(BX) == 0x0000) {
                   12194:                        // CORELCDX is not installed
                   12195:                } else {
                   12196:                        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));
                   12197:                        REG16(AX) = 0x01;
                   12198:                        m_CF = 1;
                   12199:                }
1.1.1.22  root     12200:                break;
1.1.1.21  root     12201:        default:
1.1.1.22  root     12202:                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     12203:                REG16(AX) = 0x01;
                   12204:                m_CF = 1;
                   12205:                break;
                   12206:        }
                   12207: }
                   12208: 
1.1       root     12209: inline void msdos_int_2fh_16h()
                   12210: {
                   12211:        switch(REG8(AL)) {
                   12212:        case 0x00:
1.1.1.14  root     12213:                if(no_windows) {
1.1.1.29  root     12214:                        // neither Windows 3.x enhanced mode nor Windows/386 2.x running
                   12215: //                     REG8(AL) = 0x00;
1.1.1.14  root     12216:                } else {
1.1.1.30  root     12217:                        REG8(AL) = win_major_version;
                   12218:                        REG8(AH) = win_minor_version;
1.1       root     12219:                }
                   12220:                break;
1.1.1.30  root     12221:        case 0x05:
                   12222:                // from DOSBox
                   12223:                i386_set_a20_line(1);
                   12224:                break;
1.1.1.22  root     12225:        case 0x0a:
                   12226:                if(!no_windows) {
                   12227:                        REG16(AX) = 0x0000;
1.1.1.30  root     12228:                        REG8(BH) = win_major_version;
                   12229:                        REG8(BL) = win_minor_version;
1.1.1.22  root     12230:                        REG16(CX) = 0x0003; // enhanced
                   12231:                }
                   12232:                break;
1.1.1.30  root     12233:        case 0x0b:
                   12234:                // no TRS, keep ES:DI = 0000h:0000h
1.1.1.22  root     12235:        case 0x0e:
                   12236:        case 0x0f:
1.1.1.30  root     12237:        case 0x10:
1.1.1.22  root     12238:        case 0x11:
                   12239:        case 0x12:
                   12240:        case 0x13:
                   12241:        case 0x14:
1.1.1.30  root     12242:        case 0x15:
1.1.1.33  root     12243:        case 0x86:
1.1.1.22  root     12244:        case 0x87:
1.1.1.30  root     12245:        case 0x89:
1.1.1.33  root     12246:        case 0x8a:
1.1.1.22  root     12247:                // function not supported, do not clear AX
                   12248:                break;
1.1.1.14  root     12249:        case 0x80:
                   12250:                Sleep(10);
1.1.1.35  root     12251:                REQUEST_HARDWRE_UPDATE();
1.1.1.29  root     12252:                REG8(AL) = 0x00;
1.1.1.14  root     12253:                break;
1.1.1.33  root     12254:        case 0x83:
                   12255:                REG16(BX) = 0x01; // system vm id
                   12256:                break;
1.1.1.22  root     12257:        case 0x8e:
                   12258:                REG16(AX) = 0x00; // failed
                   12259:                break;
1.1.1.20  root     12260:        case 0x8f:
                   12261:                switch(REG8(DH)) {
                   12262:                case 0x00:
                   12263:                case 0x02:
                   12264:                case 0x03:
                   12265:                        REG16(AX) = 0x00;
                   12266:                        break;
                   12267:                case 0x01:
                   12268:                        REG16(AX) = 0x168f;
                   12269:                        break;
                   12270:                }
                   12271:                break;
1.1       root     12272:        default:
1.1.1.22  root     12273:                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));
                   12274:                REG16(AX) = 0x01;
                   12275:                m_CF = 1;
                   12276:                break;
                   12277:        }
                   12278: }
                   12279: 
                   12280: inline void msdos_int_2fh_19h()
                   12281: {
                   12282:        switch(REG8(AL)) {
                   12283:        case 0x00:
1.1.1.29  root     12284:                // SHELLB.COM is not installed
                   12285: //             REG8(AL) = 0x00;
1.1.1.22  root     12286:                break;
                   12287:        case 0x01:
                   12288:        case 0x02:
                   12289:        case 0x03:
                   12290:        case 0x04:
                   12291:                REG16(AX) = 0x01;
                   12292:                m_CF = 1;
                   12293:                break;
1.1.1.29  root     12294:        case 0x80:
                   12295:                // IBM ROM-DOS v4.0 is not installed
                   12296: //             REG8(AL) = 0x00;
                   12297:                break;
1.1.1.22  root     12298:        default:
                   12299:                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     12300:                REG16(AX) = 0x01;
1.1.1.3   root     12301:                m_CF = 1;
1.1       root     12302:                break;
                   12303:        }
                   12304: }
                   12305: 
                   12306: inline void msdos_int_2fh_1ah()
                   12307: {
                   12308:        switch(REG8(AL)) {
                   12309:        case 0x00:
1.1.1.29  root     12310:                // ANSI.SYS is installed
1.1       root     12311:                REG8(AL) = 0xff;
                   12312:                break;
                   12313:        default:
1.1.1.22  root     12314:                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));
                   12315:                REG16(AX) = 0x01;
                   12316:                m_CF = 1;
                   12317:                break;
                   12318:        }
                   12319: }
                   12320: 
1.1.1.30  root     12321: inline void msdos_int_2fh_40h()
1.1.1.22  root     12322: {
                   12323:        switch(REG8(AL)) {
                   12324:        case 0x00:
1.1.1.30  root     12325:                // Windows 3+ - Get Virtual Device Driver (VDD) Capabilities
                   12326:                REG8(AL) = 0x01; // does not virtualize video access
1.1.1.22  root     12327:                break;
                   12328:        default:
                   12329:                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     12330:                REG16(AX) = 0x01;
1.1.1.3   root     12331:                m_CF = 1;
1.1       root     12332:                break;
                   12333:        }
                   12334: }
                   12335: 
                   12336: inline void msdos_int_2fh_43h()
                   12337: {
                   12338:        switch(REG8(AL)) {
                   12339:        case 0x00:
1.1.1.29  root     12340:                // XMS is installed ?
1.1.1.19  root     12341: #ifdef SUPPORT_XMS
                   12342:                if(support_xms) {
                   12343:                        REG8(AL) = 0x80;
                   12344:                } else
                   12345: #endif
                   12346:                REG8(AL) = 0x00;
                   12347:                break;
                   12348:        case 0x10:
                   12349:                SREG(ES) = XMS_TOP >> 4;
                   12350:                i386_load_segment_descriptor(ES);
1.1.1.26  root     12351:                REG16(BX) = 0x15;
1.1       root     12352:                break;
                   12353:        default:
1.1.1.22  root     12354:                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));
                   12355:                REG16(AX) = 0x01;
                   12356:                m_CF = 1;
                   12357:                break;
                   12358:        }
                   12359: }
                   12360: 
                   12361: inline void msdos_int_2fh_46h()
                   12362: {
                   12363:        switch(REG8(AL)) {
                   12364:        case 0x80:
1.1.1.29  root     12365:                // Windows v3.0 is not installed
                   12366: //             REG8(AL) = 0x00;
1.1.1.22  root     12367:                break;
                   12368:        default:
                   12369:                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));
                   12370:                REG16(AX) = 0x01;
                   12371:                m_CF = 1;
                   12372:                break;
                   12373:        }
                   12374: }
                   12375: 
                   12376: inline void msdos_int_2fh_48h()
                   12377: {
                   12378:        switch(REG8(AL)) {
                   12379:        case 0x00:
1.1.1.29  root     12380:                // DOSKEY is not installed
                   12381: //             REG8(AL) = 0x00;
1.1.1.22  root     12382:                break;
                   12383:        case 0x10:
                   12384:                msdos_int_21h_0ah();
                   12385:                REG16(AX) = 0x00;
                   12386:                break;
                   12387:        default:
                   12388:                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     12389:                REG16(AX) = 0x01;
1.1.1.3   root     12390:                m_CF = 1;
1.1       root     12391:                break;
                   12392:        }
                   12393: }
                   12394: 
                   12395: inline void msdos_int_2fh_4ah()
                   12396: {
                   12397:        switch(REG8(AL)) {
1.1.1.29  root     12398: #ifdef SUPPORT_HMA
                   12399:        case 0x01: // DOS 5.0+ - Query Free HMA Space
                   12400:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   12401:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   12402:                                // restore first free mcb in high memory area
                   12403:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   12404:                        }
                   12405:                        int offset = 0xffff;
                   12406:                        if((REG16(BX) = msdos_hma_mem_get_free(&offset)) != 0) {
                   12407:                                REG16(DI) = offset + 0x10;
                   12408:                        } else {
                   12409:                                REG16(DI) = 0xffff;
                   12410:                        }
                   12411:                } else {
                   12412:                        // HMA is already used
                   12413:                        REG16(BX) = 0;
                   12414:                        REG16(DI) = 0xffff;
                   12415:                }
                   12416:                SREG(ES) = 0xffff;
                   12417:                i386_load_segment_descriptor(ES);
                   12418:                break;
                   12419:        case 0x02: // DOS 5.0+ - Allocate HMA Space
                   12420:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   12421:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   12422:                                // restore first free mcb in high memory area
                   12423:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   12424:                        }
                   12425:                        int size = REG16(BX), offset;
                   12426:                        if((size % 16) != 0) {
                   12427:                                size &= ~15;
                   12428:                                size += 16;
                   12429:                        }
                   12430:                        if((offset = msdos_hma_mem_alloc(size, current_psp)) != -1) {
                   12431:                                REG16(BX) = size;
                   12432:                                REG16(DI) = offset + 0x10;
                   12433:                                is_hma_used_by_int_2fh = true;
                   12434:                        } else {
                   12435:                                REG16(BX) = 0;
                   12436:                                REG16(DI) = 0xffff;
                   12437:                        }
                   12438:                } else {
                   12439:                        // HMA is already used
                   12440:                        REG16(BX) = 0;
                   12441:                        REG16(DI) = 0xffff;
                   12442:                }
                   12443:                SREG(ES) = 0xffff;
                   12444:                i386_load_segment_descriptor(ES);
                   12445:                break;
                   12446:        case 0x03: // Windows95 - (De)Allocate HMA Memory Block
                   12447:                if(REG8(DL) == 0x00) {
                   12448:                        if(!is_hma_used_by_xms) {
                   12449:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   12450:                                        // restore first free mcb in high memory area
                   12451:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   12452:                                        is_hma_used_by_int_2fh = false;
                   12453:                                }
                   12454:                                int size = REG16(BX), offset;
                   12455:                                if((size % 16) != 0) {
                   12456:                                        size &= ~15;
                   12457:                                        size += 16;
                   12458:                                }
                   12459:                                if((offset = msdos_hma_mem_alloc(size, REG16(CX))) != -1) {
                   12460: //                                     REG16(BX) = size;
                   12461:                                        SREG(ES) = 0xffff;
                   12462:                                        i386_load_segment_descriptor(ES);
                   12463:                                        REG16(DI) = offset + 0x10;
                   12464:                                        is_hma_used_by_int_2fh = true;
                   12465:                                } else {
                   12466:                                        REG16(DI) = 0xffff;
                   12467:                                }
                   12468:                        } else {
                   12469:                                REG16(DI) = 0xffff;
                   12470:                        }
                   12471:                } else if(REG8(DL) == 0x01) {
                   12472:                        if(!is_hma_used_by_xms) {
                   12473:                                int size = REG16(BX);
                   12474:                                if((size % 16) != 0) {
                   12475:                                        size &= ~15;
                   12476:                                        size += 16;
                   12477:                                }
                   12478:                                if(msdos_hma_mem_realloc(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10, size) != -1) {
                   12479:                                        // memory block address is not changed
                   12480:                                } else {
                   12481:                                        REG16(DI) = 0xffff;
                   12482:                                }
                   12483:                        } else {
                   12484:                                REG16(DI) = 0xffff;
                   12485:                        }
                   12486:                } else if(REG8(DL) == 0x02) {
                   12487:                        if(!is_hma_used_by_xms) {
                   12488:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   12489:                                        // restore first free mcb in high memory area
                   12490:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   12491:                                        is_hma_used_by_int_2fh = false;
                   12492:                                } else {
                   12493:                                        msdos_hma_mem_free(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10);
                   12494:                                        if(msdos_hma_mem_get_free(NULL) == 0xffe0) {
                   12495:                                                is_hma_used_by_int_2fh = false;
                   12496:                                        }
                   12497:                                }
                   12498:                        }
                   12499:                } else {
                   12500:                        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));
                   12501:                        REG16(AX) = 0x01;
                   12502:                        m_CF = 1;
                   12503:                }
                   12504:                break;
                   12505:        case 0x04: // Windows95 - Get Start of HMA Memory Chain
                   12506:                if(!is_hma_used_by_xms) {
                   12507:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   12508:                                // restore first free mcb in high memory area
                   12509:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   12510:                                is_hma_used_by_int_2fh = false;
                   12511:                        }
                   12512:                        REG16(AX) = 0x0000;
                   12513:                        SREG(ES) = 0xffff;
                   12514:                        i386_load_segment_descriptor(ES);
                   12515:                        REG16(DI) = 0x10;
                   12516:                }
                   12517:                break;
                   12518: #else
1.1       root     12519:        case 0x01:
                   12520:        case 0x02:
1.1.1.29  root     12521:                // HMA is already used
1.1.1.27  root     12522:                REG16(BX) = 0x0000;
1.1.1.3   root     12523:                SREG(ES) = 0xffff;
                   12524:                i386_load_segment_descriptor(ES);
1.1       root     12525:                REG16(DI) = 0xffff;
                   12526:                break;
1.1.1.19  root     12527:        case 0x03:
                   12528:                // unable to allocate
                   12529:                REG16(DI) = 0xffff;
                   12530:                break;
                   12531:        case 0x04:
                   12532:                // function not supported, do not clear AX
                   12533:                break;
1.1.1.29  root     12534: #endif
                   12535:        case 0x10:
                   12536:                if(REG16(BX) == 0x0000) {
                   12537:                        // SMARTDRV is not installed
                   12538:                } else {
                   12539:                        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));
                   12540:                        REG16(AX) = 0x01;
                   12541:                        m_CF = 1;
                   12542:                }
                   12543:                break;
                   12544:        case 0x11:
                   12545:                if(REG16(BX) == 0x0000) {
                   12546:                        // DBLSPACE.BIN is not installed
                   12547:                } else {
                   12548:                        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));
                   12549:                        REG16(AX) = 0x01;
                   12550:                        m_CF = 1;
                   12551:                }
1.1.1.22  root     12552:                break;
                   12553:        default:
                   12554:                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));
                   12555:                REG16(AX) = 0x01;
                   12556:                m_CF = 1;
                   12557:                break;
                   12558:        }
                   12559: }
                   12560: 
                   12561: inline void msdos_int_2fh_4bh()
                   12562: {
                   12563:        switch(REG8(AL)) {
1.1.1.24  root     12564:        case 0x01:
1.1.1.22  root     12565:        case 0x02:
1.1.1.29  root     12566:                // Task Switcher is not installed
1.1.1.24  root     12567:                break;
                   12568:        case 0x03:
                   12569:                // this call is available from within DOSSHELL even if the task switcher is not installed
                   12570:                REG16(AX) = REG16(BX) = 0x0000; // no more avaiable switcher id
1.1.1.22  root     12571:                break;
1.1.1.30  root     12572:        case 0x04:
                   12573:                REG16(BX) = 0x0000; // free switcher id successfully
                   12574:                break;
1.1       root     12575:        default:
1.1.1.22  root     12576:                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     12577:                REG16(AX) = 0x01;
1.1.1.3   root     12578:                m_CF = 1;
1.1       root     12579:                break;
                   12580:        }
                   12581: }
                   12582: 
                   12583: inline void msdos_int_2fh_4fh()
                   12584: {
                   12585:        switch(REG8(AL)) {
                   12586:        case 0x00:
1.1.1.29  root     12587:                // BILING is installed
1.1.1.27  root     12588:                REG16(AX) = 0x0000;
                   12589:                REG8(DL) = 0x01;        // major version
                   12590:                REG8(DH) = 0x00;        // minor version
1.1       root     12591:                break;
                   12592:        case 0x01:
1.1.1.27  root     12593:                REG16(AX) = 0x0000;
1.1       root     12594:                REG16(BX) = active_code_page;
                   12595:                break;
                   12596:        default:
1.1.1.22  root     12597:                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));
                   12598:                REG16(AX) = 0x01;
                   12599:                m_CF = 1;
                   12600:                break;
                   12601:        }
                   12602: }
                   12603: 
                   12604: inline void msdos_int_2fh_55h()
                   12605: {
                   12606:        switch(REG8(AL)) {
                   12607:        case 0x00:
                   12608:        case 0x01:
                   12609: //             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));
                   12610:                break;
                   12611:        default:
                   12612:                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     12613:                REG16(AX) = 0x01;
1.1.1.3   root     12614:                m_CF = 1;
1.1       root     12615:                break;
                   12616:        }
                   12617: }
                   12618: 
1.1.1.24  root     12619: inline void msdos_int_2fh_adh()
                   12620: {
                   12621:        switch(REG8(AL)) {
                   12622:        case 0x00:
1.1.1.29  root     12623:                // DISPLAY.SYS is installed
1.1.1.24  root     12624:                REG8(AL) = 0xff;
                   12625:                REG16(BX) = 0x100; // ???
                   12626:                break;
                   12627:        case 0x01:
                   12628:                active_code_page = REG16(BX);
                   12629:                msdos_nls_tables_update();
                   12630:                REG16(AX) = 0x01;
                   12631:                break;
                   12632:        case 0x02:
                   12633:                REG16(BX) = active_code_page;
                   12634:                break;
                   12635:        case 0x03:
                   12636:                // FIXME
                   12637:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 1;
                   12638:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2) = 3;
                   12639:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4) = 1;
                   12640:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 6) = active_code_page;
                   12641:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 8) = active_code_page;
                   12642:                break;
                   12643:        case 0x80:
                   12644:                break; // keyb.com is not installed
                   12645:        default:
                   12646:                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));
                   12647:                REG16(AX) = 0x01;
                   12648:                m_CF = 1;
                   12649:                break;
                   12650:        }
                   12651: }
                   12652: 
1.1       root     12653: inline void msdos_int_2fh_aeh()
                   12654: {
                   12655:        switch(REG8(AL)) {
                   12656:        case 0x00:
1.1.1.28  root     12657:                // FIXME: we need to check the given command line
                   12658:                REG8(AL) = 0x00; // the command should be executed as usual
                   12659: //             REG8(AL) = 0xff; // this command is a TSR extension to COMMAND.COM
1.1       root     12660:                break;
                   12661:        case 0x01:
                   12662:                {
                   12663:                        char command[MAX_PATH];
                   12664:                        memset(command, 0, sizeof(command));
1.1.1.3   root     12665:                        memcpy(command, mem + SREG_BASE(DS) + REG16(SI) + 1, mem[SREG_BASE(DS) + REG16(SI)]);
1.1       root     12666:                        
                   12667:                        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   12668:                        param->env_seg = 0;
                   12669:                        param->cmd_line.w.l = 44;
                   12670:                        param->cmd_line.w.h = (WORK_TOP >> 4);
                   12671:                        param->fcb1.w.l = 24;
                   12672:                        param->fcb1.w.h = (WORK_TOP >> 4);
                   12673:                        param->fcb2.w.l = 24;
                   12674:                        param->fcb2.w.h = (WORK_TOP >> 4);
                   12675:                        
                   12676:                        memset(mem + WORK_TOP + 24, 0x20, 20);
                   12677:                        
                   12678:                        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
1.1.1.3   root     12679:                        cmd_line->len = mem[SREG_BASE(DS) + REG16(BX) + 1];
                   12680:                        memcpy(cmd_line->cmd, mem + SREG_BASE(DS) + REG16(BX) + 2, cmd_line->len);
1.1       root     12681:                        cmd_line->cmd[cmd_line->len] = 0x0d;
                   12682:                        
1.1.1.28  root     12683:                        try {
                   12684:                                msdos_process_exec(command, param, 0);
                   12685:                        } catch(...) {
                   12686:                                fatalerror("failed to start '%s' by int 2Fh, AX=AE01h\n", command);
1.1       root     12687:                        }
                   12688:                }
                   12689:                break;
                   12690:        default:
1.1.1.22  root     12691:                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     12692:                REG16(AX) = 0x01;
1.1.1.3   root     12693:                m_CF = 1;
1.1       root     12694:                break;
                   12695:        }
                   12696: }
                   12697: 
1.1.1.34  root     12698: inline void msdos_int_2fh_b7h()
                   12699: {
                   12700:        switch(REG8(AL)) {
                   12701:        case 0x00:
                   12702:                // APPEND is not installed
                   12703: //             REG8(AL) = 0x00;
                   12704:                break;
                   12705:        case 0x07:
                   12706:                // COMMAND.COM calls this service without checking APPEND is installed
                   12707:                break;
                   12708:        default:
                   12709:                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));
                   12710:                REG16(AX) = 0x01;
                   12711:                m_CF = 1;
                   12712:                break;
                   12713:        }
                   12714: }
                   12715: 
1.1.1.24  root     12716: inline void msdos_int_33h_0000h()
                   12717: {
                   12718:        REG16(AX) = 0xffff; // hardware/driver installed
                   12719:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   12720: }
                   12721: 
                   12722: inline void msdos_int_33h_0001h()
                   12723: {
1.1.1.34  root     12724:        if(mouse.hidden > 0) {
                   12725:                mouse.hidden--;
                   12726:        }
                   12727:        if(mouse.hidden == 0) {
1.1.1.24  root     12728:                if(!(dwConsoleMode & ENABLE_MOUSE_INPUT)) {
                   12729:                        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode | ENABLE_MOUSE_INPUT);
                   12730:                }
                   12731:                pic[1].imr &= ~0x10;    // enable irq12
                   12732:        }
                   12733: }
                   12734: 
                   12735: inline void msdos_int_33h_0002h()
                   12736: {
1.1.1.34  root     12737:        mouse.hidden++;
                   12738: //     SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode & ~ENABLE_MOUSE_INPUT);
                   12739:        pic[1].imr |= 0x10;     // enable irq12
1.1.1.24  root     12740: }
                   12741: 
                   12742: inline void msdos_int_33h_0003h()
                   12743: {
1.1.1.34  root     12744: //     if(mouse.hidden > 0) {
                   12745:                update_console_input();
                   12746: //     }
1.1.1.24  root     12747:        REG16(BX) = mouse.get_buttons();
1.1.1.34  root     12748:        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   12749:        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
                   12750: }
                   12751: 
                   12752: inline void msdos_int_33h_0004h()
                   12753: {
                   12754:        mouse.position.x = REG16(CX);
                   12755:        mouse.position.x = REG16(DX);
1.1.1.24  root     12756: }
                   12757: 
                   12758: inline void msdos_int_33h_0005h()
                   12759: {
1.1.1.34  root     12760: //     if(mouse.hidden > 0) {
                   12761:                update_console_input();
                   12762: //     }
1.1.1.24  root     12763:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   12764:                int idx = REG16(BX);
1.1.1.34  root     12765:                REG16(BX) = min(mouse.buttons[idx].pressed_times, 0x7fff);
                   12766:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].pressed_position.x));
                   12767:                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     12768:                mouse.buttons[idx].pressed_times = 0;
                   12769:        } else {
                   12770:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   12771:        }
                   12772:        REG16(AX) = mouse.get_buttons();
                   12773: }
                   12774: 
                   12775: inline void msdos_int_33h_0006h()
                   12776: {
1.1.1.34  root     12777: //     if(mouse.hidden > 0) {
                   12778:                update_console_input();
                   12779: //     }
1.1.1.24  root     12780:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   12781:                int idx = REG16(BX);
1.1.1.34  root     12782:                REG16(BX) = min(mouse.buttons[idx].released_times, 0x7fff);
                   12783:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].released_position.x));
                   12784:                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     12785:                mouse.buttons[idx].released_times = 0;
                   12786:        } else {
                   12787:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   12788:        }
                   12789:        REG16(AX) = mouse.get_buttons();
                   12790: }
                   12791: 
                   12792: inline void msdos_int_33h_0007h()
                   12793: {
                   12794:        mouse.min_position.x = min(REG16(CX), REG16(DX));
                   12795:        mouse.max_position.x = max(REG16(CX), REG16(DX));
                   12796: }
                   12797: 
                   12798: inline void msdos_int_33h_0008h()
                   12799: {
                   12800:        mouse.min_position.y = min(REG16(CX), REG16(DX));
                   12801:        mouse.max_position.y = max(REG16(CX), REG16(DX));
                   12802: }
                   12803: 
                   12804: inline void msdos_int_33h_0009h()
                   12805: {
                   12806:        mouse.hot_spot[0] = REG16(BX);
                   12807:        mouse.hot_spot[1] = REG16(CX);
                   12808: }
                   12809: 
                   12810: inline void msdos_int_33h_000bh()
                   12811: {
1.1.1.34  root     12812: //     if(mouse.hidden > 0) {
                   12813:                update_console_input();
                   12814: //     }
1.1.1.24  root     12815:        int dx = (mouse.position.x - mouse.prev_position.x) * mouse.mickey.x / 8;
                   12816:        int dy = (mouse.position.y - mouse.prev_position.y) * mouse.mickey.y / 8;
                   12817:        mouse.prev_position.x = mouse.position.x;
                   12818:        mouse.prev_position.y = mouse.position.y;
                   12819:        REG16(CX) = dx;
                   12820:        REG16(DX) = dy;
                   12821: }
                   12822: 
                   12823: inline void msdos_int_33h_000ch()
                   12824: {
                   12825:        mouse.call_mask = REG16(CX);
                   12826:        mouse.call_addr.w.l = REG16(DX);
                   12827:        mouse.call_addr.w.h = SREG(ES);
                   12828: }
                   12829: 
                   12830: inline void msdos_int_33h_000fh()
                   12831: {
                   12832:        mouse.mickey.x = REG16(CX);
                   12833:        mouse.mickey.y = REG16(DX);
                   12834: }
                   12835: 
                   12836: inline void msdos_int_33h_0011h()
                   12837: {
                   12838:        REG16(AX) = 0xffff;
                   12839:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   12840: }
                   12841: 
                   12842: inline void msdos_int_33h_0014h()
                   12843: {
                   12844:        UINT16 old_mask = mouse.call_mask;
                   12845:        UINT16 old_ofs = mouse.call_addr.w.l;
                   12846:        UINT16 old_seg = mouse.call_addr.w.h;
                   12847:        
                   12848:        mouse.call_mask = REG16(CX);
                   12849:        mouse.call_addr.w.l = REG16(DX);
                   12850:        mouse.call_addr.w.h = SREG(ES);
                   12851:        
                   12852:        REG16(CX) = old_mask;
                   12853:        REG16(DX) = old_ofs;
                   12854:        SREG(ES) = old_seg;
                   12855:        i386_load_segment_descriptor(ES);
                   12856: }
                   12857: 
                   12858: inline void msdos_int_33h_0015h()
                   12859: {
                   12860:        REG16(BX) = sizeof(mouse);
                   12861: }
                   12862: 
                   12863: inline void msdos_int_33h_0016h()
                   12864: {
                   12865:        memcpy(mem + SREG_BASE(ES) + REG16(DX), &mouse, sizeof(mouse));
                   12866: }
                   12867: 
                   12868: inline void msdos_int_33h_0017h()
                   12869: {
                   12870:        memcpy(&mouse, mem + SREG_BASE(ES) + REG16(DX), sizeof(mouse));
                   12871: }
                   12872: 
                   12873: inline void msdos_int_33h_001ah()
                   12874: {
                   12875:        mouse.sensitivity[0] = REG16(BX);
                   12876:        mouse.sensitivity[1] = REG16(CX);
                   12877:        mouse.sensitivity[2] = REG16(DX);
                   12878: }
                   12879: 
                   12880: inline void msdos_int_33h_001bh()
                   12881: {
                   12882:        REG16(BX) = mouse.sensitivity[0];
                   12883:        REG16(CX) = mouse.sensitivity[1];
                   12884:        REG16(DX) = mouse.sensitivity[2];
                   12885: }
                   12886: 
                   12887: inline void msdos_int_33h_001dh()
                   12888: {
                   12889:        mouse.display_page = REG16(BX);
                   12890: }
                   12891: 
                   12892: inline void msdos_int_33h_001eh()
                   12893: {
                   12894:        REG16(BX) = mouse.display_page;
                   12895: }
                   12896: 
1.1.1.34  root     12897: inline void msdos_int_33h_001fh()
                   12898: {
                   12899:        // from DOSBox
                   12900:        REG16(BX) = 0x0000;
                   12901:        SREG(ES) = 0x0000;
                   12902:        i386_load_segment_descriptor(ES);
                   12903:        mouse.enabled = false;
                   12904:        mouse.old_hidden = mouse.hidden;
                   12905:        mouse.hidden = 1;
                   12906: }
                   12907: 
                   12908: inline void msdos_int_33h_0020h()
                   12909: {
                   12910:        // from DOSBox
                   12911:        mouse.enabled = true;
                   12912:        mouse.hidden = mouse.old_hidden;
                   12913: }
                   12914: 
1.1.1.24  root     12915: inline void msdos_int_33h_0021h()
                   12916: {
                   12917:        REG16(AX) = 0xffff;
                   12918:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   12919: }
                   12920: 
                   12921: inline void msdos_int_33h_0022h()
                   12922: {
                   12923:        mouse.language = REG16(BX);
                   12924: }
                   12925: 
                   12926: inline void msdos_int_33h_0023h()
                   12927: {
                   12928:        REG16(BX) = mouse.language;
                   12929: }
                   12930: 
                   12931: inline void msdos_int_33h_0024h()
                   12932: {
                   12933:        REG16(BX) = 0x0805; // V8.05
                   12934:        REG16(CX) = 0x0400; // PS/2
                   12935: }
                   12936: 
                   12937: inline void msdos_int_33h_0026h()
                   12938: {
                   12939:        REG16(BX) = 0x0000;
                   12940:        REG16(CX) = mouse.max_position.x;
                   12941:        REG16(DX) = mouse.max_position.y;
                   12942: }
                   12943: 
                   12944: inline void msdos_int_33h_002ah()
                   12945: {
1.1.1.34  root     12946:        REG16(AX) = -mouse.hidden;
1.1.1.24  root     12947:        REG16(BX) = mouse.hot_spot[0];
                   12948:        REG16(CX) = mouse.hot_spot[1];
                   12949:        REG16(DX) = 4; // PS/2
                   12950: }
                   12951: 
                   12952: inline void msdos_int_33h_0031h()
                   12953: {
                   12954:        REG16(AX) = mouse.min_position.x;
                   12955:        REG16(BX) = mouse.min_position.y;
                   12956:        REG16(CX) = mouse.max_position.x;
                   12957:        REG16(DX) = mouse.max_position.y;
                   12958: }
                   12959: 
                   12960: inline void msdos_int_33h_0032h()
                   12961: {
                   12962:        REG16(AX) = 0;
                   12963: //     REG16(AX) |= 0x8000; // 0025h
                   12964:        REG16(AX) |= 0x4000; // 0026h
                   12965: //     REG16(AX) |= 0x2000; // 0027h
                   12966: //     REG16(AX) |= 0x1000; // 0028h
                   12967: //     REG16(AX) |= 0x0800; // 0029h
                   12968:        REG16(AX) |= 0x0400; // 002ah
                   12969: //     REG16(AX) |= 0x0200; // 002bh
                   12970: //     REG16(AX) |= 0x0100; // 002ch
                   12971: //     REG16(AX) |= 0x0080; // 002dh
                   12972: //     REG16(AX) |= 0x0040; // 002eh
                   12973:        REG16(AX) |= 0x0020; // 002fh
                   12974: //     REG16(AX) |= 0x0010; // 0030h
                   12975:        REG16(AX) |= 0x0008; // 0031h
                   12976:        REG16(AX) |= 0x0004; // 0032h
                   12977: //     REG16(AX) |= 0x0002; // 0033h
                   12978: //     REG16(AX) |= 0x0001; // 0034h
                   12979: }
                   12980: 
1.1.1.19  root     12981: inline void msdos_int_67h_40h()
                   12982: {
                   12983:        if(!support_ems) {
                   12984:                REG8(AH) = 0x84;
                   12985:        } else {
                   12986:                REG8(AH) = 0x00;
                   12987:        }
                   12988: }
                   12989: 
                   12990: inline void msdos_int_67h_41h()
                   12991: {
                   12992:        if(!support_ems) {
                   12993:                REG8(AH) = 0x84;
                   12994:        } else {
                   12995:                REG8(AH) = 0x00;
                   12996:                REG16(BX) = EMS_TOP >> 4;
                   12997:        }
                   12998: }
                   12999: 
                   13000: inline void msdos_int_67h_42h()
                   13001: {
                   13002:        if(!support_ems) {
                   13003:                REG8(AH) = 0x84;
                   13004:        } else {
                   13005:                REG8(AH) = 0x00;
                   13006:                REG16(BX) = free_ems_pages;
                   13007:                REG16(DX) = MAX_EMS_PAGES;
                   13008:        }
                   13009: }
                   13010: 
                   13011: inline void msdos_int_67h_43h()
                   13012: {
                   13013:        if(!support_ems) {
                   13014:                REG8(AH) = 0x84;
                   13015:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   13016:                REG8(AH) = 0x87;
                   13017:        } else if(REG16(BX) > free_ems_pages) {
                   13018:                REG8(AH) = 0x88;
                   13019:        } else if(REG16(BX) == 0) {
                   13020:                REG8(AH) = 0x89;
                   13021:        } else {
1.1.1.31  root     13022:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     13023:                        if(!ems_handles[i].allocated) {
                   13024:                                ems_allocate_pages(i, REG16(BX));
                   13025:                                REG8(AH) = 0x00;
                   13026:                                REG16(DX) = i;
                   13027:                                return;
                   13028:                        }
                   13029:                }
                   13030:                REG8(AH) = 0x85;
                   13031:        }
                   13032: }
                   13033: 
                   13034: inline void msdos_int_67h_44h()
                   13035: {
                   13036:        if(!support_ems) {
                   13037:                REG8(AH) = 0x84;
1.1.1.31  root     13038:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     13039:                REG8(AH) = 0x83;
                   13040:        } else if(!(REG16(BX) == 0xffff || REG16(BX) < ems_handles[REG16(DX)].pages)) {
                   13041:                REG8(AH) = 0x8a;
                   13042: //     } else if(!(REG8(AL) < 4)) {
                   13043: //             REG8(AH) = 0x8b;
                   13044:        } else if(REG16(BX) == 0xffff) {
                   13045:                ems_unmap_page(REG8(AL) & 3);
                   13046:                REG8(AH) = 0x00;
                   13047:        } else {
                   13048:                ems_map_page(REG8(AL) & 3, REG16(DX), REG16(BX));
                   13049:                REG8(AH) = 0x00;
                   13050:        }
                   13051: }
                   13052: 
                   13053: inline void msdos_int_67h_45h()
                   13054: {
                   13055:        if(!support_ems) {
                   13056:                REG8(AH) = 0x84;
1.1.1.31  root     13057:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     13058:                REG8(AH) = 0x83;
                   13059:        } else {
                   13060:                ems_release_pages(REG16(DX));
                   13061:                REG8(AH) = 0x00;
                   13062:        }
                   13063: }
                   13064: 
                   13065: inline void msdos_int_67h_46h()
                   13066: {
                   13067:        if(!support_ems) {
                   13068:                REG8(AH) = 0x84;
                   13069:        } else {
1.1.1.29  root     13070: //             REG16(AX) = 0x0032; // EMS 3.2
                   13071:                REG16(AX) = 0x0040; // EMS 4.0
1.1.1.19  root     13072:        }
                   13073: }
                   13074: 
                   13075: inline void msdos_int_67h_47h()
                   13076: {
                   13077:        // NOTE: the map data should be stored in the specified ems page, not process data
                   13078:        process_t *process = msdos_process_info_get(current_psp);
                   13079:        
                   13080:        if(!support_ems) {
                   13081:                REG8(AH) = 0x84;
1.1.1.31  root     13082: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     13083: //             REG8(AH) = 0x83;
                   13084:        } else if(process->ems_pages_stored) {
                   13085:                REG8(AH) = 0x8d;
                   13086:        } else {
                   13087:                for(int i = 0; i < 4; i++) {
                   13088:                        process->ems_pages[i].handle = ems_pages[i].handle;
                   13089:                        process->ems_pages[i].page   = ems_pages[i].page;
                   13090:                        process->ems_pages[i].mapped = ems_pages[i].mapped;
                   13091:                }
                   13092:                process->ems_pages_stored = true;
                   13093:                REG8(AH) = 0x00;
                   13094:        }
                   13095: }
                   13096: 
                   13097: inline void msdos_int_67h_48h()
                   13098: {
                   13099:        // NOTE: the map data should be restored from the specified ems page, not process data
                   13100:        process_t *process = msdos_process_info_get(current_psp);
                   13101:        
                   13102:        if(!support_ems) {
                   13103:                REG8(AH) = 0x84;
1.1.1.31  root     13104: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     13105: //             REG8(AH) = 0x83;
                   13106:        } else if(!process->ems_pages_stored) {
                   13107:                REG8(AH) = 0x8e;
                   13108:        } else {
                   13109:                for(int i = 0; i < 4; i++) {
                   13110:                        if(process->ems_pages[i].mapped) {
                   13111:                                ems_map_page(i, process->ems_pages[i].handle, process->ems_pages[i].page);
                   13112:                        } else {
                   13113:                                ems_unmap_page(i);
                   13114:                        }
                   13115:                }
                   13116:                process->ems_pages_stored = false;
                   13117:                REG8(AH) = 0x00;
                   13118:        }
                   13119: }
                   13120: 
                   13121: inline void msdos_int_67h_4bh()
                   13122: {
                   13123:        if(!support_ems) {
                   13124:                REG8(AH) = 0x84;
                   13125:        } else {
                   13126:                REG8(AH) = 0x00;
                   13127:                REG16(BX) = 0;
1.1.1.31  root     13128:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     13129:                        if(ems_handles[i].allocated) {
                   13130:                                REG16(BX)++;
                   13131:                        }
                   13132:                }
                   13133:        }
                   13134: }
                   13135: 
                   13136: inline void msdos_int_67h_4ch()
                   13137: {
                   13138:        if(!support_ems) {
                   13139:                REG8(AH) = 0x84;
1.1.1.31  root     13140:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     13141:                REG8(AH) = 0x83;
                   13142:        } else {
                   13143:                REG8(AH) = 0x00;
                   13144:                REG16(BX) = ems_handles[REG16(DX)].pages;
                   13145:        }
                   13146: }
                   13147: 
                   13148: inline void msdos_int_67h_4dh()
                   13149: {
                   13150:        if(!support_ems) {
                   13151:                REG8(AH) = 0x84;
                   13152:        } else {
                   13153:                REG8(AH) = 0x00;
                   13154:                REG16(BX) = 0;
1.1.1.31  root     13155:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     13156:                        if(ems_handles[i].allocated) {
                   13157:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 0) = i;
                   13158:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 2) = ems_handles[i].pages;
                   13159:                                REG16(BX)++;
                   13160:                        }
                   13161:                }
                   13162:        }
                   13163: }
                   13164: 
1.1.1.20  root     13165: inline void msdos_int_67h_4eh()
                   13166: {
                   13167:        if(!support_ems) {
                   13168:                REG8(AH) = 0x84;
                   13169:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   13170:                if(REG8(AL) == 0x00 || REG8(AL) == 0x02) {
                   13171:                        // save page map
                   13172:                        for(int i = 0; i < 4; i++) {
                   13173:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   13174:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   13175:                        }
                   13176:                }
                   13177:                if(REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   13178:                        // restore page map
                   13179:                        for(int i = 0; i < 4; i++) {
                   13180:                                UINT16 handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   13181:                                UINT16 page   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   13182:                                
1.1.1.31  root     13183:                                if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
1.1.1.20  root     13184:                                        ems_map_page(i, handle, page);
                   13185:                                } else {
                   13186:                                        ems_unmap_page(i);
                   13187:                                }
                   13188:                        }
                   13189:                }
                   13190:                REG8(AH) = 0x00;
                   13191:        } else if(REG8(AL) == 0x03) {
                   13192:                REG8(AH) = 0x00;
1.1.1.21  root     13193:                REG8(AL) = 4 * 4;
                   13194:        } else {
1.1.1.22  root     13195:                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     13196:                REG8(AH) = 0x8f;
                   13197:        }
                   13198: }
                   13199: 
                   13200: inline void msdos_int_67h_4fh()
                   13201: {
                   13202:        if(!support_ems) {
                   13203:                REG8(AH) = 0x84;
                   13204:        } else if(REG8(AL) == 0x00) {
                   13205:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   13206:                
                   13207:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI)) = count;
                   13208:                for(int i = 0; i < count; i++) {
                   13209:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 2 * i);
                   13210:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   13211:                        
                   13212: //                     if(!(physical < 4)) {
                   13213: //                             REG8(AH) = 0x8b;
                   13214: //                             return;
                   13215: //                     }
                   13216:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 0) = segment;
                   13217:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 2) = ems_pages[physical & 3].handle;
                   13218:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 4) = ems_pages[physical & 3].mapped ? ems_pages[physical & 3].page : 0xffff;
                   13219:                }
                   13220:                REG8(AH) = 0x00;
                   13221:        } else if(REG8(AL) == 0x01) {
                   13222:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   13223:                
                   13224:                for(int i = 0; i < count; i++) {
                   13225:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 0);
                   13226:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   13227:                        UINT16 handle   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 2);
                   13228:                        UINT16 logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 4);
                   13229:                        
                   13230: //                     if(!(physical < 4)) {
                   13231: //                             REG8(AH) = 0x8b;
                   13232: //                             return;
                   13233: //                     } else
1.1.1.31  root     13234:                        if(!(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated)) {
1.1.1.21  root     13235:                                REG8(AH) = 0x83;
                   13236:                                return;
                   13237:                        } else if(logical == 0xffff) {
                   13238:                                ems_unmap_page(physical & 3);
                   13239:                        } else if(logical < ems_handles[handle].pages) {
                   13240:                                ems_map_page(physical & 3, handle, logical);
                   13241:                        } else {
                   13242:                                REG8(AH) = 0x8a;
                   13243:                                return;
                   13244:                        }
                   13245:                }
                   13246:                REG8(AH) = 0x00;
                   13247:        } else if(REG8(AL) == 0x02) {
                   13248:                REG8(AH) = 0x00;
                   13249:                REG8(AL) = 2 + REG16(BX) * 6;
1.1.1.20  root     13250:        } else {
1.1.1.22  root     13251:                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     13252:                REG8(AH) = 0x8f;
                   13253:        }
                   13254: }
                   13255: 
                   13256: inline void msdos_int_67h_50h()
                   13257: {
                   13258:        if(!support_ems) {
                   13259:                REG8(AH) = 0x84;
1.1.1.31  root     13260:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.20  root     13261:                REG8(AH) = 0x83;
                   13262:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   13263:                for(int i = 0; i < REG16(CX); i++) {
                   13264:                        int logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   13265:                        int physical = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   13266:                        
                   13267:                        if(REG8(AL) == 0x01) {
                   13268:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   13269:                        }
                   13270: //                     if(!(physical < 4)) {
                   13271: //                             REG8(AH) = 0x8b;
                   13272: //                             return;
                   13273: //                     } else
                   13274:                        if(logical == 0xffff) {
                   13275:                                ems_unmap_page(physical & 3);
                   13276:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   13277:                                ems_map_page(physical & 3, REG16(DX), logical);
                   13278:                        } else {
                   13279:                                REG8(AH) = 0x8a;
                   13280:                                return;
                   13281:                        }
                   13282:                }
                   13283:                REG8(AH) = 0x00;
                   13284:        } else {
1.1.1.22  root     13285:                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     13286:                REG8(AH) = 0x8f;
                   13287:        }
                   13288: }
                   13289: 
1.1.1.19  root     13290: inline void msdos_int_67h_51h()
                   13291: {
                   13292:        if(!support_ems) {
                   13293:                REG8(AH) = 0x84;
1.1.1.31  root     13294:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     13295:                REG8(AH) = 0x83;
                   13296:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   13297:                REG8(AH) = 0x87;
                   13298:        } else if(REG16(BX) > free_ems_pages + ems_handles[REG16(DX)].pages) {
                   13299:                REG8(AH) = 0x88;
                   13300:        } else {
                   13301:                ems_reallocate_pages(REG16(DX), REG16(BX));
                   13302:                REG8(AH) = 0x00;
                   13303:        }
                   13304: }
                   13305: 
1.1.1.20  root     13306: inline void msdos_int_67h_52h()
                   13307: {
                   13308:        if(!support_ems) {
                   13309:                REG8(AH) = 0x84;
1.1.1.31  root     13310: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   13311: //             REG8(AH) = 0x83;
1.1.1.20  root     13312:        } else if(REG8(AL) == 0x00) {
                   13313:                REG8(AL) = 0x00; // handle is volatile
                   13314:                REG8(AH) = 0x00;
                   13315:        } else if(REG8(AL) == 0x01) {
                   13316:                if(REG8(BL) == 0x00) {
                   13317:                        REG8(AH) = 0x00;
                   13318:                } else {
                   13319:                        REG8(AH) = 0x90; // undefined attribute type
                   13320:                }
                   13321:        } else if(REG8(AL) == 0x02) {
                   13322:                REG8(AL) = 0x00; // only volatile handles supported
                   13323:                REG8(AH) = 0x00;
                   13324:        } else {
1.1.1.22  root     13325:                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     13326:                REG8(AH) = 0x8f;
                   13327:        }
                   13328: }
                   13329: 
1.1.1.19  root     13330: inline void msdos_int_67h_53h()
                   13331: {
                   13332:        if(!support_ems) {
                   13333:                REG8(AH) = 0x84;
1.1.1.31  root     13334:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     13335:                REG8(AH) = 0x83;
                   13336:        } else if(REG8(AL) == 0x00) {
                   13337:                memcpy(mem + SREG_BASE(ES) + REG16(DI), ems_handles[REG16(DX)].name, 8);
                   13338:                REG8(AH) = 0x00;
                   13339:        } else if(REG8(AL) == 0x01) {
1.1.1.31  root     13340:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     13341:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   13342:                                REG8(AH) = 0xa1;
                   13343:                                return;
                   13344:                        }
                   13345:                }
                   13346:                REG8(AH) = 0x00;
                   13347:                memcpy(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8);
                   13348:        } else {
1.1.1.22  root     13349:                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     13350:                REG8(AH) = 0x8f;
1.1.1.19  root     13351:        }
                   13352: }
                   13353: 
                   13354: inline void msdos_int_67h_54h()
                   13355: {
                   13356:        if(!support_ems) {
                   13357:                REG8(AH) = 0x84;
                   13358:        } else if(REG8(AL) == 0x00) {
1.1.1.31  root     13359:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     13360:                        if(ems_handles[i].allocated) {
                   13361:                                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, ems_handles[i].name, 10);
                   13362:                        } else {
                   13363:                                memset(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, 0, 10);
                   13364:                        }
                   13365:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 0) = i;
                   13366:                }
                   13367:                REG8(AH) = 0x00;
                   13368:                REG8(AL) = MAX_EMS_HANDLES;
                   13369:        } else if(REG8(AL) == 0x01) {
                   13370:                REG8(AH) = 0xa0; // not found
1.1.1.31  root     13371:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     13372:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   13373:                                REG8(AH) = 0x00;
                   13374:                                REG16(DX) = i;
                   13375:                                break;
                   13376:                        }
                   13377:                }
                   13378:        } else if(REG8(AL) == 0x02) {
                   13379:                REG8(AH) = 0x00;
                   13380:                REG16(BX) = MAX_EMS_HANDLES;
                   13381:        } else {
1.1.1.22  root     13382:                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     13383:                REG8(AH) = 0x8f;
                   13384:        }
                   13385: }
                   13386: 
                   13387: inline void msdos_int_67h_57h_tmp()
                   13388: {
                   13389:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   13390:        UINT8  src_type    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   13391:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x05);
                   13392:        UINT16 src_ofs     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07);
                   13393:        UINT16 src_seg     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x09);
                   13394:        UINT8  dest_type   = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0b);
                   13395:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   13396:        UINT16 dest_ofs    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0e);
                   13397:        UINT16 dest_seg    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x10);
                   13398:        
1.1.1.32  root     13399:        UINT8 *src_buffer = NULL, *dest_buffer = NULL;
1.1.1.20  root     13400:        UINT32 src_addr, dest_addr;
                   13401:        UINT32 src_addr_max, dest_addr_max;
                   13402:        
                   13403:        if(src_type == 0) {
                   13404:                src_buffer = mem;
                   13405:                src_addr = (src_seg << 4) + src_ofs;
                   13406:                src_addr_max = MAX_MEM;
                   13407:        } else {
1.1.1.31  root     13408:                if(!(src_handle >= 1 && src_handle <= MAX_EMS_HANDLES && ems_handles[src_handle].allocated)) {
1.1.1.20  root     13409:                        REG8(AH) = 0x83;
                   13410:                        return;
                   13411:                } else if(!(src_seg < ems_handles[src_handle].pages)) {
                   13412:                        REG8(AH) = 0x8a;
                   13413:                        return;
                   13414:                }
1.1.1.32  root     13415:                if(ems_handles[src_handle].buffer != NULL) {
                   13416:                        src_buffer = ems_handles[src_handle].buffer + 0x4000 * src_seg;
                   13417:                }
1.1.1.20  root     13418:                src_addr = src_ofs;
1.1.1.32  root     13419:                src_addr_max = 0x4000 * (ems_handles[src_handle].pages - src_seg);
1.1.1.20  root     13420:        }
                   13421:        if(dest_type == 0) {
                   13422:                dest_buffer = mem;
                   13423:                dest_addr = (dest_seg << 4) + dest_ofs;
                   13424:                dest_addr_max = MAX_MEM;
                   13425:        } else {
1.1.1.31  root     13426:                if(!(dest_handle >= 1 && dest_handle <= MAX_EMS_HANDLES && ems_handles[dest_handle].allocated)) {
1.1.1.20  root     13427:                        REG8(AH) = 0x83;
                   13428:                        return;
                   13429:                } else if(!(dest_seg < ems_handles[dest_handle].pages)) {
                   13430:                        REG8(AH) = 0x8a;
                   13431:                        return;
                   13432:                }
1.1.1.32  root     13433:                if(ems_handles[dest_handle].buffer != NULL) {
                   13434:                        dest_buffer = ems_handles[dest_handle].buffer + 0x4000 * dest_seg;
                   13435:                }
1.1.1.20  root     13436:                dest_addr = dest_ofs;
1.1.1.32  root     13437:                dest_addr_max = 0x4000 * (ems_handles[dest_handle].pages - dest_seg);
1.1.1.20  root     13438:        }
1.1.1.32  root     13439:        if(src_buffer != NULL && dest_buffer != NULL) {
                   13440:                for(int i = 0; i < copy_length; i++) {
                   13441:                        if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   13442:                                if(REG8(AL) == 0x00) {
                   13443:                                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   13444:                                } else if(REG8(AL) == 0x01) {
                   13445:                                        UINT8 tmp = dest_buffer[dest_addr];
                   13446:                                        dest_buffer[dest_addr++] = src_buffer[src_addr];
                   13447:                                        src_buffer[src_addr++] = tmp;
                   13448:                                }
                   13449:                        } else {
                   13450:                                REG8(AH) = 0x93;
                   13451:                                return;
1.1.1.20  root     13452:                        }
                   13453:                }
1.1.1.32  root     13454:                REG8(AH) = 0x00;
                   13455:        } else {
                   13456:                REG8(AH) = 0x80;
1.1.1.20  root     13457:        }
                   13458: }
                   13459: 
                   13460: inline void msdos_int_67h_57h()
                   13461: {
                   13462:        if(!support_ems) {
                   13463:                REG8(AH) = 0x84;
                   13464:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   13465:                struct {
                   13466:                        UINT16 handle;
                   13467:                        UINT16 page;
                   13468:                        bool mapped;
                   13469:                } tmp_pages[4];
                   13470:                
                   13471:                // unmap pages to copy memory data to ems buffer
                   13472:                for(int i = 0; i < 4; i++) {
                   13473:                        tmp_pages[i].handle = ems_pages[i].handle;
                   13474:                        tmp_pages[i].page   = ems_pages[i].page;
                   13475:                        tmp_pages[i].mapped = ems_pages[i].mapped;
                   13476:                        ems_unmap_page(i);
                   13477:                }
                   13478:                
                   13479:                // run move/exchange operation
                   13480:                msdos_int_67h_57h_tmp();
                   13481:                
                   13482:                // restore unmapped pages
                   13483:                for(int i = 0; i < 4; i++) {
                   13484:                        if(tmp_pages[i].mapped) {
                   13485:                                ems_map_page(i, tmp_pages[i].handle, tmp_pages[i].page);
                   13486:                        }
                   13487:                }
                   13488:        } else {
1.1.1.22  root     13489:                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     13490:                REG8(AH) = 0x8f;
                   13491:        }
                   13492: }
                   13493: 
                   13494: inline void msdos_int_67h_58h()
                   13495: {
                   13496:        if(!support_ems) {
                   13497:                REG8(AH) = 0x84;
                   13498:        } else if(REG8(AL) == 0x00) {
                   13499:                for(int i = 0; i < 4; i++) {
1.1.1.30  root     13500:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = (EMS_TOP + 0x4000 * i) >> 4;
                   13501:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = i;
1.1.1.20  root     13502:                }
                   13503:                REG8(AH) = 0x00;
                   13504:                REG16(CX) = 4;
                   13505:        } else if(REG8(AL) == 0x01) {
                   13506:                REG8(AH) = 0x00;
                   13507:                REG16(CX) = 4;
                   13508:        } else {
1.1.1.22  root     13509:                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     13510:                REG8(AH) = 0x8f;
                   13511:        }
                   13512: }
                   13513: 
                   13514: inline void msdos_int_67h_5ah()
                   13515: {
                   13516:        if(!support_ems) {
1.1.1.19  root     13517:                REG8(AH) = 0x84;
1.1.1.20  root     13518:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   13519:                REG8(AH) = 0x87;
                   13520:        } else if(REG16(BX) > free_ems_pages) {
                   13521:                REG8(AH) = 0x88;
                   13522: //     } else if(REG16(BX) == 0) {
                   13523: //             REG8(AH) = 0x89;
                   13524:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
1.1.1.31  root     13525:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.20  root     13526:                        if(!ems_handles[i].allocated) {
                   13527:                                ems_allocate_pages(i, REG16(BX));
                   13528:                                REG8(AH) = 0x00;
                   13529:                                REG16(DX) = i;
                   13530:                                return;
                   13531:                        }
                   13532:                }
                   13533:                REG8(AH) = 0x85;
                   13534:        } else {
1.1.1.22  root     13535:                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     13536:                REG8(AH) = 0x8f;
1.1.1.19  root     13537:        }
                   13538: }
                   13539: 
1.1.1.30  root     13540: inline void msdos_int_67h_deh()
                   13541: {
                   13542:        REG8(AH) = 0x84;
                   13543: }
                   13544: 
1.1.1.19  root     13545: #ifdef SUPPORT_XMS
                   13546: 
1.1.1.32  root     13547: void msdos_xms_init()
1.1.1.26  root     13548: {
1.1.1.30  root     13549:        emb_handle_top = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   13550:        emb_handle_top->address = EMB_TOP;
                   13551:        emb_handle_top->size_kb = (EMB_END - EMB_TOP) >> 10;
1.1.1.26  root     13552:        xms_a20_local_enb_count = 0;
                   13553: }
                   13554: 
1.1.1.32  root     13555: void msdos_xms_finish()
                   13556: {
                   13557:        msdos_xms_release();
                   13558: }
                   13559: 
                   13560: void msdos_xms_release()
1.1.1.30  root     13561: {
                   13562:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL;) {
                   13563:                emb_handle_t *next_handle = emb_handle->next;
                   13564:                free(emb_handle);
                   13565:                emb_handle = next_handle;
                   13566:        }
                   13567: }
                   13568: 
                   13569: emb_handle_t *msdos_xms_get_emb_handle(int handle)
                   13570: {
                   13571:        if(handle != 0) {
                   13572:                for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   13573:                        if(emb_handle->handle == handle) {
                   13574:                                return(emb_handle);
                   13575:                        }
                   13576:                }
                   13577:        }
                   13578:        return(NULL);
                   13579: }
                   13580: 
                   13581: int msdos_xms_get_unused_emb_handle_id()
                   13582: {
                   13583:        for(int handle = 1;; handle++) {
                   13584:                if(msdos_xms_get_emb_handle(handle) == NULL) {
                   13585:                        return(handle);
                   13586:                }
                   13587:        }
                   13588:        return(0);
                   13589: }
                   13590: 
                   13591: int msdos_xms_get_unused_emb_handle_count()
                   13592: {
                   13593:        int count = 64; //255;
                   13594:        
                   13595:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   13596:                if(emb_handle->handle != 0) {
                   13597:                        if(--count == 1) {
                   13598:                                break;
                   13599:                        }
                   13600:                }
                   13601:        }
                   13602:        return(count);
                   13603: }
                   13604: 
                   13605: void msdos_xms_split_emb_handle(emb_handle_t *emb_handle, int size_kb)
                   13606: {
                   13607:        if(emb_handle->size_kb > size_kb) {
                   13608:                emb_handle_t *new_handle = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   13609:                
                   13610:                new_handle->address = emb_handle->address + size_kb * 1024;
                   13611:                new_handle->size_kb = emb_handle->size_kb - size_kb;
                   13612:                emb_handle->size_kb = size_kb;
                   13613:                
                   13614:                new_handle->prev = emb_handle;
                   13615:                new_handle->next = emb_handle->next;
                   13616:                if(emb_handle->next != NULL) {
                   13617:                        emb_handle->next->prev = new_handle;
                   13618:                }
                   13619:                emb_handle->next = new_handle;
                   13620:        }
                   13621: }
                   13622: 
                   13623: void msdos_xms_combine_emb_handles(emb_handle_t *emb_handle)
                   13624: {
                   13625:        emb_handle_t *next_handle = emb_handle->next;
                   13626:        
                   13627:        if(next_handle != NULL) {
                   13628:                emb_handle->size_kb += next_handle->size_kb;
                   13629:                
                   13630:                if(next_handle->next != NULL) {
                   13631:                        next_handle->next->prev = emb_handle;
                   13632:                }
                   13633:                emb_handle->next = next_handle->next;
                   13634:                free(next_handle);
                   13635:        }
                   13636: }
                   13637: 
                   13638: emb_handle_t *msdos_xms_alloc_emb_handle(int size_kb)
                   13639: {
                   13640:        emb_handle_t *target_handle = NULL;
                   13641:        
                   13642:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   13643:                if(emb_handle->handle == 0 && emb_handle->size_kb >= size_kb) {
                   13644:                        if(target_handle == NULL || target_handle->size_kb > emb_handle->size_kb) {
                   13645:                                target_handle = emb_handle;
                   13646:                        }
                   13647:                }
                   13648:        }
                   13649:        if(target_handle != NULL) {
                   13650:                if(target_handle->size_kb > size_kb) {
                   13651:                        msdos_xms_split_emb_handle(target_handle, size_kb);
                   13652:                }
                   13653: //             target_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   13654:                return(target_handle);
                   13655:        }
                   13656:        return(NULL);
                   13657: }
                   13658: 
                   13659: void msdos_xms_free_emb_handle(emb_handle_t *emb_handle)
                   13660: {
                   13661:        emb_handle_t *prev_handle = emb_handle->prev;
                   13662:        emb_handle_t *next_handle = emb_handle->next;
                   13663:        
                   13664:        if(prev_handle != NULL && prev_handle->handle == 0) {
                   13665:                msdos_xms_combine_emb_handles(prev_handle);
                   13666:                emb_handle = prev_handle;
                   13667:        }
                   13668:        if(next_handle != NULL && next_handle->handle == 0) {
                   13669:                msdos_xms_combine_emb_handles(emb_handle);
                   13670:        }
                   13671:        emb_handle->handle = 0;
                   13672: }
                   13673: 
1.1.1.19  root     13674: inline void msdos_call_xms_00h()
                   13675: {
1.1.1.29  root     13676: #if defined(HAS_I386)
                   13677:        REG16(AX) = 0x0300; // V3.00 (XMS Version)
                   13678:        REG16(BX) = 0x035f; // V3.95 (Driver Revision)
                   13679: #else
                   13680:        REG16(AX) = 0x0200; // V2.00 (XMS Version)
                   13681:        REG16(BX) = 0x0270; // V2.70 (Driver Revision)
                   13682: #endif
                   13683: //     REG16(DX) = 0x0000; // HMA does not exist
                   13684:        REG16(DX) = 0x0001; // HMA does exist
1.1.1.19  root     13685: }
                   13686: 
                   13687: inline void msdos_call_xms_01h()
                   13688: {
1.1.1.29  root     13689:        if(REG8(AL) == 0x40) {
                   13690:                // HIMEM.SYS will fail function 01h with error code 91h if AL=40h and
                   13691:                // DX=KB free extended memory returned by last call of function 08h
                   13692:                REG16(AX) = 0x0000;
                   13693:                REG8(BL) = 0x91;
                   13694:                REG16(DX) = xms_dx_after_call_08h;
                   13695:        } else if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   13696:                REG16(AX) = 0x0000;
                   13697:                REG8(BL) = 0x81; // Vdisk was detected
                   13698: #ifdef SUPPORT_HMA
                   13699:        } else if(is_hma_used_by_int_2fh) {
                   13700:                REG16(AX) = 0x0000;
                   13701:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   13702:        } else if(is_hma_used_by_xms) {
                   13703:                REG16(AX) = 0x0000;
                   13704:                REG8(BL) = 0x91; // HMA is already in use
                   13705:        } else {
                   13706:                REG16(AX) = 0x0001;
                   13707:                is_hma_used_by_xms = true;
                   13708: #else
                   13709:        } else {
                   13710:                REG16(AX) = 0x0000;
                   13711:                REG8(BL) = 0x91; // HMA is already in use
                   13712: #endif
                   13713:        }
1.1.1.19  root     13714: }
                   13715: 
                   13716: inline void msdos_call_xms_02h()
                   13717: {
1.1.1.29  root     13718:        if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   13719:                REG16(AX) = 0x0000;
                   13720:                REG8(BL) = 0x81; // Vdisk was detected
                   13721: #ifdef SUPPORT_HMA
                   13722:        } else if(is_hma_used_by_int_2fh) {
                   13723:                REG16(AX) = 0x0000;
                   13724:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   13725:        } else if(!is_hma_used_by_xms) {
                   13726:                REG16(AX) = 0x0000;
                   13727:                REG8(BL) = 0x93; // HMA is not allocated
                   13728:        } else {
                   13729:                REG16(AX) = 0x0001;
                   13730:                is_hma_used_by_xms = false;
                   13731:                // restore first free mcb in high memory area
                   13732:                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   13733: #else
                   13734:        } else {
                   13735:                REG16(AX) = 0x0000;
                   13736:                REG8(BL) = 0x91; // HMA is already in use
                   13737: #endif
                   13738:        }
1.1.1.19  root     13739: }
                   13740: 
                   13741: inline void msdos_call_xms_03h()
                   13742: {
                   13743:        i386_set_a20_line(1);
                   13744:        REG16(AX) = 0x0001;
                   13745:        REG8(BL) = 0x00;
                   13746: }
                   13747: 
                   13748: inline void msdos_call_xms_04h()
                   13749: {
1.1.1.21  root     13750:        i386_set_a20_line(0);
                   13751:        REG16(AX) = 0x0001;
                   13752:        REG8(BL) = 0x00;
1.1.1.19  root     13753: }
                   13754: 
                   13755: inline void msdos_call_xms_05h()
                   13756: {
                   13757:        i386_set_a20_line(1);
                   13758:        REG16(AX) = 0x0001;
                   13759:        REG8(BL) = 0x00;
1.1.1.21  root     13760:        xms_a20_local_enb_count++;
1.1.1.19  root     13761: }
                   13762: 
                   13763: void msdos_call_xms_06h()
                   13764: {
1.1.1.21  root     13765:        if(xms_a20_local_enb_count > 0) {
                   13766:                xms_a20_local_enb_count--;
                   13767:        }
                   13768:        if(xms_a20_local_enb_count == 0) {
1.1.1.19  root     13769:                i386_set_a20_line(0);
1.1.1.21  root     13770:        }
                   13771:        if((m_a20_mask >> 20) & 1) {
1.1.1.19  root     13772:                REG16(AX) = 0x0000;
                   13773:                REG8(BL) = 0x94;
1.1.1.21  root     13774:        } else {
                   13775:                REG16(AX) = 0x0001;
                   13776:                REG8(BL) = 0x00;
1.1.1.19  root     13777:        }
                   13778: }
                   13779: 
                   13780: inline void msdos_call_xms_07h()
                   13781: {
                   13782:        REG16(AX) = (m_a20_mask >> 20) & 1;
                   13783:        REG8(BL) = 0x00;
                   13784: }
                   13785: 
                   13786: inline void msdos_call_xms_08h()
                   13787: {
                   13788:        REG16(AX) = REG16(DX) = 0x0000;
                   13789:        
1.1.1.30  root     13790:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   13791:                if(emb_handle->handle == 0) {
                   13792:                        if(REG16(AX) < emb_handle->size_kb) {
                   13793:                                REG16(AX) = emb_handle->size_kb;
1.1.1.19  root     13794:                        }
1.1.1.30  root     13795:                        REG16(DX) += emb_handle->size_kb;
1.1.1.19  root     13796:                }
                   13797:        }
                   13798:        
                   13799:        if(REG16(AX) == 0 && REG16(DX) == 0) {
                   13800:                REG8(BL) = 0xa0;
                   13801:        } else {
                   13802:                REG8(BL) = 0x00;
                   13803:        }
1.1.1.29  root     13804:        xms_dx_after_call_08h = REG16(DX);
1.1.1.19  root     13805: }
                   13806: 
1.1.1.30  root     13807: void msdos_call_xms_09h(int size_kb)
1.1.1.19  root     13808: {
1.1.1.30  root     13809:        emb_handle_t *emb_handle = msdos_xms_alloc_emb_handle(size_kb);
                   13810:        
                   13811:        if(emb_handle != NULL) {
                   13812:                emb_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   13813:                
                   13814:                REG16(AX) = 0x0001;
                   13815:                REG16(DX) = emb_handle->handle;
                   13816:                REG8(BL) = 0x00;
                   13817:        } else {
                   13818:                REG16(AX) = REG16(DX) = 0x0000;
                   13819:                REG8(BL) = 0xa0;
1.1.1.19  root     13820:        }
1.1.1.30  root     13821: }
                   13822: 
                   13823: inline void msdos_call_xms_09h()
                   13824: {
                   13825:        msdos_call_xms_09h(REG16(DX));
1.1.1.19  root     13826: }
                   13827: 
                   13828: inline void msdos_call_xms_0ah()
                   13829: {
1.1.1.30  root     13830:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   13831:        
                   13832:        if(emb_handle == NULL) {
1.1.1.19  root     13833:                REG16(AX) = 0x0000;
                   13834:                REG8(BL) = 0xa2;
1.1.1.30  root     13835:        } else if(emb_handle->lock > 0) {
1.1.1.19  root     13836:                REG16(AX) = 0x0000;
                   13837:                REG8(BL) = 0xab;
                   13838:        } else {
1.1.1.30  root     13839:                msdos_xms_free_emb_handle(emb_handle);
1.1.1.19  root     13840:                
                   13841:                REG16(AX) = 0x0001;
                   13842:                REG8(BL) = 0x00;
                   13843:        }
                   13844: }
                   13845: 
                   13846: inline void msdos_call_xms_0bh()
                   13847: {
                   13848:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   13849:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   13850:        UINT32 src_addr    = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06);
                   13851:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0a);
                   13852:        UINT32 dest_addr   = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   13853:        
                   13854:        UINT8 *src_buffer, *dest_buffer;
                   13855:        UINT32 src_addr_max, dest_addr_max;
1.1.1.30  root     13856:        emb_handle_t *emb_handle;
1.1.1.19  root     13857:        
                   13858:        if(src_handle == 0) {
                   13859:                src_buffer = mem;
                   13860:                src_addr = (((src_addr >> 16) & 0xffff) << 4) + (src_addr & 0xffff);
                   13861:                src_addr_max = MAX_MEM;
1.1.1.30  root     13862: 
1.1.1.19  root     13863:        } else {
1.1.1.30  root     13864:                if((emb_handle = msdos_xms_get_emb_handle(src_handle)) == NULL) {
1.1.1.19  root     13865:                        REG16(AX) = 0x0000;
                   13866:                        REG8(BL) = 0xa3;
                   13867:                        return;
                   13868:                }
1.1.1.30  root     13869:                src_buffer = mem + emb_handle->address;
                   13870:                src_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     13871:        }
                   13872:        if(dest_handle == 0) {
                   13873:                dest_buffer = mem;
                   13874:                dest_addr = (((dest_addr >> 16) & 0xffff) << 4) + (dest_addr & 0xffff);
                   13875:                dest_addr_max = MAX_MEM;
                   13876:        } else {
1.1.1.30  root     13877:                if((emb_handle = msdos_xms_get_emb_handle(dest_handle)) == NULL) {
1.1.1.19  root     13878:                        REG16(AX) = 0x0000;
                   13879:                        REG8(BL) = 0xa5;
                   13880:                        return;
                   13881:                }
1.1.1.30  root     13882:                dest_buffer = mem + emb_handle->address;
                   13883:                dest_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     13884:        }
                   13885:        for(int i = 0; i < copy_length; i++) {
                   13886:                if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   13887:                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   13888:                } else {
                   13889:                        break;
                   13890:                }
                   13891:        }
                   13892:        REG16(AX) = 0x0001;
                   13893:        REG8(BL) = 0x00;
                   13894: }
                   13895: 
                   13896: inline void msdos_call_xms_0ch()
                   13897: {
1.1.1.30  root     13898:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   13899:        
                   13900:        if(emb_handle == NULL) {
1.1.1.19  root     13901:                REG16(AX) = 0x0000;
                   13902:                REG8(BL) = 0xa2;
                   13903:        } else {
1.1.1.30  root     13904:                emb_handle->lock++;
1.1.1.19  root     13905:                REG16(AX) = 0x0001;
                   13906:                REG8(BL) = 0x00;
1.1.1.30  root     13907:                REG16(DX) = (emb_handle->address >> 16) & 0xffff;
                   13908:                REG16(BX) = (emb_handle->address      ) & 0xffff;
1.1.1.19  root     13909:        }
                   13910: }
                   13911: 
                   13912: inline void msdos_call_xms_0dh()
                   13913: {
1.1.1.30  root     13914:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   13915:        
                   13916:        if(emb_handle == NULL) {
1.1.1.19  root     13917:                REG16(AX) = 0x0000;
                   13918:                REG8(BL) = 0xa2;
1.1.1.30  root     13919:        } else if(!(emb_handle->lock > 0)) {
1.1.1.19  root     13920:                REG16(AX) = 0x0000;
                   13921:                REG8(BL) = 0xaa;
                   13922:        } else {
1.1.1.30  root     13923:                emb_handle->lock--;
1.1.1.19  root     13924:                REG16(AX) = 0x0001;
                   13925:                REG8(BL) = 0x00;
                   13926:        }
                   13927: }
                   13928: 
                   13929: inline void msdos_call_xms_0eh()
                   13930: {
1.1.1.30  root     13931:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   13932:        
                   13933:        if(emb_handle == NULL) {
1.1.1.19  root     13934:                REG16(AX) = 0x0000;
                   13935:                REG8(BL) = 0xa2;
                   13936:        } else {
                   13937:                REG16(AX) = 0x0001;
1.1.1.30  root     13938:                REG8(BH) = emb_handle->lock;
                   13939:                REG8(BL) = msdos_xms_get_unused_emb_handle_count();
                   13940:                REG16(DX) = emb_handle->size_kb;
1.1.1.19  root     13941:        }
                   13942: }
                   13943: 
1.1.1.30  root     13944: void msdos_call_xms_0fh(int size_kb)
1.1.1.19  root     13945: {
1.1.1.30  root     13946:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   13947:        
                   13948:        if(emb_handle == NULL) {
1.1.1.19  root     13949:                REG16(AX) = 0x0000;
                   13950:                REG8(BL) = 0xa2;
1.1.1.30  root     13951:        } else if(emb_handle->lock > 0) {
1.1.1.19  root     13952:                REG16(AX) = 0x0000;
                   13953:                REG8(BL) = 0xab;
                   13954:        } else {
1.1.1.30  root     13955:                if(emb_handle->size_kb < size_kb) {
                   13956:                        if(emb_handle->next != NULL && emb_handle->next->handle == 0 && (emb_handle->size_kb + emb_handle->next->size_kb) >= size_kb) {
                   13957:                                msdos_xms_combine_emb_handles(emb_handle);
                   13958:                                if(emb_handle->size_kb > size_kb) {
                   13959:                                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   13960:                                }
                   13961:                        } else {
                   13962:                                int old_handle = emb_handle->handle;
                   13963:                                int old_size_kb = emb_handle->size_kb;
                   13964:                                UINT8 *buffer = (UINT8 *)malloc(old_size_kb * 1024);
                   13965:                                
                   13966:                                memcpy(buffer, mem + emb_handle->address, old_size_kb * 1024);
                   13967:                                msdos_xms_free_emb_handle(emb_handle);
                   13968:                                
                   13969:                                if((emb_handle = msdos_xms_alloc_emb_handle(size_kb)) == NULL) {
                   13970:                                        emb_handle = msdos_xms_alloc_emb_handle(old_size_kb); // should be always successed
                   13971:                                }
                   13972:                                emb_handle->handle = old_handle;
                   13973:                                memcpy(mem + emb_handle->address, buffer, old_size_kb * 1024);
                   13974:                                free(buffer);
                   13975:                        }
                   13976:                } else if(emb_handle->size_kb > size_kb) {
                   13977:                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   13978:                }
                   13979:                if(emb_handle->size_kb != size_kb) {
                   13980:                        REG16(AX) = 0x0000;
                   13981:                        REG8(BL) = 0xa0;
                   13982:                } else {
                   13983:                        REG16(AX) = 0x0001;
                   13984:                        REG8(BL) = 0x00;
                   13985:                }
1.1.1.19  root     13986:        }
                   13987: }
                   13988: 
1.1.1.30  root     13989: inline void msdos_call_xms_0fh()
                   13990: {
                   13991:        msdos_call_xms_0fh(REG16(BX));
                   13992: }
                   13993: 
1.1.1.19  root     13994: inline void msdos_call_xms_10h()
                   13995: {
                   13996:        int seg;
                   13997:        
                   13998:        if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(DX), 0)) != -1) {
                   13999:                REG16(AX) = 0x0001;
                   14000:                REG16(BX) = seg;
                   14001:        } else {
                   14002:                REG16(AX) = 0x0000;
                   14003:                REG8(BL) = 0xb0;
                   14004:                REG16(DX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   14005:        }
                   14006: }
                   14007: 
                   14008: inline void msdos_call_xms_11h()
                   14009: {
                   14010:        int mcb_seg = REG16(DX) - 1;
                   14011:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   14012:        
                   14013:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   14014:                msdos_mem_free(REG16(DX));
                   14015:                REG16(AX) = 0x0001;
                   14016:                REG8(BL) = 0x00;
                   14017:        } else {
                   14018:                REG16(AX) = 0x0000;
                   14019:                REG8(BL) = 0xb2;
                   14020:        }
                   14021: }
                   14022: 
                   14023: inline void msdos_call_xms_12h()
                   14024: {
                   14025:        int mcb_seg = REG16(DX) - 1;
                   14026:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   14027:        int max_paragraphs;
                   14028:        
                   14029:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   14030:                if(!msdos_mem_realloc(REG16(DX), REG16(BX), &max_paragraphs)) {
                   14031:                        REG16(AX) = 0x0001;
                   14032:                        REG8(BL) = 0x00;
                   14033:                } else {
                   14034:                        REG16(AX) = 0x0000;
                   14035:                        REG8(BL) = 0xb0;
                   14036:                        REG16(DX) = max_paragraphs;
                   14037:                }
                   14038:        } else {
                   14039:                REG16(AX) = 0x0000;
                   14040:                REG8(BL) = 0xb2;
                   14041:        }
                   14042: }
                   14043: 
1.1.1.29  root     14044: #if defined(HAS_I386)
                   14045: 
                   14046: inline void msdos_call_xms_88h()
                   14047: {
                   14048:        REG32(EAX) = REG32(EDX) = 0x0000;
                   14049:        
1.1.1.30  root     14050:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   14051:                if(emb_handle->handle == 0) {
                   14052:                        if(REG32(EAX) < emb_handle->size_kb) {
                   14053:                                REG32(EAX) = emb_handle->size_kb;
1.1.1.29  root     14054:                        }
1.1.1.30  root     14055:                        REG32(EDX) += emb_handle->size_kb;
1.1.1.29  root     14056:                }
                   14057:        }
                   14058:        
                   14059:        if(REG32(EAX) == 0 && REG32(EDX) == 0) {
                   14060:                REG8(BL) = 0xa0;
                   14061:        } else {
                   14062:                REG8(BL) = 0x00;
                   14063:        }
                   14064:        REG32(ECX) = EMB_END - 1;
                   14065: }
                   14066: 
                   14067: inline void msdos_call_xms_89h()
                   14068: {
1.1.1.30  root     14069:        msdos_call_xms_09h(REG32(EDX));
1.1.1.29  root     14070: }
                   14071: 
                   14072: inline void msdos_call_xms_8eh()
                   14073: {
1.1.1.30  root     14074:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   14075:        
                   14076:        if(emb_handle == NULL) {
1.1.1.29  root     14077:                REG16(AX) = 0x0000;
                   14078:                REG8(BL) = 0xa2;
                   14079:        } else {
                   14080:                REG16(AX) = 0x0001;
1.1.1.30  root     14081:                REG8(BH) = emb_handle->lock;
                   14082:                REG16(CX) = msdos_xms_get_unused_emb_handle_count();
                   14083:                REG32(EDX) = emb_handle->size_kb;
1.1.1.29  root     14084:        }
                   14085: }
                   14086: 
                   14087: inline void msdos_call_xms_8fh()
                   14088: {
1.1.1.30  root     14089:        msdos_call_xms_0fh(REG32(EBX));
1.1.1.29  root     14090: }
                   14091: 
                   14092: #endif
1.1.1.19  root     14093: #endif
                   14094: 
1.1.1.26  root     14095: UINT16 msdos_get_equipment()
                   14096: {
                   14097:        static UINT16 equip = 0;
                   14098:        
                   14099:        if(equip == 0) {
                   14100: #ifdef SUPPORT_FPU
                   14101:                equip |= (1 << 1);      // 80x87 coprocessor installed
                   14102: #endif
                   14103:                equip |= (1 << 2);      // pointing device installed (PS/2)
                   14104:                equip |= (2 << 4);      // initial video mode (80x25 color)
                   14105: //             equip |= (1 << 8);      // 0 if DMA installed
                   14106:                equip |= (2 << 9);      // number of serial ports
                   14107:                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     14108:                
                   14109:                // check only A: and B: if it is floppy drive
                   14110:                DWORD dwDrives = GetLogicalDrives();
                   14111:                int n = 0;
                   14112:                for(int i = 0; i < 2; i++) {
                   14113:                        if(dwDrives & (1 << i)) {
                   14114:                                char volume[] = "A:\\";
                   14115:                                volume[0] = 'A' + i;
                   14116:                                if(GetDriveType(volume) == DRIVE_REMOVABLE) {
                   14117:                                        n++;
                   14118:                                }
                   14119:                        }
                   14120:                }
                   14121:                if(n != 0) {
                   14122:                        equip |= (1 << 0);      // floppy disk(s) installed
                   14123:                        n--;
                   14124:                        equip |= (n << 6);      // number of floppies installed less 1
                   14125:                }
                   14126: //             if(joyGetNumDevs() != 0) {
                   14127: //                     equip |= (1 << 12);     // game port installed
                   14128: //             }
1.1.1.26  root     14129:        }
                   14130:        return(equip);
                   14131: }
                   14132: 
1.1       root     14133: void msdos_syscall(unsigned num)
                   14134: {
1.1.1.22  root     14135: #ifdef ENABLE_DEBUG_SYSCALL
                   14136:        if(num == 0x68) {
                   14137:                // dummy interrupt for EMS (int 67h)
1.1.1.33  root     14138:                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     14139:        } else if(num == 0x69) {
                   14140:                // dummy interrupt for XMS (call far)
1.1.1.33  root     14141:                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     14142:        } else if(num == 0x6a) {
                   14143:                // dummy interrupt for case map routine pointed in the country info
                   14144:        } else {
1.1.1.33  root     14145:                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     14146:        }
                   14147: #endif
1.1.1.36  root     14148:        // update cursor position
                   14149:        if(cursor_moved) {
                   14150:                pcbios_update_cursor_position();
                   14151:                cursor_moved = false;
                   14152:        }
1.1.1.33  root     14153:        ctrl_break_detected = ctrl_break_pressed = ctrl_c_pressed = false;
1.1.1.22  root     14154:        
1.1       root     14155:        switch(num) {
                   14156:        case 0x00:
1.1.1.28  root     14157:                try {
                   14158:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   14159:                        error("division by zero\n");
                   14160:                } catch(...) {
                   14161:                        fatalerror("division by zero detected, and failed to terminate current process\n");
                   14162:                }
1.1       root     14163:                break;
                   14164:        case 0x04:
1.1.1.28  root     14165:                try {
                   14166:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   14167:                        error("overflow\n");
                   14168:                } catch(...) {
                   14169:                        fatalerror("overflow detected, and failed to terminate current process\n");
                   14170:                }
1.1       root     14171:                break;
                   14172:        case 0x06:
                   14173:                // NOTE: ish.com has illegal instruction...
1.1.1.14  root     14174:                if(!ignore_illegal_insn) {
1.1.1.28  root     14175:                        try {
                   14176:                                msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   14177:                                error("illegal instruction\n");
                   14178:                        } catch(...) {
                   14179:                                fatalerror("illegal instruction detected, and failed to terminate current process\n");
                   14180:                        }
1.1.1.14  root     14181:                } else {
                   14182: #if defined(HAS_I386)
                   14183:                        m_eip++;
                   14184: #else
                   14185:                        m_pc++;
                   14186: #endif
                   14187:                }
1.1       root     14188:                break;
1.1.1.33  root     14189:        case 0x09:
                   14190:                // ctrl-break is pressed
                   14191:                if(raise_int_1bh) {
                   14192: #if defined(HAS_I386)
                   14193:                        m_ext = 0; // not an external interrupt
                   14194:                        i386_trap(0x1b, 1, 0);
                   14195:                        m_ext = 1;
                   14196: #else
                   14197:                        PREFIX86(_interrupt)(0x1b);
                   14198: #endif
                   14199:                        raise_int_1bh = false;
                   14200:                }
1.1.1.8   root     14201:        case 0x08:
1.1.1.14  root     14202: //             pcbios_irq0(); // this causes too slow emulation...
1.1.1.8   root     14203:        case 0x0b:
                   14204:        case 0x0c:
                   14205:        case 0x0d:
                   14206:        case 0x0e:
                   14207:        case 0x0f:
                   14208:                // EOI
                   14209:                pic[0].isr &= ~(1 << (num - 0x08));
                   14210:                pic_update();
                   14211:                break;
1.1       root     14212:        case 0x10:
                   14213:                // PC BIOS - Video
1.1.1.14  root     14214:                if(!restore_console_on_exit) {
1.1.1.15  root     14215:                        change_console_size(scr_width, scr_height);
1.1       root     14216:                }
1.1.1.3   root     14217:                m_CF = 0;
1.1       root     14218:                switch(REG8(AH)) {
1.1.1.16  root     14219:                case 0x00: pcbios_int_10h_00h(); break;
1.1       root     14220:                case 0x01: pcbios_int_10h_01h(); break;
                   14221:                case 0x02: pcbios_int_10h_02h(); break;
                   14222:                case 0x03: pcbios_int_10h_03h(); break;
                   14223:                case 0x05: pcbios_int_10h_05h(); break;
                   14224:                case 0x06: pcbios_int_10h_06h(); break;
                   14225:                case 0x07: pcbios_int_10h_07h(); break;
                   14226:                case 0x08: pcbios_int_10h_08h(); break;
                   14227:                case 0x09: pcbios_int_10h_09h(); break;
                   14228:                case 0x0a: pcbios_int_10h_0ah(); break;
                   14229:                case 0x0b: break;
                   14230:                case 0x0c: break;
                   14231:                case 0x0d: break;
                   14232:                case 0x0e: pcbios_int_10h_0eh(); break;
                   14233:                case 0x0f: pcbios_int_10h_0fh(); break;
                   14234:                case 0x10: break;
1.1.1.14  root     14235:                case 0x11: pcbios_int_10h_11h(); break;
                   14236:                case 0x12: pcbios_int_10h_12h(); break;
1.1       root     14237:                case 0x13: pcbios_int_10h_13h(); break;
1.1.1.30  root     14238:                case 0x18: pcbios_int_10h_18h(); break;
1.1.1.14  root     14239:                case 0x1a: pcbios_int_10h_1ah(); break;
1.1.1.24  root     14240:                case 0x1b: REG8(AL) = 0x00; break; // functionality/state information is not supported
                   14241:                case 0x1c: REG8(AL) = 0x00; break; // save/restore video state is not supported
1.1       root     14242:                case 0x1d: pcbios_int_10h_1dh(); break;
1.1.1.24  root     14243:                case 0x1e: REG8(AL) = 0x00; break; // flat-panel functions are not supported
                   14244:                case 0x1f: REG8(AL) = 0x00; break; // xga functions are not supported
1.1.1.22  root     14245:                case 0x4f: pcbios_int_10h_4fh(); break;
1.1.1.30  root     14246:                case 0x6f: break;
1.1.1.22  root     14247:                case 0x80: m_CF = 1; break; // unknown
                   14248:                case 0x81: m_CF = 1; break; // unknown
1.1       root     14249:                case 0x82: pcbios_int_10h_82h(); break;
1.1.1.22  root     14250:                case 0x83: pcbios_int_10h_83h(); break;
                   14251:                case 0x8b: break;
                   14252:                case 0x8c: m_CF = 1; break; // unknown
                   14253:                case 0x8d: m_CF = 1; break; // unknown
                   14254:                case 0x8e: m_CF = 1; break; // unknown
                   14255:                case 0x90: pcbios_int_10h_90h(); break;
                   14256:                case 0x91: pcbios_int_10h_91h(); break;
                   14257:                case 0x92: break;
                   14258:                case 0x93: break;
                   14259:                case 0xef: pcbios_int_10h_efh(); break;
1.1.1.24  root     14260:                case 0xfa: break; // ega register interface library is not installed
1.1       root     14261:                case 0xfe: pcbios_int_10h_feh(); break;
                   14262:                case 0xff: pcbios_int_10h_ffh(); break;
                   14263:                default:
1.1.1.22  root     14264:                        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));
                   14265:                        m_CF = 1;
1.1       root     14266:                        break;
                   14267:                }
                   14268:                break;
                   14269:        case 0x11:
                   14270:                // PC BIOS - Get Equipment List
1.1.1.26  root     14271:                REG16(AX) = msdos_get_equipment();
1.1       root     14272:                break;
                   14273:        case 0x12:
                   14274:                // PC BIOS - Get Memory Size
1.1.1.33  root     14275:                REG16(AX) = *(UINT16 *)(mem + 0x413);
1.1       root     14276:                break;
                   14277:        case 0x13:
                   14278:                // PC BIOS - Disk
1.1.1.22  root     14279: //             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     14280:                REG8(AH) = 0xff;
1.1.1.3   root     14281:                m_CF = 1;
1.1       root     14282:                break;
                   14283:        case 0x14:
                   14284:                // PC BIOS - Serial I/O
1.1.1.25  root     14285:                switch(REG8(AH)) {
                   14286:                case 0x00: pcbios_int_14h_00h(); break;
                   14287:                case 0x01: pcbios_int_14h_01h(); break;
                   14288:                case 0x02: pcbios_int_14h_02h(); break;
                   14289:                case 0x03: pcbios_int_14h_03h(); break;
                   14290:                case 0x04: pcbios_int_14h_04h(); break;
                   14291:                case 0x05: pcbios_int_14h_05h(); break;
                   14292:                default:
                   14293:                        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));
                   14294:                        break;
                   14295:                }
1.1       root     14296:                break;
                   14297:        case 0x15:
                   14298:                // PC BIOS
1.1.1.3   root     14299:                m_CF = 0;
1.1       root     14300:                switch(REG8(AH)) {
1.1.1.14  root     14301:                case 0x10: pcbios_int_15h_10h(); break;
1.1       root     14302:                case 0x23: pcbios_int_15h_23h(); break;
                   14303:                case 0x24: pcbios_int_15h_24h(); break;
1.1.1.24  root     14304:                case 0x41: break;
1.1       root     14305:                case 0x49: pcbios_int_15h_49h(); break;
1.1.1.22  root     14306:                case 0x50: pcbios_int_15h_50h(); break;
1.1.1.30  root     14307:                case 0x53: pcbios_int_15h_53h(); break;
1.1       root     14308:                case 0x86: pcbios_int_15h_86h(); break;
                   14309:                case 0x87: pcbios_int_15h_87h(); break;
                   14310:                case 0x88: pcbios_int_15h_88h(); break;
                   14311:                case 0x89: pcbios_int_15h_89h(); break;
1.1.1.21  root     14312:                case 0x8a: pcbios_int_15h_8ah(); break;
1.1.1.22  root     14313:                case 0xc0: // PS/2 ???
                   14314:                case 0xc1:
                   14315:                case 0xc2:
1.1.1.30  root     14316:                case 0xc3: // PS50+ ???
                   14317:                case 0xc4:
1.1.1.22  root     14318:                        REG8(AH) = 0x86;
                   14319:                        m_CF = 1;
                   14320:                        break;
1.1.1.3   root     14321: #if defined(HAS_I386)
1.1       root     14322:                case 0xc9: pcbios_int_15h_c9h(); break;
1.1.1.3   root     14323: #endif
1.1       root     14324:                case 0xca: pcbios_int_15h_cah(); break;
1.1.1.22  root     14325:                case 0xe8: pcbios_int_15h_e8h(); break;
1.1       root     14326:                default:
1.1.1.22  root     14327:                        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));
                   14328:                        REG8(AH) = 0x86;
1.1.1.3   root     14329:                        m_CF = 1;
1.1       root     14330:                        break;
                   14331:                }
                   14332:                break;
                   14333:        case 0x16:
                   14334:                // PC BIOS - Keyboard
1.1.1.3   root     14335:                m_CF = 0;
1.1       root     14336:                switch(REG8(AH)) {
                   14337:                case 0x00: pcbios_int_16h_00h(); break;
                   14338:                case 0x01: pcbios_int_16h_01h(); break;
                   14339:                case 0x02: pcbios_int_16h_02h(); break;
                   14340:                case 0x03: pcbios_int_16h_03h(); break;
                   14341:                case 0x05: pcbios_int_16h_05h(); break;
                   14342:                case 0x10: pcbios_int_16h_00h(); break;
                   14343:                case 0x11: pcbios_int_16h_01h(); break;
                   14344:                case 0x12: pcbios_int_16h_12h(); break;
                   14345:                case 0x13: pcbios_int_16h_13h(); break;
                   14346:                case 0x14: pcbios_int_16h_14h(); break;
1.1.1.24  root     14347:                case 0x55: pcbios_int_16h_55h(); break;
1.1.1.30  root     14348:                case 0x6f: pcbios_int_16h_6fh(); break;
1.1.1.22  root     14349:                case 0xda: break; // unknown
                   14350:                case 0xff: break; // unknown
1.1       root     14351:                default:
1.1.1.22  root     14352:                        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     14353:                        break;
                   14354:                }
                   14355:                break;
                   14356:        case 0x17:
                   14357:                // PC BIOS - Printer
1.1.1.37  root     14358:                m_CF = 0;
                   14359:                switch(REG8(AH)) {
                   14360:                case 0x00: pcbios_int_17h_00h(); break;
                   14361:                case 0x01: pcbios_int_17h_01h(); break;
                   14362:                case 0x02: pcbios_int_17h_02h(); break;
                   14363:                case 0x03: pcbios_int_17h_03h(); break;
                   14364:                case 0x50: pcbios_int_17h_50h(); break;
                   14365:                case 0x51: pcbios_int_17h_51h(); break;
                   14366:                case 0x52: pcbios_int_17h_52h(); break;
                   14367:                case 0x84: pcbios_int_17h_84h(); break;
                   14368:                case 0x85: pcbios_int_17h_85h(); break;
                   14369:                default:
                   14370:                        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));
                   14371:                        break;
                   14372:                }
1.1       root     14373:                break;
                   14374:        case 0x1a:
                   14375:                // PC BIOS - Timer
1.1.1.3   root     14376:                m_CF = 0;
1.1       root     14377:                switch(REG8(AH)) {
                   14378:                case 0x00: pcbios_int_1ah_00h(); break;
                   14379:                case 0x01: break;
                   14380:                case 0x02: pcbios_int_1ah_02h(); break;
                   14381:                case 0x03: break;
                   14382:                case 0x04: pcbios_int_1ah_04h(); break;
                   14383:                case 0x05: break;
                   14384:                case 0x0a: pcbios_int_1ah_0ah(); break;
                   14385:                case 0x0b: break;
1.1.1.14  root     14386:                case 0x35: break; // Word Perfect Third Party Interface?
                   14387:                case 0x36: break; // Word Perfect Third Party Interface
                   14388:                case 0x70: break; // SNAP? (Simple Network Application Protocol)
1.1       root     14389:                default:
1.1.1.22  root     14390:                        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     14391:                        break;
                   14392:                }
                   14393:                break;
1.1.1.33  root     14394:        case 0x1b:
                   14395:                mem[0x471] = 0x00;
                   14396:                break;
1.1       root     14397:        case 0x20:
1.1.1.28  root     14398:                try {
                   14399:                        msdos_process_terminate(SREG(CS), retval, 1);
                   14400:                } catch(...) {
                   14401:                        fatalerror("failed to terminate the process (PSP=%04X) by int 20h\n", SREG(CS));
                   14402:                }
1.1       root     14403:                break;
                   14404:        case 0x21:
                   14405:                // MS-DOS System Call
1.1.1.3   root     14406:                m_CF = 0;
1.1.1.28  root     14407:                try {
                   14408:                        switch(REG8(AH)) {
                   14409:                        case 0x00: msdos_int_21h_00h(); break;
                   14410:                        case 0x01: msdos_int_21h_01h(); break;
                   14411:                        case 0x02: msdos_int_21h_02h(); break;
                   14412:                        case 0x03: msdos_int_21h_03h(); break;
                   14413:                        case 0x04: msdos_int_21h_04h(); break;
                   14414:                        case 0x05: msdos_int_21h_05h(); break;
                   14415:                        case 0x06: msdos_int_21h_06h(); break;
                   14416:                        case 0x07: msdos_int_21h_07h(); break;
                   14417:                        case 0x08: msdos_int_21h_08h(); break;
                   14418:                        case 0x09: msdos_int_21h_09h(); break;
                   14419:                        case 0x0a: msdos_int_21h_0ah(); break;
                   14420:                        case 0x0b: msdos_int_21h_0bh(); break;
                   14421:                        case 0x0c: msdos_int_21h_0ch(); break;
                   14422:                        case 0x0d: msdos_int_21h_0dh(); break;
                   14423:                        case 0x0e: msdos_int_21h_0eh(); break;
                   14424:                        case 0x0f: msdos_int_21h_0fh(); break;
                   14425:                        case 0x10: msdos_int_21h_10h(); break;
                   14426:                        case 0x11: msdos_int_21h_11h(); break;
                   14427:                        case 0x12: msdos_int_21h_12h(); break;
                   14428:                        case 0x13: msdos_int_21h_13h(); break;
                   14429:                        case 0x14: msdos_int_21h_14h(); break;
                   14430:                        case 0x15: msdos_int_21h_15h(); break;
                   14431:                        case 0x16: msdos_int_21h_16h(); break;
                   14432:                        case 0x17: msdos_int_21h_17h(); break;
                   14433:                        case 0x18: msdos_int_21h_18h(); break;
                   14434:                        case 0x19: msdos_int_21h_19h(); break;
                   14435:                        case 0x1a: msdos_int_21h_1ah(); break;
                   14436:                        case 0x1b: msdos_int_21h_1bh(); break;
                   14437:                        case 0x1c: msdos_int_21h_1ch(); break;
                   14438:                        case 0x1d: msdos_int_21h_1dh(); break;
                   14439:                        case 0x1e: msdos_int_21h_1eh(); break;
                   14440:                        case 0x1f: msdos_int_21h_1fh(); break;
                   14441:                        case 0x20: msdos_int_21h_20h(); break;
                   14442:                        case 0x21: msdos_int_21h_21h(); break;
                   14443:                        case 0x22: msdos_int_21h_22h(); break;
                   14444:                        case 0x23: msdos_int_21h_23h(); break;
                   14445:                        case 0x24: msdos_int_21h_24h(); break;
                   14446:                        case 0x25: msdos_int_21h_25h(); break;
                   14447:                        case 0x26: msdos_int_21h_26h(); break;
                   14448:                        case 0x27: msdos_int_21h_27h(); break;
                   14449:                        case 0x28: msdos_int_21h_28h(); break;
                   14450:                        case 0x29: msdos_int_21h_29h(); break;
                   14451:                        case 0x2a: msdos_int_21h_2ah(); break;
                   14452:                        case 0x2b: msdos_int_21h_2bh(); break;
                   14453:                        case 0x2c: msdos_int_21h_2ch(); break;
                   14454:                        case 0x2d: msdos_int_21h_2dh(); break;
                   14455:                        case 0x2e: msdos_int_21h_2eh(); break;
                   14456:                        case 0x2f: msdos_int_21h_2fh(); break;
                   14457:                        case 0x30: msdos_int_21h_30h(); break;
                   14458:                        case 0x31: msdos_int_21h_31h(); break;
                   14459:                        case 0x32: msdos_int_21h_32h(); break;
                   14460:                        case 0x33: msdos_int_21h_33h(); break;
                   14461:                        case 0x34: msdos_int_21h_34h(); break;
                   14462:                        case 0x35: msdos_int_21h_35h(); break;
                   14463:                        case 0x36: msdos_int_21h_36h(); break;
                   14464:                        case 0x37: msdos_int_21h_37h(); break;
                   14465:                        case 0x38: msdos_int_21h_38h(); break;
                   14466:                        case 0x39: msdos_int_21h_39h(0); break;
                   14467:                        case 0x3a: msdos_int_21h_3ah(0); break;
                   14468:                        case 0x3b: msdos_int_21h_3bh(0); break;
                   14469:                        case 0x3c: msdos_int_21h_3ch(); break;
                   14470:                        case 0x3d: msdos_int_21h_3dh(); break;
                   14471:                        case 0x3e: msdos_int_21h_3eh(); break;
                   14472:                        case 0x3f: msdos_int_21h_3fh(); break;
                   14473:                        case 0x40: msdos_int_21h_40h(); break;
                   14474:                        case 0x41: msdos_int_21h_41h(0); break;
                   14475:                        case 0x42: msdos_int_21h_42h(); break;
                   14476:                        case 0x43: msdos_int_21h_43h(0); break;
                   14477:                        case 0x44: msdos_int_21h_44h(); break;
                   14478:                        case 0x45: msdos_int_21h_45h(); break;
                   14479:                        case 0x46: msdos_int_21h_46h(); break;
                   14480:                        case 0x47: msdos_int_21h_47h(0); break;
                   14481:                        case 0x48: msdos_int_21h_48h(); break;
                   14482:                        case 0x49: msdos_int_21h_49h(); break;
                   14483:                        case 0x4a: msdos_int_21h_4ah(); break;
                   14484:                        case 0x4b: msdos_int_21h_4bh(); break;
                   14485:                        case 0x4c: msdos_int_21h_4ch(); break;
                   14486:                        case 0x4d: msdos_int_21h_4dh(); break;
                   14487:                        case 0x4e: msdos_int_21h_4eh(); break;
                   14488:                        case 0x4f: msdos_int_21h_4fh(); break;
                   14489:                        case 0x50: msdos_int_21h_50h(); break;
                   14490:                        case 0x51: msdos_int_21h_51h(); break;
                   14491:                        case 0x52: msdos_int_21h_52h(); break;
1.1.1.33  root     14492:                        // 0x53: Translate BIOS Parameter Block to Drive Param Bock
1.1.1.28  root     14493:                        case 0x54: msdos_int_21h_54h(); break;
                   14494:                        case 0x55: msdos_int_21h_55h(); break;
                   14495:                        case 0x56: msdos_int_21h_56h(0); break;
                   14496:                        case 0x57: msdos_int_21h_57h(); break;
                   14497:                        case 0x58: msdos_int_21h_58h(); break;
                   14498:                        case 0x59: msdos_int_21h_59h(); break;
                   14499:                        case 0x5a: msdos_int_21h_5ah(); break;
                   14500:                        case 0x5b: msdos_int_21h_5bh(); break;
                   14501:                        case 0x5c: msdos_int_21h_5ch(); break;
                   14502:                        case 0x5d: msdos_int_21h_5dh(); break;
1.1.1.33  root     14503:                        // 0x5e: MS-Network
1.1.1.30  root     14504:                        case 0x5f: msdos_int_21h_5fh(); break;
1.1.1.28  root     14505:                        case 0x60: msdos_int_21h_60h(0); break;
                   14506:                        case 0x61: msdos_int_21h_61h(); break;
                   14507:                        case 0x62: msdos_int_21h_62h(); break;
                   14508:                        case 0x63: msdos_int_21h_63h(); break;
1.1.1.33  root     14509:                        // 0x64: Set Device Driver Lockahead Flag
1.1.1.28  root     14510:                        case 0x65: msdos_int_21h_65h(); break;
                   14511:                        case 0x66: msdos_int_21h_66h(); break;
                   14512:                        case 0x67: msdos_int_21h_67h(); break;
                   14513:                        case 0x68: msdos_int_21h_68h(); break;
                   14514:                        case 0x69: msdos_int_21h_69h(); break;
                   14515:                        case 0x6a: msdos_int_21h_6ah(); break;
                   14516:                        case 0x6b: msdos_int_21h_6bh(); break;
                   14517:                        case 0x6c: msdos_int_21h_6ch(0); break;
1.1.1.33  root     14518:                        // 0x6d: Find First ROM Program
                   14519:                        // 0x6e: Find Next ROM Program
                   14520:                        // 0x6f: Get/Set ROM Scan Start Address
                   14521:                        // 0x70: Windows95 - Get/Set Internationalization Information
1.1.1.28  root     14522:                        case 0x71:
1.1.1.33  root     14523:                                // Windows95 - Long Filename Functions
1.1.1.28  root     14524:                                switch(REG8(AL)) {
                   14525:                                case 0x0d: msdos_int_21h_710dh(); break;
                   14526:                                case 0x39: msdos_int_21h_39h(1); break;
                   14527:                                case 0x3a: msdos_int_21h_3ah(1); break;
                   14528:                                case 0x3b: msdos_int_21h_3bh(1); break;
                   14529:                                case 0x41: msdos_int_21h_7141h(1); break;
                   14530:                                case 0x43: msdos_int_21h_43h(1); break;
                   14531:                                case 0x47: msdos_int_21h_47h(1); break;
                   14532:                                case 0x4e: msdos_int_21h_714eh(); break;
                   14533:                                case 0x4f: msdos_int_21h_714fh(); break;
                   14534:                                case 0x56: msdos_int_21h_56h(1); break;
                   14535:                                case 0x60: msdos_int_21h_60h(1); break;
                   14536:                                case 0x6c: msdos_int_21h_6ch(1); break;
                   14537:                                case 0xa0: msdos_int_21h_71a0h(); break;
                   14538:                                case 0xa1: msdos_int_21h_71a1h(); break;
                   14539:                                case 0xa6: msdos_int_21h_71a6h(); break;
                   14540:                                case 0xa7: msdos_int_21h_71a7h(); break;
                   14541:                                case 0xa8: msdos_int_21h_71a8h(); break;
1.1.1.33  root     14542:                                // 0xa9: Server Create/Open File
1.1.1.28  root     14543:                                case 0xaa: msdos_int_21h_71aah(); break;
                   14544:                                default:
                   14545:                                        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));
                   14546:                                        REG16(AX) = 0x7100;
                   14547:                                        m_CF = 1;
                   14548:                                        break;
                   14549:                                }
                   14550:                                break;
                   14551:                        // 0x72: Windows95 beta - LFN FindClose
                   14552:                        case 0x73:
1.1.1.33  root     14553:                                // Windows95 - FAT32 Functions
1.1.1.28  root     14554:                                switch(REG8(AL)) {
                   14555:                                case 0x00: msdos_int_21h_7300h(); break;
1.1.1.33  root     14556:                                // 0x01: Set Drive Locking ???
1.1.1.28  root     14557:                                case 0x02: msdos_int_21h_7302h(); break;
                   14558:                                case 0x03: msdos_int_21h_7303h(); break;
1.1.1.33  root     14559:                                // 0x04: Set DPB to Use for Formatting
                   14560:                                // 0x05: Extended Absolute Disk Read/Write
1.1.1.28  root     14561:                                default:
                   14562:                                        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));
                   14563:                                        REG16(AX) = 0x7300;
                   14564:                                        m_CF = 1;
                   14565:                                        break;
                   14566:                                }
1.1       root     14567:                                break;
1.1.1.30  root     14568:                        case 0xdb: msdos_int_21h_dbh(); break;
                   14569:                        case 0xdc: msdos_int_21h_dch(); break;
1.1       root     14570:                        default:
1.1.1.22  root     14571:                                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     14572:                                REG16(AX) = 0x01;
1.1.1.3   root     14573:                                m_CF = 1;
1.1       root     14574:                                break;
                   14575:                        }
1.1.1.28  root     14576:                } catch(int error) {
                   14577:                        REG16(AX) = error;
                   14578:                        m_CF = 1;
                   14579:                } catch(...) {
                   14580:                        REG16(AX) = 0x1f; // general failure
1.1.1.3   root     14581:                        m_CF = 1;
1.1       root     14582:                }
1.1.1.3   root     14583:                if(m_CF) {
1.1.1.23  root     14584:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   14585:                        sda->extended_error_code = REG16(AX);
                   14586:                        switch(sda->extended_error_code) {
                   14587:                        case  4: // Too many open files
                   14588:                        case  8: // Insufficient memory
                   14589:                                sda->error_class = 1; // Out of resource
                   14590:                                break;
                   14591:                        case  5: // Access denied
                   14592:                                sda->error_class = 3; // Authorization
                   14593:                                break;
                   14594:                        case  7: // Memory control block destroyed
                   14595:                                sda->error_class = 4; // Internal
                   14596:                                break;
                   14597:                        case  2: // File not found
                   14598:                        case  3: // Path not found
                   14599:                        case 15: // Invaid drive specified
                   14600:                        case 18: // No more files
                   14601:                                sda->error_class = 8; // Not found
                   14602:                                break;
                   14603:                        case 32: // Sharing violation
                   14604:                        case 33: // Lock violation
                   14605:                                sda->error_class = 10; // Locked
                   14606:                                break;
                   14607: //                     case 16: // Removal of current directory attempted
                   14608:                        case 19: // Attempted write on protected disk
                   14609:                        case 21: // Drive not ready
                   14610: //                     case 29: // Write failure
                   14611: //                     case 30: // Read failure
                   14612: //                     case 82: // Cannot create subdirectory
                   14613:                                sda->error_class = 11; // Media
                   14614:                                break;
                   14615:                        case 80: // File already exists
                   14616:                                sda->error_class = 12; // Already exist
                   14617:                                break;
                   14618:                        default:
                   14619:                                sda->error_class = 13; // Unknown
                   14620:                                break;
                   14621:                        }
                   14622:                        sda->suggested_action = 1; // Retry
                   14623:                        sda->locus_of_last_error = 1; // Unknown
1.1       root     14624:                }
1.1.1.33  root     14625:                if(ctrl_break_checking && ctrl_break_detected) {
1.1.1.26  root     14626:                        // raise int 23h
                   14627: #if defined(HAS_I386)
                   14628:                        m_ext = 0; // not an external interrupt
                   14629:                        i386_trap(0x23, 1, 0);
                   14630:                        m_ext = 1;
                   14631: #else
                   14632:                        PREFIX86(_interrupt)(0x23);
                   14633: #endif
                   14634:                }
1.1       root     14635:                break;
                   14636:        case 0x22:
                   14637:                fatalerror("int 22h (terminate address)\n");
                   14638:        case 0x23:
1.1.1.28  root     14639:                try {
                   14640:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x100, 1);
                   14641:                } catch(...) {
                   14642:                        fatalerror("failed to terminate the current process by int 23h\n");
                   14643:                }
1.1       root     14644:                break;
                   14645:        case 0x24:
1.1.1.32  root     14646: /*
1.1.1.28  root     14647:                try {
                   14648:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   14649:                } catch(...) {
                   14650:                        fatalerror("failed to terminate the current process by int 24h\n");
                   14651:                }
1.1.1.32  root     14652: */
                   14653:                msdos_int_24h();
1.1       root     14654:                break;
                   14655:        case 0x25:
                   14656:                msdos_int_25h();
                   14657:                break;
                   14658:        case 0x26:
                   14659:                msdos_int_26h();
                   14660:                break;
                   14661:        case 0x27:
1.1.1.28  root     14662:                try {
                   14663:                        msdos_int_27h();
                   14664:                } catch(...) {
                   14665:                        fatalerror("failed to terminate the process (PSP=%04X) by int 27h\n", SREG(CS));
                   14666:                }
1.1       root     14667:                break;
                   14668:        case 0x28:
                   14669:                Sleep(10);
1.1.1.35  root     14670:                REQUEST_HARDWRE_UPDATE();
1.1       root     14671:                break;
                   14672:        case 0x29:
                   14673:                msdos_int_29h();
                   14674:                break;
                   14675:        case 0x2e:
                   14676:                msdos_int_2eh();
                   14677:                break;
                   14678:        case 0x2f:
                   14679:                // multiplex interrupt
                   14680:                switch(REG8(AH)) {
1.1.1.22  root     14681:                case 0x05: msdos_int_2fh_05h(); break;
                   14682:                case 0x11: msdos_int_2fh_11h(); break;
1.1.1.21  root     14683:                case 0x12: msdos_int_2fh_12h(); break;
1.1.1.30  root     14684:                case 0x13: msdos_int_2fh_13h(); break;
1.1.1.22  root     14685:                case 0x14: msdos_int_2fh_14h(); break;
                   14686:                case 0x15: msdos_int_2fh_15h(); break;
1.1       root     14687:                case 0x16: msdos_int_2fh_16h(); break;
1.1.1.22  root     14688:                case 0x19: msdos_int_2fh_19h(); break;
1.1       root     14689:                case 0x1a: msdos_int_2fh_1ah(); break;
1.1.1.30  root     14690:                case 0x40: msdos_int_2fh_40h(); break;
1.1       root     14691:                case 0x43: msdos_int_2fh_43h(); break;
1.1.1.22  root     14692:                case 0x46: msdos_int_2fh_46h(); break;
                   14693:                case 0x48: msdos_int_2fh_48h(); break;
1.1       root     14694:                case 0x4a: msdos_int_2fh_4ah(); break;
1.1.1.22  root     14695:                case 0x4b: msdos_int_2fh_4bh(); break;
1.1       root     14696:                case 0x4f: msdos_int_2fh_4fh(); break;
1.1.1.22  root     14697:                case 0x55: msdos_int_2fh_55h(); break;
1.1.1.24  root     14698:                case 0xad: msdos_int_2fh_adh(); break;
1.1       root     14699:                case 0xae: msdos_int_2fh_aeh(); break;
1.1.1.34  root     14700:                case 0xb7: msdos_int_2fh_b7h(); break;
1.1.1.30  root     14701:                // Installation Check
                   14702:                case 0x01: // PRINT.COM
                   14703:                case 0x02: // PC LAN Program Redirector
                   14704:                case 0x06: // ASSIGN
                   14705:                case 0x08: // DRIVER.SYS
                   14706:                case 0x10: // SHARE
                   14707:                case 0x17: // Clibboard functions
                   14708:                case 0x1b: // XMA2EMS.SYS
                   14709:                case 0x23: // DR DOS 5.0 GRAFTABL
                   14710:                case 0x27: // DR-DOR 6.0 TaskMAX
                   14711:                case 0x2e: // Novell DOS 7 GRAFTABL
1.1.1.33  root     14712:                case 0x39: // Kingswood TSR INTERFACE
                   14713:                case 0x41: // DOS Enhanced LAN Manager 2.0+ MINIPOP/NETPOPUP
1.1.1.30  root     14714:                case 0x45: // PROF.COM
                   14715:                case 0x51: // ODIHELP.EXE
1.1.1.33  root     14716:                case 0x52: // JAM.SYS v1.10+
1.1.1.30  root     14717:                case 0x54: // POWER.EXE
                   14718:                case 0x56: // INTERLNK
1.1.1.33  root     14719:                case 0x57: // IOMEGA DRIVERS
1.1.1.30  root     14720:                case 0x70: // License Service API
1.1.1.33  root     14721:                case 0x72: // SRDISK v1.30+
1.1.1.30  root     14722:                case 0x7a: // Novell NetWare
1.1.1.33  root     14723:                case 0x7f: // PRINDIR v9.0
                   14724:                case 0x80: // FAX BIOS
                   14725:                case 0x81: // Nanosoft, Inc. TurboNET redirector
                   14726:                case 0x82: // Nanosoft, Inc. CAPDOS
                   14727:                case 0x89: // WHOA!.COM
                   14728:                case 0x90: // Resident AID
1.1.1.30  root     14729:                case 0x94: // MICRO.EXE
1.1.1.33  root     14730:                case 0x97: // Micro Focus COBOL v3.1.31
                   14731:                case 0x98: // Micro Focus COBOL v3.1.31
                   14732:                case 0x99: // DOS Navigator II
                   14733:                case 0x9e: // INTMON v2.1
                   14734:                case 0x9f: // INTCFG v2.1
                   14735:                case 0xa9: // METZTSR.COM
                   14736:                case 0xab: // SRSoft MODAL PC v2
1.1.1.30  root     14737:                case 0xac: // GRAPHICS.COM
1.1.1.33  root     14738:                case 0xaf: // WinDOS v2.11
1.1.1.30  root     14739:                case 0xb0: // GRAFTABLE.COM
1.1.1.33  root     14740:                case 0xb4: // IBM PC3270 EMULATION PROG v3
1.1.1.30  root     14741:                case 0xb8: // NETWORK
                   14742:                case 0xb9: // RECEIVER.COM
                   14743:                case 0xbc: // EGA.SYS
1.1.1.33  root     14744:                case 0xbe: // REDVIEW
1.1.1.30  root     14745:                case 0xbf: // PC LAN Program - REDIRIFS.EXE
                   14746:                case 0xc0: // Novell LSL.COM
1.1.1.33  root     14747:                case 0xc1: // Personal NetWare - STPIPX v1.00
                   14748:                case 0xc3: // SETWPR.COM
                   14749:                case 0xc5: // PC-DOS Econet v1.05
                   14750:                case 0xc7: // COLAP.COM
                   14751:                case 0xc9: // ThunderByte???
                   14752:                case 0xca: // TBSCANX
                   14753:                case 0xcb: // Communicating Applications Specification
                   14754:                case 0xcc: // Tsoft NFSDRVR
                   14755:                case 0xcd: // SWELL.EXE
                   14756:                case 0xcf: // TEMPLEXX 1.0
                   14757:                case 0xd0: // Lotus CD/Networker
1.1.1.30  root     14758:                case 0xd2: // PCL-838.EXE
1.1.1.33  root     14759:                case 0xd3: // TeleReplica
                   14760:                case 0xd6: // VEDIT VSWAP
                   14761:                case 0xd7: // Banyan VINES v4+
1.1.1.30  root     14762:                case 0xd8: // Novell NetWare Lite - CLIENT.EXE
1.1.1.33  root     14763:                case 0xda: // ZyXEL ZFAX v1.x
                   14764:                case 0xdb: // ZyXEL ZFAX v2+
                   14765:                case 0xdc: // GOLD.COM
                   14766:                case 0xdd: // MIXFIX.EXE
                   14767:                case 0xde: // Novell Netware - RPRINTER, NPRINTER
                   14768:                case 0xdf: // HyperWare programs
                   14769:                case 0xe0: // SETDRVER.COM v2.10+
                   14770:                case 0xe1: // Phantom2 v1.1+
                   14771:                case 0xe3: // ANARKEY.COM
                   14772:                case 0xed: // Phar Lap DOS EXTENDERS
                   14773:                case 0xee: // XVIEW
                   14774:                case 0xf0: // 4MAP
                   14775:                case 0xf1: // DOS EXTENDER
                   14776:                case 0xf2: // WINX
                   14777:                case 0xf4: // FINDIRQ.COM
                   14778:                case 0xf7: // AUTOPARK.COM
                   14779:                case 0xf8: // SuperStor PRO 2XON.COM
                   14780:                case 0xfb: // AutoBraille v1.1A
                   14781:                case 0xfe: // PC-NFS ???
                   14782:                case 0xff: // Topware Network Operating System
1.1.1.30  root     14783:                        switch(REG8(AL)) {
                   14784:                        case 0x00:
                   14785:                                // This is not installed
                   14786: //                             REG8(AL) = 0x00;
                   14787:                                break;
1.1.1.33  root     14788:                        case 0x01:
                   14789:                                // Banyan VINES v4+ is not installed
                   14790:                                if(REG8(AH) == 0xd7 && REG16(BX) == 0x0000) {
                   14791:                                        break;
                   14792:                                }
1.1.1.30  root     14793:                        default:
                   14794:                                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));
                   14795:                                REG16(AX) = 0x01;
                   14796:                                m_CF = 1;
                   14797:                                break;
                   14798:                        }
                   14799:                        break;
1.1.1.22  root     14800:                default:
                   14801:                        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));
                   14802:                        break;
1.1       root     14803:                }
                   14804:                break;
1.1.1.24  root     14805:        case 0x33:
                   14806:                switch(REG8(AH)) {
                   14807:                case 0x00:
                   14808:                        // Mouse
                   14809:                        switch(REG8(AL)) {
                   14810:                        case 0x00: msdos_int_33h_0000h(); break;
                   14811:                        case 0x01: msdos_int_33h_0001h(); break;
                   14812:                        case 0x02: msdos_int_33h_0002h(); break;
                   14813:                        case 0x03: msdos_int_33h_0003h(); break;
1.1.1.34  root     14814:                        case 0x04: msdos_int_33h_0004h(); break;
1.1.1.24  root     14815:                        case 0x05: msdos_int_33h_0005h(); break;
                   14816:                        case 0x06: msdos_int_33h_0006h(); break;
                   14817:                        case 0x07: msdos_int_33h_0007h(); break;
                   14818:                        case 0x08: msdos_int_33h_0008h(); break;
                   14819:                        case 0x09: msdos_int_33h_0009h(); break;
1.1.1.34  root     14820:                        case 0x0a: break; // Define Text Cursor
1.1.1.24  root     14821:                        case 0x0b: msdos_int_33h_000bh(); break;
                   14822:                        case 0x0c: msdos_int_33h_000ch(); break;
1.1.1.34  root     14823:                        case 0x0d: break; // Light Pen Emulation On
                   14824:                        case 0x0e: break; // Light Pen Emulation Off
1.1.1.24  root     14825:                        case 0x0f: msdos_int_33h_000fh(); break;
1.1.1.34  root     14826:                        case 0x10: break; // Define Screen Region for Updating
1.1.1.24  root     14827:                        case 0x11: msdos_int_33h_0011h(); break;
1.1.1.34  root     14828:                        case 0x12: REG16(AX) = 0xffff; break; // Set Large Graphics Cursor Block
                   14829:                        case 0x13: break; // Define Double-Speed Threshold
1.1.1.24  root     14830:                        case 0x14: msdos_int_33h_0014h(); break;
                   14831:                        case 0x15: msdos_int_33h_0015h(); break;
                   14832:                        case 0x16: msdos_int_33h_0016h(); break;
                   14833:                        case 0x17: msdos_int_33h_0017h(); break;
                   14834:                        case 0x1a: msdos_int_33h_001ah(); break;
                   14835:                        case 0x1b: msdos_int_33h_001bh(); break;
                   14836:                        case 0x1d: msdos_int_33h_001dh(); break;
                   14837:                        case 0x1e: msdos_int_33h_001eh(); break;
1.1.1.34  root     14838:                        case 0x1f: msdos_int_33h_001fh(); break;
                   14839:                        case 0x20: msdos_int_33h_0020h(); break;
1.1.1.24  root     14840:                        case 0x21: msdos_int_33h_0021h(); break;
                   14841:                        case 0x22: msdos_int_33h_0022h(); break;
                   14842:                        case 0x23: msdos_int_33h_0023h(); break;
                   14843:                        case 0x24: msdos_int_33h_0024h(); break;
                   14844:                        case 0x26: msdos_int_33h_0026h(); break;
                   14845:                        case 0x2a: msdos_int_33h_002ah(); break;
1.1.1.34  root     14846:                        case 0x2f: break; // Mouse Hardware Reset
1.1.1.24  root     14847:                        case 0x31: msdos_int_33h_0031h(); break;
                   14848:                        case 0x32: msdos_int_33h_0032h(); break;
                   14849:                        default:
                   14850:                                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));
                   14851:                                break;
                   14852:                        }
                   14853:                        break;
                   14854:                default:
                   14855:                        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));
                   14856:                        break;
                   14857:                }
                   14858:                break;
1.1.1.19  root     14859:        case 0x68:
                   14860:                // dummy interrupt for EMS (int 67h)
                   14861:                switch(REG8(AH)) {
                   14862:                case 0x40: msdos_int_67h_40h(); break;
                   14863:                case 0x41: msdos_int_67h_41h(); break;
                   14864:                case 0x42: msdos_int_67h_42h(); break;
                   14865:                case 0x43: msdos_int_67h_43h(); break;
                   14866:                case 0x44: msdos_int_67h_44h(); break;
                   14867:                case 0x45: msdos_int_67h_45h(); break;
                   14868:                case 0x46: msdos_int_67h_46h(); break;
                   14869:                case 0x47: msdos_int_67h_47h(); break;
                   14870:                case 0x48: msdos_int_67h_48h(); break;
1.1.1.20  root     14871:                // 0x49: LIM EMS - Reserved - Get I/O Port Address (Undocumented in EMS 3.2)
                   14872:                // 0x4a: LIM EMS - Reserved - Get Translation Array (Undocumented in EMS 3.2)
1.1.1.19  root     14873:                case 0x4b: msdos_int_67h_4bh(); break;
                   14874:                case 0x4c: msdos_int_67h_4ch(); break;
                   14875:                case 0x4d: msdos_int_67h_4dh(); break;
1.1.1.20  root     14876:                case 0x4e: msdos_int_67h_4eh(); break;
1.1.1.21  root     14877:                case 0x4f: msdos_int_67h_4fh(); break;
1.1.1.20  root     14878:                case 0x50: msdos_int_67h_50h(); break;
1.1.1.19  root     14879:                case 0x51: msdos_int_67h_51h(); break;
1.1.1.20  root     14880:                case 0x52: msdos_int_67h_52h(); break;
1.1.1.19  root     14881:                case 0x53: msdos_int_67h_53h(); break;
                   14882:                case 0x54: msdos_int_67h_54h(); break;
1.1.1.20  root     14883:                // 0x55: LIM EMS 4.0 - Alter Page Map And JUMP
                   14884:                // 0x56: LIM EMS 4.0 - Alter Page Map And CALL
                   14885:                case 0x57: msdos_int_67h_57h(); break;
                   14886:                case 0x58: msdos_int_67h_58h(); break;
                   14887:                // 0x59: LIM EMS 4.0 - Get Expanded Memory Hardware Information (for DOS Kernel)
                   14888:                case 0x5a: msdos_int_67h_5ah(); break;
                   14889:                // 0x5b: LIM EMS 4.0 - Alternate Map Register Set (for DOS Kernel)
                   14890:                // 0x5c: LIM EMS 4.0 - Prepate Expanded Memory Hardware For Warm Boot
                   14891:                // 0x5d: LIM EMS 4.0 - Enable/Disable OS Function Set Functions (for DOS Kernel)
1.1.1.31  root     14892:                // 0x60: EEMS - Get Physical Window Array
                   14893:                // 0x61: EEMS - Generic Accelerator Card Support
                   14894:                // 0x68: EEMS - Get Address of All Pge Frames om System
                   14895:                // 0x69: EEMS - Map Page into Frame
                   14896:                // 0x6a: EEMS - Page Mapping
                   14897:                // 0xde: VCPI
1.1.1.30  root     14898:                case 0xde: msdos_int_67h_deh(); break;
1.1.1.19  root     14899:                default:
1.1.1.22  root     14900:                        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     14901:                        REG8(AH) = 0x84;
                   14902:                        break;
                   14903:                }
                   14904:                break;
                   14905: #ifdef SUPPORT_XMS
                   14906:        case 0x69:
                   14907:                // dummy interrupt for XMS (call far)
1.1.1.28  root     14908:                try {
                   14909:                        switch(REG8(AH)) {
                   14910:                        case 0x00: msdos_call_xms_00h(); break;
                   14911:                        case 0x01: msdos_call_xms_01h(); break;
                   14912:                        case 0x02: msdos_call_xms_02h(); break;
                   14913:                        case 0x03: msdos_call_xms_03h(); break;
                   14914:                        case 0x04: msdos_call_xms_04h(); break;
                   14915:                        case 0x05: msdos_call_xms_05h(); break;
                   14916:                        case 0x06: msdos_call_xms_06h(); break;
                   14917:                        case 0x07: msdos_call_xms_07h(); break;
                   14918:                        case 0x08: msdos_call_xms_08h(); break;
                   14919:                        case 0x09: msdos_call_xms_09h(); break;
                   14920:                        case 0x0a: msdos_call_xms_0ah(); break;
                   14921:                        case 0x0b: msdos_call_xms_0bh(); break;
                   14922:                        case 0x0c: msdos_call_xms_0ch(); break;
                   14923:                        case 0x0d: msdos_call_xms_0dh(); break;
                   14924:                        case 0x0e: msdos_call_xms_0eh(); break;
                   14925:                        case 0x0f: msdos_call_xms_0fh(); break;
                   14926:                        case 0x10: msdos_call_xms_10h(); break;
                   14927:                        case 0x11: msdos_call_xms_11h(); break;
                   14928:                        case 0x12: msdos_call_xms_12h(); break;
1.1.1.29  root     14929: #if defined(HAS_I386)
                   14930:                        case 0x88: msdos_call_xms_88h(); break;
                   14931:                        case 0x89: msdos_call_xms_89h(); break;
                   14932:                        case 0x8e: msdos_call_xms_8eh(); break;
                   14933:                        case 0x8f: msdos_call_xms_8fh(); break;
                   14934: #endif
1.1.1.28  root     14935:                        default:
                   14936:                                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));
                   14937:                                REG16(AX) = 0x0000;
                   14938:                                REG8(BL) = 0x80; // function not implemented
                   14939:                                break;
                   14940:                        }
                   14941:                } catch(...) {
1.1.1.19  root     14942:                        REG16(AX) = 0x0000;
1.1.1.28  root     14943:                        REG8(BL) = 0x8f; // unrecoverable driver error
1.1.1.19  root     14944:                }
                   14945:                break;
                   14946: #endif
                   14947:        case 0x6a:
1.1.1.24  root     14948:                // irq12 (mouse)
                   14949:                mouse_push_ax = REG16(AX);
                   14950:                mouse_push_bx = REG16(BX);
                   14951:                mouse_push_cx = REG16(CX);
                   14952:                mouse_push_dx = REG16(DX);
                   14953:                mouse_push_si = REG16(SI);
                   14954:                mouse_push_di = REG16(DI);
                   14955:                
1.1.1.34  root     14956:                if(/*mouse.hidden == 0 && */mouse.call_addr.dw != 0) {
1.1.1.24  root     14957:                        REG16(AX) = mouse.status_irq;
                   14958:                        REG16(BX) = mouse.get_buttons();
1.1.1.34  root     14959:                        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   14960:                        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
1.1.1.24  root     14961:                        REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
                   14962:                        REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
                   14963:                        
                   14964:                        mem[0xfffd0 + 0x02] = 0x9a;     // call far
                   14965:                        mem[0xfffd0 + 0x03] = mouse.call_addr.w.l & 0xff;
                   14966:                        mem[0xfffd0 + 0x04] = mouse.call_addr.w.l >> 8;
                   14967:                        mem[0xfffd0 + 0x05] = mouse.call_addr.w.h & 0xff;
                   14968:                        mem[0xfffd0 + 0x06] = mouse.call_addr.w.h >> 8;
                   14969:                } else {
                   14970:                        mem[0xfffd0 + 0x02] = 0x90;     // nop
                   14971:                        mem[0xfffd0 + 0x03] = 0x90;     // nop
                   14972:                        mem[0xfffd0 + 0x04] = 0x90;     // nop
                   14973:                        mem[0xfffd0 + 0x05] = 0x90;     // nop
                   14974:                        mem[0xfffd0 + 0x06] = 0x90;     // nop
                   14975:                }
                   14976:                break;
                   14977:        case 0x6b:
                   14978:                // end of irq12 (mouse)
                   14979:                REG16(AX) = mouse_push_ax;
                   14980:                REG16(BX) = mouse_push_bx;
                   14981:                REG16(CX) = mouse_push_cx;
                   14982:                REG16(DX) = mouse_push_dx;
                   14983:                REG16(SI) = mouse_push_si;
                   14984:                REG16(DI) = mouse_push_di;
                   14985:                
                   14986:                // EOI
                   14987:                if((pic[1].isr &= ~(1 << 4)) == 0) {
                   14988:                        pic[0].isr &= ~(1 << 2); // master
                   14989:                }
                   14990:                pic_update();
                   14991:                break;
                   14992:        case 0x6c:
1.1.1.19  root     14993:                // dummy interrupt for case map routine pointed in the country info
                   14994:                if(REG8(AL) >= 0x80) {
                   14995:                        char tmp[2] = {0};
                   14996:                        tmp[0] = REG8(AL);
                   14997:                        my_strupr(tmp);
                   14998:                        REG8(AL) = tmp[0];
                   14999:                }
                   15000:                break;
1.1.1.27  root     15001:        case 0x6d:
                   15002:                // dummy interrupt for font read routine pointed by int 15h, ax=5000h
                   15003:                REG8(AL) = 0x86; // not supported
                   15004:                m_CF = 1;
                   15005:                break;
1.1.1.32  root     15006:        case 0x6e:
                   15007:                // dummy interrupt for parameter error message read routine pointed by int 2fh, ax=122eh, dl=08h
                   15008:                {
                   15009:                        UINT16 code = REG16(AX);
                   15010:                        if(code & 0xf0) {
                   15011:                                code = (code & 7) | ((code & 0x10) >> 1);
                   15012:                        }
                   15013:                        for(int i = 0; i < array_length(param_error_table); i++) {
                   15014:                                if(param_error_table[i].code == code || param_error_table[i].code == (UINT16)-1) {
                   15015:                                        const char *message = NULL;
                   15016:                                        if(active_code_page == 932) {
                   15017:                                                message = param_error_table[i].message_japanese;
                   15018:                                        }
                   15019:                                        if(message == NULL) {
                   15020:                                                message = param_error_table[i].message_english;
                   15021:                                        }
                   15022:                                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   15023:                                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   15024:                                        
                   15025:                                        SREG(ES) = WORK_TOP >> 4;
                   15026:                                        i386_load_segment_descriptor(ES);
                   15027:                                        REG16(DI) = 0x0000;
                   15028:                                        break;
                   15029:                                }
                   15030:                        }
                   15031:                }
                   15032:                break;
1.1.1.8   root     15033:        case 0x70:
                   15034:        case 0x71:
                   15035:        case 0x72:
                   15036:        case 0x73:
                   15037:        case 0x74:
                   15038:        case 0x75:
                   15039:        case 0x76:
                   15040:        case 0x77:
                   15041:                // EOI
                   15042:                if((pic[1].isr &= ~(1 << (num - 0x70))) == 0) {
                   15043:                        pic[0].isr &= ~(1 << 2); // master
                   15044:                }
                   15045:                pic_update();
                   15046:                break;
1.1       root     15047:        default:
1.1.1.22  root     15048: //             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     15049:                break;
                   15050:        }
                   15051:        
                   15052:        // update cursor position
                   15053:        if(cursor_moved) {
1.1.1.36  root     15054:                pcbios_update_cursor_position();
1.1       root     15055:                cursor_moved = false;
                   15056:        }
                   15057: }
                   15058: 
                   15059: // init
                   15060: 
                   15061: int msdos_init(int argc, char *argv[], char *envp[], int standard_env)
                   15062: {
                   15063:        // init file handler
                   15064:        memset(file_handler, 0, sizeof(file_handler));
                   15065:        msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   15066:        msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   15067:        msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
1.1.1.21  root     15068: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     15069:        if(_open("stdaux.txt", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
1.1.1.21  root     15070: #else
                   15071:        if(_open("NUL", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
                   15072: #endif
                   15073:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
1.1       root     15074:        }
1.1.1.21  root     15075:        if(_open("NUL", _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 4) {
1.1.1.37  root     15076:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0, 0, 1); // LPT1
1.1.1.21  root     15077:        }
1.1       root     15078:        _dup2(0, DUP_STDIN);
                   15079:        _dup2(1, DUP_STDOUT);
                   15080:        _dup2(2, DUP_STDERR);
1.1.1.21  root     15081:        _dup2(3, DUP_STDAUX);
                   15082:        _dup2(4, DUP_STDPRN);
1.1       root     15083:        
1.1.1.24  root     15084:        // init mouse
                   15085:        memset(&mouse, 0, sizeof(mouse));
1.1.1.34  root     15086:        mouse.enabled = true;   // from DOSBox
                   15087:        mouse.hidden = 1;       // hidden in default ???
                   15088:        mouse.old_hidden = 1;   // from DOSBox
                   15089:        mouse.max_position.x = 8 * (scr_width  - 1);
                   15090:        mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24  root     15091:        mouse.mickey.x = 8;
                   15092:        mouse.mickey.y = 16;
                   15093:        
1.1.1.26  root     15094: #ifdef SUPPORT_XMS
                   15095:        // init xms
                   15096:        msdos_xms_init();
                   15097: #endif
                   15098:        
1.1       root     15099:        // init process
                   15100:        memset(process, 0, sizeof(process));
                   15101:        
1.1.1.13  root     15102:        // init dtainfo
                   15103:        msdos_dta_info_init();
                   15104:        
1.1       root     15105:        // init memory
                   15106:        memset(mem, 0, sizeof(mem));
                   15107:        
                   15108:        // bios data area
1.1.1.23  root     15109:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     15110:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   15111:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.14  root     15112:        CONSOLE_FONT_INFO cfi;
                   15113:        GetCurrentConsoleFont(hStdout, FALSE, &cfi);
                   15114:        
                   15115:        int regen = min(scr_width * scr_height * 2, 0x8000);
                   15116:        text_vram_top_address = TEXT_VRAM_TOP;
                   15117:        text_vram_end_address = text_vram_top_address + regen;
                   15118:        shadow_buffer_top_address = SHADOW_BUF_TOP;
                   15119:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
                   15120:        
                   15121:        if(regen > 0x4000) {
                   15122:                regen = 0x8000;
                   15123:                vram_pages = 1;
                   15124:        } else if(regen > 0x2000) {
                   15125:                regen = 0x4000;
                   15126:                vram_pages = 2;
                   15127:        } else if(regen > 0x1000) {
                   15128:                regen = 0x2000;
                   15129:                vram_pages = 4;
                   15130:        } else {
                   15131:                regen = 0x1000;
                   15132:                vram_pages = 8;
                   15133:        }
1.1       root     15134:        
1.1.1.25  root     15135:        *(UINT16 *)(mem + 0x400) = 0x3f8; // com1 port address
                   15136:        *(UINT16 *)(mem + 0x402) = 0x2f8; // com2 port address
1.1.1.29  root     15137:        *(UINT16 *)(mem + 0x404) = 0x3e8; // com3 port address
                   15138:        *(UINT16 *)(mem + 0x406) = 0x2e8; // com4 port address
1.1.1.25  root     15139:        *(UINT16 *)(mem + 0x408) = 0x378; // lpt1 port address
1.1.1.37  root     15140:        *(UINT16 *)(mem + 0x40a) = 0x278; // lpt2 port address
                   15141:        *(UINT16 *)(mem + 0x40c) = 0x3bc; // lpt3 port address
1.1.1.32  root     15142: #ifdef EXT_BIOS_TOP
1.1.1.25  root     15143:        *(UINT16 *)(mem + 0x40e) = EXT_BIOS_TOP >> 4;
1.1.1.32  root     15144: #endif
1.1.1.26  root     15145:        *(UINT16 *)(mem + 0x410) = msdos_get_equipment();
1.1.1.33  root     15146:        *(UINT16 *)(mem + 0x413) = MEMORY_END >> 10;
1.1       root     15147:        *(UINT8  *)(mem + 0x449) = 0x03;//0x73;
1.1.1.14  root     15148:        *(UINT16 *)(mem + 0x44a) = csbi.dwSize.X;
                   15149:        *(UINT16 *)(mem + 0x44c) = regen;
1.1       root     15150:        *(UINT16 *)(mem + 0x44e) = 0;
                   15151:        *(UINT8  *)(mem + 0x450) = csbi.dwCursorPosition.X;
1.1.1.14  root     15152:        *(UINT8  *)(mem + 0x451) = csbi.dwCursorPosition.Y - scr_top;
1.1       root     15153:        *(UINT8  *)(mem + 0x460) = 7;
                   15154:        *(UINT8  *)(mem + 0x461) = 7;
                   15155:        *(UINT8  *)(mem + 0x462) = 0;
                   15156:        *(UINT16 *)(mem + 0x463) = 0x3d4;
1.1.1.19  root     15157:        *(UINT8  *)(mem + 0x465) = 0x09;
                   15158:        *(UINT32 *)(mem + 0x46c) = get_ticks_since_midnight(timeGetTime());
1.1.1.14  root     15159:        *(UINT8  *)(mem + 0x484) = csbi.srWindow.Bottom - csbi.srWindow.Top;
                   15160:        *(UINT8  *)(mem + 0x485) = cfi.dwFontSize.Y;
                   15161:        *(UINT8  *)(mem + 0x487) = 0x60;
                   15162:        *(UINT8  *)(mem + 0x496) = 0x10; // enhanced keyboard installed
1.1.1.32  root     15163: #ifdef EXT_BIOS_TOP
1.1.1.25  root     15164:        *(UINT16 *)(mem + EXT_BIOS_TOP) = 1;
1.1.1.32  root     15165: #endif
1.1.1.14  root     15166:        
                   15167:        // initial screen
                   15168:        SMALL_RECT rect;
                   15169:        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   15170:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   15171:        for(int y = 0, ofs1 = TEXT_VRAM_TOP, ofs2 = SHADOW_BUF_TOP; y < scr_height; y++) {
                   15172:                for(int x = 0; x < scr_width; x++) {
                   15173:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Char.AsciiChar;
                   15174:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Attributes;
                   15175:                }
                   15176:        }
1.1       root     15177:        
1.1.1.19  root     15178:        // init mcb
1.1       root     15179:        int seg = MEMORY_TOP >> 4;
1.1.1.19  root     15180:        
                   15181:        // iret table
                   15182:        // note: int 2eh vector should address the routine in command.com,
                   15183:        // and some softwares invite (int 2eh vector segment) - 1 must address the mcb of command.com.
                   15184:        // so move iret table into allocated memory block
                   15185:        // http://www5c.biglobe.ne.jp/~ecb/assembler2/2_6.html
1.1.1.33  root     15186:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, IRET_SIZE >> 4);
1.1.1.19  root     15187:        IRET_TOP = seg << 4;
                   15188:        seg += IRET_SIZE >> 4;
1.1.1.25  root     15189:        memset(mem + IRET_TOP, 0xcf, IRET_SIZE); // iret
1.1.1.19  root     15190:        
                   15191:        // dummy xms/ems device
1.1.1.33  root     15192:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, XMS_SIZE >> 4);
1.1.1.19  root     15193:        XMS_TOP = seg << 4;
                   15194:        seg += XMS_SIZE >> 4;
                   15195:        
                   15196:        // environment
1.1.1.33  root     15197:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, ENV_SIZE >> 4);
1.1       root     15198:        int env_seg = seg;
                   15199:        int ofs = 0;
1.1.1.32  root     15200:        char env_append[ENV_SIZE] = {0}, append_added = 0;
                   15201:        char comspec_added = 0;
1.1.1.33  root     15202:        char lastdrive_added = 0;
1.1.1.32  root     15203:        char env_msdos_path[ENV_SIZE] = {0};
                   15204:        char env_path[ENV_SIZE] = {0}, path_added = 0;
1.1.1.33  root     15205:        char prompt_added = 0;
1.1.1.32  root     15206:        char env_temp[ENV_SIZE] = {0}, temp_added = 0, tmp_added = 0;
1.1.1.33  root     15207:        char tz_added = 0;
1.1.1.32  root     15208:        char *path, *short_path;
                   15209:        
                   15210:        if((path = getenv("MSDOS_APPEND")) != NULL) {
                   15211:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   15212:                        strcpy(env_append, short_path);
                   15213:                }
                   15214:        }
                   15215:        if((path = getenv("APPEND")) != NULL) {
                   15216:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   15217:                        if(env_append[0] != '\0') {
                   15218:                                strcat(env_append, ";");
                   15219:                        }
                   15220:                        strcat(env_append, short_path);
                   15221:                }
                   15222:        }
                   15223:        
                   15224:        if((path = msdos_search_command_com(argv[0], env_path)) != NULL) {
                   15225:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   15226:                        strcpy(comspec_path, short_path);
                   15227:                }
                   15228:        }
                   15229:        if((path = getenv("MSDOS_COMSPEC")) != NULL && _access(path, 0) == 0) {
                   15230:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   15231:                        strcpy(comspec_path, short_path);
                   15232:                }
                   15233:        }
1.1       root     15234:        
1.1.1.28  root     15235:        if((path = getenv("MSDOS_PATH")) != NULL) {
1.1.1.32  root     15236:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   15237:                        strcpy(env_msdos_path, short_path);
                   15238:                        strcpy(env_path, short_path);
1.1.1.14  root     15239:                }
                   15240:        }
1.1.1.28  root     15241:        if((path = getenv("PATH")) != NULL) {
1.1.1.32  root     15242:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   15243:                        if(env_path[0] != '\0') {
                   15244:                                strcat(env_path, ";");
                   15245:                        }
                   15246:                        strcat(env_path, short_path);
1.1.1.9   root     15247:                }
                   15248:        }
1.1.1.32  root     15249:        
                   15250:        if(GetTempPath(ENV_SIZE, env_temp) != 0) {
                   15251:                strcpy(env_temp, msdos_get_multiple_short_path(env_temp));
1.1.1.15  root     15252:        }
1.1.1.32  root     15253:        for(int i = 0; i < 4; i++) {
                   15254:                static const char *name[4] = {"MSDOS_TEMP", "MSDOS_TMP", "TEMP", "TMP"};
                   15255:                if((path = getenv(name[i])) != NULL && _access(path, 0) == 0) {
                   15256:                        if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   15257:                                strcpy(env_temp, short_path);
                   15258:                                break;
                   15259:                        }
                   15260:                }
1.1.1.24  root     15261:        }
1.1.1.32  root     15262:        
1.1.1.9   root     15263:        for(char **p = envp; p != NULL && *p != NULL; p++) {
1.1       root     15264:                // lower to upper
1.1.1.28  root     15265:                char tmp[ENV_SIZE], name[ENV_SIZE];
1.1       root     15266:                strcpy(tmp, *p);
                   15267:                for(int i = 0;; i++) {
                   15268:                        if(tmp[i] == '=') {
                   15269:                                tmp[i] = '\0';
                   15270:                                sprintf(name, ";%s;", tmp);
1.1.1.25  root     15271:                                my_strupr(name);
1.1       root     15272:                                tmp[i] = '=';
                   15273:                                break;
                   15274:                        } else if(tmp[i] >= 'a' && tmp[i] <= 'z') {
1.1.1.28  root     15275:                                tmp[i] = (tmp[i] - 'a') + 'A';
1.1       root     15276:                        }
                   15277:                }
1.1.1.33  root     15278:                if(strstr(";MSDOS_APPEND;MSDOS_COMSPEC;MSDOS_LASTDRIVE;MSDOS_TEMP;MSDOS_TMP;MSDOS_TZ;", name) != NULL) {
                   15279:                        // ignore MSDOS_(APPEND/COMSPEC/LASTDRIVE/TEMP/TMP/TZ)
                   15280:                } else if(standard_env && strstr(";APPEND;COMSPEC;LASTDRIVE;MSDOS_PATH;PATH;PROMPT;TEMP;TMP;TZ;", name) == NULL) {
1.1.1.18  root     15281:                        // ignore non standard environments
                   15282:                } else {
1.1.1.33  root     15283:                        if(strncmp(tmp, "APPEND=", 7) == 0) {
1.1.1.32  root     15284:                                if(env_append[0] != '\0') {
                   15285:                                        sprintf(tmp, "APPEND=%s", env_append);
                   15286:                                } else {
                   15287:                                        sprintf(tmp, "APPEND=%s", msdos_get_multiple_short_path(tmp + 7));
                   15288:                                }
                   15289:                                append_added = 1;
                   15290:                        } else if(strncmp(tmp, "COMSPEC=", 8) == 0) {
1.1.1.14  root     15291:                                strcpy(tmp, "COMSPEC=C:\\COMMAND.COM");
1.1.1.32  root     15292:                                comspec_added = 1;
1.1.1.33  root     15293:                        } else if(strncmp(tmp, "LASTDRIVE=", 10) == 0) {
                   15294:                                char *env = getenv("MSDOS_LASTDRIVE");
                   15295:                                if(env != NULL) {
                   15296:                                        sprintf(tmp, "LASTDRIVE=%s", env);
                   15297:                                }
                   15298:                                lastdrive_added = 1;
                   15299:                        } else if(strncmp(tmp, "MSDOS_PATH=", 11) == 0) {
1.1.1.28  root     15300:                                if(env_msdos_path[0] != '\0') {
                   15301:                                        sprintf(tmp, "MSDOS_PATH=%s", env_msdos_path);
                   15302:                                } else {
                   15303:                                        sprintf(tmp, "MSDOS_PATH=%s", msdos_get_multiple_short_path(tmp + 11));
                   15304:                                }
1.1.1.33  root     15305:                        } else if(strncmp(tmp, "PATH=", 5) == 0) {
1.1.1.28  root     15306:                                if(env_path[0] != '\0') {
                   15307:                                        sprintf(tmp, "PATH=%s", env_path);
                   15308:                                } else {
                   15309:                                        sprintf(tmp, "PATH=%s", msdos_get_multiple_short_path(tmp + 5));
                   15310:                                }
1.1.1.32  root     15311:                                path_added = 1;
1.1.1.33  root     15312:                        } else if(strncmp(tmp, "PROMPT=", 7) == 0) {
                   15313:                                prompt_added = 1;
1.1.1.28  root     15314:                        } else if(strncmp(tmp, "TEMP=", 5) == 0) {
                   15315:                                if(env_temp[0] != '\0') {
                   15316:                                        sprintf(tmp, "TEMP=%s", env_temp);
                   15317:                                } else {
                   15318:                                        sprintf(tmp, "TEMP=%s", msdos_get_multiple_short_path(tmp + 5));
                   15319:                                }
1.1.1.32  root     15320:                                temp_added = 1;
1.1.1.33  root     15321:                        } else if(strncmp(tmp, "TMP=", 4) == 0) {
1.1.1.28  root     15322:                                if(env_temp[0] != '\0') {
                   15323:                                        sprintf(tmp, "TMP=%s", env_temp);
                   15324:                                } else {
                   15325:                                        sprintf(tmp, "TMP=%s", msdos_get_multiple_short_path(tmp + 4));
1.1       root     15326:                                }
1.1.1.32  root     15327:                                tmp_added = 1;
1.1.1.33  root     15328:                        } else if(strncmp(tmp, "TZ=", 3) == 0) {
                   15329:                                char *env = getenv("MSDOS_TZ");
                   15330:                                if(env != NULL) {
                   15331:                                        sprintf(tmp, "TZ=%s", env);
                   15332:                                }
                   15333:                                tz_added = 1;
1.1       root     15334:                        }
                   15335:                        int len = strlen(tmp);
1.1.1.14  root     15336:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) {
1.1       root     15337:                                fatalerror("too many environments\n");
                   15338:                        }
                   15339:                        memcpy(mem + (seg << 4) + ofs, tmp, len);
                   15340:                        ofs += len + 1;
                   15341:                }
                   15342:        }
1.1.1.32  root     15343:        if(!append_added && env_append[0] != '\0') {
                   15344:                #define SET_ENV(name, value) { \
                   15345:                        char tmp[ENV_SIZE]; \
                   15346:                        sprintf(tmp, "%s=%s", name, value); \
                   15347:                        int len = strlen(tmp); \
                   15348:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) { \
                   15349:                                fatalerror("too many environments\n"); \
                   15350:                        } \
                   15351:                        memcpy(mem + (seg << 4) + ofs, tmp, len); \
                   15352:                        ofs += len + 1; \
                   15353:                }
                   15354:                SET_ENV("APPEND", env_append);
                   15355:        }
                   15356:        if(!comspec_added) {
                   15357:                SET_ENV("COMSPEC", "C:\\COMMAND.COM");
                   15358:        }
1.1.1.33  root     15359:        if(!lastdrive_added) {
                   15360:                SET_ENV("LASTDRIVE", "Z");
                   15361:        }
1.1.1.32  root     15362:        if(!path_added) {
                   15363:                SET_ENV("PATH", env_path);
                   15364:        }
1.1.1.33  root     15365:        if(!prompt_added) {
                   15366:                SET_ENV("PROMPT", "$P$G");
                   15367:        }
1.1.1.32  root     15368:        if(!temp_added) {
                   15369:                SET_ENV("TEMP", env_temp);
                   15370:        }
                   15371:        if(!tmp_added) {
                   15372:                SET_ENV("TMP", env_temp);
                   15373:        }
1.1.1.33  root     15374:        if(!tz_added) {
                   15375:                TIME_ZONE_INFORMATION tzi;
                   15376:                HKEY hKey, hSubKey;
                   15377:                char tzi_std_name[64];
                   15378:                char tz_std[8] = "GMT";
                   15379:                char tz_dlt[8] = "GST";
                   15380:                char tz_value[32];
                   15381:                
                   15382:                // timezone name from GetTimeZoneInformation may not be english
                   15383:                bool daylight = (GetTimeZoneInformation(&tzi) != TIME_ZONE_ID_UNKNOWN);
                   15384:                setlocale(LC_CTYPE, "");
                   15385:                wcstombs(tzi_std_name, tzi.StandardName, sizeof(tzi_std_name));
                   15386:                
                   15387:                // get english timezone name from registry
                   15388:                if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones", 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
                   15389:                        for(DWORD i = 0; !tz_added; i++) {
                   15390:                                char reg_name[256], sub_key[1024], std_name[256];
                   15391:                                DWORD size;
                   15392:                                FILETIME ftTime;
                   15393:                                LONG result = RegEnumKeyEx(hKey, i, reg_name, &(size = array_length(reg_name)), NULL, NULL, NULL, &ftTime);
                   15394:                                
                   15395:                                if(result == ERROR_SUCCESS) {
                   15396:                                        sprintf(sub_key, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\%s", reg_name);
                   15397:                                        if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, sub_key, 0, KEY_QUERY_VALUE, &hSubKey) == ERROR_SUCCESS) {
                   15398:                                                if(RegQueryValueEx(hSubKey, "Std", NULL, NULL, (LPBYTE)std_name, &(size = array_length(std_name))) == ERROR_SUCCESS) {
                   15399:                                                        // search english timezone name from table
1.1.1.37  root     15400:                                                        if(strcmp(std_name, tzi_std_name) == 0) {
1.1.1.33  root     15401:                                                                for(int j = 0; j < array_length(tz_table); j++) {
                   15402:                                                                        if(strcmp(reg_name, tz_table[j].name) == 0 && (tz_table[j].lcid == 0 || tz_table[j].lcid == GetUserDefaultLCID())) {
                   15403:                                                                                if(tz_table[j].std != NULL) {
                   15404:                                                                                        strcpy(tz_std, tz_table[j].std);
                   15405:                                                                                }
                   15406:                                                                                if(tz_table[j].dlt != NULL) {
                   15407:                                                                                        strcpy(tz_dlt, tz_table[j].dlt);
                   15408:                                                                                }
                   15409:                                                                                tz_added = 1;
                   15410:                                                                                break;
                   15411:                                                                        }
                   15412:                                                                }
                   15413:                                                        }
                   15414:                                                }
                   15415:                                                RegCloseKey(hSubKey);
                   15416:                                        }
                   15417:                                } else if(result == ERROR_NO_MORE_ITEMS) {
                   15418:                                        break;
                   15419:                                }
                   15420:                        }
                   15421:                        RegCloseKey(hKey);
                   15422:                }
                   15423:                if((tzi.Bias % 60) != 0) {
                   15424:                        sprintf(tz_value, "%s%d:%2d", tz_std, tzi.Bias / 60, abs(tzi.Bias % 60));
                   15425:                } else {
                   15426:                        sprintf(tz_value, "%s%d", tz_std, tzi.Bias / 60);
                   15427:                }
                   15428:                if(daylight) {
                   15429:                        strcat(tz_value, tz_dlt);
                   15430:                }
                   15431:                SET_ENV("TZ", tz_value);
                   15432:        }
1.1       root     15433:        seg += (ENV_SIZE >> 4);
                   15434:        
                   15435:        // psp
1.1.1.33  root     15436:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, PSP_SIZE >> 4);
1.1       root     15437:        current_psp = seg;
1.1.1.35  root     15438:        psp_t *psp = msdos_psp_create(seg, seg + (PSP_SIZE >> 4), -1, env_seg);
                   15439:        psp->parent_psp = current_psp;
1.1       root     15440:        seg += (PSP_SIZE >> 4);
                   15441:        
1.1.1.19  root     15442:        // first free mcb in conventional memory
1.1.1.33  root     15443:        msdos_mcb_create(seg, 'M', 0, (MEMORY_END >> 4) - seg - 2);
1.1.1.8   root     15444:        first_mcb = seg;
                   15445:        
1.1.1.19  root     15446:        // dummy mcb to link to umb
1.1.1.33  root     15447: #if 0
                   15448:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4)); // link umb
                   15449: #else
                   15450:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0); // unlink umb
                   15451: #endif
1.1.1.19  root     15452:        
                   15453:        // first free mcb in upper memory block
1.1.1.8   root     15454:        msdos_mcb_create(UMB_TOP >> 4, 'Z', 0, (UMB_END >> 4) - (UMB_TOP >> 4) - 1);
1.1       root     15455:        
1.1.1.29  root     15456: #ifdef SUPPORT_HMA
                   15457:        // first free mcb in high memory area
                   15458:        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   15459: #endif
                   15460:        
1.1.1.26  root     15461:        // interrupt vector
                   15462:        for(int i = 0; i < 0x80; i++) {
                   15463:                *(UINT16 *)(mem + 4 * i + 0) = i;
                   15464:                *(UINT16 *)(mem + 4 * i + 2) = (IRET_TOP >> 4);
                   15465:        }
1.1.1.35  root     15466:        *(UINT16 *)(mem + 4 * 0x08 + 0) = 0x0018;       // fffd:0018 irq0 (system timer)
1.1.1.26  root     15467:        *(UINT16 *)(mem + 4 * 0x08 + 2) = 0xfffd;
                   15468:        *(UINT16 *)(mem + 4 * 0x22 + 0) = 0x0000;       // ffff:0000 boot
                   15469:        *(UINT16 *)(mem + 4 * 0x22 + 2) = 0xffff;
                   15470:        *(UINT16 *)(mem + 4 * 0x67 + 0) = 0x0012;       // xxxx:0012 ems
                   15471:        *(UINT16 *)(mem + 4 * 0x67 + 2) = XMS_TOP >> 4;
                   15472:        *(UINT16 *)(mem + 4 * 0x74 + 0) = 0x0000;       // fffd:0000 irq12 (mouse)
                   15473:        *(UINT16 *)(mem + 4 * 0x74 + 2) = 0xfffd;
                   15474:        
1.1.1.29  root     15475:        // dummy devices (NUL -> CON -> ... -> CONFIG$ -> EMMXXXX0)
1.1.1.26  root     15476:        static const struct {
                   15477:                UINT16 attributes;
                   15478:                char *dev_name;
                   15479:        } dummy_devices[] = {
                   15480:                {0x8013, "CON     "},
                   15481:                {0x8000, "AUX     "},
                   15482:                {0xa0c0, "PRN     "},
                   15483:                {0x8008, "CLOCK$  "},
                   15484:                {0x8000, "COM1    "},
                   15485:                {0xa0c0, "LPT1    "},
                   15486:                {0xa0c0, "LPT2    "},
                   15487:                {0xa0c0, "LPT3    "},
                   15488:                {0x8000, "COM2    "},
                   15489:                {0x8000, "COM3    "},
                   15490:                {0x8000, "COM4    "},
1.1.1.30  root     15491: //             {0xc000, "CONFIG$ "},
                   15492:                {0xc000, "$IBMADSP"}, // for windows3.1 setup.exe
1.1.1.26  root     15493:        };
                   15494:        static const UINT8 dummy_device_routine[] = {
                   15495:                // from NUL device of Windows 98 SE
                   15496:                // or word ptr ES:[BX+03],0100
                   15497:                0x26, 0x81, 0x4f, 0x03, 0x00, 0x01,
                   15498:                // retf
                   15499:                0xcb,
                   15500:        };
1.1.1.29  root     15501:        device_t *last = NULL;
1.1.1.32  root     15502:        for(int i = 0; i < array_length(dummy_devices); i++) {
1.1.1.26  root     15503:                device_t *device = (device_t *)(mem + DEVICE_TOP + 22 + 18 * i);
1.1.1.29  root     15504:                device->next_driver.w.l = 22 + 18 * (i + 1);
                   15505:                device->next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     15506:                device->attributes = dummy_devices[i].attributes;
1.1.1.29  root     15507:                device->strategy = DEVICE_SIZE - sizeof(dummy_device_routine);
                   15508:                device->interrupt = DEVICE_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.26  root     15509:                memcpy(device->dev_name, dummy_devices[i].dev_name, 8);
1.1.1.29  root     15510:                last = device;
                   15511:        }
                   15512:        if(last != NULL) {
                   15513:                last->next_driver.w.l = 0;
                   15514:                last->next_driver.w.h = XMS_TOP >> 4;
1.1.1.26  root     15515:        }
1.1.1.29  root     15516:        memcpy(mem + DEVICE_TOP + DEVICE_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.26  root     15517:        
1.1.1.25  root     15518:        // dos info
                   15519:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   15520:        dos_info->magic_word = 1;
                   15521:        dos_info->first_mcb = MEMORY_TOP >> 4;
                   15522:        dos_info->first_dpb.w.l = 0;
                   15523:        dos_info->first_dpb.w.h = DPB_TOP >> 4;
                   15524:        dos_info->first_sft.w.l = 0;
                   15525:        dos_info->first_sft.w.h = SFT_TOP >> 4;
1.1.1.26  root     15526:        dos_info->clock_device.w.l = 22 + 18 * 3;       // CLOCK$ is the 3rd device in IO.SYS
1.1.1.25  root     15527:        dos_info->clock_device.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     15528:        dos_info->con_device.w.l = 22 + 18 * 0;         // CON is the 1st device in IO.SYS
1.1.1.25  root     15529:        dos_info->con_device.w.h = DEVICE_TOP >> 4;
                   15530:        dos_info->max_sector_len = 512;
                   15531:        dos_info->disk_buf_info.w.l = offsetof(dos_info_t, disk_buf_heads);
                   15532:        dos_info->disk_buf_info.w.h = DOS_INFO_TOP >> 4;
                   15533:        dos_info->cds.w.l = 0;
                   15534:        dos_info->cds.w.h = CDS_TOP >> 4;
                   15535:        dos_info->fcb_table.w.l = 0;
                   15536:        dos_info->fcb_table.w.h = FCB_TABLE_TOP >> 4;
                   15537:        dos_info->last_drive = 'Z' - 'A' + 1;
                   15538:        dos_info->buffers_x = 20;
                   15539:        dos_info->buffers_y = 0;
                   15540:        dos_info->boot_drive = 'C' - 'A' + 1;
1.1.1.29  root     15541:        dos_info->nul_device.next_driver.w.l = 22;
                   15542:        dos_info->nul_device.next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.25  root     15543:        dos_info->nul_device.attributes = 0x8004;
1.1.1.29  root     15544:        dos_info->nul_device.strategy = DOS_INFO_SIZE - sizeof(dummy_device_routine);
                   15545:        dos_info->nul_device.interrupt = DOS_INFO_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     15546:        memcpy(dos_info->nul_device.dev_name, "NUL     ", 8);
                   15547:        dos_info->disk_buf_heads.w.l = 0;
                   15548:        dos_info->disk_buf_heads.w.h = DISK_BUF_TOP >> 4;
                   15549:        dos_info->first_umb_fcb = UMB_TOP >> 4;
                   15550:        dos_info->first_mcb_2 = MEMORY_TOP >> 4;
1.1.1.29  root     15551:        memcpy(mem + DOS_INFO_TOP + DOS_INFO_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.25  root     15552:        
                   15553:        char *env;
                   15554:        if((env = getenv("LASTDRIVE")) != NULL) {
                   15555:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   15556:                        dos_info->last_drive = env[0] - 'A' + 1;
                   15557:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   15558:                        dos_info->last_drive = env[0] - 'a' + 1;
                   15559:                }
                   15560:        }
                   15561:        if((env = getenv("windir")) != NULL) {
                   15562:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   15563:                        dos_info->boot_drive = env[0] - 'A' + 1;
                   15564:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   15565:                        dos_info->boot_drive = env[0] - 'a' + 1;
                   15566:                }
                   15567:        }
                   15568: #if defined(HAS_I386)
                   15569:        dos_info->i386_or_later = 1;
                   15570: #else
                   15571:        dos_info->i386_or_later = 0;
                   15572: #endif
                   15573:        dos_info->ext_mem_size = (min(MAX_MEM, 0x4000000) - 0x100000) >> 10;
                   15574:        
1.1.1.27  root     15575:        // ems (int 67h) and xms
1.1.1.25  root     15576:        device_t *xms_device = (device_t *)(mem + XMS_TOP);
                   15577:        xms_device->next_driver.w.l = 0xffff;
                   15578:        xms_device->next_driver.w.h = 0xffff;
                   15579:        xms_device->attributes = 0xc000;
1.1.1.29  root     15580:        xms_device->strategy = XMS_SIZE - sizeof(dummy_device_routine);
                   15581:        xms_device->interrupt = XMS_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     15582:        memcpy(xms_device->dev_name, "EMMXXXX0", 8);
                   15583:        
1.1.1.26  root     15584:        mem[XMS_TOP + 0x12] = 0xcd;     // int 68h (dummy)
                   15585:        mem[XMS_TOP + 0x13] = 0x68;
                   15586:        mem[XMS_TOP + 0x14] = 0xcf;     // iret
1.1.1.19  root     15587: #ifdef SUPPORT_XMS
                   15588:        if(support_xms) {
1.1.1.26  root     15589:                mem[XMS_TOP + 0x15] = 0xcd;     // int 69h (dummy)
                   15590:                mem[XMS_TOP + 0x16] = 0x69;
                   15591:                mem[XMS_TOP + 0x17] = 0xcb;     // retf
1.1.1.19  root     15592:        } else
                   15593: #endif
1.1.1.26  root     15594:        mem[XMS_TOP + 0x15] = 0xcb;     // retf
1.1.1.29  root     15595:        memcpy(mem + XMS_TOP + XMS_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.19  root     15596:        
1.1.1.26  root     15597:        // irq12 routine (mouse)
1.1.1.24  root     15598:        mem[0xfffd0 + 0x00] = 0xcd;     // int 6ah (dummy)
                   15599:        mem[0xfffd0 + 0x01] = 0x6a;
                   15600:        mem[0xfffd0 + 0x02] = 0x9a;     // call far mouse
                   15601:        mem[0xfffd0 + 0x03] = 0xff;
                   15602:        mem[0xfffd0 + 0x04] = 0xff;
                   15603:        mem[0xfffd0 + 0x05] = 0xff;
                   15604:        mem[0xfffd0 + 0x06] = 0xff;
                   15605:        mem[0xfffd0 + 0x07] = 0xcd;     // int 6bh (dummy)
                   15606:        mem[0xfffd0 + 0x08] = 0x6b;
                   15607:        mem[0xfffd0 + 0x09] = 0xcf;     // iret
                   15608:        
1.1.1.27  root     15609:        // case map routine
                   15610:        mem[0xfffd0 + 0x0a] = 0xcd;     // int 6ch (dummy)
                   15611:        mem[0xfffd0 + 0x0b] = 0x6c;
                   15612:        mem[0xfffd0 + 0x0c] = 0xcb;     // retf
                   15613:        
                   15614:        // font read routine
                   15615:        mem[0xfffd0 + 0x0d] = 0xcd;     // int 6dh (dummy)
                   15616:        mem[0xfffd0 + 0x0e] = 0x6d;
                   15617:        mem[0xfffd0 + 0x0f] = 0xcb;     // retf
1.1.1.19  root     15618:        
1.1.1.32  root     15619:        // error message read routine
                   15620:        mem[0xfffd0 + 0x10] = 0xcd;     // int 6eh (dummy)
                   15621:        mem[0xfffd0 + 0x11] = 0x6e;
                   15622:        mem[0xfffd0 + 0x12] = 0xcb;     // retf
                   15623:        
1.1.1.35  root     15624:        // dummy loop to wait BIOS/DOS service is done
                   15625:        mem[0xfffd0 + 0x13] = 0xe6;     // out f7h, al
                   15626:        mem[0xfffd0 + 0x14] = 0xf7;
                   15627:        mem[0xfffd0 + 0x15] = 0x78;     // js/jns -4
                   15628:        mem[0xfffd0 + 0x16] = 0xfc;
                   15629:        mem[0xfffd0 + 0x17] = 0xcb;     // retf
                   15630:        
1.1.1.26  root     15631:        // irq0 routine (system time)
1.1.1.35  root     15632:        mem[0xfffd0 + 0x18] = 0xcd;     // int 1ch
                   15633:        mem[0xfffd0 + 0x19] = 0x1c;
                   15634:        mem[0xfffd0 + 0x1a] = 0xea;     // jmp far (IRET_TOP >> 4):0008
                   15635:        mem[0xfffd0 + 0x1b] = 0x08;
                   15636:        mem[0xfffd0 + 0x1c] = 0x00;
                   15637:        mem[0xfffd0 + 0x1d] = ((IRET_TOP >> 4)     ) & 0xff;
                   15638:        mem[0xfffd0 + 0x1e] = ((IRET_TOP >> 4) >> 8) & 0xff;
1.1.1.14  root     15639:        
1.1.1.26  root     15640:        // boot routine
1.1       root     15641:        mem[0xffff0] = 0xf4;    // halt
                   15642:        mem[0xffff1] = 0xcd;    // int 21h
                   15643:        mem[0xffff2] = 0x21;
                   15644:        mem[0xffff3] = 0xcb;    // retf
                   15645:        
1.1.1.24  root     15646:        mem[0xffff5] = '0';     // rom date
                   15647:        mem[0xffff6] = '2';
                   15648:        mem[0xffff7] = '/';
                   15649:        mem[0xffff8] = '2';
                   15650:        mem[0xffff9] = '2';
                   15651:        mem[0xffffa] = '/';
                   15652:        mem[0xffffb] = '0';
                   15653:        mem[0xffffc] = '6';
                   15654:        mem[0xffffe] = 0xfc;    // machine id
                   15655:        mem[0xfffff] = 0x00;
                   15656:        
1.1       root     15657:        // param block
                   15658:        // + 0: param block (22bytes)
                   15659:        // +24: fcb1/2 (20bytes)
                   15660:        // +44: command tail (128bytes)
                   15661:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   15662:        param->env_seg = 0;
                   15663:        param->cmd_line.w.l = 44;
                   15664:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   15665:        param->fcb1.w.l = 24;
                   15666:        param->fcb1.w.h = (WORK_TOP >> 4);
                   15667:        param->fcb2.w.l = 24;
                   15668:        param->fcb2.w.h = (WORK_TOP >> 4);
                   15669:        
                   15670:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   15671:        
                   15672:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   15673:        if(argc > 1) {
                   15674:                sprintf(cmd_line->cmd, " %s", argv[1]);
                   15675:                for(int i = 2; i < argc; i++) {
                   15676:                        char tmp[128];
                   15677:                        sprintf(tmp, "%s %s", cmd_line->cmd, argv[i]);
                   15678:                        strcpy(cmd_line->cmd, tmp);
                   15679:                }
                   15680:                cmd_line->len = (UINT8)strlen(cmd_line->cmd);
                   15681:        } else {
                   15682:                cmd_line->len = 0;
                   15683:        }
                   15684:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   15685:        
                   15686:        // system file table
1.1.1.21  root     15687:        *(UINT32 *)(mem + SFT_TOP + 0) = 0xffffffff;
                   15688:        *(UINT16 *)(mem + SFT_TOP + 4) = 20;
1.1       root     15689:        
1.1.1.19  root     15690:        // disk buffer header (from DOSBox)
                   15691:        *(UINT16 *)(mem + DISK_BUF_TOP +  0) = 0xffff;          // forward ptr
                   15692:        *(UINT16 *)(mem + DISK_BUF_TOP +  2) = 0xffff;          // backward ptr
                   15693:        *(UINT8  *)(mem + DISK_BUF_TOP +  4) = 0xff;            // not in use
                   15694:        *(UINT8  *)(mem + DISK_BUF_TOP + 10) = 0x01;            // number of FATs
                   15695:        *(UINT32 *)(mem + DISK_BUF_TOP + 13) = 0xffffffff;      // pointer to DPB
                   15696:        
1.1       root     15697:        // current directory structure
                   15698:        msdos_cds_update(_getdrive() - 1);
                   15699:        
                   15700:        // fcb table
                   15701:        *(UINT32 *)(mem + FCB_TABLE_TOP + 0) = 0xffffffff;
1.1.1.14  root     15702:        *(UINT16 *)(mem + FCB_TABLE_TOP + 4) = 0;
1.1       root     15703:        
1.1.1.17  root     15704:        // nls stuff
                   15705:        msdos_nls_tables_init();
1.1       root     15706:        
                   15707:        // execute command
1.1.1.28  root     15708:        try {
                   15709:                if(msdos_process_exec(argv[0], param, 0)) {
                   15710:                        fatalerror("'%s' not found\n", argv[0]);
                   15711:                }
                   15712:        } catch(...) {
                   15713:                // we should not reach here :-(
                   15714:                fatalerror("failed to start '%s' because of unexpected exeption\n", argv[0]);
1.1       root     15715:        }
                   15716:        retval = 0;
                   15717:        return(0);
                   15718: }
                   15719: 
                   15720: #define remove_std_file(path) { \
                   15721:        int fd = _open(path, _O_RDONLY | _O_BINARY); \
                   15722:        if(fd != -1) { \
                   15723:                _lseek(fd, 0, SEEK_END); \
                   15724:                int size = _tell(fd); \
                   15725:                _close(fd); \
                   15726:                if(size == 0) { \
                   15727:                        remove(path); \
                   15728:                } \
                   15729:        } \
                   15730: }
                   15731: 
                   15732: void msdos_finish()
                   15733: {
                   15734:        for(int i = 0; i < MAX_FILES; i++) {
                   15735:                if(file_handler[i].valid) {
                   15736:                        _close(i);
                   15737:                }
                   15738:        }
1.1.1.21  root     15739: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     15740:        remove_std_file("stdaux.txt");
1.1.1.21  root     15741: #endif
1.1.1.30  root     15742: #ifdef SUPPORT_XMS
                   15743:        msdos_xms_finish();
                   15744: #endif
1.1       root     15745:        msdos_dbcs_table_finish();
                   15746: }
                   15747: 
                   15748: /* ----------------------------------------------------------------------------
                   15749:        PC/AT hardware emulation
                   15750: ---------------------------------------------------------------------------- */
                   15751: 
                   15752: void hardware_init()
                   15753: {
1.1.1.3   root     15754:        CPU_INIT_CALL(CPU_MODEL);
1.1       root     15755:        CPU_RESET_CALL(CPU_MODEL);
1.1.1.14  root     15756:        m_IF = 1;
1.1.1.3   root     15757: #if defined(HAS_I386)
1.1       root     15758:        cpu_type = (REG32(EDX) >> 8) & 0x0f;
                   15759:        cpu_step = (REG32(EDX) >> 0) & 0x0f;
1.1.1.3   root     15760: #endif
                   15761:        i386_set_a20_line(0);
1.1.1.14  root     15762:        
1.1.1.19  root     15763:        ems_init();
1.1.1.25  root     15764:        dma_init();
1.1       root     15765:        pic_init();
1.1.1.25  root     15766:        pio_init();
1.1.1.8   root     15767: #ifdef PIT_ALWAYS_RUNNING
                   15768:        pit_init();
                   15769: #else
1.1       root     15770:        pit_active = 0;
                   15771: #endif
1.1.1.25  root     15772:        sio_init();
1.1.1.8   root     15773:        cmos_init();
                   15774:        kbd_init();
1.1       root     15775: }
                   15776: 
1.1.1.10  root     15777: void hardware_finish()
                   15778: {
                   15779: #if defined(HAS_I386)
                   15780:        vtlb_free(m_vtlb);
                   15781: #endif
1.1.1.19  root     15782:        ems_finish();
1.1.1.37  root     15783:        pio_finish();
1.1.1.25  root     15784:        sio_finish();
1.1.1.10  root     15785: }
                   15786: 
1.1.1.28  root     15787: void hardware_release()
                   15788: {
                   15789:        // release hardware resources when this program will be terminated abnormally
                   15790: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     15791:        if(fp_debug_log != NULL) {
                   15792:                fclose(fp_debug_log);
                   15793:                fp_debug_log = NULL;
1.1.1.28  root     15794:        }
                   15795: #endif
                   15796: #if defined(HAS_I386)
                   15797:        vtlb_free(m_vtlb);
                   15798: #endif
                   15799:        ems_release();
1.1.1.37  root     15800:        pio_release();
1.1.1.28  root     15801:        sio_release();
                   15802: }
                   15803: 
1.1       root     15804: void hardware_run()
                   15805: {
1.1.1.22  root     15806: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.28  root     15807:        // open debug log file after msdos_init() is done not to use the standard file handlers
1.1.1.33  root     15808:        fp_debug_log = fopen("debug.log", "w");
1.1.1.22  root     15809: #endif
1.1.1.3   root     15810:        while(!m_halted) {
                   15811: #if defined(HAS_I386)
1.1       root     15812:                CPU_EXECUTE_CALL(i386);
1.1.1.14  root     15813:                if(m_eip != m_prev_eip) {
1.1.1.35  root     15814:                        idle_ops++;
                   15815:                }
1.1.1.14  root     15816: #else
1.1.1.35  root     15817:                CPU_EXECUTE_CALL(CPU_MODEL);
1.1.1.14  root     15818:                if(m_pc != m_prevpc) {
1.1.1.35  root     15819:                        idle_ops++;
1.1.1.14  root     15820:                }
1.1.1.35  root     15821: #endif
                   15822:                if(++update_ops == UPDATE_OPS) {
1.1       root     15823:                        hardware_update();
1.1.1.35  root     15824:                        update_ops = 0;
1.1       root     15825:                }
                   15826:        }
1.1.1.22  root     15827: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     15828:        if(fp_debug_log != NULL) {
                   15829:                fclose(fp_debug_log);
                   15830:                fp_debug_log = NULL;
1.1.1.28  root     15831:        }
1.1.1.22  root     15832: #endif
1.1       root     15833: }
                   15834: 
                   15835: void hardware_update()
                   15836: {
1.1.1.8   root     15837:        static UINT32 prev_time = 0;
                   15838:        UINT32 cur_time = timeGetTime();
                   15839:        
                   15840:        if(prev_time != cur_time) {
                   15841:                // update pit and raise irq0
                   15842: #ifndef PIT_ALWAYS_RUNNING
                   15843:                if(pit_active)
                   15844: #endif
                   15845:                {
                   15846:                        if(pit_run(0, cur_time)) {
                   15847:                                pic_req(0, 0, 1);
                   15848:                        }
                   15849:                        pit_run(1, cur_time);
                   15850:                        pit_run(2, cur_time);
                   15851:                }
1.1.1.24  root     15852:                
1.1.1.25  root     15853:                // update sio and raise irq4/3
1.1.1.29  root     15854:                for(int c = 0; c < 4; c++) {
1.1.1.25  root     15855:                        sio_update(c);
                   15856:                }
                   15857:                
1.1.1.24  root     15858:                // update keyboard and mouse
1.1.1.14  root     15859:                static UINT32 prev_tick = 0;
                   15860:                UINT32 cur_tick = cur_time / 32;
1.1.1.25  root     15861:                
1.1.1.14  root     15862:                if(prev_tick != cur_tick) {
                   15863:                        // update keyboard flags
                   15864:                        UINT8 state;
1.1.1.24  root     15865:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x0001) ? 0x80 : 0;
                   15866:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x0001) ? 0x40 : 0;
                   15867:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x0001) ? 0x20 : 0;
                   15868:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x0001) ? 0x10 : 0;
                   15869:                        state |= (GetAsyncKeyState(VK_MENU    ) & 0x8000) ? 0x08 : 0;
                   15870:                        state |= (GetAsyncKeyState(VK_CONTROL ) & 0x8000) ? 0x04 : 0;
                   15871:                        state |= (GetAsyncKeyState(VK_LSHIFT  ) & 0x8000) ? 0x02 : 0;
                   15872:                        state |= (GetAsyncKeyState(VK_RSHIFT  ) & 0x8000) ? 0x01 : 0;
1.1.1.14  root     15873:                        mem[0x417] = state;
                   15874:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x8000) ? 0x80 : 0;
                   15875:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   15876:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   15877:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
1.1.1.24  root     15878: //                     state |= (GetAsyncKeyState(VK_PAUSE   ) & 0x0001) ? 0x08 : 0;
                   15879: //                     state |= (GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x04 : 0;
1.1.1.14  root     15880:                        state |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   15881:                        state |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   15882:                        mem[0x418] = state;
                   15883:                        
1.1.1.24  root     15884:                        // update console input if needed
1.1.1.34  root     15885:                        if(!key_changed || mouse.hidden == 0) {
1.1.1.24  root     15886:                                update_console_input();
                   15887:                        }
                   15888:                        
                   15889:                        // raise irq1 if key is pressed/released
                   15890:                        if(key_changed) {
1.1.1.8   root     15891:                                pic_req(0, 1, 1);
1.1.1.24  root     15892:                                key_changed = false;
                   15893:                        }
                   15894:                        
                   15895:                        // raise irq12 if mouse status is changed
                   15896:                        if(mouse.status & mouse.call_mask) {
1.1.1.34  root     15897: //                             if(mouse.hidden == 0) {
1.1.1.24  root     15898:                                        pic_req(1, 4, 1);
                   15899:                                        mouse.status_irq = mouse.status & mouse.call_mask;
1.1.1.34  root     15900: //                             }
1.1.1.24  root     15901:                                mouse.status &= ~mouse.call_mask;
1.1.1.8   root     15902:                        }
1.1.1.24  root     15903:                        
1.1.1.14  root     15904:                        prev_tick = cur_tick;
1.1.1.8   root     15905:                }
1.1.1.24  root     15906:                
1.1.1.19  root     15907:                // update daily timer counter
                   15908:                pcbios_update_daily_timer_counter(cur_time);
1.1.1.25  root     15909:                
1.1.1.8   root     15910:                prev_time = cur_time;
1.1       root     15911:        }
                   15912: }
                   15913: 
1.1.1.19  root     15914: // ems
                   15915: 
                   15916: void ems_init()
                   15917: {
                   15918:        memset(ems_handles, 0, sizeof(ems_handles));
                   15919:        memset(ems_pages, 0, sizeof(ems_pages));
                   15920:        free_ems_pages = MAX_EMS_PAGES;
                   15921: }
                   15922: 
                   15923: void ems_finish()
                   15924: {
1.1.1.28  root     15925:        ems_release();
                   15926: }
                   15927: 
                   15928: void ems_release()
                   15929: {
1.1.1.31  root     15930:        for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.28  root     15931:                if(ems_handles[i].buffer != NULL) {
1.1.1.19  root     15932:                        free(ems_handles[i].buffer);
                   15933:                        ems_handles[i].buffer = NULL;
                   15934:                }
                   15935:        }
                   15936: }
                   15937: 
                   15938: void ems_allocate_pages(int handle, int pages)
                   15939: {
                   15940:        if(pages > 0) {
                   15941:                ems_handles[handle].buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   15942:        } else {
                   15943:                ems_handles[handle].buffer = NULL;
                   15944:        }
                   15945:        ems_handles[handle].pages = pages;
                   15946:        ems_handles[handle].allocated = true;
                   15947:        free_ems_pages -= pages;
                   15948: }
                   15949: 
                   15950: void ems_reallocate_pages(int handle, int pages)
                   15951: {
                   15952:        if(ems_handles[handle].allocated) {
                   15953:                if(ems_handles[handle].pages != pages) {
                   15954:                        UINT8 *new_buffer = NULL;
                   15955:                        
                   15956:                        if(pages > 0) {
                   15957:                                new_buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   15958:                        }
1.1.1.32  root     15959:                        if(ems_handles[handle].buffer != NULL) {
                   15960:                                if(new_buffer != NULL) {
1.1.1.19  root     15961:                                        if(pages > ems_handles[handle].pages) {
                   15962:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * ems_handles[handle].pages);
                   15963:                                        } else {
                   15964:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * pages);
                   15965:                                        }
                   15966:                                }
                   15967:                                free(ems_handles[handle].buffer);
                   15968:                                ems_handles[handle].buffer = NULL;
                   15969:                        }
                   15970:                        free_ems_pages += ems_handles[handle].pages;
                   15971:                        
                   15972:                        ems_handles[handle].buffer = new_buffer;
                   15973:                        ems_handles[handle].pages = pages;
                   15974:                        free_ems_pages -= pages;
                   15975:                }
                   15976:        } else {
                   15977:                ems_allocate_pages(handle, pages);
                   15978:        }
                   15979: }
                   15980: 
                   15981: void ems_release_pages(int handle)
                   15982: {
                   15983:        if(ems_handles[handle].allocated) {
1.1.1.32  root     15984:                if(ems_handles[handle].buffer != NULL) {
1.1.1.19  root     15985:                        free(ems_handles[handle].buffer);
                   15986:                        ems_handles[handle].buffer = NULL;
                   15987:                }
                   15988:                free_ems_pages += ems_handles[handle].pages;
                   15989:                ems_handles[handle].allocated = false;
                   15990:        }
                   15991: }
                   15992: 
                   15993: void ems_map_page(int physical, int handle, int logical)
                   15994: {
                   15995:        if(ems_pages[physical].mapped) {
                   15996:                if(ems_pages[physical].handle == handle && ems_pages[physical].page == logical) {
                   15997:                        return;
                   15998:                }
                   15999:                ems_unmap_page(physical);
                   16000:        }
1.1.1.32  root     16001:        if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     16002:                memcpy(mem + EMS_TOP + 0x4000 * physical, ems_handles[handle].buffer + 0x4000 * logical, 0x4000);
                   16003:        }
                   16004:        ems_pages[physical].handle = handle;
                   16005:        ems_pages[physical].page = logical;
                   16006:        ems_pages[physical].mapped = true;
                   16007: }
                   16008: 
                   16009: void ems_unmap_page(int physical)
                   16010: {
                   16011:        if(ems_pages[physical].mapped) {
                   16012:                int handle = ems_pages[physical].handle;
                   16013:                int logical = ems_pages[physical].page;
                   16014:                
1.1.1.32  root     16015:                if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     16016:                        memcpy(ems_handles[handle].buffer + 0x4000 * logical, mem + EMS_TOP + 0x4000 * physical, 0x4000);
                   16017:                }
                   16018:                ems_pages[physical].mapped = false;
                   16019:        }
                   16020: }
                   16021: 
1.1.1.25  root     16022: // dma
1.1       root     16023: 
1.1.1.25  root     16024: void dma_init()
1.1       root     16025: {
1.1.1.26  root     16026:        memset(dma, 0, sizeof(dma));
1.1.1.25  root     16027:        for(int c = 0; c < 2; c++) {
1.1.1.26  root     16028: //             for(int ch = 0; ch < 4; ch++) {
                   16029: //                     dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w = 0xffff;
                   16030: //             }
1.1.1.25  root     16031:                dma_reset(c);
                   16032:        }
1.1       root     16033: }
                   16034: 
1.1.1.25  root     16035: void dma_reset(int c)
1.1       root     16036: {
1.1.1.25  root     16037:        dma[c].low_high = false;
                   16038:        dma[c].cmd = dma[c].req = dma[c].tc = 0;
                   16039:        dma[c].mask = 0xff;
                   16040: }
                   16041: 
                   16042: void dma_write(int c, UINT32 addr, UINT8 data)
                   16043: {
                   16044:        int ch = (addr >> 1) & 3;
                   16045:        UINT8 bit = 1 << (data & 3);
                   16046:        
                   16047:        switch(addr & 0x0f) {
                   16048:        case 0x00: case 0x02: case 0x04: case 0x06:
                   16049:                if(dma[c].low_high) {
                   16050:                        dma[c].ch[ch].bareg.b.h = data;
1.1       root     16051:                } else {
1.1.1.25  root     16052:                        dma[c].ch[ch].bareg.b.l = data;
                   16053:                }
                   16054:                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   16055:                dma[c].low_high = !dma[c].low_high;
                   16056:                break;
                   16057:        case 0x01: case 0x03: case 0x05: case 0x07:
                   16058:                if(dma[c].low_high) {
                   16059:                        dma[c].ch[ch].bcreg.b.h = data;
                   16060:                } else {
                   16061:                        dma[c].ch[ch].bcreg.b.l = data;
                   16062:                }
                   16063:                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   16064:                dma[c].low_high = !dma[c].low_high;
                   16065:                break;
                   16066:        case 0x08:
                   16067:                // command register
                   16068:                dma[c].cmd = data;
                   16069:                break;
                   16070:        case 0x09:
                   16071:                // dma[c].request register
                   16072:                if(data & 4) {
                   16073:                        if(!(dma[c].req & bit)) {
                   16074:                                dma[c].req |= bit;
                   16075: //                             dma_run(c, ch);
                   16076:                        }
                   16077:                } else {
                   16078:                        dma[c].req &= ~bit;
                   16079:                }
                   16080:                break;
                   16081:        case 0x0a:
                   16082:                // single mask register
                   16083:                if(data & 4) {
                   16084:                        dma[c].mask |= bit;
                   16085:                } else {
                   16086:                        dma[c].mask &= ~bit;
                   16087:                }
                   16088:                break;
                   16089:        case 0x0b:
                   16090:                // mode register
                   16091:                dma[c].ch[data & 3].mode = data;
                   16092:                break;
                   16093:        case 0x0c:
                   16094:                dma[c].low_high = false;
                   16095:                break;
                   16096:        case 0x0d:
                   16097:                // clear master
                   16098:                dma_reset(c);
                   16099:                break;
                   16100:        case 0x0e:
                   16101:                // clear mask register
                   16102:                dma[c].mask = 0;
                   16103:                break;
                   16104:        case 0x0f:
                   16105:                // all mask register
                   16106:                dma[c].mask = data & 0x0f;
                   16107:                break;
                   16108:        }
                   16109: }
                   16110: 
                   16111: UINT8 dma_read(int c, UINT32 addr)
                   16112: {
                   16113:        int ch = (addr >> 1) & 3;
                   16114:        UINT8 val = 0xff;
                   16115:        
                   16116:        switch(addr & 0x0f) {
                   16117:        case 0x00: case 0x02: case 0x04: case 0x06:
                   16118:                if(dma[c].low_high) {
                   16119:                        val = dma[c].ch[ch].areg.b.h;
                   16120:                } else {
                   16121:                        val = dma[c].ch[ch].areg.b.l;
                   16122:                }
                   16123:                dma[c].low_high = !dma[c].low_high;
                   16124:                return(val);
                   16125:        case 0x01: case 0x03: case 0x05: case 0x07:
                   16126:                if(dma[c].low_high) {
                   16127:                        val = dma[c].ch[ch].creg.b.h;
                   16128:                } else {
                   16129:                        val = dma[c].ch[ch].creg.b.l;
                   16130:                }
                   16131:                dma[c].low_high = !dma[c].low_high;
                   16132:                return(val);
                   16133:        case 0x08:
                   16134:                // status register
                   16135:                val = (dma[c].req << 4) | dma[c].tc;
                   16136:                dma[c].tc = 0;
                   16137:                return(val);
                   16138:        case 0x0d:
1.1.1.26  root     16139:                // temporary register (intel 82374 does not support)
1.1.1.25  root     16140:                return(dma[c].tmp & 0xff);
1.1.1.26  root     16141:        case 0x0f:
                   16142:                // mask register (intel 82374 does support)
                   16143:                return(dma[c].mask);
1.1.1.25  root     16144:        }
                   16145:        return(0xff);
                   16146: }
                   16147: 
                   16148: void dma_page_write(int c, int ch, UINT8 data)
                   16149: {
                   16150:        dma[c].ch[ch].pagereg = data;
                   16151: }
                   16152: 
                   16153: UINT8 dma_page_read(int c, int ch)
                   16154: {
                   16155:        return(dma[c].ch[ch].pagereg);
                   16156: }
                   16157: 
                   16158: void dma_run(int c, int ch)
                   16159: {
                   16160:        UINT8 bit = 1 << ch;
                   16161:        
                   16162:        if((dma[c].req & bit) && !(dma[c].mask & bit)) {
                   16163:                // execute dma
                   16164:                while(dma[c].req & bit) {
                   16165:                        if(ch == 0 && (dma[c].cmd & 0x01)) {
                   16166:                                // memory -> memory
                   16167:                                UINT32 saddr = dma[c].ch[0].areg.w | (dma[c].ch[0].pagereg << 16);
                   16168:                                UINT32 daddr = dma[c].ch[1].areg.w | (dma[c].ch[1].pagereg << 16);
                   16169:                                
                   16170:                                if(c == 0) {
                   16171:                                        dma[c].tmp = read_byte(saddr);
                   16172:                                        write_byte(daddr, dma[c].tmp);
                   16173:                                } else {
                   16174:                                        dma[c].tmp = read_word(saddr << 1);
                   16175:                                        write_word(daddr << 1, dma[c].tmp);
                   16176:                                }
                   16177:                                if(!(dma[c].cmd & 0x02)) {
                   16178:                                        if(dma[c].ch[0].mode & 0x20) {
                   16179:                                                dma[c].ch[0].areg.w--;
                   16180:                                                if(dma[c].ch[0].areg.w == 0xffff) {
                   16181:                                                        dma[c].ch[0].pagereg--;
                   16182:                                                }
                   16183:                                        } else {
                   16184:                                                dma[c].ch[0].areg.w++;
                   16185:                                                if(dma[c].ch[0].areg.w == 0) {
                   16186:                                                        dma[c].ch[0].pagereg++;
                   16187:                                                }
                   16188:                                        }
                   16189:                                }
                   16190:                                if(dma[c].ch[1].mode & 0x20) {
                   16191:                                        dma[c].ch[1].areg.w--;
                   16192:                                        if(dma[c].ch[1].areg.w == 0xffff) {
                   16193:                                                dma[c].ch[1].pagereg--;
                   16194:                                        }
                   16195:                                } else {
                   16196:                                        dma[c].ch[1].areg.w++;
                   16197:                                        if(dma[c].ch[1].areg.w == 0) {
                   16198:                                                dma[c].ch[1].pagereg++;
                   16199:                                        }
                   16200:                                }
                   16201:                                
                   16202:                                // check dma condition
                   16203:                                if(dma[c].ch[0].creg.w-- == 0) {
                   16204:                                        if(dma[c].ch[0].mode & 0x10) {
                   16205:                                                // self initialize
                   16206:                                                dma[c].ch[0].areg.w = dma[c].ch[0].bareg.w;
                   16207:                                                dma[c].ch[0].creg.w = dma[c].ch[0].bcreg.w;
                   16208:                                        } else {
                   16209: //                                             dma[c].mask |= bit;
                   16210:                                        }
                   16211:                                }
                   16212:                                if(dma[c].ch[1].creg.w-- == 0) {
                   16213:                                        // terminal count
                   16214:                                        if(dma[c].ch[1].mode & 0x10) {
                   16215:                                                // self initialize
                   16216:                                                dma[c].ch[1].areg.w = dma[c].ch[1].bareg.w;
                   16217:                                                dma[c].ch[1].creg.w = dma[c].ch[1].bcreg.w;
                   16218:                                        } else {
                   16219:                                                dma[c].mask |= bit;
                   16220:                                        }
                   16221:                                        dma[c].req &= ~bit;
                   16222:                                        dma[c].tc |= bit;
                   16223:                                }
                   16224:                        } else {
                   16225:                                UINT32 addr = dma[c].ch[ch].areg.w | (dma[c].ch[ch].pagereg << 16);
                   16226:                                
                   16227:                                if((dma[c].ch[ch].mode & 0x0c) == 0x00) {
                   16228:                                        // verify
                   16229:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x04) {
                   16230:                                        // io -> memory
                   16231:                                        if(c == 0) {
                   16232:                                                dma[c].tmp = read_io_byte(dma[c].ch[ch].port);
                   16233:                                                write_byte(addr, dma[c].tmp);
                   16234:                                        } else {
                   16235:                                                dma[c].tmp = read_io_word(dma[c].ch[ch].port);
                   16236:                                                write_word(addr << 1, dma[c].tmp);
                   16237:                                        }
                   16238:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x08) {
                   16239:                                        // memory -> io
                   16240:                                        if(c == 0) {
                   16241:                                                dma[c].tmp = read_byte(addr);
                   16242:                                                write_io_byte(dma[c].ch[ch].port, dma[c].tmp);
                   16243:                                        } else {
                   16244:                                                dma[c].tmp = read_word(addr << 1);
                   16245:                                                write_io_word(dma[c].ch[ch].port, dma[c].tmp);
                   16246:                                        }
                   16247:                                }
                   16248:                                if(dma[c].ch[ch].mode & 0x20) {
                   16249:                                        dma[c].ch[ch].areg.w--;
                   16250:                                        if(dma[c].ch[ch].areg.w == 0xffff) {
                   16251:                                                dma[c].ch[ch].pagereg--;
                   16252:                                        }
                   16253:                                } else {
                   16254:                                        dma[c].ch[ch].areg.w++;
                   16255:                                        if(dma[c].ch[ch].areg.w == 0) {
                   16256:                                                dma[c].ch[ch].pagereg++;
                   16257:                                        }
                   16258:                                }
                   16259:                                
                   16260:                                // check dma condition
                   16261:                                if(dma[c].ch[ch].creg.w-- == 0) {
                   16262:                                        // terminal count
                   16263:                                        if(dma[c].ch[ch].mode & 0x10) {
                   16264:                                                // self initialize
                   16265:                                                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   16266:                                                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   16267:                                        } else {
                   16268:                                                dma[c].mask |= bit;
                   16269:                                        }
                   16270:                                        dma[c].req &= ~bit;
                   16271:                                        dma[c].tc |= bit;
                   16272:                                } else if((dma[c].ch[ch].mode & 0xc0) == 0x40) {
                   16273:                                        // single mode
                   16274:                                        break;
                   16275:                                }
                   16276:                        }
                   16277:                }
                   16278:        }
                   16279: }
                   16280: 
                   16281: // pic
                   16282: 
                   16283: void pic_init()
                   16284: {
                   16285:        memset(pic, 0, sizeof(pic));
                   16286:        pic[0].imr = pic[1].imr = 0xff;
                   16287:        
                   16288:        // from bochs bios
                   16289:        pic_write(0, 0, 0x11);  // icw1 = 11h
                   16290:        pic_write(0, 1, 0x08);  // icw2 = 08h
                   16291:        pic_write(0, 1, 0x04);  // icw3 = 04h
                   16292:        pic_write(0, 1, 0x01);  // icw4 = 01h
                   16293:        pic_write(0, 1, 0xb8);  // ocw1 = b8h
                   16294:        pic_write(1, 0, 0x11);  // icw1 = 11h
                   16295:        pic_write(1, 1, 0x70);  // icw2 = 70h
                   16296:        pic_write(1, 1, 0x02);  // icw3 = 02h
                   16297:        pic_write(1, 1, 0x01);  // icw4 = 01h
                   16298: }
                   16299: 
                   16300: void pic_write(int c, UINT32 addr, UINT8 data)
                   16301: {
                   16302:        if(addr & 1) {
                   16303:                if(pic[c].icw2_r) {
                   16304:                        // icw2
                   16305:                        pic[c].icw2 = data;
                   16306:                        pic[c].icw2_r = 0;
                   16307:                } else if(pic[c].icw3_r) {
                   16308:                        // icw3
                   16309:                        pic[c].icw3 = data;
                   16310:                        pic[c].icw3_r = 0;
                   16311:                } else if(pic[c].icw4_r) {
                   16312:                        // icw4
                   16313:                        pic[c].icw4 = data;
                   16314:                        pic[c].icw4_r = 0;
                   16315:                } else {
                   16316:                        // ocw1
1.1       root     16317:                        pic[c].imr = data;
                   16318:                }
                   16319:        } else {
                   16320:                if(data & 0x10) {
                   16321:                        // icw1
                   16322:                        pic[c].icw1 = data;
                   16323:                        pic[c].icw2_r = 1;
                   16324:                        pic[c].icw3_r = (data & 2) ? 0 : 1;
                   16325:                        pic[c].icw4_r = data & 1;
                   16326:                        pic[c].irr = 0;
                   16327:                        pic[c].isr = 0;
                   16328:                        pic[c].imr = 0;
                   16329:                        pic[c].prio = 0;
                   16330:                        if(!(pic[c].icw1 & 1)) {
                   16331:                                pic[c].icw4 = 0;
                   16332:                        }
                   16333:                        pic[c].ocw3 = 0;
                   16334:                } else if(data & 8) {
                   16335:                        // ocw3
                   16336:                        if(!(data & 2)) {
                   16337:                                data = (data & ~1) | (pic[c].ocw3 & 1);
                   16338:                        }
                   16339:                        if(!(data & 0x40)) {
                   16340:                                data = (data & ~0x20) | (pic[c].ocw3 & 0x20);
                   16341:                        }
                   16342:                        pic[c].ocw3 = data;
                   16343:                } else {
                   16344:                        // ocw2
                   16345:                        int level = 0;
                   16346:                        if(data & 0x40) {
                   16347:                                level = data & 7;
                   16348:                        } else {
                   16349:                                if(!pic[c].isr) {
                   16350:                                        return;
                   16351:                                }
                   16352:                                level = pic[c].prio;
                   16353:                                while(!(pic[c].isr & (1 << level))) {
                   16354:                                        level = (level + 1) & 7;
                   16355:                                }
                   16356:                        }
                   16357:                        if(data & 0x80) {
                   16358:                                pic[c].prio = (level + 1) & 7;
                   16359:                        }
                   16360:                        if(data & 0x20) {
                   16361:                                pic[c].isr &= ~(1 << level);
                   16362:                        }
                   16363:                }
                   16364:        }
                   16365:        pic_update();
                   16366: }
                   16367: 
                   16368: UINT8 pic_read(int c, UINT32 addr)
                   16369: {
                   16370:        if(addr & 1) {
                   16371:                return(pic[c].imr);
                   16372:        } else {
                   16373:                // polling mode is not supported...
                   16374:                //if(pic[c].ocw3 & 4) {
                   16375:                //      return ???;
                   16376:                //}
                   16377:                if(pic[c].ocw3 & 1) {
                   16378:                        return(pic[c].isr);
                   16379:                } else {
                   16380:                        return(pic[c].irr);
                   16381:                }
                   16382:        }
                   16383: }
                   16384: 
                   16385: void pic_req(int c, int level, int signal)
                   16386: {
                   16387:        if(signal) {
                   16388:                pic[c].irr |= (1 << level);
                   16389:        } else {
                   16390:                pic[c].irr &= ~(1 << level);
                   16391:        }
                   16392:        pic_update();
                   16393: }
                   16394: 
                   16395: int pic_ack()
                   16396: {
                   16397:        // ack (INTA=L)
                   16398:        pic[pic_req_chip].isr |= pic_req_bit;
                   16399:        pic[pic_req_chip].irr &= ~pic_req_bit;
                   16400:        if(pic_req_chip > 0) {
                   16401:                // update isr and irr of master
                   16402:                UINT8 slave = 1 << (pic[pic_req_chip].icw3 & 7);
                   16403:                pic[pic_req_chip - 1].isr |= slave;
                   16404:                pic[pic_req_chip - 1].irr &= ~slave;
                   16405:        }
                   16406:        //if(pic[pic_req_chip].icw4 & 1) {
                   16407:                // 8086 mode
                   16408:                int vector = (pic[pic_req_chip].icw2 & 0xf8) | pic_req_level;
                   16409:        //} else {
                   16410:        //      // 8080 mode
                   16411:        //      UINT16 addr = (UINT16)pic[pic_req_chip].icw2 << 8;
                   16412:        //      if(pic[pic_req_chip].icw1 & 4) {
                   16413:        //              addr |= (pic[pic_req_chip].icw1 & 0xe0) | (pic_req_level << 2);
                   16414:        //      } else {
                   16415:        //              addr |= (pic[pic_req_chip].icw1 & 0xc0) | (pic_req_level << 3);
                   16416:        //      }
                   16417:        //      vector = 0xcd | (addr << 8);
                   16418:        //}
                   16419:        if(pic[pic_req_chip].icw4 & 2) {
                   16420:                // auto eoi
                   16421:                pic[pic_req_chip].isr &= ~pic_req_bit;
                   16422:        }
                   16423:        return(vector);
                   16424: }
                   16425: 
                   16426: void pic_update()
                   16427: {
                   16428:        for(int c = 0; c < 2; c++) {
                   16429:                UINT8 irr = pic[c].irr;
                   16430:                if(c + 1 < 2) {
                   16431:                        // this is master
                   16432:                        if(pic[c + 1].irr & (~pic[c + 1].imr)) {
                   16433:                                // request from slave
                   16434:                                irr |= 1 << (pic[c + 1].icw3 & 7);
                   16435:                        }
                   16436:                }
                   16437:                irr &= (~pic[c].imr);
                   16438:                if(!irr) {
                   16439:                        break;
                   16440:                }
                   16441:                if(!(pic[c].ocw3 & 0x20)) {
                   16442:                        irr |= pic[c].isr;
                   16443:                }
                   16444:                int level = pic[c].prio;
                   16445:                UINT8 bit = 1 << level;
                   16446:                while(!(irr & bit)) {
                   16447:                        level = (level + 1) & 7;
                   16448:                        bit = 1 << level;
                   16449:                }
                   16450:                if((c + 1 < 2) && (pic[c].icw3 & bit)) {
                   16451:                        // check slave
                   16452:                        continue;
                   16453:                }
                   16454:                if(pic[c].isr & bit) {
                   16455:                        break;
                   16456:                }
                   16457:                // interrupt request
                   16458:                pic_req_chip = c;
                   16459:                pic_req_level = level;
                   16460:                pic_req_bit = bit;
1.1.1.3   root     16461:                i386_set_irq_line(INPUT_LINE_IRQ, HOLD_LINE);
1.1       root     16462:                return;
                   16463:        }
1.1.1.3   root     16464:        i386_set_irq_line(INPUT_LINE_IRQ, CLEAR_LINE);
1.1.1.2   root     16465: }
1.1       root     16466: 
1.1.1.25  root     16467: // pio
                   16468: 
                   16469: void pio_init()
                   16470: {
1.1.1.38! root     16471: //     bool conv_mode = (GetConsoleCP() == 932);
        !          16472:        
1.1.1.26  root     16473:        memset(pio, 0, sizeof(pio));
1.1.1.37  root     16474:        
1.1.1.25  root     16475:        for(int c = 0; c < 2; c++) {
1.1.1.37  root     16476:                pio[c].stat = 0xdf;
1.1.1.25  root     16477:                pio[c].ctrl = 0x0c;
1.1.1.38! root     16478: //             pio[c].conv_mode = conv_mode;
1.1.1.25  root     16479:        }
                   16480: }
                   16481: 
1.1.1.37  root     16482: void pio_finish()
                   16483: {
                   16484:        pio_release();
                   16485: }
                   16486: 
                   16487: void pio_release()
                   16488: {
                   16489:        for(int c = 0; c < 2; c++) {
                   16490:                if(pio[c].fp != NULL) {
1.1.1.38! root     16491:                        if(pio[c].jis_mode) {
        !          16492:                                fputc(0x1c, pio[c].fp);
        !          16493:                                fputc(0x2e, pio[c].fp);
        !          16494:                        }
1.1.1.37  root     16495:                        fclose(pio[c].fp);
                   16496:                        pio[c].fp = NULL;
                   16497:                }
                   16498:        }
                   16499: }
                   16500: 
1.1.1.25  root     16501: void pio_write(int c, UINT32 addr, UINT8 data)
                   16502: {
                   16503:        switch(addr & 3) {
                   16504:        case 0:
                   16505:                pio[c].data = data;
                   16506:                break;
                   16507:        case 2:
1.1.1.37  root     16508:                if((pio[c].ctrl & 0x01) && !(data & 0x01)) {
                   16509:                        // strobe H -> L
                   16510:                        if(pio[c].data == 0x0d && (data & 0x02)) {
                   16511:                                // auto feed
                   16512:                                printer_out(c, 0x0d);
                   16513:                                printer_out(c, 0x0a);
                   16514:                        } else {
                   16515:                                printer_out(c, pio[c].data);
                   16516:                        }
                   16517:                        pio[c].stat &= ~0x40; // set ack
                   16518:                }
1.1.1.25  root     16519:                pio[c].ctrl = data;
                   16520:                break;
                   16521:        }
                   16522: }
                   16523: 
                   16524: UINT8 pio_read(int c, UINT32 addr)
                   16525: {
                   16526:        switch(addr & 3) {
                   16527:        case 0:
1.1.1.37  root     16528:                if(pio[c].ctrl & 0x20) {
                   16529:                        // input mode
                   16530:                        return(0xff);
                   16531:                }
1.1.1.25  root     16532:                return(pio[c].data);
                   16533:        case 1:
1.1.1.37  root     16534:                {
                   16535:                        UINT8 stat = pio[c].stat;
                   16536:                        pio[c].stat |= 0x40; // clear ack
                   16537:                        return(stat);
                   16538:                }
1.1.1.25  root     16539:        case 2:
                   16540:                return(pio[c].ctrl);
                   16541:        }
                   16542:        return(0xff);
                   16543: }
                   16544: 
1.1.1.37  root     16545: void printer_out(int c, UINT8 data)
                   16546: {
                   16547:        SYSTEMTIME time;
1.1.1.38! root     16548:        bool jis_mode = false;
1.1.1.37  root     16549:        
                   16550:        GetLocalTime(&time);
                   16551:        
                   16552:        if(pio[c].fp != NULL) {
                   16553:                // if at least 1000ms passed from last written, close the current file
                   16554:                FILETIME ftime1;
                   16555:                FILETIME ftime2;
                   16556:                SystemTimeToFileTime(&pio[c].time, &ftime1);
                   16557:                SystemTimeToFileTime(&time, &ftime2);
                   16558:                INT64 *time1 = (INT64 *)&ftime1;
                   16559:                INT64 *time2 = (INT64 *)&ftime2;
                   16560:                INT64 msec = (*time2 - *time1) / 10000;
                   16561:                
                   16562:                if(msec >= 1000) {
1.1.1.38! root     16563:                        if(pio[c].jis_mode) {
        !          16564:                                fputc(0x1c, pio[c].fp);
        !          16565:                                fputc(0x2e, pio[c].fp);
        !          16566:                                jis_mode = true;
        !          16567:                        }
1.1.1.37  root     16568:                        fclose(pio[c].fp);
                   16569:                        pio[c].fp = NULL;
                   16570:                }
                   16571:        }
                   16572:        if(pio[c].fp == NULL) {
                   16573:                // create a new file in the temp folder
                   16574:                char file_name[MAX_PATH];
                   16575:                
                   16576:                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);
                   16577:                if(GetTempPath(MAX_PATH, pio[c].path)) {
                   16578:                        strcat(pio[c].path, file_name);
                   16579:                } else {
                   16580:                        strcpy(pio[c].path, file_name);
                   16581:                }
1.1.1.38! root     16582:                pio[c].fp = fopen(pio[c].path, "w+b");
1.1.1.37  root     16583:        }
                   16584:        if(pio[c].fp != NULL) {
1.1.1.38! root     16585:                if(jis_mode) {
        !          16586:                        fputc(0x1c, pio[c].fp);
        !          16587:                        fputc(0x26, pio[c].fp);
        !          16588:                }
1.1.1.37  root     16589:                fputc(data, pio[c].fp);
1.1.1.38! root     16590:                
        !          16591:                // reopen file if 1ch 26h 1ch 2eh (kanji-on  kanji-off) are written at the top
        !          16592:                if(data == 0x2e && ftell(pio[c].fp) == 4) {
        !          16593:                        UINT8 buffer[4];
        !          16594:                        fseek(pio[c].fp, 0, SEEK_SET);
        !          16595:                        fread(buffer, 4, 1, pio[c].fp);
        !          16596:                        if(buffer[0] == 0x1c && buffer[1] == 0x26 && buffer[2] == 0x1c/* && buffer[3] == 0x2e*/) {
        !          16597:                                fclose(pio[c].fp);
        !          16598:                                pio[c].fp = fopen(pio[c].path, "w+b");
        !          16599:                        }
        !          16600:                }
1.1.1.37  root     16601:                pio[c].time = time;
                   16602:        }
                   16603: }
                   16604: 
1.1       root     16605: // pit
                   16606: 
1.1.1.22  root     16607: #define PIT_FREQ 1193182ULL
1.1       root     16608: #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)
                   16609: 
                   16610: void pit_init()
                   16611: {
1.1.1.8   root     16612:        memset(pit, 0, sizeof(pit));
1.1       root     16613:        for(int ch = 0; ch < 3; ch++) {
                   16614:                pit[ch].count = 0x10000;
                   16615:                pit[ch].ctrl_reg = 0x34;
                   16616:                pit[ch].mode = 3;
                   16617:        }
                   16618:        
                   16619:        // from bochs bios
                   16620:        pit_write(3, 0x34);
                   16621:        pit_write(0, 0x00);
                   16622:        pit_write(0, 0x00);
                   16623: }
                   16624: 
                   16625: void pit_write(int ch, UINT8 val)
                   16626: {
1.1.1.8   root     16627: #ifndef PIT_ALWAYS_RUNNING
1.1       root     16628:        if(!pit_active) {
                   16629:                pit_active = 1;
                   16630:                pit_init();
                   16631:        }
1.1.1.8   root     16632: #endif
1.1       root     16633:        switch(ch) {
                   16634:        case 0:
                   16635:        case 1:
                   16636:        case 2:
                   16637:                // write count register
                   16638:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   16639:                        if(pit[ch].ctrl_reg & 0x10) {
                   16640:                                pit[ch].low_write = 1;
                   16641:                        }
                   16642:                        if(pit[ch].ctrl_reg & 0x20) {
                   16643:                                pit[ch].high_write = 1;
                   16644:                        }
                   16645:                }
                   16646:                if(pit[ch].low_write) {
                   16647:                        pit[ch].count_reg = val;
                   16648:                        pit[ch].low_write = 0;
                   16649:                } else if(pit[ch].high_write) {
                   16650:                        if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   16651:                                pit[ch].count_reg = val << 8;
                   16652:                        } else {
                   16653:                                pit[ch].count_reg |= val << 8;
                   16654:                        }
                   16655:                        pit[ch].high_write = 0;
                   16656:                }
                   16657:                // start count
1.1.1.8   root     16658:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   16659:                        if(pit[ch].mode == 0 || pit[ch].mode == 4 || pit[ch].prev_time == 0) {
                   16660:                                pit[ch].count = PIT_COUNT_VALUE(ch);
                   16661:                                pit[ch].prev_time = timeGetTime();
                   16662:                                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     16663:                        }
                   16664:                }
                   16665:                break;
                   16666:        case 3: // ctrl reg
                   16667:                if((val & 0xc0) == 0xc0) {
                   16668:                        // i8254 read-back command
                   16669:                        for(ch = 0; ch < 3; ch++) {
                   16670:                                if(!(val & 0x10) && !pit[ch].status_latched) {
                   16671:                                        pit[ch].status = pit[ch].ctrl_reg & 0x3f;
                   16672:                                        pit[ch].status_latched = 1;
                   16673:                                }
                   16674:                                if(!(val & 0x20) && !pit[ch].count_latched) {
                   16675:                                        pit_latch_count(ch);
                   16676:                                }
                   16677:                        }
                   16678:                        break;
                   16679:                }
                   16680:                ch = (val >> 6) & 3;
                   16681:                if(val & 0x30) {
1.1.1.35  root     16682:                        static const int modes[8] = {0, 1, 2, 3, 4, 5, 2, 3};
1.1       root     16683:                        pit[ch].mode = modes[(val >> 1) & 7];
                   16684:                        pit[ch].count_latched = 0;
                   16685:                        pit[ch].low_read = pit[ch].high_read = 0;
                   16686:                        pit[ch].low_write = pit[ch].high_write = 0;
                   16687:                        pit[ch].ctrl_reg = val;
                   16688:                        // stop count
1.1.1.8   root     16689:                        pit[ch].prev_time = pit[ch].expired_time = 0;
1.1       root     16690:                        pit[ch].count_reg = 0;
                   16691:                } else if(!pit[ch].count_latched) {
                   16692:                        pit_latch_count(ch);
                   16693:                }
                   16694:                break;
                   16695:        }
                   16696: }
                   16697: 
                   16698: UINT8 pit_read(int ch)
                   16699: {
1.1.1.8   root     16700: #ifndef PIT_ALWAYS_RUNNING
1.1       root     16701:        if(!pit_active) {
                   16702:                pit_active = 1;
                   16703:                pit_init();
                   16704:        }
1.1.1.8   root     16705: #endif
1.1       root     16706:        switch(ch) {
                   16707:        case 0:
                   16708:        case 1:
                   16709:        case 2:
                   16710:                if(pit[ch].status_latched) {
                   16711:                        pit[ch].status_latched = 0;
                   16712:                        return(pit[ch].status);
                   16713:                }
                   16714:                // if not latched, through current count
                   16715:                if(!pit[ch].count_latched) {
                   16716:                        if(!pit[ch].low_read && !pit[ch].high_read) {
                   16717:                                pit_latch_count(ch);
                   16718:                        }
                   16719:                }
                   16720:                // return latched count
                   16721:                if(pit[ch].low_read) {
                   16722:                        pit[ch].low_read = 0;
                   16723:                        if(!pit[ch].high_read) {
                   16724:                                pit[ch].count_latched = 0;
                   16725:                        }
                   16726:                        return(pit[ch].latch & 0xff);
                   16727:                } else if(pit[ch].high_read) {
                   16728:                        pit[ch].high_read = 0;
                   16729:                        pit[ch].count_latched = 0;
                   16730:                        return((pit[ch].latch >> 8) & 0xff);
                   16731:                }
                   16732:        }
                   16733:        return(0xff);
                   16734: }
                   16735: 
1.1.1.8   root     16736: int pit_run(int ch, UINT32 cur_time)
1.1       root     16737: {
1.1.1.8   root     16738:        if(pit[ch].expired_time != 0 && cur_time >= pit[ch].expired_time) {
1.1       root     16739:                pit[ch].count = PIT_COUNT_VALUE(ch);
1.1.1.8   root     16740:                pit[ch].prev_time = pit[ch].expired_time;
                   16741:                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
                   16742:                if(cur_time >= pit[ch].expired_time) {
                   16743:                        pit[ch].prev_time = cur_time;
                   16744:                        pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     16745:                }
1.1.1.8   root     16746:                return(1);
1.1       root     16747:        }
1.1.1.8   root     16748:        return(0);
1.1       root     16749: }
                   16750: 
                   16751: void pit_latch_count(int ch)
                   16752: {
1.1.1.8   root     16753:        if(pit[ch].expired_time != 0) {
1.1.1.26  root     16754:                UINT32 cur_time = timeGetTime();
1.1.1.8   root     16755:                pit_run(ch, cur_time);
                   16756:                UINT32 tmp = (pit[ch].count * (pit[ch].expired_time - cur_time)) / (pit[ch].expired_time - pit[ch].prev_time);
1.1.1.26  root     16757:                UINT16 latch = (tmp != 0) ? (UINT16)tmp : 1;
                   16758:                
                   16759:                if(pit[ch].prev_latch == latch && pit[ch].expired_time > cur_time) {
                   16760:                        // decrement counter in 1msec period
                   16761:                        if(pit[ch].next_latch == 0) {
                   16762:                                tmp = (pit[ch].count * (pit[ch].expired_time - cur_time - 1)) / (pit[ch].expired_time - pit[ch].prev_time);
                   16763:                                pit[ch].next_latch = (tmp != 0) ? (UINT16)tmp : 1;
                   16764:                        }
                   16765:                        if(pit[ch].latch > pit[ch].next_latch) {
                   16766:                                pit[ch].latch--;
                   16767:                        }
                   16768:                } else {
                   16769:                        pit[ch].prev_latch = pit[ch].latch = latch;
                   16770:                        pit[ch].next_latch = 0;
                   16771:                }
1.1.1.8   root     16772:        } else {
                   16773:                pit[ch].latch = (UINT16)pit[ch].count;
1.1.1.26  root     16774:                pit[ch].prev_latch = pit[ch].next_latch = 0;
1.1       root     16775:        }
                   16776:        pit[ch].count_latched = 1;
                   16777:        if((pit[ch].ctrl_reg & 0x30) == 0x10) {
                   16778:                // lower byte
                   16779:                pit[ch].low_read = 1;
                   16780:                pit[ch].high_read = 0;
                   16781:        } else if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   16782:                // upper byte
                   16783:                pit[ch].low_read = 0;
                   16784:                pit[ch].high_read = 1;
                   16785:        } else {
                   16786:                // lower -> upper
1.1.1.14  root     16787:                pit[ch].low_read = pit[ch].high_read = 1;
1.1       root     16788:        }
                   16789: }
                   16790: 
1.1.1.8   root     16791: int pit_get_expired_time(int ch)
1.1       root     16792: {
1.1.1.22  root     16793:        pit[ch].accum += 1024ULL * 1000ULL * (UINT64)pit[ch].count / PIT_FREQ;
                   16794:        UINT64 val = pit[ch].accum >> 10;
                   16795:        pit[ch].accum -= val << 10;
                   16796:        return((val != 0) ? val : 1);
1.1.1.8   root     16797: }
                   16798: 
1.1.1.25  root     16799: // sio
                   16800: 
                   16801: void sio_init()
                   16802: {
1.1.1.26  root     16803:        memset(sio, 0, sizeof(sio));
                   16804:        memset(sio_mt, 0, sizeof(sio_mt));
                   16805:        
1.1.1.29  root     16806:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     16807:                sio[c].send_buffer = new FIFO(SIO_BUFFER_SIZE);
                   16808:                sio[c].recv_buffer = new FIFO(SIO_BUFFER_SIZE);
                   16809:                
                   16810:                sio[c].divisor.w = 12;          // 115200Hz / 9600Baud
                   16811:                sio[c].line_ctrl = 0x03;        // 8bit, stop 1bit, non parity
1.1.1.26  root     16812:                sio[c].modem_ctrl = 0x03;       // rts=on, dtr=on
                   16813:                sio[c].set_rts = sio[c].set_dtr = true;
1.1.1.25  root     16814:                sio[c].line_stat_buf = 0x60;    // send/recv buffers are empty
                   16815:                sio[c].irq_identify = 0x01;     // no pending irq
                   16816:                
                   16817:                InitializeCriticalSection(&sio_mt[c].csSendData);
                   16818:                InitializeCriticalSection(&sio_mt[c].csRecvData);
                   16819:                InitializeCriticalSection(&sio_mt[c].csLineCtrl);
                   16820:                InitializeCriticalSection(&sio_mt[c].csLineStat);
                   16821:                InitializeCriticalSection(&sio_mt[c].csModemCtrl);
                   16822:                InitializeCriticalSection(&sio_mt[c].csModemStat);
                   16823:                
1.1.1.26  root     16824:                if(sio_port_number[c] != 0) {
1.1.1.25  root     16825:                        sio[c].channel = c;
                   16826:                        sio_mt[c].hThread = CreateThread(NULL, 0, sio_thread, &sio[c], 0, NULL);
                   16827:                }
                   16828:        }
                   16829: }
                   16830: 
                   16831: void sio_finish()
                   16832: {
1.1.1.29  root     16833:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     16834:                if(sio_mt[c].hThread != NULL) {
                   16835:                        WaitForSingleObject(sio_mt[c].hThread, INFINITE);
                   16836:                        CloseHandle(sio_mt[c].hThread);
1.1.1.28  root     16837:                        sio_mt[c].hThread = NULL;
1.1.1.25  root     16838:                }
                   16839:                DeleteCriticalSection(&sio_mt[c].csSendData);
                   16840:                DeleteCriticalSection(&sio_mt[c].csRecvData);
                   16841:                DeleteCriticalSection(&sio_mt[c].csLineCtrl);
                   16842:                DeleteCriticalSection(&sio_mt[c].csLineStat);
                   16843:                DeleteCriticalSection(&sio_mt[c].csModemCtrl);
                   16844:                DeleteCriticalSection(&sio_mt[c].csModemStat);
1.1.1.28  root     16845:        }
                   16846:        sio_release();
                   16847: }
                   16848: 
                   16849: void sio_release()
                   16850: {
1.1.1.29  root     16851:        for(int c = 0; c < 4; c++) {
1.1.1.28  root     16852:                // sio_thread() may access the resources :-(
1.1.1.32  root     16853:                bool running = (sio_mt[c].hThread != NULL);
                   16854:                
                   16855:                if(running) {
                   16856:                        EnterCriticalSection(&sio_mt[c].csSendData);
                   16857:                }
                   16858:                if(sio[c].send_buffer != NULL) {
                   16859:                        sio[c].send_buffer->release();
                   16860:                        delete sio[c].send_buffer;
                   16861:                        sio[c].send_buffer = NULL;
                   16862:                }
                   16863:                if(running) {
                   16864:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   16865:                        EnterCriticalSection(&sio_mt[c].csRecvData);
                   16866:                }
                   16867:                if(sio[c].recv_buffer != NULL) {
                   16868:                        sio[c].recv_buffer->release();
                   16869:                        delete sio[c].recv_buffer;
                   16870:                        sio[c].recv_buffer = NULL;
                   16871:                }
                   16872:                if(running) {
                   16873:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
1.1.1.28  root     16874:                }
1.1.1.25  root     16875:        }
                   16876: }
                   16877: 
                   16878: void sio_write(int c, UINT32 addr, UINT8 data)
                   16879: {
                   16880:        switch(addr & 7) {
                   16881:        case 0:
                   16882:                if(sio[c].selector & 0x80) {
                   16883:                        if(sio[c].divisor.b.l != data) {
                   16884:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   16885:                                sio[c].divisor.b.l = data;
                   16886:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   16887:                        }
                   16888:                } else {
                   16889:                        EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     16890:                        if(sio[c].send_buffer != NULL) {
                   16891:                                sio[c].send_buffer->write(data);
                   16892:                        }
1.1.1.25  root     16893:                        // transmitter holding/shift registers are not empty
                   16894:                        sio[c].line_stat_buf &= ~0x60;
                   16895:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   16896:                        
                   16897:                        if(sio[c].irq_enable & 0x02) {
                   16898:                                sio_update_irq(c);
                   16899:                        }
                   16900:                }
                   16901:                break;
                   16902:        case 1:
                   16903:                if(sio[c].selector & 0x80) {
                   16904:                        if(sio[c].divisor.b.h != data) {
                   16905:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   16906:                                sio[c].divisor.b.h = data;
                   16907:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   16908:                        }
                   16909:                } else {
                   16910:                        if(sio[c].irq_enable != data) {
                   16911:                                sio[c].irq_enable = data;
                   16912:                                sio_update_irq(c);
                   16913:                        }
                   16914:                }
                   16915:                break;
                   16916:        case 3:
                   16917:                {
                   16918:                        UINT8 line_ctrl = data & 0x3f;
                   16919:                        bool set_brk = ((data & 0x40) != 0);
                   16920:                        
                   16921:                        if(sio[c].line_ctrl != line_ctrl) {
                   16922:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   16923:                                sio[c].line_ctrl = line_ctrl;
                   16924:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   16925:                        }
                   16926:                        if(sio[c].set_brk != set_brk) {
                   16927:                                EnterCriticalSection(&sio_mt[c].csModemCtrl);
                   16928:                                sio[c].set_brk = set_brk;
                   16929:                                LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   16930:                        }
                   16931:                }
                   16932:                sio[c].selector = data;
                   16933:                break;
                   16934:        case 4:
                   16935:                {
                   16936:                        bool set_dtr = ((data & 0x01) != 0);
                   16937:                        bool set_rts = ((data & 0x02) != 0);
                   16938:                        
                   16939:                        if(sio[c].set_dtr != set_dtr || sio[c].set_rts != set_rts) {
1.1.1.26  root     16940: //                             EnterCriticalSection(&sio_mt[c].csModemCtrl);
1.1.1.25  root     16941:                                sio[c].set_dtr = set_dtr;
                   16942:                                sio[c].set_rts = set_rts;
1.1.1.26  root     16943: //                             LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   16944:                                
                   16945:                                bool state_changed = false;
                   16946:                                
                   16947:                                EnterCriticalSection(&sio_mt[c].csModemStat);
                   16948:                                if(set_dtr) {
                   16949:                                        sio[c].modem_stat |= 0x20;      // dsr on
                   16950:                                } else {
                   16951:                                        sio[c].modem_stat &= ~0x20;     // dsr off
                   16952:                                }
                   16953:                                if(set_rts) {
                   16954:                                        sio[c].modem_stat |= 0x10;      // cts on
                   16955:                                } else {
                   16956:                                        sio[c].modem_stat &= ~0x10;     // cts off
                   16957:                                }
                   16958:                                if((sio[c].prev_modem_stat & 0x20) != (sio[c].modem_stat & 0x20)) {
                   16959:                                        if(!(sio[c].modem_stat & 0x02)) {
                   16960:                                                if(sio[c].irq_enable & 0x08) {
                   16961:                                                        state_changed = true;
                   16962:                                                }
                   16963:                                                sio[c].modem_stat |= 0x02;
                   16964:                                        }
                   16965:                                }
                   16966:                                if((sio[c].prev_modem_stat & 0x10) != (sio[c].modem_stat & 0x10)) {
                   16967:                                        if(!(sio[c].modem_stat & 0x01)) {
                   16968:                                                if(sio[c].irq_enable & 0x08) {
                   16969:                                                        state_changed = true;
                   16970:                                                }
                   16971:                                                sio[c].modem_stat |= 0x01;
                   16972:                                        }
                   16973:                                }
                   16974:                                LeaveCriticalSection(&sio_mt[c].csModemStat);
                   16975:                                
                   16976:                                if(state_changed) {
                   16977:                                        sio_update_irq(c);
                   16978:                                }
1.1.1.25  root     16979:                        }
                   16980:                }
                   16981:                sio[c].modem_ctrl = data;
                   16982:                break;
                   16983:        case 7:
                   16984:                sio[c].scratch = data;
                   16985:                break;
                   16986:        }
                   16987: }
                   16988: 
                   16989: UINT8 sio_read(int c, UINT32 addr)
                   16990: {
                   16991:        switch(addr & 7) {
                   16992:        case 0:
                   16993:                if(sio[c].selector & 0x80) {
                   16994:                        return(sio[c].divisor.b.l);
                   16995:                } else {
                   16996:                        EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     16997:                        UINT8 data = 0;
                   16998:                        if(sio[c].recv_buffer != NULL) {
                   16999:                                data = sio[c].recv_buffer->read();
                   17000:                        }
1.1.1.25  root     17001:                        // data is not ready
                   17002:                        sio[c].line_stat_buf &= ~0x01;
                   17003:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
                   17004:                        
                   17005:                        if(sio[c].irq_enable & 0x01) {
                   17006:                                sio_update_irq(c);
                   17007:                        }
                   17008:                        return(data);
                   17009:                }
                   17010:        case 1:
                   17011:                if(sio[c].selector & 0x80) {
                   17012:                        return(sio[c].divisor.b.h);
                   17013:                } else {
                   17014:                        return(sio[c].irq_enable);
                   17015:                }
                   17016:        case 2:
                   17017:                return(sio[c].irq_identify);
                   17018:        case 3:
                   17019:                return(sio[c].selector);
                   17020:        case 4:
                   17021:                return(sio[c].modem_ctrl);
                   17022:        case 5:
                   17023:                {
                   17024:                        EnterCriticalSection(&sio_mt[c].csLineStat);
                   17025:                        UINT8 val = sio[c].line_stat_err | sio[c].line_stat_buf;
                   17026:                        sio[c].line_stat_err = 0x00;
                   17027:                        LeaveCriticalSection(&sio_mt[c].csLineStat);
                   17028:                        
                   17029:                        bool state_changed = false;
                   17030:                        
                   17031:                        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   17032:                                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     17033:                                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     17034:                                        // transmitter holding register will be empty first
                   17035:                                        if(sio[c].irq_enable & 0x02) {
                   17036:                                                state_changed = true;
                   17037:                                        }
                   17038:                                        sio[c].line_stat_buf |= 0x20;
                   17039:                                }
                   17040:                                LeaveCriticalSection(&sio_mt[c].csSendData);
                   17041:                        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   17042:                                // transmitter shift register will be empty later
                   17043:                                sio[c].line_stat_buf |= 0x40;
                   17044:                        }
                   17045:                        if(!(sio[c].line_stat_buf & 0x01)) {
                   17046:                                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     17047:                                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     17048:                                        // data is ready
                   17049:                                        if(sio[c].irq_enable & 0x01) {
                   17050:                                                state_changed = true;
                   17051:                                        }
                   17052:                                        sio[c].line_stat_buf |= 0x01;
                   17053:                                }
                   17054:                                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   17055:                        }
                   17056:                        if(state_changed) {
                   17057:                                sio_update_irq(c);
                   17058:                        }
                   17059:                        return(val);
                   17060:                }
                   17061:        case 6:
                   17062:                {
                   17063:                        EnterCriticalSection(&sio_mt[c].csModemStat);
                   17064:                        UINT8 val = sio[c].modem_stat;
                   17065:                        sio[c].modem_stat &= 0xf0;
                   17066:                        sio[c].prev_modem_stat = sio[c].modem_stat;
                   17067:                        LeaveCriticalSection(&sio_mt[c].csModemStat);
                   17068:                        
                   17069:                        if(sio[c].modem_ctrl & 0x10) {
                   17070:                                // loop-back
                   17071:                                val &= 0x0f;
                   17072:                                val |= (sio[c].modem_ctrl & 0x0c) << 4;
                   17073:                                val |= (sio[c].modem_ctrl & 0x01) << 5;
                   17074:                                val |= (sio[c].modem_ctrl & 0x02) << 3;
                   17075:                        }
                   17076:                        return(val);
                   17077:                }
                   17078:        case 7:
                   17079:                return(sio[c].scratch);
                   17080:        }
                   17081:        return(0xff);
                   17082: }
                   17083: 
                   17084: void sio_update(int c)
                   17085: {
                   17086:        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   17087:                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     17088:                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     17089:                        // transmitter holding/shift registers will be empty
                   17090:                        sio[c].line_stat_buf |= 0x60;
                   17091:                }
                   17092:                LeaveCriticalSection(&sio_mt[c].csSendData);
                   17093:        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   17094:                // transmitter shift register will be empty
                   17095:                sio[c].line_stat_buf |= 0x40;
                   17096:        }
                   17097:        if(!(sio[c].line_stat_buf & 0x01)) {
                   17098:                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     17099:                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     17100:                        // data is ready
                   17101:                        sio[c].line_stat_buf |= 0x01;
                   17102:                }
                   17103:                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   17104:        }
                   17105:        sio_update_irq(c);
                   17106: }
                   17107: 
                   17108: void sio_update_irq(int c)
                   17109: {
                   17110:        int level = -1;
                   17111:        
                   17112:        if(sio[c].irq_enable & 0x08) {
                   17113:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   17114:                if((sio[c].modem_stat & 0x0f) != 0) {
                   17115:                        level = 0;
                   17116:                }
                   17117:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   17118:        }
                   17119:        if(sio[c].irq_enable & 0x02) {
                   17120:                if(sio[c].line_stat_buf & 0x20) {
                   17121:                        level = 1;
                   17122:                }
                   17123:        }
                   17124:        if(sio[c].irq_enable & 0x01) {
                   17125:                if(sio[c].line_stat_buf & 0x01) {
                   17126:                        level = 2;
                   17127:                }
                   17128:        }
                   17129:        if(sio[c].irq_enable & 0x04) {
                   17130:                EnterCriticalSection(&sio_mt[c].csLineStat);
                   17131:                if(sio[c].line_stat_err != 0) {
                   17132:                        level = 3;
                   17133:                }
                   17134:                LeaveCriticalSection(&sio_mt[c].csLineStat);
                   17135:        }
1.1.1.29  root     17136:        
                   17137:        // COM1 and COM3 shares IRQ4, COM2 and COM4 shares IRQ3
1.1.1.25  root     17138:        if(level != -1) {
                   17139:                sio[c].irq_identify = level << 1;
1.1.1.29  root     17140:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 1);
1.1.1.25  root     17141:        } else {
                   17142:                sio[c].irq_identify = 1;
1.1.1.29  root     17143:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 0);
1.1.1.25  root     17144:        }
                   17145: }
                   17146: 
                   17147: DWORD WINAPI sio_thread(void *lpx)
                   17148: {
                   17149:        volatile sio_t *p = (sio_t *)lpx;
                   17150:        sio_mt_t *q = &sio_mt[p->channel];
                   17151:        
                   17152:        char name[] = "COM1";
1.1.1.26  root     17153:        name[3] = '0' + sio_port_number[p->channel];
                   17154:        HANDLE hComm = NULL;
                   17155:        COMMPROP commProp;
                   17156:        DCB dcb;
                   17157:        DWORD dwSettableBaud = 0xffb; // 75, 110, 150, 300, 600, 1200, 1800, 2400, 4800, 7200, and 9600bps
                   17158:        BYTE bytBuffer[SIO_BUFFER_SIZE];
                   17159:        
                   17160:        if((hComm = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL)) != INVALID_HANDLE_VALUE) {
                   17161:                if(GetCommProperties(hComm, &commProp)) {
                   17162:                        dwSettableBaud = commProp.dwSettableBaud;
                   17163:                }
1.1.1.25  root     17164:                EscapeCommFunction(hComm, CLRBREAK);
1.1.1.26  root     17165: //             EscapeCommFunction(hComm, SETRTS);
                   17166: //             EscapeCommFunction(hComm, SETDTR);
1.1.1.25  root     17167:                
                   17168:                while(!m_halted) {
                   17169:                        // setup comm port
                   17170:                        bool comm_state_changed = false;
                   17171:                        
                   17172:                        EnterCriticalSection(&q->csLineCtrl);
                   17173:                        if((p->prev_divisor != p->divisor.w || p->prev_line_ctrl != p->line_ctrl) && p->divisor.w != 0) {
                   17174:                                p->prev_divisor = p->divisor.w;
                   17175:                                p->prev_line_ctrl = p->line_ctrl;
                   17176:                                comm_state_changed = true;
                   17177:                        }
                   17178:                        LeaveCriticalSection(&q->csLineCtrl);
                   17179:                        
                   17180:                        if(comm_state_changed) {
1.1.1.26  root     17181:                                if(GetCommState(hComm, &dcb)) {
                   17182: //                                     dcb.BaudRate = min(9600, 115200 / p->prev_divisor);
                   17183:                                        DWORD baud = 115200 / p->prev_divisor;
                   17184:                                        dcb.BaudRate = 9600; // default
                   17185:                                        
                   17186:                                        if((dwSettableBaud & BAUD_075  ) && baud >= 75   ) dcb.BaudRate = 75;
                   17187:                                        if((dwSettableBaud & BAUD_110  ) && baud >= 110  ) dcb.BaudRate = 110;
                   17188:                                        // 134.5bps is not supported ???
                   17189: //                                     if((dwSettableBaud & BAUD_134_5) && baud >= 134.5) dcb.BaudRate = 134.5;
                   17190:                                        if((dwSettableBaud & BAUD_150  ) && baud >= 150  ) dcb.BaudRate = 150;
                   17191:                                        if((dwSettableBaud & BAUD_300  ) && baud >= 300  ) dcb.BaudRate = 300;
                   17192:                                        if((dwSettableBaud & BAUD_600  ) && baud >= 600  ) dcb.BaudRate = 600;
                   17193:                                        if((dwSettableBaud & BAUD_1200 ) && baud >= 1200 ) dcb.BaudRate = 1200;
                   17194:                                        if((dwSettableBaud & BAUD_1800 ) && baud >= 1800 ) dcb.BaudRate = 1800;
                   17195:                                        if((dwSettableBaud & BAUD_2400 ) && baud >= 2400 ) dcb.BaudRate = 2400;
                   17196:                                        if((dwSettableBaud & BAUD_4800 ) && baud >= 4800 ) dcb.BaudRate = 4800;
                   17197:                                        if((dwSettableBaud & BAUD_7200 ) && baud >= 7200 ) dcb.BaudRate = 7200;
                   17198:                                        if((dwSettableBaud & BAUD_9600 ) && baud >= 9600 ) dcb.BaudRate = 9600;
                   17199: //                                     if((dwSettableBaud & BAUD_14400) && baud >= 14400) dcb.BaudRate = 14400;
                   17200: //                                     if((dwSettableBaud & BAUD_19200) && baud >= 19200) dcb.BaudRate = 19200;
                   17201: //                                     if((dwSettableBaud & BAUD_38400) && baud >= 38400) dcb.BaudRate = 38400;
                   17202:                                        
                   17203:                                        switch(p->prev_line_ctrl & 0x03) {
                   17204:                                        case 0x00: dcb.ByteSize = 5; break;
                   17205:                                        case 0x01: dcb.ByteSize = 6; break;
                   17206:                                        case 0x02: dcb.ByteSize = 7; break;
                   17207:                                        case 0x03: dcb.ByteSize = 8; break;
                   17208:                                        }
                   17209:                                        switch(p->prev_line_ctrl & 0x04) {
                   17210:                                        case 0x00: dcb.StopBits = ONESTOPBIT; break;
                   17211:                                        case 0x04: dcb.StopBits = (dcb.ByteSize == 5) ? ONE5STOPBITS : TWOSTOPBITS; break;
                   17212:                                        }
                   17213:                                        switch(p->prev_line_ctrl & 0x38) {
                   17214:                                        case 0x08: dcb.Parity = ODDPARITY;   break;
                   17215:                                        case 0x18: dcb.Parity = EVENPARITY;  break;
                   17216:                                        case 0x28: dcb.Parity = MARKPARITY;  break;
                   17217:                                        case 0x38: dcb.Parity = SPACEPARITY; break;
                   17218:                                        default:   dcb.Parity = NOPARITY;    break;
                   17219:                                        }
                   17220:                                        dcb.fBinary = TRUE;
                   17221:                                        dcb.fParity = (dcb.Parity != NOPARITY);
                   17222:                                        dcb.fOutxCtsFlow = dcb.fOutxDsrFlow = TRUE;
                   17223:                                        dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
                   17224:                                        dcb.fDsrSensitivity = FALSE;//TRUE;
                   17225:                                        dcb.fTXContinueOnXoff = TRUE;
                   17226:                                        dcb.fOutX = dcb.fInX = FALSE;
                   17227:                                        dcb.fErrorChar = FALSE;
                   17228:                                        dcb.fNull = FALSE;
                   17229:                                        dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
                   17230:                                        dcb.fAbortOnError = FALSE;
                   17231:                                        
                   17232:                                        SetCommState(hComm, &dcb);
1.1.1.25  root     17233:                                }
                   17234:                                
                   17235:                                // check again to apply all comm state changes
                   17236:                                Sleep(10);
                   17237:                                continue;
                   17238:                        }
                   17239:                        
                   17240:                        // set comm pins
                   17241:                        bool change_brk = false;
1.1.1.26  root     17242: //                     bool change_rts = false;
                   17243: //                     bool change_dtr = false;
1.1.1.25  root     17244:                        
                   17245:                        EnterCriticalSection(&q->csModemCtrl);
                   17246:                        if(p->prev_set_brk != p->set_brk) {
                   17247:                                p->prev_set_brk = p->set_brk;
                   17248:                                change_brk = true;
                   17249:                        }
1.1.1.26  root     17250: //                     if(p->prev_set_rts != p->set_rts) {
                   17251: //                             p->prev_set_rts = p->set_rts;
                   17252: //                             change_rts = true;
                   17253: //                     }
                   17254: //                     if(p->prev_set_dtr != p->set_dtr) {
                   17255: //                             p->prev_set_dtr = p->set_dtr;
                   17256: //                             change_dtr = true;
                   17257: //                     }
1.1.1.25  root     17258:                        LeaveCriticalSection(&q->csModemCtrl);
                   17259:                        
                   17260:                        if(change_brk) {
1.1.1.26  root     17261:                                static UINT32 clear_time = 0;
                   17262:                                if(p->prev_set_brk) {
                   17263:                                        EscapeCommFunction(hComm, SETBREAK);
                   17264:                                        clear_time = timeGetTime() + 200;
                   17265:                                } else {
                   17266:                                        // keep break for at least 200msec
                   17267:                                        UINT32 cur_time = timeGetTime();
                   17268:                                        if(clear_time > cur_time) {
                   17269:                                                Sleep(clear_time - cur_time);
                   17270:                                        }
                   17271:                                        EscapeCommFunction(hComm, CLRBREAK);
                   17272:                                }
1.1.1.25  root     17273:                        }
1.1.1.26  root     17274: //                     if(change_rts) {
                   17275: //                             if(p->prev_set_rts) {
                   17276: //                                     EscapeCommFunction(hComm, SETRTS);
                   17277: //                             } else {
                   17278: //                                     EscapeCommFunction(hComm, CLRRTS);
                   17279: //                             }
                   17280: //                     }
                   17281: //                     if(change_dtr) {
                   17282: //                             if(p->prev_set_dtr) {
                   17283: //                                     EscapeCommFunction(hComm, SETDTR);
                   17284: //                             } else {
                   17285: //                                     EscapeCommFunction(hComm, CLRDTR);
                   17286: //                             }
                   17287: //                     }
1.1.1.25  root     17288:                        
                   17289:                        // get comm pins
                   17290:                        DWORD dwModemStat = 0;
                   17291:                        
                   17292:                        if(GetCommModemStatus(hComm, &dwModemStat)) {
                   17293:                                EnterCriticalSection(&q->csModemStat);
                   17294:                                if(dwModemStat & MS_RLSD_ON) {
                   17295:                                        p->modem_stat |= 0x80;
                   17296:                                } else {
                   17297:                                        p->modem_stat &= ~0x80;
                   17298:                                }
                   17299:                                if(dwModemStat & MS_RING_ON) {
                   17300:                                        p->modem_stat |= 0x40;
                   17301:                                } else {
                   17302:                                        p->modem_stat &= ~0x40;
                   17303:                                }
1.1.1.26  root     17304: //                             if(dwModemStat & MS_DSR_ON) {
                   17305: //                                     p->modem_stat |= 0x20;
                   17306: //                             } else {
                   17307: //                                     p->modem_stat &= ~0x20;
                   17308: //                             }
                   17309: //                             if(dwModemStat & MS_CTS_ON) {
                   17310: //                                     p->modem_stat |= 0x10;
                   17311: //                             } else {
                   17312: //                                     p->modem_stat &= ~0x10;
                   17313: //                             }
1.1.1.25  root     17314:                                if((p->prev_modem_stat & 0x80) != (p->modem_stat & 0x80)) {
                   17315:                                        p->modem_stat |= 0x08;
                   17316:                                }
                   17317:                                if((p->prev_modem_stat & 0x40) && !(p->modem_stat & 0x40)) {
                   17318:                                        p->modem_stat |= 0x04;
                   17319:                                }
1.1.1.26  root     17320: //                             if((p->prev_modem_stat & 0x20) != (p->modem_stat & 0x20)) {
                   17321: //                                     p->modem_stat |= 0x02;
                   17322: //                             }
                   17323: //                             if((p->prev_modem_stat & 0x10) != (p->modem_stat & 0x10)) {
                   17324: //                                     p->modem_stat |= 0x01;
                   17325: //                             }
1.1.1.25  root     17326:                                LeaveCriticalSection(&q->csModemStat);
                   17327:                        }
                   17328:                        
                   17329:                        // send data
                   17330:                        DWORD dwSend = 0;
                   17331:                        
                   17332:                        EnterCriticalSection(&q->csSendData);
1.1.1.32  root     17333:                        while(p->send_buffer != NULL && !p->send_buffer->empty()) {
1.1.1.25  root     17334:                                bytBuffer[dwSend++] = p->send_buffer->read();
                   17335:                        }
                   17336:                        LeaveCriticalSection(&q->csSendData);
                   17337:                        
                   17338:                        if(dwSend != 0) {
                   17339:                                DWORD dwWritten = 0;
                   17340:                                WriteFile(hComm, bytBuffer, dwSend, &dwWritten, NULL);
                   17341:                        }
                   17342:                        
                   17343:                        // get line status and recv data
                   17344:                        DWORD dwLineStat = 0;
                   17345:                        COMSTAT comStat;
                   17346:                        
                   17347:                        if(ClearCommError(hComm, &dwLineStat, &comStat)) {
                   17348:                                EnterCriticalSection(&q->csLineStat);
                   17349:                                if(dwLineStat & CE_BREAK) {
                   17350:                                        p->line_stat_err |= 0x10;
                   17351:                                }
                   17352:                                if(dwLineStat & CE_FRAME) {
                   17353:                                        p->line_stat_err |= 0x08;
                   17354:                                }
                   17355:                                if(dwLineStat & CE_RXPARITY) {
                   17356:                                        p->line_stat_err |= 0x04;
                   17357:                                }
                   17358:                                if(dwLineStat & CE_OVERRUN) {
                   17359:                                        p->line_stat_err |= 0x02;
                   17360:                                }
                   17361:                                LeaveCriticalSection(&q->csLineStat);
                   17362:                                
                   17363:                                if(comStat.cbInQue != 0) {
                   17364:                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     17365:                                        DWORD dwRecv = 0;
                   17366:                                        if(p->recv_buffer != NULL) {
                   17367:                                                dwRecv = min(comStat.cbInQue, p->recv_buffer->remain());
                   17368:                                        }
1.1.1.25  root     17369:                                        LeaveCriticalSection(&q->csRecvData);
                   17370:                                        
                   17371:                                        if(dwRecv != 0) {
                   17372:                                                DWORD dwRead = 0;
                   17373:                                                if(ReadFile(hComm, bytBuffer, dwRecv, &dwRead, NULL) && dwRead != 0) {
                   17374:                                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     17375:                                                        if(p->recv_buffer != NULL) {
                   17376:                                                                for(int i = 0; i < dwRead; i++) {
                   17377:                                                                        p->recv_buffer->write(bytBuffer[i]);
                   17378:                                                                }
1.1.1.25  root     17379:                                                        }
                   17380:                                                        LeaveCriticalSection(&q->csRecvData);
                   17381:                                                }
                   17382:                                        }
                   17383:                                }
                   17384:                        }
                   17385:                        Sleep(10);
                   17386:                }
                   17387:                CloseHandle(hComm);
                   17388:        }
                   17389:        return 0;
                   17390: }
                   17391: 
1.1.1.8   root     17392: // cmos
                   17393: 
                   17394: void cmos_init()
                   17395: {
                   17396:        memset(cmos, 0, sizeof(cmos));
                   17397:        cmos_addr = 0;
1.1       root     17398:        
1.1.1.8   root     17399:        // from DOSBox
                   17400:        cmos_write(0x0a, 0x26);
                   17401:        cmos_write(0x0b, 0x02);
                   17402:        cmos_write(0x0d, 0x80);
1.1       root     17403: }
                   17404: 
1.1.1.8   root     17405: void cmos_write(int addr, UINT8 val)
1.1       root     17406: {
1.1.1.8   root     17407:        cmos[addr & 0x7f] = val;
                   17408: }
                   17409: 
                   17410: #define CMOS_GET_TIME() { \
                   17411:        UINT32 cur_sec = timeGetTime() / 1000 ; \
                   17412:        if(prev_sec != cur_sec) { \
                   17413:                GetLocalTime(&time); \
                   17414:                prev_sec = cur_sec; \
                   17415:        } \
1.1       root     17416: }
1.1.1.8   root     17417: #define CMOS_BCD(v) ((cmos[0x0b] & 4) ? (v) : to_bcd(v))
1.1       root     17418: 
1.1.1.8   root     17419: UINT8 cmos_read(int addr)
1.1       root     17420: {
1.1.1.8   root     17421:        static SYSTEMTIME time;
                   17422:        static UINT32 prev_sec = 0;
1.1       root     17423:        
1.1.1.8   root     17424:        switch(addr & 0x7f) {
                   17425:        case 0x00: CMOS_GET_TIME(); return(CMOS_BCD(time.wSecond));
                   17426:        case 0x02: CMOS_GET_TIME(); return(CMOS_BCD(time.wMinute));
                   17427:        case 0x04: CMOS_GET_TIME(); return(CMOS_BCD(time.wHour));
                   17428:        case 0x06: CMOS_GET_TIME(); return(time.wDayOfWeek + 1);
                   17429:        case 0x07: CMOS_GET_TIME(); return(CMOS_BCD(time.wDay));
                   17430:        case 0x08: CMOS_GET_TIME(); return(CMOS_BCD(time.wMonth));
                   17431:        case 0x09: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear));
                   17432: //     case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 2 ? 0x80 : 0));       // 2msec
                   17433:        case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 20 ? 0x80 : 0));      // precision of timeGetTime() may not be 1msec
                   17434:        case 0x15: return((MEMORY_END >> 10) & 0xff);
                   17435:        case 0x16: return((MEMORY_END >> 18) & 0xff);
                   17436:        case 0x17: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   17437:        case 0x18: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   17438:        case 0x30: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   17439:        case 0x31: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   17440:        case 0x32: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear / 100));
1.1       root     17441:        }
1.1.1.8   root     17442:        return(cmos[addr & 0x7f]);
1.1       root     17443: }
                   17444: 
1.1.1.7   root     17445: // kbd (a20)
                   17446: 
                   17447: void kbd_init()
                   17448: {
1.1.1.8   root     17449:        kbd_data = kbd_command = 0;
1.1.1.7   root     17450:        kbd_status = 0x18;
                   17451: }
                   17452: 
                   17453: UINT8 kbd_read_data()
                   17454: {
1.1.1.8   root     17455:        kbd_status &= ~1;
1.1.1.7   root     17456:        return(kbd_data);
                   17457: }
                   17458: 
                   17459: void kbd_write_data(UINT8 val)
                   17460: {
                   17461:        switch(kbd_command) {
                   17462:        case 0xd1:
                   17463:                i386_set_a20_line((val >> 1) & 1);
                   17464:                break;
                   17465:        }
                   17466:        kbd_command = 0;
1.1.1.8   root     17467:        kbd_status &= ~8;
1.1.1.7   root     17468: }
                   17469: 
                   17470: UINT8 kbd_read_status()
                   17471: {
                   17472:        return(kbd_status);
                   17473: }
                   17474: 
                   17475: void kbd_write_command(UINT8 val)
                   17476: {
                   17477:        switch(val) {
                   17478:        case 0xd0:
                   17479:                kbd_data = ((m_a20_mask >> 19) & 2) | 1;
1.1.1.8   root     17480:                kbd_status |= 1;
1.1.1.7   root     17481:                break;
                   17482:        case 0xdd:
                   17483:                i386_set_a20_line(0);
                   17484:                break;
                   17485:        case 0xdf:
                   17486:                i386_set_a20_line(1);
                   17487:                break;
1.1.1.26  root     17488:        case 0xf0: case 0xf1: case 0xf2: case 0xf3: case 0xf4: case 0xf5: case 0xf6: case 0xf7:
                   17489:        case 0xf8: case 0xf9: case 0xfa: case 0xfb: case 0xfc: case 0xfd: case 0xfe: case 0xff:
1.1.1.7   root     17490:                if(!(val & 1)) {
1.1.1.8   root     17491:                        if((cmos[0x0f] & 0x7f) == 5) {
1.1.1.7   root     17492:                                // reset pic
                   17493:                                pic_init();
                   17494:                                pic[0].irr = pic[1].irr = 0x00;
                   17495:                                pic[0].imr = pic[1].imr = 0xff;
                   17496:                        }
                   17497:                        CPU_RESET_CALL(CPU_MODEL);
                   17498:                        i386_jmp_far(0x40, 0x67);
                   17499:                }
                   17500:                i386_set_a20_line((val >> 1) & 1);
                   17501:                break;
                   17502:        }
                   17503:        kbd_command = val;
1.1.1.8   root     17504:        kbd_status |= 8;
1.1.1.7   root     17505: }
                   17506: 
1.1.1.9   root     17507: // vga
                   17508: 
                   17509: UINT8 vga_read_status()
                   17510: {
                   17511:        // 60hz
                   17512:        static const int period[3] = {16, 17, 17};
                   17513:        static int index = 0;
                   17514:        UINT32 time = timeGetTime() % period[index];
                   17515:        
                   17516:        index = (index + 1) % 3;
1.1.1.14  root     17517:        return((time < 4 ? 0x08 : 0) | (time == 0 ? 0 : 0x01));
1.1.1.9   root     17518: }
                   17519: 
1.1       root     17520: // i/o bus
                   17521: 
1.1.1.29  root     17522: // this is ugly patch for SW1US.EXE, it sometimes mistakely read/write 01h-10h for serial I/O
                   17523: //#define SW1US_PATCH
                   17524: 
1.1.1.25  root     17525: UINT8 read_io_byte(offs_t addr)
1.1.1.33  root     17526: #ifdef USE_DEBUGGER
1.1.1.25  root     17527: {
1.1.1.33  root     17528:        if(now_debugging) {
                   17529:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   17530:                        if(in_break_point.table[i].status == 1) {
                   17531:                                if(addr == in_break_point.table[i].addr) {
                   17532:                                        in_break_point.hit = i + 1;
                   17533:                                        now_suspended = true;
                   17534:                                        break;
                   17535:                                }
                   17536:                        }
                   17537:                }
1.1.1.25  root     17538:        }
1.1.1.33  root     17539:        return(debugger_read_io_byte(addr));
1.1.1.25  root     17540: }
1.1.1.33  root     17541: UINT8 debugger_read_io_byte(offs_t addr)
1.1.1.25  root     17542: #endif
1.1       root     17543: {
1.1.1.33  root     17544:        UINT8 val = 0xff;
                   17545:        
1.1       root     17546:        switch(addr) {
1.1.1.29  root     17547: #ifdef SW1US_PATCH
                   17548:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   17549:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
1.1.1.33  root     17550:                val = sio_read(0, addr - 1);
                   17551:                break;
1.1.1.29  root     17552: #else
1.1.1.25  root     17553:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   17554:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
1.1.1.33  root     17555:                val = dma_read(0, addr);
                   17556:                break;
1.1.1.29  root     17557: #endif
1.1.1.25  root     17558:        case 0x20: case 0x21:
1.1.1.33  root     17559:                val = pic_read(0, addr);
                   17560:                break;
1.1.1.25  root     17561:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1.1.33  root     17562:                val = pit_read(addr & 0x03);
                   17563:                break;
1.1.1.7   root     17564:        case 0x60:
1.1.1.33  root     17565:                val = kbd_read_data();
                   17566:                break;
1.1.1.9   root     17567:        case 0x61:
1.1.1.33  root     17568:                val = system_port;
                   17569:                break;
1.1.1.7   root     17570:        case 0x64:
1.1.1.33  root     17571:                val = kbd_read_status();
                   17572:                break;
1.1       root     17573:        case 0x71:
1.1.1.33  root     17574:                val = cmos_read(cmos_addr);
                   17575:                break;
1.1.1.25  root     17576:        case 0x81:
1.1.1.33  root     17577:                val = dma_page_read(0, 2);
                   17578:                break;
1.1.1.25  root     17579:        case 0x82:
1.1.1.33  root     17580:                val = dma_page_read(0, 3);
                   17581:                break;
1.1.1.25  root     17582:        case 0x83:
1.1.1.33  root     17583:                val = dma_page_read(0, 1);
                   17584:                break;
1.1.1.25  root     17585:        case 0x87:
1.1.1.33  root     17586:                val = dma_page_read(0, 0);
                   17587:                break;
1.1.1.25  root     17588:        case 0x89:
1.1.1.33  root     17589:                val = dma_page_read(1, 2);
                   17590:                break;
1.1.1.25  root     17591:        case 0x8a:
1.1.1.33  root     17592:                val = dma_page_read(1, 3);
                   17593:                break;
1.1.1.25  root     17594:        case 0x8b:
1.1.1.33  root     17595:                val = dma_page_read(1, 1);
                   17596:                break;
1.1.1.25  root     17597:        case 0x8f:
1.1.1.33  root     17598:                val = dma_page_read(1, 0);
                   17599:                break;
1.1       root     17600:        case 0x92:
1.1.1.33  root     17601:                val = (m_a20_mask >> 19) & 2;
                   17602:                break;
1.1.1.25  root     17603:        case 0xa0: case 0xa1:
1.1.1.33  root     17604:                val = pic_read(1, addr);
                   17605:                break;
1.1.1.25  root     17606:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   17607:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.33  root     17608:                val = dma_read(1, (addr - 0xc0) >> 1);
                   17609:                break;
1.1.1.37  root     17610:        case 0x278: case 0x279: case 0x27a:
                   17611:                val = pio_read(1, addr);
                   17612:                break;
1.1.1.29  root     17613:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
1.1.1.33  root     17614:                val = sio_read(3, addr);
                   17615:                break;
1.1.1.25  root     17616:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
1.1.1.33  root     17617:                val = sio_read(1, addr);
                   17618:                break;
1.1.1.25  root     17619:        case 0x378: case 0x379: case 0x37a:
1.1.1.33  root     17620:                val = pio_read(0, addr);
                   17621:                break;
1.1.1.25  root     17622:        case 0x3ba: case 0x3da:
1.1.1.33  root     17623:                val = vga_read_status();
                   17624:                break;
1.1.1.37  root     17625:        case 0x3bc: case 0x3bd: case 0x3be:
                   17626:                val = pio_read(2, addr);
                   17627:                break;
1.1.1.29  root     17628:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
1.1.1.33  root     17629:                val = sio_read(2, addr);
                   17630:                break;
1.1.1.25  root     17631:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
1.1.1.33  root     17632:                val = sio_read(0, addr);
                   17633:                break;
1.1       root     17634:        default:
1.1.1.33  root     17635: //             fatalerror("unknown inb %4x\n", addr);
1.1       root     17636:                break;
                   17637:        }
1.1.1.33  root     17638: #ifdef ENABLE_DEBUG_IOPORT
                   17639:        if(fp_debug_log != NULL) {
                   17640:                fprintf(fp_debug_log, "inb %04X, %02X\n", addr, val);
                   17641:        }
                   17642: #endif
                   17643:        return(val);
1.1       root     17644: }
                   17645: 
                   17646: UINT16 read_io_word(offs_t addr)
                   17647: {
                   17648:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8));
                   17649: }
                   17650: 
1.1.1.33  root     17651: #ifdef USE_DEBUGGER
                   17652: UINT16 debugger_read_io_word(offs_t addr)
                   17653: {
                   17654:        return(debugger_read_io_byte(addr) | (debugger_read_io_byte(addr + 1) << 8));
                   17655: }
                   17656: #endif
                   17657: 
1.1       root     17658: UINT32 read_io_dword(offs_t addr)
                   17659: {
                   17660:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8) | (read_io_byte(addr + 2) << 16) | (read_io_byte(addr + 3) << 24));
                   17661: }
                   17662: 
1.1.1.33  root     17663: #ifdef USE_DEBUGGER
                   17664: UINT32 debugger_read_io_dword(offs_t addr)
                   17665: {
                   17666:        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));
                   17667: }
                   17668: #endif
                   17669: 
1.1       root     17670: void write_io_byte(offs_t addr, UINT8 val)
1.1.1.33  root     17671: #ifdef USE_DEBUGGER
                   17672: {
                   17673:        if(now_debugging) {
                   17674:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   17675:                        if(out_break_point.table[i].status == 1) {
                   17676:                                if(addr == out_break_point.table[i].addr) {
                   17677:                                        out_break_point.hit = i + 1;
                   17678:                                        now_suspended = true;
                   17679:                                        break;
                   17680:                                }
                   17681:                        }
                   17682:                }
                   17683:        }
                   17684:        debugger_write_io_byte(addr, val);
                   17685: }
                   17686: void debugger_write_io_byte(offs_t addr, UINT8 val)
                   17687: #endif
1.1       root     17688: {
1.1.1.25  root     17689: #ifdef ENABLE_DEBUG_IOPORT
1.1.1.33  root     17690:        if(fp_debug_log != NULL) {
                   17691:                fprintf(fp_debug_log, "outb %04X, %02X\n", addr, val);
1.1.1.25  root     17692:        }
                   17693: #endif
1.1       root     17694:        switch(addr) {
1.1.1.29  root     17695: #ifdef SW1US_PATCH
                   17696:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   17697:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
                   17698:                sio_write(0, addr - 1, val);
                   17699:                break;
                   17700: #else
1.1.1.25  root     17701:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   17702:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
                   17703:                dma_write(0, addr, val);
                   17704:                break;
1.1.1.29  root     17705: #endif
1.1.1.25  root     17706:        case 0x20: case 0x21:
1.1       root     17707:                pic_write(0, addr, val);
                   17708:                break;
1.1.1.25  root     17709:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1       root     17710:                pit_write(addr & 0x03, val);
                   17711:                break;
1.1.1.7   root     17712:        case 0x60:
                   17713:                kbd_write_data(val);
                   17714:                break;
1.1.1.9   root     17715:        case 0x61:
                   17716:                if((system_port & 3) != 3 && (val & 3) == 3) {
                   17717:                        // beep on
                   17718: //                     MessageBeep(-1);
                   17719:                } else if((system_port & 3) == 3 && (val & 3) != 3) {
                   17720:                        // beep off
                   17721:                }
                   17722:                system_port = val;
                   17723:                break;
1.1       root     17724:        case 0x64:
1.1.1.7   root     17725:                kbd_write_command(val);
1.1       root     17726:                break;
                   17727:        case 0x70:
                   17728:                cmos_addr = val;
                   17729:                break;
                   17730:        case 0x71:
1.1.1.8   root     17731:                cmos_write(cmos_addr, val);
1.1       root     17732:                break;
1.1.1.25  root     17733:        case 0x81:
                   17734:                dma_page_write(0, 2, val);
                   17735:        case 0x82:
                   17736:                dma_page_write(0, 3, val);
                   17737:        case 0x83:
                   17738:                dma_page_write(0, 1, val);
                   17739:        case 0x87:
                   17740:                dma_page_write(0, 0, val);
                   17741:        case 0x89:
                   17742:                dma_page_write(1, 2, val);
                   17743:        case 0x8a:
                   17744:                dma_page_write(1, 3, val);
                   17745:        case 0x8b:
                   17746:                dma_page_write(1, 1, val);
                   17747:        case 0x8f:
                   17748:                dma_page_write(1, 0, val);
1.1       root     17749:        case 0x92:
1.1.1.7   root     17750:                i386_set_a20_line((val >> 1) & 1);
1.1       root     17751:                break;
1.1.1.25  root     17752:        case 0xa0: case 0xa1:
1.1       root     17753:                pic_write(1, addr, val);
                   17754:                break;
1.1.1.25  root     17755:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   17756:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.26  root     17757:                dma_write(1, (addr - 0xc0) >> 1, val);
1.1.1.25  root     17758:                break;
1.1.1.35  root     17759: #ifdef USE_SERVICE_THREAD
                   17760:        case 0xf7:
                   17761:                // dummy i/o for BIOS/DOS service
1.1.1.36  root     17762:                if(in_service && cursor_moved) {
                   17763:                        // update cursor position before service is done
                   17764:                        pcbios_update_cursor_position();
                   17765:                        cursor_moved = false;
                   17766:                }
1.1.1.35  root     17767:                finish_service_loop();
                   17768:                break;
                   17769: #endif
1.1.1.37  root     17770:        case 0x278: case 0x279: case 0x27a:
                   17771:                pio_write(1, addr, val);
                   17772:                break;
1.1.1.29  root     17773:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
                   17774:                sio_write(3, addr, val);
                   17775:                break;
1.1.1.25  root     17776:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
                   17777:                sio_write(1, addr, val);
                   17778:                break;
                   17779:        case 0x378: case 0x379: case 0x37a:
                   17780:                pio_write(0, addr, val);
                   17781:                break;
1.1.1.37  root     17782:        case 0x3bc: case 0x3bd: case 0x3be:
                   17783:                pio_write(2, addr, val);
                   17784:                break;
1.1.1.29  root     17785:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
                   17786:                sio_write(2, addr, val);
                   17787:                break;
1.1.1.25  root     17788:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
                   17789:                sio_write(0, addr, val);
                   17790:                break;
1.1       root     17791:        default:
1.1.1.33  root     17792: //             fatalerror("unknown outb %4x,%2x\n", addr, val);
1.1       root     17793:                break;
                   17794:        }
                   17795: }
                   17796: 
                   17797: void write_io_word(offs_t addr, UINT16 val)
                   17798: {
                   17799:        write_io_byte(addr + 0, (val >> 0) & 0xff);
                   17800:        write_io_byte(addr + 1, (val >> 8) & 0xff);
                   17801: }
                   17802: 
1.1.1.33  root     17803: #ifdef USE_DEBUGGER
                   17804: void debugger_write_io_word(offs_t addr, UINT16 val)
                   17805: {
                   17806:        debugger_write_io_byte(addr + 0, (val >> 0) & 0xff);
                   17807:        debugger_write_io_byte(addr + 1, (val >> 8) & 0xff);
                   17808: }
                   17809: #endif
                   17810: 
1.1       root     17811: void write_io_dword(offs_t addr, UINT32 val)
                   17812: {
                   17813:        write_io_byte(addr + 0, (val >>  0) & 0xff);
                   17814:        write_io_byte(addr + 1, (val >>  8) & 0xff);
                   17815:        write_io_byte(addr + 2, (val >> 16) & 0xff);
                   17816:        write_io_byte(addr + 3, (val >> 24) & 0xff);
                   17817: }
1.1.1.33  root     17818: 
                   17819: #ifdef USE_DEBUGGER
                   17820: void debugger_write_io_dword(offs_t addr, UINT32 val)
                   17821: {
                   17822:        debugger_write_io_byte(addr + 0, (val >>  0) & 0xff);
                   17823:        debugger_write_io_byte(addr + 1, (val >>  8) & 0xff);
                   17824:        debugger_write_io_byte(addr + 2, (val >> 16) & 0xff);
                   17825:        debugger_write_io_byte(addr + 3, (val >> 24) & 0xff);
                   17826: }
                   17827: #endif

unix.superglobalmegacorp.com

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