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

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.39! root     5262: mcb_t *msdos_mcb_create(int mcb_seg, UINT8 mz, UINT16 psp, int paragraphs, char *prog_name)
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.1.39! root     5269:        
        !          5270:        if(prog_name != NULL) {
        !          5271:                memset(mcb->prog_name, 0, 8);
        !          5272:                memcpy(mcb->prog_name, prog_name, min(8, strlen(prog_name)));
        !          5273:        }
1.1       root     5274:        return(mcb);
                   5275: }
                   5276: 
1.1.1.39! root     5277: mcb_t *msdos_mcb_create(int mcb_seg, UINT8 mz, UINT16 psp, int paragraphs)
        !          5278: {
        !          5279:        return(msdos_mcb_create(mcb_seg, mz, psp, paragraphs, NULL));
        !          5280: }
        !          5281: 
1.1       root     5282: void msdos_mcb_check(mcb_t *mcb)
                   5283: {
                   5284:        if(!(mcb->mz == 'M' || mcb->mz == 'Z')) {
1.1.1.28  root     5285:                #if 0
                   5286:                        // shutdown now !!!
                   5287:                        fatalerror("broken memory control block\n");
                   5288:                #else
                   5289:                        // return error code and continue
                   5290:                        throw(0x07); // broken memory control block
                   5291:                #endif
1.1       root     5292:        }
                   5293: }
                   5294: 
1.1.1.39! root     5295: void msdos_mem_split(int seg, int paragraphs)
1.1       root     5296: {
                   5297:        int mcb_seg = seg - 1;
                   5298:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5299:        msdos_mcb_check(mcb);
                   5300:        
1.1.1.30  root     5301:        if(mcb->paragraphs > paragraphs) {
1.1       root     5302:                int new_seg = mcb_seg + 1 + paragraphs;
1.1.1.30  root     5303:                int new_paragraphs = mcb->paragraphs - paragraphs - 1;
1.1       root     5304:                
                   5305:                msdos_mcb_create(new_seg, mcb->mz, 0, new_paragraphs);
                   5306:                mcb->mz = 'M';
1.1.1.30  root     5307:                mcb->paragraphs = paragraphs;
1.1       root     5308:        }
                   5309: }
                   5310: 
                   5311: void msdos_mem_merge(int seg)
                   5312: {
                   5313:        int mcb_seg = seg - 1;
                   5314:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5315:        msdos_mcb_check(mcb);
                   5316:        
                   5317:        while(1) {
                   5318:                if(mcb->mz == 'Z') {
                   5319:                        break;
                   5320:                }
1.1.1.30  root     5321:                int next_seg = mcb_seg + 1 + mcb->paragraphs;
1.1       root     5322:                mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5323:                msdos_mcb_check(next_mcb);
                   5324:                
                   5325:                if(next_mcb->psp != 0) {
                   5326:                        break;
                   5327:                }
                   5328:                mcb->mz = next_mcb->mz;
1.1.1.30  root     5329:                mcb->paragraphs = mcb->paragraphs + 1 + next_mcb->paragraphs;
1.1       root     5330:        }
                   5331: }
                   5332: 
1.1.1.8   root     5333: int msdos_mem_alloc(int mcb_seg, int paragraphs, int new_process)
1.1       root     5334: {
                   5335:        while(1) {
                   5336:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     5337:                bool last_block;
1.1       root     5338:                
1.1.1.14  root     5339:                if(mcb->psp == 0) {
                   5340:                        msdos_mem_merge(mcb_seg + 1);
                   5341:                } else {
                   5342:                        msdos_mcb_check(mcb);
                   5343:                }
1.1.1.33  root     5344:                if(!(last_block = (mcb->mz == 'Z'))) {
                   5345:                        // check if the next is dummy mcb to link to umb
                   5346:                        int next_seg = mcb_seg + 1 + mcb->paragraphs;
                   5347:                        mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5348:                        last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
                   5349:                }
                   5350:                if(!(new_process && !last_block)) {
1.1.1.30  root     5351:                        if(mcb->psp == 0 && mcb->paragraphs >= paragraphs) {
1.1       root     5352:                                msdos_mem_split(mcb_seg + 1, paragraphs);
                   5353:                                mcb->psp = current_psp;
                   5354:                                return(mcb_seg + 1);
                   5355:                        }
                   5356:                }
                   5357:                if(mcb->mz == 'Z') {
                   5358:                        break;
                   5359:                }
1.1.1.30  root     5360:                mcb_seg += 1 + mcb->paragraphs;
1.1       root     5361:        }
                   5362:        return(-1);
                   5363: }
                   5364: 
                   5365: int msdos_mem_realloc(int seg, int paragraphs, int *max_paragraphs)
                   5366: {
                   5367:        int mcb_seg = seg - 1;
                   5368:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5369:        msdos_mcb_check(mcb);
1.1.1.30  root     5370:        int current_paragraphs = mcb->paragraphs;
1.1       root     5371:        
                   5372:        msdos_mem_merge(seg);
1.1.1.30  root     5373:        if(paragraphs > mcb->paragraphs) {
1.1.1.14  root     5374:                if(max_paragraphs) {
1.1.1.30  root     5375:                        *max_paragraphs = mcb->paragraphs;
1.1.1.14  root     5376:                }
1.1       root     5377:                msdos_mem_split(seg, current_paragraphs);
                   5378:                return(-1);
                   5379:        }
                   5380:        msdos_mem_split(seg, paragraphs);
                   5381:        return(0);
                   5382: }
                   5383: 
                   5384: void msdos_mem_free(int seg)
                   5385: {
                   5386:        int mcb_seg = seg - 1;
                   5387:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5388:        msdos_mcb_check(mcb);
                   5389:        
                   5390:        mcb->psp = 0;
                   5391:        msdos_mem_merge(seg);
                   5392: }
                   5393: 
1.1.1.8   root     5394: int msdos_mem_get_free(int mcb_seg, int new_process)
1.1       root     5395: {
                   5396:        int max_paragraphs = 0;
                   5397:        
                   5398:        while(1) {
                   5399:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     5400:                bool last_block;
                   5401:                
1.1       root     5402:                msdos_mcb_check(mcb);
                   5403:                
1.1.1.33  root     5404:                if(!(last_block = (mcb->mz == 'Z'))) {
                   5405:                        // check if the next is dummy mcb to link to umb
                   5406:                        int next_seg = mcb_seg + 1 + mcb->paragraphs;
                   5407:                        mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5408:                        last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
                   5409:                }
                   5410:                if(!(new_process && !last_block)) {
1.1.1.30  root     5411:                        if(mcb->psp == 0 && mcb->paragraphs > max_paragraphs) {
                   5412:                                max_paragraphs = mcb->paragraphs;
1.1       root     5413:                        }
                   5414:                }
                   5415:                if(mcb->mz == 'Z') {
                   5416:                        break;
                   5417:                }
1.1.1.30  root     5418:                mcb_seg += 1 + mcb->paragraphs;
1.1       root     5419:        }
1.1.1.14  root     5420:        return(max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs);
1.1       root     5421: }
                   5422: 
1.1.1.8   root     5423: int msdos_mem_get_last_mcb(int mcb_seg, UINT16 psp)
                   5424: {
                   5425:        int last_seg = -1;
                   5426:        
                   5427:        while(1) {
                   5428:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5429:                msdos_mcb_check(mcb);
                   5430:                
1.1.1.14  root     5431:                if(mcb->psp == psp) {
1.1.1.8   root     5432:                        last_seg = mcb_seg;
                   5433:                }
1.1.1.14  root     5434:                if(mcb->mz == 'Z') {
                   5435:                        break;
                   5436:                }
1.1.1.30  root     5437:                mcb_seg += 1 + mcb->paragraphs;
1.1.1.8   root     5438:        }
                   5439:        return(last_seg);
                   5440: }
                   5441: 
1.1.1.19  root     5442: int msdos_mem_get_umb_linked()
                   5443: {
1.1.1.33  root     5444:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   5445:        msdos_mcb_check(mcb);
1.1.1.19  root     5446:        
1.1.1.33  root     5447:        if(mcb->mz == 'M') {
                   5448:                return(-1);
1.1.1.19  root     5449:        }
                   5450:        return(0);
                   5451: }
                   5452: 
1.1.1.33  root     5453: void msdos_mem_link_umb()
1.1.1.19  root     5454: {
1.1.1.33  root     5455:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   5456:        msdos_mcb_check(mcb);
1.1.1.19  root     5457:        
1.1.1.33  root     5458:        mcb->mz = 'M';
                   5459:        mcb->paragraphs = (UMB_TOP >> 4) - (MEMORY_END >> 4);
1.1.1.39! root     5460:        
        !          5461:        ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked |= 0x01;
1.1.1.19  root     5462: }
                   5463: 
1.1.1.33  root     5464: void msdos_mem_unlink_umb()
1.1.1.19  root     5465: {
1.1.1.33  root     5466:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   5467:        msdos_mcb_check(mcb);
1.1.1.19  root     5468:        
1.1.1.33  root     5469:        mcb->mz = 'Z';
                   5470:        mcb->paragraphs = 0;
1.1.1.39! root     5471:        
        !          5472:        ((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked &= ~0x01;
1.1.1.19  root     5473: }
                   5474: 
1.1.1.29  root     5475: #ifdef SUPPORT_HMA
                   5476: 
                   5477: hma_mcb_t *msdos_hma_mcb_create(int offset, int owner, int size, int next)
                   5478: {
                   5479:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5480:        
                   5481:        mcb->ms[0] = 'M';
                   5482:        mcb->ms[1] = 'S';
                   5483:        mcb->owner = owner;
                   5484:        mcb->size = size;
                   5485:        mcb->next = next;
                   5486:        return(mcb);
                   5487: }
                   5488: 
                   5489: bool msdos_is_hma_mcb_valid(hma_mcb_t *mcb)
                   5490: {
                   5491:        return(mcb->ms[0] == 'M' && mcb->ms[1] == 'S');
                   5492: }
                   5493: 
                   5494: int msdos_hma_mem_split(int offset, int size)
                   5495: {
                   5496:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5497:        
                   5498:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5499:                return(-1);
                   5500:        }
                   5501:        if(mcb->size >= size + 0x10) {
                   5502:                int new_offset = offset + 0x10 + size;
                   5503:                int new_size = mcb->size - 0x10 - size;
                   5504:                
                   5505:                msdos_hma_mcb_create(new_offset, 0, new_size, mcb->next);
                   5506:                mcb->size = size;
                   5507:                mcb->next = new_offset;
                   5508:                return(0);
                   5509:        }
                   5510:        return(-1);
                   5511: }
                   5512: 
                   5513: void msdos_hma_mem_merge(int offset)
                   5514: {
                   5515:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5516:        
                   5517:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5518:                return;
                   5519:        }
                   5520:        while(1) {
                   5521:                if(mcb->next == 0) {
                   5522:                        break;
                   5523:                }
                   5524:                hma_mcb_t *next_mcb = (hma_mcb_t *)(mem + 0xffff0 + mcb->next);
                   5525:                
                   5526:                if(!msdos_is_hma_mcb_valid(next_mcb)) {
                   5527:                        return;
                   5528:                }
                   5529:                if(next_mcb->owner != 0) {
                   5530:                        break;
                   5531:                }
                   5532:                mcb->size += 0x10 + next_mcb->size;
                   5533:                mcb->next = next_mcb->next;
                   5534:        }
                   5535: }
                   5536: 
                   5537: int msdos_hma_mem_alloc(int size, UINT16 owner)
                   5538: {
                   5539:        int offset = 0x10; // first mcb in HMA
                   5540:        
                   5541:        while(1) {
                   5542:                hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5543:                
                   5544:                if(!msdos_is_hma_mcb_valid(mcb)) {
                   5545:                        return(-1);
                   5546:                }
                   5547:                if(mcb->owner == 0) {
                   5548:                        msdos_hma_mem_merge(offset);
                   5549:                }
                   5550:                if(mcb->owner == 0 && mcb->size >= size) {
                   5551:                        msdos_hma_mem_split(offset, size);
                   5552:                        mcb->owner = owner;
                   5553:                        return(offset);
                   5554:                }
                   5555:                if(mcb->next == 0) {
                   5556:                        break;
                   5557:                }
                   5558:                offset = mcb->next;
                   5559:        }
                   5560:        return(-1);
                   5561: }
                   5562: 
                   5563: int msdos_hma_mem_realloc(int offset, int size)
                   5564: {
                   5565:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5566:        
                   5567:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5568:                return(-1);
                   5569:        }
                   5570:        if(mcb->size < size) {
                   5571:                return(-1);
                   5572:        }
                   5573:        msdos_hma_mem_split(offset, size);
                   5574:        return(0);
                   5575: }
                   5576: 
                   5577: void msdos_hma_mem_free(int offset)
                   5578: {
                   5579:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5580:        
                   5581:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5582:                return;
                   5583:        }
                   5584:        mcb->owner = 0;
                   5585:        msdos_hma_mem_merge(offset);
                   5586: }
                   5587: 
                   5588: int msdos_hma_mem_get_free(int *available_offset)
                   5589: {
                   5590:        int offset = 0x10; // first mcb in HMA
                   5591:        int size = 0;
                   5592:        
                   5593:        while(1) {
                   5594:                hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5595:                
                   5596:                if(!msdos_is_hma_mcb_valid(mcb)) {
                   5597:                        return(0);
                   5598:                }
                   5599:                if(mcb->owner == 0 && size < mcb->size) {
                   5600:                        if(available_offset != NULL) {
                   5601:                                *available_offset = offset;
                   5602:                        }
                   5603:                        size = mcb->size;
                   5604:                }
                   5605:                if(mcb->next == 0) {
                   5606:                        break;
                   5607:                }
                   5608:                offset = mcb->next;
                   5609:        }
                   5610:        return(size);
                   5611: }
                   5612: 
                   5613: #endif
                   5614: 
1.1       root     5615: // environment
                   5616: 
                   5617: void msdos_env_set_argv(int env_seg, char *argv)
                   5618: {
                   5619:        char *dst = (char *)(mem + (env_seg << 4));
                   5620:        
                   5621:        while(1) {
                   5622:                if(dst[0] == 0) {
                   5623:                        break;
                   5624:                }
                   5625:                dst += strlen(dst) + 1;
                   5626:        }
                   5627:        *dst++ = 0; // end of environment
                   5628:        *dst++ = 1; // top of argv[0]
                   5629:        *dst++ = 0;
                   5630:        memcpy(dst, argv, strlen(argv));
                   5631:        dst += strlen(argv);
                   5632:        *dst++ = 0;
                   5633:        *dst++ = 0;
                   5634: }
                   5635: 
                   5636: char *msdos_env_get_argv(int env_seg)
                   5637: {
                   5638:        static char env[ENV_SIZE];
                   5639:        char *src = env;
                   5640:        
                   5641:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   5642:        while(1) {
                   5643:                if(src[0] == 0) {
                   5644:                        if(src[1] == 1) {
                   5645:                                return(src + 3);
                   5646:                        }
                   5647:                        break;
                   5648:                }
                   5649:                src += strlen(src) + 1;
                   5650:        }
                   5651:        return(NULL);
                   5652: }
                   5653: 
                   5654: char *msdos_env_get(int env_seg, const char *name)
                   5655: {
                   5656:        static char env[ENV_SIZE];
                   5657:        char *src = env;
                   5658:        
                   5659:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   5660:        while(1) {
                   5661:                if(src[0] == 0) {
                   5662:                        break;
                   5663:                }
                   5664:                int len = strlen(src);
                   5665:                char *n = my_strtok(src, "=");
                   5666:                char *v = src + strlen(n) + 1;
                   5667:                
                   5668:                if(_stricmp(name, n) == 0) {
                   5669:                        return(v);
                   5670:                }
                   5671:                src += len + 1;
                   5672:        }
                   5673:        return(NULL);
                   5674: }
                   5675: 
                   5676: void msdos_env_set(int env_seg, char *name, char *value)
                   5677: {
                   5678:        char env[ENV_SIZE];
                   5679:        char *src = env;
                   5680:        char *dst = (char *)(mem + (env_seg << 4));
                   5681:        char *argv = msdos_env_get_argv(env_seg);
                   5682:        int done = 0;
                   5683:        
                   5684:        memcpy(src, dst, ENV_SIZE);
                   5685:        memset(dst, 0, ENV_SIZE);
                   5686:        while(1) {
                   5687:                if(src[0] == 0) {
                   5688:                        break;
                   5689:                }
                   5690:                int len = strlen(src);
                   5691:                char *n = my_strtok(src, "=");
                   5692:                char *v = src + strlen(n) + 1;
                   5693:                char tmp[1024];
                   5694:                
                   5695:                if(_stricmp(name, n) == 0) {
                   5696:                        sprintf(tmp, "%s=%s", n, value);
                   5697:                        done = 1;
                   5698:                } else {
                   5699:                        sprintf(tmp, "%s=%s", n, v);
                   5700:                }
                   5701:                memcpy(dst, tmp, strlen(tmp));
                   5702:                dst += strlen(tmp) + 1;
                   5703:                src += len + 1;
                   5704:        }
                   5705:        if(!done) {
                   5706:                char tmp[1024];
                   5707:                
                   5708:                sprintf(tmp, "%s=%s", name, value);
                   5709:                memcpy(dst, tmp, strlen(tmp));
                   5710:                dst += strlen(tmp) + 1;
                   5711:        }
                   5712:        if(argv) {
                   5713:                *dst++ = 0; // end of environment
                   5714:                *dst++ = 1; // top of argv[0]
                   5715:                *dst++ = 0;
                   5716:                memcpy(dst, argv, strlen(argv));
                   5717:                dst += strlen(argv);
                   5718:                *dst++ = 0;
                   5719:                *dst++ = 0;
                   5720:        }
                   5721: }
                   5722: 
                   5723: // process
                   5724: 
1.1.1.8   root     5725: psp_t *msdos_psp_create(int psp_seg, UINT16 mcb_seg, UINT16 parent_psp, UINT16 env_seg)
1.1       root     5726: {
                   5727:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   5728:        
                   5729:        memset(psp, 0, PSP_SIZE);
                   5730:        psp->exit[0] = 0xcd;
                   5731:        psp->exit[1] = 0x20;
1.1.1.8   root     5732:        psp->first_mcb = mcb_seg;
1.1       root     5733:        psp->far_call = 0xea;
                   5734:        psp->cpm_entry.w.l = 0xfff1;    // int 21h, retf
                   5735:        psp->cpm_entry.w.h = 0xf000;
                   5736:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   5737:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   5738:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   5739:        psp->parent_psp = parent_psp;
1.1.1.20  root     5740:        if(parent_psp == (UINT16)-1) {
                   5741:                for(int i = 0; i < 20; i++) {
                   5742:                        if(file_handler[i].valid) {
                   5743:                                psp->file_table[i] = i;
                   5744:                        } else {
                   5745:                                psp->file_table[i] = 0xff;
                   5746:                        }
1.1       root     5747:                }
1.1.1.20  root     5748:        } else {
                   5749:                memcpy(psp->file_table, ((psp_t *)(mem + (parent_psp << 4)))->file_table, 20);
1.1       root     5750:        }
                   5751:        psp->env_seg = env_seg;
                   5752:        psp->stack.w.l = REG16(SP);
1.1.1.3   root     5753:        psp->stack.w.h = SREG(SS);
1.1.1.14  root     5754:        psp->file_table_size = 20;
                   5755:        psp->file_table_ptr.w.l = 0x18;
                   5756:        psp->file_table_ptr.w.h = psp_seg;
1.1       root     5757:        psp->service[0] = 0xcd;
                   5758:        psp->service[1] = 0x21;
                   5759:        psp->service[2] = 0xcb;
                   5760:        return(psp);
                   5761: }
                   5762: 
1.1.1.20  root     5763: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg)
                   5764: {
                   5765:        if(psp_seg && fd < 20) {
                   5766:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   5767:                psp->file_table[fd] = value;
                   5768:        }
                   5769: }
                   5770: 
                   5771: int msdos_psp_get_file_table(int fd, int psp_seg)
                   5772: {
                   5773:        if(psp_seg && fd < 20) {
                   5774:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   5775:                fd = psp->file_table[fd];
                   5776:        }
                   5777:        return fd;
                   5778: }
                   5779: 
1.1       root     5780: int msdos_process_exec(char *cmd, param_block_t *param, UINT8 al)
                   5781: {
                   5782:        // load command file
                   5783:        int fd = -1;
                   5784:        int dos_command = 0;
1.1.1.24  root     5785:        char command[MAX_PATH], path[MAX_PATH], opt[MAX_PATH], *name = NULL, name_tmp[MAX_PATH];
1.1.1.38  root     5786:        char pipe_stdin_path[MAX_PATH] = {0};
                   5787:        char pipe_stdout_path[MAX_PATH] = {0};
1.1.1.39! root     5788:        char pipe_stderr_path[MAX_PATH] = {0};
1.1       root     5789:        
                   5790:        int opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   5791:        int opt_len = mem[opt_ofs];
                   5792:        memset(opt, 0, sizeof(opt));
                   5793:        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   5794:        
1.1.1.14  root     5795:        if(strlen(cmd) >= 5 && _stricmp(&cmd[strlen(cmd) - 4], ".BAT") == 0) {
                   5796:                // this is a batch file, run command.com
                   5797:                char tmp[MAX_PATH];
                   5798:                if(opt_len != 0) {
                   5799:                        sprintf(tmp, "/C %s %s", cmd, opt);
                   5800:                } else {
                   5801:                        sprintf(tmp, "/C %s", cmd);
                   5802:                }
                   5803:                strcpy(opt, tmp);
                   5804:                opt_len = strlen(opt);
                   5805:                mem[opt_ofs] = opt_len;
                   5806:                sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   5807:                strcpy(command, comspec_path);
                   5808:                strcpy(name_tmp, "COMMAND.COM");
                   5809:        } else {
                   5810:                if(_stricmp(cmd, "C:\\COMMAND.COM") == 0) {
                   5811:                        // redirect C:\COMMAND.COM to comspec_path
                   5812:                        strcpy(command, comspec_path);
                   5813:                } else {
                   5814:                        strcpy(command, cmd);
                   5815:                }
1.1.1.24  root     5816:                if(GetFullPathName(command, MAX_PATH, path, &name) == 0) {
                   5817:                        return(-1);
                   5818:                }
1.1.1.14  root     5819:                memset(name_tmp, 0, sizeof(name_tmp));
                   5820:                strcpy(name_tmp, name);
                   5821:                
                   5822:                // check command.com
1.1.1.38  root     5823:                if((_stricmp(name, "COMMAND.COM") == 0 || _stricmp(name, "COMMAND") == 0) && _access(comspec_path, 0) != 0) {
                   5824:                        // we can not load command.com, so run program directly if "command /c (program)" is specified
1.1.1.14  root     5825:                        if(opt_len == 0) {
                   5826: //                             process_t *current_process = msdos_process_info_get(current_psp);
                   5827:                                process_t *current_process = NULL;
                   5828:                                for(int i = 0; i < MAX_PROCESS; i++) {
                   5829:                                        if(process[i].psp == current_psp) {
                   5830:                                                current_process = &process[i];
                   5831:                                                break;
                   5832:                                        }
                   5833:                                }
                   5834:                                if(current_process != NULL) {
                   5835:                                        param->cmd_line.dw = current_process->dta.dw;
                   5836:                                        opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   5837:                                        opt_len = mem[opt_ofs];
                   5838:                                        memset(opt, 0, sizeof(opt));
                   5839:                                        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   5840:                                }
                   5841:                        }
                   5842:                        for(int i = 0; i < opt_len; i++) {
                   5843:                                if(opt[i] == ' ') {
                   5844:                                        continue;
                   5845:                                }
                   5846:                                if(opt[i] == '/' && (opt[i + 1] == 'c' || opt[i + 1] == 'C') && opt[i + 2] == ' ') {
                   5847:                                        for(int j = i + 3; j < opt_len; j++) {
                   5848:                                                if(opt[j] == ' ') {
                   5849:                                                        continue;
                   5850:                                                }
                   5851:                                                char *token = my_strtok(opt + j, " ");
                   5852:                                                
1.1.1.38  root     5853:                                                strcpy(command, token);
                   5854:                                                char tmp[MAX_PATH];
                   5855:                                                strcpy(tmp, token + strlen(token) + 1);
1.1.1.39! root     5856:                                                strcpy(opt, "");
        !          5857:                                                for(int i = 0; i < strlen(tmp); i++) {
        !          5858:                                                        if(tmp[i] != ' ') {
        !          5859:                                                                strcpy(opt, tmp + i);
        !          5860:                                                                break;
        !          5861:                                                        }
        !          5862:                                                }
        !          5863:                                                strcpy(tmp, opt);
1.1.1.38  root     5864:                                                
                   5865:                                                if(al == 0x00) {
1.1.1.39! root     5866:                                                        #define GET_FILE_PATH() { \
        !          5867:                                                                if(token[0] != '>' && token[0] != '<') { \
        !          5868:                                                                        token++; \
        !          5869:                                                                } \
        !          5870:                                                                token++; \
        !          5871:                                                                while(*token == ' ') { \
        !          5872:                                                                        token++; \
        !          5873:                                                                } \
        !          5874:                                                                char *ptr = token; \
        !          5875:                                                                while(*ptr != ' ' && *ptr != '\r' && *ptr != '\0') { \
        !          5876:                                                                        ptr++; \
        !          5877:                                                                } \
        !          5878:                                                                *ptr = '\0'; \
        !          5879:                                                        }
        !          5880:                                                        if((token = strstr(opt, "0<")) != NULL || (token = strstr(opt, "<")) != NULL) {
        !          5881:                                                                GET_FILE_PATH();
1.1.1.38  root     5882:                                                                strcpy(pipe_stdin_path, token);
                   5883:                                                                strcpy(opt, tmp);
                   5884:                                                        }
1.1.1.39! root     5885:                                                        if((token = strstr(opt, "1>")) != NULL || (token = strstr(opt, ">")) != NULL) {
        !          5886:                                                                GET_FILE_PATH();
1.1.1.38  root     5887:                                                                strcpy(pipe_stdout_path, token);
                   5888:                                                                strcpy(opt, tmp);
                   5889:                                                        }
1.1.1.39! root     5890:                                                        if((token = strstr(opt, "2>")) != NULL) {
        !          5891:                                                                GET_FILE_PATH();
        !          5892:                                                                strcpy(pipe_stderr_path, token);
        !          5893:                                                                strcpy(opt, tmp);
        !          5894:                                                        }
        !          5895:                                                        #undef GET_FILE_PATH
        !          5896:                                                        
        !          5897:                                                        if((token = strstr(opt, "0<")) != NULL) {
        !          5898:                                                                *token = '\0';
        !          5899:                                                        }
        !          5900:                                                        if((token = strstr(opt, "1>")) != NULL) {
        !          5901:                                                                *token = '\0';
        !          5902:                                                        }
        !          5903:                                                        if((token = strstr(opt, "2>")) != NULL) {
        !          5904:                                                                *token = '\0';
        !          5905:                                                        }
1.1.1.38  root     5906:                                                        if((token = strstr(opt, "<")) != NULL) {
                   5907:                                                                *token = '\0';
                   5908:                                                        }
                   5909:                                                        if((token = strstr(opt, ">")) != NULL) {
                   5910:                                                                *token = '\0';
                   5911:                                                        }
1.1.1.14  root     5912:                                                }
1.1.1.39! root     5913:                                                for(int i = strlen(opt) - 1; i >= 0 && opt[i] == ' '; i--) {
        !          5914:                                                        opt[i] = '\0';
        !          5915:                                                }
1.1.1.38  root     5916:                                                opt_len = strlen(opt);
                   5917:                                                mem[opt_ofs] = opt_len;
                   5918:                                                sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   5919:                                                dos_command = 1;
1.1.1.14  root     5920:                                                break;
1.1       root     5921:                                        }
                   5922:                                }
1.1.1.14  root     5923:                                break;
1.1       root     5924:                        }
                   5925:                }
                   5926:        }
                   5927:        
                   5928:        // load command file
                   5929:        strcpy(path, command);
                   5930:        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   5931:                sprintf(path, "%s.COM", command);
                   5932:                if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   5933:                        sprintf(path, "%s.EXE", command);
                   5934:                        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
1.1.1.14  root     5935:                                sprintf(path, "%s.BAT", command);
                   5936:                                if(_access(path, 0) == 0) {
                   5937:                                        // this is a batch file, run command.com
                   5938:                                        char tmp[MAX_PATH];
                   5939:                                        if(opt_len != 0) {
                   5940:                                                sprintf(tmp, "/C %s %s", path, opt);
                   5941:                                        } else {
                   5942:                                                sprintf(tmp, "/C %s", path);
                   5943:                                        }
                   5944:                                        strcpy(opt, tmp);
                   5945:                                        opt_len = strlen(opt);
                   5946:                                        mem[opt_ofs] = opt_len;
                   5947:                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   5948:                                        strcpy(path, comspec_path);
                   5949:                                        strcpy(name_tmp, "COMMAND.COM");
                   5950:                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   5951:                                } else {
                   5952:                                        // search path in parent environments
                   5953:                                        psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
                   5954:                                        char *env = msdos_env_get(parent_psp->env_seg, "PATH");
                   5955:                                        if(env != NULL) {
                   5956:                                                char env_path[4096];
                   5957:                                                strcpy(env_path, env);
                   5958:                                                char *token = my_strtok(env_path, ";");
                   5959:                                                
                   5960:                                                while(token != NULL) {
                   5961:                                                        if(strlen(token) != 0) {
                   5962:                                                                sprintf(path, "%s", msdos_combine_path(token, command));
                   5963:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   5964:                                                                        break;
                   5965:                                                                }
                   5966:                                                                sprintf(path, "%s.COM", msdos_combine_path(token, command));
                   5967:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   5968:                                                                        break;
                   5969:                                                                }
                   5970:                                                                sprintf(path, "%s.EXE", msdos_combine_path(token, command));
                   5971:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   5972:                                                                        break;
                   5973:                                                                }
                   5974:                                                                sprintf(path, "%s.BAT", msdos_combine_path(token, command));
                   5975:                                                                if(_access(path, 0) == 0) {
                   5976:                                                                        // this is a batch file, run command.com
                   5977:                                                                        char tmp[MAX_PATH];
                   5978:                                                                        if(opt_len != 0) {
                   5979:                                                                                sprintf(tmp, "/C %s %s", path, opt);
                   5980:                                                                        } else {
                   5981:                                                                                sprintf(tmp, "/C %s", path);
                   5982:                                                                        }
                   5983:                                                                        strcpy(opt, tmp);
                   5984:                                                                        opt_len = strlen(opt);
                   5985:                                                                        mem[opt_ofs] = opt_len;
                   5986:                                                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   5987:                                                                        strcpy(path, comspec_path);
                   5988:                                                                        strcpy(name_tmp, "COMMAND.COM");
                   5989:                                                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   5990:                                                                        break;
                   5991:                                                                }
1.1.1.8   root     5992:                                                        }
1.1.1.14  root     5993:                                                        token = my_strtok(NULL, ";");
1.1       root     5994:                                                }
                   5995:                                        }
                   5996:                                }
                   5997:                        }
                   5998:                }
                   5999:        }
                   6000:        if(fd == -1) {
1.1.1.38  root     6001:                // we can not find command.com in the path, so open comspec_path
                   6002:                if(_stricmp(command, "COMMAND.COM") == 0 || _stricmp(command, "COMMAND") == 0) {
                   6003:                        strcpy(command, comspec_path);
                   6004:                        strcpy(path, command);
                   6005:                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   6006:                }
                   6007:        }
                   6008:        if(fd == -1) {
1.1       root     6009:                if(dos_command) {
                   6010:                        // may be dos command
                   6011:                        char tmp[MAX_PATH];
                   6012:                        sprintf(tmp, "%s %s", command, opt);
                   6013:                        system(tmp);
                   6014:                        return(0);
                   6015:                } else {
                   6016:                        return(-1);
                   6017:                }
                   6018:        }
                   6019:        _read(fd, file_buffer, sizeof(file_buffer));
                   6020:        _close(fd);
                   6021:        
                   6022:        // copy environment
1.1.1.29  root     6023:        int umb_linked, env_seg, psp_seg;
1.1       root     6024:        
1.1.1.29  root     6025:        if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   6026:                msdos_mem_unlink_umb();
                   6027:        }
1.1.1.8   root     6028:        if((env_seg = msdos_mem_alloc(first_mcb, ENV_SIZE >> 4, 1)) == -1) {
1.1.1.29  root     6029:                if((env_seg = msdos_mem_alloc(UMB_TOP >> 4, ENV_SIZE >> 4, 1)) == -1) {
                   6030:                        if(umb_linked != 0) {
                   6031:                                msdos_mem_link_umb();
                   6032:                        }
                   6033:                        return(-1);
                   6034:                }
1.1       root     6035:        }
                   6036:        if(param->env_seg == 0) {
                   6037:                psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
                   6038:                memcpy(mem + (env_seg << 4), mem + (parent_psp->env_seg << 4), ENV_SIZE);
                   6039:        } else {
                   6040:                memcpy(mem + (env_seg << 4), mem + (param->env_seg << 4), ENV_SIZE);
                   6041:        }
                   6042:        msdos_env_set_argv(env_seg, msdos_short_full_path(path));
                   6043:        
                   6044:        // check exe header
                   6045:        exe_header_t *header = (exe_header_t *)file_buffer;
1.1.1.8   root     6046:        int paragraphs, free_paragraphs = msdos_mem_get_free(first_mcb, 1);
1.1       root     6047:        UINT16 cs, ss, ip, sp;
                   6048:        
                   6049:        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   6050:                // memory allocation
                   6051:                int header_size = header->header_size * 16;
                   6052:                int load_size = header->pages * 512 - header_size;
                   6053:                if(header_size + load_size < 512) {
                   6054:                        load_size = 512 - header_size;
                   6055:                }
                   6056:                paragraphs = (PSP_SIZE + load_size) >> 4;
                   6057:                if(paragraphs + header->min_alloc > free_paragraphs) {
                   6058:                        msdos_mem_free(env_seg);
                   6059:                        return(-1);
                   6060:                }
                   6061:                paragraphs += header->max_alloc ? header->max_alloc : header->min_alloc;
                   6062:                if(paragraphs > free_paragraphs) {
                   6063:                        paragraphs = free_paragraphs;
                   6064:                }
1.1.1.8   root     6065:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29  root     6066:                        if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
                   6067:                                if(umb_linked != 0) {
                   6068:                                        msdos_mem_link_umb();
                   6069:                                }
                   6070:                                msdos_mem_free(env_seg);
                   6071:                                return(-1);
                   6072:                        }
1.1       root     6073:                }
                   6074:                // relocation
                   6075:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   6076:                for(int i = 0; i < header->relocations; i++) {
                   6077:                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   6078:                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   6079:                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   6080:                }
                   6081:                memcpy(mem + (start_seg << 4), file_buffer + header_size, load_size);
                   6082:                // segments
                   6083:                cs = header->init_cs + start_seg;
                   6084:                ss = header->init_ss + start_seg;
                   6085:                ip = header->init_ip;
                   6086:                sp = header->init_sp - 2; // for symdeb
                   6087:        } else {
                   6088:                // memory allocation
                   6089:                paragraphs = free_paragraphs;
1.1.1.8   root     6090:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29  root     6091:                        if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
                   6092:                                if(umb_linked != 0) {
                   6093:                                        msdos_mem_link_umb();
                   6094:                                }
                   6095:                                msdos_mem_free(env_seg);
                   6096:                                return(-1);
                   6097:                        }
1.1       root     6098:                }
                   6099:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   6100:                memcpy(mem + (start_seg << 4), file_buffer, 0x10000 - PSP_SIZE);
                   6101:                // segments
                   6102:                cs = ss = psp_seg;
                   6103:                ip = 0x100;
                   6104:                sp = 0xfffe;
                   6105:        }
1.1.1.29  root     6106:        if(umb_linked != 0) {
                   6107:                msdos_mem_link_umb();
                   6108:        }
1.1       root     6109:        
                   6110:        // create psp
1.1.1.3   root     6111:        *(UINT16 *)(mem + 4 * 0x22 + 0) = m_eip;
                   6112:        *(UINT16 *)(mem + 4 * 0x22 + 2) = SREG(CS);
1.1       root     6113:        psp_t *psp = msdos_psp_create(psp_seg, psp_seg + paragraphs, current_psp, env_seg);
                   6114:        memcpy(psp->fcb1, mem + (param->fcb1.w.h << 4) + param->fcb1.w.l, sizeof(psp->fcb1));
                   6115:        memcpy(psp->fcb2, mem + (param->fcb2.w.h << 4) + param->fcb2.w.l, sizeof(psp->fcb2));
                   6116:        memcpy(psp->buffer, mem + (param->cmd_line.w.h << 4) + param->cmd_line.w.l, sizeof(psp->buffer));
                   6117:        
                   6118:        mcb_t *mcb_env = (mcb_t *)(mem + ((env_seg - 1) << 4));
                   6119:        mcb_t *mcb_psp = (mcb_t *)(mem + ((psp_seg - 1) << 4));
                   6120:        mcb_psp->psp = mcb_env->psp = psp_seg;
                   6121:        
1.1.1.4   root     6122:        for(int i = 0; i < 8; i++) {
                   6123:                if(name_tmp[i] == '.') {
                   6124:                        mcb_psp->prog_name[i] = '\0';
                   6125:                        break;
                   6126:                } else if(i < 7 && msdos_lead_byte_check(name_tmp[i])) {
                   6127:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6128:                        i++;
                   6129:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6130:                } else if(name_tmp[i] >= 'a' && name_tmp[i] <= 'z') {
                   6131:                        mcb_psp->prog_name[i] = name_tmp[i] - 'a' + 'A';
                   6132:                } else {
                   6133:                        mcb_psp->prog_name[i] = name_tmp[i];
                   6134:                }
                   6135:        }
                   6136:        
1.1       root     6137:        // process info
                   6138:        process_t *process = msdos_process_info_create(psp_seg);
                   6139:        strcpy(process->module_dir, msdos_short_full_dir(path));
1.1.1.33  root     6140: #ifdef USE_DEBUGGER
                   6141:        strcpy(process->module_path, path);
                   6142: #endif
1.1       root     6143:        process->dta.w.l = 0x80;
                   6144:        process->dta.w.h = psp_seg;
                   6145:        process->switchar = '/';
                   6146:        process->max_files = 20;
                   6147:        process->parent_int_10h_feh_called = int_10h_feh_called;
                   6148:        process->parent_int_10h_ffh_called = int_10h_ffh_called;
1.1.1.14  root     6149:        process->parent_ds = SREG(DS);
1.1.1.31  root     6150:        process->parent_es = SREG(ES);
1.1       root     6151:        
                   6152:        current_psp = psp_seg;
1.1.1.23  root     6153:        msdos_sda_update(current_psp);
1.1       root     6154:        
                   6155:        if(al == 0x00) {
                   6156:                int_10h_feh_called = int_10h_ffh_called = false;
                   6157:                
1.1.1.38  root     6158:                // pipe
                   6159:                if(pipe_stdin_path[0] != '\0') {
                   6160:                        if((fd = _open(pipe_stdin_path, _O_RDONLY | _O_BINARY)) != -1) {
                   6161:                                UINT16 info = msdos_drive_number(pipe_stdin_path);
                   6162:                                msdos_file_handler_open(fd, pipe_stdin_path, _isatty(fd), 0, info, current_psp);
                   6163:                                psp->file_table[0] = fd;
                   6164:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6165:                        }
                   6166:                }
                   6167:                if(pipe_stdout_path[0] != '\0') {
                   6168:                        if(_access(pipe_stdout_path, 0) == 0) {
                   6169:                                SetFileAttributes(pipe_stdout_path, FILE_ATTRIBUTE_NORMAL);
                   6170:                                DeleteFile(pipe_stdout_path);
                   6171:                        }
                   6172:                        if((fd = _open(pipe_stdout_path, _O_WRONLY | _O_BINARY | _O_CREAT)) != -1) {
                   6173:                                UINT16 info = msdos_drive_number(pipe_stdout_path);
                   6174:                                msdos_file_handler_open(fd, pipe_stdout_path, _isatty(fd), 1, info, current_psp);
                   6175:                                psp->file_table[1] = fd;
                   6176:                                msdos_psp_set_file_table(fd, fd, current_psp);
                   6177:                        }
                   6178:                }
1.1.1.39! root     6179:                if(pipe_stderr_path[0] != '\0') {
        !          6180:                        if(_access(pipe_stderr_path, 0) == 0) {
        !          6181:                                SetFileAttributes(pipe_stderr_path, FILE_ATTRIBUTE_NORMAL);
        !          6182:                                DeleteFile(pipe_stderr_path);
        !          6183:                        }
        !          6184:                        if((fd = _open(pipe_stderr_path, _O_WRONLY | _O_BINARY | _O_CREAT)) != -1) {
        !          6185:                                UINT16 info = msdos_drive_number(pipe_stderr_path);
        !          6186:                                msdos_file_handler_open(fd, pipe_stderr_path, _isatty(fd), 2, info, current_psp);
        !          6187:                                psp->file_table[2] = fd;
        !          6188:                                msdos_psp_set_file_table(fd, fd, current_psp);
        !          6189:                        }
        !          6190:                }
1.1.1.38  root     6191:                
1.1       root     6192:                // registers and segments
                   6193:                REG16(AX) = REG16(BX) = 0x00;
                   6194:                REG16(CX) = 0xff;
                   6195:                REG16(DX) = psp_seg;
                   6196:                REG16(SI) = ip;
                   6197:                REG16(DI) = sp;
                   6198:                REG16(SP) = sp;
1.1.1.3   root     6199:                SREG(DS) = SREG(ES) = psp_seg;
                   6200:                SREG(SS) = ss;
                   6201:                i386_load_segment_descriptor(DS);
                   6202:                i386_load_segment_descriptor(ES);
                   6203:                i386_load_segment_descriptor(SS);
1.1       root     6204:                
                   6205:                *(UINT16 *)(mem + (ss << 4) + sp) = 0;
                   6206:                i386_jmp_far(cs, ip);
                   6207:        } else if(al == 0x01) {
                   6208:                // copy ss:sp and cs:ip to param block
                   6209:                param->sp = sp;
                   6210:                param->ss = ss;
                   6211:                param->ip = ip;
                   6212:                param->cs = cs;
1.1.1.31  root     6213:                
                   6214:                // the AX value to be passed to the child program is put on top of the child's stack
                   6215:                *(UINT16 *)(mem + (ss << 4) + sp) = REG16(AX);
1.1       root     6216:        }
                   6217:        return(0);
                   6218: }
                   6219: 
                   6220: void msdos_process_terminate(int psp_seg, int ret, int mem_free)
                   6221: {
                   6222:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   6223:        
                   6224:        *(UINT32 *)(mem + 4 * 0x22) = psp->int_22h.dw;
                   6225:        *(UINT32 *)(mem + 4 * 0x23) = psp->int_23h.dw;
                   6226:        *(UINT32 *)(mem + 4 * 0x24) = psp->int_24h.dw;
                   6227:        
1.1.1.3   root     6228:        SREG(SS) = psp->stack.w.h;
                   6229:        i386_load_segment_descriptor(SS);
1.1       root     6230:        REG16(SP) = psp->stack.w.l;
                   6231:        i386_jmp_far(psp->int_22h.w.h, psp->int_22h.w.l);
                   6232:        
1.1.1.28  root     6233: //     process_t *current_process = msdos_process_info_get(psp_seg);
                   6234:        process_t *current_process = NULL;
                   6235:        for(int i = 0; i < MAX_PROCESS; i++) {
                   6236:                if(process[i].psp == psp_seg) {
                   6237:                        current_process = &process[i];
                   6238:                        break;
                   6239:                }
                   6240:        }
                   6241:        if(current_process == NULL) {
                   6242:                throw(0x1f); // general failure
                   6243:        }
                   6244:        int_10h_feh_called = current_process->parent_int_10h_feh_called;
                   6245:        int_10h_ffh_called = current_process->parent_int_10h_ffh_called;
                   6246:        if(current_process->called_by_int2eh) {
                   6247:                REG16(AX) = ret;
                   6248:        }
                   6249:        SREG(DS) = current_process->parent_ds;
1.1.1.31  root     6250:        SREG(ES) = current_process->parent_es;
1.1.1.14  root     6251:        i386_load_segment_descriptor(DS);
1.1.1.31  root     6252:        i386_load_segment_descriptor(ES);
1.1       root     6253:        
                   6254:        if(mem_free) {
1.1.1.8   root     6255:                int mcb_seg;
                   6256:                while((mcb_seg = msdos_mem_get_last_mcb(first_mcb, psp_seg)) != -1) {
                   6257:                        msdos_mem_free(mcb_seg + 1);
                   6258:                }
                   6259:                while((mcb_seg = msdos_mem_get_last_mcb(UMB_TOP >> 4, psp_seg)) != -1) {
                   6260:                        msdos_mem_free(mcb_seg + 1);
                   6261:                }
1.1       root     6262:                
                   6263:                for(int i = 0; i < MAX_FILES; i++) {
                   6264:                        if(file_handler[i].valid && file_handler[i].psp == psp_seg) {
                   6265:                                _close(i);
1.1.1.20  root     6266:                                msdos_file_handler_close(i);
                   6267:                                msdos_psp_set_file_table(i, 0x0ff, psp_seg); // FIXME: consider duplicated file handles
1.1       root     6268:                        }
                   6269:                }
1.1.1.13  root     6270:                msdos_dta_info_free(psp_seg);
1.1       root     6271:        }
1.1.1.14  root     6272:        msdos_stdio_reopen();
1.1       root     6273:        
1.1.1.28  root     6274:        memset(current_process, 0, sizeof(process_t));
1.1       root     6275:        
                   6276:        current_psp = psp->parent_psp;
                   6277:        retval = ret;
1.1.1.23  root     6278:        msdos_sda_update(current_psp);
1.1       root     6279: }
                   6280: 
                   6281: // drive
                   6282: 
                   6283: int msdos_drive_param_block_update(int drive_num, UINT16 *seg, UINT16 *ofs, int force_update)
                   6284: {
                   6285:        *seg = DPB_TOP >> 4;
                   6286:        *ofs = sizeof(dpb_t) * drive_num;
                   6287:        dpb_t *dpb = (dpb_t *)(mem + (*seg << 4) + *ofs);
                   6288:        
                   6289:        if(!force_update && dpb->free_clusters != 0) {
                   6290:                return(dpb->bytes_per_sector ? 1 : 0);
                   6291:        }
                   6292:        memset(dpb, 0, sizeof(dpb_t));
                   6293:        
                   6294:        int res = 0;
                   6295:        char dev[64];
                   6296:        sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   6297:        
1.1.1.17  root     6298:        HANDLE hFile = CreateFile(dev, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1.1       root     6299:        if(hFile != INVALID_HANDLE_VALUE) {
                   6300:                DISK_GEOMETRY geo;
                   6301:                DWORD dwSize;
                   6302:                if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &geo, sizeof(geo), &dwSize, NULL)) {
                   6303:                        dpb->bytes_per_sector = (UINT16)geo.BytesPerSector;
                   6304:                        dpb->highest_sector_num = (UINT8)(geo.SectorsPerTrack - 1);
                   6305:                        dpb->highest_cluster_num = (UINT16)(geo.TracksPerCylinder * geo.Cylinders.QuadPart + 1);
1.1.1.14  root     6306:                        dpb->maximum_cluster_num = (UINT32)(geo.TracksPerCylinder * geo.Cylinders.QuadPart + 1);
1.1       root     6307:                        switch(geo.MediaType) {
                   6308:                        case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   6309:                                dpb->media_type = 0xff;
                   6310:                                break;
                   6311:                        case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   6312:                                dpb->media_type = 0xfe;
                   6313:                                break;
                   6314:                        case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   6315:                                dpb->media_type = 0xfd;
                   6316:                                break;
                   6317:                        case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   6318:                                dpb->media_type = 0xfc;
                   6319:                                break;
                   6320:                        case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   6321:                        case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   6322:                                dpb->media_type = 0xf9;
                   6323:                                break;
                   6324:                        case FixedMedia:        // hard disk
                   6325:                        case RemovableMedia:
1.1.1.19  root     6326:                        case Unknown:
1.1       root     6327:                                dpb->media_type = 0xf8;
                   6328:                                break;
                   6329:                        default:
                   6330:                                dpb->media_type = 0xf0;
                   6331:                                break;
                   6332:                        }
                   6333:                        res = 1;
                   6334:                }
                   6335:                dpb->drive_num = drive_num;
                   6336:                dpb->unit_num = drive_num;
                   6337:                dpb->next_dpb_ofs = *ofs + sizeof(dpb_t);
                   6338:                dpb->next_dpb_seg = *seg;
1.1.1.14  root     6339:                dpb->info_sector = 0xffff;
                   6340:                dpb->backup_boot_sector = 0xffff;
1.1       root     6341:                dpb->free_clusters = 0xffff;
1.1.1.14  root     6342:                dpb->free_search_cluster = 0xffffffff;
1.1       root     6343:                CloseHandle(hFile);
                   6344:        }
                   6345:        return(res);
                   6346: }
                   6347: 
                   6348: // pc bios
                   6349: 
1.1.1.35  root     6350: #ifdef USE_SERVICE_THREAD
                   6351: void start_service_loop(LPTHREAD_START_ROUTINE lpStartAddress)
                   6352: {
                   6353: #if defined(HAS_I386)
                   6354:        if(m_SF != 0) {
                   6355:                m_SF = 0;
                   6356:                mem[0xfffd0 + 0x15] = 0x79;     // jns -4
                   6357:        } else {
                   6358:                m_SF = 1;
                   6359:                mem[0xfffd0 + 0x15] = 0x78;     // js -4
                   6360:        }
                   6361: #else
                   6362:        if(m_SignVal < 0) {
                   6363:                m_SignVal = 0;
                   6364:                mem[0xfffd0 + 0x15] = 0x79;     // jns -4
                   6365:        } else {
                   6366:                m_SignVal = -1;
                   6367:                mem[0xfffd0 + 0x15] = 0x78;     // js -4
                   6368:        }
                   6369: #endif
                   6370:        i386_call_far(0xfffd, 0x0013);
                   6371:        in_service = true;
                   6372:        service_exit = false;
                   6373:        CloseHandle(CreateThread(NULL, 0, lpStartAddress, NULL, 0, NULL));
                   6374: }
                   6375: 
                   6376: void finish_service_loop()
                   6377: {
                   6378:        if(in_service && service_exit) {
                   6379: #if defined(HAS_I386)
                   6380:                if(m_SF != 0) {
                   6381:                        m_SF = 0;
                   6382:                } else {
                   6383:                        m_SF = 1;
                   6384:                }
                   6385: #else
                   6386:                if(m_SignVal < 0) {
                   6387:                        m_SignVal = 0;
                   6388:                } else {
                   6389:                        m_SignVal = -1;
                   6390:                }
                   6391: #endif
                   6392:                in_service = false;
                   6393:        }
                   6394: }
                   6395: #endif
                   6396: 
1.1.1.19  root     6397: UINT32 get_ticks_since_midnight(UINT32 cur_msec)
                   6398: {
                   6399:        static unsigned __int64 start_msec_since_midnight = 0;
                   6400:        static unsigned __int64 start_msec_since_hostboot = 0;
                   6401:        
                   6402:        if(start_msec_since_midnight == 0) {
                   6403:                SYSTEMTIME time;
                   6404:                GetLocalTime(&time);
                   6405:                start_msec_since_midnight = ((time.wHour * 60 + time.wMinute) * 60 + time.wSecond) * 1000 + time.wMilliseconds;
                   6406:                start_msec_since_hostboot = cur_msec;
                   6407:        }
                   6408:        unsigned __int64 msec = (start_msec_since_midnight + cur_msec - start_msec_since_hostboot) % (24 * 60 * 60 * 1000);
                   6409:        unsigned __int64 tick = msec * 0x1800b0 / (24 * 60 * 60 * 1000);
                   6410:        return (UINT32)tick;
                   6411: }
                   6412: 
                   6413: void pcbios_update_daily_timer_counter(UINT32 cur_msec)
                   6414: {
                   6415:        UINT32 prev_tick = *(UINT32 *)(mem + 0x46c);
                   6416:        UINT32 next_tick = get_ticks_since_midnight(cur_msec);
                   6417:        
                   6418:        if(prev_tick > next_tick) {
                   6419:                mem[0x470] = 1;
                   6420:        }
                   6421:        *(UINT32 *)(mem + 0x46c) = next_tick;
                   6422: }
                   6423: 
1.1.1.14  root     6424: inline void pcbios_irq0()
                   6425: {
                   6426:        //++*(UINT32 *)(mem + 0x46c);
1.1.1.19  root     6427:        pcbios_update_daily_timer_counter(timeGetTime());
1.1.1.14  root     6428: }
                   6429: 
1.1.1.16  root     6430: int pcbios_get_text_vram_address(int page)
1.1       root     6431: {
                   6432:        if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     6433:                return TEXT_VRAM_TOP;
1.1       root     6434:        } else {
1.1.1.14  root     6435:                return TEXT_VRAM_TOP + VIDEO_REGEN * (page % vram_pages);
1.1       root     6436:        }
                   6437: }
                   6438: 
1.1.1.16  root     6439: int pcbios_get_shadow_buffer_address(int page)
1.1       root     6440: {
1.1.1.14  root     6441:        if(!int_10h_feh_called) {
1.1.1.16  root     6442:                return pcbios_get_text_vram_address(page);
1.1.1.14  root     6443:        } else if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     6444:                return SHADOW_BUF_TOP;
                   6445:        } else {
1.1.1.14  root     6446:                return SHADOW_BUF_TOP + VIDEO_REGEN * (page % vram_pages);
1.1.1.8   root     6447:        }
                   6448: }
                   6449: 
1.1.1.16  root     6450: int pcbios_get_shadow_buffer_address(int page, int x, int y)
1.1.1.8   root     6451: {
1.1.1.16  root     6452:        return pcbios_get_shadow_buffer_address(page) + (x + y * scr_width) * 2;
1.1       root     6453: }
                   6454: 
1.1.1.16  root     6455: void pcbios_set_console_size(int width, int height, bool clr_screen)
1.1       root     6456: {
1.1.1.14  root     6457:        // clear the existing screen, not just the new one
                   6458:        int clr_height = max(height, scr_height);
                   6459:        
1.1.1.16  root     6460:        if(scr_width != width || scr_height != height) {
                   6461:                change_console_size(width, height);
1.1.1.14  root     6462:        }
                   6463:        mem[0x462] = 0;
                   6464:        *(UINT16 *)(mem + 0x44e) = 0;
                   6465:        
1.1.1.16  root     6466:        text_vram_top_address = pcbios_get_text_vram_address(0);
                   6467:        text_vram_end_address = text_vram_top_address + width * height * 2;
                   6468:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(0);
                   6469:        shadow_buffer_end_address = shadow_buffer_top_address + width * height * 2;
1.1       root     6470:        
1.1.1.23  root     6471:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.16  root     6472:        if(clr_screen) {
1.1.1.14  root     6473:                for(int ofs = shadow_buffer_top_address; ofs < shadow_buffer_end_address;) {
                   6474:                        mem[ofs++] = 0x20;
                   6475:                        mem[ofs++] = 0x07;
                   6476:                }
                   6477:                
1.1.1.35  root     6478: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6479:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     6480: #endif
1.1.1.14  root     6481:                for(int y = 0; y < clr_height; y++) {
                   6482:                        for(int x = 0; x < scr_width; x++) {
                   6483:                                SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6484:                                SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1       root     6485:                        }
                   6486:                }
                   6487:                SMALL_RECT rect;
1.1.1.14  root     6488:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + clr_height - 1);
                   6489:                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   6490:                vram_length_char = vram_last_length_char = 0;
                   6491:                vram_length_attr = vram_last_length_attr = 0;
1.1.1.35  root     6492: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6493:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     6494: #endif
1.1       root     6495:        }
1.1.1.14  root     6496:        COORD co;
                   6497:        co.X = 0;
                   6498:        co.Y = scr_top;
                   6499:        SetConsoleCursorPosition(hStdout, co);
                   6500:        cursor_moved = true;
                   6501:        SetConsoleTextAttribute(hStdout, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
1.1       root     6502: }
                   6503: 
1.1.1.36  root     6504: void pcbios_update_cursor_position()
                   6505: {
                   6506:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   6507:        GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
                   6508:        if(!restore_console_on_exit) {
                   6509:                scr_top = csbi.srWindow.Top;
                   6510:        }
                   6511:        mem[0x450 + mem[0x462] * 2] = csbi.dwCursorPosition.X;
                   6512:        mem[0x451 + mem[0x462] * 2] = csbi.dwCursorPosition.Y - scr_top;
                   6513: }
                   6514: 
1.1.1.16  root     6515: inline void pcbios_int_10h_00h()
                   6516: {
                   6517:        switch(REG8(AL) & 0x7f) {
                   6518:        case 0x70: // v-text mode
                   6519:        case 0x71: // extended cga v-text mode
                   6520:                pcbios_set_console_size(scr_width, scr_height, !(REG8(AL) & 0x80));
                   6521:                break;
                   6522:        default:
                   6523:                pcbios_set_console_size(80, 25, !(REG8(AL) & 0x80));
                   6524:                break;
                   6525:        }
                   6526:        if(REG8(AL) & 0x80) {
                   6527:                mem[0x487] |= 0x80;
                   6528:        } else {
                   6529:                mem[0x487] &= ~0x80;
                   6530:        }
                   6531:        mem[0x449] = REG8(AL) & 0x7f;
                   6532: }
                   6533: 
1.1       root     6534: inline void pcbios_int_10h_01h()
                   6535: {
1.1.1.13  root     6536:        mem[0x460] = REG8(CL);
                   6537:        mem[0x461] = REG8(CH);
1.1.1.14  root     6538:        
1.1.1.23  root     6539:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6540:        CONSOLE_CURSOR_INFO ci;
                   6541:        GetConsoleCursorInfo(hStdout, &ci);
                   6542: //     ci.bVisible = !(REG8(CH) & 0x60) && REG8(CH) <= REG8(CL);
                   6543: //     if(ci.bVisible) {
                   6544:                int lines = max(8, REG8(CL) + 1);
                   6545:                ci.dwSize = (REG8(CL) - REG8(CH) + 1) * 100 / lines;
                   6546: //     }
                   6547:        SetConsoleCursorInfo(hStdout, &ci);
1.1       root     6548: }
                   6549: 
                   6550: inline void pcbios_int_10h_02h()
                   6551: {
1.1.1.14  root     6552:        // continuously setting the cursor effectively stops it blinking
                   6553:        if(mem[0x462] == REG8(BH) && (REG8(DL) != mem[0x450 + REG8(BH) * 2] || REG8(DH) != mem[0x451 + REG8(BH) * 2])) {
1.1       root     6554:                COORD co;
                   6555:                co.X = REG8(DL);
1.1.1.14  root     6556:                co.Y = REG8(DH) + scr_top;
                   6557:                
                   6558:                // some programs hide the cursor by moving it off screen
                   6559:                static bool hidden = false;
1.1.1.23  root     6560:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6561:                CONSOLE_CURSOR_INFO ci;
                   6562:                GetConsoleCursorInfo(hStdout, &ci);
                   6563:                
                   6564:                if(REG8(DH) >= scr_height || !SetConsoleCursorPosition(hStdout, co)) {
                   6565:                        if(ci.bVisible) {
                   6566:                                ci.bVisible = FALSE;
                   6567: //                             SetConsoleCursorInfo(hStdout, &ci);
                   6568:                                hidden = true;
                   6569:                        }
                   6570:                } else if(hidden) {
                   6571:                        if(!ci.bVisible) {
                   6572:                                ci.bVisible = TRUE;
                   6573: //                             SetConsoleCursorInfo(hStdout, &ci);
                   6574:                        }
                   6575:                        hidden = false;
                   6576:                }
1.1       root     6577:        }
1.1.1.14  root     6578:        mem[0x450 + (REG8(BH) % vram_pages) * 2] = REG8(DL);
                   6579:        mem[0x451 + (REG8(BH) % vram_pages) * 2] = REG8(DH);
1.1       root     6580: }
                   6581: 
                   6582: inline void pcbios_int_10h_03h()
                   6583: {
1.1.1.14  root     6584:        REG8(DL) = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   6585:        REG8(DH) = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     6586:        REG8(CL) = mem[0x460];
                   6587:        REG8(CH) = mem[0x461];
                   6588: }
                   6589: 
                   6590: inline void pcbios_int_10h_05h()
                   6591: {
1.1.1.14  root     6592:        if(REG8(AL) >= vram_pages) {
                   6593:                return;
                   6594:        }
                   6595:        if(mem[0x462] != REG8(AL)) {
                   6596:                vram_flush();
                   6597:                
1.1.1.23  root     6598:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6599:                SMALL_RECT rect;
1.1.1.14  root     6600:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   6601:                ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     6602:                
1.1.1.16  root     6603:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(mem[0x462]); y < scr_height; y++) {
1.1.1.14  root     6604:                        for(int x = 0; x < scr_width; x++) {
                   6605:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   6606:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     6607:                        }
                   6608:                }
1.1.1.16  root     6609:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(REG8(AL)); y < scr_height; y++) {
1.1.1.14  root     6610:                        for(int x = 0; x < scr_width; x++) {
                   6611:                                SCR_BUF(y,x).Char.AsciiChar = mem[ofs++];
                   6612:                                SCR_BUF(y,x).Attributes = mem[ofs++];
1.1       root     6613:                        }
                   6614:                }
1.1.1.14  root     6615:                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     6616:                
                   6617:                COORD co;
1.1.1.14  root     6618:                co.X = mem[0x450 + REG8(AL) * 2];
                   6619:                co.Y = mem[0x451 + REG8(AL) * 2] + scr_top;
                   6620:                if(co.Y < scr_top + scr_height) {
                   6621:                        SetConsoleCursorPosition(hStdout, co);
                   6622:                }
1.1       root     6623:        }
1.1.1.14  root     6624:        mem[0x462] = REG8(AL);
                   6625:        *(UINT16 *)(mem + 0x44e) = REG8(AL) * VIDEO_REGEN;
                   6626:        int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.16  root     6627:        text_vram_top_address = pcbios_get_text_vram_address(mem[0x462]);
1.1.1.14  root     6628:        text_vram_end_address = text_vram_top_address + regen;
1.1.1.16  root     6629:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(mem[0x462]);
1.1.1.14  root     6630:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
1.1       root     6631: }
                   6632: 
                   6633: inline void pcbios_int_10h_06h()
                   6634: {
1.1.1.14  root     6635:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   6636:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   6637:                return;
                   6638:        }
                   6639:        vram_flush();
                   6640:        
1.1.1.23  root     6641:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6642:        SMALL_RECT rect;
1.1.1.14  root     6643:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   6644:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   6645:        
                   6646:        int right = min(REG8(DL), scr_width - 1);
                   6647:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     6648:        
                   6649:        if(REG8(AL) == 0) {
1.1.1.14  root     6650:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     6651:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     6652:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6653:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     6654:                        }
                   6655:                }
                   6656:        } else {
1.1.1.14  root     6657:                for(int y = REG8(CH), y2 = min(REG8(CH) + REG8(AL), bottom + 1); y <= bottom; y++, y2++) {
1.1.1.16  root     6658:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     6659:                                if(y2 <= bottom) {
                   6660:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     6661:                                } else {
1.1.1.14  root     6662:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6663:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     6664:                                }
1.1.1.14  root     6665:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   6666:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     6667:                        }
                   6668:                }
                   6669:        }
1.1.1.14  root     6670:        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     6671: }
                   6672: 
                   6673: inline void pcbios_int_10h_07h()
                   6674: {
1.1.1.14  root     6675:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   6676:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   6677:                return;
                   6678:        }
                   6679:        vram_flush();
                   6680:        
1.1.1.23  root     6681:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6682:        SMALL_RECT rect;
1.1.1.14  root     6683:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   6684:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   6685:        
                   6686:        int right = min(REG8(DL), scr_width - 1);
                   6687:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     6688:        
                   6689:        if(REG8(AL) == 0) {
1.1.1.14  root     6690:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     6691:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     6692:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6693:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     6694:                        }
                   6695:                }
                   6696:        } else {
1.1.1.14  root     6697:                for(int y = bottom, y2 = max(REG8(CH) - 1, bottom - REG8(AL)); y >= REG8(CH); y--, y2--) {
1.1.1.16  root     6698:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     6699:                                if(y2 >= REG8(CH)) {
                   6700:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     6701:                                } else {
1.1.1.14  root     6702:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6703:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     6704:                                }
1.1.1.14  root     6705:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   6706:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     6707:                        }
                   6708:                }
                   6709:        }
1.1.1.14  root     6710:        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     6711: }
                   6712: 
                   6713: inline void pcbios_int_10h_08h()
                   6714: {
                   6715:        COORD co;
                   6716:        DWORD num;
                   6717:        
1.1.1.14  root     6718:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   6719:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     6720:        
                   6721:        if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     6722:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6723:                co.Y += scr_top;
                   6724:                vram_flush();
1.1       root     6725:                ReadConsoleOutputCharacter(hStdout, scr_char, 1, co, &num);
                   6726:                ReadConsoleOutputAttribute(hStdout, scr_attr, 1, co, &num);
                   6727:                REG8(AL) = scr_char[0];
                   6728:                REG8(AH) = scr_attr[0];
                   6729:        } else {
1.1.1.16  root     6730:                REG16(AX) = *(UINT16 *)(mem + pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y));
1.1       root     6731:        }
                   6732: }
                   6733: 
                   6734: inline void pcbios_int_10h_09h()
                   6735: {
                   6736:        COORD co;
                   6737:        
1.1.1.14  root     6738:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   6739:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   6740:        
1.1.1.16  root     6741:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   6742:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     6743:        
                   6744:        if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     6745: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6746:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     6747: #endif
1.1.1.16  root     6748:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     6749:                while(dest < end) {
                   6750:                        write_text_vram_char(dest - vram, REG8(AL));
                   6751:                        mem[dest++] = REG8(AL);
                   6752:                        write_text_vram_attr(dest - vram, REG8(BL));
                   6753:                        mem[dest++] = REG8(BL);
1.1       root     6754:                }
1.1.1.35  root     6755: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6756:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     6757: #endif
1.1       root     6758:        } else {
1.1.1.14  root     6759:                while(dest < end) {
1.1       root     6760:                        mem[dest++] = REG8(AL);
                   6761:                        mem[dest++] = REG8(BL);
                   6762:                }
                   6763:        }
                   6764: }
                   6765: 
                   6766: inline void pcbios_int_10h_0ah()
                   6767: {
                   6768:        COORD co;
                   6769:        
1.1.1.14  root     6770:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   6771:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   6772:        
1.1.1.16  root     6773:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   6774:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     6775:        
                   6776:        if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     6777: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6778:                EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     6779: #endif
1.1.1.16  root     6780:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     6781:                while(dest < end) {
                   6782:                        write_text_vram_char(dest - vram, REG8(AL));
                   6783:                        mem[dest++] = REG8(AL);
                   6784:                        dest++;
1.1       root     6785:                }
1.1.1.35  root     6786: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6787:                LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     6788: #endif
1.1       root     6789:        } else {
1.1.1.14  root     6790:                while(dest < end) {
1.1       root     6791:                        mem[dest++] = REG8(AL);
                   6792:                        dest++;
                   6793:                }
                   6794:        }
                   6795: }
                   6796: 
                   6797: inline void pcbios_int_10h_0eh()
                   6798: {
1.1.1.14  root     6799:        DWORD num;
                   6800:        COORD co;
                   6801:        
                   6802:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   6803:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   6804:        
                   6805:        if(REG8(AL) == 7) {
                   6806:                //MessageBeep(-1);
                   6807:        } else if(REG8(AL) == 8 || REG8(AL) == 10 || REG8(AL) == 13) {
                   6808:                if(REG8(AL) == 10) {
                   6809:                        vram_flush();
                   6810:                }
1.1.1.23  root     6811:                WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), &REG8(AL), 1, &num, NULL);
1.1.1.14  root     6812:                cursor_moved = true;
                   6813:        } else {
1.1.1.16  root     6814:                int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
1.1.1.14  root     6815:                if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     6816: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6817:                        EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     6818: #endif
1.1.1.16  root     6819:                        int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     6820:                        write_text_vram_char(dest - vram, REG8(AL));
1.1.1.35  root     6821: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6822:                        LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     6823: #endif
1.1.1.14  root     6824:                        
1.1.1.23  root     6825:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6826:                        if(++co.X == scr_width) {
                   6827:                                co.X = 0;
                   6828:                                if(++co.Y == scr_height) {
                   6829:                                        vram_flush();
                   6830:                                        WriteConsole(hStdout, "\n", 1, &num, NULL);
                   6831:                                        cursor_moved = true;
                   6832:                                }
                   6833:                        }
                   6834:                        if(!cursor_moved) {
                   6835:                                co.Y += scr_top;
                   6836:                                SetConsoleCursorPosition(hStdout, co);
                   6837:                                cursor_moved = true;
                   6838:                        }
                   6839:                }
                   6840:                mem[dest] = REG8(AL);
                   6841:        }
1.1       root     6842: }
                   6843: 
                   6844: inline void pcbios_int_10h_0fh()
                   6845: {
                   6846:        REG8(AL) = mem[0x449];
                   6847:        REG8(AH) = mem[0x44a];
                   6848:        REG8(BH) = mem[0x462];
                   6849: }
                   6850: 
1.1.1.14  root     6851: inline void pcbios_int_10h_11h()
                   6852: {
                   6853:        switch(REG8(AL)) {
1.1.1.16  root     6854:        case 0x01:
1.1.1.14  root     6855:        case 0x11:
1.1.1.16  root     6856:                pcbios_set_console_size(80, 28, true);
1.1.1.14  root     6857:                break;
1.1.1.16  root     6858:        case 0x02:
1.1.1.14  root     6859:        case 0x12:
1.1.1.16  root     6860:                pcbios_set_console_size(80, 50, true);
1.1.1.14  root     6861:                break;
1.1.1.16  root     6862:        case 0x04:
1.1.1.14  root     6863:        case 0x14:
1.1.1.16  root     6864:                pcbios_set_console_size(80, 25, true);
                   6865:                break;
                   6866:        case 0x18:
                   6867:                pcbios_set_console_size(80, 50, true);
1.1.1.14  root     6868:                break;
                   6869:        case 0x30:
                   6870:                SREG(ES) = 0;
                   6871:                i386_load_segment_descriptor(ES);
                   6872:                REG16(BP) = 0;
                   6873:                REG16(CX) = mem[0x485];
                   6874:                REG8(DL) = mem[0x484];
                   6875:                break;
                   6876:        }
                   6877: }
                   6878: 
                   6879: inline void pcbios_int_10h_12h()
                   6880: {
1.1.1.16  root     6881:        switch(REG8(BL)) {
                   6882:        case 0x10:
1.1.1.14  root     6883:                REG16(BX) = 0x0003;
                   6884:                REG16(CX) = 0x0009;
1.1.1.16  root     6885:                break;
1.1.1.14  root     6886:        }
                   6887: }
                   6888: 
1.1       root     6889: inline void pcbios_int_10h_13h()
                   6890: {
1.1.1.3   root     6891:        int ofs = SREG_BASE(ES) + REG16(BP);
1.1       root     6892:        COORD co;
                   6893:        DWORD num;
                   6894:        
                   6895:        co.X = REG8(DL);
1.1.1.14  root     6896:        co.Y = REG8(DH) + scr_top;
                   6897:        
                   6898:        vram_flush();
1.1       root     6899:        
                   6900:        switch(REG8(AL)) {
                   6901:        case 0x00:
                   6902:        case 0x01:
                   6903:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     6904:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6905:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   6906:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   6907:                        SetConsoleCursorPosition(hStdout, co);
                   6908:                        
                   6909:                        if(csbi.wAttributes != REG8(BL)) {
                   6910:                                SetConsoleTextAttribute(hStdout, REG8(BL));
                   6911:                        }
1.1.1.14  root     6912:                        WriteConsole(hStdout, &mem[ofs], REG16(CX), &num, NULL);
                   6913:                        
1.1       root     6914:                        if(csbi.wAttributes != REG8(BL)) {
                   6915:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   6916:                        }
                   6917:                        if(REG8(AL) == 0x00) {
1.1.1.15  root     6918:                                if(!restore_console_on_exit) {
                   6919:                                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   6920:                                        scr_top = csbi.srWindow.Top;
                   6921:                                }
1.1.1.14  root     6922:                                co.X = mem[0x450 + REG8(BH) * 2];
                   6923:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     6924:                                SetConsoleCursorPosition(hStdout, co);
                   6925:                        } else {
                   6926:                                cursor_moved = true;
                   6927:                        }
                   6928:                } else {
1.1.1.3   root     6929:                        m_CF = 1;
1.1       root     6930:                }
                   6931:                break;
                   6932:        case 0x02:
                   6933:        case 0x03:
                   6934:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     6935:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6936:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   6937:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   6938:                        SetConsoleCursorPosition(hStdout, co);
                   6939:                        
                   6940:                        WORD wAttributes = csbi.wAttributes;
                   6941:                        for(int i = 0; i < REG16(CX); i++, ofs += 2) {
                   6942:                                if(wAttributes != mem[ofs + 1]) {
                   6943:                                        SetConsoleTextAttribute(hStdout, mem[ofs + 1]);
                   6944:                                        wAttributes = mem[ofs + 1];
                   6945:                                }
1.1.1.14  root     6946:                                WriteConsole(hStdout, &mem[ofs], 1, &num, NULL);
1.1       root     6947:                        }
                   6948:                        if(csbi.wAttributes != wAttributes) {
                   6949:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   6950:                        }
                   6951:                        if(REG8(AL) == 0x02) {
1.1.1.14  root     6952:                                co.X = mem[0x450 + REG8(BH) * 2];
                   6953:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     6954:                                SetConsoleCursorPosition(hStdout, co);
                   6955:                        } else {
                   6956:                                cursor_moved = true;
                   6957:                        }
                   6958:                } else {
1.1.1.3   root     6959:                        m_CF = 1;
1.1       root     6960:                }
                   6961:                break;
                   6962:        case 0x10:
                   6963:        case 0x11:
                   6964:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     6965:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6966:                        ReadConsoleOutputCharacter(hStdout, scr_char, REG16(CX), co, &num);
                   6967:                        ReadConsoleOutputAttribute(hStdout, scr_attr, REG16(CX), co, &num);
                   6968:                        for(int i = 0; i < num; i++) {
                   6969:                                mem[ofs++] = scr_char[i];
                   6970:                                mem[ofs++] = scr_attr[i];
                   6971:                                if(REG8(AL) == 0x11) {
                   6972:                                        mem[ofs++] = 0;
                   6973:                                        mem[ofs++] = 0;
                   6974:                                }
                   6975:                        }
                   6976:                } else {
1.1.1.16  root     6977:                        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     6978:                                mem[ofs++] = mem[src++];
                   6979:                                mem[ofs++] = mem[src++];
                   6980:                                if(REG8(AL) == 0x11) {
                   6981:                                        mem[ofs++] = 0;
                   6982:                                        mem[ofs++] = 0;
                   6983:                                }
1.1.1.14  root     6984:                                if(++co.X == scr_width) {
                   6985:                                        if(++co.Y == scr_height) {
1.1       root     6986:                                                break;
                   6987:                                        }
                   6988:                                        co.X = 0;
                   6989:                                }
                   6990:                        }
                   6991:                }
                   6992:                break;
                   6993:        case 0x20:
                   6994:        case 0x21:
                   6995:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     6996:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6997:                        int len = min(REG16(CX), scr_width * scr_height);
                   6998:                        for(int i = 0; i < len; i++) {
1.1       root     6999:                                scr_char[i] = mem[ofs++];
                   7000:                                scr_attr[i] = mem[ofs++];
                   7001:                                if(REG8(AL) == 0x21) {
                   7002:                                        ofs += 2;
                   7003:                                }
                   7004:                        }
1.1.1.14  root     7005:                        WriteConsoleOutputCharacter(hStdout, scr_char, len, co, &num);
                   7006:                        WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     7007:                } else {
1.1.1.16  root     7008:                        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     7009:                                mem[dest++] = mem[ofs++];
                   7010:                                mem[dest++] = mem[ofs++];
                   7011:                                if(REG8(AL) == 0x21) {
                   7012:                                        ofs += 2;
                   7013:                                }
1.1.1.14  root     7014:                                if(++co.X == scr_width) {
                   7015:                                        if(++co.Y == scr_height) {
1.1       root     7016:                                                break;
                   7017:                                        }
                   7018:                                        co.X = 0;
                   7019:                                }
                   7020:                        }
                   7021:                }
                   7022:                break;
                   7023:        default:
1.1.1.22  root     7024:                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     7025:                m_CF = 1;
1.1       root     7026:                break;
                   7027:        }
                   7028: }
                   7029: 
1.1.1.30  root     7030: inline void pcbios_int_10h_18h()
                   7031: {
                   7032:        switch(REG8(AL)) {
                   7033:        case 0x00:
                   7034:        case 0x01:
                   7035: //             REG8(AL) = 0x86;
                   7036:                REG8(AL) = 0x00;
                   7037:                break;
                   7038:        default:
                   7039:                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));
                   7040:                m_CF = 1;
                   7041:                break;
                   7042:        }
                   7043: }
                   7044: 
1.1.1.14  root     7045: inline void pcbios_int_10h_1ah()
                   7046: {
                   7047:        switch(REG8(AL)) {
                   7048:        case 0x00:
                   7049:                REG8(AL) = 0x1a;
                   7050:                REG8(BL) = 0x08;
                   7051:                REG8(BH) = 0x00;
                   7052:                break;
                   7053:        default:
1.1.1.22  root     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.14  root     7055:                m_CF = 1;
                   7056:                break;
                   7057:        }
                   7058: }
                   7059: 
1.1       root     7060: inline void pcbios_int_10h_1dh()
                   7061: {
                   7062:        switch(REG8(AL)) {
                   7063:        case 0x01:
                   7064:                break;
                   7065:        case 0x02:
                   7066:                REG16(BX) = 0;
                   7067:                break;
                   7068:        default:
1.1.1.22  root     7069:                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));
                   7070:                m_CF = 1;
                   7071:                break;
                   7072:        }
                   7073: }
                   7074: 
                   7075: inline void pcbios_int_10h_4fh()
                   7076: {
                   7077:        switch(REG8(AL)) {
                   7078:        case 0x00:
                   7079:                REG8(AH) = 0x02; // not supported
                   7080:                break;
                   7081:        case 0x01:
                   7082:        case 0x02:
                   7083:        case 0x03:
                   7084:        case 0x04:
                   7085:        case 0x05:
                   7086:        case 0x06:
                   7087:        case 0x07:
                   7088:        case 0x08:
                   7089:        case 0x09:
                   7090:        case 0x0a:
                   7091:        case 0x0b:
                   7092:        case 0x0c:
                   7093:                REG8(AH) = 0x01; // failed
                   7094:                break;
                   7095:        default:
                   7096:                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     7097:                m_CF = 1;
1.1       root     7098:                break;
                   7099:        }
                   7100: }
                   7101: 
                   7102: inline void pcbios_int_10h_82h()
                   7103: {
                   7104:        static UINT8 mode = 0;
                   7105:        
                   7106:        switch(REG8(AL)) {
1.1.1.22  root     7107:        case 0x00:
1.1       root     7108:                if(REG8(BL) != 0xff) {
                   7109:                        mode = REG8(BL);
                   7110:                }
                   7111:                REG8(AL) = mode;
                   7112:                break;
                   7113:        default:
1.1.1.22  root     7114:                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     7115:                m_CF = 1;
1.1       root     7116:                break;
                   7117:        }
                   7118: }
                   7119: 
1.1.1.22  root     7120: inline void pcbios_int_10h_83h()
                   7121: {
                   7122:        static UINT8 mode = 0;
                   7123:        
                   7124:        switch(REG8(AL)) {
                   7125:        case 0x00:
                   7126:                REG16(AX) = 0; // offset???
                   7127:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
                   7128:                i386_load_segment_descriptor(ES);
                   7129:                REG16(BX) = (SHADOW_BUF_TOP & 0x0f);
                   7130:                break;
                   7131:        default:
                   7132:                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));
                   7133:                m_CF = 1;
                   7134:                break;
                   7135:        }
                   7136: }
                   7137: 
                   7138: inline void pcbios_int_10h_90h()
                   7139: {
                   7140:        REG8(AL) = mem[0x449];
                   7141: }
                   7142: 
                   7143: inline void pcbios_int_10h_91h()
                   7144: {
                   7145:        REG8(AL) = 0x04; // VGA
                   7146: }
                   7147: 
                   7148: inline void pcbios_int_10h_efh()
                   7149: {
                   7150:        REG16(DX) = 0xffff;
                   7151: }
                   7152: 
1.1       root     7153: inline void pcbios_int_10h_feh()
                   7154: {
                   7155:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     7156:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
1.1.1.3   root     7157:                i386_load_segment_descriptor(ES);
1.1.1.8   root     7158:                REG16(DI) = (SHADOW_BUF_TOP & 0x0f);
1.1       root     7159:        }
                   7160:        int_10h_feh_called = true;
                   7161: }
                   7162: 
                   7163: inline void pcbios_int_10h_ffh()
                   7164: {
                   7165:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.23  root     7166:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7167:                COORD co;
                   7168:                DWORD num;
                   7169:                
1.1.1.14  root     7170:                vram_flush();
                   7171:                
                   7172:                co.X = (REG16(DI) >> 1) % scr_width;
                   7173:                co.Y = (REG16(DI) >> 1) / scr_width;
1.1.1.16  root     7174:                int ofs = pcbios_get_shadow_buffer_address(0, co.X, co.Y);
                   7175:                int end = min(ofs + REG16(CX) * 2, pcbios_get_shadow_buffer_address(0, 0, scr_height));
1.1.1.14  root     7176:                int len;
                   7177:                for(len = 0; ofs < end; len++) {
                   7178:                        scr_char[len] = mem[ofs++];
                   7179:                        scr_attr[len] = mem[ofs++];
                   7180:                }
                   7181:                co.Y += scr_top;
                   7182:                WriteConsoleOutputCharacter(hStdout, scr_char, len, co, &num);
                   7183:                WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     7184:        }
                   7185:        int_10h_ffh_called = true;
                   7186: }
                   7187: 
1.1.1.25  root     7188: inline void pcbios_int_14h_00h()
                   7189: {
1.1.1.29  root     7190:        if(REG16(DX) < 4) {
1.1.1.25  root     7191:                static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600};
                   7192:                UINT8 selector = sio_read(REG16(DX), 3);
                   7193:                selector &= ~0x3f;
                   7194:                selector |= REG8(AL) & 0x1f;
                   7195:                UINT16 divisor = 115200 / rate[REG8(AL) >> 5];
                   7196:                sio_write(REG16(DX), 3, selector | 0x80);
                   7197:                sio_write(REG16(DX), 0, divisor & 0xff);
                   7198:                sio_write(REG16(DX), 1, divisor >> 8);
                   7199:                sio_write(REG16(DX), 3, selector);
                   7200:                REG8(AH) = sio_read(REG16(DX), 5);
                   7201:                REG8(AL) = sio_read(REG16(DX), 6);
                   7202:        } else {
                   7203:                REG8(AH) = 0x80;
                   7204:        }
                   7205: }
                   7206: 
                   7207: inline void pcbios_int_14h_01h()
                   7208: {
1.1.1.29  root     7209:        if(REG16(DX) < 4) {
1.1.1.25  root     7210:                UINT8 selector = sio_read(REG16(DX), 3);
                   7211:                sio_write(REG16(DX), 3, selector & ~0x80);
                   7212:                sio_write(REG16(DX), 0, REG8(AL));
                   7213:                sio_write(REG16(DX), 3, selector);
                   7214:                REG8(AH) = sio_read(REG16(DX), 5);
                   7215:        } else {
                   7216:                REG8(AH) = 0x80;
                   7217:        }
                   7218: }
                   7219: 
                   7220: inline void pcbios_int_14h_02h()
                   7221: {
1.1.1.29  root     7222:        if(REG16(DX) < 4) {
1.1.1.25  root     7223:                UINT8 selector = sio_read(REG16(DX), 3);
                   7224:                sio_write(REG16(DX), 3, selector & ~0x80);
                   7225:                REG8(AL) = sio_read(REG16(DX), 0);
                   7226:                sio_write(REG16(DX), 3, selector);
                   7227:                REG8(AH) = sio_read(REG16(DX), 5);
                   7228:        } else {
                   7229:                REG8(AH) = 0x80;
                   7230:        }
                   7231: }
                   7232: 
                   7233: inline void pcbios_int_14h_03h()
                   7234: {
1.1.1.29  root     7235:        if(REG16(DX) < 4) {
1.1.1.25  root     7236:                REG8(AH) = sio_read(REG16(DX), 5);
                   7237:                REG8(AL) = sio_read(REG16(DX), 6);
                   7238:        } else {
                   7239:                REG8(AH) = 0x80;
                   7240:        }
                   7241: }
                   7242: 
                   7243: inline void pcbios_int_14h_04h()
                   7244: {
1.1.1.29  root     7245:        if(REG16(DX) < 4) {
1.1.1.25  root     7246:                UINT8 selector = sio_read(REG16(DX), 3);
                   7247:                if(REG8(CH) <= 0x03) {
                   7248:                        selector = (selector & ~0x03) | REG8(CH);
                   7249:                }
                   7250:                if(REG8(BL) == 0x00) {
                   7251:                        selector &= ~0x04;
                   7252:                } else if(REG8(BL) == 0x01) {
                   7253:                        selector |= 0x04;
                   7254:                }
                   7255:                if(REG8(BH) == 0x00) {
                   7256:                        selector = (selector & ~0x38) | 0x00;
                   7257:                } else if(REG8(BH) == 0x01) {
                   7258:                        selector = (selector & ~0x38) | 0x08;
                   7259:                } else if(REG8(BH) == 0x02) {
                   7260:                        selector = (selector & ~0x38) | 0x18;
                   7261:                } else if(REG8(BH) == 0x03) {
                   7262:                        selector = (selector & ~0x38) | 0x28;
                   7263:                } else if(REG8(BH) == 0x04) {
                   7264:                        selector = (selector & ~0x38) | 0x38;
                   7265:                }
                   7266:                if(REG8(AL) == 0x00) {
                   7267:                        selector |= 0x40;
                   7268:                } else if(REG8(AL) == 0x01) {
                   7269:                        selector &= ~0x40;
                   7270:                }
                   7271:                if(REG8(CL) <= 0x0b) {
                   7272:                        static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200};
                   7273:                        UINT16 divisor = 115200 / rate[REG8(CL)];
                   7274:                        sio_write(REG16(DX), 3, selector | 0x80);
                   7275:                        sio_write(REG16(DX), 0, divisor & 0xff);
                   7276:                        sio_write(REG16(DX), 1, divisor >> 8);
                   7277:                }
                   7278:                sio_write(REG16(DX), 3, selector);
                   7279:                REG8(AH) = sio_read(REG16(DX), 5);
                   7280:                REG8(AL) = sio_read(REG16(DX), 6);
                   7281:        } else {
                   7282:                REG8(AH) = 0x80;
                   7283:        }
                   7284: }
                   7285: 
                   7286: inline void pcbios_int_14h_05h()
                   7287: {
1.1.1.29  root     7288:        if(REG16(DX) < 4) {
1.1.1.25  root     7289:                if(REG8(AL) == 0x00) {
                   7290:                        REG8(BL) = sio_read(REG16(DX), 4);
                   7291:                        REG8(AH) = sio_read(REG16(DX), 5);
                   7292:                        REG8(AL) = sio_read(REG16(DX), 6);
                   7293:                } else if(REG8(AL) == 0x01) {
                   7294:                        sio_write(REG16(DX), 4, REG8(BL));
                   7295:                        REG8(AH) = sio_read(REG16(DX), 5);
                   7296:                        REG8(AL) = sio_read(REG16(DX), 6);
                   7297:                } else {
                   7298:                        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));
                   7299:                }
                   7300:        } else {
                   7301:                REG8(AH) = 0x80;
                   7302:        }
                   7303: }
                   7304: 
1.1.1.14  root     7305: inline void pcbios_int_15h_10h()
                   7306: {
1.1.1.22  root     7307:        switch(REG8(AL)) {
                   7308:        case 0x00:
1.1.1.14  root     7309:                Sleep(10);
1.1.1.35  root     7310:                REQUEST_HARDWRE_UPDATE();
1.1.1.22  root     7311:                break;
                   7312:        default:
                   7313:                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     7314:                REG8(AH) = 0x86;
                   7315:                m_CF = 1;
                   7316:        }
                   7317: }
                   7318: 
1.1       root     7319: inline void pcbios_int_15h_23h()
                   7320: {
                   7321:        switch(REG8(AL)) {
1.1.1.22  root     7322:        case 0x00:
1.1.1.8   root     7323:                REG8(CL) = cmos_read(0x2d);
                   7324:                REG8(CH) = cmos_read(0x2e);
1.1       root     7325:                break;
1.1.1.22  root     7326:        case 0x01:
1.1.1.8   root     7327:                cmos_write(0x2d, REG8(CL));
                   7328:                cmos_write(0x2e, REG8(CH));
1.1       root     7329:                break;
                   7330:        default:
1.1.1.22  root     7331:                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     7332:                REG8(AH) = 0x86;
1.1.1.3   root     7333:                m_CF = 1;
1.1       root     7334:                break;
                   7335:        }
                   7336: }
                   7337: 
                   7338: inline void pcbios_int_15h_24h()
                   7339: {
                   7340:        switch(REG8(AL)) {
1.1.1.22  root     7341:        case 0x00:
1.1.1.3   root     7342:                i386_set_a20_line(0);
1.1       root     7343:                REG8(AH) = 0;
                   7344:                break;
1.1.1.22  root     7345:        case 0x01:
1.1.1.3   root     7346:                i386_set_a20_line(1);
1.1       root     7347:                REG8(AH) = 0;
                   7348:                break;
1.1.1.22  root     7349:        case 0x02:
1.1       root     7350:                REG8(AH) = 0;
1.1.1.3   root     7351:                REG8(AL) = (m_a20_mask >> 20) & 1;
1.1       root     7352:                REG16(CX) = 0;
                   7353:                break;
1.1.1.22  root     7354:        case 0x03:
1.1       root     7355:                REG16(AX) = 0;
                   7356:                REG16(BX) = 0;
                   7357:                break;
1.1.1.22  root     7358:        default:
                   7359:                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));
                   7360:                REG8(AH) = 0x86;
                   7361:                m_CF = 1;
                   7362:                break;
1.1       root     7363:        }
                   7364: }
                   7365: 
                   7366: inline void pcbios_int_15h_49h()
                   7367: {
1.1.1.27  root     7368:        REG8(AH) = 0x00;
                   7369:        REG8(BL) = 0x00; // DOS/V
1.1       root     7370: }
                   7371: 
1.1.1.22  root     7372: inline void pcbios_int_15h_50h()
                   7373: {
                   7374:        switch(REG8(AL)) {
                   7375:        case 0x00:
                   7376:        case 0x01:
                   7377:                if(REG8(BH) != 0x00 && REG8(BH) != 0x01) {
                   7378:                        REG8(AH) = 0x01; // invalid font type in bh
                   7379:                        m_CF = 1;
1.1.1.27  root     7380:                } else if(REG8(BL) != 0x00) {
1.1.1.22  root     7381:                        REG8(AH) = 0x02; // bl not zero
                   7382:                        m_CF = 1;
                   7383:                } else if(REG16(BP) != 0 && REG16(BP) != 437 && REG16(BP) != 932 && REG16(BP) != 934 && REG16(BP) != 936 && REG16(BP) != 938) {
                   7384:                        REG8(AH) = 0x04; // invalid code page
                   7385:                        m_CF = 1;
1.1.1.27  root     7386:                } else if(REG8(AL) == 0x01) {
                   7387:                        REG8(AH) = 0x06; // font is read only
1.1.1.22  root     7388:                        m_CF = 1;
1.1.1.27  root     7389:                } else {
                   7390:                        // dummy font read routine is at fffd:000d
                   7391:                        SREG(ES) = 0xfffd;
                   7392:                        i386_load_segment_descriptor(ES);
1.1.1.32  root     7393:                        REG16(BX) = 0x000d;
1.1.1.27  root     7394:                        REG8(AH) = 0x00; // success
1.1.1.22  root     7395:                }
                   7396:                break;
                   7397:        default:
                   7398:                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));
                   7399:                REG8(AH) = 0x86;
                   7400:                m_CF = 1;
                   7401:                break;
                   7402:        }
                   7403: }
                   7404: 
1.1.1.30  root     7405: inline void pcbios_int_15h_53h()
                   7406: {
                   7407:        switch(REG8(AL)) {
                   7408:        case 0x00:
                   7409:                // APM is not installed
                   7410:                REG8(AH) = 0x86;
                   7411:                m_CF = 1;
                   7412:                break;
                   7413:        default:
                   7414:                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));
                   7415:                REG8(AH) = 0x86;
                   7416:                m_CF = 1;
                   7417:                break;
                   7418:        }
                   7419: }
                   7420: 
1.1.1.35  root     7421: 
                   7422: DWORD WINAPI pcbios_int_15h_86h_thread(LPVOID)
1.1       root     7423: {
                   7424:        UINT32 usec = (REG16(CX) << 16) | REG16(DX);
1.1.1.14  root     7425:        UINT32 msec = usec / 1000;
                   7426:        
1.1.1.35  root     7427:        while(msec && !m_halted) {
1.1.1.14  root     7428:                UINT32 tmp = min(msec, 100);
                   7429:                if(msec - tmp < 10) {
                   7430:                        tmp = msec;
                   7431:                }
                   7432:                Sleep(tmp);
                   7433:                msec -= tmp;
                   7434:        }
1.1.1.35  root     7435:        
                   7436: #ifdef USE_SERVICE_THREAD
                   7437:        service_exit = true;
                   7438: #endif
                   7439:        return(0);
                   7440: }
                   7441: 
                   7442: inline void pcbios_int_15h_86h()
                   7443: {
                   7444:        if(!(REG16(CX) == 0 && REG16(DX) == 0)) {
                   7445: #ifdef USE_SERVICE_THREAD
                   7446:                start_service_loop(pcbios_int_15h_86h_thread);
                   7447: #else
                   7448:                pcbios_int_15h_86h_thread(NULL);
                   7449:                REQUEST_HARDWRE_UPDATE();
                   7450: #endif
                   7451:        }
1.1       root     7452: }
                   7453: 
                   7454: inline void pcbios_int_15h_87h()
                   7455: {
                   7456:        // copy extended memory (from DOSBox)
                   7457:        int len = REG16(CX) * 2;
1.1.1.3   root     7458:        int ofs = SREG_BASE(ES) + REG16(SI);
1.1       root     7459:        int src = (*(UINT32 *)(mem + ofs + 0x12) & 0xffffff); // + (mem[ofs + 0x16] << 24);
                   7460:        int dst = (*(UINT32 *)(mem + ofs + 0x1a) & 0xffffff); // + (mem[ofs + 0x1e] << 24);
                   7461:        memcpy(mem + dst, mem + src, len);
                   7462:        REG16(AX) = 0x00;
                   7463: }
                   7464: 
                   7465: inline void pcbios_int_15h_88h()
                   7466: {
1.1.1.17  root     7467:        REG16(AX) = ((min(MAX_MEM, 0x4000000) - 0x100000) >> 10);
1.1       root     7468: }
                   7469: 
                   7470: inline void pcbios_int_15h_89h()
                   7471: {
1.1.1.21  root     7472: #if defined(HAS_I286) || defined(HAS_I386)
1.1       root     7473:        // switch to protected mode (from DOSBox)
                   7474:        write_io_byte(0x20, 0x10);
                   7475:        write_io_byte(0x21, REG8(BH));
                   7476:        write_io_byte(0x21, 0x00);
                   7477:        write_io_byte(0xa0, 0x10);
                   7478:        write_io_byte(0xa1, REG8(BL));
                   7479:        write_io_byte(0xa1, 0x00);
1.1.1.3   root     7480:        i386_set_a20_line(1);
                   7481:        int ofs = SREG_BASE(ES) + REG16(SI);
                   7482:        m_gdtr.limit = *(UINT16 *)(mem + ofs + 0x08);
                   7483:        m_gdtr.base = *(UINT32 *)(mem + ofs + 0x08 + 0x02) & 0xffffff;
                   7484:        m_idtr.limit = *(UINT16 *)(mem + ofs + 0x10);
                   7485:        m_idtr.base = *(UINT32 *)(mem + ofs + 0x10 + 0x02) & 0xffffff;
                   7486: #if defined(HAS_I386)
                   7487:        m_cr[0] |= 1;
                   7488: #else
                   7489:        m_msw |= 1;
                   7490: #endif
                   7491:        SREG(DS) = 0x18;
                   7492:        SREG(ES) = 0x20;
                   7493:        SREG(SS) = 0x28;
                   7494:        i386_load_segment_descriptor(DS);
                   7495:        i386_load_segment_descriptor(ES);
                   7496:        i386_load_segment_descriptor(SS);
1.1.1.21  root     7497:        UINT16 offset = *(UINT16 *)(mem + SREG_BASE(SS) + REG16(SP));
1.1       root     7498:        REG16(SP) += 6;
1.1.1.3   root     7499: #if defined(HAS_I386)
1.1.1.21  root     7500:        UINT32 flags = get_flags();
                   7501:        flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
                   7502:        set_flags(flags);
1.1.1.3   root     7503: #else
1.1.1.21  root     7504:        UINT32 flags = CompressFlags();
                   7505:        flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
                   7506:        ExpandFlags(flags);
1.1.1.3   root     7507: #endif
1.1       root     7508:        REG16(AX) = 0x00;
1.1.1.21  root     7509:        i386_jmp_far(0x30, /*REG16(CX)*/offset);
1.1       root     7510: #else
1.1.1.21  root     7511:        // i86/i186/v30: protected mode is not supported
1.1       root     7512:        REG8(AH) = 0x86;
1.1.1.3   root     7513:        m_CF = 1;
1.1       root     7514: #endif
                   7515: }
                   7516: 
1.1.1.21  root     7517: inline void pcbios_int_15h_8ah()
                   7518: {
                   7519:        UINT32 size = MAX_MEM - 0x100000;
                   7520:        REG16(AX) = size & 0xffff;
                   7521:        REG16(DX) = size >> 16;
                   7522: }
                   7523: 
1.1.1.3   root     7524: #if defined(HAS_I386)
1.1       root     7525: inline void pcbios_int_15h_c9h()
                   7526: {
                   7527:        REG8(AH) = 0x00;
                   7528:        REG8(CH) = cpu_type;
                   7529:        REG8(CL) = cpu_step;
                   7530: }
1.1.1.3   root     7531: #endif
1.1       root     7532: 
                   7533: inline void pcbios_int_15h_cah()
                   7534: {
                   7535:        switch(REG8(AL)) {
1.1.1.22  root     7536:        case 0x00:
1.1       root     7537:                if(REG8(BL) > 0x3f) {
                   7538:                        REG8(AH) = 0x03;
1.1.1.3   root     7539:                        m_CF = 1;
1.1       root     7540:                } else if(REG8(BL) < 0x0e) {
                   7541:                        REG8(AH) = 0x04;
1.1.1.3   root     7542:                        m_CF = 1;
1.1       root     7543:                } else {
1.1.1.8   root     7544:                        REG8(CL) = cmos_read(REG8(BL));
1.1       root     7545:                }
                   7546:                break;
1.1.1.22  root     7547:        case 0x01:
1.1       root     7548:                if(REG8(BL) > 0x3f) {
                   7549:                        REG8(AH) = 0x03;
1.1.1.3   root     7550:                        m_CF = 1;
1.1       root     7551:                } else if(REG8(BL) < 0x0e) {
                   7552:                        REG8(AH) = 0x04;
1.1.1.3   root     7553:                        m_CF = 1;
1.1       root     7554:                } else {
1.1.1.8   root     7555:                        cmos_write(REG8(BL), REG8(CL));
1.1       root     7556:                }
                   7557:                break;
                   7558:        default:
1.1.1.22  root     7559:                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     7560:                REG8(AH) = 0x86;
1.1.1.3   root     7561:                m_CF = 1;
1.1       root     7562:                break;
                   7563:        }
                   7564: }
                   7565: 
1.1.1.22  root     7566: inline void pcbios_int_15h_e8h()
1.1.1.17  root     7567: {
1.1.1.22  root     7568:        switch(REG8(AL)) {
                   7569: #if defined(HAS_I386)
                   7570:        case 0x01:
                   7571:                REG16(AX) = REG16(CX) = ((min(MAX_MEM, 0x1000000) - 0x100000) >> 10);
                   7572:                REG16(BX) = REG16(DX) = ((max(MAX_MEM, 0x1000000) - 0x1000000) >> 16);
                   7573:                break;
1.1.1.17  root     7574: #endif
1.1.1.22  root     7575:        default:
                   7576:                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));
                   7577:                REG8(AH) = 0x86;
                   7578:                m_CF = 1;
                   7579:                break;
                   7580:        }
                   7581: }
1.1.1.17  root     7582: 
1.1.1.33  root     7583: void pcbios_update_key_code(bool wait)
1.1       root     7584: {
1.1.1.32  root     7585:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     7586: #ifdef USE_SERVICE_THREAD
                   7587:                EnterCriticalSection(&key_buf_crit_sect);
                   7588: #endif
                   7589:                bool empty = key_buf_char->empty();
                   7590: #ifdef USE_SERVICE_THREAD
                   7591:                LeaveCriticalSection(&key_buf_crit_sect);
                   7592: #endif
                   7593:                if(empty) {
1.1.1.32  root     7594:                        if(!update_key_buffer()) {
1.1.1.33  root     7595:                                if(wait) {
1.1.1.32  root     7596:                                        Sleep(10);
                   7597:                                } else {
                   7598:                                        maybe_idle();
                   7599:                                }
1.1.1.14  root     7600:                        }
                   7601:                }
1.1.1.34  root     7602:        }
                   7603:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     7604: #ifdef USE_SERVICE_THREAD
                   7605:                EnterCriticalSection(&key_buf_crit_sect);
                   7606: #endif
1.1.1.32  root     7607:                if(key_buf_char->count() != 0) {
1.1.1.35  root     7608:                        key_code  = key_buf_char->read() << 0;
                   7609:                        key_code |= key_buf_scan->read() << 8;
                   7610:                        key_recv  = 0x0000ffff;
1.1.1.32  root     7611:                }
                   7612:                if(key_buf_char->count() != 0) {
1.1.1.35  root     7613:                        key_code |= key_buf_char->read() << 16;
                   7614:                        key_code |= key_buf_scan->read() << 24;
1.1.1.33  root     7615:                        key_recv |= 0xffff0000;
1.1.1.32  root     7616:                }
1.1.1.35  root     7617: #ifdef USE_SERVICE_THREAD
                   7618:                LeaveCriticalSection(&key_buf_crit_sect);
                   7619: #endif
1.1       root     7620:        }
                   7621: }
                   7622: 
1.1.1.35  root     7623: DWORD WINAPI pcbios_int_16h_00h_thread(LPVOID)
1.1       root     7624: {
1.1.1.33  root     7625:        while(key_recv == 0 && !m_halted) {
                   7626:                pcbios_update_key_code(true);
1.1       root     7627:        }
1.1.1.33  root     7628:        if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
                   7629:                if((key_code & 0xffff) == 0x0000 || (key_code & 0xffff) == 0xe000) {
                   7630:                        if(REG8(AH) == 0x10) {
                   7631:                                key_code = ((key_code >> 8) & 0xff) | ((key_code >> 16) & 0xff00);
                   7632:                        } else {
                   7633:                                key_code = ((key_code >> 16) & 0xff00);
                   7634:                        }
                   7635:                        key_recv >>= 16;
1.1       root     7636:                }
                   7637:        }
                   7638:        REG16(AX) = key_code & 0xffff;
                   7639:        key_code >>= 16;
1.1.1.33  root     7640:        key_recv >>= 16;
1.1.1.35  root     7641:        
                   7642: #ifdef USE_SERVICE_THREAD
                   7643:        service_exit = true;
                   7644: #endif
                   7645:        return(0);
                   7646: }
                   7647: 
                   7648: inline void pcbios_int_16h_00h()
                   7649: {
                   7650: #ifdef USE_SERVICE_THREAD
                   7651:        start_service_loop(pcbios_int_16h_00h_thread);
                   7652: #else
                   7653:        pcbios_int_16h_00h_thread(NULL);
                   7654:        REQUEST_HARDWRE_UPDATE();
                   7655: #endif
1.1       root     7656: }
                   7657: 
                   7658: inline void pcbios_int_16h_01h()
                   7659: {
1.1.1.33  root     7660:        if(key_recv == 0) {
                   7661:                pcbios_update_key_code(false);
1.1.1.5   root     7662:        }
1.1.1.33  root     7663:        if(key_recv != 0) {
                   7664:                UINT32 key_code_tmp = key_code;
                   7665:                if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
                   7666:                        if((key_code_tmp & 0xffff) == 0x0000 || (key_code_tmp & 0xffff) == 0xe000) {
                   7667:                                if(REG8(AH) == 0x11) {
                   7668:                                        key_code_tmp = ((key_code_tmp >> 8) & 0xff) | ((key_code_tmp >> 16) & 0xff00);
                   7669:                                } else {
                   7670:                                        key_code_tmp = ((key_code_tmp >> 16) & 0xff00);
                   7671:                                }
                   7672:                        }
1.1       root     7673:                }
1.1.1.5   root     7674:                REG16(AX) = key_code_tmp & 0xffff;
1.1.1.3   root     7675: #if defined(HAS_I386)
1.1.1.33  root     7676:                m_ZF = 0;
                   7677: #else
                   7678:                m_ZeroVal = 1;
                   7679: #endif
                   7680:        } else {
                   7681: #if defined(HAS_I386)
                   7682:                m_ZF = 1;
1.1.1.3   root     7683: #else
1.1.1.33  root     7684:                m_ZeroVal = 0;
1.1.1.3   root     7685: #endif
1.1.1.33  root     7686:        }
1.1       root     7687: }
                   7688: 
                   7689: inline void pcbios_int_16h_02h()
                   7690: {
                   7691:        REG8(AL)  = (GetAsyncKeyState(VK_INSERT ) & 0x0001) ? 0x80 : 0;
                   7692:        REG8(AL) |= (GetAsyncKeyState(VK_CAPITAL) & 0x0001) ? 0x40 : 0;
                   7693:        REG8(AL) |= (GetAsyncKeyState(VK_NUMLOCK) & 0x0001) ? 0x20 : 0;
                   7694:        REG8(AL) |= (GetAsyncKeyState(VK_SCROLL ) & 0x0001) ? 0x10 : 0;
                   7695:        REG8(AL) |= (GetAsyncKeyState(VK_MENU   ) & 0x8000) ? 0x08 : 0;
                   7696:        REG8(AL) |= (GetAsyncKeyState(VK_CONTROL) & 0x8000) ? 0x04 : 0;
                   7697:        REG8(AL) |= (GetAsyncKeyState(VK_LSHIFT ) & 0x8000) ? 0x02 : 0;
                   7698:        REG8(AL) |= (GetAsyncKeyState(VK_RSHIFT ) & 0x8000) ? 0x01 : 0;
                   7699: }
                   7700: 
                   7701: inline void pcbios_int_16h_03h()
                   7702: {
                   7703:        static UINT16 status = 0;
                   7704:        
                   7705:        switch(REG8(AL)) {
                   7706:        case 0x05:
                   7707:                status = REG16(BX);
                   7708:                break;
                   7709:        case 0x06:
                   7710:                REG16(BX) = status;
                   7711:                break;
                   7712:        default:
1.1.1.3   root     7713:                m_CF = 1;
1.1       root     7714:                break;
                   7715:        }
                   7716: }
                   7717: 
                   7718: inline void pcbios_int_16h_05h()
                   7719: {
1.1.1.32  root     7720:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     7721: #ifdef USE_SERVICE_THREAD
                   7722:                EnterCriticalSection(&key_buf_crit_sect);
                   7723: #endif
1.1.1.32  root     7724:                key_buf_char->write(REG8(CL));
                   7725:                key_buf_scan->write(REG8(CH));
1.1.1.35  root     7726: #ifdef USE_SERVICE_THREAD
                   7727:                LeaveCriticalSection(&key_buf_crit_sect);
                   7728: #endif
1.1.1.32  root     7729:        }
1.1       root     7730:        REG8(AL) = 0x00;
                   7731: }
                   7732: 
                   7733: inline void pcbios_int_16h_12h()
                   7734: {
                   7735:        pcbios_int_16h_02h();
                   7736:        
                   7737:        REG8(AH)  = 0;//(GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x80 : 0;
                   7738:        REG8(AH) |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   7739:        REG8(AH) |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   7740:        REG8(AH) |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
                   7741:        REG8(AH) |= (GetAsyncKeyState(VK_RMENU   ) & 0x8000) ? 0x08 : 0;
                   7742:        REG8(AH) |= (GetAsyncKeyState(VK_RCONTROL) & 0x8000) ? 0x04 : 0;
                   7743:        REG8(AH) |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   7744:        REG8(AH) |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   7745: }
                   7746: 
                   7747: inline void pcbios_int_16h_13h()
                   7748: {
                   7749:        static UINT16 status = 0;
                   7750:        
                   7751:        switch(REG8(AL)) {
                   7752:        case 0x00:
                   7753:                status = REG16(DX);
                   7754:                break;
                   7755:        case 0x01:
                   7756:                REG16(DX) = status;
                   7757:                break;
                   7758:        default:
1.1.1.22  root     7759:                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     7760:                m_CF = 1;
1.1       root     7761:                break;
                   7762:        }
                   7763: }
                   7764: 
                   7765: inline void pcbios_int_16h_14h()
                   7766: {
                   7767:        static UINT8 status = 0;
                   7768:        
                   7769:        switch(REG8(AL)) {
                   7770:        case 0x00:
                   7771:        case 0x01:
                   7772:                status = REG8(AL);
                   7773:                break;
                   7774:        case 0x02:
                   7775:                REG8(AL) = status;
                   7776:                break;
                   7777:        default:
1.1.1.22  root     7778:                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     7779:                m_CF = 1;
1.1       root     7780:                break;
                   7781:        }
                   7782: }
                   7783: 
1.1.1.24  root     7784: inline void pcbios_int_16h_55h()
                   7785: {
                   7786:        switch(REG8(AL)) {
                   7787:        case 0x00:
                   7788:                // keyboard tsr is not present
                   7789:                break;
                   7790:        case 0xfe:
                   7791:                // handlers for INT 08, INT 09, INT 16, INT 1B, and INT 1C are installed
                   7792:                break;
                   7793:        case 0xff:
                   7794:                break;
                   7795:        default:
                   7796:                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));
                   7797:                m_CF = 1;
                   7798:                break;
                   7799:        }
                   7800: }
                   7801: 
1.1.1.30  root     7802: inline void pcbios_int_16h_6fh()
                   7803: {
                   7804:        switch(REG8(AL)) {
                   7805:        case 0x00:
                   7806:                // HP Vectra EX-BIOS - "F16_INQUIRE" - Extended BIOS is not installed
                   7807:                break;
                   7808:        default:
                   7809:                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));
                   7810:                m_CF = 1;
                   7811:                break;
                   7812:        }
                   7813: }
                   7814: 
1.1.1.37  root     7815: UINT16 pcbios_printer_jis2sjis(UINT16 jis)
                   7816: {
                   7817:        UINT8 hi = jis >> 8;
                   7818:        UINT8 lo = jis & 0xff;
                   7819:        
                   7820:        lo = (hi & 0x01) ? lo - 0x1f : lo + 0x7d;
                   7821:        hi = (hi - 0x21) / 2 + 0x81;
                   7822:        hi = (hi >= 0xa0) ? hi + 0x40 : hi;
                   7823:        lo = (lo >= 0x7f) ? lo + 0x01 : lo;
                   7824:        
                   7825:        return((hi << 8) + lo);
                   7826: }
                   7827: 
                   7828: UINT16 pcbios_printer_sjis2jis(UINT16 sjis)
                   7829: {
                   7830:        UINT8 hi = sjis >> 8;
                   7831:        UINT8 lo = sjis & 0xff;
                   7832:        
                   7833:        if(hi == 0x80 || (hi >= 0xeb && hi <= 0xef) || (hi >= 0xf4 && hi <= 0xff)) {
                   7834:                return(0x2121);
                   7835:        }
                   7836:        if((lo >= 0x00 && lo <= 0x3f) || lo == 0x7f || (lo >= 0xfd && lo <= 0xff)) {
                   7837:                return(0x2121);
                   7838:        }
                   7839:        if(hi >= 0xf0 && hi <= 0xf3) {
                   7840:                // gaiji
                   7841:                if(lo >= 0x40 && lo <= 0x7e) {
                   7842:                        return(0x7721 + lo - 0x40 + (hi - 0xf0) * 0x200);
                   7843:                }
                   7844:                if(lo >= 0x80 && lo <= 0x9e) {
                   7845:                        return(0x7760 + lo - 0x80 + (hi - 0xf0) * 0x200);
                   7846:                }
                   7847:                if(lo >= 0x9f && lo <= 0xfc) {
                   7848:                        return(0x7821 + lo - 0x9f + (hi - 0xf0) * 0x200);
                   7849:                }
                   7850:        }
                   7851:        hi = (hi >= 0xe0) ? hi - 0x40 : hi;
                   7852:        lo = (lo >= 0x80) ? lo - 0x01 : lo;
                   7853:        hi = ((lo >= 0x9e) ? 1 : 0) + (hi - 0x81) * 2 + 0x21;
                   7854:        lo = ((lo >= 0x9e) ? lo - 0x9e : lo - 0x40) + 0x21;
                   7855:        
                   7856:        return((hi << 8) + lo);
                   7857: }
                   7858: 
1.1.1.38  root     7859: // AX�e�N�j�J�����t�@�����X�K�C�h 1989
                   7860: // �t�^10 ���{��g��PRINTER DRIVER NIOS�T�d�l
                   7861: // 6. �R���g���[���R�[�h�̉��
1.1.1.37  root     7862: 
                   7863: void pcbios_printer_out(int c, UINT8 data)
                   7864: {
                   7865:        if(pio[c].conv_mode) {
                   7866:                if(pio[c].sjis_hi != 0) {
                   7867:                        if(!pio[c].jis_mode) {
                   7868:                                printer_out(c, 0x1c);
                   7869:                                printer_out(c, 0x26);
                   7870:                                pio[c].jis_mode = true;
                   7871:                        }
                   7872:                        UINT16 jis = pcbios_printer_sjis2jis((pio[c].sjis_hi << 8) | data);
                   7873:                        printer_out(c, jis >> 8);
                   7874:                        printer_out(c, jis & 0xff);
                   7875:                        pio[c].sjis_hi = 0;
                   7876:                } else if(pio[c].esc_buf[0] == 0x1b) {
                   7877:                        printer_out(c, data);
                   7878:                        if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
                   7879:                                pio[c].esc_buf[pio[c].esc_len] = data;
                   7880:                        }
                   7881:                        pio[c].esc_len++;
                   7882:                        
                   7883:                        switch(pio[c].esc_buf[1]) {
1.1.1.38  root     7884:                        case 0x33: // 1Bh 33h XX
                   7885:                        case 0x4a: // 1Bh 4Ah XX
                   7886:                        case 0x4e: // 1Bh 4Eh XX
                   7887:                        case 0x51: // 1Bh 51h XX
                   7888:                        case 0x55: // 1Bh 55h XX
                   7889:                        case 0x6c: // 1Bh 6Ch XX
                   7890:                        case 0x71: // 1Bh 71h XX
                   7891:                        case 0x72: // 1Bh 72h XX
1.1.1.37  root     7892:                                if(pio[c].esc_len == 3) {
                   7893:                                        pio[c].esc_buf[0] = 0x00;
                   7894:                                }
                   7895:                                break;
1.1.1.38  root     7896:                        case 0x24: // 1Bh 24h XX XX
                   7897:                        case 0x5c: // 1Bh 5Ch XX XX
1.1.1.37  root     7898:                                if(pio[c].esc_len == 4) {
                   7899:                                        pio[c].esc_buf[0] = 0x00;
                   7900:                                }
                   7901:                                break;
1.1.1.38  root     7902:                        case 0x2a: // 1Bh 2Ah XX XX XX data
1.1.1.37  root     7903:                                if(pio[c].esc_len >= 3) {
                   7904:                                        switch(pio[c].esc_buf[2]) {
                   7905:                                        case 0: case 1: case 2: case 3: case 4: case 6:
                   7906:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 1) {
                   7907:                                                        pio[c].esc_buf[0] = 0x00;
                   7908:                                                }
                   7909:                                                break;
                   7910:                                        case 32: case 33: case 38: case 39: case 40:
                   7911:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 3) {
                   7912:                                                        pio[c].esc_buf[0] = 0x00;
                   7913:                                                }
                   7914:                                                break;
1.1.1.38  root     7915:                                        case 71: case 72: case 73:
                   7916:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 6) {
                   7917:                                                        pio[c].esc_buf[0] = 0x00;
                   7918:                                                }
                   7919:                                                break;
1.1.1.37  root     7920:                                        default:
                   7921:                                                pio[c].esc_buf[0] = 0x00;
                   7922:                                                break;
                   7923:                                        }
                   7924:                                }
                   7925:                                break;
1.1.1.38  root     7926:                        case 0x40: // 1Bh 40h
1.1.1.37  root     7927:                                if(pio[c].jis_mode) {
                   7928:                                        printer_out(c, 0x1c);
                   7929:                                        printer_out(c, 0x2e);
                   7930:                                        pio[c].jis_mode = false;
                   7931:                                }
                   7932:                                pio[c].esc_buf[0] = 0x00;
                   7933:                                break;
1.1.1.38  root     7934:                        case 0x42: // 1Bh 42h data 00h
                   7935:                        case 0x44: // 1Bh 44h data 00h
1.1.1.37  root     7936:                                if(pio[c].esc_len >= 3 && data == 0) {
                   7937:                                        pio[c].esc_buf[0] = 0x00;
                   7938:                                }
                   7939:                                break;
1.1.1.38  root     7940:                        case 0x43: // 1Bh 43h (00h) XX
1.1.1.37  root     7941:                                if(pio[c].esc_len >= 3 && data != 0) {
                   7942:                                        pio[c].esc_buf[0] = 0x00;
                   7943:                                }
                   7944:                                break;
1.1.1.38  root     7945:                        default: // 1Bh XX
1.1.1.37  root     7946:                                pio[c].esc_buf[0] = 0x00;
                   7947:                                break;
                   7948:                        }
                   7949:                } else if(pio[c].esc_buf[0] == 0x1c) {
                   7950:                        printer_out(c, data);
                   7951:                        if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
                   7952:                                pio[c].esc_buf[pio[c].esc_len] = data;
                   7953:                        }
                   7954:                        pio[c].esc_len++;
                   7955:                        
                   7956:                        switch(pio[c].esc_buf[1]) {
1.1.1.38  root     7957:                        case 0x21: // 1Ch 21h XX
                   7958:                        case 0x2d: // 1Ch 2Dh XX
                   7959:                        case 0x57: // 1Ch 57h XX
                   7960:                        case 0x6b: // 1Ch 6Bh XX
                   7961:                        case 0x72: // 1Ch 72h XX
                   7962:                        case 0x78: // 1Ch 78h XX
1.1.1.37  root     7963:                                if(pio[c].esc_len == 3) {
                   7964:                                        pio[c].esc_buf[0] = 0x00;
                   7965:                                }
                   7966:                                break;
1.1.1.38  root     7967:                        case 0x26: // 1Ch 26h
1.1.1.37  root     7968:                                pio[c].jis_mode = true;
                   7969:                                pio[c].esc_buf[0] = 0x00;
                   7970:                                break;
1.1.1.38  root     7971:                        case 0x2e: // 1Ch 2Eh
1.1.1.37  root     7972:                                pio[c].jis_mode = false;
                   7973:                                pio[c].esc_buf[0] = 0x00;
                   7974:                                break;
1.1.1.38  root     7975:                        case 0x32: // 1Ch 32h XX XX data
1.1.1.37  root     7976:                                if(pio[c].esc_len == 76) {
                   7977:                                        pio[c].esc_buf[0] = 0x00;
                   7978:                                }
                   7979:                                break;
1.1.1.38  root     7980:                        case 0x44: // 1Bh 44h data 00h
1.1.1.37  root     7981:                                if(pio[c].esc_len == 6) {
                   7982:                                        pio[c].esc_buf[0] = 0x00;
                   7983:                                }
                   7984:                                break;
1.1.1.38  root     7985:                        case 0x53: // 1Ch 53h XX XX
                   7986:                        case 0x54: // 1Ch 54h XX XX
1.1.1.37  root     7987:                                if(pio[c].esc_len == 4) {
                   7988:                                        pio[c].esc_buf[0] = 0x00;
                   7989:                                }
                   7990:                                break;
1.1.1.38  root     7991:                        default: // 1Ch XX
1.1.1.37  root     7992:                                pio[c].esc_buf[0] = 0x00;
                   7993:                                break;
                   7994:                        }
                   7995:                } else if(data == 0x1b || data == 0x1c) {
                   7996:                        printer_out(c, data);
                   7997:                        pio[c].esc_buf[0] = data;
                   7998:                        pio[c].esc_len = 1;
                   7999:                } else if((data >= 0x80 && data <= 0x9f) || (data >= 0xe0 && data <= 0xff)) {
                   8000:                        pio[c].sjis_hi = data;
                   8001:                } else {
                   8002:                        if(pio[c].jis_mode) {
                   8003:                                printer_out(c, 0x1c);
                   8004:                                printer_out(c, 0x2e);
                   8005:                                pio[c].jis_mode = false;
                   8006:                        }
                   8007:                        printer_out(c, data);
                   8008:                }
                   8009:        } else {
                   8010:                if(pio[c].jis_mode) {
                   8011:                        printer_out(c, 0x1c);
                   8012:                        printer_out(c, 0x2e);
                   8013:                        pio[c].jis_mode = false;
                   8014:                }
                   8015:                printer_out(c, data);
                   8016:        }
                   8017: }
                   8018: 
                   8019: inline void pcbios_int_17h_00h()
                   8020: {
                   8021:        if(REG16(DX) < 3) {
                   8022:                pcbios_printer_out(REG16(DX), REG8(AL));
                   8023:                REG8(AH) = 0xd0;
                   8024:        }
                   8025: }
                   8026: 
                   8027: inline void pcbios_int_17h_01h()
                   8028: {
                   8029:        if(REG16(DX) < 3) {
                   8030:                REG8(AH) = 0xd0;
                   8031:        }
                   8032: }
                   8033: 
                   8034: inline void pcbios_int_17h_02h()
                   8035: {
                   8036:        if(REG16(DX) < 3) {
                   8037:                REG8(AH) = 0xd0;
                   8038:        }
                   8039: }
                   8040: 
                   8041: inline void pcbios_int_17h_03h()
                   8042: {
                   8043:        switch(REG8(AL)) {
                   8044:        case 0x00:
                   8045:                if(REG16(DX) < 3) {
                   8046:                        if(pio[REG16(DX)].jis_mode) {
                   8047:                                printer_out(REG16(DX), 0x1c);
                   8048:                                printer_out(REG16(DX), 0x2e);
                   8049:                                pio[REG16(DX)].jis_mode = false;
                   8050:                        }
                   8051:                        for(UINT16 i = 0; i < REG16(CX); i++) {
                   8052:                                printer_out(REG16(DX), mem[SREG_BASE(ES) + REG16(BX) + i]);
                   8053:                        }
                   8054:                        REG16(CX) = 0x0000;
                   8055:                        REG8(AH) = 0xd0;
                   8056:                }
                   8057:                break;
                   8058:        default:
                   8059:                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));
                   8060:                break;
                   8061:        }
                   8062: }
                   8063: 
                   8064: inline void pcbios_int_17h_50h()
                   8065: {
                   8066:        switch(REG8(AL)) {
                   8067:        case 0x00:
                   8068:                if(REG16(DX) < 3) {
                   8069:                        if(REG16(BX) = 0x0001) {
                   8070:                                pio[REG16(DX)].conv_mode = false;
                   8071:                                REG8(AL) = 0x00;
                   8072:                        } else if(REG16(BX) = 0x0051) {
                   8073:                                pio[REG16(DX)].conv_mode = true;
                   8074:                                REG8(AL) = 0x00;
                   8075:                        } else {
                   8076:                                REG8(AL) = 0x01;
                   8077:                        }
                   8078:                } else {
                   8079:                        REG8(AL) = 0x02;
                   8080:                }
                   8081:                break;
                   8082:        case 0x01:
                   8083:                if(REG16(DX) < 3) {
                   8084:                        if(pio[REG16(DX)].conv_mode) {
                   8085:                                REG16(BX) = 0x0051;
                   8086:                        } else {
                   8087:                                REG16(BX) = 0x0001;
                   8088:                        }
                   8089:                        REG8(AL) = 0x00;
                   8090:                } else {
                   8091:                        REG8(AL) = 0x02;
                   8092:                }
                   8093:                break;
                   8094:        default:
                   8095:                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));
                   8096:                break;
                   8097:        }
                   8098: }
                   8099: 
                   8100: inline void pcbios_int_17h_51h()
                   8101: {
                   8102:        if(REG8(DH) >= 0x21 && REG8(DH) <= 0x7e && REG8(DL) >= 0x21 && REG8(DL) <= 0x7e) {
                   8103:                REG16(DX) = pcbios_printer_jis2sjis(REG16(DX));
                   8104:        } else {
                   8105:                REG16(DX) = 0x0000;
                   8106:        }
                   8107: }
                   8108: 
                   8109: inline void pcbios_int_17h_52h()
                   8110: {
                   8111:        if(((REG8(DH) >= 0x81 && REG8(DH) <= 0x9f) || (REG8(DH) >= 0xe0 && REG8(DH) <= 0xfc)) && (REG8(DL) >= 0x40 && REG8(DL) <= 0xfc && REG8(DL) != 0x7f)) {
                   8112:                REG16(DX) = pcbios_printer_sjis2jis(REG16(DX));
                   8113:        } else {
                   8114:                REG16(DX) = 0x0000;
                   8115:        }
                   8116: }
                   8117: 
                   8118: inline void pcbios_int_17h_84h()
                   8119: {
                   8120:        if(REG16(DX) < 3) {
                   8121:                if(pio[REG16(DX)].jis_mode) {
                   8122:                        printer_out(REG16(DX), 0x1c);
                   8123:                        printer_out(REG16(DX), 0x2e);
                   8124:                        pio[REG16(DX)].jis_mode = false;
                   8125:                }
                   8126:                printer_out(REG16(DX), REG8(AL));
                   8127:                REG8(AH) = 0xd0;
                   8128:        }
                   8129: }
                   8130: 
                   8131: inline void pcbios_int_17h_85h()
                   8132: {
                   8133:        pio[0].conv_mode = (REG8(AL) == 0x00);
                   8134: }
                   8135: 
1.1       root     8136: inline void pcbios_int_1ah_00h()
                   8137: {
1.1.1.19  root     8138:        pcbios_update_daily_timer_counter(timeGetTime());
                   8139:        REG16(CX) = *(UINT16 *)(mem + 0x46e);
                   8140:        REG16(DX) = *(UINT16 *)(mem + 0x46c);
                   8141:        REG8(AL) = mem[0x470];
                   8142:        mem[0x470] = 0;
1.1       root     8143: }
                   8144: 
                   8145: inline int to_bcd(int t)
                   8146: {
                   8147:        int u = (t % 100) / 10;
                   8148:        return (u << 4) | (t % 10);
                   8149: }
                   8150: 
                   8151: inline void pcbios_int_1ah_02h()
                   8152: {
                   8153:        SYSTEMTIME time;
                   8154:        
                   8155:        GetLocalTime(&time);
                   8156:        REG8(CH) = to_bcd(time.wHour);
                   8157:        REG8(CL) = to_bcd(time.wMinute);
                   8158:        REG8(DH) = to_bcd(time.wSecond);
                   8159:        REG8(DL) = 0x00;
                   8160: }
                   8161: 
                   8162: inline void pcbios_int_1ah_04h()
                   8163: {
                   8164:        SYSTEMTIME time;
                   8165:        
                   8166:        GetLocalTime(&time);
                   8167:        REG8(CH) = to_bcd(time.wYear / 100);
                   8168:        REG8(CL) = to_bcd(time.wYear);
                   8169:        REG8(DH) = to_bcd(time.wMonth);
                   8170:        REG8(DL) = to_bcd(time.wDay);
                   8171: }
                   8172: 
                   8173: inline void pcbios_int_1ah_0ah()
                   8174: {
                   8175:        SYSTEMTIME time;
                   8176:        FILETIME file_time;
                   8177:        WORD dos_date, dos_time;
                   8178:        
                   8179:        GetLocalTime(&time);
                   8180:        SystemTimeToFileTime(&time, &file_time);
                   8181:        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   8182:        REG16(CX) = dos_date;
                   8183: }
                   8184: 
                   8185: // msdos system call
                   8186: 
                   8187: inline void msdos_int_21h_00h()
                   8188: {
1.1.1.3   root     8189:        msdos_process_terminate(SREG(CS), retval, 1);
1.1       root     8190: }
                   8191: 
1.1.1.35  root     8192: DWORD WINAPI msdos_int_21h_01h_thread(LPVOID)
1.1       root     8193: {
                   8194:        REG8(AL) = msdos_getche();
1.1.1.33  root     8195:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     8196:        
1.1.1.35  root     8197: #ifdef USE_SERVICE_THREAD
                   8198:        service_exit = true;
                   8199: #endif
                   8200:        return(0);
                   8201: }
                   8202: 
                   8203: inline void msdos_int_21h_01h()
                   8204: {
                   8205: #ifdef USE_SERVICE_THREAD
                   8206:        start_service_loop(msdos_int_21h_01h_thread);
                   8207: #else
                   8208:        msdos_int_21h_01h_thread(NULL);
                   8209:        REQUEST_HARDWRE_UPDATE();
                   8210: #endif
1.1       root     8211: }
                   8212: 
                   8213: inline void msdos_int_21h_02h()
                   8214: {
1.1.1.33  root     8215:        UINT8 data = REG8(DL);
                   8216:        msdos_putch(data);
                   8217:        REG8(AL) = data;
                   8218:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     8219: }
                   8220: 
                   8221: inline void msdos_int_21h_03h()
                   8222: {
                   8223:        REG8(AL) = msdos_aux_in();
                   8224: }
                   8225: 
                   8226: inline void msdos_int_21h_04h()
                   8227: {
                   8228:        msdos_aux_out(REG8(DL));
                   8229: }
                   8230: 
                   8231: inline void msdos_int_21h_05h()
                   8232: {
                   8233:        msdos_prn_out(REG8(DL));
                   8234: }
                   8235: 
                   8236: inline void msdos_int_21h_06h()
                   8237: {
                   8238:        if(REG8(DL) == 0xff) {
                   8239:                if(msdos_kbhit()) {
                   8240:                        REG8(AL) = msdos_getch();
1.1.1.3   root     8241: #if defined(HAS_I386)
                   8242:                        m_ZF = 0;
                   8243: #else
                   8244:                        m_ZeroVal = 1;
                   8245: #endif
1.1       root     8246:                } else {
                   8247:                        REG8(AL) = 0;
1.1.1.3   root     8248: #if defined(HAS_I386)
                   8249:                        m_ZF = 1;
                   8250: #else
                   8251:                        m_ZeroVal = 0;
                   8252: #endif
1.1.1.14  root     8253:                        maybe_idle();
1.1       root     8254:                }
                   8255:        } else {
1.1.1.33  root     8256:                UINT8 data = REG8(DL);
                   8257:                msdos_putch(data);
                   8258:                REG8(AL) = data;
1.1       root     8259:        }
                   8260: }
                   8261: 
1.1.1.35  root     8262: DWORD WINAPI msdos_int_21h_07h_thread(LPVOID)
1.1       root     8263: {
                   8264:        REG8(AL) = msdos_getch();
1.1.1.26  root     8265:        
1.1.1.35  root     8266: #ifdef USE_SERVICE_THREAD
                   8267:        service_exit = true;
                   8268: #endif
                   8269:        return(0);
1.1       root     8270: }
                   8271: 
1.1.1.35  root     8272: inline void msdos_int_21h_07h()
                   8273: {
                   8274: #ifdef USE_SERVICE_THREAD
                   8275:        start_service_loop(msdos_int_21h_07h_thread);
                   8276: #else
                   8277:        msdos_int_21h_07h_thread(NULL);
                   8278:        REQUEST_HARDWRE_UPDATE();
                   8279: #endif
                   8280: }
                   8281: 
                   8282: DWORD WINAPI msdos_int_21h_08h_thread(LPVOID)
1.1       root     8283: {
                   8284:        REG8(AL) = msdos_getch();
1.1.1.33  root     8285:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     8286:        
1.1.1.35  root     8287: #ifdef USE_SERVICE_THREAD
                   8288:        service_exit = true;
                   8289: #endif
                   8290:        return(0);
                   8291: }
                   8292: 
                   8293: inline void msdos_int_21h_08h()
                   8294: {
                   8295: #ifdef USE_SERVICE_THREAD
                   8296:        start_service_loop(msdos_int_21h_08h_thread);
                   8297: #else
                   8298:        msdos_int_21h_08h_thread(NULL);
                   8299:        REQUEST_HARDWRE_UPDATE();
                   8300: #endif
1.1       root     8301: }
                   8302: 
                   8303: inline void msdos_int_21h_09h()
                   8304: {
1.1.1.21  root     8305:        msdos_stdio_reopen();
                   8306:        
1.1.1.20  root     8307:        process_t *process = msdos_process_info_get(current_psp);
                   8308:        int fd = msdos_psp_get_file_table(1, current_psp);
                   8309:        
1.1.1.14  root     8310:        char *str = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   8311:        int len = 0;
1.1       root     8312:        
1.1.1.14  root     8313:        while(str[len] != '$' && len < 0x10000) {
                   8314:                len++;
                   8315:        }
1.1.1.20  root     8316:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     8317:                // stdout is redirected to file
1.1.1.20  root     8318:                msdos_write(fd, str, len);
1.1       root     8319:        } else {
                   8320:                for(int i = 0; i < len; i++) {
1.1.1.14  root     8321:                        msdos_putch(str[i]);
1.1       root     8322:                }
                   8323:        }
1.1.1.33  root     8324:        REG8(AL) = '$';
                   8325:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     8326: }
                   8327: 
1.1.1.35  root     8328: DWORD WINAPI msdos_int_21h_0ah_thread(LPVOID)
1.1       root     8329: {
1.1.1.3   root     8330:        int ofs = SREG_BASE(DS) + REG16(DX);
1.1       root     8331:        int max = mem[ofs] - 1;
                   8332:        UINT8 *buf = mem + ofs + 2;
                   8333:        int chr, p = 0;
                   8334:        
                   8335:        while((chr = msdos_getch()) != 0x0d) {
1.1.1.33  root     8336:                if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
1.1.1.26  root     8337:                        p = 0;
1.1.1.33  root     8338:                        msdos_putch(0x03);
                   8339:                        msdos_putch(0x0d);
                   8340:                        msdos_putch(0x0a);
1.1.1.26  root     8341:                        break;
1.1.1.33  root     8342:                } else if(ctrl_break_pressed) {
                   8343:                        // skip this byte
1.1.1.26  root     8344:                } else if(chr == 0x00) {
1.1       root     8345:                        // skip 2nd byte
                   8346:                        msdos_getch();
                   8347:                } else if(chr == 0x08) {
                   8348:                        // back space
                   8349:                        if(p > 0) {
                   8350:                                p--;
1.1.1.20  root     8351:                                if(msdos_ctrl_code_check(buf[p])) {
1.1.1.34  root     8352:                                        msdos_putch(0x08);
                   8353:                                        msdos_putch(0x08);
                   8354:                                        msdos_putch(0x20);
                   8355:                                        msdos_putch(0x20);
                   8356:                                        msdos_putch(0x08);
                   8357:                                        msdos_putch(0x08);
1.1.1.36  root     8358:                                } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
                   8359:                                        p--;
                   8360:                                        msdos_putch(0x08);
                   8361:                                        msdos_putch(0x08);
                   8362:                                        msdos_putch(0x20);
                   8363:                                        msdos_putch(0x20);
                   8364:                                        msdos_putch(0x08);
                   8365:                                        msdos_putch(0x08);
1.1.1.34  root     8366:                                } else {
                   8367:                                        msdos_putch(0x08);
                   8368:                                        msdos_putch(0x20);
                   8369:                                        msdos_putch(0x08);
                   8370:                                }
                   8371:                        }
                   8372:                } else if(chr == 0x1b) {
                   8373:                        // escape
                   8374:                        while(p > 0) {
                   8375:                                p--;
                   8376:                                if(msdos_ctrl_code_check(buf[p])) {
                   8377:                                        msdos_putch(0x08);
                   8378:                                        msdos_putch(0x08);
                   8379:                                        msdos_putch(0x20);
                   8380:                                        msdos_putch(0x20);
                   8381:                                        msdos_putch(0x08);
                   8382:                                        msdos_putch(0x08);
1.1.1.20  root     8383:                                } else {
1.1.1.34  root     8384:                                        msdos_putch(0x08);
                   8385:                                        msdos_putch(0x20);
                   8386:                                        msdos_putch(0x08);
1.1.1.20  root     8387:                                }
1.1       root     8388:                        }
                   8389:                } else if(p < max) {
                   8390:                        buf[p++] = chr;
                   8391:                        msdos_putch(chr);
                   8392:                }
                   8393:        }
                   8394:        buf[p] = 0x0d;
                   8395:        mem[ofs + 1] = p;
1.1.1.33  root     8396:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     8397:        
1.1.1.35  root     8398: #ifdef USE_SERVICE_THREAD
                   8399:        service_exit = true;
                   8400: #endif
                   8401:        return(0);
                   8402: }
                   8403: 
                   8404: inline void msdos_int_21h_0ah()
                   8405: {
                   8406:        if(mem[SREG_BASE(DS) + REG16(DX)] != 0x00) {
                   8407: #ifdef USE_SERVICE_THREAD
                   8408:                start_service_loop(msdos_int_21h_0ah_thread);
                   8409: #else
                   8410:                msdos_int_21h_0ah_thread(NULL);
                   8411:                REQUEST_HARDWRE_UPDATE();
                   8412: #endif
                   8413:        }
1.1       root     8414: }
                   8415: 
                   8416: inline void msdos_int_21h_0bh()
                   8417: {
                   8418:        if(msdos_kbhit()) {
                   8419:                REG8(AL) = 0xff;
                   8420:        } else {
                   8421:                REG8(AL) = 0x00;
1.1.1.14  root     8422:                maybe_idle();
1.1       root     8423:        }
1.1.1.33  root     8424:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     8425: }
                   8426: 
                   8427: inline void msdos_int_21h_0ch()
                   8428: {
                   8429:        // clear key buffer
1.1.1.21  root     8430:        msdos_stdio_reopen();
                   8431:        
1.1.1.20  root     8432:        process_t *process = msdos_process_info_get(current_psp);
                   8433:        int fd = msdos_psp_get_file_table(0, current_psp);
                   8434:        
                   8435:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     8436:                // stdin is redirected to file
                   8437:        } else {
                   8438:                while(msdos_kbhit()) {
                   8439:                        msdos_getch();
                   8440:                }
                   8441:        }
                   8442:        
                   8443:        switch(REG8(AL)) {
                   8444:        case 0x01:
                   8445:                msdos_int_21h_01h();
                   8446:                break;
                   8447:        case 0x06:
                   8448:                msdos_int_21h_06h();
                   8449:                break;
                   8450:        case 0x07:
                   8451:                msdos_int_21h_07h();
                   8452:                break;
                   8453:        case 0x08:
                   8454:                msdos_int_21h_08h();
                   8455:                break;
                   8456:        case 0x0a:
                   8457:                msdos_int_21h_0ah();
                   8458:                break;
                   8459:        default:
1.1.1.22  root     8460: //             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));
                   8461: //             REG16(AX) = 0x01;
                   8462: //             m_CF = 1;
1.1       root     8463:                break;
                   8464:        }
                   8465: }
                   8466: 
                   8467: inline void msdos_int_21h_0dh()
                   8468: {
                   8469: }
                   8470: 
                   8471: inline void msdos_int_21h_0eh()
                   8472: {
                   8473:        if(REG8(DL) < 26) {
                   8474:                _chdrive(REG8(DL) + 1);
                   8475:                msdos_cds_update(REG8(DL));
1.1.1.23  root     8476:                msdos_sda_update(current_psp);
1.1       root     8477:        }
                   8478:        REG8(AL) = 26; // zdrive
                   8479: }
                   8480: 
1.1.1.14  root     8481: inline void msdos_int_21h_0fh()
                   8482: {
                   8483:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8484:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   8485:        char *path = msdos_fcb_path(fcb);
                   8486:        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     8487:        
1.1.1.14  root     8488:        if(hFile == INVALID_HANDLE_VALUE) {
                   8489:                REG8(AL) = 0xff;
                   8490:        } else {
                   8491:                REG8(AL) = 0;
                   8492:                fcb->current_block = 0;
                   8493:                fcb->record_size = 128;
                   8494:                fcb->file_size = GetFileSize(hFile, NULL);
                   8495:                fcb->handle = hFile;
                   8496:                fcb->cur_record = 0;
                   8497:        }
                   8498: }
                   8499: 
                   8500: inline void msdos_int_21h_10h()
                   8501: {
                   8502:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8503:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   8504:        
                   8505:        REG8(AL) = CloseHandle(fcb->handle) ? 0 : 0xff;
                   8506: }
                   8507: 
1.1       root     8508: inline void msdos_int_21h_11h()
                   8509: {
1.1.1.3   root     8510:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8511:        fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     8512:        
                   8513:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     8514:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   8515:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   8516:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     8517:        char *path = msdos_fcb_path(fcb);
                   8518:        WIN32_FIND_DATA fd;
                   8519:        
1.1.1.13  root     8520:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   8521:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   8522:                FindClose(dtainfo->find_handle);
                   8523:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     8524:        }
                   8525:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     8526:        dtainfo->allowable_mask = (ext_fcb->flag == 0xff) ? ext_fcb->attribute : 0x20;
                   8527:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     8528:        
1.1.1.14  root     8529:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   8530:                dtainfo->allowable_mask &= ~8;
1.1       root     8531:        }
1.1.1.14  root     8532:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   8533:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     8534:                      !msdos_find_file_has_8dot3name(&fd)) {
                   8535:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   8536:                                FindClose(dtainfo->find_handle);
                   8537:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     8538:                                break;
                   8539:                        }
                   8540:                }
                   8541:        }
1.1.1.13  root     8542:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     8543:                if(ext_fcb->flag == 0xff) {
                   8544:                        ext_find->flag = 0xff;
                   8545:                        memset(ext_find->reserved, 0, 5);
                   8546:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   8547:                }
                   8548:                find->drive = _getdrive();
1.1.1.13  root     8549:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     8550:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   8551:                find->nt_res = 0;
                   8552:                msdos_find_file_conv_local_time(&fd);
                   8553:                find->create_time_ms = 0;
                   8554:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   8555:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   8556:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   8557:                find->cluster_hi = find->cluster_lo = 0;
                   8558:                find->file_size = fd.nFileSizeLow;
                   8559:                REG8(AL) = 0x00;
1.1.1.14  root     8560:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     8561:                if(ext_fcb->flag == 0xff) {
                   8562:                        ext_find->flag = 0xff;
                   8563:                        memset(ext_find->reserved, 0, 5);
                   8564:                        ext_find->attribute = 8;
                   8565:                }
                   8566:                find->drive = _getdrive();
                   8567:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   8568:                find->attribute = 8;
                   8569:                find->nt_res = 0;
                   8570:                msdos_find_file_conv_local_time(&fd);
                   8571:                find->create_time_ms = 0;
                   8572:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   8573:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   8574:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   8575:                find->cluster_hi = find->cluster_lo = 0;
                   8576:                find->file_size = 0;
1.1.1.14  root     8577:                dtainfo->allowable_mask &= ~8;
1.1       root     8578:                REG8(AL) = 0x00;
                   8579:        } else {
                   8580:                REG8(AL) = 0xff;
                   8581:        }
                   8582: }
                   8583: 
                   8584: inline void msdos_int_21h_12h()
                   8585: {
1.1.1.3   root     8586:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1.1.14  root     8587: //     fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     8588:        
                   8589:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     8590:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   8591:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   8592:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     8593:        WIN32_FIND_DATA fd;
                   8594:        
1.1.1.13  root     8595:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   8596:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   8597:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     8598:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     8599:                              !msdos_find_file_has_8dot3name(&fd)) {
                   8600:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   8601:                                        FindClose(dtainfo->find_handle);
                   8602:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     8603:                                        break;
                   8604:                                }
                   8605:                        }
                   8606:                } else {
1.1.1.13  root     8607:                        FindClose(dtainfo->find_handle);
                   8608:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     8609:                }
                   8610:        }
1.1.1.13  root     8611:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     8612:                if(ext_fcb->flag == 0xff) {
                   8613:                        ext_find->flag = 0xff;
                   8614:                        memset(ext_find->reserved, 0, 5);
                   8615:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   8616:                }
                   8617:                find->drive = _getdrive();
1.1.1.13  root     8618:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     8619:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   8620:                find->nt_res = 0;
                   8621:                msdos_find_file_conv_local_time(&fd);
                   8622:                find->create_time_ms = 0;
                   8623:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   8624:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   8625:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   8626:                find->cluster_hi = find->cluster_lo = 0;
                   8627:                find->file_size = fd.nFileSizeLow;
                   8628:                REG8(AL) = 0x00;
1.1.1.14  root     8629:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     8630:                if(ext_fcb->flag == 0xff) {
                   8631:                        ext_find->flag = 0xff;
                   8632:                        memset(ext_find->reserved, 0, 5);
                   8633:                        ext_find->attribute = 8;
                   8634:                }
                   8635:                find->drive = _getdrive();
                   8636:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   8637:                find->attribute = 8;
                   8638:                find->nt_res = 0;
                   8639:                msdos_find_file_conv_local_time(&fd);
                   8640:                find->create_time_ms = 0;
                   8641:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   8642:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   8643:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   8644:                find->cluster_hi = find->cluster_lo = 0;
                   8645:                find->file_size = 0;
1.1.1.14  root     8646:                dtainfo->allowable_mask &= ~8;
1.1       root     8647:                REG8(AL) = 0x00;
                   8648:        } else {
                   8649:                REG8(AL) = 0xff;
                   8650:        }
                   8651: }
                   8652: 
                   8653: inline void msdos_int_21h_13h()
                   8654: {
1.1.1.3   root     8655:        if(remove(msdos_fcb_path((fcb_t *)(mem + SREG_BASE(DS) + REG16(DX))))) {
1.1       root     8656:                REG8(AL) = 0xff;
                   8657:        } else {
                   8658:                REG8(AL) = 0x00;
                   8659:        }
                   8660: }
                   8661: 
1.1.1.16  root     8662: inline void msdos_int_21h_14h()
                   8663: {
                   8664:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8665:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   8666:        process_t *process = msdos_process_info_get(current_psp);
                   8667:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   8668:        DWORD num = 0;
                   8669:        
                   8670:        memset(mem + dta_laddr, 0, fcb->record_size);
                   8671:        if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   8672:                REG8(AL) = 1;
                   8673:        } else {
                   8674:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   8675:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   8676:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   8677:                REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   8678:        }
                   8679: }
                   8680: 
                   8681: inline void msdos_int_21h_15h()
1.1.1.14  root     8682: {
                   8683:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8684:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.16  root     8685:        process_t *process = msdos_process_info_get(current_psp);
                   8686:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   8687:        DWORD num = 0;
1.1.1.14  root     8688:        
1.1.1.16  root     8689:        if(!WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   8690:                REG8(AL) = 1;
                   8691:        } else {
                   8692:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   8693:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   8694:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   8695:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   8696:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   8697:        }
                   8698: }
                   8699: 
                   8700: inline void msdos_int_21h_16h()
                   8701: {
                   8702:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8703:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.14  root     8704:        char *path = msdos_fcb_path(fcb);
                   8705:        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     8706:        
1.1.1.14  root     8707:        if(hFile == INVALID_HANDLE_VALUE) {
                   8708:                REG8(AL) = 0xff;
                   8709:        } else {
                   8710:                REG8(AL) = 0;
                   8711:                fcb->current_block = 0;
                   8712:                fcb->record_size = 128;
                   8713:                fcb->file_size = 0;
                   8714:                fcb->handle = hFile;
                   8715:                fcb->cur_record = 0;
                   8716:        }
                   8717: }
                   8718: 
1.1.1.16  root     8719: inline void msdos_int_21h_17h()
                   8720: {
                   8721:        ext_fcb_t *ext_fcb_src = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8722:        fcb_t *fcb_src = (fcb_t *)(ext_fcb_src + (ext_fcb_src->flag == 0xff ? 1 : 0));
                   8723:        char *path_src = msdos_fcb_path(fcb_src);
                   8724:        ext_fcb_t *ext_fcb_dst = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + 16);
                   8725:        fcb_t *fcb_dst = (fcb_t *)(ext_fcb_dst + (ext_fcb_dst->flag == 0xff ? 1 : 0));
                   8726:        char *path_dst = msdos_fcb_path(fcb_dst);
                   8727:        
                   8728:        if(rename(path_src, path_dst)) {
                   8729:                REG8(AL) = 0xff;
                   8730:        } else {
                   8731:                REG8(AL) = 0;
                   8732:        }
                   8733: }
                   8734: 
1.1       root     8735: inline void msdos_int_21h_18h()
                   8736: {
                   8737:        REG8(AL) = 0x00;
                   8738: }
                   8739: 
                   8740: inline void msdos_int_21h_19h()
                   8741: {
                   8742:        REG8(AL) = _getdrive() - 1;
                   8743: }
                   8744: 
                   8745: inline void msdos_int_21h_1ah()
                   8746: {
                   8747:        process_t *process = msdos_process_info_get(current_psp);
                   8748:        
                   8749:        process->dta.w.l = REG16(DX);
1.1.1.3   root     8750:        process->dta.w.h = SREG(DS);
1.1.1.23  root     8751:        msdos_sda_update(current_psp);
1.1       root     8752: }
                   8753: 
                   8754: inline void msdos_int_21h_1bh()
                   8755: {
                   8756:        int drive_num = _getdrive() - 1;
                   8757:        UINT16 seg, ofs;
                   8758:        
                   8759:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   8760:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   8761:                REG8(AL) = dpb->highest_sector_num + 1;
                   8762:                REG16(CX) = dpb->bytes_per_sector;
                   8763:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     8764:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     8765:        } else {
                   8766:                REG8(AL) = 0xff;
1.1.1.3   root     8767:                m_CF = 1;
1.1       root     8768:        }
                   8769: 
                   8770: }
                   8771: 
                   8772: inline void msdos_int_21h_1ch()
                   8773: {
                   8774:        int drive_num = REG8(DL) ? (REG8(DL) - 1) : (_getdrive() - 1);
                   8775:        UINT16 seg, ofs;
                   8776:        
                   8777:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   8778:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   8779:                REG8(AL) = dpb->highest_sector_num + 1;
                   8780:                REG16(CX) = dpb->bytes_per_sector;
                   8781:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     8782:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     8783:        } else {
                   8784:                REG8(AL) = 0xff;
1.1.1.3   root     8785:                m_CF = 1;
1.1       root     8786:        }
                   8787: 
                   8788: }
                   8789: 
                   8790: inline void msdos_int_21h_1dh()
                   8791: {
                   8792:        REG8(AL) = 0;
                   8793: }
                   8794: 
                   8795: inline void msdos_int_21h_1eh()
                   8796: {
                   8797:        REG8(AL) = 0;
                   8798: }
                   8799: 
                   8800: inline void msdos_int_21h_1fh()
                   8801: {
                   8802:        int drive_num = _getdrive() - 1;
                   8803:        UINT16 seg, ofs;
                   8804:        
                   8805:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   8806:                REG8(AL) = 0;
1.1.1.3   root     8807:                SREG(DS) = seg;
                   8808:                i386_load_segment_descriptor(DS);
1.1       root     8809:                REG16(BX) = ofs;
                   8810:        } else {
                   8811:                REG8(AL) = 0xff;
1.1.1.3   root     8812:                m_CF = 1;
1.1       root     8813:        }
                   8814: }
                   8815: 
                   8816: inline void msdos_int_21h_20h()
                   8817: {
                   8818:        REG8(AL) = 0;
                   8819: }
                   8820: 
1.1.1.14  root     8821: inline void msdos_int_21h_21h()
                   8822: {
                   8823:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8824:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   8825:        
                   8826:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   8827:                REG8(AL) = 1;
                   8828:        } else {
                   8829:                process_t *process = msdos_process_info_get(current_psp);
                   8830:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   8831:                memset(mem + dta_laddr, 0, fcb->record_size);
1.1.1.16  root     8832:                DWORD num = 0;
1.1.1.14  root     8833:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   8834:                        REG8(AL) = 1;
                   8835:                } else {
                   8836:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   8837:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     8838:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
1.1.1.14  root     8839:                }
                   8840:        }
                   8841: }
                   8842: 
                   8843: inline void msdos_int_21h_22h()
                   8844: {
                   8845:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8846:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   8847:        
                   8848:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   8849:                REG8(AL) = 0xff;
                   8850:        } else {
                   8851:                process_t *process = msdos_process_info_get(current_psp);
                   8852:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
1.1.1.16  root     8853:                DWORD num = 0;
1.1.1.14  root     8854:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL);
                   8855:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   8856:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   8857:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     8858:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
1.1.1.14  root     8859:        }
                   8860: }
                   8861: 
1.1.1.16  root     8862: inline void msdos_int_21h_23h()
                   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:        char *path = msdos_fcb_path(fcb);
                   8867:        HANDLE hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   8868:        
                   8869:        if(hFile == INVALID_HANDLE_VALUE) {
                   8870:                REG8(AL) = 0xff;
                   8871:        } else {
                   8872:                UINT32 size = GetFileSize(hFile, NULL);
                   8873:                fcb->rand_record = size / fcb->record_size + ((size % fcb->record_size) != 0);
                   8874:                REG8(AL) = 0;
                   8875:        }
                   8876: }
                   8877: 
                   8878: inline void msdos_int_21h_24h()
                   8879: {
                   8880:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8881:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   8882:        
                   8883:        fcb->rand_record = fcb->current_block * fcb->record_size + fcb->cur_record;
                   8884: }
                   8885: 
1.1       root     8886: inline void msdos_int_21h_25h()
                   8887: {
                   8888:        *(UINT16 *)(mem + 4 * REG8(AL) + 0) = REG16(DX);
1.1.1.3   root     8889:        *(UINT16 *)(mem + 4 * REG8(AL) + 2) = SREG(DS);
1.1       root     8890: }
                   8891: 
                   8892: inline void msdos_int_21h_26h()
                   8893: {
                   8894:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   8895:        
                   8896:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
                   8897:        psp->first_mcb = REG16(DX) + 16;
                   8898:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   8899:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   8900:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   8901:        psp->parent_psp = 0;
                   8902: }
                   8903: 
1.1.1.16  root     8904: inline void msdos_int_21h_27h()
                   8905: {
                   8906:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8907:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   8908:        
                   8909:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   8910:                REG8(AL) = 1;
                   8911:        } else {
                   8912:                process_t *process = msdos_process_info_get(current_psp);
                   8913:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   8914:                memset(mem + dta_laddr, 0, fcb->record_size * REG16(CX));
                   8915:                DWORD num = 0;
                   8916:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL) || num == 0) {
                   8917:                        REG8(AL) = 1;
                   8918:                } else {
                   8919:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   8920:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   8921:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   8922:                        REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   8923:                }
                   8924:        }
                   8925: }
                   8926: 
                   8927: inline void msdos_int_21h_28h()
                   8928: {
                   8929:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8930:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   8931:        
                   8932:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   8933:                REG8(AL) = 0xff;
                   8934:        } else {
                   8935:                process_t *process = msdos_process_info_get(current_psp);
                   8936:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   8937:                DWORD num = 0;
                   8938:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL);
                   8939:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   8940:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   8941:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   8942:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   8943:                REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   8944:        }
                   8945: }
                   8946: 
1.1       root     8947: inline void msdos_int_21h_29h()
                   8948: {
1.1.1.20  root     8949:        int ofs = 0;//SREG_BASE(DS) + REG16(SI);
                   8950:        char buffer[1024], name[MAX_PATH], ext[MAX_PATH];
1.1       root     8951:        UINT8 drv = 0;
                   8952:        char sep_chars[] = ":.;,=+";
                   8953:        char end_chars[] = "\\<>|/\"[]";
                   8954:        char spc_chars[] = " \t";
                   8955:        
1.1.1.20  root     8956:        memcpy(buffer, mem + SREG_BASE(DS) + REG16(SI), 1023);
                   8957:        buffer[1023] = 0;
                   8958:        memset(name, 0x20, sizeof(name));
                   8959:        memset(ext, 0x20, sizeof(ext));
                   8960:        
1.1       root     8961:        if(REG8(AL) & 1) {
1.1.1.20  root     8962:                ofs += strspn((char *)(buffer + ofs), spc_chars);
                   8963:                if(my_strchr(sep_chars, buffer[ofs]) && buffer[ofs] != '\0') {
1.1       root     8964:                        ofs++;
                   8965:                }
                   8966:        }
1.1.1.20  root     8967:        ofs += strspn((char *)(buffer + ofs), spc_chars);
1.1       root     8968:        
1.1.1.24  root     8969:        if(buffer[ofs + 1] == ':') {
                   8970:                if(buffer[ofs] >= 'a' && buffer[ofs] <= 'z') {
                   8971:                        drv = buffer[ofs] - 'a' + 1;
1.1.1.20  root     8972:                        ofs += 2;
1.1.1.24  root     8973:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   8974:                                ofs++;
                   8975:                        }
                   8976:                } else if(buffer[ofs] >= 'A' && buffer[ofs] <= 'Z') {
                   8977:                        drv = buffer[ofs] - 'A' + 1;
1.1       root     8978:                        ofs += 2;
1.1.1.24  root     8979:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   8980:                                ofs++;
                   8981:                        }
1.1       root     8982:                }
                   8983:        }
1.1.1.20  root     8984:        for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   8985:                UINT8 c = buffer[ofs];
                   8986:                if(is_kanji) {
                   8987:                        is_kanji = 0;
                   8988:                } else if(msdos_lead_byte_check(c)) {
                   8989:                        is_kanji = 1;
                   8990:                } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     8991:                        break;
                   8992:                } else if(c >= 'a' && c <= 'z') {
                   8993:                        c -= 0x20;
                   8994:                }
                   8995:                ofs++;
                   8996:                name[i] = c;
                   8997:        }
1.1.1.20  root     8998:        if(buffer[ofs] == '.') {
1.1       root     8999:                ofs++;
1.1.1.20  root     9000:                for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   9001:                        UINT8 c = buffer[ofs];
                   9002:                        if(is_kanji) {
                   9003:                                is_kanji = 0;
                   9004:                        } else if(msdos_lead_byte_check(c)) {
                   9005:                                is_kanji = 1;
                   9006:                        } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     9007:                                break;
                   9008:                        } else if(c >= 'a' && c <= 'z') {
                   9009:                                c -= 0x20;
                   9010:                        }
                   9011:                        ofs++;
                   9012:                        ext[i] = c;
                   9013:                }
                   9014:        }
1.1.1.20  root     9015:        int si = REG16(SI) + ofs;
1.1.1.3   root     9016:        int ds = SREG(DS);
1.1       root     9017:        while(si > 0xffff) {
                   9018:                si -= 0x10;
                   9019:                ds++;
                   9020:        }
                   9021:        REG16(SI) = si;
1.1.1.3   root     9022:        SREG(DS) = ds;
                   9023:        i386_load_segment_descriptor(DS);
1.1       root     9024:        
1.1.1.3   root     9025:        UINT8 *fcb = mem + SREG_BASE(ES) + REG16(DI);
1.1.1.20  root     9026:        if(!(REG8(AL) & 2) || drv != 0) {
                   9027:                fcb[0] = drv;
                   9028:        }
                   9029:        if(!(REG8(AL) & 4) || name[0] != 0x20) {
                   9030:                memcpy(fcb + 1, name, 8);
                   9031:        }
                   9032:        if(!(REG8(AL) & 8) || ext[0] != 0x20) {
                   9033:                memcpy(fcb + 9, ext, 3);
                   9034:        }
                   9035:        for(int i = 1, found_star = 0; i < 1 + 8; i++) {
1.1       root     9036:                if(fcb[i] == '*') {
                   9037:                        found_star = 1;
                   9038:                }
                   9039:                if(found_star) {
                   9040:                        fcb[i] = '?';
                   9041:                }
                   9042:        }
1.1.1.20  root     9043:        for(int i = 9, found_star = 0; i < 9 + 3; i++) {
1.1       root     9044:                if(fcb[i] == '*') {
                   9045:                        found_star = 1;
                   9046:                }
                   9047:                if(found_star) {
                   9048:                        fcb[i] = '?';
                   9049:                }
                   9050:        }
                   9051:        
                   9052:        if(drv == 0 || (drv > 0 && drv <= 26 && (GetLogicalDrives() & ( 1 << (drv - 1) )))) {
                   9053:                if(memchr(fcb + 1, '?', 8 + 3)) {
                   9054:                        REG8(AL) = 0x01;
1.1.1.20  root     9055:                } else {
                   9056:                        REG8(AL) = 0x00;
1.1       root     9057:                }
                   9058:        } else {
                   9059:                REG8(AL) = 0xff;
                   9060:        }
                   9061: }
                   9062: 
                   9063: inline void msdos_int_21h_2ah()
                   9064: {
                   9065:        SYSTEMTIME sTime;
                   9066:        
                   9067:        GetLocalTime(&sTime);
                   9068:        REG16(CX) = sTime.wYear;
                   9069:        REG8(DH) = (UINT8)sTime.wMonth;
                   9070:        REG8(DL) = (UINT8)sTime.wDay;
                   9071:        REG8(AL) = (UINT8)sTime.wDayOfWeek;
                   9072: }
                   9073: 
                   9074: inline void msdos_int_21h_2bh()
                   9075: {
1.1.1.14  root     9076:        REG8(AL) = 0xff;
1.1       root     9077: }
                   9078: 
                   9079: inline void msdos_int_21h_2ch()
                   9080: {
                   9081:        SYSTEMTIME sTime;
                   9082:        
                   9083:        GetLocalTime(&sTime);
                   9084:        REG8(CH) = (UINT8)sTime.wHour;
                   9085:        REG8(CL) = (UINT8)sTime.wMinute;
                   9086:        REG8(DH) = (UINT8)sTime.wSecond;
1.1.1.14  root     9087:        REG8(DL) = (UINT8)sTime.wMilliseconds / 10;
1.1       root     9088: }
                   9089: 
                   9090: inline void msdos_int_21h_2dh()
                   9091: {
                   9092:        REG8(AL) = 0x00;
                   9093: }
                   9094: 
                   9095: inline void msdos_int_21h_2eh()
                   9096: {
                   9097:        process_t *process = msdos_process_info_get(current_psp);
                   9098:        
                   9099:        process->verify = REG8(AL);
                   9100: }
                   9101: 
                   9102: inline void msdos_int_21h_2fh()
                   9103: {
                   9104:        process_t *process = msdos_process_info_get(current_psp);
                   9105:        
                   9106:        REG16(BX) = process->dta.w.l;
1.1.1.3   root     9107:        SREG(ES) = process->dta.w.h;
                   9108:        i386_load_segment_descriptor(ES);
1.1       root     9109: }
                   9110: 
                   9111: inline void msdos_int_21h_30h()
                   9112: {
                   9113:        // Version Flag / OEM
1.1.1.27  root     9114:        if(REG8(AL) == 0x01) {
1.1.1.29  root     9115: #ifdef SUPPORT_HMA
                   9116:                REG16(BX) = 0x0000;
                   9117: #else
                   9118:                REG16(BX) = 0x1000; // DOS is in HMA
                   9119: #endif
1.1       root     9120:        } else {
1.1.1.27  root     9121:                // NOTE: EXDEB invites BL shows the machine type (0=PC-98, 1=PC/AT, 2=FMR),
                   9122:                // but this is not correct on Windows 98 SE
                   9123: //             REG16(BX) = 0xff01;     // OEM = Microsoft, PC/AT
                   9124:                REG16(BX) = 0xff00;     // OEM = Microsoft
1.1       root     9125:        }
1.1.1.27  root     9126:        REG16(CX) = 0x0000;
1.1.1.30  root     9127:        REG8(AL) = dos_major_version;   // 7
                   9128:        REG8(AH) = dos_minor_version;   // 10
1.1       root     9129: }
                   9130: 
                   9131: inline void msdos_int_21h_31h()
                   9132: {
1.1.1.29  root     9133:        try {
                   9134:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   9135:        } catch(...) {
                   9136:                // recover the broken mcb
                   9137:                int mcb_seg = current_psp - 1;
                   9138:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     9139:                
1.1.1.29  root     9140:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     9141:                        mcb->mz = 'M';
                   9142:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   9143:                        
1.1.1.29  root     9144:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39! root     9145:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29  root     9146:                        } else {
1.1.1.39! root     9147:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29  root     9148:                        }
                   9149:                } else {
                   9150:                        mcb->mz = 'Z';
1.1.1.30  root     9151:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     9152:                }
                   9153:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   9154:        }
1.1       root     9155:        msdos_process_terminate(current_psp, REG8(AL) | 0x300, 0);
                   9156: }
                   9157: 
                   9158: inline void msdos_int_21h_32h()
                   9159: {
                   9160:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   9161:        UINT16 seg, ofs;
                   9162:        
                   9163:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9164:                REG8(AL) = 0;
1.1.1.3   root     9165:                SREG(DS) = seg;
                   9166:                i386_load_segment_descriptor(DS);
1.1       root     9167:                REG16(BX) = ofs;
                   9168:        } else {
                   9169:                REG8(AL) = 0xff;
1.1.1.3   root     9170:                m_CF = 1;
1.1       root     9171:        }
                   9172: }
                   9173: 
                   9174: inline void msdos_int_21h_33h()
                   9175: {
                   9176:        char path[MAX_PATH];
                   9177:        
                   9178:        switch(REG8(AL)) {
                   9179:        case 0x00:
1.1.1.33  root     9180:                REG8(DL) = ctrl_break_checking;
1.1       root     9181:                break;
                   9182:        case 0x01:
1.1.1.33  root     9183:                ctrl_break_checking = REG8(DL);
                   9184:                break;
                   9185:        case 0x02:
                   9186:                {
                   9187:                        UINT8 old = ctrl_break_checking;
                   9188:                        ctrl_break_checking = REG8(DL);
                   9189:                        REG8(DL) = old;
                   9190:                }
                   9191:                break;
                   9192:        case 0x03:
                   9193:        case 0x04:
                   9194:                // DOS 4.0+ - Unused
1.1       root     9195:                break;
                   9196:        case 0x05:
                   9197:                GetSystemDirectory(path, MAX_PATH);
                   9198:                if(path[0] >= 'a' && path[0] <= 'z') {
                   9199:                        REG8(DL) = path[0] - 'a' + 1;
                   9200:                } else {
                   9201:                        REG8(DL) = path[0] - 'A' + 1;
                   9202:                }
                   9203:                break;
                   9204:        case 0x06:
1.1.1.2   root     9205:                // MS-DOS version (7.10)
1.1       root     9206:                REG8(BL) = 7;
1.1.1.2   root     9207:                REG8(BH) = 10;
1.1       root     9208:                REG8(DL) = 0;
1.1.1.29  root     9209: #ifdef SUPPORT_HMA
                   9210:                REG8(DH) = 0x00;
                   9211: #else
                   9212:                REG8(DH) = 0x10; // DOS is in HMA
                   9213: #endif
1.1       root     9214:                break;
1.1.1.6   root     9215:        case 0x07:
                   9216:                if(REG8(DL) == 0) {
                   9217:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag &= ~0x20;
                   9218:                } else if(REG8(DL) == 1) {
                   9219:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag |= 0x20;
                   9220:                }
                   9221:                break;
1.1       root     9222:        default:
1.1.1.22  root     9223:                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     9224:                REG16(AX) = 0x01;
1.1.1.3   root     9225:                m_CF = 1;
1.1       root     9226:                break;
                   9227:        }
                   9228: }
                   9229: 
1.1.1.23  root     9230: inline void msdos_int_21h_34h()
                   9231: {
                   9232:        SREG(ES) = SDA_TOP >> 4;
                   9233:        i386_load_segment_descriptor(ES);
                   9234:        REG16(BX) = offsetof(sda_t, indos_flag);;
                   9235: }
                   9236: 
1.1       root     9237: inline void msdos_int_21h_35h()
                   9238: {
                   9239:        REG16(BX) = *(UINT16 *)(mem + 4 * REG8(AL) + 0);
1.1.1.3   root     9240:        SREG(ES) = *(UINT16 *)(mem + 4 * REG8(AL) + 2);
                   9241:        i386_load_segment_descriptor(ES);
1.1       root     9242: }
                   9243: 
                   9244: inline void msdos_int_21h_36h()
                   9245: {
                   9246:        struct _diskfree_t df = {0};
                   9247:        
                   9248:        if(_getdiskfree(REG8(DL), &df) == 0) {
                   9249:                REG16(AX) = (UINT16)df.sectors_per_cluster;
                   9250:                REG16(CX) = (UINT16)df.bytes_per_sector;
1.1.1.13  root     9251:                REG16(BX) = df.avail_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.avail_clusters;
                   9252:                REG16(DX) = df.total_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.total_clusters;
1.1       root     9253:        } else {
                   9254:                REG16(AX) = 0xffff;
                   9255:        }
                   9256: }
                   9257: 
                   9258: inline void msdos_int_21h_37h()
                   9259: {
1.1.1.22  root     9260:        static UINT8 dev_flag = 0xff;
1.1       root     9261:        
                   9262:        switch(REG8(AL)) {
                   9263:        case 0x00:
1.1.1.22  root     9264:                {
                   9265:                        process_t *process = msdos_process_info_get(current_psp);
                   9266:                        REG8(AL) = 0x00;
                   9267:                        REG8(DL) = process->switchar;
                   9268:                }
1.1       root     9269:                break;
                   9270:        case 0x01:
1.1.1.22  root     9271:                {
                   9272:                        process_t *process = msdos_process_info_get(current_psp);
                   9273:                        REG8(AL) = 0x00;
                   9274:                        process->switchar = REG8(DL);
1.1.1.23  root     9275:                        msdos_sda_update(current_psp);
1.1.1.22  root     9276:                }
                   9277:                break;
                   9278:        case 0x02:
                   9279:                REG8(DL) = dev_flag;
                   9280:                break;
                   9281:        case 0x03:
                   9282:                dev_flag = REG8(DL);
                   9283:                break;
                   9284:        case 0xd0:
                   9285:        case 0xd1:
                   9286:        case 0xd2:
                   9287:        case 0xd3:
                   9288:        case 0xd4:
                   9289:        case 0xd5:
                   9290:        case 0xd6:
                   9291:        case 0xd7:
                   9292:        case 0xdc:
                   9293:        case 0xdd:
                   9294:        case 0xde:
                   9295:        case 0xdf:
                   9296:                // diet ???
                   9297:                REG16(AX) = 1;
1.1       root     9298:                break;
                   9299:        default:
1.1.1.22  root     9300:                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     9301:                REG16(AX) = 1;
                   9302:                break;
                   9303:        }
                   9304: }
                   9305: 
1.1.1.19  root     9306: int get_country_info(country_info_t *ci)
1.1.1.17  root     9307: {
                   9308:        char LCdata[80];
                   9309:        
1.1.1.19  root     9310:        ZeroMemory(ci, offsetof(country_info_t, reserved));
1.1.1.17  root     9311:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRDIGITS, LCdata, sizeof(LCdata));
                   9312:        ci->currency_dec_digits = atoi(LCdata);
                   9313:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRENCY, LCdata, sizeof(LCdata));
                   9314:        ci->currency_format = *LCdata - '0';
                   9315:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IDATE, LCdata, sizeof(LCdata));
                   9316:        ci->date_format = *LCdata - '0';
                   9317:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SCURRENCY, LCdata, sizeof(LCdata));
                   9318:        memcpy(&ci->currency_symbol, LCdata, 4);
                   9319:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDATE, LCdata, sizeof(LCdata));
                   9320:        *ci->date_sep = *LCdata;
                   9321:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, LCdata, sizeof(LCdata));
                   9322:        *ci->dec_sep = *LCdata;
                   9323:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SLIST, LCdata, sizeof(LCdata));
                   9324:        *ci->list_sep = *LCdata;
                   9325:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, LCdata, sizeof(LCdata));
                   9326:        *ci->thou_sep = *LCdata;
                   9327:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIME, LCdata, sizeof(LCdata));
                   9328:        *ci->time_sep = *LCdata;
                   9329:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, LCdata, sizeof(LCdata));
                   9330:        if(strchr(LCdata, 'H') != NULL) {
                   9331:                ci->time_format = 1;
                   9332:        }
1.1.1.27  root     9333:        ci->case_map.w.l = 0x000a; // dummy case map routine is at fffd:000a
1.1.1.24  root     9334:        ci->case_map.w.h = 0xfffd;
1.1.1.17  root     9335:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICOUNTRY, LCdata, sizeof(LCdata));
                   9336:        return atoi(LCdata);
                   9337: }
                   9338: 
1.1.1.14  root     9339: inline void msdos_int_21h_38h()
                   9340: {
                   9341:        switch(REG8(AL)) {
                   9342:        case 0x00:
1.1.1.19  root     9343:                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1.1.14  root     9344:                break;
                   9345:        default:
1.1.1.22  root     9346:                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     9347:                REG16(AX) = 2;
                   9348:                m_CF = 1;
                   9349:                break;
                   9350:        }
                   9351: }
                   9352: 
1.1       root     9353: inline void msdos_int_21h_39h(int lfn)
                   9354: {
1.1.1.3   root     9355:        if(_mkdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     9356:                REG16(AX) = errno;
1.1.1.3   root     9357:                m_CF = 1;
1.1       root     9358:        }
                   9359: }
                   9360: 
                   9361: inline void msdos_int_21h_3ah(int lfn)
                   9362: {
1.1.1.3   root     9363:        if(_rmdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     9364:                REG16(AX) = errno;
1.1.1.3   root     9365:                m_CF = 1;
1.1       root     9366:        }
                   9367: }
                   9368: 
                   9369: inline void msdos_int_21h_3bh(int lfn)
                   9370: {
1.1.1.3   root     9371:        if(_chdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1.1.17  root     9372:                REG16(AX) = 3;  // must be 3 (path not found)
1.1.1.3   root     9373:                m_CF = 1;
1.1       root     9374:        }
                   9375: }
                   9376: 
                   9377: inline void msdos_int_21h_3ch()
                   9378: {
1.1.1.3   root     9379:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     9380:        int attr = GetFileAttributes(path);
1.1.1.37  root     9381:        int fd = -1;
1.1.1.11  root     9382:        UINT16 info;
1.1.1.37  root     9383:        int sio_port = 0;
                   9384:        int lpt_port = 0;
1.1       root     9385:        
1.1.1.11  root     9386:        if(msdos_is_con_path(path)) {
                   9387:                fd = _open("CON", _O_WRONLY | _O_BINARY);
                   9388:                info = 0x80d3;
1.1.1.37  root     9389:        } else if((sio_port = msdos_is_comm_path(path)) != 0) {
1.1.1.38  root     9390:                fd = _open("NUL", _O_RDWR | _O_BINARY);
1.1.1.14  root     9391:                info = 0x80d3;
1.1.1.37  root     9392:                msdos_set_comm_params(sio_port, path);
                   9393:        } else if((lpt_port = msdos_is_prn_path(path)) != 0) {
                   9394:                fd = _open("NUL", _O_WRONLY | _O_BINARY);
                   9395:                info = 0xa8c0;
1.1.1.29  root     9396:        } else if(msdos_is_device_path(path)) {
1.1.1.20  root     9397:                fd = _open("NUL", _O_WRONLY | _O_BINARY);
                   9398:                info = 0x80d3;
1.1       root     9399:        } else {
                   9400:                fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
1.1.1.11  root     9401:                info = msdos_drive_number(path);
1.1       root     9402:        }
                   9403:        if(fd != -1) {
                   9404:                if(attr == -1) {
                   9405:                        attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   9406:                }
                   9407:                SetFileAttributes(path, attr);
                   9408:                REG16(AX) = fd;
1.1.1.37  root     9409:                msdos_file_handler_open(fd, path, _isatty(fd), 2, info, current_psp, sio_port, lpt_port);
1.1.1.20  root     9410:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     9411:        } else {
                   9412:                REG16(AX) = errno;
1.1.1.3   root     9413:                m_CF = 1;
1.1       root     9414:        }
                   9415: }
                   9416: 
                   9417: inline void msdos_int_21h_3dh()
                   9418: {
1.1.1.3   root     9419:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     9420:        int mode = REG8(AL) & 0x03;
1.1.1.37  root     9421:        int fd = -1;
1.1.1.11  root     9422:        UINT16 info;
1.1.1.37  root     9423:        int sio_port = 0;
                   9424:        int lpt_port = 0;
1.1       root     9425:        
                   9426:        if(mode < 0x03) {
1.1.1.11  root     9427:                if(msdos_is_con_path(path)) {
1.1.1.13  root     9428:                        fd = msdos_open("CON", file_mode[mode].mode);
1.1.1.11  root     9429:                        info = 0x80d3;
1.1.1.37  root     9430:                } else if((sio_port = msdos_is_comm_path(path)) != 0) {
                   9431:                        fd = msdos_open("NUL", file_mode[mode].mode);
1.1.1.14  root     9432:                        info = 0x80d3;
1.1.1.37  root     9433:                        msdos_set_comm_params(sio_port, path);
                   9434:                } else if((lpt_port = msdos_is_prn_path(path)) != 0) {
                   9435:                        fd = _open("NUL", file_mode[mode].mode);
                   9436:                        info = 0xa8c0;
1.1.1.29  root     9437:                } else if(msdos_is_device_path(path)) {
1.1.1.20  root     9438:                        fd = msdos_open("NUL", file_mode[mode].mode);
                   9439:                        info = 0x80d3;
1.1.1.11  root     9440:                } else {
1.1.1.13  root     9441:                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     9442:                        info = msdos_drive_number(path);
                   9443:                }
1.1       root     9444:                if(fd != -1) {
                   9445:                        REG16(AX) = fd;
1.1.1.37  root     9446:                        msdos_file_handler_open(fd, path, _isatty(fd), mode, info, current_psp, sio_port, lpt_port);
1.1.1.20  root     9447:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     9448:                } else {
                   9449:                        REG16(AX) = errno;
1.1.1.3   root     9450:                        m_CF = 1;
1.1       root     9451:                }
                   9452:        } else {
                   9453:                REG16(AX) = 0x0c;
1.1.1.3   root     9454:                m_CF = 1;
1.1       root     9455:        }
                   9456: }
                   9457: 
                   9458: inline void msdos_int_21h_3eh()
                   9459: {
                   9460:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     9461:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     9462:        
1.1.1.20  root     9463:        if(fd < process->max_files && file_handler[fd].valid) {
                   9464:                _close(fd);
                   9465:                msdos_file_handler_close(fd);
                   9466:                msdos_psp_set_file_table(REG16(BX), 0x0ff, current_psp);
1.1       root     9467:        } else {
                   9468:                REG16(AX) = 0x06;
1.1.1.3   root     9469:                m_CF = 1;
1.1       root     9470:        }
                   9471: }
                   9472: 
1.1.1.35  root     9473: DWORD WINAPI msdos_int_21h_3fh_thread(LPVOID)
                   9474: {
                   9475:        UINT8 *buf = mem + SREG_BASE(DS) + REG16(DX);
                   9476:        int max = REG16(CX);
                   9477:        int p = 0;
                   9478:        
                   9479:        while(max > p) {
                   9480:                int chr = msdos_getch();
                   9481:                
                   9482:                if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
                   9483:                        p = 0;
                   9484:                        buf[p++] = 0x0d;
                   9485:                        if(max > p) {
                   9486:                                buf[p++] = 0x0a;
                   9487:                        }
                   9488:                        msdos_putch(0x03);
                   9489:                        msdos_putch(0x0d);
                   9490:                        msdos_putch(0x0a);
                   9491:                        break;
                   9492:                } else if(ctrl_break_pressed) {
                   9493:                        // skip this byte
                   9494:                } else if(chr == 0x00) {
                   9495:                        // skip 2nd byte
                   9496:                        msdos_getch();
                   9497:                } else if(chr == 0x0d) {
                   9498:                        // carriage return
                   9499:                        buf[p++] = 0x0d;
                   9500:                        if(max > p) {
                   9501:                                buf[p++] = 0x0a;
                   9502:                        }
                   9503:                        msdos_putch('\n');
                   9504:                        break;
                   9505:                } else if(chr == 0x08) {
                   9506:                        // back space
                   9507:                        if(p > 0) {
                   9508:                                p--;
                   9509:                                if(msdos_ctrl_code_check(buf[p])) {
                   9510:                                        msdos_putch(0x08);
                   9511:                                        msdos_putch(0x08);
                   9512:                                        msdos_putch(0x20);
                   9513:                                        msdos_putch(0x20);
                   9514:                                        msdos_putch(0x08);
                   9515:                                        msdos_putch(0x08);
1.1.1.36  root     9516:                                } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
                   9517:                                        p--;
                   9518:                                        msdos_putch(0x08);
                   9519:                                        msdos_putch(0x08);
                   9520:                                        msdos_putch(0x20);
                   9521:                                        msdos_putch(0x20);
                   9522:                                        msdos_putch(0x08);
                   9523:                                        msdos_putch(0x08);
1.1.1.35  root     9524:                                } else {
                   9525:                                        msdos_putch(0x08);
                   9526:                                        msdos_putch(0x20);
                   9527:                                        msdos_putch(0x08);
                   9528:                                }
                   9529:                        }
                   9530:                } else if(chr == 0x1b) {
                   9531:                        // escape
                   9532:                        while(p > 0) {
                   9533:                                p--;
                   9534:                                if(msdos_ctrl_code_check(buf[p])) {
                   9535:                                        msdos_putch(0x08);
                   9536:                                        msdos_putch(0x08);
                   9537:                                        msdos_putch(0x20);
                   9538:                                        msdos_putch(0x20);
                   9539:                                        msdos_putch(0x08);
                   9540:                                        msdos_putch(0x08);
                   9541:                                } else {
                   9542:                                        msdos_putch(0x08);
                   9543:                                        msdos_putch(0x20);
                   9544:                                        msdos_putch(0x08);
                   9545:                                }
                   9546:                        }
                   9547:                } else {
                   9548:                        buf[p++] = chr;
                   9549:                        msdos_putch(chr);
                   9550:                }
                   9551:        }
                   9552:        REG16(AX) = p;
                   9553:        
                   9554: #ifdef USE_SERVICE_THREAD
                   9555:        service_exit = true;
                   9556: #endif
                   9557:        return(0);
                   9558: }
                   9559: 
1.1       root     9560: inline void msdos_int_21h_3fh()
                   9561: {
                   9562:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     9563:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     9564:        
1.1.1.20  root     9565:        if(fd < process->max_files && file_handler[fd].valid) {
                   9566:                if(file_mode[file_handler[fd].mode].in) {
                   9567:                        if(file_handler[fd].atty) {
1.1       root     9568:                                // BX is stdin or is redirected to stdin
1.1.1.35  root     9569:                                if(REG16(CX) != 0) {
                   9570: #ifdef USE_SERVICE_THREAD
                   9571:                                        start_service_loop(msdos_int_21h_3fh_thread);
                   9572: #else
                   9573:                                        msdos_int_21h_3fh_thread(NULL);
                   9574:                                        REQUEST_HARDWRE_UPDATE();
                   9575: #endif
                   9576:                                } else {
                   9577:                                        REG16(AX) = 0;
1.1       root     9578:                                }
                   9579:                        } else {
1.1.1.37  root     9580:                                REG16(AX) = msdos_read(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     9581:                        }
                   9582:                } else {
                   9583:                        REG16(AX) = 0x05;
1.1.1.3   root     9584:                        m_CF = 1;
1.1       root     9585:                }
                   9586:        } else {
                   9587:                REG16(AX) = 0x06;
1.1.1.3   root     9588:                m_CF = 1;
1.1       root     9589:        }
                   9590: }
                   9591: 
                   9592: inline void msdos_int_21h_40h()
                   9593: {
                   9594:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     9595:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     9596:        
1.1.1.20  root     9597:        if(fd < process->max_files && file_handler[fd].valid) {
                   9598:                if(file_mode[file_handler[fd].mode].out) {
1.1       root     9599:                        if(REG16(CX)) {
1.1.1.20  root     9600:                                if(file_handler[fd].atty) {
1.1       root     9601:                                        // BX is stdout/stderr or is redirected to stdout
                   9602:                                        for(int i = 0; i < REG16(CX); i++) {
1.1.1.3   root     9603:                                                msdos_putch(mem[SREG_BASE(DS) + REG16(DX) + i]);
1.1       root     9604:                                        }
                   9605:                                        REG16(AX) = REG16(CX);
                   9606:                                } else {
1.1.1.20  root     9607:                                        REG16(AX) = msdos_write(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     9608:                                }
                   9609:                        } else {
1.1.1.20  root     9610:                                UINT32 pos = _tell(fd);
                   9611:                                _lseek(fd, 0, SEEK_END);
                   9612:                                UINT32 size = _tell(fd);
1.1.1.12  root     9613:                                if(pos < size) {
1.1.1.20  root     9614:                                        _lseek(fd, pos, SEEK_SET);
                   9615:                                        SetEndOfFile((HANDLE)_get_osfhandle(fd));
1.1.1.12  root     9616:                                } else {
                   9617:                                        for(UINT32 i = size; i < pos; i++) {
                   9618:                                                UINT8 tmp = 0;
1.1.1.23  root     9619:                                                msdos_write(fd, &tmp, 1);
1.1.1.12  root     9620:                                        }
1.1.1.20  root     9621:                                        _lseek(fd, pos, SEEK_SET);
1.1       root     9622:                                }
1.1.1.23  root     9623:                                REG16(AX) = 0;
1.1       root     9624:                        }
                   9625:                } else {
                   9626:                        REG16(AX) = 0x05;
1.1.1.3   root     9627:                        m_CF = 1;
1.1       root     9628:                }
                   9629:        } else {
                   9630:                REG16(AX) = 0x06;
1.1.1.3   root     9631:                m_CF = 1;
1.1       root     9632:        }
                   9633: }
                   9634: 
                   9635: inline void msdos_int_21h_41h(int lfn)
                   9636: {
1.1.1.3   root     9637:        if(remove(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     9638:                REG16(AX) = errno;
1.1.1.3   root     9639:                m_CF = 1;
1.1       root     9640:        }
                   9641: }
                   9642: 
                   9643: inline void msdos_int_21h_42h()
                   9644: {
                   9645:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     9646:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     9647:        
1.1.1.20  root     9648:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     9649:                if(REG8(AL) < 0x03) {
1.1.1.35  root     9650:                        static const int ptrname[] = { SEEK_SET, SEEK_CUR, SEEK_END };
1.1.1.20  root     9651:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), ptrname[REG8(AL)]);
                   9652:                        UINT32 pos = _tell(fd);
1.1       root     9653:                        REG16(AX) = pos & 0xffff;
                   9654:                        REG16(DX) = (pos >> 16);
                   9655:                } else {
                   9656:                        REG16(AX) = 0x01;
1.1.1.3   root     9657:                        m_CF = 1;
1.1       root     9658:                }
                   9659:        } else {
                   9660:                REG16(AX) = 0x06;
1.1.1.3   root     9661:                m_CF = 1;
1.1       root     9662:        }
                   9663: }
                   9664: 
                   9665: inline void msdos_int_21h_43h(int lfn)
                   9666: {
1.1.1.3   root     9667:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1       root     9668:        int attr;
                   9669:        
1.1.1.14  root     9670:        if(!lfn && REG8(AL) > 2) {
                   9671:                REG16(AX) = 0x01;
                   9672:                m_CF = 1;
                   9673:                return;
                   9674:        }
                   9675:        switch(REG8(lfn ? BL : AL)) {
1.1       root     9676:        case 0x00:
                   9677:                if((attr = GetFileAttributes(path)) != -1) {
1.1.1.14  root     9678:                        REG16(CX) = (UINT16)msdos_file_attribute_create((UINT16)attr);
                   9679:                } else {
                   9680:                        REG16(AX) = (UINT16)GetLastError();
                   9681:                        m_CF = 1;
                   9682:                }
                   9683:                break;
                   9684:        case 0x01:
                   9685:                if(!SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)))) {
                   9686:                        REG16(AX) = (UINT16)GetLastError();
                   9687:                        m_CF = 1;
                   9688:                }
                   9689:                break;
                   9690:        case 0x02:
                   9691:                {
                   9692:                        DWORD size = GetCompressedFileSize(path, NULL);
                   9693:                        if(size != INVALID_FILE_SIZE) {
                   9694:                                if(size != 0 && size == GetFileSize(path, NULL)) {
                   9695:                                        DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   9696:                                        // this isn't correct if the file is in the NTFS MFT
                   9697:                                        if(GetDiskFreeSpace(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
                   9698:                                                size = ((size - 1) | (sectors_per_cluster * bytes_per_sector - 1)) + 1;
                   9699:                                        }
                   9700:                                }
                   9701:                                REG16(AX) = LOWORD(size);
                   9702:                                REG16(DX) = HIWORD(size);
                   9703:                        } else {
                   9704:                                REG16(AX) = (UINT16)GetLastError();
                   9705:                                m_CF = 1;
1.1       root     9706:                        }
1.1.1.14  root     9707:                }
                   9708:                break;
                   9709:        case 0x03:
                   9710:        case 0x05:
                   9711:        case 0x07:
                   9712:                {
                   9713:                        HANDLE hFile = CreateFile(path, FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   9714:                        if(hFile != INVALID_HANDLE_VALUE) {
                   9715:                                FILETIME local, time;
                   9716:                                DosDateTimeToFileTime(REG16(DI), /*REG8(BL) == 5 ? 0 : */REG16(CX), &local);
                   9717:                                if(REG8(BL) == 7) {
                   9718:                                        ULARGE_INTEGER hund;
                   9719:                                        hund.LowPart = local.dwLowDateTime;
                   9720:                                        hund.HighPart = local.dwHighDateTime;
                   9721:                                        hund.QuadPart += REG16(SI) * 100000;
                   9722:                                        local.dwLowDateTime = hund.LowPart;
                   9723:                                        local.dwHighDateTime = hund.HighPart;
                   9724:                                }
                   9725:                                LocalFileTimeToFileTime(&local, &time);
                   9726:                                if(!SetFileTime(hFile, REG8(BL) == 0x07 ? &time : NULL,
                   9727:                                                       REG8(BL) == 0x05 ? &time : NULL,
                   9728:                                                       REG8(BL) == 0x03 ? &time : NULL)) {
                   9729:                                        REG16(AX) = (UINT16)GetLastError();
                   9730:                                        m_CF = 1;
                   9731:                                }
                   9732:                                CloseHandle(hFile);
                   9733:                        } else {
                   9734:                                REG16(AX) = (UINT16)GetLastError();
                   9735:                                m_CF = 1;
1.1       root     9736:                        }
1.1.1.14  root     9737:                }
                   9738:                break;
                   9739:        case 0x04:
                   9740:        case 0x06:
                   9741:        case 0x08:
                   9742:                {
                   9743:                        WIN32_FILE_ATTRIBUTE_DATA fad;
                   9744:                        if(GetFileAttributesEx(path, GetFileExInfoStandard, (LPVOID)&fad)) {
                   9745:                                FILETIME *time, local;
                   9746:                                time = REG8(BL) == 0x04 ? &fad.ftLastWriteTime :
                   9747:                                                   0x06 ? &fad.ftLastAccessTime :
                   9748:                                                          &fad.ftCreationTime;
                   9749:                                FileTimeToLocalFileTime(time, &local);
                   9750:                                FileTimeToDosDateTime(&local, &REG16(DI), &REG16(CX));
                   9751:                                if(REG8(BL) == 0x08) {
                   9752:                                        ULARGE_INTEGER hund;
                   9753:                                        hund.LowPart = local.dwLowDateTime;
                   9754:                                        hund.HighPart = local.dwHighDateTime;
                   9755:                                        hund.QuadPart /= 100000;
                   9756:                                        REG16(SI) = (UINT16)(hund.QuadPart % 200);
                   9757:                                }
                   9758:                        } else {
                   9759:                                REG16(AX) = (UINT16)GetLastError();
                   9760:                                m_CF = 1;
1.1       root     9761:                        }
1.1.1.14  root     9762:                }
                   9763:                break;
                   9764:        default:
1.1.1.22  root     9765:                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     9766:                REG16(AX) = 0x01;
                   9767:                m_CF = 1;
                   9768:                break;
                   9769:        }
                   9770: }
                   9771: 
                   9772: inline void msdos_int_21h_44h()
                   9773: {
1.1.1.22  root     9774:        static UINT16 iteration_count = 0;
                   9775:        
1.1.1.20  root     9776:        process_t *process = msdos_process_info_get(current_psp);
                   9777:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   9778:        
1.1.1.14  root     9779:        UINT32 val = DRIVE_NO_ROOT_DIR;
                   9780:        
                   9781:        switch(REG8(AL)) {
                   9782:        case 0x00:
                   9783:        case 0x01:
                   9784:        case 0x02:
                   9785:        case 0x03:
                   9786:        case 0x04:
                   9787:        case 0x05:
                   9788:        case 0x06:
                   9789:        case 0x07:
1.1.1.20  root     9790:                if(fd >= process->max_files || !file_handler[fd].valid) {
                   9791:                        REG16(AX) = 0x06;
                   9792:                        m_CF = 1;
                   9793:                        return;
1.1.1.14  root     9794:                }
                   9795:                break;
                   9796:        case 0x08:
                   9797:        case 0x09:
                   9798:                if(REG8(BL) >= ('Z' - 'A' + 1)) {
                   9799:                        // invalid drive number
                   9800:                        REG16(AX) = 0x0f;
                   9801:                        m_CF = 1;
                   9802:                        return;
                   9803:                } else {
                   9804:                        if(REG8(BL) == 0) {
                   9805:                                val = GetDriveType(NULL);
                   9806:                        } else {
                   9807:                                char tmp[8];
                   9808:                                sprintf(tmp, "%c:\\", 'A' + REG8(BL) - 1);
                   9809:                                val = GetDriveType(tmp);
                   9810:                        }
                   9811:                        if(val == DRIVE_NO_ROOT_DIR) {
                   9812:                                // no drive
                   9813:                                REG16(AX) = 0x0f;
                   9814:                                m_CF = 1;
                   9815:                                return;
1.1       root     9816:                        }
                   9817:                }
                   9818:                break;
                   9819:        }
                   9820:        switch(REG8(AL)) {
                   9821:        case 0x00: // get ioctrl data
1.1.1.20  root     9822:                REG16(DX) = file_handler[fd].info;
1.1       root     9823:                break;
                   9824:        case 0x01: // set ioctrl data
1.1.1.20  root     9825:                file_handler[fd].info |= REG8(DL);
1.1       root     9826:                break;
                   9827:        case 0x02: // recv from character device
                   9828:        case 0x03: // send to character device
                   9829:        case 0x04: // recv from block device
                   9830:        case 0x05: // send to block device
                   9831:                REG16(AX) = 0x05;
1.1.1.3   root     9832:                m_CF = 1;
1.1       root     9833:                break;
                   9834:        case 0x06: // get read status
1.1.1.20  root     9835:                if(file_mode[file_handler[fd].mode].in) {
                   9836:                        if(file_handler[fd].atty) {
1.1.1.14  root     9837:                                REG8(AL) = msdos_kbhit() ? 0xff : 0x00;
1.1       root     9838:                        } else {
1.1.1.20  root     9839:                                REG8(AL) = eof(fd) ? 0x00 : 0xff;
1.1       root     9840:                        }
1.1.1.14  root     9841:                } else {
                   9842:                        REG8(AL) = 0x00;
1.1       root     9843:                }
                   9844:                break;
                   9845:        case 0x07: // get write status
1.1.1.20  root     9846:                if(file_mode[file_handler[fd].mode].out) {
1.1.1.14  root     9847:                        REG8(AL) = 0xff;
                   9848:                } else {
                   9849:                        REG8(AL) = 0x00;
1.1       root     9850:                }
                   9851:                break;
                   9852:        case 0x08: // check removable drive
1.1.1.14  root     9853:                if(val == DRIVE_REMOVABLE || val == DRIVE_CDROM) {
                   9854:                        // removable drive
                   9855:                        REG16(AX) = 0x00;
1.1       root     9856:                } else {
1.1.1.14  root     9857:                        // fixed drive
                   9858:                        REG16(AX) = 0x01;
1.1       root     9859:                }
                   9860:                break;
                   9861:        case 0x09: // check remote drive
1.1.1.14  root     9862:                if(val == DRIVE_REMOTE) {
                   9863:                        // remote drive
                   9864:                        REG16(DX) = 0x1000;
1.1       root     9865:                } else {
1.1.1.14  root     9866:                        // local drive
                   9867:                        REG16(DX) = 0x00;
1.1       root     9868:                }
                   9869:                break;
1.1.1.21  root     9870:        case 0x0a: // check remote handle
                   9871:                REG16(DX) = 0x00; // FIXME
                   9872:                break;
1.1       root     9873:        case 0x0b: // set retry count
                   9874:                break;
1.1.1.22  root     9875:        case 0x0c: // generic character device request
                   9876:                if(REG8(CL) == 0x45) {
                   9877:                        // set iteration (retry) count
                   9878:                        iteration_count = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
                   9879:                } else if(REG8(CL) == 0x4a) {
                   9880:                        // select code page
                   9881:                        active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2);
                   9882:                        msdos_nls_tables_update();
                   9883:                } else if(REG8(CL) == 0x65) {
                   9884:                        // get iteration (retry) count
                   9885:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX)) = iteration_count;
                   9886:                } else if(REG8(CL) == 0x6a) {
                   9887:                        // query selected code page
                   9888:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2; // FIXME
                   9889:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
                   9890:                        
                   9891:                        CPINFO info;
                   9892:                        GetCPInfo(active_code_page, &info);
                   9893:                        
                   9894:                        if(info.MaxCharSize != 1) {
                   9895:                                for(int i = 0;; i++) {
                   9896:                                        UINT8 lo = info.LeadByte[2 * i + 0];
                   9897:                                        UINT8 hi = info.LeadByte[2 * i + 1];
                   9898:                                        
                   9899:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 0) = lo;
                   9900:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 2) = hi;
                   9901:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) + 4;
                   9902:                                        
                   9903:                                        if(lo == 0 && hi == 0) {
                   9904:                                                break;
                   9905:                                        }
                   9906:                                }
                   9907:                        }
                   9908:                } else if(REG8(CL) == 0x7f) {
                   9909:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00) = 0;
                   9910:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x01) = 0;
                   9911:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) = 14;
                   9912:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04) = 1;
                   9913:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06) = 1;
                   9914:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x07) = 0;
                   9915:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08) = 4;
                   9916:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a) =  8 * (*(UINT16 *)(mem + 0x44a));
                   9917:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c) = 16 * (*(UINT8  *)(mem + 0x484) + 1);
                   9918:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e) = *(UINT16 *)(mem + 0x44a);
                   9919:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10) = *(UINT8  *)(mem + 0x484) + 1;
                   9920:                } else {
                   9921:                        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));
                   9922:                        REG16(AX) = 0x01; // invalid function
                   9923:                        m_CF = 1;
                   9924:                }
                   9925:                break;
                   9926:        case 0x0d: // generic block device request
                   9927:                if(REG8(CL) == 0x40) {
                   9928:                        // set device parameters
                   9929:                } else if(REG8(CL) == 0x46) {
                   9930:                        // set volume serial number
                   9931:                } else if(REG8(CL) == 0x4a) {
                   9932:                        // lock logical volume
                   9933:                } else if(REG8(CL) == 0x4b) {
                   9934:                        // lock physical volume
                   9935:                } else if(REG8(CL) == 0x60) {
                   9936:                        // get device parameters
                   9937:                        char dev[] = "\\\\.\\A:";
                   9938:                        dev[4] = 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   9939:                        
                   9940:                        HANDLE hFile = CreateFile(dev, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   9941:                        if(hFile != INVALID_HANDLE_VALUE) {
                   9942:                                DISK_GEOMETRY geo;
                   9943:                                DWORD dwSize;
                   9944:                                if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &geo, sizeof(geo), &dwSize, NULL)) {
                   9945:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x07; // ???
                   9946:                                        switch(geo.MediaType) {
                   9947:                                        case F5_360_512:
                   9948:                                        case F5_320_512:
                   9949:                                        case F5_320_1024:
                   9950:                                        case F5_180_512:
                   9951:                                        case F5_160_512:
                   9952:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // 320K/360K disk
                   9953:                                                break;
                   9954:                                        case F5_1Pt2_512:
                   9955:                                        case F3_1Pt2_512:
                   9956:                                        case F3_1Pt23_1024:
                   9957:                                        case F5_1Pt23_1024:
                   9958:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x01; // 1.2M disk
                   9959:                                                break;
                   9960:                                        case F3_720_512:
                   9961:                                        case F3_640_512:
                   9962:                                        case F5_640_512:
                   9963:                                        case F5_720_512:
                   9964:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02; // 720K disk
                   9965:                                                break;
                   9966:                                        case F8_256_128:
                   9967:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x03; // single-density 8-inch disk
                   9968:                                                break;
                   9969:                                        case FixedMedia:
                   9970:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   9971:                                                break;
                   9972:                                        case F3_1Pt44_512:
                   9973:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   9974:                                                break;
                   9975:                                        case F3_2Pt88_512:
                   9976:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09; // (DOS 5+) 2.88M floppy
                   9977:                                                break;
                   9978:                                        default:
                   9979:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   9980: //                                             *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   9981:                                                break;
                   9982:                                        }
                   9983:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = (geo.MediaType == FixedMedia) ? 0x01 : 0x00;
                   9984:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04) = (geo.Cylinders.QuadPart > 0xffff) ? 0xffff : geo.Cylinders.QuadPart;
                   9985:                                        switch(geo.MediaType) {
                   9986:                                        case F5_360_512:
                   9987:                                        case F5_320_512:
                   9988:                                        case F5_320_1024:
                   9989:                                        case F5_180_512:
                   9990:                                        case F5_160_512:
                   9991:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x01; // 320K/360K disk
                   9992:                                                break;
                   9993:                                        default:
                   9994:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x00; // other drive types
                   9995:                                                break;
                   9996:                                        }
                   9997:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x00) = geo.BytesPerSector;
                   9998:                                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x02) = geo.SectorsPerTrack * geo.TracksPerCylinder;
                   9999:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x03) = 0;
                   10000:                                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x05) = 0;
                   10001:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x06) = 0;
                   10002:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x08) = 0;
                   10003:                                        switch(geo.MediaType) {
                   10004:                                        case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   10005:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xff;
                   10006:                                                break;
                   10007:                                        case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   10008:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfe;
                   10009:                                                break;
                   10010:                                        case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   10011:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfd;
                   10012:                                                break;
                   10013:                                        case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   10014:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfc;
                   10015:                                                break;
                   10016:                                        case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   10017:                                        case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   10018:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf9;
                   10019:                                                break;
                   10020:                                        case FixedMedia:        // hard disk
                   10021:                                        case RemovableMedia:
                   10022:                                        case Unknown:
                   10023:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf8;
                   10024:                                                break;
                   10025:                                        default:
                   10026:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf0;
                   10027:                                                break;
                   10028:                                        }
                   10029:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0d) = geo.SectorsPerTrack;
                   10030:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0f) = 1;
                   10031:                                        *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x11) = 0;
                   10032:                                        *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x15) = geo.TracksPerCylinder * geo.Cylinders.QuadPart;
                   10033:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x1f) = geo.Cylinders.QuadPart;
                   10034:                                        // 21h  BYTE    device type
                   10035:                                        // 22h  WORD    device attributes (removable or not, etc)
                   10036:                                } else {
                   10037:                                        REG16(AX) = 0x0f; // invalid drive
                   10038:                                        m_CF = 1;
                   10039:                                }
                   10040:                                CloseHandle(hFile);
                   10041:                        } else {
                   10042:                                REG16(AX) = 0x0f; // invalid drive
                   10043:                                m_CF = 1;
                   10044:                        }
                   10045:                } else if(REG8(CL) == 0x66) {
                   10046:                        // get volume serial number
                   10047:                        char path[] = "A:\\";
                   10048:                        char volume_label[MAX_PATH];
                   10049:                        DWORD serial_number = 0;
                   10050:                        char file_system[MAX_PATH];
                   10051:                        
                   10052:                        path[0] = 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   10053:                        
                   10054:                        if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
                   10055:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   10056:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = serial_number;
                   10057:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x06, 0x20, 11);
                   10058:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x06, volume_label, min(strlen(volume_label), 11));
                   10059:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x11, 0x20,  8);
                   10060:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x11, file_system, min(strlen(file_system), 8));
                   10061:                        } else {
                   10062:                                REG16(AX) = 0x0f; // invalid drive
                   10063:                                m_CF = 1;
                   10064:                        }
                   10065:                } else if(REG8(CL) == 0x67) {
                   10066:                        // get access flag
                   10067:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   10068:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 1;
                   10069:                } else if(REG8(CL) == 0x68) {
                   10070:                        // sense media type
                   10071:                        char dev[64];
                   10072:                        sprintf(dev, "\\\\.\\%c:", 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1);
                   10073:                        
                   10074:                        HANDLE hFile = CreateFile(dev, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   10075:                        if(hFile != INVALID_HANDLE_VALUE) {
                   10076:                                DISK_GEOMETRY geo;
                   10077:                                DWORD dwSize;
                   10078:                                if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &geo, sizeof(geo), &dwSize, NULL)) {
                   10079:                                        switch(geo.MediaType) {
                   10080:                                        case F3_720_512:
                   10081:                                        case F5_720_512:
                   10082:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   10083:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02;
                   10084:                                                break;
                   10085:                                        case F3_1Pt44_512:
                   10086:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   10087:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07;
                   10088:                                                break;
                   10089:                                        case F3_2Pt88_512:
                   10090:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   10091:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09;
                   10092:                                                break;
                   10093:                                        default:
                   10094:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x00;
                   10095:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // ???
                   10096:                                                break;
                   10097:                                        }
                   10098:                                } else {
                   10099:                                        REG16(AX) = 0x0f; // invalid drive
                   10100:                                        m_CF = 1;
                   10101:                                }
                   10102:                                CloseHandle(hFile);
                   10103:                        } else {
                   10104:                                REG16(AX) = 0x0f; // invalid drive
                   10105:                                m_CF = 1;
                   10106:                        }
                   10107:                } else if(REG8(CL) == 0x6a) {
                   10108:                        // unlock logical volume
                   10109:                } else if(REG8(CL) == 0x6b) {
                   10110:                        // unlock physical volume
                   10111:                } else {
                   10112:                        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));
                   10113:                        REG16(AX) = 0x01; // invalid function
                   10114:                        m_CF = 1;
                   10115:                }
                   10116:                break;
                   10117:        case 0x0e: // get logical drive map
                   10118:                {
                   10119:                        DWORD bits = 1 << ((REG8(BL) ? REG8(BL) : _getdrive()) - 1);
                   10120:                        if(!(GetLogicalDrives() & bits)) {
                   10121:                                REG16(AX) = 0x0f; // invalid drive
                   10122:                                m_CF = 1;
                   10123:                        } else {
                   10124:                                REG8(AL) = 0;
                   10125:                        }
                   10126:                }
                   10127:                break;
                   10128:        case 0x0f: // set logical drive map
                   10129:                {
                   10130:                        DWORD bits = 1 << ((REG8(BL) ? REG8(BL) : _getdrive()) - 1);
                   10131:                        if(!(GetLogicalDrives() & bits)) {
                   10132:                                REG16(AX) = 0x0f; // invalid drive
                   10133:                                m_CF = 1;
                   10134:                        }
                   10135:                }
                   10136:                break;
                   10137:        case 0x10: // query generic ioctrl capability (handle)
                   10138:                switch(REG8(CL)) {
                   10139:                case 0x45:
                   10140:                case 0x4a:
                   10141:                case 0x65:
                   10142:                case 0x6a:
                   10143:                case 0x7f:
                   10144:                        REG16(AX) = 0x0000; // supported
                   10145:                        break;
                   10146:                default:
                   10147:                        REG8(AL) = 0x01; // ioctl capability not available
                   10148:                        m_CF = 1;
                   10149:                        break;
                   10150:                }
                   10151:                break;
                   10152:        case 0x11: // query generic ioctrl capability (drive)
                   10153:                switch(REG8(CL)) {
                   10154:                case 0x40:
                   10155:                case 0x46:
                   10156:                case 0x4a:
                   10157:                case 0x4b:
                   10158:                case 0x60:
                   10159:                case 0x66:
                   10160:                case 0x67:
                   10161:                case 0x68:
                   10162:                case 0x6a:
                   10163:                case 0x6b:
                   10164:                        REG16(AX) = 0x0000; // supported
                   10165:                        break;
                   10166:                default:
                   10167:                        REG8(AL) = 0x01; // ioctl capability not available
                   10168:                        m_CF = 1;
                   10169:                        break;
                   10170:                }
                   10171:                break;
                   10172:        case 0x12: // determine dos type
                   10173:        case 0x51: // concurrent dos v3.2+ - installation check
                   10174:        case 0x52: // determine dos type/get dr dos versuin
                   10175:                REG16(AX) = 0x01; // this  is not DR-DOS
                   10176:                m_CF = 1;
                   10177:                break;
1.1       root     10178:        default:
1.1.1.22  root     10179:                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     10180:                REG16(AX) = 0x01;
1.1.1.3   root     10181:                m_CF = 1;
1.1       root     10182:                break;
                   10183:        }
                   10184: }
                   10185: 
                   10186: inline void msdos_int_21h_45h()
                   10187: {
                   10188:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10189:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10190:        
1.1.1.20  root     10191:        if(fd < process->max_files && file_handler[fd].valid) {
                   10192:                int dup_fd = _dup(fd);
                   10193:                if(dup_fd != -1) {
                   10194:                        REG16(AX) = dup_fd;
                   10195:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   10196: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   10197:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     10198:                } else {
                   10199:                        REG16(AX) = errno;
1.1.1.3   root     10200:                        m_CF = 1;
1.1       root     10201:                }
                   10202:        } else {
                   10203:                REG16(AX) = 0x06;
1.1.1.3   root     10204:                m_CF = 1;
1.1       root     10205:        }
                   10206: }
                   10207: 
                   10208: inline void msdos_int_21h_46h()
                   10209: {
                   10210:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10211:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   10212:        int dup_fd = REG16(CX);
                   10213:        int tmp_fd = msdos_psp_get_file_table(REG16(CX), current_psp);
1.1       root     10214:        
1.1.1.20  root     10215:        if(REG16(BX) == REG16(CX)) {
                   10216:                REG16(AX) = 0x06;
                   10217:                m_CF = 1;
                   10218:        } else if(fd < process->max_files && file_handler[fd].valid && dup_fd < process->max_files) {
                   10219:                if(tmp_fd < process->max_files && file_handler[tmp_fd].valid) {
                   10220:                        _close(tmp_fd);
                   10221:                        msdos_file_handler_close(tmp_fd);
                   10222:                        msdos_psp_set_file_table(dup_fd, 0x0ff, current_psp);
                   10223:                }
                   10224:                if(_dup2(fd, dup_fd) != -1) {
                   10225:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   10226: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   10227:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     10228:                } else {
                   10229:                        REG16(AX) = errno;
1.1.1.3   root     10230:                        m_CF = 1;
1.1       root     10231:                }
                   10232:        } else {
                   10233:                REG16(AX) = 0x06;
1.1.1.3   root     10234:                m_CF = 1;
1.1       root     10235:        }
                   10236: }
                   10237: 
                   10238: inline void msdos_int_21h_47h(int lfn)
                   10239: {
                   10240:        char path[MAX_PATH];
                   10241:        
                   10242:        if(_getdcwd(REG8(DL), path, MAX_PATH) != NULL) {
                   10243:                if(path[1] == ':') {
                   10244:                        // the returned path does not include a drive or the initial backslash
1.1.1.3   root     10245:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), (lfn ? path : msdos_short_path(path)) + 3);
1.1       root     10246:                } else {
1.1.1.3   root     10247:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), lfn ? path : msdos_short_path(path));
1.1       root     10248:                }
                   10249:        } else {
                   10250:                REG16(AX) = errno;
1.1.1.3   root     10251:                m_CF = 1;
1.1       root     10252:        }
                   10253: }
                   10254: 
                   10255: inline void msdos_int_21h_48h()
                   10256: {
1.1.1.19  root     10257:        int seg, umb_linked;
1.1       root     10258:        
1.1.1.8   root     10259:        if((malloc_strategy & 0xf0) == 0x00) {
1.1.1.19  root     10260:                // unlink umb not to allocate memory in umb
                   10261:                if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   10262:                        msdos_mem_unlink_umb();
                   10263:                }
1.1.1.8   root     10264:                if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   10265:                        REG16(AX) = seg;
                   10266:                } else {
                   10267:                        REG16(AX) = 0x08;
                   10268:                        REG16(BX) = msdos_mem_get_free(first_mcb, 0);
                   10269:                        m_CF = 1;
                   10270:                }
1.1.1.19  root     10271:                if(umb_linked != 0) {
                   10272:                        msdos_mem_link_umb();
                   10273:                }
1.1.1.8   root     10274:        } else if((malloc_strategy & 0xf0) == 0x40) {
                   10275:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   10276:                        REG16(AX) = seg;
                   10277:                } else {
                   10278:                        REG16(AX) = 0x08;
                   10279:                        REG16(BX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   10280:                        m_CF = 1;
                   10281:                }
                   10282:        } else if((malloc_strategy & 0xf0) == 0x80) {
                   10283:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   10284:                        REG16(AX) = seg;
                   10285:                } else if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   10286:                        REG16(AX) = seg;
                   10287:                } else {
                   10288:                        REG16(AX) = 0x08;
                   10289:                        REG16(BX) = max(msdos_mem_get_free(UMB_TOP >> 4, 0), msdos_mem_get_free(first_mcb, 0));
                   10290:                        m_CF = 1;
                   10291:                }
1.1       root     10292:        }
                   10293: }
                   10294: 
                   10295: inline void msdos_int_21h_49h()
                   10296: {
1.1.1.14  root     10297:        int mcb_seg = SREG(ES) - 1;
                   10298:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   10299:        
                   10300:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   10301:                msdos_mem_free(SREG(ES));
                   10302:        } else {
1.1.1.33  root     10303:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.14  root     10304:                m_CF = 1;
                   10305:        }
1.1       root     10306: }
                   10307: 
                   10308: inline void msdos_int_21h_4ah()
                   10309: {
1.1.1.14  root     10310:        int mcb_seg = SREG(ES) - 1;
                   10311:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1       root     10312:        int max_paragraphs;
                   10313:        
1.1.1.14  root     10314:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   10315:                if(msdos_mem_realloc(SREG(ES), REG16(BX), &max_paragraphs)) {
                   10316:                        REG16(AX) = 0x08;
                   10317:                        REG16(BX) = max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs;
                   10318:                        m_CF = 1;
                   10319:                }
                   10320:        } else {
1.1.1.33  root     10321:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.3   root     10322:                m_CF = 1;
1.1       root     10323:        }
                   10324: }
                   10325: 
                   10326: inline void msdos_int_21h_4bh()
                   10327: {
1.1.1.3   root     10328:        char *command = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   10329:        param_block_t *param = (param_block_t *)(mem + SREG_BASE(ES) + REG16(BX));
1.1       root     10330:        
                   10331:        switch(REG8(AL)) {
                   10332:        case 0x00:
                   10333:        case 0x01:
                   10334:                if(msdos_process_exec(command, param, REG8(AL))) {
                   10335:                        REG16(AX) = 0x02;
1.1.1.3   root     10336:                        m_CF = 1;
1.1       root     10337:                }
                   10338:                break;
1.1.1.14  root     10339:        case 0x03:
                   10340:                {
                   10341:                        int fd;
                   10342:                        if((fd = _open(command, _O_RDONLY | _O_BINARY)) == -1) {
                   10343:                                REG16(AX) = 0x02;
                   10344:                                m_CF = 1;
                   10345:                                break;
                   10346:                        }
                   10347:                        int size = _read(fd, file_buffer, sizeof(file_buffer));
                   10348:                        _close(fd);
                   10349:                        
                   10350:                        UINT16 *overlay = (UINT16 *)param;
                   10351:                        
                   10352:                        // check exe header
                   10353:                        exe_header_t *header = (exe_header_t *)file_buffer;
                   10354:                        int header_size = 0;
                   10355:                        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   10356:                                header_size = header->header_size * 16;
                   10357:                                // relocation
                   10358:                                int start_seg = overlay[1];
                   10359:                                for(int i = 0; i < header->relocations; i++) {
                   10360:                                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   10361:                                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   10362:                                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   10363:                                }
                   10364:                        }
                   10365:                        memcpy(mem + (overlay[0] << 4), file_buffer + header_size, size - header_size);
                   10366:                }
                   10367:                break;
1.1       root     10368:        default:
1.1.1.22  root     10369:                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     10370:                REG16(AX) = 0x01;
1.1.1.3   root     10371:                m_CF = 1;
1.1       root     10372:                break;
                   10373:        }
                   10374: }
                   10375: 
                   10376: inline void msdos_int_21h_4ch()
                   10377: {
                   10378:        msdos_process_terminate(current_psp, REG8(AL), 1);
                   10379: }
                   10380: 
                   10381: inline void msdos_int_21h_4dh()
                   10382: {
                   10383:        REG16(AX) = retval;
                   10384: }
                   10385: 
                   10386: inline void msdos_int_21h_4eh()
                   10387: {
                   10388:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     10389:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   10390:        find_t *find = (find_t *)(mem + dta_laddr);
1.1.1.3   root     10391:        char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     10392:        WIN32_FIND_DATA fd;
                   10393:        
1.1.1.14  root     10394:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   10395:        find->find_magic = FIND_MAGIC;
                   10396:        find->dta_index = dtainfo - dtalist;
1.1       root     10397:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     10398:        dtainfo->allowable_mask = REG8(CL);
                   10399:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     10400:        
1.1.1.14  root     10401:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   10402:                dtainfo->allowable_mask &= ~8;
1.1       root     10403:        }
1.1.1.14  root     10404:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   10405:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     10406:                      !msdos_find_file_has_8dot3name(&fd)) {
                   10407:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   10408:                                FindClose(dtainfo->find_handle);
                   10409:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     10410:                                break;
                   10411:                        }
                   10412:                }
                   10413:        }
1.1.1.13  root     10414:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     10415:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   10416:                msdos_find_file_conv_local_time(&fd);
                   10417:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   10418:                find->size = fd.nFileSizeLow;
1.1.1.13  root     10419:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     10420:                REG16(AX) = 0;
1.1.1.14  root     10421:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     10422:                find->attrib = 8;
                   10423:                find->size = 0;
                   10424:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     10425:                dtainfo->allowable_mask &= ~8;
1.1       root     10426:                REG16(AX) = 0;
                   10427:        } else {
                   10428:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     10429:                m_CF = 1;
1.1       root     10430:        }
                   10431: }
                   10432: 
                   10433: inline void msdos_int_21h_4fh()
                   10434: {
                   10435:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     10436:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   10437:        find_t *find = (find_t *)(mem + dta_laddr);
1.1       root     10438:        WIN32_FIND_DATA fd;
                   10439:        
1.1.1.14  root     10440:        if(find->find_magic != FIND_MAGIC || find->dta_index >= MAX_DTAINFO) {
                   10441:                REG16(AX) = 0x12;
                   10442:                m_CF = 1;
                   10443:                return;
                   10444:        }
                   10445:        dtainfo_t *dtainfo = &dtalist[find->dta_index];
1.1.1.13  root     10446:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   10447:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     10448:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     10449:                              !msdos_find_file_has_8dot3name(&fd)) {
                   10450:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   10451:                                        FindClose(dtainfo->find_handle);
                   10452:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     10453:                                        break;
                   10454:                                }
                   10455:                        }
                   10456:                } else {
1.1.1.13  root     10457:                        FindClose(dtainfo->find_handle);
                   10458:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     10459:                }
                   10460:        }
1.1.1.13  root     10461:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     10462:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   10463:                msdos_find_file_conv_local_time(&fd);
                   10464:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   10465:                find->size = fd.nFileSizeLow;
1.1.1.13  root     10466:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     10467:                REG16(AX) = 0;
1.1.1.14  root     10468:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     10469:                find->attrib = 8;
                   10470:                find->size = 0;
                   10471:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     10472:                dtainfo->allowable_mask &= ~8;
1.1       root     10473:                REG16(AX) = 0;
                   10474:        } else {
                   10475:                REG16(AX) = 0x12;
1.1.1.3   root     10476:                m_CF = 1;
1.1       root     10477:        }
                   10478: }
                   10479: 
                   10480: inline void msdos_int_21h_50h()
                   10481: {
1.1.1.8   root     10482:        if(current_psp != REG16(BX)) {
                   10483:                process_t *process = msdos_process_info_get(current_psp);
                   10484:                if(process != NULL) {
                   10485:                        process->psp = REG16(BX);
                   10486:                }
                   10487:                current_psp = REG16(BX);
1.1.1.23  root     10488:                msdos_sda_update(current_psp);
1.1.1.8   root     10489:        }
1.1       root     10490: }
                   10491: 
                   10492: inline void msdos_int_21h_51h()
                   10493: {
                   10494:        REG16(BX) = current_psp;
                   10495: }
                   10496: 
                   10497: inline void msdos_int_21h_52h()
                   10498: {
1.1.1.25  root     10499:        SREG(ES) = DOS_INFO_TOP >> 4;
1.1.1.3   root     10500:        i386_load_segment_descriptor(ES);
1.1.1.25  root     10501:        REG16(BX) = offsetof(dos_info_t, first_dpb);
1.1       root     10502: }
                   10503: 
                   10504: inline void msdos_int_21h_54h()
                   10505: {
                   10506:        process_t *process = msdos_process_info_get(current_psp);
                   10507:        
                   10508:        REG8(AL) = process->verify;
                   10509: }
                   10510: 
                   10511: inline void msdos_int_21h_55h()
                   10512: {
                   10513:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   10514:        
                   10515:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
                   10516:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   10517:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   10518:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   10519:        psp->parent_psp = current_psp;
                   10520: }
                   10521: 
                   10522: inline void msdos_int_21h_56h(int lfn)
                   10523: {
                   10524:        char src[MAX_PATH], dst[MAX_PATH];
1.1.1.3   root     10525:        strcpy(src, msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn));
                   10526:        strcpy(dst, msdos_trimmed_path((char *)(mem + SREG_BASE(ES) + REG16(DI)), lfn));
1.1       root     10527:        
                   10528:        if(rename(src, dst)) {
                   10529:                REG16(AX) = errno;
1.1.1.3   root     10530:                m_CF = 1;
1.1       root     10531:        }
                   10532: }
                   10533: 
                   10534: inline void msdos_int_21h_57h()
                   10535: {
                   10536:        FILETIME time, local;
1.1.1.14  root     10537:        FILETIME *ctime, *atime, *mtime;
1.1.1.21  root     10538:        HANDLE hHandle;
1.1       root     10539:        
1.1.1.21  root     10540:        if((hHandle = (HANDLE)_get_osfhandle(REG16(BX))) == INVALID_HANDLE_VALUE) {
1.1.1.14  root     10541:                REG16(AX) = (UINT16)GetLastError();
                   10542:                m_CF = 1;
                   10543:                return;
                   10544:        }
                   10545:        ctime = atime = mtime = NULL;
                   10546:        
1.1       root     10547:        switch(REG8(AL)) {
                   10548:        case 0x00:
1.1.1.6   root     10549:        case 0x01:
1.1.1.14  root     10550:                mtime = &time;
1.1.1.6   root     10551:                break;
                   10552:        case 0x04:
                   10553:        case 0x05:
1.1.1.14  root     10554:                atime = &time;
1.1       root     10555:                break;
1.1.1.6   root     10556:        case 0x06:
                   10557:        case 0x07:
1.1.1.14  root     10558:                ctime = &time;
                   10559:                break;
                   10560:        default:
1.1.1.22  root     10561:                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     10562:                REG16(AX) = 0x01;
                   10563:                m_CF = 1;
                   10564:                return;
                   10565:        }
                   10566:        if(REG8(AL) & 1) {
1.1       root     10567:                DosDateTimeToFileTime(REG16(DX), REG16(CX), &local);
                   10568:                LocalFileTimeToFileTime(&local, &time);
1.1.1.21  root     10569:                if(!SetFileTime(hHandle, ctime, atime, mtime)) {
1.1       root     10570:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     10571:                        m_CF = 1;
1.1       root     10572:                }
1.1.1.14  root     10573:        } else {
1.1.1.21  root     10574:                if(!GetFileTime(hHandle, ctime, atime, mtime)) {
1.1.1.14  root     10575:                        // assume a device and use the current time
                   10576:                        GetSystemTimeAsFileTime(&time);
                   10577:                }
                   10578:                FileTimeToLocalFileTime(&time, &local);
                   10579:                FileTimeToDosDateTime(&local, &REG16(DX), &REG16(CX));
1.1       root     10580:        }
                   10581: }
                   10582: 
                   10583: inline void msdos_int_21h_58h()
                   10584: {
                   10585:        switch(REG8(AL)) {
                   10586:        case 0x00:
1.1.1.7   root     10587:                REG16(AX) = malloc_strategy;
                   10588:                break;
                   10589:        case 0x01:
1.1.1.24  root     10590: //             switch(REG16(BX)) {
                   10591:                switch(REG8(BL)) {
1.1.1.7   root     10592:                case 0x0000:
                   10593:                case 0x0001:
                   10594:                case 0x0002:
                   10595:                case 0x0040:
                   10596:                case 0x0041:
                   10597:                case 0x0042:
                   10598:                case 0x0080:
                   10599:                case 0x0081:
                   10600:                case 0x0082:
                   10601:                        malloc_strategy = REG16(BX);
1.1.1.23  root     10602:                        msdos_sda_update(current_psp);
1.1.1.7   root     10603:                        break;
                   10604:                default:
1.1.1.22  root     10605:                        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     10606:                        REG16(AX) = 0x01;
                   10607:                        m_CF = 1;
                   10608:                        break;
                   10609:                }
                   10610:                break;
                   10611:        case 0x02:
1.1.1.19  root     10612:                REG8(AL) = msdos_mem_get_umb_linked() ? 1 : 0;
1.1.1.7   root     10613:                break;
                   10614:        case 0x03:
1.1.1.24  root     10615: //             switch(REG16(BX)) {
                   10616:                switch(REG8(BL)) {
1.1.1.7   root     10617:                case 0x0000:
1.1.1.19  root     10618:                        msdos_mem_unlink_umb();
                   10619:                        break;
1.1.1.7   root     10620:                case 0x0001:
1.1.1.19  root     10621:                        msdos_mem_link_umb();
1.1.1.7   root     10622:                        break;
                   10623:                default:
1.1.1.22  root     10624:                        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     10625:                        REG16(AX) = 0x01;
                   10626:                        m_CF = 1;
                   10627:                        break;
                   10628:                }
1.1       root     10629:                break;
                   10630:        default:
1.1.1.22  root     10631:                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     10632:                REG16(AX) = 0x01;
1.1.1.3   root     10633:                m_CF = 1;
1.1       root     10634:                break;
                   10635:        }
                   10636: }
                   10637: 
                   10638: inline void msdos_int_21h_59h()
                   10639: {
1.1.1.23  root     10640:        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   10641:        
                   10642:        REG16(AX) = sda->extended_error_code;
                   10643:        REG8(BH) = sda->error_class;
                   10644:        REG8(BL) = sda->suggested_action;
                   10645:        REG8(CH) = sda->locus_of_last_error;
1.1       root     10646: }
                   10647: 
                   10648: inline void msdos_int_21h_5ah()
                   10649: {
1.1.1.3   root     10650:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     10651:        int len = strlen(path);
                   10652:        char tmp[MAX_PATH];
                   10653:        
                   10654:        if(GetTempFileName(path, "TMP", 0, tmp)) {
                   10655:                int fd = _open(tmp, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   10656:                
                   10657:                SetFileAttributes(tmp, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   10658:                REG16(AX) = fd;
                   10659:                msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     10660:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     10661:                
                   10662:                strcpy(path, tmp);
                   10663:                int dx = REG16(DX) + len;
1.1.1.3   root     10664:                int ds = SREG(DS);
1.1       root     10665:                while(dx > 0xffff) {
                   10666:                        dx -= 0x10;
                   10667:                        ds++;
                   10668:                }
                   10669:                REG16(DX) = dx;
1.1.1.3   root     10670:                SREG(DS) = ds;
                   10671:                i386_load_segment_descriptor(DS);
1.1       root     10672:        } else {
                   10673:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     10674:                m_CF = 1;
1.1       root     10675:        }
                   10676: }
                   10677: 
                   10678: inline void msdos_int_21h_5bh()
                   10679: {
1.1.1.3   root     10680:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     10681:        
1.1.1.24  root     10682:        if(msdos_is_existing_file(path)) {
1.1       root     10683:                // already exists
                   10684:                REG16(AX) = 0x50;
1.1.1.3   root     10685:                m_CF = 1;
1.1       root     10686:        } else {
                   10687:                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   10688:                
                   10689:                if(fd != -1) {
                   10690:                        SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   10691:                        REG16(AX) = fd;
                   10692:                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     10693:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     10694:                } else {
                   10695:                        REG16(AX) = errno;
1.1.1.3   root     10696:                        m_CF = 1;
1.1       root     10697:                }
                   10698:        }
                   10699: }
                   10700: 
                   10701: inline void msdos_int_21h_5ch()
                   10702: {
                   10703:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10704:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10705:        
1.1.1.20  root     10706:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     10707:                if(REG8(AL) == 0 || REG8(AL) == 1) {
1.1.1.35  root     10708:                        static const int modes[2] = {_LK_LOCK, _LK_UNLCK};
1.1.1.20  root     10709:                        UINT32 pos = _tell(fd);
                   10710:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), SEEK_SET);
                   10711:                        if(_locking(fd, modes[REG8(AL)], (REG16(SI) << 16) | REG16(DI))) {
1.1       root     10712:                                REG16(AX) = errno;
1.1.1.3   root     10713:                                m_CF = 1;
1.1       root     10714:                        }
1.1.1.20  root     10715:                        _lseek(fd, pos, SEEK_SET);
1.1.1.26  root     10716:                        
1.1       root     10717:                        // some seconds may be passed in _locking()
1.1.1.35  root     10718:                        REQUEST_HARDWRE_UPDATE();
1.1       root     10719:                } else {
                   10720:                        REG16(AX) = 0x01;
1.1.1.3   root     10721:                        m_CF = 1;
1.1       root     10722:                }
                   10723:        } else {
                   10724:                REG16(AX) = 0x06;
1.1.1.3   root     10725:                m_CF = 1;
1.1       root     10726:        }
                   10727: }
                   10728: 
1.1.1.22  root     10729: inline void msdos_int_21h_5dh()
                   10730: {
                   10731:        switch(REG8(AL)) {
                   10732:        case 0x06: // get address of dos swappable data area
1.1.1.23  root     10733:                SREG(DS) = (SDA_TOP >> 4);
                   10734:                i386_load_segment_descriptor(DS);
                   10735:                REG16(SI) = offsetof(sda_t, crit_error_flag);
                   10736:                REG16(CX) = 0x80;
                   10737:                REG16(DX) = 0x1a;
                   10738:                break;
                   10739:        case 0x0b: // get dos swappable data areas
1.1.1.22  root     10740:                REG16(AX) = 0x01;
                   10741:                m_CF = 1;
                   10742:                break;
                   10743:        case 0x08: // set redirected printer mode
                   10744:        case 0x09: // flush redirected printer output
                   10745:        case 0x0a: // set extended error information
                   10746:                break;
                   10747:        default:
                   10748:                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));
                   10749:                REG16(AX) = 0x01;
                   10750:                m_CF = 1;
                   10751:                break;
                   10752:        }
                   10753: }
                   10754: 
1.1.1.30  root     10755: inline void msdos_int_21h_5fh()
                   10756: {
                   10757:        switch(REG8(AL)) {
                   10758:        case 0x02:
                   10759:                {
                   10760:                        DWORD drives = GetLogicalDrives();
                   10761:                        for(int i = 0, index = 0; i < 26; i++) {
                   10762:                                if(drives & (1 << i)) {
                   10763:                                        char volume[] = "A:\\";
                   10764:                                        volume[0] = 'A' + i;
                   10765:                                        if(GetDriveType(volume) == DRIVE_REMOTE) {
                   10766:                                                if(index == REG16(BX)) {
                   10767:                                                        DWORD dwSize = 128;
                   10768:                                                        volume[2] = '\0';
                   10769:                                                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), volume);
                   10770:                                                        WNetGetConnection(volume, (char *)(mem + SREG_BASE(ES) + REG16(DI)), &dwSize);
                   10771:                                                        REG8(BH) = 0x00; // valid
                   10772:                                                        REG8(BL) = 0x04; // disk drive
                   10773:                                                        REG16(CX) = 0x00;
                   10774:                                                        return;
                   10775:                                                }
                   10776:                                                index++;
                   10777:                                        }
                   10778:                                }
                   10779:                        }
                   10780:                }
                   10781:                REG16(AX) = 0x12; // no more files
                   10782:                m_CF = 1;
                   10783:                break;
                   10784:        default:
                   10785:                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));
                   10786:                REG16(AX) = 0x01;
                   10787:                m_CF = 1;
                   10788:                break;
                   10789:        }
                   10790: }
                   10791: 
1.1       root     10792: inline void msdos_int_21h_60h(int lfn)
                   10793: {
1.1.1.14  root     10794:        char full[MAX_PATH], *path;
                   10795:        
1.1       root     10796:        if(lfn) {
1.1.1.14  root     10797:                char *name;
                   10798:                *full = '\0';
1.1.1.3   root     10799:                GetFullPathName((char *)(mem + SREG_BASE(DS) + REG16(SI)), MAX_PATH, full, &name);
1.1.1.14  root     10800:                switch(REG8(CL)) {
                   10801:                case 1:
                   10802:                        GetShortPathName(full, full, MAX_PATH);
                   10803:                        my_strupr(full);
                   10804:                        break;
                   10805:                case 2:
                   10806:                        GetLongPathName(full, full, MAX_PATH);
                   10807:                        break;
                   10808:                }
                   10809:                path = full;
                   10810:        } else {
                   10811:                path = msdos_short_full_path((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   10812:        }
                   10813:        if(*path != '\0') {
                   10814:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
1.1       root     10815:        } else {
1.1.1.14  root     10816:                REG16(AX) = (UINT16)GetLastError();
                   10817:                m_CF = 1;
1.1       root     10818:        }
                   10819: }
                   10820: 
                   10821: inline void msdos_int_21h_61h()
                   10822: {
                   10823:        REG8(AL) = 0;
                   10824: }
                   10825: 
                   10826: inline void msdos_int_21h_62h()
                   10827: {
                   10828:        REG16(BX) = current_psp;
                   10829: }
                   10830: 
                   10831: inline void msdos_int_21h_63h()
                   10832: {
                   10833:        switch(REG8(AL)) {
                   10834:        case 0x00:
1.1.1.3   root     10835:                SREG(DS) = (DBCS_TABLE >> 4);
                   10836:                i386_load_segment_descriptor(DS);
1.1       root     10837:                REG16(SI) = (DBCS_TABLE & 0x0f);
                   10838:                REG8(AL) = 0x00;
                   10839:                break;
1.1.1.22  root     10840:        case 0x01: // set korean input mode
                   10841:        case 0x02: // get korean input mode
                   10842:                REG8(AL) = 0xff; // not supported
                   10843:                break;
1.1       root     10844:        default:
1.1.1.22  root     10845:                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     10846:                REG16(AX) = 0x01;
1.1.1.3   root     10847:                m_CF = 1;
1.1       root     10848:                break;
                   10849:        }
                   10850: }
                   10851: 
1.1.1.25  root     10852: UINT16 get_extended_country_info(UINT8 func)
1.1       root     10853: {
1.1.1.25  root     10854:        switch(func) {
1.1.1.17  root     10855:        case 0x01:
                   10856:                if(REG16(CX) >= 5) {
1.1.1.19  root     10857:                        UINT8 data[1 + 2 + 2 + 2 + sizeof(country_info_t)];
1.1.1.17  root     10858:                        if(REG16(CX) > sizeof(data))            // cx = actual transfer size
                   10859:                                REG16(CX) = sizeof(data);
                   10860:                        ZeroMemory(data, sizeof(data));
                   10861:                        data[0] = 0x01;
                   10862:                        *(UINT16 *)(data + 1) = REG16(CX) - 3;
1.1.1.19  root     10863:                        *(UINT16 *)(data + 3) = get_country_info((country_info_t*)(data + 7));
1.1.1.17  root     10864:                        *(UINT16 *)(data + 5) = active_code_page;
                   10865:                        memcpy(mem + SREG_BASE(ES) + REG16(DI), data, REG16(CX));
1.1.1.25  root     10866: //                     REG16(AX) = active_code_page;
1.1.1.17  root     10867:                } else {
1.1.1.25  root     10868:                        return(0x08); // insufficient memory
1.1.1.17  root     10869:                }
                   10870:                break;
                   10871:        case 0x02:
                   10872:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   10873:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (UPPERTABLE_TOP & 0x0f);
                   10874:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (UPPERTABLE_TOP >> 4);
1.1.1.25  root     10875: //             REG16(AX) = active_code_page;
1.1.1.17  root     10876:                REG16(CX) = 0x05;
                   10877:                break;
1.1.1.23  root     10878:        case 0x03:
                   10879:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   10880:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (LOWERTABLE_TOP & 0x0f);
                   10881:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (LOWERTABLE_TOP >> 4);
1.1.1.25  root     10882: //             REG16(AX) = active_code_page;
1.1.1.23  root     10883:                REG16(CX) = 0x05;
                   10884:                break;
1.1.1.17  root     10885:        case 0x04:
                   10886:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x04;
                   10887:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_UPPERTABLE_TOP & 0x0f);
                   10888:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_UPPERTABLE_TOP >> 4);
1.1.1.25  root     10889: //             REG16(AX) = active_code_page;
1.1.1.17  root     10890:                REG16(CX) = 0x05;
                   10891:                break;
                   10892:        case 0x05:
                   10893:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x05;
                   10894:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_TERMINATOR_TOP & 0x0f);
                   10895:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_TERMINATOR_TOP >> 4);
1.1.1.25  root     10896: //             REG16(AX) = active_code_page;
1.1.1.17  root     10897:                REG16(CX) = 0x05;
                   10898:                break;
                   10899:        case 0x06:
                   10900:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x06;
                   10901:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (COLLATING_TABLE_TOP & 0x0f);
                   10902:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (COLLATING_TABLE_TOP >> 4);
1.1.1.25  root     10903: //             REG16(AX) = active_code_page;
1.1.1.17  root     10904:                REG16(CX) = 0x05;
                   10905:                break;
1.1       root     10906:        case 0x07:
1.1.1.3   root     10907:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x07;
                   10908:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (DBCS_TOP & 0x0f);
                   10909:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (DBCS_TOP >> 4);
1.1.1.25  root     10910: //             REG16(AX) = active_code_page;
1.1       root     10911:                REG16(CX) = 0x05;
                   10912:                break;
1.1.1.25  root     10913:        default:
                   10914:                return(0x01); // function number invalid
                   10915:        }
                   10916:        return(0x00);
                   10917: }
                   10918: 
                   10919: inline void msdos_int_21h_65h()
                   10920: {
                   10921:        char tmp[0x10000];
                   10922:        
                   10923:        switch(REG8(AL)) {
                   10924:        case 0x01:
                   10925:        case 0x02:
                   10926:        case 0x03:
                   10927:        case 0x04:
                   10928:        case 0x05:
                   10929:        case 0x06:
                   10930:        case 0x07:
                   10931:                {
                   10932:                        UINT16 result = get_extended_country_info(REG8(AL));
                   10933:                        if(result) {
                   10934:                                REG16(AX) = result;
                   10935:                                m_CF = 1;
                   10936:                        } else {
                   10937:                                REG16(AX) = active_code_page; // FIXME: is this correct???
                   10938:                        }
                   10939:                }
                   10940:                break;
1.1       root     10941:        case 0x20:
1.1.1.25  root     10942:        case 0xa0:
1.1.1.19  root     10943:                memset(tmp, 0, sizeof(tmp));
                   10944:                tmp[0] = REG8(DL);
1.1       root     10945:                my_strupr(tmp);
                   10946:                REG8(DL) = tmp[0];
                   10947:                break;
                   10948:        case 0x21:
1.1.1.25  root     10949:        case 0xa1:
1.1       root     10950:                memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     10951:                memcpy(tmp, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     10952:                my_strupr(tmp);
1.1.1.3   root     10953:                memcpy(mem + SREG_BASE(DS) + REG16(DX), tmp, REG16(CX));
1.1       root     10954:                break;
                   10955:        case 0x22:
1.1.1.25  root     10956:        case 0xa2:
1.1.1.3   root     10957:                my_strupr((char *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1       root     10958:                break;
1.1.1.25  root     10959:        case 0x23:
                   10960:                // FIXME: need to check multi-byte (kanji) charactre?
                   10961:                if(REG8(DL) == 'N' || REG8(DL) == 'n' || (REG8(DL) == 0x82 && REG8(DH) == 0x78) || (REG8(DL) == 0x82 && REG8(DH) == 0x99)) {
                   10962:                        // 8278h/8299h: multi-byte (kanji) Y and y
                   10963:                        REG16(AX) = 0x00;
                   10964:                } else if(REG8(DL) == 'Y' || REG8(DL) == 'y' || (REG8(DL) == 0x82 && REG8(DH) == 0x6d) || (REG8(DL) == 0x82 && REG8(DH) == 0x8e)) {
                   10965:                        // 826dh/828eh: multi-byte (kanji) N and n
                   10966:                        REG16(AX) = 0x01;
                   10967:                } else {
                   10968:                        REG16(AX) = 0x02;
                   10969:                }
                   10970:                break;
1.1       root     10971:        default:
1.1.1.22  root     10972:                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     10973:                REG16(AX) = 0x01;
1.1.1.3   root     10974:                m_CF = 1;
1.1       root     10975:                break;
                   10976:        }
                   10977: }
                   10978: 
                   10979: inline void msdos_int_21h_66h()
                   10980: {
                   10981:        switch(REG8(AL)) {
                   10982:        case 0x01:
                   10983:                REG16(BX) = active_code_page;
                   10984:                REG16(DX) = system_code_page;
                   10985:                break;
                   10986:        case 0x02:
                   10987:                if(active_code_page == REG16(BX)) {
                   10988:                        REG16(AX) = 0xeb41;
                   10989:                } else if(_setmbcp(REG16(BX)) == 0) {
                   10990:                        active_code_page = REG16(BX);
1.1.1.17  root     10991:                        msdos_nls_tables_update();
1.1       root     10992:                        REG16(AX) = 0xeb41;
1.1.1.32  root     10993:                        SetConsoleCP(active_code_page);
                   10994:                        SetConsoleOutputCP(active_code_page);
1.1       root     10995:                } else {
                   10996:                        REG16(AX) = 0x25;
1.1.1.3   root     10997:                        m_CF = 1;
1.1       root     10998:                }
                   10999:                break;
                   11000:        default:
1.1.1.22  root     11001:                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     11002:                REG16(AX) = 0x01;
1.1.1.3   root     11003:                m_CF = 1;
1.1       root     11004:                break;
                   11005:        }
                   11006: }
                   11007: 
                   11008: inline void msdos_int_21h_67h()
                   11009: {
                   11010:        process_t *process = msdos_process_info_get(current_psp);
                   11011:        
                   11012:        if(REG16(BX) <= MAX_FILES) {
                   11013:                process->max_files = max(REG16(BX), 20);
                   11014:        } else {
                   11015:                REG16(AX) = 0x08;
1.1.1.3   root     11016:                m_CF = 1;
1.1       root     11017:        }
                   11018: }
                   11019: 
                   11020: inline void msdos_int_21h_68h()
                   11021: {
                   11022:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11023:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11024:        
1.1.1.20  root     11025:        if(fd < process->max_files && file_handler[fd].valid) {
                   11026:                // fflush(_fdopen(fd, ""));
1.1       root     11027:        } else {
                   11028:                REG16(AX) = 0x06;
1.1.1.3   root     11029:                m_CF = 1;
1.1       root     11030:        }
                   11031: }
                   11032: 
                   11033: inline void msdos_int_21h_69h()
                   11034: {
1.1.1.3   root     11035:        drive_info_t *info = (drive_info_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     11036:        char path[] = "A:\\";
                   11037:        char volume_label[MAX_PATH];
                   11038:        DWORD serial_number = 0;
                   11039:        char file_system[MAX_PATH];
                   11040:        
                   11041:        if(REG8(BL) == 0) {
                   11042:                path[0] = 'A' + _getdrive() - 1;
                   11043:        } else {
                   11044:                path[0] = 'A' + REG8(BL) - 1;
                   11045:        }
                   11046:        
                   11047:        switch(REG8(AL)) {
                   11048:        case 0x00:
                   11049:                if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
                   11050:                        info->info_level = 0;
                   11051:                        info->serial_number = serial_number;
                   11052:                        memset(info->volume_label, 0x20, 11);
                   11053:                        memcpy(info->volume_label, volume_label, min(strlen(volume_label), 11));
                   11054:                        memset(info->file_system, 0x20, 8);
                   11055:                        memcpy(info->file_system, file_system, min(strlen(file_system), 8));
                   11056:                } else {
                   11057:                        REG16(AX) = errno;
1.1.1.3   root     11058:                        m_CF = 1;
1.1       root     11059:                }
                   11060:                break;
                   11061:        case 0x01:
                   11062:                REG16(AX) = 0x03;
1.1.1.3   root     11063:                m_CF = 1;
1.1       root     11064:        }
                   11065: }
                   11066: 
                   11067: inline void msdos_int_21h_6ah()
                   11068: {
                   11069:        REG8(AH) = 0x68;
                   11070:        msdos_int_21h_68h();
                   11071: }
                   11072: 
                   11073: inline void msdos_int_21h_6bh()
                   11074: {
                   11075:        REG8(AL) = 0;
                   11076: }
                   11077: 
                   11078: inline void msdos_int_21h_6ch(int lfn)
                   11079: {
1.1.1.3   root     11080:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(SI)), lfn);
1.1       root     11081:        int mode = REG8(BL) & 0x03;
                   11082:        
                   11083:        if(mode < 0x03) {
1.1.1.29  root     11084:                if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1       root     11085:                        // file exists
                   11086:                        if(REG8(DL) & 1) {
1.1.1.37  root     11087:                                int fd = -1;
1.1.1.11  root     11088:                                UINT16 info;
1.1.1.37  root     11089:                                int sio_port = 0;
                   11090:                                int lpt_port = 0;
1.1       root     11091:                                
1.1.1.11  root     11092:                                if(msdos_is_con_path(path)) {
1.1.1.13  root     11093:                                        fd = msdos_open("CON", file_mode[mode].mode);
1.1.1.11  root     11094:                                        info = 0x80d3;
1.1.1.37  root     11095:                                } else if((sio_port = msdos_is_comm_path(path)) != 0) {
                   11096:                                        fd = msdos_open("NUL", file_mode[mode].mode);
1.1.1.14  root     11097:                                        info = 0x80d3;
1.1.1.37  root     11098:                                        msdos_set_comm_params(sio_port, path);
                   11099:                                } else if((lpt_port = msdos_is_prn_path(path)) != 0) {
                   11100:                                        fd = msdos_open("NUL", file_mode[mode].mode);
                   11101:                                        info = 0xa8c0;
1.1.1.29  root     11102:                                } else if(msdos_is_device_path(path)) {
1.1.1.20  root     11103:                                        fd = msdos_open("NUL", file_mode[mode].mode);
                   11104:                                        info = 0x80d3;
1.1.1.11  root     11105:                                } else {
1.1.1.13  root     11106:                                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     11107:                                        info = msdos_drive_number(path);
                   11108:                                }
1.1       root     11109:                                if(fd != -1) {
                   11110:                                        REG16(AX) = fd;
                   11111:                                        REG16(CX) = 1;
1.1.1.37  root     11112:                                        msdos_file_handler_open(fd, path, _isatty(fd), mode, info, current_psp, sio_port, lpt_port);
1.1.1.20  root     11113:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     11114:                                } else {
                   11115:                                        REG16(AX) = errno;
1.1.1.3   root     11116:                                        m_CF = 1;
1.1       root     11117:                                }
                   11118:                        } else if(REG8(DL) & 2) {
                   11119:                                int attr = GetFileAttributes(path);
1.1.1.37  root     11120:                                int fd = -1;
1.1.1.11  root     11121:                                UINT16 info;
1.1.1.37  root     11122:                                int sio_port = 0;
                   11123:                                int lpt_port = 0;
1.1       root     11124:                                
1.1.1.11  root     11125:                                if(msdos_is_con_path(path)) {
1.1.1.13  root     11126:                                        fd = msdos_open("CON", file_mode[mode].mode);
1.1.1.11  root     11127:                                        info = 0x80d3;
1.1.1.37  root     11128:                                } else if((sio_port = msdos_is_comm_path(path)) != 0) {
                   11129:                                        fd = msdos_open("NUL", file_mode[mode].mode);
1.1.1.14  root     11130:                                        info = 0x80d3;
1.1.1.37  root     11131:                                        msdos_set_comm_params(sio_port, path);
                   11132:                                } else if((lpt_port = msdos_is_prn_path(path)) != 0) {
                   11133:                                        fd = msdos_open("NUL", file_mode[mode].mode);
                   11134:                                        info = 0xa8c0;
1.1.1.29  root     11135:                                } else if(msdos_is_device_path(path)) {
1.1.1.20  root     11136:                                        fd = msdos_open("NUL", file_mode[mode].mode);
                   11137:                                        info = 0x80d3;
1.1       root     11138:                                } else {
                   11139:                                        fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
1.1.1.11  root     11140:                                        info = msdos_drive_number(path);
1.1       root     11141:                                }
                   11142:                                if(fd != -1) {
                   11143:                                        if(attr == -1) {
                   11144:                                                attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   11145:                                        }
                   11146:                                        SetFileAttributes(path, attr);
                   11147:                                        REG16(AX) = fd;
                   11148:                                        REG16(CX) = 3;
1.1.1.37  root     11149:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, info, current_psp, sio_port, lpt_port);
1.1.1.20  root     11150:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     11151:                                } else {
                   11152:                                        REG16(AX) = errno;
1.1.1.3   root     11153:                                        m_CF = 1;
1.1       root     11154:                                }
                   11155:                        } else {
                   11156:                                REG16(AX) = 0x50;
1.1.1.3   root     11157:                                m_CF = 1;
1.1       root     11158:                        }
                   11159:                } else {
                   11160:                        // file not exists
                   11161:                        if(REG8(DL) & 0x10) {
                   11162:                                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   11163:                                
                   11164:                                if(fd != -1) {
                   11165:                                        SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   11166:                                        REG16(AX) = fd;
                   11167:                                        REG16(CX) = 2;
                   11168:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     11169:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     11170:                                } else {
                   11171:                                        REG16(AX) = errno;
1.1.1.3   root     11172:                                        m_CF = 1;
1.1       root     11173:                                }
                   11174:                        } else {
                   11175:                                REG16(AX) = 0x02;
1.1.1.3   root     11176:                                m_CF = 1;
1.1       root     11177:                        }
                   11178:                }
                   11179:        } else {
                   11180:                REG16(AX) = 0x0c;
1.1.1.3   root     11181:                m_CF = 1;
1.1       root     11182:        }
                   11183: }
                   11184: 
                   11185: inline void msdos_int_21h_710dh()
                   11186: {
                   11187:        // reset drive
                   11188: }
                   11189: 
1.1.1.17  root     11190: inline void msdos_int_21h_7141h(int lfn)
                   11191: {
                   11192:        if(REG16(SI) == 0) {
                   11193:                msdos_int_21h_41h(lfn);
                   11194:                return;
                   11195:        }
                   11196:        if(REG16(SI) != 1) {
                   11197:                REG16(AX) = 5;
                   11198:                m_CF = 1;
                   11199:        }
                   11200:        /* wild card and matching attributes... */
                   11201:        char tmp[MAX_PATH * 2];
                   11202:        // copy search pathname (and quick check overrun)
                   11203:        ZeroMemory(tmp, sizeof(tmp));
                   11204:        tmp[MAX_PATH - 1] = '\0';
                   11205:        tmp[MAX_PATH] = 1;
                   11206:        strncpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(DX)), MAX_PATH);
                   11207:        
                   11208:        if(tmp[MAX_PATH - 1] != '\0' || tmp[MAX_PATH] != 1) {
                   11209:                REG16(AX) = 1;
                   11210:                m_CF = 1;
                   11211:                return;
                   11212:        }
                   11213:        for(char *s = tmp; *s; ++s) {
                   11214:                if(*s == '/') {
                   11215:                        *s = '\\';
                   11216:                }
                   11217:        }
                   11218:        char *tmp_name = (char *)_mbsrchr((unsigned char *)tmp, '\\');
                   11219:        if(tmp_name) {
                   11220:                ++tmp_name;
                   11221:        } else {
                   11222:                tmp_name = strchr(tmp, ':');
                   11223:                tmp_name = tmp_name ? tmp_name + 1 : tmp;
                   11224:        }
                   11225:        
                   11226:        WIN32_FIND_DATAA fd;
                   11227:        HANDLE fh = FindFirstFileA(tmp, &fd);
                   11228:        if(fh == INVALID_HANDLE_VALUE) {
                   11229:                REG16(AX) = 2;
                   11230:                m_CF = 1;
                   11231:                return;
                   11232:        }
                   11233:        do {
                   11234:                if(msdos_find_file_check_attribute(fd.dwFileAttributes, REG8(CL), REG8(CH)) && msdos_find_file_has_8dot3name(&fd)) {
                   11235:                        strcpy(tmp_name, fd.cFileName);
                   11236:                        if(remove(msdos_trimmed_path(tmp, lfn))) {
                   11237:                                REG16(AX) = 5;
                   11238:                                m_CF = 1;
                   11239:                                break;
                   11240:                        }
                   11241:                }
                   11242:        } while(FindNextFileA(fh, &fd));
                   11243:        if(!m_CF) {
                   11244:                if(GetLastError() != ERROR_NO_MORE_FILES) {
                   11245:                        m_CF = 1;
                   11246:                        REG16(AX) = 2;
                   11247:                }
                   11248:        }
                   11249:        FindClose(fh);
                   11250: }
                   11251: 
1.1       root     11252: inline void msdos_int_21h_714eh()
                   11253: {
                   11254:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     11255:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
                   11256:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     11257:        WIN32_FIND_DATA fd;
                   11258:        
1.1.1.13  root     11259:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   11260:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   11261:                FindClose(dtainfo->find_handle);
                   11262:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11263:        }
                   11264:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     11265:        dtainfo->allowable_mask = REG8(CL);
                   11266:        dtainfo->required_mask = REG8(CH);
                   11267:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     11268:        
1.1.1.14  root     11269:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   11270:                dtainfo->allowable_mask &= ~8;
1.1       root     11271:        }
1.1.1.14  root     11272:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   11273:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13  root     11274:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   11275:                                FindClose(dtainfo->find_handle);
                   11276:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11277:                                break;
                   11278:                        }
                   11279:                }
                   11280:        }
1.1.1.13  root     11281:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     11282:                find->attrib = fd.dwFileAttributes;
                   11283:                msdos_find_file_conv_local_time(&fd);
                   11284:                if(REG16(SI) == 0) {
                   11285:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   11286:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   11287:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   11288:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   11289:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   11290:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   11291:                } else {
                   11292:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   11293:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   11294:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   11295:                }
                   11296:                find->size_hi = fd.nFileSizeHigh;
                   11297:                find->size_lo = fd.nFileSizeLow;
                   11298:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     11299:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     11300:                REG16(AX) = dtainfo - dtalist + 1;
                   11301:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     11302:                // volume label
                   11303:                find->attrib = 8;
                   11304:                find->size_hi = find->size_lo = 0;
                   11305:                strcpy(find->full_name, process->volume_label);
                   11306:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     11307:                dtainfo->allowable_mask &= ~8;
                   11308:                REG16(AX) = dtainfo - dtalist + 1;
1.1       root     11309:        } else {
                   11310:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     11311:                m_CF = 1;
1.1       root     11312:        }
                   11313: }
                   11314: 
                   11315: inline void msdos_int_21h_714fh()
                   11316: {
                   11317:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     11318:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     11319:        WIN32_FIND_DATA fd;
                   11320:        
1.1.1.14  root     11321:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   11322:                REG16(AX) = 6;
1.1.1.13  root     11323:                m_CF = 1;
                   11324:                return;
                   11325:        }
1.1.1.14  root     11326:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     11327:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   11328:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     11329:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13  root     11330:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   11331:                                        FindClose(dtainfo->find_handle);
                   11332:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11333:                                        break;
                   11334:                                }
                   11335:                        }
                   11336:                } else {
1.1.1.13  root     11337:                        FindClose(dtainfo->find_handle);
                   11338:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11339:                }
                   11340:        }
1.1.1.13  root     11341:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     11342:                find->attrib = fd.dwFileAttributes;
                   11343:                msdos_find_file_conv_local_time(&fd);
                   11344:                if(REG16(SI) == 0) {
                   11345:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   11346:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   11347:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   11348:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   11349:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   11350:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   11351:                } else {
                   11352:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   11353:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   11354:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   11355:                }
                   11356:                find->size_hi = fd.nFileSizeHigh;
                   11357:                find->size_lo = fd.nFileSizeLow;
                   11358:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     11359:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     11360:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     11361:                // volume label
                   11362:                find->attrib = 8;
                   11363:                find->size_hi = find->size_lo = 0;
                   11364:                strcpy(find->full_name, process->volume_label);
                   11365:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     11366:                dtainfo->allowable_mask &= ~8;
1.1       root     11367:        } else {
                   11368:                REG16(AX) = 0x12;
1.1.1.3   root     11369:                m_CF = 1;
1.1       root     11370:        }
                   11371: }
                   11372: 
                   11373: inline void msdos_int_21h_71a0h()
                   11374: {
                   11375:        DWORD max_component_len, file_sys_flag;
                   11376:        
1.1.1.14  root     11377:        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))) {
                   11378:                REG16(BX) = (UINT16)file_sys_flag & (FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES | FILE_UNICODE_ON_DISK | FILE_VOLUME_IS_COMPRESSED);
                   11379:                REG16(BX) |= 0x4000;                            // supports LFN functions
1.1       root     11380:                REG16(CX) = (UINT16)max_component_len;          // 255
                   11381:                REG16(DX) = (UINT16)max_component_len + 5;      // 260
                   11382:        } else {
                   11383:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     11384:                m_CF = 1;
1.1       root     11385:        }
                   11386: }
                   11387: 
                   11388: inline void msdos_int_21h_71a1h()
                   11389: {
1.1.1.14  root     11390:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   11391:                REG16(AX) = 6;
1.1.1.13  root     11392:                m_CF = 1;
                   11393:                return;
                   11394:        }
1.1.1.14  root     11395:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     11396:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   11397:                FindClose(dtainfo->find_handle);
                   11398:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11399:        }
                   11400: }
                   11401: 
                   11402: inline void msdos_int_21h_71a6h()
                   11403: {
                   11404:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11405:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   11406:        
1.1.1.3   root     11407:        UINT8 *buffer = (UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     11408:        struct _stat64 status;
                   11409:        DWORD serial_number = 0;
                   11410:        
1.1.1.20  root     11411:        if(fd < process->max_files && file_handler[fd].valid) {
                   11412:                if(_fstat64(fd, &status) == 0) {
                   11413:                        if(file_handler[fd].path[1] == ':') {
1.1       root     11414:                                // NOTE: we need to consider the network file path "\\host\share\"
                   11415:                                char volume[] = "A:\\";
1.1.1.20  root     11416:                                volume[0] = file_handler[fd].path[1];
1.1       root     11417:                                GetVolumeInformation(volume, NULL, 0, &serial_number, NULL, NULL, NULL, 0);
                   11418:                        }
1.1.1.20  root     11419:                        *(UINT32 *)(buffer + 0x00) = GetFileAttributes(file_handler[fd].path);
1.1       root     11420:                        *(UINT32 *)(buffer + 0x04) = (UINT32)(status.st_ctime & 0xffffffff);
                   11421:                        *(UINT32 *)(buffer + 0x08) = (UINT32)((status.st_ctime >> 32) & 0xffffffff);
                   11422:                        *(UINT32 *)(buffer + 0x0c) = (UINT32)(status.st_atime & 0xffffffff);
                   11423:                        *(UINT32 *)(buffer + 0x10) = (UINT32)((status.st_atime >> 32) & 0xffffffff);
                   11424:                        *(UINT32 *)(buffer + 0x14) = (UINT32)(status.st_mtime & 0xffffffff);
                   11425:                        *(UINT32 *)(buffer + 0x18) = (UINT32)((status.st_mtime >> 32) & 0xffffffff);
                   11426:                        *(UINT32 *)(buffer + 0x1c) = serial_number;
                   11427:                        *(UINT32 *)(buffer + 0x20) = (UINT32)((status.st_size >> 32) & 0xffffffff);
                   11428:                        *(UINT32 *)(buffer + 0x24) = (UINT32)(status.st_size & 0xffffffff);
                   11429:                        *(UINT32 *)(buffer + 0x28) = status.st_nlink;
1.1.1.14  root     11430:                        // this is dummy id and it will be changed when it is reopened...
1.1       root     11431:                        *(UINT32 *)(buffer + 0x2c) = 0;
1.1.1.20  root     11432:                        *(UINT32 *)(buffer + 0x30) = file_handler[fd].id;
1.1       root     11433:                } else {
                   11434:                        REG16(AX) = errno;
1.1.1.3   root     11435:                        m_CF = 1;
1.1       root     11436:                }
                   11437:        } else {
                   11438:                REG16(AX) = 0x06;
1.1.1.3   root     11439:                m_CF = 1;
1.1       root     11440:        }
                   11441: }
                   11442: 
                   11443: inline void msdos_int_21h_71a7h()
                   11444: {
                   11445:        switch(REG8(BL)) {
                   11446:        case 0x00:
1.1.1.3   root     11447:                if(!FileTimeToDosDateTime((FILETIME *)(mem + SREG_BASE(DS) + REG16(SI)), &REG16(DX), &REG16(CX))) {
1.1       root     11448:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     11449:                        m_CF = 1;
1.1       root     11450:                }
                   11451:                break;
                   11452:        case 0x01:
                   11453:                // NOTE: we need to check BH that shows 10-millisecond untils past time in CX
1.1.1.3   root     11454:                if(!DosDateTimeToFileTime(REG16(DX), REG16(CX), (FILETIME *)(mem + SREG_BASE(ES) + REG16(DI)))) {
1.1       root     11455:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     11456:                        m_CF = 1;
1.1       root     11457:                }
                   11458:                break;
                   11459:        default:
1.1.1.22  root     11460:                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     11461:                REG16(AX) = 0x01;
1.1.1.3   root     11462:                m_CF = 1;
1.1       root     11463:                break;
                   11464:        }
                   11465: }
                   11466: 
                   11467: inline void msdos_int_21h_71a8h()
                   11468: {
                   11469:        if(REG8(DH) == 0) {
                   11470:                char tmp[MAX_PATH], fcb[MAX_PATH];
1.1.1.3   root     11471:                strcpy(tmp, msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     11472:                memset(fcb, 0x20, sizeof(fcb));
                   11473:                int len = strlen(tmp);
1.1.1.21  root     11474:                for(int i = 0, pos = 0; i < len; i++) {
1.1       root     11475:                        if(tmp[i] == '.') {
                   11476:                                pos = 8;
                   11477:                        } else {
                   11478:                                if(msdos_lead_byte_check(tmp[i])) {
                   11479:                                        fcb[pos++] = tmp[i++];
                   11480:                                }
                   11481:                                fcb[pos++] = tmp[i];
                   11482:                        }
                   11483:                }
1.1.1.3   root     11484:                memcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), fcb, 11);
1.1       root     11485:        } else {
1.1.1.3   root     11486:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     11487:        }
                   11488: }
                   11489: 
1.1.1.22  root     11490: inline void msdos_int_21h_71aah()
                   11491: {
                   11492:        char drv[] = "A:", path[MAX_PATH];
                   11493:        char *hoge=(char *)(mem + SREG_BASE(DS) + REG16(DX));
                   11494:        
                   11495:        if(REG8(BL) == 0) {
                   11496:                drv[0] = 'A' + _getdrive() - 1;
                   11497:        } else {
                   11498:                drv[0] = 'A' + REG8(BL) - 1;
                   11499:        }
                   11500:        switch(REG8(BH)) {
                   11501:        case 0x00:
                   11502:                if(DefineDosDevice(0, drv, (char *)(mem + SREG_BASE(DS) + REG16(DX))) == 0) {
                   11503:                        DWORD bits = 1 << ((REG8(BL) ? REG8(BL) : _getdrive()) - 1);
                   11504:                        if(GetLogicalDrives() & bits) {
                   11505:                                REG16(AX) = 0x0f; // invalid drive
                   11506:                        } else {
                   11507:                                REG16(AX) = 0x03; // path not found
                   11508:                        }
                   11509:                        m_CF = 1;
                   11510:                }
                   11511:                break;
                   11512:        case 0x01:
                   11513:                if(DefineDosDevice(DDD_REMOVE_DEFINITION, drv, NULL) == 0) {
                   11514:                        REG16(AX) = 0x0f; // invalid drive
                   11515:                        m_CF = 1;
                   11516:                }
                   11517:                break;
                   11518:        case 0x02:
                   11519:                if(QueryDosDevice(drv, path, MAX_PATH) == 0) {
                   11520:                        REG16(AX) = 0x0f; // invalid drive
                   11521:                        m_CF = 1;
                   11522:                } else if(strncmp(path, "\\??\\", 4) != 0) {
                   11523:                        REG16(AX) = 0x0f; // invalid drive
                   11524:                        m_CF = 1;
                   11525:                } else {
                   11526:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(DX)), path + 4);
                   11527:                }
                   11528:                break;
                   11529:        default:
                   11530:                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));
                   11531:                REG16(AX) = 0x01;
                   11532:                m_CF = 1;
                   11533:                break;
                   11534:        }
                   11535: }
                   11536: 
1.1.1.14  root     11537: inline void msdos_int_21h_7300h()
                   11538: {
                   11539:        if(REG8(AL) == 0) {
                   11540:                REG8(AL) = REG8(CL);
                   11541:                REG8(AH) = 0;
                   11542:        } else {
                   11543:                REG16(AX) = 0x01;
                   11544:                m_CF = 1;
                   11545:        }
                   11546: }
                   11547: 
                   11548: inline void msdos_int_21h_7302h()
                   11549: {
                   11550:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   11551:        UINT16 seg, ofs;
                   11552:        
                   11553:        if(REG16(CX) < 0x3f) {
                   11554:                REG8(AL) = 0x18;
                   11555:                m_CF = 1;
                   11556:        } else if(!msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   11557:                REG8(AL) = 0xff;
                   11558:                m_CF = 1;
                   11559:        } else {
                   11560:                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 2, mem + (seg << 4) + ofs, sizeof(dpb_t));
                   11561:        }
                   11562: }
                   11563: 
1.1       root     11564: inline void msdos_int_21h_7303h()
                   11565: {
1.1.1.3   root     11566:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   11567:        ext_space_info_t *info = (ext_space_info_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     11568:        DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   11569:        
                   11570:        if(GetDiskFreeSpace(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
                   11571:                info->size_of_structure = sizeof(ext_space_info_t);
                   11572:                info->structure_version = 0;
                   11573:                info->sectors_per_cluster = sectors_per_cluster;
                   11574:                info->bytes_per_sector = bytes_per_sector;
                   11575:                info->available_clusters_on_drive = free_clusters;
                   11576:                info->total_clusters_on_drive = total_clusters;
                   11577:                info->available_sectors_on_drive = sectors_per_cluster * free_clusters;
                   11578:                info->total_sectors_on_drive = sectors_per_cluster * total_clusters;
                   11579:                info->available_allocation_units = free_clusters;       // ???
                   11580:                info->total_allocation_units = total_clusters;          // ???
                   11581:        } else {
                   11582:                REG16(AX) = errno;
1.1.1.3   root     11583:                m_CF = 1;
1.1       root     11584:        }
                   11585: }
                   11586: 
1.1.1.30  root     11587: inline void msdos_int_21h_dbh()
                   11588: {
                   11589:        // Novell NetWare - Workstation - Get Number of Local Drives
                   11590:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   11591:        REG8(AL) = dos_info->last_drive;
                   11592: }
                   11593: 
                   11594: inline void msdos_int_21h_dch()
                   11595: {
                   11596:        // Novell NetWare - Connection Services - Get Connection Number
                   11597:        REG8(AL) = 0x00;
                   11598: }
                   11599: 
1.1.1.32  root     11600: inline void msdos_int_24h()
                   11601: {
                   11602:        const char *message = NULL;
                   11603:        int key = 0;
                   11604:        
                   11605:        for(int i = 0; i < array_length(critical_error_table); i++) {
                   11606:                if(critical_error_table[i].code == (REG16(DI) & 0xff) || critical_error_table[i].code == (UINT16)-1) {
                   11607:                        if(active_code_page == 932) {
                   11608:                                message = critical_error_table[i].message_japanese;
                   11609:                        }
                   11610:                        if(message == NULL) {
                   11611:                                message = critical_error_table[i].message_english;
                   11612:                        }
                   11613:                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   11614:                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   11615:                        
                   11616:                        SREG(ES) = WORK_TOP >> 4;
                   11617:                        i386_load_segment_descriptor(ES);
                   11618:                        REG16(DI) = 0x0000;
                   11619:                        break;
                   11620:                }
                   11621:        }
                   11622:        fprintf(stderr, "\n%s", message);
                   11623:        if(!(REG8(AH) & 0x80)) {
                   11624:                if(REG8(AH) & 0x01) {
                   11625:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�������ݒ� �h���C�u" : "writing drive", 'A' + REG8(AL));
                   11626:                } else {
                   11627:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�ǂݎ�蒆 �h���C�u" : "reading drive", 'A' + REG8(AL));
                   11628:                }
                   11629:        }
                   11630:        fprintf(stderr, "\n");
                   11631:        
1.1.1.33  root     11632:        {
1.1.1.32  root     11633:                fprintf(stderr, "%s",   (active_code_page == 932) ? "���~ (A)" : "Abort");
1.1.1.33  root     11634:        }
1.1.1.32  root     11635:        if(REG8(AH) & 0x10) {
                   11636:                fprintf(stderr, ", %s", (active_code_page == 932) ? "�Ď��s (R)" : "Retry");
                   11637:        }
                   11638:        if(REG8(AH) & 0x20) {
                   11639:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���� (I)" : "Ignore");
                   11640:        }
                   11641:        if(REG8(AH) & 0x08) {
                   11642:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���s (F)" : "Fail");
                   11643:        }
                   11644:        fprintf(stderr, "? ");
                   11645:        
                   11646:        while(1) {
                   11647:                while(!_kbhit()) {
                   11648:                        Sleep(10);
                   11649:                }
                   11650:                key = _getch();
                   11651:                
                   11652:                if(key == 'I' || key == 'i') {
                   11653:                        if(REG8(AH) & 0x20) {
                   11654:                                REG8(AL) = 0;
                   11655:                                break;
                   11656:                        }
                   11657:                } else if(key == 'R' || key == 'r') {
                   11658:                        if(REG8(AH) & 0x10) {
                   11659:                                REG8(AL) = 1;
                   11660:                                break;
                   11661:                        }
                   11662:                } else if(key == 'A' || key == 'a') {
                   11663:                        REG8(AL) = 2;
                   11664:                        break;
                   11665:                } else if(key == 'F' || key == 'f') {
                   11666:                        if(REG8(AH) & 0x08) {
                   11667:                                REG8(AL) = 3;
                   11668:                                break;
                   11669:                        }
                   11670:                }
                   11671:        }
                   11672:        fprintf(stderr, "%c\n", key);
                   11673: }
                   11674: 
1.1       root     11675: inline void msdos_int_25h()
                   11676: {
                   11677:        UINT16 seg, ofs;
                   11678:        DWORD dwSize;
                   11679:        
1.1.1.3   root     11680: #if defined(HAS_I386)
                   11681:        I386OP(pushf)();
                   11682: #else
                   11683:        PREFIX86(_pushf());
                   11684: #endif
1.1       root     11685:        
                   11686:        if(!(REG8(AL) < 26)) {
                   11687:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     11688:                m_CF = 1;
1.1       root     11689:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   11690:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     11691:                m_CF = 1;
1.1       root     11692:        } else {
                   11693:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   11694:                char dev[64];
                   11695:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   11696:                
                   11697:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   11698:                if(hFile == INVALID_HANDLE_VALUE) {
                   11699:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     11700:                        m_CF = 1;
1.1       root     11701:                } else {
1.1.1.19  root     11702:                        UINT32 top_sector  = REG16(DX);
                   11703:                        UINT16 sector_num  = REG16(CX);
                   11704:                        UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   11705:                        
                   11706:                        if(sector_num == 0xffff) {
                   11707:                                top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   11708:                                sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   11709:                                UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   11710:                                UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   11711:                                buffer_addr = (seg << 4) + ofs;
                   11712:                        }
                   11713: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   11714: //                             REG8(AL) = 0x02; // drive not ready
                   11715: //                             m_CF = 1;
                   11716: //                     } else 
                   11717:                        if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     11718:                                REG8(AL) = 0x08; // sector not found
1.1.1.3   root     11719:                                m_CF = 1;
1.1.1.19  root     11720:                        } else if(ReadFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     11721:                                REG8(AL) = 0x0b; // read error
1.1.1.3   root     11722:                                m_CF = 1;
1.1       root     11723:                        }
                   11724:                        CloseHandle(hFile);
                   11725:                }
                   11726:        }
                   11727: }
                   11728: 
                   11729: inline void msdos_int_26h()
                   11730: {
                   11731:        // this operation may cause serious damage for drives, so always returns error...
                   11732:        UINT16 seg, ofs;
                   11733:        DWORD dwSize;
                   11734:        
1.1.1.3   root     11735: #if defined(HAS_I386)
                   11736:        I386OP(pushf)();
                   11737: #else
                   11738:        PREFIX86(_pushf());
                   11739: #endif
1.1       root     11740:        
                   11741:        if(!(REG8(AL) < 26)) {
                   11742:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     11743:                m_CF = 1;
1.1       root     11744:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   11745:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     11746:                m_CF = 1;
1.1       root     11747:        } else {
                   11748:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   11749:                char dev[64];
                   11750:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   11751:                
                   11752:                if(dpb->media_type == 0xf8) {
                   11753:                        // this drive is not a floppy
1.1.1.6   root     11754: //                     if(!(((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag & 0x40)) {
                   11755: //                             fatalerror("This application tried the absolute disk write to drive %c:\n", 'A' + REG8(AL));
                   11756: //                     }
1.1       root     11757:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     11758:                        m_CF = 1;
1.1       root     11759:                } else {
                   11760:                        HANDLE hFile = CreateFile(dev, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   11761:                        if(hFile == INVALID_HANDLE_VALUE) {
                   11762:                                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     11763:                                m_CF = 1;
1.1       root     11764:                        } else {
1.1.1.19  root     11765:                                UINT32 top_sector  = REG16(DX);
                   11766:                                UINT16 sector_num  = REG16(CX);
                   11767:                                UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   11768:                                
                   11769:                                if(sector_num == 0xffff) {
                   11770:                                        top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   11771:                                        sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   11772:                                        UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   11773:                                        UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   11774:                                        buffer_addr = (seg << 4) + ofs;
                   11775:                                }
1.1       root     11776:                                if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   11777:                                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     11778:                                        m_CF = 1;
1.1.1.19  root     11779:                                } else if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     11780:                                        REG8(AL) = 0x08; // sector not found
1.1.1.3   root     11781:                                        m_CF = 1;
1.1.1.19  root     11782:                                } else if(WriteFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     11783:                                        REG8(AL) = 0x0a; // write error
1.1.1.3   root     11784:                                        m_CF = 1;
1.1       root     11785:                                }
                   11786:                                CloseHandle(hFile);
                   11787:                        }
                   11788:                }
                   11789:        }
                   11790: }
                   11791: 
                   11792: inline void msdos_int_27h()
                   11793: {
1.1.1.29  root     11794:        int paragraphs = (min(REG16(DX), 0xfff0) + 15) >> 4;
                   11795:        try {
                   11796:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   11797:        } catch(...) {
                   11798:                // recover the broken mcb
                   11799:                int mcb_seg = SREG(CS) - 1;
                   11800:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     11801:                
1.1.1.29  root     11802:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     11803:                        mcb->mz = 'M';
                   11804:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   11805:                        
1.1.1.29  root     11806:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39! root     11807:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29  root     11808:                        } else {
1.1.1.39! root     11809:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29  root     11810:                        }
                   11811:                } else {
                   11812:                        mcb->mz = 'Z';
1.1.1.30  root     11813:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     11814:                }
                   11815:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   11816:        }
1.1.1.3   root     11817:        msdos_process_terminate(SREG(CS), retval | 0x300, 0);
1.1       root     11818: }
                   11819: 
                   11820: inline void msdos_int_29h()
                   11821: {
1.1.1.14  root     11822: #if 1
                   11823:        // need to check escape sequences
1.1       root     11824:        msdos_putch(REG8(AL));
1.1.1.14  root     11825: #else
                   11826:        DWORD num;
                   11827:        vram_flush();
1.1.1.23  root     11828:        WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), &REG8(AL), 1, &num, NULL);
1.1.1.14  root     11829:        cursor_moved = true;
                   11830: #endif
1.1       root     11831: }
                   11832: 
                   11833: inline void msdos_int_2eh()
                   11834: {
                   11835:        char tmp[MAX_PATH], command[MAX_PATH], opt[MAX_PATH];
                   11836:        memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     11837:        strcpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
1.1       root     11838:        char *token = my_strtok(tmp, " ");
                   11839:        strcpy(command, token);
                   11840:        strcpy(opt, token + strlen(token) + 1);
                   11841:        
                   11842:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   11843:        param->env_seg = 0;
                   11844:        param->cmd_line.w.l = 44;
                   11845:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   11846:        param->fcb1.w.l = 24;
                   11847:        param->fcb1.w.h = (WORK_TOP >> 4);
                   11848:        param->fcb2.w.l = 24;
                   11849:        param->fcb2.w.h = (WORK_TOP >> 4);
                   11850:        
                   11851:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   11852:        
                   11853:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   11854:        cmd_line->len = strlen(opt);
                   11855:        strcpy(cmd_line->cmd, opt);
                   11856:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   11857:        
1.1.1.28  root     11858:        try {
                   11859:                if(msdos_process_exec(command, param, 0)) {
                   11860:                        REG16(AX) = 0xffff; // error before processing command
                   11861:                } else {
                   11862:                        // set flag to set retval to ax when the started process is terminated
                   11863:                        process_t *process = msdos_process_info_get(current_psp);
                   11864:                        process->called_by_int2eh = true;
                   11865:                }
                   11866:        } catch(...) {
                   11867:                REG16(AX) = 0xffff; // error before processing command
                   11868:        }
1.1       root     11869: }
                   11870: 
1.1.1.29  root     11871: inline void msdos_int_2fh_05h()
                   11872: {
                   11873:        switch(REG8(AL)) {
                   11874:        case 0x00:
1.1.1.32  root     11875:                REG8(AL) = 0xff;
                   11876:                break;
                   11877:        case 0x01:
                   11878:        case 0x02:
                   11879:                for(int i = 0; i < array_length(standard_error_table); i++) {
                   11880:                        if(standard_error_table[i].code == REG16(BX) || standard_error_table[i].code == (UINT16)-1) {
                   11881:                                const char *message = NULL;
                   11882:                                if(active_code_page == 932) {
                   11883:                                        message = standard_error_table[i].message_japanese;
                   11884:                                }
                   11885:                                if(message == NULL) {
                   11886:                                        message = standard_error_table[i].message_english;
                   11887:                                }
                   11888:                                strcpy((char *)(mem + WORK_TOP), message);
                   11889:                                
                   11890:                                SREG(ES) = WORK_TOP >> 4;
                   11891:                                i386_load_segment_descriptor(ES);
                   11892:                                REG16(DI) = 0x0000;
                   11893:                                REG8(AL) = 0x01;
                   11894:                                break;
                   11895:                        }
                   11896:                }
1.1.1.29  root     11897:                break;
                   11898:        default:
                   11899:                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));
                   11900:                m_CF = 1;
                   11901:        }
                   11902: }
                   11903: 
1.1.1.22  root     11904: inline void msdos_int_2fh_11h()
                   11905: {
                   11906:        switch(REG8(AL)) {
                   11907:        case 0x00:
1.1.1.29  root     11908:                if(i386_read_stack() == 0xdada) {
                   11909:                        // MSCDEX is not installed
                   11910: //                     REG8(AL) = 0x00;
                   11911:                } else {
                   11912:                        // Network Redirector is not installed
                   11913: //                     REG8(AL) = 0x00;
                   11914:                }
1.1.1.22  root     11915:                break;
                   11916:        default:
                   11917:                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     11918:                REG16(AX) = 0x49; //  network software not installed
1.1.1.22  root     11919:                m_CF = 1;
                   11920:                break;
                   11921:        }
                   11922: }
                   11923: 
1.1.1.21  root     11924: inline void msdos_int_2fh_12h()
                   11925: {
                   11926:        switch(REG8(AL)) {
1.1.1.22  root     11927:        case 0x00:
1.1.1.29  root     11928:                // DOS 3.0+ internal functions are installed
1.1.1.22  root     11929:                REG8(AL) = 0xff;
                   11930:                break;
1.1.1.29  root     11931: //     case 0x01: // DOS 3.0+ internal - Close Current File
                   11932:        case 0x02:
                   11933:                {
                   11934:                        UINT16 stack = i386_read_stack();
                   11935:                        REG16(BX) = *(UINT16 *)(mem + 4 * stack + 0);
                   11936:                        SREG(ES) = *(UINT16 *)(mem + 4 * stack + 2);
                   11937:                        i386_load_segment_descriptor(ES);
                   11938:                }
                   11939:                break;
1.1.1.30  root     11940:        case 0x03:
                   11941:                SREG(DS) = (DEVICE_TOP >> 4);
                   11942:                i386_load_segment_descriptor(DS);
                   11943:                break;
1.1.1.29  root     11944:        case 0x04:
                   11945:                {
                   11946:                        UINT16 stack = i386_read_stack();
                   11947:                        REG8(AL) = (stack == '/') ? '\\' : stack;
                   11948: #if defined(HAS_I386)
                   11949:                        m_ZF = (REG8(AL) == '\\');
                   11950: #else
                   11951:                        m_ZeroVal = (REG8(AL) != '\\');
                   11952: #endif
                   11953:                }
                   11954:                break;
                   11955:        case 0x05:
                   11956:                msdos_putch(i386_read_stack());
                   11957:                break;
                   11958: //     case 0x06: // DOS 3.0+ internal - Invole Critical Error
                   11959: //     case 0x07: // DOS 3.0+ internal - Make Disk Buffer Most Recentry Used
                   11960: //     case 0x08: // DOS 3.0+ internal - Decrement SFT Reference Count
                   11961: //     case 0x09: // DOS 3.0+ internal - Flush and FREE Disk Buffer
                   11962: //     case 0x0a: // DOS 3.0+ internal - Perform Critical Error Interrupt
                   11963: //     case 0x0b: // DOS 3.0+ internal - Signal Sharing Violation to User
                   11964: //     case 0x0c: // DOS 3.0+ internal - Open Device and Set SFT Owner/Mode
                   11965:        case 0x0d:
                   11966:                {
                   11967:                        SYSTEMTIME time;
                   11968:                        FILETIME file_time;
                   11969:                        WORD dos_date, dos_time;
                   11970:                        GetLocalTime(&time);
                   11971:                        SystemTimeToFileTime(&time, &file_time);
                   11972:                        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   11973:                        REG16(AX) = dos_date;
                   11974:                        REG16(DX) = dos_time;
                   11975:                }
                   11976:                break;
                   11977: //     case 0x0e: // DOS 3.0+ internal - Mark All Disk Buffers Unreferenced
                   11978: //     case 0x0f: // DOS 3.0+ internal - Make Buffer Most Recentry Used
                   11979: //     case 0x10: // DOS 3.0+ internal - Find Unreferenced Disk Buffer
                   11980:        case 0x11:
                   11981:                {
                   11982:                        char path[MAX_PATH], *p;
                   11983:                        strcpy(path, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   11984:                        my_strupr(path);
                   11985:                        while((p = my_strchr(path, '/')) != NULL) {
                   11986:                                *p = '\\';
                   11987:                        }
                   11988:                        strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
                   11989:                }
                   11990:                break;
                   11991:        case 0x12:
                   11992:                REG16(CX) = strlen((char *)(mem + SREG_BASE(ES) + REG16(DI)));
                   11993:                break;
                   11994:        case 0x13:
                   11995:                {
                   11996:                        char tmp[2] = {0};
                   11997:                        tmp[0] = i386_read_stack();
                   11998:                        my_strupr(tmp);
                   11999:                        REG8(AL) = tmp[0];
                   12000:                }
                   12001:                break;
                   12002:        case 0x14:
                   12003: #if defined(HAS_I386)
                   12004:                m_ZF = (SREG_BASE(DS) + REG16(SI) == SREG_BASE(ES) + REG16(DI));
                   12005: #else
                   12006:                m_ZeroVal = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   12007: #endif
                   12008:                m_CF = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   12009:                break;
                   12010: //     case 0x15: // DOS 3.0+ internal - Flush Buffer
1.1.1.21  root     12011:        case 0x16:
                   12012:                if(REG16(BX) < 20) {
                   12013:                        SREG(ES) = SFT_TOP >> 4;
                   12014:                        i386_load_segment_descriptor(ES);
                   12015:                        REG16(DI) = 6 + 0x3b * REG16(BX);
                   12016:                        
                   12017:                        // update system file table
                   12018:                        UINT8* sft = mem + SFT_TOP + 6 + 0x3b * REG16(BX);
                   12019:                        if(file_handler[REG16(BX)].valid) {
                   12020:                                int count = 0;
                   12021:                                for(int i = 0; i < 20; i++) {
                   12022:                                        if(msdos_psp_get_file_table(i, current_psp) == REG16(BX)) {
                   12023:                                                count++;
                   12024:                                        }
                   12025:                                }
                   12026:                                *(UINT16 *)(sft + 0x00) = count ? count : 0xffff;
                   12027:                                *(UINT32 *)(sft + 0x15) = _tell(REG16(BX));
                   12028:                                _lseek(REG16(BX), 0, SEEK_END);
                   12029:                                *(UINT32 *)(sft + 0x11) = _tell(REG16(BX));
                   12030:                                _lseek(REG16(BX), *(UINT32 *)(sft + 0x15), SEEK_SET);
                   12031:                        } else {
                   12032:                                memset(sft, 0, 0x3b);
                   12033:                        }
                   12034:                } else {
                   12035:                        REG16(AX) = 0x06;
                   12036:                        m_CF = 1;
                   12037:                }
                   12038:                break;
1.1.1.29  root     12039: //     case 0x17: // DOS 3.0+ internal - Get Current Directory Structure for Drive
                   12040: //     case 0x18: // DOS 3.0+ internal - Get Caller's Registers
                   12041: //     case 0x19: // DOS 3.0+ internal - Set Drive???
                   12042:        case 0x1a:
                   12043:                {
                   12044:                        char *path = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full[MAX_PATH];
                   12045:                        if(path[1] == ':') {
                   12046:                                if(path[0] >= 'a' && path[0] <= 'z') {
                   12047:                                        REG8(AL) = path[0] - 'a' + 1;
                   12048:                                } else if(path[0] >= 'A' && path[0] <= 'Z') {
                   12049:                                        REG8(AL) = path[0] - 'A' + 1;
                   12050:                                } else {
                   12051:                                        REG8(AL) = 0xff; // invalid
                   12052:                                }
                   12053:                                strcpy(full, path);
                   12054:                                strcpy(path, full + 2);
                   12055:                        } else if(GetFullPathName(path, MAX_PATH, full, NULL) != 0 && full[1] == ':') {
                   12056:                                if(full[0] >= 'a' && full[0] <= 'z') {
                   12057:                                        REG8(AL) = full[0] - 'a' + 1;
                   12058:                                } else if(full[0] >= 'A' && full[0] <= 'Z') {
                   12059:                                        REG8(AL) = full[0] - 'A' + 1;
                   12060:                                } else {
                   12061:                                        REG8(AL) = 0xff; // invalid
                   12062:                                }
                   12063:                        } else {
                   12064:                                REG8(AL) = 0x00; // default
                   12065:                        }
                   12066:                }
                   12067:                break;
                   12068:        case 0x1b:
                   12069:                {
                   12070:                        int year = REG16(CX) + 1980;
                   12071:                        REG8(AL) = ((year % 4) == 0 && (year % 100) != 0 && (year % 400) == 0) ? 29 : 28;
                   12072:                }
                   12073:                break;
                   12074: //     case 0x1c: // DOS 3.0+ internal - Check Sum Memory
                   12075: //     case 0x1d: // DOS 3.0+ internal - Sum Memory
                   12076:        case 0x1e:
                   12077:                {
                   12078:                        char *path_1st = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full_1st[MAX_PATH];
                   12079:                        char *path_2nd = (char *)(mem + SREG_BASE(ES) + REG16(DI)), full_2nd[MAX_PATH];
                   12080:                        if(GetFullPathName(path_1st, MAX_PATH, full_1st, NULL) != 0 && GetFullPathName(path_2nd, MAX_PATH, full_2nd, NULL) != 0) {
                   12081: #if defined(HAS_I386)
                   12082:                                m_ZF = (strcmp(full_1st, full_2nd) == 0);
                   12083: #else
                   12084:                                m_ZeroVal = (strcmp(full_1st, full_2nd) != 0);
                   12085: #endif
                   12086:                        } else {
                   12087: #if defined(HAS_I386)
                   12088:                                m_ZF = (strcmp(path_1st, path_2nd) == 0);
                   12089: #else
                   12090:                                m_ZeroVal = (strcmp(path_1st, path_2nd) != 0);
                   12091: #endif
                   12092:                        }
                   12093:                }
                   12094:                break;
                   12095: //     case 0x1f: // DOS 3.0+ internal - Build Current Directory Structure
1.1.1.21  root     12096:        case 0x20:
                   12097:                {
                   12098:                        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   12099:                        
                   12100:                        if(fd < 20) {
                   12101:                                SREG(ES) = current_psp;
                   12102:                                i386_load_segment_descriptor(ES);
                   12103:                                REG16(DI) = offsetof(psp_t, file_table) + fd;
                   12104:                        } else {
                   12105:                                REG16(AX) = 0x06;
                   12106:                                m_CF = 1;
                   12107:                        }
                   12108:                }
                   12109:                break;
1.1.1.29  root     12110:        case 0x21:
                   12111:                msdos_int_21h_60h(0);
                   12112:                break;
                   12113: //     case 0x22: // DOS 3.0+ internal - Set Extended Error Info
                   12114: //     case 0x23: // DOS 3.0+ internal - Check If Character Device
                   12115: //     case 0x24: // DOS 3.0+ internal - Sharing Retry Delay
                   12116:        case 0x25:
                   12117:                REG16(CX) = strlen((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   12118:                break;
                   12119:        case 0x26:
                   12120:                REG8(AL) = REG8(CL);
                   12121:                msdos_int_21h_3dh();
                   12122:                break;
                   12123:        case 0x27:
                   12124:                msdos_int_21h_3eh();
                   12125:                break;
                   12126:        case 0x28:
                   12127:                REG16(AX) = REG16(BP);
                   12128:                msdos_int_21h_42h();
                   12129:                break;
                   12130:        case 0x29:
                   12131:                msdos_int_21h_3fh();
                   12132:                break;
                   12133: //     case 0x2a: // DOS 3.0+ internal - Set Fast Open Entry Point
                   12134:        case 0x2b:
                   12135:                REG16(AX) = REG16(BP);
                   12136:                msdos_int_21h_44h();
                   12137:                break;
                   12138:        case 0x2c:
                   12139:                REG16(BX) = DEVICE_TOP >> 4;
                   12140:                REG16(AX) = 22;
                   12141:                break;
                   12142:        case 0x2d:
                   12143:                {
                   12144:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   12145:                        REG16(AX) = sda->extended_error_code;
                   12146:                }
                   12147:                break;
                   12148:        case 0x2e:
                   12149:                if(REG8(DL) == 0x00 || REG8(DL) == 0x02 || REG8(DL) == 0x04 || REG8(DL) == 0x06) {
1.1.1.32  root     12150:                        SREG(ES) = 0x0001;
                   12151:                        i386_load_segment_descriptor(ES);
                   12152:                        REG16(DI) = 0x00;
                   12153:                } else if(REG8(DL) == 0x08) {
                   12154:                        // dummy parameter error message read routine is at fffd:0010
                   12155:                        SREG(ES) = 0xfffd;
1.1.1.22  root     12156:                        i386_load_segment_descriptor(ES);
1.1.1.32  root     12157:                        REG16(DI) = 0x0010;
1.1.1.22  root     12158:                }
                   12159:                break;
1.1.1.29  root     12160:        case 0x2f:
                   12161:                if(REG16(DX) != 0) {
1.1.1.30  root     12162:                        dos_major_version = REG8(DL);
                   12163:                        dos_minor_version = REG8(DH);
1.1.1.29  root     12164:                } else {
                   12165:                        REG8(DL) = 7;
                   12166:                        REG8(DH) = 10;
                   12167:                }
                   12168:                break;
                   12169: //     case 0x30: // Windows95 - Find SFT Entry in Internal File Tables
                   12170: //     case 0x31: // Windows95 - Set/Clear "Report Windows to DOS Programs" Flag
1.1.1.22  root     12171:        default:
                   12172:                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));
                   12173:                REG16(AX) = 0x01;
                   12174:                m_CF = 1;
                   12175:                break;
                   12176:        }
                   12177: }
                   12178: 
1.1.1.30  root     12179: inline void msdos_int_2fh_13h()
                   12180: {
                   12181:        static UINT16 prevDS = 0, prevDX = 0;
                   12182:        static UINT16 prevES = 0, prevBX = 0;
                   12183:        UINT16 tmp;
                   12184:        
                   12185:        tmp = SREG(DS); SREG(DS) = prevDS; prevDS = tmp;
                   12186:        i386_load_segment_descriptor(DS);
                   12187:        tmp = REG16(DX); REG16(DX) = prevDX; prevDX = tmp;
                   12188:        
                   12189:        tmp = SREG(ES); SREG(ES) = prevES; prevES = tmp;
                   12190:        i386_load_segment_descriptor(ES);
                   12191:        tmp = REG16(BX); REG16(BX) = prevBX; prevBX = tmp;
                   12192: }
                   12193: 
1.1.1.22  root     12194: inline void msdos_int_2fh_14h()
                   12195: {
                   12196:        switch(REG8(AL)) {
                   12197:        case 0x00:
1.1.1.29  root     12198:                // NLSFUNC.COM is installed
                   12199:                REG8(AL) = 0xff;
1.1.1.25  root     12200:                break;
                   12201:        case 0x01:
                   12202:        case 0x03:
                   12203:                REG8(AL) = 0x00;
                   12204:                active_code_page = REG16(BX);
                   12205:                msdos_nls_tables_update();
                   12206:                break;
                   12207:        case 0x02:
                   12208:                REG8(AL) = get_extended_country_info(REG16(BP));
                   12209:                break;
                   12210:        case 0x04:
                   12211:                REG8(AL) = 0x00;
                   12212:                get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)));
1.1.1.22  root     12213:                break;
                   12214:        default:
                   12215:                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));
                   12216:                REG16(AX) = 0x01;
                   12217:                m_CF = 1;
                   12218:                break;
                   12219:        }
                   12220: }
                   12221: 
                   12222: inline void msdos_int_2fh_15h()
                   12223: {
                   12224:        switch(REG8(AL)) {
1.1.1.29  root     12225:        case 0x00: // CD-ROM - Installation Check
                   12226:                if(REG16(BX) == 0x0000) {
                   12227:                        // MSCDEX is not installed
                   12228: //                     REG8(AL) = 0x00;
                   12229:                } else {
                   12230:                        // GRAPHICS.COM is not installed
                   12231: //                     REG8(AL) = 0x00;
                   12232:                }
1.1.1.22  root     12233:                break;
                   12234:        case 0xff:
1.1.1.29  root     12235:                if(REG16(BX) == 0x0000) {
                   12236:                        // CORELCDX is not installed
                   12237:                } else {
                   12238:                        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));
                   12239:                        REG16(AX) = 0x01;
                   12240:                        m_CF = 1;
                   12241:                }
1.1.1.22  root     12242:                break;
1.1.1.21  root     12243:        default:
1.1.1.22  root     12244:                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     12245:                REG16(AX) = 0x01;
                   12246:                m_CF = 1;
                   12247:                break;
                   12248:        }
                   12249: }
                   12250: 
1.1       root     12251: inline void msdos_int_2fh_16h()
                   12252: {
                   12253:        switch(REG8(AL)) {
                   12254:        case 0x00:
1.1.1.14  root     12255:                if(no_windows) {
1.1.1.29  root     12256:                        // neither Windows 3.x enhanced mode nor Windows/386 2.x running
                   12257: //                     REG8(AL) = 0x00;
1.1.1.14  root     12258:                } else {
1.1.1.30  root     12259:                        REG8(AL) = win_major_version;
                   12260:                        REG8(AH) = win_minor_version;
1.1       root     12261:                }
                   12262:                break;
1.1.1.30  root     12263:        case 0x05:
                   12264:                // from DOSBox
                   12265:                i386_set_a20_line(1);
                   12266:                break;
1.1.1.22  root     12267:        case 0x0a:
                   12268:                if(!no_windows) {
                   12269:                        REG16(AX) = 0x0000;
1.1.1.30  root     12270:                        REG8(BH) = win_major_version;
                   12271:                        REG8(BL) = win_minor_version;
1.1.1.22  root     12272:                        REG16(CX) = 0x0003; // enhanced
                   12273:                }
                   12274:                break;
1.1.1.30  root     12275:        case 0x0b:
                   12276:                // no TRS, keep ES:DI = 0000h:0000h
1.1.1.22  root     12277:        case 0x0e:
                   12278:        case 0x0f:
1.1.1.30  root     12279:        case 0x10:
1.1.1.22  root     12280:        case 0x11:
                   12281:        case 0x12:
                   12282:        case 0x13:
                   12283:        case 0x14:
1.1.1.30  root     12284:        case 0x15:
1.1.1.33  root     12285:        case 0x86:
1.1.1.22  root     12286:        case 0x87:
1.1.1.30  root     12287:        case 0x89:
1.1.1.33  root     12288:        case 0x8a:
1.1.1.22  root     12289:                // function not supported, do not clear AX
                   12290:                break;
1.1.1.14  root     12291:        case 0x80:
                   12292:                Sleep(10);
1.1.1.35  root     12293:                REQUEST_HARDWRE_UPDATE();
1.1.1.29  root     12294:                REG8(AL) = 0x00;
1.1.1.14  root     12295:                break;
1.1.1.33  root     12296:        case 0x83:
                   12297:                REG16(BX) = 0x01; // system vm id
                   12298:                break;
1.1.1.22  root     12299:        case 0x8e:
                   12300:                REG16(AX) = 0x00; // failed
                   12301:                break;
1.1.1.20  root     12302:        case 0x8f:
                   12303:                switch(REG8(DH)) {
                   12304:                case 0x00:
                   12305:                case 0x02:
                   12306:                case 0x03:
                   12307:                        REG16(AX) = 0x00;
                   12308:                        break;
                   12309:                case 0x01:
                   12310:                        REG16(AX) = 0x168f;
                   12311:                        break;
                   12312:                }
                   12313:                break;
1.1       root     12314:        default:
1.1.1.22  root     12315:                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));
                   12316:                REG16(AX) = 0x01;
                   12317:                m_CF = 1;
                   12318:                break;
                   12319:        }
                   12320: }
                   12321: 
                   12322: inline void msdos_int_2fh_19h()
                   12323: {
                   12324:        switch(REG8(AL)) {
                   12325:        case 0x00:
1.1.1.29  root     12326:                // SHELLB.COM is not installed
                   12327: //             REG8(AL) = 0x00;
1.1.1.22  root     12328:                break;
                   12329:        case 0x01:
                   12330:        case 0x02:
                   12331:        case 0x03:
                   12332:        case 0x04:
                   12333:                REG16(AX) = 0x01;
                   12334:                m_CF = 1;
                   12335:                break;
1.1.1.29  root     12336:        case 0x80:
                   12337:                // IBM ROM-DOS v4.0 is not installed
                   12338: //             REG8(AL) = 0x00;
                   12339:                break;
1.1.1.22  root     12340:        default:
                   12341:                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     12342:                REG16(AX) = 0x01;
1.1.1.3   root     12343:                m_CF = 1;
1.1       root     12344:                break;
                   12345:        }
                   12346: }
                   12347: 
                   12348: inline void msdos_int_2fh_1ah()
                   12349: {
                   12350:        switch(REG8(AL)) {
                   12351:        case 0x00:
1.1.1.29  root     12352:                // ANSI.SYS is installed
1.1       root     12353:                REG8(AL) = 0xff;
                   12354:                break;
                   12355:        default:
1.1.1.22  root     12356:                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));
                   12357:                REG16(AX) = 0x01;
                   12358:                m_CF = 1;
                   12359:                break;
                   12360:        }
                   12361: }
                   12362: 
1.1.1.30  root     12363: inline void msdos_int_2fh_40h()
1.1.1.22  root     12364: {
                   12365:        switch(REG8(AL)) {
                   12366:        case 0x00:
1.1.1.30  root     12367:                // Windows 3+ - Get Virtual Device Driver (VDD) Capabilities
                   12368:                REG8(AL) = 0x01; // does not virtualize video access
1.1.1.22  root     12369:                break;
                   12370:        default:
                   12371:                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     12372:                REG16(AX) = 0x01;
1.1.1.3   root     12373:                m_CF = 1;
1.1       root     12374:                break;
                   12375:        }
                   12376: }
                   12377: 
                   12378: inline void msdos_int_2fh_43h()
                   12379: {
                   12380:        switch(REG8(AL)) {
                   12381:        case 0x00:
1.1.1.29  root     12382:                // XMS is installed ?
1.1.1.19  root     12383: #ifdef SUPPORT_XMS
                   12384:                if(support_xms) {
                   12385:                        REG8(AL) = 0x80;
                   12386:                } else
                   12387: #endif
                   12388:                REG8(AL) = 0x00;
                   12389:                break;
                   12390:        case 0x10:
                   12391:                SREG(ES) = XMS_TOP >> 4;
                   12392:                i386_load_segment_descriptor(ES);
1.1.1.26  root     12393:                REG16(BX) = 0x15;
1.1       root     12394:                break;
                   12395:        default:
1.1.1.22  root     12396:                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));
                   12397:                REG16(AX) = 0x01;
                   12398:                m_CF = 1;
                   12399:                break;
                   12400:        }
                   12401: }
                   12402: 
                   12403: inline void msdos_int_2fh_46h()
                   12404: {
                   12405:        switch(REG8(AL)) {
                   12406:        case 0x80:
1.1.1.29  root     12407:                // Windows v3.0 is not installed
                   12408: //             REG8(AL) = 0x00;
1.1.1.22  root     12409:                break;
                   12410:        default:
                   12411:                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));
                   12412:                REG16(AX) = 0x01;
                   12413:                m_CF = 1;
                   12414:                break;
                   12415:        }
                   12416: }
                   12417: 
                   12418: inline void msdos_int_2fh_48h()
                   12419: {
                   12420:        switch(REG8(AL)) {
                   12421:        case 0x00:
1.1.1.29  root     12422:                // DOSKEY is not installed
                   12423: //             REG8(AL) = 0x00;
1.1.1.22  root     12424:                break;
                   12425:        case 0x10:
                   12426:                msdos_int_21h_0ah();
                   12427:                REG16(AX) = 0x00;
                   12428:                break;
                   12429:        default:
                   12430:                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     12431:                REG16(AX) = 0x01;
1.1.1.3   root     12432:                m_CF = 1;
1.1       root     12433:                break;
                   12434:        }
                   12435: }
                   12436: 
                   12437: inline void msdos_int_2fh_4ah()
                   12438: {
                   12439:        switch(REG8(AL)) {
1.1.1.29  root     12440: #ifdef SUPPORT_HMA
                   12441:        case 0x01: // DOS 5.0+ - Query Free HMA Space
                   12442:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   12443:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   12444:                                // restore first free mcb in high memory area
                   12445:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   12446:                        }
                   12447:                        int offset = 0xffff;
                   12448:                        if((REG16(BX) = msdos_hma_mem_get_free(&offset)) != 0) {
                   12449:                                REG16(DI) = offset + 0x10;
                   12450:                        } else {
                   12451:                                REG16(DI) = 0xffff;
                   12452:                        }
                   12453:                } else {
                   12454:                        // HMA is already used
                   12455:                        REG16(BX) = 0;
                   12456:                        REG16(DI) = 0xffff;
                   12457:                }
                   12458:                SREG(ES) = 0xffff;
                   12459:                i386_load_segment_descriptor(ES);
                   12460:                break;
                   12461:        case 0x02: // DOS 5.0+ - Allocate HMA Space
                   12462:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   12463:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   12464:                                // restore first free mcb in high memory area
                   12465:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   12466:                        }
                   12467:                        int size = REG16(BX), offset;
                   12468:                        if((size % 16) != 0) {
                   12469:                                size &= ~15;
                   12470:                                size += 16;
                   12471:                        }
                   12472:                        if((offset = msdos_hma_mem_alloc(size, current_psp)) != -1) {
                   12473:                                REG16(BX) = size;
                   12474:                                REG16(DI) = offset + 0x10;
                   12475:                                is_hma_used_by_int_2fh = true;
                   12476:                        } else {
                   12477:                                REG16(BX) = 0;
                   12478:                                REG16(DI) = 0xffff;
                   12479:                        }
                   12480:                } else {
                   12481:                        // HMA is already used
                   12482:                        REG16(BX) = 0;
                   12483:                        REG16(DI) = 0xffff;
                   12484:                }
                   12485:                SREG(ES) = 0xffff;
                   12486:                i386_load_segment_descriptor(ES);
                   12487:                break;
                   12488:        case 0x03: // Windows95 - (De)Allocate HMA Memory Block
                   12489:                if(REG8(DL) == 0x00) {
                   12490:                        if(!is_hma_used_by_xms) {
                   12491:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   12492:                                        // restore first free mcb in high memory area
                   12493:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   12494:                                        is_hma_used_by_int_2fh = false;
                   12495:                                }
                   12496:                                int size = REG16(BX), offset;
                   12497:                                if((size % 16) != 0) {
                   12498:                                        size &= ~15;
                   12499:                                        size += 16;
                   12500:                                }
                   12501:                                if((offset = msdos_hma_mem_alloc(size, REG16(CX))) != -1) {
                   12502: //                                     REG16(BX) = size;
                   12503:                                        SREG(ES) = 0xffff;
                   12504:                                        i386_load_segment_descriptor(ES);
                   12505:                                        REG16(DI) = offset + 0x10;
                   12506:                                        is_hma_used_by_int_2fh = true;
                   12507:                                } else {
                   12508:                                        REG16(DI) = 0xffff;
                   12509:                                }
                   12510:                        } else {
                   12511:                                REG16(DI) = 0xffff;
                   12512:                        }
                   12513:                } else if(REG8(DL) == 0x01) {
                   12514:                        if(!is_hma_used_by_xms) {
                   12515:                                int size = REG16(BX);
                   12516:                                if((size % 16) != 0) {
                   12517:                                        size &= ~15;
                   12518:                                        size += 16;
                   12519:                                }
                   12520:                                if(msdos_hma_mem_realloc(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10, size) != -1) {
                   12521:                                        // memory block address is not changed
                   12522:                                } else {
                   12523:                                        REG16(DI) = 0xffff;
                   12524:                                }
                   12525:                        } else {
                   12526:                                REG16(DI) = 0xffff;
                   12527:                        }
                   12528:                } else if(REG8(DL) == 0x02) {
                   12529:                        if(!is_hma_used_by_xms) {
                   12530:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   12531:                                        // restore first free mcb in high memory area
                   12532:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   12533:                                        is_hma_used_by_int_2fh = false;
                   12534:                                } else {
                   12535:                                        msdos_hma_mem_free(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10);
                   12536:                                        if(msdos_hma_mem_get_free(NULL) == 0xffe0) {
                   12537:                                                is_hma_used_by_int_2fh = false;
                   12538:                                        }
                   12539:                                }
                   12540:                        }
                   12541:                } else {
                   12542:                        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));
                   12543:                        REG16(AX) = 0x01;
                   12544:                        m_CF = 1;
                   12545:                }
                   12546:                break;
                   12547:        case 0x04: // Windows95 - Get Start of HMA Memory Chain
                   12548:                if(!is_hma_used_by_xms) {
                   12549:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   12550:                                // restore first free mcb in high memory area
                   12551:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   12552:                                is_hma_used_by_int_2fh = false;
                   12553:                        }
                   12554:                        REG16(AX) = 0x0000;
                   12555:                        SREG(ES) = 0xffff;
                   12556:                        i386_load_segment_descriptor(ES);
                   12557:                        REG16(DI) = 0x10;
                   12558:                }
                   12559:                break;
                   12560: #else
1.1       root     12561:        case 0x01:
                   12562:        case 0x02:
1.1.1.29  root     12563:                // HMA is already used
1.1.1.27  root     12564:                REG16(BX) = 0x0000;
1.1.1.3   root     12565:                SREG(ES) = 0xffff;
                   12566:                i386_load_segment_descriptor(ES);
1.1       root     12567:                REG16(DI) = 0xffff;
                   12568:                break;
1.1.1.19  root     12569:        case 0x03:
                   12570:                // unable to allocate
                   12571:                REG16(DI) = 0xffff;
                   12572:                break;
                   12573:        case 0x04:
                   12574:                // function not supported, do not clear AX
                   12575:                break;
1.1.1.29  root     12576: #endif
                   12577:        case 0x10:
                   12578:                if(REG16(BX) == 0x0000) {
                   12579:                        // SMARTDRV is not installed
                   12580:                } else {
                   12581:                        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));
                   12582:                        REG16(AX) = 0x01;
                   12583:                        m_CF = 1;
                   12584:                }
                   12585:                break;
                   12586:        case 0x11:
                   12587:                if(REG16(BX) == 0x0000) {
                   12588:                        // DBLSPACE.BIN is not installed
                   12589:                } else {
                   12590:                        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));
                   12591:                        REG16(AX) = 0x01;
                   12592:                        m_CF = 1;
                   12593:                }
1.1.1.22  root     12594:                break;
                   12595:        default:
                   12596:                unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   12597:                REG16(AX) = 0x01;
                   12598:                m_CF = 1;
                   12599:                break;
                   12600:        }
                   12601: }
                   12602: 
                   12603: inline void msdos_int_2fh_4bh()
                   12604: {
                   12605:        switch(REG8(AL)) {
1.1.1.24  root     12606:        case 0x01:
1.1.1.22  root     12607:        case 0x02:
1.1.1.29  root     12608:                // Task Switcher is not installed
1.1.1.24  root     12609:                break;
                   12610:        case 0x03:
                   12611:                // this call is available from within DOSSHELL even if the task switcher is not installed
                   12612:                REG16(AX) = REG16(BX) = 0x0000; // no more avaiable switcher id
1.1.1.22  root     12613:                break;
1.1.1.30  root     12614:        case 0x04:
                   12615:                REG16(BX) = 0x0000; // free switcher id successfully
                   12616:                break;
1.1       root     12617:        default:
1.1.1.22  root     12618:                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     12619:                REG16(AX) = 0x01;
1.1.1.3   root     12620:                m_CF = 1;
1.1       root     12621:                break;
                   12622:        }
                   12623: }
                   12624: 
                   12625: inline void msdos_int_2fh_4fh()
                   12626: {
                   12627:        switch(REG8(AL)) {
                   12628:        case 0x00:
1.1.1.29  root     12629:                // BILING is installed
1.1.1.27  root     12630:                REG16(AX) = 0x0000;
                   12631:                REG8(DL) = 0x01;        // major version
                   12632:                REG8(DH) = 0x00;        // minor version
1.1       root     12633:                break;
                   12634:        case 0x01:
1.1.1.27  root     12635:                REG16(AX) = 0x0000;
1.1       root     12636:                REG16(BX) = active_code_page;
                   12637:                break;
                   12638:        default:
1.1.1.22  root     12639:                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));
                   12640:                REG16(AX) = 0x01;
                   12641:                m_CF = 1;
                   12642:                break;
                   12643:        }
                   12644: }
                   12645: 
                   12646: inline void msdos_int_2fh_55h()
                   12647: {
                   12648:        switch(REG8(AL)) {
                   12649:        case 0x00:
                   12650:        case 0x01:
                   12651: //             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));
                   12652:                break;
                   12653:        default:
                   12654:                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     12655:                REG16(AX) = 0x01;
1.1.1.3   root     12656:                m_CF = 1;
1.1       root     12657:                break;
                   12658:        }
                   12659: }
                   12660: 
1.1.1.24  root     12661: inline void msdos_int_2fh_adh()
                   12662: {
                   12663:        switch(REG8(AL)) {
                   12664:        case 0x00:
1.1.1.29  root     12665:                // DISPLAY.SYS is installed
1.1.1.24  root     12666:                REG8(AL) = 0xff;
                   12667:                REG16(BX) = 0x100; // ???
                   12668:                break;
                   12669:        case 0x01:
                   12670:                active_code_page = REG16(BX);
                   12671:                msdos_nls_tables_update();
                   12672:                REG16(AX) = 0x01;
                   12673:                break;
                   12674:        case 0x02:
                   12675:                REG16(BX) = active_code_page;
                   12676:                break;
                   12677:        case 0x03:
                   12678:                // FIXME
                   12679:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 1;
                   12680:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2) = 3;
                   12681:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4) = 1;
                   12682:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 6) = active_code_page;
                   12683:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 8) = active_code_page;
                   12684:                break;
                   12685:        case 0x80:
                   12686:                break; // keyb.com is not installed
                   12687:        default:
                   12688:                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));
                   12689:                REG16(AX) = 0x01;
                   12690:                m_CF = 1;
                   12691:                break;
                   12692:        }
                   12693: }
                   12694: 
1.1       root     12695: inline void msdos_int_2fh_aeh()
                   12696: {
                   12697:        switch(REG8(AL)) {
                   12698:        case 0x00:
1.1.1.28  root     12699:                // FIXME: we need to check the given command line
                   12700:                REG8(AL) = 0x00; // the command should be executed as usual
                   12701: //             REG8(AL) = 0xff; // this command is a TSR extension to COMMAND.COM
1.1       root     12702:                break;
                   12703:        case 0x01:
                   12704:                {
                   12705:                        char command[MAX_PATH];
                   12706:                        memset(command, 0, sizeof(command));
1.1.1.3   root     12707:                        memcpy(command, mem + SREG_BASE(DS) + REG16(SI) + 1, mem[SREG_BASE(DS) + REG16(SI)]);
1.1       root     12708:                        
                   12709:                        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   12710:                        param->env_seg = 0;
                   12711:                        param->cmd_line.w.l = 44;
                   12712:                        param->cmd_line.w.h = (WORK_TOP >> 4);
                   12713:                        param->fcb1.w.l = 24;
                   12714:                        param->fcb1.w.h = (WORK_TOP >> 4);
                   12715:                        param->fcb2.w.l = 24;
                   12716:                        param->fcb2.w.h = (WORK_TOP >> 4);
                   12717:                        
                   12718:                        memset(mem + WORK_TOP + 24, 0x20, 20);
                   12719:                        
                   12720:                        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
1.1.1.3   root     12721:                        cmd_line->len = mem[SREG_BASE(DS) + REG16(BX) + 1];
                   12722:                        memcpy(cmd_line->cmd, mem + SREG_BASE(DS) + REG16(BX) + 2, cmd_line->len);
1.1       root     12723:                        cmd_line->cmd[cmd_line->len] = 0x0d;
                   12724:                        
1.1.1.28  root     12725:                        try {
                   12726:                                msdos_process_exec(command, param, 0);
                   12727:                        } catch(...) {
                   12728:                                fatalerror("failed to start '%s' by int 2Fh, AX=AE01h\n", command);
1.1       root     12729:                        }
                   12730:                }
                   12731:                break;
                   12732:        default:
1.1.1.22  root     12733:                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     12734:                REG16(AX) = 0x01;
1.1.1.3   root     12735:                m_CF = 1;
1.1       root     12736:                break;
                   12737:        }
                   12738: }
                   12739: 
1.1.1.34  root     12740: inline void msdos_int_2fh_b7h()
                   12741: {
                   12742:        switch(REG8(AL)) {
                   12743:        case 0x00:
                   12744:                // APPEND is not installed
                   12745: //             REG8(AL) = 0x00;
                   12746:                break;
                   12747:        case 0x07:
                   12748:                // COMMAND.COM calls this service without checking APPEND is installed
                   12749:                break;
                   12750:        default:
                   12751:                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));
                   12752:                REG16(AX) = 0x01;
                   12753:                m_CF = 1;
                   12754:                break;
                   12755:        }
                   12756: }
                   12757: 
1.1.1.24  root     12758: inline void msdos_int_33h_0000h()
                   12759: {
                   12760:        REG16(AX) = 0xffff; // hardware/driver installed
                   12761:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   12762: }
                   12763: 
                   12764: inline void msdos_int_33h_0001h()
                   12765: {
1.1.1.34  root     12766:        if(mouse.hidden > 0) {
                   12767:                mouse.hidden--;
                   12768:        }
                   12769:        if(mouse.hidden == 0) {
1.1.1.24  root     12770:                if(!(dwConsoleMode & ENABLE_MOUSE_INPUT)) {
                   12771:                        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode | ENABLE_MOUSE_INPUT);
                   12772:                }
                   12773:                pic[1].imr &= ~0x10;    // enable irq12
                   12774:        }
                   12775: }
                   12776: 
                   12777: inline void msdos_int_33h_0002h()
                   12778: {
1.1.1.34  root     12779:        mouse.hidden++;
                   12780: //     SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode & ~ENABLE_MOUSE_INPUT);
                   12781:        pic[1].imr |= 0x10;     // enable irq12
1.1.1.24  root     12782: }
                   12783: 
                   12784: inline void msdos_int_33h_0003h()
                   12785: {
1.1.1.34  root     12786: //     if(mouse.hidden > 0) {
                   12787:                update_console_input();
                   12788: //     }
1.1.1.24  root     12789:        REG16(BX) = mouse.get_buttons();
1.1.1.34  root     12790:        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   12791:        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
                   12792: }
                   12793: 
                   12794: inline void msdos_int_33h_0004h()
                   12795: {
                   12796:        mouse.position.x = REG16(CX);
                   12797:        mouse.position.x = REG16(DX);
1.1.1.24  root     12798: }
                   12799: 
                   12800: inline void msdos_int_33h_0005h()
                   12801: {
1.1.1.34  root     12802: //     if(mouse.hidden > 0) {
                   12803:                update_console_input();
                   12804: //     }
1.1.1.24  root     12805:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   12806:                int idx = REG16(BX);
1.1.1.34  root     12807:                REG16(BX) = min(mouse.buttons[idx].pressed_times, 0x7fff);
                   12808:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].pressed_position.x));
                   12809:                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     12810:                mouse.buttons[idx].pressed_times = 0;
                   12811:        } else {
                   12812:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   12813:        }
                   12814:        REG16(AX) = mouse.get_buttons();
                   12815: }
                   12816: 
                   12817: inline void msdos_int_33h_0006h()
                   12818: {
1.1.1.34  root     12819: //     if(mouse.hidden > 0) {
                   12820:                update_console_input();
                   12821: //     }
1.1.1.24  root     12822:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   12823:                int idx = REG16(BX);
1.1.1.34  root     12824:                REG16(BX) = min(mouse.buttons[idx].released_times, 0x7fff);
                   12825:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].released_position.x));
                   12826:                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     12827:                mouse.buttons[idx].released_times = 0;
                   12828:        } else {
                   12829:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   12830:        }
                   12831:        REG16(AX) = mouse.get_buttons();
                   12832: }
                   12833: 
                   12834: inline void msdos_int_33h_0007h()
                   12835: {
                   12836:        mouse.min_position.x = min(REG16(CX), REG16(DX));
                   12837:        mouse.max_position.x = max(REG16(CX), REG16(DX));
                   12838: }
                   12839: 
                   12840: inline void msdos_int_33h_0008h()
                   12841: {
                   12842:        mouse.min_position.y = min(REG16(CX), REG16(DX));
                   12843:        mouse.max_position.y = max(REG16(CX), REG16(DX));
                   12844: }
                   12845: 
                   12846: inline void msdos_int_33h_0009h()
                   12847: {
                   12848:        mouse.hot_spot[0] = REG16(BX);
                   12849:        mouse.hot_spot[1] = REG16(CX);
                   12850: }
                   12851: 
                   12852: inline void msdos_int_33h_000bh()
                   12853: {
1.1.1.34  root     12854: //     if(mouse.hidden > 0) {
                   12855:                update_console_input();
                   12856: //     }
1.1.1.24  root     12857:        int dx = (mouse.position.x - mouse.prev_position.x) * mouse.mickey.x / 8;
                   12858:        int dy = (mouse.position.y - mouse.prev_position.y) * mouse.mickey.y / 8;
                   12859:        mouse.prev_position.x = mouse.position.x;
                   12860:        mouse.prev_position.y = mouse.position.y;
                   12861:        REG16(CX) = dx;
                   12862:        REG16(DX) = dy;
                   12863: }
                   12864: 
                   12865: inline void msdos_int_33h_000ch()
                   12866: {
                   12867:        mouse.call_mask = REG16(CX);
                   12868:        mouse.call_addr.w.l = REG16(DX);
                   12869:        mouse.call_addr.w.h = SREG(ES);
                   12870: }
                   12871: 
                   12872: inline void msdos_int_33h_000fh()
                   12873: {
                   12874:        mouse.mickey.x = REG16(CX);
                   12875:        mouse.mickey.y = REG16(DX);
                   12876: }
                   12877: 
                   12878: inline void msdos_int_33h_0011h()
                   12879: {
                   12880:        REG16(AX) = 0xffff;
                   12881:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   12882: }
                   12883: 
                   12884: inline void msdos_int_33h_0014h()
                   12885: {
                   12886:        UINT16 old_mask = mouse.call_mask;
                   12887:        UINT16 old_ofs = mouse.call_addr.w.l;
                   12888:        UINT16 old_seg = mouse.call_addr.w.h;
                   12889:        
                   12890:        mouse.call_mask = REG16(CX);
                   12891:        mouse.call_addr.w.l = REG16(DX);
                   12892:        mouse.call_addr.w.h = SREG(ES);
                   12893:        
                   12894:        REG16(CX) = old_mask;
                   12895:        REG16(DX) = old_ofs;
                   12896:        SREG(ES) = old_seg;
                   12897:        i386_load_segment_descriptor(ES);
                   12898: }
                   12899: 
                   12900: inline void msdos_int_33h_0015h()
                   12901: {
                   12902:        REG16(BX) = sizeof(mouse);
                   12903: }
                   12904: 
                   12905: inline void msdos_int_33h_0016h()
                   12906: {
                   12907:        memcpy(mem + SREG_BASE(ES) + REG16(DX), &mouse, sizeof(mouse));
                   12908: }
                   12909: 
                   12910: inline void msdos_int_33h_0017h()
                   12911: {
                   12912:        memcpy(&mouse, mem + SREG_BASE(ES) + REG16(DX), sizeof(mouse));
                   12913: }
                   12914: 
                   12915: inline void msdos_int_33h_001ah()
                   12916: {
                   12917:        mouse.sensitivity[0] = REG16(BX);
                   12918:        mouse.sensitivity[1] = REG16(CX);
                   12919:        mouse.sensitivity[2] = REG16(DX);
                   12920: }
                   12921: 
                   12922: inline void msdos_int_33h_001bh()
                   12923: {
                   12924:        REG16(BX) = mouse.sensitivity[0];
                   12925:        REG16(CX) = mouse.sensitivity[1];
                   12926:        REG16(DX) = mouse.sensitivity[2];
                   12927: }
                   12928: 
                   12929: inline void msdos_int_33h_001dh()
                   12930: {
                   12931:        mouse.display_page = REG16(BX);
                   12932: }
                   12933: 
                   12934: inline void msdos_int_33h_001eh()
                   12935: {
                   12936:        REG16(BX) = mouse.display_page;
                   12937: }
                   12938: 
1.1.1.34  root     12939: inline void msdos_int_33h_001fh()
                   12940: {
                   12941:        // from DOSBox
                   12942:        REG16(BX) = 0x0000;
                   12943:        SREG(ES) = 0x0000;
                   12944:        i386_load_segment_descriptor(ES);
                   12945:        mouse.enabled = false;
                   12946:        mouse.old_hidden = mouse.hidden;
                   12947:        mouse.hidden = 1;
                   12948: }
                   12949: 
                   12950: inline void msdos_int_33h_0020h()
                   12951: {
                   12952:        // from DOSBox
                   12953:        mouse.enabled = true;
                   12954:        mouse.hidden = mouse.old_hidden;
                   12955: }
                   12956: 
1.1.1.24  root     12957: inline void msdos_int_33h_0021h()
                   12958: {
                   12959:        REG16(AX) = 0xffff;
                   12960:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   12961: }
                   12962: 
                   12963: inline void msdos_int_33h_0022h()
                   12964: {
                   12965:        mouse.language = REG16(BX);
                   12966: }
                   12967: 
                   12968: inline void msdos_int_33h_0023h()
                   12969: {
                   12970:        REG16(BX) = mouse.language;
                   12971: }
                   12972: 
                   12973: inline void msdos_int_33h_0024h()
                   12974: {
                   12975:        REG16(BX) = 0x0805; // V8.05
                   12976:        REG16(CX) = 0x0400; // PS/2
                   12977: }
                   12978: 
                   12979: inline void msdos_int_33h_0026h()
                   12980: {
                   12981:        REG16(BX) = 0x0000;
                   12982:        REG16(CX) = mouse.max_position.x;
                   12983:        REG16(DX) = mouse.max_position.y;
                   12984: }
                   12985: 
                   12986: inline void msdos_int_33h_002ah()
                   12987: {
1.1.1.34  root     12988:        REG16(AX) = -mouse.hidden;
1.1.1.24  root     12989:        REG16(BX) = mouse.hot_spot[0];
                   12990:        REG16(CX) = mouse.hot_spot[1];
                   12991:        REG16(DX) = 4; // PS/2
                   12992: }
                   12993: 
                   12994: inline void msdos_int_33h_0031h()
                   12995: {
                   12996:        REG16(AX) = mouse.min_position.x;
                   12997:        REG16(BX) = mouse.min_position.y;
                   12998:        REG16(CX) = mouse.max_position.x;
                   12999:        REG16(DX) = mouse.max_position.y;
                   13000: }
                   13001: 
                   13002: inline void msdos_int_33h_0032h()
                   13003: {
                   13004:        REG16(AX) = 0;
                   13005: //     REG16(AX) |= 0x8000; // 0025h
                   13006:        REG16(AX) |= 0x4000; // 0026h
                   13007: //     REG16(AX) |= 0x2000; // 0027h
                   13008: //     REG16(AX) |= 0x1000; // 0028h
                   13009: //     REG16(AX) |= 0x0800; // 0029h
                   13010:        REG16(AX) |= 0x0400; // 002ah
                   13011: //     REG16(AX) |= 0x0200; // 002bh
                   13012: //     REG16(AX) |= 0x0100; // 002ch
                   13013: //     REG16(AX) |= 0x0080; // 002dh
                   13014: //     REG16(AX) |= 0x0040; // 002eh
                   13015:        REG16(AX) |= 0x0020; // 002fh
                   13016: //     REG16(AX) |= 0x0010; // 0030h
                   13017:        REG16(AX) |= 0x0008; // 0031h
                   13018:        REG16(AX) |= 0x0004; // 0032h
                   13019: //     REG16(AX) |= 0x0002; // 0033h
                   13020: //     REG16(AX) |= 0x0001; // 0034h
                   13021: }
                   13022: 
1.1.1.19  root     13023: inline void msdos_int_67h_40h()
                   13024: {
                   13025:        if(!support_ems) {
                   13026:                REG8(AH) = 0x84;
                   13027:        } else {
                   13028:                REG8(AH) = 0x00;
                   13029:        }
                   13030: }
                   13031: 
                   13032: inline void msdos_int_67h_41h()
                   13033: {
                   13034:        if(!support_ems) {
                   13035:                REG8(AH) = 0x84;
                   13036:        } else {
                   13037:                REG8(AH) = 0x00;
                   13038:                REG16(BX) = EMS_TOP >> 4;
                   13039:        }
                   13040: }
                   13041: 
                   13042: inline void msdos_int_67h_42h()
                   13043: {
                   13044:        if(!support_ems) {
                   13045:                REG8(AH) = 0x84;
                   13046:        } else {
                   13047:                REG8(AH) = 0x00;
                   13048:                REG16(BX) = free_ems_pages;
                   13049:                REG16(DX) = MAX_EMS_PAGES;
                   13050:        }
                   13051: }
                   13052: 
                   13053: inline void msdos_int_67h_43h()
                   13054: {
                   13055:        if(!support_ems) {
                   13056:                REG8(AH) = 0x84;
                   13057:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   13058:                REG8(AH) = 0x87;
                   13059:        } else if(REG16(BX) > free_ems_pages) {
                   13060:                REG8(AH) = 0x88;
                   13061:        } else if(REG16(BX) == 0) {
                   13062:                REG8(AH) = 0x89;
                   13063:        } else {
1.1.1.31  root     13064:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     13065:                        if(!ems_handles[i].allocated) {
                   13066:                                ems_allocate_pages(i, REG16(BX));
                   13067:                                REG8(AH) = 0x00;
                   13068:                                REG16(DX) = i;
                   13069:                                return;
                   13070:                        }
                   13071:                }
                   13072:                REG8(AH) = 0x85;
                   13073:        }
                   13074: }
                   13075: 
                   13076: inline void msdos_int_67h_44h()
                   13077: {
                   13078:        if(!support_ems) {
                   13079:                REG8(AH) = 0x84;
1.1.1.31  root     13080:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     13081:                REG8(AH) = 0x83;
                   13082:        } else if(!(REG16(BX) == 0xffff || REG16(BX) < ems_handles[REG16(DX)].pages)) {
                   13083:                REG8(AH) = 0x8a;
                   13084: //     } else if(!(REG8(AL) < 4)) {
                   13085: //             REG8(AH) = 0x8b;
                   13086:        } else if(REG16(BX) == 0xffff) {
                   13087:                ems_unmap_page(REG8(AL) & 3);
                   13088:                REG8(AH) = 0x00;
                   13089:        } else {
                   13090:                ems_map_page(REG8(AL) & 3, REG16(DX), REG16(BX));
                   13091:                REG8(AH) = 0x00;
                   13092:        }
                   13093: }
                   13094: 
                   13095: inline void msdos_int_67h_45h()
                   13096: {
                   13097:        if(!support_ems) {
                   13098:                REG8(AH) = 0x84;
1.1.1.31  root     13099:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     13100:                REG8(AH) = 0x83;
                   13101:        } else {
                   13102:                ems_release_pages(REG16(DX));
                   13103:                REG8(AH) = 0x00;
                   13104:        }
                   13105: }
                   13106: 
                   13107: inline void msdos_int_67h_46h()
                   13108: {
                   13109:        if(!support_ems) {
                   13110:                REG8(AH) = 0x84;
                   13111:        } else {
1.1.1.29  root     13112: //             REG16(AX) = 0x0032; // EMS 3.2
                   13113:                REG16(AX) = 0x0040; // EMS 4.0
1.1.1.19  root     13114:        }
                   13115: }
                   13116: 
                   13117: inline void msdos_int_67h_47h()
                   13118: {
                   13119:        // NOTE: the map data should be stored in the specified ems page, not process data
                   13120:        process_t *process = msdos_process_info_get(current_psp);
                   13121:        
                   13122:        if(!support_ems) {
                   13123:                REG8(AH) = 0x84;
1.1.1.31  root     13124: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     13125: //             REG8(AH) = 0x83;
                   13126:        } else if(process->ems_pages_stored) {
                   13127:                REG8(AH) = 0x8d;
                   13128:        } else {
                   13129:                for(int i = 0; i < 4; i++) {
                   13130:                        process->ems_pages[i].handle = ems_pages[i].handle;
                   13131:                        process->ems_pages[i].page   = ems_pages[i].page;
                   13132:                        process->ems_pages[i].mapped = ems_pages[i].mapped;
                   13133:                }
                   13134:                process->ems_pages_stored = true;
                   13135:                REG8(AH) = 0x00;
                   13136:        }
                   13137: }
                   13138: 
                   13139: inline void msdos_int_67h_48h()
                   13140: {
                   13141:        // NOTE: the map data should be restored from the specified ems page, not process data
                   13142:        process_t *process = msdos_process_info_get(current_psp);
                   13143:        
                   13144:        if(!support_ems) {
                   13145:                REG8(AH) = 0x84;
1.1.1.31  root     13146: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     13147: //             REG8(AH) = 0x83;
                   13148:        } else if(!process->ems_pages_stored) {
                   13149:                REG8(AH) = 0x8e;
                   13150:        } else {
                   13151:                for(int i = 0; i < 4; i++) {
                   13152:                        if(process->ems_pages[i].mapped) {
                   13153:                                ems_map_page(i, process->ems_pages[i].handle, process->ems_pages[i].page);
                   13154:                        } else {
                   13155:                                ems_unmap_page(i);
                   13156:                        }
                   13157:                }
                   13158:                process->ems_pages_stored = false;
                   13159:                REG8(AH) = 0x00;
                   13160:        }
                   13161: }
                   13162: 
                   13163: inline void msdos_int_67h_4bh()
                   13164: {
                   13165:        if(!support_ems) {
                   13166:                REG8(AH) = 0x84;
                   13167:        } else {
                   13168:                REG8(AH) = 0x00;
                   13169:                REG16(BX) = 0;
1.1.1.31  root     13170:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     13171:                        if(ems_handles[i].allocated) {
                   13172:                                REG16(BX)++;
                   13173:                        }
                   13174:                }
                   13175:        }
                   13176: }
                   13177: 
                   13178: inline void msdos_int_67h_4ch()
                   13179: {
                   13180:        if(!support_ems) {
                   13181:                REG8(AH) = 0x84;
1.1.1.31  root     13182:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     13183:                REG8(AH) = 0x83;
                   13184:        } else {
                   13185:                REG8(AH) = 0x00;
                   13186:                REG16(BX) = ems_handles[REG16(DX)].pages;
                   13187:        }
                   13188: }
                   13189: 
                   13190: inline void msdos_int_67h_4dh()
                   13191: {
                   13192:        if(!support_ems) {
                   13193:                REG8(AH) = 0x84;
                   13194:        } else {
                   13195:                REG8(AH) = 0x00;
                   13196:                REG16(BX) = 0;
1.1.1.31  root     13197:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     13198:                        if(ems_handles[i].allocated) {
                   13199:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 0) = i;
                   13200:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 2) = ems_handles[i].pages;
                   13201:                                REG16(BX)++;
                   13202:                        }
                   13203:                }
                   13204:        }
                   13205: }
                   13206: 
1.1.1.20  root     13207: inline void msdos_int_67h_4eh()
                   13208: {
                   13209:        if(!support_ems) {
                   13210:                REG8(AH) = 0x84;
                   13211:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   13212:                if(REG8(AL) == 0x00 || REG8(AL) == 0x02) {
                   13213:                        // save page map
                   13214:                        for(int i = 0; i < 4; i++) {
                   13215:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   13216:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   13217:                        }
                   13218:                }
                   13219:                if(REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   13220:                        // restore page map
                   13221:                        for(int i = 0; i < 4; i++) {
                   13222:                                UINT16 handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   13223:                                UINT16 page   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   13224:                                
1.1.1.31  root     13225:                                if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
1.1.1.20  root     13226:                                        ems_map_page(i, handle, page);
                   13227:                                } else {
                   13228:                                        ems_unmap_page(i);
                   13229:                                }
                   13230:                        }
                   13231:                }
                   13232:                REG8(AH) = 0x00;
                   13233:        } else if(REG8(AL) == 0x03) {
                   13234:                REG8(AH) = 0x00;
1.1.1.21  root     13235:                REG8(AL) = 4 * 4;
                   13236:        } else {
1.1.1.22  root     13237:                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     13238:                REG8(AH) = 0x8f;
                   13239:        }
                   13240: }
                   13241: 
                   13242: inline void msdos_int_67h_4fh()
                   13243: {
                   13244:        if(!support_ems) {
                   13245:                REG8(AH) = 0x84;
                   13246:        } else if(REG8(AL) == 0x00) {
                   13247:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   13248:                
                   13249:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI)) = count;
                   13250:                for(int i = 0; i < count; i++) {
                   13251:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 2 * i);
                   13252:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   13253:                        
                   13254: //                     if(!(physical < 4)) {
                   13255: //                             REG8(AH) = 0x8b;
                   13256: //                             return;
                   13257: //                     }
                   13258:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 0) = segment;
                   13259:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 2) = ems_pages[physical & 3].handle;
                   13260:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 4) = ems_pages[physical & 3].mapped ? ems_pages[physical & 3].page : 0xffff;
                   13261:                }
                   13262:                REG8(AH) = 0x00;
                   13263:        } else if(REG8(AL) == 0x01) {
                   13264:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   13265:                
                   13266:                for(int i = 0; i < count; i++) {
                   13267:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 0);
                   13268:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   13269:                        UINT16 handle   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 2);
                   13270:                        UINT16 logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 4);
                   13271:                        
                   13272: //                     if(!(physical < 4)) {
                   13273: //                             REG8(AH) = 0x8b;
                   13274: //                             return;
                   13275: //                     } else
1.1.1.31  root     13276:                        if(!(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated)) {
1.1.1.21  root     13277:                                REG8(AH) = 0x83;
                   13278:                                return;
                   13279:                        } else if(logical == 0xffff) {
                   13280:                                ems_unmap_page(physical & 3);
                   13281:                        } else if(logical < ems_handles[handle].pages) {
                   13282:                                ems_map_page(physical & 3, handle, logical);
                   13283:                        } else {
                   13284:                                REG8(AH) = 0x8a;
                   13285:                                return;
                   13286:                        }
                   13287:                }
                   13288:                REG8(AH) = 0x00;
                   13289:        } else if(REG8(AL) == 0x02) {
                   13290:                REG8(AH) = 0x00;
                   13291:                REG8(AL) = 2 + REG16(BX) * 6;
1.1.1.20  root     13292:        } else {
1.1.1.22  root     13293:                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     13294:                REG8(AH) = 0x8f;
                   13295:        }
                   13296: }
                   13297: 
                   13298: inline void msdos_int_67h_50h()
                   13299: {
                   13300:        if(!support_ems) {
                   13301:                REG8(AH) = 0x84;
1.1.1.31  root     13302:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.20  root     13303:                REG8(AH) = 0x83;
                   13304:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   13305:                for(int i = 0; i < REG16(CX); i++) {
                   13306:                        int logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   13307:                        int physical = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   13308:                        
                   13309:                        if(REG8(AL) == 0x01) {
                   13310:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   13311:                        }
                   13312: //                     if(!(physical < 4)) {
                   13313: //                             REG8(AH) = 0x8b;
                   13314: //                             return;
                   13315: //                     } else
                   13316:                        if(logical == 0xffff) {
                   13317:                                ems_unmap_page(physical & 3);
                   13318:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   13319:                                ems_map_page(physical & 3, REG16(DX), logical);
                   13320:                        } else {
                   13321:                                REG8(AH) = 0x8a;
                   13322:                                return;
                   13323:                        }
                   13324:                }
                   13325:                REG8(AH) = 0x00;
                   13326:        } else {
1.1.1.22  root     13327:                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     13328:                REG8(AH) = 0x8f;
                   13329:        }
                   13330: }
                   13331: 
1.1.1.19  root     13332: inline void msdos_int_67h_51h()
                   13333: {
                   13334:        if(!support_ems) {
                   13335:                REG8(AH) = 0x84;
1.1.1.31  root     13336:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     13337:                REG8(AH) = 0x83;
                   13338:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   13339:                REG8(AH) = 0x87;
                   13340:        } else if(REG16(BX) > free_ems_pages + ems_handles[REG16(DX)].pages) {
                   13341:                REG8(AH) = 0x88;
                   13342:        } else {
                   13343:                ems_reallocate_pages(REG16(DX), REG16(BX));
                   13344:                REG8(AH) = 0x00;
                   13345:        }
                   13346: }
                   13347: 
1.1.1.20  root     13348: inline void msdos_int_67h_52h()
                   13349: {
                   13350:        if(!support_ems) {
                   13351:                REG8(AH) = 0x84;
1.1.1.31  root     13352: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   13353: //             REG8(AH) = 0x83;
1.1.1.20  root     13354:        } else if(REG8(AL) == 0x00) {
                   13355:                REG8(AL) = 0x00; // handle is volatile
                   13356:                REG8(AH) = 0x00;
                   13357:        } else if(REG8(AL) == 0x01) {
                   13358:                if(REG8(BL) == 0x00) {
                   13359:                        REG8(AH) = 0x00;
                   13360:                } else {
                   13361:                        REG8(AH) = 0x90; // undefined attribute type
                   13362:                }
                   13363:        } else if(REG8(AL) == 0x02) {
                   13364:                REG8(AL) = 0x00; // only volatile handles supported
                   13365:                REG8(AH) = 0x00;
                   13366:        } else {
1.1.1.22  root     13367:                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     13368:                REG8(AH) = 0x8f;
                   13369:        }
                   13370: }
                   13371: 
1.1.1.19  root     13372: inline void msdos_int_67h_53h()
                   13373: {
                   13374:        if(!support_ems) {
                   13375:                REG8(AH) = 0x84;
1.1.1.31  root     13376:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     13377:                REG8(AH) = 0x83;
                   13378:        } else if(REG8(AL) == 0x00) {
                   13379:                memcpy(mem + SREG_BASE(ES) + REG16(DI), ems_handles[REG16(DX)].name, 8);
                   13380:                REG8(AH) = 0x00;
                   13381:        } else if(REG8(AL) == 0x01) {
1.1.1.31  root     13382:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     13383:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   13384:                                REG8(AH) = 0xa1;
                   13385:                                return;
                   13386:                        }
                   13387:                }
                   13388:                REG8(AH) = 0x00;
                   13389:                memcpy(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8);
                   13390:        } else {
1.1.1.22  root     13391:                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     13392:                REG8(AH) = 0x8f;
1.1.1.19  root     13393:        }
                   13394: }
                   13395: 
                   13396: inline void msdos_int_67h_54h()
                   13397: {
                   13398:        if(!support_ems) {
                   13399:                REG8(AH) = 0x84;
                   13400:        } else if(REG8(AL) == 0x00) {
1.1.1.31  root     13401:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     13402:                        if(ems_handles[i].allocated) {
                   13403:                                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, ems_handles[i].name, 10);
                   13404:                        } else {
                   13405:                                memset(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, 0, 10);
                   13406:                        }
                   13407:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 0) = i;
                   13408:                }
                   13409:                REG8(AH) = 0x00;
                   13410:                REG8(AL) = MAX_EMS_HANDLES;
                   13411:        } else if(REG8(AL) == 0x01) {
                   13412:                REG8(AH) = 0xa0; // not found
1.1.1.31  root     13413:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     13414:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   13415:                                REG8(AH) = 0x00;
                   13416:                                REG16(DX) = i;
                   13417:                                break;
                   13418:                        }
                   13419:                }
                   13420:        } else if(REG8(AL) == 0x02) {
                   13421:                REG8(AH) = 0x00;
                   13422:                REG16(BX) = MAX_EMS_HANDLES;
                   13423:        } else {
1.1.1.22  root     13424:                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     13425:                REG8(AH) = 0x8f;
                   13426:        }
                   13427: }
                   13428: 
                   13429: inline void msdos_int_67h_57h_tmp()
                   13430: {
                   13431:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   13432:        UINT8  src_type    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   13433:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x05);
                   13434:        UINT16 src_ofs     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07);
                   13435:        UINT16 src_seg     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x09);
                   13436:        UINT8  dest_type   = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0b);
                   13437:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   13438:        UINT16 dest_ofs    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0e);
                   13439:        UINT16 dest_seg    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x10);
                   13440:        
1.1.1.32  root     13441:        UINT8 *src_buffer = NULL, *dest_buffer = NULL;
1.1.1.20  root     13442:        UINT32 src_addr, dest_addr;
                   13443:        UINT32 src_addr_max, dest_addr_max;
                   13444:        
                   13445:        if(src_type == 0) {
                   13446:                src_buffer = mem;
                   13447:                src_addr = (src_seg << 4) + src_ofs;
                   13448:                src_addr_max = MAX_MEM;
                   13449:        } else {
1.1.1.31  root     13450:                if(!(src_handle >= 1 && src_handle <= MAX_EMS_HANDLES && ems_handles[src_handle].allocated)) {
1.1.1.20  root     13451:                        REG8(AH) = 0x83;
                   13452:                        return;
                   13453:                } else if(!(src_seg < ems_handles[src_handle].pages)) {
                   13454:                        REG8(AH) = 0x8a;
                   13455:                        return;
                   13456:                }
1.1.1.32  root     13457:                if(ems_handles[src_handle].buffer != NULL) {
                   13458:                        src_buffer = ems_handles[src_handle].buffer + 0x4000 * src_seg;
                   13459:                }
1.1.1.20  root     13460:                src_addr = src_ofs;
1.1.1.32  root     13461:                src_addr_max = 0x4000 * (ems_handles[src_handle].pages - src_seg);
1.1.1.20  root     13462:        }
                   13463:        if(dest_type == 0) {
                   13464:                dest_buffer = mem;
                   13465:                dest_addr = (dest_seg << 4) + dest_ofs;
                   13466:                dest_addr_max = MAX_MEM;
                   13467:        } else {
1.1.1.31  root     13468:                if(!(dest_handle >= 1 && dest_handle <= MAX_EMS_HANDLES && ems_handles[dest_handle].allocated)) {
1.1.1.20  root     13469:                        REG8(AH) = 0x83;
                   13470:                        return;
                   13471:                } else if(!(dest_seg < ems_handles[dest_handle].pages)) {
                   13472:                        REG8(AH) = 0x8a;
                   13473:                        return;
                   13474:                }
1.1.1.32  root     13475:                if(ems_handles[dest_handle].buffer != NULL) {
                   13476:                        dest_buffer = ems_handles[dest_handle].buffer + 0x4000 * dest_seg;
                   13477:                }
1.1.1.20  root     13478:                dest_addr = dest_ofs;
1.1.1.32  root     13479:                dest_addr_max = 0x4000 * (ems_handles[dest_handle].pages - dest_seg);
1.1.1.20  root     13480:        }
1.1.1.32  root     13481:        if(src_buffer != NULL && dest_buffer != NULL) {
                   13482:                for(int i = 0; i < copy_length; i++) {
                   13483:                        if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   13484:                                if(REG8(AL) == 0x00) {
                   13485:                                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   13486:                                } else if(REG8(AL) == 0x01) {
                   13487:                                        UINT8 tmp = dest_buffer[dest_addr];
                   13488:                                        dest_buffer[dest_addr++] = src_buffer[src_addr];
                   13489:                                        src_buffer[src_addr++] = tmp;
                   13490:                                }
                   13491:                        } else {
                   13492:                                REG8(AH) = 0x93;
                   13493:                                return;
1.1.1.20  root     13494:                        }
                   13495:                }
1.1.1.32  root     13496:                REG8(AH) = 0x00;
                   13497:        } else {
                   13498:                REG8(AH) = 0x80;
1.1.1.20  root     13499:        }
                   13500: }
                   13501: 
                   13502: inline void msdos_int_67h_57h()
                   13503: {
                   13504:        if(!support_ems) {
                   13505:                REG8(AH) = 0x84;
                   13506:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   13507:                struct {
                   13508:                        UINT16 handle;
                   13509:                        UINT16 page;
                   13510:                        bool mapped;
                   13511:                } tmp_pages[4];
                   13512:                
                   13513:                // unmap pages to copy memory data to ems buffer
                   13514:                for(int i = 0; i < 4; i++) {
                   13515:                        tmp_pages[i].handle = ems_pages[i].handle;
                   13516:                        tmp_pages[i].page   = ems_pages[i].page;
                   13517:                        tmp_pages[i].mapped = ems_pages[i].mapped;
                   13518:                        ems_unmap_page(i);
                   13519:                }
                   13520:                
                   13521:                // run move/exchange operation
                   13522:                msdos_int_67h_57h_tmp();
                   13523:                
                   13524:                // restore unmapped pages
                   13525:                for(int i = 0; i < 4; i++) {
                   13526:                        if(tmp_pages[i].mapped) {
                   13527:                                ems_map_page(i, tmp_pages[i].handle, tmp_pages[i].page);
                   13528:                        }
                   13529:                }
                   13530:        } else {
1.1.1.22  root     13531:                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     13532:                REG8(AH) = 0x8f;
                   13533:        }
                   13534: }
                   13535: 
                   13536: inline void msdos_int_67h_58h()
                   13537: {
                   13538:        if(!support_ems) {
                   13539:                REG8(AH) = 0x84;
                   13540:        } else if(REG8(AL) == 0x00) {
                   13541:                for(int i = 0; i < 4; i++) {
1.1.1.30  root     13542:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = (EMS_TOP + 0x4000 * i) >> 4;
                   13543:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = i;
1.1.1.20  root     13544:                }
                   13545:                REG8(AH) = 0x00;
                   13546:                REG16(CX) = 4;
                   13547:        } else if(REG8(AL) == 0x01) {
                   13548:                REG8(AH) = 0x00;
                   13549:                REG16(CX) = 4;
                   13550:        } else {
1.1.1.22  root     13551:                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     13552:                REG8(AH) = 0x8f;
                   13553:        }
                   13554: }
                   13555: 
                   13556: inline void msdos_int_67h_5ah()
                   13557: {
                   13558:        if(!support_ems) {
1.1.1.19  root     13559:                REG8(AH) = 0x84;
1.1.1.20  root     13560:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   13561:                REG8(AH) = 0x87;
                   13562:        } else if(REG16(BX) > free_ems_pages) {
                   13563:                REG8(AH) = 0x88;
                   13564: //     } else if(REG16(BX) == 0) {
                   13565: //             REG8(AH) = 0x89;
                   13566:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
1.1.1.31  root     13567:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.20  root     13568:                        if(!ems_handles[i].allocated) {
                   13569:                                ems_allocate_pages(i, REG16(BX));
                   13570:                                REG8(AH) = 0x00;
                   13571:                                REG16(DX) = i;
                   13572:                                return;
                   13573:                        }
                   13574:                }
                   13575:                REG8(AH) = 0x85;
                   13576:        } else {
1.1.1.22  root     13577:                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     13578:                REG8(AH) = 0x8f;
1.1.1.19  root     13579:        }
                   13580: }
                   13581: 
1.1.1.30  root     13582: inline void msdos_int_67h_deh()
                   13583: {
                   13584:        REG8(AH) = 0x84;
                   13585: }
                   13586: 
1.1.1.19  root     13587: #ifdef SUPPORT_XMS
                   13588: 
1.1.1.32  root     13589: void msdos_xms_init()
1.1.1.26  root     13590: {
1.1.1.30  root     13591:        emb_handle_top = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   13592:        emb_handle_top->address = EMB_TOP;
                   13593:        emb_handle_top->size_kb = (EMB_END - EMB_TOP) >> 10;
1.1.1.26  root     13594:        xms_a20_local_enb_count = 0;
                   13595: }
                   13596: 
1.1.1.32  root     13597: void msdos_xms_finish()
                   13598: {
                   13599:        msdos_xms_release();
                   13600: }
                   13601: 
                   13602: void msdos_xms_release()
1.1.1.30  root     13603: {
                   13604:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL;) {
                   13605:                emb_handle_t *next_handle = emb_handle->next;
                   13606:                free(emb_handle);
                   13607:                emb_handle = next_handle;
                   13608:        }
                   13609: }
                   13610: 
                   13611: emb_handle_t *msdos_xms_get_emb_handle(int handle)
                   13612: {
                   13613:        if(handle != 0) {
                   13614:                for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   13615:                        if(emb_handle->handle == handle) {
                   13616:                                return(emb_handle);
                   13617:                        }
                   13618:                }
                   13619:        }
                   13620:        return(NULL);
                   13621: }
                   13622: 
                   13623: int msdos_xms_get_unused_emb_handle_id()
                   13624: {
                   13625:        for(int handle = 1;; handle++) {
                   13626:                if(msdos_xms_get_emb_handle(handle) == NULL) {
                   13627:                        return(handle);
                   13628:                }
                   13629:        }
                   13630:        return(0);
                   13631: }
                   13632: 
                   13633: int msdos_xms_get_unused_emb_handle_count()
                   13634: {
                   13635:        int count = 64; //255;
                   13636:        
                   13637:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   13638:                if(emb_handle->handle != 0) {
                   13639:                        if(--count == 1) {
                   13640:                                break;
                   13641:                        }
                   13642:                }
                   13643:        }
                   13644:        return(count);
                   13645: }
                   13646: 
                   13647: void msdos_xms_split_emb_handle(emb_handle_t *emb_handle, int size_kb)
                   13648: {
                   13649:        if(emb_handle->size_kb > size_kb) {
                   13650:                emb_handle_t *new_handle = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   13651:                
                   13652:                new_handle->address = emb_handle->address + size_kb * 1024;
                   13653:                new_handle->size_kb = emb_handle->size_kb - size_kb;
                   13654:                emb_handle->size_kb = size_kb;
                   13655:                
                   13656:                new_handle->prev = emb_handle;
                   13657:                new_handle->next = emb_handle->next;
                   13658:                if(emb_handle->next != NULL) {
                   13659:                        emb_handle->next->prev = new_handle;
                   13660:                }
                   13661:                emb_handle->next = new_handle;
                   13662:        }
                   13663: }
                   13664: 
                   13665: void msdos_xms_combine_emb_handles(emb_handle_t *emb_handle)
                   13666: {
                   13667:        emb_handle_t *next_handle = emb_handle->next;
                   13668:        
                   13669:        if(next_handle != NULL) {
                   13670:                emb_handle->size_kb += next_handle->size_kb;
                   13671:                
                   13672:                if(next_handle->next != NULL) {
                   13673:                        next_handle->next->prev = emb_handle;
                   13674:                }
                   13675:                emb_handle->next = next_handle->next;
                   13676:                free(next_handle);
                   13677:        }
                   13678: }
                   13679: 
                   13680: emb_handle_t *msdos_xms_alloc_emb_handle(int size_kb)
                   13681: {
                   13682:        emb_handle_t *target_handle = NULL;
                   13683:        
                   13684:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   13685:                if(emb_handle->handle == 0 && emb_handle->size_kb >= size_kb) {
                   13686:                        if(target_handle == NULL || target_handle->size_kb > emb_handle->size_kb) {
                   13687:                                target_handle = emb_handle;
                   13688:                        }
                   13689:                }
                   13690:        }
                   13691:        if(target_handle != NULL) {
                   13692:                if(target_handle->size_kb > size_kb) {
                   13693:                        msdos_xms_split_emb_handle(target_handle, size_kb);
                   13694:                }
                   13695: //             target_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   13696:                return(target_handle);
                   13697:        }
                   13698:        return(NULL);
                   13699: }
                   13700: 
                   13701: void msdos_xms_free_emb_handle(emb_handle_t *emb_handle)
                   13702: {
                   13703:        emb_handle_t *prev_handle = emb_handle->prev;
                   13704:        emb_handle_t *next_handle = emb_handle->next;
                   13705:        
                   13706:        if(prev_handle != NULL && prev_handle->handle == 0) {
                   13707:                msdos_xms_combine_emb_handles(prev_handle);
                   13708:                emb_handle = prev_handle;
                   13709:        }
                   13710:        if(next_handle != NULL && next_handle->handle == 0) {
                   13711:                msdos_xms_combine_emb_handles(emb_handle);
                   13712:        }
                   13713:        emb_handle->handle = 0;
                   13714: }
                   13715: 
1.1.1.19  root     13716: inline void msdos_call_xms_00h()
                   13717: {
1.1.1.29  root     13718: #if defined(HAS_I386)
                   13719:        REG16(AX) = 0x0300; // V3.00 (XMS Version)
                   13720:        REG16(BX) = 0x035f; // V3.95 (Driver Revision)
                   13721: #else
                   13722:        REG16(AX) = 0x0200; // V2.00 (XMS Version)
                   13723:        REG16(BX) = 0x0270; // V2.70 (Driver Revision)
                   13724: #endif
                   13725: //     REG16(DX) = 0x0000; // HMA does not exist
                   13726:        REG16(DX) = 0x0001; // HMA does exist
1.1.1.19  root     13727: }
                   13728: 
                   13729: inline void msdos_call_xms_01h()
                   13730: {
1.1.1.29  root     13731:        if(REG8(AL) == 0x40) {
                   13732:                // HIMEM.SYS will fail function 01h with error code 91h if AL=40h and
                   13733:                // DX=KB free extended memory returned by last call of function 08h
                   13734:                REG16(AX) = 0x0000;
                   13735:                REG8(BL) = 0x91;
                   13736:                REG16(DX) = xms_dx_after_call_08h;
                   13737:        } else if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   13738:                REG16(AX) = 0x0000;
                   13739:                REG8(BL) = 0x81; // Vdisk was detected
                   13740: #ifdef SUPPORT_HMA
                   13741:        } else if(is_hma_used_by_int_2fh) {
                   13742:                REG16(AX) = 0x0000;
                   13743:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   13744:        } else if(is_hma_used_by_xms) {
                   13745:                REG16(AX) = 0x0000;
                   13746:                REG8(BL) = 0x91; // HMA is already in use
                   13747:        } else {
                   13748:                REG16(AX) = 0x0001;
                   13749:                is_hma_used_by_xms = true;
                   13750: #else
                   13751:        } else {
                   13752:                REG16(AX) = 0x0000;
                   13753:                REG8(BL) = 0x91; // HMA is already in use
                   13754: #endif
                   13755:        }
1.1.1.19  root     13756: }
                   13757: 
                   13758: inline void msdos_call_xms_02h()
                   13759: {
1.1.1.29  root     13760:        if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   13761:                REG16(AX) = 0x0000;
                   13762:                REG8(BL) = 0x81; // Vdisk was detected
                   13763: #ifdef SUPPORT_HMA
                   13764:        } else if(is_hma_used_by_int_2fh) {
                   13765:                REG16(AX) = 0x0000;
                   13766:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   13767:        } else if(!is_hma_used_by_xms) {
                   13768:                REG16(AX) = 0x0000;
                   13769:                REG8(BL) = 0x93; // HMA is not allocated
                   13770:        } else {
                   13771:                REG16(AX) = 0x0001;
                   13772:                is_hma_used_by_xms = false;
                   13773:                // restore first free mcb in high memory area
                   13774:                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   13775: #else
                   13776:        } else {
                   13777:                REG16(AX) = 0x0000;
                   13778:                REG8(BL) = 0x91; // HMA is already in use
                   13779: #endif
                   13780:        }
1.1.1.19  root     13781: }
                   13782: 
                   13783: inline void msdos_call_xms_03h()
                   13784: {
                   13785:        i386_set_a20_line(1);
                   13786:        REG16(AX) = 0x0001;
                   13787:        REG8(BL) = 0x00;
                   13788: }
                   13789: 
                   13790: inline void msdos_call_xms_04h()
                   13791: {
1.1.1.21  root     13792:        i386_set_a20_line(0);
                   13793:        REG16(AX) = 0x0001;
                   13794:        REG8(BL) = 0x00;
1.1.1.19  root     13795: }
                   13796: 
                   13797: inline void msdos_call_xms_05h()
                   13798: {
                   13799:        i386_set_a20_line(1);
                   13800:        REG16(AX) = 0x0001;
                   13801:        REG8(BL) = 0x00;
1.1.1.21  root     13802:        xms_a20_local_enb_count++;
1.1.1.19  root     13803: }
                   13804: 
                   13805: void msdos_call_xms_06h()
                   13806: {
1.1.1.21  root     13807:        if(xms_a20_local_enb_count > 0) {
                   13808:                xms_a20_local_enb_count--;
                   13809:        }
                   13810:        if(xms_a20_local_enb_count == 0) {
1.1.1.19  root     13811:                i386_set_a20_line(0);
1.1.1.21  root     13812:        }
                   13813:        if((m_a20_mask >> 20) & 1) {
1.1.1.19  root     13814:                REG16(AX) = 0x0000;
                   13815:                REG8(BL) = 0x94;
1.1.1.21  root     13816:        } else {
                   13817:                REG16(AX) = 0x0001;
                   13818:                REG8(BL) = 0x00;
1.1.1.19  root     13819:        }
                   13820: }
                   13821: 
                   13822: inline void msdos_call_xms_07h()
                   13823: {
                   13824:        REG16(AX) = (m_a20_mask >> 20) & 1;
                   13825:        REG8(BL) = 0x00;
                   13826: }
                   13827: 
                   13828: inline void msdos_call_xms_08h()
                   13829: {
                   13830:        REG16(AX) = REG16(DX) = 0x0000;
                   13831:        
1.1.1.30  root     13832:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   13833:                if(emb_handle->handle == 0) {
                   13834:                        if(REG16(AX) < emb_handle->size_kb) {
                   13835:                                REG16(AX) = emb_handle->size_kb;
1.1.1.19  root     13836:                        }
1.1.1.30  root     13837:                        REG16(DX) += emb_handle->size_kb;
1.1.1.19  root     13838:                }
                   13839:        }
                   13840:        
                   13841:        if(REG16(AX) == 0 && REG16(DX) == 0) {
                   13842:                REG8(BL) = 0xa0;
                   13843:        } else {
                   13844:                REG8(BL) = 0x00;
                   13845:        }
1.1.1.29  root     13846:        xms_dx_after_call_08h = REG16(DX);
1.1.1.19  root     13847: }
                   13848: 
1.1.1.30  root     13849: void msdos_call_xms_09h(int size_kb)
1.1.1.19  root     13850: {
1.1.1.30  root     13851:        emb_handle_t *emb_handle = msdos_xms_alloc_emb_handle(size_kb);
                   13852:        
                   13853:        if(emb_handle != NULL) {
                   13854:                emb_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   13855:                
                   13856:                REG16(AX) = 0x0001;
                   13857:                REG16(DX) = emb_handle->handle;
                   13858:                REG8(BL) = 0x00;
                   13859:        } else {
                   13860:                REG16(AX) = REG16(DX) = 0x0000;
                   13861:                REG8(BL) = 0xa0;
1.1.1.19  root     13862:        }
1.1.1.30  root     13863: }
                   13864: 
                   13865: inline void msdos_call_xms_09h()
                   13866: {
                   13867:        msdos_call_xms_09h(REG16(DX));
1.1.1.19  root     13868: }
                   13869: 
                   13870: inline void msdos_call_xms_0ah()
                   13871: {
1.1.1.30  root     13872:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   13873:        
                   13874:        if(emb_handle == NULL) {
1.1.1.19  root     13875:                REG16(AX) = 0x0000;
                   13876:                REG8(BL) = 0xa2;
1.1.1.30  root     13877:        } else if(emb_handle->lock > 0) {
1.1.1.19  root     13878:                REG16(AX) = 0x0000;
                   13879:                REG8(BL) = 0xab;
                   13880:        } else {
1.1.1.30  root     13881:                msdos_xms_free_emb_handle(emb_handle);
1.1.1.19  root     13882:                
                   13883:                REG16(AX) = 0x0001;
                   13884:                REG8(BL) = 0x00;
                   13885:        }
                   13886: }
                   13887: 
                   13888: inline void msdos_call_xms_0bh()
                   13889: {
                   13890:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   13891:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   13892:        UINT32 src_addr    = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06);
                   13893:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0a);
                   13894:        UINT32 dest_addr   = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   13895:        
                   13896:        UINT8 *src_buffer, *dest_buffer;
                   13897:        UINT32 src_addr_max, dest_addr_max;
1.1.1.30  root     13898:        emb_handle_t *emb_handle;
1.1.1.19  root     13899:        
                   13900:        if(src_handle == 0) {
                   13901:                src_buffer = mem;
                   13902:                src_addr = (((src_addr >> 16) & 0xffff) << 4) + (src_addr & 0xffff);
                   13903:                src_addr_max = MAX_MEM;
1.1.1.30  root     13904: 
1.1.1.19  root     13905:        } else {
1.1.1.30  root     13906:                if((emb_handle = msdos_xms_get_emb_handle(src_handle)) == NULL) {
1.1.1.19  root     13907:                        REG16(AX) = 0x0000;
                   13908:                        REG8(BL) = 0xa3;
                   13909:                        return;
                   13910:                }
1.1.1.30  root     13911:                src_buffer = mem + emb_handle->address;
                   13912:                src_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     13913:        }
                   13914:        if(dest_handle == 0) {
                   13915:                dest_buffer = mem;
                   13916:                dest_addr = (((dest_addr >> 16) & 0xffff) << 4) + (dest_addr & 0xffff);
                   13917:                dest_addr_max = MAX_MEM;
                   13918:        } else {
1.1.1.30  root     13919:                if((emb_handle = msdos_xms_get_emb_handle(dest_handle)) == NULL) {
1.1.1.19  root     13920:                        REG16(AX) = 0x0000;
                   13921:                        REG8(BL) = 0xa5;
                   13922:                        return;
                   13923:                }
1.1.1.30  root     13924:                dest_buffer = mem + emb_handle->address;
                   13925:                dest_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     13926:        }
                   13927:        for(int i = 0; i < copy_length; i++) {
                   13928:                if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   13929:                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   13930:                } else {
                   13931:                        break;
                   13932:                }
                   13933:        }
                   13934:        REG16(AX) = 0x0001;
                   13935:        REG8(BL) = 0x00;
                   13936: }
                   13937: 
                   13938: inline void msdos_call_xms_0ch()
                   13939: {
1.1.1.30  root     13940:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   13941:        
                   13942:        if(emb_handle == NULL) {
1.1.1.19  root     13943:                REG16(AX) = 0x0000;
                   13944:                REG8(BL) = 0xa2;
                   13945:        } else {
1.1.1.30  root     13946:                emb_handle->lock++;
1.1.1.19  root     13947:                REG16(AX) = 0x0001;
                   13948:                REG8(BL) = 0x00;
1.1.1.30  root     13949:                REG16(DX) = (emb_handle->address >> 16) & 0xffff;
                   13950:                REG16(BX) = (emb_handle->address      ) & 0xffff;
1.1.1.19  root     13951:        }
                   13952: }
                   13953: 
                   13954: inline void msdos_call_xms_0dh()
                   13955: {
1.1.1.30  root     13956:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   13957:        
                   13958:        if(emb_handle == NULL) {
1.1.1.19  root     13959:                REG16(AX) = 0x0000;
                   13960:                REG8(BL) = 0xa2;
1.1.1.30  root     13961:        } else if(!(emb_handle->lock > 0)) {
1.1.1.19  root     13962:                REG16(AX) = 0x0000;
                   13963:                REG8(BL) = 0xaa;
                   13964:        } else {
1.1.1.30  root     13965:                emb_handle->lock--;
1.1.1.19  root     13966:                REG16(AX) = 0x0001;
                   13967:                REG8(BL) = 0x00;
                   13968:        }
                   13969: }
                   13970: 
                   13971: inline void msdos_call_xms_0eh()
                   13972: {
1.1.1.30  root     13973:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   13974:        
                   13975:        if(emb_handle == NULL) {
1.1.1.19  root     13976:                REG16(AX) = 0x0000;
                   13977:                REG8(BL) = 0xa2;
                   13978:        } else {
                   13979:                REG16(AX) = 0x0001;
1.1.1.30  root     13980:                REG8(BH) = emb_handle->lock;
                   13981:                REG8(BL) = msdos_xms_get_unused_emb_handle_count();
                   13982:                REG16(DX) = emb_handle->size_kb;
1.1.1.19  root     13983:        }
                   13984: }
                   13985: 
1.1.1.30  root     13986: void msdos_call_xms_0fh(int size_kb)
1.1.1.19  root     13987: {
1.1.1.30  root     13988:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   13989:        
                   13990:        if(emb_handle == NULL) {
1.1.1.19  root     13991:                REG16(AX) = 0x0000;
                   13992:                REG8(BL) = 0xa2;
1.1.1.30  root     13993:        } else if(emb_handle->lock > 0) {
1.1.1.19  root     13994:                REG16(AX) = 0x0000;
                   13995:                REG8(BL) = 0xab;
                   13996:        } else {
1.1.1.30  root     13997:                if(emb_handle->size_kb < size_kb) {
                   13998:                        if(emb_handle->next != NULL && emb_handle->next->handle == 0 && (emb_handle->size_kb + emb_handle->next->size_kb) >= size_kb) {
                   13999:                                msdos_xms_combine_emb_handles(emb_handle);
                   14000:                                if(emb_handle->size_kb > size_kb) {
                   14001:                                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   14002:                                }
                   14003:                        } else {
                   14004:                                int old_handle = emb_handle->handle;
                   14005:                                int old_size_kb = emb_handle->size_kb;
                   14006:                                UINT8 *buffer = (UINT8 *)malloc(old_size_kb * 1024);
                   14007:                                
                   14008:                                memcpy(buffer, mem + emb_handle->address, old_size_kb * 1024);
                   14009:                                msdos_xms_free_emb_handle(emb_handle);
                   14010:                                
                   14011:                                if((emb_handle = msdos_xms_alloc_emb_handle(size_kb)) == NULL) {
                   14012:                                        emb_handle = msdos_xms_alloc_emb_handle(old_size_kb); // should be always successed
                   14013:                                }
                   14014:                                emb_handle->handle = old_handle;
                   14015:                                memcpy(mem + emb_handle->address, buffer, old_size_kb * 1024);
                   14016:                                free(buffer);
                   14017:                        }
                   14018:                } else if(emb_handle->size_kb > size_kb) {
                   14019:                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   14020:                }
                   14021:                if(emb_handle->size_kb != size_kb) {
                   14022:                        REG16(AX) = 0x0000;
                   14023:                        REG8(BL) = 0xa0;
                   14024:                } else {
                   14025:                        REG16(AX) = 0x0001;
                   14026:                        REG8(BL) = 0x00;
                   14027:                }
1.1.1.19  root     14028:        }
                   14029: }
                   14030: 
1.1.1.30  root     14031: inline void msdos_call_xms_0fh()
                   14032: {
                   14033:        msdos_call_xms_0fh(REG16(BX));
                   14034: }
                   14035: 
1.1.1.19  root     14036: inline void msdos_call_xms_10h()
                   14037: {
                   14038:        int seg;
                   14039:        
                   14040:        if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(DX), 0)) != -1) {
                   14041:                REG16(AX) = 0x0001;
                   14042:                REG16(BX) = seg;
                   14043:        } else {
                   14044:                REG16(AX) = 0x0000;
                   14045:                REG8(BL) = 0xb0;
                   14046:                REG16(DX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   14047:        }
                   14048: }
                   14049: 
                   14050: inline void msdos_call_xms_11h()
                   14051: {
                   14052:        int mcb_seg = REG16(DX) - 1;
                   14053:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   14054:        
                   14055:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   14056:                msdos_mem_free(REG16(DX));
                   14057:                REG16(AX) = 0x0001;
                   14058:                REG8(BL) = 0x00;
                   14059:        } else {
                   14060:                REG16(AX) = 0x0000;
                   14061:                REG8(BL) = 0xb2;
                   14062:        }
                   14063: }
                   14064: 
                   14065: inline void msdos_call_xms_12h()
                   14066: {
                   14067:        int mcb_seg = REG16(DX) - 1;
                   14068:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   14069:        int max_paragraphs;
                   14070:        
                   14071:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   14072:                if(!msdos_mem_realloc(REG16(DX), REG16(BX), &max_paragraphs)) {
                   14073:                        REG16(AX) = 0x0001;
                   14074:                        REG8(BL) = 0x00;
                   14075:                } else {
                   14076:                        REG16(AX) = 0x0000;
                   14077:                        REG8(BL) = 0xb0;
                   14078:                        REG16(DX) = max_paragraphs;
                   14079:                }
                   14080:        } else {
                   14081:                REG16(AX) = 0x0000;
                   14082:                REG8(BL) = 0xb2;
                   14083:        }
                   14084: }
                   14085: 
1.1.1.29  root     14086: #if defined(HAS_I386)
                   14087: 
                   14088: inline void msdos_call_xms_88h()
                   14089: {
                   14090:        REG32(EAX) = REG32(EDX) = 0x0000;
                   14091:        
1.1.1.30  root     14092:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   14093:                if(emb_handle->handle == 0) {
                   14094:                        if(REG32(EAX) < emb_handle->size_kb) {
                   14095:                                REG32(EAX) = emb_handle->size_kb;
1.1.1.29  root     14096:                        }
1.1.1.30  root     14097:                        REG32(EDX) += emb_handle->size_kb;
1.1.1.29  root     14098:                }
                   14099:        }
                   14100:        
                   14101:        if(REG32(EAX) == 0 && REG32(EDX) == 0) {
                   14102:                REG8(BL) = 0xa0;
                   14103:        } else {
                   14104:                REG8(BL) = 0x00;
                   14105:        }
                   14106:        REG32(ECX) = EMB_END - 1;
                   14107: }
                   14108: 
                   14109: inline void msdos_call_xms_89h()
                   14110: {
1.1.1.30  root     14111:        msdos_call_xms_09h(REG32(EDX));
1.1.1.29  root     14112: }
                   14113: 
                   14114: inline void msdos_call_xms_8eh()
                   14115: {
1.1.1.30  root     14116:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   14117:        
                   14118:        if(emb_handle == NULL) {
1.1.1.29  root     14119:                REG16(AX) = 0x0000;
                   14120:                REG8(BL) = 0xa2;
                   14121:        } else {
                   14122:                REG16(AX) = 0x0001;
1.1.1.30  root     14123:                REG8(BH) = emb_handle->lock;
                   14124:                REG16(CX) = msdos_xms_get_unused_emb_handle_count();
                   14125:                REG32(EDX) = emb_handle->size_kb;
1.1.1.29  root     14126:        }
                   14127: }
                   14128: 
                   14129: inline void msdos_call_xms_8fh()
                   14130: {
1.1.1.30  root     14131:        msdos_call_xms_0fh(REG32(EBX));
1.1.1.29  root     14132: }
                   14133: 
                   14134: #endif
1.1.1.19  root     14135: #endif
                   14136: 
1.1.1.26  root     14137: UINT16 msdos_get_equipment()
                   14138: {
                   14139:        static UINT16 equip = 0;
                   14140:        
                   14141:        if(equip == 0) {
                   14142: #ifdef SUPPORT_FPU
                   14143:                equip |= (1 << 1);      // 80x87 coprocessor installed
                   14144: #endif
                   14145:                equip |= (1 << 2);      // pointing device installed (PS/2)
                   14146:                equip |= (2 << 4);      // initial video mode (80x25 color)
                   14147: //             equip |= (1 << 8);      // 0 if DMA installed
                   14148:                equip |= (2 << 9);      // number of serial ports
                   14149:                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     14150:                
                   14151:                // check only A: and B: if it is floppy drive
                   14152:                DWORD dwDrives = GetLogicalDrives();
                   14153:                int n = 0;
                   14154:                for(int i = 0; i < 2; i++) {
                   14155:                        if(dwDrives & (1 << i)) {
                   14156:                                char volume[] = "A:\\";
                   14157:                                volume[0] = 'A' + i;
                   14158:                                if(GetDriveType(volume) == DRIVE_REMOVABLE) {
                   14159:                                        n++;
                   14160:                                }
                   14161:                        }
                   14162:                }
                   14163:                if(n != 0) {
                   14164:                        equip |= (1 << 0);      // floppy disk(s) installed
                   14165:                        n--;
                   14166:                        equip |= (n << 6);      // number of floppies installed less 1
                   14167:                }
                   14168: //             if(joyGetNumDevs() != 0) {
                   14169: //                     equip |= (1 << 12);     // game port installed
                   14170: //             }
1.1.1.26  root     14171:        }
                   14172:        return(equip);
                   14173: }
                   14174: 
1.1       root     14175: void msdos_syscall(unsigned num)
                   14176: {
1.1.1.22  root     14177: #ifdef ENABLE_DEBUG_SYSCALL
                   14178:        if(num == 0x68) {
                   14179:                // dummy interrupt for EMS (int 67h)
1.1.1.33  root     14180:                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     14181:        } else if(num == 0x69) {
                   14182:                // dummy interrupt for XMS (call far)
1.1.1.33  root     14183:                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     14184:        } else if(num == 0x6a) {
                   14185:                // dummy interrupt for case map routine pointed in the country info
                   14186:        } else {
1.1.1.33  root     14187:                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     14188:        }
                   14189: #endif
1.1.1.36  root     14190:        // update cursor position
                   14191:        if(cursor_moved) {
                   14192:                pcbios_update_cursor_position();
                   14193:                cursor_moved = false;
                   14194:        }
1.1.1.33  root     14195:        ctrl_break_detected = ctrl_break_pressed = ctrl_c_pressed = false;
1.1.1.22  root     14196:        
1.1       root     14197:        switch(num) {
                   14198:        case 0x00:
1.1.1.28  root     14199:                try {
                   14200:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   14201:                        error("division by zero\n");
                   14202:                } catch(...) {
                   14203:                        fatalerror("division by zero detected, and failed to terminate current process\n");
                   14204:                }
1.1       root     14205:                break;
                   14206:        case 0x04:
1.1.1.28  root     14207:                try {
                   14208:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   14209:                        error("overflow\n");
                   14210:                } catch(...) {
                   14211:                        fatalerror("overflow detected, and failed to terminate current process\n");
                   14212:                }
1.1       root     14213:                break;
                   14214:        case 0x06:
                   14215:                // NOTE: ish.com has illegal instruction...
1.1.1.14  root     14216:                if(!ignore_illegal_insn) {
1.1.1.28  root     14217:                        try {
                   14218:                                msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   14219:                                error("illegal instruction\n");
                   14220:                        } catch(...) {
                   14221:                                fatalerror("illegal instruction detected, and failed to terminate current process\n");
                   14222:                        }
1.1.1.14  root     14223:                } else {
                   14224: #if defined(HAS_I386)
1.1.1.39! root     14225:                        m_eip = m_int6h_skip_eip;
        !          14226: #elif defined(HAS_I286)
        !          14227:                        m_pc = m_int6h_skip_pc;
1.1.1.14  root     14228: #else
1.1.1.39! root     14229:                        // 8086/80186 ignore an invalid opcode
1.1.1.14  root     14230: #endif
                   14231:                }
1.1       root     14232:                break;
1.1.1.33  root     14233:        case 0x09:
                   14234:                // ctrl-break is pressed
                   14235:                if(raise_int_1bh) {
                   14236: #if defined(HAS_I386)
                   14237:                        m_ext = 0; // not an external interrupt
                   14238:                        i386_trap(0x1b, 1, 0);
                   14239:                        m_ext = 1;
                   14240: #else
                   14241:                        PREFIX86(_interrupt)(0x1b);
                   14242: #endif
                   14243:                        raise_int_1bh = false;
                   14244:                }
1.1.1.8   root     14245:        case 0x08:
1.1.1.14  root     14246: //             pcbios_irq0(); // this causes too slow emulation...
1.1.1.8   root     14247:        case 0x0b:
                   14248:        case 0x0c:
                   14249:        case 0x0d:
                   14250:        case 0x0e:
                   14251:        case 0x0f:
                   14252:                // EOI
                   14253:                pic[0].isr &= ~(1 << (num - 0x08));
                   14254:                pic_update();
                   14255:                break;
1.1       root     14256:        case 0x10:
                   14257:                // PC BIOS - Video
1.1.1.14  root     14258:                if(!restore_console_on_exit) {
1.1.1.15  root     14259:                        change_console_size(scr_width, scr_height);
1.1       root     14260:                }
1.1.1.3   root     14261:                m_CF = 0;
1.1       root     14262:                switch(REG8(AH)) {
1.1.1.16  root     14263:                case 0x00: pcbios_int_10h_00h(); break;
1.1       root     14264:                case 0x01: pcbios_int_10h_01h(); break;
                   14265:                case 0x02: pcbios_int_10h_02h(); break;
                   14266:                case 0x03: pcbios_int_10h_03h(); break;
                   14267:                case 0x05: pcbios_int_10h_05h(); break;
                   14268:                case 0x06: pcbios_int_10h_06h(); break;
                   14269:                case 0x07: pcbios_int_10h_07h(); break;
                   14270:                case 0x08: pcbios_int_10h_08h(); break;
                   14271:                case 0x09: pcbios_int_10h_09h(); break;
                   14272:                case 0x0a: pcbios_int_10h_0ah(); break;
                   14273:                case 0x0b: break;
                   14274:                case 0x0c: break;
                   14275:                case 0x0d: break;
                   14276:                case 0x0e: pcbios_int_10h_0eh(); break;
                   14277:                case 0x0f: pcbios_int_10h_0fh(); break;
                   14278:                case 0x10: break;
1.1.1.14  root     14279:                case 0x11: pcbios_int_10h_11h(); break;
                   14280:                case 0x12: pcbios_int_10h_12h(); break;
1.1       root     14281:                case 0x13: pcbios_int_10h_13h(); break;
1.1.1.30  root     14282:                case 0x18: pcbios_int_10h_18h(); break;
1.1.1.14  root     14283:                case 0x1a: pcbios_int_10h_1ah(); break;
1.1.1.24  root     14284:                case 0x1b: REG8(AL) = 0x00; break; // functionality/state information is not supported
                   14285:                case 0x1c: REG8(AL) = 0x00; break; // save/restore video state is not supported
1.1       root     14286:                case 0x1d: pcbios_int_10h_1dh(); break;
1.1.1.24  root     14287:                case 0x1e: REG8(AL) = 0x00; break; // flat-panel functions are not supported
                   14288:                case 0x1f: REG8(AL) = 0x00; break; // xga functions are not supported
1.1.1.22  root     14289:                case 0x4f: pcbios_int_10h_4fh(); break;
1.1.1.30  root     14290:                case 0x6f: break;
1.1.1.22  root     14291:                case 0x80: m_CF = 1; break; // unknown
                   14292:                case 0x81: m_CF = 1; break; // unknown
1.1       root     14293:                case 0x82: pcbios_int_10h_82h(); break;
1.1.1.22  root     14294:                case 0x83: pcbios_int_10h_83h(); break;
                   14295:                case 0x8b: break;
                   14296:                case 0x8c: m_CF = 1; break; // unknown
                   14297:                case 0x8d: m_CF = 1; break; // unknown
                   14298:                case 0x8e: m_CF = 1; break; // unknown
                   14299:                case 0x90: pcbios_int_10h_90h(); break;
                   14300:                case 0x91: pcbios_int_10h_91h(); break;
                   14301:                case 0x92: break;
                   14302:                case 0x93: break;
                   14303:                case 0xef: pcbios_int_10h_efh(); break;
1.1.1.24  root     14304:                case 0xfa: break; // ega register interface library is not installed
1.1       root     14305:                case 0xfe: pcbios_int_10h_feh(); break;
                   14306:                case 0xff: pcbios_int_10h_ffh(); break;
                   14307:                default:
1.1.1.22  root     14308:                        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));
                   14309:                        m_CF = 1;
1.1       root     14310:                        break;
                   14311:                }
                   14312:                break;
                   14313:        case 0x11:
                   14314:                // PC BIOS - Get Equipment List
1.1.1.26  root     14315:                REG16(AX) = msdos_get_equipment();
1.1       root     14316:                break;
                   14317:        case 0x12:
                   14318:                // PC BIOS - Get Memory Size
1.1.1.33  root     14319:                REG16(AX) = *(UINT16 *)(mem + 0x413);
1.1       root     14320:                break;
                   14321:        case 0x13:
                   14322:                // PC BIOS - Disk
1.1.1.22  root     14323: //             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     14324:                REG8(AH) = 0xff;
1.1.1.3   root     14325:                m_CF = 1;
1.1       root     14326:                break;
                   14327:        case 0x14:
                   14328:                // PC BIOS - Serial I/O
1.1.1.25  root     14329:                switch(REG8(AH)) {
                   14330:                case 0x00: pcbios_int_14h_00h(); break;
                   14331:                case 0x01: pcbios_int_14h_01h(); break;
                   14332:                case 0x02: pcbios_int_14h_02h(); break;
                   14333:                case 0x03: pcbios_int_14h_03h(); break;
                   14334:                case 0x04: pcbios_int_14h_04h(); break;
                   14335:                case 0x05: pcbios_int_14h_05h(); break;
                   14336:                default:
                   14337:                        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));
                   14338:                        break;
                   14339:                }
1.1       root     14340:                break;
                   14341:        case 0x15:
                   14342:                // PC BIOS
1.1.1.3   root     14343:                m_CF = 0;
1.1       root     14344:                switch(REG8(AH)) {
1.1.1.14  root     14345:                case 0x10: pcbios_int_15h_10h(); break;
1.1       root     14346:                case 0x23: pcbios_int_15h_23h(); break;
                   14347:                case 0x24: pcbios_int_15h_24h(); break;
1.1.1.24  root     14348:                case 0x41: break;
1.1       root     14349:                case 0x49: pcbios_int_15h_49h(); break;
1.1.1.22  root     14350:                case 0x50: pcbios_int_15h_50h(); break;
1.1.1.30  root     14351:                case 0x53: pcbios_int_15h_53h(); break;
1.1       root     14352:                case 0x86: pcbios_int_15h_86h(); break;
                   14353:                case 0x87: pcbios_int_15h_87h(); break;
                   14354:                case 0x88: pcbios_int_15h_88h(); break;
                   14355:                case 0x89: pcbios_int_15h_89h(); break;
1.1.1.21  root     14356:                case 0x8a: pcbios_int_15h_8ah(); break;
1.1.1.22  root     14357:                case 0xc0: // PS/2 ???
                   14358:                case 0xc1:
                   14359:                case 0xc2:
1.1.1.30  root     14360:                case 0xc3: // PS50+ ???
                   14361:                case 0xc4:
1.1.1.22  root     14362:                        REG8(AH) = 0x86;
                   14363:                        m_CF = 1;
                   14364:                        break;
1.1.1.3   root     14365: #if defined(HAS_I386)
1.1       root     14366:                case 0xc9: pcbios_int_15h_c9h(); break;
1.1.1.3   root     14367: #endif
1.1       root     14368:                case 0xca: pcbios_int_15h_cah(); break;
1.1.1.22  root     14369:                case 0xe8: pcbios_int_15h_e8h(); break;
1.1       root     14370:                default:
1.1.1.22  root     14371:                        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));
                   14372:                        REG8(AH) = 0x86;
1.1.1.3   root     14373:                        m_CF = 1;
1.1       root     14374:                        break;
                   14375:                }
                   14376:                break;
                   14377:        case 0x16:
                   14378:                // PC BIOS - Keyboard
1.1.1.3   root     14379:                m_CF = 0;
1.1       root     14380:                switch(REG8(AH)) {
                   14381:                case 0x00: pcbios_int_16h_00h(); break;
                   14382:                case 0x01: pcbios_int_16h_01h(); break;
                   14383:                case 0x02: pcbios_int_16h_02h(); break;
                   14384:                case 0x03: pcbios_int_16h_03h(); break;
                   14385:                case 0x05: pcbios_int_16h_05h(); break;
                   14386:                case 0x10: pcbios_int_16h_00h(); break;
                   14387:                case 0x11: pcbios_int_16h_01h(); break;
                   14388:                case 0x12: pcbios_int_16h_12h(); break;
                   14389:                case 0x13: pcbios_int_16h_13h(); break;
                   14390:                case 0x14: pcbios_int_16h_14h(); break;
1.1.1.24  root     14391:                case 0x55: pcbios_int_16h_55h(); break;
1.1.1.30  root     14392:                case 0x6f: pcbios_int_16h_6fh(); break;
1.1.1.22  root     14393:                case 0xda: break; // unknown
                   14394:                case 0xff: break; // unknown
1.1       root     14395:                default:
1.1.1.22  root     14396:                        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     14397:                        break;
                   14398:                }
                   14399:                break;
                   14400:        case 0x17:
                   14401:                // PC BIOS - Printer
1.1.1.37  root     14402:                m_CF = 0;
                   14403:                switch(REG8(AH)) {
                   14404:                case 0x00: pcbios_int_17h_00h(); break;
                   14405:                case 0x01: pcbios_int_17h_01h(); break;
                   14406:                case 0x02: pcbios_int_17h_02h(); break;
                   14407:                case 0x03: pcbios_int_17h_03h(); break;
                   14408:                case 0x50: pcbios_int_17h_50h(); break;
                   14409:                case 0x51: pcbios_int_17h_51h(); break;
                   14410:                case 0x52: pcbios_int_17h_52h(); break;
                   14411:                case 0x84: pcbios_int_17h_84h(); break;
                   14412:                case 0x85: pcbios_int_17h_85h(); break;
                   14413:                default:
                   14414:                        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));
                   14415:                        break;
                   14416:                }
1.1       root     14417:                break;
                   14418:        case 0x1a:
                   14419:                // PC BIOS - Timer
1.1.1.3   root     14420:                m_CF = 0;
1.1       root     14421:                switch(REG8(AH)) {
                   14422:                case 0x00: pcbios_int_1ah_00h(); break;
                   14423:                case 0x01: break;
                   14424:                case 0x02: pcbios_int_1ah_02h(); break;
                   14425:                case 0x03: break;
                   14426:                case 0x04: pcbios_int_1ah_04h(); break;
                   14427:                case 0x05: break;
                   14428:                case 0x0a: pcbios_int_1ah_0ah(); break;
                   14429:                case 0x0b: break;
1.1.1.14  root     14430:                case 0x35: break; // Word Perfect Third Party Interface?
                   14431:                case 0x36: break; // Word Perfect Third Party Interface
                   14432:                case 0x70: break; // SNAP? (Simple Network Application Protocol)
1.1       root     14433:                default:
1.1.1.22  root     14434:                        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     14435:                        break;
                   14436:                }
                   14437:                break;
1.1.1.33  root     14438:        case 0x1b:
                   14439:                mem[0x471] = 0x00;
                   14440:                break;
1.1       root     14441:        case 0x20:
1.1.1.28  root     14442:                try {
                   14443:                        msdos_process_terminate(SREG(CS), retval, 1);
                   14444:                } catch(...) {
                   14445:                        fatalerror("failed to terminate the process (PSP=%04X) by int 20h\n", SREG(CS));
                   14446:                }
1.1       root     14447:                break;
                   14448:        case 0x21:
                   14449:                // MS-DOS System Call
1.1.1.3   root     14450:                m_CF = 0;
1.1.1.28  root     14451:                try {
                   14452:                        switch(REG8(AH)) {
                   14453:                        case 0x00: msdos_int_21h_00h(); break;
                   14454:                        case 0x01: msdos_int_21h_01h(); break;
                   14455:                        case 0x02: msdos_int_21h_02h(); break;
                   14456:                        case 0x03: msdos_int_21h_03h(); break;
                   14457:                        case 0x04: msdos_int_21h_04h(); break;
                   14458:                        case 0x05: msdos_int_21h_05h(); break;
                   14459:                        case 0x06: msdos_int_21h_06h(); break;
                   14460:                        case 0x07: msdos_int_21h_07h(); break;
                   14461:                        case 0x08: msdos_int_21h_08h(); break;
                   14462:                        case 0x09: msdos_int_21h_09h(); break;
                   14463:                        case 0x0a: msdos_int_21h_0ah(); break;
                   14464:                        case 0x0b: msdos_int_21h_0bh(); break;
                   14465:                        case 0x0c: msdos_int_21h_0ch(); break;
                   14466:                        case 0x0d: msdos_int_21h_0dh(); break;
                   14467:                        case 0x0e: msdos_int_21h_0eh(); break;
                   14468:                        case 0x0f: msdos_int_21h_0fh(); break;
                   14469:                        case 0x10: msdos_int_21h_10h(); break;
                   14470:                        case 0x11: msdos_int_21h_11h(); break;
                   14471:                        case 0x12: msdos_int_21h_12h(); break;
                   14472:                        case 0x13: msdos_int_21h_13h(); break;
                   14473:                        case 0x14: msdos_int_21h_14h(); break;
                   14474:                        case 0x15: msdos_int_21h_15h(); break;
                   14475:                        case 0x16: msdos_int_21h_16h(); break;
                   14476:                        case 0x17: msdos_int_21h_17h(); break;
                   14477:                        case 0x18: msdos_int_21h_18h(); break;
                   14478:                        case 0x19: msdos_int_21h_19h(); break;
                   14479:                        case 0x1a: msdos_int_21h_1ah(); break;
                   14480:                        case 0x1b: msdos_int_21h_1bh(); break;
                   14481:                        case 0x1c: msdos_int_21h_1ch(); break;
                   14482:                        case 0x1d: msdos_int_21h_1dh(); break;
                   14483:                        case 0x1e: msdos_int_21h_1eh(); break;
                   14484:                        case 0x1f: msdos_int_21h_1fh(); break;
                   14485:                        case 0x20: msdos_int_21h_20h(); break;
                   14486:                        case 0x21: msdos_int_21h_21h(); break;
                   14487:                        case 0x22: msdos_int_21h_22h(); break;
                   14488:                        case 0x23: msdos_int_21h_23h(); break;
                   14489:                        case 0x24: msdos_int_21h_24h(); break;
                   14490:                        case 0x25: msdos_int_21h_25h(); break;
                   14491:                        case 0x26: msdos_int_21h_26h(); break;
                   14492:                        case 0x27: msdos_int_21h_27h(); break;
                   14493:                        case 0x28: msdos_int_21h_28h(); break;
                   14494:                        case 0x29: msdos_int_21h_29h(); break;
                   14495:                        case 0x2a: msdos_int_21h_2ah(); break;
                   14496:                        case 0x2b: msdos_int_21h_2bh(); break;
                   14497:                        case 0x2c: msdos_int_21h_2ch(); break;
                   14498:                        case 0x2d: msdos_int_21h_2dh(); break;
                   14499:                        case 0x2e: msdos_int_21h_2eh(); break;
                   14500:                        case 0x2f: msdos_int_21h_2fh(); break;
                   14501:                        case 0x30: msdos_int_21h_30h(); break;
                   14502:                        case 0x31: msdos_int_21h_31h(); break;
                   14503:                        case 0x32: msdos_int_21h_32h(); break;
                   14504:                        case 0x33: msdos_int_21h_33h(); break;
                   14505:                        case 0x34: msdos_int_21h_34h(); break;
                   14506:                        case 0x35: msdos_int_21h_35h(); break;
                   14507:                        case 0x36: msdos_int_21h_36h(); break;
                   14508:                        case 0x37: msdos_int_21h_37h(); break;
                   14509:                        case 0x38: msdos_int_21h_38h(); break;
                   14510:                        case 0x39: msdos_int_21h_39h(0); break;
                   14511:                        case 0x3a: msdos_int_21h_3ah(0); break;
                   14512:                        case 0x3b: msdos_int_21h_3bh(0); break;
                   14513:                        case 0x3c: msdos_int_21h_3ch(); break;
                   14514:                        case 0x3d: msdos_int_21h_3dh(); break;
                   14515:                        case 0x3e: msdos_int_21h_3eh(); break;
                   14516:                        case 0x3f: msdos_int_21h_3fh(); break;
                   14517:                        case 0x40: msdos_int_21h_40h(); break;
                   14518:                        case 0x41: msdos_int_21h_41h(0); break;
                   14519:                        case 0x42: msdos_int_21h_42h(); break;
                   14520:                        case 0x43: msdos_int_21h_43h(0); break;
                   14521:                        case 0x44: msdos_int_21h_44h(); break;
                   14522:                        case 0x45: msdos_int_21h_45h(); break;
                   14523:                        case 0x46: msdos_int_21h_46h(); break;
                   14524:                        case 0x47: msdos_int_21h_47h(0); break;
                   14525:                        case 0x48: msdos_int_21h_48h(); break;
                   14526:                        case 0x49: msdos_int_21h_49h(); break;
                   14527:                        case 0x4a: msdos_int_21h_4ah(); break;
                   14528:                        case 0x4b: msdos_int_21h_4bh(); break;
                   14529:                        case 0x4c: msdos_int_21h_4ch(); break;
                   14530:                        case 0x4d: msdos_int_21h_4dh(); break;
                   14531:                        case 0x4e: msdos_int_21h_4eh(); break;
                   14532:                        case 0x4f: msdos_int_21h_4fh(); break;
                   14533:                        case 0x50: msdos_int_21h_50h(); break;
                   14534:                        case 0x51: msdos_int_21h_51h(); break;
                   14535:                        case 0x52: msdos_int_21h_52h(); break;
1.1.1.33  root     14536:                        // 0x53: Translate BIOS Parameter Block to Drive Param Bock
1.1.1.28  root     14537:                        case 0x54: msdos_int_21h_54h(); break;
                   14538:                        case 0x55: msdos_int_21h_55h(); break;
                   14539:                        case 0x56: msdos_int_21h_56h(0); break;
                   14540:                        case 0x57: msdos_int_21h_57h(); break;
                   14541:                        case 0x58: msdos_int_21h_58h(); break;
                   14542:                        case 0x59: msdos_int_21h_59h(); break;
                   14543:                        case 0x5a: msdos_int_21h_5ah(); break;
                   14544:                        case 0x5b: msdos_int_21h_5bh(); break;
                   14545:                        case 0x5c: msdos_int_21h_5ch(); break;
                   14546:                        case 0x5d: msdos_int_21h_5dh(); break;
1.1.1.33  root     14547:                        // 0x5e: MS-Network
1.1.1.30  root     14548:                        case 0x5f: msdos_int_21h_5fh(); break;
1.1.1.28  root     14549:                        case 0x60: msdos_int_21h_60h(0); break;
                   14550:                        case 0x61: msdos_int_21h_61h(); break;
                   14551:                        case 0x62: msdos_int_21h_62h(); break;
                   14552:                        case 0x63: msdos_int_21h_63h(); break;
1.1.1.33  root     14553:                        // 0x64: Set Device Driver Lockahead Flag
1.1.1.28  root     14554:                        case 0x65: msdos_int_21h_65h(); break;
                   14555:                        case 0x66: msdos_int_21h_66h(); break;
                   14556:                        case 0x67: msdos_int_21h_67h(); break;
                   14557:                        case 0x68: msdos_int_21h_68h(); break;
                   14558:                        case 0x69: msdos_int_21h_69h(); break;
                   14559:                        case 0x6a: msdos_int_21h_6ah(); break;
                   14560:                        case 0x6b: msdos_int_21h_6bh(); break;
                   14561:                        case 0x6c: msdos_int_21h_6ch(0); break;
1.1.1.33  root     14562:                        // 0x6d: Find First ROM Program
                   14563:                        // 0x6e: Find Next ROM Program
                   14564:                        // 0x6f: Get/Set ROM Scan Start Address
                   14565:                        // 0x70: Windows95 - Get/Set Internationalization Information
1.1.1.28  root     14566:                        case 0x71:
1.1.1.33  root     14567:                                // Windows95 - Long Filename Functions
1.1.1.28  root     14568:                                switch(REG8(AL)) {
                   14569:                                case 0x0d: msdos_int_21h_710dh(); break;
                   14570:                                case 0x39: msdos_int_21h_39h(1); break;
                   14571:                                case 0x3a: msdos_int_21h_3ah(1); break;
                   14572:                                case 0x3b: msdos_int_21h_3bh(1); break;
                   14573:                                case 0x41: msdos_int_21h_7141h(1); break;
                   14574:                                case 0x43: msdos_int_21h_43h(1); break;
                   14575:                                case 0x47: msdos_int_21h_47h(1); break;
                   14576:                                case 0x4e: msdos_int_21h_714eh(); break;
                   14577:                                case 0x4f: msdos_int_21h_714fh(); break;
                   14578:                                case 0x56: msdos_int_21h_56h(1); break;
                   14579:                                case 0x60: msdos_int_21h_60h(1); break;
                   14580:                                case 0x6c: msdos_int_21h_6ch(1); break;
                   14581:                                case 0xa0: msdos_int_21h_71a0h(); break;
                   14582:                                case 0xa1: msdos_int_21h_71a1h(); break;
                   14583:                                case 0xa6: msdos_int_21h_71a6h(); break;
                   14584:                                case 0xa7: msdos_int_21h_71a7h(); break;
                   14585:                                case 0xa8: msdos_int_21h_71a8h(); break;
1.1.1.33  root     14586:                                // 0xa9: Server Create/Open File
1.1.1.28  root     14587:                                case 0xaa: msdos_int_21h_71aah(); break;
                   14588:                                default:
                   14589:                                        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));
                   14590:                                        REG16(AX) = 0x7100;
                   14591:                                        m_CF = 1;
                   14592:                                        break;
                   14593:                                }
                   14594:                                break;
                   14595:                        // 0x72: Windows95 beta - LFN FindClose
                   14596:                        case 0x73:
1.1.1.33  root     14597:                                // Windows95 - FAT32 Functions
1.1.1.28  root     14598:                                switch(REG8(AL)) {
                   14599:                                case 0x00: msdos_int_21h_7300h(); break;
1.1.1.33  root     14600:                                // 0x01: Set Drive Locking ???
1.1.1.28  root     14601:                                case 0x02: msdos_int_21h_7302h(); break;
                   14602:                                case 0x03: msdos_int_21h_7303h(); break;
1.1.1.33  root     14603:                                // 0x04: Set DPB to Use for Formatting
                   14604:                                // 0x05: Extended Absolute Disk Read/Write
1.1.1.28  root     14605:                                default:
                   14606:                                        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));
                   14607:                                        REG16(AX) = 0x7300;
                   14608:                                        m_CF = 1;
                   14609:                                        break;
                   14610:                                }
1.1       root     14611:                                break;
1.1.1.30  root     14612:                        case 0xdb: msdos_int_21h_dbh(); break;
                   14613:                        case 0xdc: msdos_int_21h_dch(); break;
1.1       root     14614:                        default:
1.1.1.22  root     14615:                                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     14616:                                REG16(AX) = 0x01;
1.1.1.3   root     14617:                                m_CF = 1;
1.1       root     14618:                                break;
                   14619:                        }
1.1.1.28  root     14620:                } catch(int error) {
                   14621:                        REG16(AX) = error;
                   14622:                        m_CF = 1;
                   14623:                } catch(...) {
                   14624:                        REG16(AX) = 0x1f; // general failure
1.1.1.3   root     14625:                        m_CF = 1;
1.1       root     14626:                }
1.1.1.3   root     14627:                if(m_CF) {
1.1.1.23  root     14628:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   14629:                        sda->extended_error_code = REG16(AX);
                   14630:                        switch(sda->extended_error_code) {
                   14631:                        case  4: // Too many open files
                   14632:                        case  8: // Insufficient memory
                   14633:                                sda->error_class = 1; // Out of resource
                   14634:                                break;
                   14635:                        case  5: // Access denied
                   14636:                                sda->error_class = 3; // Authorization
                   14637:                                break;
                   14638:                        case  7: // Memory control block destroyed
                   14639:                                sda->error_class = 4; // Internal
                   14640:                                break;
                   14641:                        case  2: // File not found
                   14642:                        case  3: // Path not found
                   14643:                        case 15: // Invaid drive specified
                   14644:                        case 18: // No more files
                   14645:                                sda->error_class = 8; // Not found
                   14646:                                break;
                   14647:                        case 32: // Sharing violation
                   14648:                        case 33: // Lock violation
                   14649:                                sda->error_class = 10; // Locked
                   14650:                                break;
                   14651: //                     case 16: // Removal of current directory attempted
                   14652:                        case 19: // Attempted write on protected disk
                   14653:                        case 21: // Drive not ready
                   14654: //                     case 29: // Write failure
                   14655: //                     case 30: // Read failure
                   14656: //                     case 82: // Cannot create subdirectory
                   14657:                                sda->error_class = 11; // Media
                   14658:                                break;
                   14659:                        case 80: // File already exists
                   14660:                                sda->error_class = 12; // Already exist
                   14661:                                break;
                   14662:                        default:
                   14663:                                sda->error_class = 13; // Unknown
                   14664:                                break;
                   14665:                        }
                   14666:                        sda->suggested_action = 1; // Retry
                   14667:                        sda->locus_of_last_error = 1; // Unknown
1.1       root     14668:                }
1.1.1.33  root     14669:                if(ctrl_break_checking && ctrl_break_detected) {
1.1.1.26  root     14670:                        // raise int 23h
                   14671: #if defined(HAS_I386)
                   14672:                        m_ext = 0; // not an external interrupt
                   14673:                        i386_trap(0x23, 1, 0);
                   14674:                        m_ext = 1;
                   14675: #else
                   14676:                        PREFIX86(_interrupt)(0x23);
                   14677: #endif
                   14678:                }
1.1       root     14679:                break;
                   14680:        case 0x22:
                   14681:                fatalerror("int 22h (terminate address)\n");
                   14682:        case 0x23:
1.1.1.28  root     14683:                try {
                   14684:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x100, 1);
                   14685:                } catch(...) {
                   14686:                        fatalerror("failed to terminate the current process by int 23h\n");
                   14687:                }
1.1       root     14688:                break;
                   14689:        case 0x24:
1.1.1.32  root     14690: /*
1.1.1.28  root     14691:                try {
                   14692:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   14693:                } catch(...) {
                   14694:                        fatalerror("failed to terminate the current process by int 24h\n");
                   14695:                }
1.1.1.32  root     14696: */
                   14697:                msdos_int_24h();
1.1       root     14698:                break;
                   14699:        case 0x25:
                   14700:                msdos_int_25h();
                   14701:                break;
                   14702:        case 0x26:
                   14703:                msdos_int_26h();
                   14704:                break;
                   14705:        case 0x27:
1.1.1.28  root     14706:                try {
                   14707:                        msdos_int_27h();
                   14708:                } catch(...) {
                   14709:                        fatalerror("failed to terminate the process (PSP=%04X) by int 27h\n", SREG(CS));
                   14710:                }
1.1       root     14711:                break;
                   14712:        case 0x28:
                   14713:                Sleep(10);
1.1.1.35  root     14714:                REQUEST_HARDWRE_UPDATE();
1.1       root     14715:                break;
                   14716:        case 0x29:
                   14717:                msdos_int_29h();
                   14718:                break;
                   14719:        case 0x2e:
                   14720:                msdos_int_2eh();
                   14721:                break;
                   14722:        case 0x2f:
                   14723:                // multiplex interrupt
                   14724:                switch(REG8(AH)) {
1.1.1.22  root     14725:                case 0x05: msdos_int_2fh_05h(); break;
                   14726:                case 0x11: msdos_int_2fh_11h(); break;
1.1.1.21  root     14727:                case 0x12: msdos_int_2fh_12h(); break;
1.1.1.30  root     14728:                case 0x13: msdos_int_2fh_13h(); break;
1.1.1.22  root     14729:                case 0x14: msdos_int_2fh_14h(); break;
                   14730:                case 0x15: msdos_int_2fh_15h(); break;
1.1       root     14731:                case 0x16: msdos_int_2fh_16h(); break;
1.1.1.22  root     14732:                case 0x19: msdos_int_2fh_19h(); break;
1.1       root     14733:                case 0x1a: msdos_int_2fh_1ah(); break;
1.1.1.30  root     14734:                case 0x40: msdos_int_2fh_40h(); break;
1.1       root     14735:                case 0x43: msdos_int_2fh_43h(); break;
1.1.1.22  root     14736:                case 0x46: msdos_int_2fh_46h(); break;
                   14737:                case 0x48: msdos_int_2fh_48h(); break;
1.1       root     14738:                case 0x4a: msdos_int_2fh_4ah(); break;
1.1.1.22  root     14739:                case 0x4b: msdos_int_2fh_4bh(); break;
1.1       root     14740:                case 0x4f: msdos_int_2fh_4fh(); break;
1.1.1.22  root     14741:                case 0x55: msdos_int_2fh_55h(); break;
1.1.1.24  root     14742:                case 0xad: msdos_int_2fh_adh(); break;
1.1       root     14743:                case 0xae: msdos_int_2fh_aeh(); break;
1.1.1.34  root     14744:                case 0xb7: msdos_int_2fh_b7h(); break;
1.1.1.30  root     14745:                // Installation Check
                   14746:                case 0x01: // PRINT.COM
                   14747:                case 0x02: // PC LAN Program Redirector
                   14748:                case 0x06: // ASSIGN
                   14749:                case 0x08: // DRIVER.SYS
                   14750:                case 0x10: // SHARE
                   14751:                case 0x17: // Clibboard functions
                   14752:                case 0x1b: // XMA2EMS.SYS
                   14753:                case 0x23: // DR DOS 5.0 GRAFTABL
                   14754:                case 0x27: // DR-DOR 6.0 TaskMAX
                   14755:                case 0x2e: // Novell DOS 7 GRAFTABL
1.1.1.33  root     14756:                case 0x39: // Kingswood TSR INTERFACE
                   14757:                case 0x41: // DOS Enhanced LAN Manager 2.0+ MINIPOP/NETPOPUP
1.1.1.30  root     14758:                case 0x45: // PROF.COM
                   14759:                case 0x51: // ODIHELP.EXE
1.1.1.33  root     14760:                case 0x52: // JAM.SYS v1.10+
1.1.1.30  root     14761:                case 0x54: // POWER.EXE
                   14762:                case 0x56: // INTERLNK
1.1.1.33  root     14763:                case 0x57: // IOMEGA DRIVERS
1.1.1.30  root     14764:                case 0x70: // License Service API
1.1.1.33  root     14765:                case 0x72: // SRDISK v1.30+
1.1.1.30  root     14766:                case 0x7a: // Novell NetWare
1.1.1.33  root     14767:                case 0x7f: // PRINDIR v9.0
                   14768:                case 0x80: // FAX BIOS
                   14769:                case 0x81: // Nanosoft, Inc. TurboNET redirector
                   14770:                case 0x82: // Nanosoft, Inc. CAPDOS
                   14771:                case 0x89: // WHOA!.COM
                   14772:                case 0x90: // Resident AID
1.1.1.30  root     14773:                case 0x94: // MICRO.EXE
1.1.1.33  root     14774:                case 0x97: // Micro Focus COBOL v3.1.31
                   14775:                case 0x98: // Micro Focus COBOL v3.1.31
                   14776:                case 0x99: // DOS Navigator II
                   14777:                case 0x9e: // INTMON v2.1
                   14778:                case 0x9f: // INTCFG v2.1
                   14779:                case 0xa9: // METZTSR.COM
                   14780:                case 0xab: // SRSoft MODAL PC v2
1.1.1.30  root     14781:                case 0xac: // GRAPHICS.COM
1.1.1.33  root     14782:                case 0xaf: // WinDOS v2.11
1.1.1.30  root     14783:                case 0xb0: // GRAFTABLE.COM
1.1.1.33  root     14784:                case 0xb4: // IBM PC3270 EMULATION PROG v3
1.1.1.30  root     14785:                case 0xb8: // NETWORK
                   14786:                case 0xb9: // RECEIVER.COM
                   14787:                case 0xbc: // EGA.SYS
1.1.1.33  root     14788:                case 0xbe: // REDVIEW
1.1.1.30  root     14789:                case 0xbf: // PC LAN Program - REDIRIFS.EXE
                   14790:                case 0xc0: // Novell LSL.COM
1.1.1.33  root     14791:                case 0xc1: // Personal NetWare - STPIPX v1.00
                   14792:                case 0xc3: // SETWPR.COM
                   14793:                case 0xc5: // PC-DOS Econet v1.05
                   14794:                case 0xc7: // COLAP.COM
                   14795:                case 0xc9: // ThunderByte???
                   14796:                case 0xca: // TBSCANX
                   14797:                case 0xcb: // Communicating Applications Specification
                   14798:                case 0xcc: // Tsoft NFSDRVR
                   14799:                case 0xcd: // SWELL.EXE
                   14800:                case 0xcf: // TEMPLEXX 1.0
                   14801:                case 0xd0: // Lotus CD/Networker
1.1.1.30  root     14802:                case 0xd2: // PCL-838.EXE
1.1.1.33  root     14803:                case 0xd3: // TeleReplica
                   14804:                case 0xd6: // VEDIT VSWAP
                   14805:                case 0xd7: // Banyan VINES v4+
1.1.1.30  root     14806:                case 0xd8: // Novell NetWare Lite - CLIENT.EXE
1.1.1.33  root     14807:                case 0xda: // ZyXEL ZFAX v1.x
                   14808:                case 0xdb: // ZyXEL ZFAX v2+
                   14809:                case 0xdc: // GOLD.COM
                   14810:                case 0xdd: // MIXFIX.EXE
                   14811:                case 0xde: // Novell Netware - RPRINTER, NPRINTER
                   14812:                case 0xdf: // HyperWare programs
                   14813:                case 0xe0: // SETDRVER.COM v2.10+
                   14814:                case 0xe1: // Phantom2 v1.1+
                   14815:                case 0xe3: // ANARKEY.COM
                   14816:                case 0xed: // Phar Lap DOS EXTENDERS
                   14817:                case 0xee: // XVIEW
                   14818:                case 0xf0: // 4MAP
                   14819:                case 0xf1: // DOS EXTENDER
                   14820:                case 0xf2: // WINX
                   14821:                case 0xf4: // FINDIRQ.COM
                   14822:                case 0xf7: // AUTOPARK.COM
                   14823:                case 0xf8: // SuperStor PRO 2XON.COM
                   14824:                case 0xfb: // AutoBraille v1.1A
                   14825:                case 0xfe: // PC-NFS ???
                   14826:                case 0xff: // Topware Network Operating System
1.1.1.30  root     14827:                        switch(REG8(AL)) {
                   14828:                        case 0x00:
                   14829:                                // This is not installed
                   14830: //                             REG8(AL) = 0x00;
                   14831:                                break;
1.1.1.33  root     14832:                        case 0x01:
                   14833:                                // Banyan VINES v4+ is not installed
                   14834:                                if(REG8(AH) == 0xd7 && REG16(BX) == 0x0000) {
                   14835:                                        break;
                   14836:                                }
1.1.1.30  root     14837:                        default:
                   14838:                                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));
                   14839:                                REG16(AX) = 0x01;
                   14840:                                m_CF = 1;
                   14841:                                break;
                   14842:                        }
                   14843:                        break;
1.1.1.22  root     14844:                default:
                   14845:                        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));
                   14846:                        break;
1.1       root     14847:                }
                   14848:                break;
1.1.1.24  root     14849:        case 0x33:
                   14850:                switch(REG8(AH)) {
                   14851:                case 0x00:
                   14852:                        // Mouse
                   14853:                        switch(REG8(AL)) {
                   14854:                        case 0x00: msdos_int_33h_0000h(); break;
                   14855:                        case 0x01: msdos_int_33h_0001h(); break;
                   14856:                        case 0x02: msdos_int_33h_0002h(); break;
                   14857:                        case 0x03: msdos_int_33h_0003h(); break;
1.1.1.34  root     14858:                        case 0x04: msdos_int_33h_0004h(); break;
1.1.1.24  root     14859:                        case 0x05: msdos_int_33h_0005h(); break;
                   14860:                        case 0x06: msdos_int_33h_0006h(); break;
                   14861:                        case 0x07: msdos_int_33h_0007h(); break;
                   14862:                        case 0x08: msdos_int_33h_0008h(); break;
                   14863:                        case 0x09: msdos_int_33h_0009h(); break;
1.1.1.34  root     14864:                        case 0x0a: break; // Define Text Cursor
1.1.1.24  root     14865:                        case 0x0b: msdos_int_33h_000bh(); break;
                   14866:                        case 0x0c: msdos_int_33h_000ch(); break;
1.1.1.34  root     14867:                        case 0x0d: break; // Light Pen Emulation On
                   14868:                        case 0x0e: break; // Light Pen Emulation Off
1.1.1.24  root     14869:                        case 0x0f: msdos_int_33h_000fh(); break;
1.1.1.34  root     14870:                        case 0x10: break; // Define Screen Region for Updating
1.1.1.24  root     14871:                        case 0x11: msdos_int_33h_0011h(); break;
1.1.1.34  root     14872:                        case 0x12: REG16(AX) = 0xffff; break; // Set Large Graphics Cursor Block
                   14873:                        case 0x13: break; // Define Double-Speed Threshold
1.1.1.24  root     14874:                        case 0x14: msdos_int_33h_0014h(); break;
                   14875:                        case 0x15: msdos_int_33h_0015h(); break;
                   14876:                        case 0x16: msdos_int_33h_0016h(); break;
                   14877:                        case 0x17: msdos_int_33h_0017h(); break;
                   14878:                        case 0x1a: msdos_int_33h_001ah(); break;
                   14879:                        case 0x1b: msdos_int_33h_001bh(); break;
                   14880:                        case 0x1d: msdos_int_33h_001dh(); break;
                   14881:                        case 0x1e: msdos_int_33h_001eh(); break;
1.1.1.34  root     14882:                        case 0x1f: msdos_int_33h_001fh(); break;
                   14883:                        case 0x20: msdos_int_33h_0020h(); break;
1.1.1.24  root     14884:                        case 0x21: msdos_int_33h_0021h(); break;
                   14885:                        case 0x22: msdos_int_33h_0022h(); break;
                   14886:                        case 0x23: msdos_int_33h_0023h(); break;
                   14887:                        case 0x24: msdos_int_33h_0024h(); break;
                   14888:                        case 0x26: msdos_int_33h_0026h(); break;
                   14889:                        case 0x2a: msdos_int_33h_002ah(); break;
1.1.1.34  root     14890:                        case 0x2f: break; // Mouse Hardware Reset
1.1.1.24  root     14891:                        case 0x31: msdos_int_33h_0031h(); break;
                   14892:                        case 0x32: msdos_int_33h_0032h(); break;
                   14893:                        default:
                   14894:                                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));
                   14895:                                break;
                   14896:                        }
                   14897:                        break;
                   14898:                default:
                   14899:                        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));
                   14900:                        break;
                   14901:                }
                   14902:                break;
1.1.1.19  root     14903:        case 0x68:
                   14904:                // dummy interrupt for EMS (int 67h)
                   14905:                switch(REG8(AH)) {
                   14906:                case 0x40: msdos_int_67h_40h(); break;
                   14907:                case 0x41: msdos_int_67h_41h(); break;
                   14908:                case 0x42: msdos_int_67h_42h(); break;
                   14909:                case 0x43: msdos_int_67h_43h(); break;
                   14910:                case 0x44: msdos_int_67h_44h(); break;
                   14911:                case 0x45: msdos_int_67h_45h(); break;
                   14912:                case 0x46: msdos_int_67h_46h(); break;
                   14913:                case 0x47: msdos_int_67h_47h(); break;
                   14914:                case 0x48: msdos_int_67h_48h(); break;
1.1.1.20  root     14915:                // 0x49: LIM EMS - Reserved - Get I/O Port Address (Undocumented in EMS 3.2)
                   14916:                // 0x4a: LIM EMS - Reserved - Get Translation Array (Undocumented in EMS 3.2)
1.1.1.19  root     14917:                case 0x4b: msdos_int_67h_4bh(); break;
                   14918:                case 0x4c: msdos_int_67h_4ch(); break;
                   14919:                case 0x4d: msdos_int_67h_4dh(); break;
1.1.1.20  root     14920:                case 0x4e: msdos_int_67h_4eh(); break;
1.1.1.21  root     14921:                case 0x4f: msdos_int_67h_4fh(); break;
1.1.1.20  root     14922:                case 0x50: msdos_int_67h_50h(); break;
1.1.1.19  root     14923:                case 0x51: msdos_int_67h_51h(); break;
1.1.1.20  root     14924:                case 0x52: msdos_int_67h_52h(); break;
1.1.1.19  root     14925:                case 0x53: msdos_int_67h_53h(); break;
                   14926:                case 0x54: msdos_int_67h_54h(); break;
1.1.1.20  root     14927:                // 0x55: LIM EMS 4.0 - Alter Page Map And JUMP
                   14928:                // 0x56: LIM EMS 4.0 - Alter Page Map And CALL
                   14929:                case 0x57: msdos_int_67h_57h(); break;
                   14930:                case 0x58: msdos_int_67h_58h(); break;
                   14931:                // 0x59: LIM EMS 4.0 - Get Expanded Memory Hardware Information (for DOS Kernel)
                   14932:                case 0x5a: msdos_int_67h_5ah(); break;
                   14933:                // 0x5b: LIM EMS 4.0 - Alternate Map Register Set (for DOS Kernel)
                   14934:                // 0x5c: LIM EMS 4.0 - Prepate Expanded Memory Hardware For Warm Boot
                   14935:                // 0x5d: LIM EMS 4.0 - Enable/Disable OS Function Set Functions (for DOS Kernel)
1.1.1.31  root     14936:                // 0x60: EEMS - Get Physical Window Array
                   14937:                // 0x61: EEMS - Generic Accelerator Card Support
                   14938:                // 0x68: EEMS - Get Address of All Pge Frames om System
                   14939:                // 0x69: EEMS - Map Page into Frame
                   14940:                // 0x6a: EEMS - Page Mapping
                   14941:                // 0xde: VCPI
1.1.1.30  root     14942:                case 0xde: msdos_int_67h_deh(); break;
1.1.1.19  root     14943:                default:
1.1.1.22  root     14944:                        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     14945:                        REG8(AH) = 0x84;
                   14946:                        break;
                   14947:                }
                   14948:                break;
                   14949: #ifdef SUPPORT_XMS
                   14950:        case 0x69:
                   14951:                // dummy interrupt for XMS (call far)
1.1.1.28  root     14952:                try {
                   14953:                        switch(REG8(AH)) {
                   14954:                        case 0x00: msdos_call_xms_00h(); break;
                   14955:                        case 0x01: msdos_call_xms_01h(); break;
                   14956:                        case 0x02: msdos_call_xms_02h(); break;
                   14957:                        case 0x03: msdos_call_xms_03h(); break;
                   14958:                        case 0x04: msdos_call_xms_04h(); break;
                   14959:                        case 0x05: msdos_call_xms_05h(); break;
                   14960:                        case 0x06: msdos_call_xms_06h(); break;
                   14961:                        case 0x07: msdos_call_xms_07h(); break;
                   14962:                        case 0x08: msdos_call_xms_08h(); break;
                   14963:                        case 0x09: msdos_call_xms_09h(); break;
                   14964:                        case 0x0a: msdos_call_xms_0ah(); break;
                   14965:                        case 0x0b: msdos_call_xms_0bh(); break;
                   14966:                        case 0x0c: msdos_call_xms_0ch(); break;
                   14967:                        case 0x0d: msdos_call_xms_0dh(); break;
                   14968:                        case 0x0e: msdos_call_xms_0eh(); break;
                   14969:                        case 0x0f: msdos_call_xms_0fh(); break;
                   14970:                        case 0x10: msdos_call_xms_10h(); break;
                   14971:                        case 0x11: msdos_call_xms_11h(); break;
                   14972:                        case 0x12: msdos_call_xms_12h(); break;
1.1.1.29  root     14973: #if defined(HAS_I386)
                   14974:                        case 0x88: msdos_call_xms_88h(); break;
                   14975:                        case 0x89: msdos_call_xms_89h(); break;
                   14976:                        case 0x8e: msdos_call_xms_8eh(); break;
                   14977:                        case 0x8f: msdos_call_xms_8fh(); break;
                   14978: #endif
1.1.1.28  root     14979:                        default:
                   14980:                                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));
                   14981:                                REG16(AX) = 0x0000;
                   14982:                                REG8(BL) = 0x80; // function not implemented
                   14983:                                break;
                   14984:                        }
                   14985:                } catch(...) {
1.1.1.19  root     14986:                        REG16(AX) = 0x0000;
1.1.1.28  root     14987:                        REG8(BL) = 0x8f; // unrecoverable driver error
1.1.1.19  root     14988:                }
                   14989:                break;
                   14990: #endif
                   14991:        case 0x6a:
1.1.1.24  root     14992:                // irq12 (mouse)
                   14993:                mouse_push_ax = REG16(AX);
                   14994:                mouse_push_bx = REG16(BX);
                   14995:                mouse_push_cx = REG16(CX);
                   14996:                mouse_push_dx = REG16(DX);
                   14997:                mouse_push_si = REG16(SI);
                   14998:                mouse_push_di = REG16(DI);
                   14999:                
1.1.1.34  root     15000:                if(/*mouse.hidden == 0 && */mouse.call_addr.dw != 0) {
1.1.1.24  root     15001:                        REG16(AX) = mouse.status_irq;
                   15002:                        REG16(BX) = mouse.get_buttons();
1.1.1.34  root     15003:                        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   15004:                        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
1.1.1.24  root     15005:                        REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
                   15006:                        REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
                   15007:                        
                   15008:                        mem[0xfffd0 + 0x02] = 0x9a;     // call far
                   15009:                        mem[0xfffd0 + 0x03] = mouse.call_addr.w.l & 0xff;
                   15010:                        mem[0xfffd0 + 0x04] = mouse.call_addr.w.l >> 8;
                   15011:                        mem[0xfffd0 + 0x05] = mouse.call_addr.w.h & 0xff;
                   15012:                        mem[0xfffd0 + 0x06] = mouse.call_addr.w.h >> 8;
                   15013:                } else {
                   15014:                        mem[0xfffd0 + 0x02] = 0x90;     // nop
                   15015:                        mem[0xfffd0 + 0x03] = 0x90;     // nop
                   15016:                        mem[0xfffd0 + 0x04] = 0x90;     // nop
                   15017:                        mem[0xfffd0 + 0x05] = 0x90;     // nop
                   15018:                        mem[0xfffd0 + 0x06] = 0x90;     // nop
                   15019:                }
                   15020:                break;
                   15021:        case 0x6b:
                   15022:                // end of irq12 (mouse)
                   15023:                REG16(AX) = mouse_push_ax;
                   15024:                REG16(BX) = mouse_push_bx;
                   15025:                REG16(CX) = mouse_push_cx;
                   15026:                REG16(DX) = mouse_push_dx;
                   15027:                REG16(SI) = mouse_push_si;
                   15028:                REG16(DI) = mouse_push_di;
                   15029:                
                   15030:                // EOI
                   15031:                if((pic[1].isr &= ~(1 << 4)) == 0) {
                   15032:                        pic[0].isr &= ~(1 << 2); // master
                   15033:                }
                   15034:                pic_update();
                   15035:                break;
                   15036:        case 0x6c:
1.1.1.19  root     15037:                // dummy interrupt for case map routine pointed in the country info
                   15038:                if(REG8(AL) >= 0x80) {
                   15039:                        char tmp[2] = {0};
                   15040:                        tmp[0] = REG8(AL);
                   15041:                        my_strupr(tmp);
                   15042:                        REG8(AL) = tmp[0];
                   15043:                }
                   15044:                break;
1.1.1.27  root     15045:        case 0x6d:
                   15046:                // dummy interrupt for font read routine pointed by int 15h, ax=5000h
                   15047:                REG8(AL) = 0x86; // not supported
                   15048:                m_CF = 1;
                   15049:                break;
1.1.1.32  root     15050:        case 0x6e:
                   15051:                // dummy interrupt for parameter error message read routine pointed by int 2fh, ax=122eh, dl=08h
                   15052:                {
                   15053:                        UINT16 code = REG16(AX);
                   15054:                        if(code & 0xf0) {
                   15055:                                code = (code & 7) | ((code & 0x10) >> 1);
                   15056:                        }
                   15057:                        for(int i = 0; i < array_length(param_error_table); i++) {
                   15058:                                if(param_error_table[i].code == code || param_error_table[i].code == (UINT16)-1) {
                   15059:                                        const char *message = NULL;
                   15060:                                        if(active_code_page == 932) {
                   15061:                                                message = param_error_table[i].message_japanese;
                   15062:                                        }
                   15063:                                        if(message == NULL) {
                   15064:                                                message = param_error_table[i].message_english;
                   15065:                                        }
                   15066:                                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   15067:                                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   15068:                                        
                   15069:                                        SREG(ES) = WORK_TOP >> 4;
                   15070:                                        i386_load_segment_descriptor(ES);
                   15071:                                        REG16(DI) = 0x0000;
                   15072:                                        break;
                   15073:                                }
                   15074:                        }
                   15075:                }
                   15076:                break;
1.1.1.8   root     15077:        case 0x70:
                   15078:        case 0x71:
                   15079:        case 0x72:
                   15080:        case 0x73:
                   15081:        case 0x74:
                   15082:        case 0x75:
                   15083:        case 0x76:
                   15084:        case 0x77:
                   15085:                // EOI
                   15086:                if((pic[1].isr &= ~(1 << (num - 0x70))) == 0) {
                   15087:                        pic[0].isr &= ~(1 << 2); // master
                   15088:                }
                   15089:                pic_update();
                   15090:                break;
1.1       root     15091:        default:
1.1.1.22  root     15092: //             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     15093:                break;
                   15094:        }
                   15095:        
                   15096:        // update cursor position
                   15097:        if(cursor_moved) {
1.1.1.36  root     15098:                pcbios_update_cursor_position();
1.1       root     15099:                cursor_moved = false;
                   15100:        }
                   15101: }
                   15102: 
                   15103: // init
                   15104: 
                   15105: int msdos_init(int argc, char *argv[], char *envp[], int standard_env)
                   15106: {
                   15107:        // init file handler
                   15108:        memset(file_handler, 0, sizeof(file_handler));
                   15109:        msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   15110:        msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   15111:        msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
1.1.1.21  root     15112: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     15113:        if(_open("stdaux.txt", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
1.1.1.21  root     15114: #else
                   15115:        if(_open("NUL", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
                   15116: #endif
                   15117:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
1.1       root     15118:        }
1.1.1.21  root     15119:        if(_open("NUL", _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 4) {
1.1.1.37  root     15120:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0, 0, 1); // LPT1
1.1.1.21  root     15121:        }
1.1       root     15122:        _dup2(0, DUP_STDIN);
                   15123:        _dup2(1, DUP_STDOUT);
                   15124:        _dup2(2, DUP_STDERR);
1.1.1.21  root     15125:        _dup2(3, DUP_STDAUX);
                   15126:        _dup2(4, DUP_STDPRN);
1.1       root     15127:        
1.1.1.24  root     15128:        // init mouse
                   15129:        memset(&mouse, 0, sizeof(mouse));
1.1.1.34  root     15130:        mouse.enabled = true;   // from DOSBox
                   15131:        mouse.hidden = 1;       // hidden in default ???
                   15132:        mouse.old_hidden = 1;   // from DOSBox
                   15133:        mouse.max_position.x = 8 * (scr_width  - 1);
                   15134:        mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24  root     15135:        mouse.mickey.x = 8;
                   15136:        mouse.mickey.y = 16;
                   15137:        
1.1.1.26  root     15138: #ifdef SUPPORT_XMS
                   15139:        // init xms
                   15140:        msdos_xms_init();
                   15141: #endif
                   15142:        
1.1       root     15143:        // init process
                   15144:        memset(process, 0, sizeof(process));
                   15145:        
1.1.1.13  root     15146:        // init dtainfo
                   15147:        msdos_dta_info_init();
                   15148:        
1.1       root     15149:        // init memory
                   15150:        memset(mem, 0, sizeof(mem));
                   15151:        
                   15152:        // bios data area
1.1.1.23  root     15153:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     15154:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   15155:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.14  root     15156:        CONSOLE_FONT_INFO cfi;
                   15157:        GetCurrentConsoleFont(hStdout, FALSE, &cfi);
                   15158:        
                   15159:        int regen = min(scr_width * scr_height * 2, 0x8000);
                   15160:        text_vram_top_address = TEXT_VRAM_TOP;
                   15161:        text_vram_end_address = text_vram_top_address + regen;
                   15162:        shadow_buffer_top_address = SHADOW_BUF_TOP;
                   15163:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
                   15164:        
                   15165:        if(regen > 0x4000) {
                   15166:                regen = 0x8000;
                   15167:                vram_pages = 1;
                   15168:        } else if(regen > 0x2000) {
                   15169:                regen = 0x4000;
                   15170:                vram_pages = 2;
                   15171:        } else if(regen > 0x1000) {
                   15172:                regen = 0x2000;
                   15173:                vram_pages = 4;
                   15174:        } else {
                   15175:                regen = 0x1000;
                   15176:                vram_pages = 8;
                   15177:        }
1.1       root     15178:        
1.1.1.25  root     15179:        *(UINT16 *)(mem + 0x400) = 0x3f8; // com1 port address
                   15180:        *(UINT16 *)(mem + 0x402) = 0x2f8; // com2 port address
1.1.1.29  root     15181:        *(UINT16 *)(mem + 0x404) = 0x3e8; // com3 port address
                   15182:        *(UINT16 *)(mem + 0x406) = 0x2e8; // com4 port address
1.1.1.25  root     15183:        *(UINT16 *)(mem + 0x408) = 0x378; // lpt1 port address
1.1.1.37  root     15184:        *(UINT16 *)(mem + 0x40a) = 0x278; // lpt2 port address
                   15185:        *(UINT16 *)(mem + 0x40c) = 0x3bc; // lpt3 port address
1.1.1.32  root     15186: #ifdef EXT_BIOS_TOP
1.1.1.25  root     15187:        *(UINT16 *)(mem + 0x40e) = EXT_BIOS_TOP >> 4;
1.1.1.32  root     15188: #endif
1.1.1.26  root     15189:        *(UINT16 *)(mem + 0x410) = msdos_get_equipment();
1.1.1.33  root     15190:        *(UINT16 *)(mem + 0x413) = MEMORY_END >> 10;
1.1       root     15191:        *(UINT8  *)(mem + 0x449) = 0x03;//0x73;
1.1.1.14  root     15192:        *(UINT16 *)(mem + 0x44a) = csbi.dwSize.X;
                   15193:        *(UINT16 *)(mem + 0x44c) = regen;
1.1       root     15194:        *(UINT16 *)(mem + 0x44e) = 0;
                   15195:        *(UINT8  *)(mem + 0x450) = csbi.dwCursorPosition.X;
1.1.1.14  root     15196:        *(UINT8  *)(mem + 0x451) = csbi.dwCursorPosition.Y - scr_top;
1.1       root     15197:        *(UINT8  *)(mem + 0x460) = 7;
                   15198:        *(UINT8  *)(mem + 0x461) = 7;
                   15199:        *(UINT8  *)(mem + 0x462) = 0;
                   15200:        *(UINT16 *)(mem + 0x463) = 0x3d4;
1.1.1.19  root     15201:        *(UINT8  *)(mem + 0x465) = 0x09;
                   15202:        *(UINT32 *)(mem + 0x46c) = get_ticks_since_midnight(timeGetTime());
1.1.1.14  root     15203:        *(UINT8  *)(mem + 0x484) = csbi.srWindow.Bottom - csbi.srWindow.Top;
                   15204:        *(UINT8  *)(mem + 0x485) = cfi.dwFontSize.Y;
                   15205:        *(UINT8  *)(mem + 0x487) = 0x60;
                   15206:        *(UINT8  *)(mem + 0x496) = 0x10; // enhanced keyboard installed
1.1.1.32  root     15207: #ifdef EXT_BIOS_TOP
1.1.1.25  root     15208:        *(UINT16 *)(mem + EXT_BIOS_TOP) = 1;
1.1.1.32  root     15209: #endif
1.1.1.14  root     15210:        
                   15211:        // initial screen
                   15212:        SMALL_RECT rect;
                   15213:        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   15214:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   15215:        for(int y = 0, ofs1 = TEXT_VRAM_TOP, ofs2 = SHADOW_BUF_TOP; y < scr_height; y++) {
                   15216:                for(int x = 0; x < scr_width; x++) {
                   15217:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Char.AsciiChar;
                   15218:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Attributes;
                   15219:                }
                   15220:        }
1.1       root     15221:        
1.1.1.19  root     15222:        // init mcb
1.1       root     15223:        int seg = MEMORY_TOP >> 4;
1.1.1.19  root     15224:        
                   15225:        // iret table
                   15226:        // note: int 2eh vector should address the routine in command.com,
                   15227:        // and some softwares invite (int 2eh vector segment) - 1 must address the mcb of command.com.
                   15228:        // so move iret table into allocated memory block
                   15229:        // http://www5c.biglobe.ne.jp/~ecb/assembler2/2_6.html
1.1.1.33  root     15230:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, IRET_SIZE >> 4);
1.1.1.19  root     15231:        IRET_TOP = seg << 4;
                   15232:        seg += IRET_SIZE >> 4;
1.1.1.25  root     15233:        memset(mem + IRET_TOP, 0xcf, IRET_SIZE); // iret
1.1.1.19  root     15234:        
                   15235:        // dummy xms/ems device
1.1.1.33  root     15236:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, XMS_SIZE >> 4);
1.1.1.19  root     15237:        XMS_TOP = seg << 4;
                   15238:        seg += XMS_SIZE >> 4;
                   15239:        
                   15240:        // environment
1.1.1.33  root     15241:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, ENV_SIZE >> 4);
1.1       root     15242:        int env_seg = seg;
                   15243:        int ofs = 0;
1.1.1.32  root     15244:        char env_append[ENV_SIZE] = {0}, append_added = 0;
                   15245:        char comspec_added = 0;
1.1.1.33  root     15246:        char lastdrive_added = 0;
1.1.1.32  root     15247:        char env_msdos_path[ENV_SIZE] = {0};
                   15248:        char env_path[ENV_SIZE] = {0}, path_added = 0;
1.1.1.33  root     15249:        char prompt_added = 0;
1.1.1.32  root     15250:        char env_temp[ENV_SIZE] = {0}, temp_added = 0, tmp_added = 0;
1.1.1.33  root     15251:        char tz_added = 0;
1.1.1.32  root     15252:        char *path, *short_path;
                   15253:        
                   15254:        if((path = getenv("MSDOS_APPEND")) != NULL) {
                   15255:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   15256:                        strcpy(env_append, short_path);
                   15257:                }
                   15258:        }
                   15259:        if((path = getenv("APPEND")) != NULL) {
                   15260:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   15261:                        if(env_append[0] != '\0') {
                   15262:                                strcat(env_append, ";");
                   15263:                        }
                   15264:                        strcat(env_append, short_path);
                   15265:                }
                   15266:        }
                   15267:        
                   15268:        if((path = msdos_search_command_com(argv[0], env_path)) != NULL) {
                   15269:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   15270:                        strcpy(comspec_path, short_path);
                   15271:                }
                   15272:        }
                   15273:        if((path = getenv("MSDOS_COMSPEC")) != NULL && _access(path, 0) == 0) {
                   15274:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   15275:                        strcpy(comspec_path, short_path);
                   15276:                }
                   15277:        }
1.1       root     15278:        
1.1.1.28  root     15279:        if((path = getenv("MSDOS_PATH")) != NULL) {
1.1.1.32  root     15280:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   15281:                        strcpy(env_msdos_path, short_path);
                   15282:                        strcpy(env_path, short_path);
1.1.1.14  root     15283:                }
                   15284:        }
1.1.1.28  root     15285:        if((path = getenv("PATH")) != NULL) {
1.1.1.32  root     15286:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   15287:                        if(env_path[0] != '\0') {
                   15288:                                strcat(env_path, ";");
                   15289:                        }
                   15290:                        strcat(env_path, short_path);
1.1.1.9   root     15291:                }
                   15292:        }
1.1.1.32  root     15293:        
                   15294:        if(GetTempPath(ENV_SIZE, env_temp) != 0) {
                   15295:                strcpy(env_temp, msdos_get_multiple_short_path(env_temp));
1.1.1.15  root     15296:        }
1.1.1.32  root     15297:        for(int i = 0; i < 4; i++) {
                   15298:                static const char *name[4] = {"MSDOS_TEMP", "MSDOS_TMP", "TEMP", "TMP"};
                   15299:                if((path = getenv(name[i])) != NULL && _access(path, 0) == 0) {
                   15300:                        if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   15301:                                strcpy(env_temp, short_path);
                   15302:                                break;
                   15303:                        }
                   15304:                }
1.1.1.24  root     15305:        }
1.1.1.32  root     15306:        
1.1.1.9   root     15307:        for(char **p = envp; p != NULL && *p != NULL; p++) {
1.1       root     15308:                // lower to upper
1.1.1.28  root     15309:                char tmp[ENV_SIZE], name[ENV_SIZE];
1.1       root     15310:                strcpy(tmp, *p);
                   15311:                for(int i = 0;; i++) {
                   15312:                        if(tmp[i] == '=') {
                   15313:                                tmp[i] = '\0';
                   15314:                                sprintf(name, ";%s;", tmp);
1.1.1.25  root     15315:                                my_strupr(name);
1.1       root     15316:                                tmp[i] = '=';
                   15317:                                break;
                   15318:                        } else if(tmp[i] >= 'a' && tmp[i] <= 'z') {
1.1.1.28  root     15319:                                tmp[i] = (tmp[i] - 'a') + 'A';
1.1       root     15320:                        }
                   15321:                }
1.1.1.33  root     15322:                if(strstr(";MSDOS_APPEND;MSDOS_COMSPEC;MSDOS_LASTDRIVE;MSDOS_TEMP;MSDOS_TMP;MSDOS_TZ;", name) != NULL) {
                   15323:                        // ignore MSDOS_(APPEND/COMSPEC/LASTDRIVE/TEMP/TMP/TZ)
                   15324:                } else if(standard_env && strstr(";APPEND;COMSPEC;LASTDRIVE;MSDOS_PATH;PATH;PROMPT;TEMP;TMP;TZ;", name) == NULL) {
1.1.1.18  root     15325:                        // ignore non standard environments
                   15326:                } else {
1.1.1.33  root     15327:                        if(strncmp(tmp, "APPEND=", 7) == 0) {
1.1.1.32  root     15328:                                if(env_append[0] != '\0') {
                   15329:                                        sprintf(tmp, "APPEND=%s", env_append);
                   15330:                                } else {
                   15331:                                        sprintf(tmp, "APPEND=%s", msdos_get_multiple_short_path(tmp + 7));
                   15332:                                }
                   15333:                                append_added = 1;
                   15334:                        } else if(strncmp(tmp, "COMSPEC=", 8) == 0) {
1.1.1.14  root     15335:                                strcpy(tmp, "COMSPEC=C:\\COMMAND.COM");
1.1.1.32  root     15336:                                comspec_added = 1;
1.1.1.33  root     15337:                        } else if(strncmp(tmp, "LASTDRIVE=", 10) == 0) {
                   15338:                                char *env = getenv("MSDOS_LASTDRIVE");
                   15339:                                if(env != NULL) {
                   15340:                                        sprintf(tmp, "LASTDRIVE=%s", env);
                   15341:                                }
                   15342:                                lastdrive_added = 1;
                   15343:                        } else if(strncmp(tmp, "MSDOS_PATH=", 11) == 0) {
1.1.1.28  root     15344:                                if(env_msdos_path[0] != '\0') {
                   15345:                                        sprintf(tmp, "MSDOS_PATH=%s", env_msdos_path);
                   15346:                                } else {
                   15347:                                        sprintf(tmp, "MSDOS_PATH=%s", msdos_get_multiple_short_path(tmp + 11));
                   15348:                                }
1.1.1.33  root     15349:                        } else if(strncmp(tmp, "PATH=", 5) == 0) {
1.1.1.28  root     15350:                                if(env_path[0] != '\0') {
                   15351:                                        sprintf(tmp, "PATH=%s", env_path);
                   15352:                                } else {
                   15353:                                        sprintf(tmp, "PATH=%s", msdos_get_multiple_short_path(tmp + 5));
                   15354:                                }
1.1.1.32  root     15355:                                path_added = 1;
1.1.1.33  root     15356:                        } else if(strncmp(tmp, "PROMPT=", 7) == 0) {
                   15357:                                prompt_added = 1;
1.1.1.28  root     15358:                        } else if(strncmp(tmp, "TEMP=", 5) == 0) {
                   15359:                                if(env_temp[0] != '\0') {
                   15360:                                        sprintf(tmp, "TEMP=%s", env_temp);
                   15361:                                } else {
                   15362:                                        sprintf(tmp, "TEMP=%s", msdos_get_multiple_short_path(tmp + 5));
                   15363:                                }
1.1.1.32  root     15364:                                temp_added = 1;
1.1.1.33  root     15365:                        } else if(strncmp(tmp, "TMP=", 4) == 0) {
1.1.1.28  root     15366:                                if(env_temp[0] != '\0') {
                   15367:                                        sprintf(tmp, "TMP=%s", env_temp);
                   15368:                                } else {
                   15369:                                        sprintf(tmp, "TMP=%s", msdos_get_multiple_short_path(tmp + 4));
1.1       root     15370:                                }
1.1.1.32  root     15371:                                tmp_added = 1;
1.1.1.33  root     15372:                        } else if(strncmp(tmp, "TZ=", 3) == 0) {
                   15373:                                char *env = getenv("MSDOS_TZ");
                   15374:                                if(env != NULL) {
                   15375:                                        sprintf(tmp, "TZ=%s", env);
                   15376:                                }
                   15377:                                tz_added = 1;
1.1       root     15378:                        }
                   15379:                        int len = strlen(tmp);
1.1.1.14  root     15380:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) {
1.1       root     15381:                                fatalerror("too many environments\n");
                   15382:                        }
                   15383:                        memcpy(mem + (seg << 4) + ofs, tmp, len);
                   15384:                        ofs += len + 1;
                   15385:                }
                   15386:        }
1.1.1.32  root     15387:        if(!append_added && env_append[0] != '\0') {
                   15388:                #define SET_ENV(name, value) { \
                   15389:                        char tmp[ENV_SIZE]; \
                   15390:                        sprintf(tmp, "%s=%s", name, value); \
                   15391:                        int len = strlen(tmp); \
                   15392:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) { \
                   15393:                                fatalerror("too many environments\n"); \
                   15394:                        } \
                   15395:                        memcpy(mem + (seg << 4) + ofs, tmp, len); \
                   15396:                        ofs += len + 1; \
                   15397:                }
                   15398:                SET_ENV("APPEND", env_append);
                   15399:        }
                   15400:        if(!comspec_added) {
                   15401:                SET_ENV("COMSPEC", "C:\\COMMAND.COM");
                   15402:        }
1.1.1.33  root     15403:        if(!lastdrive_added) {
                   15404:                SET_ENV("LASTDRIVE", "Z");
                   15405:        }
1.1.1.32  root     15406:        if(!path_added) {
                   15407:                SET_ENV("PATH", env_path);
                   15408:        }
1.1.1.33  root     15409:        if(!prompt_added) {
                   15410:                SET_ENV("PROMPT", "$P$G");
                   15411:        }
1.1.1.32  root     15412:        if(!temp_added) {
                   15413:                SET_ENV("TEMP", env_temp);
                   15414:        }
                   15415:        if(!tmp_added) {
                   15416:                SET_ENV("TMP", env_temp);
                   15417:        }
1.1.1.33  root     15418:        if(!tz_added) {
                   15419:                TIME_ZONE_INFORMATION tzi;
                   15420:                HKEY hKey, hSubKey;
                   15421:                char tzi_std_name[64];
                   15422:                char tz_std[8] = "GMT";
                   15423:                char tz_dlt[8] = "GST";
                   15424:                char tz_value[32];
                   15425:                
                   15426:                // timezone name from GetTimeZoneInformation may not be english
                   15427:                bool daylight = (GetTimeZoneInformation(&tzi) != TIME_ZONE_ID_UNKNOWN);
                   15428:                setlocale(LC_CTYPE, "");
                   15429:                wcstombs(tzi_std_name, tzi.StandardName, sizeof(tzi_std_name));
                   15430:                
                   15431:                // get english timezone name from registry
                   15432:                if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones", 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
                   15433:                        for(DWORD i = 0; !tz_added; i++) {
                   15434:                                char reg_name[256], sub_key[1024], std_name[256];
                   15435:                                DWORD size;
                   15436:                                FILETIME ftTime;
                   15437:                                LONG result = RegEnumKeyEx(hKey, i, reg_name, &(size = array_length(reg_name)), NULL, NULL, NULL, &ftTime);
                   15438:                                
                   15439:                                if(result == ERROR_SUCCESS) {
                   15440:                                        sprintf(sub_key, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\%s", reg_name);
                   15441:                                        if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, sub_key, 0, KEY_QUERY_VALUE, &hSubKey) == ERROR_SUCCESS) {
                   15442:                                                if(RegQueryValueEx(hSubKey, "Std", NULL, NULL, (LPBYTE)std_name, &(size = array_length(std_name))) == ERROR_SUCCESS) {
                   15443:                                                        // search english timezone name from table
1.1.1.37  root     15444:                                                        if(strcmp(std_name, tzi_std_name) == 0) {
1.1.1.33  root     15445:                                                                for(int j = 0; j < array_length(tz_table); j++) {
                   15446:                                                                        if(strcmp(reg_name, tz_table[j].name) == 0 && (tz_table[j].lcid == 0 || tz_table[j].lcid == GetUserDefaultLCID())) {
                   15447:                                                                                if(tz_table[j].std != NULL) {
                   15448:                                                                                        strcpy(tz_std, tz_table[j].std);
                   15449:                                                                                }
                   15450:                                                                                if(tz_table[j].dlt != NULL) {
                   15451:                                                                                        strcpy(tz_dlt, tz_table[j].dlt);
                   15452:                                                                                }
                   15453:                                                                                tz_added = 1;
                   15454:                                                                                break;
                   15455:                                                                        }
                   15456:                                                                }
                   15457:                                                        }
                   15458:                                                }
                   15459:                                                RegCloseKey(hSubKey);
                   15460:                                        }
                   15461:                                } else if(result == ERROR_NO_MORE_ITEMS) {
                   15462:                                        break;
                   15463:                                }
                   15464:                        }
                   15465:                        RegCloseKey(hKey);
                   15466:                }
                   15467:                if((tzi.Bias % 60) != 0) {
                   15468:                        sprintf(tz_value, "%s%d:%2d", tz_std, tzi.Bias / 60, abs(tzi.Bias % 60));
                   15469:                } else {
                   15470:                        sprintf(tz_value, "%s%d", tz_std, tzi.Bias / 60);
                   15471:                }
                   15472:                if(daylight) {
                   15473:                        strcat(tz_value, tz_dlt);
                   15474:                }
                   15475:                SET_ENV("TZ", tz_value);
                   15476:        }
1.1       root     15477:        seg += (ENV_SIZE >> 4);
                   15478:        
                   15479:        // psp
1.1.1.33  root     15480:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, PSP_SIZE >> 4);
1.1       root     15481:        current_psp = seg;
1.1.1.35  root     15482:        psp_t *psp = msdos_psp_create(seg, seg + (PSP_SIZE >> 4), -1, env_seg);
                   15483:        psp->parent_psp = current_psp;
1.1       root     15484:        seg += (PSP_SIZE >> 4);
                   15485:        
1.1.1.19  root     15486:        // first free mcb in conventional memory
1.1.1.33  root     15487:        msdos_mcb_create(seg, 'M', 0, (MEMORY_END >> 4) - seg - 2);
1.1.1.8   root     15488:        first_mcb = seg;
                   15489:        
1.1.1.19  root     15490:        // dummy mcb to link to umb
1.1.1.33  root     15491: #if 0
1.1.1.39! root     15492:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC"); // link umb
1.1.1.33  root     15493: #else
1.1.1.39! root     15494:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC"); // unlink umb
1.1.1.33  root     15495: #endif
1.1.1.19  root     15496:        
                   15497:        // first free mcb in upper memory block
1.1.1.8   root     15498:        msdos_mcb_create(UMB_TOP >> 4, 'Z', 0, (UMB_END >> 4) - (UMB_TOP >> 4) - 1);
1.1       root     15499:        
1.1.1.29  root     15500: #ifdef SUPPORT_HMA
                   15501:        // first free mcb in high memory area
                   15502:        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   15503: #endif
                   15504:        
1.1.1.26  root     15505:        // interrupt vector
                   15506:        for(int i = 0; i < 0x80; i++) {
                   15507:                *(UINT16 *)(mem + 4 * i + 0) = i;
                   15508:                *(UINT16 *)(mem + 4 * i + 2) = (IRET_TOP >> 4);
                   15509:        }
1.1.1.35  root     15510:        *(UINT16 *)(mem + 4 * 0x08 + 0) = 0x0018;       // fffd:0018 irq0 (system timer)
1.1.1.26  root     15511:        *(UINT16 *)(mem + 4 * 0x08 + 2) = 0xfffd;
                   15512:        *(UINT16 *)(mem + 4 * 0x22 + 0) = 0x0000;       // ffff:0000 boot
                   15513:        *(UINT16 *)(mem + 4 * 0x22 + 2) = 0xffff;
                   15514:        *(UINT16 *)(mem + 4 * 0x67 + 0) = 0x0012;       // xxxx:0012 ems
                   15515:        *(UINT16 *)(mem + 4 * 0x67 + 2) = XMS_TOP >> 4;
                   15516:        *(UINT16 *)(mem + 4 * 0x74 + 0) = 0x0000;       // fffd:0000 irq12 (mouse)
                   15517:        *(UINT16 *)(mem + 4 * 0x74 + 2) = 0xfffd;
                   15518:        
1.1.1.29  root     15519:        // dummy devices (NUL -> CON -> ... -> CONFIG$ -> EMMXXXX0)
1.1.1.26  root     15520:        static const struct {
                   15521:                UINT16 attributes;
                   15522:                char *dev_name;
                   15523:        } dummy_devices[] = {
                   15524:                {0x8013, "CON     "},
                   15525:                {0x8000, "AUX     "},
                   15526:                {0xa0c0, "PRN     "},
                   15527:                {0x8008, "CLOCK$  "},
                   15528:                {0x8000, "COM1    "},
                   15529:                {0xa0c0, "LPT1    "},
                   15530:                {0xa0c0, "LPT2    "},
                   15531:                {0xa0c0, "LPT3    "},
                   15532:                {0x8000, "COM2    "},
                   15533:                {0x8000, "COM3    "},
                   15534:                {0x8000, "COM4    "},
1.1.1.30  root     15535: //             {0xc000, "CONFIG$ "},
                   15536:                {0xc000, "$IBMADSP"}, // for windows3.1 setup.exe
1.1.1.26  root     15537:        };
                   15538:        static const UINT8 dummy_device_routine[] = {
                   15539:                // from NUL device of Windows 98 SE
                   15540:                // or word ptr ES:[BX+03],0100
                   15541:                0x26, 0x81, 0x4f, 0x03, 0x00, 0x01,
                   15542:                // retf
                   15543:                0xcb,
                   15544:        };
1.1.1.29  root     15545:        device_t *last = NULL;
1.1.1.32  root     15546:        for(int i = 0; i < array_length(dummy_devices); i++) {
1.1.1.26  root     15547:                device_t *device = (device_t *)(mem + DEVICE_TOP + 22 + 18 * i);
1.1.1.29  root     15548:                device->next_driver.w.l = 22 + 18 * (i + 1);
                   15549:                device->next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     15550:                device->attributes = dummy_devices[i].attributes;
1.1.1.29  root     15551:                device->strategy = DEVICE_SIZE - sizeof(dummy_device_routine);
                   15552:                device->interrupt = DEVICE_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.26  root     15553:                memcpy(device->dev_name, dummy_devices[i].dev_name, 8);
1.1.1.29  root     15554:                last = device;
                   15555:        }
                   15556:        if(last != NULL) {
                   15557:                last->next_driver.w.l = 0;
                   15558:                last->next_driver.w.h = XMS_TOP >> 4;
1.1.1.26  root     15559:        }
1.1.1.29  root     15560:        memcpy(mem + DEVICE_TOP + DEVICE_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.26  root     15561:        
1.1.1.25  root     15562:        // dos info
                   15563:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   15564:        dos_info->magic_word = 1;
                   15565:        dos_info->first_mcb = MEMORY_TOP >> 4;
                   15566:        dos_info->first_dpb.w.l = 0;
                   15567:        dos_info->first_dpb.w.h = DPB_TOP >> 4;
                   15568:        dos_info->first_sft.w.l = 0;
                   15569:        dos_info->first_sft.w.h = SFT_TOP >> 4;
1.1.1.26  root     15570:        dos_info->clock_device.w.l = 22 + 18 * 3;       // CLOCK$ is the 3rd device in IO.SYS
1.1.1.25  root     15571:        dos_info->clock_device.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     15572:        dos_info->con_device.w.l = 22 + 18 * 0;         // CON is the 1st device in IO.SYS
1.1.1.25  root     15573:        dos_info->con_device.w.h = DEVICE_TOP >> 4;
                   15574:        dos_info->max_sector_len = 512;
                   15575:        dos_info->disk_buf_info.w.l = offsetof(dos_info_t, disk_buf_heads);
                   15576:        dos_info->disk_buf_info.w.h = DOS_INFO_TOP >> 4;
                   15577:        dos_info->cds.w.l = 0;
                   15578:        dos_info->cds.w.h = CDS_TOP >> 4;
                   15579:        dos_info->fcb_table.w.l = 0;
                   15580:        dos_info->fcb_table.w.h = FCB_TABLE_TOP >> 4;
                   15581:        dos_info->last_drive = 'Z' - 'A' + 1;
                   15582:        dos_info->buffers_x = 20;
                   15583:        dos_info->buffers_y = 0;
                   15584:        dos_info->boot_drive = 'C' - 'A' + 1;
1.1.1.29  root     15585:        dos_info->nul_device.next_driver.w.l = 22;
                   15586:        dos_info->nul_device.next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.25  root     15587:        dos_info->nul_device.attributes = 0x8004;
1.1.1.29  root     15588:        dos_info->nul_device.strategy = DOS_INFO_SIZE - sizeof(dummy_device_routine);
                   15589:        dos_info->nul_device.interrupt = DOS_INFO_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     15590:        memcpy(dos_info->nul_device.dev_name, "NUL     ", 8);
                   15591:        dos_info->disk_buf_heads.w.l = 0;
                   15592:        dos_info->disk_buf_heads.w.h = DISK_BUF_TOP >> 4;
1.1.1.39! root     15593:        dos_info->umb_linked = (((mcb_t *)(mem + MEMORY_END - 16))->mz == 'M' ? 0x01 : 0x00);
1.1.1.25  root     15594:        dos_info->first_umb_fcb = UMB_TOP >> 4;
                   15595:        dos_info->first_mcb_2 = MEMORY_TOP >> 4;
1.1.1.29  root     15596:        memcpy(mem + DOS_INFO_TOP + DOS_INFO_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.25  root     15597:        
                   15598:        char *env;
                   15599:        if((env = getenv("LASTDRIVE")) != NULL) {
                   15600:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   15601:                        dos_info->last_drive = env[0] - 'A' + 1;
                   15602:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   15603:                        dos_info->last_drive = env[0] - 'a' + 1;
                   15604:                }
                   15605:        }
                   15606:        if((env = getenv("windir")) != NULL) {
                   15607:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   15608:                        dos_info->boot_drive = env[0] - 'A' + 1;
                   15609:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   15610:                        dos_info->boot_drive = env[0] - 'a' + 1;
                   15611:                }
                   15612:        }
                   15613: #if defined(HAS_I386)
                   15614:        dos_info->i386_or_later = 1;
                   15615: #else
                   15616:        dos_info->i386_or_later = 0;
                   15617: #endif
                   15618:        dos_info->ext_mem_size = (min(MAX_MEM, 0x4000000) - 0x100000) >> 10;
                   15619:        
1.1.1.27  root     15620:        // ems (int 67h) and xms
1.1.1.25  root     15621:        device_t *xms_device = (device_t *)(mem + XMS_TOP);
                   15622:        xms_device->next_driver.w.l = 0xffff;
                   15623:        xms_device->next_driver.w.h = 0xffff;
                   15624:        xms_device->attributes = 0xc000;
1.1.1.29  root     15625:        xms_device->strategy = XMS_SIZE - sizeof(dummy_device_routine);
                   15626:        xms_device->interrupt = XMS_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     15627:        memcpy(xms_device->dev_name, "EMMXXXX0", 8);
                   15628:        
1.1.1.26  root     15629:        mem[XMS_TOP + 0x12] = 0xcd;     // int 68h (dummy)
                   15630:        mem[XMS_TOP + 0x13] = 0x68;
                   15631:        mem[XMS_TOP + 0x14] = 0xcf;     // iret
1.1.1.19  root     15632: #ifdef SUPPORT_XMS
                   15633:        if(support_xms) {
1.1.1.26  root     15634:                mem[XMS_TOP + 0x15] = 0xcd;     // int 69h (dummy)
                   15635:                mem[XMS_TOP + 0x16] = 0x69;
                   15636:                mem[XMS_TOP + 0x17] = 0xcb;     // retf
1.1.1.19  root     15637:        } else
                   15638: #endif
1.1.1.26  root     15639:        mem[XMS_TOP + 0x15] = 0xcb;     // retf
1.1.1.29  root     15640:        memcpy(mem + XMS_TOP + XMS_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.19  root     15641:        
1.1.1.26  root     15642:        // irq12 routine (mouse)
1.1.1.24  root     15643:        mem[0xfffd0 + 0x00] = 0xcd;     // int 6ah (dummy)
                   15644:        mem[0xfffd0 + 0x01] = 0x6a;
                   15645:        mem[0xfffd0 + 0x02] = 0x9a;     // call far mouse
                   15646:        mem[0xfffd0 + 0x03] = 0xff;
                   15647:        mem[0xfffd0 + 0x04] = 0xff;
                   15648:        mem[0xfffd0 + 0x05] = 0xff;
                   15649:        mem[0xfffd0 + 0x06] = 0xff;
                   15650:        mem[0xfffd0 + 0x07] = 0xcd;     // int 6bh (dummy)
                   15651:        mem[0xfffd0 + 0x08] = 0x6b;
                   15652:        mem[0xfffd0 + 0x09] = 0xcf;     // iret
                   15653:        
1.1.1.27  root     15654:        // case map routine
                   15655:        mem[0xfffd0 + 0x0a] = 0xcd;     // int 6ch (dummy)
                   15656:        mem[0xfffd0 + 0x0b] = 0x6c;
                   15657:        mem[0xfffd0 + 0x0c] = 0xcb;     // retf
                   15658:        
                   15659:        // font read routine
                   15660:        mem[0xfffd0 + 0x0d] = 0xcd;     // int 6dh (dummy)
                   15661:        mem[0xfffd0 + 0x0e] = 0x6d;
                   15662:        mem[0xfffd0 + 0x0f] = 0xcb;     // retf
1.1.1.19  root     15663:        
1.1.1.32  root     15664:        // error message read routine
                   15665:        mem[0xfffd0 + 0x10] = 0xcd;     // int 6eh (dummy)
                   15666:        mem[0xfffd0 + 0x11] = 0x6e;
                   15667:        mem[0xfffd0 + 0x12] = 0xcb;     // retf
                   15668:        
1.1.1.35  root     15669:        // dummy loop to wait BIOS/DOS service is done
                   15670:        mem[0xfffd0 + 0x13] = 0xe6;     // out f7h, al
                   15671:        mem[0xfffd0 + 0x14] = 0xf7;
                   15672:        mem[0xfffd0 + 0x15] = 0x78;     // js/jns -4
                   15673:        mem[0xfffd0 + 0x16] = 0xfc;
                   15674:        mem[0xfffd0 + 0x17] = 0xcb;     // retf
                   15675:        
1.1.1.26  root     15676:        // irq0 routine (system time)
1.1.1.35  root     15677:        mem[0xfffd0 + 0x18] = 0xcd;     // int 1ch
                   15678:        mem[0xfffd0 + 0x19] = 0x1c;
                   15679:        mem[0xfffd0 + 0x1a] = 0xea;     // jmp far (IRET_TOP >> 4):0008
                   15680:        mem[0xfffd0 + 0x1b] = 0x08;
                   15681:        mem[0xfffd0 + 0x1c] = 0x00;
                   15682:        mem[0xfffd0 + 0x1d] = ((IRET_TOP >> 4)     ) & 0xff;
                   15683:        mem[0xfffd0 + 0x1e] = ((IRET_TOP >> 4) >> 8) & 0xff;
1.1.1.14  root     15684:        
1.1.1.26  root     15685:        // boot routine
1.1       root     15686:        mem[0xffff0] = 0xf4;    // halt
                   15687:        mem[0xffff1] = 0xcd;    // int 21h
                   15688:        mem[0xffff2] = 0x21;
                   15689:        mem[0xffff3] = 0xcb;    // retf
                   15690:        
1.1.1.24  root     15691:        mem[0xffff5] = '0';     // rom date
                   15692:        mem[0xffff6] = '2';
                   15693:        mem[0xffff7] = '/';
                   15694:        mem[0xffff8] = '2';
                   15695:        mem[0xffff9] = '2';
                   15696:        mem[0xffffa] = '/';
                   15697:        mem[0xffffb] = '0';
                   15698:        mem[0xffffc] = '6';
                   15699:        mem[0xffffe] = 0xfc;    // machine id
                   15700:        mem[0xfffff] = 0x00;
                   15701:        
1.1       root     15702:        // param block
                   15703:        // + 0: param block (22bytes)
                   15704:        // +24: fcb1/2 (20bytes)
                   15705:        // +44: command tail (128bytes)
                   15706:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   15707:        param->env_seg = 0;
                   15708:        param->cmd_line.w.l = 44;
                   15709:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   15710:        param->fcb1.w.l = 24;
                   15711:        param->fcb1.w.h = (WORK_TOP >> 4);
                   15712:        param->fcb2.w.l = 24;
                   15713:        param->fcb2.w.h = (WORK_TOP >> 4);
                   15714:        
                   15715:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   15716:        
                   15717:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   15718:        if(argc > 1) {
                   15719:                sprintf(cmd_line->cmd, " %s", argv[1]);
                   15720:                for(int i = 2; i < argc; i++) {
                   15721:                        char tmp[128];
                   15722:                        sprintf(tmp, "%s %s", cmd_line->cmd, argv[i]);
                   15723:                        strcpy(cmd_line->cmd, tmp);
                   15724:                }
                   15725:                cmd_line->len = (UINT8)strlen(cmd_line->cmd);
                   15726:        } else {
                   15727:                cmd_line->len = 0;
                   15728:        }
                   15729:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   15730:        
                   15731:        // system file table
1.1.1.21  root     15732:        *(UINT32 *)(mem + SFT_TOP + 0) = 0xffffffff;
                   15733:        *(UINT16 *)(mem + SFT_TOP + 4) = 20;
1.1       root     15734:        
1.1.1.19  root     15735:        // disk buffer header (from DOSBox)
                   15736:        *(UINT16 *)(mem + DISK_BUF_TOP +  0) = 0xffff;          // forward ptr
                   15737:        *(UINT16 *)(mem + DISK_BUF_TOP +  2) = 0xffff;          // backward ptr
                   15738:        *(UINT8  *)(mem + DISK_BUF_TOP +  4) = 0xff;            // not in use
                   15739:        *(UINT8  *)(mem + DISK_BUF_TOP + 10) = 0x01;            // number of FATs
                   15740:        *(UINT32 *)(mem + DISK_BUF_TOP + 13) = 0xffffffff;      // pointer to DPB
                   15741:        
1.1       root     15742:        // current directory structure
                   15743:        msdos_cds_update(_getdrive() - 1);
                   15744:        
                   15745:        // fcb table
                   15746:        *(UINT32 *)(mem + FCB_TABLE_TOP + 0) = 0xffffffff;
1.1.1.14  root     15747:        *(UINT16 *)(mem + FCB_TABLE_TOP + 4) = 0;
1.1       root     15748:        
1.1.1.17  root     15749:        // nls stuff
                   15750:        msdos_nls_tables_init();
1.1       root     15751:        
                   15752:        // execute command
1.1.1.28  root     15753:        try {
                   15754:                if(msdos_process_exec(argv[0], param, 0)) {
                   15755:                        fatalerror("'%s' not found\n", argv[0]);
                   15756:                }
                   15757:        } catch(...) {
                   15758:                // we should not reach here :-(
                   15759:                fatalerror("failed to start '%s' because of unexpected exeption\n", argv[0]);
1.1       root     15760:        }
                   15761:        retval = 0;
                   15762:        return(0);
                   15763: }
                   15764: 
                   15765: #define remove_std_file(path) { \
                   15766:        int fd = _open(path, _O_RDONLY | _O_BINARY); \
                   15767:        if(fd != -1) { \
                   15768:                _lseek(fd, 0, SEEK_END); \
                   15769:                int size = _tell(fd); \
                   15770:                _close(fd); \
                   15771:                if(size == 0) { \
                   15772:                        remove(path); \
                   15773:                } \
                   15774:        } \
                   15775: }
                   15776: 
                   15777: void msdos_finish()
                   15778: {
                   15779:        for(int i = 0; i < MAX_FILES; i++) {
                   15780:                if(file_handler[i].valid) {
                   15781:                        _close(i);
                   15782:                }
                   15783:        }
1.1.1.21  root     15784: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     15785:        remove_std_file("stdaux.txt");
1.1.1.21  root     15786: #endif
1.1.1.30  root     15787: #ifdef SUPPORT_XMS
                   15788:        msdos_xms_finish();
                   15789: #endif
1.1       root     15790:        msdos_dbcs_table_finish();
                   15791: }
                   15792: 
                   15793: /* ----------------------------------------------------------------------------
                   15794:        PC/AT hardware emulation
                   15795: ---------------------------------------------------------------------------- */
                   15796: 
                   15797: void hardware_init()
                   15798: {
1.1.1.3   root     15799:        CPU_INIT_CALL(CPU_MODEL);
1.1       root     15800:        CPU_RESET_CALL(CPU_MODEL);
1.1.1.14  root     15801:        m_IF = 1;
1.1.1.3   root     15802: #if defined(HAS_I386)
1.1       root     15803:        cpu_type = (REG32(EDX) >> 8) & 0x0f;
                   15804:        cpu_step = (REG32(EDX) >> 0) & 0x0f;
1.1.1.3   root     15805: #endif
                   15806:        i386_set_a20_line(0);
1.1.1.14  root     15807:        
1.1.1.19  root     15808:        ems_init();
1.1.1.25  root     15809:        dma_init();
1.1       root     15810:        pic_init();
1.1.1.25  root     15811:        pio_init();
1.1.1.8   root     15812: #ifdef PIT_ALWAYS_RUNNING
                   15813:        pit_init();
                   15814: #else
1.1       root     15815:        pit_active = 0;
                   15816: #endif
1.1.1.25  root     15817:        sio_init();
1.1.1.8   root     15818:        cmos_init();
                   15819:        kbd_init();
1.1       root     15820: }
                   15821: 
1.1.1.10  root     15822: void hardware_finish()
                   15823: {
                   15824: #if defined(HAS_I386)
                   15825:        vtlb_free(m_vtlb);
                   15826: #endif
1.1.1.19  root     15827:        ems_finish();
1.1.1.37  root     15828:        pio_finish();
1.1.1.25  root     15829:        sio_finish();
1.1.1.10  root     15830: }
                   15831: 
1.1.1.28  root     15832: void hardware_release()
                   15833: {
                   15834:        // release hardware resources when this program will be terminated abnormally
                   15835: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     15836:        if(fp_debug_log != NULL) {
                   15837:                fclose(fp_debug_log);
                   15838:                fp_debug_log = NULL;
1.1.1.28  root     15839:        }
                   15840: #endif
                   15841: #if defined(HAS_I386)
                   15842:        vtlb_free(m_vtlb);
                   15843: #endif
                   15844:        ems_release();
1.1.1.37  root     15845:        pio_release();
1.1.1.28  root     15846:        sio_release();
                   15847: }
                   15848: 
1.1       root     15849: void hardware_run()
                   15850: {
1.1.1.22  root     15851: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.28  root     15852:        // open debug log file after msdos_init() is done not to use the standard file handlers
1.1.1.33  root     15853:        fp_debug_log = fopen("debug.log", "w");
1.1.1.22  root     15854: #endif
1.1.1.3   root     15855:        while(!m_halted) {
                   15856: #if defined(HAS_I386)
1.1       root     15857:                CPU_EXECUTE_CALL(i386);
1.1.1.14  root     15858:                if(m_eip != m_prev_eip) {
1.1.1.35  root     15859:                        idle_ops++;
                   15860:                }
1.1.1.14  root     15861: #else
1.1.1.35  root     15862:                CPU_EXECUTE_CALL(CPU_MODEL);
1.1.1.14  root     15863:                if(m_pc != m_prevpc) {
1.1.1.35  root     15864:                        idle_ops++;
1.1.1.14  root     15865:                }
1.1.1.35  root     15866: #endif
                   15867:                if(++update_ops == UPDATE_OPS) {
1.1       root     15868:                        hardware_update();
1.1.1.35  root     15869:                        update_ops = 0;
1.1       root     15870:                }
                   15871:        }
1.1.1.22  root     15872: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     15873:        if(fp_debug_log != NULL) {
                   15874:                fclose(fp_debug_log);
                   15875:                fp_debug_log = NULL;
1.1.1.28  root     15876:        }
1.1.1.22  root     15877: #endif
1.1       root     15878: }
                   15879: 
                   15880: void hardware_update()
                   15881: {
1.1.1.8   root     15882:        static UINT32 prev_time = 0;
                   15883:        UINT32 cur_time = timeGetTime();
                   15884:        
                   15885:        if(prev_time != cur_time) {
                   15886:                // update pit and raise irq0
                   15887: #ifndef PIT_ALWAYS_RUNNING
                   15888:                if(pit_active)
                   15889: #endif
                   15890:                {
                   15891:                        if(pit_run(0, cur_time)) {
                   15892:                                pic_req(0, 0, 1);
                   15893:                        }
                   15894:                        pit_run(1, cur_time);
                   15895:                        pit_run(2, cur_time);
                   15896:                }
1.1.1.24  root     15897:                
1.1.1.25  root     15898:                // update sio and raise irq4/3
1.1.1.29  root     15899:                for(int c = 0; c < 4; c++) {
1.1.1.25  root     15900:                        sio_update(c);
                   15901:                }
                   15902:                
1.1.1.24  root     15903:                // update keyboard and mouse
1.1.1.14  root     15904:                static UINT32 prev_tick = 0;
                   15905:                UINT32 cur_tick = cur_time / 32;
1.1.1.25  root     15906:                
1.1.1.14  root     15907:                if(prev_tick != cur_tick) {
                   15908:                        // update keyboard flags
                   15909:                        UINT8 state;
1.1.1.24  root     15910:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x0001) ? 0x80 : 0;
                   15911:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x0001) ? 0x40 : 0;
                   15912:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x0001) ? 0x20 : 0;
                   15913:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x0001) ? 0x10 : 0;
                   15914:                        state |= (GetAsyncKeyState(VK_MENU    ) & 0x8000) ? 0x08 : 0;
                   15915:                        state |= (GetAsyncKeyState(VK_CONTROL ) & 0x8000) ? 0x04 : 0;
                   15916:                        state |= (GetAsyncKeyState(VK_LSHIFT  ) & 0x8000) ? 0x02 : 0;
                   15917:                        state |= (GetAsyncKeyState(VK_RSHIFT  ) & 0x8000) ? 0x01 : 0;
1.1.1.14  root     15918:                        mem[0x417] = state;
                   15919:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x8000) ? 0x80 : 0;
                   15920:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   15921:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   15922:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
1.1.1.24  root     15923: //                     state |= (GetAsyncKeyState(VK_PAUSE   ) & 0x0001) ? 0x08 : 0;
                   15924: //                     state |= (GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x04 : 0;
1.1.1.14  root     15925:                        state |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   15926:                        state |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   15927:                        mem[0x418] = state;
                   15928:                        
1.1.1.24  root     15929:                        // update console input if needed
1.1.1.34  root     15930:                        if(!key_changed || mouse.hidden == 0) {
1.1.1.24  root     15931:                                update_console_input();
                   15932:                        }
                   15933:                        
                   15934:                        // raise irq1 if key is pressed/released
                   15935:                        if(key_changed) {
1.1.1.8   root     15936:                                pic_req(0, 1, 1);
1.1.1.24  root     15937:                                key_changed = false;
                   15938:                        }
                   15939:                        
                   15940:                        // raise irq12 if mouse status is changed
                   15941:                        if(mouse.status & mouse.call_mask) {
1.1.1.34  root     15942: //                             if(mouse.hidden == 0) {
1.1.1.24  root     15943:                                        pic_req(1, 4, 1);
                   15944:                                        mouse.status_irq = mouse.status & mouse.call_mask;
1.1.1.34  root     15945: //                             }
1.1.1.24  root     15946:                                mouse.status &= ~mouse.call_mask;
1.1.1.8   root     15947:                        }
1.1.1.24  root     15948:                        
1.1.1.14  root     15949:                        prev_tick = cur_tick;
1.1.1.8   root     15950:                }
1.1.1.24  root     15951:                
1.1.1.19  root     15952:                // update daily timer counter
                   15953:                pcbios_update_daily_timer_counter(cur_time);
1.1.1.25  root     15954:                
1.1.1.8   root     15955:                prev_time = cur_time;
1.1       root     15956:        }
                   15957: }
                   15958: 
1.1.1.19  root     15959: // ems
                   15960: 
                   15961: void ems_init()
                   15962: {
                   15963:        memset(ems_handles, 0, sizeof(ems_handles));
                   15964:        memset(ems_pages, 0, sizeof(ems_pages));
                   15965:        free_ems_pages = MAX_EMS_PAGES;
                   15966: }
                   15967: 
                   15968: void ems_finish()
                   15969: {
1.1.1.28  root     15970:        ems_release();
                   15971: }
                   15972: 
                   15973: void ems_release()
                   15974: {
1.1.1.31  root     15975:        for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.28  root     15976:                if(ems_handles[i].buffer != NULL) {
1.1.1.19  root     15977:                        free(ems_handles[i].buffer);
                   15978:                        ems_handles[i].buffer = NULL;
                   15979:                }
                   15980:        }
                   15981: }
                   15982: 
                   15983: void ems_allocate_pages(int handle, int pages)
                   15984: {
                   15985:        if(pages > 0) {
                   15986:                ems_handles[handle].buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   15987:        } else {
                   15988:                ems_handles[handle].buffer = NULL;
                   15989:        }
                   15990:        ems_handles[handle].pages = pages;
                   15991:        ems_handles[handle].allocated = true;
                   15992:        free_ems_pages -= pages;
                   15993: }
                   15994: 
                   15995: void ems_reallocate_pages(int handle, int pages)
                   15996: {
                   15997:        if(ems_handles[handle].allocated) {
                   15998:                if(ems_handles[handle].pages != pages) {
                   15999:                        UINT8 *new_buffer = NULL;
                   16000:                        
                   16001:                        if(pages > 0) {
                   16002:                                new_buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   16003:                        }
1.1.1.32  root     16004:                        if(ems_handles[handle].buffer != NULL) {
                   16005:                                if(new_buffer != NULL) {
1.1.1.19  root     16006:                                        if(pages > ems_handles[handle].pages) {
                   16007:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * ems_handles[handle].pages);
                   16008:                                        } else {
                   16009:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * pages);
                   16010:                                        }
                   16011:                                }
                   16012:                                free(ems_handles[handle].buffer);
                   16013:                                ems_handles[handle].buffer = NULL;
                   16014:                        }
                   16015:                        free_ems_pages += ems_handles[handle].pages;
                   16016:                        
                   16017:                        ems_handles[handle].buffer = new_buffer;
                   16018:                        ems_handles[handle].pages = pages;
                   16019:                        free_ems_pages -= pages;
                   16020:                }
                   16021:        } else {
                   16022:                ems_allocate_pages(handle, pages);
                   16023:        }
                   16024: }
                   16025: 
                   16026: void ems_release_pages(int handle)
                   16027: {
                   16028:        if(ems_handles[handle].allocated) {
1.1.1.32  root     16029:                if(ems_handles[handle].buffer != NULL) {
1.1.1.19  root     16030:                        free(ems_handles[handle].buffer);
                   16031:                        ems_handles[handle].buffer = NULL;
                   16032:                }
                   16033:                free_ems_pages += ems_handles[handle].pages;
                   16034:                ems_handles[handle].allocated = false;
                   16035:        }
                   16036: }
                   16037: 
                   16038: void ems_map_page(int physical, int handle, int logical)
                   16039: {
                   16040:        if(ems_pages[physical].mapped) {
                   16041:                if(ems_pages[physical].handle == handle && ems_pages[physical].page == logical) {
                   16042:                        return;
                   16043:                }
                   16044:                ems_unmap_page(physical);
                   16045:        }
1.1.1.32  root     16046:        if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     16047:                memcpy(mem + EMS_TOP + 0x4000 * physical, ems_handles[handle].buffer + 0x4000 * logical, 0x4000);
                   16048:        }
                   16049:        ems_pages[physical].handle = handle;
                   16050:        ems_pages[physical].page = logical;
                   16051:        ems_pages[physical].mapped = true;
                   16052: }
                   16053: 
                   16054: void ems_unmap_page(int physical)
                   16055: {
                   16056:        if(ems_pages[physical].mapped) {
                   16057:                int handle = ems_pages[physical].handle;
                   16058:                int logical = ems_pages[physical].page;
                   16059:                
1.1.1.32  root     16060:                if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     16061:                        memcpy(ems_handles[handle].buffer + 0x4000 * logical, mem + EMS_TOP + 0x4000 * physical, 0x4000);
                   16062:                }
                   16063:                ems_pages[physical].mapped = false;
                   16064:        }
                   16065: }
                   16066: 
1.1.1.25  root     16067: // dma
1.1       root     16068: 
1.1.1.25  root     16069: void dma_init()
1.1       root     16070: {
1.1.1.26  root     16071:        memset(dma, 0, sizeof(dma));
1.1.1.25  root     16072:        for(int c = 0; c < 2; c++) {
1.1.1.26  root     16073: //             for(int ch = 0; ch < 4; ch++) {
                   16074: //                     dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w = 0xffff;
                   16075: //             }
1.1.1.25  root     16076:                dma_reset(c);
                   16077:        }
1.1       root     16078: }
                   16079: 
1.1.1.25  root     16080: void dma_reset(int c)
1.1       root     16081: {
1.1.1.25  root     16082:        dma[c].low_high = false;
                   16083:        dma[c].cmd = dma[c].req = dma[c].tc = 0;
                   16084:        dma[c].mask = 0xff;
                   16085: }
                   16086: 
                   16087: void dma_write(int c, UINT32 addr, UINT8 data)
                   16088: {
                   16089:        int ch = (addr >> 1) & 3;
                   16090:        UINT8 bit = 1 << (data & 3);
                   16091:        
                   16092:        switch(addr & 0x0f) {
                   16093:        case 0x00: case 0x02: case 0x04: case 0x06:
                   16094:                if(dma[c].low_high) {
                   16095:                        dma[c].ch[ch].bareg.b.h = data;
1.1       root     16096:                } else {
1.1.1.25  root     16097:                        dma[c].ch[ch].bareg.b.l = data;
                   16098:                }
                   16099:                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   16100:                dma[c].low_high = !dma[c].low_high;
                   16101:                break;
                   16102:        case 0x01: case 0x03: case 0x05: case 0x07:
                   16103:                if(dma[c].low_high) {
                   16104:                        dma[c].ch[ch].bcreg.b.h = data;
                   16105:                } else {
                   16106:                        dma[c].ch[ch].bcreg.b.l = data;
                   16107:                }
                   16108:                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   16109:                dma[c].low_high = !dma[c].low_high;
                   16110:                break;
                   16111:        case 0x08:
                   16112:                // command register
                   16113:                dma[c].cmd = data;
                   16114:                break;
                   16115:        case 0x09:
                   16116:                // dma[c].request register
                   16117:                if(data & 4) {
                   16118:                        if(!(dma[c].req & bit)) {
                   16119:                                dma[c].req |= bit;
                   16120: //                             dma_run(c, ch);
                   16121:                        }
                   16122:                } else {
                   16123:                        dma[c].req &= ~bit;
                   16124:                }
                   16125:                break;
                   16126:        case 0x0a:
                   16127:                // single mask register
                   16128:                if(data & 4) {
                   16129:                        dma[c].mask |= bit;
                   16130:                } else {
                   16131:                        dma[c].mask &= ~bit;
                   16132:                }
                   16133:                break;
                   16134:        case 0x0b:
                   16135:                // mode register
                   16136:                dma[c].ch[data & 3].mode = data;
                   16137:                break;
                   16138:        case 0x0c:
                   16139:                dma[c].low_high = false;
                   16140:                break;
                   16141:        case 0x0d:
                   16142:                // clear master
                   16143:                dma_reset(c);
                   16144:                break;
                   16145:        case 0x0e:
                   16146:                // clear mask register
                   16147:                dma[c].mask = 0;
                   16148:                break;
                   16149:        case 0x0f:
                   16150:                // all mask register
                   16151:                dma[c].mask = data & 0x0f;
                   16152:                break;
                   16153:        }
                   16154: }
                   16155: 
                   16156: UINT8 dma_read(int c, UINT32 addr)
                   16157: {
                   16158:        int ch = (addr >> 1) & 3;
                   16159:        UINT8 val = 0xff;
                   16160:        
                   16161:        switch(addr & 0x0f) {
                   16162:        case 0x00: case 0x02: case 0x04: case 0x06:
                   16163:                if(dma[c].low_high) {
                   16164:                        val = dma[c].ch[ch].areg.b.h;
                   16165:                } else {
                   16166:                        val = dma[c].ch[ch].areg.b.l;
                   16167:                }
                   16168:                dma[c].low_high = !dma[c].low_high;
                   16169:                return(val);
                   16170:        case 0x01: case 0x03: case 0x05: case 0x07:
                   16171:                if(dma[c].low_high) {
                   16172:                        val = dma[c].ch[ch].creg.b.h;
                   16173:                } else {
                   16174:                        val = dma[c].ch[ch].creg.b.l;
                   16175:                }
                   16176:                dma[c].low_high = !dma[c].low_high;
                   16177:                return(val);
                   16178:        case 0x08:
                   16179:                // status register
                   16180:                val = (dma[c].req << 4) | dma[c].tc;
                   16181:                dma[c].tc = 0;
                   16182:                return(val);
                   16183:        case 0x0d:
1.1.1.26  root     16184:                // temporary register (intel 82374 does not support)
1.1.1.25  root     16185:                return(dma[c].tmp & 0xff);
1.1.1.26  root     16186:        case 0x0f:
                   16187:                // mask register (intel 82374 does support)
                   16188:                return(dma[c].mask);
1.1.1.25  root     16189:        }
                   16190:        return(0xff);
                   16191: }
                   16192: 
                   16193: void dma_page_write(int c, int ch, UINT8 data)
                   16194: {
                   16195:        dma[c].ch[ch].pagereg = data;
                   16196: }
                   16197: 
                   16198: UINT8 dma_page_read(int c, int ch)
                   16199: {
                   16200:        return(dma[c].ch[ch].pagereg);
                   16201: }
                   16202: 
                   16203: void dma_run(int c, int ch)
                   16204: {
                   16205:        UINT8 bit = 1 << ch;
                   16206:        
                   16207:        if((dma[c].req & bit) && !(dma[c].mask & bit)) {
                   16208:                // execute dma
                   16209:                while(dma[c].req & bit) {
                   16210:                        if(ch == 0 && (dma[c].cmd & 0x01)) {
                   16211:                                // memory -> memory
                   16212:                                UINT32 saddr = dma[c].ch[0].areg.w | (dma[c].ch[0].pagereg << 16);
                   16213:                                UINT32 daddr = dma[c].ch[1].areg.w | (dma[c].ch[1].pagereg << 16);
                   16214:                                
                   16215:                                if(c == 0) {
                   16216:                                        dma[c].tmp = read_byte(saddr);
                   16217:                                        write_byte(daddr, dma[c].tmp);
                   16218:                                } else {
                   16219:                                        dma[c].tmp = read_word(saddr << 1);
                   16220:                                        write_word(daddr << 1, dma[c].tmp);
                   16221:                                }
                   16222:                                if(!(dma[c].cmd & 0x02)) {
                   16223:                                        if(dma[c].ch[0].mode & 0x20) {
                   16224:                                                dma[c].ch[0].areg.w--;
                   16225:                                                if(dma[c].ch[0].areg.w == 0xffff) {
                   16226:                                                        dma[c].ch[0].pagereg--;
                   16227:                                                }
                   16228:                                        } else {
                   16229:                                                dma[c].ch[0].areg.w++;
                   16230:                                                if(dma[c].ch[0].areg.w == 0) {
                   16231:                                                        dma[c].ch[0].pagereg++;
                   16232:                                                }
                   16233:                                        }
                   16234:                                }
                   16235:                                if(dma[c].ch[1].mode & 0x20) {
                   16236:                                        dma[c].ch[1].areg.w--;
                   16237:                                        if(dma[c].ch[1].areg.w == 0xffff) {
                   16238:                                                dma[c].ch[1].pagereg--;
                   16239:                                        }
                   16240:                                } else {
                   16241:                                        dma[c].ch[1].areg.w++;
                   16242:                                        if(dma[c].ch[1].areg.w == 0) {
                   16243:                                                dma[c].ch[1].pagereg++;
                   16244:                                        }
                   16245:                                }
                   16246:                                
                   16247:                                // check dma condition
                   16248:                                if(dma[c].ch[0].creg.w-- == 0) {
                   16249:                                        if(dma[c].ch[0].mode & 0x10) {
                   16250:                                                // self initialize
                   16251:                                                dma[c].ch[0].areg.w = dma[c].ch[0].bareg.w;
                   16252:                                                dma[c].ch[0].creg.w = dma[c].ch[0].bcreg.w;
                   16253:                                        } else {
                   16254: //                                             dma[c].mask |= bit;
                   16255:                                        }
                   16256:                                }
                   16257:                                if(dma[c].ch[1].creg.w-- == 0) {
                   16258:                                        // terminal count
                   16259:                                        if(dma[c].ch[1].mode & 0x10) {
                   16260:                                                // self initialize
                   16261:                                                dma[c].ch[1].areg.w = dma[c].ch[1].bareg.w;
                   16262:                                                dma[c].ch[1].creg.w = dma[c].ch[1].bcreg.w;
                   16263:                                        } else {
                   16264:                                                dma[c].mask |= bit;
                   16265:                                        }
                   16266:                                        dma[c].req &= ~bit;
                   16267:                                        dma[c].tc |= bit;
                   16268:                                }
                   16269:                        } else {
                   16270:                                UINT32 addr = dma[c].ch[ch].areg.w | (dma[c].ch[ch].pagereg << 16);
                   16271:                                
                   16272:                                if((dma[c].ch[ch].mode & 0x0c) == 0x00) {
                   16273:                                        // verify
                   16274:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x04) {
                   16275:                                        // io -> memory
                   16276:                                        if(c == 0) {
                   16277:                                                dma[c].tmp = read_io_byte(dma[c].ch[ch].port);
                   16278:                                                write_byte(addr, dma[c].tmp);
                   16279:                                        } else {
                   16280:                                                dma[c].tmp = read_io_word(dma[c].ch[ch].port);
                   16281:                                                write_word(addr << 1, dma[c].tmp);
                   16282:                                        }
                   16283:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x08) {
                   16284:                                        // memory -> io
                   16285:                                        if(c == 0) {
                   16286:                                                dma[c].tmp = read_byte(addr);
                   16287:                                                write_io_byte(dma[c].ch[ch].port, dma[c].tmp);
                   16288:                                        } else {
                   16289:                                                dma[c].tmp = read_word(addr << 1);
                   16290:                                                write_io_word(dma[c].ch[ch].port, dma[c].tmp);
                   16291:                                        }
                   16292:                                }
                   16293:                                if(dma[c].ch[ch].mode & 0x20) {
                   16294:                                        dma[c].ch[ch].areg.w--;
                   16295:                                        if(dma[c].ch[ch].areg.w == 0xffff) {
                   16296:                                                dma[c].ch[ch].pagereg--;
                   16297:                                        }
                   16298:                                } else {
                   16299:                                        dma[c].ch[ch].areg.w++;
                   16300:                                        if(dma[c].ch[ch].areg.w == 0) {
                   16301:                                                dma[c].ch[ch].pagereg++;
                   16302:                                        }
                   16303:                                }
                   16304:                                
                   16305:                                // check dma condition
                   16306:                                if(dma[c].ch[ch].creg.w-- == 0) {
                   16307:                                        // terminal count
                   16308:                                        if(dma[c].ch[ch].mode & 0x10) {
                   16309:                                                // self initialize
                   16310:                                                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   16311:                                                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   16312:                                        } else {
                   16313:                                                dma[c].mask |= bit;
                   16314:                                        }
                   16315:                                        dma[c].req &= ~bit;
                   16316:                                        dma[c].tc |= bit;
                   16317:                                } else if((dma[c].ch[ch].mode & 0xc0) == 0x40) {
                   16318:                                        // single mode
                   16319:                                        break;
                   16320:                                }
                   16321:                        }
                   16322:                }
                   16323:        }
                   16324: }
                   16325: 
                   16326: // pic
                   16327: 
                   16328: void pic_init()
                   16329: {
                   16330:        memset(pic, 0, sizeof(pic));
                   16331:        pic[0].imr = pic[1].imr = 0xff;
                   16332:        
                   16333:        // from bochs bios
                   16334:        pic_write(0, 0, 0x11);  // icw1 = 11h
                   16335:        pic_write(0, 1, 0x08);  // icw2 = 08h
                   16336:        pic_write(0, 1, 0x04);  // icw3 = 04h
                   16337:        pic_write(0, 1, 0x01);  // icw4 = 01h
                   16338:        pic_write(0, 1, 0xb8);  // ocw1 = b8h
                   16339:        pic_write(1, 0, 0x11);  // icw1 = 11h
                   16340:        pic_write(1, 1, 0x70);  // icw2 = 70h
                   16341:        pic_write(1, 1, 0x02);  // icw3 = 02h
                   16342:        pic_write(1, 1, 0x01);  // icw4 = 01h
                   16343: }
                   16344: 
                   16345: void pic_write(int c, UINT32 addr, UINT8 data)
                   16346: {
                   16347:        if(addr & 1) {
                   16348:                if(pic[c].icw2_r) {
                   16349:                        // icw2
                   16350:                        pic[c].icw2 = data;
                   16351:                        pic[c].icw2_r = 0;
                   16352:                } else if(pic[c].icw3_r) {
                   16353:                        // icw3
                   16354:                        pic[c].icw3 = data;
                   16355:                        pic[c].icw3_r = 0;
                   16356:                } else if(pic[c].icw4_r) {
                   16357:                        // icw4
                   16358:                        pic[c].icw4 = data;
                   16359:                        pic[c].icw4_r = 0;
                   16360:                } else {
                   16361:                        // ocw1
1.1       root     16362:                        pic[c].imr = data;
                   16363:                }
                   16364:        } else {
                   16365:                if(data & 0x10) {
                   16366:                        // icw1
                   16367:                        pic[c].icw1 = data;
                   16368:                        pic[c].icw2_r = 1;
                   16369:                        pic[c].icw3_r = (data & 2) ? 0 : 1;
                   16370:                        pic[c].icw4_r = data & 1;
                   16371:                        pic[c].irr = 0;
                   16372:                        pic[c].isr = 0;
                   16373:                        pic[c].imr = 0;
                   16374:                        pic[c].prio = 0;
                   16375:                        if(!(pic[c].icw1 & 1)) {
                   16376:                                pic[c].icw4 = 0;
                   16377:                        }
                   16378:                        pic[c].ocw3 = 0;
                   16379:                } else if(data & 8) {
                   16380:                        // ocw3
                   16381:                        if(!(data & 2)) {
                   16382:                                data = (data & ~1) | (pic[c].ocw3 & 1);
                   16383:                        }
                   16384:                        if(!(data & 0x40)) {
                   16385:                                data = (data & ~0x20) | (pic[c].ocw3 & 0x20);
                   16386:                        }
                   16387:                        pic[c].ocw3 = data;
                   16388:                } else {
                   16389:                        // ocw2
                   16390:                        int level = 0;
                   16391:                        if(data & 0x40) {
                   16392:                                level = data & 7;
                   16393:                        } else {
                   16394:                                if(!pic[c].isr) {
                   16395:                                        return;
                   16396:                                }
                   16397:                                level = pic[c].prio;
                   16398:                                while(!(pic[c].isr & (1 << level))) {
                   16399:                                        level = (level + 1) & 7;
                   16400:                                }
                   16401:                        }
                   16402:                        if(data & 0x80) {
                   16403:                                pic[c].prio = (level + 1) & 7;
                   16404:                        }
                   16405:                        if(data & 0x20) {
                   16406:                                pic[c].isr &= ~(1 << level);
                   16407:                        }
                   16408:                }
                   16409:        }
                   16410:        pic_update();
                   16411: }
                   16412: 
                   16413: UINT8 pic_read(int c, UINT32 addr)
                   16414: {
                   16415:        if(addr & 1) {
                   16416:                return(pic[c].imr);
                   16417:        } else {
                   16418:                // polling mode is not supported...
                   16419:                //if(pic[c].ocw3 & 4) {
                   16420:                //      return ???;
                   16421:                //}
                   16422:                if(pic[c].ocw3 & 1) {
                   16423:                        return(pic[c].isr);
                   16424:                } else {
                   16425:                        return(pic[c].irr);
                   16426:                }
                   16427:        }
                   16428: }
                   16429: 
                   16430: void pic_req(int c, int level, int signal)
                   16431: {
                   16432:        if(signal) {
                   16433:                pic[c].irr |= (1 << level);
                   16434:        } else {
                   16435:                pic[c].irr &= ~(1 << level);
                   16436:        }
                   16437:        pic_update();
                   16438: }
                   16439: 
                   16440: int pic_ack()
                   16441: {
                   16442:        // ack (INTA=L)
                   16443:        pic[pic_req_chip].isr |= pic_req_bit;
                   16444:        pic[pic_req_chip].irr &= ~pic_req_bit;
                   16445:        if(pic_req_chip > 0) {
                   16446:                // update isr and irr of master
                   16447:                UINT8 slave = 1 << (pic[pic_req_chip].icw3 & 7);
                   16448:                pic[pic_req_chip - 1].isr |= slave;
                   16449:                pic[pic_req_chip - 1].irr &= ~slave;
                   16450:        }
                   16451:        //if(pic[pic_req_chip].icw4 & 1) {
                   16452:                // 8086 mode
                   16453:                int vector = (pic[pic_req_chip].icw2 & 0xf8) | pic_req_level;
                   16454:        //} else {
                   16455:        //      // 8080 mode
                   16456:        //      UINT16 addr = (UINT16)pic[pic_req_chip].icw2 << 8;
                   16457:        //      if(pic[pic_req_chip].icw1 & 4) {
                   16458:        //              addr |= (pic[pic_req_chip].icw1 & 0xe0) | (pic_req_level << 2);
                   16459:        //      } else {
                   16460:        //              addr |= (pic[pic_req_chip].icw1 & 0xc0) | (pic_req_level << 3);
                   16461:        //      }
                   16462:        //      vector = 0xcd | (addr << 8);
                   16463:        //}
                   16464:        if(pic[pic_req_chip].icw4 & 2) {
                   16465:                // auto eoi
                   16466:                pic[pic_req_chip].isr &= ~pic_req_bit;
                   16467:        }
                   16468:        return(vector);
                   16469: }
                   16470: 
                   16471: void pic_update()
                   16472: {
                   16473:        for(int c = 0; c < 2; c++) {
                   16474:                UINT8 irr = pic[c].irr;
                   16475:                if(c + 1 < 2) {
                   16476:                        // this is master
                   16477:                        if(pic[c + 1].irr & (~pic[c + 1].imr)) {
                   16478:                                // request from slave
                   16479:                                irr |= 1 << (pic[c + 1].icw3 & 7);
                   16480:                        }
                   16481:                }
                   16482:                irr &= (~pic[c].imr);
                   16483:                if(!irr) {
                   16484:                        break;
                   16485:                }
                   16486:                if(!(pic[c].ocw3 & 0x20)) {
                   16487:                        irr |= pic[c].isr;
                   16488:                }
                   16489:                int level = pic[c].prio;
                   16490:                UINT8 bit = 1 << level;
                   16491:                while(!(irr & bit)) {
                   16492:                        level = (level + 1) & 7;
                   16493:                        bit = 1 << level;
                   16494:                }
                   16495:                if((c + 1 < 2) && (pic[c].icw3 & bit)) {
                   16496:                        // check slave
                   16497:                        continue;
                   16498:                }
                   16499:                if(pic[c].isr & bit) {
                   16500:                        break;
                   16501:                }
                   16502:                // interrupt request
                   16503:                pic_req_chip = c;
                   16504:                pic_req_level = level;
                   16505:                pic_req_bit = bit;
1.1.1.3   root     16506:                i386_set_irq_line(INPUT_LINE_IRQ, HOLD_LINE);
1.1       root     16507:                return;
                   16508:        }
1.1.1.3   root     16509:        i386_set_irq_line(INPUT_LINE_IRQ, CLEAR_LINE);
1.1.1.2   root     16510: }
1.1       root     16511: 
1.1.1.25  root     16512: // pio
                   16513: 
                   16514: void pio_init()
                   16515: {
1.1.1.38  root     16516: //     bool conv_mode = (GetConsoleCP() == 932);
                   16517:        
1.1.1.26  root     16518:        memset(pio, 0, sizeof(pio));
1.1.1.37  root     16519:        
1.1.1.25  root     16520:        for(int c = 0; c < 2; c++) {
1.1.1.37  root     16521:                pio[c].stat = 0xdf;
1.1.1.25  root     16522:                pio[c].ctrl = 0x0c;
1.1.1.38  root     16523: //             pio[c].conv_mode = conv_mode;
1.1.1.25  root     16524:        }
                   16525: }
                   16526: 
1.1.1.37  root     16527: void pio_finish()
                   16528: {
                   16529:        pio_release();
                   16530: }
                   16531: 
                   16532: void pio_release()
                   16533: {
                   16534:        for(int c = 0; c < 2; c++) {
                   16535:                if(pio[c].fp != NULL) {
1.1.1.38  root     16536:                        if(pio[c].jis_mode) {
                   16537:                                fputc(0x1c, pio[c].fp);
                   16538:                                fputc(0x2e, pio[c].fp);
                   16539:                        }
1.1.1.37  root     16540:                        fclose(pio[c].fp);
                   16541:                        pio[c].fp = NULL;
                   16542:                }
                   16543:        }
                   16544: }
                   16545: 
1.1.1.25  root     16546: void pio_write(int c, UINT32 addr, UINT8 data)
                   16547: {
                   16548:        switch(addr & 3) {
                   16549:        case 0:
                   16550:                pio[c].data = data;
                   16551:                break;
                   16552:        case 2:
1.1.1.37  root     16553:                if((pio[c].ctrl & 0x01) && !(data & 0x01)) {
                   16554:                        // strobe H -> L
                   16555:                        if(pio[c].data == 0x0d && (data & 0x02)) {
                   16556:                                // auto feed
                   16557:                                printer_out(c, 0x0d);
                   16558:                                printer_out(c, 0x0a);
                   16559:                        } else {
                   16560:                                printer_out(c, pio[c].data);
                   16561:                        }
                   16562:                        pio[c].stat &= ~0x40; // set ack
                   16563:                }
1.1.1.25  root     16564:                pio[c].ctrl = data;
                   16565:                break;
                   16566:        }
                   16567: }
                   16568: 
                   16569: UINT8 pio_read(int c, UINT32 addr)
                   16570: {
                   16571:        switch(addr & 3) {
                   16572:        case 0:
1.1.1.37  root     16573:                if(pio[c].ctrl & 0x20) {
                   16574:                        // input mode
                   16575:                        return(0xff);
                   16576:                }
1.1.1.25  root     16577:                return(pio[c].data);
                   16578:        case 1:
1.1.1.37  root     16579:                {
                   16580:                        UINT8 stat = pio[c].stat;
                   16581:                        pio[c].stat |= 0x40; // clear ack
                   16582:                        return(stat);
                   16583:                }
1.1.1.25  root     16584:        case 2:
                   16585:                return(pio[c].ctrl);
                   16586:        }
                   16587:        return(0xff);
                   16588: }
                   16589: 
1.1.1.37  root     16590: void printer_out(int c, UINT8 data)
                   16591: {
                   16592:        SYSTEMTIME time;
1.1.1.38  root     16593:        bool jis_mode = false;
1.1.1.37  root     16594:        
                   16595:        GetLocalTime(&time);
                   16596:        
                   16597:        if(pio[c].fp != NULL) {
                   16598:                // if at least 1000ms passed from last written, close the current file
                   16599:                FILETIME ftime1;
                   16600:                FILETIME ftime2;
                   16601:                SystemTimeToFileTime(&pio[c].time, &ftime1);
                   16602:                SystemTimeToFileTime(&time, &ftime2);
                   16603:                INT64 *time1 = (INT64 *)&ftime1;
                   16604:                INT64 *time2 = (INT64 *)&ftime2;
                   16605:                INT64 msec = (*time2 - *time1) / 10000;
                   16606:                
                   16607:                if(msec >= 1000) {
1.1.1.38  root     16608:                        if(pio[c].jis_mode) {
                   16609:                                fputc(0x1c, pio[c].fp);
                   16610:                                fputc(0x2e, pio[c].fp);
                   16611:                                jis_mode = true;
                   16612:                        }
1.1.1.37  root     16613:                        fclose(pio[c].fp);
                   16614:                        pio[c].fp = NULL;
                   16615:                }
                   16616:        }
                   16617:        if(pio[c].fp == NULL) {
                   16618:                // create a new file in the temp folder
                   16619:                char file_name[MAX_PATH];
                   16620:                
                   16621:                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);
                   16622:                if(GetTempPath(MAX_PATH, pio[c].path)) {
                   16623:                        strcat(pio[c].path, file_name);
                   16624:                } else {
                   16625:                        strcpy(pio[c].path, file_name);
                   16626:                }
1.1.1.38  root     16627:                pio[c].fp = fopen(pio[c].path, "w+b");
1.1.1.37  root     16628:        }
                   16629:        if(pio[c].fp != NULL) {
1.1.1.38  root     16630:                if(jis_mode) {
                   16631:                        fputc(0x1c, pio[c].fp);
                   16632:                        fputc(0x26, pio[c].fp);
                   16633:                }
1.1.1.37  root     16634:                fputc(data, pio[c].fp);
1.1.1.38  root     16635:                
                   16636:                // reopen file if 1ch 26h 1ch 2eh (kanji-on  kanji-off) are written at the top
                   16637:                if(data == 0x2e && ftell(pio[c].fp) == 4) {
                   16638:                        UINT8 buffer[4];
                   16639:                        fseek(pio[c].fp, 0, SEEK_SET);
                   16640:                        fread(buffer, 4, 1, pio[c].fp);
                   16641:                        if(buffer[0] == 0x1c && buffer[1] == 0x26 && buffer[2] == 0x1c/* && buffer[3] == 0x2e*/) {
                   16642:                                fclose(pio[c].fp);
                   16643:                                pio[c].fp = fopen(pio[c].path, "w+b");
                   16644:                        }
                   16645:                }
1.1.1.37  root     16646:                pio[c].time = time;
                   16647:        }
                   16648: }
                   16649: 
1.1       root     16650: // pit
                   16651: 
1.1.1.22  root     16652: #define PIT_FREQ 1193182ULL
1.1       root     16653: #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)
                   16654: 
                   16655: void pit_init()
                   16656: {
1.1.1.8   root     16657:        memset(pit, 0, sizeof(pit));
1.1       root     16658:        for(int ch = 0; ch < 3; ch++) {
                   16659:                pit[ch].count = 0x10000;
                   16660:                pit[ch].ctrl_reg = 0x34;
                   16661:                pit[ch].mode = 3;
                   16662:        }
                   16663:        
                   16664:        // from bochs bios
                   16665:        pit_write(3, 0x34);
                   16666:        pit_write(0, 0x00);
                   16667:        pit_write(0, 0x00);
                   16668: }
                   16669: 
                   16670: void pit_write(int ch, UINT8 val)
                   16671: {
1.1.1.8   root     16672: #ifndef PIT_ALWAYS_RUNNING
1.1       root     16673:        if(!pit_active) {
                   16674:                pit_active = 1;
                   16675:                pit_init();
                   16676:        }
1.1.1.8   root     16677: #endif
1.1       root     16678:        switch(ch) {
                   16679:        case 0:
                   16680:        case 1:
                   16681:        case 2:
                   16682:                // write count register
                   16683:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   16684:                        if(pit[ch].ctrl_reg & 0x10) {
                   16685:                                pit[ch].low_write = 1;
                   16686:                        }
                   16687:                        if(pit[ch].ctrl_reg & 0x20) {
                   16688:                                pit[ch].high_write = 1;
                   16689:                        }
                   16690:                }
                   16691:                if(pit[ch].low_write) {
                   16692:                        pit[ch].count_reg = val;
                   16693:                        pit[ch].low_write = 0;
                   16694:                } else if(pit[ch].high_write) {
                   16695:                        if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   16696:                                pit[ch].count_reg = val << 8;
                   16697:                        } else {
                   16698:                                pit[ch].count_reg |= val << 8;
                   16699:                        }
                   16700:                        pit[ch].high_write = 0;
                   16701:                }
                   16702:                // start count
1.1.1.8   root     16703:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   16704:                        if(pit[ch].mode == 0 || pit[ch].mode == 4 || pit[ch].prev_time == 0) {
                   16705:                                pit[ch].count = PIT_COUNT_VALUE(ch);
                   16706:                                pit[ch].prev_time = timeGetTime();
                   16707:                                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     16708:                        }
                   16709:                }
                   16710:                break;
                   16711:        case 3: // ctrl reg
                   16712:                if((val & 0xc0) == 0xc0) {
                   16713:                        // i8254 read-back command
                   16714:                        for(ch = 0; ch < 3; ch++) {
                   16715:                                if(!(val & 0x10) && !pit[ch].status_latched) {
                   16716:                                        pit[ch].status = pit[ch].ctrl_reg & 0x3f;
                   16717:                                        pit[ch].status_latched = 1;
                   16718:                                }
                   16719:                                if(!(val & 0x20) && !pit[ch].count_latched) {
                   16720:                                        pit_latch_count(ch);
                   16721:                                }
                   16722:                        }
                   16723:                        break;
                   16724:                }
                   16725:                ch = (val >> 6) & 3;
                   16726:                if(val & 0x30) {
1.1.1.35  root     16727:                        static const int modes[8] = {0, 1, 2, 3, 4, 5, 2, 3};
1.1       root     16728:                        pit[ch].mode = modes[(val >> 1) & 7];
                   16729:                        pit[ch].count_latched = 0;
                   16730:                        pit[ch].low_read = pit[ch].high_read = 0;
                   16731:                        pit[ch].low_write = pit[ch].high_write = 0;
                   16732:                        pit[ch].ctrl_reg = val;
                   16733:                        // stop count
1.1.1.8   root     16734:                        pit[ch].prev_time = pit[ch].expired_time = 0;
1.1       root     16735:                        pit[ch].count_reg = 0;
                   16736:                } else if(!pit[ch].count_latched) {
                   16737:                        pit_latch_count(ch);
                   16738:                }
                   16739:                break;
                   16740:        }
                   16741: }
                   16742: 
                   16743: UINT8 pit_read(int ch)
                   16744: {
1.1.1.8   root     16745: #ifndef PIT_ALWAYS_RUNNING
1.1       root     16746:        if(!pit_active) {
                   16747:                pit_active = 1;
                   16748:                pit_init();
                   16749:        }
1.1.1.8   root     16750: #endif
1.1       root     16751:        switch(ch) {
                   16752:        case 0:
                   16753:        case 1:
                   16754:        case 2:
                   16755:                if(pit[ch].status_latched) {
                   16756:                        pit[ch].status_latched = 0;
                   16757:                        return(pit[ch].status);
                   16758:                }
                   16759:                // if not latched, through current count
                   16760:                if(!pit[ch].count_latched) {
                   16761:                        if(!pit[ch].low_read && !pit[ch].high_read) {
                   16762:                                pit_latch_count(ch);
                   16763:                        }
                   16764:                }
                   16765:                // return latched count
                   16766:                if(pit[ch].low_read) {
                   16767:                        pit[ch].low_read = 0;
                   16768:                        if(!pit[ch].high_read) {
                   16769:                                pit[ch].count_latched = 0;
                   16770:                        }
                   16771:                        return(pit[ch].latch & 0xff);
                   16772:                } else if(pit[ch].high_read) {
                   16773:                        pit[ch].high_read = 0;
                   16774:                        pit[ch].count_latched = 0;
                   16775:                        return((pit[ch].latch >> 8) & 0xff);
                   16776:                }
                   16777:        }
                   16778:        return(0xff);
                   16779: }
                   16780: 
1.1.1.8   root     16781: int pit_run(int ch, UINT32 cur_time)
1.1       root     16782: {
1.1.1.8   root     16783:        if(pit[ch].expired_time != 0 && cur_time >= pit[ch].expired_time) {
1.1       root     16784:                pit[ch].count = PIT_COUNT_VALUE(ch);
1.1.1.8   root     16785:                pit[ch].prev_time = pit[ch].expired_time;
                   16786:                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
                   16787:                if(cur_time >= pit[ch].expired_time) {
                   16788:                        pit[ch].prev_time = cur_time;
                   16789:                        pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     16790:                }
1.1.1.8   root     16791:                return(1);
1.1       root     16792:        }
1.1.1.8   root     16793:        return(0);
1.1       root     16794: }
                   16795: 
                   16796: void pit_latch_count(int ch)
                   16797: {
1.1.1.8   root     16798:        if(pit[ch].expired_time != 0) {
1.1.1.26  root     16799:                UINT32 cur_time = timeGetTime();
1.1.1.8   root     16800:                pit_run(ch, cur_time);
                   16801:                UINT32 tmp = (pit[ch].count * (pit[ch].expired_time - cur_time)) / (pit[ch].expired_time - pit[ch].prev_time);
1.1.1.26  root     16802:                UINT16 latch = (tmp != 0) ? (UINT16)tmp : 1;
                   16803:                
                   16804:                if(pit[ch].prev_latch == latch && pit[ch].expired_time > cur_time) {
                   16805:                        // decrement counter in 1msec period
                   16806:                        if(pit[ch].next_latch == 0) {
                   16807:                                tmp = (pit[ch].count * (pit[ch].expired_time - cur_time - 1)) / (pit[ch].expired_time - pit[ch].prev_time);
                   16808:                                pit[ch].next_latch = (tmp != 0) ? (UINT16)tmp : 1;
                   16809:                        }
                   16810:                        if(pit[ch].latch > pit[ch].next_latch) {
                   16811:                                pit[ch].latch--;
                   16812:                        }
                   16813:                } else {
                   16814:                        pit[ch].prev_latch = pit[ch].latch = latch;
                   16815:                        pit[ch].next_latch = 0;
                   16816:                }
1.1.1.8   root     16817:        } else {
                   16818:                pit[ch].latch = (UINT16)pit[ch].count;
1.1.1.26  root     16819:                pit[ch].prev_latch = pit[ch].next_latch = 0;
1.1       root     16820:        }
                   16821:        pit[ch].count_latched = 1;
                   16822:        if((pit[ch].ctrl_reg & 0x30) == 0x10) {
                   16823:                // lower byte
                   16824:                pit[ch].low_read = 1;
                   16825:                pit[ch].high_read = 0;
                   16826:        } else if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   16827:                // upper byte
                   16828:                pit[ch].low_read = 0;
                   16829:                pit[ch].high_read = 1;
                   16830:        } else {
                   16831:                // lower -> upper
1.1.1.14  root     16832:                pit[ch].low_read = pit[ch].high_read = 1;
1.1       root     16833:        }
                   16834: }
                   16835: 
1.1.1.8   root     16836: int pit_get_expired_time(int ch)
1.1       root     16837: {
1.1.1.22  root     16838:        pit[ch].accum += 1024ULL * 1000ULL * (UINT64)pit[ch].count / PIT_FREQ;
                   16839:        UINT64 val = pit[ch].accum >> 10;
                   16840:        pit[ch].accum -= val << 10;
                   16841:        return((val != 0) ? val : 1);
1.1.1.8   root     16842: }
                   16843: 
1.1.1.25  root     16844: // sio
                   16845: 
                   16846: void sio_init()
                   16847: {
1.1.1.26  root     16848:        memset(sio, 0, sizeof(sio));
                   16849:        memset(sio_mt, 0, sizeof(sio_mt));
                   16850:        
1.1.1.29  root     16851:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     16852:                sio[c].send_buffer = new FIFO(SIO_BUFFER_SIZE);
                   16853:                sio[c].recv_buffer = new FIFO(SIO_BUFFER_SIZE);
                   16854:                
                   16855:                sio[c].divisor.w = 12;          // 115200Hz / 9600Baud
                   16856:                sio[c].line_ctrl = 0x03;        // 8bit, stop 1bit, non parity
1.1.1.26  root     16857:                sio[c].modem_ctrl = 0x03;       // rts=on, dtr=on
                   16858:                sio[c].set_rts = sio[c].set_dtr = true;
1.1.1.25  root     16859:                sio[c].line_stat_buf = 0x60;    // send/recv buffers are empty
                   16860:                sio[c].irq_identify = 0x01;     // no pending irq
                   16861:                
                   16862:                InitializeCriticalSection(&sio_mt[c].csSendData);
                   16863:                InitializeCriticalSection(&sio_mt[c].csRecvData);
                   16864:                InitializeCriticalSection(&sio_mt[c].csLineCtrl);
                   16865:                InitializeCriticalSection(&sio_mt[c].csLineStat);
                   16866:                InitializeCriticalSection(&sio_mt[c].csModemCtrl);
                   16867:                InitializeCriticalSection(&sio_mt[c].csModemStat);
                   16868:                
1.1.1.26  root     16869:                if(sio_port_number[c] != 0) {
1.1.1.25  root     16870:                        sio[c].channel = c;
                   16871:                        sio_mt[c].hThread = CreateThread(NULL, 0, sio_thread, &sio[c], 0, NULL);
                   16872:                }
                   16873:        }
                   16874: }
                   16875: 
                   16876: void sio_finish()
                   16877: {
1.1.1.29  root     16878:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     16879:                if(sio_mt[c].hThread != NULL) {
                   16880:                        WaitForSingleObject(sio_mt[c].hThread, INFINITE);
                   16881:                        CloseHandle(sio_mt[c].hThread);
1.1.1.28  root     16882:                        sio_mt[c].hThread = NULL;
1.1.1.25  root     16883:                }
                   16884:                DeleteCriticalSection(&sio_mt[c].csSendData);
                   16885:                DeleteCriticalSection(&sio_mt[c].csRecvData);
                   16886:                DeleteCriticalSection(&sio_mt[c].csLineCtrl);
                   16887:                DeleteCriticalSection(&sio_mt[c].csLineStat);
                   16888:                DeleteCriticalSection(&sio_mt[c].csModemCtrl);
                   16889:                DeleteCriticalSection(&sio_mt[c].csModemStat);
1.1.1.28  root     16890:        }
                   16891:        sio_release();
                   16892: }
                   16893: 
                   16894: void sio_release()
                   16895: {
1.1.1.29  root     16896:        for(int c = 0; c < 4; c++) {
1.1.1.28  root     16897:                // sio_thread() may access the resources :-(
1.1.1.32  root     16898:                bool running = (sio_mt[c].hThread != NULL);
                   16899:                
                   16900:                if(running) {
                   16901:                        EnterCriticalSection(&sio_mt[c].csSendData);
                   16902:                }
                   16903:                if(sio[c].send_buffer != NULL) {
                   16904:                        sio[c].send_buffer->release();
                   16905:                        delete sio[c].send_buffer;
                   16906:                        sio[c].send_buffer = NULL;
                   16907:                }
                   16908:                if(running) {
                   16909:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   16910:                        EnterCriticalSection(&sio_mt[c].csRecvData);
                   16911:                }
                   16912:                if(sio[c].recv_buffer != NULL) {
                   16913:                        sio[c].recv_buffer->release();
                   16914:                        delete sio[c].recv_buffer;
                   16915:                        sio[c].recv_buffer = NULL;
                   16916:                }
                   16917:                if(running) {
                   16918:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
1.1.1.28  root     16919:                }
1.1.1.25  root     16920:        }
                   16921: }
                   16922: 
                   16923: void sio_write(int c, UINT32 addr, UINT8 data)
                   16924: {
                   16925:        switch(addr & 7) {
                   16926:        case 0:
                   16927:                if(sio[c].selector & 0x80) {
                   16928:                        if(sio[c].divisor.b.l != data) {
                   16929:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   16930:                                sio[c].divisor.b.l = data;
                   16931:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   16932:                        }
                   16933:                } else {
                   16934:                        EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     16935:                        if(sio[c].send_buffer != NULL) {
                   16936:                                sio[c].send_buffer->write(data);
                   16937:                        }
1.1.1.25  root     16938:                        // transmitter holding/shift registers are not empty
                   16939:                        sio[c].line_stat_buf &= ~0x60;
                   16940:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   16941:                        
                   16942:                        if(sio[c].irq_enable & 0x02) {
                   16943:                                sio_update_irq(c);
                   16944:                        }
                   16945:                }
                   16946:                break;
                   16947:        case 1:
                   16948:                if(sio[c].selector & 0x80) {
                   16949:                        if(sio[c].divisor.b.h != data) {
                   16950:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   16951:                                sio[c].divisor.b.h = data;
                   16952:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   16953:                        }
                   16954:                } else {
                   16955:                        if(sio[c].irq_enable != data) {
                   16956:                                sio[c].irq_enable = data;
                   16957:                                sio_update_irq(c);
                   16958:                        }
                   16959:                }
                   16960:                break;
                   16961:        case 3:
                   16962:                {
                   16963:                        UINT8 line_ctrl = data & 0x3f;
                   16964:                        bool set_brk = ((data & 0x40) != 0);
                   16965:                        
                   16966:                        if(sio[c].line_ctrl != line_ctrl) {
                   16967:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   16968:                                sio[c].line_ctrl = line_ctrl;
                   16969:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   16970:                        }
                   16971:                        if(sio[c].set_brk != set_brk) {
                   16972:                                EnterCriticalSection(&sio_mt[c].csModemCtrl);
                   16973:                                sio[c].set_brk = set_brk;
                   16974:                                LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   16975:                        }
                   16976:                }
                   16977:                sio[c].selector = data;
                   16978:                break;
                   16979:        case 4:
                   16980:                {
                   16981:                        bool set_dtr = ((data & 0x01) != 0);
                   16982:                        bool set_rts = ((data & 0x02) != 0);
                   16983:                        
                   16984:                        if(sio[c].set_dtr != set_dtr || sio[c].set_rts != set_rts) {
1.1.1.26  root     16985: //                             EnterCriticalSection(&sio_mt[c].csModemCtrl);
1.1.1.25  root     16986:                                sio[c].set_dtr = set_dtr;
                   16987:                                sio[c].set_rts = set_rts;
1.1.1.26  root     16988: //                             LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   16989:                                
                   16990:                                bool state_changed = false;
                   16991:                                
                   16992:                                EnterCriticalSection(&sio_mt[c].csModemStat);
                   16993:                                if(set_dtr) {
                   16994:                                        sio[c].modem_stat |= 0x20;      // dsr on
                   16995:                                } else {
                   16996:                                        sio[c].modem_stat &= ~0x20;     // dsr off
                   16997:                                }
                   16998:                                if(set_rts) {
                   16999:                                        sio[c].modem_stat |= 0x10;      // cts on
                   17000:                                } else {
                   17001:                                        sio[c].modem_stat &= ~0x10;     // cts off
                   17002:                                }
                   17003:                                if((sio[c].prev_modem_stat & 0x20) != (sio[c].modem_stat & 0x20)) {
                   17004:                                        if(!(sio[c].modem_stat & 0x02)) {
                   17005:                                                if(sio[c].irq_enable & 0x08) {
                   17006:                                                        state_changed = true;
                   17007:                                                }
                   17008:                                                sio[c].modem_stat |= 0x02;
                   17009:                                        }
                   17010:                                }
                   17011:                                if((sio[c].prev_modem_stat & 0x10) != (sio[c].modem_stat & 0x10)) {
                   17012:                                        if(!(sio[c].modem_stat & 0x01)) {
                   17013:                                                if(sio[c].irq_enable & 0x08) {
                   17014:                                                        state_changed = true;
                   17015:                                                }
                   17016:                                                sio[c].modem_stat |= 0x01;
                   17017:                                        }
                   17018:                                }
                   17019:                                LeaveCriticalSection(&sio_mt[c].csModemStat);
                   17020:                                
                   17021:                                if(state_changed) {
                   17022:                                        sio_update_irq(c);
                   17023:                                }
1.1.1.25  root     17024:                        }
                   17025:                }
                   17026:                sio[c].modem_ctrl = data;
                   17027:                break;
                   17028:        case 7:
                   17029:                sio[c].scratch = data;
                   17030:                break;
                   17031:        }
                   17032: }
                   17033: 
                   17034: UINT8 sio_read(int c, UINT32 addr)
                   17035: {
                   17036:        switch(addr & 7) {
                   17037:        case 0:
                   17038:                if(sio[c].selector & 0x80) {
                   17039:                        return(sio[c].divisor.b.l);
                   17040:                } else {
                   17041:                        EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     17042:                        UINT8 data = 0;
                   17043:                        if(sio[c].recv_buffer != NULL) {
                   17044:                                data = sio[c].recv_buffer->read();
                   17045:                        }
1.1.1.25  root     17046:                        // data is not ready
                   17047:                        sio[c].line_stat_buf &= ~0x01;
                   17048:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
                   17049:                        
                   17050:                        if(sio[c].irq_enable & 0x01) {
                   17051:                                sio_update_irq(c);
                   17052:                        }
                   17053:                        return(data);
                   17054:                }
                   17055:        case 1:
                   17056:                if(sio[c].selector & 0x80) {
                   17057:                        return(sio[c].divisor.b.h);
                   17058:                } else {
                   17059:                        return(sio[c].irq_enable);
                   17060:                }
                   17061:        case 2:
                   17062:                return(sio[c].irq_identify);
                   17063:        case 3:
                   17064:                return(sio[c].selector);
                   17065:        case 4:
                   17066:                return(sio[c].modem_ctrl);
                   17067:        case 5:
                   17068:                {
                   17069:                        EnterCriticalSection(&sio_mt[c].csLineStat);
                   17070:                        UINT8 val = sio[c].line_stat_err | sio[c].line_stat_buf;
                   17071:                        sio[c].line_stat_err = 0x00;
                   17072:                        LeaveCriticalSection(&sio_mt[c].csLineStat);
                   17073:                        
                   17074:                        bool state_changed = false;
                   17075:                        
                   17076:                        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   17077:                                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     17078:                                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     17079:                                        // transmitter holding register will be empty first
                   17080:                                        if(sio[c].irq_enable & 0x02) {
                   17081:                                                state_changed = true;
                   17082:                                        }
                   17083:                                        sio[c].line_stat_buf |= 0x20;
                   17084:                                }
                   17085:                                LeaveCriticalSection(&sio_mt[c].csSendData);
                   17086:                        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   17087:                                // transmitter shift register will be empty later
                   17088:                                sio[c].line_stat_buf |= 0x40;
                   17089:                        }
                   17090:                        if(!(sio[c].line_stat_buf & 0x01)) {
                   17091:                                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     17092:                                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     17093:                                        // data is ready
                   17094:                                        if(sio[c].irq_enable & 0x01) {
                   17095:                                                state_changed = true;
                   17096:                                        }
                   17097:                                        sio[c].line_stat_buf |= 0x01;
                   17098:                                }
                   17099:                                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   17100:                        }
                   17101:                        if(state_changed) {
                   17102:                                sio_update_irq(c);
                   17103:                        }
                   17104:                        return(val);
                   17105:                }
                   17106:        case 6:
                   17107:                {
                   17108:                        EnterCriticalSection(&sio_mt[c].csModemStat);
                   17109:                        UINT8 val = sio[c].modem_stat;
                   17110:                        sio[c].modem_stat &= 0xf0;
                   17111:                        sio[c].prev_modem_stat = sio[c].modem_stat;
                   17112:                        LeaveCriticalSection(&sio_mt[c].csModemStat);
                   17113:                        
                   17114:                        if(sio[c].modem_ctrl & 0x10) {
                   17115:                                // loop-back
                   17116:                                val &= 0x0f;
                   17117:                                val |= (sio[c].modem_ctrl & 0x0c) << 4;
                   17118:                                val |= (sio[c].modem_ctrl & 0x01) << 5;
                   17119:                                val |= (sio[c].modem_ctrl & 0x02) << 3;
                   17120:                        }
                   17121:                        return(val);
                   17122:                }
                   17123:        case 7:
                   17124:                return(sio[c].scratch);
                   17125:        }
                   17126:        return(0xff);
                   17127: }
                   17128: 
                   17129: void sio_update(int c)
                   17130: {
                   17131:        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   17132:                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     17133:                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     17134:                        // transmitter holding/shift registers will be empty
                   17135:                        sio[c].line_stat_buf |= 0x60;
                   17136:                }
                   17137:                LeaveCriticalSection(&sio_mt[c].csSendData);
                   17138:        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   17139:                // transmitter shift register will be empty
                   17140:                sio[c].line_stat_buf |= 0x40;
                   17141:        }
                   17142:        if(!(sio[c].line_stat_buf & 0x01)) {
                   17143:                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     17144:                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     17145:                        // data is ready
                   17146:                        sio[c].line_stat_buf |= 0x01;
                   17147:                }
                   17148:                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   17149:        }
                   17150:        sio_update_irq(c);
                   17151: }
                   17152: 
                   17153: void sio_update_irq(int c)
                   17154: {
                   17155:        int level = -1;
                   17156:        
                   17157:        if(sio[c].irq_enable & 0x08) {
                   17158:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   17159:                if((sio[c].modem_stat & 0x0f) != 0) {
                   17160:                        level = 0;
                   17161:                }
                   17162:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   17163:        }
                   17164:        if(sio[c].irq_enable & 0x02) {
                   17165:                if(sio[c].line_stat_buf & 0x20) {
                   17166:                        level = 1;
                   17167:                }
                   17168:        }
                   17169:        if(sio[c].irq_enable & 0x01) {
                   17170:                if(sio[c].line_stat_buf & 0x01) {
                   17171:                        level = 2;
                   17172:                }
                   17173:        }
                   17174:        if(sio[c].irq_enable & 0x04) {
                   17175:                EnterCriticalSection(&sio_mt[c].csLineStat);
                   17176:                if(sio[c].line_stat_err != 0) {
                   17177:                        level = 3;
                   17178:                }
                   17179:                LeaveCriticalSection(&sio_mt[c].csLineStat);
                   17180:        }
1.1.1.29  root     17181:        
                   17182:        // COM1 and COM3 shares IRQ4, COM2 and COM4 shares IRQ3
1.1.1.25  root     17183:        if(level != -1) {
                   17184:                sio[c].irq_identify = level << 1;
1.1.1.29  root     17185:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 1);
1.1.1.25  root     17186:        } else {
                   17187:                sio[c].irq_identify = 1;
1.1.1.29  root     17188:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 0);
1.1.1.25  root     17189:        }
                   17190: }
                   17191: 
                   17192: DWORD WINAPI sio_thread(void *lpx)
                   17193: {
                   17194:        volatile sio_t *p = (sio_t *)lpx;
                   17195:        sio_mt_t *q = &sio_mt[p->channel];
                   17196:        
                   17197:        char name[] = "COM1";
1.1.1.26  root     17198:        name[3] = '0' + sio_port_number[p->channel];
                   17199:        HANDLE hComm = NULL;
                   17200:        COMMPROP commProp;
                   17201:        DCB dcb;
                   17202:        DWORD dwSettableBaud = 0xffb; // 75, 110, 150, 300, 600, 1200, 1800, 2400, 4800, 7200, and 9600bps
                   17203:        BYTE bytBuffer[SIO_BUFFER_SIZE];
                   17204:        
                   17205:        if((hComm = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL)) != INVALID_HANDLE_VALUE) {
                   17206:                if(GetCommProperties(hComm, &commProp)) {
                   17207:                        dwSettableBaud = commProp.dwSettableBaud;
                   17208:                }
1.1.1.25  root     17209:                EscapeCommFunction(hComm, CLRBREAK);
1.1.1.26  root     17210: //             EscapeCommFunction(hComm, SETRTS);
                   17211: //             EscapeCommFunction(hComm, SETDTR);
1.1.1.25  root     17212:                
                   17213:                while(!m_halted) {
                   17214:                        // setup comm port
                   17215:                        bool comm_state_changed = false;
                   17216:                        
                   17217:                        EnterCriticalSection(&q->csLineCtrl);
                   17218:                        if((p->prev_divisor != p->divisor.w || p->prev_line_ctrl != p->line_ctrl) && p->divisor.w != 0) {
                   17219:                                p->prev_divisor = p->divisor.w;
                   17220:                                p->prev_line_ctrl = p->line_ctrl;
                   17221:                                comm_state_changed = true;
                   17222:                        }
                   17223:                        LeaveCriticalSection(&q->csLineCtrl);
                   17224:                        
                   17225:                        if(comm_state_changed) {
1.1.1.26  root     17226:                                if(GetCommState(hComm, &dcb)) {
                   17227: //                                     dcb.BaudRate = min(9600, 115200 / p->prev_divisor);
                   17228:                                        DWORD baud = 115200 / p->prev_divisor;
                   17229:                                        dcb.BaudRate = 9600; // default
                   17230:                                        
                   17231:                                        if((dwSettableBaud & BAUD_075  ) && baud >= 75   ) dcb.BaudRate = 75;
                   17232:                                        if((dwSettableBaud & BAUD_110  ) && baud >= 110  ) dcb.BaudRate = 110;
                   17233:                                        // 134.5bps is not supported ???
                   17234: //                                     if((dwSettableBaud & BAUD_134_5) && baud >= 134.5) dcb.BaudRate = 134.5;
                   17235:                                        if((dwSettableBaud & BAUD_150  ) && baud >= 150  ) dcb.BaudRate = 150;
                   17236:                                        if((dwSettableBaud & BAUD_300  ) && baud >= 300  ) dcb.BaudRate = 300;
                   17237:                                        if((dwSettableBaud & BAUD_600  ) && baud >= 600  ) dcb.BaudRate = 600;
                   17238:                                        if((dwSettableBaud & BAUD_1200 ) && baud >= 1200 ) dcb.BaudRate = 1200;
                   17239:                                        if((dwSettableBaud & BAUD_1800 ) && baud >= 1800 ) dcb.BaudRate = 1800;
                   17240:                                        if((dwSettableBaud & BAUD_2400 ) && baud >= 2400 ) dcb.BaudRate = 2400;
                   17241:                                        if((dwSettableBaud & BAUD_4800 ) && baud >= 4800 ) dcb.BaudRate = 4800;
                   17242:                                        if((dwSettableBaud & BAUD_7200 ) && baud >= 7200 ) dcb.BaudRate = 7200;
                   17243:                                        if((dwSettableBaud & BAUD_9600 ) && baud >= 9600 ) dcb.BaudRate = 9600;
                   17244: //                                     if((dwSettableBaud & BAUD_14400) && baud >= 14400) dcb.BaudRate = 14400;
                   17245: //                                     if((dwSettableBaud & BAUD_19200) && baud >= 19200) dcb.BaudRate = 19200;
                   17246: //                                     if((dwSettableBaud & BAUD_38400) && baud >= 38400) dcb.BaudRate = 38400;
                   17247:                                        
                   17248:                                        switch(p->prev_line_ctrl & 0x03) {
                   17249:                                        case 0x00: dcb.ByteSize = 5; break;
                   17250:                                        case 0x01: dcb.ByteSize = 6; break;
                   17251:                                        case 0x02: dcb.ByteSize = 7; break;
                   17252:                                        case 0x03: dcb.ByteSize = 8; break;
                   17253:                                        }
                   17254:                                        switch(p->prev_line_ctrl & 0x04) {
                   17255:                                        case 0x00: dcb.StopBits = ONESTOPBIT; break;
                   17256:                                        case 0x04: dcb.StopBits = (dcb.ByteSize == 5) ? ONE5STOPBITS : TWOSTOPBITS; break;
                   17257:                                        }
                   17258:                                        switch(p->prev_line_ctrl & 0x38) {
                   17259:                                        case 0x08: dcb.Parity = ODDPARITY;   break;
                   17260:                                        case 0x18: dcb.Parity = EVENPARITY;  break;
                   17261:                                        case 0x28: dcb.Parity = MARKPARITY;  break;
                   17262:                                        case 0x38: dcb.Parity = SPACEPARITY; break;
                   17263:                                        default:   dcb.Parity = NOPARITY;    break;
                   17264:                                        }
                   17265:                                        dcb.fBinary = TRUE;
                   17266:                                        dcb.fParity = (dcb.Parity != NOPARITY);
                   17267:                                        dcb.fOutxCtsFlow = dcb.fOutxDsrFlow = TRUE;
                   17268:                                        dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
                   17269:                                        dcb.fDsrSensitivity = FALSE;//TRUE;
                   17270:                                        dcb.fTXContinueOnXoff = TRUE;
                   17271:                                        dcb.fOutX = dcb.fInX = FALSE;
                   17272:                                        dcb.fErrorChar = FALSE;
                   17273:                                        dcb.fNull = FALSE;
                   17274:                                        dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
                   17275:                                        dcb.fAbortOnError = FALSE;
                   17276:                                        
                   17277:                                        SetCommState(hComm, &dcb);
1.1.1.25  root     17278:                                }
                   17279:                                
                   17280:                                // check again to apply all comm state changes
                   17281:                                Sleep(10);
                   17282:                                continue;
                   17283:                        }
                   17284:                        
                   17285:                        // set comm pins
                   17286:                        bool change_brk = false;
1.1.1.26  root     17287: //                     bool change_rts = false;
                   17288: //                     bool change_dtr = false;
1.1.1.25  root     17289:                        
                   17290:                        EnterCriticalSection(&q->csModemCtrl);
                   17291:                        if(p->prev_set_brk != p->set_brk) {
                   17292:                                p->prev_set_brk = p->set_brk;
                   17293:                                change_brk = true;
                   17294:                        }
1.1.1.26  root     17295: //                     if(p->prev_set_rts != p->set_rts) {
                   17296: //                             p->prev_set_rts = p->set_rts;
                   17297: //                             change_rts = true;
                   17298: //                     }
                   17299: //                     if(p->prev_set_dtr != p->set_dtr) {
                   17300: //                             p->prev_set_dtr = p->set_dtr;
                   17301: //                             change_dtr = true;
                   17302: //                     }
1.1.1.25  root     17303:                        LeaveCriticalSection(&q->csModemCtrl);
                   17304:                        
                   17305:                        if(change_brk) {
1.1.1.26  root     17306:                                static UINT32 clear_time = 0;
                   17307:                                if(p->prev_set_brk) {
                   17308:                                        EscapeCommFunction(hComm, SETBREAK);
                   17309:                                        clear_time = timeGetTime() + 200;
                   17310:                                } else {
                   17311:                                        // keep break for at least 200msec
                   17312:                                        UINT32 cur_time = timeGetTime();
                   17313:                                        if(clear_time > cur_time) {
                   17314:                                                Sleep(clear_time - cur_time);
                   17315:                                        }
                   17316:                                        EscapeCommFunction(hComm, CLRBREAK);
                   17317:                                }
1.1.1.25  root     17318:                        }
1.1.1.26  root     17319: //                     if(change_rts) {
                   17320: //                             if(p->prev_set_rts) {
                   17321: //                                     EscapeCommFunction(hComm, SETRTS);
                   17322: //                             } else {
                   17323: //                                     EscapeCommFunction(hComm, CLRRTS);
                   17324: //                             }
                   17325: //                     }
                   17326: //                     if(change_dtr) {
                   17327: //                             if(p->prev_set_dtr) {
                   17328: //                                     EscapeCommFunction(hComm, SETDTR);
                   17329: //                             } else {
                   17330: //                                     EscapeCommFunction(hComm, CLRDTR);
                   17331: //                             }
                   17332: //                     }
1.1.1.25  root     17333:                        
                   17334:                        // get comm pins
                   17335:                        DWORD dwModemStat = 0;
                   17336:                        
                   17337:                        if(GetCommModemStatus(hComm, &dwModemStat)) {
                   17338:                                EnterCriticalSection(&q->csModemStat);
                   17339:                                if(dwModemStat & MS_RLSD_ON) {
                   17340:                                        p->modem_stat |= 0x80;
                   17341:                                } else {
                   17342:                                        p->modem_stat &= ~0x80;
                   17343:                                }
                   17344:                                if(dwModemStat & MS_RING_ON) {
                   17345:                                        p->modem_stat |= 0x40;
                   17346:                                } else {
                   17347:                                        p->modem_stat &= ~0x40;
                   17348:                                }
1.1.1.26  root     17349: //                             if(dwModemStat & MS_DSR_ON) {
                   17350: //                                     p->modem_stat |= 0x20;
                   17351: //                             } else {
                   17352: //                                     p->modem_stat &= ~0x20;
                   17353: //                             }
                   17354: //                             if(dwModemStat & MS_CTS_ON) {
                   17355: //                                     p->modem_stat |= 0x10;
                   17356: //                             } else {
                   17357: //                                     p->modem_stat &= ~0x10;
                   17358: //                             }
1.1.1.25  root     17359:                                if((p->prev_modem_stat & 0x80) != (p->modem_stat & 0x80)) {
                   17360:                                        p->modem_stat |= 0x08;
                   17361:                                }
                   17362:                                if((p->prev_modem_stat & 0x40) && !(p->modem_stat & 0x40)) {
                   17363:                                        p->modem_stat |= 0x04;
                   17364:                                }
1.1.1.26  root     17365: //                             if((p->prev_modem_stat & 0x20) != (p->modem_stat & 0x20)) {
                   17366: //                                     p->modem_stat |= 0x02;
                   17367: //                             }
                   17368: //                             if((p->prev_modem_stat & 0x10) != (p->modem_stat & 0x10)) {
                   17369: //                                     p->modem_stat |= 0x01;
                   17370: //                             }
1.1.1.25  root     17371:                                LeaveCriticalSection(&q->csModemStat);
                   17372:                        }
                   17373:                        
                   17374:                        // send data
                   17375:                        DWORD dwSend = 0;
                   17376:                        
                   17377:                        EnterCriticalSection(&q->csSendData);
1.1.1.32  root     17378:                        while(p->send_buffer != NULL && !p->send_buffer->empty()) {
1.1.1.25  root     17379:                                bytBuffer[dwSend++] = p->send_buffer->read();
                   17380:                        }
                   17381:                        LeaveCriticalSection(&q->csSendData);
                   17382:                        
                   17383:                        if(dwSend != 0) {
                   17384:                                DWORD dwWritten = 0;
                   17385:                                WriteFile(hComm, bytBuffer, dwSend, &dwWritten, NULL);
                   17386:                        }
                   17387:                        
                   17388:                        // get line status and recv data
                   17389:                        DWORD dwLineStat = 0;
                   17390:                        COMSTAT comStat;
                   17391:                        
                   17392:                        if(ClearCommError(hComm, &dwLineStat, &comStat)) {
                   17393:                                EnterCriticalSection(&q->csLineStat);
                   17394:                                if(dwLineStat & CE_BREAK) {
                   17395:                                        p->line_stat_err |= 0x10;
                   17396:                                }
                   17397:                                if(dwLineStat & CE_FRAME) {
                   17398:                                        p->line_stat_err |= 0x08;
                   17399:                                }
                   17400:                                if(dwLineStat & CE_RXPARITY) {
                   17401:                                        p->line_stat_err |= 0x04;
                   17402:                                }
                   17403:                                if(dwLineStat & CE_OVERRUN) {
                   17404:                                        p->line_stat_err |= 0x02;
                   17405:                                }
                   17406:                                LeaveCriticalSection(&q->csLineStat);
                   17407:                                
                   17408:                                if(comStat.cbInQue != 0) {
                   17409:                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     17410:                                        DWORD dwRecv = 0;
                   17411:                                        if(p->recv_buffer != NULL) {
                   17412:                                                dwRecv = min(comStat.cbInQue, p->recv_buffer->remain());
                   17413:                                        }
1.1.1.25  root     17414:                                        LeaveCriticalSection(&q->csRecvData);
                   17415:                                        
                   17416:                                        if(dwRecv != 0) {
                   17417:                                                DWORD dwRead = 0;
                   17418:                                                if(ReadFile(hComm, bytBuffer, dwRecv, &dwRead, NULL) && dwRead != 0) {
                   17419:                                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     17420:                                                        if(p->recv_buffer != NULL) {
                   17421:                                                                for(int i = 0; i < dwRead; i++) {
                   17422:                                                                        p->recv_buffer->write(bytBuffer[i]);
                   17423:                                                                }
1.1.1.25  root     17424:                                                        }
                   17425:                                                        LeaveCriticalSection(&q->csRecvData);
                   17426:                                                }
                   17427:                                        }
                   17428:                                }
                   17429:                        }
                   17430:                        Sleep(10);
                   17431:                }
                   17432:                CloseHandle(hComm);
                   17433:        }
                   17434:        return 0;
                   17435: }
                   17436: 
1.1.1.8   root     17437: // cmos
                   17438: 
                   17439: void cmos_init()
                   17440: {
                   17441:        memset(cmos, 0, sizeof(cmos));
                   17442:        cmos_addr = 0;
1.1       root     17443:        
1.1.1.8   root     17444:        // from DOSBox
                   17445:        cmos_write(0x0a, 0x26);
                   17446:        cmos_write(0x0b, 0x02);
                   17447:        cmos_write(0x0d, 0x80);
1.1       root     17448: }
                   17449: 
1.1.1.8   root     17450: void cmos_write(int addr, UINT8 val)
1.1       root     17451: {
1.1.1.8   root     17452:        cmos[addr & 0x7f] = val;
                   17453: }
                   17454: 
                   17455: #define CMOS_GET_TIME() { \
                   17456:        UINT32 cur_sec = timeGetTime() / 1000 ; \
                   17457:        if(prev_sec != cur_sec) { \
                   17458:                GetLocalTime(&time); \
                   17459:                prev_sec = cur_sec; \
                   17460:        } \
1.1       root     17461: }
1.1.1.8   root     17462: #define CMOS_BCD(v) ((cmos[0x0b] & 4) ? (v) : to_bcd(v))
1.1       root     17463: 
1.1.1.8   root     17464: UINT8 cmos_read(int addr)
1.1       root     17465: {
1.1.1.8   root     17466:        static SYSTEMTIME time;
                   17467:        static UINT32 prev_sec = 0;
1.1       root     17468:        
1.1.1.8   root     17469:        switch(addr & 0x7f) {
                   17470:        case 0x00: CMOS_GET_TIME(); return(CMOS_BCD(time.wSecond));
                   17471:        case 0x02: CMOS_GET_TIME(); return(CMOS_BCD(time.wMinute));
                   17472:        case 0x04: CMOS_GET_TIME(); return(CMOS_BCD(time.wHour));
                   17473:        case 0x06: CMOS_GET_TIME(); return(time.wDayOfWeek + 1);
                   17474:        case 0x07: CMOS_GET_TIME(); return(CMOS_BCD(time.wDay));
                   17475:        case 0x08: CMOS_GET_TIME(); return(CMOS_BCD(time.wMonth));
                   17476:        case 0x09: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear));
                   17477: //     case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 2 ? 0x80 : 0));       // 2msec
                   17478:        case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 20 ? 0x80 : 0));      // precision of timeGetTime() may not be 1msec
                   17479:        case 0x15: return((MEMORY_END >> 10) & 0xff);
                   17480:        case 0x16: return((MEMORY_END >> 18) & 0xff);
                   17481:        case 0x17: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   17482:        case 0x18: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   17483:        case 0x30: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   17484:        case 0x31: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   17485:        case 0x32: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear / 100));
1.1       root     17486:        }
1.1.1.8   root     17487:        return(cmos[addr & 0x7f]);
1.1       root     17488: }
                   17489: 
1.1.1.7   root     17490: // kbd (a20)
                   17491: 
                   17492: void kbd_init()
                   17493: {
1.1.1.8   root     17494:        kbd_data = kbd_command = 0;
1.1.1.7   root     17495:        kbd_status = 0x18;
                   17496: }
                   17497: 
                   17498: UINT8 kbd_read_data()
                   17499: {
1.1.1.8   root     17500:        kbd_status &= ~1;
1.1.1.7   root     17501:        return(kbd_data);
                   17502: }
                   17503: 
                   17504: void kbd_write_data(UINT8 val)
                   17505: {
                   17506:        switch(kbd_command) {
                   17507:        case 0xd1:
                   17508:                i386_set_a20_line((val >> 1) & 1);
                   17509:                break;
                   17510:        }
                   17511:        kbd_command = 0;
1.1.1.8   root     17512:        kbd_status &= ~8;
1.1.1.7   root     17513: }
                   17514: 
                   17515: UINT8 kbd_read_status()
                   17516: {
                   17517:        return(kbd_status);
                   17518: }
                   17519: 
                   17520: void kbd_write_command(UINT8 val)
                   17521: {
                   17522:        switch(val) {
                   17523:        case 0xd0:
                   17524:                kbd_data = ((m_a20_mask >> 19) & 2) | 1;
1.1.1.8   root     17525:                kbd_status |= 1;
1.1.1.7   root     17526:                break;
                   17527:        case 0xdd:
                   17528:                i386_set_a20_line(0);
                   17529:                break;
                   17530:        case 0xdf:
                   17531:                i386_set_a20_line(1);
                   17532:                break;
1.1.1.26  root     17533:        case 0xf0: case 0xf1: case 0xf2: case 0xf3: case 0xf4: case 0xf5: case 0xf6: case 0xf7:
                   17534:        case 0xf8: case 0xf9: case 0xfa: case 0xfb: case 0xfc: case 0xfd: case 0xfe: case 0xff:
1.1.1.7   root     17535:                if(!(val & 1)) {
1.1.1.8   root     17536:                        if((cmos[0x0f] & 0x7f) == 5) {
1.1.1.7   root     17537:                                // reset pic
                   17538:                                pic_init();
                   17539:                                pic[0].irr = pic[1].irr = 0x00;
                   17540:                                pic[0].imr = pic[1].imr = 0xff;
                   17541:                        }
                   17542:                        CPU_RESET_CALL(CPU_MODEL);
                   17543:                        i386_jmp_far(0x40, 0x67);
                   17544:                }
                   17545:                i386_set_a20_line((val >> 1) & 1);
                   17546:                break;
                   17547:        }
                   17548:        kbd_command = val;
1.1.1.8   root     17549:        kbd_status |= 8;
1.1.1.7   root     17550: }
                   17551: 
1.1.1.9   root     17552: // vga
                   17553: 
                   17554: UINT8 vga_read_status()
                   17555: {
                   17556:        // 60hz
                   17557:        static const int period[3] = {16, 17, 17};
                   17558:        static int index = 0;
                   17559:        UINT32 time = timeGetTime() % period[index];
                   17560:        
                   17561:        index = (index + 1) % 3;
1.1.1.14  root     17562:        return((time < 4 ? 0x08 : 0) | (time == 0 ? 0 : 0x01));
1.1.1.9   root     17563: }
                   17564: 
1.1       root     17565: // i/o bus
                   17566: 
1.1.1.29  root     17567: // this is ugly patch for SW1US.EXE, it sometimes mistakely read/write 01h-10h for serial I/O
                   17568: //#define SW1US_PATCH
                   17569: 
1.1.1.25  root     17570: UINT8 read_io_byte(offs_t addr)
1.1.1.33  root     17571: #ifdef USE_DEBUGGER
1.1.1.25  root     17572: {
1.1.1.33  root     17573:        if(now_debugging) {
                   17574:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   17575:                        if(in_break_point.table[i].status == 1) {
                   17576:                                if(addr == in_break_point.table[i].addr) {
                   17577:                                        in_break_point.hit = i + 1;
                   17578:                                        now_suspended = true;
                   17579:                                        break;
                   17580:                                }
                   17581:                        }
                   17582:                }
1.1.1.25  root     17583:        }
1.1.1.33  root     17584:        return(debugger_read_io_byte(addr));
1.1.1.25  root     17585: }
1.1.1.33  root     17586: UINT8 debugger_read_io_byte(offs_t addr)
1.1.1.25  root     17587: #endif
1.1       root     17588: {
1.1.1.33  root     17589:        UINT8 val = 0xff;
                   17590:        
1.1       root     17591:        switch(addr) {
1.1.1.29  root     17592: #ifdef SW1US_PATCH
                   17593:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   17594:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
1.1.1.33  root     17595:                val = sio_read(0, addr - 1);
                   17596:                break;
1.1.1.29  root     17597: #else
1.1.1.25  root     17598:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   17599:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
1.1.1.33  root     17600:                val = dma_read(0, addr);
                   17601:                break;
1.1.1.29  root     17602: #endif
1.1.1.25  root     17603:        case 0x20: case 0x21:
1.1.1.33  root     17604:                val = pic_read(0, addr);
                   17605:                break;
1.1.1.25  root     17606:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1.1.33  root     17607:                val = pit_read(addr & 0x03);
                   17608:                break;
1.1.1.7   root     17609:        case 0x60:
1.1.1.33  root     17610:                val = kbd_read_data();
                   17611:                break;
1.1.1.9   root     17612:        case 0x61:
1.1.1.33  root     17613:                val = system_port;
                   17614:                break;
1.1.1.7   root     17615:        case 0x64:
1.1.1.33  root     17616:                val = kbd_read_status();
                   17617:                break;
1.1       root     17618:        case 0x71:
1.1.1.33  root     17619:                val = cmos_read(cmos_addr);
                   17620:                break;
1.1.1.25  root     17621:        case 0x81:
1.1.1.33  root     17622:                val = dma_page_read(0, 2);
                   17623:                break;
1.1.1.25  root     17624:        case 0x82:
1.1.1.33  root     17625:                val = dma_page_read(0, 3);
                   17626:                break;
1.1.1.25  root     17627:        case 0x83:
1.1.1.33  root     17628:                val = dma_page_read(0, 1);
                   17629:                break;
1.1.1.25  root     17630:        case 0x87:
1.1.1.33  root     17631:                val = dma_page_read(0, 0);
                   17632:                break;
1.1.1.25  root     17633:        case 0x89:
1.1.1.33  root     17634:                val = dma_page_read(1, 2);
                   17635:                break;
1.1.1.25  root     17636:        case 0x8a:
1.1.1.33  root     17637:                val = dma_page_read(1, 3);
                   17638:                break;
1.1.1.25  root     17639:        case 0x8b:
1.1.1.33  root     17640:                val = dma_page_read(1, 1);
                   17641:                break;
1.1.1.25  root     17642:        case 0x8f:
1.1.1.33  root     17643:                val = dma_page_read(1, 0);
                   17644:                break;
1.1       root     17645:        case 0x92:
1.1.1.33  root     17646:                val = (m_a20_mask >> 19) & 2;
                   17647:                break;
1.1.1.25  root     17648:        case 0xa0: case 0xa1:
1.1.1.33  root     17649:                val = pic_read(1, addr);
                   17650:                break;
1.1.1.25  root     17651:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   17652:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.33  root     17653:                val = dma_read(1, (addr - 0xc0) >> 1);
                   17654:                break;
1.1.1.37  root     17655:        case 0x278: case 0x279: case 0x27a:
                   17656:                val = pio_read(1, addr);
                   17657:                break;
1.1.1.29  root     17658:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
1.1.1.33  root     17659:                val = sio_read(3, addr);
                   17660:                break;
1.1.1.25  root     17661:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
1.1.1.33  root     17662:                val = sio_read(1, addr);
                   17663:                break;
1.1.1.25  root     17664:        case 0x378: case 0x379: case 0x37a:
1.1.1.33  root     17665:                val = pio_read(0, addr);
                   17666:                break;
1.1.1.25  root     17667:        case 0x3ba: case 0x3da:
1.1.1.33  root     17668:                val = vga_read_status();
                   17669:                break;
1.1.1.37  root     17670:        case 0x3bc: case 0x3bd: case 0x3be:
                   17671:                val = pio_read(2, addr);
                   17672:                break;
1.1.1.29  root     17673:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
1.1.1.33  root     17674:                val = sio_read(2, addr);
                   17675:                break;
1.1.1.25  root     17676:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
1.1.1.33  root     17677:                val = sio_read(0, addr);
                   17678:                break;
1.1       root     17679:        default:
1.1.1.33  root     17680: //             fatalerror("unknown inb %4x\n", addr);
1.1       root     17681:                break;
                   17682:        }
1.1.1.33  root     17683: #ifdef ENABLE_DEBUG_IOPORT
                   17684:        if(fp_debug_log != NULL) {
                   17685:                fprintf(fp_debug_log, "inb %04X, %02X\n", addr, val);
                   17686:        }
                   17687: #endif
                   17688:        return(val);
1.1       root     17689: }
                   17690: 
                   17691: UINT16 read_io_word(offs_t addr)
                   17692: {
                   17693:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8));
                   17694: }
                   17695: 
1.1.1.33  root     17696: #ifdef USE_DEBUGGER
                   17697: UINT16 debugger_read_io_word(offs_t addr)
                   17698: {
                   17699:        return(debugger_read_io_byte(addr) | (debugger_read_io_byte(addr + 1) << 8));
                   17700: }
                   17701: #endif
                   17702: 
1.1       root     17703: UINT32 read_io_dword(offs_t addr)
                   17704: {
                   17705:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8) | (read_io_byte(addr + 2) << 16) | (read_io_byte(addr + 3) << 24));
                   17706: }
                   17707: 
1.1.1.33  root     17708: #ifdef USE_DEBUGGER
                   17709: UINT32 debugger_read_io_dword(offs_t addr)
                   17710: {
                   17711:        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));
                   17712: }
                   17713: #endif
                   17714: 
1.1       root     17715: void write_io_byte(offs_t addr, UINT8 val)
1.1.1.33  root     17716: #ifdef USE_DEBUGGER
                   17717: {
                   17718:        if(now_debugging) {
                   17719:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   17720:                        if(out_break_point.table[i].status == 1) {
                   17721:                                if(addr == out_break_point.table[i].addr) {
                   17722:                                        out_break_point.hit = i + 1;
                   17723:                                        now_suspended = true;
                   17724:                                        break;
                   17725:                                }
                   17726:                        }
                   17727:                }
                   17728:        }
                   17729:        debugger_write_io_byte(addr, val);
                   17730: }
                   17731: void debugger_write_io_byte(offs_t addr, UINT8 val)
                   17732: #endif
1.1       root     17733: {
1.1.1.25  root     17734: #ifdef ENABLE_DEBUG_IOPORT
1.1.1.33  root     17735:        if(fp_debug_log != NULL) {
                   17736:                fprintf(fp_debug_log, "outb %04X, %02X\n", addr, val);
1.1.1.25  root     17737:        }
                   17738: #endif
1.1       root     17739:        switch(addr) {
1.1.1.29  root     17740: #ifdef SW1US_PATCH
                   17741:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   17742:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
                   17743:                sio_write(0, addr - 1, val);
                   17744:                break;
                   17745: #else
1.1.1.25  root     17746:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   17747:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
                   17748:                dma_write(0, addr, val);
                   17749:                break;
1.1.1.29  root     17750: #endif
1.1.1.25  root     17751:        case 0x20: case 0x21:
1.1       root     17752:                pic_write(0, addr, val);
                   17753:                break;
1.1.1.25  root     17754:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1       root     17755:                pit_write(addr & 0x03, val);
                   17756:                break;
1.1.1.7   root     17757:        case 0x60:
                   17758:                kbd_write_data(val);
                   17759:                break;
1.1.1.9   root     17760:        case 0x61:
                   17761:                if((system_port & 3) != 3 && (val & 3) == 3) {
                   17762:                        // beep on
                   17763: //                     MessageBeep(-1);
                   17764:                } else if((system_port & 3) == 3 && (val & 3) != 3) {
                   17765:                        // beep off
                   17766:                }
                   17767:                system_port = val;
                   17768:                break;
1.1       root     17769:        case 0x64:
1.1.1.7   root     17770:                kbd_write_command(val);
1.1       root     17771:                break;
                   17772:        case 0x70:
                   17773:                cmos_addr = val;
                   17774:                break;
                   17775:        case 0x71:
1.1.1.8   root     17776:                cmos_write(cmos_addr, val);
1.1       root     17777:                break;
1.1.1.25  root     17778:        case 0x81:
                   17779:                dma_page_write(0, 2, val);
                   17780:        case 0x82:
                   17781:                dma_page_write(0, 3, val);
                   17782:        case 0x83:
                   17783:                dma_page_write(0, 1, val);
                   17784:        case 0x87:
                   17785:                dma_page_write(0, 0, val);
                   17786:        case 0x89:
                   17787:                dma_page_write(1, 2, val);
                   17788:        case 0x8a:
                   17789:                dma_page_write(1, 3, val);
                   17790:        case 0x8b:
                   17791:                dma_page_write(1, 1, val);
                   17792:        case 0x8f:
                   17793:                dma_page_write(1, 0, val);
1.1       root     17794:        case 0x92:
1.1.1.7   root     17795:                i386_set_a20_line((val >> 1) & 1);
1.1       root     17796:                break;
1.1.1.25  root     17797:        case 0xa0: case 0xa1:
1.1       root     17798:                pic_write(1, addr, val);
                   17799:                break;
1.1.1.25  root     17800:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   17801:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.26  root     17802:                dma_write(1, (addr - 0xc0) >> 1, val);
1.1.1.25  root     17803:                break;
1.1.1.35  root     17804: #ifdef USE_SERVICE_THREAD
                   17805:        case 0xf7:
                   17806:                // dummy i/o for BIOS/DOS service
1.1.1.36  root     17807:                if(in_service && cursor_moved) {
                   17808:                        // update cursor position before service is done
                   17809:                        pcbios_update_cursor_position();
                   17810:                        cursor_moved = false;
                   17811:                }
1.1.1.35  root     17812:                finish_service_loop();
                   17813:                break;
                   17814: #endif
1.1.1.37  root     17815:        case 0x278: case 0x279: case 0x27a:
                   17816:                pio_write(1, addr, val);
                   17817:                break;
1.1.1.29  root     17818:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
                   17819:                sio_write(3, addr, val);
                   17820:                break;
1.1.1.25  root     17821:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
                   17822:                sio_write(1, addr, val);
                   17823:                break;
                   17824:        case 0x378: case 0x379: case 0x37a:
                   17825:                pio_write(0, addr, val);
                   17826:                break;
1.1.1.37  root     17827:        case 0x3bc: case 0x3bd: case 0x3be:
                   17828:                pio_write(2, addr, val);
                   17829:                break;
1.1.1.29  root     17830:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
                   17831:                sio_write(2, addr, val);
                   17832:                break;
1.1.1.25  root     17833:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
                   17834:                sio_write(0, addr, val);
                   17835:                break;
1.1       root     17836:        default:
1.1.1.33  root     17837: //             fatalerror("unknown outb %4x,%2x\n", addr, val);
1.1       root     17838:                break;
                   17839:        }
                   17840: }
                   17841: 
                   17842: void write_io_word(offs_t addr, UINT16 val)
                   17843: {
                   17844:        write_io_byte(addr + 0, (val >> 0) & 0xff);
                   17845:        write_io_byte(addr + 1, (val >> 8) & 0xff);
                   17846: }
                   17847: 
1.1.1.33  root     17848: #ifdef USE_DEBUGGER
                   17849: void debugger_write_io_word(offs_t addr, UINT16 val)
                   17850: {
                   17851:        debugger_write_io_byte(addr + 0, (val >> 0) & 0xff);
                   17852:        debugger_write_io_byte(addr + 1, (val >> 8) & 0xff);
                   17853: }
                   17854: #endif
                   17855: 
1.1       root     17856: void write_io_dword(offs_t addr, UINT32 val)
                   17857: {
                   17858:        write_io_byte(addr + 0, (val >>  0) & 0xff);
                   17859:        write_io_byte(addr + 1, (val >>  8) & 0xff);
                   17860:        write_io_byte(addr + 2, (val >> 16) & 0xff);
                   17861:        write_io_byte(addr + 3, (val >> 24) & 0xff);
                   17862: }
1.1.1.33  root     17863: 
                   17864: #ifdef USE_DEBUGGER
                   17865: void debugger_write_io_dword(offs_t addr, UINT32 val)
                   17866: {
                   17867:        debugger_write_io_byte(addr + 0, (val >>  0) & 0xff);
                   17868:        debugger_write_io_byte(addr + 1, (val >>  8) & 0xff);
                   17869:        debugger_write_io_byte(addr + 2, (val >> 16) & 0xff);
                   17870:        debugger_write_io_byte(addr + 3, (val >> 24) & 0xff);
                   17871: }
                   17872: #endif

unix.superglobalmegacorp.com

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