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

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
                     37:                #define unimplemented_1ah fatalerror
                     38:                #define unimplemented_21h fatalerror
                     39:                #define unimplemented_2fh fatalerror
1.1.1.24  root       40:                #define unimplemented_33h fatalerror
1.1.1.22  root       41:                #define unimplemented_67h fatalerror
                     42:                #define unimplemented_xms fatalerror
                     43:        #endif
                     44: #endif
                     45: #ifndef unimplemented_10h
                     46:        #define unimplemented_10h nolog
                     47: #endif
1.1.1.25  root       48: #ifndef unimplemented_14h
                     49:        #define unimplemented_14h nolog
                     50: #endif
1.1.1.22  root       51: #ifndef unimplemented_15h
                     52:        #define unimplemented_15h nolog
                     53: #endif
                     54: #ifndef unimplemented_16h
                     55:        #define unimplemented_16h nolog
                     56: #endif
                     57: #ifndef unimplemented_1ah
                     58:        #define unimplemented_1ah nolog
                     59: #endif
                     60: #ifndef unimplemented_21h
                     61:        #define unimplemented_21h nolog
                     62: #endif
                     63: #ifndef unimplemented_2fh
                     64:        #define unimplemented_2fh nolog
                     65: #endif
1.1.1.24  root       66: #ifndef unimplemented_33h
                     67:        #define unimplemented_33h nolog
                     68: #endif
1.1.1.22  root       69: #ifndef unimplemented_67h
                     70:        #define unimplemented_67h nolog
                     71: #endif
                     72: #ifndef unimplemented_xms
                     73:        #define unimplemented_xms nolog
                     74: #endif
                     75: 
1.1.1.32  root       76: #define array_length(array) (sizeof(array) / sizeof(array[0]))
1.1.1.22  root       77: #define my_strchr(str, chr) (char *)_mbschr((unsigned char *)(str), (unsigned int)(chr))
                     78: #define my_strtok(tok, del) (char *)_mbstok((unsigned char *)(tok), (const unsigned char *)(del))
                     79: #define my_strupr(str) (char *)_mbsupr((unsigned char *)(str))
1.1       root       80: 
1.1.1.12  root       81: #if defined(__MINGW32__)
                     82: extern "C" int _CRT_glob = 0;
                     83: #endif
                     84: 
                     85: /*
                     86:        kludge for "more-standardized" C++
                     87: */
                     88: #if !defined(_MSC_VER)
                     89: inline int kludge_min(int a, int b) { return (a<b ? a:b); }
                     90: inline int kludge_max(int a, int b) { return (a>b ? a:b); }
                     91: #define min(a,b) kludge_min(a,b)
                     92: #define max(a,b) kludge_max(a,b)
1.1.1.14  root       93: #elif _MSC_VER >= 1400
                     94: void ignore_invalid_parameters(const wchar_t *, const wchar_t *, const wchar_t *, unsigned int, uintptr_t)
                     95: {
                     96: }
                     97: #endif
                     98: 
                     99: #define USE_THREAD
                    100: 
                    101: #ifdef USE_THREAD
                    102: static CRITICAL_SECTION vram_crit_sect;
                    103: #else
                    104: #define EnterCriticalSection(x)
                    105: #define LeaveCriticalSection(x)
                    106: #define vram_flush()
1.1.1.12  root      107: #endif
                    108: 
1.1.1.14  root      109: #define VIDEO_REGEN *(UINT16 *)(mem + 0x44c)
                    110: #define SCR_BUF(y,x) scr_buf[(y) * scr_buf_size.X + (x)]
                    111: 
                    112: void change_console_size(int width, int height);
                    113: void clear_scr_buffer(WORD attr);
                    114: 
                    115: static UINT32 vram_length_char = 0, vram_length_attr = 0;
                    116: static UINT32 vram_last_length_char = 0, vram_last_length_attr = 0;
                    117: static COORD vram_coord_char, vram_coord_attr;
                    118: 
1.1.1.28  root      119: char temp_file_path[MAX_PATH];
                    120: bool temp_file_created = false;
                    121: 
1.1.1.14  root      122: bool ignore_illegal_insn = false;
                    123: bool limit_max_memory = false;
                    124: bool no_windows = false;
                    125: bool stay_busy = false;
                    126: UINT32 iops = 0;
1.1.1.19  root      127: bool support_ems = false;
                    128: #ifdef SUPPORT_XMS
                    129: bool support_xms = false;
                    130: #endif
1.1.1.29  root      131: int sio_port_number[4] = {0, 0, 0, 0};
1.1.1.14  root      132: 
                    133: BOOL is_vista_or_later;
                    134: 
                    135: inline void maybe_idle()
                    136: {
                    137:        // if it appears to be in a tight loop, assume waiting for input
                    138:        // allow for one updated video character, for a spinning cursor
                    139:        if(!stay_busy && iops < 1000 && vram_length_char <= 1 && vram_length_attr <= 1) {
                    140:                Sleep(10);
                    141:        }
                    142:        iops = 0;
                    143: }
1.1.1.12  root      144: 
1.1       root      145: /* ----------------------------------------------------------------------------
1.1.1.3   root      146:        MAME i86/i386
1.1       root      147: ---------------------------------------------------------------------------- */
                    148: 
1.1.1.10  root      149: #ifndef __BIG_ENDIAN__
1.1       root      150: #define LSB_FIRST
1.1.1.10  root      151: #endif
1.1       root      152: 
                    153: #ifndef INLINE
                    154: #define INLINE inline
                    155: #endif
                    156: #define U64(v) UINT64(v)
                    157: 
                    158: //#define logerror(...) fprintf(stderr, __VA_ARGS__)
                    159: #define logerror(...)
                    160: //#define popmessage(...) fprintf(stderr, __VA_ARGS__)
                    161: #define popmessage(...)
                    162: 
                    163: /*****************************************************************************/
1.1.1.10  root      164: /* src/emu/devcpu.h */
                    165: 
                    166: // CPU interface functions
                    167: #define CPU_INIT_NAME(name)                    cpu_init_##name
                    168: #define CPU_INIT(name)                         void CPU_INIT_NAME(name)()
                    169: #define CPU_INIT_CALL(name)                    CPU_INIT_NAME(name)()
                    170: 
                    171: #define CPU_RESET_NAME(name)                   cpu_reset_##name
                    172: #define CPU_RESET(name)                                void CPU_RESET_NAME(name)()
                    173: #define CPU_RESET_CALL(name)                   CPU_RESET_NAME(name)()
                    174: 
                    175: #define CPU_EXECUTE_NAME(name)                 cpu_execute_##name
                    176: #define CPU_EXECUTE(name)                      void CPU_EXECUTE_NAME(name)()
                    177: #define CPU_EXECUTE_CALL(name)                 CPU_EXECUTE_NAME(name)()
                    178: 
                    179: #define CPU_TRANSLATE_NAME(name)               cpu_translate_##name
                    180: #define CPU_TRANSLATE(name)                    int CPU_TRANSLATE_NAME(name)(address_spacenum space, int intention, offs_t *address)
                    181: #define CPU_TRANSLATE_CALL(name)               CPU_TRANSLATE_NAME(name)(space, intention, address)
                    182: 
                    183: #define CPU_DISASSEMBLE_NAME(name)             cpu_disassemble_##name
                    184: #define CPU_DISASSEMBLE(name)                  int CPU_DISASSEMBLE_NAME(name)(char *buffer, offs_t eip, const UINT8 *oprom)
                    185: #define CPU_DISASSEMBLE_CALL(name)             CPU_DISASSEMBLE_NAME(name)(buffer, eip, oprom)
                    186: 
1.1.1.14  root      187: #define CPU_MODEL_STR(name)                    #name
                    188: #define CPU_MODEL_NAME(name)                   CPU_MODEL_STR(name)
                    189: 
1.1.1.10  root      190: /*****************************************************************************/
                    191: /* src/emu/didisasm.h */
                    192: 
                    193: // Disassembler constants
                    194: const UINT32 DASMFLAG_SUPPORTED     = 0x80000000;   // are disassembly flags supported?
                    195: const UINT32 DASMFLAG_STEP_OUT      = 0x40000000;   // this instruction should be the end of a step out sequence
                    196: const UINT32 DASMFLAG_STEP_OVER     = 0x20000000;   // this instruction should be stepped over by setting a breakpoint afterwards
                    197: const UINT32 DASMFLAG_OVERINSTMASK  = 0x18000000;   // number of extra instructions to skip when stepping over
                    198: const UINT32 DASMFLAG_OVERINSTSHIFT = 27;           // bits to shift after masking to get the value
                    199: const UINT32 DASMFLAG_LENGTHMASK    = 0x0000ffff;   // the low 16-bits contain the actual length
                    200: 
                    201: /*****************************************************************************/
1.1       root      202: /* src/emu/diexec.h */
                    203: 
                    204: // I/O line states
                    205: enum line_state
                    206: {
                    207:        CLEAR_LINE = 0,                         // clear (a fired or held) line
                    208:        ASSERT_LINE,                            // assert an interrupt immediately
                    209:        HOLD_LINE,                              // hold interrupt line until acknowledged
                    210:        PULSE_LINE                              // pulse interrupt line instantaneously (only for NMI, RESET)
                    211: };
                    212: 
                    213: // I/O line definitions
                    214: enum
                    215: {
                    216:        INPUT_LINE_IRQ = 0,
                    217:        INPUT_LINE_NMI
                    218: };
                    219: 
                    220: /*****************************************************************************/
1.1.1.10  root      221: /* src/emu/dimemory.h */
1.1       root      222: 
1.1.1.10  root      223: // Translation intentions
                    224: const int TRANSLATE_TYPE_MASK       = 0x03;     // read write or fetch
                    225: const int TRANSLATE_USER_MASK       = 0x04;     // user mode or fully privileged
                    226: const int TRANSLATE_DEBUG_MASK      = 0x08;     // debug mode (no side effects)
                    227: 
                    228: const int TRANSLATE_READ            = 0;        // translate for read
                    229: const int TRANSLATE_WRITE           = 1;        // translate for write
                    230: const int TRANSLATE_FETCH           = 2;        // translate for instruction fetch
                    231: const int TRANSLATE_READ_USER       = (TRANSLATE_READ | TRANSLATE_USER_MASK);
                    232: const int TRANSLATE_WRITE_USER      = (TRANSLATE_WRITE | TRANSLATE_USER_MASK);
                    233: const int TRANSLATE_FETCH_USER      = (TRANSLATE_FETCH | TRANSLATE_USER_MASK);
                    234: const int TRANSLATE_READ_DEBUG      = (TRANSLATE_READ | TRANSLATE_DEBUG_MASK);
                    235: const int TRANSLATE_WRITE_DEBUG     = (TRANSLATE_WRITE | TRANSLATE_DEBUG_MASK);
                    236: const int TRANSLATE_FETCH_DEBUG     = (TRANSLATE_FETCH | TRANSLATE_DEBUG_MASK);
1.1       root      237: 
1.1.1.10  root      238: /*****************************************************************************/
                    239: /* src/emu/emucore.h */
1.1       root      240: 
1.1.1.10  root      241: // constants for expression endianness
                    242: enum endianness_t
                    243: {
                    244:        ENDIANNESS_LITTLE,
                    245:        ENDIANNESS_BIG
                    246: };
1.1       root      247: 
1.1.1.10  root      248: // declare native endianness to be one or the other
                    249: #ifdef LSB_FIRST
                    250: const endianness_t ENDIANNESS_NATIVE = ENDIANNESS_LITTLE;
                    251: #else
                    252: const endianness_t ENDIANNESS_NATIVE = ENDIANNESS_BIG;
                    253: #endif
                    254: 
                    255: // endian-based value: first value is if 'endian' is little-endian, second is if 'endian' is big-endian
                    256: #define ENDIAN_VALUE_LE_BE(endian,leval,beval) (((endian) == ENDIANNESS_LITTLE) ? (leval) : (beval))
                    257: 
                    258: // endian-based value: first value is if native endianness is little-endian, second is if native is big-endian
                    259: #define NATIVE_ENDIAN_VALUE_LE_BE(leval,beval) ENDIAN_VALUE_LE_BE(ENDIANNESS_NATIVE, leval, beval)
                    260: 
                    261: // endian-based value: first value is if 'endian' matches native, second is if 'endian' doesn't match native
                    262: #define ENDIAN_VALUE_NE_NNE(endian,leval,beval)        (((endian) == ENDIANNESS_NATIVE) ? (neval) : (nneval))
1.1       root      263: 
                    264: /*****************************************************************************/
                    265: /* src/emu/memory.h */
                    266: 
1.1.1.10  root      267: // address spaces
                    268: enum address_spacenum
                    269: {
                    270:        AS_0,                           // first address space
                    271:        AS_1,                           // second address space
                    272:        AS_2,                           // third address space
                    273:        AS_3,                           // fourth address space
                    274:        ADDRESS_SPACES,                 // maximum number of address spaces
                    275: 
                    276:        // alternate address space names for common use
                    277:        AS_PROGRAM = AS_0,              // program address space
                    278:        AS_DATA = AS_1,                 // data address space
                    279:        AS_IO = AS_2                    // I/O address space
                    280: };
                    281: 
1.1       root      282: // offsets and addresses are 32-bit (for now...)
1.1.1.30  root      283: //typedef UINT32       offs_t;
1.1       root      284: 
                    285: // read accessors
                    286: UINT8 read_byte(offs_t byteaddress)
1.1.1.33  root      287: #ifdef USE_DEBUGGER
                    288: {
                    289:        if(now_debugging) {
                    290:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    291:                        if(rd_break_point.table[i].status == 1) {
                    292:                                if(byteaddress == rd_break_point.table[i].addr) {
                    293:                                        rd_break_point.hit = i + 1;
                    294:                                        now_suspended = true;
                    295:                                        break;
                    296:                                }
                    297:                        }
                    298:                }
                    299:        }
                    300:        return(debugger_read_byte(byteaddress));
                    301: }
                    302: UINT8 debugger_read_byte(offs_t byteaddress)
                    303: #endif
1.1       root      304: {
1.1.1.4   root      305: #if defined(HAS_I386)
1.1       root      306:        if(byteaddress < MAX_MEM) {
                    307:                return mem[byteaddress];
1.1.1.3   root      308: //     } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
                    309: //             return read_byte(byteaddress & 0xfffff);
1.1       root      310:        }
                    311:        return 0;
1.1.1.4   root      312: #else
                    313:        return mem[byteaddress];
                    314: #endif
1.1       root      315: }
                    316: 
                    317: UINT16 read_word(offs_t byteaddress)
1.1.1.33  root      318: #ifdef USE_DEBUGGER
                    319: {
                    320:        if(now_debugging) {
                    321:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    322:                        if(rd_break_point.table[i].status == 1) {
                    323:                                if(byteaddress >= rd_break_point.table[i].addr && byteaddress < rd_break_point.table[i].addr + 2) {
                    324:                                        rd_break_point.hit = i + 1;
                    325:                                        now_suspended = true;
                    326:                                        break;
                    327:                                }
                    328:                        }
                    329:                }
                    330:        }
                    331:        return(debugger_read_word(byteaddress));
                    332: }
                    333: UINT16 debugger_read_word(offs_t byteaddress)
                    334: #endif
1.1       root      335: {
1.1.1.14  root      336:        if(byteaddress == 0x41c) {
                    337:                // pointer to first free slot in keyboard buffer
                    338:                // XXX: the buffer itself doesn't actually exist in DOS memory
1.1.1.32  root      339:                if(key_buf_char != NULL) {
                    340:                        if(key_buf_char->count() == 0) {
                    341:                                maybe_idle();
                    342:                        }
                    343:                        return (UINT16)key_buf_char->count();
1.1.1.14  root      344:                }
1.1.1.32  root      345:                return 0;
1.1.1.14  root      346:        }
1.1.1.4   root      347: #if defined(HAS_I386)
1.1       root      348:        if(byteaddress < MAX_MEM - 1) {
                    349:                return *(UINT16 *)(mem + byteaddress);
1.1.1.3   root      350: //     } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
                    351: //             return read_word(byteaddress & 0xfffff);
1.1       root      352:        }
                    353:        return 0;
1.1.1.4   root      354: #else
                    355:        return *(UINT16 *)(mem + byteaddress);
                    356: #endif
1.1       root      357: }
                    358: 
                    359: UINT32 read_dword(offs_t byteaddress)
1.1.1.33  root      360: #ifdef USE_DEBUGGER
                    361: {
                    362:        if(now_debugging) {
                    363:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    364:                        if(rd_break_point.table[i].status == 1) {
                    365:                                if(byteaddress >= rd_break_point.table[i].addr && byteaddress < rd_break_point.table[i].addr + 4) {
                    366:                                        rd_break_point.hit = i + 1;
                    367:                                        now_suspended = true;
                    368:                                        break;
                    369:                                }
                    370:                        }
                    371:                }
                    372:        }
                    373:        return(debugger_read_dword(byteaddress));
                    374: }
                    375: UINT32 debugger_read_dword(offs_t byteaddress)
                    376: #endif
1.1       root      377: {
1.1.1.4   root      378: #if defined(HAS_I386)
1.1       root      379:        if(byteaddress < MAX_MEM - 3) {
                    380:                return *(UINT32 *)(mem + byteaddress);
1.1.1.3   root      381: //     } else if((byteaddress & 0xfffffff0) == 0xfffffff0) {
                    382: //             return read_dword(byteaddress & 0xfffff);
1.1       root      383:        }
                    384:        return 0;
1.1.1.4   root      385: #else
                    386:        return *(UINT32 *)(mem + byteaddress);
                    387: #endif
1.1       root      388: }
                    389: 
                    390: // write accessors
1.1.1.14  root      391: #ifdef USE_THREAD
                    392: void vram_flush_char()
                    393: {
                    394:        if(vram_length_char != 0) {
                    395:                DWORD num;
1.1.1.23  root      396:                WriteConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), scr_char, vram_length_char, vram_coord_char, &num);
1.1.1.14  root      397:                vram_length_char = vram_last_length_char = 0;
                    398:        }
                    399: }
                    400: 
                    401: void vram_flush_attr()
                    402: {
                    403:        if(vram_length_attr != 0) {
                    404:                DWORD num;
1.1.1.23  root      405:                WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), scr_attr, vram_length_attr, vram_coord_attr, &num);
1.1.1.14  root      406:                vram_length_attr = vram_last_length_attr = 0;
                    407:        }
                    408: }
                    409: 
                    410: void vram_flush()
                    411: {
                    412:        if(vram_length_char != 0 || vram_length_attr != 0) {
                    413:                EnterCriticalSection(&vram_crit_sect);
                    414:                vram_flush_char();
                    415:                vram_flush_attr();
                    416:                LeaveCriticalSection(&vram_crit_sect);
                    417:        }
                    418: }
                    419: #endif
                    420: 
                    421: void write_text_vram_char(offs_t offset, UINT8 data)
1.1.1.8   root      422: {
1.1.1.14  root      423: #ifdef USE_THREAD
                    424:        static offs_t first_offset_char, last_offset_char;
                    425:        
                    426:        if(vram_length_char != 0) {
                    427:                if(offset <= last_offset_char && offset >= first_offset_char) {
                    428:                        scr_char[(offset - first_offset_char) >> 1] = data;
                    429:                        return;
                    430:                }
                    431:                if(offset != last_offset_char + 2) {
                    432:                        vram_flush_char();
                    433:                }
                    434:        }
                    435:        if(vram_length_char == 0) {
                    436:                first_offset_char = offset;
                    437:                vram_coord_char.X = (offset >> 1) % scr_width;
                    438:                vram_coord_char.Y = (offset >> 1) / scr_width + scr_top;
                    439:        }
                    440:        scr_char[vram_length_char++] = data;
                    441:        last_offset_char = offset;
                    442: #else
1.1.1.8   root      443:        COORD co;
                    444:        DWORD num;
                    445:        
1.1.1.14  root      446:        co.X = (offset >> 1) % scr_width;
                    447:        co.Y = (offset >> 1) / scr_width;
                    448:        scr_char[0] = data;
1.1.1.23  root      449:        WriteConsoleOutputCharacter(GetStdHandle(STD_OUTPUT_HANDLE), scr_char, 1, co, &num);
1.1.1.14  root      450: #endif
                    451: }
                    452: 
                    453: void write_text_vram_attr(offs_t offset, UINT8 data)
                    454: {
                    455: #ifdef USE_THREAD
                    456:        static offs_t first_offset_attr, last_offset_attr;
                    457:        
                    458:        if(vram_length_attr != 0) {
                    459:                if(offset <= last_offset_attr && offset >= first_offset_attr) {
                    460:                        scr_attr[(offset - first_offset_attr) >> 1] = data;
                    461:                        return;
                    462:                }
                    463:                if(offset != last_offset_attr + 2) {
                    464:                        vram_flush_attr();
                    465:                }
                    466:        }
                    467:        if(vram_length_attr == 0) {
                    468:                first_offset_attr = offset;
                    469:                vram_coord_attr.X = (offset >> 1) % scr_width;
                    470:                vram_coord_attr.Y = (offset >> 1) / scr_width + scr_top;
                    471:        }
                    472:        scr_attr[vram_length_attr++] = data;
                    473:        last_offset_attr = offset;
                    474: #else
                    475:        COORD co;
                    476:        DWORD num;
1.1.1.8   root      477:        
1.1.1.14  root      478:        co.X = (offset >> 1) % scr_width;
                    479:        co.Y = (offset >> 1) / scr_width;
                    480:        scr_attr[0] = data;
1.1.1.23  root      481:        WriteConsoleOutputAttribute(GetStdHandle(STD_OUTPUT_HANDLE), scr_attr, 1, co, &num);
1.1.1.14  root      482: #endif
                    483: }
                    484: 
                    485: void write_text_vram_byte(offs_t offset, UINT8 data)
                    486: {
                    487:        EnterCriticalSection(&vram_crit_sect);
1.1.1.8   root      488:        if(offset & 1) {
1.1.1.14  root      489:                write_text_vram_attr(offset, data);
1.1.1.8   root      490:        } else {
1.1.1.14  root      491:                write_text_vram_char(offset, data);
1.1.1.8   root      492:        }
1.1.1.14  root      493:        LeaveCriticalSection(&vram_crit_sect);
1.1.1.8   root      494: }
                    495: 
                    496: void write_text_vram_word(offs_t offset, UINT16 data)
                    497: {
1.1.1.14  root      498:        EnterCriticalSection(&vram_crit_sect);
1.1.1.8   root      499:        if(offset & 1) {
1.1.1.14  root      500:                write_text_vram_attr(offset    , (data     ) & 0xff);
                    501:                write_text_vram_char(offset + 1, (data >> 8) & 0xff);
1.1.1.8   root      502:        } else {
1.1.1.14  root      503:                write_text_vram_char(offset    , (data     ) & 0xff);
                    504:                write_text_vram_attr(offset + 1, (data >> 8) & 0xff);
1.1.1.8   root      505:        }
1.1.1.14  root      506:        LeaveCriticalSection(&vram_crit_sect);
1.1.1.8   root      507: }
                    508: 
                    509: void write_text_vram_dword(offs_t offset, UINT32 data)
                    510: {
1.1.1.14  root      511:        EnterCriticalSection(&vram_crit_sect);
1.1.1.8   root      512:        if(offset & 1) {
1.1.1.14  root      513:                write_text_vram_attr(offset    , (data      ) & 0xff);
                    514:                write_text_vram_char(offset + 1, (data >>  8) & 0xff);
                    515:                write_text_vram_attr(offset + 2, (data >> 16) & 0xff);
                    516:                write_text_vram_char(offset + 3, (data >> 24) & 0xff);
                    517:        } else {
                    518:                write_text_vram_char(offset    , (data      ) & 0xff);
                    519:                write_text_vram_attr(offset + 1, (data >>  8) & 0xff);
                    520:                write_text_vram_char(offset + 2, (data >> 16) & 0xff);
                    521:                write_text_vram_attr(offset + 3, (data >> 24) & 0xff);
1.1.1.8   root      522:        }
1.1.1.14  root      523:        LeaveCriticalSection(&vram_crit_sect);
1.1.1.8   root      524: }
                    525: 
1.1       root      526: void write_byte(offs_t byteaddress, UINT8 data)
1.1.1.33  root      527: #ifdef USE_DEBUGGER
                    528: {
                    529:        if(now_debugging) {
                    530:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    531:                        if(wr_break_point.table[i].status == 1) {
                    532:                                if(byteaddress == wr_break_point.table[i].addr) {
                    533:                                        wr_break_point.hit = i + 1;
                    534:                                        now_suspended = true;
                    535:                                        break;
                    536:                                }
                    537:                        }
                    538:                }
                    539:        }
                    540:        debugger_write_byte(byteaddress, data);
                    541: }
                    542: void debugger_write_byte(offs_t byteaddress, UINT8 data)
                    543: #endif
1.1       root      544: {
1.1.1.8   root      545:        if(byteaddress < MEMORY_END) {
1.1.1.3   root      546:                mem[byteaddress] = data;
1.1.1.8   root      547:        } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14  root      548:                if(!restore_console_on_exit) {
                    549:                        change_console_size(scr_width, scr_height);
1.1.1.12  root      550:                }
1.1.1.8   root      551:                write_text_vram_byte(byteaddress - text_vram_top_address, data);
                    552:                mem[byteaddress] = data;
                    553:        } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
                    554:                if(int_10h_feh_called && !int_10h_ffh_called) {
                    555:                        write_text_vram_byte(byteaddress - shadow_buffer_top_address, data);
1.1       root      556:                }
                    557:                mem[byteaddress] = data;
1.1.1.4   root      558: #if defined(HAS_I386)
1.1.1.3   root      559:        } else if(byteaddress < MAX_MEM) {
1.1.1.4   root      560: #else
                    561:        } else {
                    562: #endif
1.1.1.3   root      563:                mem[byteaddress] = data;
1.1       root      564:        }
                    565: }
                    566: 
                    567: void write_word(offs_t byteaddress, UINT16 data)
1.1.1.33  root      568: #ifdef USE_DEBUGGER
                    569: {
                    570:        if(now_debugging) {
                    571:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    572:                        if(wr_break_point.table[i].status == 1) {
                    573:                                if(byteaddress >= wr_break_point.table[i].addr && byteaddress < wr_break_point.table[i].addr + 2) {
                    574:                                        wr_break_point.hit = i + 1;
                    575:                                        now_suspended = true;
                    576:                                        break;
                    577:                                }
                    578:                        }
                    579:                }
                    580:        }
                    581:        debugger_write_word(byteaddress, data);
                    582: }
                    583: void debugger_write_word(offs_t byteaddress, UINT16 data)
                    584: #endif
1.1       root      585: {
1.1.1.8   root      586:        if(byteaddress < MEMORY_END) {
1.1.1.14  root      587:                if(byteaddress == 0x450 + mem[0x462] * 2) {
                    588:                        COORD co;
                    589:                        co.X = data & 0xff;
                    590:                        co.Y = (data >> 8) + scr_top;
1.1.1.23  root      591:                        SetConsoleCursorPosition(GetStdHandle(STD_OUTPUT_HANDLE), co);
1.1.1.14  root      592:                }
1.1.1.3   root      593:                *(UINT16 *)(mem + byteaddress) = data;
1.1.1.8   root      594:        } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14  root      595:                if(!restore_console_on_exit) {
                    596:                        change_console_size(scr_width, scr_height);
1.1.1.12  root      597:                }
1.1.1.8   root      598:                write_text_vram_word(byteaddress - text_vram_top_address, data);
                    599:                *(UINT16 *)(mem + byteaddress) = data;
                    600:        } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
                    601:                if(int_10h_feh_called && !int_10h_ffh_called) {
                    602:                        write_text_vram_word(byteaddress - shadow_buffer_top_address, data);
1.1       root      603:                }
                    604:                *(UINT16 *)(mem + byteaddress) = data;
1.1.1.4   root      605: #if defined(HAS_I386)
1.1.1.3   root      606:        } else if(byteaddress < MAX_MEM - 1) {
1.1.1.4   root      607: #else
                    608:        } else {
                    609: #endif
1.1.1.3   root      610:                *(UINT16 *)(mem + byteaddress) = data;
1.1       root      611:        }
                    612: }
                    613: 
                    614: void write_dword(offs_t byteaddress, UINT32 data)
1.1.1.33  root      615: #ifdef USE_DEBUGGER
                    616: {
                    617:        if(now_debugging) {
                    618:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                    619:                        if(wr_break_point.table[i].status == 1) {
                    620:                                if(byteaddress >= wr_break_point.table[i].addr && byteaddress < wr_break_point.table[i].addr + 4) {
                    621:                                        wr_break_point.hit = i + 1;
                    622:                                        now_suspended = true;
                    623:                                        break;
                    624:                                }
                    625:                        }
                    626:                }
                    627:        }
                    628:        debugger_write_dword(byteaddress, data);
                    629: }
                    630: void debugger_write_dword(offs_t byteaddress, UINT32 data)
                    631: #endif
1.1       root      632: {
1.1.1.8   root      633:        if(byteaddress < MEMORY_END) {
1.1.1.3   root      634:                *(UINT32 *)(mem + byteaddress) = data;
1.1.1.8   root      635:        } else if(byteaddress >= text_vram_top_address && byteaddress < text_vram_end_address) {
1.1.1.14  root      636:                if(!restore_console_on_exit) {
                    637:                        change_console_size(scr_width, scr_height);
1.1.1.12  root      638:                }
1.1.1.8   root      639:                write_text_vram_dword(byteaddress - text_vram_top_address, data);
                    640:                *(UINT32 *)(mem + byteaddress) = data;
                    641:        } else if(byteaddress >= shadow_buffer_top_address && byteaddress < shadow_buffer_end_address) {
                    642:                if(int_10h_feh_called && !int_10h_ffh_called) {
                    643:                        write_text_vram_dword(byteaddress - shadow_buffer_top_address, data);
1.1       root      644:                }
                    645:                *(UINT32 *)(mem + byteaddress) = data;
1.1.1.4   root      646: #if defined(HAS_I386)
1.1.1.3   root      647:        } else if(byteaddress < MAX_MEM - 3) {
1.1.1.4   root      648: #else
                    649:        } else {
                    650: #endif
1.1.1.3   root      651:                *(UINT32 *)(mem + byteaddress) = data;
1.1       root      652:        }
                    653: }
                    654: 
                    655: #define read_decrypted_byte read_byte
                    656: #define read_decrypted_word read_word
                    657: #define read_decrypted_dword read_dword
                    658: 
1.1.1.3   root      659: #define read_raw_byte read_byte
                    660: #define write_raw_byte write_byte
                    661: 
                    662: #define read_word_unaligned read_word
                    663: #define write_word_unaligned write_word
                    664: 
                    665: #define read_io_word_unaligned read_io_word
                    666: #define write_io_word_unaligned write_io_word
                    667: 
1.1       root      668: UINT8 read_io_byte(offs_t byteaddress);
                    669: UINT16 read_io_word(offs_t byteaddress);
                    670: UINT32 read_io_dword(offs_t byteaddress);
                    671: 
                    672: void write_io_byte(offs_t byteaddress, UINT8 data);
                    673: void write_io_word(offs_t byteaddress, UINT16 data);
                    674: void write_io_dword(offs_t byteaddress, UINT32 data);
                    675: 
                    676: /*****************************************************************************/
                    677: /* src/osd/osdcomm.h */
                    678: 
                    679: /* Highly useful macro for compile-time knowledge of an array size */
                    680: #define ARRAY_LENGTH(x)     (sizeof(x) / sizeof(x[0]))
                    681: 
1.1.1.3   root      682: #if defined(HAS_I386)
1.1.1.10  root      683:        static CPU_TRANSLATE(i386);
                    684:        #include "mame/lib/softfloat/softfloat.c"
                    685:        #include "mame/emu/cpu/i386/i386.c"
1.1.1.12  root      686:        #include "mame/emu/cpu/vtlb.c"
1.1.1.3   root      687: #elif defined(HAS_I286)
1.1.1.10  root      688:        #include "mame/emu/cpu/i86/i286.c"
1.1.1.3   root      689: #else
1.1.1.10  root      690:        #include "mame/emu/cpu/i86/i86.c"
1.1.1.3   root      691: #endif
1.1.1.33  root      692: #ifdef USE_DEBUGGER
1.1.1.10  root      693:        #include "mame/emu/cpu/i386/i386dasm.c"
1.1       root      694: #endif
                    695: 
1.1.1.3   root      696: #if defined(HAS_I386)
                    697:        #define SREG(x)                         m_sreg[x].selector
                    698:        #define SREG_BASE(x)                    m_sreg[x].base
                    699:        int cpu_type, cpu_step;
                    700: #else
                    701:        #define REG8(x)                         m_regs.b[x]
                    702:        #define REG16(x)                        m_regs.w[x]
                    703:        #define SREG(x)                         m_sregs[x]
                    704:        #define SREG_BASE(x)                    m_base[x]
1.1.1.33  root      705:        #define m_eip                           (m_pc - m_base[CS])
1.1.1.3   root      706:        #define m_CF                            m_CarryVal
                    707:        #define m_a20_mask                      AMASK
                    708:        #define i386_load_segment_descriptor(x) m_base[x] = SegBase(x)
                    709:        #if defined(HAS_I286)
                    710:                #define i386_set_a20_line(x)    i80286_set_a20_line(x)
                    711:        #else
                    712:                #define i386_set_a20_line(x)
                    713:        #endif
                    714:        #define i386_set_irq_line(x, y)         set_irq_line(x, y)
                    715: #endif
1.1       root      716: 
                    717: void i386_jmp_far(UINT16 selector, UINT32 address)
                    718: {
1.1.1.3   root      719: #if defined(HAS_I386)
1.1       root      720:        if(PROTECTED_MODE && !V8086_MODE) {
1.1.1.3   root      721:                i386_protected_mode_jump(selector, address, 1, m_operand_size);
1.1       root      722:        } else {
1.1.1.3   root      723:                SREG(CS) = selector;
                    724:                m_performed_intersegment_jump = 1;
                    725:                i386_load_segment_descriptor(CS);
                    726:                m_eip = address;
                    727:                CHANGE_PC(m_eip);
1.1       root      728:        }
1.1.1.3   root      729: #elif defined(HAS_I286)
                    730:        i80286_code_descriptor(selector, address, 1);
                    731: #else
                    732:        SREG(CS) = selector;
                    733:        i386_load_segment_descriptor(CS);
                    734:        m_pc = (SREG_BASE(CS) + address) & m_a20_mask;
                    735: #endif
1.1       root      736: }
                    737: 
1.1.1.24  root      738: /*
                    739: void i386_call_far(UINT16 selector, UINT32 address)
                    740: {
                    741: #if defined(HAS_I386)
                    742:        if(PROTECTED_MODE && !V8086_MODE) {
                    743:                i386_protected_mode_call(selector, address, 1, m_operand_size);
                    744:        } else {
                    745:                PUSH16(SREG(CS));
                    746:                PUSH16(m_eip);
                    747:                SREG(CS) = selector;
                    748:                m_performed_intersegment_jump = 1;
                    749:                i386_load_segment_descriptor(CS);
                    750:                m_eip = address;
                    751:                CHANGE_PC(m_eip);
                    752:        }
                    753: #else
                    754:        UINT16 ip = m_pc - SREG_BASE(CS);
                    755:        UINT16 cs = SREG(CS);
                    756: #if defined(HAS_I286)
                    757:        i80286_code_descriptor(selector, address, 2);
                    758: #else
                    759:        SREG(CS) = selector;
                    760:        i386_load_segment_descriptor(CS);
                    761:        m_pc = (SREG_BASE(CS) + address) & m_a20_mask;
                    762: #endif
                    763:        PUSH(cs);
                    764:        PUSH(ip);
                    765:        CHANGE_PC(m_pc);
                    766: #endif
                    767: }
                    768: */
                    769: 
1.1.1.29  root      770: UINT16 i386_read_stack()
                    771: {
                    772: #if defined(HAS_I386)
                    773:        UINT32 ea, new_esp;
                    774:        if( STACK_32BIT ) {
                    775:                new_esp = REG32(ESP) + 2;
                    776:                ea = i386_translate(SS, new_esp - 2, 0);
                    777:        } else {
                    778:                new_esp = REG16(SP) + 2;
                    779:                ea = i386_translate(SS, (new_esp - 2) & 0xffff, 0);
                    780:        }
                    781:        return READ16(ea);
                    782: #else
                    783:        UINT16 sp = m_regs.w[SP] + 2;
                    784:        return ReadWord(((m_base[SS] + ((sp - 2) & 0xffff)) & AMASK));
                    785: #endif
                    786: }
                    787: 
1.1       root      788: /* ----------------------------------------------------------------------------
1.1.1.33  root      789:        debugger
                    790: ---------------------------------------------------------------------------- */
                    791: 
                    792: #ifdef USE_DEBUGGER
                    793: #define TELNET_BLUE      0x0004 // text color contains blue.
                    794: #define TELNET_GREEN     0x0002 // text color contains green.
                    795: #define TELNET_RED       0x0001 // text color contains red.
                    796: #define TELNET_INTENSITY 0x0008 // text color is intensified.
                    797: 
                    798: int svr_socket = 0;
                    799: int cli_socket = 0;
                    800: 
                    801: process_t *msdos_process_info_get(UINT16 psp_seg, bool show_error);
                    802: 
                    803: void debugger_init()
                    804: {
                    805:        now_debugging = false;
                    806:        now_going = false;
                    807:        now_suspended = false;
                    808:        force_suspend = false;
                    809:        
                    810:        memset(&break_point, 0, sizeof(break_point_t));
                    811:        memset(&rd_break_point, 0, sizeof(break_point_t));
                    812:        memset(&wr_break_point, 0, sizeof(break_point_t));
                    813:        memset(&in_break_point, 0, sizeof(break_point_t));
                    814:        memset(&out_break_point, 0, sizeof(break_point_t));
                    815:        memset(&int_break_point, 0, sizeof(int_break_point_t));
                    816: }
                    817: 
                    818: void telnet_send(char *string)
                    819: {
                    820:        char buffer[8192], *ptr;
                    821:        strcpy(buffer, string);
                    822:        while((ptr = strstr(buffer, "\n")) != NULL) {
                    823:                char tmp[8192];
                    824:                *ptr = '\0';
                    825:                sprintf(tmp, "%s\033E%s", buffer, ptr + 1);
                    826:                strcpy(buffer, tmp);
                    827:        }
                    828:        
                    829:        int len = strlen(buffer), res;
                    830:        ptr = buffer;
                    831:        while(len > 0) {
                    832:                if((res = send(cli_socket, ptr, len, 0)) > 0) {
                    833:                        len -= res;
                    834:                        ptr += res;
                    835:                }
                    836:        }
                    837: }
                    838: 
                    839: void telnet_command(const char *format, ...)
                    840: {
                    841:        char buffer[1024];
                    842:        va_list ap;
                    843:        va_start(ap, format);
                    844:        vsprintf(buffer, format, ap);
                    845:        va_end(ap);
                    846:        
                    847:        telnet_send(buffer);
                    848: }
                    849: 
                    850: void telnet_printf(const char *format, ...)
                    851: {
                    852:        char buffer[1024];
                    853:        va_list ap;
                    854:        va_start(ap, format);
                    855:        vsprintf(buffer, format, ap);
                    856:        va_end(ap);
                    857:        
                    858:        if(fp_debugger != NULL) {
                    859:                fprintf(fp_debugger, "%s", buffer);
                    860:        }
                    861:        telnet_send(buffer);
                    862: }
                    863: 
                    864: bool telnet_gets(char *str, int n)
                    865: {
                    866:        char buffer[1024];
                    867:        int ptr = 0;
                    868:        
                    869:        telnet_command("\033[12l"); // local echo on
                    870:        telnet_command("\033[2l");  // key unlock
                    871:        
                    872:        while(!m_halted) {
                    873:                int len = recv(cli_socket, buffer, sizeof(buffer), 0);
                    874:                
                    875:                if(len > 0 && buffer[0] != 0xff) {
                    876:                        for(int i = 0; i < len; i++) {
                    877:                                if(buffer[i] == 0x0d || buffer[i] == 0x0a) {
                    878:                                        str[ptr] = 0;
                    879:                                        telnet_command("\033[2h");  // key lock
                    880:                                        telnet_command("\033[12h"); // local echo off
                    881:                                        return(!m_halted);
                    882:                                } else if(buffer[i] == 0x08) {
                    883:                                        if(ptr > 0) {
                    884:                                                telnet_command("\033[0K"); // erase from cursor position
                    885:                                                ptr--;
                    886:                                        } else {
                    887:                                                telnet_command("\033[1C"); // move cursor forward
                    888:                                        }
                    889:                                } else if(ptr < n - 1) {
                    890:                                        if (buffer[i] >= 0x20 && buffer[i] <= 0x7e) {
                    891:                                                str[ptr++] = buffer[i];
                    892:                                        }
                    893:                                } else {
                    894:                                        telnet_command("\033[1D\033[0K"); // move cursor backward and erase from cursor position
                    895:                                }
                    896:                        }
                    897:                } else if(len == -1) {
                    898:                        if(WSAGetLastError() != WSAEWOULDBLOCK) {
                    899:                                return(false);
                    900:                        }
                    901:                } else if(len == 0) {
                    902:                        return(false);
                    903:                }
                    904:                Sleep(10);
                    905:        }
                    906:        return(!m_halted);
                    907: }
                    908: 
                    909: bool telnet_kbhit()
                    910: {
                    911:        char buffer[1024];
                    912:        
                    913:        if(!m_halted) {
                    914:                int len = recv(cli_socket, buffer, sizeof(buffer), 0);
                    915:                
                    916:                if(len > 0) {
                    917:                        for(int i = 0; i < len; i++) {
                    918:                                if(buffer[i] == 0x0d || buffer[i] == 0x0a) {
                    919:                                        return(true);
                    920:                                }
                    921:                        }
                    922:                } else if(len == 0) {
                    923:                        return(true); // disconnected
                    924:                }
                    925:        }
                    926:        return(false);
                    927: }
                    928: 
                    929: bool telnet_disconnected()
                    930: {
                    931:        char buffer[1024];
                    932:        int len = recv(cli_socket, buffer, sizeof(buffer), 0);
                    933:        
                    934:        if(len == 0) {
                    935:                return(true);
                    936:        } else if(len == -1) {
                    937:                if(WSAGetLastError() != WSAEWOULDBLOCK) {
                    938:                        return(true);
                    939:                }
                    940:        }
                    941:        return(false);
                    942: }
                    943: 
                    944: void telnet_set_color(int color)
                    945: {
                    946:        telnet_command("\033[%dm\033[3%dm", (color >> 3) & 1, (color & 7));
                    947: }
                    948: 
                    949: int debugger_dasm(char *buffer, UINT32 cs, UINT32 eip)
                    950: {
                    951: //     UINT8 *oprom = mem + (((cs << 4) + eip) & (MAX_MEM - 1));
                    952:        UINT8 ops[16];
                    953:        for(int i = 0; i < 16; i++) {
                    954:                ops[i] = debugger_read_byte(((cs << 4) + (eip + i)) & ADDR_MASK);
                    955:        }
                    956:        UINT8 *oprom = ops;
                    957:        
                    958: #if defined(HAS_I386)
                    959:        if(m_operand_size) {
                    960:                return(CPU_DISASSEMBLE_CALL(x86_32) & DASMFLAG_LENGTHMASK);
                    961:        } else
                    962: #endif
                    963:        return(CPU_DISASSEMBLE_CALL(x86_16) & DASMFLAG_LENGTHMASK);
                    964: }
                    965: 
                    966: void debugger_regs_info(char *buffer)
                    967: {
                    968: #if defined(HAS_I386)
                    969:        UINT32 flags = get_flags();
                    970: #else
                    971:        UINT32 flags = CompressFlags();
                    972: #endif
                    973:        
                    974: #if defined(HAS_I386)
                    975:        if(m_operand_size) {
                    976:                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",
                    977:                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),
                    978:                PROTECTED_MODE ? "PE" : "--",
                    979:                (flags & 0x40000) ? 'A' : '-',
                    980:                (flags & 0x20000) ? 'V' : '-',
                    981:                (flags & 0x10000) ? 'R' : '-',
                    982:                (flags & 0x04000) ? 'N' : '-',
                    983:                (flags & 0x02000) ? '1' : '0',
                    984:                (flags & 0x01000) ? '1' : '0',
                    985:                (flags & 0x00800) ? 'O' : '-',
                    986:                (flags & 0x00400) ? 'D' : '-',
                    987:                (flags & 0x00200) ? 'I' : '-',
                    988:                (flags & 0x00100) ? 'T' : '-',
                    989:                (flags & 0x00080) ? 'S' : '-',
                    990:                (flags & 0x00040) ? 'Z' : '-',
                    991:                (flags & 0x00010) ? 'A' : '-',
                    992:                (flags & 0x00004) ? 'P' : '-',
                    993:                (flags & 0x00001) ? 'C' : '-');
                    994:        } else {
                    995: #endif
                    996:                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",
                    997:                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),
                    998: #if defined(HAS_I386)
                    999:                PROTECTED_MODE ? "PE" : "--",
                   1000: #else
                   1001:                "--",
                   1002: #endif
                   1003:                (flags & 0x40000) ? 'A' : '-',
                   1004:                (flags & 0x20000) ? 'V' : '-',
                   1005:                (flags & 0x10000) ? 'R' : '-',
                   1006:                (flags & 0x04000) ? 'N' : '-',
                   1007:                (flags & 0x02000) ? '1' : '0',
                   1008:                (flags & 0x01000) ? '1' : '0',
                   1009:                (flags & 0x00800) ? 'O' : '-',
                   1010:                (flags & 0x00400) ? 'D' : '-',
                   1011:                (flags & 0x00200) ? 'I' : '-',
                   1012:                (flags & 0x00100) ? 'T' : '-',
                   1013:                (flags & 0x00080) ? 'S' : '-',
                   1014:                (flags & 0x00040) ? 'Z' : '-',
                   1015:                (flags & 0x00010) ? 'A' : '-',
                   1016:                (flags & 0x00004) ? 'P' : '-',
                   1017:                (flags & 0x00001) ? 'C' : '-');
                   1018: #if defined(HAS_I386)
                   1019:        }
                   1020: #endif
                   1021: }
                   1022: 
                   1023: void debugger_process_info(char *buffer)
                   1024: {
                   1025:        UINT16 psp_seg = current_psp;
                   1026:        process_t *process;
                   1027:        bool check[0x10000] = {0};
                   1028:        
                   1029:        buffer[0] = '\0';
                   1030:        
                   1031:        while(!check[psp_seg] && (process  = msdos_process_info_get(psp_seg, false)) != NULL) {
                   1032:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   1033:                char *file = process->module_path, *s;
                   1034:                char tmp[8192];
                   1035:                
                   1036:                while((s = strstr(file, "\\")) != NULL) {
                   1037:                        file = s + 1;
                   1038:                }
                   1039:                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));
                   1040:                strcat(tmp, buffer);
                   1041:                strcpy(buffer, tmp);
                   1042:                
                   1043:                check[psp_seg] = true;
                   1044:                psp_seg = psp->parent_psp;
                   1045:        }
                   1046: }
                   1047: 
                   1048: UINT32 debugger_get_val(const char *str)
                   1049: {
                   1050:        char tmp[1024];
                   1051:        
                   1052:        if(str == NULL || strlen(str) == 0) {
                   1053:                return(0);
                   1054:        }
                   1055:        strcpy(tmp, str);
                   1056:        
                   1057:        if(strlen(tmp) == 3 && tmp[0] == '\'' && tmp[2] == '\'') {
                   1058:                // ank
                   1059:                return(tmp[1] & 0xff);
                   1060:        } else if(tmp[0] == '%') {
                   1061:                // decimal
                   1062:                return(strtoul(tmp + 1, NULL, 10));
                   1063:        }
                   1064:        return(strtoul(tmp, NULL, 16));
                   1065: }
                   1066: 
                   1067: UINT32 debugger_get_seg(const char *str, UINT32 val)
                   1068: {
                   1069:        char tmp[1024], *s;
                   1070:        
                   1071:        if(str == NULL || strlen(str) == 0) {
                   1072:                return(val);
                   1073:        }
                   1074:        strcpy(tmp, str);
                   1075:        
                   1076:        if((s = strstr(tmp, ":")) != NULL) {
                   1077:                // 0000:0000
                   1078:                *s = '\0';
                   1079:                return(debugger_get_val(tmp));
                   1080:        }
                   1081:        return(val);
                   1082: }
                   1083: 
                   1084: UINT32 debugger_get_ofs(const char *str)
                   1085: {
                   1086:        char tmp[1024], *s;
                   1087:        
                   1088:        if(str == NULL || strlen(str) == 0) {
                   1089:                return(0);
                   1090:        }
                   1091:        strcpy(tmp, str);
                   1092:        
                   1093:        if((s = strstr(tmp, ":")) != NULL) {
                   1094:                // 0000:0000
                   1095:                return(debugger_get_val(s + 1));
                   1096:        }
                   1097:        return(debugger_get_val(tmp));
                   1098: }
                   1099: 
                   1100: void debugger_main()
                   1101: {
                   1102:        telnet_command("\033[20h"); // cr-lf
                   1103:        
                   1104:        force_suspend = true;
                   1105:        now_going = false;
                   1106:        now_debugging = true;
                   1107:        Sleep(100);
                   1108:        
                   1109:        if(!m_halted && !now_suspended) {
                   1110:                telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1111:                telnet_printf("waiting until cpu is suspended...\n");
                   1112:        }
                   1113:        while(!m_halted && !now_suspended) {
                   1114:                if(telnet_disconnected()) {
                   1115:                        break;
                   1116:                }
                   1117:                Sleep(10);
                   1118:        }
                   1119:        
                   1120:        char buffer[8192];
                   1121:        
                   1122:        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1123:        debugger_process_info(buffer);
                   1124:        telnet_printf("%s", buffer);
                   1125:        debugger_regs_info(buffer);
                   1126:        telnet_printf("%s", buffer);
                   1127:        telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1128:        telnet_printf("breaked at %04X:%04X\n", SREG(CS), m_eip);
                   1129:        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1130:        debugger_dasm(buffer, SREG(CS), m_eip);
                   1131:        telnet_printf("next\t%04X:%04X  %s\n", SREG(CS), m_eip, buffer);
                   1132:        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1133:        
                   1134:        #define MAX_COMMAND_LEN 64
                   1135:        
                   1136:        char command[MAX_COMMAND_LEN + 1];
                   1137:        char prev_command[MAX_COMMAND_LEN + 1] = {0};
                   1138:        
                   1139:        UINT32 data_seg = SREG(DS);
                   1140:        UINT32 data_ofs = 0;
                   1141:        UINT32 dasm_seg = SREG(CS);
                   1142:        UINT32 dasm_ofs = m_eip;
                   1143:        
                   1144:        while(!m_halted) {
                   1145:                telnet_printf("- ");
                   1146:                command[0] = '\0';
                   1147:                
                   1148:                if(fi_debugger != NULL) {
                   1149:                        while(command[0] == '\0') {
                   1150:                                if(fgets(command, sizeof(command), fi_debugger) == NULL) {
                   1151:                                        break;
                   1152:                                }
                   1153:                                while(strlen(command) > 0 && (command[strlen(command) - 1] == 0x0d || command[strlen(command) - 1] == 0x0a)) {
                   1154:                                        command[strlen(command) - 1] = '\0';
                   1155:                                }
                   1156:                        }
                   1157:                        if(command[0] != '\0') {
                   1158:                                telnet_command("%s\n", command);
                   1159:                        }
                   1160:                }
                   1161:                if(command[0] == '\0') {
                   1162:                        if(!telnet_gets(command, sizeof(command))) {
                   1163:                                break;
                   1164:                        }
                   1165:                }
                   1166:                if(command[0] == '\0') {
                   1167:                        strcpy(command, prev_command);
                   1168:                } else {
                   1169:                        strcpy(prev_command, command);
                   1170:                }
                   1171:                if(fp_debugger != NULL) {
                   1172:                        fprintf(fp_debugger, "%s\n", command);
                   1173:                }
                   1174:                
                   1175:                if(!m_halted && command[0] != 0) {
                   1176:                        char *params[32], *token = NULL;
                   1177:                        int num = 0;
                   1178:                        
                   1179:                        if((token = strtok(command, " ")) != NULL) {
                   1180:                                params[num++] = token;
                   1181:                                while(num < 32 && (token = strtok(NULL, " ")) != NULL) {
                   1182:                                        params[num++] = token;
                   1183:                                }
                   1184:                        }
                   1185:                        if(stricmp(params[0], "D") == 0) {
                   1186:                                if(num <= 3) {
                   1187:                                        if(num >= 2) {
                   1188:                                                data_seg = debugger_get_seg(params[1], data_seg);
                   1189:                                                data_ofs = debugger_get_ofs(params[1]);
                   1190:                                        }
                   1191:                                        UINT32 end_seg = data_seg;
                   1192:                                        UINT32 end_ofs = data_ofs + 8 * 16 - 1;
                   1193:                                        if(num == 3) {
                   1194:                                                end_seg = debugger_get_seg(params[2], data_seg);
                   1195:                                                end_ofs = debugger_get_ofs(params[2]);
                   1196:                                        }
                   1197:                                        UINT64 start_addr = (data_seg << 4) + data_ofs;
                   1198:                                        UINT64 end_addr = (end_seg << 4) + end_ofs;
                   1199: //                                     bool sjis = false;
                   1200:                                        
                   1201:                                        for(UINT64 addr = (start_addr & ~0x0f); addr <= (end_addr | 0x0f); addr++) {
                   1202:                                                if((addr & 0x0f) == 0) {
                   1203:                                                        if((data_ofs = addr - (data_seg << 4)) > 0xffff) {
                   1204:                                                                data_seg += 0x1000;
                   1205:                                                                data_ofs -= 0x10000;
                   1206:                                                        }
                   1207:                                                        telnet_printf("%06X:%04X ", data_seg, data_ofs);
                   1208:                                                        memset(buffer, 0, sizeof(buffer));
                   1209:                                                }
                   1210:                                                if(addr < start_addr || addr > end_addr) {
                   1211:                                                        telnet_printf("   ");
                   1212:                                                        buffer[addr & 0x0f] = ' ';
                   1213:                                                } else {
                   1214:                                                        UINT8 data = debugger_read_byte(addr & ADDR_MASK);
                   1215:                                                        telnet_printf(" %02X", data);
                   1216: //                                                     if(sjis) {
                   1217: //                                                             buffer[addr & 0x0f] = data;
                   1218: //                                                             sjis = false;
                   1219: //                                                     } else if(((data >= 0x81 && data <= 0x9f) || (data >= 0xe0 && data <= 0xef)) && (addr & 0x0f) < 0x0f) {
                   1220: //                                                             buffer[addr & 0x0f] = data;
                   1221: //                                                             sjis = true;
                   1222: //                                                     } else
                   1223:                                                        if((data >= 0x20 && data <= 0x7e)/* || (data >= 0xa1 && data <= 0xdf)*/) {
                   1224:                                                                buffer[addr & 0x0f] = data;
                   1225:                                                        } else {
                   1226:                                                                buffer[addr & 0x0f] = '.';
                   1227:                                                        }
                   1228:                                                }
                   1229:                                                if((addr & 0x0f) == 0x0f) {
                   1230:                                                        telnet_printf("  %s\n", buffer);
                   1231:                                                }
                   1232:                                        }
                   1233:                                        if((data_ofs = (end_addr + 1) - (data_seg << 4)) > 0xffff) {
                   1234:                                                data_seg += 0x1000;
                   1235:                                                data_ofs -= 0x10000;
                   1236:                                        }
                   1237:                                        prev_command[1] = '\0'; // remove parameters to dump continuously
                   1238:                                } else {
                   1239:                                        telnet_printf("invalid parameter number\n");
                   1240:                                }
                   1241:                        } else if(stricmp(params[0], "E") == 0 || stricmp(params[0], "EB") == 0) {
                   1242:                                if(num >= 3) {
                   1243:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1244:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1245:                                        for(int i = 2, j = 0; i < num; i++, j++) {
                   1246:                                                debugger_write_byte(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]) & 0xff);
                   1247:                                        }
                   1248:                                } else {
                   1249:                                        telnet_printf("invalid parameter number\n");
                   1250:                                }
                   1251:                        } else if(stricmp(params[0], "EW") == 0) {
                   1252:                                if(num >= 3) {
                   1253:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1254:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1255:                                        for(int i = 2, j = 0; i < num; i++, j += 2) {
                   1256:                                                debugger_write_word(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]) & 0xffff);
                   1257:                                        }
                   1258:                                } else {
                   1259:                                        telnet_printf("invalid parameter number\n");
                   1260:                                }
                   1261:                        } else if(stricmp(params[0], "ED") == 0) {
                   1262:                                if(num >= 3) {
                   1263:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1264:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1265:                                        for(int i = 2, j = 0; i < num; i++, j += 4) {
                   1266:                                                debugger_write_dword(((seg << 4) + (ofs + j)) & ADDR_MASK, debugger_get_val(params[i]));
                   1267:                                        }
                   1268:                                } else {
                   1269:                                        telnet_printf("invalid parameter number\n");
                   1270:                                }
                   1271:                        } else if(stricmp(params[0], "EA") == 0) {
                   1272:                                if(num >= 3) {
                   1273:                                        UINT32 seg = debugger_get_seg(params[1], data_seg);
                   1274:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1275:                                        strcpy(buffer, prev_command);
                   1276:                                        if((token = strtok(buffer, "\"")) != NULL && (token = strtok(NULL, "\"")) != NULL) {
                   1277:                                                int len = strlen(token);
                   1278:                                                for(int i = 0; i < len; i++) {
                   1279:                                                        debugger_write_byte(((seg << 4) + (ofs + i)) & ADDR_MASK, token[i] & 0xff);
                   1280:                                                }
                   1281:                                        } else {
                   1282:                                                telnet_printf("invalid parameter\n");
                   1283:                                        }
                   1284:                                } else {
                   1285:                                        telnet_printf("invalid parameter number\n");
                   1286:                                }
                   1287:                        } else if(stricmp(params[0], "I") == 0 || stricmp(params[0], "IB") == 0) {
                   1288:                                if(num == 2) {
                   1289:                                        telnet_printf("%02X\n", debugger_read_io_byte(debugger_get_val(params[1])) & 0xff);
                   1290:                                } else {
                   1291:                                        telnet_printf("invalid parameter number\n");
                   1292:                                }
                   1293:                        } else if(stricmp(params[0], "IW") == 0) {
                   1294:                                if(num == 2) {
                   1295:                                        telnet_printf("%04X\n", debugger_read_io_word(debugger_get_val(params[1])) & 0xffff);
                   1296:                                } else {
                   1297:                                        telnet_printf("invalid parameter number\n");
                   1298:                                }
                   1299:                        } else if(stricmp(params[0], "ID") == 0) {
                   1300:                                if(num == 2) {
                   1301:                                        telnet_printf("%08X\n", debugger_read_io_dword(debugger_get_val(params[1])));
                   1302:                                } else {
                   1303:                                        telnet_printf("invalid parameter number\n");
                   1304:                                }
                   1305:                        } else if(stricmp(params[0], "O") == 0 || stricmp(params[0], "OB") == 0) {
                   1306:                                if(num == 3) {
                   1307:                                        debugger_write_io_byte(debugger_get_val(params[1]), debugger_get_val(params[2]) & 0xff);
                   1308:                                } else {
                   1309:                                        telnet_printf("invalid parameter number\n");
                   1310:                                }
                   1311:                        } else if(stricmp(params[0], "OW") == 0) {
                   1312:                                if(num == 3) {
                   1313:                                        debugger_write_io_word(debugger_get_val(params[1]), debugger_get_val(params[2]) & 0xffff);
                   1314:                                } else {
                   1315:                                        telnet_printf("invalid parameter number\n");
                   1316:                                }
                   1317:                        } else if(stricmp(params[0], "OD") == 0) {
                   1318:                                if(num == 3) {
                   1319:                                        debugger_write_io_dword(debugger_get_val(params[1]), debugger_get_val(params[2]));
                   1320:                                } else {
                   1321:                                        telnet_printf("invalid parameter number\n");
                   1322:                                }
                   1323:                        } else if(stricmp(params[0], "R") == 0) {
                   1324:                                if(num == 1) {
                   1325:                                        debugger_regs_info(buffer);
                   1326:                                        telnet_printf("%s", buffer);
                   1327:                                } else if(num == 3) {
                   1328: #if defined(HAS_I386)
                   1329:                                        if(stricmp(params[1], "EAX") == 0) {
                   1330:                                                REG32(EAX) = debugger_get_val(params[2]);
                   1331:                                        } else if(stricmp(params[1], "EBX") == 0) {
                   1332:                                                REG32(EBX) = debugger_get_val(params[2]);
                   1333:                                        } else if(stricmp(params[1], "ECX") == 0) {
                   1334:                                                REG32(ECX) = debugger_get_val(params[2]);
                   1335:                                        } else if(stricmp(params[1], "EDX") == 0) {
                   1336:                                                REG32(EDX) = debugger_get_val(params[2]);
                   1337:                                        } else if(stricmp(params[1], "ESP") == 0) {
                   1338:                                                REG32(ESP) = debugger_get_val(params[2]);
                   1339:                                        } else if(stricmp(params[1], "EBP") == 0) {
                   1340:                                                REG32(EBP) = debugger_get_val(params[2]);
                   1341:                                        } else if(stricmp(params[1], "ESI") == 0) {
                   1342:                                                REG32(ESI) = debugger_get_val(params[2]);
                   1343:                                        } else if(stricmp(params[1], "EDI") == 0) {
                   1344:                                                REG32(EDI) = debugger_get_val(params[2]);
                   1345:                                        } else
                   1346: #endif
                   1347:                                        if(stricmp(params[1], "AX") == 0) {
                   1348:                                                REG16(AX) = debugger_get_val(params[2]);
                   1349:                                        } else if(stricmp(params[1], "BX") == 0) {
                   1350:                                                REG16(BX) = debugger_get_val(params[2]);
                   1351:                                        } else if(stricmp(params[1], "CX") == 0) {
                   1352:                                                REG16(CX) = debugger_get_val(params[2]);
                   1353:                                        } else if(stricmp(params[1], "DX") == 0) {
                   1354:                                                REG16(DX) = debugger_get_val(params[2]);
                   1355:                                        } else if(stricmp(params[1], "SP") == 0) {
                   1356:                                                REG16(SP) = debugger_get_val(params[2]);
                   1357:                                        } else if(stricmp(params[1], "BP") == 0) {
                   1358:                                                REG16(BP) = debugger_get_val(params[2]);
                   1359:                                        } else if(stricmp(params[1], "SI") == 0) {
                   1360:                                                REG16(SI) = debugger_get_val(params[2]);
                   1361:                                        } else if(stricmp(params[1], "DI") == 0) {
                   1362:                                                REG16(DI) = debugger_get_val(params[2]);
                   1363:                                        } else if(stricmp(params[1], "IP") == 0 || stricmp(params[1], "EIP") == 0) {
                   1364: #if defined(HAS_I386)
                   1365:                                                if(m_operand_size) {
                   1366:                                                        m_eip = debugger_get_val(params[2]);
                   1367:                                                } else {
                   1368:                                                        m_eip = debugger_get_val(params[2]) & 0xffff;
                   1369:                                                }
                   1370:                                                CHANGE_PC(m_eip);
                   1371: #else
                   1372:                                                m_pc = (SREG_BASE(CS) + (debugger_get_val(params[2]) & 0xffff)) & ADDR_MASK;
                   1373:                                                CHANGE_PC(m_pc);
                   1374: #endif
                   1375:                                        } else if(stricmp(params[1], "AL") == 0) {
                   1376:                                                REG8(AL) = debugger_get_val(params[2]);
                   1377:                                        } else if(stricmp(params[1], "AH") == 0) {
                   1378:                                                REG8(AH) = debugger_get_val(params[2]);
                   1379:                                        } else if(stricmp(params[1], "BL") == 0) {
                   1380:                                                REG8(BL) = debugger_get_val(params[2]);
                   1381:                                        } else if(stricmp(params[1], "BH") == 0) {
                   1382:                                                REG8(BH) = debugger_get_val(params[2]);
                   1383:                                        } else if(stricmp(params[1], "CL") == 0) {
                   1384:                                                REG8(CL) = debugger_get_val(params[2]);
                   1385:                                        } else if(stricmp(params[1], "CH") == 0) {
                   1386:                                                REG8(CH) = debugger_get_val(params[2]);
                   1387:                                        } else if(stricmp(params[1], "DL") == 0) {
                   1388:                                                REG8(DL) = debugger_get_val(params[2]);
                   1389:                                        } else if(stricmp(params[1], "DH") == 0) {
                   1390:                                                REG8(DH) = debugger_get_val(params[2]);
                   1391:                                        } else {
                   1392:                                                telnet_printf("unknown register %s\n", params[1]);
                   1393:                                        }
                   1394:                                } else {
                   1395:                                        telnet_printf("invalid parameter number\n");
                   1396:                                }
                   1397:                        } else if(_tcsicmp(params[0], "S") == 0) {
                   1398:                                if(num >= 4) {
                   1399:                                        UINT32 cur_seg = debugger_get_seg(params[1], data_seg);
                   1400:                                        UINT32 cur_ofs = debugger_get_ofs(params[1]);
                   1401:                                        UINT32 end_seg = debugger_get_seg(params[2], cur_seg);
                   1402:                                        UINT32 end_ofs = debugger_get_ofs(params[2]);
                   1403:                                        UINT8 list[32];
                   1404:                                        
                   1405:                                        for(int i = 3, j = 0; i < num && j < 32; i++, j++) {
                   1406:                                                list[j] = debugger_get_val(params[i]);
                   1407:                                        }
                   1408:                                        while((cur_seg << 4) + cur_ofs <= (end_seg << 4) + end_ofs) {
                   1409:                                                bool found = true;
                   1410:                                                for(int i = 3, j = 0; i < num && j < 32; i++, j++) {
                   1411:                                                        if(debugger_read_byte(((cur_seg << 4) + (cur_ofs + j)) & ADDR_MASK) != list[j]) {
                   1412:                                                                found = false;
                   1413:                                                                break;
                   1414:                                                        }
                   1415:                                                }
                   1416:                                                if(found) {
                   1417:                                                        telnet_printf("%04X:%04X\n", cur_seg, cur_ofs);
                   1418:                                                }
                   1419:                                                if((cur_ofs += 1) > 0xffff) {
                   1420:                                                        cur_seg += 0x1000;
                   1421:                                                        cur_ofs -= 0x10000;
                   1422:                                                }
                   1423:                                        }
                   1424:                                } else {
                   1425:                                        telnet_printf("invalid parameter number\n");
                   1426:                                }
                   1427:                        } else if(stricmp(params[0], "U") == 0) {
                   1428:                                if(num <= 3) {
                   1429:                                        if(num >= 2) {
                   1430:                                                dasm_seg = debugger_get_seg(params[1], dasm_seg);
                   1431:                                                dasm_ofs = debugger_get_ofs(params[1]);
                   1432:                                        }
                   1433:                                        if(num == 3) {
                   1434:                                                UINT32 end_seg = debugger_get_seg(params[2], dasm_seg);
                   1435:                                                UINT32 end_ofs = debugger_get_ofs(params[2]);
                   1436:                                                
                   1437:                                                while((dasm_seg << 4) + dasm_ofs <= (end_seg << 4) + end_ofs) {
                   1438:                                                        int len = debugger_dasm(buffer, dasm_seg, dasm_ofs);
                   1439:                                                        telnet_printf("%04X:%04X  ", dasm_seg, dasm_ofs);
                   1440:                                                        for(int i = 0; i < len; i++) {
                   1441:                                                                telnet_printf("%02X", debugger_read_byte(((dasm_seg << 4) + (dasm_ofs + i)) & ADDR_MASK));
                   1442:                                                        }
                   1443:                                                        for(int i = len; i < 8; i++) {
                   1444:                                                                telnet_printf("  ");
                   1445:                                                        }
                   1446:                                                        telnet_printf("  %s\n", buffer);
                   1447:                                                        if((dasm_ofs += len) > 0xffff) {
                   1448:                                                                dasm_seg += 0x1000;
                   1449:                                                                dasm_ofs -= 0x10000;
                   1450:                                                        }
                   1451:                                                }
                   1452:                                        } else {
                   1453:                                                for(int i = 0; i < 16; i++) {
                   1454:                                                        int len = debugger_dasm(buffer, dasm_seg, dasm_ofs);
                   1455:                                                        telnet_printf("%04X:%04X  ", dasm_seg, dasm_ofs);
                   1456:                                                        for(int i = 0; i < len; i++) {
                   1457:                                                                telnet_printf("%02X", debugger_read_byte(((dasm_seg << 4) + (dasm_ofs + i)) & ADDR_MASK));
                   1458:                                                        }
                   1459:                                                        for(int i = len; i < 8; i++) {
                   1460:                                                                telnet_printf("  ");
                   1461:                                                        }
                   1462:                                                        telnet_printf("  %s\n", buffer);
                   1463:                                                        if((dasm_ofs += len) > 0xffff) {
                   1464:                                                                dasm_seg += 0x1000;
                   1465:                                                                dasm_ofs -= 0x10000;
                   1466:                                                        }
                   1467:                                                }
                   1468:                                        }
                   1469:                                        prev_command[1] = '\0'; // remove parameters to disassemble continuously
                   1470:                                } else {
                   1471:                                        telnet_printf("invalid parameter number\n");
                   1472:                                }
                   1473:                        } else if(stricmp(params[0], "H") == 0) {
                   1474:                                if(num == 3) {
                   1475:                                        UINT32 l = debugger_get_val(params[1]);
                   1476:                                        UINT32 r = debugger_get_val(params[2]);
                   1477:                                        telnet_printf("%08X  %08X\n", l + r, l - r);
                   1478:                                } else {
                   1479:                                        telnet_printf("invalid parameter number\n");
                   1480:                                }
                   1481:                        } else if(stricmp(params[0], "BP") == 0 || stricmp(params[0], "RBP") == 0 || stricmp(params[0], "WBP") == 0) {
                   1482:                                break_point_t *break_point_ptr;
                   1483:                                #define GET_BREAK_POINT_PTR() { \
                   1484:                                        if(params[0][0] == 'R') { \
                   1485:                                                break_point_ptr = &rd_break_point; \
                   1486:                                        } else if(params[0][0] == 'W') { \
                   1487:                                                break_point_ptr = &wr_break_point; \
                   1488:                                        } else if(params[0][0] == 'I') { \
                   1489:                                                break_point_ptr = &in_break_point; \
                   1490:                                        } else if(params[0][0] == 'O') { \
                   1491:                                                break_point_ptr = &out_break_point; \
                   1492:                                        } else { \
                   1493:                                                break_point_ptr = &break_point; \
                   1494:                                        } \
                   1495:                                }
                   1496:                                GET_BREAK_POINT_PTR();
                   1497:                                if(num == 2) {
                   1498:                                        UINT32 seg = debugger_get_seg(params[1], SREG(CS));
                   1499:                                        UINT32 ofs = debugger_get_ofs(params[1]);
                   1500:                                        bool found = false;
                   1501:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1502:                                                if(break_point_ptr->table[i].status == 0 || break_point_ptr->table[i].addr == ((seg << 4) + ofs)) {
                   1503:                                                        break_point_ptr->table[i].addr = (seg << 4) + ofs;
                   1504:                                                        break_point_ptr->table[i].seg = seg;
                   1505:                                                        break_point_ptr->table[i].ofs = ofs;
                   1506:                                                        break_point_ptr->table[i].status = 1;
                   1507:                                                        found = true;
                   1508:                                                }
                   1509:                                        }
                   1510:                                        if(!found) {
                   1511:                                                telnet_printf("too many break points\n");
                   1512:                                        }
                   1513:                                } else {
                   1514:                                        telnet_printf("invalid parameter number\n");
                   1515:                                }
                   1516:                        } else if(stricmp(params[0], "IBP") == 0 || stricmp(params[0], "OBP") == 0) {
                   1517:                                break_point_t *break_point_ptr;
                   1518:                                GET_BREAK_POINT_PTR();
                   1519:                                if(num == 2) {
                   1520:                                        UINT32 addr = debugger_get_val(params[1]);
                   1521:                                        bool found = false;
                   1522:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1523:                                                if(break_point_ptr->table[i].status == 0 || break_point_ptr->table[i].addr == addr) {
                   1524:                                                        break_point_ptr->table[i].addr = addr;
                   1525:                                                        break_point_ptr->table[i].status = 1;
                   1526:                                                        found = true;
                   1527:                                                }
                   1528:                                        }
                   1529:                                        if(!found) {
                   1530:                                                telnet_printf("too many break points\n");
                   1531:                                        }
                   1532:                                } else {
                   1533:                                        telnet_printf("invalid parameter number\n");
                   1534:                                }
                   1535:                        } 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) {
                   1536:                                break_point_t *break_point_ptr;
                   1537:                                GET_BREAK_POINT_PTR();
                   1538:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1539:                                        memset(break_point_ptr, 0, sizeof(break_point_t));
                   1540:                                } else if(num >= 2) {
                   1541:                                        for(int i = 1; i < num; i++) {
                   1542:                                                int index = debugger_get_val(params[i]);
                   1543:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1544:                                                        telnet_printf("invalid index %x\n", index);
                   1545:                                                } else {
                   1546:                                                        break_point_ptr->table[index - 1].addr = 0;
                   1547:                                                        break_point_ptr->table[index - 1].seg = 0;
                   1548:                                                        break_point_ptr->table[index - 1].ofs = 0;
                   1549:                                                        break_point_ptr->table[index - 1].status = 0;
                   1550:                                                }
                   1551:                                        }
                   1552:                                } else {
                   1553:                                        telnet_printf("invalid parameter number\n");
                   1554:                                }
                   1555:                        } 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 ||
                   1556:                                  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) {
                   1557:                                break_point_t *break_point_ptr;
                   1558:                                GET_BREAK_POINT_PTR();
                   1559:                                bool enabled = (params[0][strlen(params[0]) - 1] == 'E' || params[0][strlen(params[0]) - 1] == 'e');
                   1560:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1561:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1562:                                                if(break_point_ptr->table[i].status != 0) {
                   1563:                                                        break_point_ptr->table[i].status = enabled ? 1 : -1;
                   1564:                                                }
                   1565:                                        }
                   1566:                                } else if(num >= 2) {
                   1567:                                        for(int i = 1; i < num; i++) {
                   1568:                                                int index = debugger_get_val(params[i]);
                   1569:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1570:                                                        telnet_printf("invalid index %x\n", index);
                   1571:                                                } else if(break_point_ptr->table[index - 1].status == 0) {
                   1572:                                                        telnet_printf("break point %x is null\n", index);
                   1573:                                                } else {
                   1574:                                                        break_point_ptr->table[index - 1].status = enabled ? 1 : -1;
                   1575:                                                }
                   1576:                                        }
                   1577:                                } else {
                   1578:                                        telnet_printf("invalid parameter number\n");
                   1579:                                }
                   1580:                        } else if(stricmp(params[0], "BL") == 0 || stricmp(params[0], "RBL") == 0 || stricmp(params[0], "WBL") == 0) {
                   1581:                                break_point_t *break_point_ptr;
                   1582:                                GET_BREAK_POINT_PTR();
                   1583:                                if(num == 1) {
                   1584:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1585:                                                if(break_point_ptr->table[i].status) {
                   1586:                                                        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);
                   1587:                                                }
                   1588:                                        }
                   1589:                                } else {
                   1590:                                        telnet_printf("invalid parameter number\n");
                   1591:                                }
                   1592:                        } else if(stricmp(params[0], "IBL") == 0 || stricmp(params[0], "OBL") == 0) {
                   1593:                                break_point_t *break_point_ptr;
                   1594:                                GET_BREAK_POINT_PTR();
                   1595:                                if(num == 1) {
                   1596:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1597:                                                if(break_point_ptr->table[i].status) {
                   1598:                                                        telnet_printf("%d %c %04X\n", i + 1, break_point_ptr->table[i].status == 1 ? 'e' : 'd', break_point_ptr->table[i].addr);
                   1599:                                                }
                   1600:                                        }
                   1601:                                } else {
                   1602:                                        telnet_printf("invalid parameter number\n");
                   1603:                                }
                   1604:                        } else if(stricmp(params[0], "INTBP") == 0) {
                   1605:                                if(num >= 2 && num <= 4) {
                   1606:                                        int int_num = debugger_get_val(params[1]);
                   1607:                                        UINT8 ah = 0, ah_registered = 0;
                   1608:                                        UINT8 al = 0, al_registered = 0;
                   1609:                                        if(num >= 3) {
                   1610:                                                ah = debugger_get_val(params[2]);
                   1611:                                                ah_registered = 1;
                   1612:                                        }
                   1613:                                        if(num == 4) {
                   1614:                                                al = debugger_get_val(params[3]);
                   1615:                                                al_registered = 1;
                   1616:                                        }
                   1617:                                        bool found = false;
                   1618:                                        for(int i = 0; i < MAX_BREAK_POINTS && !found; i++) {
                   1619:                                                if(int_break_point.table[i].status == 0 || (
                   1620:                                                   int_break_point.table[i].int_num == int_num &&
                   1621:                                                   int_break_point.table[i].ah == ah && int_break_point.table[i].ah_registered == ah_registered &&
                   1622:                                                   int_break_point.table[i].al == al && int_break_point.table[i].al_registered == al_registered)) {
                   1623:                                                        int_break_point.table[i].int_num = int_num;
                   1624:                                                        int_break_point.table[i].ah = ah;
                   1625:                                                        int_break_point.table[i].ah_registered = ah_registered;
                   1626:                                                        int_break_point.table[i].al = al;
                   1627:                                                        int_break_point.table[i].al_registered = al_registered;
                   1628:                                                        int_break_point.table[i].status = 1;
                   1629:                                                        found = true;
                   1630:                                                }
                   1631:                                        }
                   1632:                                        if(!found) {
                   1633:                                                telnet_printf("too many break points\n");
                   1634:                                        }
                   1635:                                } else {
                   1636:                                        telnet_printf("invalid parameter number\n");
                   1637:                                }
                   1638:                        } else if(stricmp(params[0], "INTBC") == 0) {
                   1639:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1640:                                        memset(&int_break_point, 0, sizeof(int_break_point_t));
                   1641:                                } else if(num >= 2) {
                   1642:                                        for(int i = 1; i < num; i++) {
                   1643:                                                int index = debugger_get_val(params[i]);
                   1644:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1645:                                                        telnet_printf("invalid index %x\n", index);
                   1646:                                                } else {
                   1647:                                                        int_break_point.table[index - 1].int_num = 0;
                   1648:                                                        int_break_point.table[index - 1].ah = 0;
                   1649:                                                        int_break_point.table[index - 1].ah_registered = 0;
                   1650:                                                        int_break_point.table[index - 1].al = 0;
                   1651:                                                        int_break_point.table[index - 1].al_registered = 0;
                   1652:                                                        int_break_point.table[index - 1].status = 0;
                   1653:                                                }
                   1654:                                        }
                   1655:                                } else {
                   1656:                                        telnet_printf("invalid parameter number\n");
                   1657:                                }
                   1658:                        } else if(stricmp(params[0], "INTBD") == 0 || stricmp(params[0], "INTBE") == 0) {
                   1659:                                bool enabled = (params[0][strlen(params[0]) - 1] == 'E' || params[0][strlen(params[0]) - 1] == 'e');
                   1660:                                if(num == 2 && (stricmp(params[1], "*") == 0 || stricmp(params[1], "ALL") == 0)) {
                   1661:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1662:                                                if(int_break_point.table[i].status != 0) {
                   1663:                                                        int_break_point.table[i].status = enabled ? 1 : -1;
                   1664:                                                }
                   1665:                                        }
                   1666:                                } else if(num >= 2) {
                   1667:                                        for(int i = 1; i < num; i++) {
                   1668:                                                int index = debugger_get_val(params[i]);
                   1669:                                                if(!(index >= 1 && index <= MAX_BREAK_POINTS)) {
                   1670:                                                        telnet_printf("invalid index %x\n", index);
                   1671:                                                } else if(int_break_point.table[index - 1].status == 0) {
                   1672:                                                        telnet_printf("break point %x is null\n", index);
                   1673:                                                } else {
                   1674:                                                        int_break_point.table[index - 1].status = enabled ? 1 : -1;
                   1675:                                                }
                   1676:                                        }
                   1677:                                } else {
                   1678:                                        telnet_printf("invalid parameter number\n");
                   1679:                                }
                   1680:                        } else if(stricmp(params[0], "INTBL") == 0) {
                   1681:                                if(num == 1) {
                   1682:                                        for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   1683:                                                if(int_break_point.table[i].status) {
                   1684:                                                        telnet_printf("%d %c %02X", i + 1, int_break_point.table[i].status == 1 ? 'e' : 'd', int_break_point.table[i].int_num);
                   1685:                                                        if(int_break_point.table[i].ah_registered) {
                   1686:                                                                telnet_printf(" %02X", int_break_point.table[i].ah);
                   1687:                                                        }
                   1688:                                                        if(int_break_point.table[i].al_registered) {
                   1689:                                                                telnet_printf(" %02X", int_break_point.table[i].al);
                   1690:                                                        }
                   1691:                                                        telnet_printf("\n");
                   1692:                                                }
                   1693:                                        }
                   1694:                                } else {
                   1695:                                        telnet_printf("invalid parameter number\n");
                   1696:                                }
                   1697:                        } else if(stricmp(params[0], "G") == 0 || stricmp(params[0], "P") == 0) {
                   1698:                                if(num == 1 || num == 2) {
                   1699:                                        break_point_t break_point_stored;
                   1700:                                        bool break_points_stored = false;
                   1701:                                        
                   1702:                                        if(stricmp(params[0], "P") == 0) {
                   1703:                                                memcpy(&break_point_stored, &break_point, sizeof(break_point_t));
                   1704:                                                memset(&break_point, 0, sizeof(break_point_t));
                   1705:                                                break_points_stored = true;
                   1706:                                                
                   1707:                                                break_point.table[0].addr = (SREG(CS) << 4) + m_eip + debugger_dasm(buffer, SREG(CS), m_eip);
                   1708:                                                break_point.table[0].status = 1;
                   1709:                                        } else if(num >= 2) {
                   1710:                                                memcpy(&break_point_stored, &break_point, sizeof(break_point_t));
                   1711:                                                memset(&break_point, 0, sizeof(break_point_t));
                   1712:                                                break_points_stored = true;
                   1713:                                                
                   1714:                                                UINT32 seg = debugger_get_seg(params[1], SREG(CS));
                   1715:                                                UINT32 ofs = debugger_get_ofs(params[1]);
                   1716:                                                break_point.table[0].addr = (seg << 4) + ofs;
                   1717:                                                break_point.table[0].seg = seg;
                   1718:                                                break_point.table[0].ofs = ofs;
                   1719:                                                break_point.table[0].status = 1;
                   1720:                                        }
                   1721:                                        break_point.hit = rd_break_point.hit = wr_break_point.hit = in_break_point.hit = out_break_point.hit = int_break_point.hit = 0;
                   1722:                                        now_going = true;
                   1723:                                        now_suspended = false;
                   1724:                                        
                   1725:                                        telnet_command("\033[2l"); // key unlock
                   1726:                                        while(!m_halted && !now_suspended) {
                   1727:                                                if(telnet_kbhit()) {
                   1728:                                                        break;
                   1729:                                                }
                   1730:                                                Sleep(10);
                   1731:                                        }
                   1732:                                        now_going = false;
                   1733:                                        telnet_command("\033[2h"); // key lock
                   1734:                                        
                   1735:                                        if(!(break_point.hit || rd_break_point.hit || wr_break_point.hit || in_break_point.hit || out_break_point.hit || int_break_point.hit)) {
                   1736:                                                Sleep(100);
                   1737:                                                if(!m_halted && !now_suspended) {
                   1738:                                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1739:                                                        telnet_printf("waiting until cpu is suspended...\n");
                   1740:                                                }
                   1741:                                        }
                   1742:                                        while(!m_halted && !now_suspended) {
                   1743:                                                if(telnet_disconnected()) {
                   1744:                                                        break;
                   1745:                                                }
                   1746:                                                Sleep(10);
                   1747:                                        }
                   1748:                                        dasm_seg = SREG(CS);
                   1749:                                        dasm_ofs = m_eip;
                   1750:                                        
                   1751:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1752:                                        debugger_dasm(buffer, m_prev_cs, m_prev_eip);
                   1753:                                        telnet_printf("done\t%04X:%04X  %s\n", m_prev_cs, m_prev_eip, buffer);
                   1754:                                        
                   1755:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1756:                                        debugger_regs_info(buffer);
                   1757:                                        telnet_printf("%s", buffer);
                   1758:                                        
                   1759:                                        if(break_point.hit) {
                   1760:                                                if(stricmp(params[0], "G") == 0 && num == 1) {
                   1761:                                                        telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1762:                                                        telnet_printf("breaked at %04X:%04X: break point is hit\n", SREG(CS), m_eip);
                   1763:                                                }
                   1764:                                        } else if(rd_break_point.hit) {
                   1765:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1766:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1767:                                                rd_break_point.table[rd_break_point.hit - 1].seg, rd_break_point.table[rd_break_point.hit - 1].ofs,
                   1768:                                                m_prev_cs, m_prev_eip);
                   1769:                                        } else if(wr_break_point.hit) {
                   1770:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1771:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1772:                                                wr_break_point.table[wr_break_point.hit - 1].seg, wr_break_point.table[wr_break_point.hit - 1].ofs,
                   1773:                                                m_prev_cs, m_prev_eip);
                   1774:                                        } else if(in_break_point.hit) {
                   1775:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1776:                                                telnet_printf("breaked at %04X:%04X: port %04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1777:                                                in_break_point.table[in_break_point.hit - 1].addr,
                   1778:                                                m_prev_cs, m_prev_eip);
                   1779:                                        } else if(out_break_point.hit) {
                   1780:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1781:                                                telnet_printf("breaked at %04X:%04X: port %04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1782:                                                out_break_point.table[out_break_point.hit - 1].addr,
                   1783:                                                m_prev_cs, m_prev_eip);
                   1784:                                        } else if(int_break_point.hit) {
                   1785:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1786:                                                telnet_printf("breaked at %04X:%04X: INT %02x", SREG(CS), m_eip, int_break_point.table[int_break_point.hit - 1].int_num);
                   1787:                                                if(int_break_point.table[int_break_point.hit - 1].ah_registered) {
                   1788:                                                        telnet_printf(" AH=%02x", int_break_point.table[int_break_point.hit - 1].ah);
                   1789:                                                }
                   1790:                                                if(int_break_point.table[int_break_point.hit - 1].al_registered) {
                   1791:                                                        telnet_printf(" AL=%02x", int_break_point.table[int_break_point.hit - 1].al);
                   1792:                                                }
                   1793:                                                telnet_printf(" is raised at %04X:%04X\n", m_prev_cs, m_prev_eip);
                   1794:                                        } else {
                   1795:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1796:                                                telnet_printf("breaked at %04X:%04X: enter key was pressed\n", SREG(CS), m_eip);
                   1797:                                        }
                   1798:                                        if(break_points_stored) {
                   1799:                                                memcpy(&break_point, &break_point_stored, sizeof(break_point_t));
                   1800:                                        }
                   1801:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1802:                                        debugger_dasm(buffer, SREG(CS), m_eip);
                   1803:                                        telnet_printf("next\t%04X:%04X  %s\n", SREG(CS), m_eip, buffer);
                   1804:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1805:                                } else {
                   1806:                                        telnet_printf("invalid parameter number\n");
                   1807:                                }
                   1808:                        } else if(stricmp(params[0], "T") == 0) {
                   1809:                                if(num == 1 || num == 2) {
                   1810:                                        int steps = 1;
                   1811:                                        if(num >= 2) {
                   1812:                                                steps = debugger_get_val(params[1]);
                   1813:                                        }
                   1814:                                        
                   1815:                                        telnet_command("\033[2l"); // key unlock
                   1816:                                        while(steps-- > 0) {
                   1817:                                                break_point.hit = rd_break_point.hit = wr_break_point.hit = in_break_point.hit = out_break_point.hit = int_break_point.hit = 0;
                   1818:                                                now_going = false;
                   1819:                                                now_suspended = false;
                   1820:                                                
                   1821:                                                while(!m_halted && !now_suspended) {
                   1822:                                                        if(telnet_disconnected()) {
                   1823:                                                                break;
                   1824:                                                        }
                   1825:                                                        Sleep(10);
                   1826:                                                }
                   1827:                                                dasm_seg = SREG(CS);
                   1828:                                                dasm_ofs = m_eip;
                   1829:                                                
                   1830:                                                telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1831:                                                debugger_dasm(buffer, m_prev_cs, m_prev_eip);
                   1832:                                                telnet_printf("done\t%04X:%04X  %s\n", m_prev_cs, m_prev_eip, buffer);
                   1833:                                                
                   1834:                                                telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1835:                                                debugger_regs_info(buffer);
                   1836:                                                telnet_printf("%s", buffer);
                   1837:                                                
                   1838:                                                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()) {
                   1839:                                                        break;
                   1840:                                                }
                   1841:                                        }
                   1842:                                        telnet_command("\033[2h"); // key lock
                   1843:                                        
                   1844:                                        if(!(break_point.hit || rd_break_point.hit || wr_break_point.hit || in_break_point.hit || out_break_point.hit || int_break_point.hit)) {
                   1845:                                                Sleep(100);
                   1846:                                                if(!m_halted && !now_suspended) {
                   1847:                                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1848:                                                        telnet_printf("waiting until cpu is suspended...\n");
                   1849:                                                }
                   1850:                                        }
                   1851:                                        while(!m_halted && !now_suspended) {
                   1852:                                                if(telnet_disconnected()) {
                   1853:                                                        break;
                   1854:                                                }
                   1855:                                                Sleep(10);
                   1856:                                        }
                   1857:                                        if(break_point.hit) {
                   1858:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1859:                                                telnet_printf("breaked at %04X:%04X: break point is hit\n", SREG(CS), m_eip);
                   1860:                                        } else if(rd_break_point.hit) {
                   1861:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1862:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1863:                                                rd_break_point.table[rd_break_point.hit - 1].seg, rd_break_point.table[rd_break_point.hit - 1].ofs,
                   1864:                                                m_prev_cs, m_prev_eip);
                   1865:                                        } else if(wr_break_point.hit) {
                   1866:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1867:                                                telnet_printf("breaked at %04X:%04X: memory %04X:%04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1868:                                                wr_break_point.table[wr_break_point.hit - 1].seg, wr_break_point.table[wr_break_point.hit - 1].ofs,
                   1869:                                                m_prev_cs, m_prev_eip);
                   1870:                                        } else if(in_break_point.hit) {
                   1871:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1872:                                                telnet_printf("breaked at %04X:%04X: port %04X was read at %04X:%04X\n", SREG(CS), m_eip,
                   1873:                                                in_break_point.table[in_break_point.hit - 1].addr,
                   1874:                                                m_prev_cs, m_prev_eip);
                   1875:                                        } else if(out_break_point.hit) {
                   1876:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1877:                                                telnet_printf("breaked at %04X:%04X: port %04X was written at %04X:%04X\n", SREG(CS), m_eip,
                   1878:                                                out_break_point.table[out_break_point.hit - 1].addr,
                   1879:                                                m_prev_cs, m_prev_eip);
                   1880:                                        } else if(int_break_point.hit) {
                   1881:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1882:                                                telnet_printf("breaked at %04X:%04X: INT %02x", SREG(CS), m_eip, int_break_point.table[int_break_point.hit - 1].int_num);
                   1883:                                                if(int_break_point.table[int_break_point.hit - 1].ah_registered) {
                   1884:                                                        telnet_printf(" AH=%02x", int_break_point.table[int_break_point.hit - 1].ah);
                   1885:                                                }
                   1886:                                                if(int_break_point.table[int_break_point.hit - 1].al_registered) {
                   1887:                                                        telnet_printf(" AL=%02x", int_break_point.table[int_break_point.hit - 1].al);
                   1888:                                                }
                   1889:                                                telnet_printf(" is raised at %04X:%04X\n", m_prev_cs, m_prev_eip);
                   1890:                                        } else if(steps > 0) {
                   1891:                                                telnet_set_color(TELNET_RED | TELNET_INTENSITY);
                   1892:                                                telnet_printf("breaked at %04X:%04X: enter key was pressed\n", SREG(CS), m_eip);
                   1893:                                        }
                   1894:                                        telnet_set_color(TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1895:                                        debugger_dasm(buffer, SREG(CS), m_eip);
                   1896:                                        telnet_printf("next\t%04X:%04X  %s\n", SREG(CS), m_eip, buffer);
                   1897:                                        telnet_set_color(TELNET_RED | TELNET_GREEN | TELNET_BLUE | TELNET_INTENSITY);
                   1898:                                } else {
                   1899:                                        telnet_printf("invalid parameter number\n");
                   1900:                                }
                   1901:                        } else if(stricmp(params[0], "Q") == 0) {
                   1902:                                break;
                   1903:                        } else if(stricmp(params[0], "X") == 0) {
                   1904:                                debugger_process_info(buffer);
                   1905:                                telnet_printf("%s", buffer);
                   1906:                        } else if(stricmp(params[0], ">") == 0) {
                   1907:                                if(num == 2) {
                   1908:                                        if(fp_debugger != NULL) {
                   1909:                                                fclose(fp_debugger);
                   1910:                                                fp_debugger = NULL;
                   1911:                                        }
                   1912:                                        fp_debugger = fopen(params[1], "w");
                   1913:                                } else {
                   1914:                                        telnet_printf("invalid parameter number\n");
                   1915:                                }
                   1916:                        } else if(stricmp(params[0], "<") == 0) {
                   1917:                                if(num == 2) {
                   1918:                                        if(fi_debugger != NULL) {
                   1919:                                                fclose(fi_debugger);
                   1920:                                                fi_debugger = NULL;
                   1921:                                        }
                   1922:                                        fi_debugger = fopen(params[1], "r");
                   1923:                                } else {
                   1924:                                        telnet_printf("invalid parameter number\n");
                   1925:                                }
                   1926:                        } else if(stricmp(params[0], "?") == 0) {
                   1927:                                telnet_printf("D [<start> [<end>]] - dump memory\n");
                   1928:                                telnet_printf("E[{B,W,D}] <address> <list> - edit memory (byte,word,dword)\n");
                   1929:                                telnet_printf("EA <address> \"<value>\" - edit memory (ascii)\n");
                   1930:                                telnet_printf("I[{B,W,D}] <port> - input port (byte,word,dword)\n");
                   1931:                                telnet_printf("O[{B,W,D}] <port> <value> - output port (byte,word,dword)\n");
                   1932:                                
                   1933:                                telnet_printf("R - show registers\n");
                   1934:                                telnet_printf("R <reg> <value> - edit register\n");
                   1935:                                telnet_printf("S <start> <end> <list> - search\n");
                   1936:                                telnet_printf("U [<start> [<end>]] - unassemble\n");
                   1937:                                
                   1938:                                telnet_printf("H <value> <value> - hexadd\n");
                   1939:                                
                   1940:                                telnet_printf("BP <address> - set breakpoint\n");
                   1941:                                telnet_printf("{R,W}BP <address> - set breakpoint (break at memory access)\n");
                   1942:                                telnet_printf("{I,O}BP <port> - set breakpoint (break at i/o access)\n");
                   1943:                                telnet_printf("INTBP <num> [<ah> [<al>]]- set breakpoint (break at interrupt)\n");
                   1944:                                telnet_printf("[{R,W,I,O,INT}]B{C,D,E} {*,<list>} - clear/disable/enable breakpoint(s)\n");
                   1945:                                telnet_printf("[{R,W,I,O,INT}]BL - list breakpoint(s)\n");
                   1946:                                
                   1947:                                telnet_printf("G - go (press enter key to break)\n");
                   1948:                                telnet_printf("G <address> - go and break at address\n");
                   1949:                                telnet_printf("P - trace one opcode (step over)\n");
                   1950:                                telnet_printf("T [<count>] - trace (step in)\n");
                   1951:                                telnet_printf("Q - quit\n");
                   1952:                                telnet_printf("X - show dos process info\n");
                   1953:                                
                   1954:                                telnet_printf("> <filename> - output logfile\n");
                   1955:                                telnet_printf("< <filename> - input commands from file\n");
                   1956:                                
                   1957:                                telnet_printf("<value> - hexa, decimal(%%d), ascii('a')\n");
                   1958:                                telnet_printf("<list> - <value> [<value> [<value> [...]]]\n");
                   1959:                        } else {
                   1960:                                telnet_printf("unknown command %s\n", params[0]);
                   1961:                        }
                   1962:                }
                   1963:        }
                   1964:        if(fp_debugger != NULL) {
                   1965:                fclose(fp_debugger);
                   1966:                fp_debugger = NULL;
                   1967:        }
                   1968:        if(fi_debugger != NULL) {
                   1969:                fclose(fi_debugger);
                   1970:                fi_debugger = NULL;
                   1971:        }
                   1972:        now_debugging = now_going = now_suspended = force_suspend = false;
                   1973:        closesocket(cli_socket);
                   1974: }
                   1975: 
                   1976: const char *debugger_get_ttermpro_path()
                   1977: {
                   1978:        static char path[MAX_PATH] = {0};
                   1979:        
                   1980:        if(getenv("ProgramFiles")) {
                   1981:                sprintf(path, "%s\\teraterm\\ttermpro.exe", getenv("ProgramFiles"));
                   1982:        }
                   1983:        return(path);
                   1984: }
                   1985: 
                   1986: const char *debugger_get_ttermpro_x86_path()
                   1987: {
                   1988:        static char path[MAX_PATH] = {0};
                   1989:        
                   1990:        if(getenv("ProgramFiles(x86)")) {
                   1991:                sprintf(path, "%s\\teraterm\\ttermpro.exe", getenv("ProgramFiles(x86)"));
                   1992:        }
                   1993:        return(path);
                   1994: }
                   1995: 
                   1996: const char *debugger_get_putty_path()
                   1997: {
                   1998:        static char path[MAX_PATH] = {0};
                   1999:        
                   2000:        if(getenv("ProgramFiles")) {
                   2001:                sprintf(path, "%s\\PuTTY\\putty.exe", getenv("ProgramFiles"));
                   2002:        }
                   2003:        return(path);
                   2004: }
                   2005: 
                   2006: const char *debugger_get_putty_x86_path()
                   2007: {
                   2008:        static char path[MAX_PATH] = {0};
                   2009:        
                   2010:        if(getenv("ProgramFiles(x86)")) {
                   2011:                sprintf(path, "%s\\PuTTY\\putty.exe", getenv("ProgramFiles(x86)"));
                   2012:        }
                   2013:        return(path);
                   2014: }
                   2015: 
                   2016: const char *debugger_get_telnet_path()
                   2017: {
                   2018:        // NOTE: When you run 32bit version of msdos.exe on Windows x64,
                   2019:        // C:\Windows\System32\telnet.exe is redirected to telnet.exe in SysWOW64.
                   2020:        // But 32bit version of telnet.exe will not be installed in SysWOW64
                   2021:        // and 64bit version of telnet.exe will be installed in System32.
                   2022:        static char path[MAX_PATH] = {0};
                   2023:        
                   2024:        if(getenv("windir") != NULL) {
                   2025:                sprintf(path, "%s\\System32\\telnet.exe", getenv("windir"));
                   2026:        }
                   2027:        return(path);
                   2028: }
                   2029: 
                   2030: DWORD WINAPI debugger_thread(LPVOID)
                   2031: {
                   2032:        WSADATA was_data;
                   2033:        struct sockaddr_in svr_addr;
                   2034:        struct sockaddr_in cli_addr;
                   2035:        int cli_addr_len = sizeof(cli_addr);
                   2036:        int port = 23;
                   2037:        int bind_stat = SOCKET_ERROR;
                   2038:        struct timeval timeout;
                   2039:        
                   2040:        WSAStartup(MAKEWORD(2,0), &was_data);
                   2041:        
                   2042:        if((svr_socket = socket(AF_INET, SOCK_STREAM, 0)) != INVALID_SOCKET) {
                   2043:                memset(&svr_addr, 0, sizeof(svr_addr));
                   2044:                svr_addr.sin_family = AF_INET;
                   2045:                svr_addr.sin_addr.s_addr = htonl(INADDR_ANY);
                   2046:                
                   2047:                while(!m_halted && port < 10000) {
                   2048:                        svr_addr.sin_port = htons(port);
                   2049:                        if((bind_stat = bind(svr_socket, (struct sockaddr *)&svr_addr, sizeof(svr_addr))) == 0) {
                   2050:                                break;
                   2051:                        } else {
                   2052:                                port = (port == 23) ? 9000 : (port + 1);
                   2053:                        }
                   2054:                }
                   2055:                if(bind_stat == 0) {
                   2056:                        timeout.tv_sec = 1;
                   2057:                        timeout.tv_usec = 0;
                   2058:                        setsockopt(svr_socket, SOL_SOCKET, SO_RCVTIMEO, (char*)&timeout, sizeof(timeout));
                   2059:                        
                   2060:                        listen(svr_socket, 1);
                   2061:                        
                   2062:                        char command[MAX_PATH] = {0};
                   2063:                        STARTUPINFO si;
                   2064:                        PROCESS_INFORMATION pi;
                   2065:                        
                   2066:                        if(_access(debugger_get_ttermpro_path(), 0) == 0) {
                   2067:                                sprintf(command, "%s localhost:%d /T=1", debugger_get_ttermpro_path(), port);
                   2068:                        } else if(_access(debugger_get_ttermpro_x86_path(), 0) == 0) {
                   2069:                                sprintf(command, "%s localhost:%d /T=1", debugger_get_ttermpro_x86_path(), port);
                   2070:                        } else if(_access(debugger_get_putty_path(), 0) == 0) {
                   2071:                                sprintf(command, "%s -telnet localhost %d", debugger_get_putty_path(), port);
                   2072:                        } else if(_access(debugger_get_putty_x86_path(), 0) == 0) {
                   2073:                                sprintf(command, "%s -telnet localhost %d", debugger_get_putty_x86_path(), port);
                   2074:                        } else if(_access(debugger_get_telnet_path(), 0) == 0) {
                   2075:                                sprintf(command, "%s -t vt100 localhost %d", debugger_get_telnet_path(), port);
                   2076:                        }
                   2077:                        if(command[0] != '\0') {
                   2078:                                memset(&si, 0, sizeof(STARTUPINFO));
                   2079:                                memset(&pi, 0, sizeof(PROCESS_INFORMATION));
                   2080:                                CreateProcess(NULL, command, NULL, NULL, FALSE, CREATE_NEW_CONSOLE, NULL, NULL, &si, &pi);
                   2081:                        }
                   2082:                        
                   2083:                        while(!m_halted) {
                   2084:                                if((cli_socket = accept(svr_socket, (struct sockaddr *) &cli_addr, &cli_addr_len)) != INVALID_SOCKET) {
                   2085:                                        u_long val = 1;
                   2086:                                        ioctlsocket(cli_socket, FIONBIO, &val);
                   2087:                                        debugger_main();
                   2088:                                }
                   2089:                        }
                   2090:                }
                   2091:        }
                   2092:        WSACleanup();
                   2093:        return(0);
                   2094: }
                   2095: #endif
                   2096: 
                   2097: /* ----------------------------------------------------------------------------
1.1       root     2098:        main
                   2099: ---------------------------------------------------------------------------- */
                   2100: 
1.1.1.28  root     2101: BOOL WINAPI ctrl_handler(DWORD dwCtrlType)
                   2102: {
                   2103:        if(dwCtrlType == CTRL_BREAK_EVENT) {
1.1.1.33  root     2104:                if(key_buf_char != NULL && key_buf_scan != NULL) {
                   2105:                        key_buf_char->clear();
                   2106:                        key_buf_scan->clear();
                   2107:                }
                   2108: //             key_code = key_recv = 0;
1.1.1.28  root     2109:                return TRUE;
                   2110:        } else if(dwCtrlType == CTRL_C_EVENT) {
                   2111:                return TRUE;
                   2112:        } else if(dwCtrlType == CTRL_CLOSE_EVENT) {
                   2113:                // this program will be terminated abnormally, do minimum end process
                   2114:                exit_handler();
                   2115:                exit(1);
                   2116:        }
                   2117:        return FALSE;
                   2118: }
                   2119: 
                   2120: void exit_handler()
                   2121: {
                   2122:        if(temp_file_created) {
                   2123:                DeleteFile(temp_file_path);
                   2124:                temp_file_created = false;
                   2125:        }
                   2126:        if(key_buf_char != NULL) {
                   2127:                key_buf_char->release();
                   2128:                delete key_buf_char;
                   2129:                key_buf_char = NULL;
                   2130:        }
                   2131:        if(key_buf_scan != NULL) {
                   2132:                key_buf_scan->release();
                   2133:                delete key_buf_scan;
                   2134:                key_buf_scan = NULL;
                   2135:        }
1.1.1.32  root     2136: #ifdef SUPPORT_XMS
                   2137:        msdos_xms_release();
                   2138: #endif
1.1.1.28  root     2139:        hardware_release();
                   2140: }
                   2141: 
                   2142: #ifdef USE_THREAD
                   2143: DWORD WINAPI vram_thread(LPVOID)
                   2144: {
                   2145:        while(!m_halted) {
                   2146:                EnterCriticalSection(&vram_crit_sect);
                   2147:                if(vram_length_char != 0 && vram_length_char == vram_last_length_char) {
                   2148:                        vram_flush_char();
                   2149:                }
                   2150:                if(vram_length_attr != 0 && vram_length_attr == vram_last_length_attr) {
                   2151:                        vram_flush_attr();
                   2152:                }
                   2153:                vram_last_length_char = vram_length_char;
                   2154:                vram_last_length_attr = vram_length_attr;
                   2155:                LeaveCriticalSection(&vram_crit_sect);
                   2156:                // this is about half the maximum keyboard repeat rate - any
                   2157:                // lower tends to be jerky, any higher misses updates
                   2158:                Sleep(15);
                   2159:        }
                   2160:        return 0;
                   2161: }
                   2162: #endif
                   2163: 
                   2164: long get_section_in_exec_file(FILE *fp, char *name)
                   2165: {
                   2166:        UINT8 header[0x400];
                   2167:        
                   2168:        long position = ftell(fp);
                   2169:        fseek(fp, 0, SEEK_SET);
                   2170:        fread(header, sizeof(header), 1, fp);
                   2171:        fseek(fp, position, SEEK_SET);
                   2172:        
                   2173:        try {
                   2174:                _IMAGE_DOS_HEADER *dosHeader = (_IMAGE_DOS_HEADER *)(header + 0);
                   2175:                DWORD dwTopOfSignature = dosHeader->e_lfanew;
                   2176:                DWORD dwTopOfCoffHeader = dwTopOfSignature + 4;
                   2177:                _IMAGE_FILE_HEADER *coffHeader = (_IMAGE_FILE_HEADER *)(header + dwTopOfCoffHeader);
                   2178:                DWORD dwTopOfOptionalHeader = dwTopOfCoffHeader + sizeof(_IMAGE_FILE_HEADER);
                   2179:                DWORD dwTopOfFirstSectionHeader = dwTopOfOptionalHeader + coffHeader->SizeOfOptionalHeader;
                   2180:                
                   2181:                for(int i = 0; i < coffHeader->NumberOfSections; i++) {
                   2182:                        _IMAGE_SECTION_HEADER *sectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * i);
                   2183:                        if(memcmp(sectionHeader->Name, name, strlen(name)) == 0) {
                   2184:                                return(sectionHeader->PointerToRawData);
                   2185:                        }
                   2186:                }
                   2187:        } catch(...) {
                   2188:        }
                   2189:        return(0);
                   2190: }
                   2191: 
1.1.1.10  root     2192: bool is_started_from_command_prompt()
                   2193: {
1.1.1.18  root     2194:        bool ret = false;
                   2195:        
                   2196:        HMODULE hLibrary = LoadLibrary(_T("Kernel32.dll"));
                   2197:        if(hLibrary) {
                   2198:                typedef DWORD (WINAPI *GetConsoleProcessListFunction)(__out LPDWORD lpdwProcessList, __in DWORD dwProcessCount);
                   2199:                GetConsoleProcessListFunction lpfnGetConsoleProcessList;
                   2200:                lpfnGetConsoleProcessList = reinterpret_cast<GetConsoleProcessListFunction>(::GetProcAddress(hLibrary, "GetConsoleProcessList"));
                   2201:                if(lpfnGetConsoleProcessList) {
                   2202:                        DWORD pl;
                   2203:                        ret = (lpfnGetConsoleProcessList(&pl, 1) > 1);
                   2204:                        FreeLibrary(hLibrary);
                   2205:                        return(ret);
                   2206:                }
                   2207:                FreeLibrary(hLibrary);
                   2208:        }
                   2209:        
                   2210:        HANDLE hSnapshot = CreateToolhelp32Snapshot(TH32CS_SNAPPROCESS, 0);
                   2211:        if(hSnapshot != INVALID_HANDLE_VALUE) {
                   2212:                DWORD dwParentProcessID = 0;
                   2213:                PROCESSENTRY32 pe32;
                   2214:                pe32.dwSize = sizeof(PROCESSENTRY32);
                   2215:                if(Process32First(hSnapshot, &pe32)) {
                   2216:                        do {
                   2217:                                if(pe32.th32ProcessID == GetCurrentProcessId()) {
                   2218:                                        dwParentProcessID = pe32.th32ParentProcessID;
                   2219:                                        break;
                   2220:                                }
                   2221:                        } while(Process32Next(hSnapshot, &pe32));
                   2222:                }
                   2223:                CloseHandle(hSnapshot);
                   2224:                if(dwParentProcessID != 0) {
                   2225:                        HANDLE hProcess = OpenProcess(PROCESS_QUERY_INFORMATION | PROCESS_VM_READ, FALSE, dwParentProcessID);
                   2226:                        if(hProcess != NULL) {
                   2227:                                HMODULE hMod;
                   2228:                                DWORD cbNeeded;
                   2229:                                if(EnumProcessModules(hProcess, &hMod, sizeof(hMod), &cbNeeded)) {
                   2230:                                        char module_name[MAX_PATH];
                   2231:                                        if(GetModuleBaseName(hProcess, hMod, module_name, sizeof(module_name))) {
                   2232:                                                ret = (_strnicmp(module_name, "cmd.exe", 7) == 0);
                   2233:                                        }
                   2234:                                }
                   2235:                                CloseHandle(hProcess);
                   2236:                        }
                   2237:                }
                   2238:        }
                   2239:        return(ret);
1.1.1.14  root     2240: }
                   2241: 
                   2242: BOOL is_greater_windows_version(DWORD dwMajorVersion, DWORD dwMinorVersion, WORD wServicePackMajor, WORD wServicePackMinor)
                   2243: {
1.1.1.24  root     2244:        // https://msdn.microsoft.com/en-us/library/windows/desktop/ms725491(v=vs.85).aspx
1.1.1.14  root     2245:        OSVERSIONINFOEX osvi;
                   2246:        DWORDLONG dwlConditionMask = 0;
                   2247:        int op = VER_GREATER_EQUAL;
                   2248:        
                   2249:        // Initialize the OSVERSIONINFOEX structure.
                   2250:        ZeroMemory(&osvi, sizeof(OSVERSIONINFOEX));
                   2251:        osvi.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
                   2252:        osvi.dwMajorVersion = dwMajorVersion;
                   2253:        osvi.dwMinorVersion = dwMinorVersion;
                   2254:        osvi.wServicePackMajor = wServicePackMajor;
                   2255:        osvi.wServicePackMinor = wServicePackMinor;
                   2256:        
                   2257:         // Initialize the condition mask.
                   2258:        VER_SET_CONDITION( dwlConditionMask, VER_MAJORVERSION, op );
                   2259:        VER_SET_CONDITION( dwlConditionMask, VER_MINORVERSION, op );
                   2260:        VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMAJOR, op );
                   2261:        VER_SET_CONDITION( dwlConditionMask, VER_SERVICEPACKMINOR, op );
                   2262:        
                   2263:        // Perform the test.
                   2264:        return VerifyVersionInfo(&osvi, VER_MAJORVERSION | VER_MINORVERSION | VER_SERVICEPACKMAJOR | VER_SERVICEPACKMINOR, dwlConditionMask);
                   2265: }
                   2266: 
1.1.1.27  root     2267: void get_sio_port_numbers()
                   2268: {
                   2269:        SP_DEVINFO_DATA DeviceInfoData = {sizeof(SP_DEVINFO_DATA)};
                   2270:        HDEVINFO hDevInfo = 0;
                   2271:        HKEY hKey = 0;
                   2272:        if((hDevInfo = SetupDiGetClassDevs(&GUID_DEVINTERFACE_COMPORT, NULL, NULL, (DIGCF_PRESENT | DIGCF_DEVICEINTERFACE))) != 0) {
                   2273:                for(int i = 0; SetupDiEnumDeviceInfo(hDevInfo, i, &DeviceInfoData); i++) {
                   2274:                        if((hKey = SetupDiOpenDevRegKey(hDevInfo, &DeviceInfoData, DICS_FLAG_GLOBAL, 0, DIREG_DEV, KEY_QUERY_VALUE)) != INVALID_HANDLE_VALUE) {
                   2275:                                char chData[256];
                   2276:                                DWORD dwType = 0;
                   2277:                                DWORD dwSize = sizeof(chData);
                   2278:                                int port_number = 0;
                   2279:                                
                   2280:                                if(RegQueryValueEx(hKey, _T("PortName"), NULL, &dwType, (BYTE *)chData, &dwSize) == ERROR_SUCCESS) {
                   2281:                                        if(_strnicmp(chData, "COM", 3) == 0) {
                   2282:                                                port_number = atoi(chData + 3);
                   2283:                                        }
                   2284:                                }
                   2285:                                RegCloseKey(hKey);
                   2286:                                
1.1.1.29  root     2287:                                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     2288:                                        continue;
                   2289:                                }
                   2290:                                if(sio_port_number[0] == 0) {
                   2291:                                        sio_port_number[0] = port_number;
                   2292:                                } else if(sio_port_number[1] == 0) {
                   2293:                                        sio_port_number[1] = port_number;
1.1.1.29  root     2294:                                } else if(sio_port_number[2] == 0) {
                   2295:                                        sio_port_number[2] = port_number;
                   2296:                                } else if(sio_port_number[3] == 0) {
                   2297:                                        sio_port_number[3] = port_number;
1.1.1.27  root     2298:                                }
1.1.1.29  root     2299:                                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     2300:                                        break;
                   2301:                                }
                   2302:                        }
                   2303:                }
                   2304:        }
                   2305: }
                   2306: 
1.1.1.28  root     2307: #define IS_NUMERIC(c) ((c) >= '0' && (c) <= '9')
                   2308: 
1.1       root     2309: int main(int argc, char *argv[], char *envp[])
                   2310: {
1.1.1.9   root     2311:        int arg_offset = 0;
                   2312:        int standard_env = 0;
1.1.1.14  root     2313:        int buf_width = 0, buf_height = 0;
1.1.1.28  root     2314:        bool get_console_info_success = false;
                   2315:        bool screen_size_changed = false;
                   2316:        
                   2317:        _TCHAR path[MAX_PATH], full[MAX_PATH], *name = NULL;
                   2318:        GetModuleFileName(NULL, path, MAX_PATH);
                   2319:        GetFullPathName(path, MAX_PATH, full, &name);
1.1       root     2320:        
1.1.1.27  root     2321:        char dummy_argv_0[] = "msdos.exe";
                   2322:        char dummy_argv_1[MAX_PATH];
                   2323:        char *dummy_argv[256] = {dummy_argv_0, dummy_argv_1, 0};
                   2324:        char new_exec_file[MAX_PATH];
                   2325:        bool convert_cmd_file = false;
1.1.1.28  root     2326:        unsigned int code_page = 0;
1.1.1.27  root     2327:        
                   2328:        if(name != NULL && stricmp(name, "msdos.exe") != 0) {
1.1.1.28  root     2329:                // check if command file is embedded to this execution file
                   2330:                // if this execution file name is msdos.exe, don't check
1.1.1.27  root     2331:                FILE* fp = fopen(full, "rb");
1.1.1.28  root     2332:                long offset = get_section_in_exec_file(fp, ".msdos");
                   2333:                if(offset != 0) {
1.1.1.30  root     2334:                        UINT8 buffer[16];
1.1.1.28  root     2335:                        fseek(fp, offset, SEEK_SET);
                   2336:                        fread(buffer, sizeof(buffer), 1, fp);
                   2337:                        
                   2338:                        // restore flags
                   2339:                        stay_busy           = ((buffer[0] & 0x01) != 0);
                   2340:                        no_windows          = ((buffer[0] & 0x02) != 0);
                   2341:                        standard_env        = ((buffer[0] & 0x04) != 0);
                   2342:                        ignore_illegal_insn = ((buffer[0] & 0x08) != 0);
                   2343:                        limit_max_memory    = ((buffer[0] & 0x10) != 0);
                   2344:                        if((buffer[0] & 0x20) != 0) {
                   2345:                                get_sio_port_numbers();
                   2346:                        }
                   2347:                        if((buffer[0] & 0x40) != 0) {
                   2348:                                UMB_TOP = EMS_TOP + EMS_SIZE;
                   2349:                                support_ems = true;
1.1.1.30  root     2350:                        }
1.1.1.27  root     2351: #ifdef SUPPORT_XMS
1.1.1.30  root     2352:                        if((buffer[0] & 0x80) != 0) {
1.1.1.28  root     2353:                                support_xms = true;
                   2354:                        }
1.1.1.30  root     2355: #endif
1.1.1.28  root     2356:                        if((buffer[1] != 0 || buffer[2] != 0) && (buffer[3] != 0 || buffer[4] != 0)) {
                   2357:                                buf_width  = buffer[1] | (buffer[2] << 8);
                   2358:                                buf_height = buffer[3] | (buffer[4] << 8);
                   2359:                        }
                   2360:                        if(buffer[5] != 0) {
1.1.1.30  root     2361:                                dos_major_version = buffer[5];
                   2362:                                dos_minor_version = buffer[6];
                   2363:                        }
                   2364:                        if(buffer[7] != 0) {
                   2365:                                win_major_version = buffer[7];
                   2366:                                win_minor_version = buffer[8];
1.1.1.28  root     2367:                        }
1.1.1.30  root     2368:                        if((code_page = buffer[9] | (buffer[10] << 8)) != 0) {
1.1.1.28  root     2369:                                SetConsoleCP(code_page);
                   2370:                                SetConsoleOutputCP(code_page);
                   2371:                        }
1.1.1.30  root     2372:                        int name_len = buffer[11];
                   2373:                        int file_len = buffer[12] | (buffer[13] << 8) | (buffer[14] << 16) | (buffer[15] << 24);
1.1.1.28  root     2374:                        
                   2375:                        // restore command file name
                   2376:                        memset(dummy_argv_1, 0, sizeof(dummy_argv_1));
                   2377:                        fread(dummy_argv_1, name_len, 1, fp);
                   2378:                        
                   2379:                        if(name_len != 0 && _access(dummy_argv_1, 0) == 0) {
                   2380:                                // if original command file exists, create a temporary file name
                   2381:                                if(GetTempFileName(_T("."), _T("DOS"), 0, dummy_argv_1) != 0) {
                   2382:                                        // create a temporary command file in the current director
                   2383:                                        DeleteFile(dummy_argv_1);
1.1.1.27  root     2384:                                } else {
1.1.1.28  root     2385:                                        // create a temporary command file in the temporary folder
                   2386:                                        GetTempPath(MAX_PATH, path);
                   2387:                                        if(GetTempFileName(path, _T("DOS"), 0, dummy_argv_1) != 0) {
                   2388:                                                DeleteFile(dummy_argv_1);
                   2389:                                        } else {
                   2390:                                                sprintf(dummy_argv_1, "%s$DOSPRG$.TMP", path);
                   2391:                                        }
1.1.1.27  root     2392:                                }
1.1.1.28  root     2393:                                // check the command file type
                   2394:                                fread(buffer, 2, 1, fp);
                   2395:                                fseek(fp, -2, SEEK_CUR);
                   2396:                                if(memcmp(buffer, "MZ", 2) != 0) {
                   2397:                                        memcpy(dummy_argv_1 + strlen(dummy_argv_1) - 4, ".COM", 4);
                   2398:                                } else {
                   2399:                                        memcpy(dummy_argv_1 + strlen(dummy_argv_1) - 4, ".EXE", 4);
1.1.1.27  root     2400:                                }
                   2401:                        }
1.1.1.28  root     2402:                        
                   2403:                        // restore command file
                   2404:                        FILE* fo = fopen(dummy_argv_1, "wb");
                   2405:                        for(int i = 0; i < file_len; i++) {
                   2406:                                fputc(fgetc(fp), fo);
                   2407:                        }
                   2408:                        fclose(fo);
                   2409:                        
                   2410:                        GetFullPathName(dummy_argv_1, MAX_PATH, temp_file_path, NULL);
                   2411:                        temp_file_created = true;
                   2412:                        SetFileAttributes(temp_file_path, FILE_ATTRIBUTE_HIDDEN);
                   2413:                        
                   2414:                        // adjust argc/argv
                   2415:                        for(int i = 1; i < argc && (i + 1) < 256; i++) {
                   2416:                                dummy_argv[i + 1] = argv[i];
                   2417:                        }
                   2418:                        argc++;
                   2419:                        argv = dummy_argv;
1.1.1.27  root     2420:                }
                   2421:                fclose(fp);
                   2422:        }
1.1.1.9   root     2423:        for(int i = 1; i < argc; i++) {
1.1.1.25  root     2424:                if(_strnicmp(argv[i], "-b", 2) == 0) {
                   2425:                        stay_busy = true;
                   2426:                        arg_offset++;
1.1.1.27  root     2427:                } else if(_strnicmp(argv[i], "-c", 2) == 0) {
                   2428:                        if(argv[i][2] != '\0') {
                   2429:                                strcpy(new_exec_file, &argv[i][2]);
                   2430:                        } else {
                   2431:                                strcpy(new_exec_file, "new_exec_file.exe");
                   2432:                        }
                   2433:                        convert_cmd_file = true;
                   2434:                        arg_offset++;
1.1.1.28  root     2435:                } else if(_strnicmp(argv[i], "-p", 2) == 0) {
                   2436:                        if(IS_NUMERIC(argv[i][2])) {
                   2437:                                code_page = atoi(&argv[i][2]);
                   2438:                        } else {
                   2439:                                code_page = GetConsoleCP();
                   2440:                        }
                   2441:                        arg_offset++;
1.1.1.25  root     2442:                } else if(_strnicmp(argv[i], "-d", 2) == 0) {
                   2443:                        no_windows = true;
                   2444:                        arg_offset++;
                   2445:                } else if(_strnicmp(argv[i], "-e", 2) == 0) {
1.1.1.9   root     2446:                        standard_env = 1;
                   2447:                        arg_offset++;
1.1.1.14  root     2448:                } else if(_strnicmp(argv[i], "-i", 2) == 0) {
                   2449:                        ignore_illegal_insn = true;
                   2450:                        arg_offset++;
                   2451:                } else if(_strnicmp(argv[i], "-m", 2) == 0) {
                   2452:                        limit_max_memory = true;
                   2453:                        arg_offset++;
                   2454:                } else if(_strnicmp(argv[i], "-n", 2) == 0) {
1.1.1.17  root     2455:                        if(sscanf(argv[1] + 2, "%d,%d", &buf_height, &buf_width) != 2) {
                   2456:                                buf_width = buf_height = 0;
                   2457:                        }
                   2458:                        if(buf_width <= 0 || buf_width > 0x7fff) {
                   2459:                                buf_width = 80;
                   2460:                        }
                   2461:                        if(buf_height <= 0 || buf_height > 0x7fff) {
                   2462:                                buf_height = 25;
                   2463:                        }
1.1.1.14  root     2464:                        arg_offset++;
1.1.1.25  root     2465:                } else if(_strnicmp(argv[i], "-s", 2) == 0) {
1.1.1.28  root     2466:                        if(IS_NUMERIC(argv[i][2])) {
1.1.1.29  root     2467:                                char *p0 = &argv[i][2], *p1, *p2, *p3;
                   2468:                                if((p1 = strchr(p0, ',')) != NULL && IS_NUMERIC(p1[1])) {
                   2469:                                        sio_port_number[1] = atoi(p1 + 1);
                   2470:                                        if((p2 = strchr(p1, ',')) != NULL && IS_NUMERIC(p2[1])) {
                   2471:                                                sio_port_number[2] = atoi(p2 + 1);
                   2472:                                                if((p3 = strchr(p2, ',')) != NULL && IS_NUMERIC(p3[1])) {
                   2473:                                                        sio_port_number[3] = atoi(p3 + 1);
                   2474:                                                }
                   2475:                                        }
1.1.1.25  root     2476:                                }
1.1.1.29  root     2477:                                sio_port_number[0] = atoi(p0);
1.1.1.25  root     2478:                        }
1.1.1.29  root     2479:                        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     2480:                                get_sio_port_numbers();
1.1.1.25  root     2481:                        }
                   2482:                        arg_offset++;
1.1.1.9   root     2483:                } else if(_strnicmp(argv[i], "-v", 2) == 0) {
1.1.1.17  root     2484:                        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     2485:                                dos_major_version = argv[i][2] - '0';
                   2486:                                dos_minor_version = (argv[i][4] - '0') * 10 + (argv[i][5] ? (argv[i][5] - '0') : 0);
                   2487:                        }
                   2488:                        arg_offset++;
                   2489:                } else if(_strnicmp(argv[i], "-w", 2) == 0) {
                   2490:                        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]))) {
                   2491:                                win_major_version = argv[i][2] - '0';
                   2492:                                win_minor_version = (argv[i][4] - '0') * 10 + (argv[i][5] ? (argv[i][5] - '0') : 0);
1.1.1.9   root     2493:                        }
                   2494:                        arg_offset++;
1.1.1.25  root     2495:                } else if(_strnicmp(argv[i], "-x", 2) == 0) {
                   2496:                        UMB_TOP = EMS_TOP + EMS_SIZE;
                   2497:                        support_ems = true;
                   2498: #ifdef SUPPORT_XMS
                   2499:                        support_xms = true;
                   2500: #endif
                   2501:                        arg_offset++;
1.1.1.9   root     2502:                } else {
                   2503:                        break;
                   2504:                }
                   2505:        }
                   2506:        if(argc < 2 + arg_offset) {
1.1       root     2507: #ifdef _WIN64
1.1.1.14  root     2508:                fprintf(stderr, "MS-DOS Player (" CPU_MODEL_NAME(CPU_MODEL) ") for Win32-x64 console\n\n");
1.1       root     2509: #else
1.1.1.14  root     2510:                fprintf(stderr, "MS-DOS Player (" CPU_MODEL_NAME(CPU_MODEL) ") for Win32 console\n\n");
1.1       root     2511: #endif
1.1.1.25  root     2512:                fprintf(stderr,
1.1.1.28  root     2513:                        "Usage: MSDOS [-b] [-c[(new exec file)] [-p[P]]] [-d] [-e] [-i] [-m] [-n[L[,C]]]\n"
1.1.1.30  root     2514:                        "             [-s[P1[,P2[,P3[,P4]]]]] [-vX.XX] [-wX.XX] [-x] (command) [options]\n"
1.1.1.25  root     2515:                        "\n"
                   2516:                        "\t-b\tstay busy during keyboard polling\n"
1.1.1.28  root     2517: #ifdef _WIN64
1.1.1.27  root     2518:                        "\t-c\tconvert command file to 64bit execution file\n"
1.1.1.28  root     2519: #else
1.1.1.27  root     2520:                        "\t-c\tconvert command file to 32bit execution file\n"
                   2521: #endif
1.1.1.28  root     2522:                        "\t-p\trecord current code page when convert command file\n"
1.1.1.25  root     2523:                        "\t-d\tpretend running under straight DOS, not Windows\n"
                   2524:                        "\t-e\tuse a reduced environment block\n"
                   2525:                        "\t-i\tignore invalid instructions\n"
                   2526:                        "\t-m\trestrict free memory to 0x7FFF paragraphs\n"
                   2527:                        "\t-n\tcreate a new buffer (25 lines, 80 columns by default)\n"
                   2528:                        "\t-s\tenable serial I/O and set host's COM port numbers\n"
                   2529:                        "\t-v\tset the DOS version\n"
1.1.1.30  root     2530:                        "\t-w\tset the Windows version\n"
1.1.1.19  root     2531: #ifdef SUPPORT_XMS
1.1.1.28  root     2532:                        "\t-x\tenable XMS and LIM EMS\n"
1.1.1.19  root     2533: #else
1.1.1.28  root     2534:                        "\t-x\tenable LIM EMS\n"
1.1.1.19  root     2535: #endif
                   2536:                );
1.1.1.10  root     2537:                
                   2538:                if(!is_started_from_command_prompt()) {
                   2539:                        fprintf(stderr, "\nStart this program from a command prompt!\n\nHit any key to quit...");
                   2540:                        while(!_kbhit()) {
                   2541:                                Sleep(10);
                   2542:                        }
                   2543:                }
1.1.1.20  root     2544: #ifdef _DEBUG
                   2545:                _CrtDumpMemoryLeaks();
                   2546: #endif
1.1       root     2547:                return(EXIT_FAILURE);
                   2548:        }
1.1.1.27  root     2549:        if(convert_cmd_file) {
                   2550:                retval = EXIT_FAILURE;
1.1.1.28  root     2551:                if(name != NULL/* && stricmp(name, "msdos.exe") == 0*/) {
1.1.1.27  root     2552:                        FILE *fp = NULL, *fs = NULL, *fo = NULL;
1.1.1.28  root     2553:                        int len = strlen(argv[arg_offset + 1]), data;
1.1.1.27  root     2554:                        
1.1.1.28  root     2555:                        if(!(len > 4 && (stricmp(argv[arg_offset + 1] + len - 4, ".COM") == 0 || stricmp(argv[arg_offset + 1] + len - 4, ".EXE") == 0))) {
                   2556:                                fprintf(stderr, "Specify command file with extenstion (.COM or .EXE)\n");
                   2557:                        } else if((fp = fopen(full, "rb")) == NULL) {
                   2558:                                fprintf(stderr, "Can't open '%s'\n", name);
1.1.1.27  root     2559:                        } else {
1.1.1.28  root     2560:                                long offset = get_section_in_exec_file(fp, ".msdos");
                   2561:                                if(offset != 0) {
                   2562:                                        UINT8 buffer[14];
                   2563:                                        fseek(fp, offset, SEEK_SET);
                   2564:                                        fread(buffer, sizeof(buffer), 1, fp);
                   2565:                                        memset(path, 0, sizeof(path));
                   2566:                                        fread(path, buffer[9], 1, fp);
                   2567:                                        fprintf(stderr, "Command file '%s' was already embedded to '%s'\n", path, name);
                   2568:                                } else if((fs = fopen(argv[arg_offset + 1], "rb")) == NULL) {
                   2569:                                        fprintf(stderr, "Can't open '%s'\n", argv[arg_offset + 1]);
                   2570:                                } else if((fo = fopen(new_exec_file, "wb")) == NULL) {
                   2571:                                        fprintf(stderr, "Can't open '%s'\n", new_exec_file);
                   2572:                                } else {
                   2573:                                        // read pe header of msdos.exe
                   2574:                                        UINT8 header[0x400];
                   2575:                                        fseek(fp, 0, SEEK_SET);
                   2576:                                        fread(header, sizeof(header), 1, fp);
                   2577:                                        
                   2578:                                        _IMAGE_DOS_HEADER *dosHeader = (_IMAGE_DOS_HEADER *)(header + 0);
                   2579:                                        DWORD dwTopOfSignature = dosHeader->e_lfanew;
                   2580:                                        DWORD dwTopOfCoffHeader = dwTopOfSignature + 4;
                   2581:                                        _IMAGE_FILE_HEADER *coffHeader = (_IMAGE_FILE_HEADER *)(header + dwTopOfCoffHeader);
                   2582:                                        DWORD dwTopOfOptionalHeader = dwTopOfCoffHeader + sizeof(_IMAGE_FILE_HEADER);
                   2583:                                        _IMAGE_OPTIONAL_HEADER *optionalHeader = (_IMAGE_OPTIONAL_HEADER *)(header + dwTopOfOptionalHeader);
                   2584:                                        DWORD dwTopOfFirstSectionHeader = dwTopOfOptionalHeader + coffHeader->SizeOfOptionalHeader;
                   2585:                                        
                   2586:                                        _IMAGE_SECTION_HEADER *lastSectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * (coffHeader->NumberOfSections - 1));
                   2587:                                        DWORD dwEndOfFile = lastSectionHeader->PointerToRawData + lastSectionHeader->SizeOfRawData;
                   2588:                                        DWORD dwLastSectionSize = lastSectionHeader->SizeOfRawData;
                   2589:                                        DWORD dwExtraLastSectionBytes = dwLastSectionSize % optionalHeader->SectionAlignment;
                   2590:                                        if(dwExtraLastSectionBytes != 0) {
                   2591:                                                DWORD dwRemain = optionalHeader->SectionAlignment - dwExtraLastSectionBytes;
                   2592:                                                dwLastSectionSize += dwRemain;
                   2593:                                        }
                   2594:                                        DWORD dwVirtualAddress = lastSectionHeader->VirtualAddress + dwLastSectionSize;
                   2595:                                        
                   2596:                                        // store msdos.exe
                   2597:                                        fseek(fp, 0, SEEK_SET);
                   2598:                                        for(int i = 0; i < dwEndOfFile; i++) {
                   2599:                                                if((data = fgetc(fp)) != EOF) {
                   2600:                                                        fputc(data, fo);
                   2601:                                                } else {
                   2602:                                                        // we should not reach here :-(
                   2603:                                                        fputc(0, fo);
                   2604:                                                }
                   2605:                                        }
                   2606:                                        
                   2607:                                        // store options
                   2608:                                        UINT8 flags = 0;
                   2609:                                        if(stay_busy) {
                   2610:                                                flags |= 0x01;
                   2611:                                        }
                   2612:                                        if(no_windows) {
                   2613:                                                flags |= 0x02;
                   2614:                                        }
                   2615:                                        if(standard_env) {
                   2616:                                                flags |= 0x04;
                   2617:                                        }
                   2618:                                        if(ignore_illegal_insn) {
                   2619:                                                flags |= 0x08;
                   2620:                                        }
                   2621:                                        if(limit_max_memory) {
                   2622:                                                flags |= 0x10;
                   2623:                                        }
1.1.1.29  root     2624:                                        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     2625:                                                flags |= 0x20;
                   2626:                                        }
                   2627:                                        if(support_ems) {
                   2628:                                                flags |= 0x40;
                   2629:                                        }
1.1.1.30  root     2630: #ifdef SUPPORT_XMS
                   2631:                                        if(support_xms) {
                   2632:                                                flags |= 0x80;
                   2633:                                        }
                   2634: #endif
1.1.1.28  root     2635:                                        
                   2636:                                        fputc(flags, fo);
                   2637:                                        fputc((buf_width  >> 0) & 0xff, fo);
                   2638:                                        fputc((buf_width  >> 8) & 0xff, fo);
                   2639:                                        fputc((buf_height >> 0) & 0xff, fo);
                   2640:                                        fputc((buf_height >> 8) & 0xff, fo);
1.1.1.30  root     2641:                                        fputc(dos_major_version, fo);
                   2642:                                        fputc(dos_minor_version, fo);
                   2643:                                        fputc(win_major_version, fo);
                   2644:                                        fputc(win_minor_version, fo);
1.1.1.28  root     2645:                                        fputc((code_page >> 0) & 0xff, fo);
                   2646:                                        fputc((code_page >> 8) & 0xff, fo);
                   2647:                                        
                   2648:                                        // store command file info
                   2649:                                        GetFullPathName(argv[arg_offset + 1], MAX_PATH, full, &name);
                   2650:                                        int name_len = strlen(name);
                   2651:                                        fseek(fs, 0, SEEK_END);
                   2652:                                        long file_size = ftell(fs);
                   2653:                                        
                   2654:                                        fputc(name_len, fo);
                   2655:                                        fputc((file_size >>  0) & 0xff, fo);
                   2656:                                        fputc((file_size >>  8) & 0xff, fo);
                   2657:                                        fputc((file_size >> 16) & 0xff, fo);
                   2658:                                        fputc((file_size >> 24) & 0xff, fo);
                   2659:                                        fwrite(name, name_len, 1, fo);
                   2660:                                        
                   2661:                                        // store command file
                   2662:                                        fseek(fs, 0, SEEK_SET);
                   2663:                                        for(int i = 0; i < file_size; i++) {
                   2664:                                                if((data = fgetc(fs)) != EOF) {
                   2665:                                                        fputc(data, fo);
                   2666:                                                } else {
                   2667:                                                        // we should not reach here :-(
                   2668:                                                        fputc(0, fo);
                   2669:                                                }
                   2670:                                        }
                   2671:                                        
                   2672:                                        // store padding data and update pe header
1.1.1.29  root     2673:                                        _IMAGE_SECTION_HEADER *newSectionHeader = (_IMAGE_SECTION_HEADER *)(header + dwTopOfFirstSectionHeader + IMAGE_SIZEOF_SECTION_HEADER * coffHeader->NumberOfSections);
                   2674:                                        coffHeader->NumberOfSections++;
                   2675:                                        memset(newSectionHeader, 0, IMAGE_SIZEOF_SECTION_HEADER);
                   2676:                                        memcpy(newSectionHeader->Name, ".msdos", 6);
                   2677:                                        newSectionHeader->VirtualAddress = dwVirtualAddress;
                   2678:                                        newSectionHeader->PointerToRawData = dwEndOfFile;
                   2679:                                        newSectionHeader->Characteristics = IMAGE_SCN_CNT_INITIALIZED_DATA | IMAGE_SCN_MEM_DISCARDABLE;
1.1.1.28  root     2680:                                        newSectionHeader->SizeOfRawData = 14 + name_len + file_size;
                   2681:                                        DWORD dwExtraRawBytes = newSectionHeader->SizeOfRawData % optionalHeader->FileAlignment;
                   2682:                                        if(dwExtraRawBytes != 0) {
1.1.1.29  root     2683:                                                static const char padding[] = "PADDINGXXPADDING";
1.1.1.28  root     2684:                                                DWORD dwRemain = optionalHeader->FileAlignment - dwExtraRawBytes;
                   2685:                                                for(int i = 0; i < dwRemain; i++) {
1.1.1.29  root     2686:                                                        if(i < 2) {
                   2687:                                                                fputc(padding[i & 15], fo);
                   2688:                                                        } else {
                   2689:                                                                fputc(padding[(i - 2) & 15], fo);
                   2690:                                                        }
1.1.1.28  root     2691:                                                }
                   2692:                                                newSectionHeader->SizeOfRawData += dwRemain;
                   2693:                                        }
                   2694:                                        newSectionHeader->Misc.VirtualSize = newSectionHeader->SizeOfRawData;
                   2695:                                        
                   2696:                                        DWORD dwNewSectionSize = newSectionHeader->SizeOfRawData;
                   2697:                                        DWORD dwExtraNewSectionBytes = dwNewSectionSize % optionalHeader->SectionAlignment;
                   2698:                                        if(dwExtraNewSectionBytes != 0) {
                   2699:                                                DWORD dwRemain = optionalHeader->SectionAlignment - dwExtraNewSectionBytes;
                   2700:                                                dwNewSectionSize += dwRemain;
                   2701:                                        }
                   2702:                                        optionalHeader->SizeOfImage += dwNewSectionSize;
                   2703:                                        
                   2704:                                        fseek(fo, 0, SEEK_SET);
                   2705:                                        fwrite(header, sizeof(header), 1, fo);
                   2706:                                        
                   2707:                                        fprintf(stderr, "'%s' is successfully created\n", new_exec_file);
                   2708:                                        retval = EXIT_SUCCESS;
1.1.1.27  root     2709:                                }
                   2710:                        }
                   2711:                        if(fp != NULL) {
                   2712:                                fclose(fp);
                   2713:                        }
                   2714:                        if(fs != NULL) {
                   2715:                                fclose(fs);
                   2716:                        }
                   2717:                        if(fo != NULL) {
                   2718:                                fclose(fo);
                   2719:                        }
                   2720:                }
                   2721: #ifdef _DEBUG
                   2722:                _CrtDumpMemoryLeaks();
                   2723: #endif
                   2724:                return(retval);
                   2725:        }
1.1       root     2726:        
1.1.1.14  root     2727:        is_vista_or_later = is_greater_windows_version(6, 0, 0, 0);
                   2728:        
1.1.1.23  root     2729:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     2730:        CONSOLE_SCREEN_BUFFER_INFO csbi;
1.1.1.14  root     2731:        CONSOLE_CURSOR_INFO ci;
1.1.1.23  root     2732:        
1.1.1.28  root     2733:        get_console_info_success = (GetConsoleScreenBufferInfo(hStdout, &csbi) != 0);
1.1.1.14  root     2734:        GetConsoleCursorInfo(hStdout, &ci);
1.1.1.24  root     2735:        GetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), &dwConsoleMode);
1.1       root     2736:        
1.1.1.14  root     2737:        for(int y = 0; y < SCR_BUF_WIDTH; y++) {
                   2738:                for(int x = 0; x < SCR_BUF_HEIGHT; x++) {
                   2739:                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   2740:                        SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1       root     2741:                }
                   2742:        }
1.1.1.28  root     2743:        if(get_console_info_success) {
1.1.1.12  root     2744:                scr_width = csbi.dwSize.X;
1.1.1.14  root     2745:                scr_height = csbi.srWindow.Bottom - csbi.srWindow.Top + 1;
                   2746:                
1.1.1.28  root     2747:                // v-text shadow buffer size must be lesser than 0x7fd0
                   2748:                if((scr_width > SCR_BUF_WIDTH) || (scr_height > SCR_BUF_HEIGHT) || (scr_width * scr_height * 2 > 0x7fd0) ||
1.1.1.14  root     2749:                   (buf_width != 0 && buf_width != scr_width) || (buf_height != 0 && buf_height != scr_height)) {
                   2750:                        scr_width = min(buf_width != 0 ? buf_width : scr_width, SCR_BUF_WIDTH);
                   2751:                        scr_height = min(buf_height != 0 ? buf_height : scr_height, SCR_BUF_HEIGHT);
1.1.1.28  root     2752:                        if(scr_width * scr_height * 2 > 0x7fd0) {
1.1.1.14  root     2753:                                scr_width = 80;
                   2754:                                scr_height = 25;
                   2755:                        }
1.1.1.28  root     2756:                        screen_size_changed = true;
1.1.1.14  root     2757:                }
1.1.1.12  root     2758:        } else {
                   2759:                // for a proof (not a console)
                   2760:                scr_width = 80;
                   2761:                scr_height = 25;
                   2762:        }
1.1.1.14  root     2763:        scr_buf_size.X = scr_width;
                   2764:        scr_buf_size.Y = scr_height;
                   2765:        scr_buf_pos.X = scr_buf_pos.Y = 0;
                   2766:        scr_top = csbi.srWindow.Top;
1.1       root     2767:        cursor_moved = false;
                   2768:        
1.1.1.25  root     2769:        key_buf_char = new FIFO(256);
                   2770:        key_buf_scan = new FIFO(256);
1.1       root     2771:        
                   2772:        hardware_init();
                   2773:        
1.1.1.33  root     2774: #ifdef USE_DEBUGGER
                   2775:        debugger_init();
                   2776: #endif
                   2777:        
1.1.1.9   root     2778:        if(msdos_init(argc - (arg_offset + 1), argv + (arg_offset + 1), envp, standard_env)) {
1.1       root     2779:                retval = EXIT_FAILURE;
                   2780:        } else {
1.1.1.27  root     2781: #if defined(_MSC_VER) && (_MSC_VER >= 1400)
1.1.1.14  root     2782:                _set_invalid_parameter_handler((_invalid_parameter_handler)ignore_invalid_parameters);
                   2783: #endif
                   2784:                SetConsoleCtrlHandler(ctrl_handler, TRUE);
                   2785:                
1.1.1.28  root     2786:                if(screen_size_changed) {
1.1.1.24  root     2787:                        change_console_size(scr_width, scr_height);
                   2788:                }
1.1.1.8   root     2789:                TIMECAPS caps;
                   2790:                timeGetDevCaps(&caps, sizeof(TIMECAPS));
                   2791:                timeBeginPeriod(caps.wPeriodMin);
1.1.1.14  root     2792: #ifdef USE_THREAD
                   2793:                InitializeCriticalSection(&vram_crit_sect);
                   2794:                CloseHandle(CreateThread(NULL, 4096, vram_thread, NULL, 0, NULL));
                   2795: #endif
1.1.1.33  root     2796: #ifdef USE_DEBUGGER
                   2797:                CloseHandle(CreateThread(NULL, 0, debugger_thread, NULL, 0, NULL));
                   2798:                // wait until telnet client starts and connects to me
                   2799:                if(_access(debugger_get_ttermpro_path(), 0) == 0 ||
                   2800:                   _access(debugger_get_ttermpro_x86_path(), 0) == 0 ||
                   2801:                   _access(debugger_get_putty_path(), 0) == 0 ||
                   2802:                   _access(debugger_get_putty_x86_path(), 0) == 0 ||
                   2803:                   _access(debugger_get_telnet_path(), 0) == 0) {
                   2804:                        for(int i = 0; i < 100 && cli_socket == 0; i++) {
                   2805:                                Sleep(100);
                   2806:                        }
                   2807:                }
                   2808: #endif
1.1       root     2809:                hardware_run();
1.1.1.14  root     2810: #ifdef USE_THREAD
                   2811:                vram_flush();
                   2812:                DeleteCriticalSection(&vram_crit_sect);
                   2813: #endif
1.1.1.24  root     2814:                timeEndPeriod(caps.wPeriodMin);
1.1.1.14  root     2815:                
1.1.1.24  root     2816:                // hStdin/hStdout (and all handles) will be closed in msdos_finish()...
1.1.1.28  root     2817:                if(get_console_info_success) {
1.1.1.23  root     2818:                        hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.12  root     2819:                        if(restore_console_on_exit) {
1.1.1.14  root     2820:                                // window can't be bigger than buffer,
                   2821:                                // buffer can't be smaller than window,
                   2822:                                // so make a tiny window,
                   2823:                                // set the required buffer,
                   2824:                                // then set the required window
                   2825:                                SMALL_RECT rect;
                   2826:                                SET_RECT(rect, 0, csbi.srWindow.Top, 0, csbi.srWindow.Top);
                   2827:                                SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.12  root     2828:                                SetConsoleScreenBufferSize(hStdout, csbi.dwSize);
1.1.1.14  root     2829:                                SET_RECT(rect, 0, 0, csbi.srWindow.Right - csbi.srWindow.Left, csbi.srWindow.Bottom - csbi.srWindow.Top);
1.1.1.12  root     2830:                                SetConsoleWindowInfo(hStdout, TRUE, &rect);
                   2831:                        }
1.1.1.14  root     2832:                        SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   2833:                        SetConsoleCursorInfo(hStdout, &ci);
1.1.1.12  root     2834:                }
1.1.1.24  root     2835:                SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode);
                   2836:                
1.1       root     2837:                msdos_finish();
1.1.1.14  root     2838:                
                   2839:                SetConsoleCtrlHandler(ctrl_handler, FALSE);
1.1       root     2840:        }
                   2841:        
1.1.1.10  root     2842:        hardware_finish();
                   2843:        
1.1.1.28  root     2844:        if(key_buf_char != NULL) {
                   2845:                key_buf_char->release();
                   2846:                delete key_buf_char;
                   2847:                key_buf_char = NULL;
                   2848:        }
                   2849:        if(key_buf_scan != NULL) {
                   2850:                key_buf_scan->release();
                   2851:                delete key_buf_scan;
                   2852:                key_buf_scan = NULL;
                   2853:        }
                   2854:        if(temp_file_created) {
                   2855:                DeleteFile(temp_file_path);
                   2856:                temp_file_created = false;
                   2857:        }
                   2858: //     if(argv == dummy_argv) {
                   2859: //             if(!is_started_from_command_prompt()) {
                   2860: //                     fprintf(stderr, "\nHit any key to quit...");
                   2861: //                     while(!_kbhit()) {
                   2862: //                             Sleep(10);
                   2863: //                     }
                   2864: //             }
                   2865: //     }
1.1.1.20  root     2866: #ifdef _DEBUG
                   2867:        _CrtDumpMemoryLeaks();
                   2868: #endif
1.1       root     2869:        return(retval);
                   2870: }
                   2871: 
1.1.1.20  root     2872: /* ----------------------------------------------------------------------------
                   2873:        console
                   2874: ---------------------------------------------------------------------------- */
                   2875: 
1.1.1.14  root     2876: void change_console_size(int width, int height)
1.1.1.12  root     2877: {
1.1.1.23  root     2878:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.12  root     2879:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   2880:        SMALL_RECT rect;
                   2881:        COORD co;
                   2882:        
                   2883:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.14  root     2884:        if(csbi.srWindow.Top != 0 || csbi.dwCursorPosition.Y > height - 1) {
                   2885:                if(csbi.srWindow.Right - csbi.srWindow.Left + 1 == width && csbi.srWindow.Bottom - csbi.srWindow.Top + 1 == height) {
                   2886:                        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &csbi.srWindow);
                   2887:                        SET_RECT(rect, 0, 0, width - 1, height - 1);
                   2888:                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   2889:                } else if(csbi.dwCursorPosition.Y > height - 1) {
                   2890:                        SET_RECT(rect, 0, csbi.dwCursorPosition.Y - (height - 1), width - 1, csbi.dwCursorPosition.Y);
                   2891:                        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   2892:                        SET_RECT(rect, 0, 0, width - 1, height - 1);
                   2893:                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1.1.12  root     2894:                }
                   2895:        }
1.1.1.14  root     2896:        if(csbi.dwCursorPosition.Y > height - 1) {
1.1.1.12  root     2897:                co.X = csbi.dwCursorPosition.X;
1.1.1.14  root     2898:                co.Y = min(height - 1, csbi.dwCursorPosition.Y - csbi.srWindow.Top);
1.1.1.12  root     2899:                SetConsoleCursorPosition(hStdout, co);
                   2900:                cursor_moved = true;
                   2901:        }
1.1.1.14  root     2902:        
                   2903:        // window can't be bigger than buffer,
                   2904:        // buffer can't be smaller than window,
                   2905:        // so make a tiny window,
                   2906:        // set the required buffer,
                   2907:        // then set the required window
                   2908:        SET_RECT(rect, 0, csbi.srWindow.Top, 0, csbi.srWindow.Top);
1.1.1.12  root     2909:        SetConsoleWindowInfo(hStdout, TRUE, &rect);
1.1.1.14  root     2910:        co.X = width;
                   2911:        co.Y = height;
1.1.1.12  root     2912:        SetConsoleScreenBufferSize(hStdout, co);
1.1.1.14  root     2913:        SET_RECT(rect, 0, 0, width - 1, height - 1);
                   2914:        SetConsoleWindowInfo(hStdout, TRUE, &rect);
                   2915:        
                   2916:        scr_width = scr_buf_size.X = width;
                   2917:        scr_height = scr_buf_size.Y = height;
                   2918:        scr_top = 0;
                   2919:        
                   2920:        clear_scr_buffer(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
                   2921:        
                   2922:        int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.15  root     2923:        text_vram_end_address = text_vram_top_address + regen;
                   2924:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
                   2925:        
1.1.1.14  root     2926:        if(regen > 0x4000) {
                   2927:                regen = 0x8000;
                   2928:                vram_pages = 1;
                   2929:        } else if(regen > 0x2000) {
                   2930:                regen = 0x4000;
                   2931:                vram_pages = 2;
                   2932:        } else if(regen > 0x1000) {
                   2933:                regen = 0x2000;
                   2934:                vram_pages = 4;
                   2935:        } else {
                   2936:                regen = 0x1000;
                   2937:                vram_pages = 8;
                   2938:        }
1.1.1.15  root     2939:        *(UINT16 *)(mem + 0x44a) = scr_width;
                   2940:        *(UINT16 *)(mem + 0x44c) = regen;
                   2941:        *(UINT8  *)(mem + 0x484) = scr_height - 1;
                   2942:        
1.1.1.24  root     2943:        mouse.min_position.x = 0;
                   2944:        mouse.min_position.y = 0;
1.1.1.34! root     2945:        mouse.max_position.x = 8 * (scr_width  - 1);
        !          2946:        mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24  root     2947:        
1.1.1.15  root     2948:        restore_console_on_exit = true;
1.1.1.14  root     2949: }
                   2950: 
                   2951: void clear_scr_buffer(WORD attr)
                   2952: {
                   2953:        for(int y = 0; y < scr_height; y++) {
                   2954:                for(int x = 0; x < scr_width; x++) {
                   2955:                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   2956:                        SCR_BUF(y,x).Attributes = attr;
                   2957:                }
                   2958:        }
1.1.1.12  root     2959: }
                   2960: 
1.1.1.24  root     2961: bool update_console_input()
1.1       root     2962: {
1.1.1.23  root     2963:        HANDLE hStdin = GetStdHandle(STD_INPUT_HANDLE);
1.1.1.8   root     2964:        DWORD dwNumberOfEvents = 0;
1.1       root     2965:        DWORD dwRead;
                   2966:        INPUT_RECORD ir[16];
1.1.1.24  root     2967:        CONSOLE_SCREEN_BUFFER_INFO csbi = {0};
                   2968:        bool result = false;
1.1       root     2969:        
1.1.1.8   root     2970:        if(GetNumberOfConsoleInputEvents(hStdin, &dwNumberOfEvents) && dwNumberOfEvents != 0) {
                   2971:                if(ReadConsoleInputA(hStdin, ir, 16, &dwRead)) {
                   2972:                        for(int i = 0; i < dwRead; i++) {
1.1.1.24  root     2973:                                if(ir[i].EventType & MOUSE_EVENT) {
1.1.1.34! root     2974:                                        if(mouse.hidden == 0) {
        !          2975:                                                // NOTE: if restore_console_on_exit, console is not scrolled
        !          2976:                                                if(!restore_console_on_exit && csbi.srWindow.Bottom == 0) {
        !          2977:                                                        GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
        !          2978:                                                }
        !          2979:                                                // FIXME: character size is always 8x8 ???
        !          2980:                                                int x = 8 * (ir[i].Event.MouseEvent.dwMousePosition.X);
        !          2981:                                                int y = 8 * (ir[i].Event.MouseEvent.dwMousePosition.Y - csbi.srWindow.Top);
        !          2982:                                                
        !          2983:                                                if(mouse.position.x != x || mouse.position.y != y) {
        !          2984:                                                        mouse.position.x = x;
        !          2985:                                                        mouse.position.y = y;
        !          2986:                                                        mouse.status |= 1;
        !          2987:                                                }
        !          2988:                                        }
        !          2989:                                        if(ir[i].Event.MouseEvent.dwEventFlags == 0) {
        !          2990:                                                for(int i = 0; i < MAX_MOUSE_BUTTONS; i++) {
        !          2991:                                                        static const DWORD bits[] = {
        !          2992:                                                                FROM_LEFT_1ST_BUTTON_PRESSED,   // left
        !          2993:                                                                RIGHTMOST_BUTTON_PRESSED,       // right
        !          2994:                                                                FROM_LEFT_2ND_BUTTON_PRESSED,   // middle
        !          2995:                                                        };
        !          2996:                                                        bool prev_status = mouse.buttons[i].status;
        !          2997:                                                        mouse.buttons[i].status = ((ir[i].Event.MouseEvent.dwButtonState & bits[i]) != 0);
        !          2998:                                                        
        !          2999:                                                        if(!prev_status && mouse.buttons[i].status) {
        !          3000:                                                                mouse.buttons[i].pressed_times++;
        !          3001:                                                                mouse.buttons[i].pressed_position.x = mouse.position.x;
        !          3002:                                                                mouse.buttons[i].pressed_position.y = mouse.position.x;
        !          3003:                                                                mouse.status |= 2 << (i * 2);
        !          3004:                                                        } else if(prev_status && !mouse.buttons[i].status) {
        !          3005:                                                                mouse.buttons[i].released_times++;
        !          3006:                                                                mouse.buttons[i].released_position.x = mouse.position.x;
        !          3007:                                                                mouse.buttons[i].released_position.y = mouse.position.x;
        !          3008:                                                                mouse.status |= 4 << (i * 2);
1.1.1.14  root     3009:                                                        }
                   3010:                                                }
                   3011:                                        }
1.1.1.24  root     3012:                                } else if(ir[i].EventType & KEY_EVENT) {
1.1.1.33  root     3013:                                        // update keyboard flags in bios data area
                   3014:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
                   3015:                                                mem[0x417] |= 0x04;
                   3016:                                        } else {
                   3017:                                                mem[0x417] &= ~0x04;
                   3018:                                        }
                   3019:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & LEFT_CTRL_PRESSED) {
                   3020:                                                mem[0x418] |= 0x01;
                   3021:                                        } else {
                   3022:                                                mem[0x418] &= ~0x01;
                   3023:                                        }
                   3024:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
                   3025:                                                mem[0x417] |= 0x08;
                   3026:                                        } else {
                   3027:                                                mem[0x417] &= ~0x08;
                   3028:                                        }
                   3029:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & LEFT_ALT_PRESSED) {
                   3030:                                                mem[0x418] |= 0x02;
                   3031:                                        } else {
                   3032:                                                mem[0x418] &= ~0x02;
                   3033:                                        }
                   3034:                                        if(ir[i].Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) {
                   3035:                                                if(!(mem[0x417] & 0x03)) {
                   3036:                                                        mem[0x417] |= 0x02; // left shift
                   3037:                                                }
                   3038:                                        } else {
                   3039:                                                mem[0x417] &= ~0x03;
                   3040:                                        }
                   3041:                                        
1.1.1.28  root     3042:                                        // set scan code of last pressed/release key to kbd_data (in-port 60h)
1.1.1.24  root     3043:                                        kbd_data = ir[i].Event.KeyEvent.wVirtualScanCode;
1.1.1.33  root     3044:                                        kbd_status |= 1;
                   3045:                                        
                   3046:                                        // update dos key buffer
                   3047:                                        UINT8 chr = ir[i].Event.KeyEvent.uChar.AsciiChar;
                   3048:                                        UINT8 scn = ir[i].Event.KeyEvent.wVirtualScanCode & 0xff;
                   3049:                                        
                   3050:                                        if(ir[i].Event.KeyEvent.bKeyDown) {
1.1.1.28  root     3051:                                                // make
1.1.1.24  root     3052:                                                kbd_data &= 0x7f;
                   3053:                                                
1.1.1.33  root     3054:                                                if(chr == 0x00) {
1.1.1.24  root     3055:                                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
                   3056:                                                                if(scn >= 0x3b && scn <= 0x44) {
                   3057:                                                                        scn += 0x68 - 0x3b;     // F1 to F10
                   3058:                                                                } else if(scn == 0x57 || scn == 0x58) {
                   3059:                                                                        scn += 0x8b - 0x57;     // F11 & F12
                   3060:                                                                } else if(scn >= 0x47 && scn <= 0x53) {
                   3061:                                                                        scn += 0x97 - 0x47;     // edit/arrow clusters
                   3062:                                                                } else if(scn == 0x35) {
                   3063:                                                                        scn = 0xa4;             // keypad /
                   3064:                                                                }
                   3065:                                                        } else if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED)) {
                   3066:                                                                if(scn == 0x07) {
                   3067:                                                                        chr = 0x1e;     // Ctrl+^
                   3068:                                                                } else if(scn == 0x0c) {
                   3069:                                                                        chr = 0x1f;     // Ctrl+_
                   3070:                                                                } else if(scn >= 0x35 && scn <= 0x58) {
                   3071:                                                                        static const UINT8 ctrl_map[] = {
                   3072:                                                                                0x95,   // keypad /
                   3073:                                                                                0,
                   3074:                                                                                0x96,   // keypad *
                   3075:                                                                                0, 0, 0,
                   3076:                                                                                0x5e,   // F1
                   3077:                                                                                0x5f,   // F2
                   3078:                                                                                0x60,   // F3
                   3079:                                                                                0x61,   // F4
                   3080:                                                                                0x62,   // F5
                   3081:                                                                                0x63,   // F6
                   3082:                                                                                0x64,   // F7
                   3083:                                                                                0x65,   // F8
                   3084:                                                                                0x66,   // F9
                   3085:                                                                                0x67,   // F10
                   3086:                                                                                0,
                   3087:                                                                                0,
                   3088:                                                                                0x77,   // Home
                   3089:                                                                                0x8d,   // Up
                   3090:                                                                                0x84,   // PgUp
                   3091:                                                                                0x8e,   // keypad -
                   3092:                                                                                0x73,   // Left
                   3093:                                                                                0x8f,   // keypad center
                   3094:                                                                                0x74,   // Right
                   3095:                                                                                0x90,   // keyapd +
                   3096:                                                                                0x75,   // End
                   3097:                                                                                0x91,   // Down
                   3098:                                                                                0x76,   // PgDn
                   3099:                                                                                0x92,   // Insert
                   3100:                                                                                0x93,   // Delete
                   3101:                                                                                0, 0, 0,
                   3102:                                                                                0x89,   // F11
                   3103:                                                                                0x8a,   // F12
                   3104:                                                                        };
                   3105:                                                                        scn = ctrl_map[scn - 0x35];
                   3106:                                                                }
                   3107:                                                        } else if(ir[i].Event.KeyEvent.dwControlKeyState & SHIFT_PRESSED) {
                   3108:                                                                if(scn >= 0x3b && scn <= 0x44) {
                   3109:                                                                        scn += 0x54 - 0x3b;     // F1 to F10
                   3110:                                                                } else if(scn == 0x57 || scn == 0x58) {
                   3111:                                                                        scn += 0x87 - 0x57;     // F11 & F12
                   3112:                                                                }
                   3113:                                                        } else if(scn == 0x57 || scn == 0x58) {
                   3114:                                                                scn += 0x85 - 0x57;
                   3115:                                                        }
                   3116:                                                        // ignore shift, ctrl, alt, win and menu keys
                   3117:                                                        if(scn != 0x1d && scn != 0x2a && scn != 0x36 && scn != 0x38 && (scn < 0x5b || scn > 0x5e)) {
1.1.1.32  root     3118:                                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
                   3119:                                                                        if(chr == 0) {
                   3120:                                                                                key_buf_char->write(0x00);
                   3121:                                                                                key_buf_scan->write(ir[i].Event.KeyEvent.dwControlKeyState & ENHANCED_KEY ? 0xe0 : 0x00);
                   3122:                                                                        }
                   3123:                                                                        key_buf_char->write(chr);
                   3124:                                                                        key_buf_scan->write(scn);
1.1.1.24  root     3125:                                                                }
                   3126:                                                        }
                   3127:                                                } else {
                   3128:                                                        if(ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_ALT_PRESSED | RIGHT_ALT_PRESSED)) {
                   3129:                                                                chr = 0;
                   3130:                                                                if(scn >= 0x02 && scn <= 0x0e) {
                   3131:                                                                        scn += 0x78 - 0x02;     // 1 to 0 - =
                   3132:                                                                }
                   3133:                                                        }
1.1.1.32  root     3134:                                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   3135:                                                                key_buf_char->write(chr);
                   3136:                                                                key_buf_scan->write(scn);
                   3137:                                                        }
1.1.1.24  root     3138:                                                }
1.1.1.33  root     3139:                                        } else if(chr == 0x03 && (ir[i].Event.KeyEvent.dwControlKeyState & (LEFT_CTRL_PRESSED | RIGHT_CTRL_PRESSED))) {
                   3140:                                                // ctrl-break, ctrl-c
                   3141:                                                if(scn == 0x46) {
                   3142:                                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   3143:                                                                key_buf_char->write(0x00);
                   3144:                                                                key_buf_scan->write(0x00);
                   3145:                                                        }
                   3146:                                                        ctrl_break_pressed = true;
                   3147:                                                        mem[0x471] = 0x80;
                   3148:                                                        raise_int_1bh = true;
                   3149:                                                } else {
                   3150:                                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   3151:                                                                key_buf_char->write(chr);
                   3152:                                                                key_buf_scan->write(scn);
                   3153:                                                        }
                   3154:                                                        ctrl_c_pressed = (scn == 0x2e);
                   3155:                                                }
                   3156:                                        } else {
                   3157:                                                // break
                   3158:                                                kbd_data |= 0x80;
1.1       root     3159:                                        }
1.1.1.24  root     3160:                                        result = key_changed = true;
1.1       root     3161:                                }
                   3162:                        }
                   3163:                }
                   3164:        }
1.1.1.24  root     3165:        return(result);
1.1.1.8   root     3166: }
                   3167: 
1.1.1.14  root     3168: bool update_key_buffer()
1.1.1.8   root     3169: {
1.1.1.32  root     3170:        return(update_console_input() || (key_buf_char != NULL && key_buf_char->count() != 0));
1.1.1.8   root     3171: }
                   3172: 
1.1.1.20  root     3173: /* ----------------------------------------------------------------------------
                   3174:        MS-DOS virtual machine
                   3175: ---------------------------------------------------------------------------- */
                   3176: 
1.1.1.32  root     3177: static const struct {
1.1.1.33  root     3178:        char *name;
                   3179:        DWORD lcid;
                   3180:        char *std;
                   3181:        char *dlt;
                   3182: } tz_table[] = {
                   3183:        // https://science.ksc.nasa.gov/software/winvn/userguide/3_1_4.htm
                   3184: //     0       GMT             Greenwich Mean Time             GMT0
                   3185:        {"GMT Standard Time",                   0x0809, "GMT", "BST"},          // (UTC+00:00) GB London (en-gb)
                   3186:        {"GMT Standard Time",                   0x1809, "GMT", "IST"},          // (UTC+00:00) IE Dublin (en-ie)
                   3187:        {"GMT Standard Time",                   0x0000, "WET", "WES"},          // (UTC+00:00) PT Lisbon
                   3188:        {"Greenwich Standard Time",             0x0000, "GMT", "GST"},          // (UTC+00:00) IS Reykjavik
                   3189: //     2       FST     FDT     Fernando De Noronha Std         FST2FDT
                   3190:        {"Mid-Atlantic Standard Time",          0x0416, "FST", "FDT"},          // (UTC-02:00) BR Noronha (pt-br)
                   3191:        {"UTC-02",                              0x0416, "FST", "FDT"},          // (UTC-02:00) BR Noronha (pt-br)
                   3192: //     3       BST             Brazil Standard Time            BST3
                   3193:        {"Bahia Standard Time",                 0x0000, "BST", "BDT"},          // (UTC-03:00) BR Bahia
                   3194:        {"SA Eastern Standard Time",            0x0000, "BST", "BDT"},          // (UTC-03:00) BR Fortaleza
                   3195:        {"Tocantins Standard Time",             0x0000, "BST", "BDT"},          // (UTC-03:00) BR Palmas
                   3196: //     3       EST     EDT     Eastern Standard (Brazil)       EST3EDT
                   3197:        {"E. South America Standard Time",      0x0000, "EST", "EDT"},          // (UTC-03:00) BR Sao Paulo
                   3198: //     3       GST             Greenland Standard Time         GST3
                   3199:        {"Greenland Standard Time",             0x0000, "GST", "GDT"},          // (UTC-03:00) GL Godthab
                   3200: //     3:30    NST     NDT     Newfoundland Standard Time      NST3:30NDT
                   3201:        {"Newfoundland Standard Time",          0x0000, "NST", "NDT"},          // (UTC-03:30) CA St.Johns
                   3202: //     4       AST     ADT     Atlantic Standard Time          AST4ADT
                   3203:        {"Atlantic Standard Time",              0x0000, "AST", "ADT"},          // (UTC-04:00) CA Halifax
                   3204: //     4       WST     WDT     Western Standard (Brazil)       WST4WDT
                   3205:        {"Central Brazilian Standard Time",     0x0000, "WST", "WDT"},          // (UTC-04:00) BR Cuiaba
                   3206:        {"SA Western Standard Time",            0x0000, "WST", "WDT"},          // (UTC-04:00) BR Manaus
                   3207: //     5       EST     EDT     Eastern Standard Time           EST5EDT
                   3208:        {"Eastern Standard Time",               0x0000, "EST", "EDT"},          // (UTC-05:00) US New York
                   3209:        {"Eastern Standard Time (Mexico)",      0x0000, "EST", "EDT"},          // (UTC-05:00) MX Cancun
                   3210:        {"US Eastern Standard Time",            0x0000, "EST", "EDT"},          // (UTC-05:00) US Indianapolis
                   3211: //     5       CST     CDT     Chile Standard Time             CST5CDT
                   3212:        {"Pacific SA Standard Time",            0x0000, "CST", "CDT"},          // (UTC-04:00) CL Santiago
                   3213: //     5       AST     ADT     Acre Standard Time              AST5ADT
                   3214:        {"SA Pacific Standard Time",            0x0000, "AST", "ADT"},          // (UTC-05:00) BR Rio Branco
                   3215: //     5       CST     CDT     Cuba Standard Time              CST5CDT
                   3216:        {"Cuba Standard Time",                  0x0000, "CST", "CDT"},          // (UTC-05:00) CU Havana
                   3217: //     6       CST     CDT     Central Standard Time           CST6CDT
                   3218:        {"Canada Central Standard Time",        0x0000, "CST", "CDT"},          // (UTC-06:00) CA Regina
                   3219:        {"Central Standard Time",               0x0000, "CST", "CDT"},          // (UTC-06:00) US Chicago
                   3220:        {"Central Standard Time (Mexico)",      0x0000, "CST", "CDT"},          // (UTC-06:00) MX Mexico City
                   3221: //     6       EST     EDT     Easter Island Standard          EST6EDT
                   3222:        {"Easter Island Standard Time",         0x0000, "EST", "EDT"},          // (UTC-06:00) CL Easter
                   3223: //     7       MST     MDT     Mountain Standard Time          MST7MDT
                   3224:        {"Mountain Standard Time",              0x0000, "MST", "MDT"},          // (UTC-07:00) US Denver
                   3225:        {"Mountain Standard Time (Mexico)",     0x0000, "MST", "MDT"},          // (UTC-07:00) MX Chihuahua
                   3226:        {"US Mountain Standard Time",           0x0000, "MST", "MDT"},          // (UTC-07:00) US Phoenix
                   3227: //     8       PST     PDT     Pacific Standard Time           PST8PDT
                   3228:        {"Pacific Standard Time",               0x0000, "PST", "PDT"},          // (UTC-08:00) US Los Angeles
                   3229:        {"Pacific Standard Time (Mexico)",      0x0000, "PST", "PDT"},          // (UTC-08:00) MX Tijuana
                   3230: //     9       AKS     AKD     Alaska Standard Time            AKS9AKD
                   3231: //     9       YST     YDT     Yukon Standard Time             YST9YST
                   3232:        {"Alaskan Standard Time",               0x0000, "AKS", "AKD"},          // (UTC-09:00) US Anchorage
                   3233: //     10      HST     HDT     Hawaii Standard Time            HST10HDT
                   3234:        {"Aleutian Standard Time",              0x0000, "HST", "HDT"},          // (UTC-10:00) US Aleutian
                   3235:        {"Hawaiian Standard Time",              0x0000, "HST", "HDT"},          // (UTC-10:00) US Honolulu
                   3236: //     11      SST             Samoa Standard Time             SST11
                   3237:        {"Samoa Standard Time",                 0x0000, "SST", "SDT"},          // (UTC-11:00) US Samoa
                   3238: //     -12     NZS     NZD     New Zealand Standard Time       NZS-12NZD
                   3239:        {"New Zealand Standard Time",           0x0000, "NZS", "NZD"},          // (UTC+12:00) NZ Auckland
                   3240: //     -10     GST             Guam Standard Time              GST-10
                   3241:        {"West Pacific Standard Time",          0x0000, "GST", "GDT"},          // (UTC+10:00) GU Guam
                   3242: //     -10     EAS     EAD     Eastern Australian Standard     EAS-10EAD
                   3243:        {"AUS Eastern Standard Time",           0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Sydney
                   3244:        {"E. Australia Standard Time",          0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Brisbane
                   3245:        {"Tasmania Standard Time",              0x0000, "EAS", "EAD"},          // (UTC+10:00) AU Hobart
                   3246: //     -9:30   CAS     CAD     Central Australian Standard     CAS-9:30CAD
                   3247:        {"AUS Central Standard Time",           0x0000, "CAS", "CAD"},          // (UTC+09:30) AU Darwin
                   3248:        {"Cen. Australia Standard Time",        0x0000, "CAS", "CAD"},          // (UTC+09:30) AU Adelaide
                   3249: //     -9      JST             Japan Standard Time             JST-9
                   3250:        {"Tokyo Standard Time",                 0x0000, "JST", "JDT"},          // (UTC+09:00) JP Tokyo
                   3251: //     -9      KST     KDT     Korean Standard Time            KST-9KDT
                   3252:        {"Korea Standard Time",                 0x0000, "KST", "KDT"},          // (UTC+09:00) KR Seoul
                   3253:        {"North Korea Standard Time",           0x0000, "KST", "KDT"},          // (UTC+08:30) KP Pyongyang
                   3254: //     -8      HKT             Hong Kong Time                  HKT-8
                   3255:        {"China Standard Time",                 0x0C04, "HKT", "HKS"},          // (UTC+08:00) HK Hong Kong (zh-hk)
                   3256: //     -8      CCT             China Coast Time                CCT-8
                   3257:        {"China Standard Time",                 0x0000, "CCT", "CDT"},          // (UTC+08:00) CN Shanghai
                   3258:        {"Taipei Standard Time",                0x0000, "CCT", "CDT"},          // (UTC+08:00) TW Taipei
                   3259: //     -8      SST             Singapore Standard Time         SST-8
                   3260:        {"Singapore Standard Time",             0x0000, "SST", "SDT"},          // (UTC+08:00) SG Singapore
                   3261: //     -8      WAS     WAD     Western Australian Standard     WAS-8WAD
                   3262:        {"Aus Central W. Standard Time",        0x0000, "WAS", "WAD"},          // (UTC+08:45) AU Eucla
                   3263:        {"W. Australia Standard Time",          0x0000, "WAS", "WAD"},          // (UTC+08:00) AU Perth
                   3264: //     -7:30   JT              Java Standard Time              JST-7:30
                   3265: //     -7      NST             North Sumatra Time              NST-7
                   3266:        {"SE Asia Standard Time",               0x0000, "NST", "NDT"},          // (UTC+07:00) ID Jakarta
                   3267: //     -5:30   IST             Indian Standard Time            IST-5:30
                   3268:        {"India Standard Time",                 0x0000, "IST", "IDT"},          // (UTC+05:30) IN Calcutta
                   3269: //     -3:30   IST     IDT     Iran Standard Time              IST-3:30IDT
                   3270:        {"Iran Standard Time",                  0x0000, "IST", "IDT"},          // (UTC+03:30) IR Tehran
                   3271: //     -3      MSK     MSD     Moscow Winter Time              MSK-3MSD
                   3272:        {"Belarus Standard Time",               0x0000, "MSK", "MSD"},          // (UTC+03:00) BY Minsk
                   3273:        {"Russian Standard Time",               0x0000, "MSK", "MSD"},          // (UTC+03:00) RU Moscow
                   3274: //     -2      EET             Eastern Europe Time             EET-2
                   3275:        {"E. Europe Standard Time",             0x0000, "EET", "EES"},          // (UTC+02:00) MD Chisinau
                   3276:        {"FLE Standard Time",                   0x0000, "EET", "EES"},          // (UTC+02:00) UA Kiev
                   3277:        {"GTB Standard Time",                   0x0000, "EET", "EES"},          // (UTC+02:00) RO Bucharest
                   3278:        {"Kaliningrad Standard Time",           0x0000, "EET", "EES"},          // (UTC+02:00) RU Kaliningrad
                   3279: //     -2      IST     IDT     Israel Standard Time            IST-2IDT
                   3280:        {"Israel Standard Time",                0x0000, "IST", "IDT"},          // (UTC+02:00) IL Jerusalem
                   3281: //     -1      MEZ     MES     Middle European Time            MEZ-1MES
                   3282: //     -1      SWT     SST     Swedish Winter Time             SWT-1SST
                   3283: //     -1      FWT     FST     French Winter Time              FWT-1FST
                   3284: //     -1      CET     CES     Central European Time           CET-1CES
                   3285:        {"Central Europe Standard Time",        0x0000, "CET", "CES"},          // (UTC+01:00) HU Budapest
                   3286:        {"Central European Standard Time",      0x0000, "CET", "CES"},          // (UTC+01:00) PL Warsaw
                   3287:        {"Romance Standard Time",               0x0000, "CET", "CES"},          // (UTC+01:00) FR Paris
                   3288:        {"W. Europe Standard Time",             0x0000, "CET", "CES"},          // (UTC+01:00) DE Berlin
                   3289: //     -1      WAT             West African Time               WAT-1
                   3290:        {"Namibia Standard Time",               0x0000, "WAT", "WAS"},          // (UTC+01:00) NA Windhoek
                   3291:        {"W. Central Africa Standard Time",     0x0000, "WAT", "WAS"},          // (UTC+01:00) NG Lagos
                   3292: //     0       UTC             Universal Coordinated Time      UTC0
                   3293:        {"UTC",                                 0x0000, "UTC", ""   },          // (UTC+00:00) GMT+0
                   3294:        {"UTC-02",                              0x0000, "UTC", ""   },          // (UTC-02:00) GMT+2
                   3295:        {"UTC-08",                              0x0000, "UTC", ""   },          // (UTC-08:00) GMT+8
                   3296:        {"UTC-09",                              0x0000, "UTC", ""   },          // (UTC-09:00) GMT+9
                   3297:        {"UTC-11",                              0x0000, "UTC", ""   },          // (UTC-11:00) GMT+11
                   3298:        {"UTC+12",                              0x0000, "UTC", ""   },          // (UTC+12:00) GMT-12
                   3299: };
                   3300: 
                   3301: static const struct {
1.1.1.32  root     3302:        UINT16 code;
                   3303:        char *message_english;
                   3304:        char *message_japanese;
                   3305: } standard_error_table[] = {
                   3306:        {0x01,  "Invalid function", "�����ȃt�@���N�V�����ł�."},
                   3307:        {0x02,  "File not found", "�t�@�C�������‚���܂���."},
                   3308:        {0x03,  "Path not found", "�p�X�����‚���܂���."},
                   3309:        {0x04,  "Too many open files", "�J����Ă���t�@�C�����������܂�."},
                   3310:        {0x05,  "Access denied", "�A�N�Z�X�͋��ۂ���܂���."},
                   3311:        {0x06,  "Invalid handle", "�����ȃn���h���ł�."},
                   3312:        {0x07,  "Memory control blocks destroyed", "����������u���b�N���j������܂���."},
                   3313:        {0x08,  "Insufficient memory", "������������܂���."},
                   3314:        {0x09,  "Invalid memory block address", "�����ȃ������u���b�N�̃A�h���X�ł�."},
                   3315:        {0x0A,  "Invalid Environment", "�����Ȋ‹��ł�."},
                   3316:        {0x0B,  "Invalid format", "�����ȃt�H�[�}�b�g�ł�."},
                   3317:        {0x0C,  "Invalid function parameter", "�����ȃt�@���N�V�����p�����[�^�ł�."},
                   3318:        {0x0D,  "Invalid data", "�����ȃf�[�^�ł�."},
                   3319:        {0x0F,  "Invalid drive specification", "�����ȃh���C�u�̎w��ł�."},
                   3320:        {0x10,  "Attempt to remove current directory", "���݂̃f�B���N�g�����폜���悤�Ƃ��܂���."},
                   3321:        {0x11,  "Not same device", "�����f�o�C�X�ł͂���܂���."},
                   3322:        {0x12,  "No more files", "�t�@�C���͂���ȏ゠��܂���."},
                   3323:        {0x13,  "Write protect error", "�������ݕی�G���[�ł�."},
                   3324:        {0x14,  "Invalid unit", "�����ȃ��j�b�g�ł�."},
                   3325:        {0x15,  "Not ready", "�������ł��Ă��܂���."},
                   3326:        {0x16,  "Invalid device request", "�����ȃf�o�C�X�v���ł�."},
                   3327:        {0x17,  "Data error", "�f�[�^�G���[�ł�."},
                   3328:        {0x18,  "Invalid device request parameters", "�����ȃf�o�C�X�v���̃p�����[�^�ł�."},
                   3329:        {0x19,  "Seek error", "�V�[�N�G���[�ł�."},
                   3330:        {0x1A,  "Invalid media type", "�����ȃ��f�B�A�̎�ނł�."},
                   3331:        {0x1B,  "Sector not found", "�Z�N�^�����‚���܂���."},
                   3332:        {0x1C,  "Printer out of paper error", "�v�����^�̗p��������܂���."},
                   3333:        {0x1D,  "Write fault error", "�������݃G���[�ł�."},
                   3334:        {0x1E,  "Read fault error", "�ǂݎ��G���[�ł�."},
                   3335:        {0x1F,  "General failure", "�G���[�ł�."},
                   3336:        {0x20,  "Sharing violation", "���L�ᔽ�ł�."},
                   3337:        {0x21,  "Lock violation", "���b�N�ᔽ�ł�."},
                   3338:        {0x22,  "Invalid disk change", "�����ȃf�B�X�N�̌����ł�."},
                   3339:        {0x23,  "FCB unavailable", "FCB �͎g���܂���."},
                   3340:        {0x24,  "System resource exhausted", "�V�X�e�����\�[�X�͂����g���܂���."},
                   3341:        {0x25,  "Code page mismatch", "�R�[�h �y�[�W����v���܂���."},
                   3342:        {0x26,  "Out of input", "���͂��I���܂���."},
                   3343:        {0x27,  "Insufficient disk space", "�f�B�X�N�̋󂫗̈悪����܂���."},
                   3344: /*
                   3345:        {0x32,  "Network request not supported", NULL},
                   3346:        {0x33,  "Remote computer not listening", NULL},
                   3347:        {0x34,  "Duplicate name on network", NULL},
                   3348:        {0x35,  "Network name not found", NULL},
                   3349:        {0x36,  "Network busy", NULL},
                   3350:        {0x37,  "Network device no longer exists", NULL},
                   3351:        {0x38,  "Network BIOS command limit exceeded", NULL},
                   3352:        {0x39,  "Network adapter hardware error", NULL},
                   3353:        {0x3A,  "Incorrect response from network", NULL},
                   3354:        {0x3B,  "Unexpected network error", NULL},
                   3355:        {0x3C,  "Incompatible remote adapter", NULL},
                   3356:        {0x3D,  "Print queue full", NULL},
                   3357:        {0x3E,  "Queue not full", NULL},
                   3358:        {0x3F,  "Not enough space to print file", NULL},
                   3359:        {0x40,  "Network name was deleted", NULL},
                   3360:        {0x41,  "Network: Access denied", NULL},
                   3361:        {0x42,  "Network device type incorrect", NULL},
                   3362:        {0x43,  "Network name not found", NULL},
                   3363:        {0x44,  "Network name limit exceeded", NULL},
                   3364:        {0x45,  "Network BIOS session limit exceeded", NULL},
                   3365:        {0x46,  "Temporarily paused", NULL},
                   3366:        {0x47,  "Network request not accepted", NULL},
                   3367:        {0x48,  "Network print/disk redirection paused", NULL},
                   3368:        {0x49,  "Network software not installed", NULL},
                   3369:        {0x4A,  "Unexpected adapter close", NULL},
                   3370: */
                   3371:        {0x50,  "File exists", "�t�@�C���͑��݂��܂�."},
1.1.1.33  root     3372:        {0x52,  "Cannot make directory entry", "�f�B���N�g���G���g�����쐬�ł��܂���."},
1.1.1.32  root     3373:        {0x53,  "Fail on INT 24", "INT 24 �Ŏ��s���܂���."},
                   3374:        {0x54,  "Too many redirections", "���_�C���N�g���������܂�."},
                   3375:        {0x55,  "Duplicate redirection", "���_�C���N�g���d�����Ă��܂�."},
                   3376:        {0x56,  "Invalid password", "�p�X���[�h���Ⴂ�܂�."},
                   3377:        {0x57,  "Invalid parameter", "�p�����[�^�̎w�肪�Ⴂ�܂�."},
                   3378:        {0x58,  "Network data fault", "�l�b�g���[�N�f�[�^�̃G���[�ł�."},
                   3379:        {0x59,  "Function not supported by network", "�t�@���N�V�����̓l�b�g���[�N�ŃT�|�[�g����Ă��܂���."},
                   3380:        {0x5A,  "Required system component not installe", "�K�v�ȃV�X�e�� �R���|�[�l���g���g�ݍ��܂�Ă��܂���."},
                   3381: /*
                   3382:        {0x64,  "Unknown error", "�s���ȃG���[�ł�."},
                   3383:        {0x65,  "Not ready", "�������ł��Ă��܂���."},
                   3384:        {0x66,  "EMS memory no longer valid", "EMS �������͂����L���ł͂���܂���."},
                   3385:        {0x67,  "CDROM not High Sierra or ISO-9660 format", "CDROM �� High Sierra �܂��� ISO-9660 �t�H�[�}�b�g�ł͂���܂���."},
                   3386:        {0x68,  "Door open", "���o�[���‚܂��Ă��܂���."
                   3387: */
                   3388:        {0xB0,  "Volume is not locked", "�{�����[�������b�N����Ă��܂���."},
                   3389:        {0xB1,  "Volume is locked in drive", "�{�����[�������b�N����Ă��܂�."},
                   3390:        {0xB2,  "Volume is not removable", "�{�����[���͎��O���ł��܂���."},
                   3391:        {0xB4,  "Lock count has been exceeded", "�{�����[��������ȏネ�b�N�ł��܂���."},
                   3392:        {0xB5,  "A valid eject request failed", "���o���Ɏ��s���܂���."},
                   3393:        {-1  ,  "Unknown error", "�s���ȃG���[�ł�."},
                   3394: };
                   3395: 
                   3396: static const struct {
                   3397:        UINT16 code;
                   3398:        char *message_english;
                   3399:        char *message_japanese;
                   3400: } param_error_table[] = {
                   3401:        {0x01,  "Too many parameters", "�p�����[�^���������܂�."},
                   3402:        {0x02,  "Required parameter missing", "�p�����[�^������܂���."},
                   3403:        {0x03,  "Invalid switch", "�����ȃX�C�b�`�ł�."},
                   3404:        {0x04,  "Invalid keyword", "�����ȃL�[���[�h�ł�."},
                   3405:        {0x06,  "Parameter value not in allowed range", "�p�����[�^�̒l���͈͂𒴂��Ă��܂�."},
                   3406:        {0x07,  "Parameter value not allowed", "���̃p�����[�^�̒l�͎g���܂���."},
                   3407:        {0x08,  "Parameter value not allowed", "���̃p�����[�^�̒l�͎g���܂���."},
                   3408:        {0x09,  "Parameter format not correct", "�p�����[�^�̏������Ⴂ�܂�."},
                   3409:        {0x0A,  "Invalid parameter", "�����ȃp�����[�^�ł�."},
                   3410:        {0x0B,  "Invalid parameter combination", "�����ȃp�����[�^�̑g�ݍ��킹�ł�."},
                   3411:        {-1  ,  "Unknown error", "�s���ȃG���[�ł�."},
                   3412: };
                   3413: 
                   3414: static const struct {
                   3415:        UINT16 code;
                   3416:        char *message_english;
                   3417:        char *message_japanese;
                   3418: } critical_error_table[] = {
                   3419:        {0x00,  "Write protect error", "�������ݕی�G���[�ł�."},
                   3420:        {0x01,  "Invalid unit", "�����ȃ��j�b�g�ł�."},
                   3421:        {0x02,  "Not ready", "�������ł��Ă��܂���."},
                   3422:        {0x03,  "Invalid device request", "�����ȃf�o�C�X�v���ł�."},
                   3423:        {0x04,  "Data error", "�f�[�^�G���[�ł�."},
                   3424:        {0x05,  "Invalid device request parameters", "�����ȃf�o�C�X�v���̃p�����[�^�ł�."},
                   3425:        {0x06,  "Seek error", "�V�[�N�G���[�ł�."},
                   3426:        {0x07,  "Invalid media type", "�����ȃ��f�B�A�̎�ނł�."},
                   3427:        {0x08,  "Sector not found", "�Z�N�^�����‚���܂���."},
                   3428:        {0x09,  "Printer out of paper error", "�v�����^�̗p��������܂���."},
                   3429:        {0x0A,  "Write fault error", "�������݃G���[�ł�."},
                   3430:        {0x0B,  "Read fault error", "�ǂݎ��G���[�ł�."},
                   3431:        {0x0C,  "General failure", "�G���[�ł�."},
                   3432:        {0x0D,  "Sharing violation", "���L�ᔽ�ł�."},
                   3433:        {0x0E,  "Lock violation", "���b�N�ᔽ�ł�."},
                   3434:        {0x0F,  "Invalid disk change", "�����ȃf�B�X�N�̌����ł�."},
                   3435:        {0x10,  "FCB unavailable", "FCB �͎g���܂���."},
                   3436:        {0x11,  "System resource exhausted", "�V�X�e�����\�[�X�͂����g���܂���."},
                   3437:        {0x12,  "Code page mismatch", "�R�[�h �y�[�W����v���܂���."},
                   3438:        {0x13,  "Out of input", "���͂��I���܂���."},
                   3439:        {0x14,  "Insufficient disk space", "�f�B�X�N�̋󂫗̈悪����܂���."},
                   3440:        {-1  ,  "Critical error", "�v���I�ȃG���[�ł�."},
                   3441: };
                   3442: 
1.1.1.20  root     3443: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg);
                   3444: int msdos_psp_get_file_table(int fd, int psp_seg);
                   3445: void msdos_putch(UINT8 data);
                   3446: 
1.1       root     3447: // process info
                   3448: 
                   3449: process_t *msdos_process_info_create(UINT16 psp_seg)
                   3450: {
                   3451:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3452:                if(process[i].psp == 0 || process[i].psp == psp_seg) {
                   3453:                        memset(&process[i], 0, sizeof(process_t));
                   3454:                        process[i].psp = psp_seg;
                   3455:                        return(&process[i]);
                   3456:                }
                   3457:        }
                   3458:        fatalerror("too many processes\n");
                   3459:        return(NULL);
                   3460: }
                   3461: 
1.1.1.33  root     3462: process_t *msdos_process_info_get(UINT16 psp_seg, bool show_error)
1.1       root     3463: {
                   3464:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3465:                if(process[i].psp == psp_seg) {
                   3466:                        return(&process[i]);
                   3467:                }
                   3468:        }
1.1.1.33  root     3469:        if(show_error) {
                   3470:                fatalerror("invalid psp address\n");
                   3471:        }
1.1       root     3472:        return(NULL);
                   3473: }
                   3474: 
1.1.1.33  root     3475: process_t *msdos_process_info_get(UINT16 psp_seg)
                   3476: {
                   3477:        return(msdos_process_info_get(psp_seg, true));
                   3478: }
                   3479: 
1.1.1.23  root     3480: void msdos_sda_update(int psp_seg)
                   3481: {
                   3482:        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   3483:        
                   3484:        for(int i = 0; i < MAX_PROCESS; i++) {
                   3485:                if(process[i].psp == psp_seg) {
                   3486:                        sda->switchar = process[i].switchar;
                   3487:                        sda->current_dta.w.l = process[i].dta.w.l;
                   3488:                        sda->current_dta.w.h = process[i].dta.w.h;
                   3489:                        sda->current_psp = process[i].psp;
                   3490:                        break;
                   3491:                }
                   3492:        }
                   3493:        sda->malloc_strategy = malloc_strategy;
                   3494:        sda->return_code = retval;
                   3495:        sda->current_drive = _getdrive();
                   3496: }
                   3497: 
1.1.1.13  root     3498: // dta info
                   3499: 
                   3500: void msdos_dta_info_init()
                   3501: {
1.1.1.14  root     3502:        for(int i = 0; i < MAX_DTAINFO; i++) {
1.1.1.13  root     3503:                dtalist[i].find_handle = INVALID_HANDLE_VALUE;
                   3504:        }
                   3505: }
                   3506: 
                   3507: dtainfo_t *msdos_dta_info_get(UINT16 psp_seg, UINT32 dta_laddr)
                   3508: {
                   3509:        dtainfo_t *free_dta = NULL;
1.1.1.14  root     3510:        for(int i = 0; i < MAX_DTAINFO; i++) {
                   3511:                if(dtalist[i].find_handle == INVALID_HANDLE_VALUE) {
                   3512:                        if(free_dta == NULL) {
1.1.1.13  root     3513:                                free_dta = &dtalist[i];
                   3514:                        }
1.1.1.14  root     3515:                } else if(dta_laddr < LFN_DTA_LADDR && dtalist[i].dta == dta_laddr) {
1.1.1.13  root     3516:                        return(&dtalist[i]);
                   3517:                }
                   3518:        }
1.1.1.14  root     3519:        if(free_dta) {
1.1.1.13  root     3520:                free_dta->psp = psp_seg;
                   3521:                free_dta->dta = dta_laddr;
                   3522:                return(free_dta);
                   3523:        }
                   3524:        fatalerror("too many dta\n");
                   3525:        return(NULL);
                   3526: }
                   3527: 
                   3528: void msdos_dta_info_free(UINT16 psp_seg)
                   3529: {
1.1.1.14  root     3530:        for(int i = 0; i < MAX_DTAINFO; i++) {
                   3531:                if(dtalist[i].psp == psp_seg && dtalist[i].find_handle != INVALID_HANDLE_VALUE) {
1.1.1.13  root     3532:                        FindClose(dtalist[i].find_handle);
                   3533:                        dtalist[i].find_handle = INVALID_HANDLE_VALUE;
                   3534:                }
                   3535:        }
                   3536: }
                   3537: 
1.1       root     3538: void msdos_cds_update(int drv)
                   3539: {
                   3540:        cds_t *cds = (cds_t *)(mem + CDS_TOP);
                   3541:        
                   3542:        memset(mem + CDS_TOP, 0, CDS_SIZE);
                   3543:        sprintf(cds->path_name, "%c:\\", 'A' + drv);
                   3544:        cds->drive_attrib = 0x4000;     // physical drive
                   3545:        cds->physical_drive_number = drv;
                   3546: }
                   3547: 
1.1.1.17  root     3548: // nls information tables
                   3549: 
                   3550: // uppercase table (func 6502h)
                   3551: void msdos_upper_table_update()
                   3552: {
                   3553:        *(UINT16 *)(mem + UPPERTABLE_TOP) = 0x80;
1.1.1.22  root     3554:        for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17  root     3555:                UINT8 c[4];
1.1.1.33  root     3556:                *(UINT32 *)c = 0;               // reset internal conversion state
                   3557:                CharUpperBuffA((LPSTR)c, 4);    // (workaround for MBCS codepage)
1.1.1.17  root     3558:                c[0] = 0x80 + i;
                   3559:                DWORD rc = CharUpperBuffA((LPSTR)c, 1);
                   3560:                mem[UPPERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
                   3561:        }
                   3562: }
                   3563: 
1.1.1.23  root     3564: // lowercase table (func 6503h)
                   3565: void msdos_lower_table_update()
                   3566: {
                   3567:        *(UINT16 *)(mem + LOWERTABLE_TOP) = 0x80;
                   3568:        for(unsigned i = 0; i < 0x80; ++i) {
                   3569:                UINT8 c[4];
1.1.1.33  root     3570:                *(UINT32 *)c = 0;               // reset internal conversion state
                   3571:                CharLowerBuffA((LPSTR)c, 4);    // (workaround for MBCS codepage)
1.1.1.23  root     3572:                c[0] = 0x80 + i;
                   3573:                DWORD rc = CharLowerBuffA((LPSTR)c, 1);
                   3574:                mem[LOWERTABLE_TOP + 2 + i] = (rc == 1 && c[0]) ? c[0] : 0x80 + i;
                   3575:        }
                   3576: }
                   3577: 
1.1.1.17  root     3578: // filename uppercase table (func 6504h)
                   3579: void msdos_filename_upper_table_init()
                   3580: {
                   3581:        // depended on (file)system, not on active codepage
                   3582:        // temporary solution: just filling data
                   3583:        *(UINT16 *)(mem + FILENAME_UPPERTABLE_TOP) = 0x80;
1.1.1.22  root     3584:        for(unsigned i = 0; i < 0x80; ++i) {
1.1.1.17  root     3585:                mem[FILENAME_UPPERTABLE_TOP + 2 + i] = 0x80 + i;
                   3586:        }
                   3587: }
                   3588: 
                   3589: // filaname terminator table (func 6505h)
                   3590: void msdos_filename_terminator_table_init()
                   3591: {
                   3592:        const char illegal_chars[] = ".\"/\\[]:|<>+=;,";        // for standard MS-DOS fs.
                   3593:        UINT8 *data = mem + FILENAME_TERMINATOR_TOP;
                   3594:        
                   3595:        data[2] = 1;            // marker? (permissible character value)
                   3596:        data[3] = 0x00;         // 00h...FFh
                   3597:        data[4] = 0xff;
                   3598:        data[5] = 0;            // marker? (excluded character)
                   3599:        data[6] = 0x00;         // 00h...20h
                   3600:        data[7] = 0x20;
                   3601:        data[8] = 2;            // marker? (illegal characters for filename)
                   3602:        data[9] = (UINT8)strlen(illegal_chars);
                   3603:        memcpy(data + 10, illegal_chars, data[9]);
                   3604:        
                   3605:        // total length
                   3606:        *(UINT16 *)data = (10 - 2) + data[9];
                   3607: }
                   3608: 
                   3609: // collating table (func 6506h)
                   3610: void msdos_collating_table_update()
                   3611: {
                   3612:        // temporary solution: just filling data
                   3613:        *(UINT16 *)(mem + COLLATING_TABLE_TOP) = 0x100;
1.1.1.22  root     3614:        for(unsigned i = 0; i < 256; ++i) {
1.1.1.17  root     3615:                mem[COLLATING_TABLE_TOP + 2 + i] = i;
                   3616:        }
                   3617: }
                   3618: 
1.1       root     3619: // dbcs
                   3620: 
                   3621: void msdos_dbcs_table_update()
                   3622: {
                   3623:        UINT8 dbcs_data[DBCS_SIZE];
                   3624:        memset(dbcs_data, 0, sizeof(dbcs_data));
                   3625:        
                   3626:        CPINFO info;
                   3627:        GetCPInfo(active_code_page, &info);
                   3628:        
                   3629:        if(info.MaxCharSize != 1) {
                   3630:                for(int i = 0;; i += 2) {
                   3631:                        UINT8 lo = info.LeadByte[i + 0];
                   3632:                        UINT8 hi = info.LeadByte[i + 1];
                   3633:                        dbcs_data[2 + i + 0] = lo;
                   3634:                        dbcs_data[2 + i + 1] = hi;
                   3635:                        if(lo == 0 && hi == 0) {
                   3636:                                dbcs_data[0] = i + 2;
                   3637:                                break;
                   3638:                        }
                   3639:                }
                   3640:        } else {
                   3641:                dbcs_data[0] = 2;       // ???
                   3642:        }
                   3643:        memcpy(mem + DBCS_TOP, dbcs_data, sizeof(dbcs_data));
                   3644: }
                   3645: 
1.1.1.17  root     3646: void msdos_dbcs_table_finish()
                   3647: {
1.1.1.32  root     3648:        if(system_code_page != _getmbcp()) {
1.1.1.17  root     3649:                _setmbcp(system_code_page);
                   3650:        }
1.1.1.32  root     3651:        if(console_code_page != GetConsoleCP()) {
                   3652:                SetConsoleCP(console_code_page);
                   3653:                SetConsoleOutputCP(console_code_page);
                   3654:        }
1.1.1.17  root     3655: }
                   3656: 
                   3657: void msdos_nls_tables_init()
1.1       root     3658: {
1.1.1.32  root     3659:        active_code_page = console_code_page = GetConsoleCP();
                   3660:        system_code_page = _getmbcp();
                   3661:        
                   3662:        if(active_code_page != system_code_page) {
                   3663:                if(_setmbcp(active_code_page) != 0) {
                   3664:                        active_code_page = system_code_page;
                   3665:                }
                   3666:        }
                   3667:        
1.1.1.17  root     3668:        msdos_upper_table_update();
1.1.1.23  root     3669:        msdos_lower_table_update();
1.1.1.17  root     3670:        msdos_filename_terminator_table_init();
                   3671:        msdos_filename_upper_table_init();
                   3672:        msdos_collating_table_update();
1.1       root     3673:        msdos_dbcs_table_update();
                   3674: }
                   3675: 
1.1.1.17  root     3676: void msdos_nls_tables_update()
1.1       root     3677: {
1.1.1.17  root     3678:        msdos_dbcs_table_update();
                   3679:        msdos_upper_table_update();
1.1.1.23  root     3680:        msdos_lower_table_update();
                   3681: //     msdos_collating_table_update();
1.1       root     3682: }
                   3683: 
                   3684: int msdos_lead_byte_check(UINT8 code)
                   3685: {
                   3686:        UINT8 *dbcs_table = mem + DBCS_TABLE;
                   3687:        
                   3688:        for(int i = 0;; i += 2) {
                   3689:                UINT8 lo = dbcs_table[i + 0];
                   3690:                UINT8 hi = dbcs_table[i + 1];
                   3691:                if(lo == 0 && hi == 0) {
                   3692:                        break;
                   3693:                }
                   3694:                if(lo <= code && code <= hi) {
                   3695:                        return(1);
                   3696:                }
                   3697:        }
                   3698:        return(0);
                   3699: }
                   3700: 
1.1.1.20  root     3701: int msdos_ctrl_code_check(UINT8 code)
                   3702: {
1.1.1.22  root     3703:        return (code >= 0x01 && code <= 0x1a && code != 0x07 && code != 0x08 && code != 0x09 && code != 0x0a && code != 0x0d);
1.1.1.20  root     3704: }
                   3705: 
1.1       root     3706: // file control
                   3707: 
1.1.1.14  root     3708: char *msdos_remove_double_quote(char *path)
                   3709: {
                   3710:        static char tmp[MAX_PATH];
                   3711:        
                   3712:        memset(tmp, 0, sizeof(tmp));
                   3713:        if(strlen(path) >= 2 && path[0] == '"' && path[strlen(path) - 1] == '"') {
                   3714:                memcpy(tmp, path + 1, strlen(path) - 2);
                   3715:        } else {
                   3716:                strcpy(tmp, path);
                   3717:        }
                   3718:        return(tmp);
                   3719: }
                   3720: 
1.1.1.32  root     3721: char *msdos_remove_end_separator(char *path)
                   3722: {
                   3723:        static char tmp[MAX_PATH];
                   3724:        
                   3725:        strcpy(tmp, path);
                   3726:        int len = strlen(tmp);
                   3727:        if(len > 3 && tmp[len - 1] == '\\') {
                   3728:                tmp[len - 1] = '\0';
                   3729:        }
                   3730:        return(tmp);
                   3731: }
                   3732: 
1.1.1.14  root     3733: char *msdos_combine_path(char *dir, const char *file)
                   3734: {
                   3735:        static char tmp[MAX_PATH];
                   3736:        char *tmp_dir = msdos_remove_double_quote(dir);
                   3737:        
                   3738:        if(strlen(tmp_dir) == 0) {
                   3739:                strcpy(tmp, file);
                   3740:        } else if(tmp_dir[strlen(tmp_dir) - 1] == '\\') {
                   3741:                sprintf(tmp, "%s%s", tmp_dir, file);
                   3742:        } else {
                   3743:                sprintf(tmp, "%s\\%s", tmp_dir, file);
                   3744:        }
                   3745:        return(tmp);
                   3746: }
                   3747: 
1.1       root     3748: char *msdos_trimmed_path(char *path, int lfn)
                   3749: {
                   3750:        static char tmp[MAX_PATH];
                   3751:        
                   3752:        if(lfn) {
                   3753:                strcpy(tmp, path);
                   3754:        } else {
                   3755:                // remove space in the path
                   3756:                char *src = path, *dst = tmp;
                   3757:                
                   3758:                while(*src != '\0') {
                   3759:                        if(msdos_lead_byte_check(*src)) {
                   3760:                                *dst++ = *src++;
                   3761:                                *dst++ = *src++;
                   3762:                        } else if(*src != ' ') {
                   3763:                                *dst++ = *src++;
                   3764:                        } else {
                   3765:                                src++;  // skip space
                   3766:                        }
                   3767:                }
                   3768:                *dst = '\0';
                   3769:        }
1.1.1.14  root     3770:        if(_stricmp(tmp, "C:\\COMMAND.COM") == 0) {
                   3771:                // redirect C:\COMMAND.COM to comspec_path
                   3772:                strcpy(tmp, comspec_path);
                   3773:        } else if(is_vista_or_later && _access(tmp, 0) != 0) {
                   3774:                // redirect new files (without wildcards) in C:\ to %TEMP%, since C:\ is not usually writable
                   3775:                static int root_drive_protected = -1;
                   3776:                char temp[MAX_PATH], name[MAX_PATH], *name_temp = NULL;
                   3777:                dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   3778:                
                   3779:                if(GetFullPathName(tmp, MAX_PATH, temp, &name_temp) != 0 &&
                   3780:                   name_temp != NULL && strstr(name_temp, "?") == NULL && strstr(name_temp, "*") == NULL) {
                   3781:                        strcpy(name, name_temp);
                   3782:                        name_temp[0] = '\0';
                   3783:                        
                   3784:                        if((temp[0] == 'A' + dos_info->boot_drive - 1 || temp[0] == 'a' + dos_info->boot_drive - 1) &&
                   3785:                           (temp[1] == ':') && (temp[2] == '\\' || temp[2] == '/') && (temp[3] == '\0')) {
                   3786:                                if(root_drive_protected == -1) {
                   3787:                                        FILE *fp = NULL;
                   3788:                                        
                   3789:                                        sprintf(temp, "%c:\\MS-DOS_Player.$$$", 'A' + dos_info->boot_drive - 1);
                   3790:                                        root_drive_protected = 1;
                   3791:                                        try {
                   3792:                                                if((fp = fopen(temp, "w")) != NULL) {
                   3793:                                                        if(fprintf(fp, "TEST") == 4) {
                   3794:                                                                root_drive_protected = 0;
                   3795:                                                        }
                   3796:                                                }
                   3797:                                        } catch(...) {
                   3798:                                        }
                   3799:                                        if(fp != NULL) {
                   3800:                                                fclose(fp);
                   3801:                                        }
                   3802:                                        if(_access(temp, 0) == 0) {
                   3803:                                                remove(temp);
                   3804:                                        }
                   3805:                                }
                   3806:                                if(root_drive_protected == 1) {
                   3807:                                        if(GetEnvironmentVariable("TEMP", temp, MAX_PATH) != 0 ||
                   3808:                                           GetEnvironmentVariable("TMP",  temp, MAX_PATH) != 0) {
                   3809:                                                strcpy(tmp, msdos_combine_path(temp, name));
                   3810:                                        }
                   3811:                                }
                   3812:                        }
                   3813:                }
                   3814:        }
1.1       root     3815:        return(tmp);
                   3816: }
                   3817: 
1.1.1.28  root     3818: char *msdos_get_multiple_short_path(char *src)
                   3819: {
1.1.1.32  root     3820:        // "LONGPATH\";"LONGPATH\";"LONGPATH\" to SHORTPATH;SHORTPATH;SHORTPATH
1.1.1.28  root     3821:        static char env_path[ENV_SIZE];
                   3822:        char tmp[ENV_SIZE], *token;
                   3823:        
                   3824:        memset(env_path, 0, sizeof(env_path));
                   3825:        strcpy(tmp, src);
                   3826:        token = my_strtok(tmp, ";");
                   3827:        
                   3828:        while(token != NULL) {
                   3829:                if(token[0] != '\0') {
                   3830:                        char *path = msdos_remove_double_quote(token), short_path[MAX_PATH];
1.1.1.32  root     3831:                        if(path != NULL && strlen(path) != 0) {
                   3832:                                if(env_path[0] != '\0') {
                   3833:                                        strcat(env_path, ";");
                   3834:                                }
1.1.1.28  root     3835:                                if(GetShortPathName(path, short_path, MAX_PATH) == 0) {
1.1.1.32  root     3836:                                        strcat(env_path, msdos_remove_end_separator(path));
1.1.1.28  root     3837:                                } else {
                   3838:                                        my_strupr(short_path);
1.1.1.32  root     3839:                                        strcat(env_path, msdos_remove_end_separator(short_path));
1.1.1.28  root     3840:                                }
                   3841:                        }
                   3842:                }
                   3843:                token = my_strtok(NULL, ";");
                   3844:        }
                   3845:        return(env_path);
                   3846: }
                   3847: 
1.1       root     3848: bool match(char *text, char *pattern)
                   3849: {
1.1.1.24  root     3850:        // http://www.prefield.com/algorithm/string/wildcard.html
1.1.1.14  root     3851:        switch(*pattern) {
1.1       root     3852:        case '\0':
                   3853:                return !*text;
                   3854:        case '*':
1.1.1.14  root     3855:                return match(text, pattern + 1) || (*text && match(text + 1, pattern));
1.1       root     3856:        case '?':
                   3857:                return *text && match(text + 1, pattern + 1);
                   3858:        default:
                   3859:                return (*text == *pattern) && match(text + 1, pattern + 1);
                   3860:        }
                   3861: }
                   3862: 
                   3863: bool msdos_match_volume_label(char *path, char *volume)
                   3864: {
                   3865:        char *p;
                   3866:        
1.1.1.14  root     3867:        if(!*volume) {
                   3868:                return false;
                   3869:        } else if((p = my_strchr(path, ':')) != NULL) {
1.1       root     3870:                return msdos_match_volume_label(p + 1, volume);
                   3871:        } else if((p = my_strchr(path, '\\')) != NULL) {
                   3872:                return msdos_match_volume_label(p + 1, volume);
                   3873:        } else if((p = my_strchr(path, '.')) != NULL) {
1.1.1.14  root     3874:                char tmp[MAX_PATH];
                   3875:                sprintf(tmp, "%.*s%s", (int)(p - path), path, p + 1);
                   3876:                return match(volume, tmp);
1.1       root     3877:        } else {
                   3878:                return match(volume, path);
                   3879:        }
                   3880: }
                   3881: 
                   3882: char *msdos_fcb_path(fcb_t *fcb)
                   3883: {
                   3884:        static char tmp[MAX_PATH];
                   3885:        char name[9], ext[4];
                   3886:        
                   3887:        memset(name, 0, sizeof(name));
                   3888:        memcpy(name, fcb->file_name, 8);
                   3889:        strcpy(name, msdos_trimmed_path(name, 0));
                   3890:        
                   3891:        memset(ext, 0, sizeof(ext));
                   3892:        memcpy(ext, fcb->file_name + 8, 3);
                   3893:        strcpy(ext, msdos_trimmed_path(ext, 0));
                   3894:        
                   3895:        if(name[0] == '\0' || strcmp(name, "????????") == 0) {
                   3896:                strcpy(name, "*");
                   3897:        }
                   3898:        if(ext[0] == '\0') {
                   3899:                strcpy(tmp, name);
                   3900:        } else {
                   3901:                if(strcmp(ext, "???") == 0) {
                   3902:                        strcpy(ext, "*");
                   3903:                }
                   3904:                sprintf(tmp, "%s.%s", name, ext);
                   3905:        }
                   3906:        return(tmp);
                   3907: }
                   3908: 
                   3909: void msdos_set_fcb_path(fcb_t *fcb, char *path)
                   3910: {
                   3911:        char *ext = my_strchr(path, '.');
                   3912:        
                   3913:        memset(fcb->file_name, 0x20, 8 + 3);
                   3914:        if(ext != NULL && path[0] != '.') {
                   3915:                *ext = '\0';
                   3916:                memcpy(fcb->file_name + 8, ext + 1, strlen(ext + 1));
                   3917:        }
                   3918:        memcpy(fcb->file_name, path, strlen(path));
                   3919: }
                   3920: 
                   3921: char *msdos_short_path(char *path)
                   3922: {
                   3923:        static char tmp[MAX_PATH];
                   3924:        
1.1.1.24  root     3925:        if(GetShortPathName(path, tmp, MAX_PATH) == 0) {
                   3926:                strcpy(tmp, path);
                   3927:        }
1.1       root     3928:        my_strupr(tmp);
                   3929:        return(tmp);
                   3930: }
                   3931: 
1.1.1.13  root     3932: char *msdos_short_name(WIN32_FIND_DATA *fd)
                   3933: {
                   3934:        static char tmp[MAX_PATH];
                   3935: 
1.1.1.14  root     3936:        if(fd->cAlternateFileName[0]) {
1.1.1.13  root     3937:                strcpy(tmp, fd->cAlternateFileName);
                   3938:        } else {
                   3939:                strcpy(tmp, fd->cFileName);
                   3940:        }
                   3941:        my_strupr(tmp);
                   3942:        return(tmp);
                   3943: }
                   3944: 
1.1       root     3945: char *msdos_short_full_path(char *path)
                   3946: {
                   3947:        static char tmp[MAX_PATH];
                   3948:        char full[MAX_PATH], *name;
                   3949:        
1.1.1.14  root     3950:        // Full works with non-existent files, but Short does not
1.1       root     3951:        GetFullPathName(path, MAX_PATH, full, &name);
1.1.1.14  root     3952:        *tmp = '\0';
                   3953:        if(GetShortPathName(full, tmp, MAX_PATH) == 0 && name > path) {
                   3954:                name[-1] = '\0';
                   3955:                DWORD len = GetShortPathName(full, tmp, MAX_PATH);
                   3956:                if(len == 0) {
                   3957:                        strcpy(tmp, full);
                   3958:                } else {
                   3959:                        tmp[len++] = '\\';
                   3960:                        strcpy(tmp + len, name);
                   3961:                }
                   3962:        }
1.1       root     3963:        my_strupr(tmp);
                   3964:        return(tmp);
                   3965: }
                   3966: 
                   3967: char *msdos_short_full_dir(char *path)
                   3968: {
                   3969:        static char tmp[MAX_PATH];
                   3970:        char full[MAX_PATH], *name;
                   3971:        
                   3972:        GetFullPathName(path, MAX_PATH, full, &name);
                   3973:        name[-1] = '\0';
1.1.1.24  root     3974:        if(GetShortPathName(full, tmp, MAX_PATH) == 0) {
                   3975:                strcpy(tmp, full);
                   3976:        }
1.1       root     3977:        my_strupr(tmp);
                   3978:        return(tmp);
                   3979: }
                   3980: 
                   3981: char *msdos_local_file_path(char *path, int lfn)
                   3982: {
                   3983:        char *trimmed = msdos_trimmed_path(path, lfn);
1.1.1.14  root     3984: #if 0
                   3985:        // I have forgotten the reason of this routine... :-(
1.1       root     3986:        if(_access(trimmed, 0) != 0) {
                   3987:                process_t *process = msdos_process_info_get(current_psp);
                   3988:                static char tmp[MAX_PATH];
                   3989:                
                   3990:                sprintf(tmp, "%s\\%s", process->module_dir, trimmed);
                   3991:                if(_access(tmp, 0) == 0) {
                   3992:                        return(tmp);
                   3993:                }
                   3994:        }
1.1.1.14  root     3995: #endif
1.1       root     3996:        return(trimmed);
                   3997: }
                   3998: 
1.1.1.29  root     3999: bool msdos_is_device_path(char *path)
1.1.1.11  root     4000: {
                   4001:        char full[MAX_PATH], *name;
                   4002:        
1.1.1.24  root     4003:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4004:                if(_stricmp(full, "\\\\.\\AUX" ) == 0 ||
                   4005:                   _stricmp(full, "\\\\.\\CON" ) == 0 ||
                   4006:                   _stricmp(full, "\\\\.\\NUL" ) == 0 ||
                   4007:                   _stricmp(full, "\\\\.\\PRN" ) == 0 ||
                   4008:                   _stricmp(full, "\\\\.\\COM1") == 0 ||
                   4009:                   _stricmp(full, "\\\\.\\COM2") == 0 ||
                   4010:                   _stricmp(full, "\\\\.\\COM3") == 0 ||
                   4011:                   _stricmp(full, "\\\\.\\COM4") == 0 ||
                   4012:                   _stricmp(full, "\\\\.\\COM5") == 0 ||
                   4013:                   _stricmp(full, "\\\\.\\COM6") == 0 ||
                   4014:                   _stricmp(full, "\\\\.\\COM7") == 0 ||
                   4015:                   _stricmp(full, "\\\\.\\COM8") == 0 ||
                   4016:                   _stricmp(full, "\\\\.\\COM9") == 0 ||
                   4017:                   _stricmp(full, "\\\\.\\LPT1") == 0 ||
                   4018:                   _stricmp(full, "\\\\.\\LPT2") == 0 ||
                   4019:                   _stricmp(full, "\\\\.\\LPT3") == 0 ||
                   4020:                   _stricmp(full, "\\\\.\\LPT4") == 0 ||
                   4021:                   _stricmp(full, "\\\\.\\LPT5") == 0 ||
                   4022:                   _stricmp(full, "\\\\.\\LPT6") == 0 ||
                   4023:                   _stricmp(full, "\\\\.\\LPT7") == 0 ||
                   4024:                   _stricmp(full, "\\\\.\\LPT8") == 0 ||
                   4025:                   _stricmp(full, "\\\\.\\LPT9") == 0) {
                   4026:                        return(true);
                   4027:                } else if(name != NULL) {
                   4028:                        if(_stricmp(name, "CLOCK$"  ) == 0 ||
                   4029:                           _stricmp(name, "CONFIG$" ) == 0 ||
1.1.1.30  root     4030:                           _stricmp(name, "EMMXXXX0") == 0 ||
                   4031:                           _stricmp(name, "$IBMAIAS") == 0) {
1.1.1.29  root     4032:                                return(true);
                   4033:                        }
                   4034:                }
1.1.1.24  root     4035:        }
                   4036:        return(false);
1.1.1.11  root     4037: }
                   4038: 
1.1.1.29  root     4039: bool msdos_is_con_path(char *path)
1.1.1.8   root     4040: {
1.1.1.14  root     4041:        char full[MAX_PATH], *name;
1.1.1.8   root     4042:        
1.1.1.24  root     4043:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4044:                return(_stricmp(full, "\\\\.\\CON") == 0);
1.1.1.24  root     4045:        }
                   4046:        return(false);
                   4047: }
                   4048: 
1.1.1.29  root     4049: int msdos_is_comm_path(char *path)
1.1.1.24  root     4050: {
                   4051:        char full[MAX_PATH], *name;
                   4052:        
                   4053:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
1.1.1.29  root     4054:                if(_stricmp(full, "\\\\.\\COM1") == 0) {
                   4055:                        return(1);
                   4056:                } else if(_stricmp(full, "\\\\.\\COM2") == 0) {
                   4057:                        return(2);
                   4058:                } else if(_stricmp(full, "\\\\.\\COM3") == 0) {
                   4059:                        return(3);
                   4060:                } else if(_stricmp(full, "\\\\.\\COM4") == 0) {
                   4061:                        return(4);
1.1.1.24  root     4062:                }
                   4063:        }
1.1.1.29  root     4064:        return(0);
                   4065: }
                   4066: 
1.1.1.30  root     4067: int msdos_is_prn_path(char *path)
                   4068: {
                   4069:        char full[MAX_PATH], *name;
                   4070:        
                   4071:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
                   4072:                if(_stricmp(full, "\\\\.\\PRN") == 0) {
                   4073:                        return(1);
                   4074:                } else if(_stricmp(full, "\\\\.\\LPT1") == 0) {
                   4075:                        return(1);
                   4076:                } else if(_stricmp(full, "\\\\.\\LPT2") == 0) {
                   4077:                        return(2);
                   4078:                } else if(_stricmp(full, "\\\\.\\LPT3") == 0) {
                   4079:                        return(3);
                   4080:                }
                   4081:        }
                   4082:        return(0);
                   4083: }
                   4084: 
1.1.1.29  root     4085: char *msdos_create_comm_path(char *path, int port)
                   4086: {
                   4087:        static char tmp[MAX_PATH];
                   4088:        char *p = NULL;
                   4089:        
                   4090:        sprintf(tmp, "COM%d", port);
                   4091:        if((p = strchr(path, ':')) != NULL) {
                   4092:                strcat(tmp, p);
                   4093:        }
                   4094:        return(tmp);
1.1.1.24  root     4095: }
                   4096: 
                   4097: bool msdos_is_existing_file(char *path)
                   4098: {
                   4099:        // http://d.hatena.ne.jp/yu-hr/20100317/1268826458
                   4100:        WIN32_FIND_DATA FindData;
                   4101:        HANDLE hFind;
                   4102:        
                   4103:        if((hFind = FindFirstFile(path, &FindData)) != INVALID_HANDLE_VALUE) {
                   4104:                FindClose(hFind);
                   4105:                return(!(FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY));
                   4106:        }
                   4107:        return(false);
1.1.1.8   root     4108: }
                   4109: 
1.1.1.9   root     4110: char *msdos_search_command_com(char *command_path, char *env_path)
                   4111: {
                   4112:        static char tmp[MAX_PATH];
1.1.1.28  root     4113:        char path[ENV_SIZE], *file_name;
1.1.1.9   root     4114:        
1.1.1.28  root     4115:        // check if COMMAND.COM is in the same directory as the target program file
1.1.1.9   root     4116:        if(GetFullPathName(command_path, MAX_PATH, tmp, &file_name) != 0) {
                   4117:                sprintf(file_name, "COMMAND.COM");
                   4118:                if(_access(tmp, 0) == 0) {
                   4119:                        return(tmp);
                   4120:                }
                   4121:        }
1.1.1.28  root     4122:        
                   4123:        // check if COMMAND.COM is in the same directory as the running msdos.exe
1.1.1.9   root     4124:        if(GetModuleFileName(NULL, path, MAX_PATH) != 0 && GetFullPathName(path, MAX_PATH, tmp, &file_name) != 0) {
                   4125:                sprintf(file_name, "COMMAND.COM");
                   4126:                if(_access(tmp, 0) == 0) {
                   4127:                        return(tmp);
                   4128:                }
                   4129:        }
1.1.1.28  root     4130:        
                   4131:        // check if COMMAND.COM is in the current directory
1.1.1.9   root     4132:        if(GetFullPathName("COMMAND.COM", MAX_PATH, tmp, &file_name) != 0) {
                   4133:                if(_access(tmp, 0) == 0) {
                   4134:                        return(tmp);
                   4135:                }
                   4136:        }
1.1.1.28  root     4137:        
                   4138:        // cehck if COMMAND.COM is in the directory that is in MSDOS_PATH and PATH environment variables
                   4139:        strcpy(path, env_path);
                   4140:        char *token = my_strtok(path, ";");
1.1.1.9   root     4141:        while(token != NULL) {
1.1.1.14  root     4142:                if(strlen(token) != 0 && token[0] != '%') {
1.1.1.9   root     4143:                        strcpy(tmp, msdos_combine_path(token, "COMMAND.COM"));
                   4144:                        if(_access(tmp, 0) == 0) {
                   4145:                                return(tmp);
                   4146:                        }
                   4147:                }
                   4148:                token = my_strtok(NULL, ";");
                   4149:        }
                   4150:        return(NULL);
                   4151: }
                   4152: 
1.1.1.14  root     4153: int msdos_drive_number(const char *path)
1.1       root     4154: {
                   4155:        char tmp[MAX_PATH], *name;
                   4156:        
                   4157:        GetFullPathName(path, MAX_PATH, tmp, &name);
                   4158:        if(tmp[0] >= 'a' && tmp[0] <= 'z') {
                   4159:                return(tmp[0] - 'a');
                   4160:        } else {
                   4161:                return(tmp[0] - 'A');
                   4162:        }
                   4163: }
                   4164: 
                   4165: char *msdos_volume_label(char *path)
                   4166: {
                   4167:        static char tmp[MAX_PATH];
                   4168:        char volume[] = "A:\\";
                   4169:        
                   4170:        if(path[1] == ':') {
                   4171:                volume[0] = path[0];
                   4172:        } else {
                   4173:                volume[0] = 'A' + _getdrive() - 1;
                   4174:        }
                   4175:        if(!GetVolumeInformation(volume, tmp, MAX_PATH, NULL, NULL, NULL, NULL, 0)) {
                   4176:                memset(tmp, 0, sizeof(tmp));
                   4177:        }
                   4178:        return(tmp);
                   4179: }
                   4180: 
                   4181: char *msdos_short_volume_label(char *label)
                   4182: {
                   4183:        static char tmp[(8 + 1 + 3) + 1];
                   4184:        char *src = label;
                   4185:        int remain = strlen(label);
                   4186:        char *dst_n = tmp;
                   4187:        char *dst_e = tmp + 9;
                   4188:        
                   4189:        strcpy(tmp, "        .   ");
                   4190:        for(int i = 0; i < 8 && remain > 0; i++) {
                   4191:                if(msdos_lead_byte_check(*src)) {
                   4192:                        if(++i == 8) {
                   4193:                                break;
                   4194:                        }
                   4195:                        *dst_n++ = *src++;
                   4196:                        remain--;
                   4197:                }
                   4198:                *dst_n++ = *src++;
                   4199:                remain--;
                   4200:        }
                   4201:        if(remain > 0) {
                   4202:                for(int i = 0; i < 3 && remain > 0; i++) {
                   4203:                        if(msdos_lead_byte_check(*src)) {
                   4204:                                if(++i == 3) {
                   4205:                                        break;
                   4206:                                }
                   4207:                                *dst_e++ = *src++;
                   4208:                                remain--;
                   4209:                        }
                   4210:                        *dst_e++ = *src++;
                   4211:                        remain--;
                   4212:                }
                   4213:                *dst_e = '\0';
                   4214:        } else {
                   4215:                *dst_n = '\0';
                   4216:        }
                   4217:        my_strupr(tmp);
                   4218:        return(tmp);
                   4219: }
                   4220: 
1.1.1.13  root     4221: errno_t msdos_maperr(unsigned long oserrno)
                   4222: {
                   4223:        _doserrno = oserrno;
1.1.1.14  root     4224:        switch(oserrno) {
1.1.1.13  root     4225:        case ERROR_FILE_NOT_FOUND:         // 2
                   4226:        case ERROR_PATH_NOT_FOUND:         // 3
                   4227:        case ERROR_INVALID_DRIVE:          // 15
                   4228:        case ERROR_NO_MORE_FILES:          // 18
                   4229:        case ERROR_BAD_NETPATH:            // 53
                   4230:        case ERROR_BAD_NET_NAME:           // 67
                   4231:        case ERROR_BAD_PATHNAME:           // 161
                   4232:        case ERROR_FILENAME_EXCED_RANGE:   // 206
                   4233:                return ENOENT;
                   4234:        case ERROR_TOO_MANY_OPEN_FILES:    // 4
                   4235:                return EMFILE;
                   4236:        case ERROR_ACCESS_DENIED:          // 5
                   4237:        case ERROR_CURRENT_DIRECTORY:      // 16
                   4238:        case ERROR_NETWORK_ACCESS_DENIED:  // 65
                   4239:        case ERROR_CANNOT_MAKE:            // 82
                   4240:        case ERROR_FAIL_I24:               // 83
                   4241:        case ERROR_DRIVE_LOCKED:           // 108
                   4242:        case ERROR_SEEK_ON_DEVICE:         // 132
                   4243:        case ERROR_NOT_LOCKED:             // 158
                   4244:        case ERROR_LOCK_FAILED:            // 167
                   4245:                return EACCES;
                   4246:        case ERROR_INVALID_HANDLE:         // 6
                   4247:        case ERROR_INVALID_TARGET_HANDLE:  // 114
                   4248:        case ERROR_DIRECT_ACCESS_HANDLE:   // 130
                   4249:                return EBADF;
                   4250:        case ERROR_ARENA_TRASHED:          // 7
                   4251:        case ERROR_NOT_ENOUGH_MEMORY:      // 8
                   4252:        case ERROR_INVALID_BLOCK:          // 9
                   4253:        case ERROR_NOT_ENOUGH_QUOTA:       // 1816
                   4254:                return ENOMEM;
                   4255:        case ERROR_BAD_ENVIRONMENT:        // 10
                   4256:                return E2BIG;
                   4257:        case ERROR_BAD_FORMAT:             // 11
                   4258:                return ENOEXEC;
                   4259:        case ERROR_NOT_SAME_DEVICE:        // 17
                   4260:                return EXDEV;
                   4261:        case ERROR_FILE_EXISTS:            // 80
                   4262:        case ERROR_ALREADY_EXISTS:         // 183
                   4263:                return EEXIST;
                   4264:        case ERROR_NO_PROC_SLOTS:          // 89
                   4265:        case ERROR_MAX_THRDS_REACHED:      // 164
                   4266:        case ERROR_NESTING_NOT_ALLOWED:    // 215
                   4267:                return EAGAIN;
                   4268:        case ERROR_BROKEN_PIPE:            // 109
                   4269:                return EPIPE;
                   4270:        case ERROR_DISK_FULL:              // 112
                   4271:                return ENOSPC;
                   4272:        case ERROR_WAIT_NO_CHILDREN:       // 128
                   4273:        case ERROR_CHILD_NOT_COMPLETE:     // 129
                   4274:                return ECHILD;
                   4275:        case ERROR_DIR_NOT_EMPTY:          // 145
                   4276:                return ENOTEMPTY;
                   4277:        }
1.1.1.14  root     4278:        if(oserrno >= ERROR_WRITE_PROTECT /* 19 */ &&
1.1.1.13  root     4279:                oserrno <= ERROR_SHARING_BUFFER_EXCEEDED /* 36 */) {
                   4280:                return EACCES;
                   4281:        }
1.1.1.14  root     4282:        if(oserrno >= ERROR_INVALID_STARTING_CODESEG /* 188 */ &&
1.1.1.13  root     4283:                oserrno <= ERROR_INFLOOP_IN_RELOC_CHAIN /* 202 */) {
                   4284:                return ENOEXEC;
                   4285:        }
                   4286:        return EINVAL;
                   4287: }
                   4288: 
                   4289: int msdos_open(const char *filename, int oflag)
                   4290: {
1.1.1.14  root     4291:        if((oflag & (_O_RDONLY | _O_WRONLY | _O_RDWR)) != _O_RDONLY) {
1.1.1.13  root     4292:                return _open(filename, oflag);
                   4293:        }
1.1.1.14  root     4294:        
                   4295:        SECURITY_ATTRIBUTES sa = { sizeof(SECURITY_ATTRIBUTES), NULL, !(oflag & _O_NOINHERIT) };
1.1.1.13  root     4296:        DWORD disposition;
1.1.1.14  root     4297:        switch(oflag & (_O_CREAT | _O_EXCL | _O_TRUNC)) {
                   4298:        default:
1.1.1.13  root     4299:        case _O_EXCL:
                   4300:                disposition = OPEN_EXISTING;
                   4301:                break;
                   4302:        case _O_CREAT:
                   4303:                disposition = OPEN_ALWAYS;
                   4304:                break;
                   4305:        case _O_CREAT | _O_EXCL:
                   4306:        case _O_CREAT | _O_TRUNC | _O_EXCL:
                   4307:                disposition = CREATE_NEW;
                   4308:                break;
                   4309:        case _O_TRUNC:
                   4310:        case _O_TRUNC | _O_EXCL:
                   4311:                disposition = TRUNCATE_EXISTING;
                   4312:                break;
                   4313:        case _O_CREAT | _O_TRUNC:
                   4314:                disposition = CREATE_ALWAYS;
                   4315:                break;
                   4316:        }
1.1.1.14  root     4317:        
1.1.1.13  root     4318:        HANDLE h = CreateFile(filename, GENERIC_READ | FILE_WRITE_ATTRIBUTES,
                   4319:                FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
                   4320:                FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14  root     4321:        if(h == INVALID_HANDLE_VALUE) {
1.1.1.13  root     4322:                // FILE_WRITE_ATTRIBUTES may not be granted for standard users.
                   4323:                // Retry without FILE_WRITE_ATTRIBUTES.
                   4324:                h = CreateFile(filename, GENERIC_READ,
                   4325:                        FILE_SHARE_READ | FILE_SHARE_WRITE, &sa, disposition,
                   4326:                        FILE_ATTRIBUTE_NORMAL, NULL);
1.1.1.14  root     4327:                if(h == INVALID_HANDLE_VALUE) {
1.1.1.13  root     4328:                        errno = msdos_maperr(GetLastError());
                   4329:                        return -1;
                   4330:                }
                   4331:        }
1.1.1.14  root     4332:        
1.1.1.13  root     4333:        int fd = _open_osfhandle((intptr_t) h, oflag);
1.1.1.14  root     4334:        if(fd == -1) {
1.1.1.13  root     4335:                CloseHandle(h);
                   4336:        }
                   4337:        return fd;
                   4338: }
                   4339: 
1.1.1.14  root     4340: void msdos_file_handler_open(int fd, const char *path, int atty, int mode, UINT16 info, UINT16 psp_seg)
1.1       root     4341: {
                   4342:        static int id = 0;
                   4343:        char full[MAX_PATH], *name;
                   4344:        
                   4345:        if(GetFullPathName(path, MAX_PATH, full, &name) != 0) {
                   4346:                strcpy(file_handler[fd].path, full);
                   4347:        } else {
                   4348:                strcpy(file_handler[fd].path, path);
                   4349:        }
1.1.1.14  root     4350:        // isatty makes no distinction between CON & NUL
                   4351:        // GetFileSize fails on CON, succeeds on NUL
                   4352:        if(atty && (info != 0x80d3 || GetFileSize((HANDLE)_get_osfhandle(fd), NULL) == 0)) {
                   4353:                info = 0x8084;
                   4354:                atty = 0;
                   4355:        } else if(!atty && info == 0x80d3) {
                   4356:                info = msdos_drive_number(".");
                   4357:        }
1.1       root     4358:        file_handler[fd].valid = 1;
                   4359:        file_handler[fd].id = id++;     // dummy id for int 21h ax=71a6h
                   4360:        file_handler[fd].atty = atty;
                   4361:        file_handler[fd].mode = mode;
                   4362:        file_handler[fd].info = info;
                   4363:        file_handler[fd].psp = psp_seg;
1.1.1.21  root     4364:        
                   4365:        // init system file table
                   4366:        if(fd < 20) {
                   4367:                UINT8 *sft = mem + SFT_TOP + 6 + 0x3b * fd;
                   4368:                
                   4369:                memset(sft, 0, 0x3b);
                   4370:                
                   4371:                *(UINT16 *)(sft + 0x00) = 1;
                   4372:                *(UINT16 *)(sft + 0x02) = file_handler[fd].mode;
                   4373:                *(UINT8  *)(sft + 0x04) = GetFileAttributes(file_handler[fd].path) & 0xff;
                   4374:                *(UINT16 *)(sft + 0x05) = file_handler[fd].info & 0xff;
                   4375:                
                   4376:                if(!(file_handler[fd].info & 0x80)) {
                   4377:                        *(UINT16 *)(sft + 0x07) = sizeof(dpb_t) * (file_handler[fd].info & 0x1f);
                   4378:                        *(UINT16 *)(sft + 0x09) = DPB_TOP >> 4;
                   4379:                        
                   4380:                        FILETIME time, local;
                   4381:                        HANDLE hHandle;
                   4382:                        WORD dos_date = 0, dos_time = 0;
                   4383:                        DWORD file_size = 0;
                   4384:                        if((hHandle = (HANDLE)_get_osfhandle(fd)) != INVALID_HANDLE_VALUE) {
                   4385:                                if(GetFileTime(hHandle, NULL, NULL, &time)) {
                   4386:                                        FileTimeToLocalFileTime(&time, &local);
                   4387:                                        FileTimeToDosDateTime(&local, &dos_date, &dos_time);
                   4388:                                }
                   4389:                                file_size = GetFileSize(hHandle, NULL);
                   4390:                        }
                   4391:                        *(UINT16 *)(sft + 0x0d) = dos_time;
                   4392:                        *(UINT16 *)(sft + 0x0f) = dos_date;
                   4393:                        *(UINT32 *)(sft + 0x11) = file_size;
                   4394:                }
                   4395:                
                   4396:                char fname[MAX_PATH] = {0}, ext[MAX_PATH] = {0};
                   4397:                _splitpath(file_handler[fd].path, NULL, NULL, fname, ext);
                   4398:                my_strupr(fname);
                   4399:                my_strupr(ext);
                   4400:                memset(sft + 0x20, 0x20, 11);
                   4401:                memcpy(sft + 0x20, fname, min(strlen(fname), 8));
                   4402:                memcpy(sft + 0x28, ext + 1, min(strlen(ext + 1), 3));
                   4403:                
                   4404:                *(UINT16 *)(sft + 0x31) = psp_seg;
                   4405:        }
1.1       root     4406: }
                   4407: 
                   4408: void msdos_file_handler_dup(int dst, int src, UINT16 psp_seg)
                   4409: {
                   4410:        strcpy(file_handler[dst].path, file_handler[src].path);
                   4411:        file_handler[dst].valid = 1;
                   4412:        file_handler[dst].id = file_handler[src].id;
                   4413:        file_handler[dst].atty = file_handler[src].atty;
                   4414:        file_handler[dst].mode = file_handler[src].mode;
                   4415:        file_handler[dst].info = file_handler[src].info;
                   4416:        file_handler[dst].psp = psp_seg;
                   4417: }
                   4418: 
1.1.1.20  root     4419: void msdos_file_handler_close(int fd)
1.1       root     4420: {
                   4421:        file_handler[fd].valid = 0;
1.1.1.21  root     4422:        
                   4423:        if(fd < 20) {
                   4424:                memset(mem + SFT_TOP + 6 + 0x3b * fd, 0, 0x3b);
                   4425:        }
1.1       root     4426: }
                   4427: 
1.1.1.14  root     4428: inline int msdos_file_attribute_create(UINT16 new_attr)
1.1       root     4429: {
1.1.1.14  root     4430:        return(new_attr & (FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN |
                   4431:                           FILE_ATTRIBUTE_SYSTEM | FILE_ATTRIBUTE_ARCHIVE |
                   4432:                           FILE_ATTRIBUTE_DIRECTORY));
1.1       root     4433: }
                   4434: 
                   4435: // find file
                   4436: 
                   4437: int msdos_find_file_check_attribute(int attribute, int allowed_mask, int required_mask)
                   4438: {
                   4439:        if((allowed_mask & 0x08) && !(attribute & FILE_ATTRIBUTE_DIRECTORY)) {
                   4440:                return(0);      // search directory only !!!
                   4441:        } else if(!(allowed_mask & 0x02) && (attribute & FILE_ATTRIBUTE_HIDDEN)) {
                   4442:                return(0);
                   4443:        } else if(!(allowed_mask & 0x04) && (attribute & FILE_ATTRIBUTE_SYSTEM)) {
                   4444:                return(0);
                   4445:        } else if(!(allowed_mask & 0x10) && (attribute & FILE_ATTRIBUTE_DIRECTORY)) {
                   4446:                return(0);
                   4447:        } else if((attribute & required_mask) != required_mask) {
                   4448:                return(0);
                   4449:        } else {
                   4450:                return(1);
                   4451:        }
                   4452: }
                   4453: 
1.1.1.13  root     4454: int msdos_find_file_has_8dot3name(WIN32_FIND_DATA *fd)
                   4455: {
1.1.1.14  root     4456:        if(fd->cAlternateFileName[0]) {
1.1.1.13  root     4457:                return 1;
                   4458:        }
                   4459:        size_t len = strlen(fd->cFileName);
1.1.1.14  root     4460:        if(len > 12) {
1.1.1.13  root     4461:                return 0;
                   4462:        }
                   4463:        const char *ext = strrchr(fd->cFileName, '.');
1.1.1.14  root     4464:        if((ext ? ext - fd->cFileName : len) > 8) {
1.1.1.13  root     4465:                return 0;
                   4466:        }
                   4467:        return 1;
                   4468: }
                   4469: 
1.1       root     4470: void msdos_find_file_conv_local_time(WIN32_FIND_DATA *fd)
                   4471: {
                   4472:        FILETIME local;
                   4473:        
                   4474:        FileTimeToLocalFileTime(&fd->ftCreationTime, &local);
                   4475:        fd->ftCreationTime.dwLowDateTime = local.dwLowDateTime;
                   4476:        fd->ftCreationTime.dwHighDateTime = local.dwHighDateTime;
                   4477:        
                   4478:        FileTimeToLocalFileTime(&fd->ftLastAccessTime, &local);
                   4479:        fd->ftLastAccessTime.dwLowDateTime = local.dwLowDateTime;
                   4480:        fd->ftLastAccessTime.dwHighDateTime = local.dwHighDateTime;
                   4481:        
                   4482:        FileTimeToLocalFileTime(&fd->ftLastWriteTime, &local);
                   4483:        fd->ftLastWriteTime.dwLowDateTime = local.dwLowDateTime;
                   4484:        fd->ftLastWriteTime.dwHighDateTime = local.dwHighDateTime;
                   4485: }
                   4486: 
                   4487: // i/o
                   4488: 
                   4489: void msdos_stdio_reopen()
                   4490: {
                   4491:        if(!file_handler[0].valid) {
                   4492:                _dup2(DUP_STDIN, 0);
                   4493:                msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   4494:        }
                   4495:        if(!file_handler[1].valid) {
                   4496:                _dup2(DUP_STDOUT, 1);
                   4497:                msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   4498:        }
                   4499:        if(!file_handler[2].valid) {
                   4500:                _dup2(DUP_STDERR, 2);
                   4501:                msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
                   4502:        }
1.1.1.21  root     4503:        if(!file_handler[3].valid) {
                   4504:                _dup2(DUP_STDAUX, 3);
                   4505:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
                   4506:        }
                   4507:        if(!file_handler[4].valid) {
                   4508:                _dup2(DUP_STDPRN, 4);
                   4509:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0);
                   4510:        }
                   4511:        for(int i = 0; i < 5; i++) {
                   4512:                if(msdos_psp_get_file_table(i, current_psp) == 0xff) {
                   4513:                        msdos_psp_set_file_table(i, i, current_psp);
                   4514:                }
                   4515:        }
1.1       root     4516: }
                   4517: 
                   4518: int msdos_kbhit()
                   4519: {
                   4520:        msdos_stdio_reopen();
                   4521:        
1.1.1.20  root     4522:        process_t *process = msdos_process_info_get(current_psp);
                   4523:        int fd = msdos_psp_get_file_table(0, current_psp);
                   4524:        
                   4525:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     4526:                // stdin is redirected to file
1.1.1.20  root     4527:                return(eof(fd) == 0);
1.1       root     4528:        }
                   4529:        
                   4530:        // check keyboard status
1.1.1.33  root     4531:        if((key_buf_char != NULL && key_buf_char->count() != 0) || key_recv != 0) {
1.1       root     4532:                return(1);
                   4533:        } else {
                   4534:                return(_kbhit());
                   4535:        }
                   4536: }
                   4537: 
                   4538: int msdos_getch_ex(int echo)
                   4539: {
                   4540:        static char prev = 0;
                   4541:        
                   4542:        msdos_stdio_reopen();
                   4543:        
1.1.1.20  root     4544:        process_t *process = msdos_process_info_get(current_psp);
                   4545:        int fd = msdos_psp_get_file_table(0, current_psp);
                   4546:        
                   4547:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     4548:                // stdin is redirected to file
                   4549: retry:
                   4550:                char data;
1.1.1.20  root     4551:                if(_read(fd, &data, 1) == 1) {
1.1       root     4552:                        char tmp = data;
                   4553:                        if(data == 0x0a) {
                   4554:                                if(prev == 0x0d) {
                   4555:                                        goto retry; // CRLF -> skip LF
                   4556:                                } else {
                   4557:                                        data = 0x0d; // LF only -> CR
                   4558:                                }
                   4559:                        }
                   4560:                        prev = tmp;
                   4561:                        return(data);
                   4562:                }
                   4563:                return(EOF);
                   4564:        }
                   4565:        
                   4566:        // input from console
1.1.1.5   root     4567:        int key_char, key_scan;
1.1.1.33  root     4568:        if(key_recv != 0) {
1.1.1.5   root     4569:                key_char = (key_code >> 0) & 0xff;
                   4570:                key_scan = (key_code >> 8) & 0xff;
                   4571:                key_code >>= 16;
1.1.1.33  root     4572:                key_recv >>= 16;
1.1.1.5   root     4573:        } else {
1.1.1.33  root     4574:                while(key_buf_char != NULL && key_buf_char->count() == 0 && !m_halted) {
1.1.1.23  root     4575:                        if(!(fd < process->max_files && file_handler[fd].valid && file_handler[fd].atty && file_mode[file_handler[fd].mode].in)) {
                   4576:                                // NOTE: stdin is redirected to stderr when we do "type (file) | more" on freedos's command.com
                   4577:                                if(_kbhit()) {
1.1.1.32  root     4578:                                        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   4579:                                                key_buf_char->write(_getch());
                   4580:                                                key_buf_scan->write(0);
                   4581:                                        }
1.1.1.23  root     4582:                                } else {
                   4583:                                        Sleep(10);
                   4584:                                }
                   4585:                        } else {
                   4586:                                if(!update_key_buffer()) {
                   4587:                                        Sleep(10);
                   4588:                                }
1.1.1.14  root     4589:                        }
                   4590:                }
                   4591:                if(m_halted) {
1.1.1.33  root     4592:                        // insert CR to terminate input loops
1.1.1.14  root     4593:                        key_char = 0x0d;
                   4594:                        key_scan = 0;
1.1.1.32  root     4595:                } else if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.14  root     4596:                        key_char = key_buf_char->read();
                   4597:                        key_scan = key_buf_scan->read();
1.1.1.5   root     4598:                }
1.1       root     4599:        }
                   4600:        if(echo && key_char) {
                   4601:                msdos_putch(key_char);
                   4602:        }
                   4603:        return key_char ? key_char : (key_scan != 0xe0) ? key_scan : 0;
                   4604: }
                   4605: 
                   4606: inline int msdos_getch()
                   4607: {
                   4608:        return(msdos_getch_ex(0));
                   4609: }
                   4610: 
                   4611: inline int msdos_getche()
                   4612: {
                   4613:        return(msdos_getch_ex(1));
                   4614: }
                   4615: 
                   4616: int msdos_write(int fd, const void *buffer, unsigned int count)
                   4617: {
                   4618:        static int is_cr = 0;
                   4619:        
                   4620:        if(fd == 1 && !file_handler[1].atty) {
                   4621:                // CR+LF -> LF
                   4622:                UINT8 *buf = (UINT8 *)buffer;
                   4623:                for(unsigned int i = 0; i < count; i++) {
                   4624:                        UINT8 data = buf[i];
                   4625:                        if(is_cr) {
                   4626:                                if(data != 0x0a) {
                   4627:                                        UINT8 tmp = 0x0d;
                   4628:                                        _write(1, &tmp, 1);
                   4629:                                }
                   4630:                                _write(1, &data, 1);
                   4631:                                is_cr = 0;
                   4632:                        } else if(data == 0x0d) {
                   4633:                                is_cr = 1;
                   4634:                        } else {
                   4635:                                _write(1, &data, 1);
                   4636:                        }
                   4637:                }
                   4638:                return(count);
                   4639:        }
1.1.1.14  root     4640:        vram_flush();
1.1       root     4641:        return(_write(fd, buffer, count));
                   4642: }
                   4643: 
                   4644: void msdos_putch(UINT8 data)
                   4645: {
1.1.1.34! root     4646:        CONSOLE_SCREEN_BUFFER_INFO csbi;
        !          4647:        SMALL_RECT rect;
        !          4648:        COORD co;
1.1       root     4649:        static int p = 0;
                   4650:        static int is_kanji = 0;
                   4651:        static int is_esc = 0;
                   4652:        static int stored_x;
                   4653:        static int stored_y;
                   4654:        static WORD stored_a;
1.1.1.20  root     4655:        static char tmp[64], out[64];
1.1       root     4656:        
                   4657:        msdos_stdio_reopen();
                   4658:        
1.1.1.20  root     4659:        process_t *process = msdos_process_info_get(current_psp);
                   4660:        int fd = msdos_psp_get_file_table(1, current_psp);
                   4661:        
                   4662:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     4663:                // stdout is redirected to file
1.1.1.20  root     4664:                msdos_write(fd, &data, 1);
1.1       root     4665:                return;
                   4666:        }
1.1.1.23  root     4667:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     4668:        
                   4669:        // output to console
                   4670:        tmp[p++] = data;
                   4671:        
1.1.1.14  root     4672:        vram_flush();
                   4673:        
1.1       root     4674:        if(is_kanji) {
                   4675:                // kanji character
                   4676:                is_kanji = 0;
                   4677:        } else if(is_esc) {
                   4678:                // escape sequense
                   4679:                if((tmp[1] == ')' || tmp[1] == '(') && p == 3) {
                   4680:                        p = is_esc = 0;
                   4681:                } else if(tmp[1] == '=' && p == 4) {
                   4682:                        co.X = tmp[3] - 0x20;
1.1.1.14  root     4683:                        co.Y = tmp[2] - 0x20 + scr_top;
1.1       root     4684:                        SetConsoleCursorPosition(hStdout, co);
                   4685:                        mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14  root     4686:                        mem[0x451 + mem[0x462] * 2] = co.Y - scr_top;
1.1       root     4687:                        cursor_moved = false;
                   4688:                        p = is_esc = 0;
                   4689:                } else if((data >= 'a' && data <= 'z') || (data >= 'A' && data <= 'Z') || data == '*') {
                   4690:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   4691:                        co.X = csbi.dwCursorPosition.X;
                   4692:                        co.Y = csbi.dwCursorPosition.Y;
                   4693:                        WORD wAttributes = csbi.wAttributes;
                   4694:                        
                   4695:                        if(tmp[1] == 'D') {
                   4696:                                co.Y++;
                   4697:                        } else if(tmp[1] == 'E') {
                   4698:                                co.X = 0;
                   4699:                                co.Y++;
                   4700:                        } else if(tmp[1] == 'M') {
                   4701:                                co.Y--;
                   4702:                        } else if(tmp[1] == '*') {
1.1.1.14  root     4703:                                SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   4704:                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   4705:                                co.X = 0;
                   4706:                                co.Y = csbi.srWindow.Top;
1.1       root     4707:                        } else if(tmp[1] == '[') {
                   4708:                                int param[256], params = 0;
                   4709:                                memset(param, 0, sizeof(param));
                   4710:                                for(int i = 2; i < p; i++) {
                   4711:                                        if(tmp[i] >= '0' && tmp[i] <= '9') {
                   4712:                                                param[params] *= 10;
                   4713:                                                param[params] += tmp[i] - '0';
                   4714:                                        } else {
                   4715:                                                params++;
                   4716:                                        }
                   4717:                                }
                   4718:                                if(data == 'A') {
1.1.1.14  root     4719:                                        co.Y -= (params == 0) ? 1 : param[0];
1.1       root     4720:                                } else if(data == 'B') {
1.1.1.14  root     4721:                                        co.Y += (params == 0) ? 1 : param[0];
1.1       root     4722:                                } else if(data == 'C') {
1.1.1.14  root     4723:                                        co.X += (params == 0) ? 1 : param[0];
1.1       root     4724:                                } else if(data == 'D') {
1.1.1.14  root     4725:                                        co.X -= (params == 0) ? 1 : param[0];
1.1       root     4726:                                } else if(data == 'H' || data == 'f') {
1.1.1.14  root     4727:                                        co.X = (param[1] == 0 ? 1 : param[1]) - 1;
                   4728:                                        co.Y = (param[0] == 0 ? 1 : param[0]) - 1 + csbi.srWindow.Top;
1.1       root     4729:                                } else if(data == 'J') {
1.1.1.14  root     4730:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     4731:                                        if(param[0] == 0) {
                   4732:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     4733:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   4734:                                                if(co.Y < csbi.srWindow.Bottom) {
                   4735:                                                        SET_RECT(rect, 0, co.Y + 1, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   4736:                                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     4737:                                                }
                   4738:                                        } else if(param[0] == 1) {
1.1.1.14  root     4739:                                                if(co.Y > csbi.srWindow.Top) {
                   4740:                                                        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, co.Y - 1);
                   4741:                                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     4742:                                                }
                   4743:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.14  root     4744:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     4745:                                        } else if(param[0] == 2) {
1.1.1.14  root     4746:                                                SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   4747:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     4748:                                                co.X = co.Y = 0;
                   4749:                                        }
                   4750:                                } else if(data == 'K') {
1.1.1.14  root     4751:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     4752:                                        if(param[0] == 0) {
                   4753:                                                SET_RECT(rect, co.X, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     4754:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     4755:                                        } else if(param[0] == 1) {
                   4756:                                                SET_RECT(rect, 0, co.Y, co.X, co.Y);
1.1.1.14  root     4757:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     4758:                                        } else if(param[0] == 2) {
                   4759:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y);
1.1.1.14  root     4760:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     4761:                                        }
                   4762:                                } else if(data == 'L') {
1.1.1.14  root     4763:                                        if(params == 0) {
                   4764:                                                param[0] = 1;
1.1       root     4765:                                        }
1.1.1.14  root     4766:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   4767:                                        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   4768:                                        SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   4769:                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   4770:                                        clear_scr_buffer(csbi.wAttributes);
1.1       root     4771:                                        SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, co.Y + param[0] - 1);
1.1.1.14  root     4772:                                        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     4773:                                        co.X = 0;
                   4774:                                } else if(data == 'M') {
1.1.1.14  root     4775:                                        if(params == 0) {
                   4776:                                                param[0] = 1;
                   4777:                                        }
                   4778:                                        if(co.Y + param[0] > csbi.srWindow.Bottom) {
                   4779:                                                clear_scr_buffer(csbi.wAttributes);
                   4780:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   4781:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     4782:                                        } else {
1.1.1.14  root     4783:                                                SET_RECT(rect, 0, co.Y + param[0], csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   4784:                                                ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   4785:                                                SET_RECT(rect, 0, co.Y, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   4786:                                                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   4787:                                                clear_scr_buffer(csbi.wAttributes);
1.1       root     4788:                                        }
                   4789:                                        co.X = 0;
                   4790:                                } else if(data == 'h') {
                   4791:                                        if(tmp[2] == '>' && tmp[3] == '5') {
                   4792:                                                CONSOLE_CURSOR_INFO cur;
                   4793:                                                GetConsoleCursorInfo(hStdout, &cur);
                   4794:                                                if(cur.bVisible) {
                   4795:                                                        cur.bVisible = FALSE;
1.1.1.14  root     4796: //                                                     SetConsoleCursorInfo(hStdout, &cur);
1.1       root     4797:                                                }
                   4798:                                        }
                   4799:                                } else if(data == 'l') {
                   4800:                                        if(tmp[2] == '>' && tmp[3] == '5') {
                   4801:                                                CONSOLE_CURSOR_INFO cur;
                   4802:                                                GetConsoleCursorInfo(hStdout, &cur);
                   4803:                                                if(!cur.bVisible) {
                   4804:                                                        cur.bVisible = TRUE;
1.1.1.14  root     4805: //                                                     SetConsoleCursorInfo(hStdout, &cur);
1.1       root     4806:                                                }
                   4807:                                        }
                   4808:                                } else if(data == 'm') {
                   4809:                                        wAttributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
                   4810:                                        int reverse = 0, hidden = 0;
                   4811:                                        for(int i = 0; i < params; i++) {
                   4812:                                                if(param[i] == 1) {
                   4813:                                                        wAttributes |= FOREGROUND_INTENSITY;
                   4814:                                                } else if(param[i] == 4) {
                   4815:                                                        wAttributes |= COMMON_LVB_UNDERSCORE;
                   4816:                                                } else if(param[i] == 7) {
                   4817:                                                        reverse = 1;
                   4818:                                                } else if(param[i] == 8 || param[i] == 16) {
                   4819:                                                        hidden = 1;
                   4820:                                                } else if((param[i] >= 17 && param[i] <= 23) || (param[i] >= 30 && param[i] <= 37)) {
                   4821:                                                        wAttributes &= ~(FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
                   4822:                                                        if(param[i] >= 17 && param[i] <= 23) {
                   4823:                                                                param[i] -= 16;
                   4824:                                                        } else {
                   4825:                                                                param[i] -= 30;
                   4826:                                                        }
                   4827:                                                        if(param[i] & 1) {
                   4828:                                                                wAttributes |= FOREGROUND_RED;
                   4829:                                                        }
                   4830:                                                        if(param[i] & 2) {
                   4831:                                                                wAttributes |= FOREGROUND_GREEN;
                   4832:                                                        }
                   4833:                                                        if(param[i] & 4) {
                   4834:                                                                wAttributes |= FOREGROUND_BLUE;
                   4835:                                                        }
                   4836:                                                } else if(param[i] >= 40 && param[i] <= 47) {
                   4837:                                                        wAttributes &= ~(BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE);
                   4838:                                                        if((param[i] - 40) & 1) {
                   4839:                                                                wAttributes |= BACKGROUND_RED;
                   4840:                                                        }
                   4841:                                                        if((param[i] - 40) & 2) {
                   4842:                                                                wAttributes |= BACKGROUND_GREEN;
                   4843:                                                        }
                   4844:                                                        if((param[i] - 40) & 4) {
                   4845:                                                                wAttributes |= BACKGROUND_BLUE;
                   4846:                                                        }
                   4847:                                                }
                   4848:                                        }
                   4849:                                        if(reverse) {
                   4850:                                                wAttributes &= ~0xff;
                   4851:                                                wAttributes |= BACKGROUND_RED | BACKGROUND_GREEN | BACKGROUND_BLUE;
                   4852:                                        }
                   4853:                                        if(hidden) {
                   4854:                                                wAttributes &= ~0x0f;
                   4855:                                                wAttributes |= (wAttributes >> 4) & 0x0f;
                   4856:                                        }
                   4857:                                } else if(data == 'n') {
                   4858:                                        if(param[0] == 6) {
                   4859:                                                char tmp[16];
                   4860:                                                sprintf(tmp, "\x1b[%d;%dR", co.Y + 1, co.X + 1);
                   4861:                                                int len = strlen(tmp);
1.1.1.32  root     4862:                                                if(key_buf_char != NULL && key_buf_scan != NULL) {
                   4863:                                                        for(int i = 0; i < len; i++) {
                   4864:                                                                key_buf_char->write(tmp[i]);
                   4865:                                                                key_buf_scan->write(0x00);
                   4866:                                                        }
1.1       root     4867:                                                }
                   4868:                                        }
                   4869:                                } else if(data == 's') {
                   4870:                                        stored_x = co.X;
                   4871:                                        stored_y = co.Y;
                   4872:                                        stored_a = wAttributes;
                   4873:                                } else if(data == 'u') {
                   4874:                                        co.X = stored_x;
                   4875:                                        co.Y = stored_y;
                   4876:                                        wAttributes = stored_a;
                   4877:                                }
                   4878:                        }
                   4879:                        if(co.X < 0) {
                   4880:                                co.X = 0;
                   4881:                        } else if(co.X >= csbi.dwSize.X) {
                   4882:                                co.X = csbi.dwSize.X - 1;
                   4883:                        }
1.1.1.14  root     4884:                        if(co.Y < csbi.srWindow.Top) {
                   4885:                                co.Y = csbi.srWindow.Top;
                   4886:                        } else if(co.Y > csbi.srWindow.Bottom) {
                   4887:                                co.Y = csbi.srWindow.Bottom;
1.1       root     4888:                        }
                   4889:                        if(co.X != csbi.dwCursorPosition.X || co.Y != csbi.dwCursorPosition.Y) {
                   4890:                                SetConsoleCursorPosition(hStdout, co);
                   4891:                                mem[0x450 + mem[0x462] * 2] = co.X;
1.1.1.14  root     4892:                                mem[0x451 + mem[0x462] * 2] = co.Y - csbi.srWindow.Top;
1.1       root     4893:                                cursor_moved = false;
                   4894:                        }
                   4895:                        if(wAttributes != csbi.wAttributes) {
                   4896:                                SetConsoleTextAttribute(hStdout, wAttributes);
                   4897:                        }
                   4898:                        p = is_esc = 0;
                   4899:                }
                   4900:                return;
                   4901:        } else {
                   4902:                if(msdos_lead_byte_check(data)) {
                   4903:                        is_kanji = 1;
                   4904:                        return;
                   4905:                } else if(data == 0x1b) {
                   4906:                        is_esc = 1;
                   4907:                        return;
                   4908:                }
                   4909:        }
1.1.1.20  root     4910:        
                   4911:        DWORD q = 0, num;
                   4912:        is_kanji = 0;
                   4913:        for(int i = 0; i < p; i++) {
                   4914:                UINT8 c = tmp[i];
                   4915:                if(is_kanji) {
                   4916:                        is_kanji = 0;
                   4917:                } else if(msdos_lead_byte_check(data)) {
                   4918:                        is_kanji = 1;
                   4919:                } else if(msdos_ctrl_code_check(data)) {
                   4920:                        out[q++] = '^';
                   4921:                        c += 'A' - 1;
                   4922:                }
                   4923:                out[q++] = c;
                   4924:        }
1.1.1.34! root     4925:        if(q == 1 && out[0] == 0x08) {
        !          4926:                // back space
        !          4927:                GetConsoleScreenBufferInfo(hStdout, &csbi);
        !          4928:                if(csbi.dwCursorPosition.X > 0) {
        !          4929:                        co.X = csbi.dwCursorPosition.X - 1;
        !          4930:                        co.Y = csbi.dwCursorPosition.Y;
        !          4931:                        SetConsoleCursorPosition(hStdout, co);
        !          4932:                } else if(csbi.dwCursorPosition.Y > 0) {
        !          4933:                        co.X = csbi.dwSize.X - 1;
        !          4934:                        co.Y = csbi.dwCursorPosition.Y - 1;
        !          4935:                        SetConsoleCursorPosition(hStdout, co);
        !          4936:                } else {
        !          4937:                        WriteConsole(hStdout, out, q, &num, NULL); // to make sure
        !          4938:                }
        !          4939:        } else {
        !          4940:                WriteConsole(hStdout, out, q, &num, NULL);
        !          4941:        }
1.1       root     4942:        p = 0;
1.1.1.14  root     4943:        
1.1.1.15  root     4944:        if(!restore_console_on_exit) {
                   4945:                GetConsoleScreenBufferInfo(hStdout, &csbi);
                   4946:                scr_top = csbi.srWindow.Top;
                   4947:        }
1.1       root     4948:        cursor_moved = true;
                   4949: }
                   4950: 
                   4951: int msdos_aux_in()
                   4952: {
1.1.1.21  root     4953:        msdos_stdio_reopen();
                   4954:        
1.1.1.20  root     4955:        process_t *process = msdos_process_info_get(current_psp);
                   4956:        int fd = msdos_psp_get_file_table(3, current_psp);
                   4957:        
                   4958:        if(fd < process->max_files && file_handler[fd].valid && !eof(fd)) {
1.1       root     4959:                char data = 0;
1.1.1.20  root     4960:                _read(fd, &data, 1);
1.1       root     4961:                return(data);
                   4962:        } else {
                   4963:                return(EOF);
                   4964:        }
                   4965: }
                   4966: 
                   4967: void msdos_aux_out(char data)
                   4968: {
1.1.1.21  root     4969:        msdos_stdio_reopen();
                   4970:        
1.1.1.20  root     4971:        process_t *process = msdos_process_info_get(current_psp);
                   4972:        int fd = msdos_psp_get_file_table(3, current_psp);
                   4973:        
                   4974:        if(fd < process->max_files && file_handler[fd].valid) {
                   4975:                msdos_write(fd, &data, 1);
1.1       root     4976:        }
                   4977: }
                   4978: 
                   4979: void msdos_prn_out(char data)
                   4980: {
1.1.1.21  root     4981:        msdos_stdio_reopen();
                   4982:        
1.1.1.20  root     4983:        process_t *process = msdos_process_info_get(current_psp);
                   4984:        int fd = msdos_psp_get_file_table(4, current_psp);
                   4985:        
                   4986:        if(fd < process->max_files && file_handler[fd].valid) {
                   4987:                msdos_write(fd, &data, 1);
1.1       root     4988:        }
                   4989: }
                   4990: 
                   4991: // memory control
                   4992: 
1.1.1.19  root     4993: mcb_t *msdos_mcb_create(int mcb_seg, UINT8 mz, UINT16 psp, int paragraphs)
1.1       root     4994: {
                   4995:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   4996:        
                   4997:        mcb->mz = mz;
                   4998:        mcb->psp = psp;
1.1.1.30  root     4999:        mcb->paragraphs = paragraphs;
1.1       root     5000:        return(mcb);
                   5001: }
                   5002: 
                   5003: void msdos_mcb_check(mcb_t *mcb)
                   5004: {
                   5005:        if(!(mcb->mz == 'M' || mcb->mz == 'Z')) {
1.1.1.28  root     5006:                #if 0
                   5007:                        // shutdown now !!!
                   5008:                        fatalerror("broken memory control block\n");
                   5009:                #else
                   5010:                        // return error code and continue
                   5011:                        throw(0x07); // broken memory control block
                   5012:                #endif
1.1       root     5013:        }
                   5014: }
                   5015: 
                   5016: int msdos_mem_split(int seg, int paragraphs)
                   5017: {
                   5018:        int mcb_seg = seg - 1;
                   5019:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5020:        msdos_mcb_check(mcb);
                   5021:        
1.1.1.30  root     5022:        if(mcb->paragraphs > paragraphs) {
1.1       root     5023:                int new_seg = mcb_seg + 1 + paragraphs;
1.1.1.30  root     5024:                int new_paragraphs = mcb->paragraphs - paragraphs - 1;
1.1       root     5025:                
                   5026:                msdos_mcb_create(new_seg, mcb->mz, 0, new_paragraphs);
                   5027:                mcb->mz = 'M';
1.1.1.30  root     5028:                mcb->paragraphs = paragraphs;
1.1       root     5029:                return(0);
                   5030:        }
                   5031:        return(-1);
                   5032: }
                   5033: 
                   5034: void msdos_mem_merge(int seg)
                   5035: {
                   5036:        int mcb_seg = seg - 1;
                   5037:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5038:        msdos_mcb_check(mcb);
                   5039:        
                   5040:        while(1) {
                   5041:                if(mcb->mz == 'Z') {
                   5042:                        break;
                   5043:                }
1.1.1.30  root     5044:                int next_seg = mcb_seg + 1 + mcb->paragraphs;
1.1       root     5045:                mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5046:                msdos_mcb_check(next_mcb);
                   5047:                
                   5048:                if(next_mcb->psp != 0) {
                   5049:                        break;
                   5050:                }
                   5051:                mcb->mz = next_mcb->mz;
1.1.1.30  root     5052:                mcb->paragraphs = mcb->paragraphs + 1 + next_mcb->paragraphs;
1.1       root     5053:        }
                   5054: }
                   5055: 
1.1.1.8   root     5056: int msdos_mem_alloc(int mcb_seg, int paragraphs, int new_process)
1.1       root     5057: {
                   5058:        while(1) {
                   5059:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     5060:                bool last_block;
1.1       root     5061:                
1.1.1.14  root     5062:                if(mcb->psp == 0) {
                   5063:                        msdos_mem_merge(mcb_seg + 1);
                   5064:                } else {
                   5065:                        msdos_mcb_check(mcb);
                   5066:                }
1.1.1.33  root     5067:                if(!(last_block = (mcb->mz == 'Z'))) {
                   5068:                        // check if the next is dummy mcb to link to umb
                   5069:                        int next_seg = mcb_seg + 1 + mcb->paragraphs;
                   5070:                        mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5071:                        last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
                   5072:                }
                   5073:                if(!(new_process && !last_block)) {
1.1.1.30  root     5074:                        if(mcb->psp == 0 && mcb->paragraphs >= paragraphs) {
1.1       root     5075:                                msdos_mem_split(mcb_seg + 1, paragraphs);
                   5076:                                mcb->psp = current_psp;
                   5077:                                return(mcb_seg + 1);
                   5078:                        }
                   5079:                }
                   5080:                if(mcb->mz == 'Z') {
                   5081:                        break;
                   5082:                }
1.1.1.30  root     5083:                mcb_seg += 1 + mcb->paragraphs;
1.1       root     5084:        }
                   5085:        return(-1);
                   5086: }
                   5087: 
                   5088: int msdos_mem_realloc(int seg, int paragraphs, int *max_paragraphs)
                   5089: {
                   5090:        int mcb_seg = seg - 1;
                   5091:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5092:        msdos_mcb_check(mcb);
1.1.1.30  root     5093:        int current_paragraphs = mcb->paragraphs;
1.1       root     5094:        
                   5095:        msdos_mem_merge(seg);
1.1.1.30  root     5096:        if(paragraphs > mcb->paragraphs) {
1.1.1.14  root     5097:                if(max_paragraphs) {
1.1.1.30  root     5098:                        *max_paragraphs = mcb->paragraphs;
1.1.1.14  root     5099:                }
1.1       root     5100:                msdos_mem_split(seg, current_paragraphs);
                   5101:                return(-1);
                   5102:        }
                   5103:        msdos_mem_split(seg, paragraphs);
                   5104:        return(0);
                   5105: }
                   5106: 
                   5107: void msdos_mem_free(int seg)
                   5108: {
                   5109:        int mcb_seg = seg - 1;
                   5110:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5111:        msdos_mcb_check(mcb);
                   5112:        
                   5113:        mcb->psp = 0;
                   5114:        msdos_mem_merge(seg);
                   5115: }
                   5116: 
1.1.1.8   root     5117: int msdos_mem_get_free(int mcb_seg, int new_process)
1.1       root     5118: {
                   5119:        int max_paragraphs = 0;
                   5120:        
                   5121:        while(1) {
                   5122:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     5123:                bool last_block;
                   5124:                
1.1       root     5125:                msdos_mcb_check(mcb);
                   5126:                
1.1.1.33  root     5127:                if(!(last_block = (mcb->mz == 'Z'))) {
                   5128:                        // check if the next is dummy mcb to link to umb
                   5129:                        int next_seg = mcb_seg + 1 + mcb->paragraphs;
                   5130:                        mcb_t *next_mcb = (mcb_t *)(mem + (next_seg << 4));
                   5131:                        last_block = (next_mcb->mz == 'Z' && next_mcb->paragraphs == 0);
                   5132:                }
                   5133:                if(!(new_process && !last_block)) {
1.1.1.30  root     5134:                        if(mcb->psp == 0 && mcb->paragraphs > max_paragraphs) {
                   5135:                                max_paragraphs = mcb->paragraphs;
1.1       root     5136:                        }
                   5137:                }
                   5138:                if(mcb->mz == 'Z') {
                   5139:                        break;
                   5140:                }
1.1.1.30  root     5141:                mcb_seg += 1 + mcb->paragraphs;
1.1       root     5142:        }
1.1.1.14  root     5143:        return(max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs);
1.1       root     5144: }
                   5145: 
1.1.1.8   root     5146: int msdos_mem_get_last_mcb(int mcb_seg, UINT16 psp)
                   5147: {
                   5148:        int last_seg = -1;
                   5149:        
                   5150:        while(1) {
                   5151:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   5152:                msdos_mcb_check(mcb);
                   5153:                
1.1.1.14  root     5154:                if(mcb->psp == psp) {
1.1.1.8   root     5155:                        last_seg = mcb_seg;
                   5156:                }
1.1.1.14  root     5157:                if(mcb->mz == 'Z') {
                   5158:                        break;
                   5159:                }
1.1.1.30  root     5160:                mcb_seg += 1 + mcb->paragraphs;
1.1.1.8   root     5161:        }
                   5162:        return(last_seg);
                   5163: }
                   5164: 
1.1.1.19  root     5165: int msdos_mem_get_umb_linked()
                   5166: {
1.1.1.33  root     5167:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   5168:        msdos_mcb_check(mcb);
1.1.1.19  root     5169:        
1.1.1.33  root     5170:        if(mcb->mz == 'M') {
                   5171:                return(-1);
1.1.1.19  root     5172:        }
                   5173:        return(0);
                   5174: }
                   5175: 
1.1.1.33  root     5176: void msdos_mem_link_umb()
1.1.1.19  root     5177: {
1.1.1.33  root     5178:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   5179:        msdos_mcb_check(mcb);
1.1.1.19  root     5180:        
1.1.1.33  root     5181:        mcb->mz = 'M';
                   5182:        mcb->paragraphs = (UMB_TOP >> 4) - (MEMORY_END >> 4);
1.1.1.19  root     5183: }
                   5184: 
1.1.1.33  root     5185: void msdos_mem_unlink_umb()
1.1.1.19  root     5186: {
1.1.1.33  root     5187:        mcb_t *mcb = (mcb_t *)(mem + MEMORY_END - 16);
                   5188:        msdos_mcb_check(mcb);
1.1.1.19  root     5189:        
1.1.1.33  root     5190:        mcb->mz = 'Z';
                   5191:        mcb->paragraphs = 0;
1.1.1.19  root     5192: }
                   5193: 
1.1.1.29  root     5194: #ifdef SUPPORT_HMA
                   5195: 
                   5196: hma_mcb_t *msdos_hma_mcb_create(int offset, int owner, int size, int next)
                   5197: {
                   5198:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5199:        
                   5200:        mcb->ms[0] = 'M';
                   5201:        mcb->ms[1] = 'S';
                   5202:        mcb->owner = owner;
                   5203:        mcb->size = size;
                   5204:        mcb->next = next;
                   5205:        return(mcb);
                   5206: }
                   5207: 
                   5208: bool msdos_is_hma_mcb_valid(hma_mcb_t *mcb)
                   5209: {
                   5210:        return(mcb->ms[0] == 'M' && mcb->ms[1] == 'S');
                   5211: }
                   5212: 
                   5213: int msdos_hma_mem_split(int offset, int size)
                   5214: {
                   5215:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5216:        
                   5217:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5218:                return(-1);
                   5219:        }
                   5220:        if(mcb->size >= size + 0x10) {
                   5221:                int new_offset = offset + 0x10 + size;
                   5222:                int new_size = mcb->size - 0x10 - size;
                   5223:                
                   5224:                msdos_hma_mcb_create(new_offset, 0, new_size, mcb->next);
                   5225:                mcb->size = size;
                   5226:                mcb->next = new_offset;
                   5227:                return(0);
                   5228:        }
                   5229:        return(-1);
                   5230: }
                   5231: 
                   5232: void msdos_hma_mem_merge(int offset)
                   5233: {
                   5234:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5235:        
                   5236:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5237:                return;
                   5238:        }
                   5239:        while(1) {
                   5240:                if(mcb->next == 0) {
                   5241:                        break;
                   5242:                }
                   5243:                hma_mcb_t *next_mcb = (hma_mcb_t *)(mem + 0xffff0 + mcb->next);
                   5244:                
                   5245:                if(!msdos_is_hma_mcb_valid(next_mcb)) {
                   5246:                        return;
                   5247:                }
                   5248:                if(next_mcb->owner != 0) {
                   5249:                        break;
                   5250:                }
                   5251:                mcb->size += 0x10 + next_mcb->size;
                   5252:                mcb->next = next_mcb->next;
                   5253:        }
                   5254: }
                   5255: 
                   5256: int msdos_hma_mem_alloc(int size, UINT16 owner)
                   5257: {
                   5258:        int offset = 0x10; // first mcb in HMA
                   5259:        
                   5260:        while(1) {
                   5261:                hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5262:                
                   5263:                if(!msdos_is_hma_mcb_valid(mcb)) {
                   5264:                        return(-1);
                   5265:                }
                   5266:                if(mcb->owner == 0) {
                   5267:                        msdos_hma_mem_merge(offset);
                   5268:                }
                   5269:                if(mcb->owner == 0 && mcb->size >= size) {
                   5270:                        msdos_hma_mem_split(offset, size);
                   5271:                        mcb->owner = owner;
                   5272:                        return(offset);
                   5273:                }
                   5274:                if(mcb->next == 0) {
                   5275:                        break;
                   5276:                }
                   5277:                offset = mcb->next;
                   5278:        }
                   5279:        return(-1);
                   5280: }
                   5281: 
                   5282: int msdos_hma_mem_realloc(int offset, int size)
                   5283: {
                   5284:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5285:        
                   5286:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5287:                return(-1);
                   5288:        }
                   5289:        if(mcb->size < size) {
                   5290:                return(-1);
                   5291:        }
                   5292:        msdos_hma_mem_split(offset, size);
                   5293:        return(0);
                   5294: }
                   5295: 
                   5296: void msdos_hma_mem_free(int offset)
                   5297: {
                   5298:        hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5299:        
                   5300:        if(!msdos_is_hma_mcb_valid(mcb)) {
                   5301:                return;
                   5302:        }
                   5303:        mcb->owner = 0;
                   5304:        msdos_hma_mem_merge(offset);
                   5305: }
                   5306: 
                   5307: int msdos_hma_mem_get_free(int *available_offset)
                   5308: {
                   5309:        int offset = 0x10; // first mcb in HMA
                   5310:        int size = 0;
                   5311:        
                   5312:        while(1) {
                   5313:                hma_mcb_t *mcb = (hma_mcb_t *)(mem + 0xffff0 + offset);
                   5314:                
                   5315:                if(!msdos_is_hma_mcb_valid(mcb)) {
                   5316:                        return(0);
                   5317:                }
                   5318:                if(mcb->owner == 0 && size < mcb->size) {
                   5319:                        if(available_offset != NULL) {
                   5320:                                *available_offset = offset;
                   5321:                        }
                   5322:                        size = mcb->size;
                   5323:                }
                   5324:                if(mcb->next == 0) {
                   5325:                        break;
                   5326:                }
                   5327:                offset = mcb->next;
                   5328:        }
                   5329:        return(size);
                   5330: }
                   5331: 
                   5332: #endif
                   5333: 
1.1       root     5334: // environment
                   5335: 
                   5336: void msdos_env_set_argv(int env_seg, char *argv)
                   5337: {
                   5338:        char *dst = (char *)(mem + (env_seg << 4));
                   5339:        
                   5340:        while(1) {
                   5341:                if(dst[0] == 0) {
                   5342:                        break;
                   5343:                }
                   5344:                dst += strlen(dst) + 1;
                   5345:        }
                   5346:        *dst++ = 0; // end of environment
                   5347:        *dst++ = 1; // top of argv[0]
                   5348:        *dst++ = 0;
                   5349:        memcpy(dst, argv, strlen(argv));
                   5350:        dst += strlen(argv);
                   5351:        *dst++ = 0;
                   5352:        *dst++ = 0;
                   5353: }
                   5354: 
                   5355: char *msdos_env_get_argv(int env_seg)
                   5356: {
                   5357:        static char env[ENV_SIZE];
                   5358:        char *src = env;
                   5359:        
                   5360:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   5361:        while(1) {
                   5362:                if(src[0] == 0) {
                   5363:                        if(src[1] == 1) {
                   5364:                                return(src + 3);
                   5365:                        }
                   5366:                        break;
                   5367:                }
                   5368:                src += strlen(src) + 1;
                   5369:        }
                   5370:        return(NULL);
                   5371: }
                   5372: 
                   5373: char *msdos_env_get(int env_seg, const char *name)
                   5374: {
                   5375:        static char env[ENV_SIZE];
                   5376:        char *src = env;
                   5377:        
                   5378:        memcpy(src, mem + (env_seg << 4), ENV_SIZE);
                   5379:        while(1) {
                   5380:                if(src[0] == 0) {
                   5381:                        break;
                   5382:                }
                   5383:                int len = strlen(src);
                   5384:                char *n = my_strtok(src, "=");
                   5385:                char *v = src + strlen(n) + 1;
                   5386:                
                   5387:                if(_stricmp(name, n) == 0) {
                   5388:                        return(v);
                   5389:                }
                   5390:                src += len + 1;
                   5391:        }
                   5392:        return(NULL);
                   5393: }
                   5394: 
                   5395: void msdos_env_set(int env_seg, char *name, char *value)
                   5396: {
                   5397:        char env[ENV_SIZE];
                   5398:        char *src = env;
                   5399:        char *dst = (char *)(mem + (env_seg << 4));
                   5400:        char *argv = msdos_env_get_argv(env_seg);
                   5401:        int done = 0;
                   5402:        
                   5403:        memcpy(src, dst, ENV_SIZE);
                   5404:        memset(dst, 0, ENV_SIZE);
                   5405:        while(1) {
                   5406:                if(src[0] == 0) {
                   5407:                        break;
                   5408:                }
                   5409:                int len = strlen(src);
                   5410:                char *n = my_strtok(src, "=");
                   5411:                char *v = src + strlen(n) + 1;
                   5412:                char tmp[1024];
                   5413:                
                   5414:                if(_stricmp(name, n) == 0) {
                   5415:                        sprintf(tmp, "%s=%s", n, value);
                   5416:                        done = 1;
                   5417:                } else {
                   5418:                        sprintf(tmp, "%s=%s", n, v);
                   5419:                }
                   5420:                memcpy(dst, tmp, strlen(tmp));
                   5421:                dst += strlen(tmp) + 1;
                   5422:                src += len + 1;
                   5423:        }
                   5424:        if(!done) {
                   5425:                char tmp[1024];
                   5426:                
                   5427:                sprintf(tmp, "%s=%s", name, value);
                   5428:                memcpy(dst, tmp, strlen(tmp));
                   5429:                dst += strlen(tmp) + 1;
                   5430:        }
                   5431:        if(argv) {
                   5432:                *dst++ = 0; // end of environment
                   5433:                *dst++ = 1; // top of argv[0]
                   5434:                *dst++ = 0;
                   5435:                memcpy(dst, argv, strlen(argv));
                   5436:                dst += strlen(argv);
                   5437:                *dst++ = 0;
                   5438:                *dst++ = 0;
                   5439:        }
                   5440: }
                   5441: 
                   5442: // process
                   5443: 
1.1.1.8   root     5444: psp_t *msdos_psp_create(int psp_seg, UINT16 mcb_seg, UINT16 parent_psp, UINT16 env_seg)
1.1       root     5445: {
                   5446:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   5447:        
                   5448:        memset(psp, 0, PSP_SIZE);
                   5449:        psp->exit[0] = 0xcd;
                   5450:        psp->exit[1] = 0x20;
1.1.1.8   root     5451:        psp->first_mcb = mcb_seg;
1.1       root     5452:        psp->far_call = 0xea;
                   5453:        psp->cpm_entry.w.l = 0xfff1;    // int 21h, retf
                   5454:        psp->cpm_entry.w.h = 0xf000;
                   5455:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   5456:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   5457:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   5458:        psp->parent_psp = parent_psp;
1.1.1.20  root     5459:        if(parent_psp == (UINT16)-1) {
                   5460:                for(int i = 0; i < 20; i++) {
                   5461:                        if(file_handler[i].valid) {
                   5462:                                psp->file_table[i] = i;
                   5463:                        } else {
                   5464:                                psp->file_table[i] = 0xff;
                   5465:                        }
1.1       root     5466:                }
1.1.1.20  root     5467:        } else {
                   5468:                memcpy(psp->file_table, ((psp_t *)(mem + (parent_psp << 4)))->file_table, 20);
1.1       root     5469:        }
                   5470:        psp->env_seg = env_seg;
                   5471:        psp->stack.w.l = REG16(SP);
1.1.1.3   root     5472:        psp->stack.w.h = SREG(SS);
1.1.1.14  root     5473:        psp->file_table_size = 20;
                   5474:        psp->file_table_ptr.w.l = 0x18;
                   5475:        psp->file_table_ptr.w.h = psp_seg;
1.1       root     5476:        psp->service[0] = 0xcd;
                   5477:        psp->service[1] = 0x21;
                   5478:        psp->service[2] = 0xcb;
                   5479:        return(psp);
                   5480: }
                   5481: 
1.1.1.20  root     5482: void msdos_psp_set_file_table(int fd, UINT8 value, int psp_seg)
                   5483: {
                   5484:        if(psp_seg && fd < 20) {
                   5485:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   5486:                psp->file_table[fd] = value;
                   5487:        }
                   5488: }
                   5489: 
                   5490: int msdos_psp_get_file_table(int fd, int psp_seg)
                   5491: {
                   5492:        if(psp_seg && fd < 20) {
                   5493:                psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   5494:                fd = psp->file_table[fd];
                   5495:        }
                   5496:        return fd;
                   5497: }
                   5498: 
1.1       root     5499: int msdos_process_exec(char *cmd, param_block_t *param, UINT8 al)
                   5500: {
                   5501:        // load command file
                   5502:        int fd = -1;
                   5503:        int dos_command = 0;
1.1.1.24  root     5504:        char command[MAX_PATH], path[MAX_PATH], opt[MAX_PATH], *name = NULL, name_tmp[MAX_PATH];
1.1       root     5505:        
                   5506:        int opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   5507:        int opt_len = mem[opt_ofs];
                   5508:        memset(opt, 0, sizeof(opt));
                   5509:        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   5510:        
1.1.1.14  root     5511:        if(strlen(cmd) >= 5 && _stricmp(&cmd[strlen(cmd) - 4], ".BAT") == 0) {
                   5512:                // this is a batch file, run command.com
                   5513:                char tmp[MAX_PATH];
                   5514:                if(opt_len != 0) {
                   5515:                        sprintf(tmp, "/C %s %s", cmd, opt);
                   5516:                } else {
                   5517:                        sprintf(tmp, "/C %s", cmd);
                   5518:                }
                   5519:                strcpy(opt, tmp);
                   5520:                opt_len = strlen(opt);
                   5521:                mem[opt_ofs] = opt_len;
                   5522:                sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   5523:                strcpy(command, comspec_path);
                   5524:                strcpy(name_tmp, "COMMAND.COM");
                   5525:        } else {
                   5526:                if(_stricmp(cmd, "C:\\COMMAND.COM") == 0) {
                   5527:                        // redirect C:\COMMAND.COM to comspec_path
                   5528:                        strcpy(command, comspec_path);
                   5529:                } else {
                   5530:                        strcpy(command, cmd);
                   5531:                }
1.1.1.24  root     5532:                if(GetFullPathName(command, MAX_PATH, path, &name) == 0) {
                   5533:                        return(-1);
                   5534:                }
1.1.1.14  root     5535:                memset(name_tmp, 0, sizeof(name_tmp));
                   5536:                strcpy(name_tmp, name);
                   5537:                
                   5538:                // check command.com
                   5539:                if(_stricmp(name, "COMMAND.COM") == 0 || _stricmp(name, "COMMAND") == 0) {
                   5540:                        if(opt_len == 0) {
                   5541: //                             process_t *current_process = msdos_process_info_get(current_psp);
                   5542:                                process_t *current_process = NULL;
                   5543:                                for(int i = 0; i < MAX_PROCESS; i++) {
                   5544:                                        if(process[i].psp == current_psp) {
                   5545:                                                current_process = &process[i];
                   5546:                                                break;
                   5547:                                        }
                   5548:                                }
                   5549:                                if(current_process != NULL) {
                   5550:                                        param->cmd_line.dw = current_process->dta.dw;
                   5551:                                        opt_ofs = (param->cmd_line.w.h << 4) + param->cmd_line.w.l;
                   5552:                                        opt_len = mem[opt_ofs];
                   5553:                                        memset(opt, 0, sizeof(opt));
                   5554:                                        memcpy(opt, mem + opt_ofs + 1, opt_len);
                   5555:                                }
                   5556:                        }
                   5557:                        for(int i = 0; i < opt_len; i++) {
                   5558:                                if(opt[i] == ' ') {
                   5559:                                        continue;
                   5560:                                }
                   5561:                                if(opt[i] == '/' && (opt[i + 1] == 'c' || opt[i + 1] == 'C') && opt[i + 2] == ' ') {
                   5562:                                        for(int j = i + 3; j < opt_len; j++) {
                   5563:                                                if(opt[j] == ' ') {
                   5564:                                                        continue;
                   5565:                                                }
                   5566:                                                char *token = my_strtok(opt + j, " ");
                   5567:                                                
                   5568:                                                if(strlen(token) >= 5 && _stricmp(&token[strlen(token) - 4], ".BAT") == 0) {
                   5569:                                                        // this is a batch file, okay to run command.com
                   5570:                                                } else {
                   5571:                                                        // run program directly without command.com
                   5572:                                                        strcpy(command, token);
                   5573:                                                        char tmp[MAX_PATH];
                   5574:                                                        strcpy(tmp, token + strlen(token) + 1);
                   5575:                                                        strcpy(opt, tmp);
                   5576:                                                        opt_len = strlen(opt);
                   5577:                                                        mem[opt_ofs] = opt_len;
                   5578:                                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   5579:                                                        dos_command = 1;
                   5580:                                                }
                   5581:                                                break;
1.1       root     5582:                                        }
                   5583:                                }
1.1.1.14  root     5584:                                break;
1.1       root     5585:                        }
                   5586:                }
                   5587:        }
                   5588:        
                   5589:        // load command file
                   5590:        strcpy(path, command);
                   5591:        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   5592:                sprintf(path, "%s.COM", command);
                   5593:                if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
                   5594:                        sprintf(path, "%s.EXE", command);
                   5595:                        if((fd = _open(path, _O_RDONLY | _O_BINARY)) == -1) {
1.1.1.14  root     5596:                                sprintf(path, "%s.BAT", command);
                   5597:                                if(_access(path, 0) == 0) {
                   5598:                                        // this is a batch file, run command.com
                   5599:                                        char tmp[MAX_PATH];
                   5600:                                        if(opt_len != 0) {
                   5601:                                                sprintf(tmp, "/C %s %s", path, opt);
                   5602:                                        } else {
                   5603:                                                sprintf(tmp, "/C %s", path);
                   5604:                                        }
                   5605:                                        strcpy(opt, tmp);
                   5606:                                        opt_len = strlen(opt);
                   5607:                                        mem[opt_ofs] = opt_len;
                   5608:                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   5609:                                        strcpy(path, comspec_path);
                   5610:                                        strcpy(name_tmp, "COMMAND.COM");
                   5611:                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   5612:                                } else {
                   5613:                                        // search path in parent environments
                   5614:                                        psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
                   5615:                                        char *env = msdos_env_get(parent_psp->env_seg, "PATH");
                   5616:                                        if(env != NULL) {
                   5617:                                                char env_path[4096];
                   5618:                                                strcpy(env_path, env);
                   5619:                                                char *token = my_strtok(env_path, ";");
                   5620:                                                
                   5621:                                                while(token != NULL) {
                   5622:                                                        if(strlen(token) != 0) {
                   5623:                                                                sprintf(path, "%s", msdos_combine_path(token, command));
                   5624:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   5625:                                                                        break;
                   5626:                                                                }
                   5627:                                                                sprintf(path, "%s.COM", msdos_combine_path(token, command));
                   5628:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   5629:                                                                        break;
                   5630:                                                                }
                   5631:                                                                sprintf(path, "%s.EXE", msdos_combine_path(token, command));
                   5632:                                                                if((fd = _open(path, _O_RDONLY | _O_BINARY)) != -1) {
                   5633:                                                                        break;
                   5634:                                                                }
                   5635:                                                                sprintf(path, "%s.BAT", msdos_combine_path(token, command));
                   5636:                                                                if(_access(path, 0) == 0) {
                   5637:                                                                        // this is a batch file, run command.com
                   5638:                                                                        char tmp[MAX_PATH];
                   5639:                                                                        if(opt_len != 0) {
                   5640:                                                                                sprintf(tmp, "/C %s %s", path, opt);
                   5641:                                                                        } else {
                   5642:                                                                                sprintf(tmp, "/C %s", path);
                   5643:                                                                        }
                   5644:                                                                        strcpy(opt, tmp);
                   5645:                                                                        opt_len = strlen(opt);
                   5646:                                                                        mem[opt_ofs] = opt_len;
                   5647:                                                                        sprintf((char *)(mem + opt_ofs + 1), "%s\x0d", opt);
                   5648:                                                                        strcpy(path, comspec_path);
                   5649:                                                                        strcpy(name_tmp, "COMMAND.COM");
                   5650:                                                                        fd = _open(path, _O_RDONLY | _O_BINARY);
                   5651:                                                                        break;
                   5652:                                                                }
1.1.1.8   root     5653:                                                        }
1.1.1.14  root     5654:                                                        token = my_strtok(NULL, ";");
1.1       root     5655:                                                }
                   5656:                                        }
                   5657:                                }
                   5658:                        }
                   5659:                }
                   5660:        }
                   5661:        if(fd == -1) {
                   5662:                if(dos_command) {
                   5663:                        // may be dos command
                   5664:                        char tmp[MAX_PATH];
                   5665:                        sprintf(tmp, "%s %s", command, opt);
                   5666:                        system(tmp);
                   5667:                        return(0);
                   5668:                } else {
                   5669:                        return(-1);
                   5670:                }
                   5671:        }
                   5672:        _read(fd, file_buffer, sizeof(file_buffer));
                   5673:        _close(fd);
                   5674:        
                   5675:        // copy environment
1.1.1.29  root     5676:        int umb_linked, env_seg, psp_seg;
1.1       root     5677:        
1.1.1.29  root     5678:        if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   5679:                msdos_mem_unlink_umb();
                   5680:        }
1.1.1.8   root     5681:        if((env_seg = msdos_mem_alloc(first_mcb, ENV_SIZE >> 4, 1)) == -1) {
1.1.1.29  root     5682:                if((env_seg = msdos_mem_alloc(UMB_TOP >> 4, ENV_SIZE >> 4, 1)) == -1) {
                   5683:                        if(umb_linked != 0) {
                   5684:                                msdos_mem_link_umb();
                   5685:                        }
                   5686:                        return(-1);
                   5687:                }
1.1       root     5688:        }
                   5689:        if(param->env_seg == 0) {
                   5690:                psp_t *parent_psp = (psp_t *)(mem + (current_psp << 4));
                   5691:                memcpy(mem + (env_seg << 4), mem + (parent_psp->env_seg << 4), ENV_SIZE);
                   5692:        } else {
                   5693:                memcpy(mem + (env_seg << 4), mem + (param->env_seg << 4), ENV_SIZE);
                   5694:        }
                   5695:        msdos_env_set_argv(env_seg, msdos_short_full_path(path));
                   5696:        
                   5697:        // check exe header
                   5698:        exe_header_t *header = (exe_header_t *)file_buffer;
1.1.1.8   root     5699:        int paragraphs, free_paragraphs = msdos_mem_get_free(first_mcb, 1);
1.1       root     5700:        UINT16 cs, ss, ip, sp;
                   5701:        
                   5702:        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   5703:                // memory allocation
                   5704:                int header_size = header->header_size * 16;
                   5705:                int load_size = header->pages * 512 - header_size;
                   5706:                if(header_size + load_size < 512) {
                   5707:                        load_size = 512 - header_size;
                   5708:                }
                   5709:                paragraphs = (PSP_SIZE + load_size) >> 4;
                   5710:                if(paragraphs + header->min_alloc > free_paragraphs) {
                   5711:                        msdos_mem_free(env_seg);
                   5712:                        return(-1);
                   5713:                }
                   5714:                paragraphs += header->max_alloc ? header->max_alloc : header->min_alloc;
                   5715:                if(paragraphs > free_paragraphs) {
                   5716:                        paragraphs = free_paragraphs;
                   5717:                }
1.1.1.8   root     5718:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29  root     5719:                        if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
                   5720:                                if(umb_linked != 0) {
                   5721:                                        msdos_mem_link_umb();
                   5722:                                }
                   5723:                                msdos_mem_free(env_seg);
                   5724:                                return(-1);
                   5725:                        }
1.1       root     5726:                }
                   5727:                // relocation
                   5728:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   5729:                for(int i = 0; i < header->relocations; i++) {
                   5730:                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   5731:                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   5732:                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   5733:                }
                   5734:                memcpy(mem + (start_seg << 4), file_buffer + header_size, load_size);
                   5735:                // segments
                   5736:                cs = header->init_cs + start_seg;
                   5737:                ss = header->init_ss + start_seg;
                   5738:                ip = header->init_ip;
                   5739:                sp = header->init_sp - 2; // for symdeb
                   5740:        } else {
                   5741:                // memory allocation
                   5742:                paragraphs = free_paragraphs;
1.1.1.8   root     5743:                if((psp_seg = msdos_mem_alloc(first_mcb, paragraphs, 1)) == -1) {
1.1.1.29  root     5744:                        if((psp_seg = msdos_mem_alloc(UMB_TOP >> 4, paragraphs, 1)) == -1) {
                   5745:                                if(umb_linked != 0) {
                   5746:                                        msdos_mem_link_umb();
                   5747:                                }
                   5748:                                msdos_mem_free(env_seg);
                   5749:                                return(-1);
                   5750:                        }
1.1       root     5751:                }
                   5752:                int start_seg = psp_seg + (PSP_SIZE >> 4);
                   5753:                memcpy(mem + (start_seg << 4), file_buffer, 0x10000 - PSP_SIZE);
                   5754:                // segments
                   5755:                cs = ss = psp_seg;
                   5756:                ip = 0x100;
                   5757:                sp = 0xfffe;
                   5758:        }
1.1.1.29  root     5759:        if(umb_linked != 0) {
                   5760:                msdos_mem_link_umb();
                   5761:        }
1.1       root     5762:        
                   5763:        // create psp
1.1.1.3   root     5764:        *(UINT16 *)(mem + 4 * 0x22 + 0) = m_eip;
                   5765:        *(UINT16 *)(mem + 4 * 0x22 + 2) = SREG(CS);
1.1       root     5766:        psp_t *psp = msdos_psp_create(psp_seg, psp_seg + paragraphs, current_psp, env_seg);
                   5767:        memcpy(psp->fcb1, mem + (param->fcb1.w.h << 4) + param->fcb1.w.l, sizeof(psp->fcb1));
                   5768:        memcpy(psp->fcb2, mem + (param->fcb2.w.h << 4) + param->fcb2.w.l, sizeof(psp->fcb2));
                   5769:        memcpy(psp->buffer, mem + (param->cmd_line.w.h << 4) + param->cmd_line.w.l, sizeof(psp->buffer));
                   5770:        
                   5771:        mcb_t *mcb_env = (mcb_t *)(mem + ((env_seg - 1) << 4));
                   5772:        mcb_t *mcb_psp = (mcb_t *)(mem + ((psp_seg - 1) << 4));
                   5773:        mcb_psp->psp = mcb_env->psp = psp_seg;
                   5774:        
1.1.1.4   root     5775:        for(int i = 0; i < 8; i++) {
                   5776:                if(name_tmp[i] == '.') {
                   5777:                        mcb_psp->prog_name[i] = '\0';
                   5778:                        break;
                   5779:                } else if(i < 7 && msdos_lead_byte_check(name_tmp[i])) {
                   5780:                        mcb_psp->prog_name[i] = name_tmp[i];
                   5781:                        i++;
                   5782:                        mcb_psp->prog_name[i] = name_tmp[i];
                   5783:                } else if(name_tmp[i] >= 'a' && name_tmp[i] <= 'z') {
                   5784:                        mcb_psp->prog_name[i] = name_tmp[i] - 'a' + 'A';
                   5785:                } else {
                   5786:                        mcb_psp->prog_name[i] = name_tmp[i];
                   5787:                }
                   5788:        }
                   5789:        
1.1       root     5790:        // process info
                   5791:        process_t *process = msdos_process_info_create(psp_seg);
                   5792:        strcpy(process->module_dir, msdos_short_full_dir(path));
1.1.1.33  root     5793: #ifdef USE_DEBUGGER
                   5794:        strcpy(process->module_path, path);
                   5795: #endif
1.1       root     5796:        process->dta.w.l = 0x80;
                   5797:        process->dta.w.h = psp_seg;
                   5798:        process->switchar = '/';
                   5799:        process->max_files = 20;
                   5800:        process->parent_int_10h_feh_called = int_10h_feh_called;
                   5801:        process->parent_int_10h_ffh_called = int_10h_ffh_called;
1.1.1.14  root     5802:        process->parent_ds = SREG(DS);
1.1.1.31  root     5803:        process->parent_es = SREG(ES);
1.1       root     5804:        
                   5805:        current_psp = psp_seg;
1.1.1.23  root     5806:        msdos_sda_update(current_psp);
1.1       root     5807:        
                   5808:        if(al == 0x00) {
                   5809:                int_10h_feh_called = int_10h_ffh_called = false;
                   5810:                
                   5811:                // registers and segments
                   5812:                REG16(AX) = REG16(BX) = 0x00;
                   5813:                REG16(CX) = 0xff;
                   5814:                REG16(DX) = psp_seg;
                   5815:                REG16(SI) = ip;
                   5816:                REG16(DI) = sp;
                   5817:                REG16(SP) = sp;
1.1.1.3   root     5818:                SREG(DS) = SREG(ES) = psp_seg;
                   5819:                SREG(SS) = ss;
                   5820:                i386_load_segment_descriptor(DS);
                   5821:                i386_load_segment_descriptor(ES);
                   5822:                i386_load_segment_descriptor(SS);
1.1       root     5823:                
                   5824:                *(UINT16 *)(mem + (ss << 4) + sp) = 0;
                   5825:                i386_jmp_far(cs, ip);
                   5826:        } else if(al == 0x01) {
                   5827:                // copy ss:sp and cs:ip to param block
                   5828:                param->sp = sp;
                   5829:                param->ss = ss;
                   5830:                param->ip = ip;
                   5831:                param->cs = cs;
1.1.1.31  root     5832:                
                   5833:                // the AX value to be passed to the child program is put on top of the child's stack
                   5834:                *(UINT16 *)(mem + (ss << 4) + sp) = REG16(AX);
1.1       root     5835:        }
                   5836:        return(0);
                   5837: }
                   5838: 
                   5839: void msdos_process_terminate(int psp_seg, int ret, int mem_free)
                   5840: {
                   5841:        psp_t *psp = (psp_t *)(mem + (psp_seg << 4));
                   5842:        
                   5843:        *(UINT32 *)(mem + 4 * 0x22) = psp->int_22h.dw;
                   5844:        *(UINT32 *)(mem + 4 * 0x23) = psp->int_23h.dw;
                   5845:        *(UINT32 *)(mem + 4 * 0x24) = psp->int_24h.dw;
                   5846:        
1.1.1.3   root     5847:        SREG(SS) = psp->stack.w.h;
                   5848:        i386_load_segment_descriptor(SS);
1.1       root     5849:        REG16(SP) = psp->stack.w.l;
                   5850:        i386_jmp_far(psp->int_22h.w.h, psp->int_22h.w.l);
                   5851:        
1.1.1.28  root     5852: //     process_t *current_process = msdos_process_info_get(psp_seg);
                   5853:        process_t *current_process = NULL;
                   5854:        for(int i = 0; i < MAX_PROCESS; i++) {
                   5855:                if(process[i].psp == psp_seg) {
                   5856:                        current_process = &process[i];
                   5857:                        break;
                   5858:                }
                   5859:        }
                   5860:        if(current_process == NULL) {
                   5861:                throw(0x1f); // general failure
                   5862:        }
                   5863:        int_10h_feh_called = current_process->parent_int_10h_feh_called;
                   5864:        int_10h_ffh_called = current_process->parent_int_10h_ffh_called;
                   5865:        if(current_process->called_by_int2eh) {
                   5866:                REG16(AX) = ret;
                   5867:        }
                   5868:        SREG(DS) = current_process->parent_ds;
1.1.1.31  root     5869:        SREG(ES) = current_process->parent_es;
1.1.1.14  root     5870:        i386_load_segment_descriptor(DS);
1.1.1.31  root     5871:        i386_load_segment_descriptor(ES);
1.1       root     5872:        
                   5873:        if(mem_free) {
1.1.1.8   root     5874:                int mcb_seg;
                   5875:                while((mcb_seg = msdos_mem_get_last_mcb(first_mcb, psp_seg)) != -1) {
                   5876:                        msdos_mem_free(mcb_seg + 1);
                   5877:                }
                   5878:                while((mcb_seg = msdos_mem_get_last_mcb(UMB_TOP >> 4, psp_seg)) != -1) {
                   5879:                        msdos_mem_free(mcb_seg + 1);
                   5880:                }
1.1       root     5881:                
                   5882:                for(int i = 0; i < MAX_FILES; i++) {
                   5883:                        if(file_handler[i].valid && file_handler[i].psp == psp_seg) {
                   5884:                                _close(i);
1.1.1.20  root     5885:                                msdos_file_handler_close(i);
                   5886:                                msdos_psp_set_file_table(i, 0x0ff, psp_seg); // FIXME: consider duplicated file handles
1.1       root     5887:                        }
                   5888:                }
1.1.1.13  root     5889:                msdos_dta_info_free(psp_seg);
1.1       root     5890:        }
1.1.1.14  root     5891:        msdos_stdio_reopen();
1.1       root     5892:        
1.1.1.28  root     5893:        memset(current_process, 0, sizeof(process_t));
1.1       root     5894:        
                   5895:        current_psp = psp->parent_psp;
                   5896:        retval = ret;
1.1.1.23  root     5897:        msdos_sda_update(current_psp);
1.1       root     5898: }
                   5899: 
                   5900: // drive
                   5901: 
                   5902: int msdos_drive_param_block_update(int drive_num, UINT16 *seg, UINT16 *ofs, int force_update)
                   5903: {
                   5904:        *seg = DPB_TOP >> 4;
                   5905:        *ofs = sizeof(dpb_t) * drive_num;
                   5906:        dpb_t *dpb = (dpb_t *)(mem + (*seg << 4) + *ofs);
                   5907:        
                   5908:        if(!force_update && dpb->free_clusters != 0) {
                   5909:                return(dpb->bytes_per_sector ? 1 : 0);
                   5910:        }
                   5911:        memset(dpb, 0, sizeof(dpb_t));
                   5912:        
                   5913:        int res = 0;
                   5914:        char dev[64];
                   5915:        sprintf(dev, "\\\\.\\%c:", 'A' + drive_num);
                   5916:        
1.1.1.17  root     5917:        HANDLE hFile = CreateFile(dev, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
1.1       root     5918:        if(hFile != INVALID_HANDLE_VALUE) {
                   5919:                DISK_GEOMETRY geo;
                   5920:                DWORD dwSize;
                   5921:                if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &geo, sizeof(geo), &dwSize, NULL)) {
                   5922:                        dpb->bytes_per_sector = (UINT16)geo.BytesPerSector;
                   5923:                        dpb->highest_sector_num = (UINT8)(geo.SectorsPerTrack - 1);
                   5924:                        dpb->highest_cluster_num = (UINT16)(geo.TracksPerCylinder * geo.Cylinders.QuadPart + 1);
1.1.1.14  root     5925:                        dpb->maximum_cluster_num = (UINT32)(geo.TracksPerCylinder * geo.Cylinders.QuadPart + 1);
1.1       root     5926:                        switch(geo.MediaType) {
                   5927:                        case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   5928:                                dpb->media_type = 0xff;
                   5929:                                break;
                   5930:                        case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   5931:                                dpb->media_type = 0xfe;
                   5932:                                break;
                   5933:                        case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   5934:                                dpb->media_type = 0xfd;
                   5935:                                break;
                   5936:                        case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   5937:                                dpb->media_type = 0xfc;
                   5938:                                break;
                   5939:                        case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   5940:                        case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   5941:                                dpb->media_type = 0xf9;
                   5942:                                break;
                   5943:                        case FixedMedia:        // hard disk
                   5944:                        case RemovableMedia:
1.1.1.19  root     5945:                        case Unknown:
1.1       root     5946:                                dpb->media_type = 0xf8;
                   5947:                                break;
                   5948:                        default:
                   5949:                                dpb->media_type = 0xf0;
                   5950:                                break;
                   5951:                        }
                   5952:                        res = 1;
                   5953:                }
                   5954:                dpb->drive_num = drive_num;
                   5955:                dpb->unit_num = drive_num;
                   5956:                dpb->next_dpb_ofs = *ofs + sizeof(dpb_t);
                   5957:                dpb->next_dpb_seg = *seg;
1.1.1.14  root     5958:                dpb->info_sector = 0xffff;
                   5959:                dpb->backup_boot_sector = 0xffff;
1.1       root     5960:                dpb->free_clusters = 0xffff;
1.1.1.14  root     5961:                dpb->free_search_cluster = 0xffffffff;
1.1       root     5962:                CloseHandle(hFile);
                   5963:        }
                   5964:        return(res);
                   5965: }
                   5966: 
                   5967: // pc bios
                   5968: 
1.1.1.19  root     5969: UINT32 get_ticks_since_midnight(UINT32 cur_msec)
                   5970: {
                   5971:        static unsigned __int64 start_msec_since_midnight = 0;
                   5972:        static unsigned __int64 start_msec_since_hostboot = 0;
                   5973:        
                   5974:        if(start_msec_since_midnight == 0) {
                   5975:                SYSTEMTIME time;
                   5976:                GetLocalTime(&time);
                   5977:                start_msec_since_midnight = ((time.wHour * 60 + time.wMinute) * 60 + time.wSecond) * 1000 + time.wMilliseconds;
                   5978:                start_msec_since_hostboot = cur_msec;
                   5979:        }
                   5980:        unsigned __int64 msec = (start_msec_since_midnight + cur_msec - start_msec_since_hostboot) % (24 * 60 * 60 * 1000);
                   5981:        unsigned __int64 tick = msec * 0x1800b0 / (24 * 60 * 60 * 1000);
                   5982:        return (UINT32)tick;
                   5983: }
                   5984: 
                   5985: void pcbios_update_daily_timer_counter(UINT32 cur_msec)
                   5986: {
                   5987:        UINT32 prev_tick = *(UINT32 *)(mem + 0x46c);
                   5988:        UINT32 next_tick = get_ticks_since_midnight(cur_msec);
                   5989:        
                   5990:        if(prev_tick > next_tick) {
                   5991:                mem[0x470] = 1;
                   5992:        }
                   5993:        *(UINT32 *)(mem + 0x46c) = next_tick;
                   5994: }
                   5995: 
1.1.1.14  root     5996: inline void pcbios_irq0()
                   5997: {
                   5998:        //++*(UINT32 *)(mem + 0x46c);
1.1.1.19  root     5999:        pcbios_update_daily_timer_counter(timeGetTime());
1.1.1.14  root     6000: }
                   6001: 
1.1.1.16  root     6002: int pcbios_get_text_vram_address(int page)
1.1       root     6003: {
                   6004:        if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     6005:                return TEXT_VRAM_TOP;
1.1       root     6006:        } else {
1.1.1.14  root     6007:                return TEXT_VRAM_TOP + VIDEO_REGEN * (page % vram_pages);
1.1       root     6008:        }
                   6009: }
                   6010: 
1.1.1.16  root     6011: int pcbios_get_shadow_buffer_address(int page)
1.1       root     6012: {
1.1.1.14  root     6013:        if(!int_10h_feh_called) {
1.1.1.16  root     6014:                return pcbios_get_text_vram_address(page);
1.1.1.14  root     6015:        } else if(/*mem[0x449] == 0x03 || */mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     6016:                return SHADOW_BUF_TOP;
                   6017:        } else {
1.1.1.14  root     6018:                return SHADOW_BUF_TOP + VIDEO_REGEN * (page % vram_pages);
1.1.1.8   root     6019:        }
                   6020: }
                   6021: 
1.1.1.16  root     6022: int pcbios_get_shadow_buffer_address(int page, int x, int y)
1.1.1.8   root     6023: {
1.1.1.16  root     6024:        return pcbios_get_shadow_buffer_address(page) + (x + y * scr_width) * 2;
1.1       root     6025: }
                   6026: 
1.1.1.16  root     6027: void pcbios_set_console_size(int width, int height, bool clr_screen)
1.1       root     6028: {
1.1.1.14  root     6029:        // clear the existing screen, not just the new one
                   6030:        int clr_height = max(height, scr_height);
                   6031:        
1.1.1.16  root     6032:        if(scr_width != width || scr_height != height) {
                   6033:                change_console_size(width, height);
1.1.1.14  root     6034:        }
                   6035:        mem[0x462] = 0;
                   6036:        *(UINT16 *)(mem + 0x44e) = 0;
                   6037:        
1.1.1.16  root     6038:        text_vram_top_address = pcbios_get_text_vram_address(0);
                   6039:        text_vram_end_address = text_vram_top_address + width * height * 2;
                   6040:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(0);
                   6041:        shadow_buffer_end_address = shadow_buffer_top_address + width * height * 2;
1.1       root     6042:        
1.1.1.23  root     6043:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.16  root     6044:        if(clr_screen) {
1.1.1.14  root     6045:                for(int ofs = shadow_buffer_top_address; ofs < shadow_buffer_end_address;) {
                   6046:                        mem[ofs++] = 0x20;
                   6047:                        mem[ofs++] = 0x07;
                   6048:                }
                   6049:                
                   6050:                EnterCriticalSection(&vram_crit_sect);
                   6051:                for(int y = 0; y < clr_height; y++) {
                   6052:                        for(int x = 0; x < scr_width; x++) {
                   6053:                                SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6054:                                SCR_BUF(y,x).Attributes = FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE;
1.1       root     6055:                        }
                   6056:                }
                   6057:                SMALL_RECT rect;
1.1.1.14  root     6058:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + clr_height - 1);
                   6059:                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   6060:                vram_length_char = vram_last_length_char = 0;
                   6061:                vram_length_attr = vram_last_length_attr = 0;
                   6062:                LeaveCriticalSection(&vram_crit_sect);
1.1       root     6063:        }
1.1.1.14  root     6064:        COORD co;
                   6065:        co.X = 0;
                   6066:        co.Y = scr_top;
                   6067:        SetConsoleCursorPosition(hStdout, co);
                   6068:        cursor_moved = true;
                   6069:        SetConsoleTextAttribute(hStdout, FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
1.1       root     6070: }
                   6071: 
1.1.1.16  root     6072: inline void pcbios_int_10h_00h()
                   6073: {
                   6074:        switch(REG8(AL) & 0x7f) {
                   6075:        case 0x70: // v-text mode
                   6076:        case 0x71: // extended cga v-text mode
                   6077:                pcbios_set_console_size(scr_width, scr_height, !(REG8(AL) & 0x80));
                   6078:                break;
                   6079:        default:
                   6080:                pcbios_set_console_size(80, 25, !(REG8(AL) & 0x80));
                   6081:                break;
                   6082:        }
                   6083:        if(REG8(AL) & 0x80) {
                   6084:                mem[0x487] |= 0x80;
                   6085:        } else {
                   6086:                mem[0x487] &= ~0x80;
                   6087:        }
                   6088:        mem[0x449] = REG8(AL) & 0x7f;
                   6089: }
                   6090: 
1.1       root     6091: inline void pcbios_int_10h_01h()
                   6092: {
1.1.1.13  root     6093:        mem[0x460] = REG8(CL);
                   6094:        mem[0x461] = REG8(CH);
1.1.1.14  root     6095:        
1.1.1.23  root     6096:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6097:        CONSOLE_CURSOR_INFO ci;
                   6098:        GetConsoleCursorInfo(hStdout, &ci);
                   6099: //     ci.bVisible = !(REG8(CH) & 0x60) && REG8(CH) <= REG8(CL);
                   6100: //     if(ci.bVisible) {
                   6101:                int lines = max(8, REG8(CL) + 1);
                   6102:                ci.dwSize = (REG8(CL) - REG8(CH) + 1) * 100 / lines;
                   6103: //     }
                   6104:        SetConsoleCursorInfo(hStdout, &ci);
1.1       root     6105: }
                   6106: 
                   6107: inline void pcbios_int_10h_02h()
                   6108: {
1.1.1.14  root     6109:        // continuously setting the cursor effectively stops it blinking
                   6110:        if(mem[0x462] == REG8(BH) && (REG8(DL) != mem[0x450 + REG8(BH) * 2] || REG8(DH) != mem[0x451 + REG8(BH) * 2])) {
1.1       root     6111:                COORD co;
                   6112:                co.X = REG8(DL);
1.1.1.14  root     6113:                co.Y = REG8(DH) + scr_top;
                   6114:                
                   6115:                // some programs hide the cursor by moving it off screen
                   6116:                static bool hidden = false;
1.1.1.23  root     6117:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6118:                CONSOLE_CURSOR_INFO ci;
                   6119:                GetConsoleCursorInfo(hStdout, &ci);
                   6120:                
                   6121:                if(REG8(DH) >= scr_height || !SetConsoleCursorPosition(hStdout, co)) {
                   6122:                        if(ci.bVisible) {
                   6123:                                ci.bVisible = FALSE;
                   6124: //                             SetConsoleCursorInfo(hStdout, &ci);
                   6125:                                hidden = true;
                   6126:                        }
                   6127:                } else if(hidden) {
                   6128:                        if(!ci.bVisible) {
                   6129:                                ci.bVisible = TRUE;
                   6130: //                             SetConsoleCursorInfo(hStdout, &ci);
                   6131:                        }
                   6132:                        hidden = false;
                   6133:                }
1.1       root     6134:        }
1.1.1.14  root     6135:        mem[0x450 + (REG8(BH) % vram_pages) * 2] = REG8(DL);
                   6136:        mem[0x451 + (REG8(BH) % vram_pages) * 2] = REG8(DH);
1.1       root     6137: }
                   6138: 
                   6139: inline void pcbios_int_10h_03h()
                   6140: {
1.1.1.14  root     6141:        REG8(DL) = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   6142:        REG8(DH) = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     6143:        REG8(CL) = mem[0x460];
                   6144:        REG8(CH) = mem[0x461];
                   6145: }
                   6146: 
                   6147: inline void pcbios_int_10h_05h()
                   6148: {
1.1.1.14  root     6149:        if(REG8(AL) >= vram_pages) {
                   6150:                return;
                   6151:        }
                   6152:        if(mem[0x462] != REG8(AL)) {
                   6153:                vram_flush();
                   6154:                
1.1.1.23  root     6155:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6156:                SMALL_RECT rect;
1.1.1.14  root     6157:                SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   6158:                ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     6159:                
1.1.1.16  root     6160:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(mem[0x462]); y < scr_height; y++) {
1.1.1.14  root     6161:                        for(int x = 0; x < scr_width; x++) {
                   6162:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   6163:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     6164:                        }
                   6165:                }
1.1.1.16  root     6166:                for(int y = 0, ofs = pcbios_get_shadow_buffer_address(REG8(AL)); y < scr_height; y++) {
1.1.1.14  root     6167:                        for(int x = 0; x < scr_width; x++) {
                   6168:                                SCR_BUF(y,x).Char.AsciiChar = mem[ofs++];
                   6169:                                SCR_BUF(y,x).Attributes = mem[ofs++];
1.1       root     6170:                        }
                   6171:                }
1.1.1.14  root     6172:                WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     6173:                
                   6174:                COORD co;
1.1.1.14  root     6175:                co.X = mem[0x450 + REG8(AL) * 2];
                   6176:                co.Y = mem[0x451 + REG8(AL) * 2] + scr_top;
                   6177:                if(co.Y < scr_top + scr_height) {
                   6178:                        SetConsoleCursorPosition(hStdout, co);
                   6179:                }
1.1       root     6180:        }
1.1.1.14  root     6181:        mem[0x462] = REG8(AL);
                   6182:        *(UINT16 *)(mem + 0x44e) = REG8(AL) * VIDEO_REGEN;
                   6183:        int regen = min(scr_width * scr_height * 2, 0x8000);
1.1.1.16  root     6184:        text_vram_top_address = pcbios_get_text_vram_address(mem[0x462]);
1.1.1.14  root     6185:        text_vram_end_address = text_vram_top_address + regen;
1.1.1.16  root     6186:        shadow_buffer_top_address = pcbios_get_shadow_buffer_address(mem[0x462]);
1.1.1.14  root     6187:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
1.1       root     6188: }
                   6189: 
                   6190: inline void pcbios_int_10h_06h()
                   6191: {
1.1.1.14  root     6192:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   6193:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   6194:                return;
                   6195:        }
                   6196:        vram_flush();
                   6197:        
1.1.1.23  root     6198:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6199:        SMALL_RECT rect;
1.1.1.14  root     6200:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   6201:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   6202:        
                   6203:        int right = min(REG8(DL), scr_width - 1);
                   6204:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     6205:        
                   6206:        if(REG8(AL) == 0) {
1.1.1.14  root     6207:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     6208:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     6209:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6210:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     6211:                        }
                   6212:                }
                   6213:        } else {
1.1.1.14  root     6214:                for(int y = REG8(CH), y2 = min(REG8(CH) + REG8(AL), bottom + 1); y <= bottom; y++, y2++) {
1.1.1.16  root     6215:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     6216:                                if(y2 <= bottom) {
                   6217:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     6218:                                } else {
1.1.1.14  root     6219:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6220:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     6221:                                }
1.1.1.14  root     6222:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   6223:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     6224:                        }
                   6225:                }
                   6226:        }
1.1.1.14  root     6227:        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     6228: }
                   6229: 
                   6230: inline void pcbios_int_10h_07h()
                   6231: {
1.1.1.14  root     6232:        if(REG8(CH) >= scr_height || REG8(CH) > REG8(DH) ||
                   6233:           REG8(CL) >= scr_width  || REG8(CL) > REG8(DL)) {
                   6234:                return;
                   6235:        }
                   6236:        vram_flush();
                   6237:        
1.1.1.23  root     6238:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6239:        SMALL_RECT rect;
1.1.1.14  root     6240:        SET_RECT(rect, 0, scr_top, scr_width - 1, scr_top + scr_height - 1);
                   6241:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   6242:        
                   6243:        int right = min(REG8(DL), scr_width - 1);
                   6244:        int bottom = min(REG8(DH), scr_height - 1);
1.1       root     6245:        
                   6246:        if(REG8(AL) == 0) {
1.1.1.14  root     6247:                for(int y = REG8(CH); y <= bottom; y++) {
1.1.1.16  root     6248:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     6249:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6250:                                mem[ofs++] = SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     6251:                        }
                   6252:                }
                   6253:        } else {
1.1.1.14  root     6254:                for(int y = bottom, y2 = max(REG8(CH) - 1, bottom - REG8(AL)); y >= REG8(CH); y--, y2--) {
1.1.1.16  root     6255:                        for(int x = REG8(CL), ofs = pcbios_get_shadow_buffer_address(mem[0x462], REG8(CL), y); x <= right; x++) {
1.1.1.14  root     6256:                                if(y2 >= REG8(CH)) {
                   6257:                                        SCR_BUF(y,x) = SCR_BUF(y2,x);
1.1       root     6258:                                } else {
1.1.1.14  root     6259:                                        SCR_BUF(y,x).Char.AsciiChar = ' ';
                   6260:                                        SCR_BUF(y,x).Attributes = REG8(BH);
1.1       root     6261:                                }
1.1.1.14  root     6262:                                mem[ofs++] = SCR_BUF(y,x).Char.AsciiChar;
                   6263:                                mem[ofs++] = SCR_BUF(y,x).Attributes;
1.1       root     6264:                        }
                   6265:                }
                   6266:        }
1.1.1.14  root     6267:        WriteConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
1.1       root     6268: }
                   6269: 
                   6270: inline void pcbios_int_10h_08h()
                   6271: {
                   6272:        COORD co;
                   6273:        DWORD num;
                   6274:        
1.1.1.14  root     6275:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   6276:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
1.1       root     6277:        
                   6278:        if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     6279:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6280:                co.Y += scr_top;
                   6281:                vram_flush();
1.1       root     6282:                ReadConsoleOutputCharacter(hStdout, scr_char, 1, co, &num);
                   6283:                ReadConsoleOutputAttribute(hStdout, scr_attr, 1, co, &num);
                   6284:                REG8(AL) = scr_char[0];
                   6285:                REG8(AH) = scr_attr[0];
                   6286:        } else {
1.1.1.16  root     6287:                REG16(AX) = *(UINT16 *)(mem + pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y));
1.1       root     6288:        }
                   6289: }
                   6290: 
                   6291: inline void pcbios_int_10h_09h()
                   6292: {
                   6293:        COORD co;
                   6294:        
1.1.1.14  root     6295:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   6296:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   6297:        
1.1.1.16  root     6298:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   6299:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     6300:        
                   6301:        if(mem[0x462] == REG8(BH)) {
1.1.1.14  root     6302:                EnterCriticalSection(&vram_crit_sect);
1.1.1.16  root     6303:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     6304:                while(dest < end) {
                   6305:                        write_text_vram_char(dest - vram, REG8(AL));
                   6306:                        mem[dest++] = REG8(AL);
                   6307:                        write_text_vram_attr(dest - vram, REG8(BL));
                   6308:                        mem[dest++] = REG8(BL);
1.1       root     6309:                }
1.1.1.14  root     6310:                LeaveCriticalSection(&vram_crit_sect);
1.1       root     6311:        } else {
1.1.1.14  root     6312:                while(dest < end) {
1.1       root     6313:                        mem[dest++] = REG8(AL);
                   6314:                        mem[dest++] = REG8(BL);
                   6315:                }
                   6316:        }
                   6317: }
                   6318: 
                   6319: inline void pcbios_int_10h_0ah()
                   6320: {
                   6321:        COORD co;
                   6322:        
1.1.1.14  root     6323:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   6324:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   6325:        
1.1.1.16  root     6326:        int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
                   6327:        int end = min(dest + REG16(CX) * 2, pcbios_get_shadow_buffer_address(REG8(BH), 0, scr_height));
1.1       root     6328:        
                   6329:        if(mem[0x462] == REG8(BH)) {
1.1.1.14  root     6330:                EnterCriticalSection(&vram_crit_sect);
1.1.1.16  root     6331:                int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     6332:                while(dest < end) {
                   6333:                        write_text_vram_char(dest - vram, REG8(AL));
                   6334:                        mem[dest++] = REG8(AL);
                   6335:                        dest++;
1.1       root     6336:                }
1.1.1.14  root     6337:                LeaveCriticalSection(&vram_crit_sect);
1.1       root     6338:        } else {
1.1.1.14  root     6339:                while(dest < end) {
1.1       root     6340:                        mem[dest++] = REG8(AL);
                   6341:                        dest++;
                   6342:                }
                   6343:        }
                   6344: }
                   6345: 
                   6346: inline void pcbios_int_10h_0eh()
                   6347: {
1.1.1.14  root     6348:        DWORD num;
                   6349:        COORD co;
                   6350:        
                   6351:        co.X = mem[0x450 + (REG8(BH) % vram_pages) * 2];
                   6352:        co.Y = mem[0x451 + (REG8(BH) % vram_pages) * 2];
                   6353:        
                   6354:        if(REG8(AL) == 7) {
                   6355:                //MessageBeep(-1);
                   6356:        } else if(REG8(AL) == 8 || REG8(AL) == 10 || REG8(AL) == 13) {
                   6357:                if(REG8(AL) == 10) {
                   6358:                        vram_flush();
                   6359:                }
1.1.1.23  root     6360:                WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), &REG8(AL), 1, &num, NULL);
1.1.1.14  root     6361:                cursor_moved = true;
                   6362:        } else {
1.1.1.16  root     6363:                int dest = pcbios_get_shadow_buffer_address(REG8(BH), co.X, co.Y);
1.1.1.14  root     6364:                if(mem[0x462] == REG8(BH)) {
                   6365:                        EnterCriticalSection(&vram_crit_sect);
1.1.1.16  root     6366:                        int vram = pcbios_get_shadow_buffer_address(REG8(BH));
1.1.1.14  root     6367:                        write_text_vram_char(dest - vram, REG8(AL));
                   6368:                        LeaveCriticalSection(&vram_crit_sect);
                   6369:                        
1.1.1.23  root     6370:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6371:                        if(++co.X == scr_width) {
                   6372:                                co.X = 0;
                   6373:                                if(++co.Y == scr_height) {
                   6374:                                        vram_flush();
                   6375:                                        WriteConsole(hStdout, "\n", 1, &num, NULL);
                   6376:                                        cursor_moved = true;
                   6377:                                }
                   6378:                        }
                   6379:                        if(!cursor_moved) {
                   6380:                                co.Y += scr_top;
                   6381:                                SetConsoleCursorPosition(hStdout, co);
                   6382:                                cursor_moved = true;
                   6383:                        }
                   6384:                }
                   6385:                mem[dest] = REG8(AL);
                   6386:        }
1.1       root     6387: }
                   6388: 
                   6389: inline void pcbios_int_10h_0fh()
                   6390: {
                   6391:        REG8(AL) = mem[0x449];
                   6392:        REG8(AH) = mem[0x44a];
                   6393:        REG8(BH) = mem[0x462];
                   6394: }
                   6395: 
1.1.1.14  root     6396: inline void pcbios_int_10h_11h()
                   6397: {
                   6398:        switch(REG8(AL)) {
1.1.1.16  root     6399:        case 0x01:
1.1.1.14  root     6400:        case 0x11:
1.1.1.16  root     6401:                pcbios_set_console_size(80, 28, true);
1.1.1.14  root     6402:                break;
1.1.1.16  root     6403:        case 0x02:
1.1.1.14  root     6404:        case 0x12:
1.1.1.16  root     6405:                pcbios_set_console_size(80, 50, true);
1.1.1.14  root     6406:                break;
1.1.1.16  root     6407:        case 0x04:
1.1.1.14  root     6408:        case 0x14:
1.1.1.16  root     6409:                pcbios_set_console_size(80, 25, true);
                   6410:                break;
                   6411:        case 0x18:
                   6412:                pcbios_set_console_size(80, 50, true);
1.1.1.14  root     6413:                break;
                   6414:        case 0x30:
                   6415:                SREG(ES) = 0;
                   6416:                i386_load_segment_descriptor(ES);
                   6417:                REG16(BP) = 0;
                   6418:                REG16(CX) = mem[0x485];
                   6419:                REG8(DL) = mem[0x484];
                   6420:                break;
                   6421:        }
                   6422: }
                   6423: 
                   6424: inline void pcbios_int_10h_12h()
                   6425: {
1.1.1.16  root     6426:        switch(REG8(BL)) {
                   6427:        case 0x10:
1.1.1.14  root     6428:                REG16(BX) = 0x0003;
                   6429:                REG16(CX) = 0x0009;
1.1.1.16  root     6430:                break;
1.1.1.14  root     6431:        }
                   6432: }
                   6433: 
1.1       root     6434: inline void pcbios_int_10h_13h()
                   6435: {
1.1.1.3   root     6436:        int ofs = SREG_BASE(ES) + REG16(BP);
1.1       root     6437:        COORD co;
                   6438:        DWORD num;
                   6439:        
                   6440:        co.X = REG8(DL);
1.1.1.14  root     6441:        co.Y = REG8(DH) + scr_top;
                   6442:        
                   6443:        vram_flush();
1.1       root     6444:        
                   6445:        switch(REG8(AL)) {
                   6446:        case 0x00:
                   6447:        case 0x01:
                   6448:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     6449:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6450:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   6451:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   6452:                        SetConsoleCursorPosition(hStdout, co);
                   6453:                        
                   6454:                        if(csbi.wAttributes != REG8(BL)) {
                   6455:                                SetConsoleTextAttribute(hStdout, REG8(BL));
                   6456:                        }
1.1.1.14  root     6457:                        WriteConsole(hStdout, &mem[ofs], REG16(CX), &num, NULL);
                   6458:                        
1.1       root     6459:                        if(csbi.wAttributes != REG8(BL)) {
                   6460:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   6461:                        }
                   6462:                        if(REG8(AL) == 0x00) {
1.1.1.15  root     6463:                                if(!restore_console_on_exit) {
                   6464:                                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   6465:                                        scr_top = csbi.srWindow.Top;
                   6466:                                }
1.1.1.14  root     6467:                                co.X = mem[0x450 + REG8(BH) * 2];
                   6468:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     6469:                                SetConsoleCursorPosition(hStdout, co);
                   6470:                        } else {
                   6471:                                cursor_moved = true;
                   6472:                        }
                   6473:                } else {
1.1.1.3   root     6474:                        m_CF = 1;
1.1       root     6475:                }
                   6476:                break;
                   6477:        case 0x02:
                   6478:        case 0x03:
                   6479:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     6480:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6481:                        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   6482:                        GetConsoleScreenBufferInfo(hStdout, &csbi);
                   6483:                        SetConsoleCursorPosition(hStdout, co);
                   6484:                        
                   6485:                        WORD wAttributes = csbi.wAttributes;
                   6486:                        for(int i = 0; i < REG16(CX); i++, ofs += 2) {
                   6487:                                if(wAttributes != mem[ofs + 1]) {
                   6488:                                        SetConsoleTextAttribute(hStdout, mem[ofs + 1]);
                   6489:                                        wAttributes = mem[ofs + 1];
                   6490:                                }
1.1.1.14  root     6491:                                WriteConsole(hStdout, &mem[ofs], 1, &num, NULL);
1.1       root     6492:                        }
                   6493:                        if(csbi.wAttributes != wAttributes) {
                   6494:                                SetConsoleTextAttribute(hStdout, csbi.wAttributes);
                   6495:                        }
                   6496:                        if(REG8(AL) == 0x02) {
1.1.1.14  root     6497:                                co.X = mem[0x450 + REG8(BH) * 2];
                   6498:                                co.Y = mem[0x451 + REG8(BH) * 2] + scr_top;
1.1       root     6499:                                SetConsoleCursorPosition(hStdout, co);
                   6500:                        } else {
                   6501:                                cursor_moved = true;
                   6502:                        }
                   6503:                } else {
1.1.1.3   root     6504:                        m_CF = 1;
1.1       root     6505:                }
                   6506:                break;
                   6507:        case 0x10:
                   6508:        case 0x11:
                   6509:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     6510:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6511:                        ReadConsoleOutputCharacter(hStdout, scr_char, REG16(CX), co, &num);
                   6512:                        ReadConsoleOutputAttribute(hStdout, scr_attr, REG16(CX), co, &num);
                   6513:                        for(int i = 0; i < num; i++) {
                   6514:                                mem[ofs++] = scr_char[i];
                   6515:                                mem[ofs++] = scr_attr[i];
                   6516:                                if(REG8(AL) == 0x11) {
                   6517:                                        mem[ofs++] = 0;
                   6518:                                        mem[ofs++] = 0;
                   6519:                                }
                   6520:                        }
                   6521:                } else {
1.1.1.16  root     6522:                        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     6523:                                mem[ofs++] = mem[src++];
                   6524:                                mem[ofs++] = mem[src++];
                   6525:                                if(REG8(AL) == 0x11) {
                   6526:                                        mem[ofs++] = 0;
                   6527:                                        mem[ofs++] = 0;
                   6528:                                }
1.1.1.14  root     6529:                                if(++co.X == scr_width) {
                   6530:                                        if(++co.Y == scr_height) {
1.1       root     6531:                                                break;
                   6532:                                        }
                   6533:                                        co.X = 0;
                   6534:                                }
                   6535:                        }
                   6536:                }
                   6537:                break;
                   6538:        case 0x20:
                   6539:        case 0x21:
                   6540:                if(mem[0x462] == REG8(BH)) {
1.1.1.23  root     6541:                        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1.1.14  root     6542:                        int len = min(REG16(CX), scr_width * scr_height);
                   6543:                        for(int i = 0; i < len; i++) {
1.1       root     6544:                                scr_char[i] = mem[ofs++];
                   6545:                                scr_attr[i] = mem[ofs++];
                   6546:                                if(REG8(AL) == 0x21) {
                   6547:                                        ofs += 2;
                   6548:                                }
                   6549:                        }
1.1.1.14  root     6550:                        WriteConsoleOutputCharacter(hStdout, scr_char, len, co, &num);
                   6551:                        WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     6552:                } else {
1.1.1.16  root     6553:                        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     6554:                                mem[dest++] = mem[ofs++];
                   6555:                                mem[dest++] = mem[ofs++];
                   6556:                                if(REG8(AL) == 0x21) {
                   6557:                                        ofs += 2;
                   6558:                                }
1.1.1.14  root     6559:                                if(++co.X == scr_width) {
                   6560:                                        if(++co.Y == scr_height) {
1.1       root     6561:                                                break;
                   6562:                                        }
                   6563:                                        co.X = 0;
                   6564:                                }
                   6565:                        }
                   6566:                }
                   6567:                break;
                   6568:        default:
1.1.1.22  root     6569:                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     6570:                m_CF = 1;
1.1       root     6571:                break;
                   6572:        }
                   6573: }
                   6574: 
1.1.1.30  root     6575: inline void pcbios_int_10h_18h()
                   6576: {
                   6577:        switch(REG8(AL)) {
                   6578:        case 0x00:
                   6579:        case 0x01:
                   6580: //             REG8(AL) = 0x86;
                   6581:                REG8(AL) = 0x00;
                   6582:                break;
                   6583:        default:
                   6584:                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));
                   6585:                m_CF = 1;
                   6586:                break;
                   6587:        }
                   6588: }
                   6589: 
1.1.1.14  root     6590: inline void pcbios_int_10h_1ah()
                   6591: {
                   6592:        switch(REG8(AL)) {
                   6593:        case 0x00:
                   6594:                REG8(AL) = 0x1a;
                   6595:                REG8(BL) = 0x08;
                   6596:                REG8(BH) = 0x00;
                   6597:                break;
                   6598:        default:
1.1.1.22  root     6599:                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     6600:                m_CF = 1;
                   6601:                break;
                   6602:        }
                   6603: }
                   6604: 
1.1       root     6605: inline void pcbios_int_10h_1dh()
                   6606: {
                   6607:        switch(REG8(AL)) {
                   6608:        case 0x01:
                   6609:                break;
                   6610:        case 0x02:
                   6611:                REG16(BX) = 0;
                   6612:                break;
                   6613:        default:
1.1.1.22  root     6614:                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));
                   6615:                m_CF = 1;
                   6616:                break;
                   6617:        }
                   6618: }
                   6619: 
                   6620: inline void pcbios_int_10h_4fh()
                   6621: {
                   6622:        switch(REG8(AL)) {
                   6623:        case 0x00:
                   6624:                REG8(AH) = 0x02; // not supported
                   6625:                break;
                   6626:        case 0x01:
                   6627:        case 0x02:
                   6628:        case 0x03:
                   6629:        case 0x04:
                   6630:        case 0x05:
                   6631:        case 0x06:
                   6632:        case 0x07:
                   6633:        case 0x08:
                   6634:        case 0x09:
                   6635:        case 0x0a:
                   6636:        case 0x0b:
                   6637:        case 0x0c:
                   6638:                REG8(AH) = 0x01; // failed
                   6639:                break;
                   6640:        default:
                   6641:                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     6642:                m_CF = 1;
1.1       root     6643:                break;
                   6644:        }
                   6645: }
                   6646: 
                   6647: inline void pcbios_int_10h_82h()
                   6648: {
                   6649:        static UINT8 mode = 0;
                   6650:        
                   6651:        switch(REG8(AL)) {
1.1.1.22  root     6652:        case 0x00:
1.1       root     6653:                if(REG8(BL) != 0xff) {
                   6654:                        mode = REG8(BL);
                   6655:                }
                   6656:                REG8(AL) = mode;
                   6657:                break;
                   6658:        default:
1.1.1.22  root     6659:                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     6660:                m_CF = 1;
1.1       root     6661:                break;
                   6662:        }
                   6663: }
                   6664: 
1.1.1.22  root     6665: inline void pcbios_int_10h_83h()
                   6666: {
                   6667:        static UINT8 mode = 0;
                   6668:        
                   6669:        switch(REG8(AL)) {
                   6670:        case 0x00:
                   6671:                REG16(AX) = 0; // offset???
                   6672:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
                   6673:                i386_load_segment_descriptor(ES);
                   6674:                REG16(BX) = (SHADOW_BUF_TOP & 0x0f);
                   6675:                break;
                   6676:        default:
                   6677:                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));
                   6678:                m_CF = 1;
                   6679:                break;
                   6680:        }
                   6681: }
                   6682: 
                   6683: inline void pcbios_int_10h_90h()
                   6684: {
                   6685:        REG8(AL) = mem[0x449];
                   6686: }
                   6687: 
                   6688: inline void pcbios_int_10h_91h()
                   6689: {
                   6690:        REG8(AL) = 0x04; // VGA
                   6691: }
                   6692: 
                   6693: inline void pcbios_int_10h_efh()
                   6694: {
                   6695:        REG16(DX) = 0xffff;
                   6696: }
                   6697: 
1.1       root     6698: inline void pcbios_int_10h_feh()
                   6699: {
                   6700:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.8   root     6701:                SREG(ES) = (SHADOW_BUF_TOP >> 4);
1.1.1.3   root     6702:                i386_load_segment_descriptor(ES);
1.1.1.8   root     6703:                REG16(DI) = (SHADOW_BUF_TOP & 0x0f);
1.1       root     6704:        }
                   6705:        int_10h_feh_called = true;
                   6706: }
                   6707: 
                   6708: inline void pcbios_int_10h_ffh()
                   6709: {
                   6710:        if(mem[0x449] == 0x03 || mem[0x449] == 0x70 || mem[0x449] == 0x71 || mem[0x449] == 0x73) {
1.1.1.23  root     6711:                HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     6712:                COORD co;
                   6713:                DWORD num;
                   6714:                
1.1.1.14  root     6715:                vram_flush();
                   6716:                
                   6717:                co.X = (REG16(DI) >> 1) % scr_width;
                   6718:                co.Y = (REG16(DI) >> 1) / scr_width;
1.1.1.16  root     6719:                int ofs = pcbios_get_shadow_buffer_address(0, co.X, co.Y);
                   6720:                int end = min(ofs + REG16(CX) * 2, pcbios_get_shadow_buffer_address(0, 0, scr_height));
1.1.1.14  root     6721:                int len;
                   6722:                for(len = 0; ofs < end; len++) {
                   6723:                        scr_char[len] = mem[ofs++];
                   6724:                        scr_attr[len] = mem[ofs++];
                   6725:                }
                   6726:                co.Y += scr_top;
                   6727:                WriteConsoleOutputCharacter(hStdout, scr_char, len, co, &num);
                   6728:                WriteConsoleOutputAttribute(hStdout, scr_attr, len, co, &num);
1.1       root     6729:        }
                   6730:        int_10h_ffh_called = true;
                   6731: }
                   6732: 
1.1.1.25  root     6733: inline void pcbios_int_14h_00h()
                   6734: {
1.1.1.29  root     6735:        if(REG16(DX) < 4) {
1.1.1.25  root     6736:                static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600};
                   6737:                UINT8 selector = sio_read(REG16(DX), 3);
                   6738:                selector &= ~0x3f;
                   6739:                selector |= REG8(AL) & 0x1f;
                   6740:                UINT16 divisor = 115200 / rate[REG8(AL) >> 5];
                   6741:                sio_write(REG16(DX), 3, selector | 0x80);
                   6742:                sio_write(REG16(DX), 0, divisor & 0xff);
                   6743:                sio_write(REG16(DX), 1, divisor >> 8);
                   6744:                sio_write(REG16(DX), 3, selector);
                   6745:                REG8(AH) = sio_read(REG16(DX), 5);
                   6746:                REG8(AL) = sio_read(REG16(DX), 6);
                   6747:        } else {
                   6748:                REG8(AH) = 0x80;
                   6749:        }
                   6750: }
                   6751: 
                   6752: inline void pcbios_int_14h_01h()
                   6753: {
1.1.1.29  root     6754:        if(REG16(DX) < 4) {
1.1.1.25  root     6755:                UINT8 selector = sio_read(REG16(DX), 3);
                   6756:                sio_write(REG16(DX), 3, selector & ~0x80);
                   6757:                sio_write(REG16(DX), 0, REG8(AL));
                   6758:                sio_write(REG16(DX), 3, selector);
                   6759:                REG8(AH) = sio_read(REG16(DX), 5);
                   6760:        } else {
                   6761:                REG8(AH) = 0x80;
                   6762:        }
                   6763: }
                   6764: 
                   6765: inline void pcbios_int_14h_02h()
                   6766: {
1.1.1.29  root     6767:        if(REG16(DX) < 4) {
1.1.1.25  root     6768:                UINT8 selector = sio_read(REG16(DX), 3);
                   6769:                sio_write(REG16(DX), 3, selector & ~0x80);
                   6770:                REG8(AL) = sio_read(REG16(DX), 0);
                   6771:                sio_write(REG16(DX), 3, selector);
                   6772:                REG8(AH) = sio_read(REG16(DX), 5);
                   6773:        } else {
                   6774:                REG8(AH) = 0x80;
                   6775:        }
                   6776: }
                   6777: 
                   6778: inline void pcbios_int_14h_03h()
                   6779: {
1.1.1.29  root     6780:        if(REG16(DX) < 4) {
1.1.1.25  root     6781:                REG8(AH) = sio_read(REG16(DX), 5);
                   6782:                REG8(AL) = sio_read(REG16(DX), 6);
                   6783:        } else {
                   6784:                REG8(AH) = 0x80;
                   6785:        }
                   6786: }
                   6787: 
                   6788: inline void pcbios_int_14h_04h()
                   6789: {
1.1.1.29  root     6790:        if(REG16(DX) < 4) {
1.1.1.25  root     6791:                UINT8 selector = sio_read(REG16(DX), 3);
                   6792:                if(REG8(CH) <= 0x03) {
                   6793:                        selector = (selector & ~0x03) | REG8(CH);
                   6794:                }
                   6795:                if(REG8(BL) == 0x00) {
                   6796:                        selector &= ~0x04;
                   6797:                } else if(REG8(BL) == 0x01) {
                   6798:                        selector |= 0x04;
                   6799:                }
                   6800:                if(REG8(BH) == 0x00) {
                   6801:                        selector = (selector & ~0x38) | 0x00;
                   6802:                } else if(REG8(BH) == 0x01) {
                   6803:                        selector = (selector & ~0x38) | 0x08;
                   6804:                } else if(REG8(BH) == 0x02) {
                   6805:                        selector = (selector & ~0x38) | 0x18;
                   6806:                } else if(REG8(BH) == 0x03) {
                   6807:                        selector = (selector & ~0x38) | 0x28;
                   6808:                } else if(REG8(BH) == 0x04) {
                   6809:                        selector = (selector & ~0x38) | 0x38;
                   6810:                }
                   6811:                if(REG8(AL) == 0x00) {
                   6812:                        selector |= 0x40;
                   6813:                } else if(REG8(AL) == 0x01) {
                   6814:                        selector &= ~0x40;
                   6815:                }
                   6816:                if(REG8(CL) <= 0x0b) {
                   6817:                        static const unsigned int rate[] = {110, 150, 300, 600, 1200, 2400, 4800, 9600, 19200, 38400, 57600, 115200};
                   6818:                        UINT16 divisor = 115200 / rate[REG8(CL)];
                   6819:                        sio_write(REG16(DX), 3, selector | 0x80);
                   6820:                        sio_write(REG16(DX), 0, divisor & 0xff);
                   6821:                        sio_write(REG16(DX), 1, divisor >> 8);
                   6822:                }
                   6823:                sio_write(REG16(DX), 3, selector);
                   6824:                REG8(AH) = sio_read(REG16(DX), 5);
                   6825:                REG8(AL) = sio_read(REG16(DX), 6);
                   6826:        } else {
                   6827:                REG8(AH) = 0x80;
                   6828:        }
                   6829: }
                   6830: 
                   6831: inline void pcbios_int_14h_05h()
                   6832: {
1.1.1.29  root     6833:        if(REG16(DX) < 4) {
1.1.1.25  root     6834:                if(REG8(AL) == 0x00) {
                   6835:                        REG8(BL) = sio_read(REG16(DX), 4);
                   6836:                        REG8(AH) = sio_read(REG16(DX), 5);
                   6837:                        REG8(AL) = sio_read(REG16(DX), 6);
                   6838:                } else if(REG8(AL) == 0x01) {
                   6839:                        sio_write(REG16(DX), 4, REG8(BL));
                   6840:                        REG8(AH) = sio_read(REG16(DX), 5);
                   6841:                        REG8(AL) = sio_read(REG16(DX), 6);
                   6842:                } else {
                   6843:                        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));
                   6844:                }
                   6845:        } else {
                   6846:                REG8(AH) = 0x80;
                   6847:        }
                   6848: }
                   6849: 
1.1.1.14  root     6850: inline void pcbios_int_15h_10h()
                   6851: {
1.1.1.22  root     6852:        switch(REG8(AL)) {
                   6853:        case 0x00:
1.1.1.14  root     6854:                Sleep(10);
                   6855:                hardware_update();
1.1.1.22  root     6856:                break;
                   6857:        default:
                   6858:                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     6859:                REG8(AH) = 0x86;
                   6860:                m_CF = 1;
                   6861:        }
                   6862: }
                   6863: 
1.1       root     6864: inline void pcbios_int_15h_23h()
                   6865: {
                   6866:        switch(REG8(AL)) {
1.1.1.22  root     6867:        case 0x00:
1.1.1.8   root     6868:                REG8(CL) = cmos_read(0x2d);
                   6869:                REG8(CH) = cmos_read(0x2e);
1.1       root     6870:                break;
1.1.1.22  root     6871:        case 0x01:
1.1.1.8   root     6872:                cmos_write(0x2d, REG8(CL));
                   6873:                cmos_write(0x2e, REG8(CH));
1.1       root     6874:                break;
                   6875:        default:
1.1.1.22  root     6876:                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     6877:                REG8(AH) = 0x86;
1.1.1.3   root     6878:                m_CF = 1;
1.1       root     6879:                break;
                   6880:        }
                   6881: }
                   6882: 
                   6883: inline void pcbios_int_15h_24h()
                   6884: {
                   6885:        switch(REG8(AL)) {
1.1.1.22  root     6886:        case 0x00:
1.1.1.3   root     6887:                i386_set_a20_line(0);
1.1       root     6888:                REG8(AH) = 0;
                   6889:                break;
1.1.1.22  root     6890:        case 0x01:
1.1.1.3   root     6891:                i386_set_a20_line(1);
1.1       root     6892:                REG8(AH) = 0;
                   6893:                break;
1.1.1.22  root     6894:        case 0x02:
1.1       root     6895:                REG8(AH) = 0;
1.1.1.3   root     6896:                REG8(AL) = (m_a20_mask >> 20) & 1;
1.1       root     6897:                REG16(CX) = 0;
                   6898:                break;
1.1.1.22  root     6899:        case 0x03:
1.1       root     6900:                REG16(AX) = 0;
                   6901:                REG16(BX) = 0;
                   6902:                break;
1.1.1.22  root     6903:        default:
                   6904:                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));
                   6905:                REG8(AH) = 0x86;
                   6906:                m_CF = 1;
                   6907:                break;
1.1       root     6908:        }
                   6909: }
                   6910: 
                   6911: inline void pcbios_int_15h_49h()
                   6912: {
1.1.1.27  root     6913:        REG8(AH) = 0x00;
                   6914:        REG8(BL) = 0x00; // DOS/V
1.1       root     6915: }
                   6916: 
1.1.1.22  root     6917: inline void pcbios_int_15h_50h()
                   6918: {
                   6919:        switch(REG8(AL)) {
                   6920:        case 0x00:
                   6921:        case 0x01:
                   6922:                if(REG8(BH) != 0x00 && REG8(BH) != 0x01) {
                   6923:                        REG8(AH) = 0x01; // invalid font type in bh
                   6924:                        m_CF = 1;
1.1.1.27  root     6925:                } else if(REG8(BL) != 0x00) {
1.1.1.22  root     6926:                        REG8(AH) = 0x02; // bl not zero
                   6927:                        m_CF = 1;
                   6928:                } else if(REG16(BP) != 0 && REG16(BP) != 437 && REG16(BP) != 932 && REG16(BP) != 934 && REG16(BP) != 936 && REG16(BP) != 938) {
                   6929:                        REG8(AH) = 0x04; // invalid code page
                   6930:                        m_CF = 1;
1.1.1.27  root     6931:                } else if(REG8(AL) == 0x01) {
                   6932:                        REG8(AH) = 0x06; // font is read only
1.1.1.22  root     6933:                        m_CF = 1;
1.1.1.27  root     6934:                } else {
                   6935:                        // dummy font read routine is at fffd:000d
                   6936:                        SREG(ES) = 0xfffd;
                   6937:                        i386_load_segment_descriptor(ES);
1.1.1.32  root     6938:                        REG16(BX) = 0x000d;
1.1.1.27  root     6939:                        REG8(AH) = 0x00; // success
1.1.1.22  root     6940:                }
                   6941:                break;
                   6942:        default:
                   6943:                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));
                   6944:                REG8(AH) = 0x86;
                   6945:                m_CF = 1;
                   6946:                break;
                   6947:        }
                   6948: }
                   6949: 
1.1.1.30  root     6950: inline void pcbios_int_15h_53h()
                   6951: {
                   6952:        switch(REG8(AL)) {
                   6953:        case 0x00:
                   6954:                // APM is not installed
                   6955:                REG8(AH) = 0x86;
                   6956:                m_CF = 1;
                   6957:                break;
                   6958:        default:
                   6959:                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));
                   6960:                REG8(AH) = 0x86;
                   6961:                m_CF = 1;
                   6962:                break;
                   6963:        }
                   6964: }
                   6965: 
1.1       root     6966: inline void pcbios_int_15h_86h()
                   6967: {
                   6968:        UINT32 usec = (REG16(CX) << 16) | REG16(DX);
1.1.1.14  root     6969:        UINT32 msec = usec / 1000;
                   6970:        
                   6971:        while(msec) {
                   6972:                UINT32 tmp = min(msec, 100);
                   6973:                if(msec - tmp < 10) {
                   6974:                        tmp = msec;
                   6975:                }
                   6976:                Sleep(tmp);
                   6977:                
                   6978:                if(m_halted) {
                   6979:                        return;
                   6980:                }
                   6981:                msec -= tmp;
                   6982:        }
1.1       root     6983: }
                   6984: 
                   6985: inline void pcbios_int_15h_87h()
                   6986: {
                   6987:        // copy extended memory (from DOSBox)
                   6988:        int len = REG16(CX) * 2;
1.1.1.3   root     6989:        int ofs = SREG_BASE(ES) + REG16(SI);
1.1       root     6990:        int src = (*(UINT32 *)(mem + ofs + 0x12) & 0xffffff); // + (mem[ofs + 0x16] << 24);
                   6991:        int dst = (*(UINT32 *)(mem + ofs + 0x1a) & 0xffffff); // + (mem[ofs + 0x1e] << 24);
                   6992:        memcpy(mem + dst, mem + src, len);
                   6993:        REG16(AX) = 0x00;
                   6994: }
                   6995: 
                   6996: inline void pcbios_int_15h_88h()
                   6997: {
1.1.1.17  root     6998:        REG16(AX) = ((min(MAX_MEM, 0x4000000) - 0x100000) >> 10);
1.1       root     6999: }
                   7000: 
                   7001: inline void pcbios_int_15h_89h()
                   7002: {
1.1.1.21  root     7003: #if defined(HAS_I286) || defined(HAS_I386)
1.1       root     7004:        // switch to protected mode (from DOSBox)
                   7005:        write_io_byte(0x20, 0x10);
                   7006:        write_io_byte(0x21, REG8(BH));
                   7007:        write_io_byte(0x21, 0x00);
                   7008:        write_io_byte(0xa0, 0x10);
                   7009:        write_io_byte(0xa1, REG8(BL));
                   7010:        write_io_byte(0xa1, 0x00);
1.1.1.3   root     7011:        i386_set_a20_line(1);
                   7012:        int ofs = SREG_BASE(ES) + REG16(SI);
                   7013:        m_gdtr.limit = *(UINT16 *)(mem + ofs + 0x08);
                   7014:        m_gdtr.base = *(UINT32 *)(mem + ofs + 0x08 + 0x02) & 0xffffff;
                   7015:        m_idtr.limit = *(UINT16 *)(mem + ofs + 0x10);
                   7016:        m_idtr.base = *(UINT32 *)(mem + ofs + 0x10 + 0x02) & 0xffffff;
                   7017: #if defined(HAS_I386)
                   7018:        m_cr[0] |= 1;
                   7019: #else
                   7020:        m_msw |= 1;
                   7021: #endif
                   7022:        SREG(DS) = 0x18;
                   7023:        SREG(ES) = 0x20;
                   7024:        SREG(SS) = 0x28;
                   7025:        i386_load_segment_descriptor(DS);
                   7026:        i386_load_segment_descriptor(ES);
                   7027:        i386_load_segment_descriptor(SS);
1.1.1.21  root     7028:        UINT16 offset = *(UINT16 *)(mem + SREG_BASE(SS) + REG16(SP));
1.1       root     7029:        REG16(SP) += 6;
1.1.1.3   root     7030: #if defined(HAS_I386)
1.1.1.21  root     7031:        UINT32 flags = get_flags();
                   7032:        flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
                   7033:        set_flags(flags);
1.1.1.3   root     7034: #else
1.1.1.21  root     7035:        UINT32 flags = CompressFlags();
                   7036:        flags &= (0x20000 | 0x40000 | 0x80000 | 0x100000 | 0x200000);
                   7037:        ExpandFlags(flags);
1.1.1.3   root     7038: #endif
1.1       root     7039:        REG16(AX) = 0x00;
1.1.1.21  root     7040:        i386_jmp_far(0x30, /*REG16(CX)*/offset);
1.1       root     7041: #else
1.1.1.21  root     7042:        // i86/i186/v30: protected mode is not supported
1.1       root     7043:        REG8(AH) = 0x86;
1.1.1.3   root     7044:        m_CF = 1;
1.1       root     7045: #endif
                   7046: }
                   7047: 
1.1.1.21  root     7048: inline void pcbios_int_15h_8ah()
                   7049: {
                   7050:        UINT32 size = MAX_MEM - 0x100000;
                   7051:        REG16(AX) = size & 0xffff;
                   7052:        REG16(DX) = size >> 16;
                   7053: }
                   7054: 
1.1.1.3   root     7055: #if defined(HAS_I386)
1.1       root     7056: inline void pcbios_int_15h_c9h()
                   7057: {
                   7058:        REG8(AH) = 0x00;
                   7059:        REG8(CH) = cpu_type;
                   7060:        REG8(CL) = cpu_step;
                   7061: }
1.1.1.3   root     7062: #endif
1.1       root     7063: 
                   7064: inline void pcbios_int_15h_cah()
                   7065: {
                   7066:        switch(REG8(AL)) {
1.1.1.22  root     7067:        case 0x00:
1.1       root     7068:                if(REG8(BL) > 0x3f) {
                   7069:                        REG8(AH) = 0x03;
1.1.1.3   root     7070:                        m_CF = 1;
1.1       root     7071:                } else if(REG8(BL) < 0x0e) {
                   7072:                        REG8(AH) = 0x04;
1.1.1.3   root     7073:                        m_CF = 1;
1.1       root     7074:                } else {
1.1.1.8   root     7075:                        REG8(CL) = cmos_read(REG8(BL));
1.1       root     7076:                }
                   7077:                break;
1.1.1.22  root     7078:        case 0x01:
1.1       root     7079:                if(REG8(BL) > 0x3f) {
                   7080:                        REG8(AH) = 0x03;
1.1.1.3   root     7081:                        m_CF = 1;
1.1       root     7082:                } else if(REG8(BL) < 0x0e) {
                   7083:                        REG8(AH) = 0x04;
1.1.1.3   root     7084:                        m_CF = 1;
1.1       root     7085:                } else {
1.1.1.8   root     7086:                        cmos_write(REG8(BL), REG8(CL));
1.1       root     7087:                }
                   7088:                break;
                   7089:        default:
1.1.1.22  root     7090:                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     7091:                REG8(AH) = 0x86;
1.1.1.3   root     7092:                m_CF = 1;
1.1       root     7093:                break;
                   7094:        }
                   7095: }
                   7096: 
1.1.1.22  root     7097: inline void pcbios_int_15h_e8h()
1.1.1.17  root     7098: {
1.1.1.22  root     7099:        switch(REG8(AL)) {
                   7100: #if defined(HAS_I386)
                   7101:        case 0x01:
                   7102:                REG16(AX) = REG16(CX) = ((min(MAX_MEM, 0x1000000) - 0x100000) >> 10);
                   7103:                REG16(BX) = REG16(DX) = ((max(MAX_MEM, 0x1000000) - 0x1000000) >> 16);
                   7104:                break;
1.1.1.17  root     7105: #endif
1.1.1.22  root     7106:        default:
                   7107:                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));
                   7108:                REG8(AH) = 0x86;
                   7109:                m_CF = 1;
                   7110:                break;
                   7111:        }
                   7112: }
1.1.1.17  root     7113: 
1.1.1.33  root     7114: void pcbios_update_key_code(bool wait)
1.1       root     7115: {
1.1.1.32  root     7116:        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   7117:                if(key_buf_char->count() == 0) {
                   7118:                        if(!update_key_buffer()) {
1.1.1.33  root     7119:                                if(wait) {
1.1.1.32  root     7120:                                        Sleep(10);
                   7121:                                } else {
                   7122:                                        maybe_idle();
                   7123:                                }
1.1.1.14  root     7124:                        }
                   7125:                }
1.1.1.34! root     7126:        }
        !          7127:        if(key_buf_char != NULL && key_buf_scan != NULL) {
1.1.1.32  root     7128:                if(key_buf_char->count() != 0) {
1.1.1.33  root     7129:                        key_code = key_buf_char->read() | (key_buf_scan->read() << 8);
                   7130:                        key_recv = 0x0000ffff;
1.1.1.32  root     7131:                }
                   7132:                if(key_buf_char->count() != 0) {
1.1.1.33  root     7133:                        key_code |= (key_buf_char->read() << 16) | (key_buf_scan->read() << 24);
                   7134:                        key_recv |= 0xffff0000;
1.1.1.32  root     7135:                }
1.1       root     7136:        }
                   7137: }
                   7138: 
                   7139: inline void pcbios_int_16h_00h()
                   7140: {
1.1.1.33  root     7141:        while(key_recv == 0 && !m_halted) {
                   7142:                pcbios_update_key_code(true);
1.1       root     7143:        }
1.1.1.33  root     7144:        if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
                   7145:                if((key_code & 0xffff) == 0x0000 || (key_code & 0xffff) == 0xe000) {
                   7146:                        if(REG8(AH) == 0x10) {
                   7147:                                key_code = ((key_code >> 8) & 0xff) | ((key_code >> 16) & 0xff00);
                   7148:                        } else {
                   7149:                                key_code = ((key_code >> 16) & 0xff00);
                   7150:                        }
                   7151:                        key_recv >>= 16;
1.1       root     7152:                }
                   7153:        }
                   7154:        REG16(AX) = key_code & 0xffff;
                   7155:        key_code >>= 16;
1.1.1.33  root     7156:        key_recv >>= 16;
1.1       root     7157: }
                   7158: 
                   7159: inline void pcbios_int_16h_01h()
                   7160: {
1.1.1.33  root     7161:        if(key_recv == 0) {
                   7162:                pcbios_update_key_code(false);
1.1.1.5   root     7163:        }
1.1.1.33  root     7164:        if(key_recv != 0) {
                   7165:                UINT32 key_code_tmp = key_code;
                   7166:                if((key_recv & 0x0000ffff) && (key_recv & 0xffff0000)) {
                   7167:                        if((key_code_tmp & 0xffff) == 0x0000 || (key_code_tmp & 0xffff) == 0xe000) {
                   7168:                                if(REG8(AH) == 0x11) {
                   7169:                                        key_code_tmp = ((key_code_tmp >> 8) & 0xff) | ((key_code_tmp >> 16) & 0xff00);
                   7170:                                } else {
                   7171:                                        key_code_tmp = ((key_code_tmp >> 16) & 0xff00);
                   7172:                                }
                   7173:                        }
1.1       root     7174:                }
1.1.1.5   root     7175:                REG16(AX) = key_code_tmp & 0xffff;
1.1.1.3   root     7176: #if defined(HAS_I386)
1.1.1.33  root     7177:                m_ZF = 0;
                   7178: #else
                   7179:                m_ZeroVal = 1;
                   7180: #endif
                   7181:        } else {
                   7182: #if defined(HAS_I386)
                   7183:                m_ZF = 1;
1.1.1.3   root     7184: #else
1.1.1.33  root     7185:                m_ZeroVal = 0;
1.1.1.3   root     7186: #endif
1.1.1.33  root     7187:        }
1.1       root     7188: }
                   7189: 
                   7190: inline void pcbios_int_16h_02h()
                   7191: {
                   7192:        REG8(AL)  = (GetAsyncKeyState(VK_INSERT ) & 0x0001) ? 0x80 : 0;
                   7193:        REG8(AL) |= (GetAsyncKeyState(VK_CAPITAL) & 0x0001) ? 0x40 : 0;
                   7194:        REG8(AL) |= (GetAsyncKeyState(VK_NUMLOCK) & 0x0001) ? 0x20 : 0;
                   7195:        REG8(AL) |= (GetAsyncKeyState(VK_SCROLL ) & 0x0001) ? 0x10 : 0;
                   7196:        REG8(AL) |= (GetAsyncKeyState(VK_MENU   ) & 0x8000) ? 0x08 : 0;
                   7197:        REG8(AL) |= (GetAsyncKeyState(VK_CONTROL) & 0x8000) ? 0x04 : 0;
                   7198:        REG8(AL) |= (GetAsyncKeyState(VK_LSHIFT ) & 0x8000) ? 0x02 : 0;
                   7199:        REG8(AL) |= (GetAsyncKeyState(VK_RSHIFT ) & 0x8000) ? 0x01 : 0;
                   7200: }
                   7201: 
                   7202: inline void pcbios_int_16h_03h()
                   7203: {
                   7204:        static UINT16 status = 0;
                   7205:        
                   7206:        switch(REG8(AL)) {
                   7207:        case 0x05:
                   7208:                status = REG16(BX);
                   7209:                break;
                   7210:        case 0x06:
                   7211:                REG16(BX) = status;
                   7212:                break;
                   7213:        default:
1.1.1.3   root     7214:                m_CF = 1;
1.1       root     7215:                break;
                   7216:        }
                   7217: }
                   7218: 
                   7219: inline void pcbios_int_16h_05h()
                   7220: {
1.1.1.32  root     7221:        if(key_buf_char != NULL && key_buf_scan != NULL) {
                   7222:                key_buf_char->write(REG8(CL));
                   7223:                key_buf_scan->write(REG8(CH));
                   7224:        }
1.1       root     7225:        REG8(AL) = 0x00;
                   7226: }
                   7227: 
                   7228: inline void pcbios_int_16h_12h()
                   7229: {
                   7230:        pcbios_int_16h_02h();
                   7231:        
                   7232:        REG8(AH)  = 0;//(GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x80 : 0;
                   7233:        REG8(AH) |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   7234:        REG8(AH) |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   7235:        REG8(AH) |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
                   7236:        REG8(AH) |= (GetAsyncKeyState(VK_RMENU   ) & 0x8000) ? 0x08 : 0;
                   7237:        REG8(AH) |= (GetAsyncKeyState(VK_RCONTROL) & 0x8000) ? 0x04 : 0;
                   7238:        REG8(AH) |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   7239:        REG8(AH) |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   7240: }
                   7241: 
                   7242: inline void pcbios_int_16h_13h()
                   7243: {
                   7244:        static UINT16 status = 0;
                   7245:        
                   7246:        switch(REG8(AL)) {
                   7247:        case 0x00:
                   7248:                status = REG16(DX);
                   7249:                break;
                   7250:        case 0x01:
                   7251:                REG16(DX) = status;
                   7252:                break;
                   7253:        default:
1.1.1.22  root     7254:                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     7255:                m_CF = 1;
1.1       root     7256:                break;
                   7257:        }
                   7258: }
                   7259: 
                   7260: inline void pcbios_int_16h_14h()
                   7261: {
                   7262:        static UINT8 status = 0;
                   7263:        
                   7264:        switch(REG8(AL)) {
                   7265:        case 0x00:
                   7266:        case 0x01:
                   7267:                status = REG8(AL);
                   7268:                break;
                   7269:        case 0x02:
                   7270:                REG8(AL) = status;
                   7271:                break;
                   7272:        default:
1.1.1.22  root     7273:                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     7274:                m_CF = 1;
1.1       root     7275:                break;
                   7276:        }
                   7277: }
                   7278: 
1.1.1.24  root     7279: inline void pcbios_int_16h_55h()
                   7280: {
                   7281:        switch(REG8(AL)) {
                   7282:        case 0x00:
                   7283:                // keyboard tsr is not present
                   7284:                break;
                   7285:        case 0xfe:
                   7286:                // handlers for INT 08, INT 09, INT 16, INT 1B, and INT 1C are installed
                   7287:                break;
                   7288:        case 0xff:
                   7289:                break;
                   7290:        default:
                   7291:                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));
                   7292:                m_CF = 1;
                   7293:                break;
                   7294:        }
                   7295: }
                   7296: 
1.1.1.30  root     7297: inline void pcbios_int_16h_6fh()
                   7298: {
                   7299:        switch(REG8(AL)) {
                   7300:        case 0x00:
                   7301:                // HP Vectra EX-BIOS - "F16_INQUIRE" - Extended BIOS is not installed
                   7302:                break;
                   7303:        default:
                   7304:                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));
                   7305:                m_CF = 1;
                   7306:                break;
                   7307:        }
                   7308: }
                   7309: 
1.1       root     7310: inline void pcbios_int_1ah_00h()
                   7311: {
1.1.1.19  root     7312:        pcbios_update_daily_timer_counter(timeGetTime());
                   7313:        REG16(CX) = *(UINT16 *)(mem + 0x46e);
                   7314:        REG16(DX) = *(UINT16 *)(mem + 0x46c);
                   7315:        REG8(AL) = mem[0x470];
                   7316:        mem[0x470] = 0;
1.1       root     7317: }
                   7318: 
                   7319: inline int to_bcd(int t)
                   7320: {
                   7321:        int u = (t % 100) / 10;
                   7322:        return (u << 4) | (t % 10);
                   7323: }
                   7324: 
                   7325: inline void pcbios_int_1ah_02h()
                   7326: {
                   7327:        SYSTEMTIME time;
                   7328:        
                   7329:        GetLocalTime(&time);
                   7330:        REG8(CH) = to_bcd(time.wHour);
                   7331:        REG8(CL) = to_bcd(time.wMinute);
                   7332:        REG8(DH) = to_bcd(time.wSecond);
                   7333:        REG8(DL) = 0x00;
                   7334: }
                   7335: 
                   7336: inline void pcbios_int_1ah_04h()
                   7337: {
                   7338:        SYSTEMTIME time;
                   7339:        
                   7340:        GetLocalTime(&time);
                   7341:        REG8(CH) = to_bcd(time.wYear / 100);
                   7342:        REG8(CL) = to_bcd(time.wYear);
                   7343:        REG8(DH) = to_bcd(time.wMonth);
                   7344:        REG8(DL) = to_bcd(time.wDay);
                   7345: }
                   7346: 
                   7347: inline void pcbios_int_1ah_0ah()
                   7348: {
                   7349:        SYSTEMTIME time;
                   7350:        FILETIME file_time;
                   7351:        WORD dos_date, dos_time;
                   7352:        
                   7353:        GetLocalTime(&time);
                   7354:        SystemTimeToFileTime(&time, &file_time);
                   7355:        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   7356:        REG16(CX) = dos_date;
                   7357: }
                   7358: 
                   7359: // msdos system call
                   7360: 
                   7361: inline void msdos_int_21h_00h()
                   7362: {
1.1.1.3   root     7363:        msdos_process_terminate(SREG(CS), retval, 1);
1.1       root     7364: }
                   7365: 
                   7366: inline void msdos_int_21h_01h()
                   7367: {
                   7368:        REG8(AL) = msdos_getche();
1.1.1.33  root     7369:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     7370:        
1.1.1.8   root     7371:        // some seconds may be passed in console
1.1       root     7372:        hardware_update();
                   7373: }
                   7374: 
                   7375: inline void msdos_int_21h_02h()
                   7376: {
1.1.1.33  root     7377:        UINT8 data = REG8(DL);
                   7378:        msdos_putch(data);
                   7379:        REG8(AL) = data;
                   7380:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     7381: }
                   7382: 
                   7383: inline void msdos_int_21h_03h()
                   7384: {
                   7385:        REG8(AL) = msdos_aux_in();
                   7386: }
                   7387: 
                   7388: inline void msdos_int_21h_04h()
                   7389: {
                   7390:        msdos_aux_out(REG8(DL));
                   7391: }
                   7392: 
                   7393: inline void msdos_int_21h_05h()
                   7394: {
                   7395:        msdos_prn_out(REG8(DL));
                   7396: }
                   7397: 
                   7398: inline void msdos_int_21h_06h()
                   7399: {
                   7400:        if(REG8(DL) == 0xff) {
                   7401:                if(msdos_kbhit()) {
                   7402:                        REG8(AL) = msdos_getch();
1.1.1.3   root     7403: #if defined(HAS_I386)
                   7404:                        m_ZF = 0;
                   7405: #else
                   7406:                        m_ZeroVal = 1;
                   7407: #endif
1.1       root     7408:                } else {
                   7409:                        REG8(AL) = 0;
1.1.1.3   root     7410: #if defined(HAS_I386)
                   7411:                        m_ZF = 1;
                   7412: #else
                   7413:                        m_ZeroVal = 0;
                   7414: #endif
1.1.1.14  root     7415:                        maybe_idle();
1.1       root     7416:                }
                   7417:        } else {
1.1.1.33  root     7418:                UINT8 data = REG8(DL);
                   7419:                msdos_putch(data);
                   7420:                REG8(AL) = data;
1.1       root     7421:        }
                   7422: }
                   7423: 
                   7424: inline void msdos_int_21h_07h()
                   7425: {
                   7426:        REG8(AL) = msdos_getch();
1.1.1.26  root     7427:        
1.1.1.8   root     7428:        // some seconds may be passed in console
1.1       root     7429:        hardware_update();
                   7430: }
                   7431: 
                   7432: inline void msdos_int_21h_08h()
                   7433: {
                   7434:        REG8(AL) = msdos_getch();
1.1.1.33  root     7435:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     7436:        
1.1.1.8   root     7437:        // some seconds may be passed in console
1.1       root     7438:        hardware_update();
                   7439: }
                   7440: 
                   7441: inline void msdos_int_21h_09h()
                   7442: {
1.1.1.21  root     7443:        msdos_stdio_reopen();
                   7444:        
1.1.1.20  root     7445:        process_t *process = msdos_process_info_get(current_psp);
                   7446:        int fd = msdos_psp_get_file_table(1, current_psp);
                   7447:        
1.1.1.14  root     7448:        char *str = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   7449:        int len = 0;
1.1       root     7450:        
1.1.1.14  root     7451:        while(str[len] != '$' && len < 0x10000) {
                   7452:                len++;
                   7453:        }
1.1.1.20  root     7454:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     7455:                // stdout is redirected to file
1.1.1.20  root     7456:                msdos_write(fd, str, len);
1.1       root     7457:        } else {
                   7458:                for(int i = 0; i < len; i++) {
1.1.1.14  root     7459:                        msdos_putch(str[i]);
1.1       root     7460:                }
                   7461:        }
1.1.1.33  root     7462:        REG8(AL) = '$';
                   7463:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     7464: }
                   7465: 
                   7466: inline void msdos_int_21h_0ah()
                   7467: {
1.1.1.3   root     7468:        int ofs = SREG_BASE(DS) + REG16(DX);
1.1       root     7469:        int max = mem[ofs] - 1;
                   7470:        UINT8 *buf = mem + ofs + 2;
                   7471:        int chr, p = 0;
                   7472:        
                   7473:        while((chr = msdos_getch()) != 0x0d) {
1.1.1.33  root     7474:                if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
1.1.1.26  root     7475:                        p = 0;
1.1.1.33  root     7476:                        msdos_putch(0x03);
                   7477:                        msdos_putch(0x0d);
                   7478:                        msdos_putch(0x0a);
1.1.1.26  root     7479:                        break;
1.1.1.33  root     7480:                } else if(ctrl_break_pressed) {
                   7481:                        // skip this byte
1.1.1.26  root     7482:                } else if(chr == 0x00) {
1.1       root     7483:                        // skip 2nd byte
                   7484:                        msdos_getch();
                   7485:                } else if(chr == 0x08) {
                   7486:                        // back space
                   7487:                        if(p > 0) {
                   7488:                                p--;
1.1.1.20  root     7489:                                if(msdos_ctrl_code_check(buf[p])) {
1.1.1.34! root     7490:                                        msdos_putch(0x08);
        !          7491:                                        msdos_putch(0x08);
        !          7492:                                        msdos_putch(0x20);
        !          7493:                                        msdos_putch(0x20);
        !          7494:                                        msdos_putch(0x08);
        !          7495:                                        msdos_putch(0x08);
        !          7496:                                } else {
        !          7497:                                        msdos_putch(0x08);
        !          7498:                                        msdos_putch(0x20);
        !          7499:                                        msdos_putch(0x08);
        !          7500:                                }
        !          7501:                        }
        !          7502:                } else if(chr == 0x1b) {
        !          7503:                        // escape
        !          7504:                        while(p > 0) {
        !          7505:                                p--;
        !          7506:                                if(msdos_ctrl_code_check(buf[p])) {
        !          7507:                                        msdos_putch(0x08);
        !          7508:                                        msdos_putch(0x08);
        !          7509:                                        msdos_putch(0x20);
        !          7510:                                        msdos_putch(0x20);
        !          7511:                                        msdos_putch(0x08);
        !          7512:                                        msdos_putch(0x08);
1.1.1.20  root     7513:                                } else {
1.1.1.34! root     7514:                                        msdos_putch(0x08);
        !          7515:                                        msdos_putch(0x20);
        !          7516:                                        msdos_putch(0x08);
1.1.1.20  root     7517:                                }
1.1       root     7518:                        }
                   7519:                } else if(p < max) {
                   7520:                        buf[p++] = chr;
                   7521:                        msdos_putch(chr);
                   7522:                }
                   7523:        }
                   7524:        buf[p] = 0x0d;
                   7525:        mem[ofs + 1] = p;
1.1.1.33  root     7526:        ctrl_break_detected = ctrl_break_pressed;
1.1.1.26  root     7527:        
1.1.1.8   root     7528:        // some seconds may be passed in console
1.1       root     7529:        hardware_update();
                   7530: }
                   7531: 
                   7532: inline void msdos_int_21h_0bh()
                   7533: {
                   7534:        if(msdos_kbhit()) {
                   7535:                REG8(AL) = 0xff;
                   7536:        } else {
                   7537:                REG8(AL) = 0x00;
1.1.1.14  root     7538:                maybe_idle();
1.1       root     7539:        }
1.1.1.33  root     7540:        ctrl_break_detected = ctrl_break_pressed;
1.1       root     7541: }
                   7542: 
                   7543: inline void msdos_int_21h_0ch()
                   7544: {
                   7545:        // clear key buffer
1.1.1.21  root     7546:        msdos_stdio_reopen();
                   7547:        
1.1.1.20  root     7548:        process_t *process = msdos_process_info_get(current_psp);
                   7549:        int fd = msdos_psp_get_file_table(0, current_psp);
                   7550:        
                   7551:        if(fd < process->max_files && file_handler[fd].valid && !file_handler[fd].atty) {
1.1       root     7552:                // stdin is redirected to file
                   7553:        } else {
                   7554:                while(msdos_kbhit()) {
                   7555:                        msdos_getch();
                   7556:                }
                   7557:        }
                   7558:        
                   7559:        switch(REG8(AL)) {
                   7560:        case 0x01:
                   7561:                msdos_int_21h_01h();
                   7562:                break;
                   7563:        case 0x06:
                   7564:                msdos_int_21h_06h();
                   7565:                break;
                   7566:        case 0x07:
                   7567:                msdos_int_21h_07h();
                   7568:                break;
                   7569:        case 0x08:
                   7570:                msdos_int_21h_08h();
                   7571:                break;
                   7572:        case 0x0a:
                   7573:                msdos_int_21h_0ah();
                   7574:                break;
                   7575:        default:
1.1.1.22  root     7576: //             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));
                   7577: //             REG16(AX) = 0x01;
                   7578: //             m_CF = 1;
1.1       root     7579:                break;
                   7580:        }
                   7581: }
                   7582: 
                   7583: inline void msdos_int_21h_0dh()
                   7584: {
                   7585: }
                   7586: 
                   7587: inline void msdos_int_21h_0eh()
                   7588: {
                   7589:        if(REG8(DL) < 26) {
                   7590:                _chdrive(REG8(DL) + 1);
                   7591:                msdos_cds_update(REG8(DL));
1.1.1.23  root     7592:                msdos_sda_update(current_psp);
1.1       root     7593:        }
                   7594:        REG8(AL) = 26; // zdrive
                   7595: }
                   7596: 
1.1.1.14  root     7597: inline void msdos_int_21h_0fh()
                   7598: {
                   7599:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   7600:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   7601:        char *path = msdos_fcb_path(fcb);
                   7602:        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     7603:        
1.1.1.14  root     7604:        if(hFile == INVALID_HANDLE_VALUE) {
                   7605:                REG8(AL) = 0xff;
                   7606:        } else {
                   7607:                REG8(AL) = 0;
                   7608:                fcb->current_block = 0;
                   7609:                fcb->record_size = 128;
                   7610:                fcb->file_size = GetFileSize(hFile, NULL);
                   7611:                fcb->handle = hFile;
                   7612:                fcb->cur_record = 0;
                   7613:        }
                   7614: }
                   7615: 
                   7616: inline void msdos_int_21h_10h()
                   7617: {
                   7618:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   7619:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   7620:        
                   7621:        REG8(AL) = CloseHandle(fcb->handle) ? 0 : 0xff;
                   7622: }
                   7623: 
1.1       root     7624: inline void msdos_int_21h_11h()
                   7625: {
1.1.1.3   root     7626:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   7627:        fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     7628:        
                   7629:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     7630:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   7631:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   7632:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     7633:        char *path = msdos_fcb_path(fcb);
                   7634:        WIN32_FIND_DATA fd;
                   7635:        
1.1.1.13  root     7636:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   7637:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   7638:                FindClose(dtainfo->find_handle);
                   7639:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     7640:        }
                   7641:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     7642:        dtainfo->allowable_mask = (ext_fcb->flag == 0xff) ? ext_fcb->attribute : 0x20;
                   7643:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     7644:        
1.1.1.14  root     7645:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   7646:                dtainfo->allowable_mask &= ~8;
1.1       root     7647:        }
1.1.1.14  root     7648:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   7649:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     7650:                      !msdos_find_file_has_8dot3name(&fd)) {
                   7651:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   7652:                                FindClose(dtainfo->find_handle);
                   7653:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     7654:                                break;
                   7655:                        }
                   7656:                }
                   7657:        }
1.1.1.13  root     7658:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     7659:                if(ext_fcb->flag == 0xff) {
                   7660:                        ext_find->flag = 0xff;
                   7661:                        memset(ext_find->reserved, 0, 5);
                   7662:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   7663:                }
                   7664:                find->drive = _getdrive();
1.1.1.13  root     7665:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     7666:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   7667:                find->nt_res = 0;
                   7668:                msdos_find_file_conv_local_time(&fd);
                   7669:                find->create_time_ms = 0;
                   7670:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   7671:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   7672:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   7673:                find->cluster_hi = find->cluster_lo = 0;
                   7674:                find->file_size = fd.nFileSizeLow;
                   7675:                REG8(AL) = 0x00;
1.1.1.14  root     7676:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     7677:                if(ext_fcb->flag == 0xff) {
                   7678:                        ext_find->flag = 0xff;
                   7679:                        memset(ext_find->reserved, 0, 5);
                   7680:                        ext_find->attribute = 8;
                   7681:                }
                   7682:                find->drive = _getdrive();
                   7683:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   7684:                find->attribute = 8;
                   7685:                find->nt_res = 0;
                   7686:                msdos_find_file_conv_local_time(&fd);
                   7687:                find->create_time_ms = 0;
                   7688:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   7689:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   7690:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   7691:                find->cluster_hi = find->cluster_lo = 0;
                   7692:                find->file_size = 0;
1.1.1.14  root     7693:                dtainfo->allowable_mask &= ~8;
1.1       root     7694:                REG8(AL) = 0x00;
                   7695:        } else {
                   7696:                REG8(AL) = 0xff;
                   7697:        }
                   7698: }
                   7699: 
                   7700: inline void msdos_int_21h_12h()
                   7701: {
1.1.1.3   root     7702:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1.1.14  root     7703: //     fcb_t *fcb = (fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     7704:        
                   7705:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     7706:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   7707:        ext_fcb_t *ext_find = (ext_fcb_t *)(mem + dta_laddr);
                   7708:        find_fcb_t *find = (find_fcb_t *)(mem + dta_laddr + (ext_fcb->flag == 0xff ? 7 : 0));
1.1       root     7709:        WIN32_FIND_DATA fd;
                   7710:        
1.1.1.13  root     7711:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, dta_laddr);
                   7712:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   7713:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     7714:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     7715:                              !msdos_find_file_has_8dot3name(&fd)) {
                   7716:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   7717:                                        FindClose(dtainfo->find_handle);
                   7718:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     7719:                                        break;
                   7720:                                }
                   7721:                        }
                   7722:                } else {
1.1.1.13  root     7723:                        FindClose(dtainfo->find_handle);
                   7724:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     7725:                }
                   7726:        }
1.1.1.13  root     7727:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     7728:                if(ext_fcb->flag == 0xff) {
                   7729:                        ext_find->flag = 0xff;
                   7730:                        memset(ext_find->reserved, 0, 5);
                   7731:                        ext_find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   7732:                }
                   7733:                find->drive = _getdrive();
1.1.1.13  root     7734:                msdos_set_fcb_path((fcb_t *)find, msdos_short_name(&fd));
1.1       root     7735:                find->attribute = (UINT8)(fd.dwFileAttributes & 0x3f);
                   7736:                find->nt_res = 0;
                   7737:                msdos_find_file_conv_local_time(&fd);
                   7738:                find->create_time_ms = 0;
                   7739:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   7740:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   7741:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   7742:                find->cluster_hi = find->cluster_lo = 0;
                   7743:                find->file_size = fd.nFileSizeLow;
                   7744:                REG8(AL) = 0x00;
1.1.1.14  root     7745:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     7746:                if(ext_fcb->flag == 0xff) {
                   7747:                        ext_find->flag = 0xff;
                   7748:                        memset(ext_find->reserved, 0, 5);
                   7749:                        ext_find->attribute = 8;
                   7750:                }
                   7751:                find->drive = _getdrive();
                   7752:                msdos_set_fcb_path((fcb_t *)find, msdos_short_volume_label(process->volume_label));
                   7753:                find->attribute = 8;
                   7754:                find->nt_res = 0;
                   7755:                msdos_find_file_conv_local_time(&fd);
                   7756:                find->create_time_ms = 0;
                   7757:                FileTimeToDosDateTime(&fd.ftCreationTime, &find->creation_date, &find->creation_time);
                   7758:                FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->last_access_date, &find->last_write_time);
                   7759:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->last_write_date, &find->last_write_time);
                   7760:                find->cluster_hi = find->cluster_lo = 0;
                   7761:                find->file_size = 0;
1.1.1.14  root     7762:                dtainfo->allowable_mask &= ~8;
1.1       root     7763:                REG8(AL) = 0x00;
                   7764:        } else {
                   7765:                REG8(AL) = 0xff;
                   7766:        }
                   7767: }
                   7768: 
                   7769: inline void msdos_int_21h_13h()
                   7770: {
1.1.1.3   root     7771:        if(remove(msdos_fcb_path((fcb_t *)(mem + SREG_BASE(DS) + REG16(DX))))) {
1.1       root     7772:                REG8(AL) = 0xff;
                   7773:        } else {
                   7774:                REG8(AL) = 0x00;
                   7775:        }
                   7776: }
                   7777: 
1.1.1.16  root     7778: inline void msdos_int_21h_14h()
                   7779: {
                   7780:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   7781:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   7782:        process_t *process = msdos_process_info_get(current_psp);
                   7783:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   7784:        DWORD num = 0;
                   7785:        
                   7786:        memset(mem + dta_laddr, 0, fcb->record_size);
                   7787:        if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   7788:                REG8(AL) = 1;
                   7789:        } else {
                   7790:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   7791:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   7792:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   7793:                REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   7794:        }
                   7795: }
                   7796: 
                   7797: inline void msdos_int_21h_15h()
1.1.1.14  root     7798: {
                   7799:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   7800:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.16  root     7801:        process_t *process = msdos_process_info_get(current_psp);
                   7802:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   7803:        DWORD num = 0;
1.1.1.14  root     7804:        
1.1.1.16  root     7805:        if(!WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   7806:                REG8(AL) = 1;
                   7807:        } else {
                   7808:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   7809:                UINT32 position = fcb->current_block * fcb->record_size + fcb->cur_record + num;
                   7810:                fcb->current_block = (position & 0xffffff) / fcb->record_size;
                   7811:                fcb->cur_record = (position & 0xffffff) % fcb->record_size;
                   7812:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   7813:        }
                   7814: }
                   7815: 
                   7816: inline void msdos_int_21h_16h()
                   7817: {
                   7818:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   7819:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
1.1.1.14  root     7820:        char *path = msdos_fcb_path(fcb);
                   7821:        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     7822:        
1.1.1.14  root     7823:        if(hFile == INVALID_HANDLE_VALUE) {
                   7824:                REG8(AL) = 0xff;
                   7825:        } else {
                   7826:                REG8(AL) = 0;
                   7827:                fcb->current_block = 0;
                   7828:                fcb->record_size = 128;
                   7829:                fcb->file_size = 0;
                   7830:                fcb->handle = hFile;
                   7831:                fcb->cur_record = 0;
                   7832:        }
                   7833: }
                   7834: 
1.1.1.16  root     7835: inline void msdos_int_21h_17h()
                   7836: {
                   7837:        ext_fcb_t *ext_fcb_src = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   7838:        fcb_t *fcb_src = (fcb_t *)(ext_fcb_src + (ext_fcb_src->flag == 0xff ? 1 : 0));
                   7839:        char *path_src = msdos_fcb_path(fcb_src);
                   7840:        ext_fcb_t *ext_fcb_dst = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX) + 16);
                   7841:        fcb_t *fcb_dst = (fcb_t *)(ext_fcb_dst + (ext_fcb_dst->flag == 0xff ? 1 : 0));
                   7842:        char *path_dst = msdos_fcb_path(fcb_dst);
                   7843:        
                   7844:        if(rename(path_src, path_dst)) {
                   7845:                REG8(AL) = 0xff;
                   7846:        } else {
                   7847:                REG8(AL) = 0;
                   7848:        }
                   7849: }
                   7850: 
1.1       root     7851: inline void msdos_int_21h_18h()
                   7852: {
                   7853:        REG8(AL) = 0x00;
                   7854: }
                   7855: 
                   7856: inline void msdos_int_21h_19h()
                   7857: {
                   7858:        REG8(AL) = _getdrive() - 1;
                   7859: }
                   7860: 
                   7861: inline void msdos_int_21h_1ah()
                   7862: {
                   7863:        process_t *process = msdos_process_info_get(current_psp);
                   7864:        
                   7865:        process->dta.w.l = REG16(DX);
1.1.1.3   root     7866:        process->dta.w.h = SREG(DS);
1.1.1.23  root     7867:        msdos_sda_update(current_psp);
1.1       root     7868: }
                   7869: 
                   7870: inline void msdos_int_21h_1bh()
                   7871: {
                   7872:        int drive_num = _getdrive() - 1;
                   7873:        UINT16 seg, ofs;
                   7874:        
                   7875:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   7876:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   7877:                REG8(AL) = dpb->highest_sector_num + 1;
                   7878:                REG16(CX) = dpb->bytes_per_sector;
                   7879:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     7880:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     7881:        } else {
                   7882:                REG8(AL) = 0xff;
1.1.1.3   root     7883:                m_CF = 1;
1.1       root     7884:        }
                   7885: 
                   7886: }
                   7887: 
                   7888: inline void msdos_int_21h_1ch()
                   7889: {
                   7890:        int drive_num = REG8(DL) ? (REG8(DL) - 1) : (_getdrive() - 1);
                   7891:        UINT16 seg, ofs;
                   7892:        
                   7893:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   7894:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   7895:                REG8(AL) = dpb->highest_sector_num + 1;
                   7896:                REG16(CX) = dpb->bytes_per_sector;
                   7897:                REG16(DX) = dpb->highest_cluster_num - 1;
1.1.1.3   root     7898:                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(BX)) = dpb->media_type;
1.1       root     7899:        } else {
                   7900:                REG8(AL) = 0xff;
1.1.1.3   root     7901:                m_CF = 1;
1.1       root     7902:        }
                   7903: 
                   7904: }
                   7905: 
                   7906: inline void msdos_int_21h_1dh()
                   7907: {
                   7908:        REG8(AL) = 0;
                   7909: }
                   7910: 
                   7911: inline void msdos_int_21h_1eh()
                   7912: {
                   7913:        REG8(AL) = 0;
                   7914: }
                   7915: 
                   7916: inline void msdos_int_21h_1fh()
                   7917: {
                   7918:        int drive_num = _getdrive() - 1;
                   7919:        UINT16 seg, ofs;
                   7920:        
                   7921:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   7922:                REG8(AL) = 0;
1.1.1.3   root     7923:                SREG(DS) = seg;
                   7924:                i386_load_segment_descriptor(DS);
1.1       root     7925:                REG16(BX) = ofs;
                   7926:        } else {
                   7927:                REG8(AL) = 0xff;
1.1.1.3   root     7928:                m_CF = 1;
1.1       root     7929:        }
                   7930: }
                   7931: 
                   7932: inline void msdos_int_21h_20h()
                   7933: {
                   7934:        REG8(AL) = 0;
                   7935: }
                   7936: 
1.1.1.14  root     7937: inline void msdos_int_21h_21h()
                   7938: {
                   7939:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   7940:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   7941:        
                   7942:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   7943:                REG8(AL) = 1;
                   7944:        } else {
                   7945:                process_t *process = msdos_process_info_get(current_psp);
                   7946:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   7947:                memset(mem + dta_laddr, 0, fcb->record_size);
1.1.1.16  root     7948:                DWORD num = 0;
1.1.1.14  root     7949:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL) || num == 0) {
                   7950:                        REG8(AL) = 1;
                   7951:                } else {
                   7952:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   7953:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     7954:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
1.1.1.14  root     7955:                }
                   7956:        }
                   7957: }
                   7958: 
                   7959: inline void msdos_int_21h_22h()
                   7960: {
                   7961:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   7962:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   7963:        
                   7964:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   7965:                REG8(AL) = 0xff;
                   7966:        } else {
                   7967:                process_t *process = msdos_process_info_get(current_psp);
                   7968:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
1.1.1.16  root     7969:                DWORD num = 0;
1.1.1.14  root     7970:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size, &num, NULL);
                   7971:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   7972:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   7973:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
1.1.1.16  root     7974:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
1.1.1.14  root     7975:        }
                   7976: }
                   7977: 
1.1.1.16  root     7978: inline void msdos_int_21h_23h()
                   7979: {
                   7980:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   7981:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   7982:        char *path = msdos_fcb_path(fcb);
                   7983:        HANDLE hFile = CreateFile(path, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   7984:        
                   7985:        if(hFile == INVALID_HANDLE_VALUE) {
                   7986:                REG8(AL) = 0xff;
                   7987:        } else {
                   7988:                UINT32 size = GetFileSize(hFile, NULL);
                   7989:                fcb->rand_record = size / fcb->record_size + ((size % fcb->record_size) != 0);
                   7990:                REG8(AL) = 0;
                   7991:        }
                   7992: }
                   7993: 
                   7994: inline void msdos_int_21h_24h()
                   7995: {
                   7996:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   7997:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   7998:        
                   7999:        fcb->rand_record = fcb->current_block * fcb->record_size + fcb->cur_record;
                   8000: }
                   8001: 
1.1       root     8002: inline void msdos_int_21h_25h()
                   8003: {
                   8004:        *(UINT16 *)(mem + 4 * REG8(AL) + 0) = REG16(DX);
1.1.1.3   root     8005:        *(UINT16 *)(mem + 4 * REG8(AL) + 2) = SREG(DS);
1.1       root     8006: }
                   8007: 
                   8008: inline void msdos_int_21h_26h()
                   8009: {
                   8010:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   8011:        
                   8012:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
                   8013:        psp->first_mcb = REG16(DX) + 16;
                   8014:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   8015:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   8016:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   8017:        psp->parent_psp = 0;
                   8018: }
                   8019: 
1.1.1.16  root     8020: inline void msdos_int_21h_27h()
                   8021: {
                   8022:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8023:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   8024:        
                   8025:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   8026:                REG8(AL) = 1;
                   8027:        } else {
                   8028:                process_t *process = msdos_process_info_get(current_psp);
                   8029:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   8030:                memset(mem + dta_laddr, 0, fcb->record_size * REG16(CX));
                   8031:                DWORD num = 0;
                   8032:                if(!ReadFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL) || num == 0) {
                   8033:                        REG8(AL) = 1;
                   8034:                } else {
                   8035:                        fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   8036:                        fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   8037:                        REG8(AL) = (num == fcb->record_size) ? 0 : 3;
                   8038:                        REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   8039:                }
                   8040:        }
                   8041: }
                   8042: 
                   8043: inline void msdos_int_21h_28h()
                   8044: {
                   8045:        ext_fcb_t *ext_fcb = (ext_fcb_t *)(mem + SREG_BASE(DS) + REG16(DX));
                   8046:        fcb_t *fcb = (fcb_t *)(ext_fcb + (ext_fcb->flag == 0xff ? 1 : 0));
                   8047:        
                   8048:        if(SetFilePointer(fcb->handle, fcb->record_size * (fcb->rand_record & 0xffffff), NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
                   8049:                REG8(AL) = 0xff;
                   8050:        } else {
                   8051:                process_t *process = msdos_process_info_get(current_psp);
                   8052:                UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   8053:                DWORD num = 0;
                   8054:                WriteFile(fcb->handle, mem + dta_laddr, fcb->record_size * REG16(CX), &num, NULL);
                   8055:                fcb->file_size = GetFileSize(fcb->handle, NULL);
                   8056:                fcb->current_block = (fcb->rand_record & 0xffffff) / fcb->record_size;
                   8057:                fcb->cur_record = (fcb->rand_record & 0xffffff) % fcb->record_size;
                   8058:                REG8(AL) = (num == fcb->record_size) ? 0 : 1;
                   8059:                REG16(CX) = num / fcb->record_size + ((num % fcb->record_size) != 0);
                   8060:        }
                   8061: }
                   8062: 
1.1       root     8063: inline void msdos_int_21h_29h()
                   8064: {
1.1.1.20  root     8065:        int ofs = 0;//SREG_BASE(DS) + REG16(SI);
                   8066:        char buffer[1024], name[MAX_PATH], ext[MAX_PATH];
1.1       root     8067:        UINT8 drv = 0;
                   8068:        char sep_chars[] = ":.;,=+";
                   8069:        char end_chars[] = "\\<>|/\"[]";
                   8070:        char spc_chars[] = " \t";
                   8071:        
1.1.1.20  root     8072:        memcpy(buffer, mem + SREG_BASE(DS) + REG16(SI), 1023);
                   8073:        buffer[1023] = 0;
                   8074:        memset(name, 0x20, sizeof(name));
                   8075:        memset(ext, 0x20, sizeof(ext));
                   8076:        
1.1       root     8077:        if(REG8(AL) & 1) {
1.1.1.20  root     8078:                ofs += strspn((char *)(buffer + ofs), spc_chars);
                   8079:                if(my_strchr(sep_chars, buffer[ofs]) && buffer[ofs] != '\0') {
1.1       root     8080:                        ofs++;
                   8081:                }
                   8082:        }
1.1.1.20  root     8083:        ofs += strspn((char *)(buffer + ofs), spc_chars);
1.1       root     8084:        
1.1.1.24  root     8085:        if(buffer[ofs + 1] == ':') {
                   8086:                if(buffer[ofs] >= 'a' && buffer[ofs] <= 'z') {
                   8087:                        drv = buffer[ofs] - 'a' + 1;
1.1.1.20  root     8088:                        ofs += 2;
1.1.1.24  root     8089:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   8090:                                ofs++;
                   8091:                        }
                   8092:                } else if(buffer[ofs] >= 'A' && buffer[ofs] <= 'Z') {
                   8093:                        drv = buffer[ofs] - 'A' + 1;
1.1       root     8094:                        ofs += 2;
1.1.1.24  root     8095:                        if(buffer[ofs] == '\\' || buffer[ofs] == '/') {
                   8096:                                ofs++;
                   8097:                        }
1.1       root     8098:                }
                   8099:        }
1.1.1.20  root     8100:        for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   8101:                UINT8 c = buffer[ofs];
                   8102:                if(is_kanji) {
                   8103:                        is_kanji = 0;
                   8104:                } else if(msdos_lead_byte_check(c)) {
                   8105:                        is_kanji = 1;
                   8106:                } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     8107:                        break;
                   8108:                } else if(c >= 'a' && c <= 'z') {
                   8109:                        c -= 0x20;
                   8110:                }
                   8111:                ofs++;
                   8112:                name[i] = c;
                   8113:        }
1.1.1.20  root     8114:        if(buffer[ofs] == '.') {
1.1       root     8115:                ofs++;
1.1.1.20  root     8116:                for(int i = 0, is_kanji = 0; i < MAX_PATH; i++) {
                   8117:                        UINT8 c = buffer[ofs];
                   8118:                        if(is_kanji) {
                   8119:                                is_kanji = 0;
                   8120:                        } else if(msdos_lead_byte_check(c)) {
                   8121:                                is_kanji = 1;
                   8122:                        } else if(c <= 0x20 || my_strchr(end_chars, c) || my_strchr(sep_chars, c)) {
1.1       root     8123:                                break;
                   8124:                        } else if(c >= 'a' && c <= 'z') {
                   8125:                                c -= 0x20;
                   8126:                        }
                   8127:                        ofs++;
                   8128:                        ext[i] = c;
                   8129:                }
                   8130:        }
1.1.1.20  root     8131:        int si = REG16(SI) + ofs;
1.1.1.3   root     8132:        int ds = SREG(DS);
1.1       root     8133:        while(si > 0xffff) {
                   8134:                si -= 0x10;
                   8135:                ds++;
                   8136:        }
                   8137:        REG16(SI) = si;
1.1.1.3   root     8138:        SREG(DS) = ds;
                   8139:        i386_load_segment_descriptor(DS);
1.1       root     8140:        
1.1.1.3   root     8141:        UINT8 *fcb = mem + SREG_BASE(ES) + REG16(DI);
1.1.1.20  root     8142:        if(!(REG8(AL) & 2) || drv != 0) {
                   8143:                fcb[0] = drv;
                   8144:        }
                   8145:        if(!(REG8(AL) & 4) || name[0] != 0x20) {
                   8146:                memcpy(fcb + 1, name, 8);
                   8147:        }
                   8148:        if(!(REG8(AL) & 8) || ext[0] != 0x20) {
                   8149:                memcpy(fcb + 9, ext, 3);
                   8150:        }
                   8151:        for(int i = 1, found_star = 0; i < 1 + 8; i++) {
1.1       root     8152:                if(fcb[i] == '*') {
                   8153:                        found_star = 1;
                   8154:                }
                   8155:                if(found_star) {
                   8156:                        fcb[i] = '?';
                   8157:                }
                   8158:        }
1.1.1.20  root     8159:        for(int i = 9, found_star = 0; i < 9 + 3; i++) {
1.1       root     8160:                if(fcb[i] == '*') {
                   8161:                        found_star = 1;
                   8162:                }
                   8163:                if(found_star) {
                   8164:                        fcb[i] = '?';
                   8165:                }
                   8166:        }
                   8167:        
                   8168:        if(drv == 0 || (drv > 0 && drv <= 26 && (GetLogicalDrives() & ( 1 << (drv - 1) )))) {
                   8169:                if(memchr(fcb + 1, '?', 8 + 3)) {
                   8170:                        REG8(AL) = 0x01;
1.1.1.20  root     8171:                } else {
                   8172:                        REG8(AL) = 0x00;
1.1       root     8173:                }
                   8174:        } else {
                   8175:                REG8(AL) = 0xff;
                   8176:        }
                   8177: }
                   8178: 
                   8179: inline void msdos_int_21h_2ah()
                   8180: {
                   8181:        SYSTEMTIME sTime;
                   8182:        
                   8183:        GetLocalTime(&sTime);
                   8184:        REG16(CX) = sTime.wYear;
                   8185:        REG8(DH) = (UINT8)sTime.wMonth;
                   8186:        REG8(DL) = (UINT8)sTime.wDay;
                   8187:        REG8(AL) = (UINT8)sTime.wDayOfWeek;
                   8188: }
                   8189: 
                   8190: inline void msdos_int_21h_2bh()
                   8191: {
1.1.1.14  root     8192:        REG8(AL) = 0xff;
1.1       root     8193: }
                   8194: 
                   8195: inline void msdos_int_21h_2ch()
                   8196: {
                   8197:        SYSTEMTIME sTime;
                   8198:        
                   8199:        GetLocalTime(&sTime);
                   8200:        REG8(CH) = (UINT8)sTime.wHour;
                   8201:        REG8(CL) = (UINT8)sTime.wMinute;
                   8202:        REG8(DH) = (UINT8)sTime.wSecond;
1.1.1.14  root     8203:        REG8(DL) = (UINT8)sTime.wMilliseconds / 10;
1.1       root     8204: }
                   8205: 
                   8206: inline void msdos_int_21h_2dh()
                   8207: {
                   8208:        REG8(AL) = 0x00;
                   8209: }
                   8210: 
                   8211: inline void msdos_int_21h_2eh()
                   8212: {
                   8213:        process_t *process = msdos_process_info_get(current_psp);
                   8214:        
                   8215:        process->verify = REG8(AL);
                   8216: }
                   8217: 
                   8218: inline void msdos_int_21h_2fh()
                   8219: {
                   8220:        process_t *process = msdos_process_info_get(current_psp);
                   8221:        
                   8222:        REG16(BX) = process->dta.w.l;
1.1.1.3   root     8223:        SREG(ES) = process->dta.w.h;
                   8224:        i386_load_segment_descriptor(ES);
1.1       root     8225: }
                   8226: 
                   8227: inline void msdos_int_21h_30h()
                   8228: {
                   8229:        // Version Flag / OEM
1.1.1.27  root     8230:        if(REG8(AL) == 0x01) {
1.1.1.29  root     8231: #ifdef SUPPORT_HMA
                   8232:                REG16(BX) = 0x0000;
                   8233: #else
                   8234:                REG16(BX) = 0x1000; // DOS is in HMA
                   8235: #endif
1.1       root     8236:        } else {
1.1.1.27  root     8237:                // NOTE: EXDEB invites BL shows the machine type (0=PC-98, 1=PC/AT, 2=FMR),
                   8238:                // but this is not correct on Windows 98 SE
                   8239: //             REG16(BX) = 0xff01;     // OEM = Microsoft, PC/AT
                   8240:                REG16(BX) = 0xff00;     // OEM = Microsoft
1.1       root     8241:        }
1.1.1.27  root     8242:        REG16(CX) = 0x0000;
1.1.1.30  root     8243:        REG8(AL) = dos_major_version;   // 7
                   8244:        REG8(AH) = dos_minor_version;   // 10
1.1       root     8245: }
                   8246: 
                   8247: inline void msdos_int_21h_31h()
                   8248: {
1.1.1.29  root     8249:        try {
                   8250:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   8251:        } catch(...) {
                   8252:                // recover the broken mcb
                   8253:                int mcb_seg = current_psp - 1;
                   8254:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     8255:                
1.1.1.29  root     8256:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     8257:                        mcb->mz = 'M';
                   8258:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   8259:                        
1.1.1.29  root     8260:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.33  root     8261:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4));
1.1.1.29  root     8262:                        } else {
1.1.1.33  root     8263:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0);
1.1.1.29  root     8264:                        }
                   8265:                } else {
                   8266:                        mcb->mz = 'Z';
1.1.1.30  root     8267:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     8268:                }
                   8269:                msdos_mem_realloc(current_psp, REG16(DX), NULL);
                   8270:        }
1.1       root     8271:        msdos_process_terminate(current_psp, REG8(AL) | 0x300, 0);
                   8272: }
                   8273: 
                   8274: inline void msdos_int_21h_32h()
                   8275: {
                   8276:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   8277:        UINT16 seg, ofs;
                   8278:        
                   8279:        if(msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   8280:                REG8(AL) = 0;
1.1.1.3   root     8281:                SREG(DS) = seg;
                   8282:                i386_load_segment_descriptor(DS);
1.1       root     8283:                REG16(BX) = ofs;
                   8284:        } else {
                   8285:                REG8(AL) = 0xff;
1.1.1.3   root     8286:                m_CF = 1;
1.1       root     8287:        }
                   8288: }
                   8289: 
                   8290: inline void msdos_int_21h_33h()
                   8291: {
                   8292:        char path[MAX_PATH];
                   8293:        
                   8294:        switch(REG8(AL)) {
                   8295:        case 0x00:
1.1.1.33  root     8296:                REG8(DL) = ctrl_break_checking;
1.1       root     8297:                break;
                   8298:        case 0x01:
1.1.1.33  root     8299:                ctrl_break_checking = REG8(DL);
                   8300:                break;
                   8301:        case 0x02:
                   8302:                {
                   8303:                        UINT8 old = ctrl_break_checking;
                   8304:                        ctrl_break_checking = REG8(DL);
                   8305:                        REG8(DL) = old;
                   8306:                }
                   8307:                break;
                   8308:        case 0x03:
                   8309:        case 0x04:
                   8310:                // DOS 4.0+ - Unused
1.1       root     8311:                break;
                   8312:        case 0x05:
                   8313:                GetSystemDirectory(path, MAX_PATH);
                   8314:                if(path[0] >= 'a' && path[0] <= 'z') {
                   8315:                        REG8(DL) = path[0] - 'a' + 1;
                   8316:                } else {
                   8317:                        REG8(DL) = path[0] - 'A' + 1;
                   8318:                }
                   8319:                break;
                   8320:        case 0x06:
1.1.1.2   root     8321:                // MS-DOS version (7.10)
1.1       root     8322:                REG8(BL) = 7;
1.1.1.2   root     8323:                REG8(BH) = 10;
1.1       root     8324:                REG8(DL) = 0;
1.1.1.29  root     8325: #ifdef SUPPORT_HMA
                   8326:                REG8(DH) = 0x00;
                   8327: #else
                   8328:                REG8(DH) = 0x10; // DOS is in HMA
                   8329: #endif
1.1       root     8330:                break;
1.1.1.6   root     8331:        case 0x07:
                   8332:                if(REG8(DL) == 0) {
                   8333:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag &= ~0x20;
                   8334:                } else if(REG8(DL) == 1) {
                   8335:                        ((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag |= 0x20;
                   8336:                }
                   8337:                break;
1.1       root     8338:        default:
1.1.1.22  root     8339:                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     8340:                REG16(AX) = 0x01;
1.1.1.3   root     8341:                m_CF = 1;
1.1       root     8342:                break;
                   8343:        }
                   8344: }
                   8345: 
1.1.1.23  root     8346: inline void msdos_int_21h_34h()
                   8347: {
                   8348:        SREG(ES) = SDA_TOP >> 4;
                   8349:        i386_load_segment_descriptor(ES);
                   8350:        REG16(BX) = offsetof(sda_t, indos_flag);;
                   8351: }
                   8352: 
1.1       root     8353: inline void msdos_int_21h_35h()
                   8354: {
                   8355:        REG16(BX) = *(UINT16 *)(mem + 4 * REG8(AL) + 0);
1.1.1.3   root     8356:        SREG(ES) = *(UINT16 *)(mem + 4 * REG8(AL) + 2);
                   8357:        i386_load_segment_descriptor(ES);
1.1       root     8358: }
                   8359: 
                   8360: inline void msdos_int_21h_36h()
                   8361: {
                   8362:        struct _diskfree_t df = {0};
                   8363:        
                   8364:        if(_getdiskfree(REG8(DL), &df) == 0) {
                   8365:                REG16(AX) = (UINT16)df.sectors_per_cluster;
                   8366:                REG16(CX) = (UINT16)df.bytes_per_sector;
1.1.1.13  root     8367:                REG16(BX) = df.avail_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.avail_clusters;
                   8368:                REG16(DX) = df.total_clusters > 0xFFFF ? 0xFFFF : (UINT16)df.total_clusters;
1.1       root     8369:        } else {
                   8370:                REG16(AX) = 0xffff;
                   8371:        }
                   8372: }
                   8373: 
                   8374: inline void msdos_int_21h_37h()
                   8375: {
1.1.1.22  root     8376:        static UINT8 dev_flag = 0xff;
1.1       root     8377:        
                   8378:        switch(REG8(AL)) {
                   8379:        case 0x00:
1.1.1.22  root     8380:                {
                   8381:                        process_t *process = msdos_process_info_get(current_psp);
                   8382:                        REG8(AL) = 0x00;
                   8383:                        REG8(DL) = process->switchar;
                   8384:                }
1.1       root     8385:                break;
                   8386:        case 0x01:
1.1.1.22  root     8387:                {
                   8388:                        process_t *process = msdos_process_info_get(current_psp);
                   8389:                        REG8(AL) = 0x00;
                   8390:                        process->switchar = REG8(DL);
1.1.1.23  root     8391:                        msdos_sda_update(current_psp);
1.1.1.22  root     8392:                }
                   8393:                break;
                   8394:        case 0x02:
                   8395:                REG8(DL) = dev_flag;
                   8396:                break;
                   8397:        case 0x03:
                   8398:                dev_flag = REG8(DL);
                   8399:                break;
                   8400:        case 0xd0:
                   8401:        case 0xd1:
                   8402:        case 0xd2:
                   8403:        case 0xd3:
                   8404:        case 0xd4:
                   8405:        case 0xd5:
                   8406:        case 0xd6:
                   8407:        case 0xd7:
                   8408:        case 0xdc:
                   8409:        case 0xdd:
                   8410:        case 0xde:
                   8411:        case 0xdf:
                   8412:                // diet ???
                   8413:                REG16(AX) = 1;
1.1       root     8414:                break;
                   8415:        default:
1.1.1.22  root     8416:                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     8417:                REG16(AX) = 1;
                   8418:                break;
                   8419:        }
                   8420: }
                   8421: 
1.1.1.19  root     8422: int get_country_info(country_info_t *ci)
1.1.1.17  root     8423: {
                   8424:        char LCdata[80];
                   8425:        
1.1.1.19  root     8426:        ZeroMemory(ci, offsetof(country_info_t, reserved));
1.1.1.17  root     8427:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRDIGITS, LCdata, sizeof(LCdata));
                   8428:        ci->currency_dec_digits = atoi(LCdata);
                   8429:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICURRENCY, LCdata, sizeof(LCdata));
                   8430:        ci->currency_format = *LCdata - '0';
                   8431:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_IDATE, LCdata, sizeof(LCdata));
                   8432:        ci->date_format = *LCdata - '0';
                   8433:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SCURRENCY, LCdata, sizeof(LCdata));
                   8434:        memcpy(&ci->currency_symbol, LCdata, 4);
                   8435:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDATE, LCdata, sizeof(LCdata));
                   8436:        *ci->date_sep = *LCdata;
                   8437:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SDECIMAL, LCdata, sizeof(LCdata));
                   8438:        *ci->dec_sep = *LCdata;
                   8439:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_SLIST, LCdata, sizeof(LCdata));
                   8440:        *ci->list_sep = *LCdata;
                   8441:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STHOUSAND, LCdata, sizeof(LCdata));
                   8442:        *ci->thou_sep = *LCdata;
                   8443:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIME, LCdata, sizeof(LCdata));
                   8444:        *ci->time_sep = *LCdata;
                   8445:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_STIMEFORMAT, LCdata, sizeof(LCdata));
                   8446:        if(strchr(LCdata, 'H') != NULL) {
                   8447:                ci->time_format = 1;
                   8448:        }
1.1.1.27  root     8449:        ci->case_map.w.l = 0x000a; // dummy case map routine is at fffd:000a
1.1.1.24  root     8450:        ci->case_map.w.h = 0xfffd;
1.1.1.17  root     8451:        GetLocaleInfo(LOCALE_USER_DEFAULT, LOCALE_ICOUNTRY, LCdata, sizeof(LCdata));
                   8452:        return atoi(LCdata);
                   8453: }
                   8454: 
1.1.1.14  root     8455: inline void msdos_int_21h_38h()
                   8456: {
                   8457:        switch(REG8(AL)) {
                   8458:        case 0x00:
1.1.1.19  root     8459:                REG16(BX) = get_country_info((country_info_t *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1.1.14  root     8460:                break;
                   8461:        default:
1.1.1.22  root     8462:                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     8463:                REG16(AX) = 2;
                   8464:                m_CF = 1;
                   8465:                break;
                   8466:        }
                   8467: }
                   8468: 
1.1       root     8469: inline void msdos_int_21h_39h(int lfn)
                   8470: {
1.1.1.3   root     8471:        if(_mkdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     8472:                REG16(AX) = errno;
1.1.1.3   root     8473:                m_CF = 1;
1.1       root     8474:        }
                   8475: }
                   8476: 
                   8477: inline void msdos_int_21h_3ah(int lfn)
                   8478: {
1.1.1.3   root     8479:        if(_rmdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     8480:                REG16(AX) = errno;
1.1.1.3   root     8481:                m_CF = 1;
1.1       root     8482:        }
                   8483: }
                   8484: 
                   8485: inline void msdos_int_21h_3bh(int lfn)
                   8486: {
1.1.1.3   root     8487:        if(_chdir(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1.1.17  root     8488:                REG16(AX) = 3;  // must be 3 (path not found)
1.1.1.3   root     8489:                m_CF = 1;
1.1       root     8490:        }
                   8491: }
                   8492: 
                   8493: inline void msdos_int_21h_3ch()
                   8494: {
1.1.1.3   root     8495:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     8496:        int attr = GetFileAttributes(path);
1.1.1.29  root     8497:        int fd = -1, c;
1.1.1.11  root     8498:        UINT16 info;
1.1       root     8499:        
1.1.1.11  root     8500:        if(msdos_is_con_path(path)) {
                   8501:                fd = _open("CON", _O_WRONLY | _O_BINARY);
                   8502:                info = 0x80d3;
1.1.1.29  root     8503:        } else if((c = msdos_is_comm_path(path)) != 0 && sio_port_number[c - 1] != 0) {
                   8504:                if((fd = _open(msdos_create_comm_path(path, sio_port_number[c - 1]), _O_WRONLY | _O_BINARY)) == -1) {
                   8505:                        fd = _open("NUL", _O_WRONLY | _O_BINARY);
                   8506:                }
1.1.1.14  root     8507:                info = 0x80d3;
1.1.1.29  root     8508:        } else if(msdos_is_device_path(path)) {
1.1.1.20  root     8509:                fd = _open("NUL", _O_WRONLY | _O_BINARY);
                   8510:                info = 0x80d3;
1.1       root     8511:        } else {
                   8512:                fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
1.1.1.11  root     8513:                info = msdos_drive_number(path);
1.1       root     8514:        }
                   8515:        if(fd != -1) {
                   8516:                if(attr == -1) {
                   8517:                        attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   8518:                }
                   8519:                SetFileAttributes(path, attr);
                   8520:                REG16(AX) = fd;
1.1.1.11  root     8521:                msdos_file_handler_open(fd, path, _isatty(fd), 2, info, current_psp);
1.1.1.20  root     8522:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     8523:        } else {
                   8524:                REG16(AX) = errno;
1.1.1.3   root     8525:                m_CF = 1;
1.1       root     8526:        }
                   8527: }
                   8528: 
                   8529: inline void msdos_int_21h_3dh()
                   8530: {
1.1.1.3   root     8531:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     8532:        int mode = REG8(AL) & 0x03;
1.1.1.29  root     8533:        int fd = -1, c;
1.1.1.11  root     8534:        UINT16 info;
1.1       root     8535:        
                   8536:        if(mode < 0x03) {
1.1.1.11  root     8537:                if(msdos_is_con_path(path)) {
1.1.1.13  root     8538:                        fd = msdos_open("CON", file_mode[mode].mode);
1.1.1.11  root     8539:                        info = 0x80d3;
1.1.1.29  root     8540:                } else if((c = msdos_is_comm_path(path)) != 0 && sio_port_number[c - 1] != 0) {
                   8541:                        if((fd = _open(msdos_create_comm_path(path, sio_port_number[c - 1]), file_mode[mode].mode)) == -1) {
                   8542:                                fd = msdos_open("NUL", file_mode[mode].mode);
                   8543:                        }
1.1.1.14  root     8544:                        info = 0x80d3;
1.1.1.29  root     8545:                } else if(msdos_is_device_path(path)) {
1.1.1.20  root     8546:                        fd = msdos_open("NUL", file_mode[mode].mode);
                   8547:                        info = 0x80d3;
1.1.1.11  root     8548:                } else {
1.1.1.13  root     8549:                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     8550:                        info = msdos_drive_number(path);
                   8551:                }
1.1       root     8552:                if(fd != -1) {
                   8553:                        REG16(AX) = fd;
1.1.1.11  root     8554:                        msdos_file_handler_open(fd, path, _isatty(fd), mode, info, current_psp);
1.1.1.20  root     8555:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     8556:                } else {
                   8557:                        REG16(AX) = errno;
1.1.1.3   root     8558:                        m_CF = 1;
1.1       root     8559:                }
                   8560:        } else {
                   8561:                REG16(AX) = 0x0c;
1.1.1.3   root     8562:                m_CF = 1;
1.1       root     8563:        }
                   8564: }
                   8565: 
                   8566: inline void msdos_int_21h_3eh()
                   8567: {
                   8568:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     8569:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     8570:        
1.1.1.20  root     8571:        if(fd < process->max_files && file_handler[fd].valid) {
                   8572:                _close(fd);
                   8573:                msdos_file_handler_close(fd);
                   8574:                msdos_psp_set_file_table(REG16(BX), 0x0ff, current_psp);
1.1       root     8575:        } else {
                   8576:                REG16(AX) = 0x06;
1.1.1.3   root     8577:                m_CF = 1;
1.1       root     8578:        }
                   8579: }
                   8580: 
                   8581: inline void msdos_int_21h_3fh()
                   8582: {
                   8583:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     8584:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     8585:        
1.1.1.20  root     8586:        if(fd < process->max_files && file_handler[fd].valid) {
                   8587:                if(file_mode[file_handler[fd].mode].in) {
                   8588:                        if(file_handler[fd].atty) {
1.1       root     8589:                                // BX is stdin or is redirected to stdin
1.1.1.3   root     8590:                                UINT8 *buf = mem + SREG_BASE(DS) + REG16(DX);
1.1       root     8591:                                int max = REG16(CX);
                   8592:                                int p = 0;
                   8593:                                
                   8594:                                while(max > p) {
                   8595:                                        int chr = msdos_getch();
                   8596:                                        
1.1.1.33  root     8597:                                        if((ctrl_break_checking && ctrl_break_pressed) || ctrl_c_pressed) {
1.1.1.26  root     8598:                                                p = 0;
                   8599:                                                buf[p++] = 0x0d;
                   8600:                                                if(max > p) {
                   8601:                                                        buf[p++] = 0x0a;
                   8602:                                                }
1.1.1.33  root     8603:                                                msdos_putch(0x03);
                   8604:                                                msdos_putch(0x0d);
                   8605:                                                msdos_putch(0x0a);
1.1.1.26  root     8606:                                                break;
1.1.1.33  root     8607:                                        } else if(ctrl_break_pressed) {
                   8608:                                                // skip this byte
1.1.1.26  root     8609:                                        } else if(chr == 0x00) {
1.1       root     8610:                                                // skip 2nd byte
                   8611:                                                msdos_getch();
                   8612:                                        } else if(chr == 0x0d) {
                   8613:                                                // carriage return
                   8614:                                                buf[p++] = 0x0d;
                   8615:                                                if(max > p) {
                   8616:                                                        buf[p++] = 0x0a;
                   8617:                                                }
1.1.1.14  root     8618:                                                msdos_putch('\n');
1.1       root     8619:                                                break;
                   8620:                                        } else if(chr == 0x08) {
                   8621:                                                // back space
                   8622:                                                if(p > 0) {
                   8623:                                                        p--;
1.1.1.20  root     8624:                                                        if(msdos_ctrl_code_check(buf[p])) {
1.1.1.34! root     8625:                                                                msdos_putch(0x08);
        !          8626:                                                                msdos_putch(0x08);
        !          8627:                                                                msdos_putch(0x20);
        !          8628:                                                                msdos_putch(0x20);
        !          8629:                                                                msdos_putch(0x08);
        !          8630:                                                                msdos_putch(0x08);
1.1.1.20  root     8631:                                                        } else {
1.1.1.34! root     8632:                                                                msdos_putch(0x08);
        !          8633:                                                                msdos_putch(0x20);
        !          8634:                                                                msdos_putch(0x08);
        !          8635:                                                        }
        !          8636:                                                }
        !          8637:                                        } else if(chr == 0x1b) {
        !          8638:                                                // escape
        !          8639:                                                while(p > 0) {
        !          8640:                                                        p--;
        !          8641:                                                        if(msdos_ctrl_code_check(buf[p])) {
        !          8642:                                                                msdos_putch(0x08);
        !          8643:                                                                msdos_putch(0x08);
        !          8644:                                                                msdos_putch(0x20);
        !          8645:                                                                msdos_putch(0x20);
        !          8646:                                                                msdos_putch(0x08);
        !          8647:                                                                msdos_putch(0x08);
        !          8648:                                                        } else {
        !          8649:                                                                msdos_putch(0x08);
        !          8650:                                                                msdos_putch(0x20);
        !          8651:                                                                msdos_putch(0x08);
1.1.1.20  root     8652:                                                        }
1.1       root     8653:                                                }
                   8654:                                        } else {
                   8655:                                                buf[p++] = chr;
                   8656:                                                msdos_putch(chr);
                   8657:                                        }
                   8658:                                }
                   8659:                                REG16(AX) = p;
1.1.1.26  root     8660:                                
1.1.1.8   root     8661:                                // some seconds may be passed in console
1.1       root     8662:                                hardware_update();
                   8663:                        } else {
1.1.1.20  root     8664:                                REG16(AX) = _read(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     8665:                        }
                   8666:                } else {
                   8667:                        REG16(AX) = 0x05;
1.1.1.3   root     8668:                        m_CF = 1;
1.1       root     8669:                }
                   8670:        } else {
                   8671:                REG16(AX) = 0x06;
1.1.1.3   root     8672:                m_CF = 1;
1.1       root     8673:        }
                   8674: }
                   8675: 
                   8676: inline void msdos_int_21h_40h()
                   8677: {
                   8678:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     8679:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     8680:        
1.1.1.20  root     8681:        if(fd < process->max_files && file_handler[fd].valid) {
                   8682:                if(file_mode[file_handler[fd].mode].out) {
1.1       root     8683:                        if(REG16(CX)) {
1.1.1.20  root     8684:                                if(file_handler[fd].atty) {
1.1       root     8685:                                        // BX is stdout/stderr or is redirected to stdout
                   8686:                                        for(int i = 0; i < REG16(CX); i++) {
1.1.1.3   root     8687:                                                msdos_putch(mem[SREG_BASE(DS) + REG16(DX) + i]);
1.1       root     8688:                                        }
                   8689:                                        REG16(AX) = REG16(CX);
                   8690:                                } else {
1.1.1.20  root     8691:                                        REG16(AX) = msdos_write(fd, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     8692:                                }
                   8693:                        } else {
1.1.1.20  root     8694:                                UINT32 pos = _tell(fd);
                   8695:                                _lseek(fd, 0, SEEK_END);
                   8696:                                UINT32 size = _tell(fd);
1.1.1.12  root     8697:                                if(pos < size) {
1.1.1.20  root     8698:                                        _lseek(fd, pos, SEEK_SET);
                   8699:                                        SetEndOfFile((HANDLE)_get_osfhandle(fd));
1.1.1.12  root     8700:                                } else {
                   8701:                                        for(UINT32 i = size; i < pos; i++) {
                   8702:                                                UINT8 tmp = 0;
1.1.1.23  root     8703:                                                msdos_write(fd, &tmp, 1);
1.1.1.12  root     8704:                                        }
1.1.1.20  root     8705:                                        _lseek(fd, pos, SEEK_SET);
1.1       root     8706:                                }
1.1.1.23  root     8707:                                REG16(AX) = 0;
1.1       root     8708:                        }
                   8709:                } else {
                   8710:                        REG16(AX) = 0x05;
1.1.1.3   root     8711:                        m_CF = 1;
1.1       root     8712:                }
                   8713:        } else {
                   8714:                REG16(AX) = 0x06;
1.1.1.3   root     8715:                m_CF = 1;
1.1       root     8716:        }
                   8717: }
                   8718: 
                   8719: inline void msdos_int_21h_41h(int lfn)
                   8720: {
1.1.1.3   root     8721:        if(remove(msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn))) {
1.1       root     8722:                REG16(AX) = errno;
1.1.1.3   root     8723:                m_CF = 1;
1.1       root     8724:        }
                   8725: }
                   8726: 
                   8727: inline void msdos_int_21h_42h()
                   8728: {
                   8729:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     8730:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     8731:        
1.1.1.20  root     8732:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     8733:                if(REG8(AL) < 0x03) {
                   8734:                        static int ptrname[] = { SEEK_SET, SEEK_CUR, SEEK_END };
1.1.1.20  root     8735:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), ptrname[REG8(AL)]);
                   8736:                        UINT32 pos = _tell(fd);
1.1       root     8737:                        REG16(AX) = pos & 0xffff;
                   8738:                        REG16(DX) = (pos >> 16);
                   8739:                } else {
                   8740:                        REG16(AX) = 0x01;
1.1.1.3   root     8741:                        m_CF = 1;
1.1       root     8742:                }
                   8743:        } else {
                   8744:                REG16(AX) = 0x06;
1.1.1.3   root     8745:                m_CF = 1;
1.1       root     8746:        }
                   8747: }
                   8748: 
                   8749: inline void msdos_int_21h_43h(int lfn)
                   8750: {
1.1.1.3   root     8751:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn);
1.1       root     8752:        int attr;
                   8753:        
1.1.1.14  root     8754:        if(!lfn && REG8(AL) > 2) {
                   8755:                REG16(AX) = 0x01;
                   8756:                m_CF = 1;
                   8757:                return;
                   8758:        }
                   8759:        switch(REG8(lfn ? BL : AL)) {
1.1       root     8760:        case 0x00:
                   8761:                if((attr = GetFileAttributes(path)) != -1) {
1.1.1.14  root     8762:                        REG16(CX) = (UINT16)msdos_file_attribute_create((UINT16)attr);
                   8763:                } else {
                   8764:                        REG16(AX) = (UINT16)GetLastError();
                   8765:                        m_CF = 1;
                   8766:                }
                   8767:                break;
                   8768:        case 0x01:
                   8769:                if(!SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)))) {
                   8770:                        REG16(AX) = (UINT16)GetLastError();
                   8771:                        m_CF = 1;
                   8772:                }
                   8773:                break;
                   8774:        case 0x02:
                   8775:                {
                   8776:                        DWORD size = GetCompressedFileSize(path, NULL);
                   8777:                        if(size != INVALID_FILE_SIZE) {
                   8778:                                if(size != 0 && size == GetFileSize(path, NULL)) {
                   8779:                                        DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   8780:                                        // this isn't correct if the file is in the NTFS MFT
                   8781:                                        if(GetDiskFreeSpace(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
                   8782:                                                size = ((size - 1) | (sectors_per_cluster * bytes_per_sector - 1)) + 1;
                   8783:                                        }
                   8784:                                }
                   8785:                                REG16(AX) = LOWORD(size);
                   8786:                                REG16(DX) = HIWORD(size);
                   8787:                        } else {
                   8788:                                REG16(AX) = (UINT16)GetLastError();
                   8789:                                m_CF = 1;
1.1       root     8790:                        }
1.1.1.14  root     8791:                }
                   8792:                break;
                   8793:        case 0x03:
                   8794:        case 0x05:
                   8795:        case 0x07:
                   8796:                {
                   8797:                        HANDLE hFile = CreateFile(path, FILE_WRITE_ATTRIBUTES, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   8798:                        if(hFile != INVALID_HANDLE_VALUE) {
                   8799:                                FILETIME local, time;
                   8800:                                DosDateTimeToFileTime(REG16(DI), /*REG8(BL) == 5 ? 0 : */REG16(CX), &local);
                   8801:                                if(REG8(BL) == 7) {
                   8802:                                        ULARGE_INTEGER hund;
                   8803:                                        hund.LowPart = local.dwLowDateTime;
                   8804:                                        hund.HighPart = local.dwHighDateTime;
                   8805:                                        hund.QuadPart += REG16(SI) * 100000;
                   8806:                                        local.dwLowDateTime = hund.LowPart;
                   8807:                                        local.dwHighDateTime = hund.HighPart;
                   8808:                                }
                   8809:                                LocalFileTimeToFileTime(&local, &time);
                   8810:                                if(!SetFileTime(hFile, REG8(BL) == 0x07 ? &time : NULL,
                   8811:                                                       REG8(BL) == 0x05 ? &time : NULL,
                   8812:                                                       REG8(BL) == 0x03 ? &time : NULL)) {
                   8813:                                        REG16(AX) = (UINT16)GetLastError();
                   8814:                                        m_CF = 1;
                   8815:                                }
                   8816:                                CloseHandle(hFile);
                   8817:                        } else {
                   8818:                                REG16(AX) = (UINT16)GetLastError();
                   8819:                                m_CF = 1;
1.1       root     8820:                        }
1.1.1.14  root     8821:                }
                   8822:                break;
                   8823:        case 0x04:
                   8824:        case 0x06:
                   8825:        case 0x08:
                   8826:                {
                   8827:                        WIN32_FILE_ATTRIBUTE_DATA fad;
                   8828:                        if(GetFileAttributesEx(path, GetFileExInfoStandard, (LPVOID)&fad)) {
                   8829:                                FILETIME *time, local;
                   8830:                                time = REG8(BL) == 0x04 ? &fad.ftLastWriteTime :
                   8831:                                                   0x06 ? &fad.ftLastAccessTime :
                   8832:                                                          &fad.ftCreationTime;
                   8833:                                FileTimeToLocalFileTime(time, &local);
                   8834:                                FileTimeToDosDateTime(&local, &REG16(DI), &REG16(CX));
                   8835:                                if(REG8(BL) == 0x08) {
                   8836:                                        ULARGE_INTEGER hund;
                   8837:                                        hund.LowPart = local.dwLowDateTime;
                   8838:                                        hund.HighPart = local.dwHighDateTime;
                   8839:                                        hund.QuadPart /= 100000;
                   8840:                                        REG16(SI) = (UINT16)(hund.QuadPart % 200);
                   8841:                                }
                   8842:                        } else {
                   8843:                                REG16(AX) = (UINT16)GetLastError();
                   8844:                                m_CF = 1;
1.1       root     8845:                        }
1.1.1.14  root     8846:                }
                   8847:                break;
                   8848:        default:
1.1.1.22  root     8849:                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     8850:                REG16(AX) = 0x01;
                   8851:                m_CF = 1;
                   8852:                break;
                   8853:        }
                   8854: }
                   8855: 
                   8856: inline void msdos_int_21h_44h()
                   8857: {
1.1.1.22  root     8858:        static UINT16 iteration_count = 0;
                   8859:        
1.1.1.20  root     8860:        process_t *process = msdos_process_info_get(current_psp);
                   8861:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   8862:        
1.1.1.14  root     8863:        UINT32 val = DRIVE_NO_ROOT_DIR;
                   8864:        
                   8865:        switch(REG8(AL)) {
                   8866:        case 0x00:
                   8867:        case 0x01:
                   8868:        case 0x02:
                   8869:        case 0x03:
                   8870:        case 0x04:
                   8871:        case 0x05:
                   8872:        case 0x06:
                   8873:        case 0x07:
1.1.1.20  root     8874:                if(fd >= process->max_files || !file_handler[fd].valid) {
                   8875:                        REG16(AX) = 0x06;
                   8876:                        m_CF = 1;
                   8877:                        return;
1.1.1.14  root     8878:                }
                   8879:                break;
                   8880:        case 0x08:
                   8881:        case 0x09:
                   8882:                if(REG8(BL) >= ('Z' - 'A' + 1)) {
                   8883:                        // invalid drive number
                   8884:                        REG16(AX) = 0x0f;
                   8885:                        m_CF = 1;
                   8886:                        return;
                   8887:                } else {
                   8888:                        if(REG8(BL) == 0) {
                   8889:                                val = GetDriveType(NULL);
                   8890:                        } else {
                   8891:                                char tmp[8];
                   8892:                                sprintf(tmp, "%c:\\", 'A' + REG8(BL) - 1);
                   8893:                                val = GetDriveType(tmp);
                   8894:                        }
                   8895:                        if(val == DRIVE_NO_ROOT_DIR) {
                   8896:                                // no drive
                   8897:                                REG16(AX) = 0x0f;
                   8898:                                m_CF = 1;
                   8899:                                return;
1.1       root     8900:                        }
                   8901:                }
                   8902:                break;
                   8903:        }
                   8904:        switch(REG8(AL)) {
                   8905:        case 0x00: // get ioctrl data
1.1.1.20  root     8906:                REG16(DX) = file_handler[fd].info;
1.1       root     8907:                break;
                   8908:        case 0x01: // set ioctrl data
1.1.1.20  root     8909:                file_handler[fd].info |= REG8(DL);
1.1       root     8910:                break;
                   8911:        case 0x02: // recv from character device
                   8912:        case 0x03: // send to character device
                   8913:        case 0x04: // recv from block device
                   8914:        case 0x05: // send to block device
                   8915:                REG16(AX) = 0x05;
1.1.1.3   root     8916:                m_CF = 1;
1.1       root     8917:                break;
                   8918:        case 0x06: // get read status
1.1.1.20  root     8919:                if(file_mode[file_handler[fd].mode].in) {
                   8920:                        if(file_handler[fd].atty) {
1.1.1.14  root     8921:                                REG8(AL) = msdos_kbhit() ? 0xff : 0x00;
1.1       root     8922:                        } else {
1.1.1.20  root     8923:                                REG8(AL) = eof(fd) ? 0x00 : 0xff;
1.1       root     8924:                        }
1.1.1.14  root     8925:                } else {
                   8926:                        REG8(AL) = 0x00;
1.1       root     8927:                }
                   8928:                break;
                   8929:        case 0x07: // get write status
1.1.1.20  root     8930:                if(file_mode[file_handler[fd].mode].out) {
1.1.1.14  root     8931:                        REG8(AL) = 0xff;
                   8932:                } else {
                   8933:                        REG8(AL) = 0x00;
1.1       root     8934:                }
                   8935:                break;
                   8936:        case 0x08: // check removable drive
1.1.1.14  root     8937:                if(val == DRIVE_REMOVABLE || val == DRIVE_CDROM) {
                   8938:                        // removable drive
                   8939:                        REG16(AX) = 0x00;
1.1       root     8940:                } else {
1.1.1.14  root     8941:                        // fixed drive
                   8942:                        REG16(AX) = 0x01;
1.1       root     8943:                }
                   8944:                break;
                   8945:        case 0x09: // check remote drive
1.1.1.14  root     8946:                if(val == DRIVE_REMOTE) {
                   8947:                        // remote drive
                   8948:                        REG16(DX) = 0x1000;
1.1       root     8949:                } else {
1.1.1.14  root     8950:                        // local drive
                   8951:                        REG16(DX) = 0x00;
1.1       root     8952:                }
                   8953:                break;
1.1.1.21  root     8954:        case 0x0a: // check remote handle
                   8955:                REG16(DX) = 0x00; // FIXME
                   8956:                break;
1.1       root     8957:        case 0x0b: // set retry count
                   8958:                break;
1.1.1.22  root     8959:        case 0x0c: // generic character device request
                   8960:                if(REG8(CL) == 0x45) {
                   8961:                        // set iteration (retry) count
                   8962:                        iteration_count = *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
                   8963:                } else if(REG8(CL) == 0x4a) {
                   8964:                        // select code page
                   8965:                        active_code_page = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2);
                   8966:                        msdos_nls_tables_update();
                   8967:                } else if(REG8(CL) == 0x65) {
                   8968:                        // get iteration (retry) count
                   8969:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(DX)) = iteration_count;
                   8970:                } else if(REG8(CL) == 0x6a) {
                   8971:                        // query selected code page
                   8972:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = 2; // FIXME
                   8973:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 2) = active_code_page;
                   8974:                        
                   8975:                        CPINFO info;
                   8976:                        GetCPInfo(active_code_page, &info);
                   8977:                        
                   8978:                        if(info.MaxCharSize != 1) {
                   8979:                                for(int i = 0;; i++) {
                   8980:                                        UINT8 lo = info.LeadByte[2 * i + 0];
                   8981:                                        UINT8 hi = info.LeadByte[2 * i + 1];
                   8982:                                        
                   8983:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 0) = lo;
                   8984:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 4 + 4 * i + 2) = hi;
                   8985:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0) + 4;
                   8986:                                        
                   8987:                                        if(lo == 0 && hi == 0) {
                   8988:                                                break;
                   8989:                                        }
                   8990:                                }
                   8991:                        }
                   8992:                } else if(REG8(CL) == 0x7f) {
                   8993:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x00) = 0;
                   8994:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x01) = 0;
                   8995:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x02) = 14;
                   8996:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x04) = 1;
                   8997:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x06) = 1;
                   8998:                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(DX) + 0x07) = 0;
                   8999:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x08) = 4;
                   9000:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0a) =  8 * (*(UINT16 *)(mem + 0x44a));
                   9001:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0c) = 16 * (*(UINT8  *)(mem + 0x484) + 1);
                   9002:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x0e) = *(UINT16 *)(mem + 0x44a);
                   9003:                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(DX) + 0x10) = *(UINT8  *)(mem + 0x484) + 1;
                   9004:                } else {
                   9005:                        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));
                   9006:                        REG16(AX) = 0x01; // invalid function
                   9007:                        m_CF = 1;
                   9008:                }
                   9009:                break;
                   9010:        case 0x0d: // generic block device request
                   9011:                if(REG8(CL) == 0x40) {
                   9012:                        // set device parameters
                   9013:                } else if(REG8(CL) == 0x46) {
                   9014:                        // set volume serial number
                   9015:                } else if(REG8(CL) == 0x4a) {
                   9016:                        // lock logical volume
                   9017:                } else if(REG8(CL) == 0x4b) {
                   9018:                        // lock physical volume
                   9019:                } else if(REG8(CL) == 0x60) {
                   9020:                        // get device parameters
                   9021:                        char dev[] = "\\\\.\\A:";
                   9022:                        dev[4] = 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   9023:                        
                   9024:                        HANDLE hFile = CreateFile(dev, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   9025:                        if(hFile != INVALID_HANDLE_VALUE) {
                   9026:                                DISK_GEOMETRY geo;
                   9027:                                DWORD dwSize;
                   9028:                                if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &geo, sizeof(geo), &dwSize, NULL)) {
                   9029:                                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x07; // ???
                   9030:                                        switch(geo.MediaType) {
                   9031:                                        case F5_360_512:
                   9032:                                        case F5_320_512:
                   9033:                                        case F5_320_1024:
                   9034:                                        case F5_180_512:
                   9035:                                        case F5_160_512:
                   9036:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // 320K/360K disk
                   9037:                                                break;
                   9038:                                        case F5_1Pt2_512:
                   9039:                                        case F3_1Pt2_512:
                   9040:                                        case F3_1Pt23_1024:
                   9041:                                        case F5_1Pt23_1024:
                   9042:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x01; // 1.2M disk
                   9043:                                                break;
                   9044:                                        case F3_720_512:
                   9045:                                        case F3_640_512:
                   9046:                                        case F5_640_512:
                   9047:                                        case F5_720_512:
                   9048:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02; // 720K disk
                   9049:                                                break;
                   9050:                                        case F8_256_128:
                   9051:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x03; // single-density 8-inch disk
                   9052:                                                break;
                   9053:                                        case FixedMedia:
                   9054:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   9055:                                                break;
                   9056:                                        case F3_1Pt44_512:
                   9057:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   9058:                                                break;
                   9059:                                        case F3_2Pt88_512:
                   9060:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09; // (DOS 5+) 2.88M floppy
                   9061:                                                break;
                   9062:                                        default:
                   9063:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // fixed disk
                   9064: //                                             *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x05; // (DOS 3.3+) other type of block device, normally 1.44M floppy
                   9065:                                                break;
                   9066:                                        }
                   9067:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = (geo.MediaType == FixedMedia) ? 0x01 : 0x00;
                   9068:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04) = (geo.Cylinders.QuadPart > 0xffff) ? 0xffff : geo.Cylinders.QuadPart;
                   9069:                                        switch(geo.MediaType) {
                   9070:                                        case F5_360_512:
                   9071:                                        case F5_320_512:
                   9072:                                        case F5_320_1024:
                   9073:                                        case F5_180_512:
                   9074:                                        case F5_160_512:
                   9075:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x01; // 320K/360K disk
                   9076:                                                break;
                   9077:                                        default:
                   9078:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06) = 0x00; // other drive types
                   9079:                                                break;
                   9080:                                        }
                   9081:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x00) = geo.BytesPerSector;
                   9082:                                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x02) = geo.SectorsPerTrack * geo.TracksPerCylinder;
                   9083:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x03) = 0;
                   9084:                                        *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x05) = 0;
                   9085:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x06) = 0;
                   9086:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x08) = 0;
                   9087:                                        switch(geo.MediaType) {
                   9088:                                        case F5_320_512:        // floppy, double-sided, 8 sectors per track (320K)
                   9089:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xff;
                   9090:                                                break;
                   9091:                                        case F5_160_512:        // floppy, single-sided, 8 sectors per track (160K)
                   9092:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfe;
                   9093:                                                break;
                   9094:                                        case F5_360_512:        // floppy, double-sided, 9 sectors per track (360K)
                   9095:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfd;
                   9096:                                                break;
                   9097:                                        case F5_180_512:        // floppy, single-sided, 9 sectors per track (180K)
                   9098:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xfc;
                   9099:                                                break;
                   9100:                                        case F5_1Pt2_512:       // floppy, double-sided, 15 sectors per track (1.2M)
                   9101:                                        case F3_720_512:        // floppy, double-sided, 9 sectors per track (720K,3.5")
                   9102:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf9;
                   9103:                                                break;
                   9104:                                        case FixedMedia:        // hard disk
                   9105:                                        case RemovableMedia:
                   9106:                                        case Unknown:
                   9107:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf8;
                   9108:                                                break;
                   9109:                                        default:
                   9110:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0a) = 0xf0;
                   9111:                                                break;
                   9112:                                        }
                   9113:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0d) = geo.SectorsPerTrack;
                   9114:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x0f) = 1;
                   9115:                                        *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x11) = 0;
                   9116:                                        *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x15) = geo.TracksPerCylinder * geo.Cylinders.QuadPart;
                   9117:                                        *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07 + 0x1f) = geo.Cylinders.QuadPart;
                   9118:                                        // 21h  BYTE    device type
                   9119:                                        // 22h  WORD    device attributes (removable or not, etc)
                   9120:                                } else {
                   9121:                                        REG16(AX) = 0x0f; // invalid drive
                   9122:                                        m_CF = 1;
                   9123:                                }
                   9124:                                CloseHandle(hFile);
                   9125:                        } else {
                   9126:                                REG16(AX) = 0x0f; // invalid drive
                   9127:                                m_CF = 1;
                   9128:                        }
                   9129:                } else if(REG8(CL) == 0x66) {
                   9130:                        // get volume serial number
                   9131:                        char path[] = "A:\\";
                   9132:                        char volume_label[MAX_PATH];
                   9133:                        DWORD serial_number = 0;
                   9134:                        char file_system[MAX_PATH];
                   9135:                        
                   9136:                        path[0] = 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1;
                   9137:                        
                   9138:                        if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
                   9139:                                *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   9140:                                *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x02) = serial_number;
                   9141:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x06, 0x20, 11);
                   9142:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x06, volume_label, min(strlen(volume_label), 11));
                   9143:                                memset(mem + SREG_BASE(DS) + REG16(SI) + 0x11, 0x20,  8);
                   9144:                                memcpy(mem + SREG_BASE(DS) + REG16(SI) + 0x11, file_system, min(strlen(file_system), 8));
                   9145:                        } else {
                   9146:                                REG16(AX) = 0x0f; // invalid drive
                   9147:                                m_CF = 1;
                   9148:                        }
                   9149:                } else if(REG8(CL) == 0x67) {
                   9150:                        // get access flag
                   9151:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0;
                   9152:                        *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 1;
                   9153:                } else if(REG8(CL) == 0x68) {
                   9154:                        // sense media type
                   9155:                        char dev[64];
                   9156:                        sprintf(dev, "\\\\.\\%c:", 'A' + (REG8(BL) ? REG8(BL) : _getdrive()) - 1);
                   9157:                        
                   9158:                        HANDLE hFile = CreateFile(dev, 0, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
                   9159:                        if(hFile != INVALID_HANDLE_VALUE) {
                   9160:                                DISK_GEOMETRY geo;
                   9161:                                DWORD dwSize;
                   9162:                                if(DeviceIoControl(hFile, IOCTL_DISK_GET_DRIVE_GEOMETRY, NULL, 0, &geo, sizeof(geo), &dwSize, NULL)) {
                   9163:                                        switch(geo.MediaType) {
                   9164:                                        case F3_720_512:
                   9165:                                        case F5_720_512:
                   9166:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   9167:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x02;
                   9168:                                                break;
                   9169:                                        case F3_1Pt44_512:
                   9170:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   9171:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x07;
                   9172:                                                break;
                   9173:                                        case F3_2Pt88_512:
                   9174:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x01;
                   9175:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x09;
                   9176:                                                break;
                   9177:                                        default:
                   9178:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00) = 0x00;
                   9179:                                                *(UINT8 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x01) = 0x00; // ???
                   9180:                                                break;
                   9181:                                        }
                   9182:                                } else {
                   9183:                                        REG16(AX) = 0x0f; // invalid drive
                   9184:                                        m_CF = 1;
                   9185:                                }
                   9186:                                CloseHandle(hFile);
                   9187:                        } else {
                   9188:                                REG16(AX) = 0x0f; // invalid drive
                   9189:                                m_CF = 1;
                   9190:                        }
                   9191:                } else if(REG8(CL) == 0x6a) {
                   9192:                        // unlock logical volume
                   9193:                } else if(REG8(CL) == 0x6b) {
                   9194:                        // unlock physical volume
                   9195:                } else {
                   9196:                        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));
                   9197:                        REG16(AX) = 0x01; // invalid function
                   9198:                        m_CF = 1;
                   9199:                }
                   9200:                break;
                   9201:        case 0x0e: // get logical drive map
                   9202:                {
                   9203:                        DWORD bits = 1 << ((REG8(BL) ? REG8(BL) : _getdrive()) - 1);
                   9204:                        if(!(GetLogicalDrives() & bits)) {
                   9205:                                REG16(AX) = 0x0f; // invalid drive
                   9206:                                m_CF = 1;
                   9207:                        } else {
                   9208:                                REG8(AL) = 0;
                   9209:                        }
                   9210:                }
                   9211:                break;
                   9212:        case 0x0f: // set logical drive map
                   9213:                {
                   9214:                        DWORD bits = 1 << ((REG8(BL) ? REG8(BL) : _getdrive()) - 1);
                   9215:                        if(!(GetLogicalDrives() & bits)) {
                   9216:                                REG16(AX) = 0x0f; // invalid drive
                   9217:                                m_CF = 1;
                   9218:                        }
                   9219:                }
                   9220:                break;
                   9221:        case 0x10: // query generic ioctrl capability (handle)
                   9222:                switch(REG8(CL)) {
                   9223:                case 0x45:
                   9224:                case 0x4a:
                   9225:                case 0x65:
                   9226:                case 0x6a:
                   9227:                case 0x7f:
                   9228:                        REG16(AX) = 0x0000; // supported
                   9229:                        break;
                   9230:                default:
                   9231:                        REG8(AL) = 0x01; // ioctl capability not available
                   9232:                        m_CF = 1;
                   9233:                        break;
                   9234:                }
                   9235:                break;
                   9236:        case 0x11: // query generic ioctrl capability (drive)
                   9237:                switch(REG8(CL)) {
                   9238:                case 0x40:
                   9239:                case 0x46:
                   9240:                case 0x4a:
                   9241:                case 0x4b:
                   9242:                case 0x60:
                   9243:                case 0x66:
                   9244:                case 0x67:
                   9245:                case 0x68:
                   9246:                case 0x6a:
                   9247:                case 0x6b:
                   9248:                        REG16(AX) = 0x0000; // supported
                   9249:                        break;
                   9250:                default:
                   9251:                        REG8(AL) = 0x01; // ioctl capability not available
                   9252:                        m_CF = 1;
                   9253:                        break;
                   9254:                }
                   9255:                break;
                   9256:        case 0x12: // determine dos type
                   9257:        case 0x51: // concurrent dos v3.2+ - installation check
                   9258:        case 0x52: // determine dos type/get dr dos versuin
                   9259:                REG16(AX) = 0x01; // this  is not DR-DOS
                   9260:                m_CF = 1;
                   9261:                break;
1.1       root     9262:        default:
1.1.1.22  root     9263:                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     9264:                REG16(AX) = 0x01;
1.1.1.3   root     9265:                m_CF = 1;
1.1       root     9266:                break;
                   9267:        }
                   9268: }
                   9269: 
                   9270: inline void msdos_int_21h_45h()
                   9271: {
                   9272:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     9273:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     9274:        
1.1.1.20  root     9275:        if(fd < process->max_files && file_handler[fd].valid) {
                   9276:                int dup_fd = _dup(fd);
                   9277:                if(dup_fd != -1) {
                   9278:                        REG16(AX) = dup_fd;
                   9279:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   9280: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   9281:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     9282:                } else {
                   9283:                        REG16(AX) = errno;
1.1.1.3   root     9284:                        m_CF = 1;
1.1       root     9285:                }
                   9286:        } else {
                   9287:                REG16(AX) = 0x06;
1.1.1.3   root     9288:                m_CF = 1;
1.1       root     9289:        }
                   9290: }
                   9291: 
                   9292: inline void msdos_int_21h_46h()
                   9293: {
                   9294:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     9295:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   9296:        int dup_fd = REG16(CX);
                   9297:        int tmp_fd = msdos_psp_get_file_table(REG16(CX), current_psp);
1.1       root     9298:        
1.1.1.20  root     9299:        if(REG16(BX) == REG16(CX)) {
                   9300:                REG16(AX) = 0x06;
                   9301:                m_CF = 1;
                   9302:        } else if(fd < process->max_files && file_handler[fd].valid && dup_fd < process->max_files) {
                   9303:                if(tmp_fd < process->max_files && file_handler[tmp_fd].valid) {
                   9304:                        _close(tmp_fd);
                   9305:                        msdos_file_handler_close(tmp_fd);
                   9306:                        msdos_psp_set_file_table(dup_fd, 0x0ff, current_psp);
                   9307:                }
                   9308:                if(_dup2(fd, dup_fd) != -1) {
                   9309:                        msdos_file_handler_dup(dup_fd, fd, current_psp);
                   9310: //                     msdos_psp_set_file_table(dup_fd, fd, current_psp);
                   9311:                        msdos_psp_set_file_table(dup_fd, dup_fd, current_psp);
1.1       root     9312:                } else {
                   9313:                        REG16(AX) = errno;
1.1.1.3   root     9314:                        m_CF = 1;
1.1       root     9315:                }
                   9316:        } else {
                   9317:                REG16(AX) = 0x06;
1.1.1.3   root     9318:                m_CF = 1;
1.1       root     9319:        }
                   9320: }
                   9321: 
                   9322: inline void msdos_int_21h_47h(int lfn)
                   9323: {
                   9324:        char path[MAX_PATH];
                   9325:        
                   9326:        if(_getdcwd(REG8(DL), path, MAX_PATH) != NULL) {
                   9327:                if(path[1] == ':') {
                   9328:                        // the returned path does not include a drive or the initial backslash
1.1.1.3   root     9329:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), (lfn ? path : msdos_short_path(path)) + 3);
1.1       root     9330:                } else {
1.1.1.3   root     9331:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), lfn ? path : msdos_short_path(path));
1.1       root     9332:                }
                   9333:        } else {
                   9334:                REG16(AX) = errno;
1.1.1.3   root     9335:                m_CF = 1;
1.1       root     9336:        }
                   9337: }
                   9338: 
                   9339: inline void msdos_int_21h_48h()
                   9340: {
1.1.1.19  root     9341:        int seg, umb_linked;
1.1       root     9342:        
1.1.1.8   root     9343:        if((malloc_strategy & 0xf0) == 0x00) {
1.1.1.19  root     9344:                // unlink umb not to allocate memory in umb
                   9345:                if((umb_linked = msdos_mem_get_umb_linked()) != 0) {
                   9346:                        msdos_mem_unlink_umb();
                   9347:                }
1.1.1.8   root     9348:                if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   9349:                        REG16(AX) = seg;
                   9350:                } else {
                   9351:                        REG16(AX) = 0x08;
                   9352:                        REG16(BX) = msdos_mem_get_free(first_mcb, 0);
                   9353:                        m_CF = 1;
                   9354:                }
1.1.1.19  root     9355:                if(umb_linked != 0) {
                   9356:                        msdos_mem_link_umb();
                   9357:                }
1.1.1.8   root     9358:        } else if((malloc_strategy & 0xf0) == 0x40) {
                   9359:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   9360:                        REG16(AX) = seg;
                   9361:                } else {
                   9362:                        REG16(AX) = 0x08;
                   9363:                        REG16(BX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   9364:                        m_CF = 1;
                   9365:                }
                   9366:        } else if((malloc_strategy & 0xf0) == 0x80) {
                   9367:                if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(BX), 0)) != -1) {
                   9368:                        REG16(AX) = seg;
                   9369:                } else if((seg = msdos_mem_alloc(first_mcb, REG16(BX), 0)) != -1) {
                   9370:                        REG16(AX) = seg;
                   9371:                } else {
                   9372:                        REG16(AX) = 0x08;
                   9373:                        REG16(BX) = max(msdos_mem_get_free(UMB_TOP >> 4, 0), msdos_mem_get_free(first_mcb, 0));
                   9374:                        m_CF = 1;
                   9375:                }
1.1       root     9376:        }
                   9377: }
                   9378: 
                   9379: inline void msdos_int_21h_49h()
                   9380: {
1.1.1.14  root     9381:        int mcb_seg = SREG(ES) - 1;
                   9382:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   9383:        
                   9384:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   9385:                msdos_mem_free(SREG(ES));
                   9386:        } else {
1.1.1.33  root     9387:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.14  root     9388:                m_CF = 1;
                   9389:        }
1.1       root     9390: }
                   9391: 
                   9392: inline void msdos_int_21h_4ah()
                   9393: {
1.1.1.14  root     9394:        int mcb_seg = SREG(ES) - 1;
                   9395:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1       root     9396:        int max_paragraphs;
                   9397:        
1.1.1.14  root     9398:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   9399:                if(msdos_mem_realloc(SREG(ES), REG16(BX), &max_paragraphs)) {
                   9400:                        REG16(AX) = 0x08;
                   9401:                        REG16(BX) = max_paragraphs > 0x7fff && limit_max_memory ? 0x7fff : max_paragraphs;
                   9402:                        m_CF = 1;
                   9403:                }
                   9404:        } else {
1.1.1.33  root     9405:                REG16(AX) = 0x09; // illegal memory block address
1.1.1.3   root     9406:                m_CF = 1;
1.1       root     9407:        }
                   9408: }
                   9409: 
                   9410: inline void msdos_int_21h_4bh()
                   9411: {
1.1.1.3   root     9412:        char *command = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   9413:        param_block_t *param = (param_block_t *)(mem + SREG_BASE(ES) + REG16(BX));
1.1       root     9414:        
                   9415:        switch(REG8(AL)) {
                   9416:        case 0x00:
                   9417:        case 0x01:
                   9418:                if(msdos_process_exec(command, param, REG8(AL))) {
                   9419:                        REG16(AX) = 0x02;
1.1.1.3   root     9420:                        m_CF = 1;
1.1       root     9421:                }
                   9422:                break;
1.1.1.14  root     9423:        case 0x03:
                   9424:                {
                   9425:                        int fd;
                   9426:                        if((fd = _open(command, _O_RDONLY | _O_BINARY)) == -1) {
                   9427:                                REG16(AX) = 0x02;
                   9428:                                m_CF = 1;
                   9429:                                break;
                   9430:                        }
                   9431:                        int size = _read(fd, file_buffer, sizeof(file_buffer));
                   9432:                        _close(fd);
                   9433:                        
                   9434:                        UINT16 *overlay = (UINT16 *)param;
                   9435:                        
                   9436:                        // check exe header
                   9437:                        exe_header_t *header = (exe_header_t *)file_buffer;
                   9438:                        int header_size = 0;
                   9439:                        if(header->mz == 0x4d5a || header->mz == 0x5a4d) {
                   9440:                                header_size = header->header_size * 16;
                   9441:                                // relocation
                   9442:                                int start_seg = overlay[1];
                   9443:                                for(int i = 0; i < header->relocations; i++) {
                   9444:                                        int ofs = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 0);
                   9445:                                        int seg = *(UINT16 *)(file_buffer + header->relocation_table + i * 4 + 2);
                   9446:                                        *(UINT16 *)(file_buffer + header_size + (seg << 4) + ofs) += start_seg;
                   9447:                                }
                   9448:                        }
                   9449:                        memcpy(mem + (overlay[0] << 4), file_buffer + header_size, size - header_size);
                   9450:                }
                   9451:                break;
1.1       root     9452:        default:
1.1.1.22  root     9453:                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     9454:                REG16(AX) = 0x01;
1.1.1.3   root     9455:                m_CF = 1;
1.1       root     9456:                break;
                   9457:        }
                   9458: }
                   9459: 
                   9460: inline void msdos_int_21h_4ch()
                   9461: {
                   9462:        msdos_process_terminate(current_psp, REG8(AL), 1);
                   9463: }
                   9464: 
                   9465: inline void msdos_int_21h_4dh()
                   9466: {
                   9467:        REG16(AX) = retval;
                   9468: }
                   9469: 
                   9470: inline void msdos_int_21h_4eh()
                   9471: {
                   9472:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     9473:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9474:        find_t *find = (find_t *)(mem + dta_laddr);
1.1.1.3   root     9475:        char *path = msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     9476:        WIN32_FIND_DATA fd;
                   9477:        
1.1.1.14  root     9478:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   9479:        find->find_magic = FIND_MAGIC;
                   9480:        find->dta_index = dtainfo - dtalist;
1.1       root     9481:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     9482:        dtainfo->allowable_mask = REG8(CL);
                   9483:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     9484:        
1.1.1.14  root     9485:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   9486:                dtainfo->allowable_mask &= ~8;
1.1       root     9487:        }
1.1.1.14  root     9488:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   9489:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     9490:                      !msdos_find_file_has_8dot3name(&fd)) {
                   9491:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   9492:                                FindClose(dtainfo->find_handle);
                   9493:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9494:                                break;
                   9495:                        }
                   9496:                }
                   9497:        }
1.1.1.13  root     9498:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     9499:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9500:                msdos_find_file_conv_local_time(&fd);
                   9501:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   9502:                find->size = fd.nFileSizeLow;
1.1.1.13  root     9503:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     9504:                REG16(AX) = 0;
1.1.1.14  root     9505:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     9506:                find->attrib = 8;
                   9507:                find->size = 0;
                   9508:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     9509:                dtainfo->allowable_mask &= ~8;
1.1       root     9510:                REG16(AX) = 0;
                   9511:        } else {
                   9512:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     9513:                m_CF = 1;
1.1       root     9514:        }
                   9515: }
                   9516: 
                   9517: inline void msdos_int_21h_4fh()
                   9518: {
                   9519:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.13  root     9520:        UINT32 dta_laddr = (process->dta.w.h << 4) + process->dta.w.l;
                   9521:        find_t *find = (find_t *)(mem + dta_laddr);
1.1       root     9522:        WIN32_FIND_DATA fd;
                   9523:        
1.1.1.14  root     9524:        if(find->find_magic != FIND_MAGIC || find->dta_index >= MAX_DTAINFO) {
                   9525:                REG16(AX) = 0x12;
                   9526:                m_CF = 1;
                   9527:                return;
                   9528:        }
                   9529:        dtainfo_t *dtainfo = &dtalist[find->dta_index];
1.1.1.13  root     9530:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   9531:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     9532:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, 0) ||
1.1.1.13  root     9533:                              !msdos_find_file_has_8dot3name(&fd)) {
                   9534:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   9535:                                        FindClose(dtainfo->find_handle);
                   9536:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9537:                                        break;
                   9538:                                }
                   9539:                        }
                   9540:                } else {
1.1.1.13  root     9541:                        FindClose(dtainfo->find_handle);
                   9542:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     9543:                }
                   9544:        }
1.1.1.13  root     9545:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     9546:                find->attrib = (UINT8)(fd.dwFileAttributes & 0x3f);
                   9547:                msdos_find_file_conv_local_time(&fd);
                   9548:                FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->date, &find->time);
                   9549:                find->size = fd.nFileSizeLow;
1.1.1.13  root     9550:                strcpy(find->name, msdos_short_name(&fd));
1.1       root     9551:                REG16(AX) = 0;
1.1.1.14  root     9552:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     9553:                find->attrib = 8;
                   9554:                find->size = 0;
                   9555:                strcpy(find->name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     9556:                dtainfo->allowable_mask &= ~8;
1.1       root     9557:                REG16(AX) = 0;
                   9558:        } else {
                   9559:                REG16(AX) = 0x12;
1.1.1.3   root     9560:                m_CF = 1;
1.1       root     9561:        }
                   9562: }
                   9563: 
                   9564: inline void msdos_int_21h_50h()
                   9565: {
1.1.1.8   root     9566:        if(current_psp != REG16(BX)) {
                   9567:                process_t *process = msdos_process_info_get(current_psp);
                   9568:                if(process != NULL) {
                   9569:                        process->psp = REG16(BX);
                   9570:                }
                   9571:                current_psp = REG16(BX);
1.1.1.23  root     9572:                msdos_sda_update(current_psp);
1.1.1.8   root     9573:        }
1.1       root     9574: }
                   9575: 
                   9576: inline void msdos_int_21h_51h()
                   9577: {
                   9578:        REG16(BX) = current_psp;
                   9579: }
                   9580: 
                   9581: inline void msdos_int_21h_52h()
                   9582: {
1.1.1.25  root     9583:        SREG(ES) = DOS_INFO_TOP >> 4;
1.1.1.3   root     9584:        i386_load_segment_descriptor(ES);
1.1.1.25  root     9585:        REG16(BX) = offsetof(dos_info_t, first_dpb);
1.1       root     9586: }
                   9587: 
                   9588: inline void msdos_int_21h_54h()
                   9589: {
                   9590:        process_t *process = msdos_process_info_get(current_psp);
                   9591:        
                   9592:        REG8(AL) = process->verify;
                   9593: }
                   9594: 
                   9595: inline void msdos_int_21h_55h()
                   9596: {
                   9597:        psp_t *psp = (psp_t *)(mem + (REG16(DX) << 4));
                   9598:        
                   9599:        memcpy(mem + (REG16(DX) << 4), mem + (current_psp << 4), sizeof(psp_t));
                   9600:        psp->int_22h.dw = *(UINT32 *)(mem + 4 * 0x22);
                   9601:        psp->int_23h.dw = *(UINT32 *)(mem + 4 * 0x23);
                   9602:        psp->int_24h.dw = *(UINT32 *)(mem + 4 * 0x24);
                   9603:        psp->parent_psp = current_psp;
                   9604: }
                   9605: 
                   9606: inline void msdos_int_21h_56h(int lfn)
                   9607: {
                   9608:        char src[MAX_PATH], dst[MAX_PATH];
1.1.1.3   root     9609:        strcpy(src, msdos_trimmed_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), lfn));
                   9610:        strcpy(dst, msdos_trimmed_path((char *)(mem + SREG_BASE(ES) + REG16(DI)), lfn));
1.1       root     9611:        
                   9612:        if(rename(src, dst)) {
                   9613:                REG16(AX) = errno;
1.1.1.3   root     9614:                m_CF = 1;
1.1       root     9615:        }
                   9616: }
                   9617: 
                   9618: inline void msdos_int_21h_57h()
                   9619: {
                   9620:        FILETIME time, local;
1.1.1.14  root     9621:        FILETIME *ctime, *atime, *mtime;
1.1.1.21  root     9622:        HANDLE hHandle;
1.1       root     9623:        
1.1.1.21  root     9624:        if((hHandle = (HANDLE)_get_osfhandle(REG16(BX))) == INVALID_HANDLE_VALUE) {
1.1.1.14  root     9625:                REG16(AX) = (UINT16)GetLastError();
                   9626:                m_CF = 1;
                   9627:                return;
                   9628:        }
                   9629:        ctime = atime = mtime = NULL;
                   9630:        
1.1       root     9631:        switch(REG8(AL)) {
                   9632:        case 0x00:
1.1.1.6   root     9633:        case 0x01:
1.1.1.14  root     9634:                mtime = &time;
1.1.1.6   root     9635:                break;
                   9636:        case 0x04:
                   9637:        case 0x05:
1.1.1.14  root     9638:                atime = &time;
1.1       root     9639:                break;
1.1.1.6   root     9640:        case 0x06:
                   9641:        case 0x07:
1.1.1.14  root     9642:                ctime = &time;
                   9643:                break;
                   9644:        default:
1.1.1.22  root     9645:                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     9646:                REG16(AX) = 0x01;
                   9647:                m_CF = 1;
                   9648:                return;
                   9649:        }
                   9650:        if(REG8(AL) & 1) {
1.1       root     9651:                DosDateTimeToFileTime(REG16(DX), REG16(CX), &local);
                   9652:                LocalFileTimeToFileTime(&local, &time);
1.1.1.21  root     9653:                if(!SetFileTime(hHandle, ctime, atime, mtime)) {
1.1       root     9654:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     9655:                        m_CF = 1;
1.1       root     9656:                }
1.1.1.14  root     9657:        } else {
1.1.1.21  root     9658:                if(!GetFileTime(hHandle, ctime, atime, mtime)) {
1.1.1.14  root     9659:                        // assume a device and use the current time
                   9660:                        GetSystemTimeAsFileTime(&time);
                   9661:                }
                   9662:                FileTimeToLocalFileTime(&time, &local);
                   9663:                FileTimeToDosDateTime(&local, &REG16(DX), &REG16(CX));
1.1       root     9664:        }
                   9665: }
                   9666: 
                   9667: inline void msdos_int_21h_58h()
                   9668: {
                   9669:        switch(REG8(AL)) {
                   9670:        case 0x00:
1.1.1.7   root     9671:                REG16(AX) = malloc_strategy;
                   9672:                break;
                   9673:        case 0x01:
1.1.1.24  root     9674: //             switch(REG16(BX)) {
                   9675:                switch(REG8(BL)) {
1.1.1.7   root     9676:                case 0x0000:
                   9677:                case 0x0001:
                   9678:                case 0x0002:
                   9679:                case 0x0040:
                   9680:                case 0x0041:
                   9681:                case 0x0042:
                   9682:                case 0x0080:
                   9683:                case 0x0081:
                   9684:                case 0x0082:
                   9685:                        malloc_strategy = REG16(BX);
1.1.1.23  root     9686:                        msdos_sda_update(current_psp);
1.1.1.7   root     9687:                        break;
                   9688:                default:
1.1.1.22  root     9689:                        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     9690:                        REG16(AX) = 0x01;
                   9691:                        m_CF = 1;
                   9692:                        break;
                   9693:                }
                   9694:                break;
                   9695:        case 0x02:
1.1.1.19  root     9696:                REG8(AL) = msdos_mem_get_umb_linked() ? 1 : 0;
1.1.1.7   root     9697:                break;
                   9698:        case 0x03:
1.1.1.24  root     9699: //             switch(REG16(BX)) {
                   9700:                switch(REG8(BL)) {
1.1.1.7   root     9701:                case 0x0000:
1.1.1.19  root     9702:                        msdos_mem_unlink_umb();
                   9703:                        break;
1.1.1.7   root     9704:                case 0x0001:
1.1.1.19  root     9705:                        msdos_mem_link_umb();
1.1.1.7   root     9706:                        break;
                   9707:                default:
1.1.1.22  root     9708:                        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     9709:                        REG16(AX) = 0x01;
                   9710:                        m_CF = 1;
                   9711:                        break;
                   9712:                }
1.1       root     9713:                break;
                   9714:        default:
1.1.1.22  root     9715:                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     9716:                REG16(AX) = 0x01;
1.1.1.3   root     9717:                m_CF = 1;
1.1       root     9718:                break;
                   9719:        }
                   9720: }
                   9721: 
                   9722: inline void msdos_int_21h_59h()
                   9723: {
1.1.1.23  root     9724:        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   9725:        
                   9726:        REG16(AX) = sda->extended_error_code;
                   9727:        REG8(BH) = sda->error_class;
                   9728:        REG8(BL) = sda->suggested_action;
                   9729:        REG8(CH) = sda->locus_of_last_error;
1.1       root     9730: }
                   9731: 
                   9732: inline void msdos_int_21h_5ah()
                   9733: {
1.1.1.3   root     9734:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     9735:        int len = strlen(path);
                   9736:        char tmp[MAX_PATH];
                   9737:        
                   9738:        if(GetTempFileName(path, "TMP", 0, tmp)) {
                   9739:                int fd = _open(tmp, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   9740:                
                   9741:                SetFileAttributes(tmp, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   9742:                REG16(AX) = fd;
                   9743:                msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     9744:                msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     9745:                
                   9746:                strcpy(path, tmp);
                   9747:                int dx = REG16(DX) + len;
1.1.1.3   root     9748:                int ds = SREG(DS);
1.1       root     9749:                while(dx > 0xffff) {
                   9750:                        dx -= 0x10;
                   9751:                        ds++;
                   9752:                }
                   9753:                REG16(DX) = dx;
1.1.1.3   root     9754:                SREG(DS) = ds;
                   9755:                i386_load_segment_descriptor(DS);
1.1       root     9756:        } else {
                   9757:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     9758:                m_CF = 1;
1.1       root     9759:        }
                   9760: }
                   9761: 
                   9762: inline void msdos_int_21h_5bh()
                   9763: {
1.1.1.3   root     9764:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(DX)), 0);
1.1       root     9765:        
1.1.1.24  root     9766:        if(msdos_is_existing_file(path)) {
1.1       root     9767:                // already exists
                   9768:                REG16(AX) = 0x50;
1.1.1.3   root     9769:                m_CF = 1;
1.1       root     9770:        } else {
                   9771:                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   9772:                
                   9773:                if(fd != -1) {
                   9774:                        SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   9775:                        REG16(AX) = fd;
                   9776:                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     9777:                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     9778:                } else {
                   9779:                        REG16(AX) = errno;
1.1.1.3   root     9780:                        m_CF = 1;
1.1       root     9781:                }
                   9782:        }
                   9783: }
                   9784: 
                   9785: inline void msdos_int_21h_5ch()
                   9786: {
                   9787:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     9788:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     9789:        
1.1.1.20  root     9790:        if(fd < process->max_files && file_handler[fd].valid) {
1.1       root     9791:                if(REG8(AL) == 0 || REG8(AL) == 1) {
                   9792:                        static int modes[2] = {_LK_LOCK, _LK_UNLCK};
1.1.1.20  root     9793:                        UINT32 pos = _tell(fd);
                   9794:                        _lseek(fd, (REG16(CX) << 16) | REG16(DX), SEEK_SET);
                   9795:                        if(_locking(fd, modes[REG8(AL)], (REG16(SI) << 16) | REG16(DI))) {
1.1       root     9796:                                REG16(AX) = errno;
1.1.1.3   root     9797:                                m_CF = 1;
1.1       root     9798:                        }
1.1.1.20  root     9799:                        _lseek(fd, pos, SEEK_SET);
1.1.1.26  root     9800:                        
1.1       root     9801:                        // some seconds may be passed in _locking()
                   9802:                        hardware_update();
                   9803:                } else {
                   9804:                        REG16(AX) = 0x01;
1.1.1.3   root     9805:                        m_CF = 1;
1.1       root     9806:                }
                   9807:        } else {
                   9808:                REG16(AX) = 0x06;
1.1.1.3   root     9809:                m_CF = 1;
1.1       root     9810:        }
                   9811: }
                   9812: 
1.1.1.22  root     9813: inline void msdos_int_21h_5dh()
                   9814: {
                   9815:        switch(REG8(AL)) {
                   9816:        case 0x06: // get address of dos swappable data area
1.1.1.23  root     9817:                SREG(DS) = (SDA_TOP >> 4);
                   9818:                i386_load_segment_descriptor(DS);
                   9819:                REG16(SI) = offsetof(sda_t, crit_error_flag);
                   9820:                REG16(CX) = 0x80;
                   9821:                REG16(DX) = 0x1a;
                   9822:                break;
                   9823:        case 0x0b: // get dos swappable data areas
1.1.1.22  root     9824:                REG16(AX) = 0x01;
                   9825:                m_CF = 1;
                   9826:                break;
                   9827:        case 0x08: // set redirected printer mode
                   9828:        case 0x09: // flush redirected printer output
                   9829:        case 0x0a: // set extended error information
                   9830:                break;
                   9831:        default:
                   9832:                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));
                   9833:                REG16(AX) = 0x01;
                   9834:                m_CF = 1;
                   9835:                break;
                   9836:        }
                   9837: }
                   9838: 
1.1.1.30  root     9839: inline void msdos_int_21h_5fh()
                   9840: {
                   9841:        switch(REG8(AL)) {
                   9842:        case 0x02:
                   9843:                {
                   9844:                        DWORD drives = GetLogicalDrives();
                   9845:                        for(int i = 0, index = 0; i < 26; i++) {
                   9846:                                if(drives & (1 << i)) {
                   9847:                                        char volume[] = "A:\\";
                   9848:                                        volume[0] = 'A' + i;
                   9849:                                        if(GetDriveType(volume) == DRIVE_REMOTE) {
                   9850:                                                if(index == REG16(BX)) {
                   9851:                                                        DWORD dwSize = 128;
                   9852:                                                        volume[2] = '\0';
                   9853:                                                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(SI)), volume);
                   9854:                                                        WNetGetConnection(volume, (char *)(mem + SREG_BASE(ES) + REG16(DI)), &dwSize);
                   9855:                                                        REG8(BH) = 0x00; // valid
                   9856:                                                        REG8(BL) = 0x04; // disk drive
                   9857:                                                        REG16(CX) = 0x00;
                   9858:                                                        return;
                   9859:                                                }
                   9860:                                                index++;
                   9861:                                        }
                   9862:                                }
                   9863:                        }
                   9864:                }
                   9865:                REG16(AX) = 0x12; // no more files
                   9866:                m_CF = 1;
                   9867:                break;
                   9868:        default:
                   9869:                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));
                   9870:                REG16(AX) = 0x01;
                   9871:                m_CF = 1;
                   9872:                break;
                   9873:        }
                   9874: }
                   9875: 
1.1       root     9876: inline void msdos_int_21h_60h(int lfn)
                   9877: {
1.1.1.14  root     9878:        char full[MAX_PATH], *path;
                   9879:        
1.1       root     9880:        if(lfn) {
1.1.1.14  root     9881:                char *name;
                   9882:                *full = '\0';
1.1.1.3   root     9883:                GetFullPathName((char *)(mem + SREG_BASE(DS) + REG16(SI)), MAX_PATH, full, &name);
1.1.1.14  root     9884:                switch(REG8(CL)) {
                   9885:                case 1:
                   9886:                        GetShortPathName(full, full, MAX_PATH);
                   9887:                        my_strupr(full);
                   9888:                        break;
                   9889:                case 2:
                   9890:                        GetLongPathName(full, full, MAX_PATH);
                   9891:                        break;
                   9892:                }
                   9893:                path = full;
                   9894:        } else {
                   9895:                path = msdos_short_full_path((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   9896:        }
                   9897:        if(*path != '\0') {
                   9898:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
1.1       root     9899:        } else {
1.1.1.14  root     9900:                REG16(AX) = (UINT16)GetLastError();
                   9901:                m_CF = 1;
1.1       root     9902:        }
                   9903: }
                   9904: 
                   9905: inline void msdos_int_21h_61h()
                   9906: {
                   9907:        REG8(AL) = 0;
                   9908: }
                   9909: 
                   9910: inline void msdos_int_21h_62h()
                   9911: {
                   9912:        REG16(BX) = current_psp;
                   9913: }
                   9914: 
                   9915: inline void msdos_int_21h_63h()
                   9916: {
                   9917:        switch(REG8(AL)) {
                   9918:        case 0x00:
1.1.1.3   root     9919:                SREG(DS) = (DBCS_TABLE >> 4);
                   9920:                i386_load_segment_descriptor(DS);
1.1       root     9921:                REG16(SI) = (DBCS_TABLE & 0x0f);
                   9922:                REG8(AL) = 0x00;
                   9923:                break;
1.1.1.22  root     9924:        case 0x01: // set korean input mode
                   9925:        case 0x02: // get korean input mode
                   9926:                REG8(AL) = 0xff; // not supported
                   9927:                break;
1.1       root     9928:        default:
1.1.1.22  root     9929:                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     9930:                REG16(AX) = 0x01;
1.1.1.3   root     9931:                m_CF = 1;
1.1       root     9932:                break;
                   9933:        }
                   9934: }
                   9935: 
1.1.1.25  root     9936: UINT16 get_extended_country_info(UINT8 func)
1.1       root     9937: {
1.1.1.25  root     9938:        switch(func) {
1.1.1.17  root     9939:        case 0x01:
                   9940:                if(REG16(CX) >= 5) {
1.1.1.19  root     9941:                        UINT8 data[1 + 2 + 2 + 2 + sizeof(country_info_t)];
1.1.1.17  root     9942:                        if(REG16(CX) > sizeof(data))            // cx = actual transfer size
                   9943:                                REG16(CX) = sizeof(data);
                   9944:                        ZeroMemory(data, sizeof(data));
                   9945:                        data[0] = 0x01;
                   9946:                        *(UINT16 *)(data + 1) = REG16(CX) - 3;
1.1.1.19  root     9947:                        *(UINT16 *)(data + 3) = get_country_info((country_info_t*)(data + 7));
1.1.1.17  root     9948:                        *(UINT16 *)(data + 5) = active_code_page;
                   9949:                        memcpy(mem + SREG_BASE(ES) + REG16(DI), data, REG16(CX));
1.1.1.25  root     9950: //                     REG16(AX) = active_code_page;
1.1.1.17  root     9951:                } else {
1.1.1.25  root     9952:                        return(0x08); // insufficient memory
1.1.1.17  root     9953:                }
                   9954:                break;
                   9955:        case 0x02:
                   9956:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   9957:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (UPPERTABLE_TOP & 0x0f);
                   9958:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (UPPERTABLE_TOP >> 4);
1.1.1.25  root     9959: //             REG16(AX) = active_code_page;
1.1.1.17  root     9960:                REG16(CX) = 0x05;
                   9961:                break;
1.1.1.23  root     9962:        case 0x03:
                   9963:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x02;
                   9964:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (LOWERTABLE_TOP & 0x0f);
                   9965:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (LOWERTABLE_TOP >> 4);
1.1.1.25  root     9966: //             REG16(AX) = active_code_page;
1.1.1.23  root     9967:                REG16(CX) = 0x05;
                   9968:                break;
1.1.1.17  root     9969:        case 0x04:
                   9970:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x04;
                   9971:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_UPPERTABLE_TOP & 0x0f);
                   9972:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_UPPERTABLE_TOP >> 4);
1.1.1.25  root     9973: //             REG16(AX) = active_code_page;
1.1.1.17  root     9974:                REG16(CX) = 0x05;
                   9975:                break;
                   9976:        case 0x05:
                   9977:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x05;
                   9978:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (FILENAME_TERMINATOR_TOP & 0x0f);
                   9979:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (FILENAME_TERMINATOR_TOP >> 4);
1.1.1.25  root     9980: //             REG16(AX) = active_code_page;
1.1.1.17  root     9981:                REG16(CX) = 0x05;
                   9982:                break;
                   9983:        case 0x06:
                   9984:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x06;
                   9985:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (COLLATING_TABLE_TOP & 0x0f);
                   9986:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (COLLATING_TABLE_TOP >> 4);
1.1.1.25  root     9987: //             REG16(AX) = active_code_page;
1.1.1.17  root     9988:                REG16(CX) = 0x05;
                   9989:                break;
1.1       root     9990:        case 0x07:
1.1.1.3   root     9991:                *(UINT8  *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 0x07;
                   9992:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 1) = (DBCS_TOP & 0x0f);
                   9993:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 3) = (DBCS_TOP >> 4);
1.1.1.25  root     9994: //             REG16(AX) = active_code_page;
1.1       root     9995:                REG16(CX) = 0x05;
                   9996:                break;
1.1.1.25  root     9997:        default:
                   9998:                return(0x01); // function number invalid
                   9999:        }
                   10000:        return(0x00);
                   10001: }
                   10002: 
                   10003: inline void msdos_int_21h_65h()
                   10004: {
                   10005:        char tmp[0x10000];
                   10006:        
                   10007:        switch(REG8(AL)) {
                   10008:        case 0x01:
                   10009:        case 0x02:
                   10010:        case 0x03:
                   10011:        case 0x04:
                   10012:        case 0x05:
                   10013:        case 0x06:
                   10014:        case 0x07:
                   10015:                {
                   10016:                        UINT16 result = get_extended_country_info(REG8(AL));
                   10017:                        if(result) {
                   10018:                                REG16(AX) = result;
                   10019:                                m_CF = 1;
                   10020:                        } else {
                   10021:                                REG16(AX) = active_code_page; // FIXME: is this correct???
                   10022:                        }
                   10023:                }
                   10024:                break;
1.1       root     10025:        case 0x20:
1.1.1.25  root     10026:        case 0xa0:
1.1.1.19  root     10027:                memset(tmp, 0, sizeof(tmp));
                   10028:                tmp[0] = REG8(DL);
1.1       root     10029:                my_strupr(tmp);
                   10030:                REG8(DL) = tmp[0];
                   10031:                break;
                   10032:        case 0x21:
1.1.1.25  root     10033:        case 0xa1:
1.1       root     10034:                memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     10035:                memcpy(tmp, mem + SREG_BASE(DS) + REG16(DX), REG16(CX));
1.1       root     10036:                my_strupr(tmp);
1.1.1.3   root     10037:                memcpy(mem + SREG_BASE(DS) + REG16(DX), tmp, REG16(CX));
1.1       root     10038:                break;
                   10039:        case 0x22:
1.1.1.25  root     10040:        case 0xa2:
1.1.1.3   root     10041:                my_strupr((char *)(mem + SREG_BASE(DS) + REG16(DX)));
1.1       root     10042:                break;
1.1.1.25  root     10043:        case 0x23:
                   10044:                // FIXME: need to check multi-byte (kanji) charactre?
                   10045:                if(REG8(DL) == 'N' || REG8(DL) == 'n' || (REG8(DL) == 0x82 && REG8(DH) == 0x78) || (REG8(DL) == 0x82 && REG8(DH) == 0x99)) {
                   10046:                        // 8278h/8299h: multi-byte (kanji) Y and y
                   10047:                        REG16(AX) = 0x00;
                   10048:                } else if(REG8(DL) == 'Y' || REG8(DL) == 'y' || (REG8(DL) == 0x82 && REG8(DH) == 0x6d) || (REG8(DL) == 0x82 && REG8(DH) == 0x8e)) {
                   10049:                        // 826dh/828eh: multi-byte (kanji) N and n
                   10050:                        REG16(AX) = 0x01;
                   10051:                } else {
                   10052:                        REG16(AX) = 0x02;
                   10053:                }
                   10054:                break;
1.1       root     10055:        default:
1.1.1.22  root     10056:                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     10057:                REG16(AX) = 0x01;
1.1.1.3   root     10058:                m_CF = 1;
1.1       root     10059:                break;
                   10060:        }
                   10061: }
                   10062: 
                   10063: inline void msdos_int_21h_66h()
                   10064: {
                   10065:        switch(REG8(AL)) {
                   10066:        case 0x01:
                   10067:                REG16(BX) = active_code_page;
                   10068:                REG16(DX) = system_code_page;
                   10069:                break;
                   10070:        case 0x02:
                   10071:                if(active_code_page == REG16(BX)) {
                   10072:                        REG16(AX) = 0xeb41;
                   10073:                } else if(_setmbcp(REG16(BX)) == 0) {
                   10074:                        active_code_page = REG16(BX);
1.1.1.17  root     10075:                        msdos_nls_tables_update();
1.1       root     10076:                        REG16(AX) = 0xeb41;
1.1.1.32  root     10077:                        SetConsoleCP(active_code_page);
                   10078:                        SetConsoleOutputCP(active_code_page);
1.1       root     10079:                } else {
                   10080:                        REG16(AX) = 0x25;
1.1.1.3   root     10081:                        m_CF = 1;
1.1       root     10082:                }
                   10083:                break;
                   10084:        default:
1.1.1.22  root     10085:                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     10086:                REG16(AX) = 0x01;
1.1.1.3   root     10087:                m_CF = 1;
1.1       root     10088:                break;
                   10089:        }
                   10090: }
                   10091: 
                   10092: inline void msdos_int_21h_67h()
                   10093: {
                   10094:        process_t *process = msdos_process_info_get(current_psp);
                   10095:        
                   10096:        if(REG16(BX) <= MAX_FILES) {
                   10097:                process->max_files = max(REG16(BX), 20);
                   10098:        } else {
                   10099:                REG16(AX) = 0x08;
1.1.1.3   root     10100:                m_CF = 1;
1.1       root     10101:        }
                   10102: }
                   10103: 
                   10104: inline void msdos_int_21h_68h()
                   10105: {
                   10106:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10107:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
1.1       root     10108:        
1.1.1.20  root     10109:        if(fd < process->max_files && file_handler[fd].valid) {
                   10110:                // fflush(_fdopen(fd, ""));
1.1       root     10111:        } else {
                   10112:                REG16(AX) = 0x06;
1.1.1.3   root     10113:                m_CF = 1;
1.1       root     10114:        }
                   10115: }
                   10116: 
                   10117: inline void msdos_int_21h_69h()
                   10118: {
1.1.1.3   root     10119:        drive_info_t *info = (drive_info_t *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     10120:        char path[] = "A:\\";
                   10121:        char volume_label[MAX_PATH];
                   10122:        DWORD serial_number = 0;
                   10123:        char file_system[MAX_PATH];
                   10124:        
                   10125:        if(REG8(BL) == 0) {
                   10126:                path[0] = 'A' + _getdrive() - 1;
                   10127:        } else {
                   10128:                path[0] = 'A' + REG8(BL) - 1;
                   10129:        }
                   10130:        
                   10131:        switch(REG8(AL)) {
                   10132:        case 0x00:
                   10133:                if(GetVolumeInformation(path, volume_label, MAX_PATH, &serial_number, NULL, NULL, file_system, MAX_PATH)) {
                   10134:                        info->info_level = 0;
                   10135:                        info->serial_number = serial_number;
                   10136:                        memset(info->volume_label, 0x20, 11);
                   10137:                        memcpy(info->volume_label, volume_label, min(strlen(volume_label), 11));
                   10138:                        memset(info->file_system, 0x20, 8);
                   10139:                        memcpy(info->file_system, file_system, min(strlen(file_system), 8));
                   10140:                } else {
                   10141:                        REG16(AX) = errno;
1.1.1.3   root     10142:                        m_CF = 1;
1.1       root     10143:                }
                   10144:                break;
                   10145:        case 0x01:
                   10146:                REG16(AX) = 0x03;
1.1.1.3   root     10147:                m_CF = 1;
1.1       root     10148:        }
                   10149: }
                   10150: 
                   10151: inline void msdos_int_21h_6ah()
                   10152: {
                   10153:        REG8(AH) = 0x68;
                   10154:        msdos_int_21h_68h();
                   10155: }
                   10156: 
                   10157: inline void msdos_int_21h_6bh()
                   10158: {
                   10159:        REG8(AL) = 0;
                   10160: }
                   10161: 
                   10162: inline void msdos_int_21h_6ch(int lfn)
                   10163: {
1.1.1.3   root     10164:        char *path = msdos_local_file_path((char *)(mem + SREG_BASE(DS) + REG16(SI)), lfn);
1.1       root     10165:        int mode = REG8(BL) & 0x03;
                   10166:        
                   10167:        if(mode < 0x03) {
1.1.1.29  root     10168:                if(msdos_is_device_path(path) || msdos_is_existing_file(path)) {
1.1       root     10169:                        // file exists
                   10170:                        if(REG8(DL) & 1) {
1.1.1.29  root     10171:                                int fd = -1, c;
1.1.1.11  root     10172:                                UINT16 info;
1.1       root     10173:                                
1.1.1.11  root     10174:                                if(msdos_is_con_path(path)) {
1.1.1.13  root     10175:                                        fd = msdos_open("CON", file_mode[mode].mode);
1.1.1.11  root     10176:                                        info = 0x80d3;
1.1.1.29  root     10177:                                } else if((c = msdos_is_comm_path(path)) != 0 && sio_port_number[c - 1] != 0) {
                   10178:                                        if((fd = _open(msdos_create_comm_path(path, sio_port_number[c - 1]), file_mode[mode].mode)) == -1) {
                   10179:                                                fd = msdos_open("NUL", file_mode[mode].mode);
                   10180:                                        }
1.1.1.14  root     10181:                                        info = 0x80d3;
1.1.1.29  root     10182:                                } else if(msdos_is_device_path(path)) {
1.1.1.20  root     10183:                                        fd = msdos_open("NUL", file_mode[mode].mode);
                   10184:                                        info = 0x80d3;
1.1.1.11  root     10185:                                } else {
1.1.1.13  root     10186:                                        fd = msdos_open(path, file_mode[mode].mode);
1.1.1.11  root     10187:                                        info = msdos_drive_number(path);
                   10188:                                }
1.1       root     10189:                                if(fd != -1) {
                   10190:                                        REG16(AX) = fd;
                   10191:                                        REG16(CX) = 1;
1.1.1.11  root     10192:                                        msdos_file_handler_open(fd, path, _isatty(fd), mode, info, current_psp);
1.1.1.20  root     10193:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     10194:                                } else {
                   10195:                                        REG16(AX) = errno;
1.1.1.3   root     10196:                                        m_CF = 1;
1.1       root     10197:                                }
                   10198:                        } else if(REG8(DL) & 2) {
                   10199:                                int attr = GetFileAttributes(path);
1.1.1.29  root     10200:                                int fd = -1, c;
1.1.1.11  root     10201:                                UINT16 info;
1.1       root     10202:                                
1.1.1.11  root     10203:                                if(msdos_is_con_path(path)) {
1.1.1.13  root     10204:                                        fd = msdos_open("CON", file_mode[mode].mode);
1.1.1.11  root     10205:                                        info = 0x80d3;
1.1.1.29  root     10206:                                } else if((c = msdos_is_comm_path(path)) != 0 && sio_port_number[c - 1] != 0) {
                   10207:                                        if((fd = _open(msdos_create_comm_path(path, sio_port_number[c - 1]), file_mode[mode].mode)) == -1) {
                   10208:                                                fd = msdos_open("NUL", file_mode[mode].mode);
                   10209:                                        }
1.1.1.14  root     10210:                                        info = 0x80d3;
1.1.1.29  root     10211:                                } else if(msdos_is_device_path(path)) {
1.1.1.20  root     10212:                                        fd = msdos_open("NUL", file_mode[mode].mode);
                   10213:                                        info = 0x80d3;
1.1       root     10214:                                } else {
                   10215:                                        fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
1.1.1.11  root     10216:                                        info = msdos_drive_number(path);
1.1       root     10217:                                }
                   10218:                                if(fd != -1) {
                   10219:                                        if(attr == -1) {
                   10220:                                                attr = msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY;
                   10221:                                        }
                   10222:                                        SetFileAttributes(path, attr);
                   10223:                                        REG16(AX) = fd;
                   10224:                                        REG16(CX) = 3;
1.1.1.11  root     10225:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, info, current_psp);
1.1.1.20  root     10226:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     10227:                                } else {
                   10228:                                        REG16(AX) = errno;
1.1.1.3   root     10229:                                        m_CF = 1;
1.1       root     10230:                                }
                   10231:                        } else {
                   10232:                                REG16(AX) = 0x50;
1.1.1.3   root     10233:                                m_CF = 1;
1.1       root     10234:                        }
                   10235:                } else {
                   10236:                        // file not exists
                   10237:                        if(REG8(DL) & 0x10) {
                   10238:                                int fd = _open(path, _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE);
                   10239:                                
                   10240:                                if(fd != -1) {
                   10241:                                        SetFileAttributes(path, msdos_file_attribute_create(REG16(CX)) & ~FILE_ATTRIBUTE_READONLY);
                   10242:                                        REG16(AX) = fd;
                   10243:                                        REG16(CX) = 2;
                   10244:                                        msdos_file_handler_open(fd, path, _isatty(fd), 2, msdos_drive_number(path), current_psp);
1.1.1.20  root     10245:                                        msdos_psp_set_file_table(fd, fd, current_psp);
1.1       root     10246:                                } else {
                   10247:                                        REG16(AX) = errno;
1.1.1.3   root     10248:                                        m_CF = 1;
1.1       root     10249:                                }
                   10250:                        } else {
                   10251:                                REG16(AX) = 0x02;
1.1.1.3   root     10252:                                m_CF = 1;
1.1       root     10253:                        }
                   10254:                }
                   10255:        } else {
                   10256:                REG16(AX) = 0x0c;
1.1.1.3   root     10257:                m_CF = 1;
1.1       root     10258:        }
                   10259: }
                   10260: 
                   10261: inline void msdos_int_21h_710dh()
                   10262: {
                   10263:        // reset drive
                   10264: }
                   10265: 
1.1.1.17  root     10266: inline void msdos_int_21h_7141h(int lfn)
                   10267: {
                   10268:        if(REG16(SI) == 0) {
                   10269:                msdos_int_21h_41h(lfn);
                   10270:                return;
                   10271:        }
                   10272:        if(REG16(SI) != 1) {
                   10273:                REG16(AX) = 5;
                   10274:                m_CF = 1;
                   10275:        }
                   10276:        /* wild card and matching attributes... */
                   10277:        char tmp[MAX_PATH * 2];
                   10278:        // copy search pathname (and quick check overrun)
                   10279:        ZeroMemory(tmp, sizeof(tmp));
                   10280:        tmp[MAX_PATH - 1] = '\0';
                   10281:        tmp[MAX_PATH] = 1;
                   10282:        strncpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(DX)), MAX_PATH);
                   10283:        
                   10284:        if(tmp[MAX_PATH - 1] != '\0' || tmp[MAX_PATH] != 1) {
                   10285:                REG16(AX) = 1;
                   10286:                m_CF = 1;
                   10287:                return;
                   10288:        }
                   10289:        for(char *s = tmp; *s; ++s) {
                   10290:                if(*s == '/') {
                   10291:                        *s = '\\';
                   10292:                }
                   10293:        }
                   10294:        char *tmp_name = (char *)_mbsrchr((unsigned char *)tmp, '\\');
                   10295:        if(tmp_name) {
                   10296:                ++tmp_name;
                   10297:        } else {
                   10298:                tmp_name = strchr(tmp, ':');
                   10299:                tmp_name = tmp_name ? tmp_name + 1 : tmp;
                   10300:        }
                   10301:        
                   10302:        WIN32_FIND_DATAA fd;
                   10303:        HANDLE fh = FindFirstFileA(tmp, &fd);
                   10304:        if(fh == INVALID_HANDLE_VALUE) {
                   10305:                REG16(AX) = 2;
                   10306:                m_CF = 1;
                   10307:                return;
                   10308:        }
                   10309:        do {
                   10310:                if(msdos_find_file_check_attribute(fd.dwFileAttributes, REG8(CL), REG8(CH)) && msdos_find_file_has_8dot3name(&fd)) {
                   10311:                        strcpy(tmp_name, fd.cFileName);
                   10312:                        if(remove(msdos_trimmed_path(tmp, lfn))) {
                   10313:                                REG16(AX) = 5;
                   10314:                                m_CF = 1;
                   10315:                                break;
                   10316:                        }
                   10317:                }
                   10318:        } while(FindNextFileA(fh, &fd));
                   10319:        if(!m_CF) {
                   10320:                if(GetLastError() != ERROR_NO_MORE_FILES) {
                   10321:                        m_CF = 1;
                   10322:                        REG16(AX) = 2;
                   10323:                }
                   10324:        }
                   10325:        FindClose(fh);
                   10326: }
                   10327: 
1.1       root     10328: inline void msdos_int_21h_714eh()
                   10329: {
                   10330:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     10331:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
                   10332:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     10333:        WIN32_FIND_DATA fd;
                   10334:        
1.1.1.13  root     10335:        dtainfo_t *dtainfo = msdos_dta_info_get(current_psp, LFN_DTA_LADDR);
                   10336:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   10337:                FindClose(dtainfo->find_handle);
                   10338:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     10339:        }
                   10340:        strcpy(process->volume_label, msdos_volume_label(path));
1.1.1.14  root     10341:        dtainfo->allowable_mask = REG8(CL);
                   10342:        dtainfo->required_mask = REG8(CH);
                   10343:        bool label_only = (dtainfo->allowable_mask == 8);
1.1       root     10344:        
1.1.1.14  root     10345:        if((dtainfo->allowable_mask & 8) && !msdos_match_volume_label(path, msdos_short_volume_label(process->volume_label))) {
                   10346:                dtainfo->allowable_mask &= ~8;
1.1       root     10347:        }
1.1.1.14  root     10348:        if(!label_only && (dtainfo->find_handle = FindFirstFile(path, &fd)) != INVALID_HANDLE_VALUE) {
                   10349:                while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13  root     10350:                        if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   10351:                                FindClose(dtainfo->find_handle);
                   10352:                                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     10353:                                break;
                   10354:                        }
                   10355:                }
                   10356:        }
1.1.1.13  root     10357:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     10358:                find->attrib = fd.dwFileAttributes;
                   10359:                msdos_find_file_conv_local_time(&fd);
                   10360:                if(REG16(SI) == 0) {
                   10361:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   10362:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   10363:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   10364:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   10365:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   10366:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   10367:                } else {
                   10368:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   10369:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   10370:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   10371:                }
                   10372:                find->size_hi = fd.nFileSizeHigh;
                   10373:                find->size_lo = fd.nFileSizeLow;
                   10374:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     10375:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     10376:                REG16(AX) = dtainfo - dtalist + 1;
                   10377:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     10378:                // volume label
                   10379:                find->attrib = 8;
                   10380:                find->size_hi = find->size_lo = 0;
                   10381:                strcpy(find->full_name, process->volume_label);
                   10382:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     10383:                dtainfo->allowable_mask &= ~8;
                   10384:                REG16(AX) = dtainfo - dtalist + 1;
1.1       root     10385:        } else {
                   10386:                REG16(AX) = 0x12;       // NOTE: return 0x02 if file path is invalid
1.1.1.3   root     10387:                m_CF = 1;
1.1       root     10388:        }
                   10389: }
                   10390: 
                   10391: inline void msdos_int_21h_714fh()
                   10392: {
                   10393:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.3   root     10394:        find_lfn_t *find = (find_lfn_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     10395:        WIN32_FIND_DATA fd;
                   10396:        
1.1.1.14  root     10397:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   10398:                REG16(AX) = 6;
1.1.1.13  root     10399:                m_CF = 1;
                   10400:                return;
                   10401:        }
1.1.1.14  root     10402:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     10403:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   10404:                if(FindNextFile(dtainfo->find_handle, &fd)) {
1.1.1.14  root     10405:                        while(!msdos_find_file_check_attribute(fd.dwFileAttributes, dtainfo->allowable_mask, dtainfo->required_mask)) {
1.1.1.13  root     10406:                                if(!FindNextFile(dtainfo->find_handle, &fd)) {
                   10407:                                        FindClose(dtainfo->find_handle);
                   10408:                                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     10409:                                        break;
                   10410:                                }
                   10411:                        }
                   10412:                } else {
1.1.1.13  root     10413:                        FindClose(dtainfo->find_handle);
                   10414:                        dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     10415:                }
                   10416:        }
1.1.1.13  root     10417:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
1.1       root     10418:                find->attrib = fd.dwFileAttributes;
                   10419:                msdos_find_file_conv_local_time(&fd);
                   10420:                if(REG16(SI) == 0) {
                   10421:                        find->ctime_lo.dw = fd.ftCreationTime.dwLowDateTime;
                   10422:                        find->ctime_hi.dw = fd.ftCreationTime.dwHighDateTime;
                   10423:                        find->atime_lo.dw = fd.ftLastAccessTime.dwLowDateTime;
                   10424:                        find->atime_hi.dw = fd.ftLastAccessTime.dwHighDateTime;
                   10425:                        find->mtime_lo.dw = fd.ftLastWriteTime.dwLowDateTime;
                   10426:                        find->mtime_hi.dw = fd.ftLastWriteTime.dwHighDateTime;
                   10427:                } else {
                   10428:                        FileTimeToDosDateTime(&fd.ftCreationTime, &find->ctime_lo.w.h, &find->ctime_lo.w.l);
                   10429:                        FileTimeToDosDateTime(&fd.ftLastAccessTime, &find->atime_lo.w.h, &find->atime_lo.w.l);
                   10430:                        FileTimeToDosDateTime(&fd.ftLastWriteTime, &find->mtime_lo.w.h, &find->mtime_lo.w.l);
                   10431:                }
                   10432:                find->size_hi = fd.nFileSizeHigh;
                   10433:                find->size_lo = fd.nFileSizeLow;
                   10434:                strcpy(find->full_name, fd.cFileName);
1.1.1.13  root     10435:                strcpy(find->short_name, fd.cAlternateFileName);
1.1.1.14  root     10436:        } else if(dtainfo->allowable_mask & 8) {
1.1       root     10437:                // volume label
                   10438:                find->attrib = 8;
                   10439:                find->size_hi = find->size_lo = 0;
                   10440:                strcpy(find->full_name, process->volume_label);
                   10441:                strcpy(find->short_name, msdos_short_volume_label(process->volume_label));
1.1.1.14  root     10442:                dtainfo->allowable_mask &= ~8;
1.1       root     10443:        } else {
                   10444:                REG16(AX) = 0x12;
1.1.1.3   root     10445:                m_CF = 1;
1.1       root     10446:        }
                   10447: }
                   10448: 
                   10449: inline void msdos_int_21h_71a0h()
                   10450: {
                   10451:        DWORD max_component_len, file_sys_flag;
                   10452:        
1.1.1.14  root     10453:        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))) {
                   10454:                REG16(BX) = (UINT16)file_sys_flag & (FILE_CASE_SENSITIVE_SEARCH | FILE_CASE_PRESERVED_NAMES | FILE_UNICODE_ON_DISK | FILE_VOLUME_IS_COMPRESSED);
                   10455:                REG16(BX) |= 0x4000;                            // supports LFN functions
1.1       root     10456:                REG16(CX) = (UINT16)max_component_len;          // 255
                   10457:                REG16(DX) = (UINT16)max_component_len + 5;      // 260
                   10458:        } else {
                   10459:                REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     10460:                m_CF = 1;
1.1       root     10461:        }
                   10462: }
                   10463: 
                   10464: inline void msdos_int_21h_71a1h()
                   10465: {
1.1.1.14  root     10466:        if(REG16(BX) - 1u >= MAX_DTAINFO) {
                   10467:                REG16(AX) = 6;
1.1.1.13  root     10468:                m_CF = 1;
                   10469:                return;
                   10470:        }
1.1.1.14  root     10471:        dtainfo_t *dtainfo = &dtalist[REG16(BX) - 1];
1.1.1.13  root     10472:        if(dtainfo->find_handle != INVALID_HANDLE_VALUE) {
                   10473:                FindClose(dtainfo->find_handle);
                   10474:                dtainfo->find_handle = INVALID_HANDLE_VALUE;
1.1       root     10475:        }
                   10476: }
                   10477: 
                   10478: inline void msdos_int_21h_71a6h()
                   10479: {
                   10480:        process_t *process = msdos_process_info_get(current_psp);
1.1.1.20  root     10481:        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   10482:        
1.1.1.3   root     10483:        UINT8 *buffer = (UINT8 *)(mem + SREG_BASE(DS) + REG16(DX));
1.1       root     10484:        struct _stat64 status;
                   10485:        DWORD serial_number = 0;
                   10486:        
1.1.1.20  root     10487:        if(fd < process->max_files && file_handler[fd].valid) {
                   10488:                if(_fstat64(fd, &status) == 0) {
                   10489:                        if(file_handler[fd].path[1] == ':') {
1.1       root     10490:                                // NOTE: we need to consider the network file path "\\host\share\"
                   10491:                                char volume[] = "A:\\";
1.1.1.20  root     10492:                                volume[0] = file_handler[fd].path[1];
1.1       root     10493:                                GetVolumeInformation(volume, NULL, 0, &serial_number, NULL, NULL, NULL, 0);
                   10494:                        }
1.1.1.20  root     10495:                        *(UINT32 *)(buffer + 0x00) = GetFileAttributes(file_handler[fd].path);
1.1       root     10496:                        *(UINT32 *)(buffer + 0x04) = (UINT32)(status.st_ctime & 0xffffffff);
                   10497:                        *(UINT32 *)(buffer + 0x08) = (UINT32)((status.st_ctime >> 32) & 0xffffffff);
                   10498:                        *(UINT32 *)(buffer + 0x0c) = (UINT32)(status.st_atime & 0xffffffff);
                   10499:                        *(UINT32 *)(buffer + 0x10) = (UINT32)((status.st_atime >> 32) & 0xffffffff);
                   10500:                        *(UINT32 *)(buffer + 0x14) = (UINT32)(status.st_mtime & 0xffffffff);
                   10501:                        *(UINT32 *)(buffer + 0x18) = (UINT32)((status.st_mtime >> 32) & 0xffffffff);
                   10502:                        *(UINT32 *)(buffer + 0x1c) = serial_number;
                   10503:                        *(UINT32 *)(buffer + 0x20) = (UINT32)((status.st_size >> 32) & 0xffffffff);
                   10504:                        *(UINT32 *)(buffer + 0x24) = (UINT32)(status.st_size & 0xffffffff);
                   10505:                        *(UINT32 *)(buffer + 0x28) = status.st_nlink;
1.1.1.14  root     10506:                        // this is dummy id and it will be changed when it is reopened...
1.1       root     10507:                        *(UINT32 *)(buffer + 0x2c) = 0;
1.1.1.20  root     10508:                        *(UINT32 *)(buffer + 0x30) = file_handler[fd].id;
1.1       root     10509:                } else {
                   10510:                        REG16(AX) = errno;
1.1.1.3   root     10511:                        m_CF = 1;
1.1       root     10512:                }
                   10513:        } else {
                   10514:                REG16(AX) = 0x06;
1.1.1.3   root     10515:                m_CF = 1;
1.1       root     10516:        }
                   10517: }
                   10518: 
                   10519: inline void msdos_int_21h_71a7h()
                   10520: {
                   10521:        switch(REG8(BL)) {
                   10522:        case 0x00:
1.1.1.3   root     10523:                if(!FileTimeToDosDateTime((FILETIME *)(mem + SREG_BASE(DS) + REG16(SI)), &REG16(DX), &REG16(CX))) {
1.1       root     10524:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     10525:                        m_CF = 1;
1.1       root     10526:                }
                   10527:                break;
                   10528:        case 0x01:
                   10529:                // NOTE: we need to check BH that shows 10-millisecond untils past time in CX
1.1.1.3   root     10530:                if(!DosDateTimeToFileTime(REG16(DX), REG16(CX), (FILETIME *)(mem + SREG_BASE(ES) + REG16(DI)))) {
1.1       root     10531:                        REG16(AX) = (UINT16)GetLastError();
1.1.1.3   root     10532:                        m_CF = 1;
1.1       root     10533:                }
                   10534:                break;
                   10535:        default:
1.1.1.22  root     10536:                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     10537:                REG16(AX) = 0x01;
1.1.1.3   root     10538:                m_CF = 1;
1.1       root     10539:                break;
                   10540:        }
                   10541: }
                   10542: 
                   10543: inline void msdos_int_21h_71a8h()
                   10544: {
                   10545:        if(REG8(DH) == 0) {
                   10546:                char tmp[MAX_PATH], fcb[MAX_PATH];
1.1.1.3   root     10547:                strcpy(tmp, msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     10548:                memset(fcb, 0x20, sizeof(fcb));
                   10549:                int len = strlen(tmp);
1.1.1.21  root     10550:                for(int i = 0, pos = 0; i < len; i++) {
1.1       root     10551:                        if(tmp[i] == '.') {
                   10552:                                pos = 8;
                   10553:                        } else {
                   10554:                                if(msdos_lead_byte_check(tmp[i])) {
                   10555:                                        fcb[pos++] = tmp[i++];
                   10556:                                }
                   10557:                                fcb[pos++] = tmp[i];
                   10558:                        }
                   10559:                }
1.1.1.3   root     10560:                memcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), fcb, 11);
1.1       root     10561:        } else {
1.1.1.3   root     10562:                strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), msdos_short_path((char *)(mem + SREG_BASE(DS) + REG16(SI))));
1.1       root     10563:        }
                   10564: }
                   10565: 
1.1.1.22  root     10566: inline void msdos_int_21h_71aah()
                   10567: {
                   10568:        char drv[] = "A:", path[MAX_PATH];
                   10569:        char *hoge=(char *)(mem + SREG_BASE(DS) + REG16(DX));
                   10570:        
                   10571:        if(REG8(BL) == 0) {
                   10572:                drv[0] = 'A' + _getdrive() - 1;
                   10573:        } else {
                   10574:                drv[0] = 'A' + REG8(BL) - 1;
                   10575:        }
                   10576:        switch(REG8(BH)) {
                   10577:        case 0x00:
                   10578:                if(DefineDosDevice(0, drv, (char *)(mem + SREG_BASE(DS) + REG16(DX))) == 0) {
                   10579:                        DWORD bits = 1 << ((REG8(BL) ? REG8(BL) : _getdrive()) - 1);
                   10580:                        if(GetLogicalDrives() & bits) {
                   10581:                                REG16(AX) = 0x0f; // invalid drive
                   10582:                        } else {
                   10583:                                REG16(AX) = 0x03; // path not found
                   10584:                        }
                   10585:                        m_CF = 1;
                   10586:                }
                   10587:                break;
                   10588:        case 0x01:
                   10589:                if(DefineDosDevice(DDD_REMOVE_DEFINITION, drv, NULL) == 0) {
                   10590:                        REG16(AX) = 0x0f; // invalid drive
                   10591:                        m_CF = 1;
                   10592:                }
                   10593:                break;
                   10594:        case 0x02:
                   10595:                if(QueryDosDevice(drv, path, MAX_PATH) == 0) {
                   10596:                        REG16(AX) = 0x0f; // invalid drive
                   10597:                        m_CF = 1;
                   10598:                } else if(strncmp(path, "\\??\\", 4) != 0) {
                   10599:                        REG16(AX) = 0x0f; // invalid drive
                   10600:                        m_CF = 1;
                   10601:                } else {
                   10602:                        strcpy((char *)(mem + SREG_BASE(DS) + REG16(DX)), path + 4);
                   10603:                }
                   10604:                break;
                   10605:        default:
                   10606:                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));
                   10607:                REG16(AX) = 0x01;
                   10608:                m_CF = 1;
                   10609:                break;
                   10610:        }
                   10611: }
                   10612: 
1.1.1.14  root     10613: inline void msdos_int_21h_7300h()
                   10614: {
                   10615:        if(REG8(AL) == 0) {
                   10616:                REG8(AL) = REG8(CL);
                   10617:                REG8(AH) = 0;
                   10618:        } else {
                   10619:                REG16(AX) = 0x01;
                   10620:                m_CF = 1;
                   10621:        }
                   10622: }
                   10623: 
                   10624: inline void msdos_int_21h_7302h()
                   10625: {
                   10626:        int drive_num = (REG8(DL) == 0) ? (_getdrive() - 1) : (REG8(DL) - 1);
                   10627:        UINT16 seg, ofs;
                   10628:        
                   10629:        if(REG16(CX) < 0x3f) {
                   10630:                REG8(AL) = 0x18;
                   10631:                m_CF = 1;
                   10632:        } else if(!msdos_drive_param_block_update(drive_num, &seg, &ofs, 1)) {
                   10633:                REG8(AL) = 0xff;
                   10634:                m_CF = 1;
                   10635:        } else {
                   10636:                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 2, mem + (seg << 4) + ofs, sizeof(dpb_t));
                   10637:        }
                   10638: }
                   10639: 
1.1       root     10640: inline void msdos_int_21h_7303h()
                   10641: {
1.1.1.3   root     10642:        char *path = (char *)(mem + SREG_BASE(DS) + REG16(DX));
                   10643:        ext_space_info_t *info = (ext_space_info_t *)(mem + SREG_BASE(ES) + REG16(DI));
1.1       root     10644:        DWORD sectors_per_cluster, bytes_per_sector, free_clusters, total_clusters;
                   10645:        
                   10646:        if(GetDiskFreeSpace(path, &sectors_per_cluster, &bytes_per_sector, &free_clusters, &total_clusters)) {
                   10647:                info->size_of_structure = sizeof(ext_space_info_t);
                   10648:                info->structure_version = 0;
                   10649:                info->sectors_per_cluster = sectors_per_cluster;
                   10650:                info->bytes_per_sector = bytes_per_sector;
                   10651:                info->available_clusters_on_drive = free_clusters;
                   10652:                info->total_clusters_on_drive = total_clusters;
                   10653:                info->available_sectors_on_drive = sectors_per_cluster * free_clusters;
                   10654:                info->total_sectors_on_drive = sectors_per_cluster * total_clusters;
                   10655:                info->available_allocation_units = free_clusters;       // ???
                   10656:                info->total_allocation_units = total_clusters;          // ???
                   10657:        } else {
                   10658:                REG16(AX) = errno;
1.1.1.3   root     10659:                m_CF = 1;
1.1       root     10660:        }
                   10661: }
                   10662: 
1.1.1.30  root     10663: inline void msdos_int_21h_dbh()
                   10664: {
                   10665:        // Novell NetWare - Workstation - Get Number of Local Drives
                   10666:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   10667:        REG8(AL) = dos_info->last_drive;
                   10668: }
                   10669: 
                   10670: inline void msdos_int_21h_dch()
                   10671: {
                   10672:        // Novell NetWare - Connection Services - Get Connection Number
                   10673:        REG8(AL) = 0x00;
                   10674: }
                   10675: 
1.1.1.32  root     10676: inline void msdos_int_24h()
                   10677: {
                   10678:        const char *message = NULL;
                   10679:        int key = 0;
                   10680:        
                   10681:        for(int i = 0; i < array_length(critical_error_table); i++) {
                   10682:                if(critical_error_table[i].code == (REG16(DI) & 0xff) || critical_error_table[i].code == (UINT16)-1) {
                   10683:                        if(active_code_page == 932) {
                   10684:                                message = critical_error_table[i].message_japanese;
                   10685:                        }
                   10686:                        if(message == NULL) {
                   10687:                                message = critical_error_table[i].message_english;
                   10688:                        }
                   10689:                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   10690:                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   10691:                        
                   10692:                        SREG(ES) = WORK_TOP >> 4;
                   10693:                        i386_load_segment_descriptor(ES);
                   10694:                        REG16(DI) = 0x0000;
                   10695:                        break;
                   10696:                }
                   10697:        }
                   10698:        fprintf(stderr, "\n%s", message);
                   10699:        if(!(REG8(AH) & 0x80)) {
                   10700:                if(REG8(AH) & 0x01) {
                   10701:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�������ݒ� �h���C�u" : "writing drive", 'A' + REG8(AL));
                   10702:                } else {
                   10703:                        fprintf(stderr, " %s %c", (active_code_page == 932) ? "�ǂݎ�蒆 �h���C�u" : "reading drive", 'A' + REG8(AL));
                   10704:                }
                   10705:        }
                   10706:        fprintf(stderr, "\n");
                   10707:        
1.1.1.33  root     10708:        {
1.1.1.32  root     10709:                fprintf(stderr, "%s",   (active_code_page == 932) ? "���~ (A)" : "Abort");
1.1.1.33  root     10710:        }
1.1.1.32  root     10711:        if(REG8(AH) & 0x10) {
                   10712:                fprintf(stderr, ", %s", (active_code_page == 932) ? "�Ď��s (R)" : "Retry");
                   10713:        }
                   10714:        if(REG8(AH) & 0x20) {
                   10715:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���� (I)" : "Ignore");
                   10716:        }
                   10717:        if(REG8(AH) & 0x08) {
                   10718:                fprintf(stderr, ", %s", (active_code_page == 932) ? "���s (F)" : "Fail");
                   10719:        }
                   10720:        fprintf(stderr, "? ");
                   10721:        
                   10722:        while(1) {
                   10723:                while(!_kbhit()) {
                   10724:                        Sleep(10);
                   10725:                }
                   10726:                key = _getch();
                   10727:                
                   10728:                if(key == 'I' || key == 'i') {
                   10729:                        if(REG8(AH) & 0x20) {
                   10730:                                REG8(AL) = 0;
                   10731:                                break;
                   10732:                        }
                   10733:                } else if(key == 'R' || key == 'r') {
                   10734:                        if(REG8(AH) & 0x10) {
                   10735:                                REG8(AL) = 1;
                   10736:                                break;
                   10737:                        }
                   10738:                } else if(key == 'A' || key == 'a') {
                   10739:                        REG8(AL) = 2;
                   10740:                        break;
                   10741:                } else if(key == 'F' || key == 'f') {
                   10742:                        if(REG8(AH) & 0x08) {
                   10743:                                REG8(AL) = 3;
                   10744:                                break;
                   10745:                        }
                   10746:                }
                   10747:        }
                   10748:        fprintf(stderr, "%c\n", key);
                   10749: }
                   10750: 
1.1       root     10751: inline void msdos_int_25h()
                   10752: {
                   10753:        UINT16 seg, ofs;
                   10754:        DWORD dwSize;
                   10755:        
1.1.1.3   root     10756: #if defined(HAS_I386)
                   10757:        I386OP(pushf)();
                   10758: #else
                   10759:        PREFIX86(_pushf());
                   10760: #endif
1.1       root     10761:        
                   10762:        if(!(REG8(AL) < 26)) {
                   10763:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     10764:                m_CF = 1;
1.1       root     10765:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   10766:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     10767:                m_CF = 1;
1.1       root     10768:        } else {
                   10769:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   10770:                char dev[64];
                   10771:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   10772:                
                   10773:                HANDLE hFile = CreateFile(dev, GENERIC_READ, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   10774:                if(hFile == INVALID_HANDLE_VALUE) {
                   10775:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     10776:                        m_CF = 1;
1.1       root     10777:                } else {
1.1.1.19  root     10778:                        UINT32 top_sector  = REG16(DX);
                   10779:                        UINT16 sector_num  = REG16(CX);
                   10780:                        UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   10781:                        
                   10782:                        if(sector_num == 0xffff) {
                   10783:                                top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   10784:                                sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   10785:                                UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   10786:                                UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   10787:                                buffer_addr = (seg << 4) + ofs;
                   10788:                        }
                   10789: //                     if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   10790: //                             REG8(AL) = 0x02; // drive not ready
                   10791: //                             m_CF = 1;
                   10792: //                     } else 
                   10793:                        if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     10794:                                REG8(AL) = 0x08; // sector not found
1.1.1.3   root     10795:                                m_CF = 1;
1.1.1.19  root     10796:                        } else if(ReadFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     10797:                                REG8(AL) = 0x0b; // read error
1.1.1.3   root     10798:                                m_CF = 1;
1.1       root     10799:                        }
                   10800:                        CloseHandle(hFile);
                   10801:                }
                   10802:        }
                   10803: }
                   10804: 
                   10805: inline void msdos_int_26h()
                   10806: {
                   10807:        // this operation may cause serious damage for drives, so always returns error...
                   10808:        UINT16 seg, ofs;
                   10809:        DWORD dwSize;
                   10810:        
1.1.1.3   root     10811: #if defined(HAS_I386)
                   10812:        I386OP(pushf)();
                   10813: #else
                   10814:        PREFIX86(_pushf());
                   10815: #endif
1.1       root     10816:        
                   10817:        if(!(REG8(AL) < 26)) {
                   10818:                REG8(AL) = 0x01; // unit unknown
1.1.1.3   root     10819:                m_CF = 1;
1.1       root     10820:        } else if(!msdos_drive_param_block_update(REG8(AL), &seg, &ofs, 0)) {
                   10821:                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     10822:                m_CF = 1;
1.1       root     10823:        } else {
                   10824:                dpb_t *dpb = (dpb_t *)(mem + (seg << 4) + ofs);
                   10825:                char dev[64];
                   10826:                sprintf(dev, "\\\\.\\%c:", 'A' + REG8(AL));
                   10827:                
                   10828:                if(dpb->media_type == 0xf8) {
                   10829:                        // this drive is not a floppy
1.1.1.6   root     10830: //                     if(!(((dos_info_t *)(mem + DOS_INFO_TOP))->dos_flag & 0x40)) {
                   10831: //                             fatalerror("This application tried the absolute disk write to drive %c:\n", 'A' + REG8(AL));
                   10832: //                     }
1.1       root     10833:                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     10834:                        m_CF = 1;
1.1       root     10835:                } else {
                   10836:                        HANDLE hFile = CreateFile(dev, GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL | FILE_FLAG_NO_BUFFERING, NULL);
                   10837:                        if(hFile == INVALID_HANDLE_VALUE) {
                   10838:                                REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     10839:                                m_CF = 1;
1.1       root     10840:                        } else {
1.1.1.19  root     10841:                                UINT32 top_sector  = REG16(DX);
                   10842:                                UINT16 sector_num  = REG16(CX);
                   10843:                                UINT32 buffer_addr = SREG_BASE(DS) + REG16(BX);
                   10844:                                
                   10845:                                if(sector_num == 0xffff) {
                   10846:                                        top_sector  = *(UINT32 *)(mem + buffer_addr + 0);
                   10847:                                        sector_num  = *(UINT16 *)(mem + buffer_addr + 4);
                   10848:                                        UINT16 ofs  = *(UINT16 *)(mem + buffer_addr + 6);
                   10849:                                        UINT16 seg  = *(UINT16 *)(mem + buffer_addr + 8);
                   10850:                                        buffer_addr = (seg << 4) + ofs;
                   10851:                                }
1.1       root     10852:                                if(DeviceIoControl(hFile, FSCTL_LOCK_VOLUME, NULL, 0, NULL, 0, &dwSize, NULL) == 0) {
                   10853:                                        REG8(AL) = 0x02; // drive not ready
1.1.1.3   root     10854:                                        m_CF = 1;
1.1.1.19  root     10855:                                } else if(SetFilePointer(hFile, top_sector * dpb->bytes_per_sector, NULL, FILE_BEGIN) == INVALID_SET_FILE_POINTER) {
1.1       root     10856:                                        REG8(AL) = 0x08; // sector not found
1.1.1.3   root     10857:                                        m_CF = 1;
1.1.1.19  root     10858:                                } else if(WriteFile(hFile, mem + buffer_addr, sector_num * dpb->bytes_per_sector, &dwSize, NULL) == 0) {
1.1       root     10859:                                        REG8(AL) = 0x0a; // write error
1.1.1.3   root     10860:                                        m_CF = 1;
1.1       root     10861:                                }
                   10862:                                CloseHandle(hFile);
                   10863:                        }
                   10864:                }
                   10865:        }
                   10866: }
                   10867: 
                   10868: inline void msdos_int_27h()
                   10869: {
1.1.1.29  root     10870:        int paragraphs = (min(REG16(DX), 0xfff0) + 15) >> 4;
                   10871:        try {
                   10872:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   10873:        } catch(...) {
                   10874:                // recover the broken mcb
                   10875:                int mcb_seg = SREG(CS) - 1;
                   10876:                mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
1.1.1.33  root     10877:                
1.1.1.29  root     10878:                if(mcb_seg < (MEMORY_END >> 4)) {
1.1.1.33  root     10879:                        mcb->mz = 'M';
                   10880:                        mcb->paragraphs = (MEMORY_END >> 4) - mcb_seg - 2;
                   10881:                        
1.1.1.29  root     10882:                        if(((dos_info_t *)(mem + DOS_INFO_TOP))->umb_linked & 0x01) {
1.1.1.33  root     10883:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4));
1.1.1.29  root     10884:                        } else {
1.1.1.33  root     10885:                                msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0);
1.1.1.29  root     10886:                        }
                   10887:                } else {
                   10888:                        mcb->mz = 'Z';
1.1.1.30  root     10889:                        mcb->paragraphs = (UMB_END >> 4) - mcb_seg - 1;
1.1.1.29  root     10890:                }
                   10891:                msdos_mem_realloc(SREG(CS), paragraphs, NULL);
                   10892:        }
1.1.1.3   root     10893:        msdos_process_terminate(SREG(CS), retval | 0x300, 0);
1.1       root     10894: }
                   10895: 
                   10896: inline void msdos_int_29h()
                   10897: {
1.1.1.14  root     10898: #if 1
                   10899:        // need to check escape sequences
1.1       root     10900:        msdos_putch(REG8(AL));
1.1.1.14  root     10901: #else
                   10902:        DWORD num;
                   10903:        vram_flush();
1.1.1.23  root     10904:        WriteConsole(GetStdHandle(STD_OUTPUT_HANDLE), &REG8(AL), 1, &num, NULL);
1.1.1.14  root     10905:        cursor_moved = true;
                   10906: #endif
1.1       root     10907: }
                   10908: 
                   10909: inline void msdos_int_2eh()
                   10910: {
                   10911:        char tmp[MAX_PATH], command[MAX_PATH], opt[MAX_PATH];
                   10912:        memset(tmp, 0, sizeof(tmp));
1.1.1.3   root     10913:        strcpy(tmp, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
1.1       root     10914:        char *token = my_strtok(tmp, " ");
                   10915:        strcpy(command, token);
                   10916:        strcpy(opt, token + strlen(token) + 1);
                   10917:        
                   10918:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   10919:        param->env_seg = 0;
                   10920:        param->cmd_line.w.l = 44;
                   10921:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   10922:        param->fcb1.w.l = 24;
                   10923:        param->fcb1.w.h = (WORK_TOP >> 4);
                   10924:        param->fcb2.w.l = 24;
                   10925:        param->fcb2.w.h = (WORK_TOP >> 4);
                   10926:        
                   10927:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   10928:        
                   10929:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   10930:        cmd_line->len = strlen(opt);
                   10931:        strcpy(cmd_line->cmd, opt);
                   10932:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   10933:        
1.1.1.28  root     10934:        try {
                   10935:                if(msdos_process_exec(command, param, 0)) {
                   10936:                        REG16(AX) = 0xffff; // error before processing command
                   10937:                } else {
                   10938:                        // set flag to set retval to ax when the started process is terminated
                   10939:                        process_t *process = msdos_process_info_get(current_psp);
                   10940:                        process->called_by_int2eh = true;
                   10941:                }
                   10942:        } catch(...) {
                   10943:                REG16(AX) = 0xffff; // error before processing command
                   10944:        }
1.1       root     10945: }
                   10946: 
1.1.1.29  root     10947: inline void msdos_int_2fh_05h()
                   10948: {
                   10949:        switch(REG8(AL)) {
                   10950:        case 0x00:
1.1.1.32  root     10951:                REG8(AL) = 0xff;
                   10952:                break;
                   10953:        case 0x01:
                   10954:        case 0x02:
                   10955:                for(int i = 0; i < array_length(standard_error_table); i++) {
                   10956:                        if(standard_error_table[i].code == REG16(BX) || standard_error_table[i].code == (UINT16)-1) {
                   10957:                                const char *message = NULL;
                   10958:                                if(active_code_page == 932) {
                   10959:                                        message = standard_error_table[i].message_japanese;
                   10960:                                }
                   10961:                                if(message == NULL) {
                   10962:                                        message = standard_error_table[i].message_english;
                   10963:                                }
                   10964:                                strcpy((char *)(mem + WORK_TOP), message);
                   10965:                                
                   10966:                                SREG(ES) = WORK_TOP >> 4;
                   10967:                                i386_load_segment_descriptor(ES);
                   10968:                                REG16(DI) = 0x0000;
                   10969:                                REG8(AL) = 0x01;
                   10970:                                break;
                   10971:                        }
                   10972:                }
1.1.1.29  root     10973:                break;
                   10974:        default:
                   10975:                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));
                   10976:                m_CF = 1;
                   10977:        }
                   10978: }
                   10979: 
1.1.1.22  root     10980: inline void msdos_int_2fh_11h()
                   10981: {
                   10982:        switch(REG8(AL)) {
                   10983:        case 0x00:
1.1.1.29  root     10984:                if(i386_read_stack() == 0xdada) {
                   10985:                        // MSCDEX is not installed
                   10986: //                     REG8(AL) = 0x00;
                   10987:                } else {
                   10988:                        // Network Redirector is not installed
                   10989: //                     REG8(AL) = 0x00;
                   10990:                }
1.1.1.22  root     10991:                break;
                   10992:        default:
                   10993:                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     10994:                REG16(AX) = 0x49; //  network software not installed
1.1.1.22  root     10995:                m_CF = 1;
                   10996:                break;
                   10997:        }
                   10998: }
                   10999: 
1.1.1.21  root     11000: inline void msdos_int_2fh_12h()
                   11001: {
                   11002:        switch(REG8(AL)) {
1.1.1.22  root     11003:        case 0x00:
1.1.1.29  root     11004:                // DOS 3.0+ internal functions are installed
1.1.1.22  root     11005:                REG8(AL) = 0xff;
                   11006:                break;
1.1.1.29  root     11007: //     case 0x01: // DOS 3.0+ internal - Close Current File
                   11008:        case 0x02:
                   11009:                {
                   11010:                        UINT16 stack = i386_read_stack();
                   11011:                        REG16(BX) = *(UINT16 *)(mem + 4 * stack + 0);
                   11012:                        SREG(ES) = *(UINT16 *)(mem + 4 * stack + 2);
                   11013:                        i386_load_segment_descriptor(ES);
                   11014:                }
                   11015:                break;
1.1.1.30  root     11016:        case 0x03:
                   11017:                SREG(DS) = (DEVICE_TOP >> 4);
                   11018:                i386_load_segment_descriptor(DS);
                   11019:                break;
1.1.1.29  root     11020:        case 0x04:
                   11021:                {
                   11022:                        UINT16 stack = i386_read_stack();
                   11023:                        REG8(AL) = (stack == '/') ? '\\' : stack;
                   11024: #if defined(HAS_I386)
                   11025:                        m_ZF = (REG8(AL) == '\\');
                   11026: #else
                   11027:                        m_ZeroVal = (REG8(AL) != '\\');
                   11028: #endif
                   11029:                }
                   11030:                break;
                   11031:        case 0x05:
                   11032:                msdos_putch(i386_read_stack());
                   11033:                break;
                   11034: //     case 0x06: // DOS 3.0+ internal - Invole Critical Error
                   11035: //     case 0x07: // DOS 3.0+ internal - Make Disk Buffer Most Recentry Used
                   11036: //     case 0x08: // DOS 3.0+ internal - Decrement SFT Reference Count
                   11037: //     case 0x09: // DOS 3.0+ internal - Flush and FREE Disk Buffer
                   11038: //     case 0x0a: // DOS 3.0+ internal - Perform Critical Error Interrupt
                   11039: //     case 0x0b: // DOS 3.0+ internal - Signal Sharing Violation to User
                   11040: //     case 0x0c: // DOS 3.0+ internal - Open Device and Set SFT Owner/Mode
                   11041:        case 0x0d:
                   11042:                {
                   11043:                        SYSTEMTIME time;
                   11044:                        FILETIME file_time;
                   11045:                        WORD dos_date, dos_time;
                   11046:                        GetLocalTime(&time);
                   11047:                        SystemTimeToFileTime(&time, &file_time);
                   11048:                        FileTimeToDosDateTime(&file_time, &dos_date, &dos_time);
                   11049:                        REG16(AX) = dos_date;
                   11050:                        REG16(DX) = dos_time;
                   11051:                }
                   11052:                break;
                   11053: //     case 0x0e: // DOS 3.0+ internal - Mark All Disk Buffers Unreferenced
                   11054: //     case 0x0f: // DOS 3.0+ internal - Make Buffer Most Recentry Used
                   11055: //     case 0x10: // DOS 3.0+ internal - Find Unreferenced Disk Buffer
                   11056:        case 0x11:
                   11057:                {
                   11058:                        char path[MAX_PATH], *p;
                   11059:                        strcpy(path, (char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   11060:                        my_strupr(path);
                   11061:                        while((p = my_strchr(path, '/')) != NULL) {
                   11062:                                *p = '\\';
                   11063:                        }
                   11064:                        strcpy((char *)(mem + SREG_BASE(ES) + REG16(DI)), path);
                   11065:                }
                   11066:                break;
                   11067:        case 0x12:
                   11068:                REG16(CX) = strlen((char *)(mem + SREG_BASE(ES) + REG16(DI)));
                   11069:                break;
                   11070:        case 0x13:
                   11071:                {
                   11072:                        char tmp[2] = {0};
                   11073:                        tmp[0] = i386_read_stack();
                   11074:                        my_strupr(tmp);
                   11075:                        REG8(AL) = tmp[0];
                   11076:                }
                   11077:                break;
                   11078:        case 0x14:
                   11079: #if defined(HAS_I386)
                   11080:                m_ZF = (SREG_BASE(DS) + REG16(SI) == SREG_BASE(ES) + REG16(DI));
                   11081: #else
                   11082:                m_ZeroVal = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   11083: #endif
                   11084:                m_CF = (SREG_BASE(DS) + REG16(SI) != SREG_BASE(ES) + REG16(DI));
                   11085:                break;
                   11086: //     case 0x15: // DOS 3.0+ internal - Flush Buffer
1.1.1.21  root     11087:        case 0x16:
                   11088:                if(REG16(BX) < 20) {
                   11089:                        SREG(ES) = SFT_TOP >> 4;
                   11090:                        i386_load_segment_descriptor(ES);
                   11091:                        REG16(DI) = 6 + 0x3b * REG16(BX);
                   11092:                        
                   11093:                        // update system file table
                   11094:                        UINT8* sft = mem + SFT_TOP + 6 + 0x3b * REG16(BX);
                   11095:                        if(file_handler[REG16(BX)].valid) {
                   11096:                                int count = 0;
                   11097:                                for(int i = 0; i < 20; i++) {
                   11098:                                        if(msdos_psp_get_file_table(i, current_psp) == REG16(BX)) {
                   11099:                                                count++;
                   11100:                                        }
                   11101:                                }
                   11102:                                *(UINT16 *)(sft + 0x00) = count ? count : 0xffff;
                   11103:                                *(UINT32 *)(sft + 0x15) = _tell(REG16(BX));
                   11104:                                _lseek(REG16(BX), 0, SEEK_END);
                   11105:                                *(UINT32 *)(sft + 0x11) = _tell(REG16(BX));
                   11106:                                _lseek(REG16(BX), *(UINT32 *)(sft + 0x15), SEEK_SET);
                   11107:                        } else {
                   11108:                                memset(sft, 0, 0x3b);
                   11109:                        }
                   11110:                } else {
                   11111:                        REG16(AX) = 0x06;
                   11112:                        m_CF = 1;
                   11113:                }
                   11114:                break;
1.1.1.29  root     11115: //     case 0x17: // DOS 3.0+ internal - Get Current Directory Structure for Drive
                   11116: //     case 0x18: // DOS 3.0+ internal - Get Caller's Registers
                   11117: //     case 0x19: // DOS 3.0+ internal - Set Drive???
                   11118:        case 0x1a:
                   11119:                {
                   11120:                        char *path = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full[MAX_PATH];
                   11121:                        if(path[1] == ':') {
                   11122:                                if(path[0] >= 'a' && path[0] <= 'z') {
                   11123:                                        REG8(AL) = path[0] - 'a' + 1;
                   11124:                                } else if(path[0] >= 'A' && path[0] <= 'Z') {
                   11125:                                        REG8(AL) = path[0] - 'A' + 1;
                   11126:                                } else {
                   11127:                                        REG8(AL) = 0xff; // invalid
                   11128:                                }
                   11129:                                strcpy(full, path);
                   11130:                                strcpy(path, full + 2);
                   11131:                        } else if(GetFullPathName(path, MAX_PATH, full, NULL) != 0 && full[1] == ':') {
                   11132:                                if(full[0] >= 'a' && full[0] <= 'z') {
                   11133:                                        REG8(AL) = full[0] - 'a' + 1;
                   11134:                                } else if(full[0] >= 'A' && full[0] <= 'Z') {
                   11135:                                        REG8(AL) = full[0] - 'A' + 1;
                   11136:                                } else {
                   11137:                                        REG8(AL) = 0xff; // invalid
                   11138:                                }
                   11139:                        } else {
                   11140:                                REG8(AL) = 0x00; // default
                   11141:                        }
                   11142:                }
                   11143:                break;
                   11144:        case 0x1b:
                   11145:                {
                   11146:                        int year = REG16(CX) + 1980;
                   11147:                        REG8(AL) = ((year % 4) == 0 && (year % 100) != 0 && (year % 400) == 0) ? 29 : 28;
                   11148:                }
                   11149:                break;
                   11150: //     case 0x1c: // DOS 3.0+ internal - Check Sum Memory
                   11151: //     case 0x1d: // DOS 3.0+ internal - Sum Memory
                   11152:        case 0x1e:
                   11153:                {
                   11154:                        char *path_1st = (char *)(mem + SREG_BASE(DS) + REG16(SI)), full_1st[MAX_PATH];
                   11155:                        char *path_2nd = (char *)(mem + SREG_BASE(ES) + REG16(DI)), full_2nd[MAX_PATH];
                   11156:                        if(GetFullPathName(path_1st, MAX_PATH, full_1st, NULL) != 0 && GetFullPathName(path_2nd, MAX_PATH, full_2nd, NULL) != 0) {
                   11157: #if defined(HAS_I386)
                   11158:                                m_ZF = (strcmp(full_1st, full_2nd) == 0);
                   11159: #else
                   11160:                                m_ZeroVal = (strcmp(full_1st, full_2nd) != 0);
                   11161: #endif
                   11162:                        } else {
                   11163: #if defined(HAS_I386)
                   11164:                                m_ZF = (strcmp(path_1st, path_2nd) == 0);
                   11165: #else
                   11166:                                m_ZeroVal = (strcmp(path_1st, path_2nd) != 0);
                   11167: #endif
                   11168:                        }
                   11169:                }
                   11170:                break;
                   11171: //     case 0x1f: // DOS 3.0+ internal - Build Current Directory Structure
1.1.1.21  root     11172:        case 0x20:
                   11173:                {
                   11174:                        int fd = msdos_psp_get_file_table(REG16(BX), current_psp);
                   11175:                        
                   11176:                        if(fd < 20) {
                   11177:                                SREG(ES) = current_psp;
                   11178:                                i386_load_segment_descriptor(ES);
                   11179:                                REG16(DI) = offsetof(psp_t, file_table) + fd;
                   11180:                        } else {
                   11181:                                REG16(AX) = 0x06;
                   11182:                                m_CF = 1;
                   11183:                        }
                   11184:                }
                   11185:                break;
1.1.1.29  root     11186:        case 0x21:
                   11187:                msdos_int_21h_60h(0);
                   11188:                break;
                   11189: //     case 0x22: // DOS 3.0+ internal - Set Extended Error Info
                   11190: //     case 0x23: // DOS 3.0+ internal - Check If Character Device
                   11191: //     case 0x24: // DOS 3.0+ internal - Sharing Retry Delay
                   11192:        case 0x25:
                   11193:                REG16(CX) = strlen((char *)(mem + SREG_BASE(DS) + REG16(SI)));
                   11194:                break;
                   11195:        case 0x26:
                   11196:                REG8(AL) = REG8(CL);
                   11197:                msdos_int_21h_3dh();
                   11198:                break;
                   11199:        case 0x27:
                   11200:                msdos_int_21h_3eh();
                   11201:                break;
                   11202:        case 0x28:
                   11203:                REG16(AX) = REG16(BP);
                   11204:                msdos_int_21h_42h();
                   11205:                break;
                   11206:        case 0x29:
                   11207:                msdos_int_21h_3fh();
                   11208:                break;
                   11209: //     case 0x2a: // DOS 3.0+ internal - Set Fast Open Entry Point
                   11210:        case 0x2b:
                   11211:                REG16(AX) = REG16(BP);
                   11212:                msdos_int_21h_44h();
                   11213:                break;
                   11214:        case 0x2c:
                   11215:                REG16(BX) = DEVICE_TOP >> 4;
                   11216:                REG16(AX) = 22;
                   11217:                break;
                   11218:        case 0x2d:
                   11219:                {
                   11220:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   11221:                        REG16(AX) = sda->extended_error_code;
                   11222:                }
                   11223:                break;
                   11224:        case 0x2e:
                   11225:                if(REG8(DL) == 0x00 || REG8(DL) == 0x02 || REG8(DL) == 0x04 || REG8(DL) == 0x06) {
1.1.1.32  root     11226:                        SREG(ES) = 0x0001;
                   11227:                        i386_load_segment_descriptor(ES);
                   11228:                        REG16(DI) = 0x00;
                   11229:                } else if(REG8(DL) == 0x08) {
                   11230:                        // dummy parameter error message read routine is at fffd:0010
                   11231:                        SREG(ES) = 0xfffd;
1.1.1.22  root     11232:                        i386_load_segment_descriptor(ES);
1.1.1.32  root     11233:                        REG16(DI) = 0x0010;
1.1.1.22  root     11234:                }
                   11235:                break;
1.1.1.29  root     11236:        case 0x2f:
                   11237:                if(REG16(DX) != 0) {
1.1.1.30  root     11238:                        dos_major_version = REG8(DL);
                   11239:                        dos_minor_version = REG8(DH);
1.1.1.29  root     11240:                } else {
                   11241:                        REG8(DL) = 7;
                   11242:                        REG8(DH) = 10;
                   11243:                }
                   11244:                break;
                   11245: //     case 0x30: // Windows95 - Find SFT Entry in Internal File Tables
                   11246: //     case 0x31: // Windows95 - Set/Clear "Report Windows to DOS Programs" Flag
1.1.1.22  root     11247:        default:
                   11248:                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));
                   11249:                REG16(AX) = 0x01;
                   11250:                m_CF = 1;
                   11251:                break;
                   11252:        }
                   11253: }
                   11254: 
1.1.1.30  root     11255: inline void msdos_int_2fh_13h()
                   11256: {
                   11257:        static UINT16 prevDS = 0, prevDX = 0;
                   11258:        static UINT16 prevES = 0, prevBX = 0;
                   11259:        UINT16 tmp;
                   11260:        
                   11261:        tmp = SREG(DS); SREG(DS) = prevDS; prevDS = tmp;
                   11262:        i386_load_segment_descriptor(DS);
                   11263:        tmp = REG16(DX); REG16(DX) = prevDX; prevDX = tmp;
                   11264:        
                   11265:        tmp = SREG(ES); SREG(ES) = prevES; prevES = tmp;
                   11266:        i386_load_segment_descriptor(ES);
                   11267:        tmp = REG16(BX); REG16(BX) = prevBX; prevBX = tmp;
                   11268: }
                   11269: 
1.1.1.22  root     11270: inline void msdos_int_2fh_14h()
                   11271: {
                   11272:        switch(REG8(AL)) {
                   11273:        case 0x00:
1.1.1.29  root     11274:                // NLSFUNC.COM is installed
                   11275:                REG8(AL) = 0xff;
1.1.1.25  root     11276:                break;
                   11277:        case 0x01:
                   11278:        case 0x03:
                   11279:                REG8(AL) = 0x00;
                   11280:                active_code_page = REG16(BX);
                   11281:                msdos_nls_tables_update();
                   11282:                break;
                   11283:        case 0x02:
                   11284:                REG8(AL) = get_extended_country_info(REG16(BP));
                   11285:                break;
                   11286:        case 0x04:
                   11287:                REG8(AL) = 0x00;
                   11288:                get_country_info((country_info_t *)(mem + SREG_BASE(ES) + REG16(DI)));
1.1.1.22  root     11289:                break;
                   11290:        default:
                   11291:                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));
                   11292:                REG16(AX) = 0x01;
                   11293:                m_CF = 1;
                   11294:                break;
                   11295:        }
                   11296: }
                   11297: 
                   11298: inline void msdos_int_2fh_15h()
                   11299: {
                   11300:        switch(REG8(AL)) {
1.1.1.29  root     11301:        case 0x00: // CD-ROM - Installation Check
                   11302:                if(REG16(BX) == 0x0000) {
                   11303:                        // MSCDEX is not installed
                   11304: //                     REG8(AL) = 0x00;
                   11305:                } else {
                   11306:                        // GRAPHICS.COM is not installed
                   11307: //                     REG8(AL) = 0x00;
                   11308:                }
1.1.1.22  root     11309:                break;
                   11310:        case 0xff:
1.1.1.29  root     11311:                if(REG16(BX) == 0x0000) {
                   11312:                        // CORELCDX is not installed
                   11313:                } else {
                   11314:                        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));
                   11315:                        REG16(AX) = 0x01;
                   11316:                        m_CF = 1;
                   11317:                }
1.1.1.22  root     11318:                break;
1.1.1.21  root     11319:        default:
1.1.1.22  root     11320:                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     11321:                REG16(AX) = 0x01;
                   11322:                m_CF = 1;
                   11323:                break;
                   11324:        }
                   11325: }
                   11326: 
1.1       root     11327: inline void msdos_int_2fh_16h()
                   11328: {
                   11329:        switch(REG8(AL)) {
                   11330:        case 0x00:
1.1.1.14  root     11331:                if(no_windows) {
1.1.1.29  root     11332:                        // neither Windows 3.x enhanced mode nor Windows/386 2.x running
                   11333: //                     REG8(AL) = 0x00;
1.1.1.14  root     11334:                } else {
1.1.1.30  root     11335:                        REG8(AL) = win_major_version;
                   11336:                        REG8(AH) = win_minor_version;
1.1       root     11337:                }
                   11338:                break;
1.1.1.30  root     11339:        case 0x05:
                   11340:                // from DOSBox
                   11341:                i386_set_a20_line(1);
                   11342:                break;
1.1.1.22  root     11343:        case 0x0a:
                   11344:                if(!no_windows) {
                   11345:                        REG16(AX) = 0x0000;
1.1.1.30  root     11346:                        REG8(BH) = win_major_version;
                   11347:                        REG8(BL) = win_minor_version;
1.1.1.22  root     11348:                        REG16(CX) = 0x0003; // enhanced
                   11349:                }
                   11350:                break;
1.1.1.30  root     11351:        case 0x0b:
                   11352:                // no TRS, keep ES:DI = 0000h:0000h
1.1.1.22  root     11353:        case 0x0e:
                   11354:        case 0x0f:
1.1.1.30  root     11355:        case 0x10:
1.1.1.22  root     11356:        case 0x11:
                   11357:        case 0x12:
                   11358:        case 0x13:
                   11359:        case 0x14:
1.1.1.30  root     11360:        case 0x15:
1.1.1.33  root     11361:        case 0x86:
1.1.1.22  root     11362:        case 0x87:
1.1.1.30  root     11363:        case 0x89:
1.1.1.33  root     11364:        case 0x8a:
1.1.1.22  root     11365:                // function not supported, do not clear AX
                   11366:                break;
1.1.1.14  root     11367:        case 0x80:
                   11368:                Sleep(10);
                   11369:                hardware_update();
1.1.1.29  root     11370:                REG8(AL) = 0x00;
1.1.1.14  root     11371:                break;
1.1.1.33  root     11372:        case 0x83:
                   11373:                REG16(BX) = 0x01; // system vm id
                   11374:                break;
1.1.1.22  root     11375:        case 0x8e:
                   11376:                REG16(AX) = 0x00; // failed
                   11377:                break;
1.1.1.20  root     11378:        case 0x8f:
                   11379:                switch(REG8(DH)) {
                   11380:                case 0x00:
                   11381:                case 0x02:
                   11382:                case 0x03:
                   11383:                        REG16(AX) = 0x00;
                   11384:                        break;
                   11385:                case 0x01:
                   11386:                        REG16(AX) = 0x168f;
                   11387:                        break;
                   11388:                }
                   11389:                break;
1.1       root     11390:        default:
1.1.1.22  root     11391:                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));
                   11392:                REG16(AX) = 0x01;
                   11393:                m_CF = 1;
                   11394:                break;
                   11395:        }
                   11396: }
                   11397: 
                   11398: inline void msdos_int_2fh_19h()
                   11399: {
                   11400:        switch(REG8(AL)) {
                   11401:        case 0x00:
1.1.1.29  root     11402:                // SHELLB.COM is not installed
                   11403: //             REG8(AL) = 0x00;
1.1.1.22  root     11404:                break;
                   11405:        case 0x01:
                   11406:        case 0x02:
                   11407:        case 0x03:
                   11408:        case 0x04:
                   11409:                REG16(AX) = 0x01;
                   11410:                m_CF = 1;
                   11411:                break;
1.1.1.29  root     11412:        case 0x80:
                   11413:                // IBM ROM-DOS v4.0 is not installed
                   11414: //             REG8(AL) = 0x00;
                   11415:                break;
1.1.1.22  root     11416:        default:
                   11417:                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     11418:                REG16(AX) = 0x01;
1.1.1.3   root     11419:                m_CF = 1;
1.1       root     11420:                break;
                   11421:        }
                   11422: }
                   11423: 
                   11424: inline void msdos_int_2fh_1ah()
                   11425: {
                   11426:        switch(REG8(AL)) {
                   11427:        case 0x00:
1.1.1.29  root     11428:                // ANSI.SYS is installed
1.1       root     11429:                REG8(AL) = 0xff;
                   11430:                break;
                   11431:        default:
1.1.1.22  root     11432:                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));
                   11433:                REG16(AX) = 0x01;
                   11434:                m_CF = 1;
                   11435:                break;
                   11436:        }
                   11437: }
                   11438: 
1.1.1.30  root     11439: inline void msdos_int_2fh_40h()
1.1.1.22  root     11440: {
                   11441:        switch(REG8(AL)) {
                   11442:        case 0x00:
1.1.1.30  root     11443:                // Windows 3+ - Get Virtual Device Driver (VDD) Capabilities
                   11444:                REG8(AL) = 0x01; // does not virtualize video access
1.1.1.22  root     11445:                break;
                   11446:        default:
                   11447:                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     11448:                REG16(AX) = 0x01;
1.1.1.3   root     11449:                m_CF = 1;
1.1       root     11450:                break;
                   11451:        }
                   11452: }
                   11453: 
                   11454: inline void msdos_int_2fh_43h()
                   11455: {
                   11456:        switch(REG8(AL)) {
                   11457:        case 0x00:
1.1.1.29  root     11458:                // XMS is installed ?
1.1.1.19  root     11459: #ifdef SUPPORT_XMS
                   11460:                if(support_xms) {
                   11461:                        REG8(AL) = 0x80;
                   11462:                } else
                   11463: #endif
                   11464:                REG8(AL) = 0x00;
                   11465:                break;
                   11466:        case 0x10:
                   11467:                SREG(ES) = XMS_TOP >> 4;
                   11468:                i386_load_segment_descriptor(ES);
1.1.1.26  root     11469:                REG16(BX) = 0x15;
1.1       root     11470:                break;
                   11471:        default:
1.1.1.22  root     11472:                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));
                   11473:                REG16(AX) = 0x01;
                   11474:                m_CF = 1;
                   11475:                break;
                   11476:        }
                   11477: }
                   11478: 
                   11479: inline void msdos_int_2fh_46h()
                   11480: {
                   11481:        switch(REG8(AL)) {
                   11482:        case 0x80:
1.1.1.29  root     11483:                // Windows v3.0 is not installed
                   11484: //             REG8(AL) = 0x00;
1.1.1.22  root     11485:                break;
                   11486:        default:
                   11487:                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));
                   11488:                REG16(AX) = 0x01;
                   11489:                m_CF = 1;
                   11490:                break;
                   11491:        }
                   11492: }
                   11493: 
                   11494: inline void msdos_int_2fh_48h()
                   11495: {
                   11496:        switch(REG8(AL)) {
                   11497:        case 0x00:
1.1.1.29  root     11498:                // DOSKEY is not installed
                   11499: //             REG8(AL) = 0x00;
1.1.1.22  root     11500:                break;
                   11501:        case 0x10:
                   11502:                msdos_int_21h_0ah();
                   11503:                REG16(AX) = 0x00;
                   11504:                break;
                   11505:        default:
                   11506:                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     11507:                REG16(AX) = 0x01;
1.1.1.3   root     11508:                m_CF = 1;
1.1       root     11509:                break;
                   11510:        }
                   11511: }
                   11512: 
                   11513: inline void msdos_int_2fh_4ah()
                   11514: {
                   11515:        switch(REG8(AL)) {
1.1.1.29  root     11516: #ifdef SUPPORT_HMA
                   11517:        case 0x01: // DOS 5.0+ - Query Free HMA Space
                   11518:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   11519:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   11520:                                // restore first free mcb in high memory area
                   11521:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   11522:                        }
                   11523:                        int offset = 0xffff;
                   11524:                        if((REG16(BX) = msdos_hma_mem_get_free(&offset)) != 0) {
                   11525:                                REG16(DI) = offset + 0x10;
                   11526:                        } else {
                   11527:                                REG16(DI) = 0xffff;
                   11528:                        }
                   11529:                } else {
                   11530:                        // HMA is already used
                   11531:                        REG16(BX) = 0;
                   11532:                        REG16(DI) = 0xffff;
                   11533:                }
                   11534:                SREG(ES) = 0xffff;
                   11535:                i386_load_segment_descriptor(ES);
                   11536:                break;
                   11537:        case 0x02: // DOS 5.0+ - Allocate HMA Space
                   11538:                if(!is_hma_used_by_xms && !is_hma_used_by_int_2fh) {
                   11539:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   11540:                                // restore first free mcb in high memory area
                   11541:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   11542:                        }
                   11543:                        int size = REG16(BX), offset;
                   11544:                        if((size % 16) != 0) {
                   11545:                                size &= ~15;
                   11546:                                size += 16;
                   11547:                        }
                   11548:                        if((offset = msdos_hma_mem_alloc(size, current_psp)) != -1) {
                   11549:                                REG16(BX) = size;
                   11550:                                REG16(DI) = offset + 0x10;
                   11551:                                is_hma_used_by_int_2fh = true;
                   11552:                        } else {
                   11553:                                REG16(BX) = 0;
                   11554:                                REG16(DI) = 0xffff;
                   11555:                        }
                   11556:                } else {
                   11557:                        // HMA is already used
                   11558:                        REG16(BX) = 0;
                   11559:                        REG16(DI) = 0xffff;
                   11560:                }
                   11561:                SREG(ES) = 0xffff;
                   11562:                i386_load_segment_descriptor(ES);
                   11563:                break;
                   11564:        case 0x03: // Windows95 - (De)Allocate HMA Memory Block
                   11565:                if(REG8(DL) == 0x00) {
                   11566:                        if(!is_hma_used_by_xms) {
                   11567:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   11568:                                        // restore first free mcb in high memory area
                   11569:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   11570:                                        is_hma_used_by_int_2fh = false;
                   11571:                                }
                   11572:                                int size = REG16(BX), offset;
                   11573:                                if((size % 16) != 0) {
                   11574:                                        size &= ~15;
                   11575:                                        size += 16;
                   11576:                                }
                   11577:                                if((offset = msdos_hma_mem_alloc(size, REG16(CX))) != -1) {
                   11578: //                                     REG16(BX) = size;
                   11579:                                        SREG(ES) = 0xffff;
                   11580:                                        i386_load_segment_descriptor(ES);
                   11581:                                        REG16(DI) = offset + 0x10;
                   11582:                                        is_hma_used_by_int_2fh = true;
                   11583:                                } else {
                   11584:                                        REG16(DI) = 0xffff;
                   11585:                                }
                   11586:                        } else {
                   11587:                                REG16(DI) = 0xffff;
                   11588:                        }
                   11589:                } else if(REG8(DL) == 0x01) {
                   11590:                        if(!is_hma_used_by_xms) {
                   11591:                                int size = REG16(BX);
                   11592:                                if((size % 16) != 0) {
                   11593:                                        size &= ~15;
                   11594:                                        size += 16;
                   11595:                                }
                   11596:                                if(msdos_hma_mem_realloc(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10, size) != -1) {
                   11597:                                        // memory block address is not changed
                   11598:                                } else {
                   11599:                                        REG16(DI) = 0xffff;
                   11600:                                }
                   11601:                        } else {
                   11602:                                REG16(DI) = 0xffff;
                   11603:                        }
                   11604:                } else if(REG8(DL) == 0x02) {
                   11605:                        if(!is_hma_used_by_xms) {
                   11606:                                if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   11607:                                        // restore first free mcb in high memory area
                   11608:                                        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   11609:                                        is_hma_used_by_int_2fh = false;
                   11610:                                } else {
                   11611:                                        msdos_hma_mem_free(SREG_BASE(ES) + REG16(DI) - 0xffff0 - 0x10);
                   11612:                                        if(msdos_hma_mem_get_free(NULL) == 0xffe0) {
                   11613:                                                is_hma_used_by_int_2fh = false;
                   11614:                                        }
                   11615:                                }
                   11616:                        }
                   11617:                } else {
                   11618:                        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));
                   11619:                        REG16(AX) = 0x01;
                   11620:                        m_CF = 1;
                   11621:                }
                   11622:                break;
                   11623:        case 0x04: // Windows95 - Get Start of HMA Memory Chain
                   11624:                if(!is_hma_used_by_xms) {
                   11625:                        if(!msdos_is_hma_mcb_valid((hma_mcb_t *)(mem + 0xffff0 + 0x10))) {
                   11626:                                // restore first free mcb in high memory area
                   11627:                                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   11628:                                is_hma_used_by_int_2fh = false;
                   11629:                        }
                   11630:                        REG16(AX) = 0x0000;
                   11631:                        SREG(ES) = 0xffff;
                   11632:                        i386_load_segment_descriptor(ES);
                   11633:                        REG16(DI) = 0x10;
                   11634:                }
                   11635:                break;
                   11636: #else
1.1       root     11637:        case 0x01:
                   11638:        case 0x02:
1.1.1.29  root     11639:                // HMA is already used
1.1.1.27  root     11640:                REG16(BX) = 0x0000;
1.1.1.3   root     11641:                SREG(ES) = 0xffff;
                   11642:                i386_load_segment_descriptor(ES);
1.1       root     11643:                REG16(DI) = 0xffff;
                   11644:                break;
1.1.1.19  root     11645:        case 0x03:
                   11646:                // unable to allocate
                   11647:                REG16(DI) = 0xffff;
                   11648:                break;
                   11649:        case 0x04:
                   11650:                // function not supported, do not clear AX
                   11651:                break;
1.1.1.29  root     11652: #endif
                   11653:        case 0x10:
                   11654:                if(REG16(BX) == 0x0000) {
                   11655:                        // SMARTDRV is not installed
                   11656:                } else {
                   11657:                        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));
                   11658:                        REG16(AX) = 0x01;
                   11659:                        m_CF = 1;
                   11660:                }
                   11661:                break;
                   11662:        case 0x11:
                   11663:                if(REG16(BX) == 0x0000) {
                   11664:                        // DBLSPACE.BIN is not installed
                   11665:                } else {
                   11666:                        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));
                   11667:                        REG16(AX) = 0x01;
                   11668:                        m_CF = 1;
                   11669:                }
1.1.1.22  root     11670:                break;
                   11671:        default:
                   11672:                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));
                   11673:                REG16(AX) = 0x01;
                   11674:                m_CF = 1;
                   11675:                break;
                   11676:        }
                   11677: }
                   11678: 
                   11679: inline void msdos_int_2fh_4bh()
                   11680: {
                   11681:        switch(REG8(AL)) {
1.1.1.24  root     11682:        case 0x01:
1.1.1.22  root     11683:        case 0x02:
1.1.1.29  root     11684:                // Task Switcher is not installed
1.1.1.24  root     11685:                break;
                   11686:        case 0x03:
                   11687:                // this call is available from within DOSSHELL even if the task switcher is not installed
                   11688:                REG16(AX) = REG16(BX) = 0x0000; // no more avaiable switcher id
1.1.1.22  root     11689:                break;
1.1.1.30  root     11690:        case 0x04:
                   11691:                REG16(BX) = 0x0000; // free switcher id successfully
                   11692:                break;
1.1       root     11693:        default:
1.1.1.22  root     11694:                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     11695:                REG16(AX) = 0x01;
1.1.1.3   root     11696:                m_CF = 1;
1.1       root     11697:                break;
                   11698:        }
                   11699: }
                   11700: 
                   11701: inline void msdos_int_2fh_4fh()
                   11702: {
                   11703:        switch(REG8(AL)) {
                   11704:        case 0x00:
1.1.1.29  root     11705:                // BILING is installed
1.1.1.27  root     11706:                REG16(AX) = 0x0000;
                   11707:                REG8(DL) = 0x01;        // major version
                   11708:                REG8(DH) = 0x00;        // minor version
1.1       root     11709:                break;
                   11710:        case 0x01:
1.1.1.27  root     11711:                REG16(AX) = 0x0000;
1.1       root     11712:                REG16(BX) = active_code_page;
                   11713:                break;
                   11714:        default:
1.1.1.22  root     11715:                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));
                   11716:                REG16(AX) = 0x01;
                   11717:                m_CF = 1;
                   11718:                break;
                   11719:        }
                   11720: }
                   11721: 
                   11722: inline void msdos_int_2fh_55h()
                   11723: {
                   11724:        switch(REG8(AL)) {
                   11725:        case 0x00:
                   11726:        case 0x01:
                   11727: //             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));
                   11728:                break;
                   11729:        default:
                   11730:                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     11731:                REG16(AX) = 0x01;
1.1.1.3   root     11732:                m_CF = 1;
1.1       root     11733:                break;
                   11734:        }
                   11735: }
                   11736: 
1.1.1.24  root     11737: inline void msdos_int_2fh_adh()
                   11738: {
                   11739:        switch(REG8(AL)) {
                   11740:        case 0x00:
1.1.1.29  root     11741:                // DISPLAY.SYS is installed
1.1.1.24  root     11742:                REG8(AL) = 0xff;
                   11743:                REG16(BX) = 0x100; // ???
                   11744:                break;
                   11745:        case 0x01:
                   11746:                active_code_page = REG16(BX);
                   11747:                msdos_nls_tables_update();
                   11748:                REG16(AX) = 0x01;
                   11749:                break;
                   11750:        case 0x02:
                   11751:                REG16(BX) = active_code_page;
                   11752:                break;
                   11753:        case 0x03:
                   11754:                // FIXME
                   11755:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 0) = 1;
                   11756:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2) = 3;
                   11757:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4) = 1;
                   11758:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 6) = active_code_page;
                   11759:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 8) = active_code_page;
                   11760:                break;
                   11761:        case 0x80:
                   11762:                break; // keyb.com is not installed
                   11763:        default:
                   11764:                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));
                   11765:                REG16(AX) = 0x01;
                   11766:                m_CF = 1;
                   11767:                break;
                   11768:        }
                   11769: }
                   11770: 
1.1       root     11771: inline void msdos_int_2fh_aeh()
                   11772: {
                   11773:        switch(REG8(AL)) {
                   11774:        case 0x00:
1.1.1.28  root     11775:                // FIXME: we need to check the given command line
                   11776:                REG8(AL) = 0x00; // the command should be executed as usual
                   11777: //             REG8(AL) = 0xff; // this command is a TSR extension to COMMAND.COM
1.1       root     11778:                break;
                   11779:        case 0x01:
                   11780:                {
                   11781:                        char command[MAX_PATH];
                   11782:                        memset(command, 0, sizeof(command));
1.1.1.3   root     11783:                        memcpy(command, mem + SREG_BASE(DS) + REG16(SI) + 1, mem[SREG_BASE(DS) + REG16(SI)]);
1.1       root     11784:                        
                   11785:                        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   11786:                        param->env_seg = 0;
                   11787:                        param->cmd_line.w.l = 44;
                   11788:                        param->cmd_line.w.h = (WORK_TOP >> 4);
                   11789:                        param->fcb1.w.l = 24;
                   11790:                        param->fcb1.w.h = (WORK_TOP >> 4);
                   11791:                        param->fcb2.w.l = 24;
                   11792:                        param->fcb2.w.h = (WORK_TOP >> 4);
                   11793:                        
                   11794:                        memset(mem + WORK_TOP + 24, 0x20, 20);
                   11795:                        
                   11796:                        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
1.1.1.3   root     11797:                        cmd_line->len = mem[SREG_BASE(DS) + REG16(BX) + 1];
                   11798:                        memcpy(cmd_line->cmd, mem + SREG_BASE(DS) + REG16(BX) + 2, cmd_line->len);
1.1       root     11799:                        cmd_line->cmd[cmd_line->len] = 0x0d;
                   11800:                        
1.1.1.28  root     11801:                        try {
                   11802:                                msdos_process_exec(command, param, 0);
                   11803:                        } catch(...) {
                   11804:                                fatalerror("failed to start '%s' by int 2Fh, AX=AE01h\n", command);
1.1       root     11805:                        }
                   11806:                }
                   11807:                break;
                   11808:        default:
1.1.1.22  root     11809:                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     11810:                REG16(AX) = 0x01;
1.1.1.3   root     11811:                m_CF = 1;
1.1       root     11812:                break;
                   11813:        }
                   11814: }
                   11815: 
1.1.1.34! root     11816: inline void msdos_int_2fh_b7h()
        !          11817: {
        !          11818:        switch(REG8(AL)) {
        !          11819:        case 0x00:
        !          11820:                // APPEND is not installed
        !          11821: //             REG8(AL) = 0x00;
        !          11822:                break;
        !          11823:        case 0x07:
        !          11824:                // COMMAND.COM calls this service without checking APPEND is installed
        !          11825:                break;
        !          11826:        default:
        !          11827:                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));
        !          11828:                REG16(AX) = 0x01;
        !          11829:                m_CF = 1;
        !          11830:                break;
        !          11831:        }
        !          11832: }
        !          11833: 
1.1.1.24  root     11834: inline void msdos_int_33h_0000h()
                   11835: {
                   11836:        REG16(AX) = 0xffff; // hardware/driver installed
                   11837:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   11838: }
                   11839: 
                   11840: inline void msdos_int_33h_0001h()
                   11841: {
1.1.1.34! root     11842:        if(mouse.hidden > 0) {
        !          11843:                mouse.hidden--;
        !          11844:        }
        !          11845:        if(mouse.hidden == 0) {
1.1.1.24  root     11846:                if(!(dwConsoleMode & ENABLE_MOUSE_INPUT)) {
                   11847:                        SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode | ENABLE_MOUSE_INPUT);
                   11848:                }
                   11849:                pic[1].imr &= ~0x10;    // enable irq12
                   11850:        }
                   11851: }
                   11852: 
                   11853: inline void msdos_int_33h_0002h()
                   11854: {
1.1.1.34! root     11855:        mouse.hidden++;
        !          11856: //     SetConsoleMode(GetStdHandle(STD_INPUT_HANDLE), dwConsoleMode & ~ENABLE_MOUSE_INPUT);
        !          11857:        pic[1].imr |= 0x10;     // enable irq12
1.1.1.24  root     11858: }
                   11859: 
                   11860: inline void msdos_int_33h_0003h()
                   11861: {
1.1.1.34! root     11862: //     if(mouse.hidden > 0) {
        !          11863:                update_console_input();
        !          11864: //     }
1.1.1.24  root     11865:        REG16(BX) = mouse.get_buttons();
1.1.1.34! root     11866:        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
        !          11867:        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
        !          11868: }
        !          11869: 
        !          11870: inline void msdos_int_33h_0004h()
        !          11871: {
        !          11872:        mouse.position.x = REG16(CX);
        !          11873:        mouse.position.x = REG16(DX);
1.1.1.24  root     11874: }
                   11875: 
                   11876: inline void msdos_int_33h_0005h()
                   11877: {
1.1.1.34! root     11878: //     if(mouse.hidden > 0) {
        !          11879:                update_console_input();
        !          11880: //     }
1.1.1.24  root     11881:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   11882:                int idx = REG16(BX);
1.1.1.34! root     11883:                REG16(BX) = min(mouse.buttons[idx].pressed_times, 0x7fff);
        !          11884:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].pressed_position.x));
        !          11885:                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     11886:                mouse.buttons[idx].pressed_times = 0;
                   11887:        } else {
                   11888:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   11889:        }
                   11890:        REG16(AX) = mouse.get_buttons();
                   11891: }
                   11892: 
                   11893: inline void msdos_int_33h_0006h()
                   11894: {
1.1.1.34! root     11895: //     if(mouse.hidden > 0) {
        !          11896:                update_console_input();
        !          11897: //     }
1.1.1.24  root     11898:        if(REG16(BX) < MAX_MOUSE_BUTTONS) {
                   11899:                int idx = REG16(BX);
1.1.1.34! root     11900:                REG16(BX) = min(mouse.buttons[idx].released_times, 0x7fff);
        !          11901:                REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.buttons[idx].released_position.x));
        !          11902:                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     11903:                mouse.buttons[idx].released_times = 0;
                   11904:        } else {
                   11905:                REG16(BX) = REG16(CX) = REG16(DX) = 0x0000;
                   11906:        }
                   11907:        REG16(AX) = mouse.get_buttons();
                   11908: }
                   11909: 
                   11910: inline void msdos_int_33h_0007h()
                   11911: {
                   11912:        mouse.min_position.x = min(REG16(CX), REG16(DX));
                   11913:        mouse.max_position.x = max(REG16(CX), REG16(DX));
                   11914: }
                   11915: 
                   11916: inline void msdos_int_33h_0008h()
                   11917: {
                   11918:        mouse.min_position.y = min(REG16(CX), REG16(DX));
                   11919:        mouse.max_position.y = max(REG16(CX), REG16(DX));
                   11920: }
                   11921: 
                   11922: inline void msdos_int_33h_0009h()
                   11923: {
                   11924:        mouse.hot_spot[0] = REG16(BX);
                   11925:        mouse.hot_spot[1] = REG16(CX);
                   11926: }
                   11927: 
                   11928: inline void msdos_int_33h_000bh()
                   11929: {
1.1.1.34! root     11930: //     if(mouse.hidden > 0) {
        !          11931:                update_console_input();
        !          11932: //     }
1.1.1.24  root     11933:        int dx = (mouse.position.x - mouse.prev_position.x) * mouse.mickey.x / 8;
                   11934:        int dy = (mouse.position.y - mouse.prev_position.y) * mouse.mickey.y / 8;
                   11935:        mouse.prev_position.x = mouse.position.x;
                   11936:        mouse.prev_position.y = mouse.position.y;
                   11937:        REG16(CX) = dx;
                   11938:        REG16(DX) = dy;
                   11939: }
                   11940: 
                   11941: inline void msdos_int_33h_000ch()
                   11942: {
                   11943:        mouse.call_mask = REG16(CX);
                   11944:        mouse.call_addr.w.l = REG16(DX);
                   11945:        mouse.call_addr.w.h = SREG(ES);
                   11946: }
                   11947: 
                   11948: inline void msdos_int_33h_000fh()
                   11949: {
                   11950:        mouse.mickey.x = REG16(CX);
                   11951:        mouse.mickey.y = REG16(DX);
                   11952: }
                   11953: 
                   11954: inline void msdos_int_33h_0011h()
                   11955: {
                   11956:        REG16(AX) = 0xffff;
                   11957:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   11958: }
                   11959: 
                   11960: inline void msdos_int_33h_0014h()
                   11961: {
                   11962:        UINT16 old_mask = mouse.call_mask;
                   11963:        UINT16 old_ofs = mouse.call_addr.w.l;
                   11964:        UINT16 old_seg = mouse.call_addr.w.h;
                   11965:        
                   11966:        mouse.call_mask = REG16(CX);
                   11967:        mouse.call_addr.w.l = REG16(DX);
                   11968:        mouse.call_addr.w.h = SREG(ES);
                   11969:        
                   11970:        REG16(CX) = old_mask;
                   11971:        REG16(DX) = old_ofs;
                   11972:        SREG(ES) = old_seg;
                   11973:        i386_load_segment_descriptor(ES);
                   11974: }
                   11975: 
                   11976: inline void msdos_int_33h_0015h()
                   11977: {
                   11978:        REG16(BX) = sizeof(mouse);
                   11979: }
                   11980: 
                   11981: inline void msdos_int_33h_0016h()
                   11982: {
                   11983:        memcpy(mem + SREG_BASE(ES) + REG16(DX), &mouse, sizeof(mouse));
                   11984: }
                   11985: 
                   11986: inline void msdos_int_33h_0017h()
                   11987: {
                   11988:        memcpy(&mouse, mem + SREG_BASE(ES) + REG16(DX), sizeof(mouse));
                   11989: }
                   11990: 
                   11991: inline void msdos_int_33h_001ah()
                   11992: {
                   11993:        mouse.sensitivity[0] = REG16(BX);
                   11994:        mouse.sensitivity[1] = REG16(CX);
                   11995:        mouse.sensitivity[2] = REG16(DX);
                   11996: }
                   11997: 
                   11998: inline void msdos_int_33h_001bh()
                   11999: {
                   12000:        REG16(BX) = mouse.sensitivity[0];
                   12001:        REG16(CX) = mouse.sensitivity[1];
                   12002:        REG16(DX) = mouse.sensitivity[2];
                   12003: }
                   12004: 
                   12005: inline void msdos_int_33h_001dh()
                   12006: {
                   12007:        mouse.display_page = REG16(BX);
                   12008: }
                   12009: 
                   12010: inline void msdos_int_33h_001eh()
                   12011: {
                   12012:        REG16(BX) = mouse.display_page;
                   12013: }
                   12014: 
1.1.1.34! root     12015: inline void msdos_int_33h_001fh()
        !          12016: {
        !          12017:        // from DOSBox
        !          12018:        REG16(BX) = 0x0000;
        !          12019:        SREG(ES) = 0x0000;
        !          12020:        i386_load_segment_descriptor(ES);
        !          12021:        mouse.enabled = false;
        !          12022:        mouse.old_hidden = mouse.hidden;
        !          12023:        mouse.hidden = 1;
        !          12024: }
        !          12025: 
        !          12026: inline void msdos_int_33h_0020h()
        !          12027: {
        !          12028:        // from DOSBox
        !          12029:        mouse.enabled = true;
        !          12030:        mouse.hidden = mouse.old_hidden;
        !          12031: }
        !          12032: 
1.1.1.24  root     12033: inline void msdos_int_33h_0021h()
                   12034: {
                   12035:        REG16(AX) = 0xffff;
                   12036:        REG16(BX) = MAX_MOUSE_BUTTONS;
                   12037: }
                   12038: 
                   12039: inline void msdos_int_33h_0022h()
                   12040: {
                   12041:        mouse.language = REG16(BX);
                   12042: }
                   12043: 
                   12044: inline void msdos_int_33h_0023h()
                   12045: {
                   12046:        REG16(BX) = mouse.language;
                   12047: }
                   12048: 
                   12049: inline void msdos_int_33h_0024h()
                   12050: {
                   12051:        REG16(BX) = 0x0805; // V8.05
                   12052:        REG16(CX) = 0x0400; // PS/2
                   12053: }
                   12054: 
                   12055: inline void msdos_int_33h_0026h()
                   12056: {
                   12057:        REG16(BX) = 0x0000;
                   12058:        REG16(CX) = mouse.max_position.x;
                   12059:        REG16(DX) = mouse.max_position.y;
                   12060: }
                   12061: 
                   12062: inline void msdos_int_33h_002ah()
                   12063: {
1.1.1.34! root     12064:        REG16(AX) = -mouse.hidden;
1.1.1.24  root     12065:        REG16(BX) = mouse.hot_spot[0];
                   12066:        REG16(CX) = mouse.hot_spot[1];
                   12067:        REG16(DX) = 4; // PS/2
                   12068: }
                   12069: 
                   12070: inline void msdos_int_33h_0031h()
                   12071: {
                   12072:        REG16(AX) = mouse.min_position.x;
                   12073:        REG16(BX) = mouse.min_position.y;
                   12074:        REG16(CX) = mouse.max_position.x;
                   12075:        REG16(DX) = mouse.max_position.y;
                   12076: }
                   12077: 
                   12078: inline void msdos_int_33h_0032h()
                   12079: {
                   12080:        REG16(AX) = 0;
                   12081: //     REG16(AX) |= 0x8000; // 0025h
                   12082:        REG16(AX) |= 0x4000; // 0026h
                   12083: //     REG16(AX) |= 0x2000; // 0027h
                   12084: //     REG16(AX) |= 0x1000; // 0028h
                   12085: //     REG16(AX) |= 0x0800; // 0029h
                   12086:        REG16(AX) |= 0x0400; // 002ah
                   12087: //     REG16(AX) |= 0x0200; // 002bh
                   12088: //     REG16(AX) |= 0x0100; // 002ch
                   12089: //     REG16(AX) |= 0x0080; // 002dh
                   12090: //     REG16(AX) |= 0x0040; // 002eh
                   12091:        REG16(AX) |= 0x0020; // 002fh
                   12092: //     REG16(AX) |= 0x0010; // 0030h
                   12093:        REG16(AX) |= 0x0008; // 0031h
                   12094:        REG16(AX) |= 0x0004; // 0032h
                   12095: //     REG16(AX) |= 0x0002; // 0033h
                   12096: //     REG16(AX) |= 0x0001; // 0034h
                   12097: }
                   12098: 
1.1.1.19  root     12099: inline void msdos_int_67h_40h()
                   12100: {
                   12101:        if(!support_ems) {
                   12102:                REG8(AH) = 0x84;
                   12103:        } else {
                   12104:                REG8(AH) = 0x00;
                   12105:        }
                   12106: }
                   12107: 
                   12108: inline void msdos_int_67h_41h()
                   12109: {
                   12110:        if(!support_ems) {
                   12111:                REG8(AH) = 0x84;
                   12112:        } else {
                   12113:                REG8(AH) = 0x00;
                   12114:                REG16(BX) = EMS_TOP >> 4;
                   12115:        }
                   12116: }
                   12117: 
                   12118: inline void msdos_int_67h_42h()
                   12119: {
                   12120:        if(!support_ems) {
                   12121:                REG8(AH) = 0x84;
                   12122:        } else {
                   12123:                REG8(AH) = 0x00;
                   12124:                REG16(BX) = free_ems_pages;
                   12125:                REG16(DX) = MAX_EMS_PAGES;
                   12126:        }
                   12127: }
                   12128: 
                   12129: inline void msdos_int_67h_43h()
                   12130: {
                   12131:        if(!support_ems) {
                   12132:                REG8(AH) = 0x84;
                   12133:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   12134:                REG8(AH) = 0x87;
                   12135:        } else if(REG16(BX) > free_ems_pages) {
                   12136:                REG8(AH) = 0x88;
                   12137:        } else if(REG16(BX) == 0) {
                   12138:                REG8(AH) = 0x89;
                   12139:        } else {
1.1.1.31  root     12140:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     12141:                        if(!ems_handles[i].allocated) {
                   12142:                                ems_allocate_pages(i, REG16(BX));
                   12143:                                REG8(AH) = 0x00;
                   12144:                                REG16(DX) = i;
                   12145:                                return;
                   12146:                        }
                   12147:                }
                   12148:                REG8(AH) = 0x85;
                   12149:        }
                   12150: }
                   12151: 
                   12152: inline void msdos_int_67h_44h()
                   12153: {
                   12154:        if(!support_ems) {
                   12155:                REG8(AH) = 0x84;
1.1.1.31  root     12156:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     12157:                REG8(AH) = 0x83;
                   12158:        } else if(!(REG16(BX) == 0xffff || REG16(BX) < ems_handles[REG16(DX)].pages)) {
                   12159:                REG8(AH) = 0x8a;
                   12160: //     } else if(!(REG8(AL) < 4)) {
                   12161: //             REG8(AH) = 0x8b;
                   12162:        } else if(REG16(BX) == 0xffff) {
                   12163:                ems_unmap_page(REG8(AL) & 3);
                   12164:                REG8(AH) = 0x00;
                   12165:        } else {
                   12166:                ems_map_page(REG8(AL) & 3, REG16(DX), REG16(BX));
                   12167:                REG8(AH) = 0x00;
                   12168:        }
                   12169: }
                   12170: 
                   12171: inline void msdos_int_67h_45h()
                   12172: {
                   12173:        if(!support_ems) {
                   12174:                REG8(AH) = 0x84;
1.1.1.31  root     12175:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     12176:                REG8(AH) = 0x83;
                   12177:        } else {
                   12178:                ems_release_pages(REG16(DX));
                   12179:                REG8(AH) = 0x00;
                   12180:        }
                   12181: }
                   12182: 
                   12183: inline void msdos_int_67h_46h()
                   12184: {
                   12185:        if(!support_ems) {
                   12186:                REG8(AH) = 0x84;
                   12187:        } else {
1.1.1.29  root     12188: //             REG16(AX) = 0x0032; // EMS 3.2
                   12189:                REG16(AX) = 0x0040; // EMS 4.0
1.1.1.19  root     12190:        }
                   12191: }
                   12192: 
                   12193: inline void msdos_int_67h_47h()
                   12194: {
                   12195:        // NOTE: the map data should be stored in the specified ems page, not process data
                   12196:        process_t *process = msdos_process_info_get(current_psp);
                   12197:        
                   12198:        if(!support_ems) {
                   12199:                REG8(AH) = 0x84;
1.1.1.31  root     12200: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     12201: //             REG8(AH) = 0x83;
                   12202:        } else if(process->ems_pages_stored) {
                   12203:                REG8(AH) = 0x8d;
                   12204:        } else {
                   12205:                for(int i = 0; i < 4; i++) {
                   12206:                        process->ems_pages[i].handle = ems_pages[i].handle;
                   12207:                        process->ems_pages[i].page   = ems_pages[i].page;
                   12208:                        process->ems_pages[i].mapped = ems_pages[i].mapped;
                   12209:                }
                   12210:                process->ems_pages_stored = true;
                   12211:                REG8(AH) = 0x00;
                   12212:        }
                   12213: }
                   12214: 
                   12215: inline void msdos_int_67h_48h()
                   12216: {
                   12217:        // NOTE: the map data should be restored from the specified ems page, not process data
                   12218:        process_t *process = msdos_process_info_get(current_psp);
                   12219:        
                   12220:        if(!support_ems) {
                   12221:                REG8(AH) = 0x84;
1.1.1.31  root     12222: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     12223: //             REG8(AH) = 0x83;
                   12224:        } else if(!process->ems_pages_stored) {
                   12225:                REG8(AH) = 0x8e;
                   12226:        } else {
                   12227:                for(int i = 0; i < 4; i++) {
                   12228:                        if(process->ems_pages[i].mapped) {
                   12229:                                ems_map_page(i, process->ems_pages[i].handle, process->ems_pages[i].page);
                   12230:                        } else {
                   12231:                                ems_unmap_page(i);
                   12232:                        }
                   12233:                }
                   12234:                process->ems_pages_stored = false;
                   12235:                REG8(AH) = 0x00;
                   12236:        }
                   12237: }
                   12238: 
                   12239: inline void msdos_int_67h_4bh()
                   12240: {
                   12241:        if(!support_ems) {
                   12242:                REG8(AH) = 0x84;
                   12243:        } else {
                   12244:                REG8(AH) = 0x00;
                   12245:                REG16(BX) = 0;
1.1.1.31  root     12246:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     12247:                        if(ems_handles[i].allocated) {
                   12248:                                REG16(BX)++;
                   12249:                        }
                   12250:                }
                   12251:        }
                   12252: }
                   12253: 
                   12254: inline void msdos_int_67h_4ch()
                   12255: {
                   12256:        if(!support_ems) {
                   12257:                REG8(AH) = 0x84;
1.1.1.31  root     12258:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     12259:                REG8(AH) = 0x83;
                   12260:        } else {
                   12261:                REG8(AH) = 0x00;
                   12262:                REG16(BX) = ems_handles[REG16(DX)].pages;
                   12263:        }
                   12264: }
                   12265: 
                   12266: inline void msdos_int_67h_4dh()
                   12267: {
                   12268:        if(!support_ems) {
                   12269:                REG8(AH) = 0x84;
                   12270:        } else {
                   12271:                REG8(AH) = 0x00;
                   12272:                REG16(BX) = 0;
1.1.1.31  root     12273:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     12274:                        if(ems_handles[i].allocated) {
                   12275:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 0) = i;
                   12276:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * REG16(BX) + 2) = ems_handles[i].pages;
                   12277:                                REG16(BX)++;
                   12278:                        }
                   12279:                }
                   12280:        }
                   12281: }
                   12282: 
1.1.1.20  root     12283: inline void msdos_int_67h_4eh()
                   12284: {
                   12285:        if(!support_ems) {
                   12286:                REG8(AH) = 0x84;
                   12287:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   12288:                if(REG8(AL) == 0x00 || REG8(AL) == 0x02) {
                   12289:                        // save page map
                   12290:                        for(int i = 0; i < 4; i++) {
                   12291:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = ems_pages[i].mapped ? ems_pages[i].handle : 0xffff;
                   12292:                                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = ems_pages[i].mapped ? ems_pages[i].page   : 0xffff;
                   12293:                        }
                   12294:                }
                   12295:                if(REG8(AL) == 0x01 || REG8(AL) == 0x02) {
                   12296:                        // restore page map
                   12297:                        for(int i = 0; i < 4; i++) {
                   12298:                                UINT16 handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   12299:                                UINT16 page   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   12300:                                
1.1.1.31  root     12301:                                if(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated && page < ems_handles[handle].pages) {
1.1.1.20  root     12302:                                        ems_map_page(i, handle, page);
                   12303:                                } else {
                   12304:                                        ems_unmap_page(i);
                   12305:                                }
                   12306:                        }
                   12307:                }
                   12308:                REG8(AH) = 0x00;
                   12309:        } else if(REG8(AL) == 0x03) {
                   12310:                REG8(AH) = 0x00;
1.1.1.21  root     12311:                REG8(AL) = 4 * 4;
                   12312:        } else {
1.1.1.22  root     12313:                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     12314:                REG8(AH) = 0x8f;
                   12315:        }
                   12316: }
                   12317: 
                   12318: inline void msdos_int_67h_4fh()
                   12319: {
                   12320:        if(!support_ems) {
                   12321:                REG8(AH) = 0x84;
                   12322:        } else if(REG8(AL) == 0x00) {
                   12323:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   12324:                
                   12325:                *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI)) = count;
                   12326:                for(int i = 0; i < count; i++) {
                   12327:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 2 * i);
                   12328:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   12329:                        
                   12330: //                     if(!(physical < 4)) {
                   12331: //                             REG8(AH) = 0x8b;
                   12332: //                             return;
                   12333: //                     }
                   12334:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 0) = segment;
                   12335:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 2) = ems_pages[physical & 3].handle;
                   12336:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 2 + 6 * i + 4) = ems_pages[physical & 3].mapped ? ems_pages[physical & 3].page : 0xffff;
                   12337:                }
                   12338:                REG8(AH) = 0x00;
                   12339:        } else if(REG8(AL) == 0x01) {
                   12340:                int count = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI));
                   12341:                
                   12342:                for(int i = 0; i < count; i++) {
                   12343:                        UINT16 segment  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 0);
                   12344:                        UINT16 physical = ((segment << 4) - EMS_TOP) / 0x4000;
                   12345:                        UINT16 handle   = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 2);
                   12346:                        UINT16 logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 2 + 6 * i + 4);
                   12347:                        
                   12348: //                     if(!(physical < 4)) {
                   12349: //                             REG8(AH) = 0x8b;
                   12350: //                             return;
                   12351: //                     } else
1.1.1.31  root     12352:                        if(!(handle >= 1 && handle <= MAX_EMS_HANDLES && ems_handles[handle].allocated)) {
1.1.1.21  root     12353:                                REG8(AH) = 0x83;
                   12354:                                return;
                   12355:                        } else if(logical == 0xffff) {
                   12356:                                ems_unmap_page(physical & 3);
                   12357:                        } else if(logical < ems_handles[handle].pages) {
                   12358:                                ems_map_page(physical & 3, handle, logical);
                   12359:                        } else {
                   12360:                                REG8(AH) = 0x8a;
                   12361:                                return;
                   12362:                        }
                   12363:                }
                   12364:                REG8(AH) = 0x00;
                   12365:        } else if(REG8(AL) == 0x02) {
                   12366:                REG8(AH) = 0x00;
                   12367:                REG8(AL) = 2 + REG16(BX) * 6;
1.1.1.20  root     12368:        } else {
1.1.1.22  root     12369:                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     12370:                REG8(AH) = 0x8f;
                   12371:        }
                   12372: }
                   12373: 
                   12374: inline void msdos_int_67h_50h()
                   12375: {
                   12376:        if(!support_ems) {
                   12377:                REG8(AH) = 0x84;
1.1.1.31  root     12378:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.20  root     12379:                REG8(AH) = 0x83;
                   12380:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   12381:                for(int i = 0; i < REG16(CX); i++) {
                   12382:                        int logical  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 0);
                   12383:                        int physical = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 4 * i + 2);
                   12384:                        
                   12385:                        if(REG8(AL) == 0x01) {
                   12386:                                physical = ((physical << 4) - EMS_TOP) / 0x4000;
                   12387:                        }
                   12388: //                     if(!(physical < 4)) {
                   12389: //                             REG8(AH) = 0x8b;
                   12390: //                             return;
                   12391: //                     } else
                   12392:                        if(logical == 0xffff) {
                   12393:                                ems_unmap_page(physical & 3);
                   12394:                        } else if(logical < ems_handles[REG16(DX)].pages) {
                   12395:                                ems_map_page(physical & 3, REG16(DX), logical);
                   12396:                        } else {
                   12397:                                REG8(AH) = 0x8a;
                   12398:                                return;
                   12399:                        }
                   12400:                }
                   12401:                REG8(AH) = 0x00;
                   12402:        } else {
1.1.1.22  root     12403:                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     12404:                REG8(AH) = 0x8f;
                   12405:        }
                   12406: }
                   12407: 
1.1.1.19  root     12408: inline void msdos_int_67h_51h()
                   12409: {
                   12410:        if(!support_ems) {
                   12411:                REG8(AH) = 0x84;
1.1.1.31  root     12412:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     12413:                REG8(AH) = 0x83;
                   12414:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   12415:                REG8(AH) = 0x87;
                   12416:        } else if(REG16(BX) > free_ems_pages + ems_handles[REG16(DX)].pages) {
                   12417:                REG8(AH) = 0x88;
                   12418:        } else {
                   12419:                ems_reallocate_pages(REG16(DX), REG16(BX));
                   12420:                REG8(AH) = 0x00;
                   12421:        }
                   12422: }
                   12423: 
1.1.1.20  root     12424: inline void msdos_int_67h_52h()
                   12425: {
                   12426:        if(!support_ems) {
                   12427:                REG8(AH) = 0x84;
1.1.1.31  root     12428: //     } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
                   12429: //             REG8(AH) = 0x83;
1.1.1.20  root     12430:        } else if(REG8(AL) == 0x00) {
                   12431:                REG8(AL) = 0x00; // handle is volatile
                   12432:                REG8(AH) = 0x00;
                   12433:        } else if(REG8(AL) == 0x01) {
                   12434:                if(REG8(BL) == 0x00) {
                   12435:                        REG8(AH) = 0x00;
                   12436:                } else {
                   12437:                        REG8(AH) = 0x90; // undefined attribute type
                   12438:                }
                   12439:        } else if(REG8(AL) == 0x02) {
                   12440:                REG8(AL) = 0x00; // only volatile handles supported
                   12441:                REG8(AH) = 0x00;
                   12442:        } else {
1.1.1.22  root     12443:                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     12444:                REG8(AH) = 0x8f;
                   12445:        }
                   12446: }
                   12447: 
1.1.1.19  root     12448: inline void msdos_int_67h_53h()
                   12449: {
                   12450:        if(!support_ems) {
                   12451:                REG8(AH) = 0x84;
1.1.1.31  root     12452:        } else if(!(REG16(DX) >= 1 && REG16(DX) <= MAX_EMS_HANDLES && ems_handles[REG16(DX)].allocated)) {
1.1.1.19  root     12453:                REG8(AH) = 0x83;
                   12454:        } else if(REG8(AL) == 0x00) {
                   12455:                memcpy(mem + SREG_BASE(ES) + REG16(DI), ems_handles[REG16(DX)].name, 8);
                   12456:                REG8(AH) = 0x00;
                   12457:        } else if(REG8(AL) == 0x01) {
1.1.1.31  root     12458:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     12459:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   12460:                                REG8(AH) = 0xa1;
                   12461:                                return;
                   12462:                        }
                   12463:                }
                   12464:                REG8(AH) = 0x00;
                   12465:                memcpy(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8);
                   12466:        } else {
1.1.1.22  root     12467:                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     12468:                REG8(AH) = 0x8f;
1.1.1.19  root     12469:        }
                   12470: }
                   12471: 
                   12472: inline void msdos_int_67h_54h()
                   12473: {
                   12474:        if(!support_ems) {
                   12475:                REG8(AH) = 0x84;
                   12476:        } else if(REG8(AL) == 0x00) {
1.1.1.31  root     12477:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     12478:                        if(ems_handles[i].allocated) {
                   12479:                                memcpy(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, ems_handles[i].name, 10);
                   12480:                        } else {
                   12481:                                memset(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 2, 0, 10);
                   12482:                        }
                   12483:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 10 * i + 0) = i;
                   12484:                }
                   12485:                REG8(AH) = 0x00;
                   12486:                REG8(AL) = MAX_EMS_HANDLES;
                   12487:        } else if(REG8(AL) == 0x01) {
                   12488:                REG8(AH) = 0xa0; // not found
1.1.1.31  root     12489:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.19  root     12490:                        if(ems_handles[i].allocated && memcmp(ems_handles[REG16(DX)].name, mem + SREG_BASE(DS) + REG16(SI), 8) == 0) {
                   12491:                                REG8(AH) = 0x00;
                   12492:                                REG16(DX) = i;
                   12493:                                break;
                   12494:                        }
                   12495:                }
                   12496:        } else if(REG8(AL) == 0x02) {
                   12497:                REG8(AH) = 0x00;
                   12498:                REG16(BX) = MAX_EMS_HANDLES;
                   12499:        } else {
1.1.1.22  root     12500:                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     12501:                REG8(AH) = 0x8f;
                   12502:        }
                   12503: }
                   12504: 
                   12505: inline void msdos_int_67h_57h_tmp()
                   12506: {
                   12507:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   12508:        UINT8  src_type    = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   12509:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x05);
                   12510:        UINT16 src_ofs     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x07);
                   12511:        UINT16 src_seg     = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x09);
                   12512:        UINT8  dest_type   = *(UINT8  *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0b);
                   12513:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   12514:        UINT16 dest_ofs    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0e);
                   12515:        UINT16 dest_seg    = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x10);
                   12516:        
1.1.1.32  root     12517:        UINT8 *src_buffer = NULL, *dest_buffer = NULL;
1.1.1.20  root     12518:        UINT32 src_addr, dest_addr;
                   12519:        UINT32 src_addr_max, dest_addr_max;
                   12520:        
                   12521:        if(src_type == 0) {
                   12522:                src_buffer = mem;
                   12523:                src_addr = (src_seg << 4) + src_ofs;
                   12524:                src_addr_max = MAX_MEM;
                   12525:        } else {
1.1.1.31  root     12526:                if(!(src_handle >= 1 && src_handle <= MAX_EMS_HANDLES && ems_handles[src_handle].allocated)) {
1.1.1.20  root     12527:                        REG8(AH) = 0x83;
                   12528:                        return;
                   12529:                } else if(!(src_seg < ems_handles[src_handle].pages)) {
                   12530:                        REG8(AH) = 0x8a;
                   12531:                        return;
                   12532:                }
1.1.1.32  root     12533:                if(ems_handles[src_handle].buffer != NULL) {
                   12534:                        src_buffer = ems_handles[src_handle].buffer + 0x4000 * src_seg;
                   12535:                }
1.1.1.20  root     12536:                src_addr = src_ofs;
1.1.1.32  root     12537:                src_addr_max = 0x4000 * (ems_handles[src_handle].pages - src_seg);
1.1.1.20  root     12538:        }
                   12539:        if(dest_type == 0) {
                   12540:                dest_buffer = mem;
                   12541:                dest_addr = (dest_seg << 4) + dest_ofs;
                   12542:                dest_addr_max = MAX_MEM;
                   12543:        } else {
1.1.1.31  root     12544:                if(!(dest_handle >= 1 && dest_handle <= MAX_EMS_HANDLES && ems_handles[dest_handle].allocated)) {
1.1.1.20  root     12545:                        REG8(AH) = 0x83;
                   12546:                        return;
                   12547:                } else if(!(dest_seg < ems_handles[dest_handle].pages)) {
                   12548:                        REG8(AH) = 0x8a;
                   12549:                        return;
                   12550:                }
1.1.1.32  root     12551:                if(ems_handles[dest_handle].buffer != NULL) {
                   12552:                        dest_buffer = ems_handles[dest_handle].buffer + 0x4000 * dest_seg;
                   12553:                }
1.1.1.20  root     12554:                dest_addr = dest_ofs;
1.1.1.32  root     12555:                dest_addr_max = 0x4000 * (ems_handles[dest_handle].pages - dest_seg);
1.1.1.20  root     12556:        }
1.1.1.32  root     12557:        if(src_buffer != NULL && dest_buffer != NULL) {
                   12558:                for(int i = 0; i < copy_length; i++) {
                   12559:                        if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   12560:                                if(REG8(AL) == 0x00) {
                   12561:                                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   12562:                                } else if(REG8(AL) == 0x01) {
                   12563:                                        UINT8 tmp = dest_buffer[dest_addr];
                   12564:                                        dest_buffer[dest_addr++] = src_buffer[src_addr];
                   12565:                                        src_buffer[src_addr++] = tmp;
                   12566:                                }
                   12567:                        } else {
                   12568:                                REG8(AH) = 0x93;
                   12569:                                return;
1.1.1.20  root     12570:                        }
                   12571:                }
1.1.1.32  root     12572:                REG8(AH) = 0x00;
                   12573:        } else {
                   12574:                REG8(AH) = 0x80;
1.1.1.20  root     12575:        }
                   12576: }
                   12577: 
                   12578: inline void msdos_int_67h_57h()
                   12579: {
                   12580:        if(!support_ems) {
                   12581:                REG8(AH) = 0x84;
                   12582:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
                   12583:                struct {
                   12584:                        UINT16 handle;
                   12585:                        UINT16 page;
                   12586:                        bool mapped;
                   12587:                } tmp_pages[4];
                   12588:                
                   12589:                // unmap pages to copy memory data to ems buffer
                   12590:                for(int i = 0; i < 4; i++) {
                   12591:                        tmp_pages[i].handle = ems_pages[i].handle;
                   12592:                        tmp_pages[i].page   = ems_pages[i].page;
                   12593:                        tmp_pages[i].mapped = ems_pages[i].mapped;
                   12594:                        ems_unmap_page(i);
                   12595:                }
                   12596:                
                   12597:                // run move/exchange operation
                   12598:                msdos_int_67h_57h_tmp();
                   12599:                
                   12600:                // restore unmapped pages
                   12601:                for(int i = 0; i < 4; i++) {
                   12602:                        if(tmp_pages[i].mapped) {
                   12603:                                ems_map_page(i, tmp_pages[i].handle, tmp_pages[i].page);
                   12604:                        }
                   12605:                }
                   12606:        } else {
1.1.1.22  root     12607:                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     12608:                REG8(AH) = 0x8f;
                   12609:        }
                   12610: }
                   12611: 
                   12612: inline void msdos_int_67h_58h()
                   12613: {
                   12614:        if(!support_ems) {
                   12615:                REG8(AH) = 0x84;
                   12616:        } else if(REG8(AL) == 0x00) {
                   12617:                for(int i = 0; i < 4; i++) {
1.1.1.30  root     12618:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 0) = (EMS_TOP + 0x4000 * i) >> 4;
                   12619:                        *(UINT16 *)(mem + SREG_BASE(ES) + REG16(DI) + 4 * i + 2) = i;
1.1.1.20  root     12620:                }
                   12621:                REG8(AH) = 0x00;
                   12622:                REG16(CX) = 4;
                   12623:        } else if(REG8(AL) == 0x01) {
                   12624:                REG8(AH) = 0x00;
                   12625:                REG16(CX) = 4;
                   12626:        } else {
1.1.1.22  root     12627:                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     12628:                REG8(AH) = 0x8f;
                   12629:        }
                   12630: }
                   12631: 
                   12632: inline void msdos_int_67h_5ah()
                   12633: {
                   12634:        if(!support_ems) {
1.1.1.19  root     12635:                REG8(AH) = 0x84;
1.1.1.20  root     12636:        } else if(REG16(BX) > MAX_EMS_PAGES) {
                   12637:                REG8(AH) = 0x87;
                   12638:        } else if(REG16(BX) > free_ems_pages) {
                   12639:                REG8(AH) = 0x88;
                   12640: //     } else if(REG16(BX) == 0) {
                   12641: //             REG8(AH) = 0x89;
                   12642:        } else if(REG8(AL) == 0x00 || REG8(AL) == 0x01) {
1.1.1.31  root     12643:                for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.20  root     12644:                        if(!ems_handles[i].allocated) {
                   12645:                                ems_allocate_pages(i, REG16(BX));
                   12646:                                REG8(AH) = 0x00;
                   12647:                                REG16(DX) = i;
                   12648:                                return;
                   12649:                        }
                   12650:                }
                   12651:                REG8(AH) = 0x85;
                   12652:        } else {
1.1.1.22  root     12653:                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     12654:                REG8(AH) = 0x8f;
1.1.1.19  root     12655:        }
                   12656: }
                   12657: 
1.1.1.30  root     12658: inline void msdos_int_67h_deh()
                   12659: {
                   12660:        REG8(AH) = 0x84;
                   12661: }
                   12662: 
1.1.1.19  root     12663: #ifdef SUPPORT_XMS
                   12664: 
1.1.1.32  root     12665: void msdos_xms_init()
1.1.1.26  root     12666: {
1.1.1.30  root     12667:        emb_handle_top = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   12668:        emb_handle_top->address = EMB_TOP;
                   12669:        emb_handle_top->size_kb = (EMB_END - EMB_TOP) >> 10;
1.1.1.26  root     12670:        xms_a20_local_enb_count = 0;
                   12671: }
                   12672: 
1.1.1.32  root     12673: void msdos_xms_finish()
                   12674: {
                   12675:        msdos_xms_release();
                   12676: }
                   12677: 
                   12678: void msdos_xms_release()
1.1.1.30  root     12679: {
                   12680:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL;) {
                   12681:                emb_handle_t *next_handle = emb_handle->next;
                   12682:                free(emb_handle);
                   12683:                emb_handle = next_handle;
                   12684:        }
                   12685: }
                   12686: 
                   12687: emb_handle_t *msdos_xms_get_emb_handle(int handle)
                   12688: {
                   12689:        if(handle != 0) {
                   12690:                for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   12691:                        if(emb_handle->handle == handle) {
                   12692:                                return(emb_handle);
                   12693:                        }
                   12694:                }
                   12695:        }
                   12696:        return(NULL);
                   12697: }
                   12698: 
                   12699: int msdos_xms_get_unused_emb_handle_id()
                   12700: {
                   12701:        for(int handle = 1;; handle++) {
                   12702:                if(msdos_xms_get_emb_handle(handle) == NULL) {
                   12703:                        return(handle);
                   12704:                }
                   12705:        }
                   12706:        return(0);
                   12707: }
                   12708: 
                   12709: int msdos_xms_get_unused_emb_handle_count()
                   12710: {
                   12711:        int count = 64; //255;
                   12712:        
                   12713:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   12714:                if(emb_handle->handle != 0) {
                   12715:                        if(--count == 1) {
                   12716:                                break;
                   12717:                        }
                   12718:                }
                   12719:        }
                   12720:        return(count);
                   12721: }
                   12722: 
                   12723: void msdos_xms_split_emb_handle(emb_handle_t *emb_handle, int size_kb)
                   12724: {
                   12725:        if(emb_handle->size_kb > size_kb) {
                   12726:                emb_handle_t *new_handle = (emb_handle_t *)calloc(1, sizeof(emb_handle_t));
                   12727:                
                   12728:                new_handle->address = emb_handle->address + size_kb * 1024;
                   12729:                new_handle->size_kb = emb_handle->size_kb - size_kb;
                   12730:                emb_handle->size_kb = size_kb;
                   12731:                
                   12732:                new_handle->prev = emb_handle;
                   12733:                new_handle->next = emb_handle->next;
                   12734:                if(emb_handle->next != NULL) {
                   12735:                        emb_handle->next->prev = new_handle;
                   12736:                }
                   12737:                emb_handle->next = new_handle;
                   12738:        }
                   12739: }
                   12740: 
                   12741: void msdos_xms_combine_emb_handles(emb_handle_t *emb_handle)
                   12742: {
                   12743:        emb_handle_t *next_handle = emb_handle->next;
                   12744:        
                   12745:        if(next_handle != NULL) {
                   12746:                emb_handle->size_kb += next_handle->size_kb;
                   12747:                
                   12748:                if(next_handle->next != NULL) {
                   12749:                        next_handle->next->prev = emb_handle;
                   12750:                }
                   12751:                emb_handle->next = next_handle->next;
                   12752:                free(next_handle);
                   12753:        }
                   12754: }
                   12755: 
                   12756: emb_handle_t *msdos_xms_alloc_emb_handle(int size_kb)
                   12757: {
                   12758:        emb_handle_t *target_handle = NULL;
                   12759:        
                   12760:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   12761:                if(emb_handle->handle == 0 && emb_handle->size_kb >= size_kb) {
                   12762:                        if(target_handle == NULL || target_handle->size_kb > emb_handle->size_kb) {
                   12763:                                target_handle = emb_handle;
                   12764:                        }
                   12765:                }
                   12766:        }
                   12767:        if(target_handle != NULL) {
                   12768:                if(target_handle->size_kb > size_kb) {
                   12769:                        msdos_xms_split_emb_handle(target_handle, size_kb);
                   12770:                }
                   12771: //             target_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   12772:                return(target_handle);
                   12773:        }
                   12774:        return(NULL);
                   12775: }
                   12776: 
                   12777: void msdos_xms_free_emb_handle(emb_handle_t *emb_handle)
                   12778: {
                   12779:        emb_handle_t *prev_handle = emb_handle->prev;
                   12780:        emb_handle_t *next_handle = emb_handle->next;
                   12781:        
                   12782:        if(prev_handle != NULL && prev_handle->handle == 0) {
                   12783:                msdos_xms_combine_emb_handles(prev_handle);
                   12784:                emb_handle = prev_handle;
                   12785:        }
                   12786:        if(next_handle != NULL && next_handle->handle == 0) {
                   12787:                msdos_xms_combine_emb_handles(emb_handle);
                   12788:        }
                   12789:        emb_handle->handle = 0;
                   12790: }
                   12791: 
1.1.1.19  root     12792: inline void msdos_call_xms_00h()
                   12793: {
1.1.1.29  root     12794: #if defined(HAS_I386)
                   12795:        REG16(AX) = 0x0300; // V3.00 (XMS Version)
                   12796:        REG16(BX) = 0x035f; // V3.95 (Driver Revision)
                   12797: #else
                   12798:        REG16(AX) = 0x0200; // V2.00 (XMS Version)
                   12799:        REG16(BX) = 0x0270; // V2.70 (Driver Revision)
                   12800: #endif
                   12801: //     REG16(DX) = 0x0000; // HMA does not exist
                   12802:        REG16(DX) = 0x0001; // HMA does exist
1.1.1.19  root     12803: }
                   12804: 
                   12805: inline void msdos_call_xms_01h()
                   12806: {
1.1.1.29  root     12807:        if(REG8(AL) == 0x40) {
                   12808:                // HIMEM.SYS will fail function 01h with error code 91h if AL=40h and
                   12809:                // DX=KB free extended memory returned by last call of function 08h
                   12810:                REG16(AX) = 0x0000;
                   12811:                REG8(BL) = 0x91;
                   12812:                REG16(DX) = xms_dx_after_call_08h;
                   12813:        } else if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   12814:                REG16(AX) = 0x0000;
                   12815:                REG8(BL) = 0x81; // Vdisk was detected
                   12816: #ifdef SUPPORT_HMA
                   12817:        } else if(is_hma_used_by_int_2fh) {
                   12818:                REG16(AX) = 0x0000;
                   12819:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   12820:        } else if(is_hma_used_by_xms) {
                   12821:                REG16(AX) = 0x0000;
                   12822:                REG8(BL) = 0x91; // HMA is already in use
                   12823:        } else {
                   12824:                REG16(AX) = 0x0001;
                   12825:                is_hma_used_by_xms = true;
                   12826: #else
                   12827:        } else {
                   12828:                REG16(AX) = 0x0000;
                   12829:                REG8(BL) = 0x91; // HMA is already in use
                   12830: #endif
                   12831:        }
1.1.1.19  root     12832: }
                   12833: 
                   12834: inline void msdos_call_xms_02h()
                   12835: {
1.1.1.29  root     12836:        if(memcmp(mem + 0x100003, "VDISK", 5) == 0) {
                   12837:                REG16(AX) = 0x0000;
                   12838:                REG8(BL) = 0x81; // Vdisk was detected
                   12839: #ifdef SUPPORT_HMA
                   12840:        } else if(is_hma_used_by_int_2fh) {
                   12841:                REG16(AX) = 0x0000;
                   12842:                REG8(BL) = 0x90; // HMA does not exist or is not managed by XMS provider
                   12843:        } else if(!is_hma_used_by_xms) {
                   12844:                REG16(AX) = 0x0000;
                   12845:                REG8(BL) = 0x93; // HMA is not allocated
                   12846:        } else {
                   12847:                REG16(AX) = 0x0001;
                   12848:                is_hma_used_by_xms = false;
                   12849:                // restore first free mcb in high memory area
                   12850:                msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   12851: #else
                   12852:        } else {
                   12853:                REG16(AX) = 0x0000;
                   12854:                REG8(BL) = 0x91; // HMA is already in use
                   12855: #endif
                   12856:        }
1.1.1.19  root     12857: }
                   12858: 
                   12859: inline void msdos_call_xms_03h()
                   12860: {
                   12861:        i386_set_a20_line(1);
                   12862:        REG16(AX) = 0x0001;
                   12863:        REG8(BL) = 0x00;
                   12864: }
                   12865: 
                   12866: inline void msdos_call_xms_04h()
                   12867: {
1.1.1.21  root     12868:        i386_set_a20_line(0);
                   12869:        REG16(AX) = 0x0001;
                   12870:        REG8(BL) = 0x00;
1.1.1.19  root     12871: }
                   12872: 
                   12873: inline void msdos_call_xms_05h()
                   12874: {
                   12875:        i386_set_a20_line(1);
                   12876:        REG16(AX) = 0x0001;
                   12877:        REG8(BL) = 0x00;
1.1.1.21  root     12878:        xms_a20_local_enb_count++;
1.1.1.19  root     12879: }
                   12880: 
                   12881: void msdos_call_xms_06h()
                   12882: {
1.1.1.21  root     12883:        if(xms_a20_local_enb_count > 0) {
                   12884:                xms_a20_local_enb_count--;
                   12885:        }
                   12886:        if(xms_a20_local_enb_count == 0) {
1.1.1.19  root     12887:                i386_set_a20_line(0);
1.1.1.21  root     12888:        }
                   12889:        if((m_a20_mask >> 20) & 1) {
1.1.1.19  root     12890:                REG16(AX) = 0x0000;
                   12891:                REG8(BL) = 0x94;
1.1.1.21  root     12892:        } else {
                   12893:                REG16(AX) = 0x0001;
                   12894:                REG8(BL) = 0x00;
1.1.1.19  root     12895:        }
                   12896: }
                   12897: 
                   12898: inline void msdos_call_xms_07h()
                   12899: {
                   12900:        REG16(AX) = (m_a20_mask >> 20) & 1;
                   12901:        REG8(BL) = 0x00;
                   12902: }
                   12903: 
                   12904: inline void msdos_call_xms_08h()
                   12905: {
                   12906:        REG16(AX) = REG16(DX) = 0x0000;
                   12907:        
1.1.1.30  root     12908:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   12909:                if(emb_handle->handle == 0) {
                   12910:                        if(REG16(AX) < emb_handle->size_kb) {
                   12911:                                REG16(AX) = emb_handle->size_kb;
1.1.1.19  root     12912:                        }
1.1.1.30  root     12913:                        REG16(DX) += emb_handle->size_kb;
1.1.1.19  root     12914:                }
                   12915:        }
                   12916:        
                   12917:        if(REG16(AX) == 0 && REG16(DX) == 0) {
                   12918:                REG8(BL) = 0xa0;
                   12919:        } else {
                   12920:                REG8(BL) = 0x00;
                   12921:        }
1.1.1.29  root     12922:        xms_dx_after_call_08h = REG16(DX);
1.1.1.19  root     12923: }
                   12924: 
1.1.1.30  root     12925: void msdos_call_xms_09h(int size_kb)
1.1.1.19  root     12926: {
1.1.1.30  root     12927:        emb_handle_t *emb_handle = msdos_xms_alloc_emb_handle(size_kb);
                   12928:        
                   12929:        if(emb_handle != NULL) {
                   12930:                emb_handle->handle = msdos_xms_get_unused_emb_handle_id();
                   12931:                
                   12932:                REG16(AX) = 0x0001;
                   12933:                REG16(DX) = emb_handle->handle;
                   12934:                REG8(BL) = 0x00;
                   12935:        } else {
                   12936:                REG16(AX) = REG16(DX) = 0x0000;
                   12937:                REG8(BL) = 0xa0;
1.1.1.19  root     12938:        }
1.1.1.30  root     12939: }
                   12940: 
                   12941: inline void msdos_call_xms_09h()
                   12942: {
                   12943:        msdos_call_xms_09h(REG16(DX));
1.1.1.19  root     12944: }
                   12945: 
                   12946: inline void msdos_call_xms_0ah()
                   12947: {
1.1.1.30  root     12948:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   12949:        
                   12950:        if(emb_handle == NULL) {
1.1.1.19  root     12951:                REG16(AX) = 0x0000;
                   12952:                REG8(BL) = 0xa2;
1.1.1.30  root     12953:        } else if(emb_handle->lock > 0) {
1.1.1.19  root     12954:                REG16(AX) = 0x0000;
                   12955:                REG8(BL) = 0xab;
                   12956:        } else {
1.1.1.30  root     12957:                msdos_xms_free_emb_handle(emb_handle);
1.1.1.19  root     12958:                
                   12959:                REG16(AX) = 0x0001;
                   12960:                REG8(BL) = 0x00;
                   12961:        }
                   12962: }
                   12963: 
                   12964: inline void msdos_call_xms_0bh()
                   12965: {
                   12966:        UINT32 copy_length = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x00);
                   12967:        UINT16 src_handle  = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x04);
                   12968:        UINT32 src_addr    = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x06);
                   12969:        UINT16 dest_handle = *(UINT16 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0a);
                   12970:        UINT32 dest_addr   = *(UINT32 *)(mem + SREG_BASE(DS) + REG16(SI) + 0x0c);
                   12971:        
                   12972:        UINT8 *src_buffer, *dest_buffer;
                   12973:        UINT32 src_addr_max, dest_addr_max;
1.1.1.30  root     12974:        emb_handle_t *emb_handle;
1.1.1.19  root     12975:        
                   12976:        if(src_handle == 0) {
                   12977:                src_buffer = mem;
                   12978:                src_addr = (((src_addr >> 16) & 0xffff) << 4) + (src_addr & 0xffff);
                   12979:                src_addr_max = MAX_MEM;
1.1.1.30  root     12980: 
1.1.1.19  root     12981:        } else {
1.1.1.30  root     12982:                if((emb_handle = msdos_xms_get_emb_handle(src_handle)) == NULL) {
1.1.1.19  root     12983:                        REG16(AX) = 0x0000;
                   12984:                        REG8(BL) = 0xa3;
                   12985:                        return;
                   12986:                }
1.1.1.30  root     12987:                src_buffer = mem + emb_handle->address;
                   12988:                src_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     12989:        }
                   12990:        if(dest_handle == 0) {
                   12991:                dest_buffer = mem;
                   12992:                dest_addr = (((dest_addr >> 16) & 0xffff) << 4) + (dest_addr & 0xffff);
                   12993:                dest_addr_max = MAX_MEM;
                   12994:        } else {
1.1.1.30  root     12995:                if((emb_handle = msdos_xms_get_emb_handle(dest_handle)) == NULL) {
1.1.1.19  root     12996:                        REG16(AX) = 0x0000;
                   12997:                        REG8(BL) = 0xa5;
                   12998:                        return;
                   12999:                }
1.1.1.30  root     13000:                dest_buffer = mem + emb_handle->address;
                   13001:                dest_addr_max = emb_handle->size_kb * 1024;
1.1.1.19  root     13002:        }
                   13003:        for(int i = 0; i < copy_length; i++) {
                   13004:                if(src_addr < src_addr_max && dest_addr < dest_addr_max) {
                   13005:                        dest_buffer[dest_addr++] = src_buffer[src_addr++];
                   13006:                } else {
                   13007:                        break;
                   13008:                }
                   13009:        }
                   13010:        REG16(AX) = 0x0001;
                   13011:        REG8(BL) = 0x00;
                   13012: }
                   13013: 
                   13014: inline void msdos_call_xms_0ch()
                   13015: {
1.1.1.30  root     13016:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   13017:        
                   13018:        if(emb_handle == NULL) {
1.1.1.19  root     13019:                REG16(AX) = 0x0000;
                   13020:                REG8(BL) = 0xa2;
                   13021:        } else {
1.1.1.30  root     13022:                emb_handle->lock++;
1.1.1.19  root     13023:                REG16(AX) = 0x0001;
                   13024:                REG8(BL) = 0x00;
1.1.1.30  root     13025:                REG16(DX) = (emb_handle->address >> 16) & 0xffff;
                   13026:                REG16(BX) = (emb_handle->address      ) & 0xffff;
1.1.1.19  root     13027:        }
                   13028: }
                   13029: 
                   13030: inline void msdos_call_xms_0dh()
                   13031: {
1.1.1.30  root     13032:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   13033:        
                   13034:        if(emb_handle == NULL) {
1.1.1.19  root     13035:                REG16(AX) = 0x0000;
                   13036:                REG8(BL) = 0xa2;
1.1.1.30  root     13037:        } else if(!(emb_handle->lock > 0)) {
1.1.1.19  root     13038:                REG16(AX) = 0x0000;
                   13039:                REG8(BL) = 0xaa;
                   13040:        } else {
1.1.1.30  root     13041:                emb_handle->lock--;
1.1.1.19  root     13042:                REG16(AX) = 0x0001;
                   13043:                REG8(BL) = 0x00;
                   13044:        }
                   13045: }
                   13046: 
                   13047: inline void msdos_call_xms_0eh()
                   13048: {
1.1.1.30  root     13049:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   13050:        
                   13051:        if(emb_handle == NULL) {
1.1.1.19  root     13052:                REG16(AX) = 0x0000;
                   13053:                REG8(BL) = 0xa2;
                   13054:        } else {
                   13055:                REG16(AX) = 0x0001;
1.1.1.30  root     13056:                REG8(BH) = emb_handle->lock;
                   13057:                REG8(BL) = msdos_xms_get_unused_emb_handle_count();
                   13058:                REG16(DX) = emb_handle->size_kb;
1.1.1.19  root     13059:        }
                   13060: }
                   13061: 
1.1.1.30  root     13062: void msdos_call_xms_0fh(int size_kb)
1.1.1.19  root     13063: {
1.1.1.30  root     13064:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   13065:        
                   13066:        if(emb_handle == NULL) {
1.1.1.19  root     13067:                REG16(AX) = 0x0000;
                   13068:                REG8(BL) = 0xa2;
1.1.1.30  root     13069:        } else if(emb_handle->lock > 0) {
1.1.1.19  root     13070:                REG16(AX) = 0x0000;
                   13071:                REG8(BL) = 0xab;
                   13072:        } else {
1.1.1.30  root     13073:                if(emb_handle->size_kb < size_kb) {
                   13074:                        if(emb_handle->next != NULL && emb_handle->next->handle == 0 && (emb_handle->size_kb + emb_handle->next->size_kb) >= size_kb) {
                   13075:                                msdos_xms_combine_emb_handles(emb_handle);
                   13076:                                if(emb_handle->size_kb > size_kb) {
                   13077:                                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   13078:                                }
                   13079:                        } else {
                   13080:                                int old_handle = emb_handle->handle;
                   13081:                                int old_size_kb = emb_handle->size_kb;
                   13082:                                UINT8 *buffer = (UINT8 *)malloc(old_size_kb * 1024);
                   13083:                                
                   13084:                                memcpy(buffer, mem + emb_handle->address, old_size_kb * 1024);
                   13085:                                msdos_xms_free_emb_handle(emb_handle);
                   13086:                                
                   13087:                                if((emb_handle = msdos_xms_alloc_emb_handle(size_kb)) == NULL) {
                   13088:                                        emb_handle = msdos_xms_alloc_emb_handle(old_size_kb); // should be always successed
                   13089:                                }
                   13090:                                emb_handle->handle = old_handle;
                   13091:                                memcpy(mem + emb_handle->address, buffer, old_size_kb * 1024);
                   13092:                                free(buffer);
                   13093:                        }
                   13094:                } else if(emb_handle->size_kb > size_kb) {
                   13095:                        msdos_xms_split_emb_handle(emb_handle, size_kb);
                   13096:                }
                   13097:                if(emb_handle->size_kb != size_kb) {
                   13098:                        REG16(AX) = 0x0000;
                   13099:                        REG8(BL) = 0xa0;
                   13100:                } else {
                   13101:                        REG16(AX) = 0x0001;
                   13102:                        REG8(BL) = 0x00;
                   13103:                }
1.1.1.19  root     13104:        }
                   13105: }
                   13106: 
1.1.1.30  root     13107: inline void msdos_call_xms_0fh()
                   13108: {
                   13109:        msdos_call_xms_0fh(REG16(BX));
                   13110: }
                   13111: 
1.1.1.19  root     13112: inline void msdos_call_xms_10h()
                   13113: {
                   13114:        int seg;
                   13115:        
                   13116:        if((seg = msdos_mem_alloc(UMB_TOP >> 4, REG16(DX), 0)) != -1) {
                   13117:                REG16(AX) = 0x0001;
                   13118:                REG16(BX) = seg;
                   13119:        } else {
                   13120:                REG16(AX) = 0x0000;
                   13121:                REG8(BL) = 0xb0;
                   13122:                REG16(DX) = msdos_mem_get_free(UMB_TOP >> 4, 0);
                   13123:        }
                   13124: }
                   13125: 
                   13126: inline void msdos_call_xms_11h()
                   13127: {
                   13128:        int mcb_seg = REG16(DX) - 1;
                   13129:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   13130:        
                   13131:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   13132:                msdos_mem_free(REG16(DX));
                   13133:                REG16(AX) = 0x0001;
                   13134:                REG8(BL) = 0x00;
                   13135:        } else {
                   13136:                REG16(AX) = 0x0000;
                   13137:                REG8(BL) = 0xb2;
                   13138:        }
                   13139: }
                   13140: 
                   13141: inline void msdos_call_xms_12h()
                   13142: {
                   13143:        int mcb_seg = REG16(DX) - 1;
                   13144:        mcb_t *mcb = (mcb_t *)(mem + (mcb_seg << 4));
                   13145:        int max_paragraphs;
                   13146:        
                   13147:        if(mcb->mz == 'M' || mcb->mz == 'Z') {
                   13148:                if(!msdos_mem_realloc(REG16(DX), REG16(BX), &max_paragraphs)) {
                   13149:                        REG16(AX) = 0x0001;
                   13150:                        REG8(BL) = 0x00;
                   13151:                } else {
                   13152:                        REG16(AX) = 0x0000;
                   13153:                        REG8(BL) = 0xb0;
                   13154:                        REG16(DX) = max_paragraphs;
                   13155:                }
                   13156:        } else {
                   13157:                REG16(AX) = 0x0000;
                   13158:                REG8(BL) = 0xb2;
                   13159:        }
                   13160: }
                   13161: 
1.1.1.29  root     13162: #if defined(HAS_I386)
                   13163: 
                   13164: inline void msdos_call_xms_88h()
                   13165: {
                   13166:        REG32(EAX) = REG32(EDX) = 0x0000;
                   13167:        
1.1.1.30  root     13168:        for(emb_handle_t *emb_handle = emb_handle_top; emb_handle != NULL; emb_handle = emb_handle->next) {
                   13169:                if(emb_handle->handle == 0) {
                   13170:                        if(REG32(EAX) < emb_handle->size_kb) {
                   13171:                                REG32(EAX) = emb_handle->size_kb;
1.1.1.29  root     13172:                        }
1.1.1.30  root     13173:                        REG32(EDX) += emb_handle->size_kb;
1.1.1.29  root     13174:                }
                   13175:        }
                   13176:        
                   13177:        if(REG32(EAX) == 0 && REG32(EDX) == 0) {
                   13178:                REG8(BL) = 0xa0;
                   13179:        } else {
                   13180:                REG8(BL) = 0x00;
                   13181:        }
                   13182:        REG32(ECX) = EMB_END - 1;
                   13183: }
                   13184: 
                   13185: inline void msdos_call_xms_89h()
                   13186: {
1.1.1.30  root     13187:        msdos_call_xms_09h(REG32(EDX));
1.1.1.29  root     13188: }
                   13189: 
                   13190: inline void msdos_call_xms_8eh()
                   13191: {
1.1.1.30  root     13192:        emb_handle_t *emb_handle = msdos_xms_get_emb_handle(REG16(DX));
                   13193:        
                   13194:        if(emb_handle == NULL) {
1.1.1.29  root     13195:                REG16(AX) = 0x0000;
                   13196:                REG8(BL) = 0xa2;
                   13197:        } else {
                   13198:                REG16(AX) = 0x0001;
1.1.1.30  root     13199:                REG8(BH) = emb_handle->lock;
                   13200:                REG16(CX) = msdos_xms_get_unused_emb_handle_count();
                   13201:                REG32(EDX) = emb_handle->size_kb;
1.1.1.29  root     13202:        }
                   13203: }
                   13204: 
                   13205: inline void msdos_call_xms_8fh()
                   13206: {
1.1.1.30  root     13207:        msdos_call_xms_0fh(REG32(EBX));
1.1.1.29  root     13208: }
                   13209: 
                   13210: #endif
1.1.1.19  root     13211: #endif
                   13212: 
1.1.1.26  root     13213: UINT16 msdos_get_equipment()
                   13214: {
                   13215:        static UINT16 equip = 0;
                   13216:        
                   13217:        if(equip == 0) {
                   13218: #ifdef SUPPORT_FPU
                   13219:                equip |= (1 << 1);      // 80x87 coprocessor installed
                   13220: #endif
                   13221:                equip |= (1 << 2);      // pointing device installed (PS/2)
                   13222:                equip |= (2 << 4);      // initial video mode (80x25 color)
                   13223: //             equip |= (1 << 8);      // 0 if DMA installed
                   13224:                equip |= (2 << 9);      // number of serial ports
                   13225:                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     13226:                
                   13227:                // check only A: and B: if it is floppy drive
                   13228:                DWORD dwDrives = GetLogicalDrives();
                   13229:                int n = 0;
                   13230:                for(int i = 0; i < 2; i++) {
                   13231:                        if(dwDrives & (1 << i)) {
                   13232:                                char volume[] = "A:\\";
                   13233:                                volume[0] = 'A' + i;
                   13234:                                if(GetDriveType(volume) == DRIVE_REMOVABLE) {
                   13235:                                        n++;
                   13236:                                }
                   13237:                        }
                   13238:                }
                   13239:                if(n != 0) {
                   13240:                        equip |= (1 << 0);      // floppy disk(s) installed
                   13241:                        n--;
                   13242:                        equip |= (n << 6);      // number of floppies installed less 1
                   13243:                }
                   13244: //             if(joyGetNumDevs() != 0) {
                   13245: //                     equip |= (1 << 12);     // game port installed
                   13246: //             }
1.1.1.26  root     13247:        }
                   13248:        return(equip);
                   13249: }
                   13250: 
1.1       root     13251: void msdos_syscall(unsigned num)
                   13252: {
1.1.1.22  root     13253: #ifdef ENABLE_DEBUG_SYSCALL
                   13254:        if(num == 0x68) {
                   13255:                // dummy interrupt for EMS (int 67h)
1.1.1.33  root     13256:                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     13257:        } else if(num == 0x69) {
                   13258:                // dummy interrupt for XMS (call far)
1.1.1.33  root     13259:                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     13260:        } else if(num == 0x6a) {
                   13261:                // dummy interrupt for case map routine pointed in the country info
                   13262:        } else {
1.1.1.33  root     13263:                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     13264:        }
                   13265: #endif
1.1.1.33  root     13266:        ctrl_break_detected = ctrl_break_pressed = ctrl_c_pressed = false;
1.1.1.22  root     13267:        
1.1       root     13268:        switch(num) {
                   13269:        case 0x00:
1.1.1.28  root     13270:                try {
                   13271:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   13272:                        error("division by zero\n");
                   13273:                } catch(...) {
                   13274:                        fatalerror("division by zero detected, and failed to terminate current process\n");
                   13275:                }
1.1       root     13276:                break;
                   13277:        case 0x04:
1.1.1.28  root     13278:                try {
                   13279:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   13280:                        error("overflow\n");
                   13281:                } catch(...) {
                   13282:                        fatalerror("overflow detected, and failed to terminate current process\n");
                   13283:                }
1.1       root     13284:                break;
                   13285:        case 0x06:
                   13286:                // NOTE: ish.com has illegal instruction...
1.1.1.14  root     13287:                if(!ignore_illegal_insn) {
1.1.1.28  root     13288:                        try {
                   13289:                                msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   13290:                                error("illegal instruction\n");
                   13291:                        } catch(...) {
                   13292:                                fatalerror("illegal instruction detected, and failed to terminate current process\n");
                   13293:                        }
1.1.1.14  root     13294:                } else {
                   13295: #if defined(HAS_I386)
                   13296:                        m_eip++;
                   13297: #else
                   13298:                        m_pc++;
                   13299: #endif
                   13300:                }
1.1       root     13301:                break;
1.1.1.33  root     13302:        case 0x09:
                   13303:                // ctrl-break is pressed
                   13304:                if(raise_int_1bh) {
                   13305: #if defined(HAS_I386)
                   13306:                        m_ext = 0; // not an external interrupt
                   13307:                        i386_trap(0x1b, 1, 0);
                   13308:                        m_ext = 1;
                   13309: #else
                   13310:                        PREFIX86(_interrupt)(0x1b);
                   13311: #endif
                   13312:                        raise_int_1bh = false;
                   13313:                }
1.1.1.8   root     13314:        case 0x08:
1.1.1.14  root     13315: //             pcbios_irq0(); // this causes too slow emulation...
1.1.1.8   root     13316:        case 0x0b:
                   13317:        case 0x0c:
                   13318:        case 0x0d:
                   13319:        case 0x0e:
                   13320:        case 0x0f:
                   13321:                // EOI
                   13322:                pic[0].isr &= ~(1 << (num - 0x08));
                   13323:                pic_update();
                   13324:                break;
1.1       root     13325:        case 0x10:
                   13326:                // PC BIOS - Video
1.1.1.14  root     13327:                if(!restore_console_on_exit) {
1.1.1.15  root     13328:                        change_console_size(scr_width, scr_height);
1.1       root     13329:                }
1.1.1.3   root     13330:                m_CF = 0;
1.1       root     13331:                switch(REG8(AH)) {
1.1.1.16  root     13332:                case 0x00: pcbios_int_10h_00h(); break;
1.1       root     13333:                case 0x01: pcbios_int_10h_01h(); break;
                   13334:                case 0x02: pcbios_int_10h_02h(); break;
                   13335:                case 0x03: pcbios_int_10h_03h(); break;
                   13336:                case 0x05: pcbios_int_10h_05h(); break;
                   13337:                case 0x06: pcbios_int_10h_06h(); break;
                   13338:                case 0x07: pcbios_int_10h_07h(); break;
                   13339:                case 0x08: pcbios_int_10h_08h(); break;
                   13340:                case 0x09: pcbios_int_10h_09h(); break;
                   13341:                case 0x0a: pcbios_int_10h_0ah(); break;
                   13342:                case 0x0b: break;
                   13343:                case 0x0c: break;
                   13344:                case 0x0d: break;
                   13345:                case 0x0e: pcbios_int_10h_0eh(); break;
                   13346:                case 0x0f: pcbios_int_10h_0fh(); break;
                   13347:                case 0x10: break;
1.1.1.14  root     13348:                case 0x11: pcbios_int_10h_11h(); break;
                   13349:                case 0x12: pcbios_int_10h_12h(); break;
1.1       root     13350:                case 0x13: pcbios_int_10h_13h(); break;
1.1.1.30  root     13351:                case 0x18: pcbios_int_10h_18h(); break;
1.1.1.14  root     13352:                case 0x1a: pcbios_int_10h_1ah(); break;
1.1.1.24  root     13353:                case 0x1b: REG8(AL) = 0x00; break; // functionality/state information is not supported
                   13354:                case 0x1c: REG8(AL) = 0x00; break; // save/restore video state is not supported
1.1       root     13355:                case 0x1d: pcbios_int_10h_1dh(); break;
1.1.1.24  root     13356:                case 0x1e: REG8(AL) = 0x00; break; // flat-panel functions are not supported
                   13357:                case 0x1f: REG8(AL) = 0x00; break; // xga functions are not supported
1.1.1.22  root     13358:                case 0x4f: pcbios_int_10h_4fh(); break;
1.1.1.30  root     13359:                case 0x6f: break;
1.1.1.22  root     13360:                case 0x80: m_CF = 1; break; // unknown
                   13361:                case 0x81: m_CF = 1; break; // unknown
1.1       root     13362:                case 0x82: pcbios_int_10h_82h(); break;
1.1.1.22  root     13363:                case 0x83: pcbios_int_10h_83h(); break;
                   13364:                case 0x8b: break;
                   13365:                case 0x8c: m_CF = 1; break; // unknown
                   13366:                case 0x8d: m_CF = 1; break; // unknown
                   13367:                case 0x8e: m_CF = 1; break; // unknown
                   13368:                case 0x90: pcbios_int_10h_90h(); break;
                   13369:                case 0x91: pcbios_int_10h_91h(); break;
                   13370:                case 0x92: break;
                   13371:                case 0x93: break;
                   13372:                case 0xef: pcbios_int_10h_efh(); break;
1.1.1.24  root     13373:                case 0xfa: break; // ega register interface library is not installed
1.1       root     13374:                case 0xfe: pcbios_int_10h_feh(); break;
                   13375:                case 0xff: pcbios_int_10h_ffh(); break;
                   13376:                default:
1.1.1.22  root     13377:                        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));
                   13378:                        m_CF = 1;
1.1       root     13379:                        break;
                   13380:                }
                   13381:                break;
                   13382:        case 0x11:
                   13383:                // PC BIOS - Get Equipment List
1.1.1.26  root     13384:                REG16(AX) = msdos_get_equipment();
1.1       root     13385:                break;
                   13386:        case 0x12:
                   13387:                // PC BIOS - Get Memory Size
1.1.1.33  root     13388:                REG16(AX) = *(UINT16 *)(mem + 0x413);
1.1       root     13389:                break;
                   13390:        case 0x13:
                   13391:                // PC BIOS - Disk
1.1.1.22  root     13392: //             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     13393:                REG8(AH) = 0xff;
1.1.1.3   root     13394:                m_CF = 1;
1.1       root     13395:                break;
                   13396:        case 0x14:
                   13397:                // PC BIOS - Serial I/O
1.1.1.25  root     13398:                switch(REG8(AH)) {
                   13399:                case 0x00: pcbios_int_14h_00h(); break;
                   13400:                case 0x01: pcbios_int_14h_01h(); break;
                   13401:                case 0x02: pcbios_int_14h_02h(); break;
                   13402:                case 0x03: pcbios_int_14h_03h(); break;
                   13403:                case 0x04: pcbios_int_14h_04h(); break;
                   13404:                case 0x05: pcbios_int_14h_05h(); break;
                   13405:                default:
                   13406:                        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));
                   13407:                        break;
                   13408:                }
1.1       root     13409:                break;
                   13410:        case 0x15:
                   13411:                // PC BIOS
1.1.1.3   root     13412:                m_CF = 0;
1.1       root     13413:                switch(REG8(AH)) {
1.1.1.14  root     13414:                case 0x10: pcbios_int_15h_10h(); break;
1.1       root     13415:                case 0x23: pcbios_int_15h_23h(); break;
                   13416:                case 0x24: pcbios_int_15h_24h(); break;
1.1.1.24  root     13417:                case 0x41: break;
1.1       root     13418:                case 0x49: pcbios_int_15h_49h(); break;
1.1.1.22  root     13419:                case 0x50: pcbios_int_15h_50h(); break;
1.1.1.30  root     13420:                case 0x53: pcbios_int_15h_53h(); break;
1.1       root     13421:                case 0x86: pcbios_int_15h_86h(); break;
                   13422:                case 0x87: pcbios_int_15h_87h(); break;
                   13423:                case 0x88: pcbios_int_15h_88h(); break;
                   13424:                case 0x89: pcbios_int_15h_89h(); break;
1.1.1.21  root     13425:                case 0x8a: pcbios_int_15h_8ah(); break;
1.1.1.22  root     13426:                case 0xc0: // PS/2 ???
                   13427:                case 0xc1:
                   13428:                case 0xc2:
1.1.1.30  root     13429:                case 0xc3: // PS50+ ???
                   13430:                case 0xc4:
1.1.1.22  root     13431:                        REG8(AH) = 0x86;
                   13432:                        m_CF = 1;
                   13433:                        break;
1.1.1.3   root     13434: #if defined(HAS_I386)
1.1       root     13435:                case 0xc9: pcbios_int_15h_c9h(); break;
1.1.1.3   root     13436: #endif
1.1       root     13437:                case 0xca: pcbios_int_15h_cah(); break;
1.1.1.22  root     13438:                case 0xe8: pcbios_int_15h_e8h(); break;
1.1       root     13439:                default:
1.1.1.22  root     13440:                        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));
                   13441:                        REG8(AH) = 0x86;
1.1.1.3   root     13442:                        m_CF = 1;
1.1       root     13443:                        break;
                   13444:                }
                   13445:                break;
                   13446:        case 0x16:
                   13447:                // PC BIOS - Keyboard
1.1.1.3   root     13448:                m_CF = 0;
1.1       root     13449:                switch(REG8(AH)) {
                   13450:                case 0x00: pcbios_int_16h_00h(); break;
                   13451:                case 0x01: pcbios_int_16h_01h(); break;
                   13452:                case 0x02: pcbios_int_16h_02h(); break;
                   13453:                case 0x03: pcbios_int_16h_03h(); break;
                   13454:                case 0x05: pcbios_int_16h_05h(); break;
                   13455:                case 0x10: pcbios_int_16h_00h(); break;
                   13456:                case 0x11: pcbios_int_16h_01h(); break;
                   13457:                case 0x12: pcbios_int_16h_12h(); break;
                   13458:                case 0x13: pcbios_int_16h_13h(); break;
                   13459:                case 0x14: pcbios_int_16h_14h(); break;
1.1.1.24  root     13460:                case 0x55: pcbios_int_16h_55h(); break;
1.1.1.30  root     13461:                case 0x6f: pcbios_int_16h_6fh(); break;
1.1.1.22  root     13462:                case 0xda: break; // unknown
                   13463:                case 0xff: break; // unknown
1.1       root     13464:                default:
1.1.1.22  root     13465:                        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     13466:                        break;
                   13467:                }
                   13468:                break;
                   13469:        case 0x17:
                   13470:                // PC BIOS - Printer
1.1.1.22  root     13471: //             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     13472:                break;
                   13473:        case 0x1a:
                   13474:                // PC BIOS - Timer
1.1.1.3   root     13475:                m_CF = 0;
1.1       root     13476:                switch(REG8(AH)) {
                   13477:                case 0x00: pcbios_int_1ah_00h(); break;
                   13478:                case 0x01: break;
                   13479:                case 0x02: pcbios_int_1ah_02h(); break;
                   13480:                case 0x03: break;
                   13481:                case 0x04: pcbios_int_1ah_04h(); break;
                   13482:                case 0x05: break;
                   13483:                case 0x0a: pcbios_int_1ah_0ah(); break;
                   13484:                case 0x0b: break;
1.1.1.14  root     13485:                case 0x35: break; // Word Perfect Third Party Interface?
                   13486:                case 0x36: break; // Word Perfect Third Party Interface
                   13487:                case 0x70: break; // SNAP? (Simple Network Application Protocol)
1.1       root     13488:                default:
1.1.1.22  root     13489:                        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     13490:                        break;
                   13491:                }
                   13492:                break;
1.1.1.33  root     13493:        case 0x1b:
                   13494:                mem[0x471] = 0x00;
                   13495:                break;
1.1       root     13496:        case 0x20:
1.1.1.28  root     13497:                try {
                   13498:                        msdos_process_terminate(SREG(CS), retval, 1);
                   13499:                } catch(...) {
                   13500:                        fatalerror("failed to terminate the process (PSP=%04X) by int 20h\n", SREG(CS));
                   13501:                }
1.1       root     13502:                break;
                   13503:        case 0x21:
                   13504:                // MS-DOS System Call
1.1.1.3   root     13505:                m_CF = 0;
1.1.1.28  root     13506:                try {
                   13507:                        switch(REG8(AH)) {
                   13508:                        case 0x00: msdos_int_21h_00h(); break;
                   13509:                        case 0x01: msdos_int_21h_01h(); break;
                   13510:                        case 0x02: msdos_int_21h_02h(); break;
                   13511:                        case 0x03: msdos_int_21h_03h(); break;
                   13512:                        case 0x04: msdos_int_21h_04h(); break;
                   13513:                        case 0x05: msdos_int_21h_05h(); break;
                   13514:                        case 0x06: msdos_int_21h_06h(); break;
                   13515:                        case 0x07: msdos_int_21h_07h(); break;
                   13516:                        case 0x08: msdos_int_21h_08h(); break;
                   13517:                        case 0x09: msdos_int_21h_09h(); break;
                   13518:                        case 0x0a: msdos_int_21h_0ah(); break;
                   13519:                        case 0x0b: msdos_int_21h_0bh(); break;
                   13520:                        case 0x0c: msdos_int_21h_0ch(); break;
                   13521:                        case 0x0d: msdos_int_21h_0dh(); break;
                   13522:                        case 0x0e: msdos_int_21h_0eh(); break;
                   13523:                        case 0x0f: msdos_int_21h_0fh(); break;
                   13524:                        case 0x10: msdos_int_21h_10h(); break;
                   13525:                        case 0x11: msdos_int_21h_11h(); break;
                   13526:                        case 0x12: msdos_int_21h_12h(); break;
                   13527:                        case 0x13: msdos_int_21h_13h(); break;
                   13528:                        case 0x14: msdos_int_21h_14h(); break;
                   13529:                        case 0x15: msdos_int_21h_15h(); break;
                   13530:                        case 0x16: msdos_int_21h_16h(); break;
                   13531:                        case 0x17: msdos_int_21h_17h(); break;
                   13532:                        case 0x18: msdos_int_21h_18h(); break;
                   13533:                        case 0x19: msdos_int_21h_19h(); break;
                   13534:                        case 0x1a: msdos_int_21h_1ah(); break;
                   13535:                        case 0x1b: msdos_int_21h_1bh(); break;
                   13536:                        case 0x1c: msdos_int_21h_1ch(); break;
                   13537:                        case 0x1d: msdos_int_21h_1dh(); break;
                   13538:                        case 0x1e: msdos_int_21h_1eh(); break;
                   13539:                        case 0x1f: msdos_int_21h_1fh(); break;
                   13540:                        case 0x20: msdos_int_21h_20h(); break;
                   13541:                        case 0x21: msdos_int_21h_21h(); break;
                   13542:                        case 0x22: msdos_int_21h_22h(); break;
                   13543:                        case 0x23: msdos_int_21h_23h(); break;
                   13544:                        case 0x24: msdos_int_21h_24h(); break;
                   13545:                        case 0x25: msdos_int_21h_25h(); break;
                   13546:                        case 0x26: msdos_int_21h_26h(); break;
                   13547:                        case 0x27: msdos_int_21h_27h(); break;
                   13548:                        case 0x28: msdos_int_21h_28h(); break;
                   13549:                        case 0x29: msdos_int_21h_29h(); break;
                   13550:                        case 0x2a: msdos_int_21h_2ah(); break;
                   13551:                        case 0x2b: msdos_int_21h_2bh(); break;
                   13552:                        case 0x2c: msdos_int_21h_2ch(); break;
                   13553:                        case 0x2d: msdos_int_21h_2dh(); break;
                   13554:                        case 0x2e: msdos_int_21h_2eh(); break;
                   13555:                        case 0x2f: msdos_int_21h_2fh(); break;
                   13556:                        case 0x30: msdos_int_21h_30h(); break;
                   13557:                        case 0x31: msdos_int_21h_31h(); break;
                   13558:                        case 0x32: msdos_int_21h_32h(); break;
                   13559:                        case 0x33: msdos_int_21h_33h(); break;
                   13560:                        case 0x34: msdos_int_21h_34h(); break;
                   13561:                        case 0x35: msdos_int_21h_35h(); break;
                   13562:                        case 0x36: msdos_int_21h_36h(); break;
                   13563:                        case 0x37: msdos_int_21h_37h(); break;
                   13564:                        case 0x38: msdos_int_21h_38h(); break;
                   13565:                        case 0x39: msdos_int_21h_39h(0); break;
                   13566:                        case 0x3a: msdos_int_21h_3ah(0); break;
                   13567:                        case 0x3b: msdos_int_21h_3bh(0); break;
                   13568:                        case 0x3c: msdos_int_21h_3ch(); break;
                   13569:                        case 0x3d: msdos_int_21h_3dh(); break;
                   13570:                        case 0x3e: msdos_int_21h_3eh(); break;
                   13571:                        case 0x3f: msdos_int_21h_3fh(); break;
                   13572:                        case 0x40: msdos_int_21h_40h(); break;
                   13573:                        case 0x41: msdos_int_21h_41h(0); break;
                   13574:                        case 0x42: msdos_int_21h_42h(); break;
                   13575:                        case 0x43: msdos_int_21h_43h(0); break;
                   13576:                        case 0x44: msdos_int_21h_44h(); break;
                   13577:                        case 0x45: msdos_int_21h_45h(); break;
                   13578:                        case 0x46: msdos_int_21h_46h(); break;
                   13579:                        case 0x47: msdos_int_21h_47h(0); break;
                   13580:                        case 0x48: msdos_int_21h_48h(); break;
                   13581:                        case 0x49: msdos_int_21h_49h(); break;
                   13582:                        case 0x4a: msdos_int_21h_4ah(); break;
                   13583:                        case 0x4b: msdos_int_21h_4bh(); break;
                   13584:                        case 0x4c: msdos_int_21h_4ch(); break;
                   13585:                        case 0x4d: msdos_int_21h_4dh(); break;
                   13586:                        case 0x4e: msdos_int_21h_4eh(); break;
                   13587:                        case 0x4f: msdos_int_21h_4fh(); break;
                   13588:                        case 0x50: msdos_int_21h_50h(); break;
                   13589:                        case 0x51: msdos_int_21h_51h(); break;
                   13590:                        case 0x52: msdos_int_21h_52h(); break;
1.1.1.33  root     13591:                        // 0x53: Translate BIOS Parameter Block to Drive Param Bock
1.1.1.28  root     13592:                        case 0x54: msdos_int_21h_54h(); break;
                   13593:                        case 0x55: msdos_int_21h_55h(); break;
                   13594:                        case 0x56: msdos_int_21h_56h(0); break;
                   13595:                        case 0x57: msdos_int_21h_57h(); break;
                   13596:                        case 0x58: msdos_int_21h_58h(); break;
                   13597:                        case 0x59: msdos_int_21h_59h(); break;
                   13598:                        case 0x5a: msdos_int_21h_5ah(); break;
                   13599:                        case 0x5b: msdos_int_21h_5bh(); break;
                   13600:                        case 0x5c: msdos_int_21h_5ch(); break;
                   13601:                        case 0x5d: msdos_int_21h_5dh(); break;
1.1.1.33  root     13602:                        // 0x5e: MS-Network
1.1.1.30  root     13603:                        case 0x5f: msdos_int_21h_5fh(); break;
1.1.1.28  root     13604:                        case 0x60: msdos_int_21h_60h(0); break;
                   13605:                        case 0x61: msdos_int_21h_61h(); break;
                   13606:                        case 0x62: msdos_int_21h_62h(); break;
                   13607:                        case 0x63: msdos_int_21h_63h(); break;
1.1.1.33  root     13608:                        // 0x64: Set Device Driver Lockahead Flag
1.1.1.28  root     13609:                        case 0x65: msdos_int_21h_65h(); break;
                   13610:                        case 0x66: msdos_int_21h_66h(); break;
                   13611:                        case 0x67: msdos_int_21h_67h(); break;
                   13612:                        case 0x68: msdos_int_21h_68h(); break;
                   13613:                        case 0x69: msdos_int_21h_69h(); break;
                   13614:                        case 0x6a: msdos_int_21h_6ah(); break;
                   13615:                        case 0x6b: msdos_int_21h_6bh(); break;
                   13616:                        case 0x6c: msdos_int_21h_6ch(0); break;
1.1.1.33  root     13617:                        // 0x6d: Find First ROM Program
                   13618:                        // 0x6e: Find Next ROM Program
                   13619:                        // 0x6f: Get/Set ROM Scan Start Address
                   13620:                        // 0x70: Windows95 - Get/Set Internationalization Information
1.1.1.28  root     13621:                        case 0x71:
1.1.1.33  root     13622:                                // Windows95 - Long Filename Functions
1.1.1.28  root     13623:                                switch(REG8(AL)) {
                   13624:                                case 0x0d: msdos_int_21h_710dh(); break;
                   13625:                                case 0x39: msdos_int_21h_39h(1); break;
                   13626:                                case 0x3a: msdos_int_21h_3ah(1); break;
                   13627:                                case 0x3b: msdos_int_21h_3bh(1); break;
                   13628:                                case 0x41: msdos_int_21h_7141h(1); break;
                   13629:                                case 0x43: msdos_int_21h_43h(1); break;
                   13630:                                case 0x47: msdos_int_21h_47h(1); break;
                   13631:                                case 0x4e: msdos_int_21h_714eh(); break;
                   13632:                                case 0x4f: msdos_int_21h_714fh(); break;
                   13633:                                case 0x56: msdos_int_21h_56h(1); break;
                   13634:                                case 0x60: msdos_int_21h_60h(1); break;
                   13635:                                case 0x6c: msdos_int_21h_6ch(1); break;
                   13636:                                case 0xa0: msdos_int_21h_71a0h(); break;
                   13637:                                case 0xa1: msdos_int_21h_71a1h(); break;
                   13638:                                case 0xa6: msdos_int_21h_71a6h(); break;
                   13639:                                case 0xa7: msdos_int_21h_71a7h(); break;
                   13640:                                case 0xa8: msdos_int_21h_71a8h(); break;
1.1.1.33  root     13641:                                // 0xa9: Server Create/Open File
1.1.1.28  root     13642:                                case 0xaa: msdos_int_21h_71aah(); break;
                   13643:                                default:
                   13644:                                        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));
                   13645:                                        REG16(AX) = 0x7100;
                   13646:                                        m_CF = 1;
                   13647:                                        break;
                   13648:                                }
                   13649:                                break;
                   13650:                        // 0x72: Windows95 beta - LFN FindClose
                   13651:                        case 0x73:
1.1.1.33  root     13652:                                // Windows95 - FAT32 Functions
1.1.1.28  root     13653:                                switch(REG8(AL)) {
                   13654:                                case 0x00: msdos_int_21h_7300h(); break;
1.1.1.33  root     13655:                                // 0x01: Set Drive Locking ???
1.1.1.28  root     13656:                                case 0x02: msdos_int_21h_7302h(); break;
                   13657:                                case 0x03: msdos_int_21h_7303h(); break;
1.1.1.33  root     13658:                                // 0x04: Set DPB to Use for Formatting
                   13659:                                // 0x05: Extended Absolute Disk Read/Write
1.1.1.28  root     13660:                                default:
                   13661:                                        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));
                   13662:                                        REG16(AX) = 0x7300;
                   13663:                                        m_CF = 1;
                   13664:                                        break;
                   13665:                                }
1.1       root     13666:                                break;
1.1.1.30  root     13667:                        case 0xdb: msdos_int_21h_dbh(); break;
                   13668:                        case 0xdc: msdos_int_21h_dch(); break;
1.1       root     13669:                        default:
1.1.1.22  root     13670:                                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     13671:                                REG16(AX) = 0x01;
1.1.1.3   root     13672:                                m_CF = 1;
1.1       root     13673:                                break;
                   13674:                        }
1.1.1.28  root     13675:                } catch(int error) {
                   13676:                        REG16(AX) = error;
                   13677:                        m_CF = 1;
                   13678:                } catch(...) {
                   13679:                        REG16(AX) = 0x1f; // general failure
1.1.1.3   root     13680:                        m_CF = 1;
1.1       root     13681:                }
1.1.1.3   root     13682:                if(m_CF) {
1.1.1.23  root     13683:                        sda_t *sda = (sda_t *)(mem + SDA_TOP);
                   13684:                        sda->extended_error_code = REG16(AX);
                   13685:                        switch(sda->extended_error_code) {
                   13686:                        case  4: // Too many open files
                   13687:                        case  8: // Insufficient memory
                   13688:                                sda->error_class = 1; // Out of resource
                   13689:                                break;
                   13690:                        case  5: // Access denied
                   13691:                                sda->error_class = 3; // Authorization
                   13692:                                break;
                   13693:                        case  7: // Memory control block destroyed
                   13694:                                sda->error_class = 4; // Internal
                   13695:                                break;
                   13696:                        case  2: // File not found
                   13697:                        case  3: // Path not found
                   13698:                        case 15: // Invaid drive specified
                   13699:                        case 18: // No more files
                   13700:                                sda->error_class = 8; // Not found
                   13701:                                break;
                   13702:                        case 32: // Sharing violation
                   13703:                        case 33: // Lock violation
                   13704:                                sda->error_class = 10; // Locked
                   13705:                                break;
                   13706: //                     case 16: // Removal of current directory attempted
                   13707:                        case 19: // Attempted write on protected disk
                   13708:                        case 21: // Drive not ready
                   13709: //                     case 29: // Write failure
                   13710: //                     case 30: // Read failure
                   13711: //                     case 82: // Cannot create subdirectory
                   13712:                                sda->error_class = 11; // Media
                   13713:                                break;
                   13714:                        case 80: // File already exists
                   13715:                                sda->error_class = 12; // Already exist
                   13716:                                break;
                   13717:                        default:
                   13718:                                sda->error_class = 13; // Unknown
                   13719:                                break;
                   13720:                        }
                   13721:                        sda->suggested_action = 1; // Retry
                   13722:                        sda->locus_of_last_error = 1; // Unknown
1.1       root     13723:                }
1.1.1.33  root     13724:                if(ctrl_break_checking && ctrl_break_detected) {
1.1.1.26  root     13725:                        // raise int 23h
                   13726: #if defined(HAS_I386)
                   13727:                        m_ext = 0; // not an external interrupt
                   13728:                        i386_trap(0x23, 1, 0);
                   13729:                        m_ext = 1;
                   13730: #else
                   13731:                        PREFIX86(_interrupt)(0x23);
                   13732: #endif
                   13733:                }
1.1       root     13734:                break;
                   13735:        case 0x22:
                   13736:                fatalerror("int 22h (terminate address)\n");
                   13737:        case 0x23:
1.1.1.28  root     13738:                try {
                   13739:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x100, 1);
                   13740:                } catch(...) {
                   13741:                        fatalerror("failed to terminate the current process by int 23h\n");
                   13742:                }
1.1       root     13743:                break;
                   13744:        case 0x24:
1.1.1.32  root     13745: /*
1.1.1.28  root     13746:                try {
                   13747:                        msdos_process_terminate(current_psp, (retval & 0xff) | 0x200, 1);
                   13748:                } catch(...) {
                   13749:                        fatalerror("failed to terminate the current process by int 24h\n");
                   13750:                }
1.1.1.32  root     13751: */
                   13752:                msdos_int_24h();
1.1       root     13753:                break;
                   13754:        case 0x25:
                   13755:                msdos_int_25h();
                   13756:                break;
                   13757:        case 0x26:
                   13758:                msdos_int_26h();
                   13759:                break;
                   13760:        case 0x27:
1.1.1.28  root     13761:                try {
                   13762:                        msdos_int_27h();
                   13763:                } catch(...) {
                   13764:                        fatalerror("failed to terminate the process (PSP=%04X) by int 27h\n", SREG(CS));
                   13765:                }
1.1       root     13766:                break;
                   13767:        case 0x28:
                   13768:                Sleep(10);
                   13769:                break;
                   13770:        case 0x29:
                   13771:                msdos_int_29h();
                   13772:                break;
                   13773:        case 0x2e:
                   13774:                msdos_int_2eh();
                   13775:                break;
                   13776:        case 0x2f:
                   13777:                // multiplex interrupt
                   13778:                switch(REG8(AH)) {
1.1.1.22  root     13779:                case 0x05: msdos_int_2fh_05h(); break;
                   13780:                case 0x11: msdos_int_2fh_11h(); break;
1.1.1.21  root     13781:                case 0x12: msdos_int_2fh_12h(); break;
1.1.1.30  root     13782:                case 0x13: msdos_int_2fh_13h(); break;
1.1.1.22  root     13783:                case 0x14: msdos_int_2fh_14h(); break;
                   13784:                case 0x15: msdos_int_2fh_15h(); break;
1.1       root     13785:                case 0x16: msdos_int_2fh_16h(); break;
1.1.1.22  root     13786:                case 0x19: msdos_int_2fh_19h(); break;
1.1       root     13787:                case 0x1a: msdos_int_2fh_1ah(); break;
1.1.1.30  root     13788:                case 0x40: msdos_int_2fh_40h(); break;
1.1       root     13789:                case 0x43: msdos_int_2fh_43h(); break;
1.1.1.22  root     13790:                case 0x46: msdos_int_2fh_46h(); break;
                   13791:                case 0x48: msdos_int_2fh_48h(); break;
1.1       root     13792:                case 0x4a: msdos_int_2fh_4ah(); break;
1.1.1.22  root     13793:                case 0x4b: msdos_int_2fh_4bh(); break;
1.1       root     13794:                case 0x4f: msdos_int_2fh_4fh(); break;
1.1.1.22  root     13795:                case 0x55: msdos_int_2fh_55h(); break;
1.1.1.24  root     13796:                case 0xad: msdos_int_2fh_adh(); break;
1.1       root     13797:                case 0xae: msdos_int_2fh_aeh(); break;
1.1.1.34! root     13798:                case 0xb7: msdos_int_2fh_b7h(); break;
1.1.1.30  root     13799:                // Installation Check
                   13800:                case 0x01: // PRINT.COM
                   13801:                case 0x02: // PC LAN Program Redirector
                   13802:                case 0x06: // ASSIGN
                   13803:                case 0x08: // DRIVER.SYS
                   13804:                case 0x10: // SHARE
                   13805:                case 0x17: // Clibboard functions
                   13806:                case 0x1b: // XMA2EMS.SYS
                   13807:                case 0x23: // DR DOS 5.0 GRAFTABL
                   13808:                case 0x27: // DR-DOR 6.0 TaskMAX
                   13809:                case 0x2e: // Novell DOS 7 GRAFTABL
1.1.1.33  root     13810:                case 0x39: // Kingswood TSR INTERFACE
                   13811:                case 0x41: // DOS Enhanced LAN Manager 2.0+ MINIPOP/NETPOPUP
1.1.1.30  root     13812:                case 0x45: // PROF.COM
                   13813:                case 0x51: // ODIHELP.EXE
1.1.1.33  root     13814:                case 0x52: // JAM.SYS v1.10+
1.1.1.30  root     13815:                case 0x54: // POWER.EXE
                   13816:                case 0x56: // INTERLNK
1.1.1.33  root     13817:                case 0x57: // IOMEGA DRIVERS
1.1.1.30  root     13818:                case 0x70: // License Service API
1.1.1.33  root     13819:                case 0x72: // SRDISK v1.30+
1.1.1.30  root     13820:                case 0x7a: // Novell NetWare
1.1.1.33  root     13821:                case 0x7f: // PRINDIR v9.0
                   13822:                case 0x80: // FAX BIOS
                   13823:                case 0x81: // Nanosoft, Inc. TurboNET redirector
                   13824:                case 0x82: // Nanosoft, Inc. CAPDOS
                   13825:                case 0x89: // WHOA!.COM
                   13826:                case 0x90: // Resident AID
1.1.1.30  root     13827:                case 0x94: // MICRO.EXE
1.1.1.33  root     13828:                case 0x97: // Micro Focus COBOL v3.1.31
                   13829:                case 0x98: // Micro Focus COBOL v3.1.31
                   13830:                case 0x99: // DOS Navigator II
                   13831:                case 0x9e: // INTMON v2.1
                   13832:                case 0x9f: // INTCFG v2.1
                   13833:                case 0xa9: // METZTSR.COM
                   13834:                case 0xab: // SRSoft MODAL PC v2
1.1.1.30  root     13835:                case 0xac: // GRAPHICS.COM
1.1.1.33  root     13836:                case 0xaf: // WinDOS v2.11
1.1.1.30  root     13837:                case 0xb0: // GRAFTABLE.COM
1.1.1.33  root     13838:                case 0xb4: // IBM PC3270 EMULATION PROG v3
1.1.1.30  root     13839:                case 0xb8: // NETWORK
                   13840:                case 0xb9: // RECEIVER.COM
                   13841:                case 0xbc: // EGA.SYS
1.1.1.33  root     13842:                case 0xbe: // REDVIEW
1.1.1.30  root     13843:                case 0xbf: // PC LAN Program - REDIRIFS.EXE
                   13844:                case 0xc0: // Novell LSL.COM
1.1.1.33  root     13845:                case 0xc1: // Personal NetWare - STPIPX v1.00
                   13846:                case 0xc3: // SETWPR.COM
                   13847:                case 0xc5: // PC-DOS Econet v1.05
                   13848:                case 0xc7: // COLAP.COM
                   13849:                case 0xc9: // ThunderByte???
                   13850:                case 0xca: // TBSCANX
                   13851:                case 0xcb: // Communicating Applications Specification
                   13852:                case 0xcc: // Tsoft NFSDRVR
                   13853:                case 0xcd: // SWELL.EXE
                   13854:                case 0xcf: // TEMPLEXX 1.0
                   13855:                case 0xd0: // Lotus CD/Networker
1.1.1.30  root     13856:                case 0xd2: // PCL-838.EXE
1.1.1.33  root     13857:                case 0xd3: // TeleReplica
                   13858:                case 0xd6: // VEDIT VSWAP
                   13859:                case 0xd7: // Banyan VINES v4+
1.1.1.30  root     13860:                case 0xd8: // Novell NetWare Lite - CLIENT.EXE
1.1.1.33  root     13861:                case 0xda: // ZyXEL ZFAX v1.x
                   13862:                case 0xdb: // ZyXEL ZFAX v2+
                   13863:                case 0xdc: // GOLD.COM
                   13864:                case 0xdd: // MIXFIX.EXE
                   13865:                case 0xde: // Novell Netware - RPRINTER, NPRINTER
                   13866:                case 0xdf: // HyperWare programs
                   13867:                case 0xe0: // SETDRVER.COM v2.10+
                   13868:                case 0xe1: // Phantom2 v1.1+
                   13869:                case 0xe3: // ANARKEY.COM
                   13870:                case 0xed: // Phar Lap DOS EXTENDERS
                   13871:                case 0xee: // XVIEW
                   13872:                case 0xf0: // 4MAP
                   13873:                case 0xf1: // DOS EXTENDER
                   13874:                case 0xf2: // WINX
                   13875:                case 0xf4: // FINDIRQ.COM
                   13876:                case 0xf7: // AUTOPARK.COM
                   13877:                case 0xf8: // SuperStor PRO 2XON.COM
                   13878:                case 0xfb: // AutoBraille v1.1A
                   13879:                case 0xfe: // PC-NFS ???
                   13880:                case 0xff: // Topware Network Operating System
1.1.1.30  root     13881:                        switch(REG8(AL)) {
                   13882:                        case 0x00:
                   13883:                                // This is not installed
                   13884: //                             REG8(AL) = 0x00;
                   13885:                                break;
1.1.1.33  root     13886:                        case 0x01:
                   13887:                                // Banyan VINES v4+ is not installed
                   13888:                                if(REG8(AH) == 0xd7 && REG16(BX) == 0x0000) {
                   13889:                                        break;
                   13890:                                }
1.1.1.30  root     13891:                        default:
                   13892:                                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));
                   13893:                                REG16(AX) = 0x01;
                   13894:                                m_CF = 1;
                   13895:                                break;
                   13896:                        }
                   13897:                        break;
1.1.1.22  root     13898:                default:
                   13899:                        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));
                   13900:                        break;
1.1       root     13901:                }
                   13902:                break;
1.1.1.24  root     13903:        case 0x33:
                   13904:                switch(REG8(AH)) {
                   13905:                case 0x00:
                   13906:                        // Mouse
                   13907:                        switch(REG8(AL)) {
                   13908:                        case 0x00: msdos_int_33h_0000h(); break;
                   13909:                        case 0x01: msdos_int_33h_0001h(); break;
                   13910:                        case 0x02: msdos_int_33h_0002h(); break;
                   13911:                        case 0x03: msdos_int_33h_0003h(); break;
1.1.1.34! root     13912:                        case 0x04: msdos_int_33h_0004h(); break;
1.1.1.24  root     13913:                        case 0x05: msdos_int_33h_0005h(); break;
                   13914:                        case 0x06: msdos_int_33h_0006h(); break;
                   13915:                        case 0x07: msdos_int_33h_0007h(); break;
                   13916:                        case 0x08: msdos_int_33h_0008h(); break;
                   13917:                        case 0x09: msdos_int_33h_0009h(); break;
1.1.1.34! root     13918:                        case 0x0a: break; // Define Text Cursor
1.1.1.24  root     13919:                        case 0x0b: msdos_int_33h_000bh(); break;
                   13920:                        case 0x0c: msdos_int_33h_000ch(); break;
1.1.1.34! root     13921:                        case 0x0d: break; // Light Pen Emulation On
        !          13922:                        case 0x0e: break; // Light Pen Emulation Off
1.1.1.24  root     13923:                        case 0x0f: msdos_int_33h_000fh(); break;
1.1.1.34! root     13924:                        case 0x10: break; // Define Screen Region for Updating
1.1.1.24  root     13925:                        case 0x11: msdos_int_33h_0011h(); break;
1.1.1.34! root     13926:                        case 0x12: REG16(AX) = 0xffff; break; // Set Large Graphics Cursor Block
        !          13927:                        case 0x13: break; // Define Double-Speed Threshold
1.1.1.24  root     13928:                        case 0x14: msdos_int_33h_0014h(); break;
                   13929:                        case 0x15: msdos_int_33h_0015h(); break;
                   13930:                        case 0x16: msdos_int_33h_0016h(); break;
                   13931:                        case 0x17: msdos_int_33h_0017h(); break;
                   13932:                        case 0x1a: msdos_int_33h_001ah(); break;
                   13933:                        case 0x1b: msdos_int_33h_001bh(); break;
                   13934:                        case 0x1d: msdos_int_33h_001dh(); break;
                   13935:                        case 0x1e: msdos_int_33h_001eh(); break;
1.1.1.34! root     13936:                        case 0x1f: msdos_int_33h_001fh(); break;
        !          13937:                        case 0x20: msdos_int_33h_0020h(); break;
1.1.1.24  root     13938:                        case 0x21: msdos_int_33h_0021h(); break;
                   13939:                        case 0x22: msdos_int_33h_0022h(); break;
                   13940:                        case 0x23: msdos_int_33h_0023h(); break;
                   13941:                        case 0x24: msdos_int_33h_0024h(); break;
                   13942:                        case 0x26: msdos_int_33h_0026h(); break;
                   13943:                        case 0x2a: msdos_int_33h_002ah(); break;
1.1.1.34! root     13944:                        case 0x2f: break; // Mouse Hardware Reset
1.1.1.24  root     13945:                        case 0x31: msdos_int_33h_0031h(); break;
                   13946:                        case 0x32: msdos_int_33h_0032h(); break;
                   13947:                        default:
                   13948:                                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));
                   13949:                                break;
                   13950:                        }
                   13951:                        break;
                   13952:                default:
                   13953:                        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));
                   13954:                        break;
                   13955:                }
                   13956:                break;
1.1.1.19  root     13957:        case 0x68:
                   13958:                // dummy interrupt for EMS (int 67h)
                   13959:                switch(REG8(AH)) {
                   13960:                case 0x40: msdos_int_67h_40h(); break;
                   13961:                case 0x41: msdos_int_67h_41h(); break;
                   13962:                case 0x42: msdos_int_67h_42h(); break;
                   13963:                case 0x43: msdos_int_67h_43h(); break;
                   13964:                case 0x44: msdos_int_67h_44h(); break;
                   13965:                case 0x45: msdos_int_67h_45h(); break;
                   13966:                case 0x46: msdos_int_67h_46h(); break;
                   13967:                case 0x47: msdos_int_67h_47h(); break;
                   13968:                case 0x48: msdos_int_67h_48h(); break;
1.1.1.20  root     13969:                // 0x49: LIM EMS - Reserved - Get I/O Port Address (Undocumented in EMS 3.2)
                   13970:                // 0x4a: LIM EMS - Reserved - Get Translation Array (Undocumented in EMS 3.2)
1.1.1.19  root     13971:                case 0x4b: msdos_int_67h_4bh(); break;
                   13972:                case 0x4c: msdos_int_67h_4ch(); break;
                   13973:                case 0x4d: msdos_int_67h_4dh(); break;
1.1.1.20  root     13974:                case 0x4e: msdos_int_67h_4eh(); break;
1.1.1.21  root     13975:                case 0x4f: msdos_int_67h_4fh(); break;
1.1.1.20  root     13976:                case 0x50: msdos_int_67h_50h(); break;
1.1.1.19  root     13977:                case 0x51: msdos_int_67h_51h(); break;
1.1.1.20  root     13978:                case 0x52: msdos_int_67h_52h(); break;
1.1.1.19  root     13979:                case 0x53: msdos_int_67h_53h(); break;
                   13980:                case 0x54: msdos_int_67h_54h(); break;
1.1.1.20  root     13981:                // 0x55: LIM EMS 4.0 - Alter Page Map And JUMP
                   13982:                // 0x56: LIM EMS 4.0 - Alter Page Map And CALL
                   13983:                case 0x57: msdos_int_67h_57h(); break;
                   13984:                case 0x58: msdos_int_67h_58h(); break;
                   13985:                // 0x59: LIM EMS 4.0 - Get Expanded Memory Hardware Information (for DOS Kernel)
                   13986:                case 0x5a: msdos_int_67h_5ah(); break;
                   13987:                // 0x5b: LIM EMS 4.0 - Alternate Map Register Set (for DOS Kernel)
                   13988:                // 0x5c: LIM EMS 4.0 - Prepate Expanded Memory Hardware For Warm Boot
                   13989:                // 0x5d: LIM EMS 4.0 - Enable/Disable OS Function Set Functions (for DOS Kernel)
1.1.1.31  root     13990:                // 0x60: EEMS - Get Physical Window Array
                   13991:                // 0x61: EEMS - Generic Accelerator Card Support
                   13992:                // 0x68: EEMS - Get Address of All Pge Frames om System
                   13993:                // 0x69: EEMS - Map Page into Frame
                   13994:                // 0x6a: EEMS - Page Mapping
                   13995:                // 0xde: VCPI
1.1.1.30  root     13996:                case 0xde: msdos_int_67h_deh(); break;
1.1.1.19  root     13997:                default:
1.1.1.22  root     13998:                        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     13999:                        REG8(AH) = 0x84;
                   14000:                        break;
                   14001:                }
                   14002:                break;
                   14003: #ifdef SUPPORT_XMS
                   14004:        case 0x69:
                   14005:                // dummy interrupt for XMS (call far)
1.1.1.28  root     14006:                try {
                   14007:                        switch(REG8(AH)) {
                   14008:                        case 0x00: msdos_call_xms_00h(); break;
                   14009:                        case 0x01: msdos_call_xms_01h(); break;
                   14010:                        case 0x02: msdos_call_xms_02h(); break;
                   14011:                        case 0x03: msdos_call_xms_03h(); break;
                   14012:                        case 0x04: msdos_call_xms_04h(); break;
                   14013:                        case 0x05: msdos_call_xms_05h(); break;
                   14014:                        case 0x06: msdos_call_xms_06h(); break;
                   14015:                        case 0x07: msdos_call_xms_07h(); break;
                   14016:                        case 0x08: msdos_call_xms_08h(); break;
                   14017:                        case 0x09: msdos_call_xms_09h(); break;
                   14018:                        case 0x0a: msdos_call_xms_0ah(); break;
                   14019:                        case 0x0b: msdos_call_xms_0bh(); break;
                   14020:                        case 0x0c: msdos_call_xms_0ch(); break;
                   14021:                        case 0x0d: msdos_call_xms_0dh(); break;
                   14022:                        case 0x0e: msdos_call_xms_0eh(); break;
                   14023:                        case 0x0f: msdos_call_xms_0fh(); break;
                   14024:                        case 0x10: msdos_call_xms_10h(); break;
                   14025:                        case 0x11: msdos_call_xms_11h(); break;
                   14026:                        case 0x12: msdos_call_xms_12h(); break;
1.1.1.29  root     14027: #if defined(HAS_I386)
                   14028:                        case 0x88: msdos_call_xms_88h(); break;
                   14029:                        case 0x89: msdos_call_xms_89h(); break;
                   14030:                        case 0x8e: msdos_call_xms_8eh(); break;
                   14031:                        case 0x8f: msdos_call_xms_8fh(); break;
                   14032: #endif
1.1.1.28  root     14033:                        default:
                   14034:                                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));
                   14035:                                REG16(AX) = 0x0000;
                   14036:                                REG8(BL) = 0x80; // function not implemented
                   14037:                                break;
                   14038:                        }
                   14039:                } catch(...) {
1.1.1.19  root     14040:                        REG16(AX) = 0x0000;
1.1.1.28  root     14041:                        REG8(BL) = 0x8f; // unrecoverable driver error
1.1.1.19  root     14042:                }
                   14043:                break;
                   14044: #endif
                   14045:        case 0x6a:
1.1.1.24  root     14046:                // irq12 (mouse)
                   14047:                mouse_push_ax = REG16(AX);
                   14048:                mouse_push_bx = REG16(BX);
                   14049:                mouse_push_cx = REG16(CX);
                   14050:                mouse_push_dx = REG16(DX);
                   14051:                mouse_push_si = REG16(SI);
                   14052:                mouse_push_di = REG16(DI);
                   14053:                
1.1.1.34! root     14054:                if(/*mouse.hidden == 0 && */mouse.call_addr.dw != 0) {
1.1.1.24  root     14055:                        REG16(AX) = mouse.status_irq;
                   14056:                        REG16(BX) = mouse.get_buttons();
1.1.1.34! root     14057:                        REG16(CX) = max(mouse.min_position.x & ~7, min(mouse.max_position.x & ~7, mouse.position.x));
        !          14058:                        REG16(DX) = max(mouse.min_position.y & ~7, min(mouse.max_position.y & ~7, mouse.position.y));
1.1.1.24  root     14059:                        REG16(SI) = REG16(CX) * mouse.mickey.x / 8;
                   14060:                        REG16(DI) = REG16(DX) * mouse.mickey.y / 8;
                   14061:                        
                   14062:                        mem[0xfffd0 + 0x02] = 0x9a;     // call far
                   14063:                        mem[0xfffd0 + 0x03] = mouse.call_addr.w.l & 0xff;
                   14064:                        mem[0xfffd0 + 0x04] = mouse.call_addr.w.l >> 8;
                   14065:                        mem[0xfffd0 + 0x05] = mouse.call_addr.w.h & 0xff;
                   14066:                        mem[0xfffd0 + 0x06] = mouse.call_addr.w.h >> 8;
                   14067:                } else {
                   14068:                        mem[0xfffd0 + 0x02] = 0x90;     // nop
                   14069:                        mem[0xfffd0 + 0x03] = 0x90;     // nop
                   14070:                        mem[0xfffd0 + 0x04] = 0x90;     // nop
                   14071:                        mem[0xfffd0 + 0x05] = 0x90;     // nop
                   14072:                        mem[0xfffd0 + 0x06] = 0x90;     // nop
                   14073:                }
                   14074:                break;
                   14075:        case 0x6b:
                   14076:                // end of irq12 (mouse)
                   14077:                REG16(AX) = mouse_push_ax;
                   14078:                REG16(BX) = mouse_push_bx;
                   14079:                REG16(CX) = mouse_push_cx;
                   14080:                REG16(DX) = mouse_push_dx;
                   14081:                REG16(SI) = mouse_push_si;
                   14082:                REG16(DI) = mouse_push_di;
                   14083:                
                   14084:                // EOI
                   14085:                if((pic[1].isr &= ~(1 << 4)) == 0) {
                   14086:                        pic[0].isr &= ~(1 << 2); // master
                   14087:                }
                   14088:                pic_update();
                   14089:                break;
                   14090:        case 0x6c:
1.1.1.19  root     14091:                // dummy interrupt for case map routine pointed in the country info
                   14092:                if(REG8(AL) >= 0x80) {
                   14093:                        char tmp[2] = {0};
                   14094:                        tmp[0] = REG8(AL);
                   14095:                        my_strupr(tmp);
                   14096:                        REG8(AL) = tmp[0];
                   14097:                }
                   14098:                break;
1.1.1.27  root     14099:        case 0x6d:
                   14100:                // dummy interrupt for font read routine pointed by int 15h, ax=5000h
                   14101:                REG8(AL) = 0x86; // not supported
                   14102:                m_CF = 1;
                   14103:                break;
1.1.1.32  root     14104:        case 0x6e:
                   14105:                // dummy interrupt for parameter error message read routine pointed by int 2fh, ax=122eh, dl=08h
                   14106:                {
                   14107:                        UINT16 code = REG16(AX);
                   14108:                        if(code & 0xf0) {
                   14109:                                code = (code & 7) | ((code & 0x10) >> 1);
                   14110:                        }
                   14111:                        for(int i = 0; i < array_length(param_error_table); i++) {
                   14112:                                if(param_error_table[i].code == code || param_error_table[i].code == (UINT16)-1) {
                   14113:                                        const char *message = NULL;
                   14114:                                        if(active_code_page == 932) {
                   14115:                                                message = param_error_table[i].message_japanese;
                   14116:                                        }
                   14117:                                        if(message == NULL) {
                   14118:                                                message = param_error_table[i].message_english;
                   14119:                                        }
                   14120:                                        *(UINT8 *)(mem + WORK_TOP) = strlen(message);
                   14121:                                        strcpy((char *)(mem + WORK_TOP + 1), message);
                   14122:                                        
                   14123:                                        SREG(ES) = WORK_TOP >> 4;
                   14124:                                        i386_load_segment_descriptor(ES);
                   14125:                                        REG16(DI) = 0x0000;
                   14126:                                        break;
                   14127:                                }
                   14128:                        }
                   14129:                }
                   14130:                break;
1.1.1.8   root     14131:        case 0x70:
                   14132:        case 0x71:
                   14133:        case 0x72:
                   14134:        case 0x73:
                   14135:        case 0x74:
                   14136:        case 0x75:
                   14137:        case 0x76:
                   14138:        case 0x77:
                   14139:                // EOI
                   14140:                if((pic[1].isr &= ~(1 << (num - 0x70))) == 0) {
                   14141:                        pic[0].isr &= ~(1 << 2); // master
                   14142:                }
                   14143:                pic_update();
                   14144:                break;
1.1       root     14145:        default:
1.1.1.22  root     14146: //             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     14147:                break;
                   14148:        }
                   14149:        
                   14150:        // update cursor position
                   14151:        if(cursor_moved) {
                   14152:                CONSOLE_SCREEN_BUFFER_INFO csbi;
1.1.1.23  root     14153:                GetConsoleScreenBufferInfo(GetStdHandle(STD_OUTPUT_HANDLE), &csbi);
1.1.1.15  root     14154:                if(!restore_console_on_exit) {
                   14155:                        scr_top = csbi.srWindow.Top;
                   14156:                }
1.1       root     14157:                mem[0x450 + mem[0x462] * 2] = csbi.dwCursorPosition.X;
1.1.1.14  root     14158:                mem[0x451 + mem[0x462] * 2] = csbi.dwCursorPosition.Y - scr_top;
1.1       root     14159:                cursor_moved = false;
                   14160:        }
                   14161: }
                   14162: 
                   14163: // init
                   14164: 
                   14165: int msdos_init(int argc, char *argv[], char *envp[], int standard_env)
                   14166: {
                   14167:        // init file handler
                   14168:        memset(file_handler, 0, sizeof(file_handler));
                   14169:        msdos_file_handler_open(0, "STDIN", _isatty(0), 0, 0x80d3, 0);
                   14170:        msdos_file_handler_open(1, "STDOUT", _isatty(1), 1, 0x80d3, 0);
                   14171:        msdos_file_handler_open(2, "STDERR", _isatty(2), 1, 0x80d3, 0);
1.1.1.21  root     14172: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     14173:        if(_open("stdaux.txt", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
1.1.1.21  root     14174: #else
                   14175:        if(_open("NUL", _O_RDWR | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 3) {
                   14176: #endif
                   14177:                msdos_file_handler_open(3, "STDAUX", 0, 2, 0x80c0, 0);
1.1       root     14178:        }
1.1.1.21  root     14179: #ifdef MAP_PRN_DEVICE_TO_FILE
1.1       root     14180:        if(_open("stdprn.txt", _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 4) {
1.1.1.21  root     14181: #else
                   14182:        if(_open("NUL", _O_WRONLY | _O_BINARY | _O_CREAT | _O_TRUNC, _S_IREAD | _S_IWRITE) == 4) {
1.1       root     14183: #endif
1.1.1.21  root     14184:                msdos_file_handler_open(4, "STDPRN", 0, 1, 0xa8c0, 0);
                   14185:        }
1.1       root     14186:        _dup2(0, DUP_STDIN);
                   14187:        _dup2(1, DUP_STDOUT);
                   14188:        _dup2(2, DUP_STDERR);
1.1.1.21  root     14189:        _dup2(3, DUP_STDAUX);
                   14190:        _dup2(4, DUP_STDPRN);
1.1       root     14191:        
1.1.1.24  root     14192:        // init mouse
                   14193:        memset(&mouse, 0, sizeof(mouse));
1.1.1.34! root     14194:        mouse.enabled = true;   // from DOSBox
        !          14195:        mouse.hidden = 1;       // hidden in default ???
        !          14196:        mouse.old_hidden = 1;   // from DOSBox
        !          14197:        mouse.max_position.x = 8 * (scr_width  - 1);
        !          14198:        mouse.max_position.y = 8 * (scr_height - 1);
1.1.1.24  root     14199:        mouse.mickey.x = 8;
                   14200:        mouse.mickey.y = 16;
                   14201:        
1.1.1.26  root     14202: #ifdef SUPPORT_XMS
                   14203:        // init xms
                   14204:        msdos_xms_init();
                   14205: #endif
                   14206:        
1.1       root     14207:        // init process
                   14208:        memset(process, 0, sizeof(process));
                   14209:        
1.1.1.13  root     14210:        // init dtainfo
                   14211:        msdos_dta_info_init();
                   14212:        
1.1       root     14213:        // init memory
                   14214:        memset(mem, 0, sizeof(mem));
                   14215:        
                   14216:        // bios data area
1.1.1.23  root     14217:        HANDLE hStdout = GetStdHandle(STD_OUTPUT_HANDLE);
1.1       root     14218:        CONSOLE_SCREEN_BUFFER_INFO csbi;
                   14219:        GetConsoleScreenBufferInfo(hStdout, &csbi);
1.1.1.14  root     14220:        CONSOLE_FONT_INFO cfi;
                   14221:        GetCurrentConsoleFont(hStdout, FALSE, &cfi);
                   14222:        
                   14223:        int regen = min(scr_width * scr_height * 2, 0x8000);
                   14224:        text_vram_top_address = TEXT_VRAM_TOP;
                   14225:        text_vram_end_address = text_vram_top_address + regen;
                   14226:        shadow_buffer_top_address = SHADOW_BUF_TOP;
                   14227:        shadow_buffer_end_address = shadow_buffer_top_address + regen;
                   14228:        
                   14229:        if(regen > 0x4000) {
                   14230:                regen = 0x8000;
                   14231:                vram_pages = 1;
                   14232:        } else if(regen > 0x2000) {
                   14233:                regen = 0x4000;
                   14234:                vram_pages = 2;
                   14235:        } else if(regen > 0x1000) {
                   14236:                regen = 0x2000;
                   14237:                vram_pages = 4;
                   14238:        } else {
                   14239:                regen = 0x1000;
                   14240:                vram_pages = 8;
                   14241:        }
1.1       root     14242:        
1.1.1.25  root     14243:        *(UINT16 *)(mem + 0x400) = 0x3f8; // com1 port address
                   14244:        *(UINT16 *)(mem + 0x402) = 0x2f8; // com2 port address
1.1.1.29  root     14245:        *(UINT16 *)(mem + 0x404) = 0x3e8; // com3 port address
                   14246:        *(UINT16 *)(mem + 0x406) = 0x2e8; // com4 port address
1.1.1.25  root     14247:        *(UINT16 *)(mem + 0x408) = 0x378; // lpt1 port address
1.1.1.26  root     14248: //     *(UINT16 *)(mem + 0x40a) = 0x278; // lpt2 port address
1.1.1.25  root     14249: //     *(UINT16 *)(mem + 0x40c) = 0x3bc; // lpt3 port address
1.1.1.32  root     14250: #ifdef EXT_BIOS_TOP
1.1.1.25  root     14251:        *(UINT16 *)(mem + 0x40e) = EXT_BIOS_TOP >> 4;
1.1.1.32  root     14252: #endif
1.1.1.26  root     14253:        *(UINT16 *)(mem + 0x410) = msdos_get_equipment();
1.1.1.33  root     14254:        *(UINT16 *)(mem + 0x413) = MEMORY_END >> 10;
1.1       root     14255:        *(UINT8  *)(mem + 0x449) = 0x03;//0x73;
1.1.1.14  root     14256:        *(UINT16 *)(mem + 0x44a) = csbi.dwSize.X;
                   14257:        *(UINT16 *)(mem + 0x44c) = regen;
1.1       root     14258:        *(UINT16 *)(mem + 0x44e) = 0;
                   14259:        *(UINT8  *)(mem + 0x450) = csbi.dwCursorPosition.X;
1.1.1.14  root     14260:        *(UINT8  *)(mem + 0x451) = csbi.dwCursorPosition.Y - scr_top;
1.1       root     14261:        *(UINT8  *)(mem + 0x460) = 7;
                   14262:        *(UINT8  *)(mem + 0x461) = 7;
                   14263:        *(UINT8  *)(mem + 0x462) = 0;
                   14264:        *(UINT16 *)(mem + 0x463) = 0x3d4;
1.1.1.19  root     14265:        *(UINT8  *)(mem + 0x465) = 0x09;
                   14266:        *(UINT32 *)(mem + 0x46c) = get_ticks_since_midnight(timeGetTime());
1.1.1.14  root     14267:        *(UINT8  *)(mem + 0x484) = csbi.srWindow.Bottom - csbi.srWindow.Top;
                   14268:        *(UINT8  *)(mem + 0x485) = cfi.dwFontSize.Y;
                   14269:        *(UINT8  *)(mem + 0x487) = 0x60;
                   14270:        *(UINT8  *)(mem + 0x496) = 0x10; // enhanced keyboard installed
1.1.1.32  root     14271: #ifdef EXT_BIOS_TOP
1.1.1.25  root     14272:        *(UINT16 *)(mem + EXT_BIOS_TOP) = 1;
1.1.1.32  root     14273: #endif
1.1.1.14  root     14274:        
                   14275:        // initial screen
                   14276:        SMALL_RECT rect;
                   14277:        SET_RECT(rect, 0, csbi.srWindow.Top, csbi.dwSize.X - 1, csbi.srWindow.Bottom);
                   14278:        ReadConsoleOutput(hStdout, scr_buf, scr_buf_size, scr_buf_pos, &rect);
                   14279:        for(int y = 0, ofs1 = TEXT_VRAM_TOP, ofs2 = SHADOW_BUF_TOP; y < scr_height; y++) {
                   14280:                for(int x = 0; x < scr_width; x++) {
                   14281:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Char.AsciiChar;
                   14282:                        mem[ofs1++] = mem[ofs2++] = SCR_BUF(y,x).Attributes;
                   14283:                }
                   14284:        }
1.1       root     14285:        
1.1.1.19  root     14286:        // init mcb
1.1       root     14287:        int seg = MEMORY_TOP >> 4;
1.1.1.19  root     14288:        
                   14289:        // iret table
                   14290:        // note: int 2eh vector should address the routine in command.com,
                   14291:        // and some softwares invite (int 2eh vector segment) - 1 must address the mcb of command.com.
                   14292:        // so move iret table into allocated memory block
                   14293:        // http://www5c.biglobe.ne.jp/~ecb/assembler2/2_6.html
1.1.1.33  root     14294:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, IRET_SIZE >> 4);
1.1.1.19  root     14295:        IRET_TOP = seg << 4;
                   14296:        seg += IRET_SIZE >> 4;
1.1.1.25  root     14297:        memset(mem + IRET_TOP, 0xcf, IRET_SIZE); // iret
1.1.1.19  root     14298:        
                   14299:        // dummy xms/ems device
1.1.1.33  root     14300:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, XMS_SIZE >> 4);
1.1.1.19  root     14301:        XMS_TOP = seg << 4;
                   14302:        seg += XMS_SIZE >> 4;
                   14303:        
                   14304:        // environment
1.1.1.33  root     14305:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, ENV_SIZE >> 4);
1.1       root     14306:        int env_seg = seg;
                   14307:        int ofs = 0;
1.1.1.32  root     14308:        char env_append[ENV_SIZE] = {0}, append_added = 0;
                   14309:        char comspec_added = 0;
1.1.1.33  root     14310:        char lastdrive_added = 0;
1.1.1.32  root     14311:        char env_msdos_path[ENV_SIZE] = {0};
                   14312:        char env_path[ENV_SIZE] = {0}, path_added = 0;
1.1.1.33  root     14313:        char prompt_added = 0;
1.1.1.32  root     14314:        char env_temp[ENV_SIZE] = {0}, temp_added = 0, tmp_added = 0;
1.1.1.33  root     14315:        char tz_added = 0;
1.1.1.32  root     14316:        char *path, *short_path;
                   14317:        
                   14318:        if((path = getenv("MSDOS_APPEND")) != NULL) {
                   14319:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   14320:                        strcpy(env_append, short_path);
                   14321:                }
                   14322:        }
                   14323:        if((path = getenv("APPEND")) != NULL) {
                   14324:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   14325:                        if(env_append[0] != '\0') {
                   14326:                                strcat(env_append, ";");
                   14327:                        }
                   14328:                        strcat(env_append, short_path);
                   14329:                }
                   14330:        }
                   14331:        
                   14332:        if((path = msdos_search_command_com(argv[0], env_path)) != NULL) {
                   14333:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   14334:                        strcpy(comspec_path, short_path);
                   14335:                }
                   14336:        }
                   14337:        if((path = getenv("MSDOS_COMSPEC")) != NULL && _access(path, 0) == 0) {
                   14338:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   14339:                        strcpy(comspec_path, short_path);
                   14340:                }
                   14341:        }
1.1       root     14342:        
1.1.1.28  root     14343:        if((path = getenv("MSDOS_PATH")) != NULL) {
1.1.1.32  root     14344:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   14345:                        strcpy(env_msdos_path, short_path);
                   14346:                        strcpy(env_path, short_path);
1.1.1.14  root     14347:                }
                   14348:        }
1.1.1.28  root     14349:        if((path = getenv("PATH")) != NULL) {
1.1.1.32  root     14350:                if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   14351:                        if(env_path[0] != '\0') {
                   14352:                                strcat(env_path, ";");
                   14353:                        }
                   14354:                        strcat(env_path, short_path);
1.1.1.9   root     14355:                }
                   14356:        }
1.1.1.32  root     14357:        
                   14358:        if(GetTempPath(ENV_SIZE, env_temp) != 0) {
                   14359:                strcpy(env_temp, msdos_get_multiple_short_path(env_temp));
1.1.1.15  root     14360:        }
1.1.1.32  root     14361:        for(int i = 0; i < 4; i++) {
                   14362:                static const char *name[4] = {"MSDOS_TEMP", "MSDOS_TMP", "TEMP", "TMP"};
                   14363:                if((path = getenv(name[i])) != NULL && _access(path, 0) == 0) {
                   14364:                        if((short_path = msdos_get_multiple_short_path(path)) != NULL && short_path[0] != '\0') {
                   14365:                                strcpy(env_temp, short_path);
                   14366:                                break;
                   14367:                        }
                   14368:                }
1.1.1.24  root     14369:        }
1.1.1.32  root     14370:        
1.1.1.9   root     14371:        for(char **p = envp; p != NULL && *p != NULL; p++) {
1.1       root     14372:                // lower to upper
1.1.1.28  root     14373:                char tmp[ENV_SIZE], name[ENV_SIZE];
1.1       root     14374:                strcpy(tmp, *p);
                   14375:                for(int i = 0;; i++) {
                   14376:                        if(tmp[i] == '=') {
                   14377:                                tmp[i] = '\0';
                   14378:                                sprintf(name, ";%s;", tmp);
1.1.1.25  root     14379:                                my_strupr(name);
1.1       root     14380:                                tmp[i] = '=';
                   14381:                                break;
                   14382:                        } else if(tmp[i] >= 'a' && tmp[i] <= 'z') {
1.1.1.28  root     14383:                                tmp[i] = (tmp[i] - 'a') + 'A';
1.1       root     14384:                        }
                   14385:                }
1.1.1.33  root     14386:                if(strstr(";MSDOS_APPEND;MSDOS_COMSPEC;MSDOS_LASTDRIVE;MSDOS_TEMP;MSDOS_TMP;MSDOS_TZ;", name) != NULL) {
                   14387:                        // ignore MSDOS_(APPEND/COMSPEC/LASTDRIVE/TEMP/TMP/TZ)
                   14388:                } else if(standard_env && strstr(";APPEND;COMSPEC;LASTDRIVE;MSDOS_PATH;PATH;PROMPT;TEMP;TMP;TZ;", name) == NULL) {
1.1.1.18  root     14389:                        // ignore non standard environments
                   14390:                } else {
1.1.1.33  root     14391:                        if(strncmp(tmp, "APPEND=", 7) == 0) {
1.1.1.32  root     14392:                                if(env_append[0] != '\0') {
                   14393:                                        sprintf(tmp, "APPEND=%s", env_append);
                   14394:                                } else {
                   14395:                                        sprintf(tmp, "APPEND=%s", msdos_get_multiple_short_path(tmp + 7));
                   14396:                                }
                   14397:                                append_added = 1;
                   14398:                        } else if(strncmp(tmp, "COMSPEC=", 8) == 0) {
1.1.1.14  root     14399:                                strcpy(tmp, "COMSPEC=C:\\COMMAND.COM");
1.1.1.32  root     14400:                                comspec_added = 1;
1.1.1.33  root     14401:                        } else if(strncmp(tmp, "LASTDRIVE=", 10) == 0) {
                   14402:                                char *env = getenv("MSDOS_LASTDRIVE");
                   14403:                                if(env != NULL) {
                   14404:                                        sprintf(tmp, "LASTDRIVE=%s", env);
                   14405:                                }
                   14406:                                lastdrive_added = 1;
                   14407:                        } else if(strncmp(tmp, "MSDOS_PATH=", 11) == 0) {
1.1.1.28  root     14408:                                if(env_msdos_path[0] != '\0') {
                   14409:                                        sprintf(tmp, "MSDOS_PATH=%s", env_msdos_path);
                   14410:                                } else {
                   14411:                                        sprintf(tmp, "MSDOS_PATH=%s", msdos_get_multiple_short_path(tmp + 11));
                   14412:                                }
1.1.1.33  root     14413:                        } else if(strncmp(tmp, "PATH=", 5) == 0) {
1.1.1.28  root     14414:                                if(env_path[0] != '\0') {
                   14415:                                        sprintf(tmp, "PATH=%s", env_path);
                   14416:                                } else {
                   14417:                                        sprintf(tmp, "PATH=%s", msdos_get_multiple_short_path(tmp + 5));
                   14418:                                }
1.1.1.32  root     14419:                                path_added = 1;
1.1.1.33  root     14420:                        } else if(strncmp(tmp, "PROMPT=", 7) == 0) {
                   14421:                                prompt_added = 1;
1.1.1.28  root     14422:                        } else if(strncmp(tmp, "TEMP=", 5) == 0) {
                   14423:                                if(env_temp[0] != '\0') {
                   14424:                                        sprintf(tmp, "TEMP=%s", env_temp);
                   14425:                                } else {
                   14426:                                        sprintf(tmp, "TEMP=%s", msdos_get_multiple_short_path(tmp + 5));
                   14427:                                }
1.1.1.32  root     14428:                                temp_added = 1;
1.1.1.33  root     14429:                        } else if(strncmp(tmp, "TMP=", 4) == 0) {
1.1.1.28  root     14430:                                if(env_temp[0] != '\0') {
                   14431:                                        sprintf(tmp, "TMP=%s", env_temp);
                   14432:                                } else {
                   14433:                                        sprintf(tmp, "TMP=%s", msdos_get_multiple_short_path(tmp + 4));
1.1       root     14434:                                }
1.1.1.32  root     14435:                                tmp_added = 1;
1.1.1.33  root     14436:                        } else if(strncmp(tmp, "TZ=", 3) == 0) {
                   14437:                                char *env = getenv("MSDOS_TZ");
                   14438:                                if(env != NULL) {
                   14439:                                        sprintf(tmp, "TZ=%s", env);
                   14440:                                }
                   14441:                                tz_added = 1;
1.1       root     14442:                        }
                   14443:                        int len = strlen(tmp);
1.1.1.14  root     14444:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) {
1.1       root     14445:                                fatalerror("too many environments\n");
                   14446:                        }
                   14447:                        memcpy(mem + (seg << 4) + ofs, tmp, len);
                   14448:                        ofs += len + 1;
                   14449:                }
                   14450:        }
1.1.1.32  root     14451:        if(!append_added && env_append[0] != '\0') {
                   14452:                #define SET_ENV(name, value) { \
                   14453:                        char tmp[ENV_SIZE]; \
                   14454:                        sprintf(tmp, "%s=%s", name, value); \
                   14455:                        int len = strlen(tmp); \
                   14456:                        if(ofs + len + 1 + (2 + (8 + 1 + 3)) + 2 > ENV_SIZE) { \
                   14457:                                fatalerror("too many environments\n"); \
                   14458:                        } \
                   14459:                        memcpy(mem + (seg << 4) + ofs, tmp, len); \
                   14460:                        ofs += len + 1; \
                   14461:                }
                   14462:                SET_ENV("APPEND", env_append);
                   14463:        }
                   14464:        if(!comspec_added) {
                   14465:                SET_ENV("COMSPEC", "C:\\COMMAND.COM");
                   14466:        }
1.1.1.33  root     14467:        if(!lastdrive_added) {
                   14468:                SET_ENV("LASTDRIVE", "Z");
                   14469:        }
1.1.1.32  root     14470:        if(!path_added) {
                   14471:                SET_ENV("PATH", env_path);
                   14472:        }
1.1.1.33  root     14473:        if(!prompt_added) {
                   14474:                SET_ENV("PROMPT", "$P$G");
                   14475:        }
1.1.1.32  root     14476:        if(!temp_added) {
                   14477:                SET_ENV("TEMP", env_temp);
                   14478:        }
                   14479:        if(!tmp_added) {
                   14480:                SET_ENV("TMP", env_temp);
                   14481:        }
1.1.1.33  root     14482:        if(!tz_added) {
                   14483:                TIME_ZONE_INFORMATION tzi;
                   14484:                HKEY hKey, hSubKey;
                   14485:                char tzi_std_name[64];
                   14486:                char tz_std[8] = "GMT";
                   14487:                char tz_dlt[8] = "GST";
                   14488:                char tz_value[32];
                   14489:                
                   14490:                // timezone name from GetTimeZoneInformation may not be english
                   14491:                bool daylight = (GetTimeZoneInformation(&tzi) != TIME_ZONE_ID_UNKNOWN);
                   14492:                setlocale(LC_CTYPE, "");
                   14493:                wcstombs(tzi_std_name, tzi.StandardName, sizeof(tzi_std_name));
                   14494:                
                   14495:                // get english timezone name from registry
                   14496:                if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones", 0, KEY_READ, &hKey) == ERROR_SUCCESS) {
                   14497:                        for(DWORD i = 0; !tz_added; i++) {
                   14498:                                char reg_name[256], sub_key[1024], std_name[256];
                   14499:                                DWORD size;
                   14500:                                FILETIME ftTime;
                   14501:                                LONG result = RegEnumKeyEx(hKey, i, reg_name, &(size = array_length(reg_name)), NULL, NULL, NULL, &ftTime);
                   14502:                                
                   14503:                                if(result == ERROR_SUCCESS) {
                   14504:                                        sprintf(sub_key, "SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\Time Zones\\%s", reg_name);
                   14505:                                        if(RegOpenKeyEx(HKEY_LOCAL_MACHINE, sub_key, 0, KEY_QUERY_VALUE, &hSubKey) == ERROR_SUCCESS) {
                   14506:                                                if(RegQueryValueEx(hSubKey, "Std", NULL, NULL, (LPBYTE)std_name, &(size = array_length(std_name))) == ERROR_SUCCESS) {
                   14507:                                                        // search english timezone name from table
                   14508:                                                        if (strcmp(std_name, tzi_std_name) == 0) {
                   14509:                                                                for(int j = 0; j < array_length(tz_table); j++) {
                   14510:                                                                        if(strcmp(reg_name, tz_table[j].name) == 0 && (tz_table[j].lcid == 0 || tz_table[j].lcid == GetUserDefaultLCID())) {
                   14511:                                                                                if(tz_table[j].std != NULL) {
                   14512:                                                                                        strcpy(tz_std, tz_table[j].std);
                   14513:                                                                                }
                   14514:                                                                                if(tz_table[j].dlt != NULL) {
                   14515:                                                                                        strcpy(tz_dlt, tz_table[j].dlt);
                   14516:                                                                                }
                   14517:                                                                                tz_added = 1;
                   14518:                                                                                break;
                   14519:                                                                        }
                   14520:                                                                }
                   14521:                                                        }
                   14522:                                                }
                   14523:                                                RegCloseKey(hSubKey);
                   14524:                                        }
                   14525:                                } else if(result == ERROR_NO_MORE_ITEMS) {
                   14526:                                        break;
                   14527:                                }
                   14528:                        }
                   14529:                        RegCloseKey(hKey);
                   14530:                }
                   14531:                if((tzi.Bias % 60) != 0) {
                   14532:                        sprintf(tz_value, "%s%d:%2d", tz_std, tzi.Bias / 60, abs(tzi.Bias % 60));
                   14533:                } else {
                   14534:                        sprintf(tz_value, "%s%d", tz_std, tzi.Bias / 60);
                   14535:                }
                   14536:                if(daylight) {
                   14537:                        strcat(tz_value, tz_dlt);
                   14538:                }
                   14539:                SET_ENV("TZ", tz_value);
                   14540:        }
1.1       root     14541:        seg += (ENV_SIZE >> 4);
                   14542:        
                   14543:        // psp
1.1.1.33  root     14544:        msdos_mcb_create(seg++, 'M', PSP_SYSTEM, PSP_SIZE >> 4);
1.1       root     14545:        current_psp = seg;
1.1.1.14  root     14546:        msdos_psp_create(seg, seg + (PSP_SIZE >> 4), -1, env_seg);
1.1       root     14547:        seg += (PSP_SIZE >> 4);
                   14548:        
1.1.1.19  root     14549:        // first free mcb in conventional memory
1.1.1.33  root     14550:        msdos_mcb_create(seg, 'M', 0, (MEMORY_END >> 4) - seg - 2);
1.1.1.8   root     14551:        first_mcb = seg;
                   14552:        
1.1.1.19  root     14553:        // dummy mcb to link to umb
1.1.1.33  root     14554: #if 0
                   14555:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'M', PSP_SYSTEM, (UMB_TOP >> 4) - (MEMORY_END >> 4)); // link umb
                   14556: #else
                   14557:        msdos_mcb_create((MEMORY_END >> 4) - 1, 'Z', PSP_SYSTEM, 0); // unlink umb
                   14558: #endif
1.1.1.19  root     14559:        
                   14560:        // first free mcb in upper memory block
1.1.1.8   root     14561:        msdos_mcb_create(UMB_TOP >> 4, 'Z', 0, (UMB_END >> 4) - (UMB_TOP >> 4) - 1);
1.1       root     14562:        
1.1.1.29  root     14563: #ifdef SUPPORT_HMA
                   14564:        // first free mcb in high memory area
                   14565:        msdos_hma_mcb_create(0x10, 0, 0xffe0, 0);
                   14566: #endif
                   14567:        
1.1.1.26  root     14568:        // interrupt vector
                   14569:        for(int i = 0; i < 0x80; i++) {
                   14570:                *(UINT16 *)(mem + 4 * i + 0) = i;
                   14571:                *(UINT16 *)(mem + 4 * i + 2) = (IRET_TOP >> 4);
                   14572:        }
1.1.1.32  root     14573:        *(UINT16 *)(mem + 4 * 0x08 + 0) = 0x0019;       // fffd:0019 irq0 (system timer)
1.1.1.26  root     14574:        *(UINT16 *)(mem + 4 * 0x08 + 2) = 0xfffd;
                   14575:        *(UINT16 *)(mem + 4 * 0x22 + 0) = 0x0000;       // ffff:0000 boot
                   14576:        *(UINT16 *)(mem + 4 * 0x22 + 2) = 0xffff;
                   14577:        *(UINT16 *)(mem + 4 * 0x67 + 0) = 0x0012;       // xxxx:0012 ems
                   14578:        *(UINT16 *)(mem + 4 * 0x67 + 2) = XMS_TOP >> 4;
                   14579:        *(UINT16 *)(mem + 4 * 0x74 + 0) = 0x0000;       // fffd:0000 irq12 (mouse)
                   14580:        *(UINT16 *)(mem + 4 * 0x74 + 2) = 0xfffd;
                   14581:        
1.1.1.29  root     14582:        // dummy devices (NUL -> CON -> ... -> CONFIG$ -> EMMXXXX0)
1.1.1.26  root     14583:        static const struct {
                   14584:                UINT16 attributes;
                   14585:                char *dev_name;
                   14586:        } dummy_devices[] = {
                   14587:                {0x8013, "CON     "},
                   14588:                {0x8000, "AUX     "},
                   14589:                {0xa0c0, "PRN     "},
                   14590:                {0x8008, "CLOCK$  "},
                   14591:                {0x8000, "COM1    "},
                   14592:                {0xa0c0, "LPT1    "},
                   14593:                {0xa0c0, "LPT2    "},
                   14594:                {0xa0c0, "LPT3    "},
                   14595:                {0x8000, "COM2    "},
                   14596:                {0x8000, "COM3    "},
                   14597:                {0x8000, "COM4    "},
1.1.1.30  root     14598: //             {0xc000, "CONFIG$ "},
                   14599:                {0xc000, "$IBMADSP"}, // for windows3.1 setup.exe
1.1.1.26  root     14600:        };
                   14601:        static const UINT8 dummy_device_routine[] = {
                   14602:                // from NUL device of Windows 98 SE
                   14603:                // or word ptr ES:[BX+03],0100
                   14604:                0x26, 0x81, 0x4f, 0x03, 0x00, 0x01,
                   14605:                // retf
                   14606:                0xcb,
                   14607:        };
1.1.1.29  root     14608:        device_t *last = NULL;
1.1.1.32  root     14609:        for(int i = 0; i < array_length(dummy_devices); i++) {
1.1.1.26  root     14610:                device_t *device = (device_t *)(mem + DEVICE_TOP + 22 + 18 * i);
1.1.1.29  root     14611:                device->next_driver.w.l = 22 + 18 * (i + 1);
                   14612:                device->next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     14613:                device->attributes = dummy_devices[i].attributes;
1.1.1.29  root     14614:                device->strategy = DEVICE_SIZE - sizeof(dummy_device_routine);
                   14615:                device->interrupt = DEVICE_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.26  root     14616:                memcpy(device->dev_name, dummy_devices[i].dev_name, 8);
1.1.1.29  root     14617:                last = device;
                   14618:        }
                   14619:        if(last != NULL) {
                   14620:                last->next_driver.w.l = 0;
                   14621:                last->next_driver.w.h = XMS_TOP >> 4;
1.1.1.26  root     14622:        }
1.1.1.29  root     14623:        memcpy(mem + DEVICE_TOP + DEVICE_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.26  root     14624:        
1.1.1.25  root     14625:        // dos info
                   14626:        dos_info_t *dos_info = (dos_info_t *)(mem + DOS_INFO_TOP);
                   14627:        dos_info->magic_word = 1;
                   14628:        dos_info->first_mcb = MEMORY_TOP >> 4;
                   14629:        dos_info->first_dpb.w.l = 0;
                   14630:        dos_info->first_dpb.w.h = DPB_TOP >> 4;
                   14631:        dos_info->first_sft.w.l = 0;
                   14632:        dos_info->first_sft.w.h = SFT_TOP >> 4;
1.1.1.26  root     14633:        dos_info->clock_device.w.l = 22 + 18 * 3;       // CLOCK$ is the 3rd device in IO.SYS
1.1.1.25  root     14634:        dos_info->clock_device.w.h = DEVICE_TOP >> 4;
1.1.1.26  root     14635:        dos_info->con_device.w.l = 22 + 18 * 0;         // CON is the 1st device in IO.SYS
1.1.1.25  root     14636:        dos_info->con_device.w.h = DEVICE_TOP >> 4;
                   14637:        dos_info->max_sector_len = 512;
                   14638:        dos_info->disk_buf_info.w.l = offsetof(dos_info_t, disk_buf_heads);
                   14639:        dos_info->disk_buf_info.w.h = DOS_INFO_TOP >> 4;
                   14640:        dos_info->cds.w.l = 0;
                   14641:        dos_info->cds.w.h = CDS_TOP >> 4;
                   14642:        dos_info->fcb_table.w.l = 0;
                   14643:        dos_info->fcb_table.w.h = FCB_TABLE_TOP >> 4;
                   14644:        dos_info->last_drive = 'Z' - 'A' + 1;
                   14645:        dos_info->buffers_x = 20;
                   14646:        dos_info->buffers_y = 0;
                   14647:        dos_info->boot_drive = 'C' - 'A' + 1;
1.1.1.29  root     14648:        dos_info->nul_device.next_driver.w.l = 22;
                   14649:        dos_info->nul_device.next_driver.w.h = DEVICE_TOP >> 4;
1.1.1.25  root     14650:        dos_info->nul_device.attributes = 0x8004;
1.1.1.29  root     14651:        dos_info->nul_device.strategy = DOS_INFO_SIZE - sizeof(dummy_device_routine);
                   14652:        dos_info->nul_device.interrupt = DOS_INFO_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     14653:        memcpy(dos_info->nul_device.dev_name, "NUL     ", 8);
                   14654:        dos_info->disk_buf_heads.w.l = 0;
                   14655:        dos_info->disk_buf_heads.w.h = DISK_BUF_TOP >> 4;
                   14656:        dos_info->first_umb_fcb = UMB_TOP >> 4;
                   14657:        dos_info->first_mcb_2 = MEMORY_TOP >> 4;
1.1.1.29  root     14658:        memcpy(mem + DOS_INFO_TOP + DOS_INFO_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.25  root     14659:        
                   14660:        char *env;
                   14661:        if((env = getenv("LASTDRIVE")) != NULL) {
                   14662:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   14663:                        dos_info->last_drive = env[0] - 'A' + 1;
                   14664:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   14665:                        dos_info->last_drive = env[0] - 'a' + 1;
                   14666:                }
                   14667:        }
                   14668:        if((env = getenv("windir")) != NULL) {
                   14669:                if(env[0] >= 'A' && env[0] <= 'Z') {
                   14670:                        dos_info->boot_drive = env[0] - 'A' + 1;
                   14671:                } else if(env[0] >= 'a' && env[0] <= 'z') {
                   14672:                        dos_info->boot_drive = env[0] - 'a' + 1;
                   14673:                }
                   14674:        }
                   14675: #if defined(HAS_I386)
                   14676:        dos_info->i386_or_later = 1;
                   14677: #else
                   14678:        dos_info->i386_or_later = 0;
                   14679: #endif
                   14680:        dos_info->ext_mem_size = (min(MAX_MEM, 0x4000000) - 0x100000) >> 10;
                   14681:        
1.1.1.27  root     14682:        // ems (int 67h) and xms
1.1.1.25  root     14683:        device_t *xms_device = (device_t *)(mem + XMS_TOP);
                   14684:        xms_device->next_driver.w.l = 0xffff;
                   14685:        xms_device->next_driver.w.h = 0xffff;
                   14686:        xms_device->attributes = 0xc000;
1.1.1.29  root     14687:        xms_device->strategy = XMS_SIZE - sizeof(dummy_device_routine);
                   14688:        xms_device->interrupt = XMS_SIZE - sizeof(dummy_device_routine) + 6;
1.1.1.25  root     14689:        memcpy(xms_device->dev_name, "EMMXXXX0", 8);
                   14690:        
1.1.1.26  root     14691:        mem[XMS_TOP + 0x12] = 0xcd;     // int 68h (dummy)
                   14692:        mem[XMS_TOP + 0x13] = 0x68;
                   14693:        mem[XMS_TOP + 0x14] = 0xcf;     // iret
1.1.1.19  root     14694: #ifdef SUPPORT_XMS
                   14695:        if(support_xms) {
1.1.1.26  root     14696:                mem[XMS_TOP + 0x15] = 0xcd;     // int 69h (dummy)
                   14697:                mem[XMS_TOP + 0x16] = 0x69;
                   14698:                mem[XMS_TOP + 0x17] = 0xcb;     // retf
1.1.1.19  root     14699:        } else
                   14700: #endif
1.1.1.26  root     14701:        mem[XMS_TOP + 0x15] = 0xcb;     // retf
1.1.1.29  root     14702:        memcpy(mem + XMS_TOP + XMS_SIZE - sizeof(dummy_device_routine), dummy_device_routine, sizeof(dummy_device_routine));
1.1.1.19  root     14703:        
1.1.1.26  root     14704:        // irq12 routine (mouse)
1.1.1.24  root     14705:        mem[0xfffd0 + 0x00] = 0xcd;     // int 6ah (dummy)
                   14706:        mem[0xfffd0 + 0x01] = 0x6a;
                   14707:        mem[0xfffd0 + 0x02] = 0x9a;     // call far mouse
                   14708:        mem[0xfffd0 + 0x03] = 0xff;
                   14709:        mem[0xfffd0 + 0x04] = 0xff;
                   14710:        mem[0xfffd0 + 0x05] = 0xff;
                   14711:        mem[0xfffd0 + 0x06] = 0xff;
                   14712:        mem[0xfffd0 + 0x07] = 0xcd;     // int 6bh (dummy)
                   14713:        mem[0xfffd0 + 0x08] = 0x6b;
                   14714:        mem[0xfffd0 + 0x09] = 0xcf;     // iret
                   14715:        
1.1.1.27  root     14716:        // case map routine
                   14717:        mem[0xfffd0 + 0x0a] = 0xcd;     // int 6ch (dummy)
                   14718:        mem[0xfffd0 + 0x0b] = 0x6c;
                   14719:        mem[0xfffd0 + 0x0c] = 0xcb;     // retf
                   14720:        
                   14721:        // font read routine
                   14722:        mem[0xfffd0 + 0x0d] = 0xcd;     // int 6dh (dummy)
                   14723:        mem[0xfffd0 + 0x0e] = 0x6d;
                   14724:        mem[0xfffd0 + 0x0f] = 0xcb;     // retf
1.1.1.19  root     14725:        
1.1.1.32  root     14726:        // error message read routine
                   14727:        mem[0xfffd0 + 0x10] = 0xcd;     // int 6eh (dummy)
                   14728:        mem[0xfffd0 + 0x11] = 0x6e;
                   14729:        mem[0xfffd0 + 0x12] = 0xcb;     // retf
                   14730:        
1.1.1.26  root     14731:        // irq0 routine (system time)
1.1.1.32  root     14732:        mem[0xfffd0 + 0x19] = 0xcd;     // int 1ch
                   14733:        mem[0xfffd0 + 0x1a] = 0x1c;
                   14734:        mem[0xfffd0 + 0x1b] = 0xea;     // jmp far (IRET_TOP >> 4):0008
                   14735:        mem[0xfffd0 + 0x1c] = 0x08;
                   14736:        mem[0xfffd0 + 0x1d] = 0x00;
                   14737:        mem[0xfffd0 + 0x1e] = ((IRET_TOP >> 4)     ) & 0xff;
                   14738:        mem[0xfffd0 + 0x1f] = ((IRET_TOP >> 4) >> 8) & 0xff;
1.1.1.14  root     14739:        
1.1.1.26  root     14740:        // boot routine
1.1       root     14741:        mem[0xffff0] = 0xf4;    // halt
                   14742:        mem[0xffff1] = 0xcd;    // int 21h
                   14743:        mem[0xffff2] = 0x21;
                   14744:        mem[0xffff3] = 0xcb;    // retf
                   14745:        
1.1.1.24  root     14746:        mem[0xffff5] = '0';     // rom date
                   14747:        mem[0xffff6] = '2';
                   14748:        mem[0xffff7] = '/';
                   14749:        mem[0xffff8] = '2';
                   14750:        mem[0xffff9] = '2';
                   14751:        mem[0xffffa] = '/';
                   14752:        mem[0xffffb] = '0';
                   14753:        mem[0xffffc] = '6';
                   14754:        mem[0xffffe] = 0xfc;    // machine id
                   14755:        mem[0xfffff] = 0x00;
                   14756:        
1.1       root     14757:        // param block
                   14758:        // + 0: param block (22bytes)
                   14759:        // +24: fcb1/2 (20bytes)
                   14760:        // +44: command tail (128bytes)
                   14761:        param_block_t *param = (param_block_t *)(mem + WORK_TOP);
                   14762:        param->env_seg = 0;
                   14763:        param->cmd_line.w.l = 44;
                   14764:        param->cmd_line.w.h = (WORK_TOP >> 4);
                   14765:        param->fcb1.w.l = 24;
                   14766:        param->fcb1.w.h = (WORK_TOP >> 4);
                   14767:        param->fcb2.w.l = 24;
                   14768:        param->fcb2.w.h = (WORK_TOP >> 4);
                   14769:        
                   14770:        memset(mem + WORK_TOP + 24, 0x20, 20);
                   14771:        
                   14772:        cmd_line_t *cmd_line = (cmd_line_t *)(mem + WORK_TOP + 44);
                   14773:        if(argc > 1) {
                   14774:                sprintf(cmd_line->cmd, " %s", argv[1]);
                   14775:                for(int i = 2; i < argc; i++) {
                   14776:                        char tmp[128];
                   14777:                        sprintf(tmp, "%s %s", cmd_line->cmd, argv[i]);
                   14778:                        strcpy(cmd_line->cmd, tmp);
                   14779:                }
                   14780:                cmd_line->len = (UINT8)strlen(cmd_line->cmd);
                   14781:        } else {
                   14782:                cmd_line->len = 0;
                   14783:        }
                   14784:        cmd_line->cmd[cmd_line->len] = 0x0d;
                   14785:        
                   14786:        // system file table
1.1.1.21  root     14787:        *(UINT32 *)(mem + SFT_TOP + 0) = 0xffffffff;
                   14788:        *(UINT16 *)(mem + SFT_TOP + 4) = 20;
1.1       root     14789:        
1.1.1.19  root     14790:        // disk buffer header (from DOSBox)
                   14791:        *(UINT16 *)(mem + DISK_BUF_TOP +  0) = 0xffff;          // forward ptr
                   14792:        *(UINT16 *)(mem + DISK_BUF_TOP +  2) = 0xffff;          // backward ptr
                   14793:        *(UINT8  *)(mem + DISK_BUF_TOP +  4) = 0xff;            // not in use
                   14794:        *(UINT8  *)(mem + DISK_BUF_TOP + 10) = 0x01;            // number of FATs
                   14795:        *(UINT32 *)(mem + DISK_BUF_TOP + 13) = 0xffffffff;      // pointer to DPB
                   14796:        
1.1       root     14797:        // current directory structure
                   14798:        msdos_cds_update(_getdrive() - 1);
                   14799:        
                   14800:        // fcb table
                   14801:        *(UINT32 *)(mem + FCB_TABLE_TOP + 0) = 0xffffffff;
1.1.1.14  root     14802:        *(UINT16 *)(mem + FCB_TABLE_TOP + 4) = 0;
1.1       root     14803:        
1.1.1.17  root     14804:        // nls stuff
                   14805:        msdos_nls_tables_init();
1.1       root     14806:        
                   14807:        // execute command
1.1.1.28  root     14808:        try {
                   14809:                if(msdos_process_exec(argv[0], param, 0)) {
                   14810:                        fatalerror("'%s' not found\n", argv[0]);
                   14811:                }
                   14812:        } catch(...) {
                   14813:                // we should not reach here :-(
                   14814:                fatalerror("failed to start '%s' because of unexpected exeption\n", argv[0]);
1.1       root     14815:        }
                   14816:        retval = 0;
                   14817:        return(0);
                   14818: }
                   14819: 
                   14820: #define remove_std_file(path) { \
                   14821:        int fd = _open(path, _O_RDONLY | _O_BINARY); \
                   14822:        if(fd != -1) { \
                   14823:                _lseek(fd, 0, SEEK_END); \
                   14824:                int size = _tell(fd); \
                   14825:                _close(fd); \
                   14826:                if(size == 0) { \
                   14827:                        remove(path); \
                   14828:                } \
                   14829:        } \
                   14830: }
                   14831: 
                   14832: void msdos_finish()
                   14833: {
                   14834:        for(int i = 0; i < MAX_FILES; i++) {
                   14835:                if(file_handler[i].valid) {
                   14836:                        _close(i);
                   14837:                }
                   14838:        }
1.1.1.21  root     14839: #ifdef MAP_AUX_DEVICE_TO_FILE
1.1       root     14840:        remove_std_file("stdaux.txt");
1.1.1.21  root     14841: #endif
                   14842: #ifdef MAP_PRN_DEVICE_TO_FILE
1.1       root     14843:        remove_std_file("stdprn.txt");
                   14844: #endif
1.1.1.30  root     14845: #ifdef SUPPORT_XMS
                   14846:        msdos_xms_finish();
                   14847: #endif
1.1       root     14848:        msdos_dbcs_table_finish();
                   14849: }
                   14850: 
                   14851: /* ----------------------------------------------------------------------------
                   14852:        PC/AT hardware emulation
                   14853: ---------------------------------------------------------------------------- */
                   14854: 
                   14855: void hardware_init()
                   14856: {
1.1.1.3   root     14857:        CPU_INIT_CALL(CPU_MODEL);
1.1       root     14858:        CPU_RESET_CALL(CPU_MODEL);
1.1.1.14  root     14859:        m_IF = 1;
1.1.1.3   root     14860: #if defined(HAS_I386)
1.1       root     14861:        cpu_type = (REG32(EDX) >> 8) & 0x0f;
                   14862:        cpu_step = (REG32(EDX) >> 0) & 0x0f;
1.1.1.3   root     14863: #endif
                   14864:        i386_set_a20_line(0);
1.1.1.14  root     14865:        
1.1.1.19  root     14866:        ems_init();
1.1.1.25  root     14867:        dma_init();
1.1       root     14868:        pic_init();
1.1.1.25  root     14869:        pio_init();
1.1.1.8   root     14870: #ifdef PIT_ALWAYS_RUNNING
                   14871:        pit_init();
                   14872: #else
1.1       root     14873:        pit_active = 0;
                   14874: #endif
1.1.1.25  root     14875:        sio_init();
1.1.1.8   root     14876:        cmos_init();
                   14877:        kbd_init();
1.1       root     14878: }
                   14879: 
1.1.1.10  root     14880: void hardware_finish()
                   14881: {
                   14882: #if defined(HAS_I386)
                   14883:        vtlb_free(m_vtlb);
                   14884: #endif
1.1.1.19  root     14885:        ems_finish();
1.1.1.25  root     14886:        sio_finish();
1.1.1.10  root     14887: }
                   14888: 
1.1.1.28  root     14889: void hardware_release()
                   14890: {
                   14891:        // release hardware resources when this program will be terminated abnormally
                   14892: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     14893:        if(fp_debug_log != NULL) {
                   14894:                fclose(fp_debug_log);
                   14895:                fp_debug_log = NULL;
1.1.1.28  root     14896:        }
                   14897: #endif
                   14898: #if defined(HAS_I386)
                   14899:        vtlb_free(m_vtlb);
                   14900: #endif
                   14901:        ems_release();
                   14902:        sio_release();
                   14903: }
                   14904: 
1.1       root     14905: void hardware_run()
                   14906: {
                   14907:        int ops = 0;
                   14908:        
1.1.1.22  root     14909: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.28  root     14910:        // open debug log file after msdos_init() is done not to use the standard file handlers
1.1.1.33  root     14911:        fp_debug_log = fopen("debug.log", "w");
1.1.1.22  root     14912: #endif
1.1.1.3   root     14913:        while(!m_halted) {
                   14914: #if defined(HAS_I386)
                   14915:                m_cycles = 1;
1.1       root     14916:                CPU_EXECUTE_CALL(i386);
1.1.1.3   root     14917: #else
                   14918:                CPU_EXECUTE_CALL(CPU_MODEL);
                   14919: #endif
1.1.1.14  root     14920: #if defined(HAS_I386)
                   14921:                if(m_eip != m_prev_eip) {
                   14922: #else
                   14923:                if(m_pc != m_prevpc) {
                   14924: #endif
                   14925:                        iops++;
                   14926:                }
1.1.1.8   root     14927:                if(++ops == 16384) {
1.1       root     14928:                        hardware_update();
                   14929:                        ops = 0;
                   14930:                }
                   14931:        }
1.1.1.22  root     14932: #ifdef EXPORT_DEBUG_TO_FILE
1.1.1.33  root     14933:        if(fp_debug_log != NULL) {
                   14934:                fclose(fp_debug_log);
                   14935:                fp_debug_log = NULL;
1.1.1.28  root     14936:        }
1.1.1.22  root     14937: #endif
1.1       root     14938: }
                   14939: 
                   14940: void hardware_update()
                   14941: {
1.1.1.8   root     14942:        static UINT32 prev_time = 0;
                   14943:        UINT32 cur_time = timeGetTime();
                   14944:        
                   14945:        if(prev_time != cur_time) {
                   14946:                // update pit and raise irq0
                   14947: #ifndef PIT_ALWAYS_RUNNING
                   14948:                if(pit_active)
                   14949: #endif
                   14950:                {
                   14951:                        if(pit_run(0, cur_time)) {
                   14952:                                pic_req(0, 0, 1);
                   14953:                        }
                   14954:                        pit_run(1, cur_time);
                   14955:                        pit_run(2, cur_time);
                   14956:                }
1.1.1.24  root     14957:                
1.1.1.25  root     14958:                // update sio and raise irq4/3
1.1.1.29  root     14959:                for(int c = 0; c < 4; c++) {
1.1.1.25  root     14960:                        sio_update(c);
                   14961:                }
                   14962:                
1.1.1.24  root     14963:                // update keyboard and mouse
1.1.1.14  root     14964:                static UINT32 prev_tick = 0;
                   14965:                UINT32 cur_tick = cur_time / 32;
1.1.1.25  root     14966:                
1.1.1.14  root     14967:                if(prev_tick != cur_tick) {
                   14968:                        // update keyboard flags
                   14969:                        UINT8 state;
1.1.1.24  root     14970:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x0001) ? 0x80 : 0;
                   14971:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x0001) ? 0x40 : 0;
                   14972:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x0001) ? 0x20 : 0;
                   14973:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x0001) ? 0x10 : 0;
                   14974:                        state |= (GetAsyncKeyState(VK_MENU    ) & 0x8000) ? 0x08 : 0;
                   14975:                        state |= (GetAsyncKeyState(VK_CONTROL ) & 0x8000) ? 0x04 : 0;
                   14976:                        state |= (GetAsyncKeyState(VK_LSHIFT  ) & 0x8000) ? 0x02 : 0;
                   14977:                        state |= (GetAsyncKeyState(VK_RSHIFT  ) & 0x8000) ? 0x01 : 0;
1.1.1.14  root     14978:                        mem[0x417] = state;
                   14979:                        state  = (GetAsyncKeyState(VK_INSERT  ) & 0x8000) ? 0x80 : 0;
                   14980:                        state |= (GetAsyncKeyState(VK_CAPITAL ) & 0x8000) ? 0x40 : 0;
                   14981:                        state |= (GetAsyncKeyState(VK_NUMLOCK ) & 0x8000) ? 0x20 : 0;
                   14982:                        state |= (GetAsyncKeyState(VK_SCROLL  ) & 0x8000) ? 0x10 : 0;
1.1.1.24  root     14983: //                     state |= (GetAsyncKeyState(VK_PAUSE   ) & 0x0001) ? 0x08 : 0;
                   14984: //                     state |= (GetAsyncKeyState(VK_SYSREQ  ) & 0x8000) ? 0x04 : 0;
1.1.1.14  root     14985:                        state |= (GetAsyncKeyState(VK_LMENU   ) & 0x8000) ? 0x02 : 0;
                   14986:                        state |= (GetAsyncKeyState(VK_LCONTROL) & 0x8000) ? 0x01 : 0;
                   14987:                        mem[0x418] = state;
                   14988:                        
1.1.1.24  root     14989:                        // update console input if needed
1.1.1.34! root     14990:                        if(!key_changed || mouse.hidden == 0) {
1.1.1.24  root     14991:                                update_console_input();
                   14992:                        }
                   14993:                        
                   14994:                        // raise irq1 if key is pressed/released
                   14995:                        if(key_changed) {
1.1.1.8   root     14996:                                pic_req(0, 1, 1);
1.1.1.24  root     14997:                                key_changed = false;
                   14998:                        }
                   14999:                        
                   15000:                        // raise irq12 if mouse status is changed
                   15001:                        if(mouse.status & mouse.call_mask) {
1.1.1.34! root     15002: //                             if(mouse.hidden == 0) {
1.1.1.24  root     15003:                                        pic_req(1, 4, 1);
                   15004:                                        mouse.status_irq = mouse.status & mouse.call_mask;
1.1.1.34! root     15005: //                             }
1.1.1.24  root     15006:                                mouse.status &= ~mouse.call_mask;
1.1.1.8   root     15007:                        }
1.1.1.24  root     15008:                        
1.1.1.14  root     15009:                        prev_tick = cur_tick;
1.1.1.8   root     15010:                }
1.1.1.24  root     15011:                
1.1.1.19  root     15012:                // update daily timer counter
                   15013:                pcbios_update_daily_timer_counter(cur_time);
1.1.1.25  root     15014:                
1.1.1.8   root     15015:                prev_time = cur_time;
1.1       root     15016:        }
                   15017: }
                   15018: 
1.1.1.19  root     15019: // ems
                   15020: 
                   15021: void ems_init()
                   15022: {
                   15023:        memset(ems_handles, 0, sizeof(ems_handles));
                   15024:        memset(ems_pages, 0, sizeof(ems_pages));
                   15025:        free_ems_pages = MAX_EMS_PAGES;
                   15026: }
                   15027: 
                   15028: void ems_finish()
                   15029: {
1.1.1.28  root     15030:        ems_release();
                   15031: }
                   15032: 
                   15033: void ems_release()
                   15034: {
1.1.1.31  root     15035:        for(int i = 1; i <= MAX_EMS_HANDLES; i++) {
1.1.1.28  root     15036:                if(ems_handles[i].buffer != NULL) {
1.1.1.19  root     15037:                        free(ems_handles[i].buffer);
                   15038:                        ems_handles[i].buffer = NULL;
                   15039:                }
                   15040:        }
                   15041: }
                   15042: 
                   15043: void ems_allocate_pages(int handle, int pages)
                   15044: {
                   15045:        if(pages > 0) {
                   15046:                ems_handles[handle].buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   15047:        } else {
                   15048:                ems_handles[handle].buffer = NULL;
                   15049:        }
                   15050:        ems_handles[handle].pages = pages;
                   15051:        ems_handles[handle].allocated = true;
                   15052:        free_ems_pages -= pages;
                   15053: }
                   15054: 
                   15055: void ems_reallocate_pages(int handle, int pages)
                   15056: {
                   15057:        if(ems_handles[handle].allocated) {
                   15058:                if(ems_handles[handle].pages != pages) {
                   15059:                        UINT8 *new_buffer = NULL;
                   15060:                        
                   15061:                        if(pages > 0) {
                   15062:                                new_buffer = (UINT8 *)calloc(1, 0x4000 * pages);
                   15063:                        }
1.1.1.32  root     15064:                        if(ems_handles[handle].buffer != NULL) {
                   15065:                                if(new_buffer != NULL) {
1.1.1.19  root     15066:                                        if(pages > ems_handles[handle].pages) {
                   15067:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * ems_handles[handle].pages);
                   15068:                                        } else {
                   15069:                                                memcpy(new_buffer, ems_handles[handle].buffer, 0x4000 * pages);
                   15070:                                        }
                   15071:                                }
                   15072:                                free(ems_handles[handle].buffer);
                   15073:                                ems_handles[handle].buffer = NULL;
                   15074:                        }
                   15075:                        free_ems_pages += ems_handles[handle].pages;
                   15076:                        
                   15077:                        ems_handles[handle].buffer = new_buffer;
                   15078:                        ems_handles[handle].pages = pages;
                   15079:                        free_ems_pages -= pages;
                   15080:                }
                   15081:        } else {
                   15082:                ems_allocate_pages(handle, pages);
                   15083:        }
                   15084: }
                   15085: 
                   15086: void ems_release_pages(int handle)
                   15087: {
                   15088:        if(ems_handles[handle].allocated) {
1.1.1.32  root     15089:                if(ems_handles[handle].buffer != NULL) {
1.1.1.19  root     15090:                        free(ems_handles[handle].buffer);
                   15091:                        ems_handles[handle].buffer = NULL;
                   15092:                }
                   15093:                free_ems_pages += ems_handles[handle].pages;
                   15094:                ems_handles[handle].allocated = false;
                   15095:        }
                   15096: }
                   15097: 
                   15098: void ems_map_page(int physical, int handle, int logical)
                   15099: {
                   15100:        if(ems_pages[physical].mapped) {
                   15101:                if(ems_pages[physical].handle == handle && ems_pages[physical].page == logical) {
                   15102:                        return;
                   15103:                }
                   15104:                ems_unmap_page(physical);
                   15105:        }
1.1.1.32  root     15106:        if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     15107:                memcpy(mem + EMS_TOP + 0x4000 * physical, ems_handles[handle].buffer + 0x4000 * logical, 0x4000);
                   15108:        }
                   15109:        ems_pages[physical].handle = handle;
                   15110:        ems_pages[physical].page = logical;
                   15111:        ems_pages[physical].mapped = true;
                   15112: }
                   15113: 
                   15114: void ems_unmap_page(int physical)
                   15115: {
                   15116:        if(ems_pages[physical].mapped) {
                   15117:                int handle = ems_pages[physical].handle;
                   15118:                int logical = ems_pages[physical].page;
                   15119:                
1.1.1.32  root     15120:                if(ems_handles[handle].allocated && ems_handles[handle].buffer != NULL && logical < ems_handles[handle].pages) {
1.1.1.19  root     15121:                        memcpy(ems_handles[handle].buffer + 0x4000 * logical, mem + EMS_TOP + 0x4000 * physical, 0x4000);
                   15122:                }
                   15123:                ems_pages[physical].mapped = false;
                   15124:        }
                   15125: }
                   15126: 
1.1.1.25  root     15127: // dma
1.1       root     15128: 
1.1.1.25  root     15129: void dma_init()
1.1       root     15130: {
1.1.1.26  root     15131:        memset(dma, 0, sizeof(dma));
1.1.1.25  root     15132:        for(int c = 0; c < 2; c++) {
1.1.1.26  root     15133: //             for(int ch = 0; ch < 4; ch++) {
                   15134: //                     dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w = 0xffff;
                   15135: //             }
1.1.1.25  root     15136:                dma_reset(c);
                   15137:        }
1.1       root     15138: }
                   15139: 
1.1.1.25  root     15140: void dma_reset(int c)
1.1       root     15141: {
1.1.1.25  root     15142:        dma[c].low_high = false;
                   15143:        dma[c].cmd = dma[c].req = dma[c].tc = 0;
                   15144:        dma[c].mask = 0xff;
                   15145: }
                   15146: 
                   15147: void dma_write(int c, UINT32 addr, UINT8 data)
                   15148: {
                   15149:        int ch = (addr >> 1) & 3;
                   15150:        UINT8 bit = 1 << (data & 3);
                   15151:        
                   15152:        switch(addr & 0x0f) {
                   15153:        case 0x00: case 0x02: case 0x04: case 0x06:
                   15154:                if(dma[c].low_high) {
                   15155:                        dma[c].ch[ch].bareg.b.h = data;
1.1       root     15156:                } else {
1.1.1.25  root     15157:                        dma[c].ch[ch].bareg.b.l = data;
                   15158:                }
                   15159:                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   15160:                dma[c].low_high = !dma[c].low_high;
                   15161:                break;
                   15162:        case 0x01: case 0x03: case 0x05: case 0x07:
                   15163:                if(dma[c].low_high) {
                   15164:                        dma[c].ch[ch].bcreg.b.h = data;
                   15165:                } else {
                   15166:                        dma[c].ch[ch].bcreg.b.l = data;
                   15167:                }
                   15168:                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   15169:                dma[c].low_high = !dma[c].low_high;
                   15170:                break;
                   15171:        case 0x08:
                   15172:                // command register
                   15173:                dma[c].cmd = data;
                   15174:                break;
                   15175:        case 0x09:
                   15176:                // dma[c].request register
                   15177:                if(data & 4) {
                   15178:                        if(!(dma[c].req & bit)) {
                   15179:                                dma[c].req |= bit;
                   15180: //                             dma_run(c, ch);
                   15181:                        }
                   15182:                } else {
                   15183:                        dma[c].req &= ~bit;
                   15184:                }
                   15185:                break;
                   15186:        case 0x0a:
                   15187:                // single mask register
                   15188:                if(data & 4) {
                   15189:                        dma[c].mask |= bit;
                   15190:                } else {
                   15191:                        dma[c].mask &= ~bit;
                   15192:                }
                   15193:                break;
                   15194:        case 0x0b:
                   15195:                // mode register
                   15196:                dma[c].ch[data & 3].mode = data;
                   15197:                break;
                   15198:        case 0x0c:
                   15199:                dma[c].low_high = false;
                   15200:                break;
                   15201:        case 0x0d:
                   15202:                // clear master
                   15203:                dma_reset(c);
                   15204:                break;
                   15205:        case 0x0e:
                   15206:                // clear mask register
                   15207:                dma[c].mask = 0;
                   15208:                break;
                   15209:        case 0x0f:
                   15210:                // all mask register
                   15211:                dma[c].mask = data & 0x0f;
                   15212:                break;
                   15213:        }
                   15214: }
                   15215: 
                   15216: UINT8 dma_read(int c, UINT32 addr)
                   15217: {
                   15218:        int ch = (addr >> 1) & 3;
                   15219:        UINT8 val = 0xff;
                   15220:        
                   15221:        switch(addr & 0x0f) {
                   15222:        case 0x00: case 0x02: case 0x04: case 0x06:
                   15223:                if(dma[c].low_high) {
                   15224:                        val = dma[c].ch[ch].areg.b.h;
                   15225:                } else {
                   15226:                        val = dma[c].ch[ch].areg.b.l;
                   15227:                }
                   15228:                dma[c].low_high = !dma[c].low_high;
                   15229:                return(val);
                   15230:        case 0x01: case 0x03: case 0x05: case 0x07:
                   15231:                if(dma[c].low_high) {
                   15232:                        val = dma[c].ch[ch].creg.b.h;
                   15233:                } else {
                   15234:                        val = dma[c].ch[ch].creg.b.l;
                   15235:                }
                   15236:                dma[c].low_high = !dma[c].low_high;
                   15237:                return(val);
                   15238:        case 0x08:
                   15239:                // status register
                   15240:                val = (dma[c].req << 4) | dma[c].tc;
                   15241:                dma[c].tc = 0;
                   15242:                return(val);
                   15243:        case 0x0d:
1.1.1.26  root     15244:                // temporary register (intel 82374 does not support)
1.1.1.25  root     15245:                return(dma[c].tmp & 0xff);
1.1.1.26  root     15246:        case 0x0f:
                   15247:                // mask register (intel 82374 does support)
                   15248:                return(dma[c].mask);
1.1.1.25  root     15249:        }
                   15250:        return(0xff);
                   15251: }
                   15252: 
                   15253: void dma_page_write(int c, int ch, UINT8 data)
                   15254: {
                   15255:        dma[c].ch[ch].pagereg = data;
                   15256: }
                   15257: 
                   15258: UINT8 dma_page_read(int c, int ch)
                   15259: {
                   15260:        return(dma[c].ch[ch].pagereg);
                   15261: }
                   15262: 
                   15263: void dma_run(int c, int ch)
                   15264: {
                   15265:        UINT8 bit = 1 << ch;
                   15266:        
                   15267:        if((dma[c].req & bit) && !(dma[c].mask & bit)) {
                   15268:                // execute dma
                   15269:                while(dma[c].req & bit) {
                   15270:                        if(ch == 0 && (dma[c].cmd & 0x01)) {
                   15271:                                // memory -> memory
                   15272:                                UINT32 saddr = dma[c].ch[0].areg.w | (dma[c].ch[0].pagereg << 16);
                   15273:                                UINT32 daddr = dma[c].ch[1].areg.w | (dma[c].ch[1].pagereg << 16);
                   15274:                                
                   15275:                                if(c == 0) {
                   15276:                                        dma[c].tmp = read_byte(saddr);
                   15277:                                        write_byte(daddr, dma[c].tmp);
                   15278:                                } else {
                   15279:                                        dma[c].tmp = read_word(saddr << 1);
                   15280:                                        write_word(daddr << 1, dma[c].tmp);
                   15281:                                }
                   15282:                                if(!(dma[c].cmd & 0x02)) {
                   15283:                                        if(dma[c].ch[0].mode & 0x20) {
                   15284:                                                dma[c].ch[0].areg.w--;
                   15285:                                                if(dma[c].ch[0].areg.w == 0xffff) {
                   15286:                                                        dma[c].ch[0].pagereg--;
                   15287:                                                }
                   15288:                                        } else {
                   15289:                                                dma[c].ch[0].areg.w++;
                   15290:                                                if(dma[c].ch[0].areg.w == 0) {
                   15291:                                                        dma[c].ch[0].pagereg++;
                   15292:                                                }
                   15293:                                        }
                   15294:                                }
                   15295:                                if(dma[c].ch[1].mode & 0x20) {
                   15296:                                        dma[c].ch[1].areg.w--;
                   15297:                                        if(dma[c].ch[1].areg.w == 0xffff) {
                   15298:                                                dma[c].ch[1].pagereg--;
                   15299:                                        }
                   15300:                                } else {
                   15301:                                        dma[c].ch[1].areg.w++;
                   15302:                                        if(dma[c].ch[1].areg.w == 0) {
                   15303:                                                dma[c].ch[1].pagereg++;
                   15304:                                        }
                   15305:                                }
                   15306:                                
                   15307:                                // check dma condition
                   15308:                                if(dma[c].ch[0].creg.w-- == 0) {
                   15309:                                        if(dma[c].ch[0].mode & 0x10) {
                   15310:                                                // self initialize
                   15311:                                                dma[c].ch[0].areg.w = dma[c].ch[0].bareg.w;
                   15312:                                                dma[c].ch[0].creg.w = dma[c].ch[0].bcreg.w;
                   15313:                                        } else {
                   15314: //                                             dma[c].mask |= bit;
                   15315:                                        }
                   15316:                                }
                   15317:                                if(dma[c].ch[1].creg.w-- == 0) {
                   15318:                                        // terminal count
                   15319:                                        if(dma[c].ch[1].mode & 0x10) {
                   15320:                                                // self initialize
                   15321:                                                dma[c].ch[1].areg.w = dma[c].ch[1].bareg.w;
                   15322:                                                dma[c].ch[1].creg.w = dma[c].ch[1].bcreg.w;
                   15323:                                        } else {
                   15324:                                                dma[c].mask |= bit;
                   15325:                                        }
                   15326:                                        dma[c].req &= ~bit;
                   15327:                                        dma[c].tc |= bit;
                   15328:                                }
                   15329:                        } else {
                   15330:                                UINT32 addr = dma[c].ch[ch].areg.w | (dma[c].ch[ch].pagereg << 16);
                   15331:                                
                   15332:                                if((dma[c].ch[ch].mode & 0x0c) == 0x00) {
                   15333:                                        // verify
                   15334:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x04) {
                   15335:                                        // io -> memory
                   15336:                                        if(c == 0) {
                   15337:                                                dma[c].tmp = read_io_byte(dma[c].ch[ch].port);
                   15338:                                                write_byte(addr, dma[c].tmp);
                   15339:                                        } else {
                   15340:                                                dma[c].tmp = read_io_word(dma[c].ch[ch].port);
                   15341:                                                write_word(addr << 1, dma[c].tmp);
                   15342:                                        }
                   15343:                                } else if((dma[c].ch[ch].mode & 0x0c) == 0x08) {
                   15344:                                        // memory -> io
                   15345:                                        if(c == 0) {
                   15346:                                                dma[c].tmp = read_byte(addr);
                   15347:                                                write_io_byte(dma[c].ch[ch].port, dma[c].tmp);
                   15348:                                        } else {
                   15349:                                                dma[c].tmp = read_word(addr << 1);
                   15350:                                                write_io_word(dma[c].ch[ch].port, dma[c].tmp);
                   15351:                                        }
                   15352:                                }
                   15353:                                if(dma[c].ch[ch].mode & 0x20) {
                   15354:                                        dma[c].ch[ch].areg.w--;
                   15355:                                        if(dma[c].ch[ch].areg.w == 0xffff) {
                   15356:                                                dma[c].ch[ch].pagereg--;
                   15357:                                        }
                   15358:                                } else {
                   15359:                                        dma[c].ch[ch].areg.w++;
                   15360:                                        if(dma[c].ch[ch].areg.w == 0) {
                   15361:                                                dma[c].ch[ch].pagereg++;
                   15362:                                        }
                   15363:                                }
                   15364:                                
                   15365:                                // check dma condition
                   15366:                                if(dma[c].ch[ch].creg.w-- == 0) {
                   15367:                                        // terminal count
                   15368:                                        if(dma[c].ch[ch].mode & 0x10) {
                   15369:                                                // self initialize
                   15370:                                                dma[c].ch[ch].areg.w = dma[c].ch[ch].bareg.w;
                   15371:                                                dma[c].ch[ch].creg.w = dma[c].ch[ch].bcreg.w;
                   15372:                                        } else {
                   15373:                                                dma[c].mask |= bit;
                   15374:                                        }
                   15375:                                        dma[c].req &= ~bit;
                   15376:                                        dma[c].tc |= bit;
                   15377:                                } else if((dma[c].ch[ch].mode & 0xc0) == 0x40) {
                   15378:                                        // single mode
                   15379:                                        break;
                   15380:                                }
                   15381:                        }
                   15382:                }
                   15383:        }
                   15384: }
                   15385: 
                   15386: // pic
                   15387: 
                   15388: void pic_init()
                   15389: {
                   15390:        memset(pic, 0, sizeof(pic));
                   15391:        pic[0].imr = pic[1].imr = 0xff;
                   15392:        
                   15393:        // from bochs bios
                   15394:        pic_write(0, 0, 0x11);  // icw1 = 11h
                   15395:        pic_write(0, 1, 0x08);  // icw2 = 08h
                   15396:        pic_write(0, 1, 0x04);  // icw3 = 04h
                   15397:        pic_write(0, 1, 0x01);  // icw4 = 01h
                   15398:        pic_write(0, 1, 0xb8);  // ocw1 = b8h
                   15399:        pic_write(1, 0, 0x11);  // icw1 = 11h
                   15400:        pic_write(1, 1, 0x70);  // icw2 = 70h
                   15401:        pic_write(1, 1, 0x02);  // icw3 = 02h
                   15402:        pic_write(1, 1, 0x01);  // icw4 = 01h
                   15403: }
                   15404: 
                   15405: void pic_write(int c, UINT32 addr, UINT8 data)
                   15406: {
                   15407:        if(addr & 1) {
                   15408:                if(pic[c].icw2_r) {
                   15409:                        // icw2
                   15410:                        pic[c].icw2 = data;
                   15411:                        pic[c].icw2_r = 0;
                   15412:                } else if(pic[c].icw3_r) {
                   15413:                        // icw3
                   15414:                        pic[c].icw3 = data;
                   15415:                        pic[c].icw3_r = 0;
                   15416:                } else if(pic[c].icw4_r) {
                   15417:                        // icw4
                   15418:                        pic[c].icw4 = data;
                   15419:                        pic[c].icw4_r = 0;
                   15420:                } else {
                   15421:                        // ocw1
1.1       root     15422:                        pic[c].imr = data;
                   15423:                }
                   15424:        } else {
                   15425:                if(data & 0x10) {
                   15426:                        // icw1
                   15427:                        pic[c].icw1 = data;
                   15428:                        pic[c].icw2_r = 1;
                   15429:                        pic[c].icw3_r = (data & 2) ? 0 : 1;
                   15430:                        pic[c].icw4_r = data & 1;
                   15431:                        pic[c].irr = 0;
                   15432:                        pic[c].isr = 0;
                   15433:                        pic[c].imr = 0;
                   15434:                        pic[c].prio = 0;
                   15435:                        if(!(pic[c].icw1 & 1)) {
                   15436:                                pic[c].icw4 = 0;
                   15437:                        }
                   15438:                        pic[c].ocw3 = 0;
                   15439:                } else if(data & 8) {
                   15440:                        // ocw3
                   15441:                        if(!(data & 2)) {
                   15442:                                data = (data & ~1) | (pic[c].ocw3 & 1);
                   15443:                        }
                   15444:                        if(!(data & 0x40)) {
                   15445:                                data = (data & ~0x20) | (pic[c].ocw3 & 0x20);
                   15446:                        }
                   15447:                        pic[c].ocw3 = data;
                   15448:                } else {
                   15449:                        // ocw2
                   15450:                        int level = 0;
                   15451:                        if(data & 0x40) {
                   15452:                                level = data & 7;
                   15453:                        } else {
                   15454:                                if(!pic[c].isr) {
                   15455:                                        return;
                   15456:                                }
                   15457:                                level = pic[c].prio;
                   15458:                                while(!(pic[c].isr & (1 << level))) {
                   15459:                                        level = (level + 1) & 7;
                   15460:                                }
                   15461:                        }
                   15462:                        if(data & 0x80) {
                   15463:                                pic[c].prio = (level + 1) & 7;
                   15464:                        }
                   15465:                        if(data & 0x20) {
                   15466:                                pic[c].isr &= ~(1 << level);
                   15467:                        }
                   15468:                }
                   15469:        }
                   15470:        pic_update();
                   15471: }
                   15472: 
                   15473: UINT8 pic_read(int c, UINT32 addr)
                   15474: {
                   15475:        if(addr & 1) {
                   15476:                return(pic[c].imr);
                   15477:        } else {
                   15478:                // polling mode is not supported...
                   15479:                //if(pic[c].ocw3 & 4) {
                   15480:                //      return ???;
                   15481:                //}
                   15482:                if(pic[c].ocw3 & 1) {
                   15483:                        return(pic[c].isr);
                   15484:                } else {
                   15485:                        return(pic[c].irr);
                   15486:                }
                   15487:        }
                   15488: }
                   15489: 
                   15490: void pic_req(int c, int level, int signal)
                   15491: {
                   15492:        if(signal) {
                   15493:                pic[c].irr |= (1 << level);
                   15494:        } else {
                   15495:                pic[c].irr &= ~(1 << level);
                   15496:        }
                   15497:        pic_update();
                   15498: }
                   15499: 
                   15500: int pic_ack()
                   15501: {
                   15502:        // ack (INTA=L)
                   15503:        pic[pic_req_chip].isr |= pic_req_bit;
                   15504:        pic[pic_req_chip].irr &= ~pic_req_bit;
                   15505:        if(pic_req_chip > 0) {
                   15506:                // update isr and irr of master
                   15507:                UINT8 slave = 1 << (pic[pic_req_chip].icw3 & 7);
                   15508:                pic[pic_req_chip - 1].isr |= slave;
                   15509:                pic[pic_req_chip - 1].irr &= ~slave;
                   15510:        }
                   15511:        //if(pic[pic_req_chip].icw4 & 1) {
                   15512:                // 8086 mode
                   15513:                int vector = (pic[pic_req_chip].icw2 & 0xf8) | pic_req_level;
                   15514:        //} else {
                   15515:        //      // 8080 mode
                   15516:        //      UINT16 addr = (UINT16)pic[pic_req_chip].icw2 << 8;
                   15517:        //      if(pic[pic_req_chip].icw1 & 4) {
                   15518:        //              addr |= (pic[pic_req_chip].icw1 & 0xe0) | (pic_req_level << 2);
                   15519:        //      } else {
                   15520:        //              addr |= (pic[pic_req_chip].icw1 & 0xc0) | (pic_req_level << 3);
                   15521:        //      }
                   15522:        //      vector = 0xcd | (addr << 8);
                   15523:        //}
                   15524:        if(pic[pic_req_chip].icw4 & 2) {
                   15525:                // auto eoi
                   15526:                pic[pic_req_chip].isr &= ~pic_req_bit;
                   15527:        }
                   15528:        return(vector);
                   15529: }
                   15530: 
                   15531: void pic_update()
                   15532: {
                   15533:        for(int c = 0; c < 2; c++) {
                   15534:                UINT8 irr = pic[c].irr;
                   15535:                if(c + 1 < 2) {
                   15536:                        // this is master
                   15537:                        if(pic[c + 1].irr & (~pic[c + 1].imr)) {
                   15538:                                // request from slave
                   15539:                                irr |= 1 << (pic[c + 1].icw3 & 7);
                   15540:                        }
                   15541:                }
                   15542:                irr &= (~pic[c].imr);
                   15543:                if(!irr) {
                   15544:                        break;
                   15545:                }
                   15546:                if(!(pic[c].ocw3 & 0x20)) {
                   15547:                        irr |= pic[c].isr;
                   15548:                }
                   15549:                int level = pic[c].prio;
                   15550:                UINT8 bit = 1 << level;
                   15551:                while(!(irr & bit)) {
                   15552:                        level = (level + 1) & 7;
                   15553:                        bit = 1 << level;
                   15554:                }
                   15555:                if((c + 1 < 2) && (pic[c].icw3 & bit)) {
                   15556:                        // check slave
                   15557:                        continue;
                   15558:                }
                   15559:                if(pic[c].isr & bit) {
                   15560:                        break;
                   15561:                }
                   15562:                // interrupt request
                   15563:                pic_req_chip = c;
                   15564:                pic_req_level = level;
                   15565:                pic_req_bit = bit;
1.1.1.3   root     15566:                i386_set_irq_line(INPUT_LINE_IRQ, HOLD_LINE);
1.1       root     15567:                return;
                   15568:        }
1.1.1.3   root     15569:        i386_set_irq_line(INPUT_LINE_IRQ, CLEAR_LINE);
1.1.1.2   root     15570: }
1.1       root     15571: 
1.1.1.25  root     15572: // pio
                   15573: 
                   15574: void pio_init()
                   15575: {
1.1.1.26  root     15576:        memset(pio, 0, sizeof(pio));
1.1.1.25  root     15577:        for(int c = 0; c < 2; c++) {
                   15578:                pio[c].stat = 0xde;
                   15579:                pio[c].ctrl = 0x0c;
                   15580:        }
                   15581: }
                   15582: 
                   15583: void pio_write(int c, UINT32 addr, UINT8 data)
                   15584: {
                   15585:        switch(addr & 3) {
                   15586:        case 0:
                   15587:                pio[c].data = data;
                   15588:                break;
                   15589:        case 2:
                   15590:                pio[c].ctrl = data;
                   15591:                break;
                   15592:        }
                   15593: }
                   15594: 
                   15595: UINT8 pio_read(int c, UINT32 addr)
                   15596: {
                   15597:        switch(addr & 3) {
                   15598:        case 0:
                   15599:                return(pio[c].data);
                   15600:        case 1:
                   15601:                return(pio[c].stat);
                   15602:        case 2:
                   15603:                return(pio[c].ctrl);
                   15604:        }
                   15605:        return(0xff);
                   15606: }
                   15607: 
1.1       root     15608: // pit
                   15609: 
1.1.1.22  root     15610: #define PIT_FREQ 1193182ULL
1.1       root     15611: #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)
                   15612: 
                   15613: void pit_init()
                   15614: {
1.1.1.8   root     15615:        memset(pit, 0, sizeof(pit));
1.1       root     15616:        for(int ch = 0; ch < 3; ch++) {
                   15617:                pit[ch].count = 0x10000;
                   15618:                pit[ch].ctrl_reg = 0x34;
                   15619:                pit[ch].mode = 3;
                   15620:        }
                   15621:        
                   15622:        // from bochs bios
                   15623:        pit_write(3, 0x34);
                   15624:        pit_write(0, 0x00);
                   15625:        pit_write(0, 0x00);
                   15626: }
                   15627: 
                   15628: void pit_write(int ch, UINT8 val)
                   15629: {
1.1.1.8   root     15630: #ifndef PIT_ALWAYS_RUNNING
1.1       root     15631:        if(!pit_active) {
                   15632:                pit_active = 1;
                   15633:                pit_init();
                   15634:        }
1.1.1.8   root     15635: #endif
1.1       root     15636:        switch(ch) {
                   15637:        case 0:
                   15638:        case 1:
                   15639:        case 2:
                   15640:                // write count register
                   15641:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   15642:                        if(pit[ch].ctrl_reg & 0x10) {
                   15643:                                pit[ch].low_write = 1;
                   15644:                        }
                   15645:                        if(pit[ch].ctrl_reg & 0x20) {
                   15646:                                pit[ch].high_write = 1;
                   15647:                        }
                   15648:                }
                   15649:                if(pit[ch].low_write) {
                   15650:                        pit[ch].count_reg = val;
                   15651:                        pit[ch].low_write = 0;
                   15652:                } else if(pit[ch].high_write) {
                   15653:                        if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   15654:                                pit[ch].count_reg = val << 8;
                   15655:                        } else {
                   15656:                                pit[ch].count_reg |= val << 8;
                   15657:                        }
                   15658:                        pit[ch].high_write = 0;
                   15659:                }
                   15660:                // start count
1.1.1.8   root     15661:                if(!pit[ch].low_write && !pit[ch].high_write) {
                   15662:                        if(pit[ch].mode == 0 || pit[ch].mode == 4 || pit[ch].prev_time == 0) {
                   15663:                                pit[ch].count = PIT_COUNT_VALUE(ch);
                   15664:                                pit[ch].prev_time = timeGetTime();
                   15665:                                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     15666:                        }
                   15667:                }
                   15668:                break;
                   15669:        case 3: // ctrl reg
                   15670:                if((val & 0xc0) == 0xc0) {
                   15671:                        // i8254 read-back command
                   15672:                        for(ch = 0; ch < 3; ch++) {
                   15673:                                if(!(val & 0x10) && !pit[ch].status_latched) {
                   15674:                                        pit[ch].status = pit[ch].ctrl_reg & 0x3f;
                   15675:                                        pit[ch].status_latched = 1;
                   15676:                                }
                   15677:                                if(!(val & 0x20) && !pit[ch].count_latched) {
                   15678:                                        pit_latch_count(ch);
                   15679:                                }
                   15680:                        }
                   15681:                        break;
                   15682:                }
                   15683:                ch = (val >> 6) & 3;
                   15684:                if(val & 0x30) {
                   15685:                        static int modes[8] = {0, 1, 2, 3, 4, 5, 2, 3};
                   15686:                        pit[ch].mode = modes[(val >> 1) & 7];
                   15687:                        pit[ch].count_latched = 0;
                   15688:                        pit[ch].low_read = pit[ch].high_read = 0;
                   15689:                        pit[ch].low_write = pit[ch].high_write = 0;
                   15690:                        pit[ch].ctrl_reg = val;
                   15691:                        // stop count
1.1.1.8   root     15692:                        pit[ch].prev_time = pit[ch].expired_time = 0;
1.1       root     15693:                        pit[ch].count_reg = 0;
                   15694:                } else if(!pit[ch].count_latched) {
                   15695:                        pit_latch_count(ch);
                   15696:                }
                   15697:                break;
                   15698:        }
                   15699: }
                   15700: 
                   15701: UINT8 pit_read(int ch)
                   15702: {
1.1.1.8   root     15703: #ifndef PIT_ALWAYS_RUNNING
1.1       root     15704:        if(!pit_active) {
                   15705:                pit_active = 1;
                   15706:                pit_init();
                   15707:        }
1.1.1.8   root     15708: #endif
1.1       root     15709:        switch(ch) {
                   15710:        case 0:
                   15711:        case 1:
                   15712:        case 2:
                   15713:                if(pit[ch].status_latched) {
                   15714:                        pit[ch].status_latched = 0;
                   15715:                        return(pit[ch].status);
                   15716:                }
                   15717:                // if not latched, through current count
                   15718:                if(!pit[ch].count_latched) {
                   15719:                        if(!pit[ch].low_read && !pit[ch].high_read) {
                   15720:                                pit_latch_count(ch);
                   15721:                        }
                   15722:                }
                   15723:                // return latched count
                   15724:                if(pit[ch].low_read) {
                   15725:                        pit[ch].low_read = 0;
                   15726:                        if(!pit[ch].high_read) {
                   15727:                                pit[ch].count_latched = 0;
                   15728:                        }
                   15729:                        return(pit[ch].latch & 0xff);
                   15730:                } else if(pit[ch].high_read) {
                   15731:                        pit[ch].high_read = 0;
                   15732:                        pit[ch].count_latched = 0;
                   15733:                        return((pit[ch].latch >> 8) & 0xff);
                   15734:                }
                   15735:        }
                   15736:        return(0xff);
                   15737: }
                   15738: 
1.1.1.8   root     15739: int pit_run(int ch, UINT32 cur_time)
1.1       root     15740: {
1.1.1.8   root     15741:        if(pit[ch].expired_time != 0 && cur_time >= pit[ch].expired_time) {
1.1       root     15742:                pit[ch].count = PIT_COUNT_VALUE(ch);
1.1.1.8   root     15743:                pit[ch].prev_time = pit[ch].expired_time;
                   15744:                pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
                   15745:                if(cur_time >= pit[ch].expired_time) {
                   15746:                        pit[ch].prev_time = cur_time;
                   15747:                        pit[ch].expired_time = pit[ch].prev_time + pit_get_expired_time(ch);
1.1       root     15748:                }
1.1.1.8   root     15749:                return(1);
1.1       root     15750:        }
1.1.1.8   root     15751:        return(0);
1.1       root     15752: }
                   15753: 
                   15754: void pit_latch_count(int ch)
                   15755: {
1.1.1.8   root     15756:        if(pit[ch].expired_time != 0) {
1.1.1.26  root     15757:                UINT32 cur_time = timeGetTime();
1.1.1.8   root     15758:                pit_run(ch, cur_time);
                   15759:                UINT32 tmp = (pit[ch].count * (pit[ch].expired_time - cur_time)) / (pit[ch].expired_time - pit[ch].prev_time);
1.1.1.26  root     15760:                UINT16 latch = (tmp != 0) ? (UINT16)tmp : 1;
                   15761:                
                   15762:                if(pit[ch].prev_latch == latch && pit[ch].expired_time > cur_time) {
                   15763:                        // decrement counter in 1msec period
                   15764:                        if(pit[ch].next_latch == 0) {
                   15765:                                tmp = (pit[ch].count * (pit[ch].expired_time - cur_time - 1)) / (pit[ch].expired_time - pit[ch].prev_time);
                   15766:                                pit[ch].next_latch = (tmp != 0) ? (UINT16)tmp : 1;
                   15767:                        }
                   15768:                        if(pit[ch].latch > pit[ch].next_latch) {
                   15769:                                pit[ch].latch--;
                   15770:                        }
                   15771:                } else {
                   15772:                        pit[ch].prev_latch = pit[ch].latch = latch;
                   15773:                        pit[ch].next_latch = 0;
                   15774:                }
1.1.1.8   root     15775:        } else {
                   15776:                pit[ch].latch = (UINT16)pit[ch].count;
1.1.1.26  root     15777:                pit[ch].prev_latch = pit[ch].next_latch = 0;
1.1       root     15778:        }
                   15779:        pit[ch].count_latched = 1;
                   15780:        if((pit[ch].ctrl_reg & 0x30) == 0x10) {
                   15781:                // lower byte
                   15782:                pit[ch].low_read = 1;
                   15783:                pit[ch].high_read = 0;
                   15784:        } else if((pit[ch].ctrl_reg & 0x30) == 0x20) {
                   15785:                // upper byte
                   15786:                pit[ch].low_read = 0;
                   15787:                pit[ch].high_read = 1;
                   15788:        } else {
                   15789:                // lower -> upper
1.1.1.14  root     15790:                pit[ch].low_read = pit[ch].high_read = 1;
1.1       root     15791:        }
                   15792: }
                   15793: 
1.1.1.8   root     15794: int pit_get_expired_time(int ch)
1.1       root     15795: {
1.1.1.22  root     15796:        pit[ch].accum += 1024ULL * 1000ULL * (UINT64)pit[ch].count / PIT_FREQ;
                   15797:        UINT64 val = pit[ch].accum >> 10;
                   15798:        pit[ch].accum -= val << 10;
                   15799:        return((val != 0) ? val : 1);
1.1.1.8   root     15800: }
                   15801: 
1.1.1.25  root     15802: // sio
                   15803: 
                   15804: void sio_init()
                   15805: {
1.1.1.26  root     15806:        memset(sio, 0, sizeof(sio));
                   15807:        memset(sio_mt, 0, sizeof(sio_mt));
                   15808:        
1.1.1.29  root     15809:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     15810:                sio[c].send_buffer = new FIFO(SIO_BUFFER_SIZE);
                   15811:                sio[c].recv_buffer = new FIFO(SIO_BUFFER_SIZE);
                   15812:                
                   15813:                sio[c].divisor.w = 12;          // 115200Hz / 9600Baud
                   15814:                sio[c].line_ctrl = 0x03;        // 8bit, stop 1bit, non parity
1.1.1.26  root     15815:                sio[c].modem_ctrl = 0x03;       // rts=on, dtr=on
                   15816:                sio[c].set_rts = sio[c].set_dtr = true;
1.1.1.25  root     15817:                sio[c].line_stat_buf = 0x60;    // send/recv buffers are empty
                   15818:                sio[c].irq_identify = 0x01;     // no pending irq
                   15819:                
                   15820:                InitializeCriticalSection(&sio_mt[c].csSendData);
                   15821:                InitializeCriticalSection(&sio_mt[c].csRecvData);
                   15822:                InitializeCriticalSection(&sio_mt[c].csLineCtrl);
                   15823:                InitializeCriticalSection(&sio_mt[c].csLineStat);
                   15824:                InitializeCriticalSection(&sio_mt[c].csModemCtrl);
                   15825:                InitializeCriticalSection(&sio_mt[c].csModemStat);
                   15826:                
1.1.1.26  root     15827:                if(sio_port_number[c] != 0) {
1.1.1.25  root     15828:                        sio[c].channel = c;
                   15829:                        sio_mt[c].hThread = CreateThread(NULL, 0, sio_thread, &sio[c], 0, NULL);
                   15830:                }
                   15831:        }
                   15832: }
                   15833: 
                   15834: void sio_finish()
                   15835: {
1.1.1.29  root     15836:        for(int c = 0; c < 4; c++) {
1.1.1.25  root     15837:                if(sio_mt[c].hThread != NULL) {
                   15838:                        WaitForSingleObject(sio_mt[c].hThread, INFINITE);
                   15839:                        CloseHandle(sio_mt[c].hThread);
1.1.1.28  root     15840:                        sio_mt[c].hThread = NULL;
1.1.1.25  root     15841:                }
                   15842:                DeleteCriticalSection(&sio_mt[c].csSendData);
                   15843:                DeleteCriticalSection(&sio_mt[c].csRecvData);
                   15844:                DeleteCriticalSection(&sio_mt[c].csLineCtrl);
                   15845:                DeleteCriticalSection(&sio_mt[c].csLineStat);
                   15846:                DeleteCriticalSection(&sio_mt[c].csModemCtrl);
                   15847:                DeleteCriticalSection(&sio_mt[c].csModemStat);
1.1.1.28  root     15848:        }
                   15849:        sio_release();
                   15850: }
                   15851: 
                   15852: void sio_release()
                   15853: {
1.1.1.29  root     15854:        for(int c = 0; c < 4; c++) {
1.1.1.28  root     15855:                // sio_thread() may access the resources :-(
1.1.1.32  root     15856:                bool running = (sio_mt[c].hThread != NULL);
                   15857:                
                   15858:                if(running) {
                   15859:                        EnterCriticalSection(&sio_mt[c].csSendData);
                   15860:                }
                   15861:                if(sio[c].send_buffer != NULL) {
                   15862:                        sio[c].send_buffer->release();
                   15863:                        delete sio[c].send_buffer;
                   15864:                        sio[c].send_buffer = NULL;
                   15865:                }
                   15866:                if(running) {
                   15867:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   15868:                        EnterCriticalSection(&sio_mt[c].csRecvData);
                   15869:                }
                   15870:                if(sio[c].recv_buffer != NULL) {
                   15871:                        sio[c].recv_buffer->release();
                   15872:                        delete sio[c].recv_buffer;
                   15873:                        sio[c].recv_buffer = NULL;
                   15874:                }
                   15875:                if(running) {
                   15876:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
1.1.1.28  root     15877:                }
1.1.1.25  root     15878:        }
                   15879: }
                   15880: 
                   15881: void sio_write(int c, UINT32 addr, UINT8 data)
                   15882: {
                   15883:        switch(addr & 7) {
                   15884:        case 0:
                   15885:                if(sio[c].selector & 0x80) {
                   15886:                        if(sio[c].divisor.b.l != data) {
                   15887:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   15888:                                sio[c].divisor.b.l = data;
                   15889:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   15890:                        }
                   15891:                } else {
                   15892:                        EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     15893:                        if(sio[c].send_buffer != NULL) {
                   15894:                                sio[c].send_buffer->write(data);
                   15895:                        }
1.1.1.25  root     15896:                        // transmitter holding/shift registers are not empty
                   15897:                        sio[c].line_stat_buf &= ~0x60;
                   15898:                        LeaveCriticalSection(&sio_mt[c].csSendData);
                   15899:                        
                   15900:                        if(sio[c].irq_enable & 0x02) {
                   15901:                                sio_update_irq(c);
                   15902:                        }
                   15903:                }
                   15904:                break;
                   15905:        case 1:
                   15906:                if(sio[c].selector & 0x80) {
                   15907:                        if(sio[c].divisor.b.h != data) {
                   15908:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   15909:                                sio[c].divisor.b.h = data;
                   15910:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   15911:                        }
                   15912:                } else {
                   15913:                        if(sio[c].irq_enable != data) {
                   15914:                                sio[c].irq_enable = data;
                   15915:                                sio_update_irq(c);
                   15916:                        }
                   15917:                }
                   15918:                break;
                   15919:        case 3:
                   15920:                {
                   15921:                        UINT8 line_ctrl = data & 0x3f;
                   15922:                        bool set_brk = ((data & 0x40) != 0);
                   15923:                        
                   15924:                        if(sio[c].line_ctrl != line_ctrl) {
                   15925:                                EnterCriticalSection(&sio_mt[c].csLineCtrl);
                   15926:                                sio[c].line_ctrl = line_ctrl;
                   15927:                                LeaveCriticalSection(&sio_mt[c].csLineCtrl);
                   15928:                        }
                   15929:                        if(sio[c].set_brk != set_brk) {
                   15930:                                EnterCriticalSection(&sio_mt[c].csModemCtrl);
                   15931:                                sio[c].set_brk = set_brk;
                   15932:                                LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   15933:                        }
                   15934:                }
                   15935:                sio[c].selector = data;
                   15936:                break;
                   15937:        case 4:
                   15938:                {
                   15939:                        bool set_dtr = ((data & 0x01) != 0);
                   15940:                        bool set_rts = ((data & 0x02) != 0);
                   15941:                        
                   15942:                        if(sio[c].set_dtr != set_dtr || sio[c].set_rts != set_rts) {
1.1.1.26  root     15943: //                             EnterCriticalSection(&sio_mt[c].csModemCtrl);
1.1.1.25  root     15944:                                sio[c].set_dtr = set_dtr;
                   15945:                                sio[c].set_rts = set_rts;
1.1.1.26  root     15946: //                             LeaveCriticalSection(&sio_mt[c].csModemCtrl);
                   15947:                                
                   15948:                                bool state_changed = false;
                   15949:                                
                   15950:                                EnterCriticalSection(&sio_mt[c].csModemStat);
                   15951:                                if(set_dtr) {
                   15952:                                        sio[c].modem_stat |= 0x20;      // dsr on
                   15953:                                } else {
                   15954:                                        sio[c].modem_stat &= ~0x20;     // dsr off
                   15955:                                }
                   15956:                                if(set_rts) {
                   15957:                                        sio[c].modem_stat |= 0x10;      // cts on
                   15958:                                } else {
                   15959:                                        sio[c].modem_stat &= ~0x10;     // cts off
                   15960:                                }
                   15961:                                if((sio[c].prev_modem_stat & 0x20) != (sio[c].modem_stat & 0x20)) {
                   15962:                                        if(!(sio[c].modem_stat & 0x02)) {
                   15963:                                                if(sio[c].irq_enable & 0x08) {
                   15964:                                                        state_changed = true;
                   15965:                                                }
                   15966:                                                sio[c].modem_stat |= 0x02;
                   15967:                                        }
                   15968:                                }
                   15969:                                if((sio[c].prev_modem_stat & 0x10) != (sio[c].modem_stat & 0x10)) {
                   15970:                                        if(!(sio[c].modem_stat & 0x01)) {
                   15971:                                                if(sio[c].irq_enable & 0x08) {
                   15972:                                                        state_changed = true;
                   15973:                                                }
                   15974:                                                sio[c].modem_stat |= 0x01;
                   15975:                                        }
                   15976:                                }
                   15977:                                LeaveCriticalSection(&sio_mt[c].csModemStat);
                   15978:                                
                   15979:                                if(state_changed) {
                   15980:                                        sio_update_irq(c);
                   15981:                                }
1.1.1.25  root     15982:                        }
                   15983:                }
                   15984:                sio[c].modem_ctrl = data;
                   15985:                break;
                   15986:        case 7:
                   15987:                sio[c].scratch = data;
                   15988:                break;
                   15989:        }
                   15990: }
                   15991: 
                   15992: UINT8 sio_read(int c, UINT32 addr)
                   15993: {
                   15994:        switch(addr & 7) {
                   15995:        case 0:
                   15996:                if(sio[c].selector & 0x80) {
                   15997:                        return(sio[c].divisor.b.l);
                   15998:                } else {
                   15999:                        EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     16000:                        UINT8 data = 0;
                   16001:                        if(sio[c].recv_buffer != NULL) {
                   16002:                                data = sio[c].recv_buffer->read();
                   16003:                        }
1.1.1.25  root     16004:                        // data is not ready
                   16005:                        sio[c].line_stat_buf &= ~0x01;
                   16006:                        LeaveCriticalSection(&sio_mt[c].csRecvData);
                   16007:                        
                   16008:                        if(sio[c].irq_enable & 0x01) {
                   16009:                                sio_update_irq(c);
                   16010:                        }
                   16011:                        return(data);
                   16012:                }
                   16013:        case 1:
                   16014:                if(sio[c].selector & 0x80) {
                   16015:                        return(sio[c].divisor.b.h);
                   16016:                } else {
                   16017:                        return(sio[c].irq_enable);
                   16018:                }
                   16019:        case 2:
                   16020:                return(sio[c].irq_identify);
                   16021:        case 3:
                   16022:                return(sio[c].selector);
                   16023:        case 4:
                   16024:                return(sio[c].modem_ctrl);
                   16025:        case 5:
                   16026:                {
                   16027:                        EnterCriticalSection(&sio_mt[c].csLineStat);
                   16028:                        UINT8 val = sio[c].line_stat_err | sio[c].line_stat_buf;
                   16029:                        sio[c].line_stat_err = 0x00;
                   16030:                        LeaveCriticalSection(&sio_mt[c].csLineStat);
                   16031:                        
                   16032:                        bool state_changed = false;
                   16033:                        
                   16034:                        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   16035:                                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     16036:                                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     16037:                                        // transmitter holding register will be empty first
                   16038:                                        if(sio[c].irq_enable & 0x02) {
                   16039:                                                state_changed = true;
                   16040:                                        }
                   16041:                                        sio[c].line_stat_buf |= 0x20;
                   16042:                                }
                   16043:                                LeaveCriticalSection(&sio_mt[c].csSendData);
                   16044:                        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   16045:                                // transmitter shift register will be empty later
                   16046:                                sio[c].line_stat_buf |= 0x40;
                   16047:                        }
                   16048:                        if(!(sio[c].line_stat_buf & 0x01)) {
                   16049:                                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     16050:                                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     16051:                                        // data is ready
                   16052:                                        if(sio[c].irq_enable & 0x01) {
                   16053:                                                state_changed = true;
                   16054:                                        }
                   16055:                                        sio[c].line_stat_buf |= 0x01;
                   16056:                                }
                   16057:                                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   16058:                        }
                   16059:                        if(state_changed) {
                   16060:                                sio_update_irq(c);
                   16061:                        }
                   16062:                        return(val);
                   16063:                }
                   16064:        case 6:
                   16065:                {
                   16066:                        EnterCriticalSection(&sio_mt[c].csModemStat);
                   16067:                        UINT8 val = sio[c].modem_stat;
                   16068:                        sio[c].modem_stat &= 0xf0;
                   16069:                        sio[c].prev_modem_stat = sio[c].modem_stat;
                   16070:                        LeaveCriticalSection(&sio_mt[c].csModemStat);
                   16071:                        
                   16072:                        if(sio[c].modem_ctrl & 0x10) {
                   16073:                                // loop-back
                   16074:                                val &= 0x0f;
                   16075:                                val |= (sio[c].modem_ctrl & 0x0c) << 4;
                   16076:                                val |= (sio[c].modem_ctrl & 0x01) << 5;
                   16077:                                val |= (sio[c].modem_ctrl & 0x02) << 3;
                   16078:                        }
                   16079:                        return(val);
                   16080:                }
                   16081:        case 7:
                   16082:                return(sio[c].scratch);
                   16083:        }
                   16084:        return(0xff);
                   16085: }
                   16086: 
                   16087: void sio_update(int c)
                   16088: {
                   16089:        if((sio[c].line_stat_buf & 0x60) == 0x00) {
                   16090:                EnterCriticalSection(&sio_mt[c].csSendData);
1.1.1.32  root     16091:                if(sio[c].send_buffer != NULL && !sio[c].send_buffer->full()) {
1.1.1.25  root     16092:                        // transmitter holding/shift registers will be empty
                   16093:                        sio[c].line_stat_buf |= 0x60;
                   16094:                }
                   16095:                LeaveCriticalSection(&sio_mt[c].csSendData);
                   16096:        } else if((sio[c].line_stat_buf & 0x60) == 0x20) {
                   16097:                // transmitter shift register will be empty
                   16098:                sio[c].line_stat_buf |= 0x40;
                   16099:        }
                   16100:        if(!(sio[c].line_stat_buf & 0x01)) {
                   16101:                EnterCriticalSection(&sio_mt[c].csRecvData);
1.1.1.32  root     16102:                if(sio[c].recv_buffer != NULL && !sio[c].recv_buffer->empty()) {
1.1.1.25  root     16103:                        // data is ready
                   16104:                        sio[c].line_stat_buf |= 0x01;
                   16105:                }
                   16106:                LeaveCriticalSection(&sio_mt[c].csRecvData);
                   16107:        }
                   16108:        sio_update_irq(c);
                   16109: }
                   16110: 
                   16111: void sio_update_irq(int c)
                   16112: {
                   16113:        int level = -1;
                   16114:        
                   16115:        if(sio[c].irq_enable & 0x08) {
                   16116:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   16117:                if((sio[c].modem_stat & 0x0f) != 0) {
                   16118:                        level = 0;
                   16119:                }
                   16120:                EnterCriticalSection(&sio_mt[c].csModemStat);
                   16121:        }
                   16122:        if(sio[c].irq_enable & 0x02) {
                   16123:                if(sio[c].line_stat_buf & 0x20) {
                   16124:                        level = 1;
                   16125:                }
                   16126:        }
                   16127:        if(sio[c].irq_enable & 0x01) {
                   16128:                if(sio[c].line_stat_buf & 0x01) {
                   16129:                        level = 2;
                   16130:                }
                   16131:        }
                   16132:        if(sio[c].irq_enable & 0x04) {
                   16133:                EnterCriticalSection(&sio_mt[c].csLineStat);
                   16134:                if(sio[c].line_stat_err != 0) {
                   16135:                        level = 3;
                   16136:                }
                   16137:                LeaveCriticalSection(&sio_mt[c].csLineStat);
                   16138:        }
1.1.1.29  root     16139:        
                   16140:        // COM1 and COM3 shares IRQ4, COM2 and COM4 shares IRQ3
1.1.1.25  root     16141:        if(level != -1) {
                   16142:                sio[c].irq_identify = level << 1;
1.1.1.29  root     16143:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 1);
1.1.1.25  root     16144:        } else {
                   16145:                sio[c].irq_identify = 1;
1.1.1.29  root     16146:                pic_req(0, (c == 0 || c == 2) ? 4 : 3, 0);
1.1.1.25  root     16147:        }
                   16148: }
                   16149: 
                   16150: DWORD WINAPI sio_thread(void *lpx)
                   16151: {
                   16152:        volatile sio_t *p = (sio_t *)lpx;
                   16153:        sio_mt_t *q = &sio_mt[p->channel];
                   16154:        
                   16155:        char name[] = "COM1";
1.1.1.26  root     16156:        name[3] = '0' + sio_port_number[p->channel];
                   16157:        HANDLE hComm = NULL;
                   16158:        COMMPROP commProp;
                   16159:        DCB dcb;
                   16160:        DWORD dwSettableBaud = 0xffb; // 75, 110, 150, 300, 600, 1200, 1800, 2400, 4800, 7200, and 9600bps
                   16161:        BYTE bytBuffer[SIO_BUFFER_SIZE];
                   16162:        
                   16163:        if((hComm = CreateFile(name, GENERIC_READ | GENERIC_WRITE, 0, NULL, OPEN_EXISTING, 0, NULL)) != INVALID_HANDLE_VALUE) {
                   16164:                if(GetCommProperties(hComm, &commProp)) {
                   16165:                        dwSettableBaud = commProp.dwSettableBaud;
                   16166:                }
1.1.1.25  root     16167:                EscapeCommFunction(hComm, CLRBREAK);
1.1.1.26  root     16168: //             EscapeCommFunction(hComm, SETRTS);
                   16169: //             EscapeCommFunction(hComm, SETDTR);
1.1.1.25  root     16170:                
                   16171:                while(!m_halted) {
                   16172:                        // setup comm port
                   16173:                        bool comm_state_changed = false;
                   16174:                        
                   16175:                        EnterCriticalSection(&q->csLineCtrl);
                   16176:                        if((p->prev_divisor != p->divisor.w || p->prev_line_ctrl != p->line_ctrl) && p->divisor.w != 0) {
                   16177:                                p->prev_divisor = p->divisor.w;
                   16178:                                p->prev_line_ctrl = p->line_ctrl;
                   16179:                                comm_state_changed = true;
                   16180:                        }
                   16181:                        LeaveCriticalSection(&q->csLineCtrl);
                   16182:                        
                   16183:                        if(comm_state_changed) {
1.1.1.26  root     16184:                                if(GetCommState(hComm, &dcb)) {
                   16185: //                                     dcb.BaudRate = min(9600, 115200 / p->prev_divisor);
                   16186:                                        DWORD baud = 115200 / p->prev_divisor;
                   16187:                                        dcb.BaudRate = 9600; // default
                   16188:                                        
                   16189:                                        if((dwSettableBaud & BAUD_075  ) && baud >= 75   ) dcb.BaudRate = 75;
                   16190:                                        if((dwSettableBaud & BAUD_110  ) && baud >= 110  ) dcb.BaudRate = 110;
                   16191:                                        // 134.5bps is not supported ???
                   16192: //                                     if((dwSettableBaud & BAUD_134_5) && baud >= 134.5) dcb.BaudRate = 134.5;
                   16193:                                        if((dwSettableBaud & BAUD_150  ) && baud >= 150  ) dcb.BaudRate = 150;
                   16194:                                        if((dwSettableBaud & BAUD_300  ) && baud >= 300  ) dcb.BaudRate = 300;
                   16195:                                        if((dwSettableBaud & BAUD_600  ) && baud >= 600  ) dcb.BaudRate = 600;
                   16196:                                        if((dwSettableBaud & BAUD_1200 ) && baud >= 1200 ) dcb.BaudRate = 1200;
                   16197:                                        if((dwSettableBaud & BAUD_1800 ) && baud >= 1800 ) dcb.BaudRate = 1800;
                   16198:                                        if((dwSettableBaud & BAUD_2400 ) && baud >= 2400 ) dcb.BaudRate = 2400;
                   16199:                                        if((dwSettableBaud & BAUD_4800 ) && baud >= 4800 ) dcb.BaudRate = 4800;
                   16200:                                        if((dwSettableBaud & BAUD_7200 ) && baud >= 7200 ) dcb.BaudRate = 7200;
                   16201:                                        if((dwSettableBaud & BAUD_9600 ) && baud >= 9600 ) dcb.BaudRate = 9600;
                   16202: //                                     if((dwSettableBaud & BAUD_14400) && baud >= 14400) dcb.BaudRate = 14400;
                   16203: //                                     if((dwSettableBaud & BAUD_19200) && baud >= 19200) dcb.BaudRate = 19200;
                   16204: //                                     if((dwSettableBaud & BAUD_38400) && baud >= 38400) dcb.BaudRate = 38400;
                   16205:                                        
                   16206:                                        switch(p->prev_line_ctrl & 0x03) {
                   16207:                                        case 0x00: dcb.ByteSize = 5; break;
                   16208:                                        case 0x01: dcb.ByteSize = 6; break;
                   16209:                                        case 0x02: dcb.ByteSize = 7; break;
                   16210:                                        case 0x03: dcb.ByteSize = 8; break;
                   16211:                                        }
                   16212:                                        switch(p->prev_line_ctrl & 0x04) {
                   16213:                                        case 0x00: dcb.StopBits = ONESTOPBIT; break;
                   16214:                                        case 0x04: dcb.StopBits = (dcb.ByteSize == 5) ? ONE5STOPBITS : TWOSTOPBITS; break;
                   16215:                                        }
                   16216:                                        switch(p->prev_line_ctrl & 0x38) {
                   16217:                                        case 0x08: dcb.Parity = ODDPARITY;   break;
                   16218:                                        case 0x18: dcb.Parity = EVENPARITY;  break;
                   16219:                                        case 0x28: dcb.Parity = MARKPARITY;  break;
                   16220:                                        case 0x38: dcb.Parity = SPACEPARITY; break;
                   16221:                                        default:   dcb.Parity = NOPARITY;    break;
                   16222:                                        }
                   16223:                                        dcb.fBinary = TRUE;
                   16224:                                        dcb.fParity = (dcb.Parity != NOPARITY);
                   16225:                                        dcb.fOutxCtsFlow = dcb.fOutxDsrFlow = TRUE;
                   16226:                                        dcb.fDtrControl = DTR_CONTROL_HANDSHAKE;
                   16227:                                        dcb.fDsrSensitivity = FALSE;//TRUE;
                   16228:                                        dcb.fTXContinueOnXoff = TRUE;
                   16229:                                        dcb.fOutX = dcb.fInX = FALSE;
                   16230:                                        dcb.fErrorChar = FALSE;
                   16231:                                        dcb.fNull = FALSE;
                   16232:                                        dcb.fRtsControl = RTS_CONTROL_HANDSHAKE;
                   16233:                                        dcb.fAbortOnError = FALSE;
                   16234:                                        
                   16235:                                        SetCommState(hComm, &dcb);
1.1.1.25  root     16236:                                }
                   16237:                                
                   16238:                                // check again to apply all comm state changes
                   16239:                                Sleep(10);
                   16240:                                continue;
                   16241:                        }
                   16242:                        
                   16243:                        // set comm pins
                   16244:                        bool change_brk = false;
1.1.1.26  root     16245: //                     bool change_rts = false;
                   16246: //                     bool change_dtr = false;
1.1.1.25  root     16247:                        
                   16248:                        EnterCriticalSection(&q->csModemCtrl);
                   16249:                        if(p->prev_set_brk != p->set_brk) {
                   16250:                                p->prev_set_brk = p->set_brk;
                   16251:                                change_brk = true;
                   16252:                        }
1.1.1.26  root     16253: //                     if(p->prev_set_rts != p->set_rts) {
                   16254: //                             p->prev_set_rts = p->set_rts;
                   16255: //                             change_rts = true;
                   16256: //                     }
                   16257: //                     if(p->prev_set_dtr != p->set_dtr) {
                   16258: //                             p->prev_set_dtr = p->set_dtr;
                   16259: //                             change_dtr = true;
                   16260: //                     }
1.1.1.25  root     16261:                        LeaveCriticalSection(&q->csModemCtrl);
                   16262:                        
                   16263:                        if(change_brk) {
1.1.1.26  root     16264:                                static UINT32 clear_time = 0;
                   16265:                                if(p->prev_set_brk) {
                   16266:                                        EscapeCommFunction(hComm, SETBREAK);
                   16267:                                        clear_time = timeGetTime() + 200;
                   16268:                                } else {
                   16269:                                        // keep break for at least 200msec
                   16270:                                        UINT32 cur_time = timeGetTime();
                   16271:                                        if(clear_time > cur_time) {
                   16272:                                                Sleep(clear_time - cur_time);
                   16273:                                        }
                   16274:                                        EscapeCommFunction(hComm, CLRBREAK);
                   16275:                                }
1.1.1.25  root     16276:                        }
1.1.1.26  root     16277: //                     if(change_rts) {
                   16278: //                             if(p->prev_set_rts) {
                   16279: //                                     EscapeCommFunction(hComm, SETRTS);
                   16280: //                             } else {
                   16281: //                                     EscapeCommFunction(hComm, CLRRTS);
                   16282: //                             }
                   16283: //                     }
                   16284: //                     if(change_dtr) {
                   16285: //                             if(p->prev_set_dtr) {
                   16286: //                                     EscapeCommFunction(hComm, SETDTR);
                   16287: //                             } else {
                   16288: //                                     EscapeCommFunction(hComm, CLRDTR);
                   16289: //                             }
                   16290: //                     }
1.1.1.25  root     16291:                        
                   16292:                        // get comm pins
                   16293:                        DWORD dwModemStat = 0;
                   16294:                        
                   16295:                        if(GetCommModemStatus(hComm, &dwModemStat)) {
                   16296:                                EnterCriticalSection(&q->csModemStat);
                   16297:                                if(dwModemStat & MS_RLSD_ON) {
                   16298:                                        p->modem_stat |= 0x80;
                   16299:                                } else {
                   16300:                                        p->modem_stat &= ~0x80;
                   16301:                                }
                   16302:                                if(dwModemStat & MS_RING_ON) {
                   16303:                                        p->modem_stat |= 0x40;
                   16304:                                } else {
                   16305:                                        p->modem_stat &= ~0x40;
                   16306:                                }
1.1.1.26  root     16307: //                             if(dwModemStat & MS_DSR_ON) {
                   16308: //                                     p->modem_stat |= 0x20;
                   16309: //                             } else {
                   16310: //                                     p->modem_stat &= ~0x20;
                   16311: //                             }
                   16312: //                             if(dwModemStat & MS_CTS_ON) {
                   16313: //                                     p->modem_stat |= 0x10;
                   16314: //                             } else {
                   16315: //                                     p->modem_stat &= ~0x10;
                   16316: //                             }
1.1.1.25  root     16317:                                if((p->prev_modem_stat & 0x80) != (p->modem_stat & 0x80)) {
                   16318:                                        p->modem_stat |= 0x08;
                   16319:                                }
                   16320:                                if((p->prev_modem_stat & 0x40) && !(p->modem_stat & 0x40)) {
                   16321:                                        p->modem_stat |= 0x04;
                   16322:                                }
1.1.1.26  root     16323: //                             if((p->prev_modem_stat & 0x20) != (p->modem_stat & 0x20)) {
                   16324: //                                     p->modem_stat |= 0x02;
                   16325: //                             }
                   16326: //                             if((p->prev_modem_stat & 0x10) != (p->modem_stat & 0x10)) {
                   16327: //                                     p->modem_stat |= 0x01;
                   16328: //                             }
1.1.1.25  root     16329:                                LeaveCriticalSection(&q->csModemStat);
                   16330:                        }
                   16331:                        
                   16332:                        // send data
                   16333:                        DWORD dwSend = 0;
                   16334:                        
                   16335:                        EnterCriticalSection(&q->csSendData);
1.1.1.32  root     16336:                        while(p->send_buffer != NULL && !p->send_buffer->empty()) {
1.1.1.25  root     16337:                                bytBuffer[dwSend++] = p->send_buffer->read();
                   16338:                        }
                   16339:                        LeaveCriticalSection(&q->csSendData);
                   16340:                        
                   16341:                        if(dwSend != 0) {
                   16342:                                DWORD dwWritten = 0;
                   16343:                                WriteFile(hComm, bytBuffer, dwSend, &dwWritten, NULL);
                   16344:                        }
                   16345:                        
                   16346:                        // get line status and recv data
                   16347:                        DWORD dwLineStat = 0;
                   16348:                        COMSTAT comStat;
                   16349:                        
                   16350:                        if(ClearCommError(hComm, &dwLineStat, &comStat)) {
                   16351:                                EnterCriticalSection(&q->csLineStat);
                   16352:                                if(dwLineStat & CE_BREAK) {
                   16353:                                        p->line_stat_err |= 0x10;
                   16354:                                }
                   16355:                                if(dwLineStat & CE_FRAME) {
                   16356:                                        p->line_stat_err |= 0x08;
                   16357:                                }
                   16358:                                if(dwLineStat & CE_RXPARITY) {
                   16359:                                        p->line_stat_err |= 0x04;
                   16360:                                }
                   16361:                                if(dwLineStat & CE_OVERRUN) {
                   16362:                                        p->line_stat_err |= 0x02;
                   16363:                                }
                   16364:                                LeaveCriticalSection(&q->csLineStat);
                   16365:                                
                   16366:                                if(comStat.cbInQue != 0) {
                   16367:                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     16368:                                        DWORD dwRecv = 0;
                   16369:                                        if(p->recv_buffer != NULL) {
                   16370:                                                dwRecv = min(comStat.cbInQue, p->recv_buffer->remain());
                   16371:                                        }
1.1.1.25  root     16372:                                        LeaveCriticalSection(&q->csRecvData);
                   16373:                                        
                   16374:                                        if(dwRecv != 0) {
                   16375:                                                DWORD dwRead = 0;
                   16376:                                                if(ReadFile(hComm, bytBuffer, dwRecv, &dwRead, NULL) && dwRead != 0) {
                   16377:                                                        EnterCriticalSection(&q->csRecvData);
1.1.1.32  root     16378:                                                        if(p->recv_buffer != NULL) {
                   16379:                                                                for(int i = 0; i < dwRead; i++) {
                   16380:                                                                        p->recv_buffer->write(bytBuffer[i]);
                   16381:                                                                }
1.1.1.25  root     16382:                                                        }
                   16383:                                                        LeaveCriticalSection(&q->csRecvData);
                   16384:                                                }
                   16385:                                        }
                   16386:                                }
                   16387:                        }
                   16388:                        Sleep(10);
                   16389:                }
                   16390:                CloseHandle(hComm);
                   16391:        }
                   16392:        return 0;
                   16393: }
                   16394: 
1.1.1.8   root     16395: // cmos
                   16396: 
                   16397: void cmos_init()
                   16398: {
                   16399:        memset(cmos, 0, sizeof(cmos));
                   16400:        cmos_addr = 0;
1.1       root     16401:        
1.1.1.8   root     16402:        // from DOSBox
                   16403:        cmos_write(0x0a, 0x26);
                   16404:        cmos_write(0x0b, 0x02);
                   16405:        cmos_write(0x0d, 0x80);
1.1       root     16406: }
                   16407: 
1.1.1.8   root     16408: void cmos_write(int addr, UINT8 val)
1.1       root     16409: {
1.1.1.8   root     16410:        cmos[addr & 0x7f] = val;
                   16411: }
                   16412: 
                   16413: #define CMOS_GET_TIME() { \
                   16414:        UINT32 cur_sec = timeGetTime() / 1000 ; \
                   16415:        if(prev_sec != cur_sec) { \
                   16416:                GetLocalTime(&time); \
                   16417:                prev_sec = cur_sec; \
                   16418:        } \
1.1       root     16419: }
1.1.1.8   root     16420: #define CMOS_BCD(v) ((cmos[0x0b] & 4) ? (v) : to_bcd(v))
1.1       root     16421: 
1.1.1.8   root     16422: UINT8 cmos_read(int addr)
1.1       root     16423: {
1.1.1.8   root     16424:        static SYSTEMTIME time;
                   16425:        static UINT32 prev_sec = 0;
1.1       root     16426:        
1.1.1.8   root     16427:        switch(addr & 0x7f) {
                   16428:        case 0x00: CMOS_GET_TIME(); return(CMOS_BCD(time.wSecond));
                   16429:        case 0x02: CMOS_GET_TIME(); return(CMOS_BCD(time.wMinute));
                   16430:        case 0x04: CMOS_GET_TIME(); return(CMOS_BCD(time.wHour));
                   16431:        case 0x06: CMOS_GET_TIME(); return(time.wDayOfWeek + 1);
                   16432:        case 0x07: CMOS_GET_TIME(); return(CMOS_BCD(time.wDay));
                   16433:        case 0x08: CMOS_GET_TIME(); return(CMOS_BCD(time.wMonth));
                   16434:        case 0x09: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear));
                   16435: //     case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 2 ? 0x80 : 0));       // 2msec
                   16436:        case 0x0a: return((cmos[0x0a] & 0x7f) | ((timeGetTime() % 1000) < 20 ? 0x80 : 0));      // precision of timeGetTime() may not be 1msec
                   16437:        case 0x15: return((MEMORY_END >> 10) & 0xff);
                   16438:        case 0x16: return((MEMORY_END >> 18) & 0xff);
                   16439:        case 0x17: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   16440:        case 0x18: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   16441:        case 0x30: return(((MAX_MEM - 0x100000) >> 10) & 0xff);
                   16442:        case 0x31: return(((MAX_MEM - 0x100000) >> 18) & 0xff);
                   16443:        case 0x32: CMOS_GET_TIME(); return(CMOS_BCD(time.wYear / 100));
1.1       root     16444:        }
1.1.1.8   root     16445:        return(cmos[addr & 0x7f]);
1.1       root     16446: }
                   16447: 
1.1.1.7   root     16448: // kbd (a20)
                   16449: 
                   16450: void kbd_init()
                   16451: {
1.1.1.8   root     16452:        kbd_data = kbd_command = 0;
1.1.1.7   root     16453:        kbd_status = 0x18;
                   16454: }
                   16455: 
                   16456: UINT8 kbd_read_data()
                   16457: {
1.1.1.8   root     16458:        kbd_status &= ~1;
1.1.1.7   root     16459:        return(kbd_data);
                   16460: }
                   16461: 
                   16462: void kbd_write_data(UINT8 val)
                   16463: {
                   16464:        switch(kbd_command) {
                   16465:        case 0xd1:
                   16466:                i386_set_a20_line((val >> 1) & 1);
                   16467:                break;
                   16468:        }
                   16469:        kbd_command = 0;
1.1.1.8   root     16470:        kbd_status &= ~8;
1.1.1.7   root     16471: }
                   16472: 
                   16473: UINT8 kbd_read_status()
                   16474: {
                   16475:        return(kbd_status);
                   16476: }
                   16477: 
                   16478: void kbd_write_command(UINT8 val)
                   16479: {
                   16480:        switch(val) {
                   16481:        case 0xd0:
                   16482:                kbd_data = ((m_a20_mask >> 19) & 2) | 1;
1.1.1.8   root     16483:                kbd_status |= 1;
1.1.1.7   root     16484:                break;
                   16485:        case 0xdd:
                   16486:                i386_set_a20_line(0);
                   16487:                break;
                   16488:        case 0xdf:
                   16489:                i386_set_a20_line(1);
                   16490:                break;
1.1.1.26  root     16491:        case 0xf0: case 0xf1: case 0xf2: case 0xf3: case 0xf4: case 0xf5: case 0xf6: case 0xf7:
                   16492:        case 0xf8: case 0xf9: case 0xfa: case 0xfb: case 0xfc: case 0xfd: case 0xfe: case 0xff:
1.1.1.7   root     16493:                if(!(val & 1)) {
1.1.1.8   root     16494:                        if((cmos[0x0f] & 0x7f) == 5) {
1.1.1.7   root     16495:                                // reset pic
                   16496:                                pic_init();
                   16497:                                pic[0].irr = pic[1].irr = 0x00;
                   16498:                                pic[0].imr = pic[1].imr = 0xff;
                   16499:                        }
                   16500:                        CPU_RESET_CALL(CPU_MODEL);
                   16501:                        i386_jmp_far(0x40, 0x67);
                   16502:                }
                   16503:                i386_set_a20_line((val >> 1) & 1);
                   16504:                break;
                   16505:        }
                   16506:        kbd_command = val;
1.1.1.8   root     16507:        kbd_status |= 8;
1.1.1.7   root     16508: }
                   16509: 
1.1.1.9   root     16510: // vga
                   16511: 
                   16512: UINT8 vga_read_status()
                   16513: {
                   16514:        // 60hz
                   16515:        static const int period[3] = {16, 17, 17};
                   16516:        static int index = 0;
                   16517:        UINT32 time = timeGetTime() % period[index];
                   16518:        
                   16519:        index = (index + 1) % 3;
1.1.1.14  root     16520:        return((time < 4 ? 0x08 : 0) | (time == 0 ? 0 : 0x01));
1.1.1.9   root     16521: }
                   16522: 
1.1       root     16523: // i/o bus
                   16524: 
1.1.1.29  root     16525: // this is ugly patch for SW1US.EXE, it sometimes mistakely read/write 01h-10h for serial I/O
                   16526: //#define SW1US_PATCH
                   16527: 
1.1.1.25  root     16528: UINT8 read_io_byte(offs_t addr)
1.1.1.33  root     16529: #ifdef USE_DEBUGGER
1.1.1.25  root     16530: {
1.1.1.33  root     16531:        if(now_debugging) {
                   16532:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   16533:                        if(in_break_point.table[i].status == 1) {
                   16534:                                if(addr == in_break_point.table[i].addr) {
                   16535:                                        in_break_point.hit = i + 1;
                   16536:                                        now_suspended = true;
                   16537:                                        break;
                   16538:                                }
                   16539:                        }
                   16540:                }
1.1.1.25  root     16541:        }
1.1.1.33  root     16542:        return(debugger_read_io_byte(addr));
1.1.1.25  root     16543: }
1.1.1.33  root     16544: UINT8 debugger_read_io_byte(offs_t addr)
1.1.1.25  root     16545: #endif
1.1       root     16546: {
1.1.1.33  root     16547:        UINT8 val = 0xff;
                   16548:        
1.1       root     16549:        switch(addr) {
1.1.1.29  root     16550: #ifdef SW1US_PATCH
                   16551:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   16552:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
1.1.1.33  root     16553:                val = sio_read(0, addr - 1);
                   16554:                break;
1.1.1.29  root     16555: #else
1.1.1.25  root     16556:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   16557:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
1.1.1.33  root     16558:                val = dma_read(0, addr);
                   16559:                break;
1.1.1.29  root     16560: #endif
1.1.1.25  root     16561:        case 0x20: case 0x21:
1.1.1.33  root     16562:                val = pic_read(0, addr);
                   16563:                break;
1.1.1.25  root     16564:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1.1.33  root     16565:                val = pit_read(addr & 0x03);
                   16566:                break;
1.1.1.7   root     16567:        case 0x60:
1.1.1.33  root     16568:                val = kbd_read_data();
                   16569:                break;
1.1.1.9   root     16570:        case 0x61:
1.1.1.33  root     16571:                val = system_port;
                   16572:                break;
1.1.1.7   root     16573:        case 0x64:
1.1.1.33  root     16574:                val = kbd_read_status();
                   16575:                break;
1.1       root     16576:        case 0x71:
1.1.1.33  root     16577:                val = cmos_read(cmos_addr);
                   16578:                break;
1.1.1.25  root     16579:        case 0x81:
1.1.1.33  root     16580:                val = dma_page_read(0, 2);
                   16581:                break;
1.1.1.25  root     16582:        case 0x82:
1.1.1.33  root     16583:                val = dma_page_read(0, 3);
                   16584:                break;
1.1.1.25  root     16585:        case 0x83:
1.1.1.33  root     16586:                val = dma_page_read(0, 1);
                   16587:                break;
1.1.1.25  root     16588:        case 0x87:
1.1.1.33  root     16589:                val = dma_page_read(0, 0);
                   16590:                break;
1.1.1.25  root     16591:        case 0x89:
1.1.1.33  root     16592:                val = dma_page_read(1, 2);
                   16593:                break;
1.1.1.25  root     16594:        case 0x8a:
1.1.1.33  root     16595:                val = dma_page_read(1, 3);
                   16596:                break;
1.1.1.25  root     16597:        case 0x8b:
1.1.1.33  root     16598:                val = dma_page_read(1, 1);
                   16599:                break;
1.1.1.25  root     16600:        case 0x8f:
1.1.1.33  root     16601:                val = dma_page_read(1, 0);
                   16602:                break;
1.1       root     16603:        case 0x92:
1.1.1.33  root     16604:                val = (m_a20_mask >> 19) & 2;
                   16605:                break;
1.1.1.25  root     16606:        case 0xa0: case 0xa1:
1.1.1.33  root     16607:                val = pic_read(1, addr);
                   16608:                break;
1.1.1.25  root     16609:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   16610:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.33  root     16611:                val = dma_read(1, (addr - 0xc0) >> 1);
                   16612:                break;
1.1.1.26  root     16613: //     case 0x278: case 0x279: case 0x27a:
1.1.1.33  root     16614: //             val = pio_read(1, addr);
                   16615: //             break;
1.1.1.29  root     16616:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
1.1.1.33  root     16617:                val = sio_read(3, addr);
                   16618:                break;
1.1.1.25  root     16619:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
1.1.1.33  root     16620:                val = sio_read(1, addr);
                   16621:                break;
1.1.1.25  root     16622:        case 0x378: case 0x379: case 0x37a:
1.1.1.33  root     16623:                val = pio_read(0, addr);
                   16624:                break;
1.1.1.25  root     16625:        case 0x3ba: case 0x3da:
1.1.1.33  root     16626:                val = vga_read_status();
                   16627:                break;
1.1.1.29  root     16628:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
1.1.1.33  root     16629:                val = sio_read(2, addr);
                   16630:                break;
1.1.1.25  root     16631:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
1.1.1.33  root     16632:                val = sio_read(0, addr);
                   16633:                break;
1.1       root     16634:        default:
1.1.1.33  root     16635: //             fatalerror("unknown inb %4x\n", addr);
1.1       root     16636:                break;
                   16637:        }
1.1.1.33  root     16638: #ifdef ENABLE_DEBUG_IOPORT
                   16639:        if(fp_debug_log != NULL) {
                   16640:                fprintf(fp_debug_log, "inb %04X, %02X\n", addr, val);
                   16641:        }
                   16642: #endif
                   16643:        return(val);
1.1       root     16644: }
                   16645: 
                   16646: UINT16 read_io_word(offs_t addr)
                   16647: {
                   16648:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8));
                   16649: }
                   16650: 
1.1.1.33  root     16651: #ifdef USE_DEBUGGER
                   16652: UINT16 debugger_read_io_word(offs_t addr)
                   16653: {
                   16654:        return(debugger_read_io_byte(addr) | (debugger_read_io_byte(addr + 1) << 8));
                   16655: }
                   16656: #endif
                   16657: 
1.1       root     16658: UINT32 read_io_dword(offs_t addr)
                   16659: {
                   16660:        return(read_io_byte(addr) | (read_io_byte(addr + 1) << 8) | (read_io_byte(addr + 2) << 16) | (read_io_byte(addr + 3) << 24));
                   16661: }
                   16662: 
1.1.1.33  root     16663: #ifdef USE_DEBUGGER
                   16664: UINT32 debugger_read_io_dword(offs_t addr)
                   16665: {
                   16666:        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));
                   16667: }
                   16668: #endif
                   16669: 
1.1       root     16670: void write_io_byte(offs_t addr, UINT8 val)
1.1.1.33  root     16671: #ifdef USE_DEBUGGER
                   16672: {
                   16673:        if(now_debugging) {
                   16674:                for(int i = 0; i < MAX_BREAK_POINTS; i++) {
                   16675:                        if(out_break_point.table[i].status == 1) {
                   16676:                                if(addr == out_break_point.table[i].addr) {
                   16677:                                        out_break_point.hit = i + 1;
                   16678:                                        now_suspended = true;
                   16679:                                        break;
                   16680:                                }
                   16681:                        }
                   16682:                }
                   16683:        }
                   16684:        debugger_write_io_byte(addr, val);
                   16685: }
                   16686: void debugger_write_io_byte(offs_t addr, UINT8 val)
                   16687: #endif
1.1       root     16688: {
1.1.1.25  root     16689: #ifdef ENABLE_DEBUG_IOPORT
1.1.1.33  root     16690:        if(fp_debug_log != NULL) {
                   16691:                fprintf(fp_debug_log, "outb %04X, %02X\n", addr, val);
1.1.1.25  root     16692:        }
                   16693: #endif
1.1       root     16694:        switch(addr) {
1.1.1.29  root     16695: #ifdef SW1US_PATCH
                   16696:        case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07: case 0x08:
                   16697:        case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f: case 0x10:
                   16698:                sio_write(0, addr - 1, val);
                   16699:                break;
                   16700: #else
1.1.1.25  root     16701:        case 0x00: case 0x01: case 0x02: case 0x03: case 0x04: case 0x05: case 0x06: case 0x07:
                   16702:        case 0x08: case 0x09: case 0x0a: case 0x0b: case 0x0c: case 0x0d: case 0x0e: case 0x0f:
                   16703:                dma_write(0, addr, val);
                   16704:                break;
1.1.1.29  root     16705: #endif
1.1.1.25  root     16706:        case 0x20: case 0x21:
1.1       root     16707:                pic_write(0, addr, val);
                   16708:                break;
1.1.1.25  root     16709:        case 0x40: case 0x41: case 0x42: case 0x43:
1.1       root     16710:                pit_write(addr & 0x03, val);
                   16711:                break;
1.1.1.7   root     16712:        case 0x60:
                   16713:                kbd_write_data(val);
                   16714:                break;
1.1.1.9   root     16715:        case 0x61:
                   16716:                if((system_port & 3) != 3 && (val & 3) == 3) {
                   16717:                        // beep on
                   16718: //                     MessageBeep(-1);
                   16719:                } else if((system_port & 3) == 3 && (val & 3) != 3) {
                   16720:                        // beep off
                   16721:                }
                   16722:                system_port = val;
                   16723:                break;
1.1       root     16724:        case 0x64:
1.1.1.7   root     16725:                kbd_write_command(val);
1.1       root     16726:                break;
                   16727:        case 0x70:
                   16728:                cmos_addr = val;
                   16729:                break;
                   16730:        case 0x71:
1.1.1.8   root     16731:                cmos_write(cmos_addr, val);
1.1       root     16732:                break;
1.1.1.25  root     16733:        case 0x81:
                   16734:                dma_page_write(0, 2, val);
                   16735:        case 0x82:
                   16736:                dma_page_write(0, 3, val);
                   16737:        case 0x83:
                   16738:                dma_page_write(0, 1, val);
                   16739:        case 0x87:
                   16740:                dma_page_write(0, 0, val);
                   16741:        case 0x89:
                   16742:                dma_page_write(1, 2, val);
                   16743:        case 0x8a:
                   16744:                dma_page_write(1, 3, val);
                   16745:        case 0x8b:
                   16746:                dma_page_write(1, 1, val);
                   16747:        case 0x8f:
                   16748:                dma_page_write(1, 0, val);
1.1       root     16749:        case 0x92:
1.1.1.7   root     16750:                i386_set_a20_line((val >> 1) & 1);
1.1       root     16751:                break;
1.1.1.25  root     16752:        case 0xa0: case 0xa1:
1.1       root     16753:                pic_write(1, addr, val);
                   16754:                break;
1.1.1.25  root     16755:        case 0xc0: case 0xc2: case 0xc4: case 0xc6: case 0xc8: case 0xca: case 0xcc: case 0xce:
                   16756:        case 0xd0: case 0xd2: case 0xd4: case 0xd6: case 0xd8: case 0xda: case 0xdc: case 0xde:
1.1.1.26  root     16757:                dma_write(1, (addr - 0xc0) >> 1, val);
1.1.1.25  root     16758:                break;
1.1.1.26  root     16759: //     case 0x278: case 0x279: case 0x27a:
                   16760: //             pio_write(1, addr, val);
                   16761: //             break;
1.1.1.29  root     16762:        case 0x2e8: case 0x2e9: case 0x2ea: case 0x2eb: case 0x2ec: case 0x2ed: case 0x2ee: case 0x2ef:
                   16763:                sio_write(3, addr, val);
                   16764:                break;
1.1.1.25  root     16765:        case 0x2f8: case 0x2f9: case 0x2fa: case 0x2fb: case 0x2fc: case 0x2fd: case 0x2fe: case 0x2ff:
                   16766:                sio_write(1, addr, val);
                   16767:                break;
                   16768:        case 0x378: case 0x379: case 0x37a:
                   16769:                pio_write(0, addr, val);
                   16770:                break;
1.1.1.29  root     16771:        case 0x3e8: case 0x3e9: case 0x3ea: case 0x3eb: case 0x3ec: case 0x3ed: case 0x3ee: case 0x3ef:
                   16772:                sio_write(2, addr, val);
                   16773:                break;
1.1.1.25  root     16774:        case 0x3f8: case 0x3f9: case 0x3fa: case 0x3fb: case 0x3fc: case 0x3fd: case 0x3fe: case 0x3ff:
                   16775:                sio_write(0, addr, val);
                   16776:                break;
1.1       root     16777:        default:
1.1.1.33  root     16778: //             fatalerror("unknown outb %4x,%2x\n", addr, val);
1.1       root     16779:                break;
                   16780:        }
                   16781: }
                   16782: 
                   16783: void write_io_word(offs_t addr, UINT16 val)
                   16784: {
                   16785:        write_io_byte(addr + 0, (val >> 0) & 0xff);
                   16786:        write_io_byte(addr + 1, (val >> 8) & 0xff);
                   16787: }
                   16788: 
1.1.1.33  root     16789: #ifdef USE_DEBUGGER
                   16790: void debugger_write_io_word(offs_t addr, UINT16 val)
                   16791: {
                   16792:        debugger_write_io_byte(addr + 0, (val >> 0) & 0xff);
                   16793:        debugger_write_io_byte(addr + 1, (val >> 8) & 0xff);
                   16794: }
                   16795: #endif
                   16796: 
1.1       root     16797: void write_io_dword(offs_t addr, UINT32 val)
                   16798: {
                   16799:        write_io_byte(addr + 0, (val >>  0) & 0xff);
                   16800:        write_io_byte(addr + 1, (val >>  8) & 0xff);
                   16801:        write_io_byte(addr + 2, (val >> 16) & 0xff);
                   16802:        write_io_byte(addr + 3, (val >> 24) & 0xff);
                   16803: }
1.1.1.33  root     16804: 
                   16805: #ifdef USE_DEBUGGER
                   16806: void debugger_write_io_dword(offs_t addr, UINT32 val)
                   16807: {
                   16808:        debugger_write_io_byte(addr + 0, (val >>  0) & 0xff);
                   16809:        debugger_write_io_byte(addr + 1, (val >>  8) & 0xff);
                   16810:        debugger_write_io_byte(addr + 2, (val >> 16) & 0xff);
                   16811:        debugger_write_io_byte(addr + 3, (val >> 24) & 0xff);
                   16812: }
                   16813: #endif

unix.superglobalmegacorp.com

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