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

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: 
1.1.1.40! root     6797: HDC get_console_window_device_context()
        !          6798: {
        !          6799:        static HWND hwndFound = 0;
        !          6800:        
        !          6801:        if(hwndFound == 0) {
        !          6802:                // https://support.microsoft.com/en-us/help/124103/how-to-obtain-a-console-window-handle-hwnd
        !          6803:                char pszNewWindowTitle[1024];
        !          6804:                char pszOldWindowTitle[1024];
        !          6805:                
        !          6806:                GetConsoleTitle(pszOldWindowTitle, 1024);
        !          6807:                wsprintf(pszNewWindowTitle,"%d/%d", GetTickCount(), GetCurrentProcessId());
        !          6808:                SetConsoleTitle(pszNewWindowTitle);
        !          6809:                Sleep(100);
        !          6810:                hwndFound = FindWindow(NULL, pszNewWindowTitle);
        !          6811:                SetConsoleTitle(pszOldWindowTitle);
        !          6812:        }
        !          6813:        return GetDC(hwndFound);
        !          6814: }
        !          6815: 
        !          6816: inline void pcbios_int_10h_0ch()
        !          6817: {
        !          6818:        HDC hdc = get_console_window_device_context();
        !          6819:        
        !          6820:        if(hdc != NULL) {
        !          6821:                BYTE r = (REG8(AL) & 2) ? 255 : 0;
        !          6822:                BYTE g = (REG8(AL) & 4) ? 255 : 0;
        !          6823:                BYTE b = (REG8(AL) & 1) ? 255 : 0;
        !          6824:                
        !          6825:                if(REG8(AL) & 0x80) {
        !          6826:                        COLORREF color = GetPixel(hdc, REG16(CX), REG16(DX));
        !          6827:                        if(color != CLR_INVALID) {
        !          6828:                                r ^= ((DWORD)color & 0x0000ff) ? 255 : 0;
        !          6829:                                g ^= ((DWORD)color & 0x00ff00) ? 255 : 0;
        !          6830:                                b ^= ((DWORD)color & 0xff0000) ? 255 : 0;
        !          6831:                        }
        !          6832:                }
        !          6833:                SetPixel(hdc, REG16(CX), REG16(DX), RGB(r, g, b));
        !          6834:        }
        !          6835: }
        !          6836: 
        !          6837: inline void pcbios_int_10h_0dh()
        !          6838: {
        !          6839:        HDC hdc = get_console_window_device_context();
        !          6840:        BYTE r = 0;
        !          6841:        BYTE g = 0;
        !          6842:        BYTE b = 0;
        !          6843:        
        !          6844:        if(hdc != NULL) {
        !          6845:                COLORREF color = GetPixel(hdc, REG16(CX), REG16(DX));
        !          6846:                if(color != CLR_INVALID) {
        !          6847:                        r = ((DWORD)color & 0x0000ff) ? 255 : 0;
        !          6848:                        g = ((DWORD)color & 0x00ff00) ? 255 : 0;
        !          6849:                        b = ((DWORD)color & 0xff0000) ? 255 : 0;
        !          6850:                }
        !          6851:        }
        !          6852:        REG8(AL) = ((b != 0) ? 1 : 0) | ((r != 0) ? 2 : 0) | ((g != 0) ? 4 : 0);
        !          6853: }
        !          6854: 
1.1       root     6855: inline void pcbios_int_10h_0eh()
                   6856: {
1.1.1.14  root     6857:        DWORD num;
                   6858:        COORD co;
                   6859:        
                   6860:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   6861:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   6862:        
                   6863:        if(REG8(AL) == 7) {
                   6864:                //MessageBeep(-1);
                   6865:        } else if(REG8(AL) == 8 || REG8(AL) == 10 || REG8(AL) == 13) {
                   6866:                if(REG8(AL) == 10) {
                   6867:                        vram_flush();
                   6868:                }
1.1.1.23  root     6869:                WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), &REG8(AL), 1, &num, NULL);
1.1.1.14  root     6870:                cursor_moved = true;
                   6871:        } else {
1.1.1.16  root     6872:                int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
1.1.1.14  root     6873:                if(mem[0x462] == REG8(BH)) {
1.1.1.35  root     6874: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6875:                        EnterCriticalSection(&vram_crit_sect);
1.1.1.35  root     6876: #endif
1.1.1.16  root     6877:                        int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     6878:                        write_text_vram_char(dest - vram, REG8(AL));
1.1.1.35  root     6879: #ifdef USE_VRAM_THREAD
1.1.1.14  root     6880:                        LeaveCriticalSection(&vram_crit_sect);
1.1.1.35  root     6881: #endif
1.1.1.14  root     6882:                        
1.1.1.23  root     6883:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6884:                        if(++co.X == scr_width) {
                   6885:                                co.X = 0;
                   6886:                                if(++co.Y == scr_height) {
                   6887:                                        vram_flush();
                   6888:                                        WriteConsole(hStdout, "\n", 1, &num, NULL);
                   6889:                                        cursor_moved = true;
                   6890:                                }
                   6891:                        }
                   6892:                        if(!cursor_moved) {
                   6893:                                co.Y += scr_top;
                   6894:                                SetConsoleCursorPosition(hStdout, co);
                   6895:                                cursor_moved = true;
                   6896:                        }
                   6897:                }
                   6898:                mem[dest] = REG8(AL);
                   6899:        }
1.1       root     6900: }
                   6901: 
                   6902: inline void pcbios_int_10h_0fh()
                   6903: {
                   6904:        REG8(AL) = mem[0x449];
                   6905:        REG8(AH) = mem[0x44a];
                   6906:        REG8(BH) = mem[0x462];
                   6907: }
                   6908: 
1.1.1.14  root     6909: inline void pcbios_int_10h_11h()
                   6910: {
                   6911:        switch(REG8(AL)) {
1.1.1.16  root     6912:        case 0x01:
1.1.1.14  root     6913:        case 0x11:
1.1.1.16  root     6914:                pcbios_set_console_size(80, 28, true);
1.1.1.14  root     6915:                break;
1.1.1.16  root     6916:        case 0x02:
1.1.1.14  root     6917:        case 0x12:
1.1.1.16  root     6918:                pcbios_set_console_size(80, 50, true);
1.1.1.14  root     6919:                break;
1.1.1.16  root     6920:        case 0x04:
1.1.1.14  root     6921:        case 0x14:
1.1.1.16  root     6922:                pcbios_set_console_size(80, 25, true);
                   6923:                break;
                   6924:        case 0x18:
                   6925:                pcbios_set_console_size(80, 50, true);
1.1.1.14  root     6926:                break;
                   6927:        case 0x30:
                   6928:                SREG(ES) = 0;
                   6929:                i386_load_segment_descriptor(ES);
                   6930:                REG16(BP) = 0;
                   6931:                REG16(CX) = mem[0x485];
                   6932:                REG8(DL) = mem[0x484];
                   6933:                break;
                   6934:        }
                   6935: }
                   6936: 
                   6937: inline void pcbios_int_10h_12h()
                   6938: {
1.1.1.16  root     6939:        switch(REG8(BL)) {
                   6940:        case 0x10:
1.1.1.14  root     6941:                REG16(BX) = 0x0003;
                   6942:                REG16(CX) = 0x0009;
1.1.1.16  root     6943:                break;
1.1.1.14  root     6944:        }
                   6945: }
                   6946: 
1.1       root     6947: inline void pcbios_int_10h_13h()
                   6948: {
1.1.1.3   root     6949:        int ofs = SREG_BASE(ES) + REG16(BP);
1.1       root     6950:        COORD co;
                   6951:        DWORD num;
                   6952:        
                   6953:        co.X = REG8(DL);
1.1.1.14  root     6954:        co.Y = REG8(DH) + scr_top;
                   6955:        
                   6956:        vram_flush();
1.1       root     6957:        
                   6958:        switch(REG8(AL)) {
                   6959:        case 0x00:
                   6960:        case 0x01:
                   6961:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     6962:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6963:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   6964:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   6965:                        SetConsoleCursorPosition(hStdout, co);
                   6966:                        
                   6967:                        if(csbi.wAttributes != REG8(BL)) {
                   6968:                                SetConsoleTextAttribute(hStdout, REG8(BL));
                   6969:                        }
1.1.1.14  root     6970:                        WriteConsole(hStdout, &mem[ofs], REG16(CX), &num, NULL);
                   6971:                        
1.1       root     6972:                        if(csbi.wAttributes != REG8(BL)) {
                   6973:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   6974:                        }
                   6975:                        if(REG8(AL) == 0x00) {
1.1.1.15  root     6976:                                if(!restore_console_on_exit) {
                   6977:                                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   6978:                                        scr_top = csbi.srWindow.Top;
                   6979:                                }
1.1.1.14  root     6980:                                co.X = mem[0x450 + REG8(BH) * 2];
                   6981:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     6982:                                SetConsoleCursorPosition(hStdout, co);
                   6983:                        } else {
                   6984:                                cursor_moved = true;
                   6985:                        }
                   6986:                } else {
1.1.1.3   root     6987:                        m_CF = 1;
1.1       root     6988:                }
                   6989:                break;
                   6990:        case 0x02:
                   6991:        case 0x03:
                   6992:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     6993:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6994:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   6995:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   6996:                        SetConsoleCursorPosition(hStdout, co);
                   6997:                        
                   6998:                        WORD wAttributes = csbi.wAttributes;
                   6999:                        for(int i = 0; i < REG16(CX); i++, ofs += 2) {
                   7000:                                if(wAttributes != mem[ofs + 1]) {
                   7001:                                        SetConsoleTextAttribute(hStdout, mem[ofs + 1]);
                   7002:                                        wAttributes = mem[ofs + 1];
                   7003:                                }
1.1.1.14  root     7004:                                WriteConsole(hStdout, &mem[ofs], 1, &num, NULL);
1.1       root     7005:                        }
                   7006:                        if(csbi.wAttributes != wAttributes) {
                   7007:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   7008:                        }
                   7009:                        if(REG8(AL) == 0x02) {
1.1.1.14  root     7010:                                co.X = mem[0x450 + REG8(BH) * 2];
                   7011:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     7012:                                SetConsoleCursorPosition(hStdout, co);
                   7013:                        } else {
                   7014:                                cursor_moved = true;
                   7015:                        }
                   7016:                } else {
1.1.1.3   root     7017:                        m_CF = 1;
1.1       root     7018:                }
                   7019:                break;
                   7020:        case 0x10:
                   7021:        case 0x11:
                   7022:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7023:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7024:                        ReadConsoleOutputCharacter(hStdout, scr_char, REG16(CX), co, &num);
                   7025:                        ReadConsoleOutputAttribute(hStdout, scr_attr, REG16(CX), co, &num);
                   7026:                        for(int i = 0; i < num; i++) {
                   7027:                                mem[ofs++] = scr_char[i];
                   7028:                                mem[ofs++] = scr_attr[i];
                   7029:                                if(REG8(AL) == 0x11) {
                   7030:                                        mem[ofs++] = 0;
                   7031:                                        mem[ofs++] = 0;
                   7032:                                }
                   7033:                        }
                   7034:                } else {
1.1.1.16  root     7035:                        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     7036:                                mem[ofs++] = mem[src++];
                   7037:                                mem[ofs++] = mem[src++];
                   7038:                                if(REG8(AL) == 0x11) {
                   7039:                                        mem[ofs++] = 0;
                   7040:                                        mem[ofs++] = 0;
                   7041:                                }
1.1.1.14  root     7042:                                if(++co.X == scr_width) {
                   7043:                                        if(++co.Y == scr_height) {
1.1       root     7044:                                                break;
                   7045:                                        }
                   7046:                                        co.X = 0;
                   7047:                                }
                   7048:                        }
                   7049:                }
                   7050:                break;
                   7051:        case 0x20:
                   7052:        case 0x21:
                   7053:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     7054:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     7055:                        int len = min(REG16(CX), scr_width * scr_height);
                   7056:                        for(int i = 0; i < len; i++) {
1.1       root     7057:                                scr_char[i] = mem[ofs++];
                   7058:                                scr_attr[i] = mem[ofs++];
                   7059:                                if(REG8(AL) == 0x21) {
                   7060:                                        ofs += 2;
                   7061:                                }
                   7062:                        }
1.1.1.14  root     7063:                        WriteConsoleOutputCharacter(hStdout, scr_char, len, co, &num);
                   7064:                        WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     7065:                } else {
1.1.1.16  root     7066:                        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     7067:                                mem[dest++] = mem[ofs++];
                   7068:                                mem[dest++] = mem[ofs++];
                   7069:                                if(REG8(AL) == 0x21) {
                   7070:                                        ofs += 2;
                   7071:                                }
1.1.1.14  root     7072:                                if(++co.X == scr_width) {
                   7073:                                        if(++co.Y == scr_height) {
1.1       root     7074:                                                break;
                   7075:                                        }
                   7076:                                        co.X = 0;
                   7077:                                }
                   7078:                        }
                   7079:                }
                   7080:                break;
                   7081:        default:
1.1.1.22  root     7082:                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     7083:                m_CF = 1;
1.1       root     7084:                break;
                   7085:        }
                   7086: }
                   7087: 
1.1.1.30  root     7088: inline void pcbios_int_10h_18h()
                   7089: {
                   7090:        switch(REG8(AL)) {
                   7091:        case 0x00:
                   7092:        case 0x01:
                   7093: //             REG8(AL) = 0x86;
                   7094:                REG8(AL) = 0x00;
                   7095:                break;
                   7096:        default:
                   7097:                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));
                   7098:                m_CF = 1;
                   7099:                break;
                   7100:        }
                   7101: }
                   7102: 
1.1.1.14  root     7103: inline void pcbios_int_10h_1ah()
                   7104: {
                   7105:        switch(REG8(AL)) {
                   7106:        case 0x00:
                   7107:                REG8(AL) = 0x1a;
                   7108:                REG8(BL) = 0x08;
                   7109:                REG8(BH) = 0x00;
                   7110:                break;
                   7111:        default:
1.1.1.22  root     7112:                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     7113:                m_CF = 1;
                   7114:                break;
                   7115:        }
                   7116: }
                   7117: 
1.1       root     7118: inline void pcbios_int_10h_1dh()
                   7119: {
                   7120:        switch(REG8(AL)) {
                   7121:        case 0x01:
                   7122:                break;
                   7123:        case 0x02:
                   7124:                REG16(BX) = 0;
                   7125:                break;
                   7126:        default:
1.1.1.22  root     7127:                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));
                   7128:                m_CF = 1;
                   7129:                break;
                   7130:        }
                   7131: }
                   7132: 
                   7133: inline void pcbios_int_10h_4fh()
                   7134: {
                   7135:        switch(REG8(AL)) {
                   7136:        case 0x00:
                   7137:                REG8(AH) = 0x02; // not supported
                   7138:                break;
                   7139:        case 0x01:
                   7140:        case 0x02:
                   7141:        case 0x03:
                   7142:        case 0x04:
                   7143:        case 0x05:
                   7144:        case 0x06:
                   7145:        case 0x07:
                   7146:        case 0x08:
                   7147:        case 0x09:
                   7148:        case 0x0a:
                   7149:        case 0x0b:
                   7150:        case 0x0c:
                   7151:                REG8(AH) = 0x01; // failed
                   7152:                break;
                   7153:        default:
                   7154:                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     7155:                m_CF = 1;
1.1       root     7156:                break;
                   7157:        }
                   7158: }
                   7159: 
                   7160: inline void pcbios_int_10h_82h()
                   7161: {
                   7162:        static UINT8 mode = 0;
                   7163:        
                   7164:        switch(REG8(AL)) {
1.1.1.22  root     7165:        case 0x00:
1.1       root     7166:                if(REG8(BL) != 0xff) {
                   7167:                        mode = REG8(BL);
                   7168:                }
                   7169:                REG8(AL) = mode;
                   7170:                break;
                   7171:        default:
1.1.1.22  root     7172:                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     7173:                m_CF = 1;
1.1       root     7174:                break;
                   7175:        }
                   7176: }
                   7177: 
1.1.1.22  root     7178: inline void pcbios_int_10h_83h()
                   7179: {
                   7180:        static UINT8 mode = 0;
                   7181:        
                   7182:        switch(REG8(AL)) {
                   7183:        case 0x00:
                   7184:                REG16(AX) = 0; // offset???
                   7185:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
                   7186:                i386_load_segment_descriptor(ES);
                   7187:                REG16(BX) = (SHADOW_BUF_TOP & 0x0f);
                   7188:                break;
                   7189:        default:
                   7190:                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));
                   7191:                m_CF = 1;
                   7192:                break;
                   7193:        }
                   7194: }
                   7195: 
                   7196: inline void pcbios_int_10h_90h()
                   7197: {
                   7198:        REG8(AL) = mem[0x449];
                   7199: }
                   7200: 
                   7201: inline void pcbios_int_10h_91h()
                   7202: {
                   7203:        REG8(AL) = 0x04; // VGA
                   7204: }
                   7205: 
                   7206: inline void pcbios_int_10h_efh()
                   7207: {
                   7208:        REG16(DX) = 0xffff;
                   7209: }
                   7210: 
1.1       root     7211: inline void pcbios_int_10h_feh()
                   7212: {
                   7213:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     7214:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
1.1.1.3   root     7215:                i386_load_segment_descriptor(ES);
1.1.1.8   root     7216:                REG16(DI) = (SHADOW_BUF_TOP & 0x0f);
1.1       root     7217:        }
                   7218:        int_10h_feh_called = true;
                   7219: }
                   7220: 
                   7221: inline void pcbios_int_10h_ffh()
                   7222: {
                   7223:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.23  root     7224:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     7225:                COORD co;
                   7226:                DWORD num;
                   7227:                
1.1.1.14  root     7228:                vram_flush();
                   7229:                
                   7230:                co.X = (REG16(DI) >> 1) % scr_width;
                   7231:                co.Y = (REG16(DI) >> 1) / scr_width;
1.1.1.16  root     7232:                int ofs = pcbios_get_shadow_buffer_address(0, co.X, co.Y);
                   7233:                int end = min(ofs + REG16(CX) * 2, pcbios_get_shadow_buffer_address(0, 0, scr_height));
1.1.1.14  root     7234:                int len;
                   7235:                for(len = 0; ofs < end; len++) {
                   7236:                        scr_char[len] = mem[ofs++];
                   7237:                        scr_attr[len] = mem[ofs++];
                   7238:                }
                   7239:                co.Y += scr_top;
                   7240:                WriteConsoleOutputCharacter(hStdout, scr_char, len, co, &num);
                   7241:                WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     7242:        }
                   7243:        int_10h_ffh_called = true;
                   7244: }
                   7245: 
1.1.1.25  root     7246: inline void pcbios_int_14h_00h()
                   7247: {
1.1.1.29  root     7248:        if(REG16(DX) < 4) {
1.1.1.25  root     7249:                static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600};
                   7250:                UINT8 selector = sio_read(REG16(DX), 3);
                   7251:                selector &= ~0x3f;
                   7252:                selector |= REG8(AL) & 0x1f;
                   7253:                UINT16 divisor = 115200 / rate[REG8(AL) >> 5];
                   7254:                sio_write(REG16(DX), 3, selector | 0x80);
                   7255:                sio_write(REG16(DX), 0, divisor & 0xff);
                   7256:                sio_write(REG16(DX), 1, divisor >> 8);
                   7257:                sio_write(REG16(DX), 3, selector);
                   7258:                REG8(AH) = sio_read(REG16(DX), 5);
                   7259:                REG8(AL) = sio_read(REG16(DX), 6);
                   7260:        } else {
                   7261:                REG8(AH) = 0x80;
                   7262:        }
                   7263: }
                   7264: 
                   7265: inline void pcbios_int_14h_01h()
                   7266: {
1.1.1.29  root     7267:        if(REG16(DX) < 4) {
1.1.1.25  root     7268:                UINT8 selector = sio_read(REG16(DX), 3);
                   7269:                sio_write(REG16(DX), 3, selector & ~0x80);
                   7270:                sio_write(REG16(DX), 0, REG8(AL));
                   7271:                sio_write(REG16(DX), 3, selector);
                   7272:                REG8(AH) = sio_read(REG16(DX), 5);
                   7273:        } else {
                   7274:                REG8(AH) = 0x80;
                   7275:        }
                   7276: }
                   7277: 
                   7278: inline void pcbios_int_14h_02h()
                   7279: {
1.1.1.29  root     7280:        if(REG16(DX) < 4) {
1.1.1.25  root     7281:                UINT8 selector = sio_read(REG16(DX), 3);
                   7282:                sio_write(REG16(DX), 3, selector & ~0x80);
                   7283:                REG8(AL) = sio_read(REG16(DX), 0);
                   7284:                sio_write(REG16(DX), 3, selector);
                   7285:                REG8(AH) = sio_read(REG16(DX), 5);
                   7286:        } else {
                   7287:                REG8(AH) = 0x80;
                   7288:        }
                   7289: }
                   7290: 
                   7291: inline void pcbios_int_14h_03h()
                   7292: {
1.1.1.29  root     7293:        if(REG16(DX) < 4) {
1.1.1.25  root     7294:                REG8(AH) = sio_read(REG16(DX), 5);
                   7295:                REG8(AL) = sio_read(REG16(DX), 6);
                   7296:        } else {
                   7297:                REG8(AH) = 0x80;
                   7298:        }
                   7299: }
                   7300: 
                   7301: inline void pcbios_int_14h_04h()
                   7302: {
1.1.1.29  root     7303:        if(REG16(DX) < 4) {
1.1.1.25  root     7304:                UINT8 selector = sio_read(REG16(DX), 3);
                   7305:                if(REG8(CH) <= 0x03) {
                   7306:                        selector = (selector & ~0x03) | REG8(CH);
                   7307:                }
                   7308:                if(REG8(BL) == 0x00) {
                   7309:                        selector &= ~0x04;
                   7310:                } else if(REG8(BL) == 0x01) {
                   7311:                        selector |= 0x04;
                   7312:                }
                   7313:                if(REG8(BH) == 0x00) {
                   7314:                        selector = (selector & ~0x38) | 0x00;
                   7315:                } else if(REG8(BH) == 0x01) {
                   7316:                        selector = (selector & ~0x38) | 0x08;
                   7317:                } else if(REG8(BH) == 0x02) {
                   7318:                        selector = (selector & ~0x38) | 0x18;
                   7319:                } else if(REG8(BH) == 0x03) {
                   7320:                        selector = (selector & ~0x38) | 0x28;
                   7321:                } else if(REG8(BH) == 0x04) {
                   7322:                        selector = (selector & ~0x38) | 0x38;
                   7323:                }
                   7324:                if(REG8(AL) == 0x00) {
                   7325:                        selector |= 0x40;
                   7326:                } else if(REG8(AL) == 0x01) {
                   7327:                        selector &= ~0x40;
                   7328:                }
                   7329:                if(REG8(CL) <= 0x0b) {
                   7330:                        static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200};
                   7331:                        UINT16 divisor = 115200 / rate[REG8(CL)];
                   7332:                        sio_write(REG16(DX), 3, selector | 0x80);
                   7333:                        sio_write(REG16(DX), 0, divisor & 0xff);
                   7334:                        sio_write(REG16(DX), 1, divisor >> 8);
                   7335:                }
                   7336:                sio_write(REG16(DX), 3, selector);
                   7337:                REG8(AH) = sio_read(REG16(DX), 5);
                   7338:                REG8(AL) = sio_read(REG16(DX), 6);
                   7339:        } else {
                   7340:                REG8(AH) = 0x80;
                   7341:        }
                   7342: }
                   7343: 
                   7344: inline void pcbios_int_14h_05h()
                   7345: {
1.1.1.29  root     7346:        if(REG16(DX) < 4) {
1.1.1.25  root     7347:                if(REG8(AL) == 0x00) {
                   7348:                        REG8(BL) = sio_read(REG16(DX), 4);
                   7349:                        REG8(AH) = sio_read(REG16(DX), 5);
                   7350:                        REG8(AL) = sio_read(REG16(DX), 6);
                   7351:                } else if(REG8(AL) == 0x01) {
                   7352:                        sio_write(REG16(DX), 4, REG8(BL));
                   7353:                        REG8(AH) = sio_read(REG16(DX), 5);
                   7354:                        REG8(AL) = sio_read(REG16(DX), 6);
                   7355:                } else {
                   7356:                        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));
                   7357:                }
                   7358:        } else {
                   7359:                REG8(AH) = 0x80;
                   7360:        }
                   7361: }
                   7362: 
1.1.1.14  root     7363: inline void pcbios_int_15h_10h()
                   7364: {
1.1.1.22  root     7365:        switch(REG8(AL)) {
                   7366:        case 0x00:
1.1.1.14  root     7367:                Sleep(10);
1.1.1.35  root     7368:                REQUEST_HARDWRE_UPDATE();
1.1.1.22  root     7369:                break;
                   7370:        default:
                   7371:                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     7372:                REG8(AH) = 0x86;
                   7373:                m_CF = 1;
                   7374:        }
                   7375: }
                   7376: 
1.1       root     7377: inline void pcbios_int_15h_23h()
                   7378: {
                   7379:        switch(REG8(AL)) {
1.1.1.22  root     7380:        case 0x00:
1.1.1.8   root     7381:                REG8(CL) = cmos_read(0x2d);
                   7382:                REG8(CH) = cmos_read(0x2e);
1.1       root     7383:                break;
1.1.1.22  root     7384:        case 0x01:
1.1.1.8   root     7385:                cmos_write(0x2d, REG8(CL));
                   7386:                cmos_write(0x2e, REG8(CH));
1.1       root     7387:                break;
                   7388:        default:
1.1.1.22  root     7389:                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     7390:                REG8(AH) = 0x86;
1.1.1.3   root     7391:                m_CF = 1;
1.1       root     7392:                break;
                   7393:        }
                   7394: }
                   7395: 
                   7396: inline void pcbios_int_15h_24h()
                   7397: {
                   7398:        switch(REG8(AL)) {
1.1.1.22  root     7399:        case 0x00:
1.1.1.3   root     7400:                i386_set_a20_line(0);
1.1       root     7401:                REG8(AH) = 0;
                   7402:                break;
1.1.1.22  root     7403:        case 0x01:
1.1.1.3   root     7404:                i386_set_a20_line(1);
1.1       root     7405:                REG8(AH) = 0;
                   7406:                break;
1.1.1.22  root     7407:        case 0x02:
1.1       root     7408:                REG8(AH) = 0;
1.1.1.3   root     7409:                REG8(AL) = (m_a20_mask >> 20) & 1;
1.1       root     7410:                REG16(CX) = 0;
                   7411:                break;
1.1.1.22  root     7412:        case 0x03:
1.1       root     7413:                REG16(AX) = 0;
                   7414:                REG16(BX) = 0;
                   7415:                break;
1.1.1.22  root     7416:        default:
                   7417:                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));
                   7418:                REG8(AH) = 0x86;
                   7419:                m_CF = 1;
                   7420:                break;
1.1       root     7421:        }
                   7422: }
                   7423: 
                   7424: inline void pcbios_int_15h_49h()
                   7425: {
1.1.1.27  root     7426:        REG8(AH) = 0x00;
                   7427:        REG8(BL) = 0x00; // DOS/V
1.1       root     7428: }
                   7429: 
1.1.1.22  root     7430: inline void pcbios_int_15h_50h()
                   7431: {
                   7432:        switch(REG8(AL)) {
                   7433:        case 0x00:
                   7434:        case 0x01:
                   7435:                if(REG8(BH) != 0x00 && REG8(BH) != 0x01) {
                   7436:                        REG8(AH) = 0x01; // invalid font type in bh
                   7437:                        m_CF = 1;
1.1.1.27  root     7438:                } else if(REG8(BL) != 0x00) {
1.1.1.22  root     7439:                        REG8(AH) = 0x02; // bl not zero
                   7440:                        m_CF = 1;
                   7441:                } else if(REG16(BP) != 0 && REG16(BP) != 437 && REG16(BP) != 932 && REG16(BP) != 934 && REG16(BP) != 936 && REG16(BP) != 938) {
                   7442:                        REG8(AH) = 0x04; // invalid code page
                   7443:                        m_CF = 1;
1.1.1.27  root     7444:                } else if(REG8(AL) == 0x01) {
                   7445:                        REG8(AH) = 0x06; // font is read only
1.1.1.22  root     7446:                        m_CF = 1;
1.1.1.27  root     7447:                } else {
                   7448:                        // dummy font read routine is at fffd:000d
                   7449:                        SREG(ES) = 0xfffd;
                   7450:                        i386_load_segment_descriptor(ES);
1.1.1.32  root     7451:                        REG16(BX) = 0x000d;
1.1.1.27  root     7452:                        REG8(AH) = 0x00; // success
1.1.1.22  root     7453:                }
                   7454:                break;
                   7455:        default:
                   7456:                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));
                   7457:                REG8(AH) = 0x86;
                   7458:                m_CF = 1;
                   7459:                break;
                   7460:        }
                   7461: }
                   7462: 
1.1.1.30  root     7463: inline void pcbios_int_15h_53h()
                   7464: {
                   7465:        switch(REG8(AL)) {
                   7466:        case 0x00:
                   7467:                // APM is not installed
                   7468:                REG8(AH) = 0x86;
                   7469:                m_CF = 1;
                   7470:                break;
                   7471:        default:
                   7472:                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));
                   7473:                REG8(AH) = 0x86;
                   7474:                m_CF = 1;
                   7475:                break;
                   7476:        }
                   7477: }
                   7478: 
1.1.1.35  root     7479: 
                   7480: DWORD WINAPI pcbios_int_15h_86h_thread(LPVOID)
1.1       root     7481: {
                   7482:        UINT32 usec = (REG16(CX) << 16) | REG16(DX);
1.1.1.14  root     7483:        UINT32 msec = usec / 1000;
                   7484:        
1.1.1.35  root     7485:        while(msec && !m_halted) {
1.1.1.14  root     7486:                UINT32 tmp = min(msec, 100);
                   7487:                if(msec - tmp < 10) {
                   7488:                        tmp = msec;
                   7489:                }
                   7490:                Sleep(tmp);
                   7491:                msec -= tmp;
                   7492:        }
1.1.1.35  root     7493:        
                   7494: #ifdef USE_SERVICE_THREAD
                   7495:        service_exit = true;
                   7496: #endif
                   7497:        return(0);
                   7498: }
                   7499: 
                   7500: inline void pcbios_int_15h_86h()
                   7501: {
                   7502:        if(!(REG16(CX) == 0 && REG16(DX) == 0)) {
                   7503: #ifdef USE_SERVICE_THREAD
                   7504:                start_service_loop(pcbios_int_15h_86h_thread);
                   7505: #else
                   7506:                pcbios_int_15h_86h_thread(NULL);
                   7507:                REQUEST_HARDWRE_UPDATE();
                   7508: #endif
                   7509:        }
1.1       root     7510: }
                   7511: 
                   7512: inline void pcbios_int_15h_87h()
                   7513: {
                   7514:        // copy extended memory (from DOSBox)
                   7515:        int len = REG16(CX) * 2;
1.1.1.3   root     7516:        int ofs = SREG_BASE(ES) + REG16(SI);
1.1       root     7517:        int src = (*(UINT32 *)(mem + ofs + 0x12) & 0xffffff); // + (mem[ofs + 0x16] << 24);
                   7518:        int dst = (*(UINT32 *)(mem + ofs + 0x1a) & 0xffffff); // + (mem[ofs + 0x1e] << 24);
                   7519:        memcpy(mem + dst, mem + src, len);
                   7520:        REG16(AX) = 0x00;
                   7521: }
                   7522: 
                   7523: inline void pcbios_int_15h_88h()
                   7524: {
1.1.1.17  root     7525:        REG16(AX) = ((min(MAX_MEM, 0x4000000) - 0x100000) >> 10);
1.1       root     7526: }
                   7527: 
                   7528: inline void pcbios_int_15h_89h()
                   7529: {
1.1.1.21  root     7530: #if defined(HAS_I286) || defined(HAS_I386)
1.1       root     7531:        // switch to protected mode (from DOSBox)
                   7532:        write_io_byte(0x20, 0x10);
                   7533:        write_io_byte(0x21, REG8(BH));
                   7534:        write_io_byte(0x21, 0x00);
                   7535:        write_io_byte(0xa0, 0x10);
                   7536:        write_io_byte(0xa1, REG8(BL));
                   7537:        write_io_byte(0xa1, 0x00);
1.1.1.3   root     7538:        i386_set_a20_line(1);
                   7539:        int ofs = SREG_BASE(ES) + REG16(SI);
                   7540:        m_gdtr.limit = *(UINT16 *)(mem + ofs + 0x08);
                   7541:        m_gdtr.base = *(UINT32 *)(mem + ofs + 0x08 + 0x02) & 0xffffff;
                   7542:        m_idtr.limit = *(UINT16 *)(mem + ofs + 0x10);
                   7543:        m_idtr.base = *(UINT32 *)(mem + ofs + 0x10 + 0x02) & 0xffffff;
                   7544: #if defined(HAS_I386)
                   7545:        m_cr[0] |= 1;
                   7546: #else
                   7547:        m_msw |= 1;
                   7548: #endif
                   7549:        SREG(DS) = 0x18;
                   7550:        SREG(ES) = 0x20;
                   7551:        SREG(SS) = 0x28;
                   7552:        i386_load_segment_descriptor(DS);
                   7553:        i386_load_segment_descriptor(ES);
                   7554:        i386_load_segment_descriptor(SS);
1.1.1.21  root     7555:        UINT16 offset = *(UINT16 *)(mem + SREG_BASE(SS) + REG16(SP));
1.1       root     7556:        REG16(SP) += 6;
1.1.1.3   root     7557: #if defined(HAS_I386)
1.1.1.21  root     7558:        UINT32 flags = get_flags();
                   7559:        flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
                   7560:        set_flags(flags);
1.1.1.3   root     7561: #else
1.1.1.21  root     7562:        UINT32 flags = CompressFlags();
                   7563:        flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
                   7564:        ExpandFlags(flags);
1.1.1.3   root     7565: #endif
1.1       root     7566:        REG16(AX) = 0x00;
1.1.1.21  root     7567:        i386_jmp_far(0x30, /*REG16(CX)*/offset);
1.1       root     7568: #else
1.1.1.21  root     7569:        // i86/i186/v30: protected mode is not supported
1.1       root     7570:        REG8(AH) = 0x86;
1.1.1.3   root     7571:        m_CF = 1;
1.1       root     7572: #endif
                   7573: }
                   7574: 
1.1.1.21  root     7575: inline void pcbios_int_15h_8ah()
                   7576: {
                   7577:        UINT32 size = MAX_MEM - 0x100000;
                   7578:        REG16(AX) = size & 0xffff;
                   7579:        REG16(DX) = size >> 16;
                   7580: }
                   7581: 
1.1.1.3   root     7582: #if defined(HAS_I386)
1.1       root     7583: inline void pcbios_int_15h_c9h()
                   7584: {
                   7585:        REG8(AH) = 0x00;
                   7586:        REG8(CH) = cpu_type;
                   7587:        REG8(CL) = cpu_step;
                   7588: }
1.1.1.3   root     7589: #endif
1.1       root     7590: 
                   7591: inline void pcbios_int_15h_cah()
                   7592: {
                   7593:        switch(REG8(AL)) {
1.1.1.22  root     7594:        case 0x00:
1.1       root     7595:                if(REG8(BL) > 0x3f) {
                   7596:                        REG8(AH) = 0x03;
1.1.1.3   root     7597:                        m_CF = 1;
1.1       root     7598:                } else if(REG8(BL) < 0x0e) {
                   7599:                        REG8(AH) = 0x04;
1.1.1.3   root     7600:                        m_CF = 1;
1.1       root     7601:                } else {
1.1.1.8   root     7602:                        REG8(CL) = cmos_read(REG8(BL));
1.1       root     7603:                }
                   7604:                break;
1.1.1.22  root     7605:        case 0x01:
1.1       root     7606:                if(REG8(BL) > 0x3f) {
                   7607:                        REG8(AH) = 0x03;
1.1.1.3   root     7608:                        m_CF = 1;
1.1       root     7609:                } else if(REG8(BL) < 0x0e) {
                   7610:                        REG8(AH) = 0x04;
1.1.1.3   root     7611:                        m_CF = 1;
1.1       root     7612:                } else {
1.1.1.8   root     7613:                        cmos_write(REG8(BL), REG8(CL));
1.1       root     7614:                }
                   7615:                break;
                   7616:        default:
1.1.1.22  root     7617:                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     7618:                REG8(AH) = 0x86;
1.1.1.3   root     7619:                m_CF = 1;
1.1       root     7620:                break;
                   7621:        }
                   7622: }
                   7623: 
1.1.1.22  root     7624: inline void pcbios_int_15h_e8h()
1.1.1.17  root     7625: {
1.1.1.22  root     7626:        switch(REG8(AL)) {
                   7627: #if defined(HAS_I386)
                   7628:        case 0x01:
                   7629:                REG16(AX) = REG16(CX) = ((min(MAX_MEM, 0x1000000) - 0x100000) >> 10);
                   7630:                REG16(BX) = REG16(DX) = ((max(MAX_MEM, 0x1000000) - 0x1000000) >> 16);
                   7631:                break;
1.1.1.17  root     7632: #endif
1.1.1.22  root     7633:        default:
                   7634:                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));
                   7635:                REG8(AH) = 0x86;
                   7636:                m_CF = 1;
                   7637:                break;
                   7638:        }
                   7639: }
1.1.1.17  root     7640: 
1.1.1.33  root     7641: void pcbios_update_key_code(bool wait)
1.1       root     7642: {
1.1.1.32  root     7643:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     7644: #ifdef USE_SERVICE_THREAD
                   7645:                EnterCriticalSection(&key_buf_crit_sect);
                   7646: #endif
                   7647:                bool empty = key_buf_char->empty();
                   7648: #ifdef USE_SERVICE_THREAD
                   7649:                LeaveCriticalSection(&key_buf_crit_sect);
                   7650: #endif
                   7651:                if(empty) {
1.1.1.32  root     7652:                        if(!update_key_buffer()) {
1.1.1.33  root     7653:                                if(wait) {
1.1.1.32  root     7654:                                        Sleep(10);
                   7655:                                } else {
                   7656:                                        maybe_idle();
                   7657:                                }
1.1.1.14  root     7658:                        }
                   7659:                }
1.1.1.34  root     7660:        }
                   7661:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     7662: #ifdef USE_SERVICE_THREAD
                   7663:                EnterCriticalSection(&key_buf_crit_sect);
                   7664: #endif
1.1.1.32  root     7665:                if(key_buf_char->count() != 0) {
1.1.1.35  root     7666:                        key_code  = key_buf_char->read() << 0;
                   7667:                        key_code |= key_buf_scan->read() << 8;
                   7668:                        key_recv  = 0x0000ffff;
1.1.1.32  root     7669:                }
                   7670:                if(key_buf_char->count() != 0) {
1.1.1.35  root     7671:                        key_code |= key_buf_char->read() << 16;
                   7672:                        key_code |= key_buf_scan->read() << 24;
1.1.1.33  root     7673:                        key_recv |= 0xffff0000;
1.1.1.32  root     7674:                }
1.1.1.35  root     7675: #ifdef USE_SERVICE_THREAD
                   7676:                LeaveCriticalSection(&key_buf_crit_sect);
                   7677: #endif
1.1       root     7678:        }
                   7679: }
                   7680: 
1.1.1.35  root     7681: DWORD WINAPI pcbios_int_16h_00h_thread(LPVOID)
1.1       root     7682: {
1.1.1.33  root     7683:        while(key_recv == 0 && !m_halted) {
                   7684:                pcbios_update_key_code(true);
1.1       root     7685:        }
1.1.1.33  root     7686:        if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
                   7687:                if((key_code & 0xffff) == 0x0000 || (key_code & 0xffff) == 0xe000) {
                   7688:                        if(REG8(AH) == 0x10) {
                   7689:                                key_code = ((key_code >> 8) & 0xff) | ((key_code >> 16) & 0xff00);
                   7690:                        } else {
                   7691:                                key_code = ((key_code >> 16) & 0xff00);
                   7692:                        }
                   7693:                        key_recv >>= 16;
1.1       root     7694:                }
                   7695:        }
                   7696:        REG16(AX) = key_code & 0xffff;
                   7697:        key_code >>= 16;
1.1.1.33  root     7698:        key_recv >>= 16;
1.1.1.35  root     7699:        
                   7700: #ifdef USE_SERVICE_THREAD
                   7701:        service_exit = true;
                   7702: #endif
                   7703:        return(0);
                   7704: }
                   7705: 
                   7706: inline void pcbios_int_16h_00h()
                   7707: {
                   7708: #ifdef USE_SERVICE_THREAD
                   7709:        start_service_loop(pcbios_int_16h_00h_thread);
                   7710: #else
                   7711:        pcbios_int_16h_00h_thread(NULL);
                   7712:        REQUEST_HARDWRE_UPDATE();
                   7713: #endif
1.1       root     7714: }
                   7715: 
                   7716: inline void pcbios_int_16h_01h()
                   7717: {
1.1.1.33  root     7718:        if(key_recv == 0) {
                   7719:                pcbios_update_key_code(false);
1.1.1.5   root     7720:        }
1.1.1.33  root     7721:        if(key_recv != 0) {
                   7722:                UINT32 key_code_tmp = key_code;
                   7723:                if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
                   7724:                        if((key_code_tmp & 0xffff) == 0x0000 || (key_code_tmp & 0xffff) == 0xe000) {
                   7725:                                if(REG8(AH) == 0x11) {
                   7726:                                        key_code_tmp = ((key_code_tmp >> 8) & 0xff) | ((key_code_tmp >> 16) & 0xff00);
                   7727:                                } else {
                   7728:                                        key_code_tmp = ((key_code_tmp >> 16) & 0xff00);
                   7729:                                }
                   7730:                        }
1.1       root     7731:                }
1.1.1.5   root     7732:                REG16(AX) = key_code_tmp & 0xffff;
1.1.1.3   root     7733: #if defined(HAS_I386)
1.1.1.33  root     7734:                m_ZF = 0;
                   7735: #else
                   7736:                m_ZeroVal = 1;
                   7737: #endif
                   7738:        } else {
                   7739: #if defined(HAS_I386)
                   7740:                m_ZF = 1;
1.1.1.3   root     7741: #else
1.1.1.33  root     7742:                m_ZeroVal = 0;
1.1.1.3   root     7743: #endif
1.1.1.33  root     7744:        }
1.1       root     7745: }
                   7746: 
                   7747: inline void pcbios_int_16h_02h()
                   7748: {
                   7749:        REG8(AL)  = (GetAsyncKeyState(VK_INSERT ) & 0x0001) ? 0x80 : 0;
                   7750:        REG8(AL) |= (GetAsyncKeyState(VK_CAPITAL) & 0x0001) ? 0x40 : 0;
                   7751:        REG8(AL) |= (GetAsyncKeyState(VK_NUMLOCK) & 0x0001) ? 0x20 : 0;
                   7752:        REG8(AL) |= (GetAsyncKeyState(VK_SCROLL ) & 0x0001) ? 0x10 : 0;
                   7753:        REG8(AL) |= (GetAsyncKeyState(VK_MENU   ) & 0x8000) ? 0x08 : 0;
                   7754:        REG8(AL) |= (GetAsyncKeyState(VK_CONTROL) & 0x8000) ? 0x04 : 0;
                   7755:        REG8(AL) |= (GetAsyncKeyState(VK_LSHIFT ) & 0x8000) ? 0x02 : 0;
                   7756:        REG8(AL) |= (GetAsyncKeyState(VK_RSHIFT ) & 0x8000) ? 0x01 : 0;
                   7757: }
                   7758: 
                   7759: inline void pcbios_int_16h_03h()
                   7760: {
                   7761:        static UINT16 status = 0;
                   7762:        
                   7763:        switch(REG8(AL)) {
                   7764:        case 0x05:
                   7765:                status = REG16(BX);
                   7766:                break;
                   7767:        case 0x06:
                   7768:                REG16(BX) = status;
                   7769:                break;
                   7770:        default:
1.1.1.3   root     7771:                m_CF = 1;
1.1       root     7772:                break;
                   7773:        }
                   7774: }
                   7775: 
                   7776: inline void pcbios_int_16h_05h()
                   7777: {
1.1.1.32  root     7778:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.35  root     7779: #ifdef USE_SERVICE_THREAD
                   7780:                EnterCriticalSection(&key_buf_crit_sect);
                   7781: #endif
1.1.1.32  root     7782:                key_buf_char->write(REG8(CL));
                   7783:                key_buf_scan->write(REG8(CH));
1.1.1.35  root     7784: #ifdef USE_SERVICE_THREAD
                   7785:                LeaveCriticalSection(&key_buf_crit_sect);
                   7786: #endif
1.1.1.32  root     7787:        }
1.1       root     7788:        REG8(AL) = 0x00;
                   7789: }
                   7790: 
                   7791: inline void pcbios_int_16h_12h()
                   7792: {
                   7793:        pcbios_int_16h_02h();
                   7794:        
                   7795:        REG8(AH)  = 0;//(GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x80 : 0;
                   7796:        REG8(AH) |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   7797:        REG8(AH) |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   7798:        REG8(AH) |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
                   7799:        REG8(AH) |= (GetAsyncKeyState(VK_RMENU   ) & 0x8000) ? 0x08 : 0;
                   7800:        REG8(AH) |= (GetAsyncKeyState(VK_RCONTROL) & 0x8000) ? 0x04 : 0;
                   7801:        REG8(AH) |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   7802:        REG8(AH) |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   7803: }
                   7804: 
                   7805: inline void pcbios_int_16h_13h()
                   7806: {
                   7807:        static UINT16 status = 0;
                   7808:        
                   7809:        switch(REG8(AL)) {
                   7810:        case 0x00:
                   7811:                status = REG16(DX);
                   7812:                break;
                   7813:        case 0x01:
                   7814:                REG16(DX) = status;
                   7815:                break;
                   7816:        default:
1.1.1.22  root     7817:                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     7818:                m_CF = 1;
1.1       root     7819:                break;
                   7820:        }
                   7821: }
                   7822: 
                   7823: inline void pcbios_int_16h_14h()
                   7824: {
                   7825:        static UINT8 status = 0;
                   7826:        
                   7827:        switch(REG8(AL)) {
                   7828:        case 0x00:
                   7829:        case 0x01:
                   7830:                status = REG8(AL);
                   7831:                break;
                   7832:        case 0x02:
                   7833:                REG8(AL) = status;
                   7834:                break;
                   7835:        default:
1.1.1.22  root     7836:                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     7837:                m_CF = 1;
1.1       root     7838:                break;
                   7839:        }
                   7840: }
                   7841: 
1.1.1.24  root     7842: inline void pcbios_int_16h_55h()
                   7843: {
                   7844:        switch(REG8(AL)) {
                   7845:        case 0x00:
                   7846:                // keyboard tsr is not present
                   7847:                break;
                   7848:        case 0xfe:
                   7849:                // handlers for INT 08, INT 09, INT 16, INT 1B, and INT 1C are installed
                   7850:                break;
                   7851:        case 0xff:
                   7852:                break;
                   7853:        default:
                   7854:                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));
                   7855:                m_CF = 1;
                   7856:                break;
                   7857:        }
                   7858: }
                   7859: 
1.1.1.30  root     7860: inline void pcbios_int_16h_6fh()
                   7861: {
                   7862:        switch(REG8(AL)) {
                   7863:        case 0x00:
                   7864:                // HP Vectra EX-BIOS - "F16_INQUIRE" - Extended BIOS is not installed
                   7865:                break;
                   7866:        default:
                   7867:                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));
                   7868:                m_CF = 1;
                   7869:                break;
                   7870:        }
                   7871: }
                   7872: 
1.1.1.37  root     7873: UINT16 pcbios_printer_jis2sjis(UINT16 jis)
                   7874: {
                   7875:        UINT8 hi = jis >> 8;
                   7876:        UINT8 lo = jis & 0xff;
                   7877:        
                   7878:        lo = (hi & 0x01) ? lo - 0x1f : lo + 0x7d;
                   7879:        hi = (hi - 0x21) / 2 + 0x81;
                   7880:        hi = (hi >= 0xa0) ? hi + 0x40 : hi;
                   7881:        lo = (lo >= 0x7f) ? lo + 0x01 : lo;
                   7882:        
                   7883:        return((hi << 8) + lo);
                   7884: }
                   7885: 
                   7886: UINT16 pcbios_printer_sjis2jis(UINT16 sjis)
                   7887: {
                   7888:        UINT8 hi = sjis >> 8;
                   7889:        UINT8 lo = sjis & 0xff;
                   7890:        
                   7891:        if(hi == 0x80 || (hi >= 0xeb && hi <= 0xef) || (hi >= 0xf4 && hi <= 0xff)) {
                   7892:                return(0x2121);
                   7893:        }
                   7894:        if((lo >= 0x00 && lo <= 0x3f) || lo == 0x7f || (lo >= 0xfd && lo <= 0xff)) {
                   7895:                return(0x2121);
                   7896:        }
                   7897:        if(hi >= 0xf0 && hi <= 0xf3) {
                   7898:                // gaiji
                   7899:                if(lo >= 0x40 && lo <= 0x7e) {
                   7900:                        return(0x7721 + lo - 0x40 + (hi - 0xf0) * 0x200);
                   7901:                }
                   7902:                if(lo >= 0x80 && lo <= 0x9e) {
                   7903:                        return(0x7760 + lo - 0x80 + (hi - 0xf0) * 0x200);
                   7904:                }
                   7905:                if(lo >= 0x9f && lo <= 0xfc) {
                   7906:                        return(0x7821 + lo - 0x9f + (hi - 0xf0) * 0x200);
                   7907:                }
                   7908:        }
                   7909:        hi = (hi >= 0xe0) ? hi - 0x40 : hi;
                   7910:        lo = (lo >= 0x80) ? lo - 0x01 : lo;
                   7911:        hi = ((lo >= 0x9e) ? 1 : 0) + (hi - 0x81) * 2 + 0x21;
                   7912:        lo = ((lo >= 0x9e) ? lo - 0x9e : lo - 0x40) + 0x21;
                   7913:        
                   7914:        return((hi << 8) + lo);
                   7915: }
                   7916: 
1.1.1.38  root     7917: // AX�e�N�j�J�����t�@�����X�K�C�h 1989
                   7918: // �t�^10 ���{��g��PRINTER DRIVER NIOS�T�d�l
                   7919: // 6. �R���g���[���R�[�h�̉��
1.1.1.37  root     7920: 
                   7921: void pcbios_printer_out(int c, UINT8 data)
                   7922: {
                   7923:        if(pio[c].conv_mode) {
                   7924:                if(pio[c].sjis_hi != 0) {
                   7925:                        if(!pio[c].jis_mode) {
                   7926:                                printer_out(c, 0x1c);
                   7927:                                printer_out(c, 0x26);
                   7928:                                pio[c].jis_mode = true;
                   7929:                        }
                   7930:                        UINT16 jis = pcbios_printer_sjis2jis((pio[c].sjis_hi << 8) | data);
                   7931:                        printer_out(c, jis >> 8);
                   7932:                        printer_out(c, jis & 0xff);
                   7933:                        pio[c].sjis_hi = 0;
                   7934:                } else if(pio[c].esc_buf[0] == 0x1b) {
                   7935:                        printer_out(c, data);
                   7936:                        if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
                   7937:                                pio[c].esc_buf[pio[c].esc_len] = data;
                   7938:                        }
                   7939:                        pio[c].esc_len++;
                   7940:                        
                   7941:                        switch(pio[c].esc_buf[1]) {
1.1.1.38  root     7942:                        case 0x33: // 1Bh 33h XX
                   7943:                        case 0x4a: // 1Bh 4Ah XX
                   7944:                        case 0x4e: // 1Bh 4Eh XX
                   7945:                        case 0x51: // 1Bh 51h XX
                   7946:                        case 0x55: // 1Bh 55h XX
                   7947:                        case 0x6c: // 1Bh 6Ch XX
                   7948:                        case 0x71: // 1Bh 71h XX
                   7949:                        case 0x72: // 1Bh 72h XX
1.1.1.37  root     7950:                                if(pio[c].esc_len == 3) {
                   7951:                                        pio[c].esc_buf[0] = 0x00;
                   7952:                                }
                   7953:                                break;
1.1.1.38  root     7954:                        case 0x24: // 1Bh 24h XX XX
                   7955:                        case 0x5c: // 1Bh 5Ch XX XX
1.1.1.37  root     7956:                                if(pio[c].esc_len == 4) {
                   7957:                                        pio[c].esc_buf[0] = 0x00;
                   7958:                                }
                   7959:                                break;
1.1.1.38  root     7960:                        case 0x2a: // 1Bh 2Ah XX XX XX data
1.1.1.37  root     7961:                                if(pio[c].esc_len >= 3) {
                   7962:                                        switch(pio[c].esc_buf[2]) {
                   7963:                                        case 0: case 1: case 2: case 3: case 4: case 6:
                   7964:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 1) {
                   7965:                                                        pio[c].esc_buf[0] = 0x00;
                   7966:                                                }
                   7967:                                                break;
                   7968:                                        case 32: case 33: case 38: case 39: case 40:
                   7969:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 3) {
                   7970:                                                        pio[c].esc_buf[0] = 0x00;
                   7971:                                                }
                   7972:                                                break;
1.1.1.38  root     7973:                                        case 71: case 72: case 73:
                   7974:                                                if(pio[c].esc_len >= 5 && pio[c].esc_len == 5 + (pio[c].esc_buf[3] + pio[c].esc_buf[4] * 256) * 6) {
                   7975:                                                        pio[c].esc_buf[0] = 0x00;
                   7976:                                                }
                   7977:                                                break;
1.1.1.37  root     7978:                                        default:
                   7979:                                                pio[c].esc_buf[0] = 0x00;
                   7980:                                                break;
                   7981:                                        }
                   7982:                                }
                   7983:                                break;
1.1.1.38  root     7984:                        case 0x40: // 1Bh 40h
1.1.1.37  root     7985:                                if(pio[c].jis_mode) {
                   7986:                                        printer_out(c, 0x1c);
                   7987:                                        printer_out(c, 0x2e);
                   7988:                                        pio[c].jis_mode = false;
                   7989:                                }
                   7990:                                pio[c].esc_buf[0] = 0x00;
                   7991:                                break;
1.1.1.38  root     7992:                        case 0x42: // 1Bh 42h data 00h
                   7993:                        case 0x44: // 1Bh 44h data 00h
1.1.1.37  root     7994:                                if(pio[c].esc_len >= 3 && data == 0) {
                   7995:                                        pio[c].esc_buf[0] = 0x00;
                   7996:                                }
                   7997:                                break;
1.1.1.38  root     7998:                        case 0x43: // 1Bh 43h (00h) XX
1.1.1.37  root     7999:                                if(pio[c].esc_len >= 3 && data != 0) {
                   8000:                                        pio[c].esc_buf[0] = 0x00;
                   8001:                                }
                   8002:                                break;
1.1.1.38  root     8003:                        default: // 1Bh XX
1.1.1.37  root     8004:                                pio[c].esc_buf[0] = 0x00;
                   8005:                                break;
                   8006:                        }
                   8007:                } else if(pio[c].esc_buf[0] == 0x1c) {
                   8008:                        printer_out(c, data);
                   8009:                        if(pio[c].esc_len < sizeof(pio[c].esc_buf)) {
                   8010:                                pio[c].esc_buf[pio[c].esc_len] = data;
                   8011:                        }
                   8012:                        pio[c].esc_len++;
                   8013:                        
                   8014:                        switch(pio[c].esc_buf[1]) {
1.1.1.38  root     8015:                        case 0x21: // 1Ch 21h XX
                   8016:                        case 0x2d: // 1Ch 2Dh XX
                   8017:                        case 0x57: // 1Ch 57h XX
                   8018:                        case 0x6b: // 1Ch 6Bh XX
                   8019:                        case 0x72: // 1Ch 72h XX
                   8020:                        case 0x78: // 1Ch 78h XX
1.1.1.37  root     8021:                                if(pio[c].esc_len == 3) {
                   8022:                                        pio[c].esc_buf[0] = 0x00;
                   8023:                                }
                   8024:                                break;
1.1.1.38  root     8025:                        case 0x26: // 1Ch 26h
1.1.1.37  root     8026:                                pio[c].jis_mode = true;
                   8027:                                pio[c].esc_buf[0] = 0x00;
                   8028:                                break;
1.1.1.38  root     8029:                        case 0x2e: // 1Ch 2Eh
1.1.1.37  root     8030:                                pio[c].jis_mode = false;
                   8031:                                pio[c].esc_buf[0] = 0x00;
                   8032:                                break;
1.1.1.38  root     8033:                        case 0x32: // 1Ch 32h XX XX data
1.1.1.37  root     8034:                                if(pio[c].esc_len == 76) {
                   8035:                                        pio[c].esc_buf[0] = 0x00;
                   8036:                                }
                   8037:                                break;
1.1.1.38  root     8038:                        case 0x44: // 1Bh 44h data 00h
1.1.1.37  root     8039:                                if(pio[c].esc_len == 6) {
                   8040:                                        pio[c].esc_buf[0] = 0x00;
                   8041:                                }
                   8042:                                break;
1.1.1.38  root     8043:                        case 0x53: // 1Ch 53h XX XX
                   8044:                        case 0x54: // 1Ch 54h XX XX
1.1.1.37  root     8045:                                if(pio[c].esc_len == 4) {
                   8046:                                        pio[c].esc_buf[0] = 0x00;
                   8047:                                }
                   8048:                                break;
1.1.1.38  root     8049:                        default: // 1Ch XX
1.1.1.37  root     8050:                                pio[c].esc_buf[0] = 0x00;
                   8051:                                break;
                   8052:                        }
                   8053:                } else if(data == 0x1b || data == 0x1c) {
                   8054:                        printer_out(c, data);
                   8055:                        pio[c].esc_buf[0] = data;
                   8056:                        pio[c].esc_len = 1;
                   8057:                } else if((data >= 0x80 && data <= 0x9f) || (data >= 0xe0 && data <= 0xff)) {
                   8058:                        pio[c].sjis_hi = data;
                   8059:                } else {
                   8060:                        if(pio[c].jis_mode) {
                   8061:                                printer_out(c, 0x1c);
                   8062:                                printer_out(c, 0x2e);
                   8063:                                pio[c].jis_mode = false;
                   8064:                        }
                   8065:                        printer_out(c, data);
                   8066:                }
                   8067:        } else {
                   8068:                if(pio[c].jis_mode) {
                   8069:                        printer_out(c, 0x1c);
                   8070:                        printer_out(c, 0x2e);
                   8071:                        pio[c].jis_mode = false;
                   8072:                }
                   8073:                printer_out(c, data);
                   8074:        }
                   8075: }
                   8076: 
                   8077: inline void pcbios_int_17h_00h()
                   8078: {
                   8079:        if(REG16(DX) < 3) {
                   8080:                pcbios_printer_out(REG16(DX), REG8(AL));
                   8081:                REG8(AH) = 0xd0;
                   8082:        }
                   8083: }
                   8084: 
                   8085: inline void pcbios_int_17h_01h()
                   8086: {
                   8087:        if(REG16(DX) < 3) {
                   8088:                REG8(AH) = 0xd0;
                   8089:        }
                   8090: }
                   8091: 
                   8092: inline void pcbios_int_17h_02h()
                   8093: {
                   8094:        if(REG16(DX) < 3) {
                   8095:                REG8(AH) = 0xd0;
                   8096:        }
                   8097: }
                   8098: 
                   8099: inline void pcbios_int_17h_03h()
                   8100: {
                   8101:        switch(REG8(AL)) {
                   8102:        case 0x00:
                   8103:                if(REG16(DX) < 3) {
                   8104:                        if(pio[REG16(DX)].jis_mode) {
                   8105:                                printer_out(REG16(DX), 0x1c);
                   8106:                                printer_out(REG16(DX), 0x2e);
                   8107:                                pio[REG16(DX)].jis_mode = false;
                   8108:                        }
                   8109:                        for(UINT16 i = 0; i < REG16(CX); i++) {
                   8110:                                printer_out(REG16(DX), mem[SREG_BASE(ES) + REG16(BX) + i]);
                   8111:                        }
                   8112:                        REG16(CX) = 0x0000;
                   8113:                        REG8(AH) = 0xd0;
                   8114:                }
                   8115:                break;
                   8116:        default:
                   8117:                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));
                   8118:                break;
                   8119:        }
                   8120: }
                   8121: 
                   8122: inline void pcbios_int_17h_50h()
                   8123: {
                   8124:        switch(REG8(AL)) {
                   8125:        case 0x00:
                   8126:                if(REG16(DX) < 3) {
                   8127:                        if(REG16(BX) = 0x0001) {
                   8128:                                pio[REG16(DX)].conv_mode = false;
                   8129:                                REG8(AL) = 0x00;
                   8130:                        } else if(REG16(BX) = 0x0051) {
                   8131:                                pio[REG16(DX)].conv_mode = true;
                   8132:                                REG8(AL) = 0x00;
                   8133:                        } else {
                   8134:                                REG8(AL) = 0x01;
                   8135:                        }
                   8136:                } else {
                   8137:                        REG8(AL) = 0x02;
                   8138:                }
                   8139:                break;
                   8140:        case 0x01:
                   8141:                if(REG16(DX) < 3) {
                   8142:                        if(pio[REG16(DX)].conv_mode) {
                   8143:                                REG16(BX) = 0x0051;
                   8144:                        } else {
                   8145:                                REG16(BX) = 0x0001;
                   8146:                        }
                   8147:                        REG8(AL) = 0x00;
                   8148:                } else {
                   8149:                        REG8(AL) = 0x02;
                   8150:                }
                   8151:                break;
                   8152:        default:
                   8153:                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));
                   8154:                break;
                   8155:        }
                   8156: }
                   8157: 
                   8158: inline void pcbios_int_17h_51h()
                   8159: {
                   8160:        if(REG8(DH) >= 0x21 && REG8(DH) <= 0x7e && REG8(DL) >= 0x21 && REG8(DL) <= 0x7e) {
                   8161:                REG16(DX) = pcbios_printer_jis2sjis(REG16(DX));
                   8162:        } else {
                   8163:                REG16(DX) = 0x0000;
                   8164:        }
                   8165: }
                   8166: 
                   8167: inline void pcbios_int_17h_52h()
                   8168: {
                   8169:        if(((REG8(DH) >= 0x81 && REG8(DH) <= 0x9f) || (REG8(DH) >= 0xe0 && REG8(DH) <= 0xfc)) && (REG8(DL) >= 0x40 && REG8(DL) <= 0xfc && REG8(DL) != 0x7f)) {
                   8170:                REG16(DX) = pcbios_printer_sjis2jis(REG16(DX));
                   8171:        } else {
                   8172:                REG16(DX) = 0x0000;
                   8173:        }
                   8174: }
                   8175: 
                   8176: inline void pcbios_int_17h_84h()
                   8177: {
                   8178:        if(REG16(DX) < 3) {
                   8179:                if(pio[REG16(DX)].jis_mode) {
                   8180:                        printer_out(REG16(DX), 0x1c);
                   8181:                        printer_out(REG16(DX), 0x2e);
                   8182:                        pio[REG16(DX)].jis_mode = false;
                   8183:                }
                   8184:                printer_out(REG16(DX), REG8(AL));
                   8185:                REG8(AH) = 0xd0;
                   8186:        }
                   8187: }
                   8188: 
                   8189: inline void pcbios_int_17h_85h()
                   8190: {
                   8191:        pio[0].conv_mode = (REG8(AL) == 0x00);
                   8192: }
                   8193: 
1.1       root     8194: inline void pcbios_int_1ah_00h()
                   8195: {
1.1.1.19  root     8196:        pcbios_update_daily_timer_counter(timeGetTime());
                   8197:        REG16(CX) = *(UINT16 *)(mem + 0x46e);
                   8198:        REG16(DX) = *(UINT16 *)(mem + 0x46c);
                   8199:        REG8(AL) = mem[0x470];
                   8200:        mem[0x470] = 0;
1.1       root     8201: }
                   8202: 
                   8203: inline int to_bcd(int t)
                   8204: {
                   8205:        int u = (t % 100) / 10;
                   8206:        return (u << 4) | (t % 10);
                   8207: }
                   8208: 
                   8209: inline void pcbios_int_1ah_02h()
                   8210: {
                   8211:        SYSTEMTIME time;
                   8212:        
                   8213:        GetLocalTime(&time);
                   8214:        REG8(CH) = to_bcd(time.wHour);
                   8215:        REG8(CL) = to_bcd(time.wMinute);
                   8216:        REG8(DH) = to_bcd(time.wSecond);
                   8217:        REG8(DL) = 0x00;
                   8218: }
                   8219: 
                   8220: inline void pcbios_int_1ah_04h()
                   8221: {
                   8222:        SYSTEMTIME time;
                   8223:        
                   8224:        GetLocalTime(&time);
                   8225:        REG8(CH) = to_bcd(time.wYear / 100);
                   8226:        REG8(CL) = to_bcd(time.wYear);
                   8227:        REG8(DH) = to_bcd(time.wMonth);
                   8228:        REG8(DL) = to_bcd(time.wDay);
                   8229: }
                   8230: 
                   8231: inline void pcbios_int_1ah_0ah()
                   8232: {
                   8233:        SYSTEMTIME time;
                   8234:        FILETIME file_time;
                   8235:        WORD dos_date, dos_time;
                   8236:        
                   8237:        GetLocalTime(&time);
                   8238:        SystemTimeToFileTime(&time, &file_time);
                   8239:        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   8240:        REG16(CX) = dos_date;
                   8241: }
                   8242: 
                   8243: // msdos system call
                   8244: 
                   8245: inline void msdos_int_21h_00h()
                   8246: {
1.1.1.3   root     8247:        msdos_process_terminate(SREG(CS), retval, 1);
1.1       root     8248: }
                   8249: 
1.1.1.35  root     8250: DWORD WINAPI msdos_int_21h_01h_thread(LPVOID)
1.1       root     8251: {
                   8252:        REG8(AL) = msdos_getche();
1.1.1.33  root     8253:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     8254:        
1.1.1.35  root     8255: #ifdef USE_SERVICE_THREAD
                   8256:        service_exit = true;
                   8257: #endif
                   8258:        return(0);
                   8259: }
                   8260: 
                   8261: inline void msdos_int_21h_01h()
                   8262: {
                   8263: #ifdef USE_SERVICE_THREAD
                   8264:        start_service_loop(msdos_int_21h_01h_thread);
                   8265: #else
                   8266:        msdos_int_21h_01h_thread(NULL);
                   8267:        REQUEST_HARDWRE_UPDATE();
                   8268: #endif
1.1       root     8269: }
                   8270: 
                   8271: inline void msdos_int_21h_02h()
                   8272: {
1.1.1.33  root     8273:        UINT8 data = REG8(DL);
                   8274:        msdos_putch(data);
                   8275:        REG8(AL) = data;
                   8276:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     8277: }
                   8278: 
                   8279: inline void msdos_int_21h_03h()
                   8280: {
                   8281:        REG8(AL) = msdos_aux_in();
                   8282: }
                   8283: 
                   8284: inline void msdos_int_21h_04h()
                   8285: {
                   8286:        msdos_aux_out(REG8(DL));
                   8287: }
                   8288: 
                   8289: inline void msdos_int_21h_05h()
                   8290: {
                   8291:        msdos_prn_out(REG8(DL));
                   8292: }
                   8293: 
                   8294: inline void msdos_int_21h_06h()
                   8295: {
                   8296:        if(REG8(DL) == 0xff) {
                   8297:                if(msdos_kbhit()) {
                   8298:                        REG8(AL) = msdos_getch();
1.1.1.3   root     8299: #if defined(HAS_I386)
                   8300:                        m_ZF = 0;
                   8301: #else
                   8302:                        m_ZeroVal = 1;
                   8303: #endif
1.1       root     8304:                } else {
                   8305:                        REG8(AL) = 0;
1.1.1.3   root     8306: #if defined(HAS_I386)
                   8307:                        m_ZF = 1;
                   8308: #else
                   8309:                        m_ZeroVal = 0;
                   8310: #endif
1.1.1.14  root     8311:                        maybe_idle();
1.1       root     8312:                }
                   8313:        } else {
1.1.1.33  root     8314:                UINT8 data = REG8(DL);
                   8315:                msdos_putch(data);
                   8316:                REG8(AL) = data;
1.1       root     8317:        }
                   8318: }
                   8319: 
1.1.1.35  root     8320: DWORD WINAPI msdos_int_21h_07h_thread(LPVOID)
1.1       root     8321: {
                   8322:        REG8(AL) = msdos_getch();
1.1.1.26  root     8323:        
1.1.1.35  root     8324: #ifdef USE_SERVICE_THREAD
                   8325:        service_exit = true;
                   8326: #endif
                   8327:        return(0);
1.1       root     8328: }
                   8329: 
1.1.1.35  root     8330: inline void msdos_int_21h_07h()
                   8331: {
                   8332: #ifdef USE_SERVICE_THREAD
                   8333:        start_service_loop(msdos_int_21h_07h_thread);
                   8334: #else
                   8335:        msdos_int_21h_07h_thread(NULL);
                   8336:        REQUEST_HARDWRE_UPDATE();
                   8337: #endif
                   8338: }
                   8339: 
                   8340: DWORD WINAPI msdos_int_21h_08h_thread(LPVOID)
1.1       root     8341: {
                   8342:        REG8(AL) = msdos_getch();
1.1.1.33  root     8343:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     8344:        
1.1.1.35  root     8345: #ifdef USE_SERVICE_THREAD
                   8346:        service_exit = true;
                   8347: #endif
                   8348:        return(0);
                   8349: }
                   8350: 
                   8351: inline void msdos_int_21h_08h()
                   8352: {
                   8353: #ifdef USE_SERVICE_THREAD
                   8354:        start_service_loop(msdos_int_21h_08h_thread);
                   8355: #else
                   8356:        msdos_int_21h_08h_thread(NULL);
                   8357:        REQUEST_HARDWRE_UPDATE();
                   8358: #endif
1.1       root     8359: }
                   8360: 
                   8361: inline void msdos_int_21h_09h()
                   8362: {
1.1.1.21  root     8363:        msdos_stdio_reopen();
                   8364:        
1.1.1.20  root     8365:        process_t *process = msdos_process_info_get(current_psp);
                   8366:        int fd = msdos_psp_get_file_table(1, current_psp);
                   8367:        
1.1.1.14  root     8368:        char *str = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   8369:        int len = 0;
1.1       root     8370:        
1.1.1.14  root     8371:        while(str[len] != '$' && len < 0x10000) {
                   8372:                len++;
                   8373:        }
1.1.1.20  root     8374:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     8375:                // stdout is redirected to file
1.1.1.20  root     8376:                msdos_write(fd, str, len);
1.1       root     8377:        } else {
                   8378:                for(int i = 0; i < len; i++) {
1.1.1.14  root     8379:                        msdos_putch(str[i]);
1.1       root     8380:                }
                   8381:        }
1.1.1.33  root     8382:        REG8(AL) = '$';
                   8383:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     8384: }
                   8385: 
1.1.1.35  root     8386: DWORD WINAPI msdos_int_21h_0ah_thread(LPVOID)
1.1       root     8387: {
1.1.1.3   root     8388:        int ofs = SREG_BASE(DS) + REG16(DX);
1.1       root     8389:        int max = mem[ofs] - 1;
                   8390:        UINT8 *buf = mem + ofs + 2;
                   8391:        int chr, p = 0;
                   8392:        
                   8393:        while((chr = msdos_getch()) != 0x0d) {
1.1.1.33  root     8394:                if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
1.1.1.26  root     8395:                        p = 0;
1.1.1.33  root     8396:                        msdos_putch(0x03);
                   8397:                        msdos_putch(0x0d);
                   8398:                        msdos_putch(0x0a);
1.1.1.26  root     8399:                        break;
1.1.1.33  root     8400:                } else if(ctrl_break_pressed) {
                   8401:                        // skip this byte
1.1.1.26  root     8402:                } else if(chr == 0x00) {
1.1       root     8403:                        // skip 2nd byte
                   8404:                        msdos_getch();
                   8405:                } else if(chr == 0x08) {
                   8406:                        // back space
                   8407:                        if(p > 0) {
                   8408:                                p--;
1.1.1.20  root     8409:                                if(msdos_ctrl_code_check(buf[p])) {
1.1.1.34  root     8410:                                        msdos_putch(0x08);
                   8411:                                        msdos_putch(0x08);
                   8412:                                        msdos_putch(0x20);
                   8413:                                        msdos_putch(0x20);
                   8414:                                        msdos_putch(0x08);
                   8415:                                        msdos_putch(0x08);
1.1.1.36  root     8416:                                } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
                   8417:                                        p--;
                   8418:                                        msdos_putch(0x08);
                   8419:                                        msdos_putch(0x08);
                   8420:                                        msdos_putch(0x20);
                   8421:                                        msdos_putch(0x20);
                   8422:                                        msdos_putch(0x08);
                   8423:                                        msdos_putch(0x08);
1.1.1.34  root     8424:                                } else {
                   8425:                                        msdos_putch(0x08);
                   8426:                                        msdos_putch(0x20);
                   8427:                                        msdos_putch(0x08);
                   8428:                                }
                   8429:                        }
                   8430:                } else if(chr == 0x1b) {
                   8431:                        // escape
                   8432:                        while(p > 0) {
                   8433:                                p--;
                   8434:                                if(msdos_ctrl_code_check(buf[p])) {
                   8435:                                        msdos_putch(0x08);
                   8436:                                        msdos_putch(0x08);
                   8437:                                        msdos_putch(0x20);
                   8438:                                        msdos_putch(0x20);
                   8439:                                        msdos_putch(0x08);
                   8440:                                        msdos_putch(0x08);
1.1.1.20  root     8441:                                } else {
1.1.1.34  root     8442:                                        msdos_putch(0x08);
                   8443:                                        msdos_putch(0x20);
                   8444:                                        msdos_putch(0x08);
1.1.1.20  root     8445:                                }
1.1       root     8446:                        }
                   8447:                } else if(p < max) {
                   8448:                        buf[p++] = chr;
                   8449:                        msdos_putch(chr);
                   8450:                }
                   8451:        }
                   8452:        buf[p] = 0x0d;
                   8453:        mem[ofs + 1] = p;
1.1.1.33  root     8454:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     8455:        
1.1.1.35  root     8456: #ifdef USE_SERVICE_THREAD
                   8457:        service_exit = true;
                   8458: #endif
                   8459:        return(0);
                   8460: }
                   8461: 
                   8462: inline void msdos_int_21h_0ah()
                   8463: {
                   8464:        if(mem[SREG_BASE(DS) + REG16(DX)] != 0x00) {
                   8465: #ifdef USE_SERVICE_THREAD
                   8466:                start_service_loop(msdos_int_21h_0ah_thread);
                   8467: #else
                   8468:                msdos_int_21h_0ah_thread(NULL);
                   8469:                REQUEST_HARDWRE_UPDATE();
                   8470: #endif
                   8471:        }
1.1       root     8472: }
                   8473: 
                   8474: inline void msdos_int_21h_0bh()
                   8475: {
                   8476:        if(msdos_kbhit()) {
                   8477:                REG8(AL) = 0xff;
                   8478:        } else {
                   8479:                REG8(AL) = 0x00;
1.1.1.14  root     8480:                maybe_idle();
1.1       root     8481:        }
1.1.1.33  root     8482:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     8483: }
                   8484: 
                   8485: inline void msdos_int_21h_0ch()
                   8486: {
                   8487:        // clear key buffer
1.1.1.21  root     8488:        msdos_stdio_reopen();
                   8489:        
1.1.1.20  root     8490:        process_t *process = msdos_process_info_get(current_psp);
                   8491:        int fd = msdos_psp_get_file_table(0, current_psp);
                   8492:        
                   8493:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     8494:                // stdin is redirected to file
                   8495:        } else {
                   8496:                while(msdos_kbhit()) {
                   8497:                        msdos_getch();
                   8498:                }
                   8499:        }
                   8500:        
                   8501:        switch(REG8(AL)) {
                   8502:        case 0x01:
                   8503:                msdos_int_21h_01h();
                   8504:                break;
                   8505:        case 0x06:
                   8506:                msdos_int_21h_06h();
                   8507:                break;
                   8508:        case 0x07:
                   8509:                msdos_int_21h_07h();
                   8510:                break;
                   8511:        case 0x08:
                   8512:                msdos_int_21h_08h();
                   8513:                break;
                   8514:        case 0x0a:
                   8515:                msdos_int_21h_0ah();
                   8516:                break;
                   8517:        default:
1.1.1.22  root     8518: //             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));
                   8519: //             REG16(AX) = 0x01;
                   8520: //             m_CF = 1;
1.1       root     8521:                break;
                   8522:        }
                   8523: }
                   8524: 
                   8525: inline void msdos_int_21h_0dh()
                   8526: {
                   8527: }
                   8528: 
                   8529: inline void msdos_int_21h_0eh()
                   8530: {
                   8531:        if(REG8(DL) < 26) {
                   8532:                _chdrive(REG8(DL) + 1);
                   8533:                msdos_cds_update(REG8(DL));
1.1.1.23  root     8534:                msdos_sda_update(current_psp);
1.1       root     8535:        }
                   8536:        REG8(AL) = 26; // zdrive
                   8537: }
                   8538: 
1.1.1.14  root     8539: inline void msdos_int_21h_0fh()
                   8540: {
                   8541:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8542:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   8543:        char *path = msdos_fcb_path(fcb);
                   8544:        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     8545:        
1.1.1.14  root     8546:        if(hFile == INVALID_HANDLE_VALUE) {
                   8547:                REG8(AL) = 0xff;
                   8548:        } else {
                   8549:                REG8(AL) = 0;
                   8550:                fcb->current_block = 0;
                   8551:                fcb->record_size = 128;
                   8552:                fcb->file_size = GetFileSize(hFile, NULL);
                   8553:                fcb->handle = hFile;
                   8554:                fcb->cur_record = 0;
                   8555:        }
                   8556: }
                   8557: 
                   8558: inline void msdos_int_21h_10h()
                   8559: {
                   8560:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8561:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   8562:        
                   8563:        REG8(AL) = CloseHandle(fcb->handle) ? 0 : 0xff;
                   8564: }
                   8565: 
1.1       root     8566: inline void msdos_int_21h_11h()
                   8567: {
1.1.1.3   root     8568:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8569:        fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     8570:        
                   8571:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     8572:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   8573:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   8574:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     8575:        char *path = msdos_fcb_path(fcb);
                   8576:        WIN32_FIND_DATA fd;
                   8577:        
1.1.1.13  root     8578:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   8579:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   8580:                FindClose(dtainfo->find_handle);
                   8581:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     8582:        }
                   8583:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     8584:        dtainfo->allowable_mask = (ext_fcb->flag == 0xff) ? ext_fcb->attribute : 0x20;
                   8585:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     8586:        
1.1.1.14  root     8587:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   8588:                dtainfo->allowable_mask &= ~8;
1.1       root     8589:        }
1.1.1.14  root     8590:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   8591:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     8592:                      !msdos_find_file_has_8dot3name(&fd)) {
                   8593:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   8594:                                FindClose(dtainfo->find_handle);
                   8595:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     8596:                                break;
                   8597:                        }
                   8598:                }
                   8599:        }
1.1.1.13  root     8600:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     8601:                if(ext_fcb->flag == 0xff) {
                   8602:                        ext_find->flag = 0xff;
                   8603:                        memset(ext_find->reserved, 0, 5);
                   8604:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   8605:                }
                   8606:                find->drive = _getdrive();
1.1.1.13  root     8607:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     8608:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   8609:                find->nt_res = 0;
                   8610:                msdos_find_file_conv_local_time(&fd);
                   8611:                find->create_time_ms = 0;
                   8612:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   8613:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   8614:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   8615:                find->cluster_hi = find->cluster_lo = 0;
                   8616:                find->file_size = fd.nFileSizeLow;
                   8617:                REG8(AL) = 0x00;
1.1.1.14  root     8618:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     8619:                if(ext_fcb->flag == 0xff) {
                   8620:                        ext_find->flag = 0xff;
                   8621:                        memset(ext_find->reserved, 0, 5);
                   8622:                        ext_find->attribute = 8;
                   8623:                }
                   8624:                find->drive = _getdrive();
                   8625:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   8626:                find->attribute = 8;
                   8627:                find->nt_res = 0;
                   8628:                msdos_find_file_conv_local_time(&fd);
                   8629:                find->create_time_ms = 0;
                   8630:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   8631:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   8632:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   8633:                find->cluster_hi = find->cluster_lo = 0;
                   8634:                find->file_size = 0;
1.1.1.14  root     8635:                dtainfo->allowable_mask &= ~8;
1.1       root     8636:                REG8(AL) = 0x00;
                   8637:        } else {
                   8638:                REG8(AL) = 0xff;
                   8639:        }
                   8640: }
                   8641: 
                   8642: inline void msdos_int_21h_12h()
                   8643: {
1.1.1.3   root     8644:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1.1.14  root     8645: //     fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     8646:        
                   8647:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     8648:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   8649:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   8650:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     8651:        WIN32_FIND_DATA fd;
                   8652:        
1.1.1.13  root     8653:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   8654:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   8655:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     8656:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     8657:                              !msdos_find_file_has_8dot3name(&fd)) {
                   8658:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   8659:                                        FindClose(dtainfo->find_handle);
                   8660:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     8661:                                        break;
                   8662:                                }
                   8663:                        }
                   8664:                } else {
1.1.1.13  root     8665:                        FindClose(dtainfo->find_handle);
                   8666:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     8667:                }
                   8668:        }
1.1.1.13  root     8669:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     8670:                if(ext_fcb->flag == 0xff) {
                   8671:                        ext_find->flag = 0xff;
                   8672:                        memset(ext_find->reserved, 0, 5);
                   8673:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   8674:                }
                   8675:                find->drive = _getdrive();
1.1.1.13  root     8676:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     8677:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   8678:                find->nt_res = 0;
                   8679:                msdos_find_file_conv_local_time(&fd);
                   8680:                find->create_time_ms = 0;
                   8681:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   8682:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   8683:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   8684:                find->cluster_hi = find->cluster_lo = 0;
                   8685:                find->file_size = fd.nFileSizeLow;
                   8686:                REG8(AL) = 0x00;
1.1.1.14  root     8687:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     8688:                if(ext_fcb->flag == 0xff) {
                   8689:                        ext_find->flag = 0xff;
                   8690:                        memset(ext_find->reserved, 0, 5);
                   8691:                        ext_find->attribute = 8;
                   8692:                }
                   8693:                find->drive = _getdrive();
                   8694:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   8695:                find->attribute = 8;
                   8696:                find->nt_res = 0;
                   8697:                msdos_find_file_conv_local_time(&fd);
                   8698:                find->create_time_ms = 0;
                   8699:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   8700:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   8701:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   8702:                find->cluster_hi = find->cluster_lo = 0;
                   8703:                find->file_size = 0;
1.1.1.14  root     8704:                dtainfo->allowable_mask &= ~8;
1.1       root     8705:                REG8(AL) = 0x00;
                   8706:        } else {
                   8707:                REG8(AL) = 0xff;
                   8708:        }
                   8709: }
                   8710: 
                   8711: inline void msdos_int_21h_13h()
                   8712: {
1.1.1.3   root     8713:        if(remove(msdos_fcb_path((fcb_t *)(mem + SREG_BASE(DS) + REG16(DX))))) {
1.1       root     8714:                REG8(AL) = 0xff;
                   8715:        } else {
                   8716:                REG8(AL) = 0x00;
                   8717:        }
                   8718: }
                   8719: 
1.1.1.16  root     8720: inline void msdos_int_21h_14h()
                   8721: {
                   8722:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8723:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   8724:        process_t *process = msdos_process_info_get(current_psp);
                   8725:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   8726:        DWORD num = 0;
                   8727:        
                   8728:        memset(mem + dta_laddr, 0, fcb->record_size);
                   8729:        if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   8730:                REG8(AL) = 1;
                   8731:        } else {
                   8732:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   8733:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   8734:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   8735:                REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   8736:        }
                   8737: }
                   8738: 
                   8739: inline void msdos_int_21h_15h()
1.1.1.14  root     8740: {
                   8741:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8742:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.16  root     8743:        process_t *process = msdos_process_info_get(current_psp);
                   8744:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   8745:        DWORD num = 0;
1.1.1.14  root     8746:        
1.1.1.16  root     8747:        if(!WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   8748:                REG8(AL) = 1;
                   8749:        } else {
                   8750:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   8751:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   8752:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   8753:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   8754:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   8755:        }
                   8756: }
                   8757: 
                   8758: inline void msdos_int_21h_16h()
                   8759: {
                   8760:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8761:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.14  root     8762:        char *path = msdos_fcb_path(fcb);
                   8763:        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     8764:        
1.1.1.14  root     8765:        if(hFile == INVALID_HANDLE_VALUE) {
                   8766:                REG8(AL) = 0xff;
                   8767:        } else {
                   8768:                REG8(AL) = 0;
                   8769:                fcb->current_block = 0;
                   8770:                fcb->record_size = 128;
                   8771:                fcb->file_size = 0;
                   8772:                fcb->handle = hFile;
                   8773:                fcb->cur_record = 0;
                   8774:        }
                   8775: }
                   8776: 
1.1.1.16  root     8777: inline void msdos_int_21h_17h()
                   8778: {
                   8779:        ext_fcb_t *ext_fcb_src = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8780:        fcb_t *fcb_src = (fcb_t *)(ext_fcb_src + (ext_fcb_src->flag == 0xff ? 1 : 0));
                   8781:        char *path_src = msdos_fcb_path(fcb_src);
                   8782:        ext_fcb_t *ext_fcb_dst = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + 16);
                   8783:        fcb_t *fcb_dst = (fcb_t *)(ext_fcb_dst + (ext_fcb_dst->flag == 0xff ? 1 : 0));
                   8784:        char *path_dst = msdos_fcb_path(fcb_dst);
                   8785:        
                   8786:        if(rename(path_src, path_dst)) {
                   8787:                REG8(AL) = 0xff;
                   8788:        } else {
                   8789:                REG8(AL) = 0;
                   8790:        }
                   8791: }
                   8792: 
1.1       root     8793: inline void msdos_int_21h_18h()
                   8794: {
                   8795:        REG8(AL) = 0x00;
                   8796: }
                   8797: 
                   8798: inline void msdos_int_21h_19h()
                   8799: {
                   8800:        REG8(AL) = _getdrive() - 1;
                   8801: }
                   8802: 
                   8803: inline void msdos_int_21h_1ah()
                   8804: {
                   8805:        process_t *process = msdos_process_info_get(current_psp);
                   8806:        
                   8807:        process->dta.w.l = REG16(DX);
1.1.1.3   root     8808:        process->dta.w.h = SREG(DS);
1.1.1.23  root     8809:        msdos_sda_update(current_psp);
1.1       root     8810: }
                   8811: 
                   8812: inline void msdos_int_21h_1bh()
                   8813: {
                   8814:        int drive_num = _getdrive() - 1;
                   8815:        UINT16 seg, ofs;
                   8816:        
                   8817:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   8818:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   8819:                REG8(AL) = dpb->highest_sector_num + 1;
                   8820:                REG16(CX) = dpb->bytes_per_sector;
                   8821:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     8822:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     8823:        } else {
                   8824:                REG8(AL) = 0xff;
1.1.1.3   root     8825:                m_CF = 1;
1.1       root     8826:        }
                   8827: 
                   8828: }
                   8829: 
                   8830: inline void msdos_int_21h_1ch()
                   8831: {
                   8832:        int drive_num = REG8(DL) ? (REG8(DL) - 1) : (_getdrive() - 1);
                   8833:        UINT16 seg, ofs;
                   8834:        
                   8835:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   8836:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   8837:                REG8(AL) = dpb->highest_sector_num + 1;
                   8838:                REG16(CX) = dpb->bytes_per_sector;
                   8839:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     8840:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     8841:        } else {
                   8842:                REG8(AL) = 0xff;
1.1.1.3   root     8843:                m_CF = 1;
1.1       root     8844:        }
                   8845: 
                   8846: }
                   8847: 
                   8848: inline void msdos_int_21h_1dh()
                   8849: {
                   8850:        REG8(AL) = 0;
                   8851: }
                   8852: 
                   8853: inline void msdos_int_21h_1eh()
                   8854: {
                   8855:        REG8(AL) = 0;
                   8856: }
                   8857: 
                   8858: inline void msdos_int_21h_1fh()
                   8859: {
                   8860:        int drive_num = _getdrive() - 1;
                   8861:        UINT16 seg, ofs;
                   8862:        
                   8863:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   8864:                REG8(AL) = 0;
1.1.1.3   root     8865:                SREG(DS) = seg;
                   8866:                i386_load_segment_descriptor(DS);
1.1       root     8867:                REG16(BX) = ofs;
                   8868:        } else {
                   8869:                REG8(AL) = 0xff;
1.1.1.3   root     8870:                m_CF = 1;
1.1       root     8871:        }
                   8872: }
                   8873: 
                   8874: inline void msdos_int_21h_20h()
                   8875: {
                   8876:        REG8(AL) = 0;
                   8877: }
                   8878: 
1.1.1.14  root     8879: inline void msdos_int_21h_21h()
                   8880: {
                   8881:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8882:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   8883:        
                   8884:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   8885:                REG8(AL) = 1;
                   8886:        } else {
                   8887:                process_t *process = msdos_process_info_get(current_psp);
                   8888:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   8889:                memset(mem + dta_laddr, 0, fcb->record_size);
1.1.1.16  root     8890:                DWORD num = 0;
1.1.1.14  root     8891:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   8892:                        REG8(AL) = 1;
                   8893:                } else {
                   8894:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   8895:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     8896:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
1.1.1.14  root     8897:                }
                   8898:        }
                   8899: }
                   8900: 
                   8901: inline void msdos_int_21h_22h()
                   8902: {
                   8903:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8904:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   8905:        
                   8906:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   8907:                REG8(AL) = 0xff;
                   8908:        } else {
                   8909:                process_t *process = msdos_process_info_get(current_psp);
                   8910:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
1.1.1.16  root     8911:                DWORD num = 0;
1.1.1.14  root     8912:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL);
                   8913:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   8914:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   8915:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     8916:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
1.1.1.14  root     8917:        }
                   8918: }
                   8919: 
1.1.1.16  root     8920: inline void msdos_int_21h_23h()
                   8921: {
                   8922:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8923:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   8924:        char *path = msdos_fcb_path(fcb);
                   8925:        HANDLE hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   8926:        
                   8927:        if(hFile == INVALID_HANDLE_VALUE) {
                   8928:                REG8(AL) = 0xff;
                   8929:        } else {
                   8930:                UINT32 size = GetFileSize(hFile, NULL);
                   8931:                fcb->rand_record = size / fcb->record_size + ((size % fcb->record_size) != 0);
                   8932:                REG8(AL) = 0;
                   8933:        }
                   8934: }
                   8935: 
                   8936: inline void msdos_int_21h_24h()
                   8937: {
                   8938:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8939:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   8940:        
                   8941:        fcb->rand_record = fcb->current_block * fcb->record_size + fcb->cur_record;
                   8942: }
                   8943: 
1.1       root     8944: inline void msdos_int_21h_25h()
                   8945: {
                   8946:        *(UINT16 *)(mem + 4 * REG8(AL) + 0) = REG16(DX);
1.1.1.3   root     8947:        *(UINT16 *)(mem + 4 * REG8(AL) + 2) = SREG(DS);
1.1       root     8948: }
                   8949: 
                   8950: inline void msdos_int_21h_26h()
                   8951: {
                   8952:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   8953:        
                   8954:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
                   8955:        psp->first_mcb = REG16(DX) + 16;
                   8956:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   8957:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   8958:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   8959:        psp->parent_psp = 0;
                   8960: }
                   8961: 
1.1.1.16  root     8962: inline void msdos_int_21h_27h()
                   8963: {
                   8964:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8965:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   8966:        
                   8967:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   8968:                REG8(AL) = 1;
                   8969:        } else {
                   8970:                process_t *process = msdos_process_info_get(current_psp);
                   8971:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   8972:                memset(mem + dta_laddr, 0, fcb->record_size * REG16(CX));
                   8973:                DWORD num = 0;
                   8974:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL) || num == 0) {
                   8975:                        REG8(AL) = 1;
                   8976:                } else {
                   8977:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   8978:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   8979:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   8980:                        REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   8981:                }
                   8982:        }
                   8983: }
                   8984: 
                   8985: inline void msdos_int_21h_28h()
                   8986: {
                   8987:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8988:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   8989:        
                   8990:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   8991:                REG8(AL) = 0xff;
                   8992:        } else {
                   8993:                process_t *process = msdos_process_info_get(current_psp);
                   8994:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   8995:                DWORD num = 0;
                   8996:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL);
                   8997:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   8998:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   8999:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   9000:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   9001:                REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   9002:        }
                   9003: }
                   9004: 
1.1       root     9005: inline void msdos_int_21h_29h()
                   9006: {
1.1.1.20  root     9007:        int ofs = 0;//SREG_BASE(DS) + REG16(SI);
                   9008:        char buffer[1024], name[MAX_PATH], ext[MAX_PATH];
1.1       root     9009:        UINT8 drv = 0;
                   9010:        char sep_chars[] = ":.;,=+";
                   9011:        char end_chars[] = "\\<>|/\"[]";
                   9012:        char spc_chars[] = " \t";
                   9013:        
1.1.1.20  root     9014:        memcpy(buffer, mem + SREG_BASE(DS) + REG16(SI), 1023);
                   9015:        buffer[1023] = 0;
                   9016:        memset(name, 0x20, sizeof(name));
                   9017:        memset(ext, 0x20, sizeof(ext));
                   9018:        
1.1       root     9019:        if(REG8(AL) & 1) {
1.1.1.20  root     9020:                ofs += strspn((char *)(buffer + ofs), spc_chars);
                   9021:                if(my_strchr(sep_chars, buffer[ofs]) && buffer[ofs] != '\0') {
1.1       root     9022:                        ofs++;
                   9023:                }
                   9024:        }
1.1.1.20  root     9025:        ofs += strspn((char *)(buffer + ofs), spc_chars);
1.1       root     9026:        
1.1.1.24  root     9027:        if(buffer[ofs + 1] == ':') {
                   9028:                if(buffer[ofs] >= 'a' && buffer[ofs] <= 'z') {
                   9029:                        drv = buffer[ofs] - 'a' + 1;
1.1.1.20  root     9030:                        ofs += 2;
1.1.1.24  root     9031:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   9032:                                ofs++;
                   9033:                        }
                   9034:                } else if(buffer[ofs] >= 'A' && buffer[ofs] <= 'Z') {
                   9035:                        drv = buffer[ofs] - 'A' + 1;
1.1       root     9036:                        ofs += 2;
1.1.1.24  root     9037:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   9038:                                ofs++;
                   9039:                        }
1.1       root     9040:                }
                   9041:        }
1.1.1.20  root     9042:        for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   9043:                UINT8 c = buffer[ofs];
                   9044:                if(is_kanji) {
                   9045:                        is_kanji = 0;
                   9046:                } else if(msdos_lead_byte_check(c)) {
                   9047:                        is_kanji = 1;
                   9048:                } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     9049:                        break;
                   9050:                } else if(c >= 'a' && c <= 'z') {
                   9051:                        c -= 0x20;
                   9052:                }
                   9053:                ofs++;
                   9054:                name[i] = c;
                   9055:        }
1.1.1.20  root     9056:        if(buffer[ofs] == '.') {
1.1       root     9057:                ofs++;
1.1.1.20  root     9058:                for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   9059:                        UINT8 c = buffer[ofs];
                   9060:                        if(is_kanji) {
                   9061:                                is_kanji = 0;
                   9062:                        } else if(msdos_lead_byte_check(c)) {
                   9063:                                is_kanji = 1;
                   9064:                        } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     9065:                                break;
                   9066:                        } else if(c >= 'a' && c <= 'z') {
                   9067:                                c -= 0x20;
                   9068:                        }
                   9069:                        ofs++;
                   9070:                        ext[i] = c;
                   9071:                }
                   9072:        }
1.1.1.20  root     9073:        int si = REG16(SI) + ofs;
1.1.1.3   root     9074:        int ds = SREG(DS);
1.1       root     9075:        while(si > 0xffff) {
                   9076:                si -= 0x10;
                   9077:                ds++;
                   9078:        }
                   9079:        REG16(SI) = si;
1.1.1.3   root     9080:        SREG(DS) = ds;
                   9081:        i386_load_segment_descriptor(DS);
1.1       root     9082:        
1.1.1.3   root     9083:        UINT8 *fcb = mem + SREG_BASE(ES) + REG16(DI);
1.1.1.20  root     9084:        if(!(REG8(AL) & 2) || drv != 0) {
                   9085:                fcb[0] = drv;
                   9086:        }
                   9087:        if(!(REG8(AL) & 4) || name[0] != 0x20) {
                   9088:                memcpy(fcb + 1, name, 8);
                   9089:        }
                   9090:        if(!(REG8(AL) & 8) || ext[0] != 0x20) {
                   9091:                memcpy(fcb + 9, ext, 3);
                   9092:        }
                   9093:        for(int i = 1, found_star = 0; i < 1 + 8; i++) {
1.1       root     9094:                if(fcb[i] == '*') {
                   9095:                        found_star = 1;
                   9096:                }
                   9097:                if(found_star) {
                   9098:                        fcb[i] = '?';
                   9099:                }
                   9100:        }
1.1.1.20  root     9101:        for(int i = 9, found_star = 0; i < 9 + 3; i++) {
1.1       root     9102:                if(fcb[i] == '*') {
                   9103:                        found_star = 1;
                   9104:                }
                   9105:                if(found_star) {
                   9106:                        fcb[i] = '?';
                   9107:                }
                   9108:        }
                   9109:        
                   9110:        if(drv == 0 || (drv > 0 && drv <= 26 && (GetLogicalDrives() & ( 1 << (drv - 1) )))) {
                   9111:                if(memchr(fcb + 1, '?', 8 + 3)) {
                   9112:                        REG8(AL) = 0x01;
1.1.1.20  root     9113:                } else {
                   9114:                        REG8(AL) = 0x00;
1.1       root     9115:                }
                   9116:        } else {
                   9117:                REG8(AL) = 0xff;
                   9118:        }
                   9119: }
                   9120: 
                   9121: inline void msdos_int_21h_2ah()
                   9122: {
                   9123:        SYSTEMTIME sTime;
                   9124:        
                   9125:        GetLocalTime(&sTime);
                   9126:        REG16(CX) = sTime.wYear;
                   9127:        REG8(DH) = (UINT8)sTime.wMonth;
                   9128:        REG8(DL) = (UINT8)sTime.wDay;
                   9129:        REG8(AL) = (UINT8)sTime.wDayOfWeek;
                   9130: }
                   9131: 
                   9132: inline void msdos_int_21h_2bh()
                   9133: {
1.1.1.14  root     9134:        REG8(AL) = 0xff;
1.1       root     9135: }
                   9136: 
                   9137: inline void msdos_int_21h_2ch()
                   9138: {
                   9139:        SYSTEMTIME sTime;
                   9140:        
                   9141:        GetLocalTime(&sTime);
                   9142:        REG8(CH) = (UINT8)sTime.wHour;
                   9143:        REG8(CL) = (UINT8)sTime.wMinute;
                   9144:        REG8(DH) = (UINT8)sTime.wSecond;
1.1.1.14  root     9145:        REG8(DL) = (UINT8)sTime.wMilliseconds / 10;
1.1       root     9146: }
                   9147: 
                   9148: inline void msdos_int_21h_2dh()
                   9149: {
                   9150:        REG8(AL) = 0x00;
                   9151: }
                   9152: 
                   9153: inline void msdos_int_21h_2eh()
                   9154: {
                   9155:        process_t *process = msdos_process_info_get(current_psp);
                   9156:        
                   9157:        process->verify = REG8(AL);
                   9158: }
                   9159: 
                   9160: inline void msdos_int_21h_2fh()
                   9161: {
                   9162:        process_t *process = msdos_process_info_get(current_psp);
                   9163:        
                   9164:        REG16(BX) = process->dta.w.l;
1.1.1.3   root     9165:        SREG(ES) = process->dta.w.h;
                   9166:        i386_load_segment_descriptor(ES);
1.1       root     9167: }
                   9168: 
                   9169: inline void msdos_int_21h_30h()
                   9170: {
                   9171:        // Version Flag / OEM
1.1.1.27  root     9172:        if(REG8(AL) == 0x01) {
1.1.1.29  root     9173: #ifdef SUPPORT_HMA
                   9174:                REG16(BX) = 0x0000;
                   9175: #else
                   9176:                REG16(BX) = 0x1000; // DOS is in HMA
                   9177: #endif
1.1       root     9178:        } else {
1.1.1.27  root     9179:                // NOTE: EXDEB invites BL shows the machine type (0=PC-98, 1=PC/AT, 2=FMR),
                   9180:                // but this is not correct on Windows 98 SE
                   9181: //             REG16(BX) = 0xff01;     // OEM = Microsoft, PC/AT
                   9182:                REG16(BX) = 0xff00;     // OEM = Microsoft
1.1       root     9183:        }
1.1.1.27  root     9184:        REG16(CX) = 0x0000;
1.1.1.30  root     9185:        REG8(AL) = dos_major_version;   // 7
                   9186:        REG8(AH) = dos_minor_version;   // 10
1.1       root     9187: }
                   9188: 
                   9189: inline void msdos_int_21h_31h()
                   9190: {
1.1.1.29  root     9191:        try {
                   9192:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   9193:        } catch(...) {
                   9194:                // recover the broken mcb
                   9195:                int mcb_seg = current_psp - 1;
                   9196:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     9197:                
1.1.1.29  root     9198:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     9199:                        mcb->mz = 'M';
                   9200:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   9201:                        
1.1.1.29  root     9202:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39  root     9203:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29  root     9204:                        } else {
1.1.1.39  root     9205:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29  root     9206:                        }
                   9207:                } else {
                   9208:                        mcb->mz = 'Z';
1.1.1.30  root     9209:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     9210:                }
                   9211:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   9212:        }
1.1       root     9213:        msdos_process_terminate(current_psp, REG8(AL) | 0x300, 0);
                   9214: }
                   9215: 
                   9216: inline void msdos_int_21h_32h()
                   9217: {
                   9218:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   9219:        UINT16 seg, ofs;
                   9220:        
                   9221:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   9222:                REG8(AL) = 0;
1.1.1.3   root     9223:                SREG(DS) = seg;
                   9224:                i386_load_segment_descriptor(DS);
1.1       root     9225:                REG16(BX) = ofs;
                   9226:        } else {
                   9227:                REG8(AL) = 0xff;
1.1.1.3   root     9228:                m_CF = 1;
1.1       root     9229:        }
                   9230: }
                   9231: 
                   9232: inline void msdos_int_21h_33h()
                   9233: {
                   9234:        char path[MAX_PATH];
                   9235:        
                   9236:        switch(REG8(AL)) {
                   9237:        case 0x00:
1.1.1.33  root     9238:                REG8(DL) = ctrl_break_checking;
1.1       root     9239:                break;
                   9240:        case 0x01:
1.1.1.33  root     9241:                ctrl_break_checking = REG8(DL);
                   9242:                break;
                   9243:        case 0x02:
                   9244:                {
                   9245:                        UINT8 old = ctrl_break_checking;
                   9246:                        ctrl_break_checking = REG8(DL);
                   9247:                        REG8(DL) = old;
                   9248:                }
                   9249:                break;
                   9250:        case 0x03:
                   9251:        case 0x04:
                   9252:                // DOS 4.0+ - Unused
1.1       root     9253:                break;
                   9254:        case 0x05:
                   9255:                GetSystemDirectory(path, MAX_PATH);
                   9256:                if(path[0] >= 'a' && path[0] <= 'z') {
                   9257:                        REG8(DL) = path[0] - 'a' + 1;
                   9258:                } else {
                   9259:                        REG8(DL) = path[0] - 'A' + 1;
                   9260:                }
                   9261:                break;
                   9262:        case 0x06:
1.1.1.2   root     9263:                // MS-DOS version (7.10)
1.1       root     9264:                REG8(BL) = 7;
1.1.1.2   root     9265:                REG8(BH) = 10;
1.1       root     9266:                REG8(DL) = 0;
1.1.1.29  root     9267: #ifdef SUPPORT_HMA
                   9268:                REG8(DH) = 0x00;
                   9269: #else
                   9270:                REG8(DH) = 0x10; // DOS is in HMA
                   9271: #endif
1.1       root     9272:                break;
1.1.1.6   root     9273:        case 0x07:
                   9274:                if(REG8(DL) == 0) {
                   9275:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag &= ~0x20;
                   9276:                } else if(REG8(DL) == 1) {
                   9277:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag |= 0x20;
                   9278:                }
                   9279:                break;
1.1       root     9280:        default:
1.1.1.22  root     9281:                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     9282:                REG16(AX) = 0x01;
1.1.1.3   root     9283:                m_CF = 1;
1.1       root     9284:                break;
                   9285:        }
                   9286: }
                   9287: 
1.1.1.23  root     9288: inline void msdos_int_21h_34h()
                   9289: {
                   9290:        SREG(ES) = SDA_TOP >> 4;
                   9291:        i386_load_segment_descriptor(ES);
                   9292:        REG16(BX) = offsetof(sda_t, indos_flag);;
                   9293: }
                   9294: 
1.1       root     9295: inline void msdos_int_21h_35h()
                   9296: {
                   9297:        REG16(BX) = *(UINT16 *)(mem + 4 * REG8(AL) + 0);
1.1.1.3   root     9298:        SREG(ES) = *(UINT16 *)(mem + 4 * REG8(AL) + 2);
                   9299:        i386_load_segment_descriptor(ES);
1.1       root     9300: }
                   9301: 
                   9302: inline void msdos_int_21h_36h()
                   9303: {
                   9304:        struct _diskfree_t df = {0};
                   9305:        
                   9306:        if(_getdiskfree(REG8(DL), &df) == 0) {
                   9307:                REG16(AX) = (UINT16)df.sectors_per_cluster;
                   9308:                REG16(CX) = (UINT16)df.bytes_per_sector;
1.1.1.13  root     9309:                REG16(BX) = df.avail_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.avail_clusters;
                   9310:                REG16(DX) = df.total_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.total_clusters;
1.1       root     9311:        } else {
                   9312:                REG16(AX) = 0xffff;
                   9313:        }
                   9314: }
                   9315: 
                   9316: inline void msdos_int_21h_37h()
                   9317: {
1.1.1.22  root     9318:        static UINT8 dev_flag = 0xff;
1.1       root     9319:        
                   9320:        switch(REG8(AL)) {
                   9321:        case 0x00:
1.1.1.22  root     9322:                {
                   9323:                        process_t *process = msdos_process_info_get(current_psp);
                   9324:                        REG8(AL) = 0x00;
                   9325:                        REG8(DL) = process->switchar;
                   9326:                }
1.1       root     9327:                break;
                   9328:        case 0x01:
1.1.1.22  root     9329:                {
                   9330:                        process_t *process = msdos_process_info_get(current_psp);
                   9331:                        REG8(AL) = 0x00;
                   9332:                        process->switchar = REG8(DL);
1.1.1.23  root     9333:                        msdos_sda_update(current_psp);
1.1.1.22  root     9334:                }
                   9335:                break;
                   9336:        case 0x02:
                   9337:                REG8(DL) = dev_flag;
                   9338:                break;
                   9339:        case 0x03:
                   9340:                dev_flag = REG8(DL);
                   9341:                break;
                   9342:        case 0xd0:
                   9343:        case 0xd1:
                   9344:        case 0xd2:
                   9345:        case 0xd3:
                   9346:        case 0xd4:
                   9347:        case 0xd5:
                   9348:        case 0xd6:
                   9349:        case 0xd7:
                   9350:        case 0xdc:
                   9351:        case 0xdd:
                   9352:        case 0xde:
                   9353:        case 0xdf:
                   9354:                // diet ???
                   9355:                REG16(AX) = 1;
1.1       root     9356:                break;
                   9357:        default:
1.1.1.22  root     9358:                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     9359:                REG16(AX) = 1;
                   9360:                break;
                   9361:        }
                   9362: }
                   9363: 
1.1.1.19  root     9364: int get_country_info(country_info_t *ci)
1.1.1.17  root     9365: {
                   9366:        char LCdata[80];
                   9367:        
1.1.1.19  root     9368:        ZeroMemory(ci, offsetof(country_info_t, reserved));
1.1.1.17  root     9369:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRDIGITS, LCdata, sizeof(LCdata));
                   9370:        ci->currency_dec_digits = atoi(LCdata);
                   9371:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRENCY, LCdata, sizeof(LCdata));
                   9372:        ci->currency_format = *LCdata - '0';
                   9373:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IDATE, LCdata, sizeof(LCdata));
                   9374:        ci->date_format = *LCdata - '0';
                   9375:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SCURRENCY, LCdata, sizeof(LCdata));
                   9376:        memcpy(&ci->currency_symbol, LCdata, 4);
                   9377:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDATE, LCdata, sizeof(LCdata));
                   9378:        *ci->date_sep = *LCdata;
                   9379:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, LCdata, sizeof(LCdata));
                   9380:        *ci->dec_sep = *LCdata;
                   9381:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SLIST, LCdata, sizeof(LCdata));
                   9382:        *ci->list_sep = *LCdata;
                   9383:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, LCdata, sizeof(LCdata));
                   9384:        *ci->thou_sep = *LCdata;
                   9385:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIME, LCdata, sizeof(LCdata));
                   9386:        *ci->time_sep = *LCdata;
                   9387:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, LCdata, sizeof(LCdata));
                   9388:        if(strchr(LCdata, 'H') != NULL) {
                   9389:                ci->time_format = 1;
                   9390:        }
1.1.1.27  root     9391:        ci->case_map.w.l = 0x000a; // dummy case map routine is at fffd:000a
1.1.1.24  root     9392:        ci->case_map.w.h = 0xfffd;
1.1.1.17  root     9393:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICOUNTRY, LCdata, sizeof(LCdata));
                   9394:        return atoi(LCdata);
                   9395: }
                   9396: 
1.1.1.14  root     9397: inline void msdos_int_21h_38h()
                   9398: {
                   9399:        switch(REG8(AL)) {
                   9400:        case 0x00:
1.1.1.19  root     9401:                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1.1.14  root     9402:                break;
                   9403:        default:
1.1.1.22  root     9404:                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     9405:                REG16(AX) = 2;
                   9406:                m_CF = 1;
                   9407:                break;
                   9408:        }
                   9409: }
                   9410: 
1.1       root     9411: inline void msdos_int_21h_39h(int lfn)
                   9412: {
1.1.1.3   root     9413:        if(_mkdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     9414:                REG16(AX) = errno;
1.1.1.3   root     9415:                m_CF = 1;
1.1       root     9416:        }
                   9417: }
                   9418: 
                   9419: inline void msdos_int_21h_3ah(int lfn)
                   9420: {
1.1.1.3   root     9421:        if(_rmdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     9422:                REG16(AX) = errno;
1.1.1.3   root     9423:                m_CF = 1;
1.1       root     9424:        }
                   9425: }
                   9426: 
                   9427: inline void msdos_int_21h_3bh(int lfn)
                   9428: {
1.1.1.3   root     9429:        if(_chdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1.1.17  root     9430:                REG16(AX) = 3;  // must be 3 (path not found)
1.1.1.3   root     9431:                m_CF = 1;
1.1       root     9432:        }
                   9433: }
                   9434: 
                   9435: inline void msdos_int_21h_3ch()
                   9436: {
1.1.1.3   root     9437:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     9438:        int attr = GetFileAttributes(path);
1.1.1.37  root     9439:        int fd = -1;
1.1.1.11  root     9440:        UINT16 info;
1.1.1.37  root     9441:        int sio_port = 0;
                   9442:        int lpt_port = 0;
1.1       root     9443:        
1.1.1.11  root     9444:        if(msdos_is_con_path(path)) {
                   9445:                fd = _open("CON", _O_WRONLY | _O_BINARY);
                   9446:                info = 0x80d3;
1.1.1.37  root     9447:        } else if((sio_port = msdos_is_comm_path(path)) != 0) {
1.1.1.38  root     9448:                fd = _open("NUL", _O_RDWR | _O_BINARY);
1.1.1.14  root     9449:                info = 0x80d3;
1.1.1.37  root     9450:                msdos_set_comm_params(sio_port, path);
                   9451:        } else if((lpt_port = msdos_is_prn_path(path)) != 0) {
                   9452:                fd = _open("NUL", _O_WRONLY | _O_BINARY);
                   9453:                info = 0xa8c0;
1.1.1.29  root     9454:        } else if(msdos_is_device_path(path)) {
1.1.1.20  root     9455:                fd = _open("NUL", _O_WRONLY | _O_BINARY);
                   9456:                info = 0x80d3;
1.1       root     9457:        } else {
                   9458:                fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
1.1.1.11  root     9459:                info = msdos_drive_number(path);
1.1       root     9460:        }
                   9461:        if(fd != -1) {
                   9462:                if(attr == -1) {
                   9463:                        attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   9464:                }
                   9465:                SetFileAttributes(path, attr);
                   9466:                REG16(AX) = fd;
1.1.1.37  root     9467:                msdos_file_handler_open(fd, path, _isatty(fd), 2, info, current_psp, sio_port, lpt_port);
1.1.1.20  root     9468:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     9469:        } else {
                   9470:                REG16(AX) = errno;
1.1.1.3   root     9471:                m_CF = 1;
1.1       root     9472:        }
                   9473: }
                   9474: 
                   9475: inline void msdos_int_21h_3dh()
                   9476: {
1.1.1.3   root     9477:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     9478:        int mode = REG8(AL) & 0x03;
1.1.1.37  root     9479:        int fd = -1;
1.1.1.11  root     9480:        UINT16 info;
1.1.1.37  root     9481:        int sio_port = 0;
                   9482:        int lpt_port = 0;
1.1       root     9483:        
                   9484:        if(mode < 0x03) {
1.1.1.11  root     9485:                if(msdos_is_con_path(path)) {
1.1.1.13  root     9486:                        fd = msdos_open("CON", file_mode[mode].mode);
1.1.1.11  root     9487:                        info = 0x80d3;
1.1.1.37  root     9488:                } else if((sio_port = msdos_is_comm_path(path)) != 0) {
                   9489:                        fd = msdos_open("NUL", file_mode[mode].mode);
1.1.1.14  root     9490:                        info = 0x80d3;
1.1.1.37  root     9491:                        msdos_set_comm_params(sio_port, path);
                   9492:                } else if((lpt_port = msdos_is_prn_path(path)) != 0) {
                   9493:                        fd = _open("NUL", file_mode[mode].mode);
                   9494:                        info = 0xa8c0;
1.1.1.29  root     9495:                } else if(msdos_is_device_path(path)) {
1.1.1.20  root     9496:                        fd = msdos_open("NUL", file_mode[mode].mode);
                   9497:                        info = 0x80d3;
1.1.1.11  root     9498:                } else {
1.1.1.13  root     9499:                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     9500:                        info = msdos_drive_number(path);
                   9501:                }
1.1       root     9502:                if(fd != -1) {
                   9503:                        REG16(AX) = fd;
1.1.1.37  root     9504:                        msdos_file_handler_open(fd, path, _isatty(fd), mode, info, current_psp, sio_port, lpt_port);
1.1.1.20  root     9505:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     9506:                } else {
                   9507:                        REG16(AX) = errno;
1.1.1.3   root     9508:                        m_CF = 1;
1.1       root     9509:                }
                   9510:        } else {
                   9511:                REG16(AX) = 0x0c;
1.1.1.3   root     9512:                m_CF = 1;
1.1       root     9513:        }
                   9514: }
                   9515: 
                   9516: inline void msdos_int_21h_3eh()
                   9517: {
                   9518:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     9519:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     9520:        
1.1.1.20  root     9521:        if(fd < process->max_files && file_handler[fd].valid) {
                   9522:                _close(fd);
                   9523:                msdos_file_handler_close(fd);
                   9524:                msdos_psp_set_file_table(REG16(BX), 0x0ff, current_psp);
1.1       root     9525:        } else {
                   9526:                REG16(AX) = 0x06;
1.1.1.3   root     9527:                m_CF = 1;
1.1       root     9528:        }
                   9529: }
                   9530: 
1.1.1.35  root     9531: DWORD WINAPI msdos_int_21h_3fh_thread(LPVOID)
                   9532: {
                   9533:        UINT8 *buf = mem + SREG_BASE(DS) + REG16(DX);
                   9534:        int max = REG16(CX);
                   9535:        int p = 0;
                   9536:        
                   9537:        while(max > p) {
                   9538:                int chr = msdos_getch();
                   9539:                
                   9540:                if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
                   9541:                        p = 0;
                   9542:                        buf[p++] = 0x0d;
                   9543:                        if(max > p) {
                   9544:                                buf[p++] = 0x0a;
                   9545:                        }
                   9546:                        msdos_putch(0x03);
                   9547:                        msdos_putch(0x0d);
                   9548:                        msdos_putch(0x0a);
                   9549:                        break;
                   9550:                } else if(ctrl_break_pressed) {
                   9551:                        // skip this byte
                   9552:                } else if(chr == 0x00) {
                   9553:                        // skip 2nd byte
                   9554:                        msdos_getch();
                   9555:                } else if(chr == 0x0d) {
                   9556:                        // carriage return
                   9557:                        buf[p++] = 0x0d;
                   9558:                        if(max > p) {
                   9559:                                buf[p++] = 0x0a;
                   9560:                        }
                   9561:                        msdos_putch('\n');
                   9562:                        break;
                   9563:                } else if(chr == 0x08) {
                   9564:                        // back space
                   9565:                        if(p > 0) {
                   9566:                                p--;
                   9567:                                if(msdos_ctrl_code_check(buf[p])) {
                   9568:                                        msdos_putch(0x08);
                   9569:                                        msdos_putch(0x08);
                   9570:                                        msdos_putch(0x20);
                   9571:                                        msdos_putch(0x20);
                   9572:                                        msdos_putch(0x08);
                   9573:                                        msdos_putch(0x08);
1.1.1.36  root     9574:                                } else if(p > 0 && msdos_kanji_2nd_byte_check(buf, p)) {
                   9575:                                        p--;
                   9576:                                        msdos_putch(0x08);
                   9577:                                        msdos_putch(0x08);
                   9578:                                        msdos_putch(0x20);
                   9579:                                        msdos_putch(0x20);
                   9580:                                        msdos_putch(0x08);
                   9581:                                        msdos_putch(0x08);
1.1.1.35  root     9582:                                } else {
                   9583:                                        msdos_putch(0x08);
                   9584:                                        msdos_putch(0x20);
                   9585:                                        msdos_putch(0x08);
                   9586:                                }
                   9587:                        }
                   9588:                } else if(chr == 0x1b) {
                   9589:                        // escape
                   9590:                        while(p > 0) {
                   9591:                                p--;
                   9592:                                if(msdos_ctrl_code_check(buf[p])) {
                   9593:                                        msdos_putch(0x08);
                   9594:                                        msdos_putch(0x08);
                   9595:                                        msdos_putch(0x20);
                   9596:                                        msdos_putch(0x20);
                   9597:                                        msdos_putch(0x08);
                   9598:                                        msdos_putch(0x08);
                   9599:                                } else {
                   9600:                                        msdos_putch(0x08);
                   9601:                                        msdos_putch(0x20);
                   9602:                                        msdos_putch(0x08);
                   9603:                                }
                   9604:                        }
                   9605:                } else {
                   9606:                        buf[p++] = chr;
                   9607:                        msdos_putch(chr);
                   9608:                }
                   9609:        }
                   9610:        REG16(AX) = p;
                   9611:        
                   9612: #ifdef USE_SERVICE_THREAD
                   9613:        service_exit = true;
                   9614: #endif
                   9615:        return(0);
                   9616: }
                   9617: 
1.1       root     9618: inline void msdos_int_21h_3fh()
                   9619: {
                   9620:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     9621:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     9622:        
1.1.1.20  root     9623:        if(fd < process->max_files && file_handler[fd].valid) {
                   9624:                if(file_mode[file_handler[fd].mode].in) {
                   9625:                        if(file_handler[fd].atty) {
1.1       root     9626:                                // BX is stdin or is redirected to stdin
1.1.1.35  root     9627:                                if(REG16(CX) != 0) {
                   9628: #ifdef USE_SERVICE_THREAD
                   9629:                                        start_service_loop(msdos_int_21h_3fh_thread);
                   9630: #else
                   9631:                                        msdos_int_21h_3fh_thread(NULL);
                   9632:                                        REQUEST_HARDWRE_UPDATE();
                   9633: #endif
                   9634:                                } else {
                   9635:                                        REG16(AX) = 0;
1.1       root     9636:                                }
                   9637:                        } else {
1.1.1.37  root     9638:                                REG16(AX) = msdos_read(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     9639:                        }
                   9640:                } else {
                   9641:                        REG16(AX) = 0x05;
1.1.1.3   root     9642:                        m_CF = 1;
1.1       root     9643:                }
                   9644:        } else {
                   9645:                REG16(AX) = 0x06;
1.1.1.3   root     9646:                m_CF = 1;
1.1       root     9647:        }
                   9648: }
                   9649: 
                   9650: inline void msdos_int_21h_40h()
                   9651: {
                   9652:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     9653:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     9654:        
1.1.1.20  root     9655:        if(fd < process->max_files && file_handler[fd].valid) {
                   9656:                if(file_mode[file_handler[fd].mode].out) {
1.1       root     9657:                        if(REG16(CX)) {
1.1.1.20  root     9658:                                if(file_handler[fd].atty) {
1.1       root     9659:                                        // BX is stdout/stderr or is redirected to stdout
                   9660:                                        for(int i = 0; i < REG16(CX); i++) {
1.1.1.3   root     9661:                                                msdos_putch(mem[SREG_BASE(DS) + REG16(DX) + i]);
1.1       root     9662:                                        }
                   9663:                                        REG16(AX) = REG16(CX);
                   9664:                                } else {
1.1.1.20  root     9665:                                        REG16(AX) = msdos_write(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     9666:                                }
                   9667:                        } else {
1.1.1.20  root     9668:                                UINT32 pos = _tell(fd);
                   9669:                                _lseek(fd, 0, SEEK_END);
                   9670:                                UINT32 size = _tell(fd);
1.1.1.12  root     9671:                                if(pos < size) {
1.1.1.20  root     9672:                                        _lseek(fd, pos, SEEK_SET);
                   9673:                                        SetEndOfFile((HANDLE)_get_osfhandle(fd));
1.1.1.12  root     9674:                                } else {
                   9675:                                        for(UINT32 i = size; i < pos; i++) {
                   9676:                                                UINT8 tmp = 0;
1.1.1.23  root     9677:                                                msdos_write(fd, &tmp, 1);
1.1.1.12  root     9678:                                        }
1.1.1.20  root     9679:                                        _lseek(fd, pos, SEEK_SET);
1.1       root     9680:                                }
1.1.1.23  root     9681:                                REG16(AX) = 0;
1.1       root     9682:                        }
                   9683:                } else {
                   9684:                        REG16(AX) = 0x05;
1.1.1.3   root     9685:                        m_CF = 1;
1.1       root     9686:                }
                   9687:        } else {
                   9688:                REG16(AX) = 0x06;
1.1.1.3   root     9689:                m_CF = 1;
1.1       root     9690:        }
                   9691: }
                   9692: 
                   9693: inline void msdos_int_21h_41h(int lfn)
                   9694: {
1.1.1.3   root     9695:        if(remove(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     9696:                REG16(AX) = errno;
1.1.1.3   root     9697:                m_CF = 1;
1.1       root     9698:        }
                   9699: }
                   9700: 
                   9701: inline void msdos_int_21h_42h()
                   9702: {
                   9703:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     9704:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     9705:        
1.1.1.20  root     9706:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     9707:                if(REG8(AL) < 0x03) {
1.1.1.35  root     9708:                        static const int ptrname[] = { SEEK_SET, SEEK_CUR, SEEK_END };
1.1.1.20  root     9709:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), ptrname[REG8(AL)]);
                   9710:                        UINT32 pos = _tell(fd);
1.1       root     9711:                        REG16(AX) = pos & 0xffff;
                   9712:                        REG16(DX) = (pos >> 16);
                   9713:                } else {
                   9714:                        REG16(AX) = 0x01;
1.1.1.3   root     9715:                        m_CF = 1;
1.1       root     9716:                }
                   9717:        } else {
                   9718:                REG16(AX) = 0x06;
1.1.1.3   root     9719:                m_CF = 1;
1.1       root     9720:        }
                   9721: }
                   9722: 
                   9723: inline void msdos_int_21h_43h(int lfn)
                   9724: {
1.1.1.3   root     9725:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1       root     9726:        int attr;
                   9727:        
1.1.1.14  root     9728:        if(!lfn && REG8(AL) > 2) {
                   9729:                REG16(AX) = 0x01;
                   9730:                m_CF = 1;
                   9731:                return;
                   9732:        }
                   9733:        switch(REG8(lfn ? BL : AL)) {
1.1       root     9734:        case 0x00:
                   9735:                if((attr = GetFileAttributes(path)) != -1) {
1.1.1.14  root     9736:                        REG16(CX) = (UINT16)msdos_file_attribute_create((UINT16)attr);
                   9737:                } else {
                   9738:                        REG16(AX) = (UINT16)GetLastError();
                   9739:                        m_CF = 1;
                   9740:                }
                   9741:                break;
                   9742:        case 0x01:
                   9743:                if(!SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)))) {
                   9744:                        REG16(AX) = (UINT16)GetLastError();
                   9745:                        m_CF = 1;
                   9746:                }
                   9747:                break;
                   9748:        case 0x02:
                   9749:                {
                   9750:                        DWORD size = GetCompressedFileSize(path, NULL);
                   9751:                        if(size != INVALID_FILE_SIZE) {
                   9752:                                if(size != 0 && size == GetFileSize(path, NULL)) {
                   9753:                                        DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   9754:                                        // this isn't correct if the file is in the NTFS MFT
                   9755:                                        if(GetDiskFreeSpace(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
                   9756:                                                size = ((size - 1) | (sectors_per_cluster * bytes_per_sector - 1)) + 1;
                   9757:                                        }
                   9758:                                }
                   9759:                                REG16(AX) = LOWORD(size);
                   9760:                                REG16(DX) = HIWORD(size);
                   9761:                        } else {
                   9762:                                REG16(AX) = (UINT16)GetLastError();
                   9763:                                m_CF = 1;
1.1       root     9764:                        }
1.1.1.14  root     9765:                }
                   9766:                break;
                   9767:        case 0x03:
                   9768:        case 0x05:
                   9769:        case 0x07:
                   9770:                {
                   9771:                        HANDLE hFile = CreateFile(path, FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   9772:                        if(hFile != INVALID_HANDLE_VALUE) {
                   9773:                                FILETIME local, time;
                   9774:                                DosDateTimeToFileTime(REG16(DI), /*REG8(BL) == 5 ? 0 : */REG16(CX), &local);
                   9775:                                if(REG8(BL) == 7) {
                   9776:                                        ULARGE_INTEGER hund;
                   9777:                                        hund.LowPart = local.dwLowDateTime;
                   9778:                                        hund.HighPart = local.dwHighDateTime;
                   9779:                                        hund.QuadPart += REG16(SI) * 100000;
                   9780:                                        local.dwLowDateTime = hund.LowPart;
                   9781:                                        local.dwHighDateTime = hund.HighPart;
                   9782:                                }
                   9783:                                LocalFileTimeToFileTime(&local, &time);
                   9784:                                if(!SetFileTime(hFile, REG8(BL) == 0x07 ? &time : NULL,
                   9785:                                                       REG8(BL) == 0x05 ? &time : NULL,
                   9786:                                                       REG8(BL) == 0x03 ? &time : NULL)) {
                   9787:                                        REG16(AX) = (UINT16)GetLastError();
                   9788:                                        m_CF = 1;
                   9789:                                }
                   9790:                                CloseHandle(hFile);
                   9791:                        } else {
                   9792:                                REG16(AX) = (UINT16)GetLastError();
                   9793:                                m_CF = 1;
1.1       root     9794:                        }
1.1.1.14  root     9795:                }
                   9796:                break;
                   9797:        case 0x04:
                   9798:        case 0x06:
                   9799:        case 0x08:
                   9800:                {
                   9801:                        WIN32_FILE_ATTRIBUTE_DATA fad;
                   9802:                        if(GetFileAttributesEx(path, GetFileExInfoStandard, (LPVOID)&fad)) {
                   9803:                                FILETIME *time, local;
                   9804:                                time = REG8(BL) == 0x04 ? &fad.ftLastWriteTime :
                   9805:                                                   0x06 ? &fad.ftLastAccessTime :
                   9806:                                                          &fad.ftCreationTime;
                   9807:                                FileTimeToLocalFileTime(time, &local);
                   9808:                                FileTimeToDosDateTime(&local, &REG16(DI), &REG16(CX));
                   9809:                                if(REG8(BL) == 0x08) {
                   9810:                                        ULARGE_INTEGER hund;
                   9811:                                        hund.LowPart = local.dwLowDateTime;
                   9812:                                        hund.HighPart = local.dwHighDateTime;
                   9813:                                        hund.QuadPart /= 100000;
                   9814:                                        REG16(SI) = (UINT16)(hund.QuadPart % 200);
                   9815:                                }
                   9816:                        } else {
                   9817:                                REG16(AX) = (UINT16)GetLastError();
                   9818:                                m_CF = 1;
1.1       root     9819:                        }
1.1.1.14  root     9820:                }
                   9821:                break;
                   9822:        default:
1.1.1.22  root     9823:                unimplemented_21h("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x21, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
1.1.1.14  root     9824:                REG16(AX) = 0x01;
                   9825:                m_CF = 1;
                   9826:                break;
                   9827:        }
                   9828: }
                   9829: 
                   9830: inline void msdos_int_21h_44h()
                   9831: {
1.1.1.22  root     9832:        static UINT16 iteration_count = 0;
                   9833:        
1.1.1.20  root     9834:        process_t *process = msdos_process_info_get(current_psp);
                   9835:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   9836:        
1.1.1.14  root     9837:        UINT32 val = DRIVE_NO_ROOT_DIR;
                   9838:        
                   9839:        switch(REG8(AL)) {
                   9840:        case 0x00:
                   9841:        case 0x01:
                   9842:        case 0x02:
                   9843:        case 0x03:
                   9844:        case 0x04:
                   9845:        case 0x05:
                   9846:        case 0x06:
                   9847:        case 0x07:
1.1.1.20  root     9848:                if(fd >= process->max_files || !file_handler[fd].valid) {
                   9849:                        REG16(AX) = 0x06;
                   9850:                        m_CF = 1;
                   9851:                        return;
1.1.1.14  root     9852:                }
                   9853:                break;
                   9854:        case 0x08:
                   9855:        case 0x09:
                   9856:                if(REG8(BL) >= ('Z' - 'A' + 1)) {
                   9857:                        // invalid drive number
                   9858:                        REG16(AX) = 0x0f;
                   9859:                        m_CF = 1;
                   9860:                        return;
                   9861:                } else {
                   9862:                        if(REG8(BL) == 0) {
                   9863:                                val = GetDriveType(NULL);
                   9864:                        } else {
                   9865:                                char tmp[8];
                   9866:                                sprintf(tmp, "%c:\\", 'A' + REG8(BL) - 1);
                   9867:                                val = GetDriveType(tmp);
                   9868:                        }
                   9869:                        if(val == DRIVE_NO_ROOT_DIR) {
                   9870:                                // no drive
                   9871:                                REG16(AX) = 0x0f;
                   9872:                                m_CF = 1;
                   9873:                                return;
1.1       root     9874:                        }
                   9875:                }
                   9876:                break;
                   9877:        }
                   9878:        switch(REG8(AL)) {
                   9879:        case 0x00: // get ioctrl data
1.1.1.20  root     9880:                REG16(DX) = file_handler[fd].info;
1.1       root     9881:                break;
                   9882:        case 0x01: // set ioctrl data
1.1.1.20  root     9883:                file_handler[fd].info |= REG8(DL);
1.1       root     9884:                break;
                   9885:        case 0x02: // recv from character device
                   9886:        case 0x03: // send to character device
                   9887:        case 0x04: // recv from block device
                   9888:        case 0x05: // send to block device
                   9889:                REG16(AX) = 0x05;
1.1.1.3   root     9890:                m_CF = 1;
1.1       root     9891:                break;
                   9892:        case 0x06: // get read status
1.1.1.20  root     9893:                if(file_mode[file_handler[fd].mode].in) {
                   9894:                        if(file_handler[fd].atty) {
1.1.1.14  root     9895:                                REG8(AL) = msdos_kbhit() ? 0xff : 0x00;
1.1       root     9896:                        } else {
1.1.1.20  root     9897:                                REG8(AL) = eof(fd) ? 0x00 : 0xff;
1.1       root     9898:                        }
1.1.1.14  root     9899:                } else {
                   9900:                        REG8(AL) = 0x00;
1.1       root     9901:                }
                   9902:                break;
                   9903:        case 0x07: // get write status
1.1.1.20  root     9904:                if(file_mode[file_handler[fd].mode].out) {
1.1.1.14  root     9905:                        REG8(AL) = 0xff;
                   9906:                } else {
                   9907:                        REG8(AL) = 0x00;
1.1       root     9908:                }
                   9909:                break;
                   9910:        case 0x08: // check removable drive
1.1.1.14  root     9911:                if(val == DRIVE_REMOVABLE || val == DRIVE_CDROM) {
                   9912:                        // removable drive
                   9913:                        REG16(AX) = 0x00;
1.1       root     9914:                } else {
1.1.1.14  root     9915:                        // fixed drive
                   9916:                        REG16(AX) = 0x01;
1.1       root     9917:                }
                   9918:                break;
                   9919:        case 0x09: // check remote drive
1.1.1.14  root     9920:                if(val == DRIVE_REMOTE) {
                   9921:                        // remote drive
                   9922:                        REG16(DX) = 0x1000;
1.1       root     9923:                } else {
1.1.1.14  root     9924:                        // local drive
                   9925:                        REG16(DX) = 0x00;
1.1       root     9926:                }
                   9927:                break;
1.1.1.21  root     9928:        case 0x0a: // check remote handle
                   9929:                REG16(DX) = 0x00; // FIXME
                   9930:                break;
1.1       root     9931:        case 0x0b: // set retry count
                   9932:                break;
1.1.1.22  root     9933:        case 0x0c: // generic character device request
                   9934:                if(REG8(CL) == 0x45) {
                   9935:                        // set iteration (retry) count
                   9936:                        iteration_count = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
                   9937:                } else if(REG8(CL) == 0x4a) {
                   9938:                        // select code page
                   9939:                        active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2);
                   9940:                        msdos_nls_tables_update();
                   9941:                } else if(REG8(CL) == 0x65) {
                   9942:                        // get iteration (retry) count
                   9943:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX)) = iteration_count;
                   9944:                } else if(REG8(CL) == 0x6a) {
                   9945:                        // query selected code page
                   9946:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2; // FIXME
                   9947:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
                   9948:                        
                   9949:                        CPINFO info;
                   9950:                        GetCPInfo(active_code_page, &info);
                   9951:                        
                   9952:                        if(info.MaxCharSize != 1) {
                   9953:                                for(int i = 0;; i++) {
                   9954:                                        UINT8 lo = info.LeadByte[2 * i + 0];
                   9955:                                        UINT8 hi = info.LeadByte[2 * i + 1];
                   9956:                                        
                   9957:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 0) = lo;
                   9958:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 2) = hi;
                   9959:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) + 4;
                   9960:                                        
                   9961:                                        if(lo == 0 && hi == 0) {
                   9962:                                                break;
                   9963:                                        }
                   9964:                                }
                   9965:                        }
                   9966:                } else if(REG8(CL) == 0x7f) {
                   9967:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00) = 0;
                   9968:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x01) = 0;
                   9969:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) = 14;
                   9970:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04) = 1;
                   9971:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06) = 1;
                   9972:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x07) = 0;
                   9973:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08) = 4;
                   9974:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a) =  8 * (*(UINT16 *)(mem + 0x44a));
                   9975:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c) = 16 * (*(UINT8  *)(mem + 0x484) + 1);
                   9976:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e) = *(UINT16 *)(mem + 0x44a);
                   9977:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10) = *(UINT8  *)(mem + 0x484) + 1;
                   9978:                } else {
                   9979:                        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));
                   9980:                        REG16(AX) = 0x01; // invalid function
                   9981:                        m_CF = 1;
                   9982:                }
                   9983:                break;
                   9984:        case 0x0d: // generic block device request
                   9985:                if(REG8(CL) == 0x40) {
                   9986:                        // set device parameters
                   9987:                } else if(REG8(CL) == 0x46) {
                   9988:                        // set volume serial number
                   9989:                } else if(REG8(CL) == 0x4a) {
                   9990:                        // lock logical volume
                   9991:                } else if(REG8(CL) == 0x4b) {
                   9992:                        // lock physical volume
                   9993:                } else if(REG8(CL) == 0x60) {
                   9994:                        // get device parameters
                   9995:                        char dev[] = "\\\\.\\A:";
                   9996:                        dev[4] = 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   9997:                        
                   9998:                        HANDLE hFile = CreateFile(dev, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   9999:                        if(hFile != INVALID_HANDLE_VALUE) {
                   10000:                                DISK_GEOMETRY geo;
                   10001:                                DWORD dwSize;
                   10002:                                if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &geo, sizeof(geo), &dwSize, NULL)) {
                   10003:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x07; // ???
                   10004:                                        switch(geo.MediaType) {
                   10005:                                        case F5_360_512:
                   10006:                                        case F5_320_512:
                   10007:                                        case F5_320_1024:
                   10008:                                        case F5_180_512:
                   10009:                                        case F5_160_512:
                   10010:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // 320K/360K disk
                   10011:                                                break;
                   10012:                                        case F5_1Pt2_512:
                   10013:                                        case F3_1Pt2_512:
                   10014:                                        case F3_1Pt23_1024:
                   10015:                                        case F5_1Pt23_1024:
                   10016:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x01; // 1.2M disk
                   10017:                                                break;
                   10018:                                        case F3_720_512:
                   10019:                                        case F3_640_512:
                   10020:                                        case F5_640_512:
                   10021:                                        case F5_720_512:
                   10022:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02; // 720K disk
                   10023:                                                break;
                   10024:                                        case F8_256_128:
                   10025:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x03; // single-density 8-inch disk
                   10026:                                                break;
                   10027:                                        case FixedMedia:
                   10028:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   10029:                                                break;
                   10030:                                        case F3_1Pt44_512:
                   10031:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   10032:                                                break;
                   10033:                                        case F3_2Pt88_512:
                   10034:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09; // (DOS 5+) 2.88M floppy
                   10035:                                                break;
                   10036:                                        default:
                   10037:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   10038: //                                             *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   10039:                                                break;
                   10040:                                        }
                   10041:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = (geo.MediaType == FixedMedia) ? 0x01 : 0x00;
                   10042:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04) = (geo.Cylinders.QuadPart > 0xffff) ? 0xffff : geo.Cylinders.QuadPart;
                   10043:                                        switch(geo.MediaType) {
                   10044:                                        case F5_360_512:
                   10045:                                        case F5_320_512:
                   10046:                                        case F5_320_1024:
                   10047:                                        case F5_180_512:
                   10048:                                        case F5_160_512:
                   10049:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x01; // 320K/360K disk
                   10050:                                                break;
                   10051:                                        default:
                   10052:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x00; // other drive types
                   10053:                                                break;
                   10054:                                        }
                   10055:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x00) = geo.BytesPerSector;
                   10056:                                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x02) = geo.SectorsPerTrack * geo.TracksPerCylinder;
                   10057:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x03) = 0;
                   10058:                                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x05) = 0;
                   10059:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x06) = 0;
                   10060:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x08) = 0;
                   10061:                                        switch(geo.MediaType) {
                   10062:                                        case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   10063:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xff;
                   10064:                                                break;
                   10065:                                        case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   10066:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfe;
                   10067:                                                break;
                   10068:                                        case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   10069:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfd;
                   10070:                                                break;
                   10071:                                        case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   10072:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfc;
                   10073:                                                break;
                   10074:                                        case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   10075:                                        case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   10076:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf9;
                   10077:                                                break;
                   10078:                                        case FixedMedia:        // hard disk
                   10079:                                        case RemovableMedia:
                   10080:                                        case Unknown:
                   10081:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf8;
                   10082:                                                break;
                   10083:                                        default:
                   10084:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf0;
                   10085:                                                break;
                   10086:                                        }
                   10087:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0d) = geo.SectorsPerTrack;
                   10088:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0f) = 1;
                   10089:                                        *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x11) = 0;
                   10090:                                        *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x15) = geo.TracksPerCylinder * geo.Cylinders.QuadPart;
                   10091:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x1f) = geo.Cylinders.QuadPart;
                   10092:                                        // 21h  BYTE    device type
                   10093:                                        // 22h  WORD    device attributes (removable or not, etc)
                   10094:                                } else {
                   10095:                                        REG16(AX) = 0x0f; // invalid drive
                   10096:                                        m_CF = 1;
                   10097:                                }
                   10098:                                CloseHandle(hFile);
                   10099:                        } else {
                   10100:                                REG16(AX) = 0x0f; // invalid drive
                   10101:                                m_CF = 1;
                   10102:                        }
                   10103:                } else if(REG8(CL) == 0x66) {
                   10104:                        // get volume serial number
                   10105:                        char path[] = "A:\\";
                   10106:                        char volume_label[MAX_PATH];
                   10107:                        DWORD serial_number = 0;
                   10108:                        char file_system[MAX_PATH];
                   10109:                        
                   10110:                        path[0] = 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   10111:                        
                   10112:                        if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
                   10113:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   10114:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = serial_number;
                   10115:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x06, 0x20, 11);
                   10116:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x06, volume_label, min(strlen(volume_label), 11));
                   10117:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x11, 0x20,  8);
                   10118:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x11, file_system, min(strlen(file_system), 8));
                   10119:                        } else {
                   10120:                                REG16(AX) = 0x0f; // invalid drive
                   10121:                                m_CF = 1;
                   10122:                        }
                   10123:                } else if(REG8(CL) == 0x67) {
                   10124:                        // get access flag
                   10125:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   10126:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 1;
                   10127:                } else if(REG8(CL) == 0x68) {
                   10128:                        // sense media type
                   10129:                        char dev[64];
                   10130:                        sprintf(dev, "\\\\.\\%c:", 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1);
                   10131:                        
                   10132:                        HANDLE hFile = CreateFile(dev, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   10133:                        if(hFile != INVALID_HANDLE_VALUE) {
                   10134:                                DISK_GEOMETRY geo;
                   10135:                                DWORD dwSize;
                   10136:                                if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &geo, sizeof(geo), &dwSize, NULL)) {
                   10137:                                        switch(geo.MediaType) {
                   10138:                                        case F3_720_512:
                   10139:                                        case F5_720_512:
                   10140:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   10141:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02;
                   10142:                                                break;
                   10143:                                        case F3_1Pt44_512:
                   10144:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   10145:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07;
                   10146:                                                break;
                   10147:                                        case F3_2Pt88_512:
                   10148:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   10149:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09;
                   10150:                                                break;
                   10151:                                        default:
                   10152:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x00;
                   10153:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // ???
                   10154:                                                break;
                   10155:                                        }
                   10156:                                } else {
                   10157:                                        REG16(AX) = 0x0f; // invalid drive
                   10158:                                        m_CF = 1;
                   10159:                                }
                   10160:                                CloseHandle(hFile);
                   10161:                        } else {
                   10162:                                REG16(AX) = 0x0f; // invalid drive
                   10163:                                m_CF = 1;
                   10164:                        }
                   10165:                } else if(REG8(CL) == 0x6a) {
                   10166:                        // unlock logical volume
                   10167:                } else if(REG8(CL) == 0x6b) {
                   10168:                        // unlock physical volume
                   10169:                } else {
                   10170:                        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));
                   10171:                        REG16(AX) = 0x01; // invalid function
                   10172:                        m_CF = 1;
                   10173:                }
                   10174:                break;
                   10175:        case 0x0e: // get logical drive map
                   10176:                {
                   10177:                        DWORD bits = 1 << ((REG8(BL) ? REG8(BL) : _getdrive()) - 1);
                   10178:                        if(!(GetLogicalDrives() & bits)) {
                   10179:                                REG16(AX) = 0x0f; // invalid drive
                   10180:                                m_CF = 1;
                   10181:                        } else {
                   10182:                                REG8(AL) = 0;
                   10183:                        }
                   10184:                }
                   10185:                break;
                   10186:        case 0x0f: // set logical drive map
                   10187:                {
                   10188:                        DWORD bits = 1 << ((REG8(BL) ? REG8(BL) : _getdrive()) - 1);
                   10189:                        if(!(GetLogicalDrives() & bits)) {
                   10190:                                REG16(AX) = 0x0f; // invalid drive
                   10191:                                m_CF = 1;
                   10192:                        }
                   10193:                }
                   10194:                break;
                   10195:        case 0x10: // query generic ioctrl capability (handle)
                   10196:                switch(REG8(CL)) {
                   10197:                case 0x45:
                   10198:                case 0x4a:
                   10199:                case 0x65:
                   10200:                case 0x6a:
                   10201:                case 0x7f:
                   10202:                        REG16(AX) = 0x0000; // supported
                   10203:                        break;
                   10204:                default:
                   10205:                        REG8(AL) = 0x01; // ioctl capability not available
                   10206:                        m_CF = 1;
                   10207:                        break;
                   10208:                }
                   10209:                break;
                   10210:        case 0x11: // query generic ioctrl capability (drive)
                   10211:                switch(REG8(CL)) {
                   10212:                case 0x40:
                   10213:                case 0x46:
                   10214:                case 0x4a:
                   10215:                case 0x4b:
                   10216:                case 0x60:
                   10217:                case 0x66:
                   10218:                case 0x67:
                   10219:                case 0x68:
                   10220:                case 0x6a:
                   10221:                case 0x6b:
                   10222:                        REG16(AX) = 0x0000; // supported
                   10223:                        break;
                   10224:                default:
                   10225:                        REG8(AL) = 0x01; // ioctl capability not available
                   10226:                        m_CF = 1;
                   10227:                        break;
                   10228:                }
                   10229:                break;
                   10230:        case 0x12: // determine dos type
                   10231:        case 0x51: // concurrent dos v3.2+ - installation check
                   10232:        case 0x52: // determine dos type/get dr dos versuin
                   10233:                REG16(AX) = 0x01; // this  is not DR-DOS
                   10234:                m_CF = 1;
                   10235:                break;
1.1       root     10236:        default:
1.1.1.22  root     10237:                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     10238:                REG16(AX) = 0x01;
1.1.1.3   root     10239:                m_CF = 1;
1.1       root     10240:                break;
                   10241:        }
                   10242: }
                   10243: 
                   10244: inline void msdos_int_21h_45h()
                   10245: {
                   10246:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10247:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10248:        
1.1.1.20  root     10249:        if(fd < process->max_files && file_handler[fd].valid) {
                   10250:                int dup_fd = _dup(fd);
                   10251:                if(dup_fd != -1) {
                   10252:                        REG16(AX) = dup_fd;
                   10253:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   10254: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   10255:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     10256:                } else {
                   10257:                        REG16(AX) = errno;
1.1.1.3   root     10258:                        m_CF = 1;
1.1       root     10259:                }
                   10260:        } else {
                   10261:                REG16(AX) = 0x06;
1.1.1.3   root     10262:                m_CF = 1;
1.1       root     10263:        }
                   10264: }
                   10265: 
                   10266: inline void msdos_int_21h_46h()
                   10267: {
                   10268:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10269:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   10270:        int dup_fd = REG16(CX);
                   10271:        int tmp_fd = msdos_psp_get_file_table(REG16(CX), current_psp);
1.1       root     10272:        
1.1.1.20  root     10273:        if(REG16(BX) == REG16(CX)) {
                   10274:                REG16(AX) = 0x06;
                   10275:                m_CF = 1;
                   10276:        } else if(fd < process->max_files && file_handler[fd].valid && dup_fd < process->max_files) {
                   10277:                if(tmp_fd < process->max_files && file_handler[tmp_fd].valid) {
                   10278:                        _close(tmp_fd);
                   10279:                        msdos_file_handler_close(tmp_fd);
                   10280:                        msdos_psp_set_file_table(dup_fd, 0x0ff, current_psp);
                   10281:                }
                   10282:                if(_dup2(fd, dup_fd) != -1) {
                   10283:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   10284: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   10285:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     10286:                } else {
                   10287:                        REG16(AX) = errno;
1.1.1.3   root     10288:                        m_CF = 1;
1.1       root     10289:                }
                   10290:        } else {
                   10291:                REG16(AX) = 0x06;
1.1.1.3   root     10292:                m_CF = 1;
1.1       root     10293:        }
                   10294: }
                   10295: 
                   10296: inline void msdos_int_21h_47h(int lfn)
                   10297: {
                   10298:        char path[MAX_PATH];
                   10299:        
                   10300:        if(_getdcwd(REG8(DL), path, MAX_PATH) != NULL) {
                   10301:                if(path[1] == ':') {
                   10302:                        // the returned path does not include a drive or the initial backslash
1.1.1.3   root     10303:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), (lfn ? path : msdos_short_path(path)) + 3);
1.1       root     10304:                } else {
1.1.1.3   root     10305:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), lfn ? path : msdos_short_path(path));
1.1       root     10306:                }
                   10307:        } else {
                   10308:                REG16(AX) = errno;
1.1.1.3   root     10309:                m_CF = 1;
1.1       root     10310:        }
                   10311: }
                   10312: 
                   10313: inline void msdos_int_21h_48h()
                   10314: {
1.1.1.19  root     10315:        int seg, umb_linked;
1.1       root     10316:        
1.1.1.8   root     10317:        if((malloc_strategy & 0xf0) == 0x00) {
1.1.1.19  root     10318:                // unlink umb not to allocate memory in umb
                   10319:                if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   10320:                        msdos_mem_unlink_umb();
                   10321:                }
1.1.1.8   root     10322:                if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   10323:                        REG16(AX) = seg;
                   10324:                } else {
                   10325:                        REG16(AX) = 0x08;
                   10326:                        REG16(BX) = msdos_mem_get_free(first_mcb, 0);
                   10327:                        m_CF = 1;
                   10328:                }
1.1.1.19  root     10329:                if(umb_linked != 0) {
                   10330:                        msdos_mem_link_umb();
                   10331:                }
1.1.1.8   root     10332:        } else if((malloc_strategy & 0xf0) == 0x40) {
                   10333:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   10334:                        REG16(AX) = seg;
                   10335:                } else {
                   10336:                        REG16(AX) = 0x08;
                   10337:                        REG16(BX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   10338:                        m_CF = 1;
                   10339:                }
                   10340:        } else if((malloc_strategy & 0xf0) == 0x80) {
                   10341:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   10342:                        REG16(AX) = seg;
                   10343:                } else if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   10344:                        REG16(AX) = seg;
                   10345:                } else {
                   10346:                        REG16(AX) = 0x08;
                   10347:                        REG16(BX) = max(msdos_mem_get_free(UMB_TOP >> 4, 0), msdos_mem_get_free(first_mcb, 0));
                   10348:                        m_CF = 1;
                   10349:                }
1.1       root     10350:        }
                   10351: }
                   10352: 
                   10353: inline void msdos_int_21h_49h()
                   10354: {
1.1.1.14  root     10355:        int mcb_seg = SREG(ES) - 1;
                   10356:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   10357:        
                   10358:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   10359:                msdos_mem_free(SREG(ES));
                   10360:        } else {
1.1.1.33  root     10361:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.14  root     10362:                m_CF = 1;
                   10363:        }
1.1       root     10364: }
                   10365: 
                   10366: inline void msdos_int_21h_4ah()
                   10367: {
1.1.1.14  root     10368:        int mcb_seg = SREG(ES) - 1;
                   10369:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1       root     10370:        int max_paragraphs;
                   10371:        
1.1.1.14  root     10372:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   10373:                if(msdos_mem_realloc(SREG(ES), REG16(BX), &max_paragraphs)) {
                   10374:                        REG16(AX) = 0x08;
                   10375:                        REG16(BX) = max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs;
                   10376:                        m_CF = 1;
                   10377:                }
                   10378:        } else {
1.1.1.33  root     10379:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.3   root     10380:                m_CF = 1;
1.1       root     10381:        }
                   10382: }
                   10383: 
                   10384: inline void msdos_int_21h_4bh()
                   10385: {
1.1.1.3   root     10386:        char *command = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   10387:        param_block_t *param = (param_block_t *)(mem + SREG_BASE(ES) + REG16(BX));
1.1       root     10388:        
                   10389:        switch(REG8(AL)) {
                   10390:        case 0x00:
                   10391:        case 0x01:
                   10392:                if(msdos_process_exec(command, param, REG8(AL))) {
                   10393:                        REG16(AX) = 0x02;
1.1.1.3   root     10394:                        m_CF = 1;
1.1       root     10395:                }
                   10396:                break;
1.1.1.14  root     10397:        case 0x03:
                   10398:                {
                   10399:                        int fd;
                   10400:                        if((fd = _open(command, _O_RDONLY | _O_BINARY)) == -1) {
                   10401:                                REG16(AX) = 0x02;
                   10402:                                m_CF = 1;
                   10403:                                break;
                   10404:                        }
                   10405:                        int size = _read(fd, file_buffer, sizeof(file_buffer));
                   10406:                        _close(fd);
                   10407:                        
                   10408:                        UINT16 *overlay = (UINT16 *)param;
                   10409:                        
                   10410:                        // check exe header
                   10411:                        exe_header_t *header = (exe_header_t *)file_buffer;
                   10412:                        int header_size = 0;
                   10413:                        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   10414:                                header_size = header->header_size * 16;
                   10415:                                // relocation
                   10416:                                int start_seg = overlay[1];
                   10417:                                for(int i = 0; i < header->relocations; i++) {
                   10418:                                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   10419:                                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   10420:                                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   10421:                                }
                   10422:                        }
                   10423:                        memcpy(mem + (overlay[0] << 4), file_buffer + header_size, size - header_size);
                   10424:                }
                   10425:                break;
1.1       root     10426:        default:
1.1.1.22  root     10427:                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     10428:                REG16(AX) = 0x01;
1.1.1.3   root     10429:                m_CF = 1;
1.1       root     10430:                break;
                   10431:        }
                   10432: }
                   10433: 
                   10434: inline void msdos_int_21h_4ch()
                   10435: {
                   10436:        msdos_process_terminate(current_psp, REG8(AL), 1);
                   10437: }
                   10438: 
                   10439: inline void msdos_int_21h_4dh()
                   10440: {
                   10441:        REG16(AX) = retval;
                   10442: }
                   10443: 
                   10444: inline void msdos_int_21h_4eh()
                   10445: {
                   10446:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     10447:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   10448:        find_t *find = (find_t *)(mem + dta_laddr);
1.1.1.3   root     10449:        char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     10450:        WIN32_FIND_DATA fd;
                   10451:        
1.1.1.14  root     10452:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   10453:        find->find_magic = FIND_MAGIC;
                   10454:        find->dta_index = dtainfo - dtalist;
1.1       root     10455:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     10456:        dtainfo->allowable_mask = REG8(CL);
                   10457:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     10458:        
1.1.1.14  root     10459:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   10460:                dtainfo->allowable_mask &= ~8;
1.1       root     10461:        }
1.1.1.14  root     10462:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   10463:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     10464:                      !msdos_find_file_has_8dot3name(&fd)) {
                   10465:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   10466:                                FindClose(dtainfo->find_handle);
                   10467:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     10468:                                break;
                   10469:                        }
                   10470:                }
                   10471:        }
1.1.1.13  root     10472:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     10473:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   10474:                msdos_find_file_conv_local_time(&fd);
                   10475:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   10476:                find->size = fd.nFileSizeLow;
1.1.1.13  root     10477:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     10478:                REG16(AX) = 0;
1.1.1.14  root     10479:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     10480:                find->attrib = 8;
                   10481:                find->size = 0;
                   10482:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     10483:                dtainfo->allowable_mask &= ~8;
1.1       root     10484:                REG16(AX) = 0;
                   10485:        } else {
                   10486:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     10487:                m_CF = 1;
1.1       root     10488:        }
                   10489: }
                   10490: 
                   10491: inline void msdos_int_21h_4fh()
                   10492: {
                   10493:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     10494:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   10495:        find_t *find = (find_t *)(mem + dta_laddr);
1.1       root     10496:        WIN32_FIND_DATA fd;
                   10497:        
1.1.1.14  root     10498:        if(find->find_magic != FIND_MAGIC || find->dta_index >= MAX_DTAINFO) {
                   10499:                REG16(AX) = 0x12;
                   10500:                m_CF = 1;
                   10501:                return;
                   10502:        }
                   10503:        dtainfo_t *dtainfo = &dtalist[find->dta_index];
1.1.1.13  root     10504:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   10505:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     10506:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     10507:                              !msdos_find_file_has_8dot3name(&fd)) {
                   10508:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   10509:                                        FindClose(dtainfo->find_handle);
                   10510:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     10511:                                        break;
                   10512:                                }
                   10513:                        }
                   10514:                } else {
1.1.1.13  root     10515:                        FindClose(dtainfo->find_handle);
                   10516:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     10517:                }
                   10518:        }
1.1.1.13  root     10519:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     10520:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   10521:                msdos_find_file_conv_local_time(&fd);
                   10522:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   10523:                find->size = fd.nFileSizeLow;
1.1.1.13  root     10524:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     10525:                REG16(AX) = 0;
1.1.1.14  root     10526:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     10527:                find->attrib = 8;
                   10528:                find->size = 0;
                   10529:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     10530:                dtainfo->allowable_mask &= ~8;
1.1       root     10531:                REG16(AX) = 0;
                   10532:        } else {
                   10533:                REG16(AX) = 0x12;
1.1.1.3   root     10534:                m_CF = 1;
1.1       root     10535:        }
                   10536: }
                   10537: 
                   10538: inline void msdos_int_21h_50h()
                   10539: {
1.1.1.8   root     10540:        if(current_psp != REG16(BX)) {
                   10541:                process_t *process = msdos_process_info_get(current_psp);
                   10542:                if(process != NULL) {
                   10543:                        process->psp = REG16(BX);
                   10544:                }
                   10545:                current_psp = REG16(BX);
1.1.1.23  root     10546:                msdos_sda_update(current_psp);
1.1.1.8   root     10547:        }
1.1       root     10548: }
                   10549: 
                   10550: inline void msdos_int_21h_51h()
                   10551: {
                   10552:        REG16(BX) = current_psp;
                   10553: }
                   10554: 
                   10555: inline void msdos_int_21h_52h()
                   10556: {
1.1.1.25  root     10557:        SREG(ES) = DOS_INFO_TOP >> 4;
1.1.1.3   root     10558:        i386_load_segment_descriptor(ES);
1.1.1.25  root     10559:        REG16(BX) = offsetof(dos_info_t, first_dpb);
1.1       root     10560: }
                   10561: 
                   10562: inline void msdos_int_21h_54h()
                   10563: {
                   10564:        process_t *process = msdos_process_info_get(current_psp);
                   10565:        
                   10566:        REG8(AL) = process->verify;
                   10567: }
                   10568: 
                   10569: inline void msdos_int_21h_55h()
                   10570: {
                   10571:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   10572:        
                   10573:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
                   10574:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   10575:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   10576:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   10577:        psp->parent_psp = current_psp;
                   10578: }
                   10579: 
                   10580: inline void msdos_int_21h_56h(int lfn)
                   10581: {
                   10582:        char src[MAX_PATH], dst[MAX_PATH];
1.1.1.3   root     10583:        strcpy(src, msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn));
                   10584:        strcpy(dst, msdos_trimmed_path((char *)(mem + SREG_BASE(ES) + REG16(DI)), lfn));
1.1       root     10585:        
                   10586:        if(rename(src, dst)) {
                   10587:                REG16(AX) = errno;
1.1.1.3   root     10588:                m_CF = 1;
1.1       root     10589:        }
                   10590: }
                   10591: 
                   10592: inline void msdos_int_21h_57h()
                   10593: {
                   10594:        FILETIME time, local;
1.1.1.14  root     10595:        FILETIME *ctime, *atime, *mtime;
1.1.1.21  root     10596:        HANDLE hHandle;
1.1       root     10597:        
1.1.1.21  root     10598:        if((hHandle = (HANDLE)_get_osfhandle(REG16(BX))) == INVALID_HANDLE_VALUE) {
1.1.1.14  root     10599:                REG16(AX) = (UINT16)GetLastError();
                   10600:                m_CF = 1;
                   10601:                return;
                   10602:        }
                   10603:        ctime = atime = mtime = NULL;
                   10604:        
1.1       root     10605:        switch(REG8(AL)) {
                   10606:        case 0x00:
1.1.1.6   root     10607:        case 0x01:
1.1.1.14  root     10608:                mtime = &time;
1.1.1.6   root     10609:                break;
                   10610:        case 0x04:
                   10611:        case 0x05:
1.1.1.14  root     10612:                atime = &time;
1.1       root     10613:                break;
1.1.1.6   root     10614:        case 0x06:
                   10615:        case 0x07:
1.1.1.14  root     10616:                ctime = &time;
                   10617:                break;
                   10618:        default:
1.1.1.22  root     10619:                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     10620:                REG16(AX) = 0x01;
                   10621:                m_CF = 1;
                   10622:                return;
                   10623:        }
                   10624:        if(REG8(AL) & 1) {
1.1       root     10625:                DosDateTimeToFileTime(REG16(DX), REG16(CX), &local);
                   10626:                LocalFileTimeToFileTime(&local, &time);
1.1.1.21  root     10627:                if(!SetFileTime(hHandle, ctime, atime, mtime)) {
1.1       root     10628:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     10629:                        m_CF = 1;
1.1       root     10630:                }
1.1.1.14  root     10631:        } else {
1.1.1.21  root     10632:                if(!GetFileTime(hHandle, ctime, atime, mtime)) {
1.1.1.14  root     10633:                        // assume a device and use the current time
                   10634:                        GetSystemTimeAsFileTime(&time);
                   10635:                }
                   10636:                FileTimeToLocalFileTime(&time, &local);
                   10637:                FileTimeToDosDateTime(&local, &REG16(DX), &REG16(CX));
1.1       root     10638:        }
                   10639: }
                   10640: 
                   10641: inline void msdos_int_21h_58h()
                   10642: {
                   10643:        switch(REG8(AL)) {
                   10644:        case 0x00:
1.1.1.7   root     10645:                REG16(AX) = malloc_strategy;
                   10646:                break;
                   10647:        case 0x01:
1.1.1.24  root     10648: //             switch(REG16(BX)) {
                   10649:                switch(REG8(BL)) {
1.1.1.7   root     10650:                case 0x0000:
                   10651:                case 0x0001:
                   10652:                case 0x0002:
                   10653:                case 0x0040:
                   10654:                case 0x0041:
                   10655:                case 0x0042:
                   10656:                case 0x0080:
                   10657:                case 0x0081:
                   10658:                case 0x0082:
                   10659:                        malloc_strategy = REG16(BX);
1.1.1.23  root     10660:                        msdos_sda_update(current_psp);
1.1.1.7   root     10661:                        break;
                   10662:                default:
1.1.1.22  root     10663:                        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     10664:                        REG16(AX) = 0x01;
                   10665:                        m_CF = 1;
                   10666:                        break;
                   10667:                }
                   10668:                break;
                   10669:        case 0x02:
1.1.1.19  root     10670:                REG8(AL) = msdos_mem_get_umb_linked() ? 1 : 0;
1.1.1.7   root     10671:                break;
                   10672:        case 0x03:
1.1.1.24  root     10673: //             switch(REG16(BX)) {
                   10674:                switch(REG8(BL)) {
1.1.1.7   root     10675:                case 0x0000:
1.1.1.19  root     10676:                        msdos_mem_unlink_umb();
                   10677:                        break;
1.1.1.7   root     10678:                case 0x0001:
1.1.1.19  root     10679:                        msdos_mem_link_umb();
1.1.1.7   root     10680:                        break;
                   10681:                default:
1.1.1.22  root     10682:                        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     10683:                        REG16(AX) = 0x01;
                   10684:                        m_CF = 1;
                   10685:                        break;
                   10686:                }
1.1       root     10687:                break;
                   10688:        default:
1.1.1.22  root     10689:                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     10690:                REG16(AX) = 0x01;
1.1.1.3   root     10691:                m_CF = 1;
1.1       root     10692:                break;
                   10693:        }
                   10694: }
                   10695: 
                   10696: inline void msdos_int_21h_59h()
                   10697: {
1.1.1.23  root     10698:        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   10699:        
                   10700:        REG16(AX) = sda->extended_error_code;
                   10701:        REG8(BH) = sda->error_class;
                   10702:        REG8(BL) = sda->suggested_action;
                   10703:        REG8(CH) = sda->locus_of_last_error;
1.1       root     10704: }
                   10705: 
                   10706: inline void msdos_int_21h_5ah()
                   10707: {
1.1.1.3   root     10708:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     10709:        int len = strlen(path);
                   10710:        char tmp[MAX_PATH];
                   10711:        
                   10712:        if(GetTempFileName(path, "TMP", 0, tmp)) {
                   10713:                int fd = _open(tmp, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   10714:                
                   10715:                SetFileAttributes(tmp, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   10716:                REG16(AX) = fd;
                   10717:                msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     10718:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     10719:                
                   10720:                strcpy(path, tmp);
                   10721:                int dx = REG16(DX) + len;
1.1.1.3   root     10722:                int ds = SREG(DS);
1.1       root     10723:                while(dx > 0xffff) {
                   10724:                        dx -= 0x10;
                   10725:                        ds++;
                   10726:                }
                   10727:                REG16(DX) = dx;
1.1.1.3   root     10728:                SREG(DS) = ds;
                   10729:                i386_load_segment_descriptor(DS);
1.1       root     10730:        } else {
                   10731:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     10732:                m_CF = 1;
1.1       root     10733:        }
                   10734: }
                   10735: 
                   10736: inline void msdos_int_21h_5bh()
                   10737: {
1.1.1.3   root     10738:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     10739:        
1.1.1.24  root     10740:        if(msdos_is_existing_file(path)) {
1.1       root     10741:                // already exists
                   10742:                REG16(AX) = 0x50;
1.1.1.3   root     10743:                m_CF = 1;
1.1       root     10744:        } else {
                   10745:                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   10746:                
                   10747:                if(fd != -1) {
                   10748:                        SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   10749:                        REG16(AX) = fd;
                   10750:                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     10751:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     10752:                } else {
                   10753:                        REG16(AX) = errno;
1.1.1.3   root     10754:                        m_CF = 1;
1.1       root     10755:                }
                   10756:        }
                   10757: }
                   10758: 
                   10759: inline void msdos_int_21h_5ch()
                   10760: {
                   10761:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10762:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10763:        
1.1.1.20  root     10764:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     10765:                if(REG8(AL) == 0 || REG8(AL) == 1) {
1.1.1.35  root     10766:                        static const int modes[2] = {_LK_LOCK, _LK_UNLCK};
1.1.1.20  root     10767:                        UINT32 pos = _tell(fd);
                   10768:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), SEEK_SET);
                   10769:                        if(_locking(fd, modes[REG8(AL)], (REG16(SI) << 16) | REG16(DI))) {
1.1       root     10770:                                REG16(AX) = errno;
1.1.1.3   root     10771:                                m_CF = 1;
1.1       root     10772:                        }
1.1.1.20  root     10773:                        _lseek(fd, pos, SEEK_SET);
1.1.1.26  root     10774:                        
1.1       root     10775:                        // some seconds may be passed in _locking()
1.1.1.35  root     10776:                        REQUEST_HARDWRE_UPDATE();
1.1       root     10777:                } else {
                   10778:                        REG16(AX) = 0x01;
1.1.1.3   root     10779:                        m_CF = 1;
1.1       root     10780:                }
                   10781:        } else {
                   10782:                REG16(AX) = 0x06;
1.1.1.3   root     10783:                m_CF = 1;
1.1       root     10784:        }
                   10785: }
                   10786: 
1.1.1.22  root     10787: inline void msdos_int_21h_5dh()
                   10788: {
                   10789:        switch(REG8(AL)) {
                   10790:        case 0x06: // get address of dos swappable data area
1.1.1.23  root     10791:                SREG(DS) = (SDA_TOP >> 4);
                   10792:                i386_load_segment_descriptor(DS);
                   10793:                REG16(SI) = offsetof(sda_t, crit_error_flag);
                   10794:                REG16(CX) = 0x80;
                   10795:                REG16(DX) = 0x1a;
                   10796:                break;
                   10797:        case 0x0b: // get dos swappable data areas
1.1.1.22  root     10798:                REG16(AX) = 0x01;
                   10799:                m_CF = 1;
                   10800:                break;
                   10801:        case 0x08: // set redirected printer mode
                   10802:        case 0x09: // flush redirected printer output
                   10803:        case 0x0a: // set extended error information
                   10804:                break;
                   10805:        default:
                   10806:                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));
                   10807:                REG16(AX) = 0x01;
                   10808:                m_CF = 1;
                   10809:                break;
                   10810:        }
                   10811: }
                   10812: 
1.1.1.30  root     10813: inline void msdos_int_21h_5fh()
                   10814: {
                   10815:        switch(REG8(AL)) {
                   10816:        case 0x02:
                   10817:                {
                   10818:                        DWORD drives = GetLogicalDrives();
                   10819:                        for(int i = 0, index = 0; i < 26; i++) {
                   10820:                                if(drives & (1 << i)) {
                   10821:                                        char volume[] = "A:\\";
                   10822:                                        volume[0] = 'A' + i;
                   10823:                                        if(GetDriveType(volume) == DRIVE_REMOTE) {
                   10824:                                                if(index == REG16(BX)) {
                   10825:                                                        DWORD dwSize = 128;
                   10826:                                                        volume[2] = '\0';
                   10827:                                                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), volume);
                   10828:                                                        WNetGetConnection(volume, (char *)(mem + SREG_BASE(ES) + REG16(DI)), &dwSize);
                   10829:                                                        REG8(BH) = 0x00; // valid
                   10830:                                                        REG8(BL) = 0x04; // disk drive
                   10831:                                                        REG16(CX) = 0x00;
                   10832:                                                        return;
                   10833:                                                }
                   10834:                                                index++;
                   10835:                                        }
                   10836:                                }
                   10837:                        }
                   10838:                }
                   10839:                REG16(AX) = 0x12; // no more files
                   10840:                m_CF = 1;
                   10841:                break;
                   10842:        default:
                   10843:                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));
                   10844:                REG16(AX) = 0x01;
                   10845:                m_CF = 1;
                   10846:                break;
                   10847:        }
                   10848: }
                   10849: 
1.1       root     10850: inline void msdos_int_21h_60h(int lfn)
                   10851: {
1.1.1.14  root     10852:        char full[MAX_PATH], *path;
                   10853:        
1.1       root     10854:        if(lfn) {
1.1.1.14  root     10855:                char *name;
                   10856:                *full = '\0';
1.1.1.3   root     10857:                GetFullPathName((char *)(mem + SREG_BASE(DS) + REG16(SI)), MAX_PATH, full, &name);
1.1.1.14  root     10858:                switch(REG8(CL)) {
                   10859:                case 1:
                   10860:                        GetShortPathName(full, full, MAX_PATH);
                   10861:                        my_strupr(full);
                   10862:                        break;
                   10863:                case 2:
                   10864:                        GetLongPathName(full, full, MAX_PATH);
                   10865:                        break;
                   10866:                }
                   10867:                path = full;
                   10868:        } else {
                   10869:                path = msdos_short_full_path((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   10870:        }
                   10871:        if(*path != '\0') {
                   10872:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
1.1       root     10873:        } else {
1.1.1.14  root     10874:                REG16(AX) = (UINT16)GetLastError();
                   10875:                m_CF = 1;
1.1       root     10876:        }
                   10877: }
                   10878: 
                   10879: inline void msdos_int_21h_61h()
                   10880: {
                   10881:        REG8(AL) = 0;
                   10882: }
                   10883: 
                   10884: inline void msdos_int_21h_62h()
                   10885: {
                   10886:        REG16(BX) = current_psp;
                   10887: }
                   10888: 
                   10889: inline void msdos_int_21h_63h()
                   10890: {
                   10891:        switch(REG8(AL)) {
                   10892:        case 0x00:
1.1.1.3   root     10893:                SREG(DS) = (DBCS_TABLE >> 4);
                   10894:                i386_load_segment_descriptor(DS);
1.1       root     10895:                REG16(SI) = (DBCS_TABLE & 0x0f);
                   10896:                REG8(AL) = 0x00;
                   10897:                break;
1.1.1.22  root     10898:        case 0x01: // set korean input mode
                   10899:        case 0x02: // get korean input mode
                   10900:                REG8(AL) = 0xff; // not supported
                   10901:                break;
1.1       root     10902:        default:
1.1.1.22  root     10903:                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     10904:                REG16(AX) = 0x01;
1.1.1.3   root     10905:                m_CF = 1;
1.1       root     10906:                break;
                   10907:        }
                   10908: }
                   10909: 
1.1.1.25  root     10910: UINT16 get_extended_country_info(UINT8 func)
1.1       root     10911: {
1.1.1.25  root     10912:        switch(func) {
1.1.1.17  root     10913:        case 0x01:
                   10914:                if(REG16(CX) >= 5) {
1.1.1.19  root     10915:                        UINT8 data[1 + 2 + 2 + 2 + sizeof(country_info_t)];
1.1.1.17  root     10916:                        if(REG16(CX) > sizeof(data))            // cx = actual transfer size
                   10917:                                REG16(CX) = sizeof(data);
                   10918:                        ZeroMemory(data, sizeof(data));
                   10919:                        data[0] = 0x01;
                   10920:                        *(UINT16 *)(data + 1) = REG16(CX) - 3;
1.1.1.19  root     10921:                        *(UINT16 *)(data + 3) = get_country_info((country_info_t*)(data + 7));
1.1.1.17  root     10922:                        *(UINT16 *)(data + 5) = active_code_page;
                   10923:                        memcpy(mem + SREG_BASE(ES) + REG16(DI), data, REG16(CX));
1.1.1.25  root     10924: //                     REG16(AX) = active_code_page;
1.1.1.17  root     10925:                } else {
1.1.1.25  root     10926:                        return(0x08); // insufficient memory
1.1.1.17  root     10927:                }
                   10928:                break;
                   10929:        case 0x02:
                   10930:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   10931:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (UPPERTABLE_TOP & 0x0f);
                   10932:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (UPPERTABLE_TOP >> 4);
1.1.1.25  root     10933: //             REG16(AX) = active_code_page;
1.1.1.17  root     10934:                REG16(CX) = 0x05;
                   10935:                break;
1.1.1.23  root     10936:        case 0x03:
                   10937:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   10938:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (LOWERTABLE_TOP & 0x0f);
                   10939:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (LOWERTABLE_TOP >> 4);
1.1.1.25  root     10940: //             REG16(AX) = active_code_page;
1.1.1.23  root     10941:                REG16(CX) = 0x05;
                   10942:                break;
1.1.1.17  root     10943:        case 0x04:
                   10944:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x04;
                   10945:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_UPPERTABLE_TOP & 0x0f);
                   10946:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_UPPERTABLE_TOP >> 4);
1.1.1.25  root     10947: //             REG16(AX) = active_code_page;
1.1.1.17  root     10948:                REG16(CX) = 0x05;
                   10949:                break;
                   10950:        case 0x05:
                   10951:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x05;
                   10952:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_TERMINATOR_TOP & 0x0f);
                   10953:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_TERMINATOR_TOP >> 4);
1.1.1.25  root     10954: //             REG16(AX) = active_code_page;
1.1.1.17  root     10955:                REG16(CX) = 0x05;
                   10956:                break;
                   10957:        case 0x06:
                   10958:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x06;
                   10959:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (COLLATING_TABLE_TOP & 0x0f);
                   10960:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (COLLATING_TABLE_TOP >> 4);
1.1.1.25  root     10961: //             REG16(AX) = active_code_page;
1.1.1.17  root     10962:                REG16(CX) = 0x05;
                   10963:                break;
1.1       root     10964:        case 0x07:
1.1.1.3   root     10965:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x07;
                   10966:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (DBCS_TOP & 0x0f);
                   10967:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (DBCS_TOP >> 4);
1.1.1.25  root     10968: //             REG16(AX) = active_code_page;
1.1       root     10969:                REG16(CX) = 0x05;
                   10970:                break;
1.1.1.25  root     10971:        default:
                   10972:                return(0x01); // function number invalid
                   10973:        }
                   10974:        return(0x00);
                   10975: }
                   10976: 
                   10977: inline void msdos_int_21h_65h()
                   10978: {
                   10979:        char tmp[0x10000];
                   10980:        
                   10981:        switch(REG8(AL)) {
                   10982:        case 0x01:
                   10983:        case 0x02:
                   10984:        case 0x03:
                   10985:        case 0x04:
                   10986:        case 0x05:
                   10987:        case 0x06:
                   10988:        case 0x07:
                   10989:                {
                   10990:                        UINT16 result = get_extended_country_info(REG8(AL));
                   10991:                        if(result) {
                   10992:                                REG16(AX) = result;
                   10993:                                m_CF = 1;
                   10994:                        } else {
                   10995:                                REG16(AX) = active_code_page; // FIXME: is this correct???
                   10996:                        }
                   10997:                }
                   10998:                break;
1.1       root     10999:        case 0x20:
1.1.1.25  root     11000:        case 0xa0:
1.1.1.19  root     11001:                memset(tmp, 0, sizeof(tmp));
                   11002:                tmp[0] = REG8(DL);
1.1       root     11003:                my_strupr(tmp);
                   11004:                REG8(DL) = tmp[0];
                   11005:                break;
                   11006:        case 0x21:
1.1.1.25  root     11007:        case 0xa1:
1.1       root     11008:                memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     11009:                memcpy(tmp, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     11010:                my_strupr(tmp);
1.1.1.3   root     11011:                memcpy(mem + SREG_BASE(DS) + REG16(DX), tmp, REG16(CX));
1.1       root     11012:                break;
                   11013:        case 0x22:
1.1.1.25  root     11014:        case 0xa2:
1.1.1.3   root     11015:                my_strupr((char *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1       root     11016:                break;
1.1.1.25  root     11017:        case 0x23:
                   11018:                // FIXME: need to check multi-byte (kanji) charactre?
                   11019:                if(REG8(DL) == 'N' || REG8(DL) == 'n' || (REG8(DL) == 0x82 && REG8(DH) == 0x78) || (REG8(DL) == 0x82 && REG8(DH) == 0x99)) {
                   11020:                        // 8278h/8299h: multi-byte (kanji) Y and y
                   11021:                        REG16(AX) = 0x00;
                   11022:                } else if(REG8(DL) == 'Y' || REG8(DL) == 'y' || (REG8(DL) == 0x82 && REG8(DH) == 0x6d) || (REG8(DL) == 0x82 && REG8(DH) == 0x8e)) {
                   11023:                        // 826dh/828eh: multi-byte (kanji) N and n
                   11024:                        REG16(AX) = 0x01;
                   11025:                } else {
                   11026:                        REG16(AX) = 0x02;
                   11027:                }
                   11028:                break;
1.1       root     11029:        default:
1.1.1.22  root     11030:                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     11031:                REG16(AX) = 0x01;
1.1.1.3   root     11032:                m_CF = 1;
1.1       root     11033:                break;
                   11034:        }
                   11035: }
                   11036: 
                   11037: inline void msdos_int_21h_66h()
                   11038: {
                   11039:        switch(REG8(AL)) {
                   11040:        case 0x01:
                   11041:                REG16(BX) = active_code_page;
                   11042:                REG16(DX) = system_code_page;
                   11043:                break;
                   11044:        case 0x02:
                   11045:                if(active_code_page == REG16(BX)) {
                   11046:                        REG16(AX) = 0xeb41;
                   11047:                } else if(_setmbcp(REG16(BX)) == 0) {
                   11048:                        active_code_page = REG16(BX);
1.1.1.17  root     11049:                        msdos_nls_tables_update();
1.1       root     11050:                        REG16(AX) = 0xeb41;
1.1.1.32  root     11051:                        SetConsoleCP(active_code_page);
                   11052:                        SetConsoleOutputCP(active_code_page);
1.1       root     11053:                } else {
                   11054:                        REG16(AX) = 0x25;
1.1.1.3   root     11055:                        m_CF = 1;
1.1       root     11056:                }
                   11057:                break;
                   11058:        default:
1.1.1.22  root     11059:                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     11060:                REG16(AX) = 0x01;
1.1.1.3   root     11061:                m_CF = 1;
1.1       root     11062:                break;
                   11063:        }
                   11064: }
                   11065: 
                   11066: inline void msdos_int_21h_67h()
                   11067: {
                   11068:        process_t *process = msdos_process_info_get(current_psp);
                   11069:        
                   11070:        if(REG16(BX) <= MAX_FILES) {
                   11071:                process->max_files = max(REG16(BX), 20);
                   11072:        } else {
                   11073:                REG16(AX) = 0x08;
1.1.1.3   root     11074:                m_CF = 1;
1.1       root     11075:        }
                   11076: }
                   11077: 
                   11078: inline void msdos_int_21h_68h()
                   11079: {
                   11080:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11081:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     11082:        
1.1.1.20  root     11083:        if(fd < process->max_files && file_handler[fd].valid) {
                   11084:                // fflush(_fdopen(fd, ""));
1.1       root     11085:        } else {
                   11086:                REG16(AX) = 0x06;
1.1.1.3   root     11087:                m_CF = 1;
1.1       root     11088:        }
                   11089: }
                   11090: 
                   11091: inline void msdos_int_21h_69h()
                   11092: {
1.1.1.3   root     11093:        drive_info_t *info = (drive_info_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     11094:        char path[] = "A:\\";
                   11095:        char volume_label[MAX_PATH];
                   11096:        DWORD serial_number = 0;
                   11097:        char file_system[MAX_PATH];
                   11098:        
                   11099:        if(REG8(BL) == 0) {
                   11100:                path[0] = 'A' + _getdrive() - 1;
                   11101:        } else {
                   11102:                path[0] = 'A' + REG8(BL) - 1;
                   11103:        }
                   11104:        
                   11105:        switch(REG8(AL)) {
                   11106:        case 0x00:
                   11107:                if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
                   11108:                        info->info_level = 0;
                   11109:                        info->serial_number = serial_number;
                   11110:                        memset(info->volume_label, 0x20, 11);
                   11111:                        memcpy(info->volume_label, volume_label, min(strlen(volume_label), 11));
                   11112:                        memset(info->file_system, 0x20, 8);
                   11113:                        memcpy(info->file_system, file_system, min(strlen(file_system), 8));
                   11114:                } else {
                   11115:                        REG16(AX) = errno;
1.1.1.3   root     11116:                        m_CF = 1;
1.1       root     11117:                }
                   11118:                break;
                   11119:        case 0x01:
                   11120:                REG16(AX) = 0x03;
1.1.1.3   root     11121:                m_CF = 1;
1.1       root     11122:        }
                   11123: }
                   11124: 
                   11125: inline void msdos_int_21h_6ah()
                   11126: {
                   11127:        REG8(AH) = 0x68;
                   11128:        msdos_int_21h_68h();
                   11129: }
                   11130: 
                   11131: inline void msdos_int_21h_6bh()
                   11132: {
                   11133:        REG8(AL) = 0;
                   11134: }
                   11135: 
                   11136: inline void msdos_int_21h_6ch(int lfn)
                   11137: {
1.1.1.3   root     11138:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(SI)), lfn);
1.1       root     11139:        int mode = REG8(BL) & 0x03;
                   11140:        
                   11141:        if(mode < 0x03) {
1.1.1.29  root     11142:                if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1       root     11143:                        // file exists
                   11144:                        if(REG8(DL) & 1) {
1.1.1.37  root     11145:                                int fd = -1;
1.1.1.11  root     11146:                                UINT16 info;
1.1.1.37  root     11147:                                int sio_port = 0;
                   11148:                                int lpt_port = 0;
1.1       root     11149:                                
1.1.1.11  root     11150:                                if(msdos_is_con_path(path)) {
1.1.1.13  root     11151:                                        fd = msdos_open("CON", file_mode[mode].mode);
1.1.1.11  root     11152:                                        info = 0x80d3;
1.1.1.37  root     11153:                                } else if((sio_port = msdos_is_comm_path(path)) != 0) {
                   11154:                                        fd = msdos_open("NUL", file_mode[mode].mode);
1.1.1.14  root     11155:                                        info = 0x80d3;
1.1.1.37  root     11156:                                        msdos_set_comm_params(sio_port, path);
                   11157:                                } else if((lpt_port = msdos_is_prn_path(path)) != 0) {
                   11158:                                        fd = msdos_open("NUL", file_mode[mode].mode);
                   11159:                                        info = 0xa8c0;
1.1.1.29  root     11160:                                } else if(msdos_is_device_path(path)) {
1.1.1.20  root     11161:                                        fd = msdos_open("NUL", file_mode[mode].mode);
                   11162:                                        info = 0x80d3;
1.1.1.11  root     11163:                                } else {
1.1.1.13  root     11164:                                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     11165:                                        info = msdos_drive_number(path);
                   11166:                                }
1.1       root     11167:                                if(fd != -1) {
                   11168:                                        REG16(AX) = fd;
                   11169:                                        REG16(CX) = 1;
1.1.1.37  root     11170:                                        msdos_file_handler_open(fd, path, _isatty(fd), mode, info, current_psp, sio_port, lpt_port);
1.1.1.20  root     11171:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     11172:                                } else {
                   11173:                                        REG16(AX) = errno;
1.1.1.3   root     11174:                                        m_CF = 1;
1.1       root     11175:                                }
                   11176:                        } else if(REG8(DL) & 2) {
                   11177:                                int attr = GetFileAttributes(path);
1.1.1.37  root     11178:                                int fd = -1;
1.1.1.11  root     11179:                                UINT16 info;
1.1.1.37  root     11180:                                int sio_port = 0;
                   11181:                                int lpt_port = 0;
1.1       root     11182:                                
1.1.1.11  root     11183:                                if(msdos_is_con_path(path)) {
1.1.1.13  root     11184:                                        fd = msdos_open("CON", file_mode[mode].mode);
1.1.1.11  root     11185:                                        info = 0x80d3;
1.1.1.37  root     11186:                                } else if((sio_port = msdos_is_comm_path(path)) != 0) {
                   11187:                                        fd = msdos_open("NUL", file_mode[mode].mode);
1.1.1.14  root     11188:                                        info = 0x80d3;
1.1.1.37  root     11189:                                        msdos_set_comm_params(sio_port, path);
                   11190:                                } else if((lpt_port = msdos_is_prn_path(path)) != 0) {
                   11191:                                        fd = msdos_open("NUL", file_mode[mode].mode);
                   11192:                                        info = 0xa8c0;
1.1.1.29  root     11193:                                } else if(msdos_is_device_path(path)) {
1.1.1.20  root     11194:                                        fd = msdos_open("NUL", file_mode[mode].mode);
                   11195:                                        info = 0x80d3;
1.1       root     11196:                                } else {
                   11197:                                        fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
1.1.1.11  root     11198:                                        info = msdos_drive_number(path);
1.1       root     11199:                                }
                   11200:                                if(fd != -1) {
                   11201:                                        if(attr == -1) {
                   11202:                                                attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   11203:                                        }
                   11204:                                        SetFileAttributes(path, attr);
                   11205:                                        REG16(AX) = fd;
                   11206:                                        REG16(CX) = 3;
1.1.1.37  root     11207:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, info, current_psp, sio_port, lpt_port);
1.1.1.20  root     11208:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     11209:                                } else {
                   11210:                                        REG16(AX) = errno;
1.1.1.3   root     11211:                                        m_CF = 1;
1.1       root     11212:                                }
                   11213:                        } else {
                   11214:                                REG16(AX) = 0x50;
1.1.1.3   root     11215:                                m_CF = 1;
1.1       root     11216:                        }
                   11217:                } else {
                   11218:                        // file not exists
                   11219:                        if(REG8(DL) & 0x10) {
                   11220:                                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   11221:                                
                   11222:                                if(fd != -1) {
                   11223:                                        SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   11224:                                        REG16(AX) = fd;
                   11225:                                        REG16(CX) = 2;
                   11226:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     11227:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     11228:                                } else {
                   11229:                                        REG16(AX) = errno;
1.1.1.3   root     11230:                                        m_CF = 1;
1.1       root     11231:                                }
                   11232:                        } else {
                   11233:                                REG16(AX) = 0x02;
1.1.1.3   root     11234:                                m_CF = 1;
1.1       root     11235:                        }
                   11236:                }
                   11237:        } else {
                   11238:                REG16(AX) = 0x0c;
1.1.1.3   root     11239:                m_CF = 1;
1.1       root     11240:        }
                   11241: }
                   11242: 
                   11243: inline void msdos_int_21h_710dh()
                   11244: {
                   11245:        // reset drive
                   11246: }
                   11247: 
1.1.1.17  root     11248: inline void msdos_int_21h_7141h(int lfn)
                   11249: {
                   11250:        if(REG16(SI) == 0) {
                   11251:                msdos_int_21h_41h(lfn);
                   11252:                return;
                   11253:        }
                   11254:        if(REG16(SI) != 1) {
                   11255:                REG16(AX) = 5;
                   11256:                m_CF = 1;
                   11257:        }
                   11258:        /* wild card and matching attributes... */
                   11259:        char tmp[MAX_PATH * 2];
                   11260:        // copy search pathname (and quick check overrun)
                   11261:        ZeroMemory(tmp, sizeof(tmp));
                   11262:        tmp[MAX_PATH - 1] = '\0';
                   11263:        tmp[MAX_PATH] = 1;
                   11264:        strncpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(DX)), MAX_PATH);
                   11265:        
                   11266:        if(tmp[MAX_PATH - 1] != '\0' || tmp[MAX_PATH] != 1) {
                   11267:                REG16(AX) = 1;
                   11268:                m_CF = 1;
                   11269:                return;
                   11270:        }
                   11271:        for(char *s = tmp; *s; ++s) {
                   11272:                if(*s == '/') {
                   11273:                        *s = '\\';
                   11274:                }
                   11275:        }
                   11276:        char *tmp_name = (char *)_mbsrchr((unsigned char *)tmp, '\\');
                   11277:        if(tmp_name) {
                   11278:                ++tmp_name;
                   11279:        } else {
                   11280:                tmp_name = strchr(tmp, ':');
                   11281:                tmp_name = tmp_name ? tmp_name + 1 : tmp;
                   11282:        }
                   11283:        
                   11284:        WIN32_FIND_DATAA fd;
                   11285:        HANDLE fh = FindFirstFileA(tmp, &fd);
                   11286:        if(fh == INVALID_HANDLE_VALUE) {
                   11287:                REG16(AX) = 2;
                   11288:                m_CF = 1;
                   11289:                return;
                   11290:        }
                   11291:        do {
                   11292:                if(msdos_find_file_check_attribute(fd.dwFileAttributes, REG8(CL), REG8(CH)) && msdos_find_file_has_8dot3name(&fd)) {
                   11293:                        strcpy(tmp_name, fd.cFileName);
                   11294:                        if(remove(msdos_trimmed_path(tmp, lfn))) {
                   11295:                                REG16(AX) = 5;
                   11296:                                m_CF = 1;
                   11297:                                break;
                   11298:                        }
                   11299:                }
                   11300:        } while(FindNextFileA(fh, &fd));
                   11301:        if(!m_CF) {
                   11302:                if(GetLastError() != ERROR_NO_MORE_FILES) {
                   11303:                        m_CF = 1;
                   11304:                        REG16(AX) = 2;
                   11305:                }
                   11306:        }
                   11307:        FindClose(fh);
                   11308: }
                   11309: 
1.1       root     11310: inline void msdos_int_21h_714eh()
                   11311: {
                   11312:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     11313:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
                   11314:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     11315:        WIN32_FIND_DATA fd;
                   11316:        
1.1.1.13  root     11317:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   11318:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   11319:                FindClose(dtainfo->find_handle);
                   11320:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11321:        }
                   11322:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     11323:        dtainfo->allowable_mask = REG8(CL);
                   11324:        dtainfo->required_mask = REG8(CH);
                   11325:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     11326:        
1.1.1.14  root     11327:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   11328:                dtainfo->allowable_mask &= ~8;
1.1       root     11329:        }
1.1.1.14  root     11330:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   11331:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13  root     11332:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   11333:                                FindClose(dtainfo->find_handle);
                   11334:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11335:                                break;
                   11336:                        }
                   11337:                }
                   11338:        }
1.1.1.13  root     11339:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     11340:                find->attrib = fd.dwFileAttributes;
                   11341:                msdos_find_file_conv_local_time(&fd);
                   11342:                if(REG16(SI) == 0) {
                   11343:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   11344:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   11345:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   11346:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   11347:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   11348:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   11349:                } else {
                   11350:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   11351:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   11352:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   11353:                }
                   11354:                find->size_hi = fd.nFileSizeHigh;
                   11355:                find->size_lo = fd.nFileSizeLow;
                   11356:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     11357:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     11358:                REG16(AX) = dtainfo - dtalist + 1;
                   11359:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     11360:                // volume label
                   11361:                find->attrib = 8;
                   11362:                find->size_hi = find->size_lo = 0;
                   11363:                strcpy(find->full_name, process->volume_label);
                   11364:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     11365:                dtainfo->allowable_mask &= ~8;
                   11366:                REG16(AX) = dtainfo - dtalist + 1;
1.1       root     11367:        } else {
                   11368:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     11369:                m_CF = 1;
1.1       root     11370:        }
                   11371: }
                   11372: 
                   11373: inline void msdos_int_21h_714fh()
                   11374: {
                   11375:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     11376:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     11377:        WIN32_FIND_DATA fd;
                   11378:        
1.1.1.14  root     11379:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   11380:                REG16(AX) = 6;
1.1.1.13  root     11381:                m_CF = 1;
                   11382:                return;
                   11383:        }
1.1.1.14  root     11384:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     11385:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   11386:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     11387:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13  root     11388:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   11389:                                        FindClose(dtainfo->find_handle);
                   11390:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11391:                                        break;
                   11392:                                }
                   11393:                        }
                   11394:                } else {
1.1.1.13  root     11395:                        FindClose(dtainfo->find_handle);
                   11396:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11397:                }
                   11398:        }
1.1.1.13  root     11399:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     11400:                find->attrib = fd.dwFileAttributes;
                   11401:                msdos_find_file_conv_local_time(&fd);
                   11402:                if(REG16(SI) == 0) {
                   11403:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   11404:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   11405:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   11406:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   11407:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   11408:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   11409:                } else {
                   11410:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   11411:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   11412:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   11413:                }
                   11414:                find->size_hi = fd.nFileSizeHigh;
                   11415:                find->size_lo = fd.nFileSizeLow;
                   11416:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     11417:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     11418:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     11419:                // volume label
                   11420:                find->attrib = 8;
                   11421:                find->size_hi = find->size_lo = 0;
                   11422:                strcpy(find->full_name, process->volume_label);
                   11423:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     11424:                dtainfo->allowable_mask &= ~8;
1.1       root     11425:        } else {
                   11426:                REG16(AX) = 0x12;
1.1.1.3   root     11427:                m_CF = 1;
1.1       root     11428:        }
                   11429: }
                   11430: 
                   11431: inline void msdos_int_21h_71a0h()
                   11432: {
                   11433:        DWORD max_component_len, file_sys_flag;
                   11434:        
1.1.1.14  root     11435:        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))) {
                   11436:                REG16(BX) = (UINT16)file_sys_flag & (FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES | FILE_UNICODE_ON_DISK | FILE_VOLUME_IS_COMPRESSED);
                   11437:                REG16(BX) |= 0x4000;                            // supports LFN functions
1.1       root     11438:                REG16(CX) = (UINT16)max_component_len;          // 255
                   11439:                REG16(DX) = (UINT16)max_component_len + 5;      // 260
                   11440:        } else {
                   11441:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     11442:                m_CF = 1;
1.1       root     11443:        }
                   11444: }
                   11445: 
                   11446: inline void msdos_int_21h_71a1h()
                   11447: {
1.1.1.14  root     11448:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   11449:                REG16(AX) = 6;
1.1.1.13  root     11450:                m_CF = 1;
                   11451:                return;
                   11452:        }
1.1.1.14  root     11453:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     11454:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   11455:                FindClose(dtainfo->find_handle);
                   11456:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     11457:        }
                   11458: }
                   11459: 
                   11460: inline void msdos_int_21h_71a6h()
                   11461: {
                   11462:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     11463:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   11464:        
1.1.1.3   root     11465:        UINT8 *buffer = (UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     11466:        struct _stat64 status;
                   11467:        DWORD serial_number = 0;
                   11468:        
1.1.1.20  root     11469:        if(fd < process->max_files && file_handler[fd].valid) {
                   11470:                if(_fstat64(fd, &status) == 0) {
                   11471:                        if(file_handler[fd].path[1] == ':') {
1.1       root     11472:                                // NOTE: we need to consider the network file path "\\host\share\"
                   11473:                                char volume[] = "A:\\";
1.1.1.20  root     11474:                                volume[0] = file_handler[fd].path[1];
1.1       root     11475:                                GetVolumeInformation(volume, NULL, 0, &serial_number, NULL, NULL, NULL, 0);
                   11476:                        }
1.1.1.20  root     11477:                        *(UINT32 *)(buffer + 0x00) = GetFileAttributes(file_handler[fd].path);
1.1       root     11478:                        *(UINT32 *)(buffer + 0x04) = (UINT32)(status.st_ctime & 0xffffffff);
                   11479:                        *(UINT32 *)(buffer + 0x08) = (UINT32)((status.st_ctime >> 32) & 0xffffffff);
                   11480:                        *(UINT32 *)(buffer + 0x0c) = (UINT32)(status.st_atime & 0xffffffff);
                   11481:                        *(UINT32 *)(buffer + 0x10) = (UINT32)((status.st_atime >> 32) & 0xffffffff);
                   11482:                        *(UINT32 *)(buffer + 0x14) = (UINT32)(status.st_mtime & 0xffffffff);
                   11483:                        *(UINT32 *)(buffer + 0x18) = (UINT32)((status.st_mtime >> 32) & 0xffffffff);
                   11484:                        *(UINT32 *)(buffer + 0x1c) = serial_number;
                   11485:                        *(UINT32 *)(buffer + 0x20) = (UINT32)((status.st_size >> 32) & 0xffffffff);
                   11486:                        *(UINT32 *)(buffer + 0x24) = (UINT32)(status.st_size & 0xffffffff);
                   11487:                        *(UINT32 *)(buffer + 0x28) = status.st_nlink;
1.1.1.14  root     11488:                        // this is dummy id and it will be changed when it is reopened...
1.1       root     11489:                        *(UINT32 *)(buffer + 0x2c) = 0;
1.1.1.20  root     11490:                        *(UINT32 *)(buffer + 0x30) = file_handler[fd].id;
1.1       root     11491:                } else {
                   11492:                        REG16(AX) = errno;
1.1.1.3   root     11493:                        m_CF = 1;
1.1       root     11494:                }
                   11495:        } else {
                   11496:                REG16(AX) = 0x06;
1.1.1.3   root     11497:                m_CF = 1;
1.1       root     11498:        }
                   11499: }
                   11500: 
                   11501: inline void msdos_int_21h_71a7h()
                   11502: {
                   11503:        switch(REG8(BL)) {
                   11504:        case 0x00:
1.1.1.3   root     11505:                if(!FileTimeToDosDateTime((FILETIME *)(mem + SREG_BASE(DS) + REG16(SI)), &REG16(DX), &REG16(CX))) {
1.1       root     11506:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     11507:                        m_CF = 1;
1.1       root     11508:                }
                   11509:                break;
                   11510:        case 0x01:
                   11511:                // NOTE: we need to check BH that shows 10-millisecond untils past time in CX
1.1.1.3   root     11512:                if(!DosDateTimeToFileTime(REG16(DX), REG16(CX), (FILETIME *)(mem + SREG_BASE(ES) + REG16(DI)))) {
1.1       root     11513:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     11514:                        m_CF = 1;
1.1       root     11515:                }
                   11516:                break;
                   11517:        default:
1.1.1.22  root     11518:                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     11519:                REG16(AX) = 0x01;
1.1.1.3   root     11520:                m_CF = 1;
1.1       root     11521:                break;
                   11522:        }
                   11523: }
                   11524: 
                   11525: inline void msdos_int_21h_71a8h()
                   11526: {
                   11527:        if(REG8(DH) == 0) {
                   11528:                char tmp[MAX_PATH], fcb[MAX_PATH];
1.1.1.3   root     11529:                strcpy(tmp, msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     11530:                memset(fcb, 0x20, sizeof(fcb));
                   11531:                int len = strlen(tmp);
1.1.1.21  root     11532:                for(int i = 0, pos = 0; i < len; i++) {
1.1       root     11533:                        if(tmp[i] == '.') {
                   11534:                                pos = 8;
                   11535:                        } else {
                   11536:                                if(msdos_lead_byte_check(tmp[i])) {
                   11537:                                        fcb[pos++] = tmp[i++];
                   11538:                                }
                   11539:                                fcb[pos++] = tmp[i];
                   11540:                        }
                   11541:                }
1.1.1.3   root     11542:                memcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), fcb, 11);
1.1       root     11543:        } else {
1.1.1.3   root     11544:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     11545:        }
                   11546: }
                   11547: 
1.1.1.22  root     11548: inline void msdos_int_21h_71aah()
                   11549: {
                   11550:        char drv[] = "A:", path[MAX_PATH];
                   11551:        char *hoge=(char *)(mem + SREG_BASE(DS) + REG16(DX));
                   11552:        
                   11553:        if(REG8(BL) == 0) {
                   11554:                drv[0] = 'A' + _getdrive() - 1;
                   11555:        } else {
                   11556:                drv[0] = 'A' + REG8(BL) - 1;
                   11557:        }
                   11558:        switch(REG8(BH)) {
                   11559:        case 0x00:
                   11560:                if(DefineDosDevice(0, drv, (char *)(mem + SREG_BASE(DS) + REG16(DX))) == 0) {
                   11561:                        DWORD bits = 1 << ((REG8(BL) ? REG8(BL) : _getdrive()) - 1);
                   11562:                        if(GetLogicalDrives() & bits) {
                   11563:                                REG16(AX) = 0x0f; // invalid drive
                   11564:                        } else {
                   11565:                                REG16(AX) = 0x03; // path not found
                   11566:                        }
                   11567:                        m_CF = 1;
                   11568:                }
                   11569:                break;
                   11570:        case 0x01:
                   11571:                if(DefineDosDevice(DDD_REMOVE_DEFINITION, drv, NULL) == 0) {
                   11572:                        REG16(AX) = 0x0f; // invalid drive
                   11573:                        m_CF = 1;
                   11574:                }
                   11575:                break;
                   11576:        case 0x02:
                   11577:                if(QueryDosDevice(drv, path, MAX_PATH) == 0) {
                   11578:                        REG16(AX) = 0x0f; // invalid drive
                   11579:                        m_CF = 1;
                   11580:                } else if(strncmp(path, "\\??\\", 4) != 0) {
                   11581:                        REG16(AX) = 0x0f; // invalid drive
                   11582:                        m_CF = 1;
                   11583:                } else {
                   11584:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(DX)), path + 4);
                   11585:                }
                   11586:                break;
                   11587:        default:
                   11588:                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));
                   11589:                REG16(AX) = 0x01;
                   11590:                m_CF = 1;
                   11591:                break;
                   11592:        }
                   11593: }
                   11594: 
1.1.1.14  root     11595: inline void msdos_int_21h_7300h()
                   11596: {
                   11597:        if(REG8(AL) == 0) {
                   11598:                REG8(AL) = REG8(CL);
                   11599:                REG8(AH) = 0;
                   11600:        } else {
                   11601:                REG16(AX) = 0x01;
                   11602:                m_CF = 1;
                   11603:        }
                   11604: }
                   11605: 
                   11606: inline void msdos_int_21h_7302h()
                   11607: {
                   11608:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   11609:        UINT16 seg, ofs;
                   11610:        
                   11611:        if(REG16(CX) < 0x3f) {
                   11612:                REG8(AL) = 0x18;
                   11613:                m_CF = 1;
                   11614:        } else if(!msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   11615:                REG8(AL) = 0xff;
                   11616:                m_CF = 1;
                   11617:        } else {
                   11618:                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 2, mem + (seg << 4) + ofs, sizeof(dpb_t));
                   11619:        }
                   11620: }
                   11621: 
1.1       root     11622: inline void msdos_int_21h_7303h()
                   11623: {
1.1.1.3   root     11624:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   11625:        ext_space_info_t *info = (ext_space_info_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     11626:        DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   11627:        
                   11628:        if(GetDiskFreeSpace(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
                   11629:                info->size_of_structure = sizeof(ext_space_info_t);
                   11630:                info->structure_version = 0;
                   11631:                info->sectors_per_cluster = sectors_per_cluster;
                   11632:                info->bytes_per_sector = bytes_per_sector;
                   11633:                info->available_clusters_on_drive = free_clusters;
                   11634:                info->total_clusters_on_drive = total_clusters;
                   11635:                info->available_sectors_on_drive = sectors_per_cluster * free_clusters;
                   11636:                info->total_sectors_on_drive = sectors_per_cluster * total_clusters;
                   11637:                info->available_allocation_units = free_clusters;       // ???
                   11638:                info->total_allocation_units = total_clusters;          // ???
                   11639:        } else {
                   11640:                REG16(AX) = errno;
1.1.1.3   root     11641:                m_CF = 1;
1.1       root     11642:        }
                   11643: }
                   11644: 
1.1.1.30  root     11645: inline void msdos_int_21h_dbh()
                   11646: {
                   11647:        // Novell NetWare - Workstation - Get Number of Local Drives
                   11648:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   11649:        REG8(AL) = dos_info->last_drive;
                   11650: }
                   11651: 
                   11652: inline void msdos_int_21h_dch()
                   11653: {
                   11654:        // Novell NetWare - Connection Services - Get Connection Number
                   11655:        REG8(AL) = 0x00;
                   11656: }
                   11657: 
1.1.1.32  root     11658: inline void msdos_int_24h()
                   11659: {
                   11660:        const char *message = NULL;
                   11661:        int key = 0;
                   11662:        
                   11663:        for(int i = 0; i < array_length(critical_error_table); i++) {
                   11664:                if(critical_error_table[i].code == (REG16(DI) & 0xff) || critical_error_table[i].code == (UINT16)-1) {
                   11665:                        if(active_code_page == 932) {
                   11666:                                message = critical_error_table[i].message_japanese;
                   11667:                        }
                   11668:                        if(message == NULL) {
                   11669:                                message = critical_error_table[i].message_english;
                   11670:                        }
                   11671:                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   11672:                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   11673:                        
                   11674:                        SREG(ES) = WORK_TOP >> 4;
                   11675:                        i386_load_segment_descriptor(ES);
                   11676:                        REG16(DI) = 0x0000;
                   11677:                        break;
                   11678:                }
                   11679:        }
                   11680:        fprintf(stderr, "\n%s", message);
                   11681:        if(!(REG8(AH) & 0x80)) {
                   11682:                if(REG8(AH) & 0x01) {
                   11683:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�������ݒ� �h���C�u" : "writing drive", 'A' + REG8(AL));
                   11684:                } else {
                   11685:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�ǂݎ�蒆 �h���C�u" : "reading drive", 'A' + REG8(AL));
                   11686:                }
                   11687:        }
                   11688:        fprintf(stderr, "\n");
                   11689:        
1.1.1.33  root     11690:        {
1.1.1.32  root     11691:                fprintf(stderr, "%s",   (active_code_page == 932) ? "���~ (A)" : "Abort");
1.1.1.33  root     11692:        }
1.1.1.32  root     11693:        if(REG8(AH) & 0x10) {
                   11694:                fprintf(stderr, ", %s", (active_code_page == 932) ? "�Ď��s (R)" : "Retry");
                   11695:        }
                   11696:        if(REG8(AH) & 0x20) {
                   11697:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���� (I)" : "Ignore");
                   11698:        }
                   11699:        if(REG8(AH) & 0x08) {
                   11700:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���s (F)" : "Fail");
                   11701:        }
                   11702:        fprintf(stderr, "? ");
                   11703:        
                   11704:        while(1) {
                   11705:                while(!_kbhit()) {
                   11706:                        Sleep(10);
                   11707:                }
                   11708:                key = _getch();
                   11709:                
                   11710:                if(key == 'I' || key == 'i') {
                   11711:                        if(REG8(AH) & 0x20) {
                   11712:                                REG8(AL) = 0;
                   11713:                                break;
                   11714:                        }
                   11715:                } else if(key == 'R' || key == 'r') {
                   11716:                        if(REG8(AH) & 0x10) {
                   11717:                                REG8(AL) = 1;
                   11718:                                break;
                   11719:                        }
                   11720:                } else if(key == 'A' || key == 'a') {
                   11721:                        REG8(AL) = 2;
                   11722:                        break;
                   11723:                } else if(key == 'F' || key == 'f') {
                   11724:                        if(REG8(AH) & 0x08) {
                   11725:                                REG8(AL) = 3;
                   11726:                                break;
                   11727:                        }
                   11728:                }
                   11729:        }
                   11730:        fprintf(stderr, "%c\n", key);
                   11731: }
                   11732: 
1.1       root     11733: inline void msdos_int_25h()
                   11734: {
                   11735:        UINT16 seg, ofs;
                   11736:        DWORD dwSize;
                   11737:        
1.1.1.3   root     11738: #if defined(HAS_I386)
                   11739:        I386OP(pushf)();
                   11740: #else
                   11741:        PREFIX86(_pushf());
                   11742: #endif
1.1       root     11743:        
                   11744:        if(!(REG8(AL) < 26)) {
                   11745:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     11746:                m_CF = 1;
1.1       root     11747:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   11748:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     11749:                m_CF = 1;
1.1       root     11750:        } else {
                   11751:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   11752:                char dev[64];
                   11753:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   11754:                
                   11755:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   11756:                if(hFile == INVALID_HANDLE_VALUE) {
                   11757:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     11758:                        m_CF = 1;
1.1       root     11759:                } else {
1.1.1.19  root     11760:                        UINT32 top_sector  = REG16(DX);
                   11761:                        UINT16 sector_num  = REG16(CX);
                   11762:                        UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   11763:                        
                   11764:                        if(sector_num == 0xffff) {
                   11765:                                top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   11766:                                sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   11767:                                UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   11768:                                UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   11769:                                buffer_addr = (seg << 4) + ofs;
                   11770:                        }
                   11771: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   11772: //                             REG8(AL) = 0x02; // drive not ready
                   11773: //                             m_CF = 1;
                   11774: //                     } else 
                   11775:                        if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     11776:                                REG8(AL) = 0x08; // sector not found
1.1.1.3   root     11777:                                m_CF = 1;
1.1.1.19  root     11778:                        } else if(ReadFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     11779:                                REG8(AL) = 0x0b; // read error
1.1.1.3   root     11780:                                m_CF = 1;
1.1       root     11781:                        }
                   11782:                        CloseHandle(hFile);
                   11783:                }
                   11784:        }
                   11785: }
                   11786: 
                   11787: inline void msdos_int_26h()
                   11788: {
                   11789:        // this operation may cause serious damage for drives, so always returns error...
                   11790:        UINT16 seg, ofs;
                   11791:        DWORD dwSize;
                   11792:        
1.1.1.3   root     11793: #if defined(HAS_I386)
                   11794:        I386OP(pushf)();
                   11795: #else
                   11796:        PREFIX86(_pushf());
                   11797: #endif
1.1       root     11798:        
                   11799:        if(!(REG8(AL) < 26)) {
                   11800:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     11801:                m_CF = 1;
1.1       root     11802:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   11803:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     11804:                m_CF = 1;
1.1       root     11805:        } else {
                   11806:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   11807:                char dev[64];
                   11808:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   11809:                
                   11810:                if(dpb->media_type == 0xf8) {
                   11811:                        // this drive is not a floppy
1.1.1.6   root     11812: //                     if(!(((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag & 0x40)) {
                   11813: //                             fatalerror("This application tried the absolute disk write to drive %c:\n", 'A' + REG8(AL));
                   11814: //                     }
1.1       root     11815:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     11816:                        m_CF = 1;
1.1       root     11817:                } else {
                   11818:                        HANDLE hFile = CreateFile(dev, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   11819:                        if(hFile == INVALID_HANDLE_VALUE) {
                   11820:                                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     11821:                                m_CF = 1;
1.1       root     11822:                        } else {
1.1.1.19  root     11823:                                UINT32 top_sector  = REG16(DX);
                   11824:                                UINT16 sector_num  = REG16(CX);
                   11825:                                UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   11826:                                
                   11827:                                if(sector_num == 0xffff) {
                   11828:                                        top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   11829:                                        sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   11830:                                        UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   11831:                                        UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   11832:                                        buffer_addr = (seg << 4) + ofs;
                   11833:                                }
1.1       root     11834:                                if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   11835:                                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     11836:                                        m_CF = 1;
1.1.1.19  root     11837:                                } else if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     11838:                                        REG8(AL) = 0x08; // sector not found
1.1.1.3   root     11839:                                        m_CF = 1;
1.1.1.19  root     11840:                                } else if(WriteFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     11841:                                        REG8(AL) = 0x0a; // write error
1.1.1.3   root     11842:                                        m_CF = 1;
1.1       root     11843:                                }
                   11844:                                CloseHandle(hFile);
                   11845:                        }
                   11846:                }
                   11847:        }
                   11848: }
                   11849: 
                   11850: inline void msdos_int_27h()
                   11851: {
1.1.1.29  root     11852:        int paragraphs = (min(REG16(DX), 0xfff0) + 15) >> 4;
                   11853:        try {
                   11854:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   11855:        } catch(...) {
                   11856:                // recover the broken mcb
                   11857:                int mcb_seg = SREG(CS) - 1;
                   11858:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     11859:                
1.1.1.29  root     11860:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     11861:                        mcb->mz = 'M';
                   11862:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   11863:                        
1.1.1.29  root     11864:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.39  root     11865:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC");
1.1.1.29  root     11866:                        } else {
1.1.1.39  root     11867:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC");
1.1.1.29  root     11868:                        }
                   11869:                } else {
                   11870:                        mcb->mz = 'Z';
1.1.1.30  root     11871:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     11872:                }
                   11873:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   11874:        }
1.1.1.3   root     11875:        msdos_process_terminate(SREG(CS), retval | 0x300, 0);
1.1       root     11876: }
                   11877: 
                   11878: inline void msdos_int_29h()
                   11879: {
1.1.1.14  root     11880: #if 1
                   11881:        // need to check escape sequences
1.1       root     11882:        msdos_putch(REG8(AL));
1.1.1.14  root     11883: #else
                   11884:        DWORD num;
                   11885:        vram_flush();
1.1.1.23  root     11886:        WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), &REG8(AL), 1, &num, NULL);
1.1.1.14  root     11887:        cursor_moved = true;
                   11888: #endif
1.1       root     11889: }
                   11890: 
                   11891: inline void msdos_int_2eh()
                   11892: {
                   11893:        char tmp[MAX_PATH], command[MAX_PATH], opt[MAX_PATH];
                   11894:        memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     11895:        strcpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
1.1       root     11896:        char *token = my_strtok(tmp, " ");
                   11897:        strcpy(command, token);
                   11898:        strcpy(opt, token + strlen(token) + 1);
                   11899:        
                   11900:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   11901:        param->env_seg = 0;
                   11902:        param->cmd_line.w.l = 44;
                   11903:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   11904:        param->fcb1.w.l = 24;
                   11905:        param->fcb1.w.h = (WORK_TOP >> 4);
                   11906:        param->fcb2.w.l = 24;
                   11907:        param->fcb2.w.h = (WORK_TOP >> 4);
                   11908:        
                   11909:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   11910:        
                   11911:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   11912:        cmd_line->len = strlen(opt);
                   11913:        strcpy(cmd_line->cmd, opt);
                   11914:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   11915:        
1.1.1.28  root     11916:        try {
                   11917:                if(msdos_process_exec(command, param, 0)) {
                   11918:                        REG16(AX) = 0xffff; // error before processing command
                   11919:                } else {
                   11920:                        // set flag to set retval to ax when the started process is terminated
                   11921:                        process_t *process = msdos_process_info_get(current_psp);
                   11922:                        process->called_by_int2eh = true;
                   11923:                }
                   11924:        } catch(...) {
                   11925:                REG16(AX) = 0xffff; // error before processing command
                   11926:        }
1.1       root     11927: }
                   11928: 
1.1.1.29  root     11929: inline void msdos_int_2fh_05h()
                   11930: {
                   11931:        switch(REG8(AL)) {
                   11932:        case 0x00:
1.1.1.32  root     11933:                REG8(AL) = 0xff;
                   11934:                break;
                   11935:        case 0x01:
                   11936:        case 0x02:
                   11937:                for(int i = 0; i < array_length(standard_error_table); i++) {
                   11938:                        if(standard_error_table[i].code == REG16(BX) || standard_error_table[i].code == (UINT16)-1) {
                   11939:                                const char *message = NULL;
                   11940:                                if(active_code_page == 932) {
                   11941:                                        message = standard_error_table[i].message_japanese;
                   11942:                                }
                   11943:                                if(message == NULL) {
                   11944:                                        message = standard_error_table[i].message_english;
                   11945:                                }
                   11946:                                strcpy((char *)(mem + WORK_TOP), message);
                   11947:                                
                   11948:                                SREG(ES) = WORK_TOP >> 4;
                   11949:                                i386_load_segment_descriptor(ES);
                   11950:                                REG16(DI) = 0x0000;
                   11951:                                REG8(AL) = 0x01;
                   11952:                                break;
                   11953:                        }
                   11954:                }
1.1.1.29  root     11955:                break;
                   11956:        default:
                   11957:                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));
                   11958:                m_CF = 1;
                   11959:        }
                   11960: }
                   11961: 
1.1.1.22  root     11962: inline void msdos_int_2fh_11h()
                   11963: {
                   11964:        switch(REG8(AL)) {
                   11965:        case 0x00:
1.1.1.29  root     11966:                if(i386_read_stack() == 0xdada) {
                   11967:                        // MSCDEX is not installed
                   11968: //                     REG8(AL) = 0x00;
                   11969:                } else {
                   11970:                        // Network Redirector is not installed
                   11971: //                     REG8(AL) = 0x00;
                   11972:                }
1.1.1.22  root     11973:                break;
                   11974:        default:
                   11975:                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     11976:                REG16(AX) = 0x49; //  network software not installed
1.1.1.22  root     11977:                m_CF = 1;
                   11978:                break;
                   11979:        }
                   11980: }
                   11981: 
1.1.1.21  root     11982: inline void msdos_int_2fh_12h()
                   11983: {
                   11984:        switch(REG8(AL)) {
1.1.1.22  root     11985:        case 0x00:
1.1.1.29  root     11986:                // DOS 3.0+ internal functions are installed
1.1.1.22  root     11987:                REG8(AL) = 0xff;
                   11988:                break;
1.1.1.29  root     11989: //     case 0x01: // DOS 3.0+ internal - Close Current File
                   11990:        case 0x02:
                   11991:                {
                   11992:                        UINT16 stack = i386_read_stack();
                   11993:                        REG16(BX) = *(UINT16 *)(mem + 4 * stack + 0);
                   11994:                        SREG(ES) = *(UINT16 *)(mem + 4 * stack + 2);
                   11995:                        i386_load_segment_descriptor(ES);
                   11996:                }
                   11997:                break;
1.1.1.30  root     11998:        case 0x03:
                   11999:                SREG(DS) = (DEVICE_TOP >> 4);
                   12000:                i386_load_segment_descriptor(DS);
                   12001:                break;
1.1.1.29  root     12002:        case 0x04:
                   12003:                {
                   12004:                        UINT16 stack = i386_read_stack();
                   12005:                        REG8(AL) = (stack == '/') ? '\\' : stack;
                   12006: #if defined(HAS_I386)
                   12007:                        m_ZF = (REG8(AL) == '\\');
                   12008: #else
                   12009:                        m_ZeroVal = (REG8(AL) != '\\');
                   12010: #endif
                   12011:                }
                   12012:                break;
                   12013:        case 0x05:
                   12014:                msdos_putch(i386_read_stack());
                   12015:                break;
                   12016: //     case 0x06: // DOS 3.0+ internal - Invole Critical Error
                   12017: //     case 0x07: // DOS 3.0+ internal - Make Disk Buffer Most Recentry Used
                   12018: //     case 0x08: // DOS 3.0+ internal - Decrement SFT Reference Count
                   12019: //     case 0x09: // DOS 3.0+ internal - Flush and FREE Disk Buffer
                   12020: //     case 0x0a: // DOS 3.0+ internal - Perform Critical Error Interrupt
                   12021: //     case 0x0b: // DOS 3.0+ internal - Signal Sharing Violation to User
                   12022: //     case 0x0c: // DOS 3.0+ internal - Open Device and Set SFT Owner/Mode
                   12023:        case 0x0d:
                   12024:                {
                   12025:                        SYSTEMTIME time;
                   12026:                        FILETIME file_time;
                   12027:                        WORD dos_date, dos_time;
                   12028:                        GetLocalTime(&time);
                   12029:                        SystemTimeToFileTime(&time, &file_time);
                   12030:                        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   12031:                        REG16(AX) = dos_date;
                   12032:                        REG16(DX) = dos_time;
                   12033:                }
                   12034:                break;
                   12035: //     case 0x0e: // DOS 3.0+ internal - Mark All Disk Buffers Unreferenced
                   12036: //     case 0x0f: // DOS 3.0+ internal - Make Buffer Most Recentry Used
                   12037: //     case 0x10: // DOS 3.0+ internal - Find Unreferenced Disk Buffer
                   12038:        case 0x11:
                   12039:                {
                   12040:                        char path[MAX_PATH], *p;
                   12041:                        strcpy(path, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   12042:                        my_strupr(path);
                   12043:                        while((p = my_strchr(path, '/')) != NULL) {
                   12044:                                *p = '\\';
                   12045:                        }
                   12046:                        strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
                   12047:                }
                   12048:                break;
                   12049:        case 0x12:
                   12050:                REG16(CX) = strlen((char *)(mem + SREG_BASE(ES) + REG16(DI)));
                   12051:                break;
                   12052:        case 0x13:
                   12053:                {
                   12054:                        char tmp[2] = {0};
                   12055:                        tmp[0] = i386_read_stack();
                   12056:                        my_strupr(tmp);
                   12057:                        REG8(AL) = tmp[0];
                   12058:                }
                   12059:                break;
                   12060:        case 0x14:
                   12061: #if defined(HAS_I386)
                   12062:                m_ZF = (SREG_BASE(DS) + REG16(SI) == SREG_BASE(ES) + REG16(DI));
                   12063: #else
                   12064:                m_ZeroVal = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   12065: #endif
                   12066:                m_CF = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   12067:                break;
                   12068: //     case 0x15: // DOS 3.0+ internal - Flush Buffer
1.1.1.21  root     12069:        case 0x16:
                   12070:                if(REG16(BX) < 20) {
                   12071:                        SREG(ES) = SFT_TOP >> 4;
                   12072:                        i386_load_segment_descriptor(ES);
                   12073:                        REG16(DI) = 6 + 0x3b * REG16(BX);
                   12074:                        
                   12075:                        // update system file table
                   12076:                        UINT8* sft = mem + SFT_TOP + 6 + 0x3b * REG16(BX);
                   12077:                        if(file_handler[REG16(BX)].valid) {
                   12078:                                int count = 0;
                   12079:                                for(int i = 0; i < 20; i++) {
                   12080:                                        if(msdos_psp_get_file_table(i, current_psp) == REG16(BX)) {
                   12081:                                                count++;
                   12082:                                        }
                   12083:                                }
                   12084:                                *(UINT16 *)(sft + 0x00) = count ? count : 0xffff;
                   12085:                                *(UINT32 *)(sft + 0x15) = _tell(REG16(BX));
                   12086:                                _lseek(REG16(BX), 0, SEEK_END);
                   12087:                                *(UINT32 *)(sft + 0x11) = _tell(REG16(BX));
                   12088:                                _lseek(REG16(BX), *(UINT32 *)(sft + 0x15), SEEK_SET);
                   12089:                        } else {
                   12090:                                memset(sft, 0, 0x3b);
                   12091:                        }
                   12092:                } else {
                   12093:                        REG16(AX) = 0x06;
                   12094:                        m_CF = 1;
                   12095:                }
                   12096:                break;
1.1.1.29  root     12097: //     case 0x17: // DOS 3.0+ internal - Get Current Directory Structure for Drive
                   12098: //     case 0x18: // DOS 3.0+ internal - Get Caller's Registers
                   12099: //     case 0x19: // DOS 3.0+ internal - Set Drive???
                   12100:        case 0x1a:
                   12101:                {
                   12102:                        char *path = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full[MAX_PATH];
                   12103:                        if(path[1] == ':') {
                   12104:                                if(path[0] >= 'a' && path[0] <= 'z') {
                   12105:                                        REG8(AL) = path[0] - 'a' + 1;
                   12106:                                } else if(path[0] >= 'A' && path[0] <= 'Z') {
                   12107:                                        REG8(AL) = path[0] - 'A' + 1;
                   12108:                                } else {
                   12109:                                        REG8(AL) = 0xff; // invalid
                   12110:                                }
                   12111:                                strcpy(full, path);
                   12112:                                strcpy(path, full + 2);
                   12113:                        } else if(GetFullPathName(path, MAX_PATH, full, NULL) != 0 && full[1] == ':') {
                   12114:                                if(full[0] >= 'a' && full[0] <= 'z') {
                   12115:                                        REG8(AL) = full[0] - 'a' + 1;
                   12116:                                } else if(full[0] >= 'A' && full[0] <= 'Z') {
                   12117:                                        REG8(AL) = full[0] - 'A' + 1;
                   12118:                                } else {
                   12119:                                        REG8(AL) = 0xff; // invalid
                   12120:                                }
                   12121:                        } else {
                   12122:                                REG8(AL) = 0x00; // default
                   12123:                        }
                   12124:                }
                   12125:                break;
                   12126:        case 0x1b:
                   12127:                {
                   12128:                        int year = REG16(CX) + 1980;
                   12129:                        REG8(AL) = ((year % 4) == 0 && (year % 100) != 0 && (year % 400) == 0) ? 29 : 28;
                   12130:                }
                   12131:                break;
                   12132: //     case 0x1c: // DOS 3.0+ internal - Check Sum Memory
                   12133: //     case 0x1d: // DOS 3.0+ internal - Sum Memory
                   12134:        case 0x1e:
                   12135:                {
                   12136:                        char *path_1st = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full_1st[MAX_PATH];
                   12137:                        char *path_2nd = (char *)(mem + SREG_BASE(ES) + REG16(DI)), full_2nd[MAX_PATH];
                   12138:                        if(GetFullPathName(path_1st, MAX_PATH, full_1st, NULL) != 0 && GetFullPathName(path_2nd, MAX_PATH, full_2nd, NULL) != 0) {
                   12139: #if defined(HAS_I386)
                   12140:                                m_ZF = (strcmp(full_1st, full_2nd) == 0);
                   12141: #else
                   12142:                                m_ZeroVal = (strcmp(full_1st, full_2nd) != 0);
                   12143: #endif
                   12144:                        } else {
                   12145: #if defined(HAS_I386)
                   12146:                                m_ZF = (strcmp(path_1st, path_2nd) == 0);
                   12147: #else
                   12148:                                m_ZeroVal = (strcmp(path_1st, path_2nd) != 0);
                   12149: #endif
                   12150:                        }
                   12151:                }
                   12152:                break;
                   12153: //     case 0x1f: // DOS 3.0+ internal - Build Current Directory Structure
1.1.1.21  root     12154:        case 0x20:
                   12155:                {
                   12156:                        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   12157:                        
                   12158:                        if(fd < 20) {
                   12159:                                SREG(ES) = current_psp;
                   12160:                                i386_load_segment_descriptor(ES);
                   12161:                                REG16(DI) = offsetof(psp_t, file_table) + fd;
                   12162:                        } else {
                   12163:                                REG16(AX) = 0x06;
                   12164:                                m_CF = 1;
                   12165:                        }
                   12166:                }
                   12167:                break;
1.1.1.29  root     12168:        case 0x21:
                   12169:                msdos_int_21h_60h(0);
                   12170:                break;
                   12171: //     case 0x22: // DOS 3.0+ internal - Set Extended Error Info
                   12172: //     case 0x23: // DOS 3.0+ internal - Check If Character Device
                   12173: //     case 0x24: // DOS 3.0+ internal - Sharing Retry Delay
                   12174:        case 0x25:
                   12175:                REG16(CX) = strlen((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   12176:                break;
                   12177:        case 0x26:
                   12178:                REG8(AL) = REG8(CL);
                   12179:                msdos_int_21h_3dh();
                   12180:                break;
                   12181:        case 0x27:
                   12182:                msdos_int_21h_3eh();
                   12183:                break;
                   12184:        case 0x28:
                   12185:                REG16(AX) = REG16(BP);
                   12186:                msdos_int_21h_42h();
                   12187:                break;
                   12188:        case 0x29:
                   12189:                msdos_int_21h_3fh();
                   12190:                break;
                   12191: //     case 0x2a: // DOS 3.0+ internal - Set Fast Open Entry Point
                   12192:        case 0x2b:
                   12193:                REG16(AX) = REG16(BP);
                   12194:                msdos_int_21h_44h();
                   12195:                break;
                   12196:        case 0x2c:
                   12197:                REG16(BX) = DEVICE_TOP >> 4;
                   12198:                REG16(AX) = 22;
                   12199:                break;
                   12200:        case 0x2d:
                   12201:                {
                   12202:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   12203:                        REG16(AX) = sda->extended_error_code;
                   12204:                }
                   12205:                break;
                   12206:        case 0x2e:
                   12207:                if(REG8(DL) == 0x00 || REG8(DL) == 0x02 || REG8(DL) == 0x04 || REG8(DL) == 0x06) {
1.1.1.32  root     12208:                        SREG(ES) = 0x0001;
                   12209:                        i386_load_segment_descriptor(ES);
                   12210:                        REG16(DI) = 0x00;
                   12211:                } else if(REG8(DL) == 0x08) {
                   12212:                        // dummy parameter error message read routine is at fffd:0010
                   12213:                        SREG(ES) = 0xfffd;
1.1.1.22  root     12214:                        i386_load_segment_descriptor(ES);
1.1.1.32  root     12215:                        REG16(DI) = 0x0010;
1.1.1.22  root     12216:                }
                   12217:                break;
1.1.1.29  root     12218:        case 0x2f:
                   12219:                if(REG16(DX) != 0) {
1.1.1.30  root     12220:                        dos_major_version = REG8(DL);
                   12221:                        dos_minor_version = REG8(DH);
1.1.1.29  root     12222:                } else {
                   12223:                        REG8(DL) = 7;
                   12224:                        REG8(DH) = 10;
                   12225:                }
                   12226:                break;
                   12227: //     case 0x30: // Windows95 - Find SFT Entry in Internal File Tables
                   12228: //     case 0x31: // Windows95 - Set/Clear "Report Windows to DOS Programs" Flag
1.1.1.22  root     12229:        default:
                   12230:                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));
                   12231:                REG16(AX) = 0x01;
                   12232:                m_CF = 1;
                   12233:                break;
                   12234:        }
                   12235: }
                   12236: 
1.1.1.30  root     12237: inline void msdos_int_2fh_13h()
                   12238: {
                   12239:        static UINT16 prevDS = 0, prevDX = 0;
                   12240:        static UINT16 prevES = 0, prevBX = 0;
                   12241:        UINT16 tmp;
                   12242:        
                   12243:        tmp = SREG(DS); SREG(DS) = prevDS; prevDS = tmp;
                   12244:        i386_load_segment_descriptor(DS);
                   12245:        tmp = REG16(DX); REG16(DX) = prevDX; prevDX = tmp;
                   12246:        
                   12247:        tmp = SREG(ES); SREG(ES) = prevES; prevES = tmp;
                   12248:        i386_load_segment_descriptor(ES);
                   12249:        tmp = REG16(BX); REG16(BX) = prevBX; prevBX = tmp;
                   12250: }
                   12251: 
1.1.1.22  root     12252: inline void msdos_int_2fh_14h()
                   12253: {
                   12254:        switch(REG8(AL)) {
                   12255:        case 0x00:
1.1.1.29  root     12256:                // NLSFUNC.COM is installed
                   12257:                REG8(AL) = 0xff;
1.1.1.25  root     12258:                break;
                   12259:        case 0x01:
                   12260:        case 0x03:
                   12261:                REG8(AL) = 0x00;
                   12262:                active_code_page = REG16(BX);
                   12263:                msdos_nls_tables_update();
                   12264:                break;
                   12265:        case 0x02:
                   12266:                REG8(AL) = get_extended_country_info(REG16(BP));
                   12267:                break;
                   12268:        case 0x04:
                   12269:                REG8(AL) = 0x00;
                   12270:                get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)));
1.1.1.22  root     12271:                break;
                   12272:        default:
                   12273:                unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   12274:                REG16(AX) = 0x01;
                   12275:                m_CF = 1;
                   12276:                break;
                   12277:        }
                   12278: }
                   12279: 
                   12280: inline void msdos_int_2fh_15h()
                   12281: {
                   12282:        switch(REG8(AL)) {
1.1.1.29  root     12283:        case 0x00: // CD-ROM - Installation Check
                   12284:                if(REG16(BX) == 0x0000) {
                   12285:                        // MSCDEX is not installed
                   12286: //                     REG8(AL) = 0x00;
                   12287:                } else {
                   12288:                        // GRAPHICS.COM is not installed
                   12289: //                     REG8(AL) = 0x00;
                   12290:                }
1.1.1.22  root     12291:                break;
                   12292:        case 0xff:
1.1.1.29  root     12293:                if(REG16(BX) == 0x0000) {
                   12294:                        // CORELCDX is not installed
                   12295:                } else {
                   12296:                        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));
                   12297:                        REG16(AX) = 0x01;
                   12298:                        m_CF = 1;
                   12299:                }
1.1.1.22  root     12300:                break;
1.1.1.21  root     12301:        default:
1.1.1.22  root     12302:                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     12303:                REG16(AX) = 0x01;
                   12304:                m_CF = 1;
                   12305:                break;
                   12306:        }
                   12307: }
                   12308: 
1.1       root     12309: inline void msdos_int_2fh_16h()
                   12310: {
                   12311:        switch(REG8(AL)) {
                   12312:        case 0x00:
1.1.1.14  root     12313:                if(no_windows) {
1.1.1.29  root     12314:                        // neither Windows 3.x enhanced mode nor Windows/386 2.x running
                   12315: //                     REG8(AL) = 0x00;
1.1.1.14  root     12316:                } else {
1.1.1.30  root     12317:                        REG8(AL) = win_major_version;
                   12318:                        REG8(AH) = win_minor_version;
1.1       root     12319:                }
                   12320:                break;
1.1.1.30  root     12321:        case 0x05:
                   12322:                // from DOSBox
                   12323:                i386_set_a20_line(1);
                   12324:                break;
1.1.1.22  root     12325:        case 0x0a:
                   12326:                if(!no_windows) {
                   12327:                        REG16(AX) = 0x0000;
1.1.1.30  root     12328:                        REG8(BH) = win_major_version;
                   12329:                        REG8(BL) = win_minor_version;
1.1.1.22  root     12330:                        REG16(CX) = 0x0003; // enhanced
                   12331:                }
                   12332:                break;
1.1.1.30  root     12333:        case 0x0b:
                   12334:                // no TRS, keep ES:DI = 0000h:0000h
1.1.1.22  root     12335:        case 0x0e:
                   12336:        case 0x0f:
1.1.1.30  root     12337:        case 0x10:
1.1.1.22  root     12338:        case 0x11:
                   12339:        case 0x12:
                   12340:        case 0x13:
                   12341:        case 0x14:
1.1.1.30  root     12342:        case 0x15:
1.1.1.33  root     12343:        case 0x86:
1.1.1.22  root     12344:        case 0x87:
1.1.1.30  root     12345:        case 0x89:
1.1.1.33  root     12346:        case 0x8a:
1.1.1.22  root     12347:                // function not supported, do not clear AX
                   12348:                break;
1.1.1.14  root     12349:        case 0x80:
                   12350:                Sleep(10);
1.1.1.35  root     12351:                REQUEST_HARDWRE_UPDATE();
1.1.1.29  root     12352:                REG8(AL) = 0x00;
1.1.1.14  root     12353:                break;
1.1.1.33  root     12354:        case 0x83:
                   12355:                REG16(BX) = 0x01; // system vm id
                   12356:                break;
1.1.1.22  root     12357:        case 0x8e:
                   12358:                REG16(AX) = 0x00; // failed
                   12359:                break;
1.1.1.20  root     12360:        case 0x8f:
                   12361:                switch(REG8(DH)) {
                   12362:                case 0x00:
                   12363:                case 0x02:
                   12364:                case 0x03:
                   12365:                        REG16(AX) = 0x00;
                   12366:                        break;
                   12367:                case 0x01:
                   12368:                        REG16(AX) = 0x168f;
                   12369:                        break;
                   12370:                }
                   12371:                break;
1.1       root     12372:        default:
1.1.1.22  root     12373:                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));
                   12374:                REG16(AX) = 0x01;
                   12375:                m_CF = 1;
                   12376:                break;
                   12377:        }
                   12378: }
                   12379: 
                   12380: inline void msdos_int_2fh_19h()
                   12381: {
                   12382:        switch(REG8(AL)) {
                   12383:        case 0x00:
1.1.1.29  root     12384:                // SHELLB.COM is not installed
                   12385: //             REG8(AL) = 0x00;
1.1.1.22  root     12386:                break;
                   12387:        case 0x01:
                   12388:        case 0x02:
                   12389:        case 0x03:
                   12390:        case 0x04:
                   12391:                REG16(AX) = 0x01;
                   12392:                m_CF = 1;
                   12393:                break;
1.1.1.29  root     12394:        case 0x80:
                   12395:                // IBM ROM-DOS v4.0 is not installed
                   12396: //             REG8(AL) = 0x00;
                   12397:                break;
1.1.1.22  root     12398:        default:
                   12399:                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     12400:                REG16(AX) = 0x01;
1.1.1.3   root     12401:                m_CF = 1;
1.1       root     12402:                break;
                   12403:        }
                   12404: }
                   12405: 
                   12406: inline void msdos_int_2fh_1ah()
                   12407: {
                   12408:        switch(REG8(AL)) {
                   12409:        case 0x00:
1.1.1.29  root     12410:                // ANSI.SYS is installed
1.1       root     12411:                REG8(AL) = 0xff;
                   12412:                break;
                   12413:        default:
1.1.1.22  root     12414:                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));
                   12415:                REG16(AX) = 0x01;
                   12416:                m_CF = 1;
                   12417:                break;
                   12418:        }
                   12419: }
                   12420: 
1.1.1.30  root     12421: inline void msdos_int_2fh_40h()
1.1.1.22  root     12422: {
                   12423:        switch(REG8(AL)) {
                   12424:        case 0x00:
1.1.1.30  root     12425:                // Windows 3+ - Get Virtual Device Driver (VDD) Capabilities
                   12426:                REG8(AL) = 0x01; // does not virtualize video access
1.1.1.22  root     12427:                break;
                   12428:        default:
                   12429:                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     12430:                REG16(AX) = 0x01;
1.1.1.3   root     12431:                m_CF = 1;
1.1       root     12432:                break;
                   12433:        }
                   12434: }
                   12435: 
                   12436: inline void msdos_int_2fh_43h()
                   12437: {
                   12438:        switch(REG8(AL)) {
                   12439:        case 0x00:
1.1.1.29  root     12440:                // XMS is installed ?
1.1.1.19  root     12441: #ifdef SUPPORT_XMS
                   12442:                if(support_xms) {
                   12443:                        REG8(AL) = 0x80;
                   12444:                } else
                   12445: #endif
                   12446:                REG8(AL) = 0x00;
                   12447:                break;
                   12448:        case 0x10:
                   12449:                SREG(ES) = XMS_TOP >> 4;
                   12450:                i386_load_segment_descriptor(ES);
1.1.1.26  root     12451:                REG16(BX) = 0x15;
1.1       root     12452:                break;
                   12453:        default:
1.1.1.22  root     12454:                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));
                   12455:                REG16(AX) = 0x01;
                   12456:                m_CF = 1;
                   12457:                break;
                   12458:        }
                   12459: }
                   12460: 
                   12461: inline void msdos_int_2fh_46h()
                   12462: {
                   12463:        switch(REG8(AL)) {
                   12464:        case 0x80:
1.1.1.29  root     12465:                // Windows v3.0 is not installed
                   12466: //             REG8(AL) = 0x00;
1.1.1.22  root     12467:                break;
                   12468:        default:
                   12469:                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));
                   12470:                REG16(AX) = 0x01;
                   12471:                m_CF = 1;
                   12472:                break;
                   12473:        }
                   12474: }
                   12475: 
                   12476: inline void msdos_int_2fh_48h()
                   12477: {
                   12478:        switch(REG8(AL)) {
                   12479:        case 0x00:
1.1.1.29  root     12480:                // DOSKEY is not installed
                   12481: //             REG8(AL) = 0x00;
1.1.1.22  root     12482:                break;
                   12483:        case 0x10:
                   12484:                msdos_int_21h_0ah();
                   12485:                REG16(AX) = 0x00;
                   12486:                break;
                   12487:        default:
                   12488:                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     12489:                REG16(AX) = 0x01;
1.1.1.3   root     12490:                m_CF = 1;
1.1       root     12491:                break;
                   12492:        }
                   12493: }
                   12494: 
                   12495: inline void msdos_int_2fh_4ah()
                   12496: {
                   12497:        switch(REG8(AL)) {
1.1.1.29  root     12498: #ifdef SUPPORT_HMA
                   12499:        case 0x01: // DOS 5.0+ - Query Free HMA Space
                   12500:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   12501:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   12502:                                // restore first free mcb in high memory area
                   12503:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   12504:                        }
                   12505:                        int offset = 0xffff;
                   12506:                        if((REG16(BX) = msdos_hma_mem_get_free(&offset)) != 0) {
                   12507:                                REG16(DI) = offset + 0x10;
                   12508:                        } else {
                   12509:                                REG16(DI) = 0xffff;
                   12510:                        }
                   12511:                } else {
                   12512:                        // HMA is already used
                   12513:                        REG16(BX) = 0;
                   12514:                        REG16(DI) = 0xffff;
                   12515:                }
                   12516:                SREG(ES) = 0xffff;
                   12517:                i386_load_segment_descriptor(ES);
                   12518:                break;
                   12519:        case 0x02: // DOS 5.0+ - Allocate HMA Space
                   12520:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   12521:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   12522:                                // restore first free mcb in high memory area
                   12523:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   12524:                        }
                   12525:                        int size = REG16(BX), offset;
                   12526:                        if((size % 16) != 0) {
                   12527:                                size &= ~15;
                   12528:                                size += 16;
                   12529:                        }
                   12530:                        if((offset = msdos_hma_mem_alloc(size, current_psp)) != -1) {
                   12531:                                REG16(BX) = size;
                   12532:                                REG16(DI) = offset + 0x10;
                   12533:                                is_hma_used_by_int_2fh = true;
                   12534:                        } else {
                   12535:                                REG16(BX) = 0;
                   12536:                                REG16(DI) = 0xffff;
                   12537:                        }
                   12538:                } else {
                   12539:                        // HMA is already used
                   12540:                        REG16(BX) = 0;
                   12541:                        REG16(DI) = 0xffff;
                   12542:                }
                   12543:                SREG(ES) = 0xffff;
                   12544:                i386_load_segment_descriptor(ES);
                   12545:                break;
                   12546:        case 0x03: // Windows95 - (De)Allocate HMA Memory Block
                   12547:                if(REG8(DL) == 0x00) {
                   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:                                int size = REG16(BX), offset;
                   12555:                                if((size % 16) != 0) {
                   12556:                                        size &= ~15;
                   12557:                                        size += 16;
                   12558:                                }
                   12559:                                if((offset = msdos_hma_mem_alloc(size, REG16(CX))) != -1) {
                   12560: //                                     REG16(BX) = size;
                   12561:                                        SREG(ES) = 0xffff;
                   12562:                                        i386_load_segment_descriptor(ES);
                   12563:                                        REG16(DI) = offset + 0x10;
                   12564:                                        is_hma_used_by_int_2fh = true;
                   12565:                                } else {
                   12566:                                        REG16(DI) = 0xffff;
                   12567:                                }
                   12568:                        } else {
                   12569:                                REG16(DI) = 0xffff;
                   12570:                        }
                   12571:                } else if(REG8(DL) == 0x01) {
                   12572:                        if(!is_hma_used_by_xms) {
                   12573:                                int size = REG16(BX);
                   12574:                                if((size % 16) != 0) {
                   12575:                                        size &= ~15;
                   12576:                                        size += 16;
                   12577:                                }
                   12578:                                if(msdos_hma_mem_realloc(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10, size) != -1) {
                   12579:                                        // memory block address is not changed
                   12580:                                } else {
                   12581:                                        REG16(DI) = 0xffff;
                   12582:                                }
                   12583:                        } else {
                   12584:                                REG16(DI) = 0xffff;
                   12585:                        }
                   12586:                } else if(REG8(DL) == 0x02) {
                   12587:                        if(!is_hma_used_by_xms) {
                   12588:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   12589:                                        // restore first free mcb in high memory area
                   12590:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   12591:                                        is_hma_used_by_int_2fh = false;
                   12592:                                } else {
                   12593:                                        msdos_hma_mem_free(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10);
                   12594:                                        if(msdos_hma_mem_get_free(NULL) == 0xffe0) {
                   12595:                                                is_hma_used_by_int_2fh = false;
                   12596:                                        }
                   12597:                                }
                   12598:                        }
                   12599:                } else {
                   12600:                        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));
                   12601:                        REG16(AX) = 0x01;
                   12602:                        m_CF = 1;
                   12603:                }
                   12604:                break;
                   12605:        case 0x04: // Windows95 - Get Start of HMA Memory Chain
                   12606:                if(!is_hma_used_by_xms) {
                   12607:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   12608:                                // restore first free mcb in high memory area
                   12609:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   12610:                                is_hma_used_by_int_2fh = false;
                   12611:                        }
                   12612:                        REG16(AX) = 0x0000;
                   12613:                        SREG(ES) = 0xffff;
                   12614:                        i386_load_segment_descriptor(ES);
                   12615:                        REG16(DI) = 0x10;
                   12616:                }
                   12617:                break;
                   12618: #else
1.1       root     12619:        case 0x01:
                   12620:        case 0x02:
1.1.1.29  root     12621:                // HMA is already used
1.1.1.27  root     12622:                REG16(BX) = 0x0000;
1.1.1.3   root     12623:                SREG(ES) = 0xffff;
                   12624:                i386_load_segment_descriptor(ES);
1.1       root     12625:                REG16(DI) = 0xffff;
                   12626:                break;
1.1.1.19  root     12627:        case 0x03:
                   12628:                // unable to allocate
                   12629:                REG16(DI) = 0xffff;
                   12630:                break;
                   12631:        case 0x04:
                   12632:                // function not supported, do not clear AX
                   12633:                break;
1.1.1.29  root     12634: #endif
                   12635:        case 0x10:
                   12636:                if(REG16(BX) == 0x0000) {
                   12637:                        // SMARTDRV is not installed
                   12638:                } else {
                   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:                }
                   12643:                break;
                   12644:        case 0x11:
                   12645:                if(REG16(BX) == 0x0000) {
                   12646:                        // DBLSPACE.BIN is not installed
                   12647:                } else {
                   12648:                        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));
                   12649:                        REG16(AX) = 0x01;
                   12650:                        m_CF = 1;
                   12651:                }
1.1.1.22  root     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));
                   12655:                REG16(AX) = 0x01;
                   12656:                m_CF = 1;
                   12657:                break;
                   12658:        }
                   12659: }
                   12660: 
                   12661: inline void msdos_int_2fh_4bh()
                   12662: {
                   12663:        switch(REG8(AL)) {
1.1.1.24  root     12664:        case 0x01:
1.1.1.22  root     12665:        case 0x02:
1.1.1.29  root     12666:                // Task Switcher is not installed
1.1.1.24  root     12667:                break;
                   12668:        case 0x03:
                   12669:                // this call is available from within DOSSHELL even if the task switcher is not installed
                   12670:                REG16(AX) = REG16(BX) = 0x0000; // no more avaiable switcher id
1.1.1.22  root     12671:                break;
1.1.1.30  root     12672:        case 0x04:
                   12673:                REG16(BX) = 0x0000; // free switcher id successfully
                   12674:                break;
1.1       root     12675:        default:
1.1.1.22  root     12676:                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     12677:                REG16(AX) = 0x01;
1.1.1.3   root     12678:                m_CF = 1;
1.1       root     12679:                break;
                   12680:        }
                   12681: }
                   12682: 
                   12683: inline void msdos_int_2fh_4fh()
                   12684: {
                   12685:        switch(REG8(AL)) {
                   12686:        case 0x00:
1.1.1.29  root     12687:                // BILING is installed
1.1.1.27  root     12688:                REG16(AX) = 0x0000;
                   12689:                REG8(DL) = 0x01;        // major version
                   12690:                REG8(DH) = 0x00;        // minor version
1.1       root     12691:                break;
                   12692:        case 0x01:
1.1.1.27  root     12693:                REG16(AX) = 0x0000;
1.1       root     12694:                REG16(BX) = active_code_page;
                   12695:                break;
                   12696:        default:
1.1.1.22  root     12697:                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));
                   12698:                REG16(AX) = 0x01;
                   12699:                m_CF = 1;
                   12700:                break;
                   12701:        }
                   12702: }
                   12703: 
                   12704: inline void msdos_int_2fh_55h()
                   12705: {
                   12706:        switch(REG8(AL)) {
                   12707:        case 0x00:
                   12708:        case 0x01:
                   12709: //             unimplemented_2fh("int %02Xh (AX=%04X BX=%04X CX=%04X DX=%04X SI=%04X DI=%04X DS=%04X ES=%04X)\n", 0x2f, REG16(AX), REG16(BX), REG16(CX), REG16(DX), REG16(SI), REG16(DI), SREG(DS), SREG(ES));
                   12710:                break;
                   12711:        default:
                   12712:                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     12713:                REG16(AX) = 0x01;
1.1.1.3   root     12714:                m_CF = 1;
1.1       root     12715:                break;
                   12716:        }
                   12717: }
                   12718: 
1.1.1.24  root     12719: inline void msdos_int_2fh_adh()
                   12720: {
                   12721:        switch(REG8(AL)) {
                   12722:        case 0x00:
1.1.1.29  root     12723:                // DISPLAY.SYS is installed
1.1.1.24  root     12724:                REG8(AL) = 0xff;
                   12725:                REG16(BX) = 0x100; // ???
                   12726:                break;
                   12727:        case 0x01:
                   12728:                active_code_page = REG16(BX);
                   12729:                msdos_nls_tables_update();
                   12730:                REG16(AX) = 0x01;
                   12731:                break;
                   12732:        case 0x02:
                   12733:                REG16(BX) = active_code_page;
                   12734:                break;
                   12735:        case 0x03:
                   12736:                // FIXME
                   12737:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 1;
                   12738:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2) = 3;
                   12739:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4) = 1;
                   12740:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 6) = active_code_page;
                   12741:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 8) = active_code_page;
                   12742:                break;
                   12743:        case 0x80:
                   12744:                break; // keyb.com is not installed
                   12745:        default:
                   12746:                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));
                   12747:                REG16(AX) = 0x01;
                   12748:                m_CF = 1;
                   12749:                break;
                   12750:        }
                   12751: }
                   12752: 
1.1       root     12753: inline void msdos_int_2fh_aeh()
                   12754: {
                   12755:        switch(REG8(AL)) {
                   12756:        case 0x00:
1.1.1.28  root     12757:                // FIXME: we need to check the given command line
                   12758:                REG8(AL) = 0x00; // the command should be executed as usual
                   12759: //             REG8(AL) = 0xff; // this command is a TSR extension to COMMAND.COM
1.1       root     12760:                break;
                   12761:        case 0x01:
                   12762:                {
                   12763:                        char command[MAX_PATH];
                   12764:                        memset(command, 0, sizeof(command));
1.1.1.3   root     12765:                        memcpy(command, mem + SREG_BASE(DS) + REG16(SI) + 1, mem[SREG_BASE(DS) + REG16(SI)]);
1.1       root     12766:                        
                   12767:                        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   12768:                        param->env_seg = 0;
                   12769:                        param->cmd_line.w.l = 44;
                   12770:                        param->cmd_line.w.h = (WORK_TOP >> 4);
                   12771:                        param->fcb1.w.l = 24;
                   12772:                        param->fcb1.w.h = (WORK_TOP >> 4);
                   12773:                        param->fcb2.w.l = 24;
                   12774:                        param->fcb2.w.h = (WORK_TOP >> 4);
                   12775:                        
                   12776:                        memset(mem + WORK_TOP + 24, 0x20, 20);
                   12777:                        
                   12778:                        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
1.1.1.3   root     12779:                        cmd_line->len = mem[SREG_BASE(DS) + REG16(BX) + 1];
                   12780:                        memcpy(cmd_line->cmd, mem + SREG_BASE(DS) + REG16(BX) + 2, cmd_line->len);
1.1       root     12781:                        cmd_line->cmd[cmd_line->len] = 0x0d;
                   12782:                        
1.1.1.28  root     12783:                        try {
                   12784:                                msdos_process_exec(command, param, 0);
                   12785:                        } catch(...) {
                   12786:                                fatalerror("failed to start '%s' by int 2Fh, AX=AE01h\n", command);
1.1       root     12787:                        }
                   12788:                }
                   12789:                break;
                   12790:        default:
1.1.1.22  root     12791:                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     12792:                REG16(AX) = 0x01;
1.1.1.3   root     12793:                m_CF = 1;
1.1       root     12794:                break;
                   12795:        }
                   12796: }
                   12797: 
1.1.1.34  root     12798: inline void msdos_int_2fh_b7h()
                   12799: {
                   12800:        switch(REG8(AL)) {
                   12801:        case 0x00:
                   12802:                // APPEND is not installed
                   12803: //             REG8(AL) = 0x00;
                   12804:                break;
                   12805:        case 0x07:
                   12806:                // COMMAND.COM calls this service without checking APPEND is installed
                   12807:                break;
                   12808:        default:
                   12809:                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));
                   12810:                REG16(AX) = 0x01;
                   12811:                m_CF = 1;
                   12812:                break;
                   12813:        }
                   12814: }
                   12815: 
1.1.1.24  root     12816: inline void msdos_int_33h_0000h()
                   12817: {
                   12818:        REG16(AX) = 0xffff; // hardware/driver installed
                   12819:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   12820: }
                   12821: 
                   12822: inline void msdos_int_33h_0001h()
                   12823: {
1.1.1.34  root     12824:        if(mouse.hidden > 0) {
                   12825:                mouse.hidden--;
                   12826:        }
                   12827:        if(mouse.hidden == 0) {
1.1.1.24  root     12828:                if(!(dwConsoleMode & ENABLE_MOUSE_INPUT)) {
                   12829:                        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode | ENABLE_MOUSE_INPUT);
                   12830:                }
                   12831:                pic[1].imr &= ~0x10;    // enable irq12
                   12832:        }
                   12833: }
                   12834: 
                   12835: inline void msdos_int_33h_0002h()
                   12836: {
1.1.1.34  root     12837:        mouse.hidden++;
                   12838: //     SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode & ~ENABLE_MOUSE_INPUT);
                   12839:        pic[1].imr |= 0x10;     // enable irq12
1.1.1.24  root     12840: }
                   12841: 
                   12842: inline void msdos_int_33h_0003h()
                   12843: {
1.1.1.34  root     12844: //     if(mouse.hidden > 0) {
                   12845:                update_console_input();
                   12846: //     }
1.1.1.24  root     12847:        REG16(BX) = mouse.get_buttons();
1.1.1.34  root     12848:        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   12849:        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
                   12850: }
                   12851: 
                   12852: inline void msdos_int_33h_0004h()
                   12853: {
                   12854:        mouse.position.x = REG16(CX);
                   12855:        mouse.position.x = REG16(DX);
1.1.1.24  root     12856: }
                   12857: 
                   12858: inline void msdos_int_33h_0005h()
                   12859: {
1.1.1.34  root     12860: //     if(mouse.hidden > 0) {
                   12861:                update_console_input();
                   12862: //     }
1.1.1.24  root     12863:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   12864:                int idx = REG16(BX);
1.1.1.34  root     12865:                REG16(BX) = min(mouse.buttons[idx].pressed_times, 0x7fff);
                   12866:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].pressed_position.x));
                   12867:                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     12868:                mouse.buttons[idx].pressed_times = 0;
                   12869:        } else {
                   12870:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   12871:        }
                   12872:        REG16(AX) = mouse.get_buttons();
                   12873: }
                   12874: 
                   12875: inline void msdos_int_33h_0006h()
                   12876: {
1.1.1.34  root     12877: //     if(mouse.hidden > 0) {
                   12878:                update_console_input();
                   12879: //     }
1.1.1.24  root     12880:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   12881:                int idx = REG16(BX);
1.1.1.34  root     12882:                REG16(BX) = min(mouse.buttons[idx].released_times, 0x7fff);
                   12883:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].released_position.x));
                   12884:                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     12885:                mouse.buttons[idx].released_times = 0;
                   12886:        } else {
                   12887:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   12888:        }
                   12889:        REG16(AX) = mouse.get_buttons();
                   12890: }
                   12891: 
                   12892: inline void msdos_int_33h_0007h()
                   12893: {
                   12894:        mouse.min_position.x = min(REG16(CX), REG16(DX));
                   12895:        mouse.max_position.x = max(REG16(CX), REG16(DX));
                   12896: }
                   12897: 
                   12898: inline void msdos_int_33h_0008h()
                   12899: {
                   12900:        mouse.min_position.y = min(REG16(CX), REG16(DX));
                   12901:        mouse.max_position.y = max(REG16(CX), REG16(DX));
                   12902: }
                   12903: 
                   12904: inline void msdos_int_33h_0009h()
                   12905: {
                   12906:        mouse.hot_spot[0] = REG16(BX);
                   12907:        mouse.hot_spot[1] = REG16(CX);
                   12908: }
                   12909: 
                   12910: inline void msdos_int_33h_000bh()
                   12911: {
1.1.1.34  root     12912: //     if(mouse.hidden > 0) {
                   12913:                update_console_input();
                   12914: //     }
1.1.1.24  root     12915:        int dx = (mouse.position.x - mouse.prev_position.x) * mouse.mickey.x / 8;
                   12916:        int dy = (mouse.position.y - mouse.prev_position.y) * mouse.mickey.y / 8;
                   12917:        mouse.prev_position.x = mouse.position.x;
                   12918:        mouse.prev_position.y = mouse.position.y;
                   12919:        REG16(CX) = dx;
                   12920:        REG16(DX) = dy;
                   12921: }
                   12922: 
                   12923: inline void msdos_int_33h_000ch()
                   12924: {
                   12925:        mouse.call_mask = REG16(CX);
                   12926:        mouse.call_addr.w.l = REG16(DX);
                   12927:        mouse.call_addr.w.h = SREG(ES);
                   12928: }
                   12929: 
                   12930: inline void msdos_int_33h_000fh()
                   12931: {
                   12932:        mouse.mickey.x = REG16(CX);
                   12933:        mouse.mickey.y = REG16(DX);
                   12934: }
                   12935: 
                   12936: inline void msdos_int_33h_0011h()
                   12937: {
                   12938:        REG16(AX) = 0xffff;
                   12939:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   12940: }
                   12941: 
                   12942: inline void msdos_int_33h_0014h()
                   12943: {
                   12944:        UINT16 old_mask = mouse.call_mask;
                   12945:        UINT16 old_ofs = mouse.call_addr.w.l;
                   12946:        UINT16 old_seg = mouse.call_addr.w.h;
                   12947:        
                   12948:        mouse.call_mask = REG16(CX);
                   12949:        mouse.call_addr.w.l = REG16(DX);
                   12950:        mouse.call_addr.w.h = SREG(ES);
                   12951:        
                   12952:        REG16(CX) = old_mask;
                   12953:        REG16(DX) = old_ofs;
                   12954:        SREG(ES) = old_seg;
                   12955:        i386_load_segment_descriptor(ES);
                   12956: }
                   12957: 
                   12958: inline void msdos_int_33h_0015h()
                   12959: {
                   12960:        REG16(BX) = sizeof(mouse);
                   12961: }
                   12962: 
                   12963: inline void msdos_int_33h_0016h()
                   12964: {
                   12965:        memcpy(mem + SREG_BASE(ES) + REG16(DX), &mouse, sizeof(mouse));
                   12966: }
                   12967: 
                   12968: inline void msdos_int_33h_0017h()
                   12969: {
                   12970:        memcpy(&mouse, mem + SREG_BASE(ES) + REG16(DX), sizeof(mouse));
                   12971: }
                   12972: 
                   12973: inline void msdos_int_33h_001ah()
                   12974: {
                   12975:        mouse.sensitivity[0] = REG16(BX);
                   12976:        mouse.sensitivity[1] = REG16(CX);
                   12977:        mouse.sensitivity[2] = REG16(DX);
                   12978: }
                   12979: 
                   12980: inline void msdos_int_33h_001bh()
                   12981: {
                   12982:        REG16(BX) = mouse.sensitivity[0];
                   12983:        REG16(CX) = mouse.sensitivity[1];
                   12984:        REG16(DX) = mouse.sensitivity[2];
                   12985: }
                   12986: 
                   12987: inline void msdos_int_33h_001dh()
                   12988: {
                   12989:        mouse.display_page = REG16(BX);
                   12990: }
                   12991: 
                   12992: inline void msdos_int_33h_001eh()
                   12993: {
                   12994:        REG16(BX) = mouse.display_page;
                   12995: }
                   12996: 
1.1.1.34  root     12997: inline void msdos_int_33h_001fh()
                   12998: {
                   12999:        // from DOSBox
                   13000:        REG16(BX) = 0x0000;
                   13001:        SREG(ES) = 0x0000;
                   13002:        i386_load_segment_descriptor(ES);
                   13003:        mouse.enabled = false;
                   13004:        mouse.old_hidden = mouse.hidden;
                   13005:        mouse.hidden = 1;
                   13006: }
                   13007: 
                   13008: inline void msdos_int_33h_0020h()
                   13009: {
                   13010:        // from DOSBox
                   13011:        mouse.enabled = true;
                   13012:        mouse.hidden = mouse.old_hidden;
                   13013: }
                   13014: 
1.1.1.24  root     13015: inline void msdos_int_33h_0021h()
                   13016: {
                   13017:        REG16(AX) = 0xffff;
                   13018:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   13019: }
                   13020: 
                   13021: inline void msdos_int_33h_0022h()
                   13022: {
                   13023:        mouse.language = REG16(BX);
                   13024: }
                   13025: 
                   13026: inline void msdos_int_33h_0023h()
                   13027: {
                   13028:        REG16(BX) = mouse.language;
                   13029: }
                   13030: 
                   13031: inline void msdos_int_33h_0024h()
                   13032: {
                   13033:        REG16(BX) = 0x0805; // V8.05
                   13034:        REG16(CX) = 0x0400; // PS/2
                   13035: }
                   13036: 
                   13037: inline void msdos_int_33h_0026h()
                   13038: {
                   13039:        REG16(BX) = 0x0000;
                   13040:        REG16(CX) = mouse.max_position.x;
                   13041:        REG16(DX) = mouse.max_position.y;
                   13042: }
                   13043: 
                   13044: inline void msdos_int_33h_002ah()
                   13045: {
1.1.1.34  root     13046:        REG16(AX) = -mouse.hidden;
1.1.1.24  root     13047:        REG16(BX) = mouse.hot_spot[0];
                   13048:        REG16(CX) = mouse.hot_spot[1];
                   13049:        REG16(DX) = 4; // PS/2
                   13050: }
                   13051: 
                   13052: inline void msdos_int_33h_0031h()
                   13053: {
                   13054:        REG16(AX) = mouse.min_position.x;
                   13055:        REG16(BX) = mouse.min_position.y;
                   13056:        REG16(CX) = mouse.max_position.x;
                   13057:        REG16(DX) = mouse.max_position.y;
                   13058: }
                   13059: 
                   13060: inline void msdos_int_33h_0032h()
                   13061: {
                   13062:        REG16(AX) = 0;
                   13063: //     REG16(AX) |= 0x8000; // 0025h
                   13064:        REG16(AX) |= 0x4000; // 0026h
                   13065: //     REG16(AX) |= 0x2000; // 0027h
                   13066: //     REG16(AX) |= 0x1000; // 0028h
                   13067: //     REG16(AX) |= 0x0800; // 0029h
                   13068:        REG16(AX) |= 0x0400; // 002ah
                   13069: //     REG16(AX) |= 0x0200; // 002bh
                   13070: //     REG16(AX) |= 0x0100; // 002ch
                   13071: //     REG16(AX) |= 0x0080; // 002dh
                   13072: //     REG16(AX) |= 0x0040; // 002eh
                   13073:        REG16(AX) |= 0x0020; // 002fh
                   13074: //     REG16(AX) |= 0x0010; // 0030h
                   13075:        REG16(AX) |= 0x0008; // 0031h
                   13076:        REG16(AX) |= 0x0004; // 0032h
                   13077: //     REG16(AX) |= 0x0002; // 0033h
                   13078: //     REG16(AX) |= 0x0001; // 0034h
                   13079: }
                   13080: 
1.1.1.19  root     13081: inline void msdos_int_67h_40h()
                   13082: {
                   13083:        if(!support_ems) {
                   13084:                REG8(AH) = 0x84;
                   13085:        } else {
                   13086:                REG8(AH) = 0x00;
                   13087:        }
                   13088: }
                   13089: 
                   13090: inline void msdos_int_67h_41h()
                   13091: {
                   13092:        if(!support_ems) {
                   13093:                REG8(AH) = 0x84;
                   13094:        } else {
                   13095:                REG8(AH) = 0x00;
                   13096:                REG16(BX) = EMS_TOP >> 4;
                   13097:        }
                   13098: }
                   13099: 
                   13100: inline void msdos_int_67h_42h()
                   13101: {
                   13102:        if(!support_ems) {
                   13103:                REG8(AH) = 0x84;
                   13104:        } else {
                   13105:                REG8(AH) = 0x00;
                   13106:                REG16(BX) = free_ems_pages;
                   13107:                REG16(DX) = MAX_EMS_PAGES;
                   13108:        }
                   13109: }
                   13110: 
                   13111: inline void msdos_int_67h_43h()
                   13112: {
                   13113:        if(!support_ems) {
                   13114:                REG8(AH) = 0x84;
                   13115:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   13116:                REG8(AH) = 0x87;
                   13117:        } else if(REG16(BX) > free_ems_pages) {
                   13118:                REG8(AH) = 0x88;
                   13119:        } else if(REG16(BX) == 0) {
                   13120:                REG8(AH) = 0x89;
                   13121:        } else {
1.1.1.31  root     13122:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     13123:                        if(!ems_handles[i].allocated) {
                   13124:                                ems_allocate_pages(i, REG16(BX));
                   13125:                                REG8(AH) = 0x00;
                   13126:                                REG16(DX) = i;
                   13127:                                return;
                   13128:                        }
                   13129:                }
                   13130:                REG8(AH) = 0x85;
                   13131:        }
                   13132: }
                   13133: 
                   13134: inline void msdos_int_67h_44h()
                   13135: {
                   13136:        if(!support_ems) {
                   13137:                REG8(AH) = 0x84;
1.1.1.31  root     13138:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     13139:                REG8(AH) = 0x83;
                   13140:        } else if(!(REG16(BX) == 0xffff || REG16(BX) < ems_handles[REG16(DX)].pages)) {
                   13141:                REG8(AH) = 0x8a;
                   13142: //     } else if(!(REG8(AL) < 4)) {
                   13143: //             REG8(AH) = 0x8b;
                   13144:        } else if(REG16(BX) == 0xffff) {
                   13145:                ems_unmap_page(REG8(AL) & 3);
                   13146:                REG8(AH) = 0x00;
                   13147:        } else {
                   13148:                ems_map_page(REG8(AL) & 3, REG16(DX), REG16(BX));
                   13149:                REG8(AH) = 0x00;
                   13150:        }
                   13151: }
                   13152: 
                   13153: inline void msdos_int_67h_45h()
                   13154: {
                   13155:        if(!support_ems) {
                   13156:                REG8(AH) = 0x84;
1.1.1.31  root     13157:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     13158:                REG8(AH) = 0x83;
                   13159:        } else {
                   13160:                ems_release_pages(REG16(DX));
                   13161:                REG8(AH) = 0x00;
                   13162:        }
                   13163: }
                   13164: 
                   13165: inline void msdos_int_67h_46h()
                   13166: {
                   13167:        if(!support_ems) {
                   13168:                REG8(AH) = 0x84;
                   13169:        } else {
1.1.1.29  root     13170: //             REG16(AX) = 0x0032; // EMS 3.2
                   13171:                REG16(AX) = 0x0040; // EMS 4.0
1.1.1.19  root     13172:        }
                   13173: }
                   13174: 
                   13175: inline void msdos_int_67h_47h()
                   13176: {
                   13177:        // NOTE: the map data should be stored in the specified ems page, not process data
                   13178:        process_t *process = msdos_process_info_get(current_psp);
                   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 if(process->ems_pages_stored) {
                   13185:                REG8(AH) = 0x8d;
                   13186:        } else {
                   13187:                for(int i = 0; i < 4; i++) {
                   13188:                        process->ems_pages[i].handle = ems_pages[i].handle;
                   13189:                        process->ems_pages[i].page   = ems_pages[i].page;
                   13190:                        process->ems_pages[i].mapped = ems_pages[i].mapped;
                   13191:                }
                   13192:                process->ems_pages_stored = true;
                   13193:                REG8(AH) = 0x00;
                   13194:        }
                   13195: }
                   13196: 
                   13197: inline void msdos_int_67h_48h()
                   13198: {
                   13199:        // NOTE: the map data should be restored from the specified ems page, not process data
                   13200:        process_t *process = msdos_process_info_get(current_psp);
                   13201:        
                   13202:        if(!support_ems) {
                   13203:                REG8(AH) = 0x84;
1.1.1.31  root     13204: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     13205: //             REG8(AH) = 0x83;
                   13206:        } else if(!process->ems_pages_stored) {
                   13207:                REG8(AH) = 0x8e;
                   13208:        } else {
                   13209:                for(int i = 0; i < 4; i++) {
                   13210:                        if(process->ems_pages[i].mapped) {
                   13211:                                ems_map_page(i, process->ems_pages[i].handle, process->ems_pages[i].page);
                   13212:                        } else {
                   13213:                                ems_unmap_page(i);
                   13214:                        }
                   13215:                }
                   13216:                process->ems_pages_stored = false;
                   13217:                REG8(AH) = 0x00;
                   13218:        }
                   13219: }
                   13220: 
                   13221: inline void msdos_int_67h_4bh()
                   13222: {
                   13223:        if(!support_ems) {
                   13224:                REG8(AH) = 0x84;
                   13225:        } else {
                   13226:                REG8(AH) = 0x00;
                   13227:                REG16(BX) = 0;
1.1.1.31  root     13228:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     13229:                        if(ems_handles[i].allocated) {
                   13230:                                REG16(BX)++;
                   13231:                        }
                   13232:                }
                   13233:        }
                   13234: }
                   13235: 
                   13236: inline void msdos_int_67h_4ch()
                   13237: {
                   13238:        if(!support_ems) {
                   13239:                REG8(AH) = 0x84;
1.1.1.31  root     13240:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     13241:                REG8(AH) = 0x83;
                   13242:        } else {
                   13243:                REG8(AH) = 0x00;
                   13244:                REG16(BX) = ems_handles[REG16(DX)].pages;
                   13245:        }
                   13246: }
                   13247: 
                   13248: inline void msdos_int_67h_4dh()
                   13249: {
                   13250:        if(!support_ems) {
                   13251:                REG8(AH) = 0x84;
                   13252:        } else {
                   13253:                REG8(AH) = 0x00;
                   13254:                REG16(BX) = 0;
1.1.1.31  root     13255:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     13256:                        if(ems_handles[i].allocated) {
                   13257:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 0) = i;
                   13258:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 2) = ems_handles[i].pages;
                   13259:                                REG16(BX)++;
                   13260:                        }
                   13261:                }
                   13262:        }
                   13263: }
                   13264: 
1.1.1.20  root     13265: inline void msdos_int_67h_4eh()
                   13266: {
                   13267:        if(!support_ems) {
                   13268:                REG8(AH) = 0x84;
                   13269:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   13270:                if(REG8(AL) == 0x00 || REG8(AL) == 0x02) {
                   13271:                        // save page map
                   13272:                        for(int i = 0; i < 4; i++) {
                   13273:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   13274:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   13275:                        }
                   13276:                }
                   13277:                if(REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   13278:                        // restore page map
                   13279:                        for(int i = 0; i < 4; i++) {
                   13280:                                UINT16 handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   13281:                                UINT16 page   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   13282:                                
1.1.1.31  root     13283:                                if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
1.1.1.20  root     13284:                                        ems_map_page(i, handle, page);
                   13285:                                } else {
                   13286:                                        ems_unmap_page(i);
                   13287:                                }
                   13288:                        }
                   13289:                }
                   13290:                REG8(AH) = 0x00;
                   13291:        } else if(REG8(AL) == 0x03) {
                   13292:                REG8(AH) = 0x00;
1.1.1.21  root     13293:                REG8(AL) = 4 * 4;
                   13294:        } else {
1.1.1.22  root     13295:                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     13296:                REG8(AH) = 0x8f;
                   13297:        }
                   13298: }
                   13299: 
                   13300: inline void msdos_int_67h_4fh()
                   13301: {
                   13302:        if(!support_ems) {
                   13303:                REG8(AH) = 0x84;
                   13304:        } else if(REG8(AL) == 0x00) {
                   13305:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   13306:                
                   13307:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI)) = count;
                   13308:                for(int i = 0; i < count; i++) {
                   13309:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 2 * i);
                   13310:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   13311:                        
                   13312: //                     if(!(physical < 4)) {
                   13313: //                             REG8(AH) = 0x8b;
                   13314: //                             return;
                   13315: //                     }
                   13316:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 0) = segment;
                   13317:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 2) = ems_pages[physical & 3].handle;
                   13318:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 4) = ems_pages[physical & 3].mapped ? ems_pages[physical & 3].page : 0xffff;
                   13319:                }
                   13320:                REG8(AH) = 0x00;
                   13321:        } else if(REG8(AL) == 0x01) {
                   13322:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   13323:                
                   13324:                for(int i = 0; i < count; i++) {
                   13325:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 0);
                   13326:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   13327:                        UINT16 handle   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 2);
                   13328:                        UINT16 logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 4);
                   13329:                        
                   13330: //                     if(!(physical < 4)) {
                   13331: //                             REG8(AH) = 0x8b;
                   13332: //                             return;
                   13333: //                     } else
1.1.1.31  root     13334:                        if(!(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated)) {
1.1.1.21  root     13335:                                REG8(AH) = 0x83;
                   13336:                                return;
                   13337:                        } else if(logical == 0xffff) {
                   13338:                                ems_unmap_page(physical & 3);
                   13339:                        } else if(logical < ems_handles[handle].pages) {
                   13340:                                ems_map_page(physical & 3, handle, logical);
                   13341:                        } else {
                   13342:                                REG8(AH) = 0x8a;
                   13343:                                return;
                   13344:                        }
                   13345:                }
                   13346:                REG8(AH) = 0x00;
                   13347:        } else if(REG8(AL) == 0x02) {
                   13348:                REG8(AH) = 0x00;
                   13349:                REG8(AL) = 2 + REG16(BX) * 6;
1.1.1.20  root     13350:        } else {
1.1.1.22  root     13351:                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     13352:                REG8(AH) = 0x8f;
                   13353:        }
                   13354: }
                   13355: 
                   13356: inline void msdos_int_67h_50h()
                   13357: {
                   13358:        if(!support_ems) {
                   13359:                REG8(AH) = 0x84;
1.1.1.31  root     13360:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.20  root     13361:                REG8(AH) = 0x83;
                   13362:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   13363:                for(int i = 0; i < REG16(CX); i++) {
                   13364:                        int logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   13365:                        int physical = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   13366:                        
                   13367:                        if(REG8(AL) == 0x01) {
                   13368:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   13369:                        }
                   13370: //                     if(!(physical < 4)) {
                   13371: //                             REG8(AH) = 0x8b;
                   13372: //                             return;
                   13373: //                     } else
                   13374:                        if(logical == 0xffff) {
                   13375:                                ems_unmap_page(physical & 3);
                   13376:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   13377:                                ems_map_page(physical & 3, REG16(DX), logical);
                   13378:                        } else {
                   13379:                                REG8(AH) = 0x8a;
                   13380:                                return;
                   13381:                        }
                   13382:                }
                   13383:                REG8(AH) = 0x00;
                   13384:        } else {
1.1.1.22  root     13385:                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     13386:                REG8(AH) = 0x8f;
                   13387:        }
                   13388: }
                   13389: 
1.1.1.19  root     13390: inline void msdos_int_67h_51h()
                   13391: {
                   13392:        if(!support_ems) {
                   13393:                REG8(AH) = 0x84;
1.1.1.31  root     13394:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     13395:                REG8(AH) = 0x83;
                   13396:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   13397:                REG8(AH) = 0x87;
                   13398:        } else if(REG16(BX) > free_ems_pages + ems_handles[REG16(DX)].pages) {
                   13399:                REG8(AH) = 0x88;
                   13400:        } else {
                   13401:                ems_reallocate_pages(REG16(DX), REG16(BX));
                   13402:                REG8(AH) = 0x00;
                   13403:        }
                   13404: }
                   13405: 
1.1.1.20  root     13406: inline void msdos_int_67h_52h()
                   13407: {
                   13408:        if(!support_ems) {
                   13409:                REG8(AH) = 0x84;
1.1.1.31  root     13410: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   13411: //             REG8(AH) = 0x83;
1.1.1.20  root     13412:        } else if(REG8(AL) == 0x00) {
                   13413:                REG8(AL) = 0x00; // handle is volatile
                   13414:                REG8(AH) = 0x00;
                   13415:        } else if(REG8(AL) == 0x01) {
                   13416:                if(REG8(BL) == 0x00) {
                   13417:                        REG8(AH) = 0x00;
                   13418:                } else {
                   13419:                        REG8(AH) = 0x90; // undefined attribute type
                   13420:                }
                   13421:        } else if(REG8(AL) == 0x02) {
                   13422:                REG8(AL) = 0x00; // only volatile handles supported
                   13423:                REG8(AH) = 0x00;
                   13424:        } else {
1.1.1.22  root     13425:                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     13426:                REG8(AH) = 0x8f;
                   13427:        }
                   13428: }
                   13429: 
1.1.1.19  root     13430: inline void msdos_int_67h_53h()
                   13431: {
                   13432:        if(!support_ems) {
                   13433:                REG8(AH) = 0x84;
1.1.1.31  root     13434:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     13435:                REG8(AH) = 0x83;
                   13436:        } else if(REG8(AL) == 0x00) {
                   13437:                memcpy(mem + SREG_BASE(ES) + REG16(DI), ems_handles[REG16(DX)].name, 8);
                   13438:                REG8(AH) = 0x00;
                   13439:        } else if(REG8(AL) == 0x01) {
1.1.1.31  root     13440:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     13441:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   13442:                                REG8(AH) = 0xa1;
                   13443:                                return;
                   13444:                        }
                   13445:                }
                   13446:                REG8(AH) = 0x00;
                   13447:                memcpy(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8);
                   13448:        } else {
1.1.1.22  root     13449:                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     13450:                REG8(AH) = 0x8f;
1.1.1.19  root     13451:        }
                   13452: }
                   13453: 
                   13454: inline void msdos_int_67h_54h()
                   13455: {
                   13456:        if(!support_ems) {
                   13457:                REG8(AH) = 0x84;
                   13458:        } else if(REG8(AL) == 0x00) {
1.1.1.31  root     13459:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     13460:                        if(ems_handles[i].allocated) {
                   13461:                                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, ems_handles[i].name, 10);
                   13462:                        } else {
                   13463:                                memset(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, 0, 10);
                   13464:                        }
                   13465:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 0) = i;
                   13466:                }
                   13467:                REG8(AH) = 0x00;
                   13468:                REG8(AL) = MAX_EMS_HANDLES;
                   13469:        } else if(REG8(AL) == 0x01) {
                   13470:                REG8(AH) = 0xa0; // not found
1.1.1.31  root     13471:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     13472:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   13473:                                REG8(AH) = 0x00;
                   13474:                                REG16(DX) = i;
                   13475:                                break;
                   13476:                        }
                   13477:                }
                   13478:        } else if(REG8(AL) == 0x02) {
                   13479:                REG8(AH) = 0x00;
                   13480:                REG16(BX) = MAX_EMS_HANDLES;
                   13481:        } else {
1.1.1.22  root     13482:                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     13483:                REG8(AH) = 0x8f;
                   13484:        }
                   13485: }
                   13486: 
                   13487: inline void msdos_int_67h_57h_tmp()
                   13488: {
                   13489:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   13490:        UINT8  src_type    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   13491:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x05);
                   13492:        UINT16 src_ofs     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07);
                   13493:        UINT16 src_seg     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x09);
                   13494:        UINT8  dest_type   = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0b);
                   13495:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   13496:        UINT16 dest_ofs    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0e);
                   13497:        UINT16 dest_seg    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x10);
                   13498:        
1.1.1.32  root     13499:        UINT8 *src_buffer = NULL, *dest_buffer = NULL;
1.1.1.20  root     13500:        UINT32 src_addr, dest_addr;
                   13501:        UINT32 src_addr_max, dest_addr_max;
                   13502:        
                   13503:        if(src_type == 0) {
                   13504:                src_buffer = mem;
                   13505:                src_addr = (src_seg << 4) + src_ofs;
                   13506:                src_addr_max = MAX_MEM;
                   13507:        } else {
1.1.1.31  root     13508:                if(!(src_handle >= 1 && src_handle <= MAX_EMS_HANDLES && ems_handles[src_handle].allocated)) {
1.1.1.20  root     13509:                        REG8(AH) = 0x83;
                   13510:                        return;
                   13511:                } else if(!(src_seg < ems_handles[src_handle].pages)) {
                   13512:                        REG8(AH) = 0x8a;
                   13513:                        return;
                   13514:                }
1.1.1.32  root     13515:                if(ems_handles[src_handle].buffer != NULL) {
                   13516:                        src_buffer = ems_handles[src_handle].buffer + 0x4000 * src_seg;
                   13517:                }
1.1.1.20  root     13518:                src_addr = src_ofs;
1.1.1.32  root     13519:                src_addr_max = 0x4000 * (ems_handles[src_handle].pages - src_seg);
1.1.1.20  root     13520:        }
                   13521:        if(dest_type == 0) {
                   13522:                dest_buffer = mem;
                   13523:                dest_addr = (dest_seg << 4) + dest_ofs;
                   13524:                dest_addr_max = MAX_MEM;
                   13525:        } else {
1.1.1.31  root     13526:                if(!(dest_handle >= 1 && dest_handle <= MAX_EMS_HANDLES && ems_handles[dest_handle].allocated)) {
1.1.1.20  root     13527:                        REG8(AH) = 0x83;
                   13528:                        return;
                   13529:                } else if(!(dest_seg < ems_handles[dest_handle].pages)) {
                   13530:                        REG8(AH) = 0x8a;
                   13531:                        return;
                   13532:                }
1.1.1.32  root     13533:                if(ems_handles[dest_handle].buffer != NULL) {
                   13534:                        dest_buffer = ems_handles[dest_handle].buffer + 0x4000 * dest_seg;
                   13535:                }
1.1.1.20  root     13536:                dest_addr = dest_ofs;
1.1.1.32  root     13537:                dest_addr_max = 0x4000 * (ems_handles[dest_handle].pages - dest_seg);
1.1.1.20  root     13538:        }
1.1.1.32  root     13539:        if(src_buffer != NULL && dest_buffer != NULL) {
                   13540:                for(int i = 0; i < copy_length; i++) {
                   13541:                        if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   13542:                                if(REG8(AL) == 0x00) {
                   13543:                                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   13544:                                } else if(REG8(AL) == 0x01) {
                   13545:                                        UINT8 tmp = dest_buffer[dest_addr];
                   13546:                                        dest_buffer[dest_addr++] = src_buffer[src_addr];
                   13547:                                        src_buffer[src_addr++] = tmp;
                   13548:                                }
                   13549:                        } else {
                   13550:                                REG8(AH) = 0x93;
                   13551:                                return;
1.1.1.20  root     13552:                        }
                   13553:                }
1.1.1.32  root     13554:                REG8(AH) = 0x00;
                   13555:        } else {
                   13556:                REG8(AH) = 0x80;
1.1.1.20  root     13557:        }
                   13558: }
                   13559: 
                   13560: inline void msdos_int_67h_57h()
                   13561: {
                   13562:        if(!support_ems) {
                   13563:                REG8(AH) = 0x84;
                   13564:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   13565:                struct {
                   13566:                        UINT16 handle;
                   13567:                        UINT16 page;
                   13568:                        bool mapped;
                   13569:                } tmp_pages[4];
                   13570:                
                   13571:                // unmap pages to copy memory data to ems buffer
                   13572:                for(int i = 0; i < 4; i++) {
                   13573:                        tmp_pages[i].handle = ems_pages[i].handle;
                   13574:                        tmp_pages[i].page   = ems_pages[i].page;
                   13575:                        tmp_pages[i].mapped = ems_pages[i].mapped;
                   13576:                        ems_unmap_page(i);
                   13577:                }
                   13578:                
                   13579:                // run move/exchange operation
                   13580:                msdos_int_67h_57h_tmp();
                   13581:                
                   13582:                // restore unmapped pages
                   13583:                for(int i = 0; i < 4; i++) {
                   13584:                        if(tmp_pages[i].mapped) {
                   13585:                                ems_map_page(i, tmp_pages[i].handle, tmp_pages[i].page);
                   13586:                        }
                   13587:                }
                   13588:        } else {
1.1.1.22  root     13589:                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     13590:                REG8(AH) = 0x8f;
                   13591:        }
                   13592: }
                   13593: 
                   13594: inline void msdos_int_67h_58h()
                   13595: {
                   13596:        if(!support_ems) {
                   13597:                REG8(AH) = 0x84;
                   13598:        } else if(REG8(AL) == 0x00) {
                   13599:                for(int i = 0; i < 4; i++) {
1.1.1.30  root     13600:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = (EMS_TOP + 0x4000 * i) >> 4;
                   13601:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = i;
1.1.1.20  root     13602:                }
                   13603:                REG8(AH) = 0x00;
                   13604:                REG16(CX) = 4;
                   13605:        } else if(REG8(AL) == 0x01) {
                   13606:                REG8(AH) = 0x00;
                   13607:                REG16(CX) = 4;
                   13608:        } else {
1.1.1.22  root     13609:                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     13610:                REG8(AH) = 0x8f;
                   13611:        }
                   13612: }
                   13613: 
                   13614: inline void msdos_int_67h_5ah()
                   13615: {
                   13616:        if(!support_ems) {
1.1.1.19  root     13617:                REG8(AH) = 0x84;
1.1.1.20  root     13618:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   13619:                REG8(AH) = 0x87;
                   13620:        } else if(REG16(BX) > free_ems_pages) {
                   13621:                REG8(AH) = 0x88;
                   13622: //     } else if(REG16(BX) == 0) {
                   13623: //             REG8(AH) = 0x89;
                   13624:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
1.1.1.31  root     13625:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.20  root     13626:                        if(!ems_handles[i].allocated) {
                   13627:                                ems_allocate_pages(i, REG16(BX));
                   13628:                                REG8(AH) = 0x00;
                   13629:                                REG16(DX) = i;
                   13630:                                return;
                   13631:                        }
                   13632:                }
                   13633:                REG8(AH) = 0x85;
                   13634:        } else {
1.1.1.22  root     13635:                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     13636:                REG8(AH) = 0x8f;
1.1.1.19  root     13637:        }
                   13638: }
                   13639: 
1.1.1.30  root     13640: inline void msdos_int_67h_deh()
                   13641: {
                   13642:        REG8(AH) = 0x84;
                   13643: }
                   13644: 
1.1.1.19  root     13645: #ifdef SUPPORT_XMS
                   13646: 
1.1.1.32  root     13647: void msdos_xms_init()
1.1.1.26  root     13648: {
1.1.1.30  root     13649:        emb_handle_top = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   13650:        emb_handle_top->address = EMB_TOP;
                   13651:        emb_handle_top->size_kb = (EMB_END - EMB_TOP) >> 10;
1.1.1.26  root     13652:        xms_a20_local_enb_count = 0;
                   13653: }
                   13654: 
1.1.1.32  root     13655: void msdos_xms_finish()
                   13656: {
                   13657:        msdos_xms_release();
                   13658: }
                   13659: 
                   13660: void msdos_xms_release()
1.1.1.30  root     13661: {
                   13662:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL;) {
                   13663:                emb_handle_t *next_handle = emb_handle->next;
                   13664:                free(emb_handle);
                   13665:                emb_handle = next_handle;
                   13666:        }
                   13667: }
                   13668: 
                   13669: emb_handle_t *msdos_xms_get_emb_handle(int handle)
                   13670: {
                   13671:        if(handle != 0) {
                   13672:                for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   13673:                        if(emb_handle->handle == handle) {
                   13674:                                return(emb_handle);
                   13675:                        }
                   13676:                }
                   13677:        }
                   13678:        return(NULL);
                   13679: }
                   13680: 
                   13681: int msdos_xms_get_unused_emb_handle_id()
                   13682: {
                   13683:        for(int handle = 1;; handle++) {
                   13684:                if(msdos_xms_get_emb_handle(handle) == NULL) {
                   13685:                        return(handle);
                   13686:                }
                   13687:        }
                   13688:        return(0);
                   13689: }
                   13690: 
                   13691: int msdos_xms_get_unused_emb_handle_count()
                   13692: {
                   13693:        int count = 64; //255;
                   13694:        
                   13695:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   13696:                if(emb_handle->handle != 0) {
                   13697:                        if(--count == 1) {
                   13698:                                break;
                   13699:                        }
                   13700:                }
                   13701:        }
                   13702:        return(count);
                   13703: }
                   13704: 
                   13705: void msdos_xms_split_emb_handle(emb_handle_t *emb_handle, int size_kb)
                   13706: {
                   13707:        if(emb_handle->size_kb > size_kb) {
                   13708:                emb_handle_t *new_handle = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   13709:                
                   13710:                new_handle->address = emb_handle->address + size_kb * 1024;
                   13711:                new_handle->size_kb = emb_handle->size_kb - size_kb;
                   13712:                emb_handle->size_kb = size_kb;
                   13713:                
                   13714:                new_handle->prev = emb_handle;
                   13715:                new_handle->next = emb_handle->next;
                   13716:                if(emb_handle->next != NULL) {
                   13717:                        emb_handle->next->prev = new_handle;
                   13718:                }
                   13719:                emb_handle->next = new_handle;
                   13720:        }
                   13721: }
                   13722: 
                   13723: void msdos_xms_combine_emb_handles(emb_handle_t *emb_handle)
                   13724: {
                   13725:        emb_handle_t *next_handle = emb_handle->next;
                   13726:        
                   13727:        if(next_handle != NULL) {
                   13728:                emb_handle->size_kb += next_handle->size_kb;
                   13729:                
                   13730:                if(next_handle->next != NULL) {
                   13731:                        next_handle->next->prev = emb_handle;
                   13732:                }
                   13733:                emb_handle->next = next_handle->next;
                   13734:                free(next_handle);
                   13735:        }
                   13736: }
                   13737: 
                   13738: emb_handle_t *msdos_xms_alloc_emb_handle(int size_kb)
                   13739: {
                   13740:        emb_handle_t *target_handle = NULL;
                   13741:        
                   13742:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   13743:                if(emb_handle->handle == 0 && emb_handle->size_kb >= size_kb) {
                   13744:                        if(target_handle == NULL || target_handle->size_kb > emb_handle->size_kb) {
                   13745:                                target_handle = emb_handle;
                   13746:                        }
                   13747:                }
                   13748:        }
                   13749:        if(target_handle != NULL) {
                   13750:                if(target_handle->size_kb > size_kb) {
                   13751:                        msdos_xms_split_emb_handle(target_handle, size_kb);
                   13752:                }
                   13753: //             target_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   13754:                return(target_handle);
                   13755:        }
                   13756:        return(NULL);
                   13757: }
                   13758: 
                   13759: void msdos_xms_free_emb_handle(emb_handle_t *emb_handle)
                   13760: {
                   13761:        emb_handle_t *prev_handle = emb_handle->prev;
                   13762:        emb_handle_t *next_handle = emb_handle->next;
                   13763:        
                   13764:        if(prev_handle != NULL && prev_handle->handle == 0) {
                   13765:                msdos_xms_combine_emb_handles(prev_handle);
                   13766:                emb_handle = prev_handle;
                   13767:        }
                   13768:        if(next_handle != NULL && next_handle->handle == 0) {
                   13769:                msdos_xms_combine_emb_handles(emb_handle);
                   13770:        }
                   13771:        emb_handle->handle = 0;
                   13772: }
                   13773: 
1.1.1.19  root     13774: inline void msdos_call_xms_00h()
                   13775: {
1.1.1.29  root     13776: #if defined(HAS_I386)
                   13777:        REG16(AX) = 0x0300; // V3.00 (XMS Version)
                   13778:        REG16(BX) = 0x035f; // V3.95 (Driver Revision)
                   13779: #else
                   13780:        REG16(AX) = 0x0200; // V2.00 (XMS Version)
                   13781:        REG16(BX) = 0x0270; // V2.70 (Driver Revision)
                   13782: #endif
                   13783: //     REG16(DX) = 0x0000; // HMA does not exist
                   13784:        REG16(DX) = 0x0001; // HMA does exist
1.1.1.19  root     13785: }
                   13786: 
                   13787: inline void msdos_call_xms_01h()
                   13788: {
1.1.1.29  root     13789:        if(REG8(AL) == 0x40) {
                   13790:                // HIMEM.SYS will fail function 01h with error code 91h if AL=40h and
                   13791:                // DX=KB free extended memory returned by last call of function 08h
                   13792:                REG16(AX) = 0x0000;
                   13793:                REG8(BL) = 0x91;
                   13794:                REG16(DX) = xms_dx_after_call_08h;
                   13795:        } else if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   13796:                REG16(AX) = 0x0000;
                   13797:                REG8(BL) = 0x81; // Vdisk was detected
                   13798: #ifdef SUPPORT_HMA
                   13799:        } else if(is_hma_used_by_int_2fh) {
                   13800:                REG16(AX) = 0x0000;
                   13801:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   13802:        } else if(is_hma_used_by_xms) {
                   13803:                REG16(AX) = 0x0000;
                   13804:                REG8(BL) = 0x91; // HMA is already in use
                   13805:        } else {
                   13806:                REG16(AX) = 0x0001;
                   13807:                is_hma_used_by_xms = true;
                   13808: #else
                   13809:        } else {
                   13810:                REG16(AX) = 0x0000;
                   13811:                REG8(BL) = 0x91; // HMA is already in use
                   13812: #endif
                   13813:        }
1.1.1.19  root     13814: }
                   13815: 
                   13816: inline void msdos_call_xms_02h()
                   13817: {
1.1.1.29  root     13818:        if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   13819:                REG16(AX) = 0x0000;
                   13820:                REG8(BL) = 0x81; // Vdisk was detected
                   13821: #ifdef SUPPORT_HMA
                   13822:        } else if(is_hma_used_by_int_2fh) {
                   13823:                REG16(AX) = 0x0000;
                   13824:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   13825:        } else if(!is_hma_used_by_xms) {
                   13826:                REG16(AX) = 0x0000;
                   13827:                REG8(BL) = 0x93; // HMA is not allocated
                   13828:        } else {
                   13829:                REG16(AX) = 0x0001;
                   13830:                is_hma_used_by_xms = false;
                   13831:                // restore first free mcb in high memory area
                   13832:                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   13833: #else
                   13834:        } else {
                   13835:                REG16(AX) = 0x0000;
                   13836:                REG8(BL) = 0x91; // HMA is already in use
                   13837: #endif
                   13838:        }
1.1.1.19  root     13839: }
                   13840: 
                   13841: inline void msdos_call_xms_03h()
                   13842: {
                   13843:        i386_set_a20_line(1);
                   13844:        REG16(AX) = 0x0001;
                   13845:        REG8(BL) = 0x00;
                   13846: }
                   13847: 
                   13848: inline void msdos_call_xms_04h()
                   13849: {
1.1.1.21  root     13850:        i386_set_a20_line(0);
                   13851:        REG16(AX) = 0x0001;
                   13852:        REG8(BL) = 0x00;
1.1.1.19  root     13853: }
                   13854: 
                   13855: inline void msdos_call_xms_05h()
                   13856: {
                   13857:        i386_set_a20_line(1);
                   13858:        REG16(AX) = 0x0001;
                   13859:        REG8(BL) = 0x00;
1.1.1.21  root     13860:        xms_a20_local_enb_count++;
1.1.1.19  root     13861: }
                   13862: 
                   13863: void msdos_call_xms_06h()
                   13864: {
1.1.1.21  root     13865:        if(xms_a20_local_enb_count > 0) {
                   13866:                xms_a20_local_enb_count--;
                   13867:        }
                   13868:        if(xms_a20_local_enb_count == 0) {
1.1.1.19  root     13869:                i386_set_a20_line(0);
1.1.1.21  root     13870:        }
                   13871:        if((m_a20_mask >> 20) & 1) {
1.1.1.19  root     13872:                REG16(AX) = 0x0000;
                   13873:                REG8(BL) = 0x94;
1.1.1.21  root     13874:        } else {
                   13875:                REG16(AX) = 0x0001;
                   13876:                REG8(BL) = 0x00;
1.1.1.19  root     13877:        }
                   13878: }
                   13879: 
                   13880: inline void msdos_call_xms_07h()
                   13881: {
                   13882:        REG16(AX) = (m_a20_mask >> 20) & 1;
                   13883:        REG8(BL) = 0x00;
                   13884: }
                   13885: 
                   13886: inline void msdos_call_xms_08h()
                   13887: {
                   13888:        REG16(AX) = REG16(DX) = 0x0000;
                   13889:        
1.1.1.30  root     13890:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   13891:                if(emb_handle->handle == 0) {
                   13892:                        if(REG16(AX) < emb_handle->size_kb) {
                   13893:                                REG16(AX) = emb_handle->size_kb;
1.1.1.19  root     13894:                        }
1.1.1.30  root     13895:                        REG16(DX) += emb_handle->size_kb;
1.1.1.19  root     13896:                }
                   13897:        }
                   13898:        
                   13899:        if(REG16(AX) == 0 && REG16(DX) == 0) {
                   13900:                REG8(BL) = 0xa0;
                   13901:        } else {
                   13902:                REG8(BL) = 0x00;
                   13903:        }
1.1.1.29  root     13904:        xms_dx_after_call_08h = REG16(DX);
1.1.1.19  root     13905: }
                   13906: 
1.1.1.30  root     13907: void msdos_call_xms_09h(int size_kb)
1.1.1.19  root     13908: {
1.1.1.30  root     13909:        emb_handle_t *emb_handle = msdos_xms_alloc_emb_handle(size_kb);
                   13910:        
                   13911:        if(emb_handle != NULL) {
                   13912:                emb_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   13913:                
                   13914:                REG16(AX) = 0x0001;
                   13915:                REG16(DX) = emb_handle->handle;
                   13916:                REG8(BL) = 0x00;
                   13917:        } else {
                   13918:                REG16(AX) = REG16(DX) = 0x0000;
                   13919:                REG8(BL) = 0xa0;
1.1.1.19  root     13920:        }
1.1.1.30  root     13921: }
                   13922: 
                   13923: inline void msdos_call_xms_09h()
                   13924: {
                   13925:        msdos_call_xms_09h(REG16(DX));
1.1.1.19  root     13926: }
                   13927: 
                   13928: inline void msdos_call_xms_0ah()
                   13929: {
1.1.1.30  root     13930:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   13931:        
                   13932:        if(emb_handle == NULL) {
1.1.1.19  root     13933:                REG16(AX) = 0x0000;
                   13934:                REG8(BL) = 0xa2;
1.1.1.30  root     13935:        } else if(emb_handle->lock > 0) {
1.1.1.19  root     13936:                REG16(AX) = 0x0000;
                   13937:                REG8(BL) = 0xab;
                   13938:        } else {
1.1.1.30  root     13939:                msdos_xms_free_emb_handle(emb_handle);
1.1.1.19  root     13940:                
                   13941:                REG16(AX) = 0x0001;
                   13942:                REG8(BL) = 0x00;
                   13943:        }
                   13944: }
                   13945: 
                   13946: inline void msdos_call_xms_0bh()
                   13947: {
                   13948:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   13949:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   13950:        UINT32 src_addr    = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06);
                   13951:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0a);
                   13952:        UINT32 dest_addr   = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   13953:        
                   13954:        UINT8 *src_buffer, *dest_buffer;
                   13955:        UINT32 src_addr_max, dest_addr_max;
1.1.1.30  root     13956:        emb_handle_t *emb_handle;
1.1.1.19  root     13957:        
                   13958:        if(src_handle == 0) {
                   13959:                src_buffer = mem;
                   13960:                src_addr = (((src_addr >> 16) & 0xffff) << 4) + (src_addr & 0xffff);
                   13961:                src_addr_max = MAX_MEM;
1.1.1.30  root     13962: 
1.1.1.19  root     13963:        } else {
1.1.1.30  root     13964:                if((emb_handle = msdos_xms_get_emb_handle(src_handle)) == NULL) {
1.1.1.19  root     13965:                        REG16(AX) = 0x0000;
                   13966:                        REG8(BL) = 0xa3;
                   13967:                        return;
                   13968:                }
1.1.1.30  root     13969:                src_buffer = mem + emb_handle->address;
                   13970:                src_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     13971:        }
                   13972:        if(dest_handle == 0) {
                   13973:                dest_buffer = mem;
                   13974:                dest_addr = (((dest_addr >> 16) & 0xffff) << 4) + (dest_addr & 0xffff);
                   13975:                dest_addr_max = MAX_MEM;
                   13976:        } else {
1.1.1.30  root     13977:                if((emb_handle = msdos_xms_get_emb_handle(dest_handle)) == NULL) {
1.1.1.19  root     13978:                        REG16(AX) = 0x0000;
                   13979:                        REG8(BL) = 0xa5;
                   13980:                        return;
                   13981:                }
1.1.1.30  root     13982:                dest_buffer = mem + emb_handle->address;
                   13983:                dest_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     13984:        }
                   13985:        for(int i = 0; i < copy_length; i++) {
                   13986:                if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   13987:                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   13988:                } else {
                   13989:                        break;
                   13990:                }
                   13991:        }
                   13992:        REG16(AX) = 0x0001;
                   13993:        REG8(BL) = 0x00;
                   13994: }
                   13995: 
                   13996: inline void msdos_call_xms_0ch()
                   13997: {
1.1.1.30  root     13998:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   13999:        
                   14000:        if(emb_handle == NULL) {
1.1.1.19  root     14001:                REG16(AX) = 0x0000;
                   14002:                REG8(BL) = 0xa2;
                   14003:        } else {
1.1.1.30  root     14004:                emb_handle->lock++;
1.1.1.19  root     14005:                REG16(AX) = 0x0001;
                   14006:                REG8(BL) = 0x00;
1.1.1.30  root     14007:                REG16(DX) = (emb_handle->address >> 16) & 0xffff;
                   14008:                REG16(BX) = (emb_handle->address      ) & 0xffff;
1.1.1.19  root     14009:        }
                   14010: }
                   14011: 
                   14012: inline void msdos_call_xms_0dh()
                   14013: {
1.1.1.30  root     14014:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   14015:        
                   14016:        if(emb_handle == NULL) {
1.1.1.19  root     14017:                REG16(AX) = 0x0000;
                   14018:                REG8(BL) = 0xa2;
1.1.1.30  root     14019:        } else if(!(emb_handle->lock > 0)) {
1.1.1.19  root     14020:                REG16(AX) = 0x0000;
                   14021:                REG8(BL) = 0xaa;
                   14022:        } else {
1.1.1.30  root     14023:                emb_handle->lock--;
1.1.1.19  root     14024:                REG16(AX) = 0x0001;
                   14025:                REG8(BL) = 0x00;
                   14026:        }
                   14027: }
                   14028: 
                   14029: inline void msdos_call_xms_0eh()
                   14030: {
1.1.1.30  root     14031:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   14032:        
                   14033:        if(emb_handle == NULL) {
1.1.1.19  root     14034:                REG16(AX) = 0x0000;
                   14035:                REG8(BL) = 0xa2;
                   14036:        } else {
                   14037:                REG16(AX) = 0x0001;
1.1.1.30  root     14038:                REG8(BH) = emb_handle->lock;
                   14039:                REG8(BL) = msdos_xms_get_unused_emb_handle_count();
                   14040:                REG16(DX) = emb_handle->size_kb;
1.1.1.19  root     14041:        }
                   14042: }
                   14043: 
1.1.1.30  root     14044: void msdos_call_xms_0fh(int size_kb)
1.1.1.19  root     14045: {
1.1.1.30  root     14046:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   14047:        
                   14048:        if(emb_handle == NULL) {
1.1.1.19  root     14049:                REG16(AX) = 0x0000;
                   14050:                REG8(BL) = 0xa2;
1.1.1.30  root     14051:        } else if(emb_handle->lock > 0) {
1.1.1.19  root     14052:                REG16(AX) = 0x0000;
                   14053:                REG8(BL) = 0xab;
                   14054:        } else {
1.1.1.30  root     14055:                if(emb_handle->size_kb < size_kb) {
                   14056:                        if(emb_handle->next != NULL && emb_handle->next->handle == 0 && (emb_handle->size_kb + emb_handle->next->size_kb) >= size_kb) {
                   14057:                                msdos_xms_combine_emb_handles(emb_handle);
                   14058:                                if(emb_handle->size_kb > size_kb) {
                   14059:                                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   14060:                                }
                   14061:                        } else {
                   14062:                                int old_handle = emb_handle->handle;
                   14063:                                int old_size_kb = emb_handle->size_kb;
                   14064:                                UINT8 *buffer = (UINT8 *)malloc(old_size_kb * 1024);
                   14065:                                
                   14066:                                memcpy(buffer, mem + emb_handle->address, old_size_kb * 1024);
                   14067:                                msdos_xms_free_emb_handle(emb_handle);
                   14068:                                
                   14069:                                if((emb_handle = msdos_xms_alloc_emb_handle(size_kb)) == NULL) {
                   14070:                                        emb_handle = msdos_xms_alloc_emb_handle(old_size_kb); // should be always successed
                   14071:                                }
                   14072:                                emb_handle->handle = old_handle;
                   14073:                                memcpy(mem + emb_handle->address, buffer, old_size_kb * 1024);
                   14074:                                free(buffer);
                   14075:                        }
                   14076:                } else if(emb_handle->size_kb > size_kb) {
                   14077:                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   14078:                }
                   14079:                if(emb_handle->size_kb != size_kb) {
                   14080:                        REG16(AX) = 0x0000;
                   14081:                        REG8(BL) = 0xa0;
                   14082:                } else {
                   14083:                        REG16(AX) = 0x0001;
                   14084:                        REG8(BL) = 0x00;
                   14085:                }
1.1.1.19  root     14086:        }
                   14087: }
                   14088: 
1.1.1.30  root     14089: inline void msdos_call_xms_0fh()
                   14090: {
                   14091:        msdos_call_xms_0fh(REG16(BX));
                   14092: }
                   14093: 
1.1.1.19  root     14094: inline void msdos_call_xms_10h()
                   14095: {
                   14096:        int seg;
                   14097:        
                   14098:        if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(DX), 0)) != -1) {
                   14099:                REG16(AX) = 0x0001;
                   14100:                REG16(BX) = seg;
                   14101:        } else {
                   14102:                REG16(AX) = 0x0000;
                   14103:                REG8(BL) = 0xb0;
                   14104:                REG16(DX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   14105:        }
                   14106: }
                   14107: 
                   14108: inline void msdos_call_xms_11h()
                   14109: {
                   14110:        int mcb_seg = REG16(DX) - 1;
                   14111:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   14112:        
                   14113:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   14114:                msdos_mem_free(REG16(DX));
                   14115:                REG16(AX) = 0x0001;
                   14116:                REG8(BL) = 0x00;
                   14117:        } else {
                   14118:                REG16(AX) = 0x0000;
                   14119:                REG8(BL) = 0xb2;
                   14120:        }
                   14121: }
                   14122: 
                   14123: inline void msdos_call_xms_12h()
                   14124: {
                   14125:        int mcb_seg = REG16(DX) - 1;
                   14126:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   14127:        int max_paragraphs;
                   14128:        
                   14129:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   14130:                if(!msdos_mem_realloc(REG16(DX), REG16(BX), &max_paragraphs)) {
                   14131:                        REG16(AX) = 0x0001;
                   14132:                        REG8(BL) = 0x00;
                   14133:                } else {
                   14134:                        REG16(AX) = 0x0000;
                   14135:                        REG8(BL) = 0xb0;
                   14136:                        REG16(DX) = max_paragraphs;
                   14137:                }
                   14138:        } else {
                   14139:                REG16(AX) = 0x0000;
                   14140:                REG8(BL) = 0xb2;
                   14141:        }
                   14142: }
                   14143: 
1.1.1.29  root     14144: #if defined(HAS_I386)
                   14145: 
                   14146: inline void msdos_call_xms_88h()
                   14147: {
                   14148:        REG32(EAX) = REG32(EDX) = 0x0000;
                   14149:        
1.1.1.30  root     14150:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   14151:                if(emb_handle->handle == 0) {
                   14152:                        if(REG32(EAX) < emb_handle->size_kb) {
                   14153:                                REG32(EAX) = emb_handle->size_kb;
1.1.1.29  root     14154:                        }
1.1.1.30  root     14155:                        REG32(EDX) += emb_handle->size_kb;
1.1.1.29  root     14156:                }
                   14157:        }
                   14158:        
                   14159:        if(REG32(EAX) == 0 && REG32(EDX) == 0) {
                   14160:                REG8(BL) = 0xa0;
                   14161:        } else {
                   14162:                REG8(BL) = 0x00;
                   14163:        }
                   14164:        REG32(ECX) = EMB_END - 1;
                   14165: }
                   14166: 
                   14167: inline void msdos_call_xms_89h()
                   14168: {
1.1.1.30  root     14169:        msdos_call_xms_09h(REG32(EDX));
1.1.1.29  root     14170: }
                   14171: 
                   14172: inline void msdos_call_xms_8eh()
                   14173: {
1.1.1.30  root     14174:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   14175:        
                   14176:        if(emb_handle == NULL) {
1.1.1.29  root     14177:                REG16(AX) = 0x0000;
                   14178:                REG8(BL) = 0xa2;
                   14179:        } else {
                   14180:                REG16(AX) = 0x0001;
1.1.1.30  root     14181:                REG8(BH) = emb_handle->lock;
                   14182:                REG16(CX) = msdos_xms_get_unused_emb_handle_count();
                   14183:                REG32(EDX) = emb_handle->size_kb;
1.1.1.29  root     14184:        }
                   14185: }
                   14186: 
                   14187: inline void msdos_call_xms_8fh()
                   14188: {
1.1.1.30  root     14189:        msdos_call_xms_0fh(REG32(EBX));
1.1.1.29  root     14190: }
                   14191: 
                   14192: #endif
1.1.1.19  root     14193: #endif
                   14194: 
1.1.1.26  root     14195: UINT16 msdos_get_equipment()
                   14196: {
                   14197:        static UINT16 equip = 0;
                   14198:        
                   14199:        if(equip == 0) {
                   14200: #ifdef SUPPORT_FPU
                   14201:                equip |= (1 << 1);      // 80x87 coprocessor installed
                   14202: #endif
                   14203:                equip |= (1 << 2);      // pointing device installed (PS/2)
                   14204:                equip |= (2 << 4);      // initial video mode (80x25 color)
                   14205: //             equip |= (1 << 8);      // 0 if DMA installed
                   14206:                equip |= (2 << 9);      // number of serial ports
                   14207:                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     14208:                
                   14209:                // check only A: and B: if it is floppy drive
                   14210:                DWORD dwDrives = GetLogicalDrives();
                   14211:                int n = 0;
                   14212:                for(int i = 0; i < 2; i++) {
                   14213:                        if(dwDrives & (1 << i)) {
                   14214:                                char volume[] = "A:\\";
                   14215:                                volume[0] = 'A' + i;
                   14216:                                if(GetDriveType(volume) == DRIVE_REMOVABLE) {
                   14217:                                        n++;
                   14218:                                }
                   14219:                        }
                   14220:                }
                   14221:                if(n != 0) {
                   14222:                        equip |= (1 << 0);      // floppy disk(s) installed
                   14223:                        n--;
                   14224:                        equip |= (n << 6);      // number of floppies installed less 1
                   14225:                }
                   14226: //             if(joyGetNumDevs() != 0) {
                   14227: //                     equip |= (1 << 12);     // game port installed
                   14228: //             }
1.1.1.26  root     14229:        }
                   14230:        return(equip);
                   14231: }
                   14232: 
1.1       root     14233: void msdos_syscall(unsigned num)
                   14234: {
1.1.1.22  root     14235: #ifdef ENABLE_DEBUG_SYSCALL
                   14236:        if(num == 0x68) {
                   14237:                // dummy interrupt for EMS (int 67h)
1.1.1.33  root     14238:                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     14239:        } else if(num == 0x69) {
                   14240:                // dummy interrupt for XMS (call far)
1.1.1.33  root     14241:                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     14242:        } else if(num == 0x6a) {
                   14243:                // dummy interrupt for case map routine pointed in the country info
                   14244:        } else {
1.1.1.33  root     14245:                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     14246:        }
                   14247: #endif
1.1.1.36  root     14248:        // update cursor position
                   14249:        if(cursor_moved) {
                   14250:                pcbios_update_cursor_position();
                   14251:                cursor_moved = false;
                   14252:        }
1.1.1.33  root     14253:        ctrl_break_detected = ctrl_break_pressed = ctrl_c_pressed = false;
1.1.1.22  root     14254:        
1.1       root     14255:        switch(num) {
                   14256:        case 0x00:
1.1.1.28  root     14257:                try {
                   14258:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   14259:                        error("division by zero\n");
                   14260:                } catch(...) {
                   14261:                        fatalerror("division by zero detected, and failed to terminate current process\n");
                   14262:                }
1.1       root     14263:                break;
                   14264:        case 0x04:
1.1.1.28  root     14265:                try {
                   14266:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   14267:                        error("overflow\n");
                   14268:                } catch(...) {
                   14269:                        fatalerror("overflow detected, and failed to terminate current process\n");
                   14270:                }
1.1       root     14271:                break;
                   14272:        case 0x06:
                   14273:                // NOTE: ish.com has illegal instruction...
1.1.1.14  root     14274:                if(!ignore_illegal_insn) {
1.1.1.28  root     14275:                        try {
                   14276:                                msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   14277:                                error("illegal instruction\n");
                   14278:                        } catch(...) {
                   14279:                                fatalerror("illegal instruction detected, and failed to terminate current process\n");
                   14280:                        }
1.1.1.14  root     14281:                } else {
                   14282: #if defined(HAS_I386)
1.1.1.39  root     14283:                        m_eip = m_int6h_skip_eip;
                   14284: #elif defined(HAS_I286)
                   14285:                        m_pc = m_int6h_skip_pc;
1.1.1.14  root     14286: #else
1.1.1.39  root     14287:                        // 8086/80186 ignore an invalid opcode
1.1.1.14  root     14288: #endif
                   14289:                }
1.1       root     14290:                break;
1.1.1.33  root     14291:        case 0x09:
                   14292:                // ctrl-break is pressed
                   14293:                if(raise_int_1bh) {
                   14294: #if defined(HAS_I386)
                   14295:                        m_ext = 0; // not an external interrupt
                   14296:                        i386_trap(0x1b, 1, 0);
                   14297:                        m_ext = 1;
                   14298: #else
                   14299:                        PREFIX86(_interrupt)(0x1b);
                   14300: #endif
                   14301:                        raise_int_1bh = false;
                   14302:                }
1.1.1.8   root     14303:        case 0x08:
1.1.1.14  root     14304: //             pcbios_irq0(); // this causes too slow emulation...
1.1.1.8   root     14305:        case 0x0b:
                   14306:        case 0x0c:
                   14307:        case 0x0d:
                   14308:        case 0x0e:
                   14309:        case 0x0f:
                   14310:                // EOI
                   14311:                pic[0].isr &= ~(1 << (num - 0x08));
                   14312:                pic_update();
                   14313:                break;
1.1       root     14314:        case 0x10:
                   14315:                // PC BIOS - Video
1.1.1.14  root     14316:                if(!restore_console_on_exit) {
1.1.1.15  root     14317:                        change_console_size(scr_width, scr_height);
1.1       root     14318:                }
1.1.1.3   root     14319:                m_CF = 0;
1.1       root     14320:                switch(REG8(AH)) {
1.1.1.16  root     14321:                case 0x00: pcbios_int_10h_00h(); break;
1.1       root     14322:                case 0x01: pcbios_int_10h_01h(); break;
                   14323:                case 0x02: pcbios_int_10h_02h(); break;
                   14324:                case 0x03: pcbios_int_10h_03h(); break;
                   14325:                case 0x05: pcbios_int_10h_05h(); break;
                   14326:                case 0x06: pcbios_int_10h_06h(); break;
                   14327:                case 0x07: pcbios_int_10h_07h(); break;
                   14328:                case 0x08: pcbios_int_10h_08h(); break;
                   14329:                case 0x09: pcbios_int_10h_09h(); break;
                   14330:                case 0x0a: pcbios_int_10h_0ah(); break;
                   14331:                case 0x0b: break;
1.1.1.40! root     14332:                case 0x0c: pcbios_int_10h_0ch(); break;
        !          14333:                case 0x0d: pcbios_int_10h_0dh(); break;
1.1       root     14334:                case 0x0e: pcbios_int_10h_0eh(); break;
                   14335:                case 0x0f: pcbios_int_10h_0fh(); break;
                   14336:                case 0x10: break;
1.1.1.14  root     14337:                case 0x11: pcbios_int_10h_11h(); break;
                   14338:                case 0x12: pcbios_int_10h_12h(); break;
1.1       root     14339:                case 0x13: pcbios_int_10h_13h(); break;
1.1.1.30  root     14340:                case 0x18: pcbios_int_10h_18h(); break;
1.1.1.14  root     14341:                case 0x1a: pcbios_int_10h_1ah(); break;
1.1.1.24  root     14342:                case 0x1b: REG8(AL) = 0x00; break; // functionality/state information is not supported
                   14343:                case 0x1c: REG8(AL) = 0x00; break; // save/restore video state is not supported
1.1       root     14344:                case 0x1d: pcbios_int_10h_1dh(); break;
1.1.1.24  root     14345:                case 0x1e: REG8(AL) = 0x00; break; // flat-panel functions are not supported
                   14346:                case 0x1f: REG8(AL) = 0x00; break; // xga functions are not supported
1.1.1.22  root     14347:                case 0x4f: pcbios_int_10h_4fh(); break;
1.1.1.30  root     14348:                case 0x6f: break;
1.1.1.22  root     14349:                case 0x80: m_CF = 1; break; // unknown
                   14350:                case 0x81: m_CF = 1; break; // unknown
1.1       root     14351:                case 0x82: pcbios_int_10h_82h(); break;
1.1.1.22  root     14352:                case 0x83: pcbios_int_10h_83h(); break;
                   14353:                case 0x8b: break;
                   14354:                case 0x8c: m_CF = 1; break; // unknown
                   14355:                case 0x8d: m_CF = 1; break; // unknown
                   14356:                case 0x8e: m_CF = 1; break; // unknown
                   14357:                case 0x90: pcbios_int_10h_90h(); break;
                   14358:                case 0x91: pcbios_int_10h_91h(); break;
                   14359:                case 0x92: break;
                   14360:                case 0x93: break;
                   14361:                case 0xef: pcbios_int_10h_efh(); break;
1.1.1.24  root     14362:                case 0xfa: break; // ega register interface library is not installed
1.1       root     14363:                case 0xfe: pcbios_int_10h_feh(); break;
                   14364:                case 0xff: pcbios_int_10h_ffh(); break;
                   14365:                default:
1.1.1.22  root     14366:                        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));
                   14367:                        m_CF = 1;
1.1       root     14368:                        break;
                   14369:                }
                   14370:                break;
                   14371:        case 0x11:
                   14372:                // PC BIOS - Get Equipment List
1.1.1.26  root     14373:                REG16(AX) = msdos_get_equipment();
1.1       root     14374:                break;
                   14375:        case 0x12:
                   14376:                // PC BIOS - Get Memory Size
1.1.1.33  root     14377:                REG16(AX) = *(UINT16 *)(mem + 0x413);
1.1       root     14378:                break;
                   14379:        case 0x13:
                   14380:                // PC BIOS - Disk
1.1.1.22  root     14381: //             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     14382:                REG8(AH) = 0xff;
1.1.1.3   root     14383:                m_CF = 1;
1.1       root     14384:                break;
                   14385:        case 0x14:
                   14386:                // PC BIOS - Serial I/O
1.1.1.25  root     14387:                switch(REG8(AH)) {
                   14388:                case 0x00: pcbios_int_14h_00h(); break;
                   14389:                case 0x01: pcbios_int_14h_01h(); break;
                   14390:                case 0x02: pcbios_int_14h_02h(); break;
                   14391:                case 0x03: pcbios_int_14h_03h(); break;
                   14392:                case 0x04: pcbios_int_14h_04h(); break;
                   14393:                case 0x05: pcbios_int_14h_05h(); break;
                   14394:                default:
                   14395:                        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));
                   14396:                        break;
                   14397:                }
1.1       root     14398:                break;
                   14399:        case 0x15:
                   14400:                // PC BIOS
1.1.1.3   root     14401:                m_CF = 0;
1.1       root     14402:                switch(REG8(AH)) {
1.1.1.14  root     14403:                case 0x10: pcbios_int_15h_10h(); break;
1.1       root     14404:                case 0x23: pcbios_int_15h_23h(); break;
                   14405:                case 0x24: pcbios_int_15h_24h(); break;
1.1.1.24  root     14406:                case 0x41: break;
1.1       root     14407:                case 0x49: pcbios_int_15h_49h(); break;
1.1.1.22  root     14408:                case 0x50: pcbios_int_15h_50h(); break;
1.1.1.30  root     14409:                case 0x53: pcbios_int_15h_53h(); break;
1.1       root     14410:                case 0x86: pcbios_int_15h_86h(); break;
                   14411:                case 0x87: pcbios_int_15h_87h(); break;
                   14412:                case 0x88: pcbios_int_15h_88h(); break;
                   14413:                case 0x89: pcbios_int_15h_89h(); break;
1.1.1.21  root     14414:                case 0x8a: pcbios_int_15h_8ah(); break;
1.1.1.22  root     14415:                case 0xc0: // PS/2 ???
                   14416:                case 0xc1:
                   14417:                case 0xc2:
1.1.1.30  root     14418:                case 0xc3: // PS50+ ???
                   14419:                case 0xc4:
1.1.1.22  root     14420:                        REG8(AH) = 0x86;
                   14421:                        m_CF = 1;
                   14422:                        break;
1.1.1.3   root     14423: #if defined(HAS_I386)
1.1       root     14424:                case 0xc9: pcbios_int_15h_c9h(); break;
1.1.1.3   root     14425: #endif
1.1       root     14426:                case 0xca: pcbios_int_15h_cah(); break;
1.1.1.22  root     14427:                case 0xe8: pcbios_int_15h_e8h(); break;
1.1       root     14428:                default:
1.1.1.22  root     14429:                        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));
                   14430:                        REG8(AH) = 0x86;
1.1.1.3   root     14431:                        m_CF = 1;
1.1       root     14432:                        break;
                   14433:                }
                   14434:                break;
                   14435:        case 0x16:
                   14436:                // PC BIOS - Keyboard
1.1.1.3   root     14437:                m_CF = 0;
1.1       root     14438:                switch(REG8(AH)) {
                   14439:                case 0x00: pcbios_int_16h_00h(); break;
                   14440:                case 0x01: pcbios_int_16h_01h(); break;
                   14441:                case 0x02: pcbios_int_16h_02h(); break;
                   14442:                case 0x03: pcbios_int_16h_03h(); break;
                   14443:                case 0x05: pcbios_int_16h_05h(); break;
                   14444:                case 0x10: pcbios_int_16h_00h(); break;
                   14445:                case 0x11: pcbios_int_16h_01h(); break;
                   14446:                case 0x12: pcbios_int_16h_12h(); break;
                   14447:                case 0x13: pcbios_int_16h_13h(); break;
                   14448:                case 0x14: pcbios_int_16h_14h(); break;
1.1.1.24  root     14449:                case 0x55: pcbios_int_16h_55h(); break;
1.1.1.30  root     14450:                case 0x6f: pcbios_int_16h_6fh(); break;
1.1.1.22  root     14451:                case 0xda: break; // unknown
                   14452:                case 0xff: break; // unknown
1.1       root     14453:                default:
1.1.1.22  root     14454:                        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     14455:                        break;
                   14456:                }
                   14457:                break;
                   14458:        case 0x17:
                   14459:                // PC BIOS - Printer
1.1.1.37  root     14460:                m_CF = 0;
                   14461:                switch(REG8(AH)) {
                   14462:                case 0x00: pcbios_int_17h_00h(); break;
                   14463:                case 0x01: pcbios_int_17h_01h(); break;
                   14464:                case 0x02: pcbios_int_17h_02h(); break;
                   14465:                case 0x03: pcbios_int_17h_03h(); break;
                   14466:                case 0x50: pcbios_int_17h_50h(); break;
                   14467:                case 0x51: pcbios_int_17h_51h(); break;
                   14468:                case 0x52: pcbios_int_17h_52h(); break;
                   14469:                case 0x84: pcbios_int_17h_84h(); break;
                   14470:                case 0x85: pcbios_int_17h_85h(); break;
                   14471:                default:
                   14472:                        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));
                   14473:                        break;
                   14474:                }
1.1       root     14475:                break;
                   14476:        case 0x1a:
                   14477:                // PC BIOS - Timer
1.1.1.3   root     14478:                m_CF = 0;
1.1       root     14479:                switch(REG8(AH)) {
                   14480:                case 0x00: pcbios_int_1ah_00h(); break;
                   14481:                case 0x01: break;
                   14482:                case 0x02: pcbios_int_1ah_02h(); break;
                   14483:                case 0x03: break;
                   14484:                case 0x04: pcbios_int_1ah_04h(); break;
                   14485:                case 0x05: break;
                   14486:                case 0x0a: pcbios_int_1ah_0ah(); break;
                   14487:                case 0x0b: break;
1.1.1.14  root     14488:                case 0x35: break; // Word Perfect Third Party Interface?
                   14489:                case 0x36: break; // Word Perfect Third Party Interface
                   14490:                case 0x70: break; // SNAP? (Simple Network Application Protocol)
1.1       root     14491:                default:
1.1.1.22  root     14492:                        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     14493:                        break;
                   14494:                }
                   14495:                break;
1.1.1.33  root     14496:        case 0x1b:
                   14497:                mem[0x471] = 0x00;
                   14498:                break;
1.1       root     14499:        case 0x20:
1.1.1.28  root     14500:                try {
                   14501:                        msdos_process_terminate(SREG(CS), retval, 1);
                   14502:                } catch(...) {
                   14503:                        fatalerror("failed to terminate the process (PSP=%04X) by int 20h\n", SREG(CS));
                   14504:                }
1.1       root     14505:                break;
                   14506:        case 0x21:
                   14507:                // MS-DOS System Call
1.1.1.3   root     14508:                m_CF = 0;
1.1.1.28  root     14509:                try {
                   14510:                        switch(REG8(AH)) {
                   14511:                        case 0x00: msdos_int_21h_00h(); break;
                   14512:                        case 0x01: msdos_int_21h_01h(); break;
                   14513:                        case 0x02: msdos_int_21h_02h(); break;
                   14514:                        case 0x03: msdos_int_21h_03h(); break;
                   14515:                        case 0x04: msdos_int_21h_04h(); break;
                   14516:                        case 0x05: msdos_int_21h_05h(); break;
                   14517:                        case 0x06: msdos_int_21h_06h(); break;
                   14518:                        case 0x07: msdos_int_21h_07h(); break;
                   14519:                        case 0x08: msdos_int_21h_08h(); break;
                   14520:                        case 0x09: msdos_int_21h_09h(); break;
                   14521:                        case 0x0a: msdos_int_21h_0ah(); break;
                   14522:                        case 0x0b: msdos_int_21h_0bh(); break;
                   14523:                        case 0x0c: msdos_int_21h_0ch(); break;
                   14524:                        case 0x0d: msdos_int_21h_0dh(); break;
                   14525:                        case 0x0e: msdos_int_21h_0eh(); break;
                   14526:                        case 0x0f: msdos_int_21h_0fh(); break;
                   14527:                        case 0x10: msdos_int_21h_10h(); break;
                   14528:                        case 0x11: msdos_int_21h_11h(); break;
                   14529:                        case 0x12: msdos_int_21h_12h(); break;
                   14530:                        case 0x13: msdos_int_21h_13h(); break;
                   14531:                        case 0x14: msdos_int_21h_14h(); break;
                   14532:                        case 0x15: msdos_int_21h_15h(); break;
                   14533:                        case 0x16: msdos_int_21h_16h(); break;
                   14534:                        case 0x17: msdos_int_21h_17h(); break;
                   14535:                        case 0x18: msdos_int_21h_18h(); break;
                   14536:                        case 0x19: msdos_int_21h_19h(); break;
                   14537:                        case 0x1a: msdos_int_21h_1ah(); break;
                   14538:                        case 0x1b: msdos_int_21h_1bh(); break;
                   14539:                        case 0x1c: msdos_int_21h_1ch(); break;
                   14540:                        case 0x1d: msdos_int_21h_1dh(); break;
                   14541:                        case 0x1e: msdos_int_21h_1eh(); break;
                   14542:                        case 0x1f: msdos_int_21h_1fh(); break;
                   14543:                        case 0x20: msdos_int_21h_20h(); break;
                   14544:                        case 0x21: msdos_int_21h_21h(); break;
                   14545:                        case 0x22: msdos_int_21h_22h(); break;
                   14546:                        case 0x23: msdos_int_21h_23h(); break;
                   14547:                        case 0x24: msdos_int_21h_24h(); break;
                   14548:                        case 0x25: msdos_int_21h_25h(); break;
                   14549:                        case 0x26: msdos_int_21h_26h(); break;
                   14550:                        case 0x27: msdos_int_21h_27h(); break;
                   14551:                        case 0x28: msdos_int_21h_28h(); break;
                   14552:                        case 0x29: msdos_int_21h_29h(); break;
                   14553:                        case 0x2a: msdos_int_21h_2ah(); break;
                   14554:                        case 0x2b: msdos_int_21h_2bh(); break;
                   14555:                        case 0x2c: msdos_int_21h_2ch(); break;
                   14556:                        case 0x2d: msdos_int_21h_2dh(); break;
                   14557:                        case 0x2e: msdos_int_21h_2eh(); break;
                   14558:                        case 0x2f: msdos_int_21h_2fh(); break;
                   14559:                        case 0x30: msdos_int_21h_30h(); break;
                   14560:                        case 0x31: msdos_int_21h_31h(); break;
                   14561:                        case 0x32: msdos_int_21h_32h(); break;
                   14562:                        case 0x33: msdos_int_21h_33h(); break;
                   14563:                        case 0x34: msdos_int_21h_34h(); break;
                   14564:                        case 0x35: msdos_int_21h_35h(); break;
                   14565:                        case 0x36: msdos_int_21h_36h(); break;
                   14566:                        case 0x37: msdos_int_21h_37h(); break;
                   14567:                        case 0x38: msdos_int_21h_38h(); break;
                   14568:                        case 0x39: msdos_int_21h_39h(0); break;
                   14569:                        case 0x3a: msdos_int_21h_3ah(0); break;
                   14570:                        case 0x3b: msdos_int_21h_3bh(0); break;
                   14571:                        case 0x3c: msdos_int_21h_3ch(); break;
                   14572:                        case 0x3d: msdos_int_21h_3dh(); break;
                   14573:                        case 0x3e: msdos_int_21h_3eh(); break;
                   14574:                        case 0x3f: msdos_int_21h_3fh(); break;
                   14575:                        case 0x40: msdos_int_21h_40h(); break;
                   14576:                        case 0x41: msdos_int_21h_41h(0); break;
                   14577:                        case 0x42: msdos_int_21h_42h(); break;
                   14578:                        case 0x43: msdos_int_21h_43h(0); break;
                   14579:                        case 0x44: msdos_int_21h_44h(); break;
                   14580:                        case 0x45: msdos_int_21h_45h(); break;
                   14581:                        case 0x46: msdos_int_21h_46h(); break;
                   14582:                        case 0x47: msdos_int_21h_47h(0); break;
                   14583:                        case 0x48: msdos_int_21h_48h(); break;
                   14584:                        case 0x49: msdos_int_21h_49h(); break;
                   14585:                        case 0x4a: msdos_int_21h_4ah(); break;
                   14586:                        case 0x4b: msdos_int_21h_4bh(); break;
                   14587:                        case 0x4c: msdos_int_21h_4ch(); break;
                   14588:                        case 0x4d: msdos_int_21h_4dh(); break;
                   14589:                        case 0x4e: msdos_int_21h_4eh(); break;
                   14590:                        case 0x4f: msdos_int_21h_4fh(); break;
                   14591:                        case 0x50: msdos_int_21h_50h(); break;
                   14592:                        case 0x51: msdos_int_21h_51h(); break;
                   14593:                        case 0x52: msdos_int_21h_52h(); break;
1.1.1.33  root     14594:                        // 0x53: Translate BIOS Parameter Block to Drive Param Bock
1.1.1.28  root     14595:                        case 0x54: msdos_int_21h_54h(); break;
                   14596:                        case 0x55: msdos_int_21h_55h(); break;
                   14597:                        case 0x56: msdos_int_21h_56h(0); break;
                   14598:                        case 0x57: msdos_int_21h_57h(); break;
                   14599:                        case 0x58: msdos_int_21h_58h(); break;
                   14600:                        case 0x59: msdos_int_21h_59h(); break;
                   14601:                        case 0x5a: msdos_int_21h_5ah(); break;
                   14602:                        case 0x5b: msdos_int_21h_5bh(); break;
                   14603:                        case 0x5c: msdos_int_21h_5ch(); break;
                   14604:                        case 0x5d: msdos_int_21h_5dh(); break;
1.1.1.33  root     14605:                        // 0x5e: MS-Network
1.1.1.30  root     14606:                        case 0x5f: msdos_int_21h_5fh(); break;
1.1.1.28  root     14607:                        case 0x60: msdos_int_21h_60h(0); break;
                   14608:                        case 0x61: msdos_int_21h_61h(); break;
                   14609:                        case 0x62: msdos_int_21h_62h(); break;
                   14610:                        case 0x63: msdos_int_21h_63h(); break;
1.1.1.33  root     14611:                        // 0x64: Set Device Driver Lockahead Flag
1.1.1.28  root     14612:                        case 0x65: msdos_int_21h_65h(); break;
                   14613:                        case 0x66: msdos_int_21h_66h(); break;
                   14614:                        case 0x67: msdos_int_21h_67h(); break;
                   14615:                        case 0x68: msdos_int_21h_68h(); break;
                   14616:                        case 0x69: msdos_int_21h_69h(); break;
                   14617:                        case 0x6a: msdos_int_21h_6ah(); break;
                   14618:                        case 0x6b: msdos_int_21h_6bh(); break;
                   14619:                        case 0x6c: msdos_int_21h_6ch(0); break;
1.1.1.33  root     14620:                        // 0x6d: Find First ROM Program
                   14621:                        // 0x6e: Find Next ROM Program
                   14622:                        // 0x6f: Get/Set ROM Scan Start Address
                   14623:                        // 0x70: Windows95 - Get/Set Internationalization Information
1.1.1.28  root     14624:                        case 0x71:
1.1.1.33  root     14625:                                // Windows95 - Long Filename Functions
1.1.1.28  root     14626:                                switch(REG8(AL)) {
                   14627:                                case 0x0d: msdos_int_21h_710dh(); break;
                   14628:                                case 0x39: msdos_int_21h_39h(1); break;
                   14629:                                case 0x3a: msdos_int_21h_3ah(1); break;
                   14630:                                case 0x3b: msdos_int_21h_3bh(1); break;
                   14631:                                case 0x41: msdos_int_21h_7141h(1); break;
                   14632:                                case 0x43: msdos_int_21h_43h(1); break;
                   14633:                                case 0x47: msdos_int_21h_47h(1); break;
                   14634:                                case 0x4e: msdos_int_21h_714eh(); break;
                   14635:                                case 0x4f: msdos_int_21h_714fh(); break;
                   14636:                                case 0x56: msdos_int_21h_56h(1); break;
                   14637:                                case 0x60: msdos_int_21h_60h(1); break;
                   14638:                                case 0x6c: msdos_int_21h_6ch(1); break;
                   14639:                                case 0xa0: msdos_int_21h_71a0h(); break;
                   14640:                                case 0xa1: msdos_int_21h_71a1h(); break;
                   14641:                                case 0xa6: msdos_int_21h_71a6h(); break;
                   14642:                                case 0xa7: msdos_int_21h_71a7h(); break;
                   14643:                                case 0xa8: msdos_int_21h_71a8h(); break;
1.1.1.33  root     14644:                                // 0xa9: Server Create/Open File
1.1.1.28  root     14645:                                case 0xaa: msdos_int_21h_71aah(); break;
                   14646:                                default:
                   14647:                                        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));
                   14648:                                        REG16(AX) = 0x7100;
                   14649:                                        m_CF = 1;
                   14650:                                        break;
                   14651:                                }
                   14652:                                break;
                   14653:                        // 0x72: Windows95 beta - LFN FindClose
                   14654:                        case 0x73:
1.1.1.33  root     14655:                                // Windows95 - FAT32 Functions
1.1.1.28  root     14656:                                switch(REG8(AL)) {
                   14657:                                case 0x00: msdos_int_21h_7300h(); break;
1.1.1.33  root     14658:                                // 0x01: Set Drive Locking ???
1.1.1.28  root     14659:                                case 0x02: msdos_int_21h_7302h(); break;
                   14660:                                case 0x03: msdos_int_21h_7303h(); break;
1.1.1.33  root     14661:                                // 0x04: Set DPB to Use for Formatting
                   14662:                                // 0x05: Extended Absolute Disk Read/Write
1.1.1.28  root     14663:                                default:
                   14664:                                        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));
                   14665:                                        REG16(AX) = 0x7300;
                   14666:                                        m_CF = 1;
                   14667:                                        break;
                   14668:                                }
1.1       root     14669:                                break;
1.1.1.30  root     14670:                        case 0xdb: msdos_int_21h_dbh(); break;
                   14671:                        case 0xdc: msdos_int_21h_dch(); break;
1.1       root     14672:                        default:
1.1.1.22  root     14673:                                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     14674:                                REG16(AX) = 0x01;
1.1.1.3   root     14675:                                m_CF = 1;
1.1       root     14676:                                break;
                   14677:                        }
1.1.1.28  root     14678:                } catch(int error) {
                   14679:                        REG16(AX) = error;
                   14680:                        m_CF = 1;
                   14681:                } catch(...) {
                   14682:                        REG16(AX) = 0x1f; // general failure
1.1.1.3   root     14683:                        m_CF = 1;
1.1       root     14684:                }
1.1.1.3   root     14685:                if(m_CF) {
1.1.1.23  root     14686:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   14687:                        sda->extended_error_code = REG16(AX);
                   14688:                        switch(sda->extended_error_code) {
                   14689:                        case  4: // Too many open files
                   14690:                        case  8: // Insufficient memory
                   14691:                                sda->error_class = 1; // Out of resource
                   14692:                                break;
                   14693:                        case  5: // Access denied
                   14694:                                sda->error_class = 3; // Authorization
                   14695:                                break;
                   14696:                        case  7: // Memory control block destroyed
                   14697:                                sda->error_class = 4; // Internal
                   14698:                                break;
                   14699:                        case  2: // File not found
                   14700:                        case  3: // Path not found
                   14701:                        case 15: // Invaid drive specified
                   14702:                        case 18: // No more files
                   14703:                                sda->error_class = 8; // Not found
                   14704:                                break;
                   14705:                        case 32: // Sharing violation
                   14706:                        case 33: // Lock violation
                   14707:                                sda->error_class = 10; // Locked
                   14708:                                break;
                   14709: //                     case 16: // Removal of current directory attempted
                   14710:                        case 19: // Attempted write on protected disk
                   14711:                        case 21: // Drive not ready
                   14712: //                     case 29: // Write failure
                   14713: //                     case 30: // Read failure
                   14714: //                     case 82: // Cannot create subdirectory
                   14715:                                sda->error_class = 11; // Media
                   14716:                                break;
                   14717:                        case 80: // File already exists
                   14718:                                sda->error_class = 12; // Already exist
                   14719:                                break;
                   14720:                        default:
                   14721:                                sda->error_class = 13; // Unknown
                   14722:                                break;
                   14723:                        }
                   14724:                        sda->suggested_action = 1; // Retry
                   14725:                        sda->locus_of_last_error = 1; // Unknown
1.1       root     14726:                }
1.1.1.33  root     14727:                if(ctrl_break_checking && ctrl_break_detected) {
1.1.1.26  root     14728:                        // raise int 23h
                   14729: #if defined(HAS_I386)
                   14730:                        m_ext = 0; // not an external interrupt
                   14731:                        i386_trap(0x23, 1, 0);
                   14732:                        m_ext = 1;
                   14733: #else
                   14734:                        PREFIX86(_interrupt)(0x23);
                   14735: #endif
                   14736:                }
1.1       root     14737:                break;
                   14738:        case 0x22:
                   14739:                fatalerror("int 22h (terminate address)\n");
                   14740:        case 0x23:
1.1.1.28  root     14741:                try {
                   14742:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x100, 1);
                   14743:                } catch(...) {
                   14744:                        fatalerror("failed to terminate the current process by int 23h\n");
                   14745:                }
1.1       root     14746:                break;
                   14747:        case 0x24:
1.1.1.32  root     14748: /*
1.1.1.28  root     14749:                try {
                   14750:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   14751:                } catch(...) {
                   14752:                        fatalerror("failed to terminate the current process by int 24h\n");
                   14753:                }
1.1.1.32  root     14754: */
                   14755:                msdos_int_24h();
1.1       root     14756:                break;
                   14757:        case 0x25:
                   14758:                msdos_int_25h();
                   14759:                break;
                   14760:        case 0x26:
                   14761:                msdos_int_26h();
                   14762:                break;
                   14763:        case 0x27:
1.1.1.28  root     14764:                try {
                   14765:                        msdos_int_27h();
                   14766:                } catch(...) {
                   14767:                        fatalerror("failed to terminate the process (PSP=%04X) by int 27h\n", SREG(CS));
                   14768:                }
1.1       root     14769:                break;
                   14770:        case 0x28:
                   14771:                Sleep(10);
1.1.1.35  root     14772:                REQUEST_HARDWRE_UPDATE();
1.1       root     14773:                break;
                   14774:        case 0x29:
                   14775:                msdos_int_29h();
                   14776:                break;
                   14777:        case 0x2e:
                   14778:                msdos_int_2eh();
                   14779:                break;
                   14780:        case 0x2f:
                   14781:                // multiplex interrupt
                   14782:                switch(REG8(AH)) {
1.1.1.22  root     14783:                case 0x05: msdos_int_2fh_05h(); break;
                   14784:                case 0x11: msdos_int_2fh_11h(); break;
1.1.1.21  root     14785:                case 0x12: msdos_int_2fh_12h(); break;
1.1.1.30  root     14786:                case 0x13: msdos_int_2fh_13h(); break;
1.1.1.22  root     14787:                case 0x14: msdos_int_2fh_14h(); break;
                   14788:                case 0x15: msdos_int_2fh_15h(); break;
1.1       root     14789:                case 0x16: msdos_int_2fh_16h(); break;
1.1.1.22  root     14790:                case 0x19: msdos_int_2fh_19h(); break;
1.1       root     14791:                case 0x1a: msdos_int_2fh_1ah(); break;
1.1.1.30  root     14792:                case 0x40: msdos_int_2fh_40h(); break;
1.1       root     14793:                case 0x43: msdos_int_2fh_43h(); break;
1.1.1.22  root     14794:                case 0x46: msdos_int_2fh_46h(); break;
                   14795:                case 0x48: msdos_int_2fh_48h(); break;
1.1       root     14796:                case 0x4a: msdos_int_2fh_4ah(); break;
1.1.1.22  root     14797:                case 0x4b: msdos_int_2fh_4bh(); break;
1.1       root     14798:                case 0x4f: msdos_int_2fh_4fh(); break;
1.1.1.22  root     14799:                case 0x55: msdos_int_2fh_55h(); break;
1.1.1.24  root     14800:                case 0xad: msdos_int_2fh_adh(); break;
1.1       root     14801:                case 0xae: msdos_int_2fh_aeh(); break;
1.1.1.34  root     14802:                case 0xb7: msdos_int_2fh_b7h(); break;
1.1.1.30  root     14803:                // Installation Check
                   14804:                case 0x01: // PRINT.COM
                   14805:                case 0x02: // PC LAN Program Redirector
                   14806:                case 0x06: // ASSIGN
                   14807:                case 0x08: // DRIVER.SYS
                   14808:                case 0x10: // SHARE
                   14809:                case 0x17: // Clibboard functions
                   14810:                case 0x1b: // XMA2EMS.SYS
                   14811:                case 0x23: // DR DOS 5.0 GRAFTABL
                   14812:                case 0x27: // DR-DOR 6.0 TaskMAX
                   14813:                case 0x2e: // Novell DOS 7 GRAFTABL
1.1.1.33  root     14814:                case 0x39: // Kingswood TSR INTERFACE
                   14815:                case 0x41: // DOS Enhanced LAN Manager 2.0+ MINIPOP/NETPOPUP
1.1.1.30  root     14816:                case 0x45: // PROF.COM
                   14817:                case 0x51: // ODIHELP.EXE
1.1.1.33  root     14818:                case 0x52: // JAM.SYS v1.10+
1.1.1.30  root     14819:                case 0x54: // POWER.EXE
                   14820:                case 0x56: // INTERLNK
1.1.1.33  root     14821:                case 0x57: // IOMEGA DRIVERS
1.1.1.30  root     14822:                case 0x70: // License Service API
1.1.1.33  root     14823:                case 0x72: // SRDISK v1.30+
1.1.1.30  root     14824:                case 0x7a: // Novell NetWare
1.1.1.33  root     14825:                case 0x7f: // PRINDIR v9.0
                   14826:                case 0x80: // FAX BIOS
                   14827:                case 0x81: // Nanosoft, Inc. TurboNET redirector
                   14828:                case 0x82: // Nanosoft, Inc. CAPDOS
                   14829:                case 0x89: // WHOA!.COM
                   14830:                case 0x90: // Resident AID
1.1.1.30  root     14831:                case 0x94: // MICRO.EXE
1.1.1.33  root     14832:                case 0x97: // Micro Focus COBOL v3.1.31
                   14833:                case 0x98: // Micro Focus COBOL v3.1.31
                   14834:                case 0x99: // DOS Navigator II
                   14835:                case 0x9e: // INTMON v2.1
                   14836:                case 0x9f: // INTCFG v2.1
                   14837:                case 0xa9: // METZTSR.COM
                   14838:                case 0xab: // SRSoft MODAL PC v2
1.1.1.30  root     14839:                case 0xac: // GRAPHICS.COM
1.1.1.33  root     14840:                case 0xaf: // WinDOS v2.11
1.1.1.30  root     14841:                case 0xb0: // GRAFTABLE.COM
1.1.1.33  root     14842:                case 0xb4: // IBM PC3270 EMULATION PROG v3
1.1.1.30  root     14843:                case 0xb8: // NETWORK
                   14844:                case 0xb9: // RECEIVER.COM
                   14845:                case 0xbc: // EGA.SYS
1.1.1.33  root     14846:                case 0xbe: // REDVIEW
1.1.1.30  root     14847:                case 0xbf: // PC LAN Program - REDIRIFS.EXE
                   14848:                case 0xc0: // Novell LSL.COM
1.1.1.33  root     14849:                case 0xc1: // Personal NetWare - STPIPX v1.00
                   14850:                case 0xc3: // SETWPR.COM
                   14851:                case 0xc5: // PC-DOS Econet v1.05
                   14852:                case 0xc7: // COLAP.COM
                   14853:                case 0xc9: // ThunderByte???
                   14854:                case 0xca: // TBSCANX
                   14855:                case 0xcb: // Communicating Applications Specification
                   14856:                case 0xcc: // Tsoft NFSDRVR
                   14857:                case 0xcd: // SWELL.EXE
                   14858:                case 0xcf: // TEMPLEXX 1.0
                   14859:                case 0xd0: // Lotus CD/Networker
1.1.1.30  root     14860:                case 0xd2: // PCL-838.EXE
1.1.1.33  root     14861:                case 0xd3: // TeleReplica
                   14862:                case 0xd6: // VEDIT VSWAP
                   14863:                case 0xd7: // Banyan VINES v4+
1.1.1.30  root     14864:                case 0xd8: // Novell NetWare Lite - CLIENT.EXE
1.1.1.33  root     14865:                case 0xda: // ZyXEL ZFAX v1.x
                   14866:                case 0xdb: // ZyXEL ZFAX v2+
                   14867:                case 0xdc: // GOLD.COM
                   14868:                case 0xdd: // MIXFIX.EXE
                   14869:                case 0xde: // Novell Netware - RPRINTER, NPRINTER
                   14870:                case 0xdf: // HyperWare programs
                   14871:                case 0xe0: // SETDRVER.COM v2.10+
                   14872:                case 0xe1: // Phantom2 v1.1+
                   14873:                case 0xe3: // ANARKEY.COM
                   14874:                case 0xed: // Phar Lap DOS EXTENDERS
                   14875:                case 0xee: // XVIEW
                   14876:                case 0xf0: // 4MAP
                   14877:                case 0xf1: // DOS EXTENDER
                   14878:                case 0xf2: // WINX
                   14879:                case 0xf4: // FINDIRQ.COM
                   14880:                case 0xf7: // AUTOPARK.COM
                   14881:                case 0xf8: // SuperStor PRO 2XON.COM
                   14882:                case 0xfb: // AutoBraille v1.1A
                   14883:                case 0xfe: // PC-NFS ???
                   14884:                case 0xff: // Topware Network Operating System
1.1.1.30  root     14885:                        switch(REG8(AL)) {
                   14886:                        case 0x00:
                   14887:                                // This is not installed
                   14888: //                             REG8(AL) = 0x00;
                   14889:                                break;
1.1.1.33  root     14890:                        case 0x01:
                   14891:                                // Banyan VINES v4+ is not installed
                   14892:                                if(REG8(AH) == 0xd7 && REG16(BX) == 0x0000) {
                   14893:                                        break;
                   14894:                                }
1.1.1.30  root     14895:                        default:
                   14896:                                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));
                   14897:                                REG16(AX) = 0x01;
                   14898:                                m_CF = 1;
                   14899:                                break;
                   14900:                        }
                   14901:                        break;
1.1.1.22  root     14902:                default:
                   14903:                        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));
                   14904:                        break;
1.1       root     14905:                }
                   14906:                break;
1.1.1.24  root     14907:        case 0x33:
                   14908:                switch(REG8(AH)) {
                   14909:                case 0x00:
                   14910:                        // Mouse
                   14911:                        switch(REG8(AL)) {
                   14912:                        case 0x00: msdos_int_33h_0000h(); break;
                   14913:                        case 0x01: msdos_int_33h_0001h(); break;
                   14914:                        case 0x02: msdos_int_33h_0002h(); break;
                   14915:                        case 0x03: msdos_int_33h_0003h(); break;
1.1.1.34  root     14916:                        case 0x04: msdos_int_33h_0004h(); break;
1.1.1.24  root     14917:                        case 0x05: msdos_int_33h_0005h(); break;
                   14918:                        case 0x06: msdos_int_33h_0006h(); break;
                   14919:                        case 0x07: msdos_int_33h_0007h(); break;
                   14920:                        case 0x08: msdos_int_33h_0008h(); break;
                   14921:                        case 0x09: msdos_int_33h_0009h(); break;
1.1.1.34  root     14922:                        case 0x0a: break; // Define Text Cursor
1.1.1.24  root     14923:                        case 0x0b: msdos_int_33h_000bh(); break;
                   14924:                        case 0x0c: msdos_int_33h_000ch(); break;
1.1.1.34  root     14925:                        case 0x0d: break; // Light Pen Emulation On
                   14926:                        case 0x0e: break; // Light Pen Emulation Off
1.1.1.24  root     14927:                        case 0x0f: msdos_int_33h_000fh(); break;
1.1.1.34  root     14928:                        case 0x10: break; // Define Screen Region for Updating
1.1.1.24  root     14929:                        case 0x11: msdos_int_33h_0011h(); break;
1.1.1.34  root     14930:                        case 0x12: REG16(AX) = 0xffff; break; // Set Large Graphics Cursor Block
                   14931:                        case 0x13: break; // Define Double-Speed Threshold
1.1.1.24  root     14932:                        case 0x14: msdos_int_33h_0014h(); break;
                   14933:                        case 0x15: msdos_int_33h_0015h(); break;
                   14934:                        case 0x16: msdos_int_33h_0016h(); break;
                   14935:                        case 0x17: msdos_int_33h_0017h(); break;
                   14936:                        case 0x1a: msdos_int_33h_001ah(); break;
                   14937:                        case 0x1b: msdos_int_33h_001bh(); break;
                   14938:                        case 0x1d: msdos_int_33h_001dh(); break;
                   14939:                        case 0x1e: msdos_int_33h_001eh(); break;
1.1.1.34  root     14940:                        case 0x1f: msdos_int_33h_001fh(); break;
                   14941:                        case 0x20: msdos_int_33h_0020h(); break;
1.1.1.24  root     14942:                        case 0x21: msdos_int_33h_0021h(); break;
                   14943:                        case 0x22: msdos_int_33h_0022h(); break;
                   14944:                        case 0x23: msdos_int_33h_0023h(); break;
                   14945:                        case 0x24: msdos_int_33h_0024h(); break;
                   14946:                        case 0x26: msdos_int_33h_0026h(); break;
                   14947:                        case 0x2a: msdos_int_33h_002ah(); break;
1.1.1.34  root     14948:                        case 0x2f: break; // Mouse Hardware Reset
1.1.1.24  root     14949:                        case 0x31: msdos_int_33h_0031h(); break;
                   14950:                        case 0x32: msdos_int_33h_0032h(); break;
                   14951:                        default:
                   14952:                                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));
                   14953:                                break;
                   14954:                        }
                   14955:                        break;
                   14956:                default:
                   14957:                        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));
                   14958:                        break;
                   14959:                }
                   14960:                break;
1.1.1.19  root     14961:        case 0x68:
                   14962:                // dummy interrupt for EMS (int 67h)
                   14963:                switch(REG8(AH)) {
                   14964:                case 0x40: msdos_int_67h_40h(); break;
                   14965:                case 0x41: msdos_int_67h_41h(); break;
                   14966:                case 0x42: msdos_int_67h_42h(); break;
                   14967:                case 0x43: msdos_int_67h_43h(); break;
                   14968:                case 0x44: msdos_int_67h_44h(); break;
                   14969:                case 0x45: msdos_int_67h_45h(); break;
                   14970:                case 0x46: msdos_int_67h_46h(); break;
                   14971:                case 0x47: msdos_int_67h_47h(); break;
                   14972:                case 0x48: msdos_int_67h_48h(); break;
1.1.1.20  root     14973:                // 0x49: LIM EMS - Reserved - Get I/O Port Address (Undocumented in EMS 3.2)
                   14974:                // 0x4a: LIM EMS - Reserved - Get Translation Array (Undocumented in EMS 3.2)
1.1.1.19  root     14975:                case 0x4b: msdos_int_67h_4bh(); break;
                   14976:                case 0x4c: msdos_int_67h_4ch(); break;
                   14977:                case 0x4d: msdos_int_67h_4dh(); break;
1.1.1.20  root     14978:                case 0x4e: msdos_int_67h_4eh(); break;
1.1.1.21  root     14979:                case 0x4f: msdos_int_67h_4fh(); break;
1.1.1.20  root     14980:                case 0x50: msdos_int_67h_50h(); break;
1.1.1.19  root     14981:                case 0x51: msdos_int_67h_51h(); break;
1.1.1.20  root     14982:                case 0x52: msdos_int_67h_52h(); break;
1.1.1.19  root     14983:                case 0x53: msdos_int_67h_53h(); break;
                   14984:                case 0x54: msdos_int_67h_54h(); break;
1.1.1.20  root     14985:                // 0x55: LIM EMS 4.0 - Alter Page Map And JUMP
                   14986:                // 0x56: LIM EMS 4.0 - Alter Page Map And CALL
                   14987:                case 0x57: msdos_int_67h_57h(); break;
                   14988:                case 0x58: msdos_int_67h_58h(); break;
                   14989:                // 0x59: LIM EMS 4.0 - Get Expanded Memory Hardware Information (for DOS Kernel)
                   14990:                case 0x5a: msdos_int_67h_5ah(); break;
                   14991:                // 0x5b: LIM EMS 4.0 - Alternate Map Register Set (for DOS Kernel)
                   14992:                // 0x5c: LIM EMS 4.0 - Prepate Expanded Memory Hardware For Warm Boot
                   14993:                // 0x5d: LIM EMS 4.0 - Enable/Disable OS Function Set Functions (for DOS Kernel)
1.1.1.31  root     14994:                // 0x60: EEMS - Get Physical Window Array
                   14995:                // 0x61: EEMS - Generic Accelerator Card Support
                   14996:                // 0x68: EEMS - Get Address of All Pge Frames om System
                   14997:                // 0x69: EEMS - Map Page into Frame
                   14998:                // 0x6a: EEMS - Page Mapping
                   14999:                // 0xde: VCPI
1.1.1.30  root     15000:                case 0xde: msdos_int_67h_deh(); break;
1.1.1.19  root     15001:                default:
1.1.1.22  root     15002:                        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     15003:                        REG8(AH) = 0x84;
                   15004:                        break;
                   15005:                }
                   15006:                break;
                   15007: #ifdef SUPPORT_XMS
                   15008:        case 0x69:
                   15009:                // dummy interrupt for XMS (call far)
1.1.1.28  root     15010:                try {
                   15011:                        switch(REG8(AH)) {
                   15012:                        case 0x00: msdos_call_xms_00h(); break;
                   15013:                        case 0x01: msdos_call_xms_01h(); break;
                   15014:                        case 0x02: msdos_call_xms_02h(); break;
                   15015:                        case 0x03: msdos_call_xms_03h(); break;
                   15016:                        case 0x04: msdos_call_xms_04h(); break;
                   15017:                        case 0x05: msdos_call_xms_05h(); break;
                   15018:                        case 0x06: msdos_call_xms_06h(); break;
                   15019:                        case 0x07: msdos_call_xms_07h(); break;
                   15020:                        case 0x08: msdos_call_xms_08h(); break;
                   15021:                        case 0x09: msdos_call_xms_09h(); break;
                   15022:                        case 0x0a: msdos_call_xms_0ah(); break;
                   15023:                        case 0x0b: msdos_call_xms_0bh(); break;
                   15024:                        case 0x0c: msdos_call_xms_0ch(); break;
                   15025:                        case 0x0d: msdos_call_xms_0dh(); break;
                   15026:                        case 0x0e: msdos_call_xms_0eh(); break;
                   15027:                        case 0x0f: msdos_call_xms_0fh(); break;
                   15028:                        case 0x10: msdos_call_xms_10h(); break;
                   15029:                        case 0x11: msdos_call_xms_11h(); break;
                   15030:                        case 0x12: msdos_call_xms_12h(); break;
1.1.1.29  root     15031: #if defined(HAS_I386)
                   15032:                        case 0x88: msdos_call_xms_88h(); break;
                   15033:                        case 0x89: msdos_call_xms_89h(); break;
                   15034:                        case 0x8e: msdos_call_xms_8eh(); break;
                   15035:                        case 0x8f: msdos_call_xms_8fh(); break;
                   15036: #endif
1.1.1.28  root     15037:                        default:
                   15038:                                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));
                   15039:                                REG16(AX) = 0x0000;
                   15040:                                REG8(BL) = 0x80; // function not implemented
                   15041:                                break;
                   15042:                        }
                   15043:                } catch(...) {
1.1.1.19  root     15044:                        REG16(AX) = 0x0000;
1.1.1.28  root     15045:                        REG8(BL) = 0x8f; // unrecoverable driver error
1.1.1.19  root     15046:                }
                   15047:                break;
                   15048: #endif
                   15049:        case 0x6a:
1.1.1.24  root     15050:                // irq12 (mouse)
                   15051:                mouse_push_ax = REG16(AX);
                   15052:                mouse_push_bx = REG16(BX);
                   15053:                mouse_push_cx = REG16(CX);
                   15054:                mouse_push_dx = REG16(DX);
                   15055:                mouse_push_si = REG16(SI);
                   15056:                mouse_push_di = REG16(DI);
                   15057:                
1.1.1.34  root     15058:                if(/*mouse.hidden == 0 && */mouse.call_addr.dw != 0) {
1.1.1.24  root     15059:                        REG16(AX) = mouse.status_irq;
                   15060:                        REG16(BX) = mouse.get_buttons();
1.1.1.34  root     15061:                        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
                   15062:                        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
1.1.1.24  root     15063:                        REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
                   15064:                        REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
                   15065:                        
                   15066:                        mem[0xfffd0 + 0x02] = 0x9a;     // call far
                   15067:                        mem[0xfffd0 + 0x03] = mouse.call_addr.w.l & 0xff;
                   15068:                        mem[0xfffd0 + 0x04] = mouse.call_addr.w.l >> 8;
                   15069:                        mem[0xfffd0 + 0x05] = mouse.call_addr.w.h & 0xff;
                   15070:                        mem[0xfffd0 + 0x06] = mouse.call_addr.w.h >> 8;
                   15071:                } else {
                   15072:                        mem[0xfffd0 + 0x02] = 0x90;     // nop
                   15073:                        mem[0xfffd0 + 0x03] = 0x90;     // nop
                   15074:                        mem[0xfffd0 + 0x04] = 0x90;     // nop
                   15075:                        mem[0xfffd0 + 0x05] = 0x90;     // nop
                   15076:                        mem[0xfffd0 + 0x06] = 0x90;     // nop
                   15077:                }
                   15078:                break;
                   15079:        case 0x6b:
                   15080:                // end of irq12 (mouse)
                   15081:                REG16(AX) = mouse_push_ax;
                   15082:                REG16(BX) = mouse_push_bx;
                   15083:                REG16(CX) = mouse_push_cx;
                   15084:                REG16(DX) = mouse_push_dx;
                   15085:                REG16(SI) = mouse_push_si;
                   15086:                REG16(DI) = mouse_push_di;
                   15087:                
                   15088:                // EOI
                   15089:                if((pic[1].isr &= ~(1 << 4)) == 0) {
                   15090:                        pic[0].isr &= ~(1 << 2); // master
                   15091:                }
                   15092:                pic_update();
                   15093:                break;
                   15094:        case 0x6c:
1.1.1.19  root     15095:                // dummy interrupt for case map routine pointed in the country info
                   15096:                if(REG8(AL) >= 0x80) {
                   15097:                        char tmp[2] = {0};
                   15098:                        tmp[0] = REG8(AL);
                   15099:                        my_strupr(tmp);
                   15100:                        REG8(AL) = tmp[0];
                   15101:                }
                   15102:                break;
1.1.1.27  root     15103:        case 0x6d:
                   15104:                // dummy interrupt for font read routine pointed by int 15h, ax=5000h
                   15105:                REG8(AL) = 0x86; // not supported
                   15106:                m_CF = 1;
                   15107:                break;
1.1.1.32  root     15108:        case 0x6e:
                   15109:                // dummy interrupt for parameter error message read routine pointed by int 2fh, ax=122eh, dl=08h
                   15110:                {
                   15111:                        UINT16 code = REG16(AX);
                   15112:                        if(code & 0xf0) {
                   15113:                                code = (code & 7) | ((code & 0x10) >> 1);
                   15114:                        }
                   15115:                        for(int i = 0; i < array_length(param_error_table); i++) {
                   15116:                                if(param_error_table[i].code == code || param_error_table[i].code == (UINT16)-1) {
                   15117:                                        const char *message = NULL;
                   15118:                                        if(active_code_page == 932) {
                   15119:                                                message = param_error_table[i].message_japanese;
                   15120:                                        }
                   15121:                                        if(message == NULL) {
                   15122:                                                message = param_error_table[i].message_english;
                   15123:                                        }
                   15124:                                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   15125:                                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   15126:                                        
                   15127:                                        SREG(ES) = WORK_TOP >> 4;
                   15128:                                        i386_load_segment_descriptor(ES);
                   15129:                                        REG16(DI) = 0x0000;
                   15130:                                        break;
                   15131:                                }
                   15132:                        }
                   15133:                }
                   15134:                break;
1.1.1.8   root     15135:        case 0x70:
                   15136:        case 0x71:
                   15137:        case 0x72:
                   15138:        case 0x73:
                   15139:        case 0x74:
                   15140:        case 0x75:
                   15141:        case 0x76:
                   15142:        case 0x77:
                   15143:                // EOI
                   15144:                if((pic[1].isr &= ~(1 << (num - 0x70))) == 0) {
                   15145:                        pic[0].isr &= ~(1 << 2); // master
                   15146:                }
                   15147:                pic_update();
                   15148:                break;
1.1       root     15149:        default:
1.1.1.22  root     15150: //             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     15151:                break;
                   15152:        }
                   15153:        
                   15154:        // update cursor position
                   15155:        if(cursor_moved) {
1.1.1.36  root     15156:                pcbios_update_cursor_position();
1.1       root     15157:                cursor_moved = false;
                   15158:        }
                   15159: }
                   15160: 
                   15161: // init
                   15162: 
                   15163: int msdos_init(int argc, char *argv[], char *envp[], int standard_env)
                   15164: {
                   15165:        // init file handler
                   15166:        memset(file_handler, 0, sizeof(file_handler));
                   15167:        msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   15168:        msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   15169:        msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
1.1.1.21  root     15170: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     15171:        if(_open("stdaux.txt", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
1.1.1.21  root     15172: #else
                   15173:        if(_open("NUL", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
                   15174: #endif
                   15175:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
1.1       root     15176:        }
1.1.1.21  root     15177:        if(_open("NUL", _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 4) {
1.1.1.37  root     15178:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0, 0, 1); // LPT1
1.1.1.21  root     15179:        }
1.1       root     15180:        _dup2(0, DUP_STDIN);
                   15181:        _dup2(1, DUP_STDOUT);
                   15182:        _dup2(2, DUP_STDERR);
1.1.1.21  root     15183:        _dup2(3, DUP_STDAUX);
                   15184:        _dup2(4, DUP_STDPRN);
1.1       root     15185:        
1.1.1.24  root     15186:        // init mouse
                   15187:        memset(&mouse, 0, sizeof(mouse));
1.1.1.34  root     15188:        mouse.enabled = true;   // from DOSBox
                   15189:        mouse.hidden = 1;       // hidden in default ???
                   15190:        mouse.old_hidden = 1;   // from DOSBox
                   15191:        mouse.max_position.x = 8 * (scr_width  - 1);
                   15192:        mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24  root     15193:        mouse.mickey.x = 8;
                   15194:        mouse.mickey.y = 16;
                   15195:        
1.1.1.26  root     15196: #ifdef SUPPORT_XMS
                   15197:        // init xms
                   15198:        msdos_xms_init();
                   15199: #endif
                   15200:        
1.1       root     15201:        // init process
                   15202:        memset(process, 0, sizeof(process));
                   15203:        
1.1.1.13  root     15204:        // init dtainfo
                   15205:        msdos_dta_info_init();
                   15206:        
1.1       root     15207:        // init memory
                   15208:        memset(mem, 0, sizeof(mem));
                   15209:        
                   15210:        // bios data area
1.1.1.23  root     15211:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     15212:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   15213:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.14  root     15214:        CONSOLE_FONT_INFO cfi;
                   15215:        GetCurrentConsoleFont(hStdout, FALSE, &cfi);
                   15216:        
                   15217:        int regen = min(scr_width * scr_height * 2, 0x8000);
                   15218:        text_vram_top_address = TEXT_VRAM_TOP;
                   15219:        text_vram_end_address = text_vram_top_address + regen;
                   15220:        shadow_buffer_top_address = SHADOW_BUF_TOP;
                   15221:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
                   15222:        
                   15223:        if(regen > 0x4000) {
                   15224:                regen = 0x8000;
                   15225:                vram_pages = 1;
                   15226:        } else if(regen > 0x2000) {
                   15227:                regen = 0x4000;
                   15228:                vram_pages = 2;
                   15229:        } else if(regen > 0x1000) {
                   15230:                regen = 0x2000;
                   15231:                vram_pages = 4;
                   15232:        } else {
                   15233:                regen = 0x1000;
                   15234:                vram_pages = 8;
                   15235:        }
1.1       root     15236:        
1.1.1.25  root     15237:        *(UINT16 *)(mem + 0x400) = 0x3f8; // com1 port address
                   15238:        *(UINT16 *)(mem + 0x402) = 0x2f8; // com2 port address
1.1.1.29  root     15239:        *(UINT16 *)(mem + 0x404) = 0x3e8; // com3 port address
                   15240:        *(UINT16 *)(mem + 0x406) = 0x2e8; // com4 port address
1.1.1.25  root     15241:        *(UINT16 *)(mem + 0x408) = 0x378; // lpt1 port address
1.1.1.37  root     15242:        *(UINT16 *)(mem + 0x40a) = 0x278; // lpt2 port address
                   15243:        *(UINT16 *)(mem + 0x40c) = 0x3bc; // lpt3 port address
1.1.1.32  root     15244: #ifdef EXT_BIOS_TOP
1.1.1.25  root     15245:        *(UINT16 *)(mem + 0x40e) = EXT_BIOS_TOP >> 4;
1.1.1.32  root     15246: #endif
1.1.1.26  root     15247:        *(UINT16 *)(mem + 0x410) = msdos_get_equipment();
1.1.1.33  root     15248:        *(UINT16 *)(mem + 0x413) = MEMORY_END >> 10;
1.1       root     15249:        *(UINT8  *)(mem + 0x449) = 0x03;//0x73;
1.1.1.14  root     15250:        *(UINT16 *)(mem + 0x44a) = csbi.dwSize.X;
                   15251:        *(UINT16 *)(mem + 0x44c) = regen;
1.1       root     15252:        *(UINT16 *)(mem + 0x44e) = 0;
                   15253:        *(UINT8  *)(mem + 0x450) = csbi.dwCursorPosition.X;
1.1.1.14  root     15254:        *(UINT8  *)(mem + 0x451) = csbi.dwCursorPosition.Y - scr_top;
1.1       root     15255:        *(UINT8  *)(mem + 0x460) = 7;
                   15256:        *(UINT8  *)(mem + 0x461) = 7;
                   15257:        *(UINT8  *)(mem + 0x462) = 0;
                   15258:        *(UINT16 *)(mem + 0x463) = 0x3d4;
1.1.1.19  root     15259:        *(UINT8  *)(mem + 0x465) = 0x09;
                   15260:        *(UINT32 *)(mem + 0x46c) = get_ticks_since_midnight(timeGetTime());
1.1.1.40! root     15261:        *(UINT16 *)(mem + 0x472) = 0x4321; // preserve memory in cpu reset
1.1.1.14  root     15262:        *(UINT8  *)(mem + 0x484) = csbi.srWindow.Bottom - csbi.srWindow.Top;
                   15263:        *(UINT8  *)(mem + 0x485) = cfi.dwFontSize.Y;
                   15264:        *(UINT8  *)(mem + 0x487) = 0x60;
                   15265:        *(UINT8  *)(mem + 0x496) = 0x10; // enhanced keyboard installed
1.1.1.32  root     15266: #ifdef EXT_BIOS_TOP
1.1.1.25  root     15267:        *(UINT16 *)(mem + EXT_BIOS_TOP) = 1;
1.1.1.32  root     15268: #endif
1.1.1.14  root     15269:        
                   15270:        // initial screen
                   15271:        SMALL_RECT rect;
                   15272:        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   15273:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   15274:        for(int y = 0, ofs1 = TEXT_VRAM_TOP, ofs2 = SHADOW_BUF_TOP; y < scr_height; y++) {
                   15275:                for(int x = 0; x < scr_width; x++) {
                   15276:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Char.AsciiChar;
                   15277:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Attributes;
                   15278:                }
                   15279:        }
1.1       root     15280:        
1.1.1.19  root     15281:        // init mcb
1.1       root     15282:        int seg = MEMORY_TOP >> 4;
1.1.1.19  root     15283:        
                   15284:        // iret table
                   15285:        // note: int 2eh vector should address the routine in command.com,
                   15286:        // and some softwares invite (int 2eh vector segment) - 1 must address the mcb of command.com.
                   15287:        // so move iret table into allocated memory block
                   15288:        // http://www5c.biglobe.ne.jp/~ecb/assembler2/2_6.html
1.1.1.33  root     15289:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, IRET_SIZE >> 4);
1.1.1.19  root     15290:        IRET_TOP = seg << 4;
                   15291:        seg += IRET_SIZE >> 4;
1.1.1.25  root     15292:        memset(mem + IRET_TOP, 0xcf, IRET_SIZE); // iret
1.1.1.19  root     15293:        
                   15294:        // dummy xms/ems device
1.1.1.33  root     15295:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, XMS_SIZE >> 4);
1.1.1.19  root     15296:        XMS_TOP = seg << 4;
                   15297:        seg += XMS_SIZE >> 4;
                   15298:        
                   15299:        // environment
1.1.1.33  root     15300:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, ENV_SIZE >> 4);
1.1       root     15301:        int env_seg = seg;
                   15302:        int ofs = 0;
1.1.1.32  root     15303:        char env_append[ENV_SIZE] = {0}, append_added = 0;
                   15304:        char comspec_added = 0;
1.1.1.33  root     15305:        char lastdrive_added = 0;
1.1.1.32  root     15306:        char env_msdos_path[ENV_SIZE] = {0};
                   15307:        char env_path[ENV_SIZE] = {0}, path_added = 0;
1.1.1.33  root     15308:        char prompt_added = 0;
1.1.1.32  root     15309:        char env_temp[ENV_SIZE] = {0}, temp_added = 0, tmp_added = 0;
1.1.1.33  root     15310:        char tz_added = 0;
1.1.1.32  root     15311:        char *path, *short_path;
                   15312:        
                   15313:        if((path = getenv("MSDOS_APPEND")) != NULL) {
                   15314:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   15315:                        strcpy(env_append, short_path);
                   15316:                }
                   15317:        }
                   15318:        if((path = getenv("APPEND")) != NULL) {
                   15319:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   15320:                        if(env_append[0] != '\0') {
                   15321:                                strcat(env_append, ";");
                   15322:                        }
                   15323:                        strcat(env_append, short_path);
                   15324:                }
                   15325:        }
                   15326:        
                   15327:        if((path = msdos_search_command_com(argv[0], env_path)) != NULL) {
                   15328:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   15329:                        strcpy(comspec_path, short_path);
                   15330:                }
                   15331:        }
                   15332:        if((path = getenv("MSDOS_COMSPEC")) != NULL && _access(path, 0) == 0) {
                   15333:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   15334:                        strcpy(comspec_path, short_path);
                   15335:                }
                   15336:        }
1.1       root     15337:        
1.1.1.28  root     15338:        if((path = getenv("MSDOS_PATH")) != NULL) {
1.1.1.32  root     15339:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   15340:                        strcpy(env_msdos_path, short_path);
                   15341:                        strcpy(env_path, short_path);
1.1.1.14  root     15342:                }
                   15343:        }
1.1.1.28  root     15344:        if((path = getenv("PATH")) != NULL) {
1.1.1.32  root     15345:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   15346:                        if(env_path[0] != '\0') {
                   15347:                                strcat(env_path, ";");
                   15348:                        }
                   15349:                        strcat(env_path, short_path);
1.1.1.9   root     15350:                }
                   15351:        }
1.1.1.32  root     15352:        
                   15353:        if(GetTempPath(ENV_SIZE, env_temp) != 0) {
                   15354:                strcpy(env_temp, msdos_get_multiple_short_path(env_temp));
1.1.1.15  root     15355:        }
1.1.1.32  root     15356:        for(int i = 0; i < 4; i++) {
                   15357:                static const char *name[4] = {"MSDOS_TEMP", "MSDOS_TMP", "TEMP", "TMP"};
                   15358:                if((path = getenv(name[i])) != NULL && _access(path, 0) == 0) {
                   15359:                        if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   15360:                                strcpy(env_temp, short_path);
                   15361:                                break;
                   15362:                        }
                   15363:                }
1.1.1.24  root     15364:        }
1.1.1.32  root     15365:        
1.1.1.9   root     15366:        for(char **p = envp; p != NULL && *p != NULL; p++) {
1.1       root     15367:                // lower to upper
1.1.1.28  root     15368:                char tmp[ENV_SIZE], name[ENV_SIZE];
1.1       root     15369:                strcpy(tmp, *p);
                   15370:                for(int i = 0;; i++) {
                   15371:                        if(tmp[i] == '=') {
                   15372:                                tmp[i] = '\0';
                   15373:                                sprintf(name, ";%s;", tmp);
1.1.1.25  root     15374:                                my_strupr(name);
1.1       root     15375:                                tmp[i] = '=';
                   15376:                                break;
                   15377:                        } else if(tmp[i] >= 'a' && tmp[i] <= 'z') {
1.1.1.28  root     15378:                                tmp[i] = (tmp[i] - 'a') + 'A';
1.1       root     15379:                        }
                   15380:                }
1.1.1.33  root     15381:                if(strstr(";MSDOS_APPEND;MSDOS_COMSPEC;MSDOS_LASTDRIVE;MSDOS_TEMP;MSDOS_TMP;MSDOS_TZ;", name) != NULL) {
                   15382:                        // ignore MSDOS_(APPEND/COMSPEC/LASTDRIVE/TEMP/TMP/TZ)
                   15383:                } else if(standard_env && strstr(";APPEND;COMSPEC;LASTDRIVE;MSDOS_PATH;PATH;PROMPT;TEMP;TMP;TZ;", name) == NULL) {
1.1.1.18  root     15384:                        // ignore non standard environments
                   15385:                } else {
1.1.1.33  root     15386:                        if(strncmp(tmp, "APPEND=", 7) == 0) {
1.1.1.32  root     15387:                                if(env_append[0] != '\0') {
                   15388:                                        sprintf(tmp, "APPEND=%s", env_append);
                   15389:                                } else {
                   15390:                                        sprintf(tmp, "APPEND=%s", msdos_get_multiple_short_path(tmp + 7));
                   15391:                                }
                   15392:                                append_added = 1;
                   15393:                        } else if(strncmp(tmp, "COMSPEC=", 8) == 0) {
1.1.1.14  root     15394:                                strcpy(tmp, "COMSPEC=C:\\COMMAND.COM");
1.1.1.32  root     15395:                                comspec_added = 1;
1.1.1.33  root     15396:                        } else if(strncmp(tmp, "LASTDRIVE=", 10) == 0) {
                   15397:                                char *env = getenv("MSDOS_LASTDRIVE");
                   15398:                                if(env != NULL) {
                   15399:                                        sprintf(tmp, "LASTDRIVE=%s", env);
                   15400:                                }
                   15401:                                lastdrive_added = 1;
                   15402:                        } else if(strncmp(tmp, "MSDOS_PATH=", 11) == 0) {
1.1.1.28  root     15403:                                if(env_msdos_path[0] != '\0') {
                   15404:                                        sprintf(tmp, "MSDOS_PATH=%s", env_msdos_path);
                   15405:                                } else {
                   15406:                                        sprintf(tmp, "MSDOS_PATH=%s", msdos_get_multiple_short_path(tmp + 11));
                   15407:                                }
1.1.1.33  root     15408:                        } else if(strncmp(tmp, "PATH=", 5) == 0) {
1.1.1.28  root     15409:                                if(env_path[0] != '\0') {
                   15410:                                        sprintf(tmp, "PATH=%s", env_path);
                   15411:                                } else {
                   15412:                                        sprintf(tmp, "PATH=%s", msdos_get_multiple_short_path(tmp + 5));
                   15413:                                }
1.1.1.32  root     15414:                                path_added = 1;
1.1.1.33  root     15415:                        } else if(strncmp(tmp, "PROMPT=", 7) == 0) {
                   15416:                                prompt_added = 1;
1.1.1.28  root     15417:                        } else if(strncmp(tmp, "TEMP=", 5) == 0) {
                   15418:                                if(env_temp[0] != '\0') {
                   15419:                                        sprintf(tmp, "TEMP=%s", env_temp);
                   15420:                                } else {
                   15421:                                        sprintf(tmp, "TEMP=%s", msdos_get_multiple_short_path(tmp + 5));
                   15422:                                }
1.1.1.32  root     15423:                                temp_added = 1;
1.1.1.33  root     15424:                        } else if(strncmp(tmp, "TMP=", 4) == 0) {
1.1.1.28  root     15425:                                if(env_temp[0] != '\0') {
                   15426:                                        sprintf(tmp, "TMP=%s", env_temp);
                   15427:                                } else {
                   15428:                                        sprintf(tmp, "TMP=%s", msdos_get_multiple_short_path(tmp + 4));
1.1       root     15429:                                }
1.1.1.32  root     15430:                                tmp_added = 1;
1.1.1.33  root     15431:                        } else if(strncmp(tmp, "TZ=", 3) == 0) {
                   15432:                                char *env = getenv("MSDOS_TZ");
                   15433:                                if(env != NULL) {
                   15434:                                        sprintf(tmp, "TZ=%s", env);
                   15435:                                }
                   15436:                                tz_added = 1;
1.1       root     15437:                        }
                   15438:                        int len = strlen(tmp);
1.1.1.14  root     15439:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) {
1.1       root     15440:                                fatalerror("too many environments\n");
                   15441:                        }
                   15442:                        memcpy(mem + (seg << 4) + ofs, tmp, len);
                   15443:                        ofs += len + 1;
                   15444:                }
                   15445:        }
1.1.1.32  root     15446:        if(!append_added && env_append[0] != '\0') {
                   15447:                #define SET_ENV(name, value) { \
                   15448:                        char tmp[ENV_SIZE]; \
                   15449:                        sprintf(tmp, "%s=%s", name, value); \
                   15450:                        int len = strlen(tmp); \
                   15451:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) { \
                   15452:                                fatalerror("too many environments\n"); \
                   15453:                        } \
                   15454:                        memcpy(mem + (seg << 4) + ofs, tmp, len); \
                   15455:                        ofs += len + 1; \
                   15456:                }
                   15457:                SET_ENV("APPEND", env_append);
                   15458:        }
                   15459:        if(!comspec_added) {
                   15460:                SET_ENV("COMSPEC", "C:\\COMMAND.COM");
                   15461:        }
1.1.1.33  root     15462:        if(!lastdrive_added) {
                   15463:                SET_ENV("LASTDRIVE", "Z");
                   15464:        }
1.1.1.32  root     15465:        if(!path_added) {
                   15466:                SET_ENV("PATH", env_path);
                   15467:        }
1.1.1.33  root     15468:        if(!prompt_added) {
                   15469:                SET_ENV("PROMPT", "$P$G");
                   15470:        }
1.1.1.32  root     15471:        if(!temp_added) {
                   15472:                SET_ENV("TEMP", env_temp);
                   15473:        }
                   15474:        if(!tmp_added) {
                   15475:                SET_ENV("TMP", env_temp);
                   15476:        }
1.1.1.33  root     15477:        if(!tz_added) {
                   15478:                TIME_ZONE_INFORMATION tzi;
                   15479:                HKEY hKey, hSubKey;
                   15480:                char tzi_std_name[64];
                   15481:                char tz_std[8] = "GMT";
                   15482:                char tz_dlt[8] = "GST";
                   15483:                char tz_value[32];
                   15484:                
                   15485:                // timezone name from GetTimeZoneInformation may not be english
                   15486:                bool daylight = (GetTimeZoneInformation(&tzi) != TIME_ZONE_ID_UNKNOWN);
                   15487:                setlocale(LC_CTYPE, "");
                   15488:                wcstombs(tzi_std_name, tzi.StandardName, sizeof(tzi_std_name));
                   15489:                
                   15490:                // get english timezone name from registry
                   15491:                if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones", 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
                   15492:                        for(DWORD i = 0; !tz_added; i++) {
                   15493:                                char reg_name[256], sub_key[1024], std_name[256];
                   15494:                                DWORD size;
                   15495:                                FILETIME ftTime;
                   15496:                                LONG result = RegEnumKeyEx(hKey, i, reg_name, &(size = array_length(reg_name)), NULL, NULL, NULL, &ftTime);
                   15497:                                
                   15498:                                if(result == ERROR_SUCCESS) {
                   15499:                                        sprintf(sub_key, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\%s", reg_name);
                   15500:                                        if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, sub_key, 0, KEY_QUERY_VALUE, &hSubKey) == ERROR_SUCCESS) {
                   15501:                                                if(RegQueryValueEx(hSubKey, "Std", NULL, NULL, (LPBYTE)std_name, &(size = array_length(std_name))) == ERROR_SUCCESS) {
                   15502:                                                        // search english timezone name from table
1.1.1.37  root     15503:                                                        if(strcmp(std_name, tzi_std_name) == 0) {
1.1.1.33  root     15504:                                                                for(int j = 0; j < array_length(tz_table); j++) {
                   15505:                                                                        if(strcmp(reg_name, tz_table[j].name) == 0 && (tz_table[j].lcid == 0 || tz_table[j].lcid == GetUserDefaultLCID())) {
                   15506:                                                                                if(tz_table[j].std != NULL) {
                   15507:                                                                                        strcpy(tz_std, tz_table[j].std);
                   15508:                                                                                }
                   15509:                                                                                if(tz_table[j].dlt != NULL) {
                   15510:                                                                                        strcpy(tz_dlt, tz_table[j].dlt);
                   15511:                                                                                }
                   15512:                                                                                tz_added = 1;
                   15513:                                                                                break;
                   15514:                                                                        }
                   15515:                                                                }
                   15516:                                                        }
                   15517:                                                }
                   15518:                                                RegCloseKey(hSubKey);
                   15519:                                        }
                   15520:                                } else if(result == ERROR_NO_MORE_ITEMS) {
                   15521:                                        break;
                   15522:                                }
                   15523:                        }
                   15524:                        RegCloseKey(hKey);
                   15525:                }
                   15526:                if((tzi.Bias % 60) != 0) {
                   15527:                        sprintf(tz_value, "%s%d:%2d", tz_std, tzi.Bias / 60, abs(tzi.Bias % 60));
                   15528:                } else {
                   15529:                        sprintf(tz_value, "%s%d", tz_std, tzi.Bias / 60);
                   15530:                }
                   15531:                if(daylight) {
                   15532:                        strcat(tz_value, tz_dlt);
                   15533:                }
                   15534:                SET_ENV("TZ", tz_value);
                   15535:        }
1.1       root     15536:        seg += (ENV_SIZE >> 4);
                   15537:        
                   15538:        // psp
1.1.1.33  root     15539:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, PSP_SIZE >> 4);
1.1       root     15540:        current_psp = seg;
1.1.1.35  root     15541:        psp_t *psp = msdos_psp_create(seg, seg + (PSP_SIZE >> 4), -1, env_seg);
                   15542:        psp->parent_psp = current_psp;
1.1       root     15543:        seg += (PSP_SIZE >> 4);
                   15544:        
1.1.1.19  root     15545:        // first free mcb in conventional memory
1.1.1.33  root     15546:        msdos_mcb_create(seg, 'M', 0, (MEMORY_END >> 4) - seg - 2);
1.1.1.8   root     15547:        first_mcb = seg;
                   15548:        
1.1.1.19  root     15549:        // dummy mcb to link to umb
1.1.1.33  root     15550: #if 0
1.1.1.39  root     15551:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4), "SC"); // link umb
1.1.1.33  root     15552: #else
1.1.1.39  root     15553:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0, "SC"); // unlink umb
1.1.1.33  root     15554: #endif
1.1.1.19  root     15555:        
                   15556:        // first free mcb in upper memory block
1.1.1.8   root     15557:        msdos_mcb_create(UMB_TOP >> 4, 'Z', 0, (UMB_END >> 4) - (UMB_TOP >> 4) - 1);
1.1       root     15558:        
1.1.1.29  root     15559: #ifdef SUPPORT_HMA
                   15560:        // first free mcb in high memory area
                   15561:        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   15562: #endif
                   15563:        
1.1.1.26  root     15564:        // interrupt vector
                   15565:        for(int i = 0; i < 0x80; i++) {
                   15566:                *(UINT16 *)(mem + 4 * i + 0) = i;
                   15567:                *(UINT16 *)(mem + 4 * i + 2) = (IRET_TOP >> 4);
                   15568:        }
1.1.1.35  root     15569:        *(UINT16 *)(mem + 4 * 0x08 + 0) = 0x0018;       // fffd:0018 irq0 (system timer)
1.1.1.26  root     15570:        *(UINT16 *)(mem + 4 * 0x08 + 2) = 0xfffd;
                   15571:        *(UINT16 *)(mem + 4 * 0x22 + 0) = 0x0000;       // ffff:0000 boot
                   15572:        *(UINT16 *)(mem + 4 * 0x22 + 2) = 0xffff;
                   15573:        *(UINT16 *)(mem + 4 * 0x67 + 0) = 0x0012;       // xxxx:0012 ems
                   15574:        *(UINT16 *)(mem + 4 * 0x67 + 2) = XMS_TOP >> 4;
                   15575:        *(UINT16 *)(mem + 4 * 0x74 + 0) = 0x0000;       // fffd:0000 irq12 (mouse)
                   15576:        *(UINT16 *)(mem + 4 * 0x74 + 2) = 0xfffd;
                   15577:        
1.1.1.29  root     15578:        // dummy devices (NUL -> CON -> ... -> CONFIG$ -> EMMXXXX0)
1.1.1.26  root     15579:        static const struct {
                   15580:                UINT16 attributes;
                   15581:                char *dev_name;
                   15582:        } dummy_devices[] = {
                   15583:                {0x8013, "CON     "},
                   15584:                {0x8000, "AUX     "},
                   15585:                {0xa0c0, "PRN     "},
                   15586:                {0x8008, "CLOCK$  "},
                   15587:                {0x8000, "COM1    "},
                   15588:                {0xa0c0, "LPT1    "},
                   15589:                {0xa0c0, "LPT2    "},
                   15590:                {0xa0c0, "LPT3    "},
                   15591:                {0x8000, "COM2    "},
                   15592:                {0x8000, "COM3    "},
                   15593:                {0x8000, "COM4    "},
1.1.1.30  root     15594: //             {0xc000, "CONFIG$ "},
                   15595:                {0xc000, "$IBMADSP"}, // for windows3.1 setup.exe
1.1.1.26  root     15596:        };
                   15597:        static const UINT8 dummy_device_routine[] = {
                   15598:                // from NUL device of Windows 98 SE
                   15599:                // or word ptr ES:[BX+03],0100
                   15600:                0x26, 0x81, 0x4f, 0x03, 0x00, 0x01,
                   15601:                // retf
                   15602:                0xcb,
                   15603:        };
1.1.1.29  root     15604:        device_t *last = NULL;
1.1.1.32  root     15605:        for(int i = 0; i < array_length(dummy_devices); i++) {
1.1.1.26  root     15606:                device_t *device = (device_t *)(mem + DEVICE_TOP + 22 + 18 * i);
1.1.1.29  root     15607:                device->next_driver.w.l = 22 + 18 * (i + 1);
                   15608:                device->next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     15609:                device->attributes = dummy_devices[i].attributes;
1.1.1.29  root     15610:                device->strategy = DEVICE_SIZE - sizeof(dummy_device_routine);
                   15611:                device->interrupt = DEVICE_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.26  root     15612:                memcpy(device->dev_name, dummy_devices[i].dev_name, 8);
1.1.1.29  root     15613:                last = device;
                   15614:        }
                   15615:        if(last != NULL) {
                   15616:                last->next_driver.w.l = 0;
                   15617:                last->next_driver.w.h = XMS_TOP >> 4;
1.1.1.26  root     15618:        }
1.1.1.29  root     15619:        memcpy(mem + DEVICE_TOP + DEVICE_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.26  root     15620:        
1.1.1.25  root     15621:        // dos info
                   15622:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   15623:        dos_info->magic_word = 1;
                   15624:        dos_info->first_mcb = MEMORY_TOP >> 4;
                   15625:        dos_info->first_dpb.w.l = 0;
                   15626:        dos_info->first_dpb.w.h = DPB_TOP >> 4;
                   15627:        dos_info->first_sft.w.l = 0;
                   15628:        dos_info->first_sft.w.h = SFT_TOP >> 4;
1.1.1.26  root     15629:        dos_info->clock_device.w.l = 22 + 18 * 3;       // CLOCK$ is the 3rd device in IO.SYS
1.1.1.25  root     15630:        dos_info->clock_device.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     15631:        dos_info->con_device.w.l = 22 + 18 * 0;         // CON is the 1st device in IO.SYS
1.1.1.25  root     15632:        dos_info->con_device.w.h = DEVICE_TOP >> 4;
                   15633:        dos_info->max_sector_len = 512;
                   15634:        dos_info->disk_buf_info.w.l = offsetof(dos_info_t, disk_buf_heads);
                   15635:        dos_info->disk_buf_info.w.h = DOS_INFO_TOP >> 4;
                   15636:        dos_info->cds.w.l = 0;
                   15637:        dos_info->cds.w.h = CDS_TOP >> 4;
                   15638:        dos_info->fcb_table.w.l = 0;
                   15639:        dos_info->fcb_table.w.h = FCB_TABLE_TOP >> 4;
                   15640:        dos_info->last_drive = 'Z' - 'A' + 1;
                   15641:        dos_info->buffers_x = 20;
                   15642:        dos_info->buffers_y = 0;
                   15643:        dos_info->boot_drive = 'C' - 'A' + 1;
1.1.1.29  root     15644:        dos_info->nul_device.next_driver.w.l = 22;
                   15645:        dos_info->nul_device.next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.25  root     15646:        dos_info->nul_device.attributes = 0x8004;
1.1.1.29  root     15647:        dos_info->nul_device.strategy = DOS_INFO_SIZE - sizeof(dummy_device_routine);
                   15648:        dos_info->nul_device.interrupt = DOS_INFO_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     15649:        memcpy(dos_info->nul_device.dev_name, "NUL     ", 8);
                   15650:        dos_info->disk_buf_heads.w.l = 0;
                   15651:        dos_info->disk_buf_heads.w.h = DISK_BUF_TOP >> 4;
1.1.1.39  root     15652:        dos_info->umb_linked = (((mcb_t *)(mem + MEMORY_END - 16))->mz == 'M' ? 0x01 : 0x00);
1.1.1.25  root     15653:        dos_info->first_umb_fcb = UMB_TOP >> 4;
                   15654:        dos_info->first_mcb_2 = MEMORY_TOP >> 4;
1.1.1.29  root     15655:        memcpy(mem + DOS_INFO_TOP + DOS_INFO_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.25  root     15656:        
                   15657:        char *env;
                   15658:        if((env = getenv("LASTDRIVE")) != NULL) {
                   15659:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   15660:                        dos_info->last_drive = env[0] - 'A' + 1;
                   15661:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   15662:                        dos_info->last_drive = env[0] - 'a' + 1;
                   15663:                }
                   15664:        }
                   15665:        if((env = getenv("windir")) != NULL) {
                   15666:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   15667:                        dos_info->boot_drive = env[0] - 'A' + 1;
                   15668:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   15669:                        dos_info->boot_drive = env[0] - 'a' + 1;
                   15670:                }
                   15671:        }
                   15672: #if defined(HAS_I386)
                   15673:        dos_info->i386_or_later = 1;
                   15674: #else
                   15675:        dos_info->i386_or_later = 0;
                   15676: #endif
                   15677:        dos_info->ext_mem_size = (min(MAX_MEM, 0x4000000) - 0x100000) >> 10;
                   15678:        
1.1.1.27  root     15679:        // ems (int 67h) and xms
1.1.1.25  root     15680:        device_t *xms_device = (device_t *)(mem + XMS_TOP);
                   15681:        xms_device->next_driver.w.l = 0xffff;
                   15682:        xms_device->next_driver.w.h = 0xffff;
                   15683:        xms_device->attributes = 0xc000;
1.1.1.29  root     15684:        xms_device->strategy = XMS_SIZE - sizeof(dummy_device_routine);
                   15685:        xms_device->interrupt = XMS_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     15686:        memcpy(xms_device->dev_name, "EMMXXXX0", 8);
                   15687:        
1.1.1.26  root     15688:        mem[XMS_TOP + 0x12] = 0xcd;     // int 68h (dummy)
                   15689:        mem[XMS_TOP + 0x13] = 0x68;
                   15690:        mem[XMS_TOP + 0x14] = 0xcf;     // iret
1.1.1.19  root     15691: #ifdef SUPPORT_XMS
                   15692:        if(support_xms) {
1.1.1.26  root     15693:                mem[XMS_TOP + 0x15] = 0xcd;     // int 69h (dummy)
                   15694:                mem[XMS_TOP + 0x16] = 0x69;
                   15695:                mem[XMS_TOP + 0x17] = 0xcb;     // retf
1.1.1.19  root     15696:        } else
                   15697: #endif
1.1.1.26  root     15698:        mem[XMS_TOP + 0x15] = 0xcb;     // retf
1.1.1.29  root     15699:        memcpy(mem + XMS_TOP + XMS_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.19  root     15700:        
1.1.1.26  root     15701:        // irq12 routine (mouse)
1.1.1.24  root     15702:        mem[0xfffd0 + 0x00] = 0xcd;     // int 6ah (dummy)
                   15703:        mem[0xfffd0 + 0x01] = 0x6a;
                   15704:        mem[0xfffd0 + 0x02] = 0x9a;     // call far mouse
                   15705:        mem[0xfffd0 + 0x03] = 0xff;
                   15706:        mem[0xfffd0 + 0x04] = 0xff;
                   15707:        mem[0xfffd0 + 0x05] = 0xff;
                   15708:        mem[0xfffd0 + 0x06] = 0xff;
                   15709:        mem[0xfffd0 + 0x07] = 0xcd;     // int 6bh (dummy)
                   15710:        mem[0xfffd0 + 0x08] = 0x6b;
                   15711:        mem[0xfffd0 + 0x09] = 0xcf;     // iret
                   15712:        
1.1.1.27  root     15713:        // case map routine
                   15714:        mem[0xfffd0 + 0x0a] = 0xcd;     // int 6ch (dummy)
                   15715:        mem[0xfffd0 + 0x0b] = 0x6c;
                   15716:        mem[0xfffd0 + 0x0c] = 0xcb;     // retf
                   15717:        
                   15718:        // font read routine
                   15719:        mem[0xfffd0 + 0x0d] = 0xcd;     // int 6dh (dummy)
                   15720:        mem[0xfffd0 + 0x0e] = 0x6d;
                   15721:        mem[0xfffd0 + 0x0f] = 0xcb;     // retf
1.1.1.19  root     15722:        
1.1.1.32  root     15723:        // error message read routine
                   15724:        mem[0xfffd0 + 0x10] = 0xcd;     // int 6eh (dummy)
                   15725:        mem[0xfffd0 + 0x11] = 0x6e;
                   15726:        mem[0xfffd0 + 0x12] = 0xcb;     // retf
                   15727:        
1.1.1.35  root     15728:        // dummy loop to wait BIOS/DOS service is done
                   15729:        mem[0xfffd0 + 0x13] = 0xe6;     // out f7h, al
                   15730:        mem[0xfffd0 + 0x14] = 0xf7;
                   15731:        mem[0xfffd0 + 0x15] = 0x78;     // js/jns -4
                   15732:        mem[0xfffd0 + 0x16] = 0xfc;
                   15733:        mem[0xfffd0 + 0x17] = 0xcb;     // retf
                   15734:        
1.1.1.26  root     15735:        // irq0 routine (system time)
1.1.1.35  root     15736:        mem[0xfffd0 + 0x18] = 0xcd;     // int 1ch
                   15737:        mem[0xfffd0 + 0x19] = 0x1c;
                   15738:        mem[0xfffd0 + 0x1a] = 0xea;     // jmp far (IRET_TOP >> 4):0008
                   15739:        mem[0xfffd0 + 0x1b] = 0x08;
                   15740:        mem[0xfffd0 + 0x1c] = 0x00;
                   15741:        mem[0xfffd0 + 0x1d] = ((IRET_TOP >> 4)     ) & 0xff;
                   15742:        mem[0xfffd0 + 0x1e] = ((IRET_TOP >> 4) >> 8) & 0xff;
1.1.1.14  root     15743:        
1.1.1.26  root     15744:        // boot routine
1.1       root     15745:        mem[0xffff0] = 0xf4;    // halt
                   15746:        mem[0xffff1] = 0xcd;    // int 21h
                   15747:        mem[0xffff2] = 0x21;
                   15748:        mem[0xffff3] = 0xcb;    // retf
                   15749:        
1.1.1.24  root     15750:        mem[0xffff5] = '0';     // rom date
                   15751:        mem[0xffff6] = '2';
                   15752:        mem[0xffff7] = '/';
                   15753:        mem[0xffff8] = '2';
                   15754:        mem[0xffff9] = '2';
                   15755:        mem[0xffffa] = '/';
                   15756:        mem[0xffffb] = '0';
                   15757:        mem[0xffffc] = '6';
                   15758:        mem[0xffffe] = 0xfc;    // machine id
                   15759:        mem[0xfffff] = 0x00;
                   15760:        
1.1       root     15761:        // param block
                   15762:        // + 0: param block (22bytes)
                   15763:        // +24: fcb1/2 (20bytes)
                   15764:        // +44: command tail (128bytes)
                   15765:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   15766:        param->env_seg = 0;
                   15767:        param->cmd_line.w.l = 44;
                   15768:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   15769:        param->fcb1.w.l = 24;
                   15770:        param->fcb1.w.h = (WORK_TOP >> 4);
                   15771:        param->fcb2.w.l = 24;
                   15772:        param->fcb2.w.h = (WORK_TOP >> 4);
                   15773:        
                   15774:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   15775:        
                   15776:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   15777:        if(argc > 1) {
                   15778:                sprintf(cmd_line->cmd, " %s", argv[1]);
                   15779:                for(int i = 2; i < argc; i++) {
                   15780:                        char tmp[128];
                   15781:                        sprintf(tmp, "%s %s", cmd_line->cmd, argv[i]);
                   15782:                        strcpy(cmd_line->cmd, tmp);
                   15783:                }
                   15784:                cmd_line->len = (UINT8)strlen(cmd_line->cmd);
                   15785:        } else {
                   15786:                cmd_line->len = 0;
                   15787:        }
                   15788:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   15789:        
                   15790:        // system file table
1.1.1.21  root     15791:        *(UINT32 *)(mem + SFT_TOP + 0) = 0xffffffff;
                   15792:        *(UINT16 *)(mem + SFT_TOP + 4) = 20;
1.1       root     15793:        
1.1.1.19  root     15794:        // disk buffer header (from DOSBox)
                   15795:        *(UINT16 *)(mem + DISK_BUF_TOP +  0) = 0xffff;          // forward ptr
                   15796:        *(UINT16 *)(mem + DISK_BUF_TOP +  2) = 0xffff;          // backward ptr
                   15797:        *(UINT8  *)(mem + DISK_BUF_TOP +  4) = 0xff;            // not in use
                   15798:        *(UINT8  *)(mem + DISK_BUF_TOP + 10) = 0x01;            // number of FATs
                   15799:        *(UINT32 *)(mem + DISK_BUF_TOP + 13) = 0xffffffff;      // pointer to DPB
                   15800:        
1.1       root     15801:        // current directory structure
                   15802:        msdos_cds_update(_getdrive() - 1);
                   15803:        
                   15804:        // fcb table
                   15805:        *(UINT32 *)(mem + FCB_TABLE_TOP + 0) = 0xffffffff;
1.1.1.14  root     15806:        *(UINT16 *)(mem + FCB_TABLE_TOP + 4) = 0;
1.1       root     15807:        
1.1.1.17  root     15808:        // nls stuff
                   15809:        msdos_nls_tables_init();
1.1       root     15810:        
                   15811:        // execute command
1.1.1.28  root     15812:        try {
                   15813:                if(msdos_process_exec(argv[0], param, 0)) {
                   15814:                        fatalerror("'%s' not found\n", argv[0]);
                   15815:                }
                   15816:        } catch(...) {
                   15817:                // we should not reach here :-(
                   15818:                fatalerror("failed to start '%s' because of unexpected exeption\n", argv[0]);
1.1       root     15819:        }
                   15820:        retval = 0;
                   15821:        return(0);
                   15822: }
                   15823: 
                   15824: #define remove_std_file(path) { \
                   15825:        int fd = _open(path, _O_RDONLY | _O_BINARY); \
                   15826:        if(fd != -1) { \
                   15827:                _lseek(fd, 0, SEEK_END); \
                   15828:                int size = _tell(fd); \
                   15829:                _close(fd); \
                   15830:                if(size == 0) { \
                   15831:                        remove(path); \
                   15832:                } \
                   15833:        } \
                   15834: }
                   15835: 
                   15836: void msdos_finish()
                   15837: {
                   15838:        for(int i = 0; i < MAX_FILES; i++) {
                   15839:                if(file_handler[i].valid) {
                   15840:                        _close(i);
                   15841:                }
                   15842:        }
1.1.1.21  root     15843: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     15844:        remove_std_file("stdaux.txt");
1.1.1.21  root     15845: #endif
1.1.1.30  root     15846: #ifdef SUPPORT_XMS
                   15847:        msdos_xms_finish();
                   15848: #endif
1.1       root     15849:        msdos_dbcs_table_finish();
                   15850: }
                   15851: 
                   15852: /* ----------------------------------------------------------------------------
                   15853:        PC/AT hardware emulation
                   15854: ---------------------------------------------------------------------------- */
                   15855: 
                   15856: void hardware_init()
                   15857: {
1.1.1.3   root     15858:        CPU_INIT_CALL(CPU_MODEL);
1.1       root     15859:        CPU_RESET_CALL(CPU_MODEL);
1.1.1.14  root     15860:        m_IF = 1;
1.1.1.3   root     15861: #if defined(HAS_I386)
1.1       root     15862:        cpu_type = (REG32(EDX) >> 8) & 0x0f;
                   15863:        cpu_step = (REG32(EDX) >> 0) & 0x0f;
1.1.1.3   root     15864: #endif
                   15865:        i386_set_a20_line(0);
1.1.1.14  root     15866:        
1.1.1.19  root     15867:        ems_init();
1.1.1.25  root     15868:        dma_init();
1.1       root     15869:        pic_init();
1.1.1.25  root     15870:        pio_init();
1.1.1.8   root     15871: #ifdef PIT_ALWAYS_RUNNING
                   15872:        pit_init();
                   15873: #else
1.1       root     15874:        pit_active = 0;
                   15875: #endif
1.1.1.25  root     15876:        sio_init();
1.1.1.8   root     15877:        cmos_init();
                   15878:        kbd_init();
1.1       root     15879: }
                   15880: 
1.1.1.10  root     15881: void hardware_finish()
                   15882: {
                   15883: #if defined(HAS_I386)
                   15884:        vtlb_free(m_vtlb);
                   15885: #endif
1.1.1.19  root     15886:        ems_finish();
1.1.1.37  root     15887:        pio_finish();
1.1.1.25  root     15888:        sio_finish();
1.1.1.10  root     15889: }
                   15890: 
1.1.1.28  root     15891: void hardware_release()
                   15892: {
                   15893:        // release hardware resources when this program will be terminated abnormally
                   15894: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     15895:        if(fp_debug_log != NULL) {
                   15896:                fclose(fp_debug_log);
                   15897:                fp_debug_log = NULL;
1.1.1.28  root     15898:        }
                   15899: #endif
                   15900: #if defined(HAS_I386)
                   15901:        vtlb_free(m_vtlb);
                   15902: #endif
                   15903:        ems_release();
1.1.1.37  root     15904:        pio_release();
1.1.1.28  root     15905:        sio_release();
                   15906: }
                   15907: 
1.1       root     15908: void hardware_run()
                   15909: {
1.1.1.22  root     15910: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.28  root     15911:        // open debug log file after msdos_init() is done not to use the standard file handlers
1.1.1.33  root     15912:        fp_debug_log = fopen("debug.log", "w");
1.1.1.22  root     15913: #endif
1.1.1.3   root     15914:        while(!m_halted) {
                   15915: #if defined(HAS_I386)
1.1       root     15916:                CPU_EXECUTE_CALL(i386);
1.1.1.14  root     15917:                if(m_eip != m_prev_eip) {
1.1.1.35  root     15918:                        idle_ops++;
                   15919:                }
1.1.1.14  root     15920: #else
1.1.1.35  root     15921:                CPU_EXECUTE_CALL(CPU_MODEL);
1.1.1.14  root     15922:                if(m_pc != m_prevpc) {
1.1.1.35  root     15923:                        idle_ops++;
1.1.1.14  root     15924:                }
1.1.1.35  root     15925: #endif
                   15926:                if(++update_ops == UPDATE_OPS) {
1.1       root     15927:                        hardware_update();
1.1.1.35  root     15928:                        update_ops = 0;
1.1       root     15929:                }
                   15930:        }
1.1.1.22  root     15931: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     15932:        if(fp_debug_log != NULL) {
                   15933:                fclose(fp_debug_log);
                   15934:                fp_debug_log = NULL;
1.1.1.28  root     15935:        }
1.1.1.22  root     15936: #endif
1.1       root     15937: }
                   15938: 
                   15939: void hardware_update()
                   15940: {
1.1.1.8   root     15941:        static UINT32 prev_time = 0;
                   15942:        UINT32 cur_time = timeGetTime();
                   15943:        
                   15944:        if(prev_time != cur_time) {
                   15945:                // update pit and raise irq0
                   15946: #ifndef PIT_ALWAYS_RUNNING
                   15947:                if(pit_active)
                   15948: #endif
                   15949:                {
                   15950:                        if(pit_run(0, cur_time)) {
                   15951:                                pic_req(0, 0, 1);
                   15952:                        }
                   15953:                        pit_run(1, cur_time);
                   15954:                        pit_run(2, cur_time);
                   15955:                }
1.1.1.24  root     15956:                
1.1.1.25  root     15957:                // update sio and raise irq4/3
1.1.1.29  root     15958:                for(int c = 0; c < 4; c++) {
1.1.1.25  root     15959:                        sio_update(c);
                   15960:                }
                   15961:                
1.1.1.24  root     15962:                // update keyboard and mouse
1.1.1.14  root     15963:                static UINT32 prev_tick = 0;
                   15964:                UINT32 cur_tick = cur_time / 32;
1.1.1.25  root     15965:                
1.1.1.14  root     15966:                if(prev_tick != cur_tick) {
                   15967:                        // update keyboard flags
                   15968:                        UINT8 state;
1.1.1.24  root     15969:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x0001) ? 0x80 : 0;
                   15970:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x0001) ? 0x40 : 0;
                   15971:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x0001) ? 0x20 : 0;
                   15972:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x0001) ? 0x10 : 0;
                   15973:                        state |= (GetAsyncKeyState(VK_MENU    ) & 0x8000) ? 0x08 : 0;
                   15974:                        state |= (GetAsyncKeyState(VK_CONTROL ) & 0x8000) ? 0x04 : 0;
                   15975:                        state |= (GetAsyncKeyState(VK_LSHIFT  ) & 0x8000) ? 0x02 : 0;
                   15976:                        state |= (GetAsyncKeyState(VK_RSHIFT  ) & 0x8000) ? 0x01 : 0;
1.1.1.14  root     15977:                        mem[0x417] = state;
                   15978:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x8000) ? 0x80 : 0;
                   15979:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   15980:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   15981:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
1.1.1.24  root     15982: //                     state |= (GetAsyncKeyState(VK_PAUSE   ) & 0x0001) ? 0x08 : 0;
                   15983: //                     state |= (GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x04 : 0;
1.1.1.14  root     15984:                        state |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   15985:                        state |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   15986:                        mem[0x418] = state;
                   15987:                        
1.1.1.24  root     15988:                        // update console input if needed
1.1.1.34  root     15989:                        if(!key_changed || mouse.hidden == 0) {
1.1.1.24  root     15990:                                update_console_input();
                   15991:                        }
                   15992:                        
                   15993:                        // raise irq1 if key is pressed/released
                   15994:                        if(key_changed) {
1.1.1.8   root     15995:                                pic_req(0, 1, 1);
1.1.1.24  root     15996:                                key_changed = false;
                   15997:                        }
                   15998:                        
                   15999:                        // raise irq12 if mouse status is changed
                   16000:                        if(mouse.status & mouse.call_mask) {
1.1.1.34  root     16001: //                             if(mouse.hidden == 0) {
1.1.1.24  root     16002:                                        pic_req(1, 4, 1);
                   16003:                                        mouse.status_irq = mouse.status & mouse.call_mask;
1.1.1.34  root     16004: //                             }
1.1.1.24  root     16005:                                mouse.status &= ~mouse.call_mask;
1.1.1.8   root     16006:                        }
1.1.1.24  root     16007:                        
1.1.1.14  root     16008:                        prev_tick = cur_tick;
1.1.1.8   root     16009:                }
1.1.1.24  root     16010:                
1.1.1.19  root     16011:                // update daily timer counter
                   16012:                pcbios_update_daily_timer_counter(cur_time);
1.1.1.25  root     16013:                
1.1.1.8   root     16014:                prev_time = cur_time;
1.1       root     16015:        }
                   16016: }
                   16017: 
1.1.1.19  root     16018: // ems
                   16019: 
                   16020: void ems_init()
                   16021: {
                   16022:        memset(ems_handles, 0, sizeof(ems_handles));
                   16023:        memset(ems_pages, 0, sizeof(ems_pages));
                   16024:        free_ems_pages = MAX_EMS_PAGES;
                   16025: }
                   16026: 
                   16027: void ems_finish()
                   16028: {
1.1.1.28  root     16029:        ems_release();
                   16030: }
                   16031: 
                   16032: void ems_release()
                   16033: {
1.1.1.31  root     16034:        for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.28  root     16035:                if(ems_handles[i].buffer != NULL) {
1.1.1.19  root     16036:                        free(ems_handles[i].buffer);
                   16037:                        ems_handles[i].buffer = NULL;
                   16038:                }
                   16039:        }
                   16040: }
                   16041: 
                   16042: void ems_allocate_pages(int handle, int pages)
                   16043: {
                   16044:        if(pages > 0) {
                   16045:                ems_handles[handle].buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   16046:        } else {
                   16047:                ems_handles[handle].buffer = NULL;
                   16048:        }
                   16049:        ems_handles[handle].pages = pages;
                   16050:        ems_handles[handle].allocated = true;
                   16051:        free_ems_pages -= pages;
                   16052: }
                   16053: 
                   16054: void ems_reallocate_pages(int handle, int pages)
                   16055: {
                   16056:        if(ems_handles[handle].allocated) {
                   16057:                if(ems_handles[handle].pages != pages) {
                   16058:                        UINT8 *new_buffer = NULL;
                   16059:                        
                   16060:                        if(pages > 0) {
                   16061:                                new_buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   16062:                        }
1.1.1.32  root     16063:                        if(ems_handles[handle].buffer != NULL) {
                   16064:                                if(new_buffer != NULL) {
1.1.1.19  root     16065:                                        if(pages > ems_handles[handle].pages) {
                   16066:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * ems_handles[handle].pages);
                   16067:                                        } else {
                   16068:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * pages);
                   16069:                                        }
                   16070:                                }
                   16071:                                free(ems_handles[handle].buffer);
                   16072:                                ems_handles[handle].buffer = NULL;
                   16073:                        }
                   16074:                        free_ems_pages += ems_handles[handle].pages;
                   16075:                        
                   16076:                        ems_handles[handle].buffer = new_buffer;
                   16077:                        ems_handles[handle].pages = pages;
                   16078:                        free_ems_pages -= pages;
                   16079:                }
                   16080:        } else {
                   16081:                ems_allocate_pages(handle, pages);
                   16082:        }
                   16083: }
                   16084: 
                   16085: void ems_release_pages(int handle)
                   16086: {
                   16087:        if(ems_handles[handle].allocated) {
1.1.1.32  root     16088:                if(ems_handles[handle].buffer != NULL) {
1.1.1.19  root     16089:                        free(ems_handles[handle].buffer);
                   16090:                        ems_handles[handle].buffer = NULL;
                   16091:                }
                   16092:                free_ems_pages += ems_handles[handle].pages;
                   16093:                ems_handles[handle].allocated = false;
                   16094:        }
                   16095: }
                   16096: 
                   16097: void ems_map_page(int physical, int handle, int logical)
                   16098: {
                   16099:        if(ems_pages[physical].mapped) {
                   16100:                if(ems_pages[physical].handle == handle && ems_pages[physical].page == logical) {
                   16101:                        return;
                   16102:                }
                   16103:                ems_unmap_page(physical);
                   16104:        }
1.1.1.32  root     16105:        if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     16106:                memcpy(mem + EMS_TOP + 0x4000 * physical, ems_handles[handle].buffer + 0x4000 * logical, 0x4000);
                   16107:        }
                   16108:        ems_pages[physical].handle = handle;
                   16109:        ems_pages[physical].page = logical;
                   16110:        ems_pages[physical].mapped = true;
                   16111: }
                   16112: 
                   16113: void ems_unmap_page(int physical)
                   16114: {
                   16115:        if(ems_pages[physical].mapped) {
                   16116:                int handle = ems_pages[physical].handle;
                   16117:                int logical = ems_pages[physical].page;
                   16118:                
1.1.1.32  root     16119:                if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     16120:                        memcpy(ems_handles[handle].buffer + 0x4000 * logical, mem + EMS_TOP + 0x4000 * physical, 0x4000);
                   16121:                }
                   16122:                ems_pages[physical].mapped = false;
                   16123:        }
                   16124: }
                   16125: 
1.1.1.25  root     16126: // dma
1.1       root     16127: 
1.1.1.25  root     16128: void dma_init()
1.1       root     16129: {
1.1.1.26  root     16130:        memset(dma, 0, sizeof(dma));
1.1.1.25  root     16131:        for(int c = 0; c < 2; c++) {
1.1.1.26  root     16132: //             for(int ch = 0; ch < 4; ch++) {
                   16133: //                     dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w = 0xffff;
                   16134: //             }
1.1.1.25  root     16135:                dma_reset(c);
                   16136:        }
1.1       root     16137: }
                   16138: 
1.1.1.25  root     16139: void dma_reset(int c)
1.1       root     16140: {
1.1.1.25  root     16141:        dma[c].low_high = false;
                   16142:        dma[c].cmd = dma[c].req = dma[c].tc = 0;
                   16143:        dma[c].mask = 0xff;
                   16144: }
                   16145: 
                   16146: void dma_write(int c, UINT32 addr, UINT8 data)
                   16147: {
                   16148:        int ch = (addr >> 1) & 3;
                   16149:        UINT8 bit = 1 << (data & 3);
                   16150:        
                   16151:        switch(addr & 0x0f) {
                   16152:        case 0x00: case 0x02: case 0x04: case 0x06:
                   16153:                if(dma[c].low_high) {
                   16154:                        dma[c].ch[ch].bareg.b.h = data;
1.1       root     16155:                } else {
1.1.1.25  root     16156:                        dma[c].ch[ch].bareg.b.l = data;
                   16157:                }
                   16158:                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   16159:                dma[c].low_high = !dma[c].low_high;
                   16160:                break;
                   16161:        case 0x01: case 0x03: case 0x05: case 0x07:
                   16162:                if(dma[c].low_high) {
                   16163:                        dma[c].ch[ch].bcreg.b.h = data;
                   16164:                } else {
                   16165:                        dma[c].ch[ch].bcreg.b.l = data;
                   16166:                }
                   16167:                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   16168:                dma[c].low_high = !dma[c].low_high;
                   16169:                break;
                   16170:        case 0x08:
                   16171:                // command register
                   16172:                dma[c].cmd = data;
                   16173:                break;
                   16174:        case 0x09:
                   16175:                // dma[c].request register
                   16176:                if(data & 4) {
                   16177:                        if(!(dma[c].req & bit)) {
                   16178:                                dma[c].req |= bit;
                   16179: //                             dma_run(c, ch);
                   16180:                        }
                   16181:                } else {
                   16182:                        dma[c].req &= ~bit;
                   16183:                }
                   16184:                break;
                   16185:        case 0x0a:
                   16186:                // single mask register
                   16187:                if(data & 4) {
                   16188:                        dma[c].mask |= bit;
                   16189:                } else {
                   16190:                        dma[c].mask &= ~bit;
                   16191:                }
                   16192:                break;
                   16193:        case 0x0b:
                   16194:                // mode register
                   16195:                dma[c].ch[data & 3].mode = data;
                   16196:                break;
                   16197:        case 0x0c:
                   16198:                dma[c].low_high = false;
                   16199:                break;
                   16200:        case 0x0d:
                   16201:                // clear master
                   16202:                dma_reset(c);
                   16203:                break;
                   16204:        case 0x0e:
                   16205:                // clear mask register
                   16206:                dma[c].mask = 0;
                   16207:                break;
                   16208:        case 0x0f:
                   16209:                // all mask register
                   16210:                dma[c].mask = data & 0x0f;
                   16211:                break;
                   16212:        }
                   16213: }
                   16214: 
                   16215: UINT8 dma_read(int c, UINT32 addr)
                   16216: {
                   16217:        int ch = (addr >> 1) & 3;
                   16218:        UINT8 val = 0xff;
                   16219:        
                   16220:        switch(addr & 0x0f) {
                   16221:        case 0x00: case 0x02: case 0x04: case 0x06:
                   16222:                if(dma[c].low_high) {
                   16223:                        val = dma[c].ch[ch].areg.b.h;
                   16224:                } else {
                   16225:                        val = dma[c].ch[ch].areg.b.l;
                   16226:                }
                   16227:                dma[c].low_high = !dma[c].low_high;
                   16228:                return(val);
                   16229:        case 0x01: case 0x03: case 0x05: case 0x07:
                   16230:                if(dma[c].low_high) {
                   16231:                        val = dma[c].ch[ch].creg.b.h;
                   16232:                } else {
                   16233:                        val = dma[c].ch[ch].creg.b.l;
                   16234:                }
                   16235:                dma[c].low_high = !dma[c].low_high;
                   16236:                return(val);
                   16237:        case 0x08:
                   16238:                // status register
                   16239:                val = (dma[c].req << 4) | dma[c].tc;
                   16240:                dma[c].tc = 0;
                   16241:                return(val);
                   16242:        case 0x0d:
1.1.1.26  root     16243:                // temporary register (intel 82374 does not support)
1.1.1.25  root     16244:                return(dma[c].tmp & 0xff);
1.1.1.26  root     16245:        case 0x0f:
                   16246:                // mask register (intel 82374 does support)
                   16247:                return(dma[c].mask);
1.1.1.25  root     16248:        }
                   16249:        return(0xff);
                   16250: }
                   16251: 
                   16252: void dma_page_write(int c, int ch, UINT8 data)
                   16253: {
                   16254:        dma[c].ch[ch].pagereg = data;
                   16255: }
                   16256: 
                   16257: UINT8 dma_page_read(int c, int ch)
                   16258: {
                   16259:        return(dma[c].ch[ch].pagereg);
                   16260: }
                   16261: 
                   16262: void dma_run(int c, int ch)
                   16263: {
                   16264:        UINT8 bit = 1 << ch;
                   16265:        
                   16266:        if((dma[c].req & bit) && !(dma[c].mask & bit)) {
                   16267:                // execute dma
                   16268:                while(dma[c].req & bit) {
                   16269:                        if(ch == 0 && (dma[c].cmd & 0x01)) {
                   16270:                                // memory -> memory
                   16271:                                UINT32 saddr = dma[c].ch[0].areg.w | (dma[c].ch[0].pagereg << 16);
                   16272:                                UINT32 daddr = dma[c].ch[1].areg.w | (dma[c].ch[1].pagereg << 16);
                   16273:                                
                   16274:                                if(c == 0) {
                   16275:                                        dma[c].tmp = read_byte(saddr);
                   16276:                                        write_byte(daddr, dma[c].tmp);
                   16277:                                } else {
                   16278:                                        dma[c].tmp = read_word(saddr << 1);
                   16279:                                        write_word(daddr << 1, dma[c].tmp);
                   16280:                                }
                   16281:                                if(!(dma[c].cmd & 0x02)) {
                   16282:                                        if(dma[c].ch[0].mode & 0x20) {
                   16283:                                                dma[c].ch[0].areg.w--;
                   16284:                                                if(dma[c].ch[0].areg.w == 0xffff) {
                   16285:                                                        dma[c].ch[0].pagereg--;
                   16286:                                                }
                   16287:                                        } else {
                   16288:                                                dma[c].ch[0].areg.w++;
                   16289:                                                if(dma[c].ch[0].areg.w == 0) {
                   16290:                                                        dma[c].ch[0].pagereg++;
                   16291:                                                }
                   16292:                                        }
                   16293:                                }
                   16294:                                if(dma[c].ch[1].mode & 0x20) {
                   16295:                                        dma[c].ch[1].areg.w--;
                   16296:                                        if(dma[c].ch[1].areg.w == 0xffff) {
                   16297:                                                dma[c].ch[1].pagereg--;
                   16298:                                        }
                   16299:                                } else {
                   16300:                                        dma[c].ch[1].areg.w++;
                   16301:                                        if(dma[c].ch[1].areg.w == 0) {
                   16302:                                                dma[c].ch[1].pagereg++;
                   16303:                                        }
                   16304:                                }
                   16305:                                
                   16306:                                // check dma condition
                   16307:                                if(dma[c].ch[0].creg.w-- == 0) {
                   16308:                                        if(dma[c].ch[0].mode & 0x10) {
                   16309:                                                // self initialize
                   16310:                                                dma[c].ch[0].areg.w = dma[c].ch[0].bareg.w;
                   16311:                                                dma[c].ch[0].creg.w = dma[c].ch[0].bcreg.w;
                   16312:                                        } else {
                   16313: //                                             dma[c].mask |= bit;
                   16314:                                        }
                   16315:                                }
                   16316:                                if(dma[c].ch[1].creg.w-- == 0) {
                   16317:                                        // terminal count
                   16318:                                        if(dma[c].ch[1].mode & 0x10) {
                   16319:                                                // self initialize
                   16320:                                                dma[c].ch[1].areg.w = dma[c].ch[1].bareg.w;
                   16321:                                                dma[c].ch[1].creg.w = dma[c].ch[1].bcreg.w;
                   16322:                                        } else {
                   16323:                                                dma[c].mask |= bit;
                   16324:                                        }
                   16325:                                        dma[c].req &= ~bit;
                   16326:                                        dma[c].tc |= bit;
                   16327:                                }
                   16328:                        } else {
                   16329:                                UINT32 addr = dma[c].ch[ch].areg.w | (dma[c].ch[ch].pagereg << 16);
                   16330:                                
                   16331:                                if((dma[c].ch[ch].mode & 0x0c) == 0x00) {
                   16332:                                        // verify
                   16333:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x04) {
                   16334:                                        // io -> memory
                   16335:                                        if(c == 0) {
                   16336:                                                dma[c].tmp = read_io_byte(dma[c].ch[ch].port);
                   16337:                                                write_byte(addr, dma[c].tmp);
                   16338:                                        } else {
                   16339:                                                dma[c].tmp = read_io_word(dma[c].ch[ch].port);
                   16340:                                                write_word(addr << 1, dma[c].tmp);
                   16341:                                        }
                   16342:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x08) {
                   16343:                                        // memory -> io
                   16344:                                        if(c == 0) {
                   16345:                                                dma[c].tmp = read_byte(addr);
                   16346:                                                write_io_byte(dma[c].ch[ch].port, dma[c].tmp);
                   16347:                                        } else {
                   16348:                                                dma[c].tmp = read_word(addr << 1);
                   16349:                                                write_io_word(dma[c].ch[ch].port, dma[c].tmp);
                   16350:                                        }
                   16351:                                }
                   16352:                                if(dma[c].ch[ch].mode & 0x20) {
                   16353:                                        dma[c].ch[ch].areg.w--;
                   16354:                                        if(dma[c].ch[ch].areg.w == 0xffff) {
                   16355:                                                dma[c].ch[ch].pagereg--;
                   16356:                                        }
                   16357:                                } else {
                   16358:                                        dma[c].ch[ch].areg.w++;
                   16359:                                        if(dma[c].ch[ch].areg.w == 0) {
                   16360:                                                dma[c].ch[ch].pagereg++;
                   16361:                                        }
                   16362:                                }
                   16363:                                
                   16364:                                // check dma condition
                   16365:                                if(dma[c].ch[ch].creg.w-- == 0) {
                   16366:                                        // terminal count
                   16367:                                        if(dma[c].ch[ch].mode & 0x10) {
                   16368:                                                // self initialize
                   16369:                                                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   16370:                                                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   16371:                                        } else {
                   16372:                                                dma[c].mask |= bit;
                   16373:                                        }
                   16374:                                        dma[c].req &= ~bit;
                   16375:                                        dma[c].tc |= bit;
                   16376:                                } else if((dma[c].ch[ch].mode & 0xc0) == 0x40) {
                   16377:                                        // single mode
                   16378:                                        break;
                   16379:                                }
                   16380:                        }
                   16381:                }
                   16382:        }
                   16383: }
                   16384: 
                   16385: // pic
                   16386: 
                   16387: void pic_init()
                   16388: {
                   16389:        memset(pic, 0, sizeof(pic));
                   16390:        pic[0].imr = pic[1].imr = 0xff;
                   16391:        
                   16392:        // from bochs bios
                   16393:        pic_write(0, 0, 0x11);  // icw1 = 11h
                   16394:        pic_write(0, 1, 0x08);  // icw2 = 08h
                   16395:        pic_write(0, 1, 0x04);  // icw3 = 04h
                   16396:        pic_write(0, 1, 0x01);  // icw4 = 01h
                   16397:        pic_write(0, 1, 0xb8);  // ocw1 = b8h
                   16398:        pic_write(1, 0, 0x11);  // icw1 = 11h
                   16399:        pic_write(1, 1, 0x70);  // icw2 = 70h
                   16400:        pic_write(1, 1, 0x02);  // icw3 = 02h
                   16401:        pic_write(1, 1, 0x01);  // icw4 = 01h
                   16402: }
                   16403: 
                   16404: void pic_write(int c, UINT32 addr, UINT8 data)
                   16405: {
                   16406:        if(addr & 1) {
                   16407:                if(pic[c].icw2_r) {
                   16408:                        // icw2
                   16409:                        pic[c].icw2 = data;
                   16410:                        pic[c].icw2_r = 0;
                   16411:                } else if(pic[c].icw3_r) {
                   16412:                        // icw3
                   16413:                        pic[c].icw3 = data;
                   16414:                        pic[c].icw3_r = 0;
                   16415:                } else if(pic[c].icw4_r) {
                   16416:                        // icw4
                   16417:                        pic[c].icw4 = data;
                   16418:                        pic[c].icw4_r = 0;
                   16419:                } else {
                   16420:                        // ocw1
1.1       root     16421:                        pic[c].imr = data;
                   16422:                }
                   16423:        } else {
                   16424:                if(data & 0x10) {
                   16425:                        // icw1
                   16426:                        pic[c].icw1 = data;
                   16427:                        pic[c].icw2_r = 1;
                   16428:                        pic[c].icw3_r = (data & 2) ? 0 : 1;
                   16429:                        pic[c].icw4_r = data & 1;
                   16430:                        pic[c].irr = 0;
                   16431:                        pic[c].isr = 0;
                   16432:                        pic[c].imr = 0;
                   16433:                        pic[c].prio = 0;
                   16434:                        if(!(pic[c].icw1 & 1)) {
                   16435:                                pic[c].icw4 = 0;
                   16436:                        }
                   16437:                        pic[c].ocw3 = 0;
                   16438:                } else if(data & 8) {
                   16439:                        // ocw3
                   16440:                        if(!(data & 2)) {
                   16441:                                data = (data & ~1) | (pic[c].ocw3 & 1);
                   16442:                        }
                   16443:                        if(!(data & 0x40)) {
                   16444:                                data = (data & ~0x20) | (pic[c].ocw3 & 0x20);
                   16445:                        }
                   16446:                        pic[c].ocw3 = data;
                   16447:                } else {
                   16448:                        // ocw2
                   16449:                        int level = 0;
                   16450:                        if(data & 0x40) {
                   16451:                                level = data & 7;
                   16452:                        } else {
                   16453:                                if(!pic[c].isr) {
                   16454:                                        return;
                   16455:                                }
                   16456:                                level = pic[c].prio;
                   16457:                                while(!(pic[c].isr & (1 << level))) {
                   16458:                                        level = (level + 1) & 7;
                   16459:                                }
                   16460:                        }
                   16461:                        if(data & 0x80) {
                   16462:                                pic[c].prio = (level + 1) & 7;
                   16463:                        }
                   16464:                        if(data & 0x20) {
                   16465:                                pic[c].isr &= ~(1 << level);
                   16466:                        }
                   16467:                }
                   16468:        }
                   16469:        pic_update();
                   16470: }
                   16471: 
                   16472: UINT8 pic_read(int c, UINT32 addr)
                   16473: {
                   16474:        if(addr & 1) {
                   16475:                return(pic[c].imr);
                   16476:        } else {
                   16477:                // polling mode is not supported...
                   16478:                //if(pic[c].ocw3 & 4) {
                   16479:                //      return ???;
                   16480:                //}
                   16481:                if(pic[c].ocw3 & 1) {
                   16482:                        return(pic[c].isr);
                   16483:                } else {
                   16484:                        return(pic[c].irr);
                   16485:                }
                   16486:        }
                   16487: }
                   16488: 
                   16489: void pic_req(int c, int level, int signal)
                   16490: {
                   16491:        if(signal) {
                   16492:                pic[c].irr |= (1 << level);
                   16493:        } else {
                   16494:                pic[c].irr &= ~(1 << level);
                   16495:        }
                   16496:        pic_update();
                   16497: }
                   16498: 
                   16499: int pic_ack()
                   16500: {
                   16501:        // ack (INTA=L)
                   16502:        pic[pic_req_chip].isr |= pic_req_bit;
                   16503:        pic[pic_req_chip].irr &= ~pic_req_bit;
                   16504:        if(pic_req_chip > 0) {
                   16505:                // update isr and irr of master
                   16506:                UINT8 slave = 1 << (pic[pic_req_chip].icw3 & 7);
                   16507:                pic[pic_req_chip - 1].isr |= slave;
                   16508:                pic[pic_req_chip - 1].irr &= ~slave;
                   16509:        }
                   16510:        //if(pic[pic_req_chip].icw4 & 1) {
                   16511:                // 8086 mode
                   16512:                int vector = (pic[pic_req_chip].icw2 & 0xf8) | pic_req_level;
                   16513:        //} else {
                   16514:        //      // 8080 mode
                   16515:        //      UINT16 addr = (UINT16)pic[pic_req_chip].icw2 << 8;
                   16516:        //      if(pic[pic_req_chip].icw1 & 4) {
                   16517:        //              addr |= (pic[pic_req_chip].icw1 & 0xe0) | (pic_req_level << 2);
                   16518:        //      } else {
                   16519:        //              addr |= (pic[pic_req_chip].icw1 & 0xc0) | (pic_req_level << 3);
                   16520:        //      }
                   16521:        //      vector = 0xcd | (addr << 8);
                   16522:        //}
                   16523:        if(pic[pic_req_chip].icw4 & 2) {
                   16524:                // auto eoi
                   16525:                pic[pic_req_chip].isr &= ~pic_req_bit;
                   16526:        }
                   16527:        return(vector);
                   16528: }
                   16529: 
                   16530: void pic_update()
                   16531: {
                   16532:        for(int c = 0; c < 2; c++) {
                   16533:                UINT8 irr = pic[c].irr;
                   16534:                if(c + 1 < 2) {
                   16535:                        // this is master
                   16536:                        if(pic[c + 1].irr & (~pic[c + 1].imr)) {
                   16537:                                // request from slave
                   16538:                                irr |= 1 << (pic[c + 1].icw3 & 7);
                   16539:                        }
                   16540:                }
                   16541:                irr &= (~pic[c].imr);
                   16542:                if(!irr) {
                   16543:                        break;
                   16544:                }
                   16545:                if(!(pic[c].ocw3 & 0x20)) {
                   16546:                        irr |= pic[c].isr;
                   16547:                }
                   16548:                int level = pic[c].prio;
                   16549:                UINT8 bit = 1 << level;
                   16550:                while(!(irr & bit)) {
                   16551:                        level = (level + 1) & 7;
                   16552:                        bit = 1 << level;
                   16553:                }
                   16554:                if((c + 1 < 2) && (pic[c].icw3 & bit)) {
                   16555:                        // check slave
                   16556:                        continue;
                   16557:                }
                   16558:                if(pic[c].isr & bit) {
                   16559:                        break;
                   16560:                }
                   16561:                // interrupt request
                   16562:                pic_req_chip = c;
                   16563:                pic_req_level = level;
                   16564:                pic_req_bit = bit;
1.1.1.3   root     16565:                i386_set_irq_line(INPUT_LINE_IRQ, HOLD_LINE);
1.1       root     16566:                return;
                   16567:        }
1.1.1.3   root     16568:        i386_set_irq_line(INPUT_LINE_IRQ, CLEAR_LINE);
1.1.1.2   root     16569: }
1.1       root     16570: 
1.1.1.25  root     16571: // pio
                   16572: 
                   16573: void pio_init()
                   16574: {
1.1.1.38  root     16575: //     bool conv_mode = (GetConsoleCP() == 932);
                   16576:        
1.1.1.26  root     16577:        memset(pio, 0, sizeof(pio));
1.1.1.37  root     16578:        
1.1.1.25  root     16579:        for(int c = 0; c < 2; c++) {
1.1.1.37  root     16580:                pio[c].stat = 0xdf;
1.1.1.25  root     16581:                pio[c].ctrl = 0x0c;
1.1.1.38  root     16582: //             pio[c].conv_mode = conv_mode;
1.1.1.25  root     16583:        }
                   16584: }
                   16585: 
1.1.1.37  root     16586: void pio_finish()
                   16587: {
                   16588:        pio_release();
                   16589: }
                   16590: 
                   16591: void pio_release()
                   16592: {
                   16593:        for(int c = 0; c < 2; c++) {
                   16594:                if(pio[c].fp != NULL) {
1.1.1.38  root     16595:                        if(pio[c].jis_mode) {
                   16596:                                fputc(0x1c, pio[c].fp);
                   16597:                                fputc(0x2e, pio[c].fp);
                   16598:                        }
1.1.1.37  root     16599:                        fclose(pio[c].fp);
                   16600:                        pio[c].fp = NULL;
                   16601:                }
                   16602:        }
                   16603: }
                   16604: 
1.1.1.25  root     16605: void pio_write(int c, UINT32 addr, UINT8 data)
                   16606: {
                   16607:        switch(addr & 3) {
                   16608:        case 0:
                   16609:                pio[c].data = data;
                   16610:                break;
                   16611:        case 2:
1.1.1.37  root     16612:                if((pio[c].ctrl & 0x01) && !(data & 0x01)) {
                   16613:                        // strobe H -> L
                   16614:                        if(pio[c].data == 0x0d && (data & 0x02)) {
                   16615:                                // auto feed
                   16616:                                printer_out(c, 0x0d);
                   16617:                                printer_out(c, 0x0a);
                   16618:                        } else {
                   16619:                                printer_out(c, pio[c].data);
                   16620:                        }
                   16621:                        pio[c].stat &= ~0x40; // set ack
                   16622:                }
1.1.1.25  root     16623:                pio[c].ctrl = data;
                   16624:                break;
                   16625:        }
                   16626: }
                   16627: 
                   16628: UINT8 pio_read(int c, UINT32 addr)
                   16629: {
                   16630:        switch(addr & 3) {
                   16631:        case 0:
1.1.1.37  root     16632:                if(pio[c].ctrl & 0x20) {
                   16633:                        // input mode
                   16634:                        return(0xff);
                   16635:                }
1.1.1.25  root     16636:                return(pio[c].data);
                   16637:        case 1:
1.1.1.37  root     16638:                {
                   16639:                        UINT8 stat = pio[c].stat;
                   16640:                        pio[c].stat |= 0x40; // clear ack
                   16641:                        return(stat);
                   16642:                }
1.1.1.25  root     16643:        case 2:
                   16644:                return(pio[c].ctrl);
                   16645:        }
                   16646:        return(0xff);
                   16647: }
                   16648: 
1.1.1.37  root     16649: void printer_out(int c, UINT8 data)
                   16650: {
                   16651:        SYSTEMTIME time;
1.1.1.38  root     16652:        bool jis_mode = false;
1.1.1.37  root     16653:        
                   16654:        GetLocalTime(&time);
                   16655:        
                   16656:        if(pio[c].fp != NULL) {
                   16657:                // if at least 1000ms passed from last written, close the current file
                   16658:                FILETIME ftime1;
                   16659:                FILETIME ftime2;
                   16660:                SystemTimeToFileTime(&pio[c].time, &ftime1);
                   16661:                SystemTimeToFileTime(&time, &ftime2);
                   16662:                INT64 *time1 = (INT64 *)&ftime1;
                   16663:                INT64 *time2 = (INT64 *)&ftime2;
                   16664:                INT64 msec = (*time2 - *time1) / 10000;
                   16665:                
                   16666:                if(msec >= 1000) {
1.1.1.38  root     16667:                        if(pio[c].jis_mode) {
                   16668:                                fputc(0x1c, pio[c].fp);
                   16669:                                fputc(0x2e, pio[c].fp);
                   16670:                                jis_mode = true;
                   16671:                        }
1.1.1.37  root     16672:                        fclose(pio[c].fp);
                   16673:                        pio[c].fp = NULL;
                   16674:                }
                   16675:        }
                   16676:        if(pio[c].fp == NULL) {
                   16677:                // create a new file in the temp folder
                   16678:                char file_name[MAX_PATH];
                   16679:                
                   16680:                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);
                   16681:                if(GetTempPath(MAX_PATH, pio[c].path)) {
                   16682:                        strcat(pio[c].path, file_name);
                   16683:                } else {
                   16684:                        strcpy(pio[c].path, file_name);
                   16685:                }
1.1.1.38  root     16686:                pio[c].fp = fopen(pio[c].path, "w+b");
1.1.1.37  root     16687:        }
                   16688:        if(pio[c].fp != NULL) {
1.1.1.38  root     16689:                if(jis_mode) {
                   16690:                        fputc(0x1c, pio[c].fp);
                   16691:                        fputc(0x26, pio[c].fp);
                   16692:                }
1.1.1.37  root     16693:                fputc(data, pio[c].fp);
1.1.1.38  root     16694:                
                   16695:                // reopen file if 1ch 26h 1ch 2eh (kanji-on  kanji-off) are written at the top
                   16696:                if(data == 0x2e && ftell(pio[c].fp) == 4) {
                   16697:                        UINT8 buffer[4];
                   16698:                        fseek(pio[c].fp, 0, SEEK_SET);
                   16699:                        fread(buffer, 4, 1, pio[c].fp);
                   16700:                        if(buffer[0] == 0x1c && buffer[1] == 0x26 && buffer[2] == 0x1c/* && buffer[3] == 0x2e*/) {
                   16701:                                fclose(pio[c].fp);
                   16702:                                pio[c].fp = fopen(pio[c].path, "w+b");
                   16703:                        }
                   16704:                }
1.1.1.37  root     16705:                pio[c].time = time;
                   16706:        }
                   16707: }
                   16708: 
1.1       root     16709: // pit
                   16710: 
1.1.1.22  root     16711: #define PIT_FREQ 1193182ULL
1.1       root     16712: #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)
                   16713: 
                   16714: void pit_init()
                   16715: {
1.1.1.8   root     16716:        memset(pit, 0, sizeof(pit));
1.1       root     16717:        for(int ch = 0; ch < 3; ch++) {
                   16718:                pit[ch].count = 0x10000;
                   16719:                pit[ch].ctrl_reg = 0x34;
                   16720:                pit[ch].mode = 3;
                   16721:        }
                   16722:        
                   16723:        // from bochs bios
                   16724:        pit_write(3, 0x34);
                   16725:        pit_write(0, 0x00);
                   16726:        pit_write(0, 0x00);
                   16727: }
                   16728: 
                   16729: void pit_write(int ch, UINT8 val)
                   16730: {
1.1.1.8   root     16731: #ifndef PIT_ALWAYS_RUNNING
1.1       root     16732:        if(!pit_active) {
                   16733:                pit_active = 1;
                   16734:                pit_init();
                   16735:        }
1.1.1.8   root     16736: #endif
1.1       root     16737:        switch(ch) {
                   16738:        case 0:
                   16739:        case 1:
                   16740:        case 2:
                   16741:                // write count register
                   16742:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   16743:                        if(pit[ch].ctrl_reg & 0x10) {
                   16744:                                pit[ch].low_write = 1;
                   16745:                        }
                   16746:                        if(pit[ch].ctrl_reg & 0x20) {
                   16747:                                pit[ch].high_write = 1;
                   16748:                        }
                   16749:                }
                   16750:                if(pit[ch].low_write) {
                   16751:                        pit[ch].count_reg = val;
                   16752:                        pit[ch].low_write = 0;
                   16753:                } else if(pit[ch].high_write) {
                   16754:                        if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   16755:                                pit[ch].count_reg = val << 8;
                   16756:                        } else {
                   16757:                                pit[ch].count_reg |= val << 8;
                   16758:                        }
                   16759:                        pit[ch].high_write = 0;
                   16760:                }
                   16761:                // start count
1.1.1.8   root     16762:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   16763:                        if(pit[ch].mode == 0 || pit[ch].mode == 4 || pit[ch].prev_time == 0) {
                   16764:                                pit[ch].count = PIT_COUNT_VALUE(ch);
                   16765:                                pit[ch].prev_time = timeGetTime();
                   16766:                                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     16767:                        }
                   16768:                }
                   16769:                break;
                   16770:        case 3: // ctrl reg
                   16771:                if((val & 0xc0) == 0xc0) {
                   16772:                        // i8254 read-back command
                   16773:                        for(ch = 0; ch < 3; ch++) {
                   16774:                                if(!(val & 0x10) && !pit[ch].status_latched) {
                   16775:                                        pit[ch].status = pit[ch].ctrl_reg & 0x3f;
                   16776:                                        pit[ch].status_latched = 1;
                   16777:                                }
                   16778:                                if(!(val & 0x20) && !pit[ch].count_latched) {
                   16779:                                        pit_latch_count(ch);
                   16780:                                }
                   16781:                        }
                   16782:                        break;
                   16783:                }
                   16784:                ch = (val >> 6) & 3;
                   16785:                if(val & 0x30) {
1.1.1.35  root     16786:                        static const int modes[8] = {0, 1, 2, 3, 4, 5, 2, 3};
1.1       root     16787:                        pit[ch].mode = modes[(val >> 1) & 7];
                   16788:                        pit[ch].count_latched = 0;
                   16789:                        pit[ch].low_read = pit[ch].high_read = 0;
                   16790:                        pit[ch].low_write = pit[ch].high_write = 0;
                   16791:                        pit[ch].ctrl_reg = val;
                   16792:                        // stop count
1.1.1.8   root     16793:                        pit[ch].prev_time = pit[ch].expired_time = 0;
1.1       root     16794:                        pit[ch].count_reg = 0;
                   16795:                } else if(!pit[ch].count_latched) {
                   16796:                        pit_latch_count(ch);
                   16797:                }
                   16798:                break;
                   16799:        }
                   16800: }
                   16801: 
                   16802: UINT8 pit_read(int ch)
                   16803: {
1.1.1.8   root     16804: #ifndef PIT_ALWAYS_RUNNING
1.1       root     16805:        if(!pit_active) {
                   16806:                pit_active = 1;
                   16807:                pit_init();
                   16808:        }
1.1.1.8   root     16809: #endif
1.1       root     16810:        switch(ch) {
                   16811:        case 0:
                   16812:        case 1:
                   16813:        case 2:
                   16814:                if(pit[ch].status_latched) {
                   16815:                        pit[ch].status_latched = 0;
                   16816:                        return(pit[ch].status);
                   16817:                }
                   16818:                // if not latched, through current count
                   16819:                if(!pit[ch].count_latched) {
                   16820:                        if(!pit[ch].low_read && !pit[ch].high_read) {
                   16821:                                pit_latch_count(ch);
                   16822:                        }
                   16823:                }
                   16824:                // return latched count
                   16825:                if(pit[ch].low_read) {
                   16826:                        pit[ch].low_read = 0;
                   16827:                        if(!pit[ch].high_read) {
                   16828:                                pit[ch].count_latched = 0;
                   16829:                        }
                   16830:                        return(pit[ch].latch & 0xff);
                   16831:                } else if(pit[ch].high_read) {
                   16832:                        pit[ch].high_read = 0;
                   16833:                        pit[ch].count_latched = 0;
                   16834:                        return((pit[ch].latch >> 8) & 0xff);
                   16835:                }
                   16836:        }
                   16837:        return(0xff);
                   16838: }
                   16839: 
1.1.1.8   root     16840: int pit_run(int ch, UINT32 cur_time)
1.1       root     16841: {
1.1.1.8   root     16842:        if(pit[ch].expired_time != 0 && cur_time >= pit[ch].expired_time) {
1.1       root     16843:                pit[ch].count = PIT_COUNT_VALUE(ch);
1.1.1.8   root     16844:                pit[ch].prev_time = pit[ch].expired_time;
                   16845:                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
                   16846:                if(cur_time >= pit[ch].expired_time) {
                   16847:                        pit[ch].prev_time = cur_time;
                   16848:                        pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     16849:                }
1.1.1.8   root     16850:                return(1);
1.1       root     16851:        }
1.1.1.8   root     16852:        return(0);
1.1       root     16853: }
                   16854: 
                   16855: void pit_latch_count(int ch)
                   16856: {
1.1.1.8   root     16857:        if(pit[ch].expired_time != 0) {
1.1.1.26  root     16858:                UINT32 cur_time = timeGetTime();
1.1.1.8   root     16859:                pit_run(ch, cur_time);
                   16860:                UINT32 tmp = (pit[ch].count * (pit[ch].expired_time - cur_time)) / (pit[ch].expired_time - pit[ch].prev_time);
1.1.1.26  root     16861:                UINT16 latch = (tmp != 0) ? (UINT16)tmp : 1;
                   16862:                
                   16863:                if(pit[ch].prev_latch == latch && pit[ch].expired_time > cur_time) {
                   16864:                        // decrement counter in 1msec period
                   16865:                        if(pit[ch].next_latch == 0) {
                   16866:                                tmp = (pit[ch].count * (pit[ch].expired_time - cur_time - 1)) / (pit[ch].expired_time - pit[ch].prev_time);
                   16867:                                pit[ch].next_latch = (tmp != 0) ? (UINT16)tmp : 1;
                   16868:                        }
                   16869:                        if(pit[ch].latch > pit[ch].next_latch) {
                   16870:                                pit[ch].latch--;
                   16871:                        }
                   16872:                } else {
                   16873:                        pit[ch].prev_latch = pit[ch].latch = latch;
                   16874:                        pit[ch].next_latch = 0;
                   16875:                }
1.1.1.8   root     16876:        } else {
                   16877:                pit[ch].latch = (UINT16)pit[ch].count;
1.1.1.26  root     16878:                pit[ch].prev_latch = pit[ch].next_latch = 0;
1.1       root     16879:        }
                   16880:        pit[ch].count_latched = 1;
                   16881:        if((pit[ch].ctrl_reg & 0x30) == 0x10) {
                   16882:                // lower byte
                   16883:                pit[ch].low_read = 1;
                   16884:                pit[ch].high_read = 0;
                   16885:        } else if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   16886:                // upper byte
                   16887:                pit[ch].low_read = 0;
                   16888:                pit[ch].high_read = 1;
                   16889:        } else {
                   16890:                // lower -> upper
1.1.1.14  root     16891:                pit[ch].low_read = pit[ch].high_read = 1;
1.1       root     16892:        }
                   16893: }
                   16894: 
1.1.1.8   root     16895: int pit_get_expired_time(int ch)
1.1       root     16896: {
1.1.1.22  root     16897:        pit[ch].accum += 1024ULL * 1000ULL * (UINT64)pit[ch].count / PIT_FREQ;
                   16898:        UINT64 val = pit[ch].accum >> 10;
                   16899:        pit[ch].accum -= val << 10;
                   16900:        return((val != 0) ? val : 1);
1.1.1.8   root     16901: }
                   16902: 
1.1.1.25  root     16903: // sio
                   16904: 
                   16905: void sio_init()
                   16906: {
1.1.1.26  root     16907:        memset(sio, 0, sizeof(sio));
                   16908:        memset(sio_mt, 0, sizeof(sio_mt));
                   16909:        
1.1.1.29  root     16910:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     16911:                sio[c].send_buffer = new FIFO(SIO_BUFFER_SIZE);
                   16912:                sio[c].recv_buffer = new FIFO(SIO_BUFFER_SIZE);
                   16913:                
                   16914:                sio[c].divisor.w = 12;          // 115200Hz / 9600Baud
                   16915:                sio[c].line_ctrl = 0x03;        // 8bit, stop 1bit, non parity
1.1.1.26  root     16916:                sio[c].modem_ctrl = 0x03;       // rts=on, dtr=on
                   16917:                sio[c].set_rts = sio[c].set_dtr = true;
1.1.1.25  root     16918:                sio[c].line_stat_buf = 0x60;    // send/recv buffers are empty
                   16919:                sio[c].irq_identify = 0x01;     // no pending irq
                   16920:                
                   16921:                InitializeCriticalSection(&sio_mt[c].csSendData);
                   16922:                InitializeCriticalSection(&sio_mt[c].csRecvData);
                   16923:                InitializeCriticalSection(&sio_mt[c].csLineCtrl);
                   16924:                InitializeCriticalSection(&sio_mt[c].csLineStat);
                   16925:                InitializeCriticalSection(&sio_mt[c].csModemCtrl);
                   16926:                InitializeCriticalSection(&sio_mt[c].csModemStat);
                   16927:                
1.1.1.26  root     16928:                if(sio_port_number[c] != 0) {
1.1.1.25  root     16929:                        sio[c].channel = c;
                   16930:                        sio_mt[c].hThread = CreateThread(NULL, 0, sio_thread, &sio[c], 0, NULL);
                   16931:                }
                   16932:        }
                   16933: }
                   16934: 
                   16935: void sio_finish()
                   16936: {
1.1.1.29  root     16937:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     16938:                if(sio_mt[c].hThread != NULL) {
                   16939:                        WaitForSingleObject(sio_mt[c].hThread, INFINITE);
                   16940:                        CloseHandle(sio_mt[c].hThread);
1.1.1.28  root     16941:                        sio_mt[c].hThread = NULL;
1.1.1.25  root     16942:                }
                   16943:                DeleteCriticalSection(&sio_mt[c].csSendData);
                   16944:                DeleteCriticalSection(&sio_mt[c].csRecvData);
                   16945:                DeleteCriticalSection(&sio_mt[c].csLineCtrl);
                   16946:                DeleteCriticalSection(&sio_mt[c].csLineStat);
                   16947:                DeleteCriticalSection(&sio_mt[c].csModemCtrl);
                   16948:                DeleteCriticalSection(&sio_mt[c].csModemStat);
1.1.1.28  root     16949:        }
                   16950:        sio_release();
                   16951: }
                   16952: 
                   16953: void sio_release()
                   16954: {
1.1.1.29  root     16955:        for(int c = 0; c < 4; c++) {
1.1.1.28  root     16956:                // sio_thread() may access the resources :-(
1.1.1.32  root     16957:                bool running = (sio_mt[c].hThread != NULL);
                   16958:                
                   16959:                if(running) {
                   16960:                        EnterCriticalSection(&sio_mt[c].csSendData);
                   16961:                }
                   16962:                if(sio[c].send_buffer != NULL) {
                   16963:                        sio[c].send_buffer->release();
                   16964:                        delete sio[c].send_buffer;
                   16965:                        sio[c].send_buffer = NULL;
                   16966:                }
                   16967:                if(running) {
                   16968:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   16969:                        EnterCriticalSection(&sio_mt[c].csRecvData);
                   16970:                }
                   16971:                if(sio[c].recv_buffer != NULL) {
                   16972:                        sio[c].recv_buffer->release();
                   16973:                        delete sio[c].recv_buffer;
                   16974:                        sio[c].recv_buffer = NULL;
                   16975:                }
                   16976:                if(running) {
                   16977:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
1.1.1.28  root     16978:                }
1.1.1.25  root     16979:        }
                   16980: }
                   16981: 
                   16982: void sio_write(int c, UINT32 addr, UINT8 data)
                   16983: {
                   16984:        switch(addr & 7) {
                   16985:        case 0:
                   16986:                if(sio[c].selector & 0x80) {
                   16987:                        if(sio[c].divisor.b.l != data) {
                   16988:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   16989:                                sio[c].divisor.b.l = data;
                   16990:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   16991:                        }
                   16992:                } else {
                   16993:                        EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     16994:                        if(sio[c].send_buffer != NULL) {
                   16995:                                sio[c].send_buffer->write(data);
                   16996:                        }
1.1.1.25  root     16997:                        // transmitter holding/shift registers are not empty
                   16998:                        sio[c].line_stat_buf &= ~0x60;
                   16999:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   17000:                        
                   17001:                        if(sio[c].irq_enable & 0x02) {
                   17002:                                sio_update_irq(c);
                   17003:                        }
                   17004:                }
                   17005:                break;
                   17006:        case 1:
                   17007:                if(sio[c].selector & 0x80) {
                   17008:                        if(sio[c].divisor.b.h != data) {
                   17009:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   17010:                                sio[c].divisor.b.h = data;
                   17011:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   17012:                        }
                   17013:                } else {
                   17014:                        if(sio[c].irq_enable != data) {
                   17015:                                sio[c].irq_enable = data;
                   17016:                                sio_update_irq(c);
                   17017:                        }
                   17018:                }
                   17019:                break;
                   17020:        case 3:
                   17021:                {
                   17022:                        UINT8 line_ctrl = data & 0x3f;
                   17023:                        bool set_brk = ((data & 0x40) != 0);
                   17024:                        
                   17025:                        if(sio[c].line_ctrl != line_ctrl) {
                   17026:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   17027:                                sio[c].line_ctrl = line_ctrl;
                   17028:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   17029:                        }
                   17030:                        if(sio[c].set_brk != set_brk) {
                   17031:                                EnterCriticalSection(&sio_mt[c].csModemCtrl);
                   17032:                                sio[c].set_brk = set_brk;
                   17033:                                LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   17034:                        }
                   17035:                }
                   17036:                sio[c].selector = data;
                   17037:                break;
                   17038:        case 4:
                   17039:                {
                   17040:                        bool set_dtr = ((data & 0x01) != 0);
                   17041:                        bool set_rts = ((data & 0x02) != 0);
                   17042:                        
                   17043:                        if(sio[c].set_dtr != set_dtr || sio[c].set_rts != set_rts) {
1.1.1.26  root     17044: //                             EnterCriticalSection(&sio_mt[c].csModemCtrl);
1.1.1.25  root     17045:                                sio[c].set_dtr = set_dtr;
                   17046:                                sio[c].set_rts = set_rts;
1.1.1.26  root     17047: //                             LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   17048:                                
                   17049:                                bool state_changed = false;
                   17050:                                
                   17051:                                EnterCriticalSection(&sio_mt[c].csModemStat);
                   17052:                                if(set_dtr) {
                   17053:                                        sio[c].modem_stat |= 0x20;      // dsr on
                   17054:                                } else {
                   17055:                                        sio[c].modem_stat &= ~0x20;     // dsr off
                   17056:                                }
                   17057:                                if(set_rts) {
                   17058:                                        sio[c].modem_stat |= 0x10;      // cts on
                   17059:                                } else {
                   17060:                                        sio[c].modem_stat &= ~0x10;     // cts off
                   17061:                                }
                   17062:                                if((sio[c].prev_modem_stat & 0x20) != (sio[c].modem_stat & 0x20)) {
                   17063:                                        if(!(sio[c].modem_stat & 0x02)) {
                   17064:                                                if(sio[c].irq_enable & 0x08) {
                   17065:                                                        state_changed = true;
                   17066:                                                }
                   17067:                                                sio[c].modem_stat |= 0x02;
                   17068:                                        }
                   17069:                                }
                   17070:                                if((sio[c].prev_modem_stat & 0x10) != (sio[c].modem_stat & 0x10)) {
                   17071:                                        if(!(sio[c].modem_stat & 0x01)) {
                   17072:                                                if(sio[c].irq_enable & 0x08) {
                   17073:                                                        state_changed = true;
                   17074:                                                }
                   17075:                                                sio[c].modem_stat |= 0x01;
                   17076:                                        }
                   17077:                                }
                   17078:                                LeaveCriticalSection(&sio_mt[c].csModemStat);
                   17079:                                
                   17080:                                if(state_changed) {
                   17081:                                        sio_update_irq(c);
                   17082:                                }
1.1.1.25  root     17083:                        }
                   17084:                }
                   17085:                sio[c].modem_ctrl = data;
                   17086:                break;
                   17087:        case 7:
                   17088:                sio[c].scratch = data;
                   17089:                break;
                   17090:        }
                   17091: }
                   17092: 
                   17093: UINT8 sio_read(int c, UINT32 addr)
                   17094: {
                   17095:        switch(addr & 7) {
                   17096:        case 0:
                   17097:                if(sio[c].selector & 0x80) {
                   17098:                        return(sio[c].divisor.b.l);
                   17099:                } else {
                   17100:                        EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     17101:                        UINT8 data = 0;
                   17102:                        if(sio[c].recv_buffer != NULL) {
                   17103:                                data = sio[c].recv_buffer->read();
                   17104:                        }
1.1.1.25  root     17105:                        // data is not ready
                   17106:                        sio[c].line_stat_buf &= ~0x01;
                   17107:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
                   17108:                        
                   17109:                        if(sio[c].irq_enable & 0x01) {
                   17110:                                sio_update_irq(c);
                   17111:                        }
                   17112:                        return(data);
                   17113:                }
                   17114:        case 1:
                   17115:                if(sio[c].selector & 0x80) {
                   17116:                        return(sio[c].divisor.b.h);
                   17117:                } else {
                   17118:                        return(sio[c].irq_enable);
                   17119:                }
                   17120:        case 2:
                   17121:                return(sio[c].irq_identify);
                   17122:        case 3:
                   17123:                return(sio[c].selector);
                   17124:        case 4:
                   17125:                return(sio[c].modem_ctrl);
                   17126:        case 5:
                   17127:                {
                   17128:                        EnterCriticalSection(&sio_mt[c].csLineStat);
                   17129:                        UINT8 val = sio[c].line_stat_err | sio[c].line_stat_buf;
                   17130:                        sio[c].line_stat_err = 0x00;
                   17131:                        LeaveCriticalSection(&sio_mt[c].csLineStat);
                   17132:                        
                   17133:                        bool state_changed = false;
                   17134:                        
                   17135:                        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   17136:                                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     17137:                                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     17138:                                        // transmitter holding register will be empty first
                   17139:                                        if(sio[c].irq_enable & 0x02) {
                   17140:                                                state_changed = true;
                   17141:                                        }
                   17142:                                        sio[c].line_stat_buf |= 0x20;
                   17143:                                }
                   17144:                                LeaveCriticalSection(&sio_mt[c].csSendData);
                   17145:                        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   17146:                                // transmitter shift register will be empty later
                   17147:                                sio[c].line_stat_buf |= 0x40;
                   17148:                        }
                   17149:                        if(!(sio[c].line_stat_buf & 0x01)) {
                   17150:                                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     17151:                                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     17152:                                        // data is ready
                   17153:                                        if(sio[c].irq_enable & 0x01) {
                   17154:                                                state_changed = true;
                   17155:                                        }
                   17156:                                        sio[c].line_stat_buf |= 0x01;
                   17157:                                }
                   17158:                                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   17159:                        }
                   17160:                        if(state_changed) {
                   17161:                                sio_update_irq(c);
                   17162:                        }
                   17163:                        return(val);
                   17164:                }
                   17165:        case 6:
                   17166:                {
                   17167:                        EnterCriticalSection(&sio_mt[c].csModemStat);
                   17168:                        UINT8 val = sio[c].modem_stat;
                   17169:                        sio[c].modem_stat &= 0xf0;
                   17170:                        sio[c].prev_modem_stat = sio[c].modem_stat;
                   17171:                        LeaveCriticalSection(&sio_mt[c].csModemStat);
                   17172:                        
                   17173:                        if(sio[c].modem_ctrl & 0x10) {
                   17174:                                // loop-back
                   17175:                                val &= 0x0f;
                   17176:                                val |= (sio[c].modem_ctrl & 0x0c) << 4;
                   17177:                                val |= (sio[c].modem_ctrl & 0x01) << 5;
                   17178:                                val |= (sio[c].modem_ctrl & 0x02) << 3;
                   17179:                        }
                   17180:                        return(val);
                   17181:                }
                   17182:        case 7:
                   17183:                return(sio[c].scratch);
                   17184:        }
                   17185:        return(0xff);
                   17186: }
                   17187: 
                   17188: void sio_update(int c)
                   17189: {
                   17190:        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   17191:                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     17192:                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     17193:                        // transmitter holding/shift registers will be empty
                   17194:                        sio[c].line_stat_buf |= 0x60;
                   17195:                }
                   17196:                LeaveCriticalSection(&sio_mt[c].csSendData);
                   17197:        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   17198:                // transmitter shift register will be empty
                   17199:                sio[c].line_stat_buf |= 0x40;
                   17200:        }
                   17201:        if(!(sio[c].line_stat_buf & 0x01)) {
                   17202:                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     17203:                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     17204:                        // data is ready
                   17205:                        sio[c].line_stat_buf |= 0x01;
                   17206:                }
                   17207:                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   17208:        }
                   17209:        sio_update_irq(c);
                   17210: }
                   17211: 
                   17212: void sio_update_irq(int c)
                   17213: {
                   17214:        int level = -1;
                   17215:        
                   17216:        if(sio[c].irq_enable & 0x08) {
                   17217:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   17218:                if((sio[c].modem_stat & 0x0f) != 0) {
                   17219:                        level = 0;
                   17220:                }
                   17221:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   17222:        }
                   17223:        if(sio[c].irq_enable & 0x02) {
                   17224:                if(sio[c].line_stat_buf & 0x20) {
                   17225:                        level = 1;
                   17226:                }
                   17227:        }
                   17228:        if(sio[c].irq_enable & 0x01) {
                   17229:                if(sio[c].line_stat_buf & 0x01) {
                   17230:                        level = 2;
                   17231:                }
                   17232:        }
                   17233:        if(sio[c].irq_enable & 0x04) {
                   17234:                EnterCriticalSection(&sio_mt[c].csLineStat);
                   17235:                if(sio[c].line_stat_err != 0) {
                   17236:                        level = 3;
                   17237:                }
                   17238:                LeaveCriticalSection(&sio_mt[c].csLineStat);
                   17239:        }
1.1.1.29  root     17240:        
                   17241:        // COM1 and COM3 shares IRQ4, COM2 and COM4 shares IRQ3
1.1.1.25  root     17242:        if(level != -1) {
                   17243:                sio[c].irq_identify = level << 1;
1.1.1.29  root     17244:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 1);
1.1.1.25  root     17245:        } else {
                   17246:                sio[c].irq_identify = 1;
1.1.1.29  root     17247:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 0);
1.1.1.25  root     17248:        }
                   17249: }
                   17250: 
                   17251: DWORD WINAPI sio_thread(void *lpx)
                   17252: {
                   17253:        volatile sio_t *p = (sio_t *)lpx;
                   17254:        sio_mt_t *q = &sio_mt[p->channel];
                   17255:        
                   17256:        char name[] = "COM1";
1.1.1.26  root     17257:        name[3] = '0' + sio_port_number[p->channel];
                   17258:        HANDLE hComm = NULL;
                   17259:        COMMPROP commProp;
                   17260:        DCB dcb;
                   17261:        DWORD dwSettableBaud = 0xffb; // 75, 110, 150, 300, 600, 1200, 1800, 2400, 4800, 7200, and 9600bps
                   17262:        BYTE bytBuffer[SIO_BUFFER_SIZE];
                   17263:        
                   17264:        if((hComm = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL)) != INVALID_HANDLE_VALUE) {
                   17265:                if(GetCommProperties(hComm, &commProp)) {
                   17266:                        dwSettableBaud = commProp.dwSettableBaud;
                   17267:                }
1.1.1.25  root     17268:                EscapeCommFunction(hComm, CLRBREAK);
1.1.1.26  root     17269: //             EscapeCommFunction(hComm, SETRTS);
                   17270: //             EscapeCommFunction(hComm, SETDTR);
1.1.1.25  root     17271:                
                   17272:                while(!m_halted) {
                   17273:                        // setup comm port
                   17274:                        bool comm_state_changed = false;
                   17275:                        
                   17276:                        EnterCriticalSection(&q->csLineCtrl);
                   17277:                        if((p->prev_divisor != p->divisor.w || p->prev_line_ctrl != p->line_ctrl) && p->divisor.w != 0) {
                   17278:                                p->prev_divisor = p->divisor.w;
                   17279:                                p->prev_line_ctrl = p->line_ctrl;
                   17280:                                comm_state_changed = true;
                   17281:                        }
                   17282:                        LeaveCriticalSection(&q->csLineCtrl);
                   17283:                        
                   17284:                        if(comm_state_changed) {
1.1.1.26  root     17285:                                if(GetCommState(hComm, &dcb)) {
                   17286: //                                     dcb.BaudRate = min(9600, 115200 / p->prev_divisor);
                   17287:                                        DWORD baud = 115200 / p->prev_divisor;
                   17288:                                        dcb.BaudRate = 9600; // default
                   17289:                                        
                   17290:                                        if((dwSettableBaud & BAUD_075  ) && baud >= 75   ) dcb.BaudRate = 75;
                   17291:                                        if((dwSettableBaud & BAUD_110  ) && baud >= 110  ) dcb.BaudRate = 110;
                   17292:                                        // 134.5bps is not supported ???
                   17293: //                                     if((dwSettableBaud & BAUD_134_5) && baud >= 134.5) dcb.BaudRate = 134.5;
                   17294:                                        if((dwSettableBaud & BAUD_150  ) && baud >= 150  ) dcb.BaudRate = 150;
                   17295:                                        if((dwSettableBaud & BAUD_300  ) && baud >= 300  ) dcb.BaudRate = 300;
                   17296:                                        if((dwSettableBaud & BAUD_600  ) && baud >= 600  ) dcb.BaudRate = 600;
                   17297:                                        if((dwSettableBaud & BAUD_1200 ) && baud >= 1200 ) dcb.BaudRate = 1200;
                   17298:                                        if((dwSettableBaud & BAUD_1800 ) && baud >= 1800 ) dcb.BaudRate = 1800;
                   17299:                                        if((dwSettableBaud & BAUD_2400 ) && baud >= 2400 ) dcb.BaudRate = 2400;
                   17300:                                        if((dwSettableBaud & BAUD_4800 ) && baud >= 4800 ) dcb.BaudRate = 4800;
                   17301:                                        if((dwSettableBaud & BAUD_7200 ) && baud >= 7200 ) dcb.BaudRate = 7200;
                   17302:                                        if((dwSettableBaud & BAUD_9600 ) && baud >= 9600 ) dcb.BaudRate = 9600;
                   17303: //                                     if((dwSettableBaud & BAUD_14400) && baud >= 14400) dcb.BaudRate = 14400;
                   17304: //                                     if((dwSettableBaud & BAUD_19200) && baud >= 19200) dcb.BaudRate = 19200;
                   17305: //                                     if((dwSettableBaud & BAUD_38400) && baud >= 38400) dcb.BaudRate = 38400;
                   17306:                                        
                   17307:                                        switch(p->prev_line_ctrl & 0x03) {
                   17308:                                        case 0x00: dcb.ByteSize = 5; break;
                   17309:                                        case 0x01: dcb.ByteSize = 6; break;
                   17310:                                        case 0x02: dcb.ByteSize = 7; break;
                   17311:                                        case 0x03: dcb.ByteSize = 8; break;
                   17312:                                        }
                   17313:                                        switch(p->prev_line_ctrl & 0x04) {
                   17314:                                        case 0x00: dcb.StopBits = ONESTOPBIT; break;
                   17315:                                        case 0x04: dcb.StopBits = (dcb.ByteSize == 5) ? ONE5STOPBITS : TWOSTOPBITS; break;
                   17316:                                        }
                   17317:                                        switch(p->prev_line_ctrl & 0x38) {
                   17318:                                        case 0x08: dcb.Parity = ODDPARITY;   break;
                   17319:                                        case 0x18: dcb.Parity = EVENPARITY;  break;
                   17320:                                        case 0x28: dcb.Parity = MARKPARITY;  break;
                   17321:                                        case 0x38: dcb.Parity = SPACEPARITY; break;
                   17322:                                        default:   dcb.Parity = NOPARITY;    break;
                   17323:                                        }
                   17324:                                        dcb.fBinary = TRUE;
                   17325:                                        dcb.fParity = (dcb.Parity != NOPARITY);
                   17326:                                        dcb.fOutxCtsFlow = dcb.fOutxDsrFlow = TRUE;
                   17327:                                        dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
                   17328:                                        dcb.fDsrSensitivity = FALSE;//TRUE;
                   17329:                                        dcb.fTXContinueOnXoff = TRUE;
                   17330:                                        dcb.fOutX = dcb.fInX = FALSE;
                   17331:                                        dcb.fErrorChar = FALSE;
                   17332:                                        dcb.fNull = FALSE;
                   17333:                                        dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
                   17334:                                        dcb.fAbortOnError = FALSE;
                   17335:                                        
                   17336:                                        SetCommState(hComm, &dcb);
1.1.1.25  root     17337:                                }
                   17338:                                
                   17339:                                // check again to apply all comm state changes
                   17340:                                Sleep(10);
                   17341:                                continue;
                   17342:                        }
                   17343:                        
                   17344:                        // set comm pins
                   17345:                        bool change_brk = false;
1.1.1.26  root     17346: //                     bool change_rts = false;
                   17347: //                     bool change_dtr = false;
1.1.1.25  root     17348:                        
                   17349:                        EnterCriticalSection(&q->csModemCtrl);
                   17350:                        if(p->prev_set_brk != p->set_brk) {
                   17351:                                p->prev_set_brk = p->set_brk;
                   17352:                                change_brk = true;
                   17353:                        }
1.1.1.26  root     17354: //                     if(p->prev_set_rts != p->set_rts) {
                   17355: //                             p->prev_set_rts = p->set_rts;
                   17356: //                             change_rts = true;
                   17357: //                     }
                   17358: //                     if(p->prev_set_dtr != p->set_dtr) {
                   17359: //                             p->prev_set_dtr = p->set_dtr;
                   17360: //                             change_dtr = true;
                   17361: //                     }
1.1.1.25  root     17362:                        LeaveCriticalSection(&q->csModemCtrl);
                   17363:                        
                   17364:                        if(change_brk) {
1.1.1.26  root     17365:                                static UINT32 clear_time = 0;
                   17366:                                if(p->prev_set_brk) {
                   17367:                                        EscapeCommFunction(hComm, SETBREAK);
                   17368:                                        clear_time = timeGetTime() + 200;
                   17369:                                } else {
                   17370:                                        // keep break for at least 200msec
                   17371:                                        UINT32 cur_time = timeGetTime();
                   17372:                                        if(clear_time > cur_time) {
                   17373:                                                Sleep(clear_time - cur_time);
                   17374:                                        }
                   17375:                                        EscapeCommFunction(hComm, CLRBREAK);
                   17376:                                }
1.1.1.25  root     17377:                        }
1.1.1.26  root     17378: //                     if(change_rts) {
                   17379: //                             if(p->prev_set_rts) {
                   17380: //                                     EscapeCommFunction(hComm, SETRTS);
                   17381: //                             } else {
                   17382: //                                     EscapeCommFunction(hComm, CLRRTS);
                   17383: //                             }
                   17384: //                     }
                   17385: //                     if(change_dtr) {
                   17386: //                             if(p->prev_set_dtr) {
                   17387: //                                     EscapeCommFunction(hComm, SETDTR);
                   17388: //                             } else {
                   17389: //                                     EscapeCommFunction(hComm, CLRDTR);
                   17390: //                             }
                   17391: //                     }
1.1.1.25  root     17392:                        
                   17393:                        // get comm pins
                   17394:                        DWORD dwModemStat = 0;
                   17395:                        
                   17396:                        if(GetCommModemStatus(hComm, &dwModemStat)) {
                   17397:                                EnterCriticalSection(&q->csModemStat);
                   17398:                                if(dwModemStat & MS_RLSD_ON) {
                   17399:                                        p->modem_stat |= 0x80;
                   17400:                                } else {
                   17401:                                        p->modem_stat &= ~0x80;
                   17402:                                }
                   17403:                                if(dwModemStat & MS_RING_ON) {
                   17404:                                        p->modem_stat |= 0x40;
                   17405:                                } else {
                   17406:                                        p->modem_stat &= ~0x40;
                   17407:                                }
1.1.1.26  root     17408: //                             if(dwModemStat & MS_DSR_ON) {
                   17409: //                                     p->modem_stat |= 0x20;
                   17410: //                             } else {
                   17411: //                                     p->modem_stat &= ~0x20;
                   17412: //                             }
                   17413: //                             if(dwModemStat & MS_CTS_ON) {
                   17414: //                                     p->modem_stat |= 0x10;
                   17415: //                             } else {
                   17416: //                                     p->modem_stat &= ~0x10;
                   17417: //                             }
1.1.1.25  root     17418:                                if((p->prev_modem_stat & 0x80) != (p->modem_stat & 0x80)) {
                   17419:                                        p->modem_stat |= 0x08;
                   17420:                                }
                   17421:                                if((p->prev_modem_stat & 0x40) && !(p->modem_stat & 0x40)) {
                   17422:                                        p->modem_stat |= 0x04;
                   17423:                                }
1.1.1.26  root     17424: //                             if((p->prev_modem_stat & 0x20) != (p->modem_stat & 0x20)) {
                   17425: //                                     p->modem_stat |= 0x02;
                   17426: //                             }
                   17427: //                             if((p->prev_modem_stat & 0x10) != (p->modem_stat & 0x10)) {
                   17428: //                                     p->modem_stat |= 0x01;
                   17429: //                             }
1.1.1.25  root     17430:                                LeaveCriticalSection(&q->csModemStat);
                   17431:                        }
                   17432:                        
                   17433:                        // send data
                   17434:                        DWORD dwSend = 0;
                   17435:                        
                   17436:                        EnterCriticalSection(&q->csSendData);
1.1.1.32  root     17437:                        while(p->send_buffer != NULL && !p->send_buffer->empty()) {
1.1.1.25  root     17438:                                bytBuffer[dwSend++] = p->send_buffer->read();
                   17439:                        }
                   17440:                        LeaveCriticalSection(&q->csSendData);
                   17441:                        
                   17442:                        if(dwSend != 0) {
                   17443:                                DWORD dwWritten = 0;
                   17444:                                WriteFile(hComm, bytBuffer, dwSend, &dwWritten, NULL);
                   17445:                        }
                   17446:                        
                   17447:                        // get line status and recv data
                   17448:                        DWORD dwLineStat = 0;
                   17449:                        COMSTAT comStat;
                   17450:                        
                   17451:                        if(ClearCommError(hComm, &dwLineStat, &comStat)) {
                   17452:                                EnterCriticalSection(&q->csLineStat);
                   17453:                                if(dwLineStat & CE_BREAK) {
                   17454:                                        p->line_stat_err |= 0x10;
                   17455:                                }
                   17456:                                if(dwLineStat & CE_FRAME) {
                   17457:                                        p->line_stat_err |= 0x08;
                   17458:                                }
                   17459:                                if(dwLineStat & CE_RXPARITY) {
                   17460:                                        p->line_stat_err |= 0x04;
                   17461:                                }
                   17462:                                if(dwLineStat & CE_OVERRUN) {
                   17463:                                        p->line_stat_err |= 0x02;
                   17464:                                }
                   17465:                                LeaveCriticalSection(&q->csLineStat);
                   17466:                                
                   17467:                                if(comStat.cbInQue != 0) {
                   17468:                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     17469:                                        DWORD dwRecv = 0;
                   17470:                                        if(p->recv_buffer != NULL) {
                   17471:                                                dwRecv = min(comStat.cbInQue, p->recv_buffer->remain());
                   17472:                                        }
1.1.1.25  root     17473:                                        LeaveCriticalSection(&q->csRecvData);
                   17474:                                        
                   17475:                                        if(dwRecv != 0) {
                   17476:                                                DWORD dwRead = 0;
                   17477:                                                if(ReadFile(hComm, bytBuffer, dwRecv, &dwRead, NULL) && dwRead != 0) {
                   17478:                                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     17479:                                                        if(p->recv_buffer != NULL) {
                   17480:                                                                for(int i = 0; i < dwRead; i++) {
                   17481:                                                                        p->recv_buffer->write(bytBuffer[i]);
                   17482:                                                                }
1.1.1.25  root     17483:                                                        }
                   17484:                                                        LeaveCriticalSection(&q->csRecvData);
                   17485:                                                }
                   17486:                                        }
                   17487:                                }
                   17488:                        }
                   17489:                        Sleep(10);
                   17490:                }
                   17491:                CloseHandle(hComm);
                   17492:        }
                   17493:        return 0;
                   17494: }
                   17495: 
1.1.1.8   root     17496: // cmos
                   17497: 
                   17498: void cmos_init()
                   17499: {
                   17500:        memset(cmos, 0, sizeof(cmos));
                   17501:        cmos_addr = 0;
1.1       root     17502:        
1.1.1.8   root     17503:        // from DOSBox
                   17504:        cmos_write(0x0a, 0x26);
                   17505:        cmos_write(0x0b, 0x02);
                   17506:        cmos_write(0x0d, 0x80);
1.1       root     17507: }
                   17508: 
1.1.1.8   root     17509: void cmos_write(int addr, UINT8 val)
1.1       root     17510: {
1.1.1.8   root     17511:        cmos[addr & 0x7f] = val;
                   17512: }
                   17513: 
                   17514: #define CMOS_GET_TIME() { \
                   17515:        UINT32 cur_sec = timeGetTime() / 1000 ; \
                   17516:        if(prev_sec != cur_sec) { \
                   17517:                GetLocalTime(&time); \
                   17518:                prev_sec = cur_sec; \
                   17519:        } \
1.1       root     17520: }
1.1.1.8   root     17521: #define CMOS_BCD(v) ((cmos[0x0b] & 4) ? (v) : to_bcd(v))
1.1       root     17522: 
1.1.1.8   root     17523: UINT8 cmos_read(int addr)
1.1       root     17524: {
1.1.1.8   root     17525:        static SYSTEMTIME time;
                   17526:        static UINT32 prev_sec = 0;
1.1       root     17527:        
1.1.1.8   root     17528:        switch(addr & 0x7f) {
                   17529:        case 0x00: CMOS_GET_TIME(); return(CMOS_BCD(time.wSecond));
                   17530:        case 0x02: CMOS_GET_TIME(); return(CMOS_BCD(time.wMinute));
                   17531:        case 0x04: CMOS_GET_TIME(); return(CMOS_BCD(time.wHour));
                   17532:        case 0x06: CMOS_GET_TIME(); return(time.wDayOfWeek + 1);
                   17533:        case 0x07: CMOS_GET_TIME(); return(CMOS_BCD(time.wDay));
                   17534:        case 0x08: CMOS_GET_TIME(); return(CMOS_BCD(time.wMonth));
                   17535:        case 0x09: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear));
                   17536: //     case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 2 ? 0x80 : 0));       // 2msec
                   17537:        case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 20 ? 0x80 : 0));      // precision of timeGetTime() may not be 1msec
                   17538:        case 0x15: return((MEMORY_END >> 10) & 0xff);
                   17539:        case 0x16: return((MEMORY_END >> 18) & 0xff);
                   17540:        case 0x17: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   17541:        case 0x18: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   17542:        case 0x30: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   17543:        case 0x31: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   17544:        case 0x32: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear / 100));
1.1       root     17545:        }
1.1.1.8   root     17546:        return(cmos[addr & 0x7f]);
1.1       root     17547: }
                   17548: 
1.1.1.7   root     17549: // kbd (a20)
                   17550: 
                   17551: void kbd_init()
                   17552: {
1.1.1.8   root     17553:        kbd_data = kbd_command = 0;
1.1.1.7   root     17554:        kbd_status = 0x18;
                   17555: }
                   17556: 
                   17557: UINT8 kbd_read_data()
                   17558: {
1.1.1.8   root     17559:        kbd_status &= ~1;
1.1.1.7   root     17560:        return(kbd_data);
                   17561: }
                   17562: 
                   17563: void kbd_write_data(UINT8 val)
                   17564: {
                   17565:        switch(kbd_command) {
                   17566:        case 0xd1:
                   17567:                i386_set_a20_line((val >> 1) & 1);
                   17568:                break;
                   17569:        }
                   17570:        kbd_command = 0;
1.1.1.8   root     17571:        kbd_status &= ~8;
1.1.1.7   root     17572: }
                   17573: 
                   17574: UINT8 kbd_read_status()
                   17575: {
                   17576:        return(kbd_status);
                   17577: }
                   17578: 
                   17579: void kbd_write_command(UINT8 val)
                   17580: {
                   17581:        switch(val) {
                   17582:        case 0xd0:
                   17583:                kbd_data = ((m_a20_mask >> 19) & 2) | 1;
1.1.1.8   root     17584:                kbd_status |= 1;
1.1.1.7   root     17585:                break;
                   17586:        case 0xdd:
                   17587:                i386_set_a20_line(0);
                   17588:                break;
                   17589:        case 0xdf:
                   17590:                i386_set_a20_line(1);
                   17591:                break;
1.1.1.26  root     17592:        case 0xf0: case 0xf1: case 0xf2: case 0xf3: case 0xf4: case 0xf5: case 0xf6: case 0xf7:
                   17593:        case 0xf8: case 0xf9: case 0xfa: case 0xfb: case 0xfc: case 0xfd: case 0xfe: case 0xff:
1.1.1.7   root     17594:                if(!(val & 1)) {
1.1.1.8   root     17595:                        if((cmos[0x0f] & 0x7f) == 5) {
1.1.1.7   root     17596:                                // reset pic
                   17597:                                pic_init();
                   17598:                                pic[0].irr = pic[1].irr = 0x00;
                   17599:                                pic[0].imr = pic[1].imr = 0xff;
                   17600:                        }
                   17601:                        CPU_RESET_CALL(CPU_MODEL);
1.1.1.40! root     17602:                        UINT16 address = *(UINT16 *)(mem + 0x467);
        !          17603:                        UINT16 selector = *(UINT16 *)(mem + 0x469);
        !          17604:                        i386_jmp_far(selector, address);
1.1.1.7   root     17605:                }
                   17606:                i386_set_a20_line((val >> 1) & 1);
                   17607:                break;
                   17608:        }
                   17609:        kbd_command = val;
1.1.1.8   root     17610:        kbd_status |= 8;
1.1.1.7   root     17611: }
                   17612: 
1.1.1.9   root     17613: // vga
                   17614: 
                   17615: UINT8 vga_read_status()
                   17616: {
                   17617:        // 60hz
                   17618:        static const int period[3] = {16, 17, 17};
                   17619:        static int index = 0;
                   17620:        UINT32 time = timeGetTime() % period[index];
                   17621:        
                   17622:        index = (index + 1) % 3;
1.1.1.14  root     17623:        return((time < 4 ? 0x08 : 0) | (time == 0 ? 0 : 0x01));
1.1.1.9   root     17624: }
                   17625: 
1.1       root     17626: // i/o bus
                   17627: 
1.1.1.29  root     17628: // this is ugly patch for SW1US.EXE, it sometimes mistakely read/write 01h-10h for serial I/O
                   17629: //#define SW1US_PATCH
                   17630: 
1.1.1.25  root     17631: UINT8 read_io_byte(offs_t addr)
1.1.1.33  root     17632: #ifdef USE_DEBUGGER
1.1.1.25  root     17633: {
1.1.1.33  root     17634:        if(now_debugging) {
                   17635:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   17636:                        if(in_break_point.table[i].status == 1) {
                   17637:                                if(addr == in_break_point.table[i].addr) {
                   17638:                                        in_break_point.hit = i + 1;
                   17639:                                        now_suspended = true;
                   17640:                                        break;
                   17641:                                }
                   17642:                        }
                   17643:                }
1.1.1.25  root     17644:        }
1.1.1.33  root     17645:        return(debugger_read_io_byte(addr));
1.1.1.25  root     17646: }
1.1.1.33  root     17647: UINT8 debugger_read_io_byte(offs_t addr)
1.1.1.25  root     17648: #endif
1.1       root     17649: {
1.1.1.33  root     17650:        UINT8 val = 0xff;
                   17651:        
1.1       root     17652:        switch(addr) {
1.1.1.29  root     17653: #ifdef SW1US_PATCH
                   17654:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   17655:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
1.1.1.33  root     17656:                val = sio_read(0, addr - 1);
                   17657:                break;
1.1.1.29  root     17658: #else
1.1.1.25  root     17659:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   17660:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
1.1.1.33  root     17661:                val = dma_read(0, addr);
                   17662:                break;
1.1.1.29  root     17663: #endif
1.1.1.25  root     17664:        case 0x20: case 0x21:
1.1.1.33  root     17665:                val = pic_read(0, addr);
                   17666:                break;
1.1.1.25  root     17667:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1.1.33  root     17668:                val = pit_read(addr & 0x03);
                   17669:                break;
1.1.1.7   root     17670:        case 0x60:
1.1.1.33  root     17671:                val = kbd_read_data();
                   17672:                break;
1.1.1.9   root     17673:        case 0x61:
1.1.1.33  root     17674:                val = system_port;
                   17675:                break;
1.1.1.7   root     17676:        case 0x64:
1.1.1.33  root     17677:                val = kbd_read_status();
                   17678:                break;
1.1       root     17679:        case 0x71:
1.1.1.33  root     17680:                val = cmos_read(cmos_addr);
                   17681:                break;
1.1.1.25  root     17682:        case 0x81:
1.1.1.33  root     17683:                val = dma_page_read(0, 2);
                   17684:                break;
1.1.1.25  root     17685:        case 0x82:
1.1.1.33  root     17686:                val = dma_page_read(0, 3);
                   17687:                break;
1.1.1.25  root     17688:        case 0x83:
1.1.1.33  root     17689:                val = dma_page_read(0, 1);
                   17690:                break;
1.1.1.25  root     17691:        case 0x87:
1.1.1.33  root     17692:                val = dma_page_read(0, 0);
                   17693:                break;
1.1.1.25  root     17694:        case 0x89:
1.1.1.33  root     17695:                val = dma_page_read(1, 2);
                   17696:                break;
1.1.1.25  root     17697:        case 0x8a:
1.1.1.33  root     17698:                val = dma_page_read(1, 3);
                   17699:                break;
1.1.1.25  root     17700:        case 0x8b:
1.1.1.33  root     17701:                val = dma_page_read(1, 1);
                   17702:                break;
1.1.1.25  root     17703:        case 0x8f:
1.1.1.33  root     17704:                val = dma_page_read(1, 0);
                   17705:                break;
1.1       root     17706:        case 0x92:
1.1.1.33  root     17707:                val = (m_a20_mask >> 19) & 2;
                   17708:                break;
1.1.1.25  root     17709:        case 0xa0: case 0xa1:
1.1.1.33  root     17710:                val = pic_read(1, addr);
                   17711:                break;
1.1.1.25  root     17712:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   17713:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.33  root     17714:                val = dma_read(1, (addr - 0xc0) >> 1);
                   17715:                break;
1.1.1.37  root     17716:        case 0x278: case 0x279: case 0x27a:
                   17717:                val = pio_read(1, addr);
                   17718:                break;
1.1.1.29  root     17719:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
1.1.1.33  root     17720:                val = sio_read(3, addr);
                   17721:                break;
1.1.1.25  root     17722:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
1.1.1.33  root     17723:                val = sio_read(1, addr);
                   17724:                break;
1.1.1.25  root     17725:        case 0x378: case 0x379: case 0x37a:
1.1.1.33  root     17726:                val = pio_read(0, addr);
                   17727:                break;
1.1.1.25  root     17728:        case 0x3ba: case 0x3da:
1.1.1.33  root     17729:                val = vga_read_status();
                   17730:                break;
1.1.1.37  root     17731:        case 0x3bc: case 0x3bd: case 0x3be:
                   17732:                val = pio_read(2, addr);
                   17733:                break;
1.1.1.29  root     17734:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
1.1.1.33  root     17735:                val = sio_read(2, addr);
                   17736:                break;
1.1.1.25  root     17737:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
1.1.1.33  root     17738:                val = sio_read(0, addr);
                   17739:                break;
1.1       root     17740:        default:
1.1.1.33  root     17741: //             fatalerror("unknown inb %4x\n", addr);
1.1       root     17742:                break;
                   17743:        }
1.1.1.33  root     17744: #ifdef ENABLE_DEBUG_IOPORT
                   17745:        if(fp_debug_log != NULL) {
                   17746:                fprintf(fp_debug_log, "inb %04X, %02X\n", addr, val);
                   17747:        }
                   17748: #endif
                   17749:        return(val);
1.1       root     17750: }
                   17751: 
                   17752: UINT16 read_io_word(offs_t addr)
                   17753: {
                   17754:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8));
                   17755: }
                   17756: 
1.1.1.33  root     17757: #ifdef USE_DEBUGGER
                   17758: UINT16 debugger_read_io_word(offs_t addr)
                   17759: {
                   17760:        return(debugger_read_io_byte(addr) | (debugger_read_io_byte(addr + 1) << 8));
                   17761: }
                   17762: #endif
                   17763: 
1.1       root     17764: UINT32 read_io_dword(offs_t addr)
                   17765: {
                   17766:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8) | (read_io_byte(addr + 2) << 16) | (read_io_byte(addr + 3) << 24));
                   17767: }
                   17768: 
1.1.1.33  root     17769: #ifdef USE_DEBUGGER
                   17770: UINT32 debugger_read_io_dword(offs_t addr)
                   17771: {
                   17772:        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));
                   17773: }
                   17774: #endif
                   17775: 
1.1       root     17776: void write_io_byte(offs_t addr, UINT8 val)
1.1.1.33  root     17777: #ifdef USE_DEBUGGER
                   17778: {
                   17779:        if(now_debugging) {
                   17780:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   17781:                        if(out_break_point.table[i].status == 1) {
                   17782:                                if(addr == out_break_point.table[i].addr) {
                   17783:                                        out_break_point.hit = i + 1;
                   17784:                                        now_suspended = true;
                   17785:                                        break;
                   17786:                                }
                   17787:                        }
                   17788:                }
                   17789:        }
                   17790:        debugger_write_io_byte(addr, val);
                   17791: }
                   17792: void debugger_write_io_byte(offs_t addr, UINT8 val)
                   17793: #endif
1.1       root     17794: {
1.1.1.25  root     17795: #ifdef ENABLE_DEBUG_IOPORT
1.1.1.33  root     17796:        if(fp_debug_log != NULL) {
                   17797:                fprintf(fp_debug_log, "outb %04X, %02X\n", addr, val);
1.1.1.25  root     17798:        }
                   17799: #endif
1.1       root     17800:        switch(addr) {
1.1.1.29  root     17801: #ifdef SW1US_PATCH
                   17802:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   17803:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
                   17804:                sio_write(0, addr - 1, val);
                   17805:                break;
                   17806: #else
1.1.1.25  root     17807:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   17808:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
                   17809:                dma_write(0, addr, val);
                   17810:                break;
1.1.1.29  root     17811: #endif
1.1.1.25  root     17812:        case 0x20: case 0x21:
1.1       root     17813:                pic_write(0, addr, val);
                   17814:                break;
1.1.1.25  root     17815:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1       root     17816:                pit_write(addr & 0x03, val);
                   17817:                break;
1.1.1.7   root     17818:        case 0x60:
                   17819:                kbd_write_data(val);
                   17820:                break;
1.1.1.9   root     17821:        case 0x61:
                   17822:                if((system_port & 3) != 3 && (val & 3) == 3) {
                   17823:                        // beep on
                   17824: //                     MessageBeep(-1);
                   17825:                } else if((system_port & 3) == 3 && (val & 3) != 3) {
                   17826:                        // beep off
                   17827:                }
                   17828:                system_port = val;
                   17829:                break;
1.1       root     17830:        case 0x64:
1.1.1.7   root     17831:                kbd_write_command(val);
1.1       root     17832:                break;
                   17833:        case 0x70:
                   17834:                cmos_addr = val;
                   17835:                break;
                   17836:        case 0x71:
1.1.1.8   root     17837:                cmos_write(cmos_addr, val);
1.1       root     17838:                break;
1.1.1.25  root     17839:        case 0x81:
                   17840:                dma_page_write(0, 2, val);
                   17841:        case 0x82:
                   17842:                dma_page_write(0, 3, val);
                   17843:        case 0x83:
                   17844:                dma_page_write(0, 1, val);
                   17845:        case 0x87:
                   17846:                dma_page_write(0, 0, val);
                   17847:        case 0x89:
                   17848:                dma_page_write(1, 2, val);
                   17849:        case 0x8a:
                   17850:                dma_page_write(1, 3, val);
                   17851:        case 0x8b:
                   17852:                dma_page_write(1, 1, val);
                   17853:        case 0x8f:
                   17854:                dma_page_write(1, 0, val);
1.1       root     17855:        case 0x92:
1.1.1.7   root     17856:                i386_set_a20_line((val >> 1) & 1);
1.1       root     17857:                break;
1.1.1.25  root     17858:        case 0xa0: case 0xa1:
1.1       root     17859:                pic_write(1, addr, val);
                   17860:                break;
1.1.1.25  root     17861:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   17862:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.26  root     17863:                dma_write(1, (addr - 0xc0) >> 1, val);
1.1.1.25  root     17864:                break;
1.1.1.35  root     17865: #ifdef USE_SERVICE_THREAD
                   17866:        case 0xf7:
                   17867:                // dummy i/o for BIOS/DOS service
1.1.1.36  root     17868:                if(in_service && cursor_moved) {
                   17869:                        // update cursor position before service is done
                   17870:                        pcbios_update_cursor_position();
                   17871:                        cursor_moved = false;
                   17872:                }
1.1.1.35  root     17873:                finish_service_loop();
                   17874:                break;
                   17875: #endif
1.1.1.37  root     17876:        case 0x278: case 0x279: case 0x27a:
                   17877:                pio_write(1, addr, val);
                   17878:                break;
1.1.1.29  root     17879:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
                   17880:                sio_write(3, addr, val);
                   17881:                break;
1.1.1.25  root     17882:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
                   17883:                sio_write(1, addr, val);
                   17884:                break;
                   17885:        case 0x378: case 0x379: case 0x37a:
                   17886:                pio_write(0, addr, val);
                   17887:                break;
1.1.1.37  root     17888:        case 0x3bc: case 0x3bd: case 0x3be:
                   17889:                pio_write(2, addr, val);
                   17890:                break;
1.1.1.29  root     17891:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
                   17892:                sio_write(2, addr, val);
                   17893:                break;
1.1.1.25  root     17894:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
                   17895:                sio_write(0, addr, val);
                   17896:                break;
1.1       root     17897:        default:
1.1.1.33  root     17898: //             fatalerror("unknown outb %4x,%2x\n", addr, val);
1.1       root     17899:                break;
                   17900:        }
                   17901: }
                   17902: 
                   17903: void write_io_word(offs_t addr, UINT16 val)
                   17904: {
                   17905:        write_io_byte(addr + 0, (val >> 0) & 0xff);
                   17906:        write_io_byte(addr + 1, (val >> 8) & 0xff);
                   17907: }
                   17908: 
1.1.1.33  root     17909: #ifdef USE_DEBUGGER
                   17910: void debugger_write_io_word(offs_t addr, UINT16 val)
                   17911: {
                   17912:        debugger_write_io_byte(addr + 0, (val >> 0) & 0xff);
                   17913:        debugger_write_io_byte(addr + 1, (val >> 8) & 0xff);
                   17914: }
                   17915: #endif
                   17916: 
1.1       root     17917: void write_io_dword(offs_t addr, UINT32 val)
                   17918: {
                   17919:        write_io_byte(addr + 0, (val >>  0) & 0xff);
                   17920:        write_io_byte(addr + 1, (val >>  8) & 0xff);
                   17921:        write_io_byte(addr + 2, (val >> 16) & 0xff);
                   17922:        write_io_byte(addr + 3, (val >> 24) & 0xff);
                   17923: }
1.1.1.33  root     17924: 
                   17925: #ifdef USE_DEBUGGER
                   17926: void debugger_write_io_dword(offs_t addr, UINT32 val)
                   17927: {
                   17928:        debugger_write_io_byte(addr + 0, (val >>  0) & 0xff);
                   17929:        debugger_write_io_byte(addr + 1, (val >>  8) & 0xff);
                   17930:        debugger_write_io_byte(addr + 2, (val >> 16) & 0xff);
                   17931:        debugger_write_io_byte(addr + 3, (val >> 24) & 0xff);
                   17932: }
                   17933: #endif

unix.superglobalmegacorp.com

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